gup.c 43 KB

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