gup.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. /*
  244. * mmap_sem must be held on entry. If @nonblocking != NULL and
  245. * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
  246. * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
  247. */
  248. static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
  249. unsigned long address, unsigned int *flags, int *nonblocking)
  250. {
  251. struct mm_struct *mm = vma->vm_mm;
  252. unsigned int fault_flags = 0;
  253. int ret;
  254. /* For mlock, just skip the stack guard page. */
  255. if ((*flags & FOLL_MLOCK) &&
  256. (stack_guard_page_start(vma, address) ||
  257. stack_guard_page_end(vma, address + PAGE_SIZE)))
  258. return -ENOENT;
  259. if (*flags & FOLL_WRITE)
  260. fault_flags |= FAULT_FLAG_WRITE;
  261. if (nonblocking)
  262. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  263. if (*flags & FOLL_NOWAIT)
  264. fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
  265. ret = handle_mm_fault(mm, vma, address, fault_flags);
  266. if (ret & VM_FAULT_ERROR) {
  267. if (ret & VM_FAULT_OOM)
  268. return -ENOMEM;
  269. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  270. return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
  271. if (ret & VM_FAULT_SIGBUS)
  272. return -EFAULT;
  273. BUG();
  274. }
  275. if (tsk) {
  276. if (ret & VM_FAULT_MAJOR)
  277. tsk->maj_flt++;
  278. else
  279. tsk->min_flt++;
  280. }
  281. if (ret & VM_FAULT_RETRY) {
  282. if (nonblocking)
  283. *nonblocking = 0;
  284. return -EBUSY;
  285. }
  286. /*
  287. * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
  288. * necessary, even if maybe_mkwrite decided not to set pte_write. We
  289. * can thus safely do subsequent page lookups as if they were reads.
  290. * But only do so when looping for pte_write is futile: in some cases
  291. * userspace may also be wanting to write to the gotten user page,
  292. * which a read fault here might prevent (a readonly page might get
  293. * reCOWed by userspace write).
  294. */
  295. if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
  296. *flags &= ~FOLL_WRITE;
  297. return 0;
  298. }
  299. static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
  300. {
  301. vm_flags_t vm_flags = vma->vm_flags;
  302. if (vm_flags & (VM_IO | VM_PFNMAP))
  303. return -EFAULT;
  304. if (gup_flags & FOLL_WRITE) {
  305. if (!(vm_flags & VM_WRITE)) {
  306. if (!(gup_flags & FOLL_FORCE))
  307. return -EFAULT;
  308. /*
  309. * We used to let the write,force case do COW in a
  310. * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
  311. * set a breakpoint in a read-only mapping of an
  312. * executable, without corrupting the file (yet only
  313. * when that file had been opened for writing!).
  314. * Anon pages in shared mappings are surprising: now
  315. * just reject it.
  316. */
  317. if (!is_cow_mapping(vm_flags)) {
  318. WARN_ON_ONCE(vm_flags & VM_MAYWRITE);
  319. return -EFAULT;
  320. }
  321. }
  322. } else if (!(vm_flags & VM_READ)) {
  323. if (!(gup_flags & FOLL_FORCE))
  324. return -EFAULT;
  325. /*
  326. * Is there actually any vma we can reach here which does not
  327. * have VM_MAYREAD set?
  328. */
  329. if (!(vm_flags & VM_MAYREAD))
  330. return -EFAULT;
  331. }
  332. return 0;
  333. }
  334. /**
  335. * __get_user_pages() - pin user pages in memory
  336. * @tsk: task_struct of target task
  337. * @mm: mm_struct of target mm
  338. * @start: starting user address
  339. * @nr_pages: number of pages from start to pin
  340. * @gup_flags: flags modifying pin behaviour
  341. * @pages: array that receives pointers to the pages pinned.
  342. * Should be at least nr_pages long. Or NULL, if caller
  343. * only intends to ensure the pages are faulted in.
  344. * @vmas: array of pointers to vmas corresponding to each page.
  345. * Or NULL if the caller does not require them.
  346. * @nonblocking: whether waiting for disk IO or mmap_sem contention
  347. *
  348. * Returns number of pages pinned. This may be fewer than the number
  349. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  350. * were pinned, returns -errno. Each page returned must be released
  351. * with a put_page() call when it is finished with. vmas will only
  352. * remain valid while mmap_sem is held.
  353. *
  354. * Must be called with mmap_sem held. It may be released. See below.
  355. *
  356. * __get_user_pages walks a process's page tables and takes a reference to
  357. * each struct page that each user address corresponds to at a given
  358. * instant. That is, it takes the page that would be accessed if a user
  359. * thread accesses the given user virtual address at that instant.
  360. *
  361. * This does not guarantee that the page exists in the user mappings when
  362. * __get_user_pages returns, and there may even be a completely different
  363. * page there in some cases (eg. if mmapped pagecache has been invalidated
  364. * and subsequently re faulted). However it does guarantee that the page
  365. * won't be freed completely. And mostly callers simply care that the page
  366. * contains data that was valid *at some point in time*. Typically, an IO
  367. * or similar operation cannot guarantee anything stronger anyway because
  368. * locks can't be held over the syscall boundary.
  369. *
  370. * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
  371. * the page is written to, set_page_dirty (or set_page_dirty_lock, as
  372. * appropriate) must be called after the page is finished with, and
  373. * before put_page is called.
  374. *
  375. * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
  376. * or mmap_sem contention, and if waiting is needed to pin all pages,
  377. * *@nonblocking will be set to 0. Further, if @gup_flags does not
  378. * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
  379. * this case.
  380. *
  381. * A caller using such a combination of @nonblocking and @gup_flags
  382. * must therefore hold the mmap_sem for reading only, and recognize
  383. * when it's been released. Otherwise, it must be held for either
  384. * reading or writing and will not be released.
  385. *
  386. * In most cases, get_user_pages or get_user_pages_fast should be used
  387. * instead of __get_user_pages. __get_user_pages should be used only if
  388. * you need some special @gup_flags.
  389. */
  390. long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  391. unsigned long start, unsigned long nr_pages,
  392. unsigned int gup_flags, struct page **pages,
  393. struct vm_area_struct **vmas, int *nonblocking)
  394. {
  395. long i = 0;
  396. unsigned int page_mask;
  397. struct vm_area_struct *vma = NULL;
  398. if (!nr_pages)
  399. return 0;
  400. VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
  401. /*
  402. * If FOLL_FORCE is set then do not force a full fault as the hinting
  403. * fault information is unrelated to the reference behaviour of a task
  404. * using the address space
  405. */
  406. if (!(gup_flags & FOLL_FORCE))
  407. gup_flags |= FOLL_NUMA;
  408. do {
  409. struct page *page;
  410. unsigned int foll_flags = gup_flags;
  411. unsigned int page_increm;
  412. /* first iteration or cross vma bound */
  413. if (!vma || start >= vma->vm_end) {
  414. vma = find_extend_vma(mm, start);
  415. if (!vma && in_gate_area(mm, start)) {
  416. int ret;
  417. ret = get_gate_page(mm, start & PAGE_MASK,
  418. gup_flags, &vma,
  419. pages ? &pages[i] : NULL);
  420. if (ret)
  421. return i ? : ret;
  422. page_mask = 0;
  423. goto next_page;
  424. }
  425. if (!vma || check_vma_flags(vma, gup_flags))
  426. return i ? : -EFAULT;
  427. if (is_vm_hugetlb_page(vma)) {
  428. i = follow_hugetlb_page(mm, vma, pages, vmas,
  429. &start, &nr_pages, i,
  430. gup_flags);
  431. continue;
  432. }
  433. }
  434. retry:
  435. /*
  436. * If we have a pending SIGKILL, don't keep faulting pages and
  437. * potentially allocating memory.
  438. */
  439. if (unlikely(fatal_signal_pending(current)))
  440. return i ? i : -ERESTARTSYS;
  441. cond_resched();
  442. page = follow_page_mask(vma, start, foll_flags, &page_mask);
  443. if (!page) {
  444. int ret;
  445. ret = faultin_page(tsk, vma, start, &foll_flags,
  446. nonblocking);
  447. switch (ret) {
  448. case 0:
  449. goto retry;
  450. case -EFAULT:
  451. case -ENOMEM:
  452. case -EHWPOISON:
  453. return i ? i : ret;
  454. case -EBUSY:
  455. return i;
  456. case -ENOENT:
  457. goto next_page;
  458. }
  459. BUG();
  460. }
  461. if (IS_ERR(page))
  462. return i ? i : PTR_ERR(page);
  463. if (pages) {
  464. pages[i] = page;
  465. flush_anon_page(vma, page, start);
  466. flush_dcache_page(page);
  467. page_mask = 0;
  468. }
  469. next_page:
  470. if (vmas) {
  471. vmas[i] = vma;
  472. page_mask = 0;
  473. }
  474. page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
  475. if (page_increm > nr_pages)
  476. page_increm = nr_pages;
  477. i += page_increm;
  478. start += page_increm * PAGE_SIZE;
  479. nr_pages -= page_increm;
  480. } while (nr_pages);
  481. return i;
  482. }
  483. EXPORT_SYMBOL(__get_user_pages);
  484. /*
  485. * fixup_user_fault() - manually resolve a user page fault
  486. * @tsk: the task_struct to use for page fault accounting, or
  487. * NULL if faults are not to be recorded.
  488. * @mm: mm_struct of target mm
  489. * @address: user address
  490. * @fault_flags:flags to pass down to handle_mm_fault()
  491. *
  492. * This is meant to be called in the specific scenario where for locking reasons
  493. * we try to access user memory in atomic context (within a pagefault_disable()
  494. * section), this returns -EFAULT, and we want to resolve the user fault before
  495. * trying again.
  496. *
  497. * Typically this is meant to be used by the futex code.
  498. *
  499. * The main difference with get_user_pages() is that this function will
  500. * unconditionally call handle_mm_fault() which will in turn perform all the
  501. * necessary SW fixup of the dirty and young bits in the PTE, while
  502. * handle_mm_fault() only guarantees to update these in the struct page.
  503. *
  504. * This is important for some architectures where those bits also gate the
  505. * access permission to the page because they are maintained in software. On
  506. * such architectures, gup() will not be enough to make a subsequent access
  507. * succeed.
  508. *
  509. * This has the same semantics wrt the @mm->mmap_sem as does filemap_fault().
  510. */
  511. int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  512. unsigned long address, unsigned int fault_flags)
  513. {
  514. struct vm_area_struct *vma;
  515. vm_flags_t vm_flags;
  516. int ret;
  517. vma = find_extend_vma(mm, address);
  518. if (!vma || address < vma->vm_start)
  519. return -EFAULT;
  520. vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
  521. if (!(vm_flags & vma->vm_flags))
  522. return -EFAULT;
  523. ret = handle_mm_fault(mm, vma, address, fault_flags);
  524. if (ret & VM_FAULT_ERROR) {
  525. if (ret & VM_FAULT_OOM)
  526. return -ENOMEM;
  527. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  528. return -EHWPOISON;
  529. if (ret & VM_FAULT_SIGBUS)
  530. return -EFAULT;
  531. BUG();
  532. }
  533. if (tsk) {
  534. if (ret & VM_FAULT_MAJOR)
  535. tsk->maj_flt++;
  536. else
  537. tsk->min_flt++;
  538. }
  539. return 0;
  540. }
  541. /*
  542. * get_user_pages() - pin user pages in memory
  543. * @tsk: the task_struct to use for page fault accounting, or
  544. * NULL if faults are not to be recorded.
  545. * @mm: mm_struct of target mm
  546. * @start: starting user address
  547. * @nr_pages: number of pages from start to pin
  548. * @write: whether pages will be written to by the caller
  549. * @force: whether to force access even when user mapping is currently
  550. * protected (but never forces write access to shared mapping).
  551. * @pages: array that receives pointers to the pages pinned.
  552. * Should be at least nr_pages long. Or NULL, if caller
  553. * only intends to ensure the pages are faulted in.
  554. * @vmas: array of pointers to vmas corresponding to each page.
  555. * Or NULL if the caller does not require them.
  556. *
  557. * Returns number of pages pinned. This may be fewer than the number
  558. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  559. * were pinned, returns -errno. Each page returned must be released
  560. * with a put_page() call when it is finished with. vmas will only
  561. * remain valid while mmap_sem is held.
  562. *
  563. * Must be called with mmap_sem held for read or write.
  564. *
  565. * get_user_pages walks a process's page tables and takes a reference to
  566. * each struct page that each user address corresponds to at a given
  567. * instant. That is, it takes the page that would be accessed if a user
  568. * thread accesses the given user virtual address at that instant.
  569. *
  570. * This does not guarantee that the page exists in the user mappings when
  571. * get_user_pages returns, and there may even be a completely different
  572. * page there in some cases (eg. if mmapped pagecache has been invalidated
  573. * and subsequently re faulted). However it does guarantee that the page
  574. * won't be freed completely. And mostly callers simply care that the page
  575. * contains data that was valid *at some point in time*. Typically, an IO
  576. * or similar operation cannot guarantee anything stronger anyway because
  577. * locks can't be held over the syscall boundary.
  578. *
  579. * If write=0, the page must not be written to. If the page is written to,
  580. * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
  581. * after the page is finished with, and before put_page is called.
  582. *
  583. * get_user_pages is typically used for fewer-copy IO operations, to get a
  584. * handle on the memory by some means other than accesses via the user virtual
  585. * addresses. The pages may be submitted for DMA to devices or accessed via
  586. * their kernel linear mapping (via the kmap APIs). Care should be taken to
  587. * use the correct cache flushing APIs.
  588. *
  589. * See also get_user_pages_fast, for performance critical applications.
  590. */
  591. long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  592. unsigned long start, unsigned long nr_pages, int write,
  593. int force, struct page **pages, struct vm_area_struct **vmas)
  594. {
  595. int flags = FOLL_TOUCH;
  596. if (pages)
  597. flags |= FOLL_GET;
  598. if (write)
  599. flags |= FOLL_WRITE;
  600. if (force)
  601. flags |= FOLL_FORCE;
  602. return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
  603. NULL);
  604. }
  605. EXPORT_SYMBOL(get_user_pages);
  606. /**
  607. * get_dump_page() - pin user page in memory while writing it to core dump
  608. * @addr: user address
  609. *
  610. * Returns struct page pointer of user page pinned for dump,
  611. * to be freed afterwards by page_cache_release() or put_page().
  612. *
  613. * Returns NULL on any kind of failure - a hole must then be inserted into
  614. * the corefile, to preserve alignment with its headers; and also returns
  615. * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
  616. * allowing a hole to be left in the corefile to save diskspace.
  617. *
  618. * Called without mmap_sem, but after all other threads have been killed.
  619. */
  620. #ifdef CONFIG_ELF_CORE
  621. struct page *get_dump_page(unsigned long addr)
  622. {
  623. struct vm_area_struct *vma;
  624. struct page *page;
  625. if (__get_user_pages(current, current->mm, addr, 1,
  626. FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
  627. NULL) < 1)
  628. return NULL;
  629. flush_cache_page(vma, addr, page_to_pfn(page));
  630. return page;
  631. }
  632. #endif /* CONFIG_ELF_CORE */