userfaultfd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 __mcopy_atomic(struct mm_struct *dst_mm,
  332. unsigned long dst_start,
  333. unsigned long src_start,
  334. unsigned long len,
  335. bool zeropage)
  336. {
  337. struct vm_area_struct *dst_vma;
  338. ssize_t err;
  339. pmd_t *dst_pmd;
  340. unsigned long src_addr, dst_addr;
  341. long copied;
  342. struct page *page;
  343. /*
  344. * Sanitize the command parameters:
  345. */
  346. BUG_ON(dst_start & ~PAGE_MASK);
  347. BUG_ON(len & ~PAGE_MASK);
  348. /* Does the address range wrap, or is the span zero-sized? */
  349. BUG_ON(src_start + len <= src_start);
  350. BUG_ON(dst_start + len <= dst_start);
  351. src_addr = src_start;
  352. dst_addr = dst_start;
  353. copied = 0;
  354. page = NULL;
  355. retry:
  356. down_read(&dst_mm->mmap_sem);
  357. /*
  358. * Make sure the vma is not shared, that the dst range is
  359. * both valid and fully within a single existing vma.
  360. */
  361. err = -ENOENT;
  362. dst_vma = find_vma(dst_mm, dst_start);
  363. if (!dst_vma)
  364. goto out_unlock;
  365. /*
  366. * Be strict and only allow __mcopy_atomic on userfaultfd
  367. * registered ranges to prevent userland errors going
  368. * unnoticed. As far as the VM consistency is concerned, it
  369. * would be perfectly safe to remove this check, but there's
  370. * no useful usage for __mcopy_atomic ouside of userfaultfd
  371. * registered ranges. This is after all why these are ioctls
  372. * belonging to the userfaultfd and not syscalls.
  373. */
  374. if (!dst_vma->vm_userfaultfd_ctx.ctx)
  375. goto out_unlock;
  376. if (dst_start < dst_vma->vm_start ||
  377. dst_start + len > dst_vma->vm_end)
  378. goto out_unlock;
  379. err = -EINVAL;
  380. /*
  381. * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
  382. * it will overwrite vm_ops, so vma_is_anonymous must return false.
  383. */
  384. if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
  385. dst_vma->vm_flags & VM_SHARED))
  386. goto out_unlock;
  387. /*
  388. * If this is a HUGETLB vma, pass off to appropriate routine
  389. */
  390. if (is_vm_hugetlb_page(dst_vma))
  391. return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start,
  392. src_start, len, zeropage);
  393. if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
  394. goto out_unlock;
  395. /*
  396. * Ensure the dst_vma has a anon_vma or this page
  397. * would get a NULL anon_vma when moved in the
  398. * dst_vma.
  399. */
  400. err = -ENOMEM;
  401. if (vma_is_anonymous(dst_vma) && unlikely(anon_vma_prepare(dst_vma)))
  402. goto out_unlock;
  403. while (src_addr < src_start + len) {
  404. pmd_t dst_pmdval;
  405. BUG_ON(dst_addr >= dst_start + len);
  406. dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
  407. if (unlikely(!dst_pmd)) {
  408. err = -ENOMEM;
  409. break;
  410. }
  411. dst_pmdval = pmd_read_atomic(dst_pmd);
  412. /*
  413. * If the dst_pmd is mapped as THP don't
  414. * override it and just be strict.
  415. */
  416. if (unlikely(pmd_trans_huge(dst_pmdval))) {
  417. err = -EEXIST;
  418. break;
  419. }
  420. if (unlikely(pmd_none(dst_pmdval)) &&
  421. unlikely(__pte_alloc(dst_mm, dst_pmd, dst_addr))) {
  422. err = -ENOMEM;
  423. break;
  424. }
  425. /* If an huge pmd materialized from under us fail */
  426. if (unlikely(pmd_trans_huge(*dst_pmd))) {
  427. err = -EFAULT;
  428. break;
  429. }
  430. BUG_ON(pmd_none(*dst_pmd));
  431. BUG_ON(pmd_trans_huge(*dst_pmd));
  432. if (vma_is_anonymous(dst_vma)) {
  433. if (!zeropage)
  434. err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
  435. dst_addr, src_addr,
  436. &page);
  437. else
  438. err = mfill_zeropage_pte(dst_mm, dst_pmd,
  439. dst_vma, dst_addr);
  440. } else {
  441. err = -EINVAL; /* if zeropage is true return -EINVAL */
  442. if (likely(!zeropage))
  443. err = shmem_mcopy_atomic_pte(dst_mm, dst_pmd,
  444. dst_vma, dst_addr,
  445. src_addr, &page);
  446. }
  447. cond_resched();
  448. if (unlikely(err == -EFAULT)) {
  449. void *page_kaddr;
  450. up_read(&dst_mm->mmap_sem);
  451. BUG_ON(!page);
  452. page_kaddr = kmap(page);
  453. err = copy_from_user(page_kaddr,
  454. (const void __user *) src_addr,
  455. PAGE_SIZE);
  456. kunmap(page);
  457. if (unlikely(err)) {
  458. err = -EFAULT;
  459. goto out;
  460. }
  461. goto retry;
  462. } else
  463. BUG_ON(page);
  464. if (!err) {
  465. dst_addr += PAGE_SIZE;
  466. src_addr += PAGE_SIZE;
  467. copied += PAGE_SIZE;
  468. if (fatal_signal_pending(current))
  469. err = -EINTR;
  470. }
  471. if (err)
  472. break;
  473. }
  474. out_unlock:
  475. up_read(&dst_mm->mmap_sem);
  476. out:
  477. if (page)
  478. put_page(page);
  479. BUG_ON(copied < 0);
  480. BUG_ON(err > 0);
  481. BUG_ON(!copied && !err);
  482. return copied ? copied : err;
  483. }
  484. ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start,
  485. unsigned long src_start, unsigned long len)
  486. {
  487. return __mcopy_atomic(dst_mm, dst_start, src_start, len, false);
  488. }
  489. ssize_t mfill_zeropage(struct mm_struct *dst_mm, unsigned long start,
  490. unsigned long len)
  491. {
  492. return __mcopy_atomic(dst_mm, start, 0, len, true);
  493. }