mm/mprotect: drop pgprot_t parameter from change_protection()
authorDavid Hildenbrand <david@redhat.com>
Fri, 23 Dec 2022 15:56:16 +0000 (16:56 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 19 Jan 2023 01:12:50 +0000 (17:12 -0800)
Being able to provide a custom protection opens the door for
inconsistencies and BUGs: for example, accidentally allowing for more
permissions than desired by other mechanisms (e.g., softdirty tracking).
vma->vm_page_prot should be the single source of truth.

Only PROT_NUMA is special: there is no way we can erroneously allow
for more permissions when removing all permissions. Special-case using
the MM_CP_PROT_NUMA flag.

[david@redhat.com: PAGE_NONE might not be defined without CONFIG_NUMA_BALANCING]
Link: https://lkml.kernel.org/r/5084ff1c-ebb3-f918-6a60-bacabf550a88@redhat.com
Link: https://lkml.kernel.org/r/20221223155616.297723-3-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Nadav Amit <nadav.amit@gmail.com>
Cc: Peter Xu <peterx@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/mm.h
mm/mempolicy.c
mm/mprotect.c
mm/userfaultfd.c

index d68579b..329ed67 100644 (file)
@@ -2134,8 +2134,7 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
                             pte_t pte);
 extern unsigned long change_protection(struct mmu_gather *tlb,
                              struct vm_area_struct *vma, unsigned long start,
-                             unsigned long end, pgprot_t newprot,
-                             unsigned long cp_flags);
+                             unsigned long end, unsigned long cp_flags);
 extern int mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
                          struct vm_area_struct **pprev, unsigned long start,
                          unsigned long end, unsigned long newflags);
index becf41e..d355824 100644 (file)
@@ -635,8 +635,7 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,
 
        tlb_gather_mmu(&tlb, vma->vm_mm);
 
-       nr_updated = change_protection(&tlb, vma, addr, end, PAGE_NONE,
-                                      MM_CP_PROT_NUMA);
+       nr_updated = change_protection(&tlb, vma, addr, end, MM_CP_PROT_NUMA);
        if (nr_updated)
                count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated);
 
index bf8fa0a..71358e4 100644 (file)
@@ -507,13 +507,25 @@ static unsigned long change_protection_range(struct mmu_gather *tlb,
 
 unsigned long change_protection(struct mmu_gather *tlb,
                       struct vm_area_struct *vma, unsigned long start,
-                      unsigned long end, pgprot_t newprot,
-                      unsigned long cp_flags)
+                      unsigned long end, unsigned long cp_flags)
 {
+       pgprot_t newprot = vma->vm_page_prot;
        unsigned long pages;
 
        BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
 
+#ifdef CONFIG_NUMA_BALANCING
+       /*
+        * Ordinary protection updates (mprotect, uffd-wp, softdirty tracking)
+        * are expected to reflect their requirements via VMA flags such that
+        * vma_set_page_prot() will adjust vma->vm_page_prot accordingly.
+        */
+       if (cp_flags & MM_CP_PROT_NUMA)
+               newprot = PAGE_NONE;
+#else
+       WARN_ON_ONCE(cp_flags & MM_CP_PROT_NUMA);
+#endif
+
        if (is_vm_hugetlb_page(vma))
                pages = hugetlb_change_protection(vma, start, end, newprot,
                                                  cp_flags);
@@ -642,7 +654,7 @@ success:
                mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
        vma_set_page_prot(vma);
 
-       change_protection(tlb, vma, start, end, vma->vm_page_prot, mm_cp_flags);
+       change_protection(tlb, vma, start, end, mm_cp_flags);
 
        /*
         * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
index 4677136..65ad172 100644 (file)
@@ -730,8 +730,7 @@ void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
        if (!enable_wp && vma_wants_manual_pte_write_upgrade(dst_vma))
                mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
        tlb_gather_mmu(&tlb, dst_mm);
-       change_protection(&tlb, dst_vma, start, start + len, vma->vm_page_prot,
-                         mm_cp_flags);
+       change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags);
        tlb_finish_mmu(&tlb);
 }