userfaultfd.c 15 KB

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