userfaultfd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * mm/userfaultfd.c
  3. *
  4. * Copyright (C) 2015 Red Hat, Inc.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2. See
  7. * the COPYING file in the top-level directory.
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/sched/signal.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/rmap.h>
  13. #include <linux/swap.h>
  14. #include <linux/swapops.h>
  15. #include <linux/userfaultfd_k.h>
  16. #include <linux/mmu_notifier.h>
  17. #include <linux/hugetlb.h>
  18. #include <linux/shmem_fs.h>
  19. #include <asm/tlbflush.h>
  20. #include "internal.h"
  21. static int mcopy_atomic_pte(struct mm_struct *dst_mm,
  22. pmd_t *dst_pmd,
  23. struct vm_area_struct *dst_vma,
  24. unsigned long dst_addr,
  25. unsigned long src_addr,
  26. struct page **pagep)
  27. {
  28. struct mem_cgroup *memcg;
  29. pte_t _dst_pte, *dst_pte;
  30. spinlock_t *ptl;
  31. void *page_kaddr;
  32. int ret;
  33. struct page *page;
  34. pgoff_t offset, max_off;
  35. struct inode *inode;
  36. if (!*pagep) {
  37. ret = -ENOMEM;
  38. page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr);
  39. if (!page)
  40. goto out;
  41. page_kaddr = kmap_atomic(page);
  42. ret = copy_from_user(page_kaddr,
  43. (const void __user *) src_addr,
  44. PAGE_SIZE);
  45. kunmap_atomic(page_kaddr);
  46. /* fallback to copy_from_user outside mmap_sem */
  47. if (unlikely(ret)) {
  48. ret = -ENOENT;
  49. *pagep = page;
  50. /* don't free the page */
  51. goto out;
  52. }
  53. } else {
  54. page = *pagep;
  55. *pagep = NULL;
  56. }
  57. /*
  58. * The memory barrier inside __SetPageUptodate makes sure that
  59. * preceeding stores to the page contents become visible before
  60. * the set_pte_at() write.
  61. */
  62. __SetPageUptodate(page);
  63. ret = -ENOMEM;
  64. if (mem_cgroup_try_charge(page, dst_mm, GFP_KERNEL, &memcg, false))
  65. goto out_release;
  66. _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
  67. if (dst_vma->vm_flags & VM_WRITE)
  68. _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
  69. dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
  70. if (dst_vma->vm_file) {
  71. /* the shmem MAP_PRIVATE case requires checking the i_size */
  72. inode = dst_vma->vm_file->f_inode;
  73. offset = linear_page_index(dst_vma, dst_addr);
  74. max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  75. ret = -EFAULT;
  76. if (unlikely(offset >= max_off))
  77. goto out_release_uncharge_unlock;
  78. }
  79. ret = -EEXIST;
  80. if (!pte_none(*dst_pte))
  81. goto out_release_uncharge_unlock;
  82. inc_mm_counter(dst_mm, MM_ANONPAGES);
  83. page_add_new_anon_rmap(page, dst_vma, dst_addr, false);
  84. mem_cgroup_commit_charge(page, memcg, false, false);
  85. lru_cache_add_active_or_unevictable(page, dst_vma);
  86. set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
  87. /* No need to invalidate - it was non-present before */
  88. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  89. pte_unmap_unlock(dst_pte, ptl);
  90. ret = 0;
  91. out:
  92. return ret;
  93. out_release_uncharge_unlock:
  94. pte_unmap_unlock(dst_pte, ptl);
  95. mem_cgroup_cancel_charge(page, memcg, false);
  96. out_release:
  97. put_page(page);
  98. goto out;
  99. }
  100. static int mfill_zeropage_pte(struct mm_struct *dst_mm,
  101. pmd_t *dst_pmd,
  102. struct vm_area_struct *dst_vma,
  103. unsigned long dst_addr)
  104. {
  105. pte_t _dst_pte, *dst_pte;
  106. spinlock_t *ptl;
  107. int ret;
  108. pgoff_t offset, max_off;
  109. struct inode *inode;
  110. _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
  111. dst_vma->vm_page_prot));
  112. dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
  113. if (dst_vma->vm_file) {
  114. /* the shmem MAP_PRIVATE case requires checking the i_size */
  115. inode = dst_vma->vm_file->f_inode;
  116. offset = linear_page_index(dst_vma, dst_addr);
  117. max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  118. ret = -EFAULT;
  119. if (unlikely(offset >= max_off))
  120. goto out_unlock;
  121. }
  122. ret = -EEXIST;
  123. if (!pte_none(*dst_pte))
  124. goto out_unlock;
  125. set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
  126. /* No need to invalidate - it was non-present before */
  127. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  128. ret = 0;
  129. out_unlock:
  130. pte_unmap_unlock(dst_pte, ptl);
  131. return ret;
  132. }
  133. static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
  134. {
  135. pgd_t *pgd;
  136. p4d_t *p4d;
  137. pud_t *pud;
  138. pgd = pgd_offset(mm, address);
  139. p4d = p4d_alloc(mm, pgd, address);
  140. if (!p4d)
  141. return NULL;
  142. pud = pud_alloc(mm, p4d, address);
  143. if (!pud)
  144. return NULL;
  145. /*
  146. * Note that we didn't run this because the pmd was
  147. * missing, the *pmd may be already established and in
  148. * turn it may also be a trans_huge_pmd.
  149. */
  150. return pmd_alloc(mm, pud, address);
  151. }
  152. #ifdef CONFIG_HUGETLB_PAGE
  153. /*
  154. * __mcopy_atomic processing for HUGETLB vmas. Note that this routine is
  155. * called with mmap_sem held, it will release mmap_sem before returning.
  156. */
  157. static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
  158. struct vm_area_struct *dst_vma,
  159. unsigned long dst_start,
  160. unsigned long src_start,
  161. unsigned long len,
  162. bool zeropage)
  163. {
  164. int vm_alloc_shared = dst_vma->vm_flags & VM_SHARED;
  165. int vm_shared = dst_vma->vm_flags & VM_SHARED;
  166. ssize_t err;
  167. pte_t *dst_pte;
  168. unsigned long src_addr, dst_addr;
  169. long copied;
  170. struct page *page;
  171. struct hstate *h;
  172. unsigned long vma_hpagesize;
  173. pgoff_t idx;
  174. u32 hash;
  175. struct address_space *mapping;
  176. /*
  177. * There is no default zero huge page for all huge page sizes as
  178. * supported by hugetlb. A PMD_SIZE huge pages may exist as used
  179. * by THP. Since we can not reliably insert a zero page, this
  180. * feature is not supported.
  181. */
  182. if (zeropage) {
  183. up_read(&dst_mm->mmap_sem);
  184. return -EINVAL;
  185. }
  186. src_addr = src_start;
  187. dst_addr = dst_start;
  188. copied = 0;
  189. page = NULL;
  190. vma_hpagesize = vma_kernel_pagesize(dst_vma);
  191. /*
  192. * Validate alignment based on huge page size
  193. */
  194. err = -EINVAL;
  195. if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
  196. goto out_unlock;
  197. retry:
  198. /*
  199. * On routine entry dst_vma is set. If we had to drop mmap_sem and
  200. * retry, dst_vma will be set to NULL and we must lookup again.
  201. */
  202. if (!dst_vma) {
  203. err = -ENOENT;
  204. dst_vma = find_vma(dst_mm, dst_start);
  205. if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
  206. goto out_unlock;
  207. /*
  208. * Check the vma is registered in uffd, this is
  209. * required to enforce the VM_MAYWRITE check done at
  210. * uffd registration time.
  211. */
  212. if (!dst_vma->vm_userfaultfd_ctx.ctx)
  213. goto out_unlock;
  214. if (dst_start < dst_vma->vm_start ||
  215. dst_start + len > dst_vma->vm_end)
  216. goto out_unlock;
  217. err = -EINVAL;
  218. if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
  219. goto out_unlock;
  220. vm_shared = dst_vma->vm_flags & VM_SHARED;
  221. }
  222. if (WARN_ON(dst_addr & (vma_hpagesize - 1) ||
  223. (len - copied) & (vma_hpagesize - 1)))
  224. goto out_unlock;
  225. /*
  226. * If not shared, ensure the dst_vma has a anon_vma.
  227. */
  228. err = -ENOMEM;
  229. if (!vm_shared) {
  230. if (unlikely(anon_vma_prepare(dst_vma)))
  231. goto out_unlock;
  232. }
  233. h = hstate_vma(dst_vma);
  234. while (src_addr < src_start + len) {
  235. pte_t dst_pteval;
  236. BUG_ON(dst_addr >= dst_start + len);
  237. VM_BUG_ON(dst_addr & ~huge_page_mask(h));
  238. /*
  239. * Serialize via hugetlb_fault_mutex
  240. */
  241. idx = linear_page_index(dst_vma, dst_addr);
  242. mapping = dst_vma->vm_file->f_mapping;
  243. hash = hugetlb_fault_mutex_hash(h, mapping, idx, dst_addr);
  244. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  245. err = -ENOMEM;
  246. dst_pte = huge_pte_alloc(dst_mm, dst_addr, huge_page_size(h));
  247. if (!dst_pte) {
  248. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  249. goto out_unlock;
  250. }
  251. err = -EEXIST;
  252. dst_pteval = huge_ptep_get(dst_pte);
  253. if (!huge_pte_none(dst_pteval)) {
  254. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  255. goto out_unlock;
  256. }
  257. err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma,
  258. dst_addr, src_addr, &page);
  259. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  260. vm_alloc_shared = vm_shared;
  261. cond_resched();
  262. if (unlikely(err == -ENOENT)) {
  263. up_read(&dst_mm->mmap_sem);
  264. BUG_ON(!page);
  265. err = copy_huge_page_from_user(page,
  266. (const void __user *)src_addr,
  267. pages_per_huge_page(h), true);
  268. if (unlikely(err)) {
  269. err = -EFAULT;
  270. goto out;
  271. }
  272. down_read(&dst_mm->mmap_sem);
  273. dst_vma = NULL;
  274. goto retry;
  275. } else
  276. BUG_ON(page);
  277. if (!err) {
  278. dst_addr += vma_hpagesize;
  279. src_addr += vma_hpagesize;
  280. copied += vma_hpagesize;
  281. if (fatal_signal_pending(current))
  282. err = -EINTR;
  283. }
  284. if (err)
  285. break;
  286. }
  287. out_unlock:
  288. up_read(&dst_mm->mmap_sem);
  289. out:
  290. if (page) {
  291. /*
  292. * We encountered an error and are about to free a newly
  293. * allocated huge page.
  294. *
  295. * Reservation handling is very subtle, and is different for
  296. * private and shared mappings. See the routine
  297. * restore_reserve_on_error for details. Unfortunately, we
  298. * can not call restore_reserve_on_error now as it would
  299. * require holding mmap_sem.
  300. *
  301. * If a reservation for the page existed in the reservation
  302. * map of a private mapping, the map was modified to indicate
  303. * the reservation was consumed when the page was allocated.
  304. * We clear the PagePrivate flag now so that the global
  305. * reserve count will not be incremented in free_huge_page.
  306. * The reservation map will still indicate the reservation
  307. * was consumed and possibly prevent later page allocation.
  308. * This is better than leaking a global reservation. If no
  309. * reservation existed, it is still safe to clear PagePrivate
  310. * as no adjustments to reservation counts were made during
  311. * allocation.
  312. *
  313. * The reservation map for shared mappings indicates which
  314. * pages have reservations. When a huge page is allocated
  315. * for an address with a reservation, no change is made to
  316. * the reserve map. In this case PagePrivate will be set
  317. * to indicate that the global reservation count should be
  318. * incremented when the page is freed. This is the desired
  319. * behavior. However, when a huge page is allocated for an
  320. * address without a reservation a reservation entry is added
  321. * to the reservation map, and PagePrivate will not be set.
  322. * When the page is freed, the global reserve count will NOT
  323. * be incremented and it will appear as though we have leaked
  324. * reserved page. In this case, set PagePrivate so that the
  325. * global reserve count will be incremented to match the
  326. * reservation map entry which was created.
  327. *
  328. * Note that vm_alloc_shared is based on the flags of the vma
  329. * for which the page was originally allocated. dst_vma could
  330. * be different or NULL on error.
  331. */
  332. if (vm_alloc_shared)
  333. SetPagePrivate(page);
  334. else
  335. ClearPagePrivate(page);
  336. put_page(page);
  337. }
  338. BUG_ON(copied < 0);
  339. BUG_ON(err > 0);
  340. BUG_ON(!copied && !err);
  341. return copied ? copied : err;
  342. }
  343. #else /* !CONFIG_HUGETLB_PAGE */
  344. /* fail at build time if gcc attempts to use this */
  345. extern ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
  346. struct vm_area_struct *dst_vma,
  347. unsigned long dst_start,
  348. unsigned long src_start,
  349. unsigned long len,
  350. bool zeropage);
  351. #endif /* CONFIG_HUGETLB_PAGE */
  352. static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
  353. pmd_t *dst_pmd,
  354. struct vm_area_struct *dst_vma,
  355. unsigned long dst_addr,
  356. unsigned long src_addr,
  357. struct page **page,
  358. bool zeropage)
  359. {
  360. ssize_t err;
  361. /*
  362. * The normal page fault path for a shmem will invoke the
  363. * fault, fill the hole in the file and COW it right away. The
  364. * result generates plain anonymous memory. So when we are
  365. * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
  366. * generate anonymous memory directly without actually filling
  367. * the hole. For the MAP_PRIVATE case the robustness check
  368. * only happens in the pagetable (to verify it's still none)
  369. * and not in the radix tree.
  370. */
  371. if (!(dst_vma->vm_flags & VM_SHARED)) {
  372. if (!zeropage)
  373. err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
  374. dst_addr, src_addr, page);
  375. else
  376. err = mfill_zeropage_pte(dst_mm, dst_pmd,
  377. dst_vma, dst_addr);
  378. } else {
  379. if (!zeropage)
  380. err = shmem_mcopy_atomic_pte(dst_mm, dst_pmd,
  381. dst_vma, dst_addr,
  382. src_addr, page);
  383. else
  384. err = shmem_mfill_zeropage_pte(dst_mm, dst_pmd,
  385. dst_vma, dst_addr);
  386. }
  387. return err;
  388. }
  389. static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
  390. unsigned long dst_start,
  391. unsigned long src_start,
  392. unsigned long len,
  393. bool zeropage,
  394. bool *mmap_changing)
  395. {
  396. struct vm_area_struct *dst_vma;
  397. ssize_t err;
  398. pmd_t *dst_pmd;
  399. unsigned long src_addr, dst_addr;
  400. long copied;
  401. struct page *page;
  402. /*
  403. * Sanitize the command parameters:
  404. */
  405. BUG_ON(dst_start & ~PAGE_MASK);
  406. BUG_ON(len & ~PAGE_MASK);
  407. /* Does the address range wrap, or is the span zero-sized? */
  408. BUG_ON(src_start + len <= src_start);
  409. BUG_ON(dst_start + len <= dst_start);
  410. src_addr = src_start;
  411. dst_addr = dst_start;
  412. copied = 0;
  413. page = NULL;
  414. retry:
  415. down_read(&dst_mm->mmap_sem);
  416. /*
  417. * If memory mappings are changing because of non-cooperative
  418. * operation (e.g. mremap) running in parallel, bail out and
  419. * request the user to retry later
  420. */
  421. err = -EAGAIN;
  422. if (mmap_changing && READ_ONCE(*mmap_changing))
  423. goto out_unlock;
  424. /*
  425. * Make sure the vma is not shared, that the dst range is
  426. * both valid and fully within a single existing vma.
  427. */
  428. err = -ENOENT;
  429. dst_vma = find_vma(dst_mm, dst_start);
  430. if (!dst_vma)
  431. goto out_unlock;
  432. /*
  433. * Check the vma is registered in uffd, this is required to
  434. * enforce the VM_MAYWRITE check done at uffd registration
  435. * time.
  436. */
  437. if (!dst_vma->vm_userfaultfd_ctx.ctx)
  438. goto out_unlock;
  439. if (dst_start < dst_vma->vm_start ||
  440. dst_start + len > dst_vma->vm_end)
  441. goto out_unlock;
  442. err = -EINVAL;
  443. /*
  444. * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
  445. * it will overwrite vm_ops, so vma_is_anonymous must return false.
  446. */
  447. if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
  448. dst_vma->vm_flags & VM_SHARED))
  449. goto out_unlock;
  450. /*
  451. * If this is a HUGETLB vma, pass off to appropriate routine
  452. */
  453. if (is_vm_hugetlb_page(dst_vma))
  454. return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
  455. src_start, len, zeropage);
  456. if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
  457. goto out_unlock;
  458. /*
  459. * Ensure the dst_vma has a anon_vma or this page
  460. * would get a NULL anon_vma when moved in the
  461. * dst_vma.
  462. */
  463. err = -ENOMEM;
  464. if (!(dst_vma->vm_flags & VM_SHARED) &&
  465. unlikely(anon_vma_prepare(dst_vma)))
  466. goto out_unlock;
  467. while (src_addr < src_start + len) {
  468. pmd_t dst_pmdval;
  469. BUG_ON(dst_addr >= dst_start + len);
  470. dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
  471. if (unlikely(!dst_pmd)) {
  472. err = -ENOMEM;
  473. break;
  474. }
  475. dst_pmdval = pmd_read_atomic(dst_pmd);
  476. /*
  477. * If the dst_pmd is mapped as THP don't
  478. * override it and just be strict.
  479. */
  480. if (unlikely(pmd_trans_huge(dst_pmdval))) {
  481. err = -EEXIST;
  482. break;
  483. }
  484. if (unlikely(pmd_none(dst_pmdval)) &&
  485. unlikely(__pte_alloc(dst_mm, dst_pmd, dst_addr))) {
  486. err = -ENOMEM;
  487. break;
  488. }
  489. /* If an huge pmd materialized from under us fail */
  490. if (unlikely(pmd_trans_huge(*dst_pmd))) {
  491. err = -EFAULT;
  492. break;
  493. }
  494. BUG_ON(pmd_none(*dst_pmd));
  495. BUG_ON(pmd_trans_huge(*dst_pmd));
  496. err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
  497. src_addr, &page, zeropage);
  498. cond_resched();
  499. if (unlikely(err == -ENOENT)) {
  500. void *page_kaddr;
  501. up_read(&dst_mm->mmap_sem);
  502. BUG_ON(!page);
  503. page_kaddr = kmap(page);
  504. err = copy_from_user(page_kaddr,
  505. (const void __user *) src_addr,
  506. PAGE_SIZE);
  507. kunmap(page);
  508. if (unlikely(err)) {
  509. err = -EFAULT;
  510. goto out;
  511. }
  512. goto retry;
  513. } else
  514. BUG_ON(page);
  515. if (!err) {
  516. dst_addr += PAGE_SIZE;
  517. src_addr += PAGE_SIZE;
  518. copied += PAGE_SIZE;
  519. if (fatal_signal_pending(current))
  520. err = -EINTR;
  521. }
  522. if (err)
  523. break;
  524. }
  525. out_unlock:
  526. up_read(&dst_mm->mmap_sem);
  527. out:
  528. if (page)
  529. put_page(page);
  530. BUG_ON(copied < 0);
  531. BUG_ON(err > 0);
  532. BUG_ON(!copied && !err);
  533. return copied ? copied : err;
  534. }
  535. ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
  536. unsigned long src_start, unsigned long len,
  537. bool *mmap_changing)
  538. {
  539. return __mcopy_atomic(dst_mm, dst_start, src_start, len, false,
  540. mmap_changing);
  541. }
  542. ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
  543. unsigned long len, bool *mmap_changing)
  544. {
  545. return __mcopy_atomic(dst_mm, start, 0, len, true, mmap_changing);
  546. }