gup.c 51 KB

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