gup.c 45 KB

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