Skip to contentSkip to conversation

KVM: don't zap secondary MMU on permission upgrade

PATCH In review Carlos López 5 participants 41 messages Nov 12 – Nov 26

Subsystems (12)
Memory management x86 KVM MM core +8 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover KVM: don't zap secondary MMU on permission upgrade

Stop KVM from zapping its mappings on permission upgrades on x86 and s390, as these keep the PFNs unchanged and revoke no access. KVM faults in the new permissions on demand, so dropping mappings early gains nothing.

To do so, add an MMU notifier flag that marks an MMU_NOTIFY_PROTECTION_VMA invalidation as a strict permission upgrade. For the flag to be accurate, change_protection() keeps writable entries writable on such changes. A few cases where it needlessly makes or leaves entries read-only are fixed along the way.

Background
==========

change_protection() pairs every protection change with an MMU_NOTIFY_PROTECTION_VMA invalidation, and KVM zaps its SPTEs in response, as it cannot tell whether permissions widened or narrowed. This is costly, e.g. during a QEMU background snapshot, which uses uffd-wp to write-protect guest RAM and unprotects each page once it is saved. Every unprotect then makes the guest refault memory it could already access.

Secondary MMUs that share the CPU page tables (e.g. ARM SMMU, IOMMU/ATS) can keep read-only entries in their TLBs that a fault does not flush, so they are already notified of permission upgrades on the CPU TLB flush path [1]. KVM keeps its own page tables and faults in permissions on demand, so it needs no notification, as Sean noted when that signalling was first proposed [2].

Series overview
===============

Patches 1-4 fix cases where change_protection() needlessly makes or leaves entries read-only, so that the next write to them faults. They only affect performance and can be picked up independently.

Patches 5-6 add MM_CP_PERM_UPGRADE for mprotect() calls that only add permissions, and MMU_NOTIFIER_RANGE_PERM_UPGRADE, which is set for these and for userfaultfd unprotects.

Patches 7-9 pass the MMU notifier event and flags to KVM and skip the zap on x86 and s390.

Considerations
==============

1. This series covers only x86 and s390. Claudio confirmed that skipping the zap should be safe on s390 and offered to test it [3], so patch 9 is only compile-tested. Other architectures should be able to skip it as well, but I have not tested or benchmarked them. Moving the code to arch-independent KVM would also avoid the SEV cache flush in kvm_arch_guest_memory_reclaimed().

2. struct kvm_gfn_range carries the MMU notifier event and flags, as agreed at LPC [4] and proposed in the past for other purposes
[5] [6]. The fields are only set for MMU notifier invalidations.

3. Writable migration and device-private entries are still made read-only on a permission upgrade. This does not affect secondary MMUs (see patch 6), but the downgrade could be skipped as well if preferred.

4. hugetlb does not benefit, as hugetlb_change_protection() does not restore the write bit and may unshare page tables (see patch 6).

5. The notification cannot simply be skipped in MM code (see [7]). Secondary MMUs that keep their own page tables but cannot fault rely on it to pick up new permissions. A flag lets each user opt in instead.

Benchmarks
==========

These were measured on v1.

AMD EPYC 9654 96-Core Processor host running a recent Linux kernel (6b4b543fa187), with a 64GB, 64-vCPU guest running stress-ng --vm while the host takes a QEMU background snapshot. Results are aggregated over 5 runs on a stock kernel and on one with this series, rebooting the guest between runs, with NUMA balancing disabled.

pf/s and tlb/s (per-second deltas of KVM's pf_taken and remote_tlb_flush debugfs stats):

guest pattern baseline patched
-----------------------------------------------------------------------

  read64   (read)        pf/s=950,015   tlb/s=1,440  pf/s=2,503 tlb/s=0.1
  rand-set (read+write)  pf/s=1,004,353 tlb/s=862    pf/s=4,073 tlb/s=0.3
  write64  (write)       pf/s=968,018   tlb/s=94     pf/s=2,292 tlb/s=0.3

Guest read latency was sampled with a paced probe thread in the guest, reading one byte per page with an idle gap between reads, while stress-ng ran with read64. Each figure is the median across 5 runs, with the run-to-run min and max, in ns:

metric baseline patched
------------------------------------------------------
p50 (min max) 340 (270 380) 260 (250 260)

  p99 (min max)    19789 (16885 20200)  5207 (4076 5638)
  p99.9 (min max)  23445 (22845 24036)  6850 (6259 7110)

The median improves modestly, while p99 drops ~3.8x.

A separate stress-ng run with write64 and --verify showed no data corruption and no livelocks.

Changes in v3
=============

- Patch 4 (was 5): check the old entry in the existing write-bit helpers, for any MM_CP_TRY_CHANGE_WRITABLE change (David).
- Patch 6: add mm_cp_is_perm_upgrade() here instead of in the old patch 5.
- Trim some commit messages.

Changes in v2
=============

- New patch 1: fix setting MM_CP_TRY_CHANGE_WRITABLE for shmem with uffd-wp or soft-dirty (Sashiko).
- New patches 2-3: honor MM_CP_TRY_CHANGE_WRITABLE in change_huge_pud(), adding pud_soft_dirty() on riscv (Sashiko).
- Patch 4 (was 1): only treat changes to the read, write and execute flags as an upgrade, plus VM_ACCOUNT (Sashiko).
- New patch 5: keep writable entries writable on permission upgrades (Sashiko).
- Patch 6 (was 2): don't set the flag for hugetlb (Sashiko).
- Patch 7 (was 3): pass the MMU notifier event and flags instead of a bool (Claudio).
- Patch 8 (was 4): check the flag in range->flags. The APIC-access page reload issue that Sashiko pointed out is pre-existing.
- New patch 9: skip the unmap on s390 as well (Claudio).

v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

References
==========

[1] https://lore.kernel.org/linux-mm/cover.1eca029b8603ef4eebe5b41eae51facfc5920c41.1690292440.git-series.apopple@nvidia.com/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/20261030133939.630e29af@p-imbrenda/
[4] https://lpc.events/event/20/contributions/2552/
[5] https://lore.kernel.org/all/[email protected]
[6] https://lore.kernel.org/all/[email protected]
[7] https://lore.kernel.org/all/[email protected]/

Carlos López (9):
mm: fix eager write-bit restore for write-notified shared mappings
riscv: mm: add pud_soft_dirty()
mm/huge_memory: honor MM_CP_TRY_CHANGE_WRITABLE in change_huge_pud()
mm: keep writable entries writable on protection changes mm/mprotect: add MM_CP_PERM_UPGRADE
mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE
KVM: pass the mmu_notifier event and flags to kvm_gfn_range
KVM: x86/mmu: don't zap secondary MMU on permission upgrade
KVM: s390: don't unmap the gmap on permission upgrade

 arch/riscv/include/asm/pgtable.h |  5 ++++
 arch/s390/kvm/arm64/arm.c        |  7 +++++
 arch/s390/kvm/s390/s390.c        |  7 +++++
 arch/x86/kvm/mmu/mmu.c           |  7 +++++
 include/linux/kvm_host.h         |  3 +++
 include/linux/mm.h               |  2 ++
 include/linux/mmu_notifier.h     |  5 ++++
 include/linux/pgtable.h          |  5 ++++
 mm/huge_memory.c                 | 46 ++++++++++++++++++++++++++++----
 mm/hugetlb.c                     |  4 +++
 mm/internal.h                    |  7 +++++
 mm/mprotect.c                    | 29 ++++++++++++++++++--
 mm/vma.h                         | 23 +++++++++-------
 virt/kvm/kvm_main.c              |  8 ++++++
 14 files changed, 142 insertions(+), 16 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v3 1/9 mm: fix eager write-bit restore for write-notified shared mappings

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. Callers set MM_CP_TRY_CHANGE_WRITABLE when vma_wants_manual_pte_write_upgrade() returns true, so that entries that can be written without a fault, such as dirty PTEs in a shared mapping, are made writable right away.

For shared mappings, that function passes vma->vm_page_prot to vma_wants_writenotify(). Once writenotify is enabled, this protection lacks the write bit, while the vma_pgprot_modify() result it is compared against adds it back. The two never match, so vma_wants_writenotify() returns false before checking for soft-dirty, uffd-wp and writeback.

As a result, write-notified shmem mappings never get MM_CP_TRY_CHANGE_WRITABLE. For example, commit 931298e103c2 ("mm/userfaultfd: rely on vma->vm_page_prot in uffd_wp_range()") made uffd-wp unprotect set the flag so that dirty shmem PTEs become writable again, but this never takes effect. Unprotecting N dirty shmem pages still leads to N faults that only set the write bit.

This dates back to the commit in the Fixes tag, which moved the equivalent check in mprotect_fixup() before vma_set_page_prot().

Fix this by passing the protection recomputed from the VMA flags, as vma_set_page_prot() does.

Note: this only affects performance, as the next write fault restores the write bit anyway.

Fixes 64e455079e1b ("mm: softdirty: enable write notifications on VMAs after VM_SOFTDIRTY cleared")
---
 mm/vma.h | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
mm/vma.h +14 -9
index 3fc3b47a954b..e83700e248e4 100644
@@ -589,6 +589,14 @@ int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	      unsigned long addr, int new_below);
 #endif
 
+#ifdef CONFIG_MMU
+static inline pgprot_t vma_pgprot_modify(pgprot_t oldprot, vma_flags_t vma_flags)
+{
+	const pgprot_t prot = vma_flags_to_page_prot(vma_flags);
+
+	return pgprot_modify(oldprot, prot);
+}
+
 static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma)
 {
 	/*
@@ -596,19 +604,16 @@ static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma
 	 * if we can't do that automatically for all PTEs in a mapping. For
 	 * private mappings, that's always the case when we have write
 	 * permissions as we properly have to handle COW.
+	 *
+	 * vma->vm_page_prot lacks the write bit if writenotify is already
+	 * enabled, so pass it with the bits restored, as vma_set_page_prot()
+	 * does.
 	 */
 	if (vma->vm_flags & VM_SHARED)
-		return vma_wants_writenotify(vma, vma->vm_page_prot);
+		return vma_wants_writenotify(vma,
+		       vma_pgprot_modify(vma->vm_page_prot, vma->flags));
 	return !!(vma->vm_flags & VM_WRITE);
 }
-
-#ifdef CONFIG_MMU
-static inline pgprot_t vma_pgprot_modify(pgprot_t oldprot, vma_flags_t vma_flags)
-{
-	const pgprot_t prot = vma_flags_to_page_prot(vma_flags);
-
-	return pgprot_modify(oldprot, prot);
-}
 #endif
 
 static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi,
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH v3 1/9] mm: fix eager write-bit restore for write-notified shared mappings

Sashiko has reviewed this patch and found no issues.
Quoted 22 lines

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. Callers set MM_CP_TRY_CHANGE_WRITABLE when vma_wants_manual_pte_write_upgrade() returns true, so that entries that can be written without a fault, such as dirty PTEs in a shared mapping, are made writable right away.

For shared mappings, that function passes vma->vm_page_prot to vma_wants_writenotify(). Once writenotify is enabled, this protection lacks the write bit, while the vma_pgprot_modify() result it is compared against adds it back. The two never match, so vma_wants_writenotify() returns false before checking for soft-dirty, uffd-wp and writeback.

As a result, write-notified shmem mappings never get MM_CP_TRY_CHANGE_WRITABLE. For example, commit 931298e103c2 ("mm/userfaultfd: rely on vma->vm_page_prot in uffd_wp_range()") made uffd-wp unprotect set the flag so that dirty shmem PTEs become writable again, but this never takes effect. Unprotecting N dirty shmem pages still leads to N faults that only set the write bit.

This dates back to the commit in the Fixes tag, which moved the equivalent check in mprotect_fixup() before vma_set_page_prot().

Quoted 8 lines

Fix this by passing the protection recomputed from the VMA flags, as vma_set_page_prot() does.

Note: this only affects performance, as the next write fault restores the write bit anyway.

Fixes 64e455079e1b ("mm: softdirty: enable write notifications on VMAs after VM_SOFTDIRTY cleared")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v3 1/9] mm: fix eager write-bit restore for write-notified shared mappings

In reply to Carlos López
Quoted 54 lines · Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. Callers set MM_CP_TRY_CHANGE_WRITABLE when vma_wants_manual_pte_write_upgrade() returns true, so that entries that can be written without a fault, such as dirty PTEs in a shared mapping, are made writable right away.

For shared mappings, that function passes vma->vm_page_prot to vma_wants_writenotify(). Once writenotify is enabled, this protection lacks the write bit, while the vma_pgprot_modify() result it is compared against adds it back. The two never match, so vma_wants_writenotify() returns false before checking for soft-dirty, uffd-wp and writeback.

As a result, write-notified shmem mappings never get MM_CP_TRY_CHANGE_WRITABLE. For example, commit 931298e103c2 ("mm/userfaultfd: rely on vma->vm_page_prot in uffd_wp_range()") made uffd-wp unprotect set the flag so that dirty shmem PTEs become writable again, but this never takes effect. Unprotecting N dirty shmem pages still leads to N faults that only set the write bit.

This dates back to the commit in the Fixes tag, which moved the equivalent check in mprotect_fixup() before vma_set_page_prot().

Fix this by passing the protection recomputed from the VMA flags, as vma_set_page_prot() does.

Note: this only affects performance, as the next write fault restores the write bit anyway.

Fixes 64e455079e1b ("mm: softdirty: enable write notifications on VMAs after VM_SOFTDIRTY cleared")
---
 mm/vma.h | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
mm/vma.h +8 -0
index 3fc3b47a954b..e83700e248e4 100644
@@ -589,6 +589,14 @@ int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	      unsigned long addr, int new_below);
 #endif
 
+#ifdef CONFIG_MMU
+static inline pgprot_t vma_pgprot_modify(pgprot_t oldprot, vma_flags_t vma_flags)
+{
+	const pgprot_t prot = vma_flags_to_page_prot(vma_flags);
+
+	return pgprot_modify(oldprot, prot);
+}
+
 static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma)
 {
 	/*

mm/vma.h static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma · line 610

Quoted 7 lines
mm/vma.h +4 -0
@@ -596,19 +604,16 @@ static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma
 	 * if we can't do that automatically for all PTEs in a mapping. For
 	 * private mappings, that's always the case when we have write
 	 * permissions as we properly have to handle COW.
+	 *
+	 * vma->vm_page_prot lacks the write bit if writenotify is already
+	 * enabled, so pass it with the bits restored, as vma_set_page_prot()
+	 * does.

We don't write code in comments.

mm/vma.h static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma · line 614

Quoted 5 lines
mm/vma.h +2 -1
 	 */
 	if (vma->vm_flags & VM_SHARED)
-		return vma_wants_writenotify(vma, vma->vm_page_prot);
+		return vma_wants_writenotify(vma,
+		       vma_pgprot_modify(vma->vm_page_prot, vma->flags));

You mention vma_set_page_prot() but then implement it differently from there and make no effort to abstract it?

And so both callers now do the same thing, and both open code it... you should abstract that.

Quoted 14 lines
mm/vma.h +0 -9
 	return !!(vma->vm_flags & VM_WRITE);
 }
-
-#ifdef CONFIG_MMU
-static inline pgprot_t vma_pgprot_modify(pgprot_t oldprot, vma_flags_t vma_flags)
-{
-	const pgprot_t prot = vma_flags_to_page_prot(vma_flags);
-
-	return pgprot_modify(oldprot, prot);
-}
 #endif
 
 static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi,
--
Quoted 2 lines

2.51.0

--
Cheers, Lorenzo

#4 lore raw

Re: [PATCH v3 1/9] mm: fix eager write-bit restore for write-notified shared mappings

Quoted 5 lines · Lorenzo Stoakes (ARM)
Quoted 5 lines

static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma) {

mm/vma.h +0 -0
 	/*
@@ -596,19 +604,16 @@ static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma
 	 * if we can't do that automatically for all PTEs in a mapping. For
Quoted 8 lines

mm/vma.h static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma · line 610

Quoted 6 lines
mm/vma.h +4 -0
 	 * private mappings, that's always the case when we have write
 	 * permissions as we properly have to handle COW.
+	 *
+	 * vma->vm_page_prot lacks the write bit if writenotify is already
+	 * enabled, so pass it with the bits restored, as vma_set_page_prot()
+	 * does.

We don't write code in comments.

I'll drop it.

Quoted 11 lines

mm/vma.h static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma · line 614

Quoted 5 lines
mm/vma.h +2 -1
 	 */
 	if (vma->vm_flags & VM_SHARED)
-		return vma_wants_writenotify(vma, vma->vm_page_prot);
+		return vma_wants_writenotify(vma,
+		       vma_pgprot_modify(vma->vm_page_prot, vma->flags));

You mention vma_set_page_prot() but then implement it differently from there and make no effort to abstract it?

And so both callers now do the same thing, and both open code it... you should abstract that.

Okay, I'll add a helper for both.

#5 lore raw

PATCH v3 2/9 riscv: mm: add pud_soft_dirty()

In reply to Carlos López

riscv supports soft-dirty and PUD-sized transparent huge pages, but only provides pmd_soft_dirty() and not pud_soft_dirty(). Add it, so generic code can check the soft-dirty bit of a huge PUD.

---
 arch/riscv/include/asm/pgtable.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/include/asm/pgtable.h +5 -0
index 2ebd5fa5adc5..40890f51dec6 100644
@@ -949,6 +949,11 @@ static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
 	return pte_pmd(pte_clear_soft_dirty(pmd_pte(pmd)));
 }
 
+static inline bool pud_soft_dirty(pud_t pud)
+{
+	return pte_soft_dirty(pud_pte(pud));
+}
+
 #ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
 static inline bool pmd_swp_soft_dirty(pmd_t pmd)
 {
Signature · 1 line
2.51.0
#6 lore raw

Re: [PATCH v3 2/9] riscv: mm: add pud_soft_dirty()

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

riscv supports soft-dirty and PUD-sized transparent huge pages, but only provides pmd_soft_dirty() and not pud_soft_dirty(). Add it, so generic code can check the soft-dirty bit of a huge PUD.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

PATCH v3 3/9 mm/huge_memory: honor MM_CP_TRY_CHANGE_WRITABLE in change_huge_pud()

In reply to Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit.

However, change_huge_pud() ignores the flag. For example, device DAX mappings are write-notified once soft-dirty tracking is armed through /proc/pid/clear_refs, and a protection change then leaves dirty PUDs read-only, so the next write to each of them faults.

Fix this by adding can_change_pud_writable(), based on can_change_pmd_writable(). It omits the NUMA and uffd-wp checks, as change_huge_pud() bails out on those changes.

---
 include/linux/pgtable.h |  5 +++++
 mm/huge_memory.c        | 28 +++++++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)
include/linux/pgtable.h +5 -0
index 780fe849ff8b..943d7e95f426 100644
@@ -1866,6 +1866,11 @@ static inline int pmd_soft_dirty(pmd_t pmd)
 	return 0;
 }
 
+static inline int pud_soft_dirty(pud_t pud)
+{
+	return 0;
+}
+
 static inline pte_t pte_mksoft_dirty(pte_t pte)
 {
 	return pte;
mm/huge_memory.c +27 -1
index eb913acb03b7..080b718eb7de 100644
@@ -2833,6 +2833,29 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	return ret;
 }
 
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+static inline bool can_change_pud_writable(struct vm_area_struct *vma,
+					   unsigned long addr, pud_t pud)
+{
+	struct page *page;
+
+	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
+		return false;
+
+	/* Do we need write faults for softdirty tracking? */
+	if (vma_soft_dirty_enabled(vma) && !pud_soft_dirty(pud))
+		return false;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		/* See can_change_pte_writable(). */
+		page = vm_normal_page_pud(vma, addr, pud);
+		return page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	/* See can_change_pte_writable(). */
+	return pud_dirty(pud);
+}
+
 /*
  * Returns:
  *
@@ -2840,7 +2863,6 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  * - 1: if pud can be skipped
  * - HPAGE_PUD_NR: if pud was successfully processed
  */
-#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
 int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		    pud_t *pudp, unsigned long addr, pgprot_t newprot,
 		    unsigned long cp_flags)
@@ -2872,6 +2894,10 @@ int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	 */
 	oldpud = pudp_invalidate(vma, addr, pudp);
 	entry = pud_modify(oldpud, newprot);
+	/* See change_pte_range(). */
+	if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pud_write(entry) &&
+	    can_change_pud_writable(vma, addr, entry))
+		entry = pud_mkwrite(entry);
 	set_pud_at(mm, addr, pudp, entry);
 	tlb_flush_pud_range(tlb, addr, HPAGE_PUD_SIZE);
 
Signature · 1 line
2.51.0
#8 lore raw

Re: [PATCH v3 3/9] mm/huge_memory: honor MM_CP_TRY_CHANGE_WRITABLE in change_huge_pud()

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit.

However, change_huge_pud() ignores the flag. For example, device DAX mappings are write-notified once soft-dirty tracking is armed through /proc/pid/clear_refs, and a protection change then leaves dirty PUDs read-only, so the next write to each of them faults.

Quoted 5 lines

Fix this by adding can_change_pud_writable(), based on can_change_pmd_writable(). It omits the NUMA and uffd-wp checks, as change_huge_pud() bails out on those changes.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#9 lore raw

Re: [PATCH v3 3/9] mm/huge_memory: honor MM_CP_TRY_CHANGE_WRITABLE in change_huge_pud()

In reply to Carlos López
Quoted 4 lines · Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit.

Quoted 8 lines

However, change_huge_pud() ignores the flag. For example, device DAX mappings are write-notified once soft-dirty tracking is armed through /proc/pid/clear_refs, and a protection change then leaves dirty PUDs read-only, so the next write to each of them faults.

Fix this by adding can_change_pud_writable(), based on can_change_pmd_writable(). It omits the NUMA and uffd-wp checks, as change_huge_pud() bails out on those changes.

Again, please keep MM changes to a bare minimum for your work.

If this is not required for your work, please drop it.

Signature · 3 lines
Cheers,

David
#10 lore raw

Re: [PATCH v3 3/9] mm/huge_memory: honor MM_CP_TRY_CHANGE_WRITABLE in change_huge_pud()

Quoted 10 lines · David Hildenbrand (Arm)
Quoted 9 lines · Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit.

However, change_huge_pud() ignores the flag. For example, device DAX mappings are write-notified once soft-dirty tracking is armed through /proc/pid/clear_refs, and a protection change then leaves dirty PUDs read-only, so the next write to each of them faults.

Quoted 7 lines
Quoted 3 lines

Fix this by adding can_change_pud_writable(), based on can_change_pmd_writable(). It omits the NUMA and uffd-wp checks, as change_huge_pud() bails out on those changes.

Again, please keep MM changes to a bare minimum for your work.

If this is not required for your work, please drop it.

It is required. The new flag is not correct without it, as a protection change that removes no permission bits still write-protects writable PUDs. I'll explain this better.

#11 lore raw

Re: [PATCH v3 3/9] mm/huge_memory: honor MM_CP_TRY_CHANGE_WRITABLE in change_huge_pud()

In reply to Carlos López
Quoted 9 lines · Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit.

However, change_huge_pud() ignores the flag. For example, device DAX mappings are write-notified once soft-dirty tracking is armed through /proc/pid/clear_refs, and a protection change then leaves dirty PUDs read-only, so the next write to each of them faults.

Quoted 5 lines

Fix this by adding can_change_pud_writable(), based on can_change_pmd_writable(). It omits the NUMA and uffd-wp checks, as change_huge_pud() bails out on those changes.

Is this a real problem that actual people have had?

The why of all this is still eluding me, especially for THP PUD...

Quoted 33 lines
---
 include/linux/pgtable.h |  5 +++++
 mm/huge_memory.c        | 28 +++++++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)
include/linux/pgtable.h +5 -0
index 780fe849ff8b..943d7e95f426 100644
@@ -1866,6 +1866,11 @@ static inline int pmd_soft_dirty(pmd_t pmd)
 	return 0;
 }
 
+static inline int pud_soft_dirty(pud_t pud)
+{
+	return 0;
+}
+
 static inline pte_t pte_mksoft_dirty(pte_t pte)
 {
 	return pte;
mm/huge_memory.c +4 -0
index eb913acb03b7..080b718eb7de 100644
@@ -2833,6 +2833,29 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	return ret;
 }
 
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+static inline bool can_change_pud_writable(struct vm_area_struct *vma,
+					   unsigned long addr, pud_t pud)
+{

mm/huge_memory.c int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 2849

Quoted 10 lines
mm/huge_memory.c +10 -0
+	struct page *page;
+
+	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
+		return false;
+
+	/* Do we need write faults for softdirty tracking? */
+	if (vma_soft_dirty_enabled(vma) && !pud_soft_dirty(pud))
+		return false;
+
+	if (!(vma->vm_flags & VM_SHARED)) {

This is getting annoying now, you're seemingly _knowingly_ writing deprecated VMA flag API code on purpose.

Quoted 32 lines
mm/huge_memory.c +13 -2
+		/* See can_change_pte_writable(). */
+		page = vm_normal_page_pud(vma, addr, pud);
+		return page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	/* See can_change_pte_writable(). */
+	return pud_dirty(pud);
+}
+
 /*
  * Returns:
  *
@@ -2840,7 +2863,6 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  * - 1: if pud can be skipped
  * - HPAGE_PUD_NR: if pud was successfully processed
  */
-#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
 int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		    pud_t *pudp, unsigned long addr, pgprot_t newprot,
 		    unsigned long cp_flags)
@@ -2872,6 +2894,10 @@ int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	 */
 	oldpud = pudp_invalidate(vma, addr, pudp);
 	entry = pud_modify(oldpud, newprot);
+	/* See change_pte_range(). */
+	if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pud_write(entry) &&
+	    can_change_pud_writable(vma, addr, entry))
+		entry = pud_mkwrite(entry);
 	set_pud_at(mm, addr, pudp, entry);
 	tlb_flush_pud_range(tlb, addr, HPAGE_PUD_SIZE);
 
--
Quoted 2 lines

2.51.0

--
Cheers, Lorenzo

#12 lore raw

Re: [PATCH v3 3/9] mm/huge_memory: honor MM_CP_TRY_CHANGE_WRITABLE in change_huge_pud()

Quoted 14 lines · Lorenzo Stoakes (ARM)
Quoted 13 lines · Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit.

However, change_huge_pud() ignores the flag. For example, device DAX mappings are write-notified once soft-dirty tracking is armed through /proc/pid/clear_refs, and a protection change then leaves dirty PUDs read-only, so the next write to each of them faults.

Fix this by adding can_change_pud_writable(), based on can_change_pmd_writable(). It omits the NUMA and uffd-wp checks, as change_huge_pud() bails out on those changes.

Quoted 5 lines
Quoted 1 line

Is this a real problem that actual people have had?

The why of all this is still eluding me, especially for THP PUD...

Nobody reported it, but the new flag depends on it, see my reply to David for this same patch.

Quoted 10 lines
Quoted 10 lines

[...]

mm/huge_memory.c +9 -0
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+static inline bool can_change_pud_writable(struct vm_area_struct *vma,
+					   unsigned long addr, pud_t pud)
+{
+	struct page *page;
+
+	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
+		return false;
+
Quoted 8 lines

mm/huge_memory.c int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 2849

Quoted 5 lines
mm/huge_memory.c +5 -0
+	/* Do we need write faults for softdirty tracking? */
+	if (vma_soft_dirty_enabled(vma) && !pud_soft_dirty(pud))
+		return false;
+
+	if (!(vma->vm_flags & VM_SHARED)) {

This is getting annoying now, you're seemingly _knowingly_ writing deprecated VMA flag API code on purpose.

Sorry, I based it on can_change_pmd_writable(), I'll use vma_test() instead.

#13 lore raw

PATCH v3 4/9 mm: keep writable entries writable on protection changes

In reply to Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit. In shared mappings, it relies on the dirty bit for this, as a clean entry might still need a write fault to notify the FS.

However, this misses clean entries that were already writable. For example, a uffd-wp unprotect makes shmem PTEs read-only if they were mapped by a read fault before uffd-wp was registered, so the next write to each of them faults.

Entries that were writable before the change can stay writable, as anything that relies on write faults write-protects the entry first, and MM_CP_TRY_CHANGE_WRITABLE is only set if the VMA stays writable. So check the old entry before the shared and private rules.

Note: writable anonymous entries are always exclusive and thus already kept writable, so in private mappings this only affects PFNs that drivers map writable into them.

---
 mm/huge_memory.c | 22 ++++++++++++++++------
 mm/mprotect.c    | 11 +++++++++++
 2 files changed, 27 insertions(+), 6 deletions(-)
mm/huge_memory.c +16 -6
index 080b718eb7de..69f4485c277e 100644
@@ -2269,7 +2269,8 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf)
 }
 
 static inline bool can_change_pmd_writable(struct vm_area_struct *vma,
-					   unsigned long addr, pmd_t pmd)
+					   unsigned long addr, pmd_t oldpmd,
+					   pmd_t pmd)
 {
 	struct page *page;
 
@@ -2288,6 +2289,10 @@ static inline bool can_change_pmd_writable(struct vm_area_struct *vma,
 	if (userfaultfd_huge_pmd_wp(vma, pmd))
 		return false;
 
+	/* Writes did not need a fault before the change either. */
+	if (pmd_write(oldpmd))
+		return true;
+
 	if (!(vma->vm_flags & VM_SHARED)) {
 		/* See can_change_pte_writable(). */
 		page = vm_normal_page_pmd(vma, addr, pmd);
@@ -2317,7 +2322,7 @@ vm_fault_t do_huge_pmd_uffd_rwp(struct vm_fault *vmf)
 	pmd = pmd_mkyoung(pmd);
 	if (!pmd_write(pmd) &&
 	    vma_wants_manual_pte_write_upgrade(vma) &&
-	    can_change_pmd_writable(vma, vmf->address, pmd))
+	    can_change_pmd_writable(vma, vmf->address, vmf->orig_pmd, pmd))
 		pmd = pmd_mkwrite(pmd, vma);
 	set_pmd_at(vma->vm_mm, vmf->address & HPAGE_PMD_MASK,
 		   vmf->pmd, pmd);
@@ -2354,7 +2359,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
 	 */
 	writable = pmd_write(pmd);
 	if (!writable && vma_wants_manual_pte_write_upgrade(vma) &&
-	    can_change_pmd_writable(vma, vmf->address, pmd))
+	    can_change_pmd_writable(vma, vmf->address, old_pmd, pmd))
 		writable = true;
 
 	folio = vm_normal_folio_pmd(vma, haddr, pmd);
@@ -2820,7 +2825,7 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 
 	/* See change_pte_range(). */
 	if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pmd_write(entry) &&
-	    can_change_pmd_writable(vma, addr, entry))
+	    can_change_pmd_writable(vma, addr, oldpmd, entry))
 		entry = pmd_mkwrite(entry, vma);
 
 	ret = HPAGE_PMD_NR;
@@ -2835,7 +2840,8 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 
 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
 static inline bool can_change_pud_writable(struct vm_area_struct *vma,
-					   unsigned long addr, pud_t pud)
+					   unsigned long addr, pud_t oldpud,
+					   pud_t pud)
 {
 	struct page *page;
 
@@ -2846,6 +2852,10 @@ static inline bool can_change_pud_writable(struct vm_area_struct *vma,
 	if (vma_soft_dirty_enabled(vma) && !pud_soft_dirty(pud))
 		return false;
 
+	/* Writes did not need a fault before the change either. */
+	if (pud_write(oldpud))
+		return true;
+
 	if (!(vma->vm_flags & VM_SHARED)) {
 		/* See can_change_pte_writable(). */
 		page = vm_normal_page_pud(vma, addr, pud);
@@ -2896,7 +2906,7 @@ int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	entry = pud_modify(oldpud, newprot);
 	/* See change_pte_range(). */
 	if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pud_write(entry) &&
-	    can_change_pud_writable(vma, addr, entry))
+	    can_change_pud_writable(vma, addr, oldpud, entry))
 		entry = pud_mkwrite(entry);
 	set_pud_at(mm, addr, pudp, entry);
 	tlb_flush_pud_range(tlb, addr, HPAGE_PUD_SIZE);
mm/mprotect.c +11 -0
index e18d4e12fc34..84bcfe0281d3 100644
@@ -194,6 +194,17 @@ static __always_inline void set_write_prot_commit_flush_ptes(struct vm_area_stru
 {
 	bool set_write;
 
+	/*
+	 * Writes did not need a fault before the change either. All PTEs in
+	 * the batch share the write bit.
+	 */
+	if (pte_write(oldpte)) {
+		set_write = maybe_change_pte_writable(vma, ptent);
+		prot_commit_flush_ptes(vma, addr, ptep, oldpte, ptent, nr_ptes,
+				       /* idx = */ 0, set_write, tlb);
+		return;
+	}
+
 	if (vma_test(vma, VMA_SHARED_BIT)) {
 		set_write = can_change_shared_pte_writable(vma, ptent);
 		prot_commit_flush_ptes(vma, addr, ptep, oldpte, ptent, nr_ptes,
Signature · 1 line
2.51.0
#14 lore raw

Re: [PATCH v3 4/9] mm: keep writable entries writable on protection changes

Sashiko has reviewed this patch and found no issues.
Quoted 16 lines

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit. In shared mappings, it relies on the dirty bit for this, as a clean entry might still need a write fault to notify the FS.

However, this misses clean entries that were already writable. For example, a uffd-wp unprotect makes shmem PTEs read-only if they were mapped by a read fault before uffd-wp was registered, so the next write to each of them faults.

Entries that were writable before the change can stay writable, as anything that relies on write faults write-protects the entry first, and MM_CP_TRY_CHANGE_WRITABLE is only set if the VMA stays writable. So check the old entry before the shared and private rules.

Quoted 5 lines

Note: writable anonymous entries are always exclusive and thus already kept writable, so in private mappings this only affects PFNs that drivers map writable into them.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#15 lore raw

Re: [PATCH v3 4/9] mm: keep writable entries writable on protection changes

In reply to Carlos López
Quoted 10 lines · Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit. In shared mappings, it relies on the dirty bit for this, as a clean entry might still need a write fault to notify the FS.

However, this misses clean entries that were already writable. For example, a uffd-wp unprotect makes shmem PTEs read-only if they were mapped by a read fault before uffd-wp was registered, so the next write to each of them faults.

See my other reply, adding such optimizations without any motivation why we should even care is not really what we want.

Drop it from your series and keep the MM changes to a bare minimum please.

Or is there a real dependency for your work?

Signature · 3 lines
Cheers,

David
#16 lore raw

Re: [PATCH v3 4/9] mm: keep writable entries writable on protection changes

Quoted 12 lines · David Hildenbrand (Arm)
Quoted 10 lines · Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit. In shared mappings, it relies on the dirty bit for this, as a clean entry might still need a write fault to notify the FS.

However, this misses clean entries that were already writable. For example, a uffd-wp unprotect makes shmem PTEs read-only if they were mapped by a read fault before uffd-wp was registered, so the next write to each of them faults.

Quoted 7 lines

See my other reply, adding such optimizations without any motivation why we should even care is not really what we want.

Drop it from your series and keep the MM changes to a bare minimum please.

Or is there a real dependency for your work?

Yes there is, but I clearly did not explain it properly.

The whole point of the series is to avoid zaps when no permission bits are removed by a protection change, e.g. mprotect(PROT_READ | PROT_WRITE) on a read-only VMA. My proposal is for change_protection() to signal this is the case to MMU notifiers with a new flag. However, change_protection() sometimes write-protects entries that were writable, which makes the flag wrong, so this patch fixes that.

I'll be sure to explain this properly.

#17 lore raw

Re: [PATCH v3 4/9] mm: keep writable entries writable on protection changes

Quoted 23 lines · Carlos López
Quoted 12 lines · David Hildenbrand (Arm)
Quoted 10 lines · Carlos López

change_protection() applies vma->vm_page_prot, which lacks the write bit for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes entries writable again if a write fault would only set the write bit. In shared mappings, it relies on the dirty bit for this, as a clean entry might still need a write fault to notify the FS.

However, this misses clean entries that were already writable. For example, a uffd-wp unprotect makes shmem PTEs read-only if they were mapped by a read fault before uffd-wp was registered, so the next write to each of them faults.

Quoted 7 lines

See my other reply, adding such optimizations without any motivation why we should even care is not really what we want.

Drop it from your series and keep the MM changes to a bare minimum please.

Or is there a real dependency for your work?

Yes there is, but I clearly did not explain it properly.

Quoted 8 lines

The whole point of the series is to avoid zaps when no permission bits are removed by a protection change, e.g. mprotect(PROT_READ | PROT_WRITE) on a read-only VMA. My proposal is for change_protection() to signal this is the case to MMU notifiers with a new flag. However, change_protection() sometimes write-protects entries that were writable, which makes the flag wrong, so this patch fixes that.

I'll be sure to explain this properly.

I think there are some weird interactions between things.

I'm swamped with other stuff right now, but once you resend and explain it better, maybe I can figure out how to do this cleanly.

Signature · 3 lines
Cheers,

David
#18 lore raw

PATCH v3 5/9 mm/mprotect: add MM_CP_PERM_UPGRADE

In reply to Carlos López

Add a change_protection() flag for changes that only add permissions, so that MMU notifier users can keep their mappings and fault in the new permissions later. mprotect() sets it when the new VMA flags only add read, write or execute permissions, or VM_ACCOUNT, which does not affect the page tables. Any other change, such as a different protection key, is not an upgrade.

---
 include/linux/mm.h |  2 ++
 mm/mprotect.c      | 13 +++++++++++++
 2 files changed, 15 insertions(+)
include/linux/mm.h +2 -0
index 6af3b7008f1a..001829a61405 100644
@@ -3585,6 +3585,8 @@ int get_cmdline(struct task_struct *task, char *buffer, int buflen);
 #define  MM_CP_UFFD_RWP_RESOLVE            (1UL << 5) /* resolve rwp */
 #define  MM_CP_UFFD_RWP_ALL                (MM_CP_UFFD_RWP | \
 					    MM_CP_UFFD_RWP_RESOLVE)
+/* Whether this change only upgrades/adds permissions */
+#define  MM_CP_PERM_UPGRADE                (1UL << 6)
 
 bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 			     pte_t pte);
mm/mprotect.c +13 -0
index 84bcfe0281d3..8c0fcd10f5a3 100644
@@ -767,6 +767,17 @@ static const struct mm_walk_ops prot_none_walk_ops = {
 	.walk_lock		= PGWALK_WRLOCK,
 };
 
+static bool vma_flags_is_perm_upgrade(vma_flags_t old_vma_flags,
+				      vma_flags_t new_vma_flags)
+{
+	vm_flags_t old = vma_flags_to_legacy(old_vma_flags);
+	vm_flags_t new = vma_flags_to_legacy(new_vma_flags);
+	/* VM_ACCOUNT does not affect page tables */
+	vm_flags_t mask = VM_READ | VM_WRITE | VM_EXEC | VM_ACCOUNT;
+
+	return new == (old | (new & mask));
+}
+
 int
 mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
 	       struct vm_area_struct *vma, struct vm_area_struct **pprev,
@@ -847,6 +858,8 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
 	vma_overwrite_flags(vma, new_vma_flags);
 	if (vma_wants_manual_pte_write_upgrade(vma))
 		mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
+	if (vma_flags_is_perm_upgrade(old_vma_flags, new_vma_flags))
+		mm_cp_flags |= MM_CP_PERM_UPGRADE;
 	vma_set_page_prot(vma);
 
 	change_protection(tlb, vma, start, end, mm_cp_flags);
Signature · 1 line
2.51.0
#19 lore raw

Re: [PATCH v3 5/9] mm/mprotect: add MM_CP_PERM_UPGRADE

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

Add a change_protection() flag for changes that only add permissions, so that MMU notifier users can keep their mappings and fault in the new permissions later. mprotect() sets it when the new VMA flags only add read, write or execute permissions, or VM_ACCOUNT, which does not affect the page tables. Any other change, such as a different protection key, is not an upgrade.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#20 lore raw

Re: [PATCH v3 5/9] mm/mprotect: add MM_CP_PERM_UPGRADE

In reply to Carlos López
Quoted 31 lines · Carlos López

Add a change_protection() flag for changes that only add permissions, so that MMU notifier users can keep their mappings and fault in the new permissions later. mprotect() sets it when the new VMA flags only add read, write or execute permissions, or VM_ACCOUNT, which does not affect the page tables. Any other change, such as a different protection key, is not an upgrade.

---
 include/linux/mm.h |  2 ++
 mm/mprotect.c      | 13 +++++++++++++
 2 files changed, 15 insertions(+)
include/linux/mm.h +2 -0
index 6af3b7008f1a..001829a61405 100644
@@ -3585,6 +3585,8 @@ int get_cmdline(struct task_struct *task, char *buffer, int buflen);
 #define  MM_CP_UFFD_RWP_RESOLVE            (1UL << 5) /* resolve rwp */
 #define  MM_CP_UFFD_RWP_ALL                (MM_CP_UFFD_RWP | \
 					    MM_CP_UFFD_RWP_RESOLVE)
+/* Whether this change only upgrades/adds permissions */
+#define  MM_CP_PERM_UPGRADE                (1UL << 6)
 
 bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 			     pte_t pte);
mm/mprotect.c +0 -0
index 84bcfe0281d3..8c0fcd10f5a3 100644

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 771

Quoted 5 lines
mm/mprotect.c +2 -0
@@ -767,6 +767,17 @@ static const struct mm_walk_ops prot_none_walk_ops = {
 	.walk_lock		= PGWALK_WRLOCK,
 };
 
+static bool vma_flags_is_perm_upgrade(vma_flags_t old_vma_flags,
+				      vma_flags_t new_vma_flags)

Two tab indent.

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 776

Quoted 5 lines
mm/mprotect.c +5 -0
+{
+	vm_flags_t old = vma_flags_to_legacy(old_vma_flags);
+	vm_flags_t new = vma_flags_to_legacy(new_vma_flags);
+	/* VM_ACCOUNT does not affect page tables */
+	vm_flags_t mask = VM_READ | VM_WRITE | VM_EXEC | VM_ACCOUNT;

Why is VM_ACCOUNT a permission upgrade? At best it's "no change".

What am I missing?

Also, what you really care about is "no permission downgrade", and "no permission upgrade" is not the inverse of that ;)

Signature · 3 lines
Cheers,

David
#21 lore raw

Re: [PATCH v3 5/9] mm/mprotect: add MM_CP_PERM_UPGRADE

Quoted 8 lines · David Hildenbrand (Arm)
Quoted 8 lines
mm/mprotect.c +0 -0
index 84bcfe0281d3..8c0fcd10f5a3 100644
@@ -767,6 +767,17 @@ static const struct mm_walk_ops prot_none_walk_ops = {
 	.walk_lock		= PGWALK_WRLOCK,
 };
 
Quoted 4 lines

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 771

Quoted 2 lines
mm/mprotect.c +2 -0
+static bool vma_flags_is_perm_upgrade(vma_flags_t old_vma_flags,
+				      vma_flags_t new_vma_flags)

Two tab indent.

Will fix

Quoted 9 lines

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 776

Quoted 5 lines
mm/mprotect.c +5 -0
+{
+	vm_flags_t old = vma_flags_to_legacy(old_vma_flags);
+	vm_flags_t new = vma_flags_to_legacy(new_vma_flags);
+	/* VM_ACCOUNT does not affect page tables */
+	vm_flags_t mask = VM_READ | VM_WRITE | VM_EXEC | VM_ACCOUNT;

Why is VM_ACCOUNT a permission upgrade? At best it's "no change".

What am I missing?

It isn't, this just means to cover the case where mprotect(PROT_READ | PROT_WRITE) on a private read-only VMA sets VM_ACCOUNT as well. Without this, the change is not detected as an upgrade.

Quoted 2 lines

Also, what you really care about is "no permission downgrade", and "no permission upgrade" is not the inverse of that ;)

Right, I'll rename it.

#22 lore raw

Re: [PATCH v3 5/9] mm/mprotect: add MM_CP_PERM_UPGRADE

Quoted 24 lines · Carlos López
Quoted 8 lines · David Hildenbrand (Arm)
Quoted 8 lines
mm/mprotect.c +0 -0
index 84bcfe0281d3..8c0fcd10f5a3 100644
@@ -767,6 +767,17 @@ static const struct mm_walk_ops prot_none_walk_ops = {
 	.walk_lock		= PGWALK_WRLOCK,
 };
 
Quoted 4 lines

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 771

Quoted 2 lines
mm/mprotect.c +2 -0
+static bool vma_flags_is_perm_upgrade(vma_flags_t old_vma_flags,
+				      vma_flags_t new_vma_flags)

Two tab indent.

Will fix

Quoted 8 lines

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 776

Quoted 5 lines
mm/mprotect.c +5 -0
+{
+	vm_flags_t old = vma_flags_to_legacy(old_vma_flags);
+	vm_flags_t new = vma_flags_to_legacy(new_vma_flags);
+	/* VM_ACCOUNT does not affect page tables */
+	vm_flags_t mask = VM_READ | VM_WRITE | VM_EXEC | VM_ACCOUNT;

Why is VM_ACCOUNT a permission upgrade? At best it's "no change".

Quoted 5 lines
Quoted 1 line

What am I missing?

It isn't, this just means to cover the case where mprotect(PROT_READ | PROT_WRITE) on a private read-only VMA sets VM_ACCOUNT as well. Without this, the change is not detected as an upgrade.

Why is setting VM_ACCOUNT and upgrade for permissions?!

I'm sorry but this is all way to shaky and confusing.

Take a step back and think how to do all of this cleanly without making this code here unmaintainable :)

Signature · 3 lines
Cheers,

David
#23 lore raw

Re: [PATCH v3 5/9] mm/mprotect: add MM_CP_PERM_UPGRADE

In reply to Carlos López
Quoted 6 lines · Carlos López

Add a change_protection() flag for changes that only add permissions, so that MMU notifier users can keep their mappings and fault in the new permissions later. mprotect() sets it when the new VMA flags only add read, write or execute permissions, or VM_ACCOUNT, which does not affect the page tables. Any other change, such as a different protection key, is not an upgrade.

I'm struggling to comprehend what this means.

'Add a flag for changes that only add 'permissions' (not defined) so that 'MMU notifier users' can keep mappings and 'fault' in 'the new permissions' later.

The rest of the message just states what the code does but speaks about it as if it was already what happened.

This needs to be completely rewritten...!

Quoted 12 lines
---
 include/linux/mm.h |  2 ++
 mm/mprotect.c      | 13 +++++++++++++
 2 files changed, 15 insertions(+)
include/linux/mm.h +0 -0
index 6af3b7008f1a..001829a61405 100644

include/linux/mm.h int get_cmdline(struct task_struct *task, char *buffer, int buflen); · line 3589

Quoted 5 lines
include/linux/mm.h +2 -0
@@ -3585,6 +3585,8 @@ int get_cmdline(struct task_struct *task, char *buffer, int buflen);
 #define  MM_CP_UFFD_RWP_RESOLVE            (1UL << 5) /* resolve rwp */
 #define  MM_CP_UFFD_RWP_ALL                (MM_CP_UFFD_RWP | \
 					    MM_CP_UFFD_RWP_RESOLVE)
+/* Whether this change only upgrades/adds permissions */
+#define  MM_CP_PERM_UPGRADE                (1UL << 6)

What are 'permissions' exactly?

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 771

Quoted 13 lines
include/linux/mm.h +0 -0
 bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 			     pte_t pte);
mm/mprotect.c +2 -0
index 84bcfe0281d3..8c0fcd10f5a3 100644
@@ -767,6 +767,17 @@ static const struct mm_walk_ops prot_none_walk_ops = {
 	.walk_lock		= PGWALK_WRLOCK,
 };
 
+static bool vma_flags_is_perm_upgrade(vma_flags_t old_vma_flags,
+				      vma_flags_t new_vma_flags)

No comment, random abbreviation (but just one word). Not ideal.

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 774

Quoted 3 lines
mm/mprotect.c +3 -0
+{
+	vm_flags_t old = vma_flags_to_legacy(old_vma_flags);
+	vm_flags_t new = vma_flags_to_legacy(new_vma_flags);

This is beyond appalling and a complete abuse of the VMA flags API.

You seem to have decided that you won't try to learn how the API works, so instead you'll just write deprecated code that I have to find time to come and fix up later I guess?

And we're at v3...!

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 775

Quoted 1 line
mm/mprotect.c +1 -0
+	/* VM_ACCOUNT does not affect page tables */

What does this mean? And why are you referring to a deprecated constant?

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 778

Quoted 3 lines
mm/mprotect.c +3 -0
+	vm_flags_t mask = VM_READ | VM_WRITE | VM_EXEC | VM_ACCOUNT;
+
+	return new == (old | (new & mask));

Ordinarily I'd try to help you use the new API, but the very fact you'd send it like this dissuades me :/

Quoted 15 lines
mm/mprotect.c +4 -1
+}
+
 int
 mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
 	       struct vm_area_struct *vma, struct vm_area_struct **pprev,
@@ -847,6 +858,8 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
 	vma_overwrite_flags(vma, new_vma_flags);
 	if (vma_wants_manual_pte_write_upgrade(vma))
 		mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
+	if (vma_flags_is_perm_upgrade(old_vma_flags, new_vma_flags))
+		mm_cp_flags |= MM_CP_PERM_UPGRADE;
 	vma_set_page_prot(vma);
 
 	change_protection(tlb, vma, start, end, mm_cp_flags);
--
Quoted 2 lines

2.51.0

--
Cheers, Lorenzo

#24 lore raw

Re: [PATCH v3 5/9] mm/mprotect: add MM_CP_PERM_UPGRADE

Quoted 10 lines · Lorenzo Stoakes (ARM)
Quoted 6 lines · Carlos López

Add a change_protection() flag for changes that only add permissions, so that MMU notifier users can keep their mappings and fault in the new permissions later. mprotect() sets it when the new VMA flags only add read, write or execute permissions, or VM_ACCOUNT, which does not affect the page tables. Any other change, such as a different protection key, is not an upgrade.

I'm struggling to comprehend what this means.

Quoted 7 lines

'Add a flag for changes that only add 'permissions' (not defined) so that 'MMU notifier users' can keep mappings and 'fault' in 'the new permissions' later.

The rest of the message just states what the code does but speaks about it as if it was already what happened.

This needs to be completely rewritten...!

I'll rewrite it. By permissions I mean the access granted by the protection bits in the page table entries.

Quoted 5 lines

[...]

include/linux/mm.h int get_cmdline(struct task_struct *task, char *buffer, int buflen); · line 3589

Quoted 2 lines
include/linux/mm.h +2 -0
+/* Whether this change only upgrades/adds permissions */
+#define  MM_CP_PERM_UPGRADE                (1UL << 6)

What are 'permissions' exactly?

See above

Quoted 5 lines

[...]

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 771

Quoted 2 lines
mm/mprotect.c +2 -0
+static bool vma_flags_is_perm_upgrade(vma_flags_t old_vma_flags,
+				      vma_flags_t new_vma_flags)

No comment, random abbreviation (but just one word). Not ideal.

I'll rename. As David noted, the name I chose is not really correct.

Quoted 9 lines

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 774

Quoted 3 lines
mm/mprotect.c +3 -0
+{
+	vm_flags_t old = vma_flags_to_legacy(old_vma_flags);
+	vm_flags_t new = vma_flags_to_legacy(new_vma_flags);

This is beyond appalling and a complete abuse of the VMA flags API.

You seem to have decided that you won't try to learn how the API works, so instead you'll just write deprecated code that I have to find time to come and fix up later I guess?

Sorry, I'll rewrite the helper with the vma_flags_t API, using VMA_ACCESS_FLAGS and VMA_ACCOUNT_BIT, and document it.

Quoted 5 lines

And we're at v3...!

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 775

Quoted 1 line
mm/mprotect.c +1 -0
+	/* VM_ACCOUNT does not affect page tables */

What does this mean? And why are you referring to a deprecated constant?

I meant that setting VM_ACCOUNT does not affect the protection bits of an entry, so it should not prevent the new flag from being set. mprotect() sets VM_ACCOUNT when a private mapping becomes writable, which is the case I wanted to cover. I'll explain this better.

#25 lore raw

Re: [PATCH v3 5/9] mm/mprotect: add MM_CP_PERM_UPGRADE

Quoted 53 lines · Carlos López
Quoted 10 lines · Lorenzo Stoakes (ARM)
Quoted 6 lines · Carlos López

Add a change_protection() flag for changes that only add permissions, so that MMU notifier users can keep their mappings and fault in the new permissions later. mprotect() sets it when the new VMA flags only add read, write or execute permissions, or VM_ACCOUNT, which does not affect the page tables. Any other change, such as a different protection key, is not an upgrade.

I'm struggling to comprehend what this means.

Quoted 7 lines

'Add a flag for changes that only add 'permissions' (not defined) so that 'MMU notifier users' can keep mappings and 'fault' in 'the new permissions' later.

The rest of the message just states what the code does but speaks about it as if it was already what happened.

This needs to be completely rewritten...!

I'll rewrite it. By permissions I mean the access granted by the protection bits in the page table entries.

Quoted 5 lines

[...]

include/linux/mm.h int get_cmdline(struct task_struct *task, char *buffer, int buflen); · line 3589

Quoted 2 lines
include/linux/mm.h +2 -0
+/* Whether this change only upgrades/adds permissions */
+#define  MM_CP_PERM_UPGRADE                (1UL << 6)

What are 'permissions' exactly?

See above

Quoted 5 lines

[...]

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 771

Quoted 2 lines
mm/mprotect.c +2 -0
+static bool vma_flags_is_perm_upgrade(vma_flags_t old_vma_flags,
+				      vma_flags_t new_vma_flags)

No comment, random abbreviation (but just one word). Not ideal.

I'll rename. As David noted, the name I chose is not really correct.

Quoted 9 lines

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 774

Quoted 3 lines
mm/mprotect.c +3 -0
+{
+	vm_flags_t old = vma_flags_to_legacy(old_vma_flags);
+	vm_flags_t new = vma_flags_to_legacy(new_vma_flags);

This is beyond appalling and a complete abuse of the VMA flags API.

You seem to have decided that you won't try to learn how the API works, so instead you'll just write deprecated code that I have to find time to come and fix up later I guess?

Sorry, I'll rewrite the helper with the vma_flags_t API, using VMA_ACCESS_FLAGS and VMA_ACCOUNT_BIT, and document it.

Quoted 2 lines

And we're at v3...!

Quoted 8 lines
Quoted 3 lines

mm/mprotect.c static const struct mm_walk_ops prot_none_walk_ops = { · line 775

Quoted 1 line
mm/mprotect.c +1 -0
+	/* VM_ACCOUNT does not affect page tables */

What does this mean? And why are you referring to a deprecated constant?

I meant that setting VM_ACCOUNT does not affect the protection bits of an entry, so it should not prevent the new flag from being set. mprotect() sets VM_ACCOUNT when a private mapping becomes writable, which is the case I wanted to cover. I'll explain this better.

Thanks for all! :)

--
Cheers, Lorenzo

#26 lore raw

PATCH v3 6/9 mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE

In reply to Carlos López

Add a range flag for invalidations that only upgrade permissions, so that secondary MMUs can keep their mappings rather than zap them. change_protection() sets it for MM_CP_PERM_UPGRADE and for the userfaultfd resolves, which only restore access.

Now that change_protection() keeps writable entries writable on these changes, no present entry loses access. Writable migration and device-private entries are still made read-only, which does not affect secondary MMUs. Migration entries are only installed after secondary MMUs are invalidated, and device-private pages are migrated back, with an invalidation, before the CPU can access them.

hugetlb_change_protection() never sets the flag, as hugetlb does not restore the write bit and may unshare page tables.

---
 include/linux/mmu_notifier.h | 5 +++++
 mm/hugetlb.c                 | 4 ++++
 mm/internal.h                | 7 +++++++
 mm/mprotect.c                | 5 +++--
 4 files changed, 19 insertions(+), 2 deletions(-)
include/linux/mmu_notifier.h +5 -0
index 5db78e5761c5..b2f186ebacf9 100644
@@ -60,6 +60,11 @@ enum mmu_notifier_event {
 };
 
 #define MMU_NOTIFIER_RANGE_BLOCKABLE (1 << 0)
+/*
+ * Set on an invalidation that only expands permissions without
+ * revoking any existing access, nor freeing/moving the underlying page.
+ */
+#define MMU_NOTIFIER_RANGE_PERM_UPGRADE (1 << 1)
 
 struct mmu_notifier_ops {
 	/*
mm/hugetlb.c +4 -0
index ce9c8e57b8e5..06fd31b8fbc4 100644
@@ -6632,6 +6632,10 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
 	 * In the case of shared PMDs, the area to flush could be beyond
 	 * start/end.  Set range.start/range.end to cover the maximum possible
 	 * range if PMD sharing is possible.
+	 *
+	 * MMU_NOTIFIER_RANGE_PERM_UPGRADE is never set here. hugetlb does not
+	 * restore the write bit, and huge_pmd_unshare() may drop the mapping,
+	 * so this is not a strict permission upgrade.
 	 */
 	mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
 				0, mm, start, end);
mm/internal.h +7 -0
index e3f92883bf23..b74ea18c6103 100644
@@ -1453,6 +1453,13 @@ static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte
 	return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte);
 }
 
+/* Whether a change_protection() only adds permissions. */
+static inline bool mm_cp_is_perm_upgrade(unsigned long cp_flags)
+{
+	return cp_flags & (MM_CP_PERM_UPGRADE | MM_CP_UFFD_WP_RESOLVE |
+			   MM_CP_UFFD_RWP_RESOLVE);
+}
+
 /* shrinker related functions */
 unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg,
 			  int priority);
mm/mprotect.c +3 -2
index 8c0fcd10f5a3..b7accf60c3d4 100644
@@ -608,8 +608,9 @@ static inline long change_pud_range(struct mmu_gather *tlb,
 			continue;
 
 		if (!range.start) {
-			mmu_notifier_range_init(&range,
-						MMU_NOTIFY_PROTECTION_VMA, 0,
+			mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
+						mm_cp_is_perm_upgrade(cp_flags) ?
+						MMU_NOTIFIER_RANGE_PERM_UPGRADE : 0,
 						vma->vm_mm, addr, end);
 			mmu_notifier_invalidate_range_start(&range);
 		}
Signature · 1 line
2.51.0
#27 lore raw

Re: [PATCH v3 6/9] mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm: change_protection() strips write bit for soft-dirty on perm upgrade --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm: change_protection() strips write bit for soft-dirty on perm upgrade
--

commit 292879801a9c3725b053aaa1391bad7e3b1f0588
Author: Carlos López <[email protected]>

mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE

This commit adds the MMU_NOTIFIER_RANGE_PERM_UPGRADE flag to indicate that a range invalidation only upgrades access permissions, and sets it in change_protection() for permission upgrades and userfaultfd write-protect resolves.

Quoted 6 lines
mm/mprotect.c +0 -0
index 70922e19c741d..ce3dd4b23c25b 100644
@@ -582,8 +582,9 @@ static inline long change_pud_range(struct mmu_gather *tlb,
 			continue;

mm/mprotect.c static inline long change_pud_range(struct mmu_gather *tlb, · line 590

Quoted 10 lines
mm/mprotect.c +3 -2
 
 		if (!range.start) {
-			mmu_notifier_range_init(&range,
-						MMU_NOTIFY_PROTECTION_VMA, 0,
+			mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
+						mm_cp_is_perm_upgrade(cp_flags) ?
+						MMU_NOTIFIER_RANGE_PERM_UPGRADE : 0,
 						vma->vm_mm, addr, end);
 			mmu_notifier_invalidate_range_start(&range);
 		}

[Severity: Medium]
Can change_protection() still downgrade writable entries to read-only when MMU_NOTIFIER_RANGE_PERM_UPGRADE is set on a VMA with soft-dirty tracking enabled?

When vma_soft_dirty_enabled(vma) is true, newprot lacks the write bit, so pte_modify(), pmd_modify(), and pud_modify() clear it. When restoring writability, set_write_prot_commit_flush_ptes() delegates to maybe_change_pte_writable(), which returns false whenever pte_needs_soft_dirty_wp() is true even if pte_write(oldpte) was set:

mm/mprotect.c:set_write_prot_commit_flush_ptes() {
	if (pte_write(oldpte)) {
		set_write = maybe_change_pte_writable(vma, ptent);
		prot_commit_flush_ptes(vma, addr, ptep, oldpte, ptent, nr_ptes,
				       /* idx = */ 0, set_write, tlb);
		return;
	}
    ...
}

can_change_pmd_writable() and can_change_pud_writable() similarly reject entries lacking the soft-dirty bit before checking pmd_write(oldpmd) or pud_write(oldpud).

Writing 4 (CLEAR_REFS_SOFT_DIRTY) to /proc/pid/clear_refs enables soft-dirty tracking on all VMAs while clear_refs_test_walk() skips VM_PFNMAP VMAs, and clear_soft_dirty() skips pinned PTEs via pte_is_pinned() (which can be writable without the soft-dirty bit after a read swap-in of an exclusive anonymous page). A subsequent permission upgrade or userfaultfd resolve then advertises MMU_NOTIFIER_RANGE_PERM_UPGRADE while write-protecting those present entries, leaving secondary MMUs with stale writable mappings or breaking CoW pin coherency.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#28 lore raw

PATCH v3 7/9 KVM: pass the mmu_notifier event and flags to kvm_gfn_range

In reply to Carlos López

Propagate the event and flags of the mmu_notifier invalidation into struct kvm_gfn_range, so that architecture handlers can detect a permission upgrade (MMU_NOTIFIER_RANGE_PERM_UPGRADE). No functional change.

---
 include/linux/kvm_host.h | 3 +++
 virt/kvm/kvm_main.c      | 8 ++++++++
 2 files changed, 11 insertions(+)
include/linux/kvm_host.h +3 -0
index e7fc94e2910a..1f5ce4410ddb 100644
@@ -274,6 +274,9 @@ struct kvm_gfn_range {
 	enum kvm_gfn_range_filter attr_filter;
 	bool may_block;
 	bool lockless;
+	/* From the mmu_notifier invalidation, if any. */
+	enum mmu_notifier_event event;
+	unsigned int flags;
 };
 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range);
 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
virt/kvm/kvm_main.c +8 -0
index cf4cb586f140..c517d1deb488 100644
@@ -532,6 +532,8 @@ struct kvm_mmu_notifier_range {
 	bool flush_on_ret;
 	bool may_block;
 	bool lockless;
+	enum mmu_notifier_event event;
+	unsigned int flags;
 };
 
 /*
@@ -613,6 +615,8 @@ static __always_inline kvm_mn_ret_t kvm_handle_hva_range(struct kvm *kvm,
 			 */
 			gfn_range.arg = range->arg;
 			gfn_range.may_block = range->may_block;
+			gfn_range.event = range->event;
+			gfn_range.flags = range->flags;
 			/*
 			 * HVA-based notifications aren't relevant to private
 			 * mappings as they don't have a userspace mapping.
@@ -738,6 +742,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
 		.on_lock	= kvm_mmu_invalidate_start,
 		.flush_on_ret	= true,
 		.may_block	= mmu_notifier_range_blockable(range),
+		.event		= range->event,
+		.flags		= range->flags,
 	};
 
 	trace_kvm_unmap_hva_range(range->start, range->end);
@@ -2552,6 +2558,8 @@ static __always_inline void kvm_handle_gfn_range(struct kvm *kvm,
 
 	gfn_range.arg = range->arg;
 	gfn_range.may_block = range->may_block;
+	gfn_range.event = range->event;
+	gfn_range.flags = range->flags;
 
 	/*
 	 * If/when KVM supports more attributes beyond private .vs shared, this
Signature · 1 line
2.51.0
#29 lore raw

Re: [PATCH v3 7/9] KVM: pass the mmu_notifier event and flags to kvm_gfn_range

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

Propagate the event and flags of the mmu_notifier invalidation into struct kvm_gfn_range, so that architecture handlers can detect a permission upgrade (MMU_NOTIFIER_RANGE_PERM_UPGRADE). No functional change.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
maintainer Reviewed-by on v3 7/9
#30 lore raw

Re: [PATCH v3 7/9] KVM: pass the mmu_notifier event and flags to kvm_gfn_range

In reply to Carlos López
Quoted 6 lines · On Thu, 12 Nov 2026 15:20:36 +0100 Carlos López <[email protected]> wrote:

Propagate the event and flags of the mmu_notifier invalidation into struct kvm_gfn_range, so that architecture handlers can detect a permission upgrade (MMU_NOTIFIER_RANGE_PERM_UPGRADE). No functional change.

straightforward, and also exactly what I wanted to implement myself, thanks for doing it for me :D

Quoted 59 lines
---
 include/linux/kvm_host.h | 3 +++
 virt/kvm/kvm_main.c      | 8 ++++++++
 2 files changed, 11 insertions(+)
include/linux/kvm_host.h +3 -0
index e7fc94e2910a..1f5ce4410ddb 100644
@@ -274,6 +274,9 @@ struct kvm_gfn_range {
 	enum kvm_gfn_range_filter attr_filter;
 	bool may_block;
 	bool lockless;
+	/* From the mmu_notifier invalidation, if any. */
+	enum mmu_notifier_event event;
+	unsigned int flags;
 };
 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range);
 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
virt/kvm/kvm_main.c +8 -0
index cf4cb586f140..c517d1deb488 100644
@@ -532,6 +532,8 @@ struct kvm_mmu_notifier_range {
 	bool flush_on_ret;
 	bool may_block;
 	bool lockless;
+	enum mmu_notifier_event event;
+	unsigned int flags;
 };
 
 /*
@@ -613,6 +615,8 @@ static __always_inline kvm_mn_ret_t kvm_handle_hva_range(struct kvm *kvm,
 			 */
 			gfn_range.arg = range->arg;
 			gfn_range.may_block = range->may_block;
+			gfn_range.event = range->event;
+			gfn_range.flags = range->flags;
 			/*
 			 * HVA-based notifications aren't relevant to private
 			 * mappings as they don't have a userspace mapping.
@@ -738,6 +742,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
 		.on_lock	= kvm_mmu_invalidate_start,
 		.flush_on_ret	= true,
 		.may_block	= mmu_notifier_range_blockable(range),
+		.event		= range->event,
+		.flags		= range->flags,
 	};
 
 	trace_kvm_unmap_hva_range(range->start, range->end);
@@ -2552,6 +2558,8 @@ static __always_inline void kvm_handle_gfn_range(struct kvm *kvm,
 
 	gfn_range.arg = range->arg;
 	gfn_range.may_block = range->may_block;
+	gfn_range.event = range->event;
+	gfn_range.flags = range->flags;
 
 	/*
 	 * If/when KVM supports more attributes beyond private .vs shared, this
#31 lore raw

PATCH v3 8/9 KVM: x86/mmu: don't zap secondary MMU on permission upgrade

In reply to Carlos López

When an mmu_notifier invalidation only upgrades permissions (MMU_NOTIFIER_RANGE_PERM_UPGRADE), keep the existing SPTEs instead of zapping them. The PFN is unchanged and no access is revoked, so an existing SPTE is no more permissive than the new host PTE, and the new permissions are picked up on the next fault.

The motivating case is a userfaultfd-wp dirty tracker such as QEMU's background snapshot, which unprotects every guest page as it saves it. Zapping on each unprotect makes the guest refault its whole working set. On a 64GB guest under a background snapshot, a read-mostly workload drops secondary-MMU refaults from ~1M/s to ~2.5K/s and cross-vCPU TLB shootdowns from ~1400/s to near zero.

Note: this also skips the APIC-access page reload, which is not needed as the page's PFN is unchanged.

---
 arch/x86/kvm/mmu/mmu.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kvm/mmu/mmu.c +7 -0
index f89e9f8a6cc2..3c92379bdbe0 100644
@@ -1719,6 +1719,13 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 	lockdep_assert_once(kvm->mmu_invalidate_in_progress ||
 			    lockdep_is_held(&kvm->slots_lock));
 
+	/*
+	 * Do not bother zapping SPTEs if this is only a protection upgrade.
+	 * Permissions will be faulted-in on demand.
+	 */
+	if (range->flags & MMU_NOTIFIER_RANGE_PERM_UPGRADE)
+		return false;
+
 	if (kvm_memslots_have_rmaps(kvm))
 		flush = __kvm_rmap_zap_gfn_range(kvm, range->slot,
 						 range->start, range->end,
Signature · 1 line
2.51.0
#32 lore raw

Re: [PATCH v3 8/9] KVM: x86/mmu: don't zap secondary MMU on permission upgrade

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

When an mmu_notifier invalidation only upgrades permissions (MMU_NOTIFIER_RANGE_PERM_UPGRADE), keep the existing SPTEs instead of zapping them. The PFN is unchanged and no access is revoked, so an existing SPTE is no more permissive than the new host PTE, and the new permissions are picked up on the next fault.

The motivating case is a userfaultfd-wp dirty tracker such as QEMU's background snapshot, which unprotects every guest page as it saves it. Zapping on each unprotect makes the guest refault its whole working set. On a 64GB guest under a background snapshot, a read-mostly workload drops secondary-MMU refaults from ~1M/s to ~2.5K/s and cross-vCPU TLB shootdowns from ~1400/s to near zero.

Quoted 4 lines

Note: this also skips the APIC-access page reload, which is not needed as the page's PFN is unchanged.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#33 lore raw

PATCH v3 9/9 KVM: s390: don't unmap the gmap on permission upgrade

In reply to Carlos López

When an mmu_notifier invalidation only upgrades permissions (MMU_NOTIFIER_RANGE_PERM_UPGRADE), keep the existing gmap entries instead of unmapping them. The PFN is unchanged and no access is revoked, so an existing entry is no more permissive than the new host PTE.

A write to an entry that is still read-only causes a protection exception, which kvm_s390_faultin_gfn() handles as a write fault. This applies to both s390 and arm64 guests.

---
 arch/s390/kvm/arm64/arm.c | 7 +++++++
 arch/s390/kvm/s390/s390.c | 7 +++++++
 2 files changed, 14 insertions(+)
arch/s390/kvm/arm64/arm.c +7 -0
index bd9d85235f43..5aee1817aad8 100644
@@ -678,6 +678,13 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, struct kvm_memory_slot *old,
 
 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 {
+	/*
+	 * Do not bother unmapping if this is only a protection upgrade.
+	 * Permissions will be faulted-in on demand.
+	 */
+	if (range->flags & MMU_NOTIFIER_RANGE_PERM_UPGRADE)
+		return false;
+
 	return gmap_unmap_gfn_range(kvm->arch.gmap, range->slot, range->start, range->end);
 }
 
arch/s390/kvm/s390/s390.c +7 -0
index 18fb200f854a..954bf6b64683 100644
@@ -5871,6 +5871,13 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  */
 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 {
+	/*
+	 * Do not bother unmapping if this is only a protection upgrade.
+	 * Permissions will be faulted-in on demand.
+	 */
+	if (range->flags & MMU_NOTIFIER_RANGE_PERM_UPGRADE)
+		return false;
+
 	return gmap_unmap_gfn_range(kvm->arch.gmap, range->slot, range->start, range->end);
 }
 
Signature · 1 line
2.51.0
#34 lore raw

Re: [PATCH v3 9/9] KVM: s390: don't unmap the gmap on permission upgrade

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

When an mmu_notifier invalidation only upgrades permissions (MMU_NOTIFIER_RANGE_PERM_UPGRADE), keep the existing gmap entries instead of unmapping them. The PFN is unchanged and no access is revoked, so an existing entry is no more permissive than the new host PTE.

A write to an entry that is still read-only causes a protection exception, which kvm_s390_faultin_gfn() handles as a write fault. This applies to both s390 and arm64 guests.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
maintainer
#35 lore raw

Re: [PATCH v3 9/9] KVM: s390: don't unmap the gmap on permission upgrade

In reply to Carlos López
Quoted 21 lines · On Thu, 12 Nov 2026 15:20:38 +0100 Carlos López <[email protected]> wrote:

When an mmu_notifier invalidation only upgrades permissions (MMU_NOTIFIER_RANGE_PERM_UPGRADE), keep the existing gmap entries instead of unmapping them. The PFN is unchanged and no access is revoked, so an existing entry is no more permissive than the new host PTE.

A write to an entry that is still read-only causes a protection exception, which kvm_s390_faultin_gfn() handles as a write fault. This applies to both s390 and arm64 guests.

---
 arch/s390/kvm/arm64/arm.c | 7 +++++++
 arch/s390/kvm/s390/s390.c | 7 +++++++
 2 files changed, 14 insertions(+)
arch/s390/kvm/arm64/arm.c +0 -0
index bd9d85235f43..5aee1817aad8 100644

arch/s390/kvm/arm64/arm.c void kvm_arch_commit_memory_region(struct kvm *kvm, struct kvm_memory_slot *old, · line 686

Quoted 9 lines
arch/s390/kvm/arm64/arm.c +6 -0
@@ -678,6 +678,13 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, struct kvm_memory_slot *old,
 
 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 {
+	/*
+	 * Do not bother unmapping if this is only a protection upgrade.
+	 * Permissions will be faulted-in on demand.
+	 */
+	if (range->flags & MMU_NOTIFIER_RANGE_PERM_UPGRADE)
+		return false;

this works for now, but in general the meaning of the flags bits depends on the value of range->event; it wouldn't hurt to add a check like this:

	range->event == MMU_NOTIFY_PROTECTION_VMA
Quoted 22 lines
arch/s390/kvm/arm64/arm.c +1 -0
+
 	return gmap_unmap_gfn_range(kvm->arch.gmap, range->slot, range->start, range->end);
 }
 
arch/s390/kvm/s390/s390.c +7 -0
index 18fb200f854a..954bf6b64683 100644
@@ -5871,6 +5871,13 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  */
 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 {
+	/*
+	 * Do not bother unmapping if this is only a protection upgrade.
+	 * Permissions will be faulted-in on demand.
+	 */
+	if (range->flags & MMU_NOTIFIER_RANGE_PERM_UPGRADE)
+		return false;
+
 	return gmap_unmap_gfn_range(kvm->arch.gmap, range->slot, range->start, range->end);
 }
 
#36 lore raw
In reply to Carlos López
Quoted 29 lines · Carlos López

Stop KVM from zapping its mappings on permission upgrades on x86 and s390, as these keep the PFNs unchanged and revoke no access. KVM faults in the new permissions on demand, so dropping mappings early gains nothing.

To do so, add an MMU notifier flag that marks an MMU_NOTIFY_PROTECTION_VMA invalidation as a strict permission upgrade. For the flag to be accurate, change_protection() keeps writable entries writable on such changes. A few cases where it needlessly makes or leaves entries read-only are fixed along the way.

Background
==========

change_protection() pairs every protection change with an MMU_NOTIFY_PROTECTION_VMA invalidation, and KVM zaps its SPTEs in response, as it cannot tell whether permissions widened or narrowed. This is costly, e.g. during a QEMU background snapshot, which uses uffd-wp to write-protect guest RAM and unprotects each page once it is saved. Every unprotect then makes the guest refault memory it could already access.

Secondary MMUs that share the CPU page tables (e.g. ARM SMMU, IOMMU/ATS) can keep read-only entries in their TLBs that a fault does not flush, so they are already notified of permission upgrades on the CPU TLB flush path [1]. KVM keeps its own page tables and faults in permissions on demand, so it needs no notification, as Sean noted when that signalling was first proposed [2].

Quoted 6 lines

Series overview
===============

Patches 1-4 fix cases where change_protection() needlessly makes or leaves entries read-only, so that the next write to them faults. They only affect performance and can be picked up independently.

We generally don't add performance improvements without clear motivation and data to back it up.

MM is currently busy with more important stuff, so best to keep your changes to a bare minimum.

Signature · 3 lines
Cheers,

David
#37 lore raw
Quoted 6 lines · David Hildenbrand (Arm)

On 11/12/26 15:20, Carlos López wrote:
We generally don't add performance improvements without clear motivation and data to back it up.

MM is currently busy with more important stuff, so best to keep your changes to a bare minimum.

I'm sorry after what I've seen from this series I'm pretty convinced it'd need a whole bunch of revisions, and we just don't have time for this kind of thing this cycle.

This is v3 and I already want to NAK it...

--
Cheers, Lorenzo

#38 lore raw
In reply to David Hildenbrand (Arm)
Quoted 9 lines · David Hildenbrand (Arm)
Quoted 6 lines

Series overview
===============

Patches 1-4 fix cases where change_protection() needlessly makes or leaves entries read-only, so that the next write to them faults. They only affect performance and can be picked up independently.

We generally don't add performance improvements without clear motivation and data to back it up.

Right, this is on me for not explaining the dependency better in the cover letter and the commit messages. Patches 1-4 are required for the new notifier flag to be correct. I'll explain this better.

Quoted 2 lines

MM is currently busy with more important stuff, so best to keep your changes to a bare minimum.

I had not seen your "Firefighting mode" email earlier this week, sorry.

#39 lore raw
In reply to Carlos López

Sorry but as I said to David I really question what you're doing with this series on the mm side, and I wonder whether we should even consider it for this cycle, frankly, given [0].

And, myself being the fool who is engaged in the Sisyphean task of converting VMA flags everywhere, you can imagine my delight when I saw that you (seemingly quite knowingly!) chose to abuse that API, writing code that I'll personally have to rewrite later.

Don't do that please :)

Please also do not keep respinning based on sashiko feedback. FIRST get a clear understanding of what maintainers want.

Sashiko is a bug finder, not a reviewer.

And see [1]:

"Wait for a minimum of one week before resubmitting or pinging reviewers - possibly longer during busy times like merge windows."

Please only respin once a week at the minimum.

--
Cheers, Lorenzo

[0]: https://lore.kernel.org/linux-mm/[email protected]/
[1]:https://docs.kernel.org/process/submitting-patches.html#don-t-get-discouraged-or-impatient

#40 lore raw
Quoted 3 lines · Lorenzo Stoakes (ARM)

Sorry but as I said to David I really question what you're doing with this series on the mm side, and I wonder whether we should even consider it for this cycle, frankly, given [0].

I was not aware, sorry. Considering this, I am happy to wait until 7.5-rc1 to send the next version.

Quoted 6 lines

And, myself being the fool who is engaged in the Sisyphean task of converting VMA flags everywhere, you can imagine my delight when I saw that you (seemingly quite knowingly!) chose to abuse that API, writing code that I'll personally have to rewrite later.

Don't do that please :)

I was also not aware of the ongoing effort, I'll use the new API.

Quoted 11 lines

Please also do not keep respinning based on sashiko feedback. FIRST get a clear understanding of what maintainers want.

Sashiko is a bug finder, not a reviewer.

And see [1]:

"Wait for a minimum of one week before resubmitting or pinging reviewers - possibly longer during busy times like merge windows."

Please only respin once a week at the minimum.

Understood. For what it's worth, each version went out about a week after the previous one, but fair enough, I should have waited for David and you to agree on direction.