gup.c 41 KB

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