userfaultfd.c 16 KB

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