gup.c 44 KB

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