gup.c 42 KB

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