gup.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. #include <linux/kernel.h>
  2. #include <linux/errno.h>
  3. #include <linux/err.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/mm.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/rmap.h>
  8. #include <linux/swap.h>
  9. #include <linux/swapops.h>
  10. #include <linux/sched.h>
  11. #include <linux/rwsem.h>
  12. #include <linux/hugetlb.h>
  13. #include <asm/pgtable.h>
  14. #include "internal.h"
  15. static struct page *no_page_table(struct vm_area_struct *vma,
  16. unsigned int flags)
  17. {
  18. /*
  19. * When core dumping an enormous anonymous area that nobody
  20. * has touched so far, we don't want to allocate unnecessary pages or
  21. * page tables. Return error instead of NULL to skip handle_mm_fault,
  22. * then get_dump_page() will return NULL to leave a hole in the dump.
  23. * But we can only make this optimization where a hole would surely
  24. * be zero-filled if handle_mm_fault() actually did handle it.
  25. */
  26. if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
  27. return ERR_PTR(-EFAULT);
  28. return NULL;
  29. }
  30. static struct page *follow_page_pte(struct vm_area_struct *vma,
  31. unsigned long address, pmd_t *pmd, unsigned int flags)
  32. {
  33. struct mm_struct *mm = vma->vm_mm;
  34. struct page *page;
  35. spinlock_t *ptl;
  36. pte_t *ptep, pte;
  37. retry:
  38. if (unlikely(pmd_bad(*pmd)))
  39. return no_page_table(vma, flags);
  40. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  41. pte = *ptep;
  42. if (!pte_present(pte)) {
  43. swp_entry_t entry;
  44. /*
  45. * KSM's break_ksm() relies upon recognizing a ksm page
  46. * even while it is being migrated, so for that case we
  47. * need migration_entry_wait().
  48. */
  49. if (likely(!(flags & FOLL_MIGRATION)))
  50. goto no_page;
  51. if (pte_none(pte))
  52. goto no_page;
  53. entry = pte_to_swp_entry(pte);
  54. if (!is_migration_entry(entry))
  55. goto no_page;
  56. pte_unmap_unlock(ptep, ptl);
  57. migration_entry_wait(mm, pmd, address);
  58. goto retry;
  59. }
  60. if ((flags & FOLL_NUMA) && pte_protnone(pte))
  61. goto no_page;
  62. if ((flags & FOLL_WRITE) && !pte_write(pte)) {
  63. pte_unmap_unlock(ptep, ptl);
  64. return NULL;
  65. }
  66. page = vm_normal_page(vma, address, pte);
  67. if (unlikely(!page)) {
  68. if ((flags & FOLL_DUMP) ||
  69. !is_zero_pfn(pte_pfn(pte)))
  70. goto bad_page;
  71. page = pte_page(pte);
  72. }
  73. if (flags & FOLL_GET)
  74. get_page_foll(page);
  75. if (flags & FOLL_TOUCH) {
  76. if ((flags & FOLL_WRITE) &&
  77. !pte_dirty(pte) && !PageDirty(page))
  78. set_page_dirty(page);
  79. /*
  80. * pte_mkyoung() would be more correct here, but atomic care
  81. * is needed to avoid losing the dirty bit: it is easier to use
  82. * mark_page_accessed().
  83. */
  84. mark_page_accessed(page);
  85. }
  86. if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
  87. /*
  88. * The preliminary mapping check is mainly to avoid the
  89. * pointless overhead of lock_page on the ZERO_PAGE
  90. * which might bounce very badly if there is contention.
  91. *
  92. * If the page is already locked, we don't need to
  93. * handle it now - vmscan will handle it later if and
  94. * when it attempts to reclaim the page.
  95. */
  96. if (page->mapping && trylock_page(page)) {
  97. lru_add_drain(); /* push cached pages to LRU */
  98. /*
  99. * Because we lock page here, and migration is
  100. * blocked by the pte's page reference, and we
  101. * know the page is still mapped, we don't even
  102. * need to check for file-cache page truncation.
  103. */
  104. mlock_vma_page(page);
  105. unlock_page(page);
  106. }
  107. }
  108. pte_unmap_unlock(ptep, ptl);
  109. return page;
  110. bad_page:
  111. pte_unmap_unlock(ptep, ptl);
  112. return ERR_PTR(-EFAULT);
  113. no_page:
  114. pte_unmap_unlock(ptep, ptl);
  115. if (!pte_none(pte))
  116. return NULL;
  117. return no_page_table(vma, flags);
  118. }
  119. /**
  120. * follow_page_mask - look up a page descriptor from a user-virtual address
  121. * @vma: vm_area_struct mapping @address
  122. * @address: virtual address to look up
  123. * @flags: flags modifying lookup behaviour
  124. * @page_mask: on output, *page_mask is set according to the size of the page
  125. *
  126. * @flags can have FOLL_ flags set, defined in <linux/mm.h>
  127. *
  128. * Returns the mapped (struct page *), %NULL if no mapping exists, or
  129. * an error pointer if there is a mapping to something not represented
  130. * by a page descriptor (see also vm_normal_page()).
  131. */
  132. struct page *follow_page_mask(struct vm_area_struct *vma,
  133. unsigned long address, unsigned int flags,
  134. unsigned int *page_mask)
  135. {
  136. pgd_t *pgd;
  137. pud_t *pud;
  138. pmd_t *pmd;
  139. spinlock_t *ptl;
  140. struct page *page;
  141. struct mm_struct *mm = vma->vm_mm;
  142. *page_mask = 0;
  143. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  144. if (!IS_ERR(page)) {
  145. BUG_ON(flags & FOLL_GET);
  146. return page;
  147. }
  148. pgd = pgd_offset(mm, address);
  149. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  150. return no_page_table(vma, flags);
  151. pud = pud_offset(pgd, address);
  152. if (pud_none(*pud))
  153. return no_page_table(vma, flags);
  154. if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
  155. page = follow_huge_pud(mm, address, pud, flags);
  156. if (page)
  157. return page;
  158. return no_page_table(vma, flags);
  159. }
  160. if (unlikely(pud_bad(*pud)))
  161. return no_page_table(vma, flags);
  162. pmd = pmd_offset(pud, address);
  163. if (pmd_none(*pmd))
  164. return no_page_table(vma, flags);
  165. if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
  166. page = follow_huge_pmd(mm, address, pmd, flags);
  167. if (page)
  168. return page;
  169. return no_page_table(vma, flags);
  170. }
  171. if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
  172. return no_page_table(vma, flags);
  173. if (pmd_trans_huge(*pmd)) {
  174. if (flags & FOLL_SPLIT) {
  175. split_huge_page_pmd(vma, address, pmd);
  176. return follow_page_pte(vma, address, pmd, flags);
  177. }
  178. ptl = pmd_lock(mm, pmd);
  179. if (likely(pmd_trans_huge(*pmd))) {
  180. if (unlikely(pmd_trans_splitting(*pmd))) {
  181. spin_unlock(ptl);
  182. wait_split_huge_page(vma->anon_vma, pmd);
  183. } else {
  184. page = follow_trans_huge_pmd(vma, address,
  185. pmd, flags);
  186. spin_unlock(ptl);
  187. *page_mask = HPAGE_PMD_NR - 1;
  188. return page;
  189. }
  190. } else
  191. spin_unlock(ptl);
  192. }
  193. return follow_page_pte(vma, address, pmd, flags);
  194. }
  195. static int get_gate_page(struct mm_struct *mm, unsigned long address,
  196. unsigned int gup_flags, struct vm_area_struct **vma,
  197. struct page **page)
  198. {
  199. pgd_t *pgd;
  200. pud_t *pud;
  201. pmd_t *pmd;
  202. pte_t *pte;
  203. int ret = -EFAULT;
  204. /* user gate pages are read-only */
  205. if (gup_flags & FOLL_WRITE)
  206. return -EFAULT;
  207. if (address > TASK_SIZE)
  208. pgd = pgd_offset_k(address);
  209. else
  210. pgd = pgd_offset_gate(mm, address);
  211. BUG_ON(pgd_none(*pgd));
  212. pud = pud_offset(pgd, address);
  213. BUG_ON(pud_none(*pud));
  214. pmd = pmd_offset(pud, address);
  215. if (pmd_none(*pmd))
  216. return -EFAULT;
  217. VM_BUG_ON(pmd_trans_huge(*pmd));
  218. pte = pte_offset_map(pmd, address);
  219. if (pte_none(*pte))
  220. goto unmap;
  221. *vma = get_gate_vma(mm);
  222. if (!page)
  223. goto out;
  224. *page = vm_normal_page(*vma, address, *pte);
  225. if (!*page) {
  226. if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
  227. goto unmap;
  228. *page = pte_page(*pte);
  229. }
  230. get_page(*page);
  231. out:
  232. ret = 0;
  233. unmap:
  234. pte_unmap(pte);
  235. return ret;
  236. }
  237. /*
  238. * mmap_sem must be held on entry. If @nonblocking != NULL and
  239. * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
  240. * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
  241. */
  242. static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
  243. unsigned long address, unsigned int *flags, int *nonblocking)
  244. {
  245. struct mm_struct *mm = vma->vm_mm;
  246. unsigned int fault_flags = 0;
  247. int ret;
  248. /* For mlock, just skip the stack guard page. */
  249. if ((*flags & FOLL_MLOCK) &&
  250. (stack_guard_page_start(vma, address) ||
  251. stack_guard_page_end(vma, address + PAGE_SIZE)))
  252. return -ENOENT;
  253. if (*flags & FOLL_WRITE)
  254. fault_flags |= FAULT_FLAG_WRITE;
  255. if (nonblocking)
  256. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  257. if (*flags & FOLL_NOWAIT)
  258. fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
  259. if (*flags & FOLL_TRIED) {
  260. VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
  261. fault_flags |= FAULT_FLAG_TRIED;
  262. }
  263. ret = handle_mm_fault(mm, vma, address, fault_flags);
  264. if (ret & VM_FAULT_ERROR) {
  265. if (ret & VM_FAULT_OOM)
  266. return -ENOMEM;
  267. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  268. return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
  269. if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  270. return -EFAULT;
  271. BUG();
  272. }
  273. if (tsk) {
  274. if (ret & VM_FAULT_MAJOR)
  275. tsk->maj_flt++;
  276. else
  277. tsk->min_flt++;
  278. }
  279. if (ret & VM_FAULT_RETRY) {
  280. if (nonblocking)
  281. *nonblocking = 0;
  282. return -EBUSY;
  283. }
  284. /*
  285. * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
  286. * necessary, even if maybe_mkwrite decided not to set pte_write. We
  287. * can thus safely do subsequent page lookups as if they were reads.
  288. * But only do so when looping for pte_write is futile: in some cases
  289. * userspace may also be wanting to write to the gotten user page,
  290. * which a read fault here might prevent (a readonly page might get
  291. * reCOWed by userspace write).
  292. */
  293. if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
  294. *flags &= ~FOLL_WRITE;
  295. return 0;
  296. }
  297. static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
  298. {
  299. vm_flags_t vm_flags = vma->vm_flags;
  300. if (vm_flags & (VM_IO | VM_PFNMAP))
  301. return -EFAULT;
  302. if (gup_flags & FOLL_WRITE) {
  303. if (!(vm_flags & VM_WRITE)) {
  304. if (!(gup_flags & FOLL_FORCE))
  305. return -EFAULT;
  306. /*
  307. * We used to let the write,force case do COW in a
  308. * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
  309. * set a breakpoint in a read-only mapping of an
  310. * executable, without corrupting the file (yet only
  311. * when that file had been opened for writing!).
  312. * Anon pages in shared mappings are surprising: now
  313. * just reject it.
  314. */
  315. if (!is_cow_mapping(vm_flags)) {
  316. WARN_ON_ONCE(vm_flags & VM_MAYWRITE);
  317. return -EFAULT;
  318. }
  319. }
  320. } else if (!(vm_flags & VM_READ)) {
  321. if (!(gup_flags & FOLL_FORCE))
  322. return -EFAULT;
  323. /*
  324. * Is there actually any vma we can reach here which does not
  325. * have VM_MAYREAD set?
  326. */
  327. if (!(vm_flags & VM_MAYREAD))
  328. return -EFAULT;
  329. }
  330. return 0;
  331. }
  332. /**
  333. * __get_user_pages() - pin user pages in memory
  334. * @tsk: task_struct of target task
  335. * @mm: mm_struct of target mm
  336. * @start: starting user address
  337. * @nr_pages: number of pages from start to pin
  338. * @gup_flags: flags modifying pin behaviour
  339. * @pages: array that receives pointers to the pages pinned.
  340. * Should be at least nr_pages long. Or NULL, if caller
  341. * only intends to ensure the pages are faulted in.
  342. * @vmas: array of pointers to vmas corresponding to each page.
  343. * Or NULL if the caller does not require them.
  344. * @nonblocking: whether waiting for disk IO or mmap_sem contention
  345. *
  346. * Returns number of pages pinned. This may be fewer than the number
  347. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  348. * were pinned, returns -errno. Each page returned must be released
  349. * with a put_page() call when it is finished with. vmas will only
  350. * remain valid while mmap_sem is held.
  351. *
  352. * Must be called with mmap_sem held. It may be released. See below.
  353. *
  354. * __get_user_pages walks a process's page tables and takes a reference to
  355. * each struct page that each user address corresponds to at a given
  356. * instant. That is, it takes the page that would be accessed if a user
  357. * thread accesses the given user virtual address at that instant.
  358. *
  359. * This does not guarantee that the page exists in the user mappings when
  360. * __get_user_pages returns, and there may even be a completely different
  361. * page there in some cases (eg. if mmapped pagecache has been invalidated
  362. * and subsequently re faulted). However it does guarantee that the page
  363. * won't be freed completely. And mostly callers simply care that the page
  364. * contains data that was valid *at some point in time*. Typically, an IO
  365. * or similar operation cannot guarantee anything stronger anyway because
  366. * locks can't be held over the syscall boundary.
  367. *
  368. * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
  369. * the page is written to, set_page_dirty (or set_page_dirty_lock, as
  370. * appropriate) must be called after the page is finished with, and
  371. * before put_page is called.
  372. *
  373. * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
  374. * or mmap_sem contention, and if waiting is needed to pin all pages,
  375. * *@nonblocking will be set to 0. Further, if @gup_flags does not
  376. * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
  377. * this case.
  378. *
  379. * A caller using such a combination of @nonblocking and @gup_flags
  380. * must therefore hold the mmap_sem for reading only, and recognize
  381. * when it's been released. Otherwise, it must be held for either
  382. * reading or writing and will not be released.
  383. *
  384. * In most cases, get_user_pages or get_user_pages_fast should be used
  385. * instead of __get_user_pages. __get_user_pages should be used only if
  386. * you need some special @gup_flags.
  387. */
  388. long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  389. unsigned long start, unsigned long nr_pages,
  390. unsigned int gup_flags, struct page **pages,
  391. struct vm_area_struct **vmas, int *nonblocking)
  392. {
  393. long i = 0;
  394. unsigned int page_mask;
  395. struct vm_area_struct *vma = NULL;
  396. if (!nr_pages)
  397. return 0;
  398. VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
  399. /*
  400. * If FOLL_FORCE is set then do not force a full fault as the hinting
  401. * fault information is unrelated to the reference behaviour of a task
  402. * using the address space
  403. */
  404. if (!(gup_flags & FOLL_FORCE))
  405. gup_flags |= FOLL_NUMA;
  406. do {
  407. struct page *page;
  408. unsigned int foll_flags = gup_flags;
  409. unsigned int page_increm;
  410. /* first iteration or cross vma bound */
  411. if (!vma || start >= vma->vm_end) {
  412. vma = find_extend_vma(mm, start);
  413. if (!vma && in_gate_area(mm, start)) {
  414. int ret;
  415. ret = get_gate_page(mm, start & PAGE_MASK,
  416. gup_flags, &vma,
  417. pages ? &pages[i] : NULL);
  418. if (ret)
  419. return i ? : ret;
  420. page_mask = 0;
  421. goto next_page;
  422. }
  423. if (!vma || check_vma_flags(vma, gup_flags))
  424. return i ? : -EFAULT;
  425. if (is_vm_hugetlb_page(vma)) {
  426. i = follow_hugetlb_page(mm, vma, pages, vmas,
  427. &start, &nr_pages, i,
  428. gup_flags);
  429. continue;
  430. }
  431. }
  432. retry:
  433. /*
  434. * If we have a pending SIGKILL, don't keep faulting pages and
  435. * potentially allocating memory.
  436. */
  437. if (unlikely(fatal_signal_pending(current)))
  438. return i ? i : -ERESTARTSYS;
  439. cond_resched();
  440. page = follow_page_mask(vma, start, foll_flags, &page_mask);
  441. if (!page) {
  442. int ret;
  443. ret = faultin_page(tsk, vma, start, &foll_flags,
  444. nonblocking);
  445. switch (ret) {
  446. case 0:
  447. goto retry;
  448. case -EFAULT:
  449. case -ENOMEM:
  450. case -EHWPOISON:
  451. return i ? i : ret;
  452. case -EBUSY:
  453. return i;
  454. case -ENOENT:
  455. goto next_page;
  456. }
  457. BUG();
  458. }
  459. if (IS_ERR(page))
  460. return i ? i : PTR_ERR(page);
  461. if (pages) {
  462. pages[i] = page;
  463. flush_anon_page(vma, page, start);
  464. flush_dcache_page(page);
  465. page_mask = 0;
  466. }
  467. next_page:
  468. if (vmas) {
  469. vmas[i] = vma;
  470. page_mask = 0;
  471. }
  472. page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
  473. if (page_increm > nr_pages)
  474. page_increm = nr_pages;
  475. i += page_increm;
  476. start += page_increm * PAGE_SIZE;
  477. nr_pages -= page_increm;
  478. } while (nr_pages);
  479. return i;
  480. }
  481. EXPORT_SYMBOL(__get_user_pages);
  482. /*
  483. * fixup_user_fault() - manually resolve a user page fault
  484. * @tsk: the task_struct to use for page fault accounting, or
  485. * NULL if faults are not to be recorded.
  486. * @mm: mm_struct of target mm
  487. * @address: user address
  488. * @fault_flags:flags to pass down to handle_mm_fault()
  489. *
  490. * This is meant to be called in the specific scenario where for locking reasons
  491. * we try to access user memory in atomic context (within a pagefault_disable()
  492. * section), this returns -EFAULT, and we want to resolve the user fault before
  493. * trying again.
  494. *
  495. * Typically this is meant to be used by the futex code.
  496. *
  497. * The main difference with get_user_pages() is that this function will
  498. * unconditionally call handle_mm_fault() which will in turn perform all the
  499. * necessary SW fixup of the dirty and young bits in the PTE, while
  500. * handle_mm_fault() only guarantees to update these in the struct page.
  501. *
  502. * This is important for some architectures where those bits also gate the
  503. * access permission to the page because they are maintained in software. On
  504. * such architectures, gup() will not be enough to make a subsequent access
  505. * succeed.
  506. *
  507. * This has the same semantics wrt the @mm->mmap_sem as does filemap_fault().
  508. */
  509. int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  510. unsigned long address, unsigned int fault_flags)
  511. {
  512. struct vm_area_struct *vma;
  513. vm_flags_t vm_flags;
  514. int ret;
  515. vma = find_extend_vma(mm, address);
  516. if (!vma || address < vma->vm_start)
  517. return -EFAULT;
  518. vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
  519. if (!(vm_flags & vma->vm_flags))
  520. return -EFAULT;
  521. ret = handle_mm_fault(mm, vma, address, fault_flags);
  522. if (ret & VM_FAULT_ERROR) {
  523. if (ret & VM_FAULT_OOM)
  524. return -ENOMEM;
  525. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  526. return -EHWPOISON;
  527. if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  528. return -EFAULT;
  529. BUG();
  530. }
  531. if (tsk) {
  532. if (ret & VM_FAULT_MAJOR)
  533. tsk->maj_flt++;
  534. else
  535. tsk->min_flt++;
  536. }
  537. return 0;
  538. }
  539. static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
  540. struct mm_struct *mm,
  541. unsigned long start,
  542. unsigned long nr_pages,
  543. int write, int force,
  544. struct page **pages,
  545. struct vm_area_struct **vmas,
  546. int *locked, bool notify_drop,
  547. unsigned int flags)
  548. {
  549. long ret, pages_done;
  550. bool lock_dropped;
  551. if (locked) {
  552. /* if VM_FAULT_RETRY can be returned, vmas become invalid */
  553. BUG_ON(vmas);
  554. /* check caller initialized locked */
  555. BUG_ON(*locked != 1);
  556. }
  557. if (pages)
  558. flags |= FOLL_GET;
  559. if (write)
  560. flags |= FOLL_WRITE;
  561. if (force)
  562. flags |= FOLL_FORCE;
  563. pages_done = 0;
  564. lock_dropped = false;
  565. for (;;) {
  566. ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
  567. vmas, locked);
  568. if (!locked)
  569. /* VM_FAULT_RETRY couldn't trigger, bypass */
  570. return ret;
  571. /* VM_FAULT_RETRY cannot return errors */
  572. if (!*locked) {
  573. BUG_ON(ret < 0);
  574. BUG_ON(ret >= nr_pages);
  575. }
  576. if (!pages)
  577. /* If it's a prefault don't insist harder */
  578. return ret;
  579. if (ret > 0) {
  580. nr_pages -= ret;
  581. pages_done += ret;
  582. if (!nr_pages)
  583. break;
  584. }
  585. if (*locked) {
  586. /* VM_FAULT_RETRY didn't trigger */
  587. if (!pages_done)
  588. pages_done = ret;
  589. break;
  590. }
  591. /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
  592. pages += ret;
  593. start += ret << PAGE_SHIFT;
  594. /*
  595. * Repeat on the address that fired VM_FAULT_RETRY
  596. * without FAULT_FLAG_ALLOW_RETRY but with
  597. * FAULT_FLAG_TRIED.
  598. */
  599. *locked = 1;
  600. lock_dropped = true;
  601. down_read(&mm->mmap_sem);
  602. ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
  603. pages, NULL, NULL);
  604. if (ret != 1) {
  605. BUG_ON(ret > 1);
  606. if (!pages_done)
  607. pages_done = ret;
  608. break;
  609. }
  610. nr_pages--;
  611. pages_done++;
  612. if (!nr_pages)
  613. break;
  614. pages++;
  615. start += PAGE_SIZE;
  616. }
  617. if (notify_drop && lock_dropped && *locked) {
  618. /*
  619. * We must let the caller know we temporarily dropped the lock
  620. * and so the critical section protected by it was lost.
  621. */
  622. up_read(&mm->mmap_sem);
  623. *locked = 0;
  624. }
  625. return pages_done;
  626. }
  627. /*
  628. * We can leverage the VM_FAULT_RETRY functionality in the page fault
  629. * paths better by using either get_user_pages_locked() or
  630. * get_user_pages_unlocked().
  631. *
  632. * get_user_pages_locked() is suitable to replace the form:
  633. *
  634. * down_read(&mm->mmap_sem);
  635. * do_something()
  636. * get_user_pages(tsk, mm, ..., pages, NULL);
  637. * up_read(&mm->mmap_sem);
  638. *
  639. * to:
  640. *
  641. * int locked = 1;
  642. * down_read(&mm->mmap_sem);
  643. * do_something()
  644. * get_user_pages_locked(tsk, mm, ..., pages, &locked);
  645. * if (locked)
  646. * up_read(&mm->mmap_sem);
  647. */
  648. long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm,
  649. unsigned long start, unsigned long nr_pages,
  650. int write, int force, struct page **pages,
  651. int *locked)
  652. {
  653. return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
  654. pages, NULL, locked, true, FOLL_TOUCH);
  655. }
  656. EXPORT_SYMBOL(get_user_pages_locked);
  657. /*
  658. * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows to
  659. * pass additional gup_flags as last parameter (like FOLL_HWPOISON).
  660. *
  661. * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the
  662. * caller if required (just like with __get_user_pages). "FOLL_GET",
  663. * "FOLL_WRITE" and "FOLL_FORCE" are set implicitly as needed
  664. * according to the parameters "pages", "write", "force"
  665. * respectively.
  666. */
  667. __always_inline long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
  668. unsigned long start, unsigned long nr_pages,
  669. int write, int force, struct page **pages,
  670. unsigned int gup_flags)
  671. {
  672. long ret;
  673. int locked = 1;
  674. down_read(&mm->mmap_sem);
  675. ret = __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
  676. pages, NULL, &locked, false, gup_flags);
  677. if (locked)
  678. up_read(&mm->mmap_sem);
  679. return ret;
  680. }
  681. EXPORT_SYMBOL(__get_user_pages_unlocked);
  682. /*
  683. * get_user_pages_unlocked() is suitable to replace the form:
  684. *
  685. * down_read(&mm->mmap_sem);
  686. * get_user_pages(tsk, mm, ..., pages, NULL);
  687. * up_read(&mm->mmap_sem);
  688. *
  689. * with:
  690. *
  691. * get_user_pages_unlocked(tsk, mm, ..., pages);
  692. *
  693. * It is functionally equivalent to get_user_pages_fast so
  694. * get_user_pages_fast should be used instead, if the two parameters
  695. * "tsk" and "mm" are respectively equal to current and current->mm,
  696. * or if "force" shall be set to 1 (get_user_pages_fast misses the
  697. * "force" parameter).
  698. */
  699. long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
  700. unsigned long start, unsigned long nr_pages,
  701. int write, int force, struct page **pages)
  702. {
  703. return __get_user_pages_unlocked(tsk, mm, start, nr_pages, write,
  704. force, pages, FOLL_TOUCH);
  705. }
  706. EXPORT_SYMBOL(get_user_pages_unlocked);
  707. /*
  708. * get_user_pages() - pin user pages in memory
  709. * @tsk: the task_struct to use for page fault accounting, or
  710. * NULL if faults are not to be recorded.
  711. * @mm: mm_struct of target mm
  712. * @start: starting user address
  713. * @nr_pages: number of pages from start to pin
  714. * @write: whether pages will be written to by the caller
  715. * @force: whether to force access even when user mapping is currently
  716. * protected (but never forces write access to shared mapping).
  717. * @pages: array that receives pointers to the pages pinned.
  718. * Should be at least nr_pages long. Or NULL, if caller
  719. * only intends to ensure the pages are faulted in.
  720. * @vmas: array of pointers to vmas corresponding to each page.
  721. * Or NULL if the caller does not require them.
  722. *
  723. * Returns number of pages pinned. This may be fewer than the number
  724. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  725. * were pinned, returns -errno. Each page returned must be released
  726. * with a put_page() call when it is finished with. vmas will only
  727. * remain valid while mmap_sem is held.
  728. *
  729. * Must be called with mmap_sem held for read or write.
  730. *
  731. * get_user_pages walks a process's page tables and takes a reference to
  732. * each struct page that each user address corresponds to at a given
  733. * instant. That is, it takes the page that would be accessed if a user
  734. * thread accesses the given user virtual address at that instant.
  735. *
  736. * This does not guarantee that the page exists in the user mappings when
  737. * get_user_pages returns, and there may even be a completely different
  738. * page there in some cases (eg. if mmapped pagecache has been invalidated
  739. * and subsequently re faulted). However it does guarantee that the page
  740. * won't be freed completely. And mostly callers simply care that the page
  741. * contains data that was valid *at some point in time*. Typically, an IO
  742. * or similar operation cannot guarantee anything stronger anyway because
  743. * locks can't be held over the syscall boundary.
  744. *
  745. * If write=0, the page must not be written to. If the page is written to,
  746. * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
  747. * after the page is finished with, and before put_page is called.
  748. *
  749. * get_user_pages is typically used for fewer-copy IO operations, to get a
  750. * handle on the memory by some means other than accesses via the user virtual
  751. * addresses. The pages may be submitted for DMA to devices or accessed via
  752. * their kernel linear mapping (via the kmap APIs). Care should be taken to
  753. * use the correct cache flushing APIs.
  754. *
  755. * See also get_user_pages_fast, for performance critical applications.
  756. *
  757. * get_user_pages should be phased out in favor of
  758. * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
  759. * should use get_user_pages because it cannot pass
  760. * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
  761. */
  762. long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  763. unsigned long start, unsigned long nr_pages, int write,
  764. int force, struct page **pages, struct vm_area_struct **vmas)
  765. {
  766. return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
  767. pages, vmas, NULL, false, FOLL_TOUCH);
  768. }
  769. EXPORT_SYMBOL(get_user_pages);
  770. /**
  771. * get_dump_page() - pin user page in memory while writing it to core dump
  772. * @addr: user address
  773. *
  774. * Returns struct page pointer of user page pinned for dump,
  775. * to be freed afterwards by page_cache_release() or put_page().
  776. *
  777. * Returns NULL on any kind of failure - a hole must then be inserted into
  778. * the corefile, to preserve alignment with its headers; and also returns
  779. * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
  780. * allowing a hole to be left in the corefile to save diskspace.
  781. *
  782. * Called without mmap_sem, but after all other threads have been killed.
  783. */
  784. #ifdef CONFIG_ELF_CORE
  785. struct page *get_dump_page(unsigned long addr)
  786. {
  787. struct vm_area_struct *vma;
  788. struct page *page;
  789. if (__get_user_pages(current, current->mm, addr, 1,
  790. FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
  791. NULL) < 1)
  792. return NULL;
  793. flush_cache_page(vma, addr, page_to_pfn(page));
  794. return page;
  795. }
  796. #endif /* CONFIG_ELF_CORE */
  797. /*
  798. * Generic RCU Fast GUP
  799. *
  800. * get_user_pages_fast attempts to pin user pages by walking the page
  801. * tables directly and avoids taking locks. Thus the walker needs to be
  802. * protected from page table pages being freed from under it, and should
  803. * block any THP splits.
  804. *
  805. * One way to achieve this is to have the walker disable interrupts, and
  806. * rely on IPIs from the TLB flushing code blocking before the page table
  807. * pages are freed. This is unsuitable for architectures that do not need
  808. * to broadcast an IPI when invalidating TLBs.
  809. *
  810. * Another way to achieve this is to batch up page table containing pages
  811. * belonging to more than one mm_user, then rcu_sched a callback to free those
  812. * pages. Disabling interrupts will allow the fast_gup walker to both block
  813. * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
  814. * (which is a relatively rare event). The code below adopts this strategy.
  815. *
  816. * Before activating this code, please be aware that the following assumptions
  817. * are currently made:
  818. *
  819. * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
  820. * pages containing page tables.
  821. *
  822. * *) THP splits will broadcast an IPI, this can be achieved by overriding
  823. * pmdp_splitting_flush.
  824. *
  825. * *) ptes can be read atomically by the architecture.
  826. *
  827. * *) access_ok is sufficient to validate userspace address ranges.
  828. *
  829. * The last two assumptions can be relaxed by the addition of helper functions.
  830. *
  831. * This code is based heavily on the PowerPC implementation by Nick Piggin.
  832. */
  833. #ifdef CONFIG_HAVE_GENERIC_RCU_GUP
  834. #ifdef __HAVE_ARCH_PTE_SPECIAL
  835. static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  836. int write, struct page **pages, int *nr)
  837. {
  838. pte_t *ptep, *ptem;
  839. int ret = 0;
  840. ptem = ptep = pte_offset_map(&pmd, addr);
  841. do {
  842. /*
  843. * In the line below we are assuming that the pte can be read
  844. * atomically. If this is not the case for your architecture,
  845. * please wrap this in a helper function!
  846. *
  847. * for an example see gup_get_pte in arch/x86/mm/gup.c
  848. */
  849. pte_t pte = ACCESS_ONCE(*ptep);
  850. struct page *page;
  851. /*
  852. * Similar to the PMD case below, NUMA hinting must take slow
  853. * path using the pte_protnone check.
  854. */
  855. if (!pte_present(pte) || pte_special(pte) ||
  856. pte_protnone(pte) || (write && !pte_write(pte)))
  857. goto pte_unmap;
  858. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  859. page = pte_page(pte);
  860. if (!page_cache_get_speculative(page))
  861. goto pte_unmap;
  862. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  863. put_page(page);
  864. goto pte_unmap;
  865. }
  866. pages[*nr] = page;
  867. (*nr)++;
  868. } while (ptep++, addr += PAGE_SIZE, addr != end);
  869. ret = 1;
  870. pte_unmap:
  871. pte_unmap(ptem);
  872. return ret;
  873. }
  874. #else
  875. /*
  876. * If we can't determine whether or not a pte is special, then fail immediately
  877. * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
  878. * to be special.
  879. *
  880. * For a futex to be placed on a THP tail page, get_futex_key requires a
  881. * __get_user_pages_fast implementation that can pin pages. Thus it's still
  882. * useful to have gup_huge_pmd even if we can't operate on ptes.
  883. */
  884. static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  885. int write, struct page **pages, int *nr)
  886. {
  887. return 0;
  888. }
  889. #endif /* __HAVE_ARCH_PTE_SPECIAL */
  890. static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
  891. unsigned long end, int write, struct page **pages, int *nr)
  892. {
  893. struct page *head, *page, *tail;
  894. int refs;
  895. if (write && !pmd_write(orig))
  896. return 0;
  897. refs = 0;
  898. head = pmd_page(orig);
  899. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  900. tail = page;
  901. do {
  902. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  903. pages[*nr] = page;
  904. (*nr)++;
  905. page++;
  906. refs++;
  907. } while (addr += PAGE_SIZE, addr != end);
  908. if (!page_cache_add_speculative(head, refs)) {
  909. *nr -= refs;
  910. return 0;
  911. }
  912. if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
  913. *nr -= refs;
  914. while (refs--)
  915. put_page(head);
  916. return 0;
  917. }
  918. /*
  919. * Any tail pages need their mapcount reference taken before we
  920. * return. (This allows the THP code to bump their ref count when
  921. * they are split into base pages).
  922. */
  923. while (refs--) {
  924. if (PageTail(tail))
  925. get_huge_page_tail(tail);
  926. tail++;
  927. }
  928. return 1;
  929. }
  930. static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
  931. unsigned long end, int write, struct page **pages, int *nr)
  932. {
  933. struct page *head, *page, *tail;
  934. int refs;
  935. if (write && !pud_write(orig))
  936. return 0;
  937. refs = 0;
  938. head = pud_page(orig);
  939. page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  940. tail = page;
  941. do {
  942. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  943. pages[*nr] = page;
  944. (*nr)++;
  945. page++;
  946. refs++;
  947. } while (addr += PAGE_SIZE, addr != end);
  948. if (!page_cache_add_speculative(head, refs)) {
  949. *nr -= refs;
  950. return 0;
  951. }
  952. if (unlikely(pud_val(orig) != pud_val(*pudp))) {
  953. *nr -= refs;
  954. while (refs--)
  955. put_page(head);
  956. return 0;
  957. }
  958. while (refs--) {
  959. if (PageTail(tail))
  960. get_huge_page_tail(tail);
  961. tail++;
  962. }
  963. return 1;
  964. }
  965. static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
  966. unsigned long end, int write,
  967. struct page **pages, int *nr)
  968. {
  969. int refs;
  970. struct page *head, *page, *tail;
  971. if (write && !pgd_write(orig))
  972. return 0;
  973. refs = 0;
  974. head = pgd_page(orig);
  975. page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
  976. tail = page;
  977. do {
  978. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  979. pages[*nr] = page;
  980. (*nr)++;
  981. page++;
  982. refs++;
  983. } while (addr += PAGE_SIZE, addr != end);
  984. if (!page_cache_add_speculative(head, refs)) {
  985. *nr -= refs;
  986. return 0;
  987. }
  988. if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
  989. *nr -= refs;
  990. while (refs--)
  991. put_page(head);
  992. return 0;
  993. }
  994. while (refs--) {
  995. if (PageTail(tail))
  996. get_huge_page_tail(tail);
  997. tail++;
  998. }
  999. return 1;
  1000. }
  1001. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  1002. int write, struct page **pages, int *nr)
  1003. {
  1004. unsigned long next;
  1005. pmd_t *pmdp;
  1006. pmdp = pmd_offset(&pud, addr);
  1007. do {
  1008. pmd_t pmd = READ_ONCE(*pmdp);
  1009. next = pmd_addr_end(addr, end);
  1010. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  1011. return 0;
  1012. if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
  1013. /*
  1014. * NUMA hinting faults need to be handled in the GUP
  1015. * slowpath for accounting purposes and so that they
  1016. * can be serialised against THP migration.
  1017. */
  1018. if (pmd_protnone(pmd))
  1019. return 0;
  1020. if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
  1021. pages, nr))
  1022. return 0;
  1023. } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
  1024. /*
  1025. * architecture have different format for hugetlbfs
  1026. * pmd format and THP pmd format
  1027. */
  1028. if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
  1029. PMD_SHIFT, next, write, pages, nr))
  1030. return 0;
  1031. } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  1032. return 0;
  1033. } while (pmdp++, addr = next, addr != end);
  1034. return 1;
  1035. }
  1036. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  1037. int write, struct page **pages, int *nr)
  1038. {
  1039. unsigned long next;
  1040. pud_t *pudp;
  1041. pudp = pud_offset(&pgd, addr);
  1042. do {
  1043. pud_t pud = READ_ONCE(*pudp);
  1044. next = pud_addr_end(addr, end);
  1045. if (pud_none(pud))
  1046. return 0;
  1047. if (unlikely(pud_huge(pud))) {
  1048. if (!gup_huge_pud(pud, pudp, addr, next, write,
  1049. pages, nr))
  1050. return 0;
  1051. } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
  1052. if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
  1053. PUD_SHIFT, next, write, pages, nr))
  1054. return 0;
  1055. } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  1056. return 0;
  1057. } while (pudp++, addr = next, addr != end);
  1058. return 1;
  1059. }
  1060. /*
  1061. * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
  1062. * the regular GUP. It will only return non-negative values.
  1063. */
  1064. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  1065. struct page **pages)
  1066. {
  1067. struct mm_struct *mm = current->mm;
  1068. unsigned long addr, len, end;
  1069. unsigned long next, flags;
  1070. pgd_t *pgdp;
  1071. int nr = 0;
  1072. start &= PAGE_MASK;
  1073. addr = start;
  1074. len = (unsigned long) nr_pages << PAGE_SHIFT;
  1075. end = start + len;
  1076. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  1077. start, len)))
  1078. return 0;
  1079. /*
  1080. * Disable interrupts. We use the nested form as we can already have
  1081. * interrupts disabled by get_futex_key.
  1082. *
  1083. * With interrupts disabled, we block page table pages from being
  1084. * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
  1085. * for more details.
  1086. *
  1087. * We do not adopt an rcu_read_lock(.) here as we also want to
  1088. * block IPIs that come from THPs splitting.
  1089. */
  1090. local_irq_save(flags);
  1091. pgdp = pgd_offset(mm, addr);
  1092. do {
  1093. pgd_t pgd = ACCESS_ONCE(*pgdp);
  1094. next = pgd_addr_end(addr, end);
  1095. if (pgd_none(pgd))
  1096. break;
  1097. if (unlikely(pgd_huge(pgd))) {
  1098. if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
  1099. pages, &nr))
  1100. break;
  1101. } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
  1102. if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
  1103. PGDIR_SHIFT, next, write, pages, &nr))
  1104. break;
  1105. } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  1106. break;
  1107. } while (pgdp++, addr = next, addr != end);
  1108. local_irq_restore(flags);
  1109. return nr;
  1110. }
  1111. /**
  1112. * get_user_pages_fast() - pin user pages in memory
  1113. * @start: starting user address
  1114. * @nr_pages: number of pages from start to pin
  1115. * @write: whether pages will be written to
  1116. * @pages: array that receives pointers to the pages pinned.
  1117. * Should be at least nr_pages long.
  1118. *
  1119. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  1120. * If not successful, it will fall back to taking the lock and
  1121. * calling get_user_pages().
  1122. *
  1123. * Returns number of pages pinned. This may be fewer than the number
  1124. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  1125. * were pinned, returns -errno.
  1126. */
  1127. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  1128. struct page **pages)
  1129. {
  1130. struct mm_struct *mm = current->mm;
  1131. int nr, ret;
  1132. start &= PAGE_MASK;
  1133. nr = __get_user_pages_fast(start, nr_pages, write, pages);
  1134. ret = nr;
  1135. if (nr < nr_pages) {
  1136. /* Try to get the remaining pages with get_user_pages */
  1137. start += nr << PAGE_SHIFT;
  1138. pages += nr;
  1139. ret = get_user_pages_unlocked(current, mm, start,
  1140. nr_pages - nr, write, 0, pages);
  1141. /* Have to be a bit careful with return values */
  1142. if (nr > 0) {
  1143. if (ret < 0)
  1144. ret = nr;
  1145. else
  1146. ret += nr;
  1147. }
  1148. }
  1149. return ret;
  1150. }
  1151. #endif /* CONFIG_HAVE_GENERIC_RCU_GUP */