userfaultfd.c 15 KB

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