1 // SPDX-License-Identifier: GPL-2.0
5 * (C) Copyright 1994 Linus Torvalds
6 * (C) Copyright 2002 Christoph Hellwig
8 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
9 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
12 #include <linux/pagewalk.h>
13 #include <linux/hugetlb.h>
14 #include <linux/shm.h>
15 #include <linux/mman.h>
17 #include <linux/highmem.h>
18 #include <linux/security.h>
19 #include <linux/mempolicy.h>
20 #include <linux/personality.h>
21 #include <linux/syscalls.h>
22 #include <linux/swap.h>
23 #include <linux/swapops.h>
24 #include <linux/mmu_notifier.h>
25 #include <linux/migrate.h>
26 #include <linux/perf_event.h>
27 #include <linux/pkeys.h>
28 #include <linux/ksm.h>
29 #include <linux/uaccess.h>
30 #include <linux/mm_inline.h>
31 #include <linux/pgtable.h>
32 #include <linux/sched/sysctl.h>
33 #include <asm/cacheflush.h>
34 #include <asm/mmu_context.h>
35 #include <asm/tlbflush.h>
39 static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
40 unsigned long addr, unsigned long end, pgprot_t newprot,
41 unsigned long cp_flags)
45 unsigned long pages = 0;
46 int target_node = NUMA_NO_NODE;
47 bool dirty_accountable = cp_flags & MM_CP_DIRTY_ACCT;
48 bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
49 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
50 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
53 * Can be called with only the mmap_lock for reading by
54 * prot_numa so we must check the pmd isn't constantly
55 * changing from under us from pmd_none to pmd_trans_huge
56 * and/or the other way around.
58 if (pmd_trans_unstable(pmd))
62 * The pmd points to a regular pte so the pmd can't change
63 * from under us even if the mmap_lock is only hold for
66 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
68 /* Get target node for single threaded private VMAs */
69 if (prot_numa && !(vma->vm_flags & VM_SHARED) &&
70 atomic_read(&vma->vm_mm->mm_users) == 1)
71 target_node = numa_node_id();
73 flush_tlb_batched_pending(vma->vm_mm);
74 arch_enter_lazy_mmu_mode();
77 if (pte_present(oldpte)) {
79 bool preserve_write = prot_numa && pte_write(oldpte);
82 * Avoid trapping faults against the zero or KSM
83 * pages. See similar comment in change_huge_pmd.
89 /* Avoid TLB flush if possible */
90 if (pte_protnone(oldpte))
93 page = vm_normal_page(vma, addr, oldpte);
94 if (!page || PageKsm(page))
97 /* Also skip shared copy-on-write pages */
98 if (is_cow_mapping(vma->vm_flags) &&
99 page_count(page) != 1)
103 * While migration can move some dirty pages,
104 * it cannot move them all from MIGRATE_ASYNC
107 if (page_is_file_lru(page) && PageDirty(page))
111 * Don't mess with PTEs if page is already on the node
112 * a single-threaded process is running on.
114 nid = page_to_nid(page);
115 if (target_node == nid)
119 * Skip scanning top tier node if normal numa
120 * balancing is disabled
122 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
123 node_is_toptier(nid))
127 oldpte = ptep_modify_prot_start(vma, addr, pte);
128 ptent = pte_modify(oldpte, newprot);
130 ptent = pte_mk_savedwrite(ptent);
133 ptent = pte_wrprotect(ptent);
134 ptent = pte_mkuffd_wp(ptent);
135 } else if (uffd_wp_resolve) {
137 * Leave the write bit to be handled
138 * by PF interrupt handler, then
139 * things like COW could be properly
142 ptent = pte_clear_uffd_wp(ptent);
145 /* Avoid taking write faults for known dirty pages */
146 if (dirty_accountable && pte_dirty(ptent) &&
147 (pte_soft_dirty(ptent) ||
148 !(vma->vm_flags & VM_SOFTDIRTY))) {
149 ptent = pte_mkwrite(ptent);
151 ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
153 } else if (is_swap_pte(oldpte)) {
154 swp_entry_t entry = pte_to_swp_entry(oldpte);
157 if (is_writable_migration_entry(entry)) {
159 * A protection check is difficult so
160 * just be safe and disable write
162 entry = make_readable_migration_entry(
164 newpte = swp_entry_to_pte(entry);
165 if (pte_swp_soft_dirty(oldpte))
166 newpte = pte_swp_mksoft_dirty(newpte);
167 if (pte_swp_uffd_wp(oldpte))
168 newpte = pte_swp_mkuffd_wp(newpte);
169 } else if (is_writable_device_private_entry(entry)) {
171 * We do not preserve soft-dirtiness. See
172 * copy_one_pte() for explanation.
174 entry = make_readable_device_private_entry(
176 newpte = swp_entry_to_pte(entry);
177 if (pte_swp_uffd_wp(oldpte))
178 newpte = pte_swp_mkuffd_wp(newpte);
179 } else if (is_writable_device_exclusive_entry(entry)) {
180 entry = make_readable_device_exclusive_entry(
182 newpte = swp_entry_to_pte(entry);
183 if (pte_swp_soft_dirty(oldpte))
184 newpte = pte_swp_mksoft_dirty(newpte);
185 if (pte_swp_uffd_wp(oldpte))
186 newpte = pte_swp_mkuffd_wp(newpte);
192 newpte = pte_swp_mkuffd_wp(newpte);
193 else if (uffd_wp_resolve)
194 newpte = pte_swp_clear_uffd_wp(newpte);
196 if (!pte_same(oldpte, newpte)) {
197 set_pte_at(vma->vm_mm, addr, pte, newpte);
201 } while (pte++, addr += PAGE_SIZE, addr != end);
202 arch_leave_lazy_mmu_mode();
203 pte_unmap_unlock(pte - 1, ptl);
209 * Used when setting automatic NUMA hinting protection where it is
210 * critical that a numa hinting PMD is not confused with a bad PMD.
212 static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd)
214 pmd_t pmdval = pmd_read_atomic(pmd);
216 /* See pmd_none_or_trans_huge_or_clear_bad for info on barrier */
217 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
221 if (pmd_none(pmdval))
223 if (pmd_trans_huge(pmdval))
225 if (unlikely(pmd_bad(pmdval))) {
233 static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
234 pud_t *pud, unsigned long addr, unsigned long end,
235 pgprot_t newprot, unsigned long cp_flags)
239 unsigned long pages = 0;
240 unsigned long nr_huge_updates = 0;
241 struct mmu_notifier_range range;
245 pmd = pmd_offset(pud, addr);
247 unsigned long this_pages;
249 next = pmd_addr_end(addr, end);
252 * Automatic NUMA balancing walks the tables with mmap_lock
253 * held for read. It's possible a parallel update to occur
254 * between pmd_trans_huge() and a pmd_none_or_clear_bad()
255 * check leading to a false positive and clearing.
256 * Hence, it's necessary to atomically read the PMD value
257 * for all the checks.
259 if (!is_swap_pmd(*pmd) && !pmd_devmap(*pmd) &&
260 pmd_none_or_clear_bad_unless_trans_huge(pmd))
263 /* invoke the mmu notifier if the pmd is populated */
265 mmu_notifier_range_init(&range,
266 MMU_NOTIFY_PROTECTION_VMA, 0,
267 vma, vma->vm_mm, addr, end);
268 mmu_notifier_invalidate_range_start(&range);
271 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
272 if (next - addr != HPAGE_PMD_SIZE) {
273 __split_huge_pmd(vma, pmd, addr, false, NULL);
275 int nr_ptes = change_huge_pmd(vma, pmd, addr,
279 if (nr_ptes == HPAGE_PMD_NR) {
280 pages += HPAGE_PMD_NR;
284 /* huge pmd was handled */
288 /* fall through, the trans huge pmd just split */
290 this_pages = change_pte_range(vma, pmd, addr, next, newprot,
295 } while (pmd++, addr = next, addr != end);
298 mmu_notifier_invalidate_range_end(&range);
301 count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
305 static inline unsigned long change_pud_range(struct vm_area_struct *vma,
306 p4d_t *p4d, unsigned long addr, unsigned long end,
307 pgprot_t newprot, unsigned long cp_flags)
311 unsigned long pages = 0;
313 pud = pud_offset(p4d, addr);
315 next = pud_addr_end(addr, end);
316 if (pud_none_or_clear_bad(pud))
318 pages += change_pmd_range(vma, pud, addr, next, newprot,
320 } while (pud++, addr = next, addr != end);
325 static inline unsigned long change_p4d_range(struct vm_area_struct *vma,
326 pgd_t *pgd, unsigned long addr, unsigned long end,
327 pgprot_t newprot, unsigned long cp_flags)
331 unsigned long pages = 0;
333 p4d = p4d_offset(pgd, addr);
335 next = p4d_addr_end(addr, end);
336 if (p4d_none_or_clear_bad(p4d))
338 pages += change_pud_range(vma, p4d, addr, next, newprot,
340 } while (p4d++, addr = next, addr != end);
345 static unsigned long change_protection_range(struct vm_area_struct *vma,
346 unsigned long addr, unsigned long end, pgprot_t newprot,
347 unsigned long cp_flags)
349 struct mm_struct *mm = vma->vm_mm;
352 unsigned long start = addr;
353 unsigned long pages = 0;
356 pgd = pgd_offset(mm, addr);
357 flush_cache_range(vma, addr, end);
358 inc_tlb_flush_pending(mm);
360 next = pgd_addr_end(addr, end);
361 if (pgd_none_or_clear_bad(pgd))
363 pages += change_p4d_range(vma, pgd, addr, next, newprot,
365 } while (pgd++, addr = next, addr != end);
367 /* Only flush the TLB if we actually modified any entries: */
369 flush_tlb_range(vma, start, end);
370 dec_tlb_flush_pending(mm);
375 unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
376 unsigned long end, pgprot_t newprot,
377 unsigned long cp_flags)
381 BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
383 if (is_vm_hugetlb_page(vma))
384 pages = hugetlb_change_protection(vma, start, end, newprot);
386 pages = change_protection_range(vma, start, end, newprot,
392 static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
393 unsigned long next, struct mm_walk *walk)
395 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
399 static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
400 unsigned long addr, unsigned long next,
401 struct mm_walk *walk)
403 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
407 static int prot_none_test(unsigned long addr, unsigned long next,
408 struct mm_walk *walk)
413 static const struct mm_walk_ops prot_none_walk_ops = {
414 .pte_entry = prot_none_pte_entry,
415 .hugetlb_entry = prot_none_hugetlb_entry,
416 .test_walk = prot_none_test,
420 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
421 unsigned long start, unsigned long end, unsigned long newflags)
423 struct mm_struct *mm = vma->vm_mm;
424 unsigned long oldflags = vma->vm_flags;
425 long nrpages = (end - start) >> PAGE_SHIFT;
426 unsigned long charged = 0;
429 int dirty_accountable = 0;
431 if (newflags == oldflags) {
437 * Do PROT_NONE PFN permission checks here when we can still
438 * bail out without undoing a lot of state. This is a rather
439 * uncommon case, so doesn't need to be very optimized.
441 if (arch_has_pfn_modify_check() &&
442 (vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
443 (newflags & VM_ACCESS_FLAGS) == 0) {
444 pgprot_t new_pgprot = vm_get_page_prot(newflags);
446 error = walk_page_range(current->mm, start, end,
447 &prot_none_walk_ops, &new_pgprot);
453 * If we make a private mapping writable we increase our commit;
454 * but (without finer accounting) cannot reduce our commit if we
455 * make it unwritable again. hugetlb mapping were accounted for
456 * even if read-only so there is no need to account for them here
458 if (newflags & VM_WRITE) {
459 /* Check space limits when area turns into data. */
460 if (!may_expand_vm(mm, newflags, nrpages) &&
461 may_expand_vm(mm, oldflags, nrpages))
463 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
464 VM_SHARED|VM_NORESERVE))) {
466 if (security_vm_enough_memory_mm(mm, charged))
468 newflags |= VM_ACCOUNT;
473 * First try to merge with previous and/or next vma.
475 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
476 *pprev = vma_merge(mm, *pprev, start, end, newflags,
477 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
478 vma->vm_userfaultfd_ctx, anon_vma_name(vma));
481 VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
487 if (start != vma->vm_start) {
488 error = split_vma(mm, vma, start, 1);
493 if (end != vma->vm_end) {
494 error = split_vma(mm, vma, end, 0);
501 * vm_flags and vm_page_prot are protected by the mmap_lock
502 * held in write mode.
504 vma->vm_flags = newflags;
505 dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
506 vma_set_page_prot(vma);
508 change_protection(vma, start, end, vma->vm_page_prot,
509 dirty_accountable ? MM_CP_DIRTY_ACCT : 0);
512 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
515 if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
516 (newflags & VM_WRITE)) {
517 populate_vma_page_range(vma, start, end, NULL);
520 vm_stat_account(mm, oldflags, -nrpages);
521 vm_stat_account(mm, newflags, nrpages);
522 perf_event_mmap(vma);
526 vm_unacct_memory(charged);
531 * pkey==-1 when doing a legacy mprotect()
533 static int do_mprotect_pkey(unsigned long start, size_t len,
534 unsigned long prot, int pkey)
536 unsigned long nstart, end, tmp, reqprot;
537 struct vm_area_struct *vma, *prev;
539 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
540 const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
543 start = untagged_addr(start);
545 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
546 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
549 if (start & ~PAGE_MASK)
553 len = PAGE_ALIGN(len);
557 if (!arch_validate_prot(prot, start))
562 if (mmap_write_lock_killable(current->mm))
566 * If userspace did not allocate the pkey, do not let
570 if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
573 vma = find_vma(current->mm, start);
578 if (unlikely(grows & PROT_GROWSDOWN)) {
579 if (vma->vm_start >= end)
581 start = vma->vm_start;
583 if (!(vma->vm_flags & VM_GROWSDOWN))
586 if (vma->vm_start > start)
588 if (unlikely(grows & PROT_GROWSUP)) {
591 if (!(vma->vm_flags & VM_GROWSUP))
596 if (start > vma->vm_start)
601 for (nstart = start ; ; ) {
602 unsigned long mask_off_old_flags;
603 unsigned long newflags;
606 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
608 /* Does the application expect PROT_READ to imply PROT_EXEC */
609 if (rier && (vma->vm_flags & VM_MAYEXEC))
613 * Each mprotect() call explicitly passes r/w/x permissions.
614 * If a permission is not passed to mprotect(), it must be
615 * cleared from the VMA.
617 mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
620 new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
621 newflags = calc_vm_prot_bits(prot, new_vma_pkey);
622 newflags |= (vma->vm_flags & ~mask_off_old_flags);
624 /* newflags >> 4 shift VM_MAY% in place of VM_% */
625 if ((newflags & ~(newflags >> 4)) & VM_ACCESS_FLAGS) {
630 /* Allow architectures to sanity-check the new flags */
631 if (!arch_validate_flags(newflags)) {
636 error = security_file_mprotect(vma, reqprot, prot);
644 if (vma->vm_ops && vma->vm_ops->mprotect) {
645 error = vma->vm_ops->mprotect(vma, nstart, tmp, newflags);
650 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
656 if (nstart < prev->vm_end)
657 nstart = prev->vm_end;
662 if (!vma || vma->vm_start != nstart) {
669 mmap_write_unlock(current->mm);
673 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
676 return do_mprotect_pkey(start, len, prot, -1);
679 #ifdef CONFIG_ARCH_HAS_PKEYS
681 SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
682 unsigned long, prot, int, pkey)
684 return do_mprotect_pkey(start, len, prot, pkey);
687 SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
692 /* No flags supported yet. */
695 /* check for unsupported init values */
696 if (init_val & ~PKEY_ACCESS_MASK)
699 mmap_write_lock(current->mm);
700 pkey = mm_pkey_alloc(current->mm);
706 ret = arch_set_user_pkey_access(current, pkey, init_val);
708 mm_pkey_free(current->mm, pkey);
713 mmap_write_unlock(current->mm);
717 SYSCALL_DEFINE1(pkey_free, int, pkey)
721 mmap_write_lock(current->mm);
722 ret = mm_pkey_free(current->mm, pkey);
723 mmap_write_unlock(current->mm);
726 * We could provide warnings or errors if any VMA still
727 * has the pkey set here.
732 #endif /* CONFIG_ARCH_HAS_PKEYS */