gup.c 42 KB

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