1 // SPDX-License-Identifier: GPL-2.0
5 * (C) Copyright 1994 Linus Torvalds
6 * (C) Copyright 2002 Christoph Hellwig
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>
40 static unsigned long change_pte_range(struct mmu_gather *tlb,
41 struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
42 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
46 unsigned long pages = 0;
47 int target_node = NUMA_NO_NODE;
48 bool dirty_accountable = cp_flags & MM_CP_DIRTY_ACCT;
49 bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
50 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
51 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
53 tlb_change_page_size(tlb, PAGE_SIZE);
56 * Can be called with only the mmap_lock for reading by
57 * prot_numa so we must check the pmd isn't constantly
58 * changing from under us from pmd_none to pmd_trans_huge
59 * and/or the other way around.
61 if (pmd_trans_unstable(pmd))
65 * The pmd points to a regular pte so the pmd can't change
66 * from under us even if the mmap_lock is only hold for
69 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
71 /* Get target node for single threaded private VMAs */
72 if (prot_numa && !(vma->vm_flags & VM_SHARED) &&
73 atomic_read(&vma->vm_mm->mm_users) == 1)
74 target_node = numa_node_id();
76 flush_tlb_batched_pending(vma->vm_mm);
77 arch_enter_lazy_mmu_mode();
80 if (pte_present(oldpte)) {
82 bool preserve_write = prot_numa && pte_write(oldpte);
85 * Avoid trapping faults against the zero or KSM
86 * pages. See similar comment in change_huge_pmd.
92 /* Avoid TLB flush if possible */
93 if (pte_protnone(oldpte))
96 page = vm_normal_page(vma, addr, oldpte);
97 if (!page || PageKsm(page))
100 /* Also skip shared copy-on-write pages */
101 if (is_cow_mapping(vma->vm_flags) &&
102 page_count(page) != 1)
106 * While migration can move some dirty pages,
107 * it cannot move them all from MIGRATE_ASYNC
110 if (page_is_file_lru(page) && PageDirty(page))
114 * Don't mess with PTEs if page is already on the node
115 * a single-threaded process is running on.
117 nid = page_to_nid(page);
118 if (target_node == nid)
122 * Skip scanning top tier node if normal numa
123 * balancing is disabled
125 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
126 node_is_toptier(nid))
130 oldpte = ptep_modify_prot_start(vma, addr, pte);
131 ptent = pte_modify(oldpte, newprot);
133 ptent = pte_mk_savedwrite(ptent);
136 ptent = pte_wrprotect(ptent);
137 ptent = pte_mkuffd_wp(ptent);
138 } else if (uffd_wp_resolve) {
140 * Leave the write bit to be handled
141 * by PF interrupt handler, then
142 * things like COW could be properly
145 ptent = pte_clear_uffd_wp(ptent);
148 /* Avoid taking write faults for known dirty pages */
149 if (dirty_accountable && pte_dirty(ptent) &&
150 (pte_soft_dirty(ptent) ||
151 !(vma->vm_flags & VM_SOFTDIRTY))) {
152 ptent = pte_mkwrite(ptent);
154 ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
155 tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
157 } else if (is_swap_pte(oldpte)) {
158 swp_entry_t entry = pte_to_swp_entry(oldpte);
159 struct page *page = pfn_swap_entry_to_page(entry);
162 if (is_writable_migration_entry(entry)) {
164 * A protection check is difficult so
165 * just be safe and disable write
168 entry = make_readable_exclusive_migration_entry(
171 entry = make_readable_migration_entry(swp_offset(entry));
172 newpte = swp_entry_to_pte(entry);
173 if (pte_swp_soft_dirty(oldpte))
174 newpte = pte_swp_mksoft_dirty(newpte);
175 if (pte_swp_uffd_wp(oldpte))
176 newpte = pte_swp_mkuffd_wp(newpte);
177 } else if (is_writable_device_private_entry(entry)) {
179 * We do not preserve soft-dirtiness. See
180 * copy_one_pte() for explanation.
182 entry = make_readable_device_private_entry(
184 newpte = swp_entry_to_pte(entry);
185 if (pte_swp_uffd_wp(oldpte))
186 newpte = pte_swp_mkuffd_wp(newpte);
187 } else if (is_writable_device_exclusive_entry(entry)) {
188 entry = make_readable_device_exclusive_entry(
190 newpte = swp_entry_to_pte(entry);
191 if (pte_swp_soft_dirty(oldpte))
192 newpte = pte_swp_mksoft_dirty(newpte);
193 if (pte_swp_uffd_wp(oldpte))
194 newpte = pte_swp_mkuffd_wp(newpte);
200 newpte = pte_swp_mkuffd_wp(newpte);
201 else if (uffd_wp_resolve)
202 newpte = pte_swp_clear_uffd_wp(newpte);
204 if (!pte_same(oldpte, newpte)) {
205 set_pte_at(vma->vm_mm, addr, pte, newpte);
209 } while (pte++, addr += PAGE_SIZE, addr != end);
210 arch_leave_lazy_mmu_mode();
211 pte_unmap_unlock(pte - 1, ptl);
217 * Used when setting automatic NUMA hinting protection where it is
218 * critical that a numa hinting PMD is not confused with a bad PMD.
220 static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd)
222 pmd_t pmdval = pmd_read_atomic(pmd);
224 /* See pmd_none_or_trans_huge_or_clear_bad for info on barrier */
225 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
229 if (pmd_none(pmdval))
231 if (pmd_trans_huge(pmdval))
233 if (unlikely(pmd_bad(pmdval))) {
241 static inline unsigned long change_pmd_range(struct mmu_gather *tlb,
242 struct vm_area_struct *vma, pud_t *pud, unsigned long addr,
243 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
247 unsigned long pages = 0;
248 unsigned long nr_huge_updates = 0;
249 struct mmu_notifier_range range;
253 pmd = pmd_offset(pud, addr);
255 unsigned long this_pages;
257 next = pmd_addr_end(addr, end);
260 * Automatic NUMA balancing walks the tables with mmap_lock
261 * held for read. It's possible a parallel update to occur
262 * between pmd_trans_huge() and a pmd_none_or_clear_bad()
263 * check leading to a false positive and clearing.
264 * Hence, it's necessary to atomically read the PMD value
265 * for all the checks.
267 if (!is_swap_pmd(*pmd) && !pmd_devmap(*pmd) &&
268 pmd_none_or_clear_bad_unless_trans_huge(pmd))
271 /* invoke the mmu notifier if the pmd is populated */
273 mmu_notifier_range_init(&range,
274 MMU_NOTIFY_PROTECTION_VMA, 0,
275 vma, vma->vm_mm, addr, end);
276 mmu_notifier_invalidate_range_start(&range);
279 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
280 if (next - addr != HPAGE_PMD_SIZE) {
281 __split_huge_pmd(vma, pmd, addr, false, NULL);
284 * change_huge_pmd() does not defer TLB flushes,
285 * so no need to propagate the tlb argument.
287 int nr_ptes = change_huge_pmd(tlb, vma, pmd,
288 addr, newprot, cp_flags);
291 if (nr_ptes == HPAGE_PMD_NR) {
292 pages += HPAGE_PMD_NR;
296 /* huge pmd was handled */
300 /* fall through, the trans huge pmd just split */
302 this_pages = change_pte_range(tlb, vma, pmd, addr, next,
307 } while (pmd++, addr = next, addr != end);
310 mmu_notifier_invalidate_range_end(&range);
313 count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
317 static inline unsigned long change_pud_range(struct mmu_gather *tlb,
318 struct vm_area_struct *vma, p4d_t *p4d, unsigned long addr,
319 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
323 unsigned long pages = 0;
325 pud = pud_offset(p4d, addr);
327 next = pud_addr_end(addr, end);
328 if (pud_none_or_clear_bad(pud))
330 pages += change_pmd_range(tlb, vma, pud, addr, next, newprot,
332 } while (pud++, addr = next, addr != end);
337 static inline unsigned long change_p4d_range(struct mmu_gather *tlb,
338 struct vm_area_struct *vma, pgd_t *pgd, unsigned long addr,
339 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
343 unsigned long pages = 0;
345 p4d = p4d_offset(pgd, addr);
347 next = p4d_addr_end(addr, end);
348 if (p4d_none_or_clear_bad(p4d))
350 pages += change_pud_range(tlb, vma, p4d, addr, next, newprot,
352 } while (p4d++, addr = next, addr != end);
357 static unsigned long change_protection_range(struct mmu_gather *tlb,
358 struct vm_area_struct *vma, unsigned long addr,
359 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
361 struct mm_struct *mm = vma->vm_mm;
364 unsigned long pages = 0;
367 pgd = pgd_offset(mm, addr);
368 tlb_start_vma(tlb, vma);
370 next = pgd_addr_end(addr, end);
371 if (pgd_none_or_clear_bad(pgd))
373 pages += change_p4d_range(tlb, vma, pgd, addr, next, newprot,
375 } while (pgd++, addr = next, addr != end);
377 tlb_end_vma(tlb, vma);
382 unsigned long change_protection(struct mmu_gather *tlb,
383 struct vm_area_struct *vma, unsigned long start,
384 unsigned long end, pgprot_t newprot,
385 unsigned long cp_flags)
389 BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
391 if (is_vm_hugetlb_page(vma))
392 pages = hugetlb_change_protection(vma, start, end, newprot);
394 pages = change_protection_range(tlb, vma, start, end, newprot,
400 static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
401 unsigned long next, struct mm_walk *walk)
403 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
407 static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
408 unsigned long addr, unsigned long next,
409 struct mm_walk *walk)
411 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
415 static int prot_none_test(unsigned long addr, unsigned long next,
416 struct mm_walk *walk)
421 static const struct mm_walk_ops prot_none_walk_ops = {
422 .pte_entry = prot_none_pte_entry,
423 .hugetlb_entry = prot_none_hugetlb_entry,
424 .test_walk = prot_none_test,
428 mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
429 struct vm_area_struct **pprev, unsigned long start,
430 unsigned long end, unsigned long newflags)
432 struct mm_struct *mm = vma->vm_mm;
433 unsigned long oldflags = vma->vm_flags;
434 long nrpages = (end - start) >> PAGE_SHIFT;
435 unsigned long charged = 0;
438 int dirty_accountable = 0;
440 if (newflags == oldflags) {
446 * Do PROT_NONE PFN permission checks here when we can still
447 * bail out without undoing a lot of state. This is a rather
448 * uncommon case, so doesn't need to be very optimized.
450 if (arch_has_pfn_modify_check() &&
451 (vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
452 (newflags & VM_ACCESS_FLAGS) == 0) {
453 pgprot_t new_pgprot = vm_get_page_prot(newflags);
455 error = walk_page_range(current->mm, start, end,
456 &prot_none_walk_ops, &new_pgprot);
462 * If we make a private mapping writable we increase our commit;
463 * but (without finer accounting) cannot reduce our commit if we
464 * make it unwritable again. hugetlb mapping were accounted for
465 * even if read-only so there is no need to account for them here
467 if (newflags & VM_WRITE) {
468 /* Check space limits when area turns into data. */
469 if (!may_expand_vm(mm, newflags, nrpages) &&
470 may_expand_vm(mm, oldflags, nrpages))
472 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
473 VM_SHARED|VM_NORESERVE))) {
475 if (security_vm_enough_memory_mm(mm, charged))
477 newflags |= VM_ACCOUNT;
482 * First try to merge with previous and/or next vma.
484 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
485 *pprev = vma_merge(mm, *pprev, start, end, newflags,
486 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
487 vma->vm_userfaultfd_ctx, anon_vma_name(vma));
490 VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
496 if (start != vma->vm_start) {
497 error = split_vma(mm, vma, start, 1);
502 if (end != vma->vm_end) {
503 error = split_vma(mm, vma, end, 0);
510 * vm_flags and vm_page_prot are protected by the mmap_lock
511 * held in write mode.
513 vma->vm_flags = newflags;
514 dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
515 vma_set_page_prot(vma);
517 change_protection(tlb, vma, start, end, vma->vm_page_prot,
518 dirty_accountable ? MM_CP_DIRTY_ACCT : 0);
521 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
524 if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
525 (newflags & VM_WRITE)) {
526 populate_vma_page_range(vma, start, end, NULL);
529 vm_stat_account(mm, oldflags, -nrpages);
530 vm_stat_account(mm, newflags, nrpages);
531 perf_event_mmap(vma);
535 vm_unacct_memory(charged);
540 * pkey==-1 when doing a legacy mprotect()
542 static int do_mprotect_pkey(unsigned long start, size_t len,
543 unsigned long prot, int pkey)
545 unsigned long nstart, end, tmp, reqprot;
546 struct vm_area_struct *vma, *prev;
548 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
549 const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
551 struct mmu_gather tlb;
553 start = untagged_addr(start);
555 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
556 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
559 if (start & ~PAGE_MASK)
563 len = PAGE_ALIGN(len);
567 if (!arch_validate_prot(prot, start))
572 if (mmap_write_lock_killable(current->mm))
576 * If userspace did not allocate the pkey, do not let
580 if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
583 vma = find_vma(current->mm, start);
588 if (unlikely(grows & PROT_GROWSDOWN)) {
589 if (vma->vm_start >= end)
591 start = vma->vm_start;
593 if (!(vma->vm_flags & VM_GROWSDOWN))
596 if (vma->vm_start > start)
598 if (unlikely(grows & PROT_GROWSUP)) {
601 if (!(vma->vm_flags & VM_GROWSUP))
606 if (start > vma->vm_start)
611 tlb_gather_mmu(&tlb, current->mm);
612 for (nstart = start ; ; ) {
613 unsigned long mask_off_old_flags;
614 unsigned long newflags;
617 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
619 /* Does the application expect PROT_READ to imply PROT_EXEC */
620 if (rier && (vma->vm_flags & VM_MAYEXEC))
624 * Each mprotect() call explicitly passes r/w/x permissions.
625 * If a permission is not passed to mprotect(), it must be
626 * cleared from the VMA.
628 mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
631 new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
632 newflags = calc_vm_prot_bits(prot, new_vma_pkey);
633 newflags |= (vma->vm_flags & ~mask_off_old_flags);
635 /* newflags >> 4 shift VM_MAY% in place of VM_% */
636 if ((newflags & ~(newflags >> 4)) & VM_ACCESS_FLAGS) {
641 /* Allow architectures to sanity-check the new flags */
642 if (!arch_validate_flags(newflags)) {
647 error = security_file_mprotect(vma, reqprot, prot);
655 if (vma->vm_ops && vma->vm_ops->mprotect) {
656 error = vma->vm_ops->mprotect(vma, nstart, tmp, newflags);
661 error = mprotect_fixup(&tlb, vma, &prev, nstart, tmp, newflags);
667 if (nstart < prev->vm_end)
668 nstart = prev->vm_end;
673 if (!vma || vma->vm_start != nstart) {
679 tlb_finish_mmu(&tlb);
681 mmap_write_unlock(current->mm);
685 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
688 return do_mprotect_pkey(start, len, prot, -1);
691 #ifdef CONFIG_ARCH_HAS_PKEYS
693 SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
694 unsigned long, prot, int, pkey)
696 return do_mprotect_pkey(start, len, prot, pkey);
699 SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
704 /* No flags supported yet. */
707 /* check for unsupported init values */
708 if (init_val & ~PKEY_ACCESS_MASK)
711 mmap_write_lock(current->mm);
712 pkey = mm_pkey_alloc(current->mm);
718 ret = arch_set_user_pkey_access(current, pkey, init_val);
720 mm_pkey_free(current->mm, pkey);
725 mmap_write_unlock(current->mm);
729 SYSCALL_DEFINE1(pkey_free, int, pkey)
733 mmap_write_lock(current->mm);
734 ret = mm_pkey_free(current->mm, pkey);
735 mmap_write_unlock(current->mm);
738 * We could provide warnings or errors if any VMA still
739 * has the pkey set here.
744 #endif /* CONFIG_ARCH_HAS_PKEYS */