gup.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. #include <linux/kernel.h>
  2. #include <linux/errno.h>
  3. #include <linux/err.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/hugetlb.h>
  6. #include <linux/mm.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/rmap.h>
  9. #include <linux/swap.h>
  10. #include <linux/swapops.h>
  11. #include <linux/sched.h>
  12. #include <linux/rwsem.h>
  13. #include <asm/pgtable.h>
  14. #include "internal.h"
  15. static struct page *no_page_table(struct vm_area_struct *vma,
  16. unsigned int flags)
  17. {
  18. /*
  19. * When core dumping an enormous anonymous area that nobody
  20. * has touched so far, we don't want to allocate unnecessary pages or
  21. * page tables. Return error instead of NULL to skip handle_mm_fault,
  22. * then get_dump_page() will return NULL to leave a hole in the dump.
  23. * But we can only make this optimization where a hole would surely
  24. * be zero-filled if handle_mm_fault() actually did handle it.
  25. */
  26. if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
  27. return ERR_PTR(-EFAULT);
  28. return NULL;
  29. }
  30. /*
  31. * FOLL_FORCE can write to even unwritable pte's, but only
  32. * after we've gone through a COW cycle and they are dirty.
  33. */
  34. static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
  35. {
  36. return pte_write(pte) ||
  37. ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
  38. }
  39. static struct page *follow_page_pte(struct vm_area_struct *vma,
  40. unsigned long address, pmd_t *pmd, unsigned int flags)
  41. {
  42. struct mm_struct *mm = vma->vm_mm;
  43. struct page *page;
  44. spinlock_t *ptl;
  45. pte_t *ptep, pte;
  46. retry:
  47. if (unlikely(pmd_bad(*pmd)))
  48. return no_page_table(vma, flags);
  49. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  50. pte = *ptep;
  51. if (!pte_present(pte)) {
  52. swp_entry_t entry;
  53. /*
  54. * KSM's break_ksm() relies upon recognizing a ksm page
  55. * even while it is being migrated, so for that case we
  56. * need migration_entry_wait().
  57. */
  58. if (likely(!(flags & FOLL_MIGRATION)))
  59. goto no_page;
  60. if (pte_none(pte) || pte_file(pte))
  61. goto no_page;
  62. entry = pte_to_swp_entry(pte);
  63. if (!is_migration_entry(entry))
  64. goto no_page;
  65. pte_unmap_unlock(ptep, ptl);
  66. migration_entry_wait(mm, pmd, address);
  67. goto retry;
  68. }
  69. if ((flags & FOLL_NUMA) && pte_numa(pte))
  70. goto no_page;
  71. if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) {
  72. pte_unmap_unlock(ptep, ptl);
  73. return NULL;
  74. }
  75. page = vm_normal_page(vma, address, pte);
  76. if (unlikely(!page)) {
  77. if ((flags & FOLL_DUMP) ||
  78. !is_zero_pfn(pte_pfn(pte)))
  79. goto bad_page;
  80. page = pte_page(pte);
  81. }
  82. if (flags & FOLL_GET)
  83. get_page_foll(page);
  84. if (flags & FOLL_TOUCH) {
  85. if ((flags & FOLL_WRITE) &&
  86. !pte_dirty(pte) && !PageDirty(page))
  87. set_page_dirty(page);
  88. /*
  89. * pte_mkyoung() would be more correct here, but atomic care
  90. * is needed to avoid losing the dirty bit: it is easier to use
  91. * mark_page_accessed().
  92. */
  93. mark_page_accessed(page);
  94. }
  95. if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
  96. /*
  97. * The preliminary mapping check is mainly to avoid the
  98. * pointless overhead of lock_page on the ZERO_PAGE
  99. * which might bounce very badly if there is contention.
  100. *
  101. * If the page is already locked, we don't need to
  102. * handle it now - vmscan will handle it later if and
  103. * when it attempts to reclaim the page.
  104. */
  105. if (page->mapping && trylock_page(page)) {
  106. lru_add_drain(); /* push cached pages to LRU */
  107. /*
  108. * Because we lock page here, and migration is
  109. * blocked by the pte's page reference, and we
  110. * know the page is still mapped, we don't even
  111. * need to check for file-cache page truncation.
  112. */
  113. mlock_vma_page(page);
  114. unlock_page(page);
  115. }
  116. }
  117. pte_unmap_unlock(ptep, ptl);
  118. return page;
  119. bad_page:
  120. pte_unmap_unlock(ptep, ptl);
  121. return ERR_PTR(-EFAULT);
  122. no_page:
  123. pte_unmap_unlock(ptep, ptl);
  124. if (!pte_none(pte))
  125. return NULL;
  126. return no_page_table(vma, flags);
  127. }
  128. /**
  129. * follow_page_mask - look up a page descriptor from a user-virtual address
  130. * @vma: vm_area_struct mapping @address
  131. * @address: virtual address to look up
  132. * @flags: flags modifying lookup behaviour
  133. * @page_mask: on output, *page_mask is set according to the size of the page
  134. *
  135. * @flags can have FOLL_ flags set, defined in <linux/mm.h>
  136. *
  137. * Returns the mapped (struct page *), %NULL if no mapping exists, or
  138. * an error pointer if there is a mapping to something not represented
  139. * by a page descriptor (see also vm_normal_page()).
  140. */
  141. struct page *follow_page_mask(struct vm_area_struct *vma,
  142. unsigned long address, unsigned int flags,
  143. unsigned int *page_mask)
  144. {
  145. pgd_t *pgd;
  146. pud_t *pud;
  147. pmd_t *pmd;
  148. spinlock_t *ptl;
  149. struct page *page;
  150. struct mm_struct *mm = vma->vm_mm;
  151. *page_mask = 0;
  152. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  153. if (!IS_ERR(page)) {
  154. BUG_ON(flags & FOLL_GET);
  155. return page;
  156. }
  157. pgd = pgd_offset(mm, address);
  158. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  159. return no_page_table(vma, flags);
  160. pud = pud_offset(pgd, address);
  161. if (pud_none(*pud))
  162. return no_page_table(vma, flags);
  163. if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
  164. page = follow_huge_pud(mm, address, pud, flags);
  165. if (page)
  166. return page;
  167. return no_page_table(vma, flags);
  168. }
  169. if (unlikely(pud_bad(*pud)))
  170. return no_page_table(vma, flags);
  171. pmd = pmd_offset(pud, address);
  172. if (pmd_none(*pmd))
  173. return no_page_table(vma, flags);
  174. if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
  175. page = follow_huge_pmd(mm, address, pmd, flags);
  176. if (page)
  177. return page;
  178. return no_page_table(vma, flags);
  179. }
  180. if ((flags & FOLL_NUMA) && pmd_numa(*pmd))
  181. return no_page_table(vma, flags);
  182. if (pmd_trans_huge(*pmd)) {
  183. if (flags & FOLL_SPLIT) {
  184. split_huge_page_pmd(vma, address, pmd);
  185. return follow_page_pte(vma, address, pmd, flags);
  186. }
  187. ptl = pmd_lock(mm, pmd);
  188. if (likely(pmd_trans_huge(*pmd))) {
  189. if (unlikely(pmd_trans_splitting(*pmd))) {
  190. spin_unlock(ptl);
  191. wait_split_huge_page(vma->anon_vma, pmd);
  192. } else {
  193. page = follow_trans_huge_pmd(vma, address,
  194. pmd, flags);
  195. spin_unlock(ptl);
  196. *page_mask = HPAGE_PMD_NR - 1;
  197. return page;
  198. }
  199. } else
  200. spin_unlock(ptl);
  201. }
  202. return follow_page_pte(vma, address, pmd, flags);
  203. }
  204. static int get_gate_page(struct mm_struct *mm, unsigned long address,
  205. unsigned int gup_flags, struct vm_area_struct **vma,
  206. struct page **page)
  207. {
  208. pgd_t *pgd;
  209. pud_t *pud;
  210. pmd_t *pmd;
  211. pte_t *pte;
  212. int ret = -EFAULT;
  213. /* user gate pages are read-only */
  214. if (gup_flags & FOLL_WRITE)
  215. return -EFAULT;
  216. if (address > TASK_SIZE)
  217. pgd = pgd_offset_k(address);
  218. else
  219. pgd = pgd_offset_gate(mm, address);
  220. BUG_ON(pgd_none(*pgd));
  221. pud = pud_offset(pgd, address);
  222. BUG_ON(pud_none(*pud));
  223. pmd = pmd_offset(pud, address);
  224. if (pmd_none(*pmd))
  225. return -EFAULT;
  226. VM_BUG_ON(pmd_trans_huge(*pmd));
  227. pte = pte_offset_map(pmd, address);
  228. if (pte_none(*pte))
  229. goto unmap;
  230. *vma = get_gate_vma(mm);
  231. if (!page)
  232. goto out;
  233. *page = vm_normal_page(*vma, address, *pte);
  234. if (!*page) {
  235. if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
  236. goto unmap;
  237. *page = pte_page(*pte);
  238. }
  239. get_page(*page);
  240. out:
  241. ret = 0;
  242. unmap:
  243. pte_unmap(pte);
  244. return ret;
  245. }
  246. /*
  247. * mmap_sem must be held on entry. If @nonblocking != NULL and
  248. * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
  249. * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
  250. */
  251. static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
  252. unsigned long address, unsigned int *flags, int *nonblocking)
  253. {
  254. struct mm_struct *mm = vma->vm_mm;
  255. unsigned int fault_flags = 0;
  256. int ret;
  257. /* For mlock, just skip the stack guard page. */
  258. if ((*flags & FOLL_MLOCK) &&
  259. (stack_guard_page_start(vma, address) ||
  260. stack_guard_page_end(vma, address + PAGE_SIZE)))
  261. return -ENOENT;
  262. if (*flags & FOLL_WRITE)
  263. fault_flags |= FAULT_FLAG_WRITE;
  264. if (nonblocking)
  265. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  266. if (*flags & FOLL_NOWAIT)
  267. fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
  268. if (*flags & FOLL_TRIED) {
  269. VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
  270. fault_flags |= FAULT_FLAG_TRIED;
  271. }
  272. ret = handle_mm_fault(mm, vma, address, fault_flags);
  273. if (ret & VM_FAULT_ERROR) {
  274. if (ret & VM_FAULT_OOM)
  275. return -ENOMEM;
  276. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  277. return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
  278. if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  279. return -EFAULT;
  280. BUG();
  281. }
  282. if (tsk) {
  283. if (ret & VM_FAULT_MAJOR)
  284. tsk->maj_flt++;
  285. else
  286. tsk->min_flt++;
  287. }
  288. if (ret & VM_FAULT_RETRY) {
  289. if (nonblocking)
  290. *nonblocking = 0;
  291. return -EBUSY;
  292. }
  293. /*
  294. * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
  295. * necessary, even if maybe_mkwrite decided not to set pte_write. We
  296. * can thus safely do subsequent page lookups as if they were reads.
  297. * But only do so when looping for pte_write is futile: in some cases
  298. * userspace may also be wanting to write to the gotten user page,
  299. * which a read fault here might prevent (a readonly page might get
  300. * reCOWed by userspace write).
  301. */
  302. if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
  303. *flags |= FOLL_COW;
  304. return 0;
  305. }
  306. static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
  307. {
  308. vm_flags_t vm_flags = vma->vm_flags;
  309. #ifdef CONFIG_MTK_EXTMEM
  310. if (vm_flags & (VM_IO | VM_PFNMAP)) {
  311. /*
  312. * Would pass VM_IO | VM_RESERVED | VM_PFNMAP.
  313. * (for Reserved Physical Memory PFN Mapping Usage)
  314. */
  315. if (!((vma->vm_flags&VM_IO) &&
  316. (vma->vm_flags&VM_RESERVED) &&
  317. (vma->vm_flags&VM_PFNMAP)))
  318. return -EFAULT;
  319. }
  320. #else
  321. if (vm_flags & (VM_IO | VM_PFNMAP))
  322. return -EFAULT;
  323. #endif
  324. if (gup_flags & FOLL_WRITE) {
  325. if (!(vm_flags & VM_WRITE)) {
  326. if (!(gup_flags & FOLL_FORCE))
  327. return -EFAULT;
  328. /*
  329. * We used to let the write,force case do COW in a
  330. * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
  331. * set a breakpoint in a read-only mapping of an
  332. * executable, without corrupting the file (yet only
  333. * when that file had been opened for writing!).
  334. * Anon pages in shared mappings are surprising: now
  335. * just reject it.
  336. */
  337. if (!is_cow_mapping(vm_flags)) {
  338. WARN_ON_ONCE(vm_flags & VM_MAYWRITE);
  339. return -EFAULT;
  340. }
  341. }
  342. } else if (!(vm_flags & VM_READ)) {
  343. if (!(gup_flags & FOLL_FORCE))
  344. return -EFAULT;
  345. /*
  346. * Is there actually any vma we can reach here which does not
  347. * have VM_MAYREAD set?
  348. */
  349. if (!(vm_flags & VM_MAYREAD))
  350. return -EFAULT;
  351. }
  352. return 0;
  353. }
  354. /**
  355. * __get_user_pages() - pin user pages in memory
  356. * @tsk: task_struct of target task
  357. * @mm: mm_struct of target mm
  358. * @start: starting user address
  359. * @nr_pages: number of pages from start to pin
  360. * @gup_flags: flags modifying pin behaviour
  361. * @pages: array that receives pointers to the pages pinned.
  362. * Should be at least nr_pages long. Or NULL, if caller
  363. * only intends to ensure the pages are faulted in.
  364. * @vmas: array of pointers to vmas corresponding to each page.
  365. * Or NULL if the caller does not require them.
  366. * @nonblocking: whether waiting for disk IO or mmap_sem contention
  367. *
  368. * Returns number of pages pinned. This may be fewer than the number
  369. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  370. * were pinned, returns -errno. Each page returned must be released
  371. * with a put_page() call when it is finished with. vmas will only
  372. * remain valid while mmap_sem is held.
  373. *
  374. * Must be called with mmap_sem held. It may be released. See below.
  375. *
  376. * __get_user_pages walks a process's page tables and takes a reference to
  377. * each struct page that each user address corresponds to at a given
  378. * instant. That is, it takes the page that would be accessed if a user
  379. * thread accesses the given user virtual address at that instant.
  380. *
  381. * This does not guarantee that the page exists in the user mappings when
  382. * __get_user_pages returns, and there may even be a completely different
  383. * page there in some cases (eg. if mmapped pagecache has been invalidated
  384. * and subsequently re faulted). However it does guarantee that the page
  385. * won't be freed completely. And mostly callers simply care that the page
  386. * contains data that was valid *at some point in time*. Typically, an IO
  387. * or similar operation cannot guarantee anything stronger anyway because
  388. * locks can't be held over the syscall boundary.
  389. *
  390. * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
  391. * the page is written to, set_page_dirty (or set_page_dirty_lock, as
  392. * appropriate) must be called after the page is finished with, and
  393. * before put_page is called.
  394. *
  395. * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
  396. * or mmap_sem contention, and if waiting is needed to pin all pages,
  397. * *@nonblocking will be set to 0. Further, if @gup_flags does not
  398. * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
  399. * this case.
  400. *
  401. * A caller using such a combination of @nonblocking and @gup_flags
  402. * must therefore hold the mmap_sem for reading only, and recognize
  403. * when it's been released. Otherwise, it must be held for either
  404. * reading or writing and will not be released.
  405. *
  406. * In most cases, get_user_pages or get_user_pages_fast should be used
  407. * instead of __get_user_pages. __get_user_pages should be used only if
  408. * you need some special @gup_flags.
  409. */
  410. long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  411. unsigned long start, unsigned long nr_pages,
  412. unsigned int gup_flags, struct page **pages,
  413. struct vm_area_struct **vmas, int *nonblocking)
  414. {
  415. long i = 0;
  416. unsigned int page_mask;
  417. struct vm_area_struct *vma = NULL;
  418. if (!nr_pages)
  419. return 0;
  420. VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
  421. /*
  422. * If FOLL_FORCE is set then do not force a full fault as the hinting
  423. * fault information is unrelated to the reference behaviour of a task
  424. * using the address space
  425. */
  426. if (!(gup_flags & FOLL_FORCE))
  427. gup_flags |= FOLL_NUMA;
  428. do {
  429. struct page *page;
  430. unsigned int foll_flags = gup_flags;
  431. unsigned int page_increm;
  432. /* first iteration or cross vma bound */
  433. if (!vma || start >= vma->vm_end) {
  434. vma = find_extend_vma(mm, start);
  435. if (!vma && in_gate_area(mm, start)) {
  436. int ret;
  437. ret = get_gate_page(mm, start & PAGE_MASK,
  438. gup_flags, &vma,
  439. pages ? &pages[i] : NULL);
  440. if (ret)
  441. return i ? : ret;
  442. page_mask = 0;
  443. goto next_page;
  444. }
  445. if (!vma || check_vma_flags(vma, gup_flags))
  446. return i ? : -EFAULT;
  447. if (is_vm_hugetlb_page(vma)) {
  448. i = follow_hugetlb_page(mm, vma, pages, vmas,
  449. &start, &nr_pages, i,
  450. gup_flags);
  451. continue;
  452. }
  453. }
  454. retry:
  455. /*
  456. * If we have a pending SIGKILL, don't keep faulting pages and
  457. * potentially allocating memory.
  458. */
  459. if (unlikely(fatal_signal_pending(current)))
  460. return i ? i : -ERESTARTSYS;
  461. cond_resched();
  462. page = follow_page_mask(vma, start, foll_flags, &page_mask);
  463. if (!page) {
  464. int ret;
  465. ret = faultin_page(tsk, vma, start, &foll_flags,
  466. nonblocking);
  467. switch (ret) {
  468. case 0:
  469. goto retry;
  470. case -EFAULT:
  471. case -ENOMEM:
  472. case -EHWPOISON:
  473. return i ? i : ret;
  474. case -EBUSY:
  475. return i;
  476. case -ENOENT:
  477. goto next_page;
  478. }
  479. BUG();
  480. }
  481. if (IS_ERR(page))
  482. return i ? i : PTR_ERR(page);
  483. if (pages) {
  484. pages[i] = page;
  485. flush_anon_page(vma, page, start);
  486. flush_dcache_page(page);
  487. page_mask = 0;
  488. }
  489. next_page:
  490. if (vmas) {
  491. vmas[i] = vma;
  492. page_mask = 0;
  493. }
  494. page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
  495. if (page_increm > nr_pages)
  496. page_increm = nr_pages;
  497. i += page_increm;
  498. start += page_increm * PAGE_SIZE;
  499. nr_pages -= page_increm;
  500. } while (nr_pages);
  501. return i;
  502. }
  503. EXPORT_SYMBOL(__get_user_pages);
  504. /*
  505. * fixup_user_fault() - manually resolve a user page fault
  506. * @tsk: the task_struct to use for page fault accounting, or
  507. * NULL if faults are not to be recorded.
  508. * @mm: mm_struct of target mm
  509. * @address: user address
  510. * @fault_flags:flags to pass down to handle_mm_fault()
  511. *
  512. * This is meant to be called in the specific scenario where for locking reasons
  513. * we try to access user memory in atomic context (within a pagefault_disable()
  514. * section), this returns -EFAULT, and we want to resolve the user fault before
  515. * trying again.
  516. *
  517. * Typically this is meant to be used by the futex code.
  518. *
  519. * The main difference with get_user_pages() is that this function will
  520. * unconditionally call handle_mm_fault() which will in turn perform all the
  521. * necessary SW fixup of the dirty and young bits in the PTE, while
  522. * handle_mm_fault() only guarantees to update these in the struct page.
  523. *
  524. * This is important for some architectures where those bits also gate the
  525. * access permission to the page because they are maintained in software. On
  526. * such architectures, gup() will not be enough to make a subsequent access
  527. * succeed.
  528. *
  529. * This has the same semantics wrt the @mm->mmap_sem as does filemap_fault().
  530. */
  531. int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  532. unsigned long address, unsigned int fault_flags)
  533. {
  534. struct vm_area_struct *vma;
  535. vm_flags_t vm_flags;
  536. int ret;
  537. vma = find_extend_vma(mm, address);
  538. if (!vma || address < vma->vm_start)
  539. return -EFAULT;
  540. vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
  541. if (!(vm_flags & vma->vm_flags))
  542. return -EFAULT;
  543. ret = handle_mm_fault(mm, vma, address, fault_flags);
  544. if (ret & VM_FAULT_ERROR) {
  545. if (ret & VM_FAULT_OOM)
  546. return -ENOMEM;
  547. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  548. return -EHWPOISON;
  549. if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  550. return -EFAULT;
  551. BUG();
  552. }
  553. if (tsk) {
  554. if (ret & VM_FAULT_MAJOR)
  555. tsk->maj_flt++;
  556. else
  557. tsk->min_flt++;
  558. }
  559. return 0;
  560. }
  561. /*
  562. * get_user_pages() - pin user pages in memory
  563. * @tsk: the task_struct to use for page fault accounting, or
  564. * NULL if faults are not to be recorded.
  565. * @mm: mm_struct of target mm
  566. * @start: starting user address
  567. * @nr_pages: number of pages from start to pin
  568. * @write: whether pages will be written to by the caller
  569. * @force: whether to force access even when user mapping is currently
  570. * protected (but never forces write access to shared mapping).
  571. * @pages: array that receives pointers to the pages pinned.
  572. * Should be at least nr_pages long. Or NULL, if caller
  573. * only intends to ensure the pages are faulted in.
  574. * @vmas: array of pointers to vmas corresponding to each page.
  575. * Or NULL if the caller does not require them.
  576. *
  577. * Returns number of pages pinned. This may be fewer than the number
  578. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  579. * were pinned, returns -errno. Each page returned must be released
  580. * with a put_page() call when it is finished with. vmas will only
  581. * remain valid while mmap_sem is held.
  582. *
  583. * Must be called with mmap_sem held for read or write.
  584. *
  585. * get_user_pages walks a process's page tables and takes a reference to
  586. * each struct page that each user address corresponds to at a given
  587. * instant. That is, it takes the page that would be accessed if a user
  588. * thread accesses the given user virtual address at that instant.
  589. *
  590. * This does not guarantee that the page exists in the user mappings when
  591. * get_user_pages returns, and there may even be a completely different
  592. * page there in some cases (eg. if mmapped pagecache has been invalidated
  593. * and subsequently re faulted). However it does guarantee that the page
  594. * won't be freed completely. And mostly callers simply care that the page
  595. * contains data that was valid *at some point in time*. Typically, an IO
  596. * or similar operation cannot guarantee anything stronger anyway because
  597. * locks can't be held over the syscall boundary.
  598. *
  599. * If write=0, the page must not be written to. If the page is written to,
  600. * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
  601. * after the page is finished with, and before put_page is called.
  602. *
  603. * get_user_pages is typically used for fewer-copy IO operations, to get a
  604. * handle on the memory by some means other than accesses via the user virtual
  605. * addresses. The pages may be submitted for DMA to devices or accessed via
  606. * their kernel linear mapping (via the kmap APIs). Care should be taken to
  607. * use the correct cache flushing APIs.
  608. *
  609. * See also get_user_pages_fast, for performance critical applications.
  610. */
  611. long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  612. unsigned long start, unsigned long nr_pages, int write,
  613. int force, struct page **pages, struct vm_area_struct **vmas)
  614. {
  615. int flags = FOLL_TOUCH;
  616. if (pages)
  617. flags |= FOLL_GET;
  618. if (write)
  619. flags |= FOLL_WRITE;
  620. if (force)
  621. flags |= FOLL_FORCE;
  622. return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
  623. NULL);
  624. }
  625. EXPORT_SYMBOL(get_user_pages);
  626. /**
  627. * get_dump_page() - pin user page in memory while writing it to core dump
  628. * @addr: user address
  629. *
  630. * Returns struct page pointer of user page pinned for dump,
  631. * to be freed afterwards by page_cache_release() or put_page().
  632. *
  633. * Returns NULL on any kind of failure - a hole must then be inserted into
  634. * the corefile, to preserve alignment with its headers; and also returns
  635. * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
  636. * allowing a hole to be left in the corefile to save diskspace.
  637. *
  638. * Called without mmap_sem, but after all other threads have been killed.
  639. */
  640. #ifdef CONFIG_ELF_CORE
  641. struct page *get_dump_page(unsigned long addr)
  642. {
  643. struct vm_area_struct *vma;
  644. struct page *page;
  645. if (__get_user_pages(current, current->mm, addr, 1,
  646. FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
  647. NULL) < 1)
  648. return NULL;
  649. flush_cache_page(vma, addr, page_to_pfn(page));
  650. return page;
  651. }
  652. #endif /* CONFIG_ELF_CORE */
  653. /*
  654. * Generic RCU Fast GUP
  655. *
  656. * get_user_pages_fast attempts to pin user pages by walking the page
  657. * tables directly and avoids taking locks. Thus the walker needs to be
  658. * protected from page table pages being freed from under it, and should
  659. * block any THP splits.
  660. *
  661. * One way to achieve this is to have the walker disable interrupts, and
  662. * rely on IPIs from the TLB flushing code blocking before the page table
  663. * pages are freed. This is unsuitable for architectures that do not need
  664. * to broadcast an IPI when invalidating TLBs.
  665. *
  666. * Another way to achieve this is to batch up page table containing pages
  667. * belonging to more than one mm_user, then rcu_sched a callback to free those
  668. * pages. Disabling interrupts will allow the fast_gup walker to both block
  669. * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
  670. * (which is a relatively rare event). The code below adopts this strategy.
  671. *
  672. * Before activating this code, please be aware that the following assumptions
  673. * are currently made:
  674. *
  675. * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
  676. * pages containing page tables.
  677. *
  678. * *) THP splits will broadcast an IPI, this can be achieved by overriding
  679. * pmdp_splitting_flush.
  680. *
  681. * *) ptes can be read atomically by the architecture.
  682. *
  683. * *) access_ok is sufficient to validate userspace address ranges.
  684. *
  685. * The last two assumptions can be relaxed by the addition of helper functions.
  686. *
  687. * This code is based heavily on the PowerPC implementation by Nick Piggin.
  688. */
  689. #ifdef CONFIG_HAVE_GENERIC_RCU_GUP
  690. #ifdef __HAVE_ARCH_PTE_SPECIAL
  691. static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  692. int write, struct page **pages, int *nr)
  693. {
  694. pte_t *ptep, *ptem;
  695. int ret = 0;
  696. ptem = ptep = pte_offset_map(&pmd, addr);
  697. do {
  698. /*
  699. * In the line below we are assuming that the pte can be read
  700. * atomically. If this is not the case for your architecture,
  701. * please wrap this in a helper function!
  702. *
  703. * for an example see gup_get_pte in arch/x86/mm/gup.c
  704. */
  705. pte_t pte = ACCESS_ONCE(*ptep);
  706. struct page *page;
  707. /*
  708. * Similar to the PMD case below, NUMA hinting must take slow
  709. * path
  710. */
  711. if (!pte_present(pte) || pte_special(pte) ||
  712. pte_numa(pte) || (write && !pte_write(pte)))
  713. goto pte_unmap;
  714. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  715. page = pte_page(pte);
  716. if (!page_cache_get_speculative(page))
  717. goto pte_unmap;
  718. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  719. put_page(page);
  720. goto pte_unmap;
  721. }
  722. pages[*nr] = page;
  723. (*nr)++;
  724. } while (ptep++, addr += PAGE_SIZE, addr != end);
  725. ret = 1;
  726. pte_unmap:
  727. pte_unmap(ptem);
  728. return ret;
  729. }
  730. #else
  731. /*
  732. * If we can't determine whether or not a pte is special, then fail immediately
  733. * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
  734. * to be special.
  735. *
  736. * For a futex to be placed on a THP tail page, get_futex_key requires a
  737. * __get_user_pages_fast implementation that can pin pages. Thus it's still
  738. * useful to have gup_huge_pmd even if we can't operate on ptes.
  739. */
  740. static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  741. int write, struct page **pages, int *nr)
  742. {
  743. return 0;
  744. }
  745. #endif /* __HAVE_ARCH_PTE_SPECIAL */
  746. static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
  747. unsigned long end, int write, struct page **pages, int *nr)
  748. {
  749. struct page *head, *page, *tail;
  750. int refs;
  751. if (write && !pmd_write(orig))
  752. return 0;
  753. refs = 0;
  754. head = pmd_page(orig);
  755. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  756. tail = page;
  757. do {
  758. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  759. pages[*nr] = page;
  760. (*nr)++;
  761. page++;
  762. refs++;
  763. } while (addr += PAGE_SIZE, addr != end);
  764. if (!page_cache_add_speculative(head, refs)) {
  765. *nr -= refs;
  766. return 0;
  767. }
  768. if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
  769. *nr -= refs;
  770. while (refs--)
  771. put_page(head);
  772. return 0;
  773. }
  774. /*
  775. * Any tail pages need their mapcount reference taken before we
  776. * return. (This allows the THP code to bump their ref count when
  777. * they are split into base pages).
  778. */
  779. while (refs--) {
  780. if (PageTail(tail))
  781. get_huge_page_tail(tail);
  782. tail++;
  783. }
  784. return 1;
  785. }
  786. static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
  787. unsigned long end, int write, struct page **pages, int *nr)
  788. {
  789. struct page *head, *page, *tail;
  790. int refs;
  791. if (write && !pud_write(orig))
  792. return 0;
  793. refs = 0;
  794. head = pud_page(orig);
  795. page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  796. tail = page;
  797. do {
  798. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  799. pages[*nr] = page;
  800. (*nr)++;
  801. page++;
  802. refs++;
  803. } while (addr += PAGE_SIZE, addr != end);
  804. if (!page_cache_add_speculative(head, refs)) {
  805. *nr -= refs;
  806. return 0;
  807. }
  808. if (unlikely(pud_val(orig) != pud_val(*pudp))) {
  809. *nr -= refs;
  810. while (refs--)
  811. put_page(head);
  812. return 0;
  813. }
  814. while (refs--) {
  815. if (PageTail(tail))
  816. get_huge_page_tail(tail);
  817. tail++;
  818. }
  819. return 1;
  820. }
  821. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  822. int write, struct page **pages, int *nr)
  823. {
  824. unsigned long next;
  825. pmd_t *pmdp;
  826. pmdp = pmd_offset(&pud, addr);
  827. do {
  828. pmd_t pmd = ACCESS_ONCE(*pmdp);
  829. next = pmd_addr_end(addr, end);
  830. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  831. return 0;
  832. if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
  833. /*
  834. * NUMA hinting faults need to be handled in the GUP
  835. * slowpath for accounting purposes and so that they
  836. * can be serialised against THP migration.
  837. */
  838. if (pmd_numa(pmd))
  839. return 0;
  840. if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
  841. pages, nr))
  842. return 0;
  843. } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  844. return 0;
  845. } while (pmdp++, addr = next, addr != end);
  846. return 1;
  847. }
  848. static int gup_pud_range(pgd_t *pgdp, unsigned long addr, unsigned long end,
  849. int write, struct page **pages, int *nr)
  850. {
  851. unsigned long next;
  852. pud_t *pudp;
  853. pudp = pud_offset(pgdp, addr);
  854. do {
  855. pud_t pud = ACCESS_ONCE(*pudp);
  856. next = pud_addr_end(addr, end);
  857. if (pud_none(pud))
  858. return 0;
  859. if (pud_huge(pud)) {
  860. if (!gup_huge_pud(pud, pudp, addr, next, write,
  861. pages, nr))
  862. return 0;
  863. } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  864. return 0;
  865. } while (pudp++, addr = next, addr != end);
  866. return 1;
  867. }
  868. /*
  869. * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
  870. * the regular GUP. It will only return non-negative values.
  871. */
  872. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  873. struct page **pages)
  874. {
  875. struct mm_struct *mm = current->mm;
  876. unsigned long addr, len, end;
  877. unsigned long next, flags;
  878. pgd_t *pgdp;
  879. int nr = 0;
  880. start &= PAGE_MASK;
  881. addr = start;
  882. len = (unsigned long) nr_pages << PAGE_SHIFT;
  883. end = start + len;
  884. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  885. start, len)))
  886. return 0;
  887. /*
  888. * Disable interrupts. We use the nested form as we can already have
  889. * interrupts disabled by get_futex_key.
  890. *
  891. * With interrupts disabled, we block page table pages from being
  892. * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
  893. * for more details.
  894. *
  895. * We do not adopt an rcu_read_lock(.) here as we also want to
  896. * block IPIs that come from THPs splitting.
  897. */
  898. local_irq_save(flags);
  899. pgdp = pgd_offset(mm, addr);
  900. do {
  901. next = pgd_addr_end(addr, end);
  902. if (pgd_none(*pgdp))
  903. break;
  904. else if (!gup_pud_range(pgdp, addr, next, write, pages, &nr))
  905. break;
  906. } while (pgdp++, addr = next, addr != end);
  907. local_irq_restore(flags);
  908. return nr;
  909. }
  910. /**
  911. * get_user_pages_fast() - pin user pages in memory
  912. * @start: starting user address
  913. * @nr_pages: number of pages from start to pin
  914. * @write: whether pages will be written to
  915. * @pages: array that receives pointers to the pages pinned.
  916. * Should be at least nr_pages long.
  917. *
  918. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  919. * If not successful, it will fall back to taking the lock and
  920. * calling get_user_pages().
  921. *
  922. * Returns number of pages pinned. This may be fewer than the number
  923. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  924. * were pinned, returns -errno.
  925. */
  926. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  927. struct page **pages)
  928. {
  929. struct mm_struct *mm = current->mm;
  930. int nr, ret;
  931. start &= PAGE_MASK;
  932. nr = __get_user_pages_fast(start, nr_pages, write, pages);
  933. ret = nr;
  934. if (nr < nr_pages) {
  935. /* Try to get the remaining pages with get_user_pages */
  936. start += nr << PAGE_SHIFT;
  937. pages += nr;
  938. down_read(&mm->mmap_sem);
  939. ret = get_user_pages(current, mm, start,
  940. nr_pages - nr, write, 0, pages, NULL);
  941. up_read(&mm->mmap_sem);
  942. /* Have to be a bit careful with return values */
  943. if (nr > 0) {
  944. if (ret < 0)
  945. ret = nr;
  946. else
  947. ret += nr;
  948. }
  949. }
  950. return ret;
  951. }
  952. #endif /* CONFIG_HAVE_GENERIC_RCU_GUP */