gup.c 52 KB

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