gup.c 44 KB

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