gup.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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. if (pmd_none(*pmd))
  265. return no_page_table(vma, flags);
  266. }
  267. return ret ? ERR_PTR(ret) :
  268. follow_page_pte(vma, address, pmd, flags);
  269. }
  270. page = follow_trans_huge_pmd(vma, address, pmd, flags);
  271. spin_unlock(ptl);
  272. *page_mask = HPAGE_PMD_NR - 1;
  273. return page;
  274. }
  275. static int get_gate_page(struct mm_struct *mm, unsigned long address,
  276. unsigned int gup_flags, struct vm_area_struct **vma,
  277. struct page **page)
  278. {
  279. pgd_t *pgd;
  280. pud_t *pud;
  281. pmd_t *pmd;
  282. pte_t *pte;
  283. int ret = -EFAULT;
  284. /* user gate pages are read-only */
  285. if (gup_flags & FOLL_WRITE)
  286. return -EFAULT;
  287. if (address > TASK_SIZE)
  288. pgd = pgd_offset_k(address);
  289. else
  290. pgd = pgd_offset_gate(mm, address);
  291. BUG_ON(pgd_none(*pgd));
  292. pud = pud_offset(pgd, address);
  293. BUG_ON(pud_none(*pud));
  294. pmd = pmd_offset(pud, address);
  295. if (pmd_none(*pmd))
  296. return -EFAULT;
  297. VM_BUG_ON(pmd_trans_huge(*pmd));
  298. pte = pte_offset_map(pmd, address);
  299. if (pte_none(*pte))
  300. goto unmap;
  301. *vma = get_gate_vma(mm);
  302. if (!page)
  303. goto out;
  304. *page = vm_normal_page(*vma, address, *pte);
  305. if (!*page) {
  306. if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
  307. goto unmap;
  308. *page = pte_page(*pte);
  309. }
  310. get_page(*page);
  311. out:
  312. ret = 0;
  313. unmap:
  314. pte_unmap(pte);
  315. return ret;
  316. }
  317. /*
  318. * mmap_sem must be held on entry. If @nonblocking != NULL and
  319. * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
  320. * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
  321. */
  322. static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
  323. unsigned long address, unsigned int *flags, int *nonblocking)
  324. {
  325. unsigned int fault_flags = 0;
  326. int ret;
  327. /* mlock all present pages, but do not fault in new pages */
  328. if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK)
  329. return -ENOENT;
  330. /* For mm_populate(), just skip the stack guard page. */
  331. if ((*flags & FOLL_POPULATE) &&
  332. (stack_guard_page_start(vma, address) ||
  333. stack_guard_page_end(vma, address + PAGE_SIZE)))
  334. return -ENOENT;
  335. if (*flags & FOLL_WRITE)
  336. fault_flags |= FAULT_FLAG_WRITE;
  337. if (*flags & FOLL_REMOTE)
  338. fault_flags |= FAULT_FLAG_REMOTE;
  339. if (nonblocking)
  340. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  341. if (*flags & FOLL_NOWAIT)
  342. fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
  343. if (*flags & FOLL_TRIED) {
  344. VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
  345. fault_flags |= FAULT_FLAG_TRIED;
  346. }
  347. ret = handle_mm_fault(vma, address, fault_flags);
  348. if (ret & VM_FAULT_ERROR) {
  349. if (ret & VM_FAULT_OOM)
  350. return -ENOMEM;
  351. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  352. return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
  353. if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  354. return -EFAULT;
  355. BUG();
  356. }
  357. if (tsk) {
  358. if (ret & VM_FAULT_MAJOR)
  359. tsk->maj_flt++;
  360. else
  361. tsk->min_flt++;
  362. }
  363. if (ret & VM_FAULT_RETRY) {
  364. if (nonblocking)
  365. *nonblocking = 0;
  366. return -EBUSY;
  367. }
  368. /*
  369. * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
  370. * necessary, even if maybe_mkwrite decided not to set pte_write. We
  371. * can thus safely do subsequent page lookups as if they were reads.
  372. * But only do so when looping for pte_write is futile: in some cases
  373. * userspace may also be wanting to write to the gotten user page,
  374. * which a read fault here might prevent (a readonly page might get
  375. * reCOWed by userspace write).
  376. */
  377. if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
  378. *flags &= ~FOLL_WRITE;
  379. return 0;
  380. }
  381. static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
  382. {
  383. vm_flags_t vm_flags = vma->vm_flags;
  384. int write = (gup_flags & FOLL_WRITE);
  385. int foreign = (gup_flags & FOLL_REMOTE);
  386. if (vm_flags & (VM_IO | VM_PFNMAP))
  387. return -EFAULT;
  388. if (write) {
  389. if (!(vm_flags & VM_WRITE)) {
  390. if (!(gup_flags & FOLL_FORCE))
  391. return -EFAULT;
  392. /*
  393. * We used to let the write,force case do COW in a
  394. * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
  395. * set a breakpoint in a read-only mapping of an
  396. * executable, without corrupting the file (yet only
  397. * when that file had been opened for writing!).
  398. * Anon pages in shared mappings are surprising: now
  399. * just reject it.
  400. */
  401. if (!is_cow_mapping(vm_flags))
  402. return -EFAULT;
  403. }
  404. } else if (!(vm_flags & VM_READ)) {
  405. if (!(gup_flags & FOLL_FORCE))
  406. return -EFAULT;
  407. /*
  408. * Is there actually any vma we can reach here which does not
  409. * have VM_MAYREAD set?
  410. */
  411. if (!(vm_flags & VM_MAYREAD))
  412. return -EFAULT;
  413. }
  414. /*
  415. * gups are always data accesses, not instruction
  416. * fetches, so execute=false here
  417. */
  418. if (!arch_vma_access_permitted(vma, write, false, foreign))
  419. return -EFAULT;
  420. return 0;
  421. }
  422. /**
  423. * __get_user_pages() - pin user pages in memory
  424. * @tsk: task_struct of target task
  425. * @mm: mm_struct of target mm
  426. * @start: starting user address
  427. * @nr_pages: number of pages from start to pin
  428. * @gup_flags: flags modifying pin behaviour
  429. * @pages: array that receives pointers to the pages pinned.
  430. * Should be at least nr_pages long. Or NULL, if caller
  431. * only intends to ensure the pages are faulted in.
  432. * @vmas: array of pointers to vmas corresponding to each page.
  433. * Or NULL if the caller does not require them.
  434. * @nonblocking: whether waiting for disk IO or mmap_sem contention
  435. *
  436. * Returns number of pages pinned. This may be fewer than the number
  437. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  438. * were pinned, returns -errno. Each page returned must be released
  439. * with a put_page() call when it is finished with. vmas will only
  440. * remain valid while mmap_sem is held.
  441. *
  442. * Must be called with mmap_sem held. It may be released. See below.
  443. *
  444. * __get_user_pages walks a process's page tables and takes a reference to
  445. * each struct page that each user address corresponds to at a given
  446. * instant. That is, it takes the page that would be accessed if a user
  447. * thread accesses the given user virtual address at that instant.
  448. *
  449. * This does not guarantee that the page exists in the user mappings when
  450. * __get_user_pages returns, and there may even be a completely different
  451. * page there in some cases (eg. if mmapped pagecache has been invalidated
  452. * and subsequently re faulted). However it does guarantee that the page
  453. * won't be freed completely. And mostly callers simply care that the page
  454. * contains data that was valid *at some point in time*. Typically, an IO
  455. * or similar operation cannot guarantee anything stronger anyway because
  456. * locks can't be held over the syscall boundary.
  457. *
  458. * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
  459. * the page is written to, set_page_dirty (or set_page_dirty_lock, as
  460. * appropriate) must be called after the page is finished with, and
  461. * before put_page is called.
  462. *
  463. * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
  464. * or mmap_sem contention, and if waiting is needed to pin all pages,
  465. * *@nonblocking will be set to 0. Further, if @gup_flags does not
  466. * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
  467. * this case.
  468. *
  469. * A caller using such a combination of @nonblocking and @gup_flags
  470. * must therefore hold the mmap_sem for reading only, and recognize
  471. * when it's been released. Otherwise, it must be held for either
  472. * reading or writing and will not be released.
  473. *
  474. * In most cases, get_user_pages or get_user_pages_fast should be used
  475. * instead of __get_user_pages. __get_user_pages should be used only if
  476. * you need some special @gup_flags.
  477. */
  478. long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  479. unsigned long start, unsigned long nr_pages,
  480. unsigned int gup_flags, struct page **pages,
  481. struct vm_area_struct **vmas, int *nonblocking)
  482. {
  483. long i = 0;
  484. unsigned int page_mask;
  485. struct vm_area_struct *vma = NULL;
  486. if (!nr_pages)
  487. return 0;
  488. VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
  489. /*
  490. * If FOLL_FORCE is set then do not force a full fault as the hinting
  491. * fault information is unrelated to the reference behaviour of a task
  492. * using the address space
  493. */
  494. if (!(gup_flags & FOLL_FORCE))
  495. gup_flags |= FOLL_NUMA;
  496. do {
  497. struct page *page;
  498. unsigned int foll_flags = gup_flags;
  499. unsigned int page_increm;
  500. /* first iteration or cross vma bound */
  501. if (!vma || start >= vma->vm_end) {
  502. vma = find_extend_vma(mm, start);
  503. if (!vma && in_gate_area(mm, start)) {
  504. int ret;
  505. ret = get_gate_page(mm, start & PAGE_MASK,
  506. gup_flags, &vma,
  507. pages ? &pages[i] : NULL);
  508. if (ret)
  509. return i ? : ret;
  510. page_mask = 0;
  511. goto next_page;
  512. }
  513. if (!vma || check_vma_flags(vma, gup_flags))
  514. return i ? : -EFAULT;
  515. if (is_vm_hugetlb_page(vma)) {
  516. i = follow_hugetlb_page(mm, vma, pages, vmas,
  517. &start, &nr_pages, i,
  518. gup_flags);
  519. continue;
  520. }
  521. }
  522. retry:
  523. /*
  524. * If we have a pending SIGKILL, don't keep faulting pages and
  525. * potentially allocating memory.
  526. */
  527. if (unlikely(fatal_signal_pending(current)))
  528. return i ? i : -ERESTARTSYS;
  529. cond_resched();
  530. page = follow_page_mask(vma, start, foll_flags, &page_mask);
  531. if (!page) {
  532. int ret;
  533. ret = faultin_page(tsk, vma, start, &foll_flags,
  534. nonblocking);
  535. switch (ret) {
  536. case 0:
  537. goto retry;
  538. case -EFAULT:
  539. case -ENOMEM:
  540. case -EHWPOISON:
  541. return i ? i : ret;
  542. case -EBUSY:
  543. return i;
  544. case -ENOENT:
  545. goto next_page;
  546. }
  547. BUG();
  548. } else if (PTR_ERR(page) == -EEXIST) {
  549. /*
  550. * Proper page table entry exists, but no corresponding
  551. * struct page.
  552. */
  553. goto next_page;
  554. } else if (IS_ERR(page)) {
  555. return i ? i : PTR_ERR(page);
  556. }
  557. if (pages) {
  558. pages[i] = page;
  559. flush_anon_page(vma, page, start);
  560. flush_dcache_page(page);
  561. page_mask = 0;
  562. }
  563. next_page:
  564. if (vmas) {
  565. vmas[i] = vma;
  566. page_mask = 0;
  567. }
  568. page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
  569. if (page_increm > nr_pages)
  570. page_increm = nr_pages;
  571. i += page_increm;
  572. start += page_increm * PAGE_SIZE;
  573. nr_pages -= page_increm;
  574. } while (nr_pages);
  575. return i;
  576. }
  577. EXPORT_SYMBOL(__get_user_pages);
  578. bool vma_permits_fault(struct vm_area_struct *vma, unsigned int fault_flags)
  579. {
  580. bool write = !!(fault_flags & FAULT_FLAG_WRITE);
  581. bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE);
  582. vm_flags_t vm_flags = write ? VM_WRITE : VM_READ;
  583. if (!(vm_flags & vma->vm_flags))
  584. return false;
  585. /*
  586. * The architecture might have a hardware protection
  587. * mechanism other than read/write that can deny access.
  588. *
  589. * gup always represents data access, not instruction
  590. * fetches, so execute=false here:
  591. */
  592. if (!arch_vma_access_permitted(vma, write, false, foreign))
  593. return false;
  594. return true;
  595. }
  596. /*
  597. * fixup_user_fault() - manually resolve a user page fault
  598. * @tsk: the task_struct to use for page fault accounting, or
  599. * NULL if faults are not to be recorded.
  600. * @mm: mm_struct of target mm
  601. * @address: user address
  602. * @fault_flags:flags to pass down to handle_mm_fault()
  603. * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller
  604. * does not allow retry
  605. *
  606. * This is meant to be called in the specific scenario where for locking reasons
  607. * we try to access user memory in atomic context (within a pagefault_disable()
  608. * section), this returns -EFAULT, and we want to resolve the user fault before
  609. * trying again.
  610. *
  611. * Typically this is meant to be used by the futex code.
  612. *
  613. * The main difference with get_user_pages() is that this function will
  614. * unconditionally call handle_mm_fault() which will in turn perform all the
  615. * necessary SW fixup of the dirty and young bits in the PTE, while
  616. * get_user_pages() only guarantees to update these in the struct page.
  617. *
  618. * This is important for some architectures where those bits also gate the
  619. * access permission to the page because they are maintained in software. On
  620. * such architectures, gup() will not be enough to make a subsequent access
  621. * succeed.
  622. *
  623. * This function will not return with an unlocked mmap_sem. So it has not the
  624. * same semantics wrt the @mm->mmap_sem as does filemap_fault().
  625. */
  626. int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  627. unsigned long address, unsigned int fault_flags,
  628. bool *unlocked)
  629. {
  630. struct vm_area_struct *vma;
  631. int ret, major = 0;
  632. if (unlocked)
  633. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  634. retry:
  635. vma = find_extend_vma(mm, address);
  636. if (!vma || address < vma->vm_start)
  637. return -EFAULT;
  638. if (!vma_permits_fault(vma, fault_flags))
  639. return -EFAULT;
  640. ret = handle_mm_fault(vma, address, fault_flags);
  641. major |= ret & VM_FAULT_MAJOR;
  642. if (ret & VM_FAULT_ERROR) {
  643. if (ret & VM_FAULT_OOM)
  644. return -ENOMEM;
  645. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  646. return -EHWPOISON;
  647. if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  648. return -EFAULT;
  649. BUG();
  650. }
  651. if (ret & VM_FAULT_RETRY) {
  652. down_read(&mm->mmap_sem);
  653. if (!(fault_flags & FAULT_FLAG_TRIED)) {
  654. *unlocked = true;
  655. fault_flags &= ~FAULT_FLAG_ALLOW_RETRY;
  656. fault_flags |= FAULT_FLAG_TRIED;
  657. goto retry;
  658. }
  659. }
  660. if (tsk) {
  661. if (major)
  662. tsk->maj_flt++;
  663. else
  664. tsk->min_flt++;
  665. }
  666. return 0;
  667. }
  668. EXPORT_SYMBOL_GPL(fixup_user_fault);
  669. static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
  670. struct mm_struct *mm,
  671. unsigned long start,
  672. unsigned long nr_pages,
  673. int write, int force,
  674. struct page **pages,
  675. struct vm_area_struct **vmas,
  676. int *locked, bool notify_drop,
  677. unsigned int flags)
  678. {
  679. long ret, pages_done;
  680. bool lock_dropped;
  681. if (locked) {
  682. /* if VM_FAULT_RETRY can be returned, vmas become invalid */
  683. BUG_ON(vmas);
  684. /* check caller initialized locked */
  685. BUG_ON(*locked != 1);
  686. }
  687. if (pages)
  688. flags |= FOLL_GET;
  689. if (write)
  690. flags |= FOLL_WRITE;
  691. if (force)
  692. flags |= FOLL_FORCE;
  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. int write, int force, struct page **pages,
  780. int *locked)
  781. {
  782. return __get_user_pages_locked(current, current->mm, start, nr_pages,
  783. write, force, pages, NULL, locked, true,
  784. 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. int write, int force, struct page **pages,
  800. unsigned int gup_flags)
  801. {
  802. long ret;
  803. int locked = 1;
  804. down_read(&mm->mmap_sem);
  805. ret = __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
  806. pages, NULL, &locked, false, gup_flags);
  807. if (locked)
  808. up_read(&mm->mmap_sem);
  809. return ret;
  810. }
  811. EXPORT_SYMBOL(__get_user_pages_unlocked);
  812. /*
  813. * get_user_pages_unlocked() is suitable to replace the form:
  814. *
  815. * down_read(&mm->mmap_sem);
  816. * get_user_pages(tsk, mm, ..., pages, NULL);
  817. * up_read(&mm->mmap_sem);
  818. *
  819. * with:
  820. *
  821. * get_user_pages_unlocked(tsk, mm, ..., pages);
  822. *
  823. * It is functionally equivalent to get_user_pages_fast so
  824. * get_user_pages_fast should be used instead, if the two parameters
  825. * "tsk" and "mm" are respectively equal to current and current->mm,
  826. * or if "force" shall be set to 1 (get_user_pages_fast misses the
  827. * "force" parameter).
  828. */
  829. long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
  830. int write, int force, struct page **pages)
  831. {
  832. return __get_user_pages_unlocked(current, current->mm, start, nr_pages,
  833. write, force, pages, FOLL_TOUCH);
  834. }
  835. EXPORT_SYMBOL(get_user_pages_unlocked);
  836. /*
  837. * get_user_pages_remote() - pin user pages in memory
  838. * @tsk: the task_struct to use for page fault accounting, or
  839. * NULL if faults are not to be recorded.
  840. * @mm: mm_struct of target mm
  841. * @start: starting user address
  842. * @nr_pages: number of pages from start to pin
  843. * @write: whether pages will be written to by the caller
  844. * @force: whether to force access even when user mapping is currently
  845. * protected (but never forces write access to shared mapping).
  846. * @pages: array that receives pointers to the pages pinned.
  847. * Should be at least nr_pages long. Or NULL, if caller
  848. * only intends to ensure the pages are faulted in.
  849. * @vmas: array of pointers to vmas corresponding to each page.
  850. * Or NULL if the caller does not require them.
  851. *
  852. * Returns number of pages pinned. This may be fewer than the number
  853. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  854. * were pinned, returns -errno. Each page returned must be released
  855. * with a put_page() call when it is finished with. vmas will only
  856. * remain valid while mmap_sem is held.
  857. *
  858. * Must be called with mmap_sem held for read or write.
  859. *
  860. * get_user_pages walks a process's page tables and takes a reference to
  861. * each struct page that each user address corresponds to at a given
  862. * instant. That is, it takes the page that would be accessed if a user
  863. * thread accesses the given user virtual address at that instant.
  864. *
  865. * This does not guarantee that the page exists in the user mappings when
  866. * get_user_pages returns, and there may even be a completely different
  867. * page there in some cases (eg. if mmapped pagecache has been invalidated
  868. * and subsequently re faulted). However it does guarantee that the page
  869. * won't be freed completely. And mostly callers simply care that the page
  870. * contains data that was valid *at some point in time*. Typically, an IO
  871. * or similar operation cannot guarantee anything stronger anyway because
  872. * locks can't be held over the syscall boundary.
  873. *
  874. * If write=0, the page must not be written to. If the page is written to,
  875. * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
  876. * after the page is finished with, and before put_page is called.
  877. *
  878. * get_user_pages is typically used for fewer-copy IO operations, to get a
  879. * handle on the memory by some means other than accesses via the user virtual
  880. * addresses. The pages may be submitted for DMA to devices or accessed via
  881. * their kernel linear mapping (via the kmap APIs). Care should be taken to
  882. * use the correct cache flushing APIs.
  883. *
  884. * See also get_user_pages_fast, for performance critical applications.
  885. *
  886. * get_user_pages should be phased out in favor of
  887. * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
  888. * should use get_user_pages because it cannot pass
  889. * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
  890. */
  891. long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
  892. unsigned long start, unsigned long nr_pages,
  893. int write, int force, struct page **pages,
  894. struct vm_area_struct **vmas)
  895. {
  896. return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
  897. pages, vmas, NULL, false,
  898. FOLL_TOUCH | FOLL_REMOTE);
  899. }
  900. EXPORT_SYMBOL(get_user_pages_remote);
  901. /*
  902. * This is the same as get_user_pages_remote(), just with a
  903. * less-flexible calling convention where we assume that the task
  904. * and mm being operated on are the current task's. We also
  905. * obviously don't pass FOLL_REMOTE in here.
  906. */
  907. long get_user_pages(unsigned long start, unsigned long nr_pages,
  908. int write, int force, struct page **pages,
  909. struct vm_area_struct **vmas)
  910. {
  911. return __get_user_pages_locked(current, current->mm, start, nr_pages,
  912. write, force, pages, vmas, NULL, false,
  913. FOLL_TOUCH);
  914. }
  915. EXPORT_SYMBOL(get_user_pages);
  916. /**
  917. * populate_vma_page_range() - populate a range of pages in the vma.
  918. * @vma: target vma
  919. * @start: start address
  920. * @end: end address
  921. * @nonblocking:
  922. *
  923. * This takes care of mlocking the pages too if VM_LOCKED is set.
  924. *
  925. * return 0 on success, negative error code on error.
  926. *
  927. * vma->vm_mm->mmap_sem must be held.
  928. *
  929. * If @nonblocking is NULL, it may be held for read or write and will
  930. * be unperturbed.
  931. *
  932. * If @nonblocking is non-NULL, it must held for read only and may be
  933. * released. If it's released, *@nonblocking will be set to 0.
  934. */
  935. long populate_vma_page_range(struct vm_area_struct *vma,
  936. unsigned long start, unsigned long end, int *nonblocking)
  937. {
  938. struct mm_struct *mm = vma->vm_mm;
  939. unsigned long nr_pages = (end - start) / PAGE_SIZE;
  940. int gup_flags;
  941. VM_BUG_ON(start & ~PAGE_MASK);
  942. VM_BUG_ON(end & ~PAGE_MASK);
  943. VM_BUG_ON_VMA(start < vma->vm_start, vma);
  944. VM_BUG_ON_VMA(end > vma->vm_end, vma);
  945. VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
  946. gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK;
  947. if (vma->vm_flags & VM_LOCKONFAULT)
  948. gup_flags &= ~FOLL_POPULATE;
  949. /*
  950. * We want to touch writable mappings with a write fault in order
  951. * to break COW, except for shared mappings because these don't COW
  952. * and we would not want to dirty them for nothing.
  953. */
  954. if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
  955. gup_flags |= FOLL_WRITE;
  956. /*
  957. * We want mlock to succeed for regions that have any permissions
  958. * other than PROT_NONE.
  959. */
  960. if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
  961. gup_flags |= FOLL_FORCE;
  962. /*
  963. * We made sure addr is within a VMA, so the following will
  964. * not result in a stack expansion that recurses back here.
  965. */
  966. return __get_user_pages(current, mm, start, nr_pages, gup_flags,
  967. NULL, NULL, nonblocking);
  968. }
  969. /*
  970. * __mm_populate - populate and/or mlock pages within a range of address space.
  971. *
  972. * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
  973. * flags. VMAs must be already marked with the desired vm_flags, and
  974. * mmap_sem must not be held.
  975. */
  976. int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
  977. {
  978. struct mm_struct *mm = current->mm;
  979. unsigned long end, nstart, nend;
  980. struct vm_area_struct *vma = NULL;
  981. int locked = 0;
  982. long ret = 0;
  983. VM_BUG_ON(start & ~PAGE_MASK);
  984. VM_BUG_ON(len != PAGE_ALIGN(len));
  985. end = start + len;
  986. for (nstart = start; nstart < end; nstart = nend) {
  987. /*
  988. * We want to fault in pages for [nstart; end) address range.
  989. * Find first corresponding VMA.
  990. */
  991. if (!locked) {
  992. locked = 1;
  993. down_read(&mm->mmap_sem);
  994. vma = find_vma(mm, nstart);
  995. } else if (nstart >= vma->vm_end)
  996. vma = vma->vm_next;
  997. if (!vma || vma->vm_start >= end)
  998. break;
  999. /*
  1000. * Set [nstart; nend) to intersection of desired address
  1001. * range with the first VMA. Also, skip undesirable VMA types.
  1002. */
  1003. nend = min(end, vma->vm_end);
  1004. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  1005. continue;
  1006. if (nstart < vma->vm_start)
  1007. nstart = vma->vm_start;
  1008. /*
  1009. * Now fault in a range of pages. populate_vma_page_range()
  1010. * double checks the vma flags, so that it won't mlock pages
  1011. * if the vma was already munlocked.
  1012. */
  1013. ret = populate_vma_page_range(vma, nstart, nend, &locked);
  1014. if (ret < 0) {
  1015. if (ignore_errors) {
  1016. ret = 0;
  1017. continue; /* continue at next VMA */
  1018. }
  1019. break;
  1020. }
  1021. nend = nstart + ret * PAGE_SIZE;
  1022. ret = 0;
  1023. }
  1024. if (locked)
  1025. up_read(&mm->mmap_sem);
  1026. return ret; /* 0 or negative error code */
  1027. }
  1028. /**
  1029. * get_dump_page() - pin user page in memory while writing it to core dump
  1030. * @addr: user address
  1031. *
  1032. * Returns struct page pointer of user page pinned for dump,
  1033. * to be freed afterwards by put_page().
  1034. *
  1035. * Returns NULL on any kind of failure - a hole must then be inserted into
  1036. * the corefile, to preserve alignment with its headers; and also returns
  1037. * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
  1038. * allowing a hole to be left in the corefile to save diskspace.
  1039. *
  1040. * Called without mmap_sem, but after all other threads have been killed.
  1041. */
  1042. #ifdef CONFIG_ELF_CORE
  1043. struct page *get_dump_page(unsigned long addr)
  1044. {
  1045. struct vm_area_struct *vma;
  1046. struct page *page;
  1047. if (__get_user_pages(current, current->mm, addr, 1,
  1048. FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
  1049. NULL) < 1)
  1050. return NULL;
  1051. flush_cache_page(vma, addr, page_to_pfn(page));
  1052. return page;
  1053. }
  1054. #endif /* CONFIG_ELF_CORE */
  1055. /*
  1056. * Generic RCU Fast GUP
  1057. *
  1058. * get_user_pages_fast attempts to pin user pages by walking the page
  1059. * tables directly and avoids taking locks. Thus the walker needs to be
  1060. * protected from page table pages being freed from under it, and should
  1061. * block any THP splits.
  1062. *
  1063. * One way to achieve this is to have the walker disable interrupts, and
  1064. * rely on IPIs from the TLB flushing code blocking before the page table
  1065. * pages are freed. This is unsuitable for architectures that do not need
  1066. * to broadcast an IPI when invalidating TLBs.
  1067. *
  1068. * Another way to achieve this is to batch up page table containing pages
  1069. * belonging to more than one mm_user, then rcu_sched a callback to free those
  1070. * pages. Disabling interrupts will allow the fast_gup walker to both block
  1071. * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
  1072. * (which is a relatively rare event). The code below adopts this strategy.
  1073. *
  1074. * Before activating this code, please be aware that the following assumptions
  1075. * are currently made:
  1076. *
  1077. * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
  1078. * pages containing page tables.
  1079. *
  1080. * *) ptes can be read atomically by the architecture.
  1081. *
  1082. * *) access_ok is sufficient to validate userspace address ranges.
  1083. *
  1084. * The last two assumptions can be relaxed by the addition of helper functions.
  1085. *
  1086. * This code is based heavily on the PowerPC implementation by Nick Piggin.
  1087. */
  1088. #ifdef CONFIG_HAVE_GENERIC_RCU_GUP
  1089. #ifdef __HAVE_ARCH_PTE_SPECIAL
  1090. static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  1091. int write, struct page **pages, int *nr)
  1092. {
  1093. pte_t *ptep, *ptem;
  1094. int ret = 0;
  1095. ptem = ptep = pte_offset_map(&pmd, addr);
  1096. do {
  1097. /*
  1098. * In the line below we are assuming that the pte can be read
  1099. * atomically. If this is not the case for your architecture,
  1100. * please wrap this in a helper function!
  1101. *
  1102. * for an example see gup_get_pte in arch/x86/mm/gup.c
  1103. */
  1104. pte_t pte = READ_ONCE(*ptep);
  1105. struct page *head, *page;
  1106. /*
  1107. * Similar to the PMD case below, NUMA hinting must take slow
  1108. * path using the pte_protnone check.
  1109. */
  1110. if (!pte_present(pte) || pte_special(pte) ||
  1111. pte_protnone(pte) || (write && !pte_write(pte)))
  1112. goto pte_unmap;
  1113. if (!arch_pte_access_permitted(pte, write))
  1114. goto pte_unmap;
  1115. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  1116. page = pte_page(pte);
  1117. head = compound_head(page);
  1118. if (!page_cache_get_speculative(head))
  1119. goto pte_unmap;
  1120. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  1121. put_page(head);
  1122. goto pte_unmap;
  1123. }
  1124. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  1125. pages[*nr] = page;
  1126. (*nr)++;
  1127. } while (ptep++, addr += PAGE_SIZE, addr != end);
  1128. ret = 1;
  1129. pte_unmap:
  1130. pte_unmap(ptem);
  1131. return ret;
  1132. }
  1133. #else
  1134. /*
  1135. * If we can't determine whether or not a pte is special, then fail immediately
  1136. * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
  1137. * to be special.
  1138. *
  1139. * For a futex to be placed on a THP tail page, get_futex_key requires a
  1140. * __get_user_pages_fast implementation that can pin pages. Thus it's still
  1141. * useful to have gup_huge_pmd even if we can't operate on ptes.
  1142. */
  1143. static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  1144. int write, struct page **pages, int *nr)
  1145. {
  1146. return 0;
  1147. }
  1148. #endif /* __HAVE_ARCH_PTE_SPECIAL */
  1149. static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
  1150. unsigned long end, int write, struct page **pages, int *nr)
  1151. {
  1152. struct page *head, *page;
  1153. int refs;
  1154. if (write && !pmd_write(orig))
  1155. return 0;
  1156. refs = 0;
  1157. head = pmd_page(orig);
  1158. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  1159. do {
  1160. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  1161. pages[*nr] = page;
  1162. (*nr)++;
  1163. page++;
  1164. refs++;
  1165. } while (addr += PAGE_SIZE, addr != end);
  1166. if (!page_cache_add_speculative(head, refs)) {
  1167. *nr -= refs;
  1168. return 0;
  1169. }
  1170. if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
  1171. *nr -= refs;
  1172. while (refs--)
  1173. put_page(head);
  1174. return 0;
  1175. }
  1176. return 1;
  1177. }
  1178. static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
  1179. unsigned long end, int write, struct page **pages, int *nr)
  1180. {
  1181. struct page *head, *page;
  1182. int refs;
  1183. if (write && !pud_write(orig))
  1184. return 0;
  1185. refs = 0;
  1186. head = pud_page(orig);
  1187. page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  1188. do {
  1189. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  1190. pages[*nr] = page;
  1191. (*nr)++;
  1192. page++;
  1193. refs++;
  1194. } while (addr += PAGE_SIZE, addr != end);
  1195. if (!page_cache_add_speculative(head, refs)) {
  1196. *nr -= refs;
  1197. return 0;
  1198. }
  1199. if (unlikely(pud_val(orig) != pud_val(*pudp))) {
  1200. *nr -= refs;
  1201. while (refs--)
  1202. put_page(head);
  1203. return 0;
  1204. }
  1205. return 1;
  1206. }
  1207. static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
  1208. unsigned long end, int write,
  1209. struct page **pages, int *nr)
  1210. {
  1211. int refs;
  1212. struct page *head, *page;
  1213. if (write && !pgd_write(orig))
  1214. return 0;
  1215. refs = 0;
  1216. head = pgd_page(orig);
  1217. page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
  1218. do {
  1219. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  1220. pages[*nr] = page;
  1221. (*nr)++;
  1222. page++;
  1223. refs++;
  1224. } while (addr += PAGE_SIZE, addr != end);
  1225. if (!page_cache_add_speculative(head, refs)) {
  1226. *nr -= refs;
  1227. return 0;
  1228. }
  1229. if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
  1230. *nr -= refs;
  1231. while (refs--)
  1232. put_page(head);
  1233. return 0;
  1234. }
  1235. return 1;
  1236. }
  1237. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  1238. int write, struct page **pages, int *nr)
  1239. {
  1240. unsigned long next;
  1241. pmd_t *pmdp;
  1242. pmdp = pmd_offset(&pud, addr);
  1243. do {
  1244. pmd_t pmd = READ_ONCE(*pmdp);
  1245. next = pmd_addr_end(addr, end);
  1246. if (pmd_none(pmd))
  1247. return 0;
  1248. if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
  1249. /*
  1250. * NUMA hinting faults need to be handled in the GUP
  1251. * slowpath for accounting purposes and so that they
  1252. * can be serialised against THP migration.
  1253. */
  1254. if (pmd_protnone(pmd))
  1255. return 0;
  1256. if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
  1257. pages, nr))
  1258. return 0;
  1259. } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
  1260. /*
  1261. * architecture have different format for hugetlbfs
  1262. * pmd format and THP pmd format
  1263. */
  1264. if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
  1265. PMD_SHIFT, next, write, pages, nr))
  1266. return 0;
  1267. } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  1268. return 0;
  1269. } while (pmdp++, addr = next, addr != end);
  1270. return 1;
  1271. }
  1272. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  1273. int write, struct page **pages, int *nr)
  1274. {
  1275. unsigned long next;
  1276. pud_t *pudp;
  1277. pudp = pud_offset(&pgd, addr);
  1278. do {
  1279. pud_t pud = READ_ONCE(*pudp);
  1280. next = pud_addr_end(addr, end);
  1281. if (pud_none(pud))
  1282. return 0;
  1283. if (unlikely(pud_huge(pud))) {
  1284. if (!gup_huge_pud(pud, pudp, addr, next, write,
  1285. pages, nr))
  1286. return 0;
  1287. } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
  1288. if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
  1289. PUD_SHIFT, next, write, pages, nr))
  1290. return 0;
  1291. } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  1292. return 0;
  1293. } while (pudp++, addr = next, addr != end);
  1294. return 1;
  1295. }
  1296. /*
  1297. * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
  1298. * the regular GUP. It will only return non-negative values.
  1299. */
  1300. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  1301. struct page **pages)
  1302. {
  1303. struct mm_struct *mm = current->mm;
  1304. unsigned long addr, len, end;
  1305. unsigned long next, flags;
  1306. pgd_t *pgdp;
  1307. int nr = 0;
  1308. start &= PAGE_MASK;
  1309. addr = start;
  1310. len = (unsigned long) nr_pages << PAGE_SHIFT;
  1311. end = start + len;
  1312. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  1313. start, len)))
  1314. return 0;
  1315. /*
  1316. * Disable interrupts. We use the nested form as we can already have
  1317. * interrupts disabled by get_futex_key.
  1318. *
  1319. * With interrupts disabled, we block page table pages from being
  1320. * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
  1321. * for more details.
  1322. *
  1323. * We do not adopt an rcu_read_lock(.) here as we also want to
  1324. * block IPIs that come from THPs splitting.
  1325. */
  1326. local_irq_save(flags);
  1327. pgdp = pgd_offset(mm, addr);
  1328. do {
  1329. pgd_t pgd = READ_ONCE(*pgdp);
  1330. next = pgd_addr_end(addr, end);
  1331. if (pgd_none(pgd))
  1332. break;
  1333. if (unlikely(pgd_huge(pgd))) {
  1334. if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
  1335. pages, &nr))
  1336. break;
  1337. } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
  1338. if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
  1339. PGDIR_SHIFT, next, write, pages, &nr))
  1340. break;
  1341. } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  1342. break;
  1343. } while (pgdp++, addr = next, addr != end);
  1344. local_irq_restore(flags);
  1345. return nr;
  1346. }
  1347. /**
  1348. * get_user_pages_fast() - pin user pages in memory
  1349. * @start: starting user address
  1350. * @nr_pages: number of pages from start to pin
  1351. * @write: whether pages will be written to
  1352. * @pages: array that receives pointers to the pages pinned.
  1353. * Should be at least nr_pages long.
  1354. *
  1355. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  1356. * If not successful, it will fall back to taking the lock and
  1357. * calling get_user_pages().
  1358. *
  1359. * Returns number of pages pinned. This may be fewer than the number
  1360. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  1361. * were pinned, returns -errno.
  1362. */
  1363. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  1364. struct page **pages)
  1365. {
  1366. int nr, ret;
  1367. start &= PAGE_MASK;
  1368. nr = __get_user_pages_fast(start, nr_pages, write, pages);
  1369. ret = nr;
  1370. if (nr < nr_pages) {
  1371. /* Try to get the remaining pages with get_user_pages */
  1372. start += nr << PAGE_SHIFT;
  1373. pages += nr;
  1374. ret = get_user_pages_unlocked(start, nr_pages - nr, write, 0, pages);
  1375. /* Have to be a bit careful with return values */
  1376. if (nr > 0) {
  1377. if (ret < 0)
  1378. ret = nr;
  1379. else
  1380. ret += nr;
  1381. }
  1382. }
  1383. return ret;
  1384. }
  1385. #endif /* CONFIG_HAVE_GENERIC_RCU_GUP */