gup.c 40 KB

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