gup.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. #include <linux/kernel.h>
  2. #include <linux/errno.h>
  3. #include <linux/err.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/hugetlb.h>
  6. #include <linux/mm.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/rmap.h>
  9. #include <linux/swap.h>
  10. #include <linux/swapops.h>
  11. #include "internal.h"
  12. static struct page *no_page_table(struct vm_area_struct *vma,
  13. unsigned int flags)
  14. {
  15. /*
  16. * When core dumping an enormous anonymous area that nobody
  17. * has touched so far, we don't want to allocate unnecessary pages or
  18. * page tables. Return error instead of NULL to skip handle_mm_fault,
  19. * then get_dump_page() will return NULL to leave a hole in the dump.
  20. * But we can only make this optimization where a hole would surely
  21. * be zero-filled if handle_mm_fault() actually did handle it.
  22. */
  23. if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
  24. return ERR_PTR(-EFAULT);
  25. return NULL;
  26. }
  27. static struct page *follow_page_pte(struct vm_area_struct *vma,
  28. unsigned long address, pmd_t *pmd, unsigned int flags)
  29. {
  30. struct mm_struct *mm = vma->vm_mm;
  31. struct page *page;
  32. spinlock_t *ptl;
  33. pte_t *ptep, pte;
  34. retry:
  35. if (unlikely(pmd_bad(*pmd)))
  36. return no_page_table(vma, flags);
  37. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  38. pte = *ptep;
  39. if (!pte_present(pte)) {
  40. swp_entry_t entry;
  41. /*
  42. * KSM's break_ksm() relies upon recognizing a ksm page
  43. * even while it is being migrated, so for that case we
  44. * need migration_entry_wait().
  45. */
  46. if (likely(!(flags & FOLL_MIGRATION)))
  47. goto no_page;
  48. if (pte_none(pte) || pte_file(pte))
  49. goto no_page;
  50. entry = pte_to_swp_entry(pte);
  51. if (!is_migration_entry(entry))
  52. goto no_page;
  53. pte_unmap_unlock(ptep, ptl);
  54. migration_entry_wait(mm, pmd, address);
  55. goto retry;
  56. }
  57. if ((flags & FOLL_NUMA) && pte_numa(pte))
  58. goto no_page;
  59. if ((flags & FOLL_WRITE) && !pte_write(pte)) {
  60. pte_unmap_unlock(ptep, ptl);
  61. return NULL;
  62. }
  63. page = vm_normal_page(vma, address, pte);
  64. if (unlikely(!page)) {
  65. if ((flags & FOLL_DUMP) ||
  66. !is_zero_pfn(pte_pfn(pte)))
  67. goto bad_page;
  68. page = pte_page(pte);
  69. }
  70. if (flags & FOLL_GET)
  71. get_page_foll(page);
  72. if (flags & FOLL_TOUCH) {
  73. if ((flags & FOLL_WRITE) &&
  74. !pte_dirty(pte) && !PageDirty(page))
  75. set_page_dirty(page);
  76. /*
  77. * pte_mkyoung() would be more correct here, but atomic care
  78. * is needed to avoid losing the dirty bit: it is easier to use
  79. * mark_page_accessed().
  80. */
  81. mark_page_accessed(page);
  82. }
  83. if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
  84. /*
  85. * The preliminary mapping check is mainly to avoid the
  86. * pointless overhead of lock_page on the ZERO_PAGE
  87. * which might bounce very badly if there is contention.
  88. *
  89. * If the page is already locked, we don't need to
  90. * handle it now - vmscan will handle it later if and
  91. * when it attempts to reclaim the page.
  92. */
  93. if (page->mapping && trylock_page(page)) {
  94. lru_add_drain(); /* push cached pages to LRU */
  95. /*
  96. * Because we lock page here, and migration is
  97. * blocked by the pte's page reference, and we
  98. * know the page is still mapped, we don't even
  99. * need to check for file-cache page truncation.
  100. */
  101. mlock_vma_page(page);
  102. unlock_page(page);
  103. }
  104. }
  105. pte_unmap_unlock(ptep, ptl);
  106. return page;
  107. bad_page:
  108. pte_unmap_unlock(ptep, ptl);
  109. return ERR_PTR(-EFAULT);
  110. no_page:
  111. pte_unmap_unlock(ptep, ptl);
  112. if (!pte_none(pte))
  113. return NULL;
  114. return no_page_table(vma, flags);
  115. }
  116. /**
  117. * follow_page_mask - look up a page descriptor from a user-virtual address
  118. * @vma: vm_area_struct mapping @address
  119. * @address: virtual address to look up
  120. * @flags: flags modifying lookup behaviour
  121. * @page_mask: on output, *page_mask is set according to the size of the page
  122. *
  123. * @flags can have FOLL_ flags set, defined in <linux/mm.h>
  124. *
  125. * Returns the mapped (struct page *), %NULL if no mapping exists, or
  126. * an error pointer if there is a mapping to something not represented
  127. * by a page descriptor (see also vm_normal_page()).
  128. */
  129. struct page *follow_page_mask(struct vm_area_struct *vma,
  130. unsigned long address, unsigned int flags,
  131. unsigned int *page_mask)
  132. {
  133. pgd_t *pgd;
  134. pud_t *pud;
  135. pmd_t *pmd;
  136. spinlock_t *ptl;
  137. struct page *page;
  138. struct mm_struct *mm = vma->vm_mm;
  139. *page_mask = 0;
  140. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  141. if (!IS_ERR(page)) {
  142. BUG_ON(flags & FOLL_GET);
  143. return page;
  144. }
  145. pgd = pgd_offset(mm, address);
  146. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  147. return no_page_table(vma, flags);
  148. pud = pud_offset(pgd, address);
  149. if (pud_none(*pud))
  150. return no_page_table(vma, flags);
  151. if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
  152. if (flags & FOLL_GET)
  153. return NULL;
  154. page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
  155. return page;
  156. }
  157. if (unlikely(pud_bad(*pud)))
  158. return no_page_table(vma, flags);
  159. pmd = pmd_offset(pud, address);
  160. if (pmd_none(*pmd))
  161. return no_page_table(vma, flags);
  162. if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
  163. page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
  164. if (flags & FOLL_GET) {
  165. /*
  166. * Refcount on tail pages are not well-defined and
  167. * shouldn't be taken. The caller should handle a NULL
  168. * return when trying to follow tail pages.
  169. */
  170. if (PageHead(page))
  171. get_page(page);
  172. else
  173. page = NULL;
  174. }
  175. return page;
  176. }
  177. if ((flags & FOLL_NUMA) && pmd_numa(*pmd))
  178. return no_page_table(vma, flags);
  179. if (pmd_trans_huge(*pmd)) {
  180. if (flags & FOLL_SPLIT) {
  181. split_huge_page_pmd(vma, address, pmd);
  182. return follow_page_pte(vma, address, pmd, flags);
  183. }
  184. ptl = pmd_lock(mm, pmd);
  185. if (likely(pmd_trans_huge(*pmd))) {
  186. if (unlikely(pmd_trans_splitting(*pmd))) {
  187. spin_unlock(ptl);
  188. wait_split_huge_page(vma->anon_vma, pmd);
  189. } else {
  190. page = follow_trans_huge_pmd(vma, address,
  191. pmd, flags);
  192. spin_unlock(ptl);
  193. *page_mask = HPAGE_PMD_NR - 1;
  194. return page;
  195. }
  196. } else
  197. spin_unlock(ptl);
  198. }
  199. return follow_page_pte(vma, address, pmd, flags);
  200. }
  201. static int get_gate_page(struct mm_struct *mm, unsigned long address,
  202. unsigned int gup_flags, struct vm_area_struct **vma,
  203. struct page **page)
  204. {
  205. pgd_t *pgd;
  206. pud_t *pud;
  207. pmd_t *pmd;
  208. pte_t *pte;
  209. int ret = -EFAULT;
  210. /* user gate pages are read-only */
  211. if (gup_flags & FOLL_WRITE)
  212. return -EFAULT;
  213. if (address > TASK_SIZE)
  214. pgd = pgd_offset_k(address);
  215. else
  216. pgd = pgd_offset_gate(mm, address);
  217. BUG_ON(pgd_none(*pgd));
  218. pud = pud_offset(pgd, address);
  219. BUG_ON(pud_none(*pud));
  220. pmd = pmd_offset(pud, address);
  221. if (pmd_none(*pmd))
  222. return -EFAULT;
  223. VM_BUG_ON(pmd_trans_huge(*pmd));
  224. pte = pte_offset_map(pmd, address);
  225. if (pte_none(*pte))
  226. goto unmap;
  227. *vma = get_gate_vma(mm);
  228. if (!page)
  229. goto out;
  230. *page = vm_normal_page(*vma, address, *pte);
  231. if (!*page) {
  232. if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
  233. goto unmap;
  234. *page = pte_page(*pte);
  235. }
  236. get_page(*page);
  237. out:
  238. ret = 0;
  239. unmap:
  240. pte_unmap(pte);
  241. return ret;
  242. }
  243. static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
  244. unsigned long address, unsigned int *flags, int *nonblocking)
  245. {
  246. struct mm_struct *mm = vma->vm_mm;
  247. unsigned int fault_flags = 0;
  248. int ret;
  249. /* For mlock, just skip the stack guard page. */
  250. if ((*flags & FOLL_MLOCK) &&
  251. (stack_guard_page_start(vma, address) ||
  252. stack_guard_page_end(vma, address + PAGE_SIZE)))
  253. return -ENOENT;
  254. if (*flags & FOLL_WRITE)
  255. fault_flags |= FAULT_FLAG_WRITE;
  256. if (nonblocking)
  257. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  258. if (*flags & FOLL_NOWAIT)
  259. fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
  260. ret = handle_mm_fault(mm, vma, address, fault_flags);
  261. if (ret & VM_FAULT_ERROR) {
  262. if (ret & VM_FAULT_OOM)
  263. return -ENOMEM;
  264. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  265. return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
  266. if (ret & VM_FAULT_SIGBUS)
  267. return -EFAULT;
  268. BUG();
  269. }
  270. if (tsk) {
  271. if (ret & VM_FAULT_MAJOR)
  272. tsk->maj_flt++;
  273. else
  274. tsk->min_flt++;
  275. }
  276. if (ret & VM_FAULT_RETRY) {
  277. if (nonblocking)
  278. *nonblocking = 0;
  279. return -EBUSY;
  280. }
  281. /*
  282. * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
  283. * necessary, even if maybe_mkwrite decided not to set pte_write. We
  284. * can thus safely do subsequent page lookups as if they were reads.
  285. * But only do so when looping for pte_write is futile: in some cases
  286. * userspace may also be wanting to write to the gotten user page,
  287. * which a read fault here might prevent (a readonly page might get
  288. * reCOWed by userspace write).
  289. */
  290. if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
  291. *flags &= ~FOLL_WRITE;
  292. return 0;
  293. }
  294. static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
  295. {
  296. vm_flags_t vm_flags = vma->vm_flags;
  297. if (vm_flags & (VM_IO | VM_PFNMAP))
  298. return -EFAULT;
  299. if (gup_flags & FOLL_WRITE) {
  300. if (!(vm_flags & VM_WRITE)) {
  301. if (!(gup_flags & FOLL_FORCE))
  302. return -EFAULT;
  303. /*
  304. * We used to let the write,force case do COW in a
  305. * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
  306. * set a breakpoint in a read-only mapping of an
  307. * executable, without corrupting the file (yet only
  308. * when that file had been opened for writing!).
  309. * Anon pages in shared mappings are surprising: now
  310. * just reject it.
  311. */
  312. if (!is_cow_mapping(vm_flags)) {
  313. WARN_ON_ONCE(vm_flags & VM_MAYWRITE);
  314. return -EFAULT;
  315. }
  316. }
  317. } else if (!(vm_flags & VM_READ)) {
  318. if (!(gup_flags & FOLL_FORCE))
  319. return -EFAULT;
  320. /*
  321. * Is there actually any vma we can reach here which does not
  322. * have VM_MAYREAD set?
  323. */
  324. if (!(vm_flags & VM_MAYREAD))
  325. return -EFAULT;
  326. }
  327. return 0;
  328. }
  329. /**
  330. * __get_user_pages() - pin user pages in memory
  331. * @tsk: task_struct of target task
  332. * @mm: mm_struct of target mm
  333. * @start: starting user address
  334. * @nr_pages: number of pages from start to pin
  335. * @gup_flags: flags modifying pin behaviour
  336. * @pages: array that receives pointers to the pages pinned.
  337. * Should be at least nr_pages long. Or NULL, if caller
  338. * only intends to ensure the pages are faulted in.
  339. * @vmas: array of pointers to vmas corresponding to each page.
  340. * Or NULL if the caller does not require them.
  341. * @nonblocking: whether waiting for disk IO or mmap_sem contention
  342. *
  343. * Returns number of pages pinned. This may be fewer than the number
  344. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  345. * were pinned, returns -errno. Each page returned must be released
  346. * with a put_page() call when it is finished with. vmas will only
  347. * remain valid while mmap_sem is held.
  348. *
  349. * Must be called with mmap_sem held for read or write.
  350. *
  351. * __get_user_pages walks a process's page tables and takes a reference to
  352. * each struct page that each user address corresponds to at a given
  353. * instant. That is, it takes the page that would be accessed if a user
  354. * thread accesses the given user virtual address at that instant.
  355. *
  356. * This does not guarantee that the page exists in the user mappings when
  357. * __get_user_pages returns, and there may even be a completely different
  358. * page there in some cases (eg. if mmapped pagecache has been invalidated
  359. * and subsequently re faulted). However it does guarantee that the page
  360. * won't be freed completely. And mostly callers simply care that the page
  361. * contains data that was valid *at some point in time*. Typically, an IO
  362. * or similar operation cannot guarantee anything stronger anyway because
  363. * locks can't be held over the syscall boundary.
  364. *
  365. * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
  366. * the page is written to, set_page_dirty (or set_page_dirty_lock, as
  367. * appropriate) must be called after the page is finished with, and
  368. * before put_page is called.
  369. *
  370. * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
  371. * or mmap_sem contention, and if waiting is needed to pin all pages,
  372. * *@nonblocking will be set to 0.
  373. *
  374. * In most cases, get_user_pages or get_user_pages_fast should be used
  375. * instead of __get_user_pages. __get_user_pages should be used only if
  376. * you need some special @gup_flags.
  377. */
  378. long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  379. unsigned long start, unsigned long nr_pages,
  380. unsigned int gup_flags, struct page **pages,
  381. struct vm_area_struct **vmas, int *nonblocking)
  382. {
  383. long i = 0;
  384. unsigned int page_mask;
  385. struct vm_area_struct *vma = NULL;
  386. if (!nr_pages)
  387. return 0;
  388. VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
  389. /*
  390. * If FOLL_FORCE is set then do not force a full fault as the hinting
  391. * fault information is unrelated to the reference behaviour of a task
  392. * using the address space
  393. */
  394. if (!(gup_flags & FOLL_FORCE))
  395. gup_flags |= FOLL_NUMA;
  396. do {
  397. struct page *page;
  398. unsigned int foll_flags = gup_flags;
  399. unsigned int page_increm;
  400. /* first iteration or cross vma bound */
  401. if (!vma || start >= vma->vm_end) {
  402. vma = find_extend_vma(mm, start);
  403. if (!vma && in_gate_area(mm, start)) {
  404. int ret;
  405. ret = get_gate_page(mm, start & PAGE_MASK,
  406. gup_flags, &vma,
  407. pages ? &pages[i] : NULL);
  408. if (ret)
  409. return i ? : ret;
  410. page_mask = 0;
  411. goto next_page;
  412. }
  413. if (!vma || check_vma_flags(vma, gup_flags))
  414. return i ? : -EFAULT;
  415. if (is_vm_hugetlb_page(vma)) {
  416. i = follow_hugetlb_page(mm, vma, pages, vmas,
  417. &start, &nr_pages, i,
  418. gup_flags);
  419. continue;
  420. }
  421. }
  422. retry:
  423. /*
  424. * If we have a pending SIGKILL, don't keep faulting pages and
  425. * potentially allocating memory.
  426. */
  427. if (unlikely(fatal_signal_pending(current)))
  428. return i ? i : -ERESTARTSYS;
  429. cond_resched();
  430. page = follow_page_mask(vma, start, foll_flags, &page_mask);
  431. if (!page) {
  432. int ret;
  433. ret = faultin_page(tsk, vma, start, &foll_flags,
  434. nonblocking);
  435. switch (ret) {
  436. case 0:
  437. goto retry;
  438. case -EFAULT:
  439. case -ENOMEM:
  440. case -EHWPOISON:
  441. return i ? i : ret;
  442. case -EBUSY:
  443. return i;
  444. case -ENOENT:
  445. goto next_page;
  446. }
  447. BUG();
  448. }
  449. if (IS_ERR(page))
  450. return i ? i : PTR_ERR(page);
  451. if (pages) {
  452. pages[i] = page;
  453. flush_anon_page(vma, page, start);
  454. flush_dcache_page(page);
  455. page_mask = 0;
  456. }
  457. next_page:
  458. if (vmas) {
  459. vmas[i] = vma;
  460. page_mask = 0;
  461. }
  462. page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
  463. if (page_increm > nr_pages)
  464. page_increm = nr_pages;
  465. i += page_increm;
  466. start += page_increm * PAGE_SIZE;
  467. nr_pages -= page_increm;
  468. } while (nr_pages);
  469. return i;
  470. }
  471. EXPORT_SYMBOL(__get_user_pages);
  472. /*
  473. * fixup_user_fault() - manually resolve a user page fault
  474. * @tsk: the task_struct to use for page fault accounting, or
  475. * NULL if faults are not to be recorded.
  476. * @mm: mm_struct of target mm
  477. * @address: user address
  478. * @fault_flags:flags to pass down to handle_mm_fault()
  479. *
  480. * This is meant to be called in the specific scenario where for locking reasons
  481. * we try to access user memory in atomic context (within a pagefault_disable()
  482. * section), this returns -EFAULT, and we want to resolve the user fault before
  483. * trying again.
  484. *
  485. * Typically this is meant to be used by the futex code.
  486. *
  487. * The main difference with get_user_pages() is that this function will
  488. * unconditionally call handle_mm_fault() which will in turn perform all the
  489. * necessary SW fixup of the dirty and young bits in the PTE, while
  490. * handle_mm_fault() only guarantees to update these in the struct page.
  491. *
  492. * This is important for some architectures where those bits also gate the
  493. * access permission to the page because they are maintained in software. On
  494. * such architectures, gup() will not be enough to make a subsequent access
  495. * succeed.
  496. *
  497. * This should be called with the mm_sem held for read.
  498. */
  499. int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  500. unsigned long address, unsigned int fault_flags)
  501. {
  502. struct vm_area_struct *vma;
  503. vm_flags_t vm_flags;
  504. int ret;
  505. vma = find_extend_vma(mm, address);
  506. if (!vma || address < vma->vm_start)
  507. return -EFAULT;
  508. vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
  509. if (!(vm_flags & vma->vm_flags))
  510. return -EFAULT;
  511. ret = handle_mm_fault(mm, vma, address, fault_flags);
  512. if (ret & VM_FAULT_ERROR) {
  513. if (ret & VM_FAULT_OOM)
  514. return -ENOMEM;
  515. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  516. return -EHWPOISON;
  517. if (ret & VM_FAULT_SIGBUS)
  518. return -EFAULT;
  519. BUG();
  520. }
  521. if (tsk) {
  522. if (ret & VM_FAULT_MAJOR)
  523. tsk->maj_flt++;
  524. else
  525. tsk->min_flt++;
  526. }
  527. return 0;
  528. }
  529. /*
  530. * get_user_pages() - pin user pages in memory
  531. * @tsk: the task_struct to use for page fault accounting, or
  532. * NULL if faults are not to be recorded.
  533. * @mm: mm_struct of target mm
  534. * @start: starting user address
  535. * @nr_pages: number of pages from start to pin
  536. * @write: whether pages will be written to by the caller
  537. * @force: whether to force access even when user mapping is currently
  538. * protected (but never forces write access to shared mapping).
  539. * @pages: array that receives pointers to the pages pinned.
  540. * Should be at least nr_pages long. Or NULL, if caller
  541. * only intends to ensure the pages are faulted in.
  542. * @vmas: array of pointers to vmas corresponding to each page.
  543. * Or NULL if the caller does not require them.
  544. *
  545. * Returns number of pages pinned. This may be fewer than the number
  546. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  547. * were pinned, returns -errno. Each page returned must be released
  548. * with a put_page() call when it is finished with. vmas will only
  549. * remain valid while mmap_sem is held.
  550. *
  551. * Must be called with mmap_sem held for read or write.
  552. *
  553. * get_user_pages walks a process's page tables and takes a reference to
  554. * each struct page that each user address corresponds to at a given
  555. * instant. That is, it takes the page that would be accessed if a user
  556. * thread accesses the given user virtual address at that instant.
  557. *
  558. * This does not guarantee that the page exists in the user mappings when
  559. * get_user_pages returns, and there may even be a completely different
  560. * page there in some cases (eg. if mmapped pagecache has been invalidated
  561. * and subsequently re faulted). However it does guarantee that the page
  562. * won't be freed completely. And mostly callers simply care that the page
  563. * contains data that was valid *at some point in time*. Typically, an IO
  564. * or similar operation cannot guarantee anything stronger anyway because
  565. * locks can't be held over the syscall boundary.
  566. *
  567. * If write=0, the page must not be written to. If the page is written to,
  568. * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
  569. * after the page is finished with, and before put_page is called.
  570. *
  571. * get_user_pages is typically used for fewer-copy IO operations, to get a
  572. * handle on the memory by some means other than accesses via the user virtual
  573. * addresses. The pages may be submitted for DMA to devices or accessed via
  574. * their kernel linear mapping (via the kmap APIs). Care should be taken to
  575. * use the correct cache flushing APIs.
  576. *
  577. * See also get_user_pages_fast, for performance critical applications.
  578. */
  579. long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  580. unsigned long start, unsigned long nr_pages, int write,
  581. int force, struct page **pages, struct vm_area_struct **vmas)
  582. {
  583. int flags = FOLL_TOUCH;
  584. if (pages)
  585. flags |= FOLL_GET;
  586. if (write)
  587. flags |= FOLL_WRITE;
  588. if (force)
  589. flags |= FOLL_FORCE;
  590. return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
  591. NULL);
  592. }
  593. EXPORT_SYMBOL(get_user_pages);
  594. /**
  595. * get_dump_page() - pin user page in memory while writing it to core dump
  596. * @addr: user address
  597. *
  598. * Returns struct page pointer of user page pinned for dump,
  599. * to be freed afterwards by page_cache_release() or put_page().
  600. *
  601. * Returns NULL on any kind of failure - a hole must then be inserted into
  602. * the corefile, to preserve alignment with its headers; and also returns
  603. * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
  604. * allowing a hole to be left in the corefile to save diskspace.
  605. *
  606. * Called without mmap_sem, but after all other threads have been killed.
  607. */
  608. #ifdef CONFIG_ELF_CORE
  609. struct page *get_dump_page(unsigned long addr)
  610. {
  611. struct vm_area_struct *vma;
  612. struct page *page;
  613. if (__get_user_pages(current, current->mm, addr, 1,
  614. FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
  615. NULL) < 1)
  616. return NULL;
  617. flush_cache_page(vma, addr, page_to_pfn(page));
  618. return page;
  619. }
  620. #endif /* CONFIG_ELF_CORE */