gup.c 51 KB

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