gup.c 47 KB

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