gup.c 30 KB

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