Skip to content

Commit 1ef488e

Browse files
davidhildenbrandakpm00
authored andcommitted
mm/mprotect: drop pgprot_t parameter from change_protection()
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>
1 parent 931298e commit 1ef488e

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

include/linux/mm.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,8 +2134,7 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
21342134
pte_t pte);
21352135
extern unsigned long change_protection(struct mmu_gather *tlb,
21362136
struct vm_area_struct *vma, unsigned long start,
2137-
unsigned long end, pgprot_t newprot,
2138-
unsigned long cp_flags);
2137+
unsigned long end, unsigned long cp_flags);
21392138
extern int mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
21402139
struct vm_area_struct **pprev, unsigned long start,
21412140
unsigned long end, unsigned long newflags);

mm/mempolicy.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,7 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,
635635

636636
tlb_gather_mmu(&tlb, vma->vm_mm);
637637

638-
nr_updated = change_protection(&tlb, vma, addr, end, PAGE_NONE,
639-
MM_CP_PROT_NUMA);
638+
nr_updated = change_protection(&tlb, vma, addr, end, MM_CP_PROT_NUMA);
640639
if (nr_updated)
641640
count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated);
642641

mm/mprotect.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,25 @@ static unsigned long change_protection_range(struct mmu_gather *tlb,
507507

508508
unsigned long change_protection(struct mmu_gather *tlb,
509509
struct vm_area_struct *vma, unsigned long start,
510-
unsigned long end, pgprot_t newprot,
511-
unsigned long cp_flags)
510+
unsigned long end, unsigned long cp_flags)
512511
{
512+
pgprot_t newprot = vma->vm_page_prot;
513513
unsigned long pages;
514514

515515
BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
516516

517+
#ifdef CONFIG_NUMA_BALANCING
518+
/*
519+
* Ordinary protection updates (mprotect, uffd-wp, softdirty tracking)
520+
* are expected to reflect their requirements via VMA flags such that
521+
* vma_set_page_prot() will adjust vma->vm_page_prot accordingly.
522+
*/
523+
if (cp_flags & MM_CP_PROT_NUMA)
524+
newprot = PAGE_NONE;
525+
#else
526+
WARN_ON_ONCE(cp_flags & MM_CP_PROT_NUMA);
527+
#endif
528+
517529
if (is_vm_hugetlb_page(vma))
518530
pages = hugetlb_change_protection(vma, start, end, newprot,
519531
cp_flags);
@@ -642,7 +654,7 @@ mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
642654
mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
643655
vma_set_page_prot(vma);
644656

645-
change_protection(tlb, vma, start, end, vma->vm_page_prot, mm_cp_flags);
657+
change_protection(tlb, vma, start, end, mm_cp_flags);
646658

647659
/*
648660
* Private VM_LOCKED VMA becoming writable: trigger COW to avoid major

mm/userfaultfd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,7 @@ void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
730730
if (!enable_wp && vma_wants_manual_pte_write_upgrade(dst_vma))
731731
mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
732732
tlb_gather_mmu(&tlb, dst_mm);
733-
change_protection(&tlb, dst_vma, start, start + len, vma->vm_page_prot,
734-
mm_cp_flags);
733+
change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags);
735734
tlb_finish_mmu(&tlb);
736735
}
737736

0 commit comments

Comments
 (0)