* [RFC PATCH 0/4] KVM: x86/mmu: don't zap secondary MMU on permission upgrade
@ 2026-10-29 14:33 Carlos López
2026-10-29 14:33 ` [RFC PATCH 1/4] mm/mprotect: add MM_CP_PERM_UPGRADE Carlos López
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Carlos López @ 2026-10-29 14:33 UTC (permalink / raw)
To: kvm, linux-mm, seanjc, pbonzini; +Cc: linux-s390, imbrenda, Carlos López
Prevent x86 KVM from over-zapping its secondary MMU on PTE permission
upgrades, which leave the PFNs unchanged and do not revoke access.
Permissions for SPTEs that KVM holds are updated on the next fault, so
dropping them early gains nothing.
Do this by adding a new MMU notifier flag to signal that a
MMU_NOTIFY_PROTECTION_VMA strictly upgrades (loosens) permissions on a
particular range.
See below for benchmark results.
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 relies on
uffd-wp to write-protect guest RAM and then restore permissions as each
page is saved. Without this series, the permission restore causes the
guest to re-fault read-only memory needlessly.
Note, the reverse situation is already handled for secondary MMUs
that reuse the CPU page tables (e.g. ARM SMMU, IOMMU/ATS). They cannot
refault, so they are notified of a permission upgrade on the CPU
TLB-flush path [1].
KVM, however, owns separate page tables and faults permissions on demand,
so it needs no such notification (as Sean noted when that signalling
was first proposed on the fault path [2]). This series applies the same
principle to the change_protection() path, signalling the permission
upgrade so KVM can avoid the unnecessary zap.
RFC Considerations
==================
1. Having the zap skip in KVM's arch-independent code does not seem
obviously correct, as s390 caches additional translation state
that may not be rebuilt by refaults. Maybe an expert can correct
me. The skip could be extended to arm64 and RISC-V, but I have not
benchmarked the benefits and I'd like to get feedback first. Hence,
x86-only for now.
2. change_protection() could simply not emit the invalidation at all
for a pure permission upgrade (shared-page-table MMUs invalidate
on the CPU TLB-flush path, as noted above, so they do not need it).
But again, the s390 case could be broken, and even then, that would
seem too much intrusion for KVM's benefit.
3. The flag reaches KVM as a bool on struct kvm_gfn_range.
Alternatively, KVM could forward the notifier event, as Claudio
Imbrenda suggested at LPC [3] and others have proposed in the past
for different purposes [4] [5]. That, along with the event flags,
could serve the same purpose. I'd be happy to take that approach if
there is agreement that other consumers would want it.
(The LPC talk recording has some audio issues so I am not too sure
on what the final agreement was)
Benchmarks
==========
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. All results are aggregated
over 5 runs, and compared between the stock kernel and one with this
patch series. Guest is rebooted across each individual run, and NUMA
balancing is disabled.
pf/s and tlb/s (per-second deltas of KVM's pf_taken and remote_tlb_flush
debugfs stats) clearly improve:
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 and the refault cost shows in the tail,
where p99 drops ~3.8x.
A separate stress-ng run with write64 and --verify showed no data
corruption and no livelocks.
References
==========
[1] https://lore.kernel.org/linux-mm/cover.1eca029b8603ef4eebe5b41eae51facfc5920c41.1690292440.git-series.apopple@nvidia.com/
[2] https://lore.kernel.org/linux-mm/ZGu1vsbscdO48V6h@google.com/
[3] https://lpc.events/event/20/contributions/2552/
[4] https://lore.kernel.org/all/20240801183453.57199-1-seanjc@google.com
[5] https://lore.kernel.org/all/20231110003734.1014084-1-jackyli@google.com
Carlos López (4):
mm/mprotect: add MM_CP_PERM_UPGRADE
mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE
KVM: plumb MMU_NOTIFIER_RANGE_PERM_UPGRADE into kvm_gfn_range
KVM: x86/mmu: don't zap the secondary MMU on permission upgrade
arch/x86/kvm/mmu/mmu.c | 7 +++++++
include/linux/kvm_host.h | 1 +
include/linux/mm.h | 8 ++++++++
include/linux/mmu_notifier.h | 5 +++++
mm/hugetlb.c | 6 +++++-
mm/mprotect.c | 20 +++++++++++++++++++-
virt/kvm/kvm_main.c | 4 ++++
7 files changed, 49 insertions(+), 2 deletions(-)
--
2.51.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH 1/4] mm/mprotect: add MM_CP_PERM_UPGRADE
2026-10-29 14:33 [RFC PATCH 0/4] KVM: x86/mmu: don't zap secondary MMU on permission upgrade Carlos López
@ 2026-10-29 14:33 ` Carlos López
2026-10-29 14:33 ` [RFC PATCH 2/4] mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE Carlos López
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Carlos López @ 2026-10-29 14:33 UTC (permalink / raw)
To: kvm, linux-mm, seanjc, pbonzini
Cc: linux-s390, imbrenda, Carlos López, Andrew Morton,
David Hildenbrand, Lorenzo Stoakes, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
Jann Horn, Pedro Falcato, open list
Add a change_protection() flag for changes that only add permissions:
the PFN stays the same and no access is revoked. mprotect() sets it when
the new VMA permissions are a superset of the old, and the protection
key is unchanged.
This will allow MMU notifier users to perform optimizations, as they
can avoid invalidating PTEs whose permissions can be faulted in.
Signed-off-by: Carlos López <clopez@suse.de>
---
include/linux/mm.h | 2 ++
mm/mprotect.c | 13 +++++++++++++
2 files changed, 15 insertions(+)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 691cef038d1e..f406ce9d1032 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3415,6 +3415,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);
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 2888ee638d87..e9b636809c42 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -756,6 +756,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_flags_t rwx = VM_READ | VM_WRITE | VM_EXEC;
+
+ return !(old & rwx & ~new) &&
+ (old & ARCH_VM_PKEY_FLAGS) == (new & ARCH_VM_PKEY_FLAGS);
+}
+
int
mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
struct vm_area_struct *vma, struct vm_area_struct **pprev,
@@ -837,6 +848,8 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
vma_flags_reset_once(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);
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [RFC PATCH 2/4] mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE
2026-10-29 14:33 [RFC PATCH 0/4] KVM: x86/mmu: don't zap secondary MMU on permission upgrade Carlos López
2026-10-29 14:33 ` [RFC PATCH 1/4] mm/mprotect: add MM_CP_PERM_UPGRADE Carlos López
@ 2026-10-29 14:33 ` Carlos López
2026-10-29 14:33 ` [RFC PATCH 3/4] KVM: plumb MMU_NOTIFIER_RANGE_PERM_UPGRADE into kvm_gfn_range Carlos López
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Carlos López @ 2026-10-29 14:33 UTC (permalink / raw)
To: kvm, linux-mm, seanjc, pbonzini
Cc: linux-s390, imbrenda, Carlos López, Andrew Morton,
David Hildenbrand, Lorenzo Stoakes, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
Muchun Song, Oscar Salvador, Jann Horn, Pedro Falcato, open list
Add a range flag marking an invalidation that only upgrades permissions
(that is, no PFN change and no revoked access). change_protection() sets
it for MM_CP_PERM_UPGRADE and for the userfaultfd permission resolves,
which unprotect a page and thus only restore access.
A secondary MMU can use the flag to keep its mappings rather than zap
them. Consumers that ignore it are unaffected.
Signed-off-by: Carlos López <clopez@suse.de>
---
include/linux/mm.h | 6 ++++++
include/linux/mmu_notifier.h | 5 +++++
mm/hugetlb.c | 6 +++++-
mm/mprotect.c | 7 ++++++-
4 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index f406ce9d1032..c4fc3876a4b7 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3418,6 +3418,12 @@ int get_cmdline(struct task_struct *task, char *buffer, int buflen);
/* Whether this change only upgrades/adds permissions */
#define MM_CP_PERM_UPGRADE (1UL << 6)
+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);
+}
+
bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
pte_t pte);
extern long change_protection(struct mmu_gather *tlb,
diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h
index a11a44eef521..112be28cdf6b 100644
--- a/include/linux/mmu_notifier.h
+++ b/include/linux/mmu_notifier.h
@@ -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 {
/*
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index b017ae1dfd6f..68d44044fdd2 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -6548,19 +6548,23 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
long pages = 0, psize = huge_page_size(h);
struct mmu_notifier_range range;
unsigned long last_addr_mask;
+ unsigned int mn_flags = 0;
bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
bool uffd_rwp = cp_flags & MM_CP_UFFD_RWP;
bool uffd_rwp_resolve = cp_flags & MM_CP_UFFD_RWP_RESOLVE;
struct mmu_gather tlb;
+ if (mm_cp_is_perm_upgrade(cp_flags))
+ mn_flags |= MMU_NOTIFIER_RANGE_PERM_UPGRADE;
+
/*
* 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_init(&range, MMU_NOTIFY_PROTECTION_VMA,
- 0, mm, start, end);
+ mn_flags, mm, start, end);
adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
BUG_ON(address >= end);
diff --git a/mm/mprotect.c b/mm/mprotect.c
index e9b636809c42..b48d2fd3b7b5 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -597,8 +597,13 @@ static inline long change_pud_range(struct mmu_gather *tlb,
continue;
if (!range.start) {
+ unsigned int mn_flags = 0;
+
+ if (mm_cp_is_perm_upgrade(cp_flags))
+ mn_flags |= MMU_NOTIFIER_RANGE_PERM_UPGRADE;
+
mmu_notifier_range_init(&range,
- MMU_NOTIFY_PROTECTION_VMA, 0,
+ MMU_NOTIFY_PROTECTION_VMA, mn_flags,
vma->vm_mm, addr, end);
mmu_notifier_invalidate_range_start(&range);
}
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [RFC PATCH 3/4] KVM: plumb MMU_NOTIFIER_RANGE_PERM_UPGRADE into kvm_gfn_range
2026-10-29 14:33 [RFC PATCH 0/4] KVM: x86/mmu: don't zap secondary MMU on permission upgrade Carlos López
2026-10-29 14:33 ` [RFC PATCH 1/4] mm/mprotect: add MM_CP_PERM_UPGRADE Carlos López
2026-10-29 14:33 ` [RFC PATCH 2/4] mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE Carlos López
@ 2026-10-29 14:33 ` Carlos López
2026-10-29 14:33 ` [RFC PATCH 4/4] KVM: x86/mmu: don't zap the secondary MMU on permission upgrade Carlos López
2026-10-29 16:52 ` [RFC PATCH 0/4] KVM: x86/mmu: don't zap " Claudio Imbrenda
4 siblings, 0 replies; 6+ messages in thread
From: Carlos López @ 2026-10-29 14:33 UTC (permalink / raw)
To: kvm, linux-mm, seanjc, pbonzini
Cc: linux-s390, imbrenda, Carlos López, open list
Propagate the permission upgrade flag from the mmu_notifier range into
struct kvm_gfn_range, so the unmap handler can react accordingly. The
value is not consumed yet, so no functional change.
Signed-off-by: Carlos López <clopez@suse.de>
---
include/linux/kvm_host.h | 1 +
virt/kvm/kvm_main.c | 4 ++++
2 files changed, 5 insertions(+)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index e7fc94e2910a..aa109818cede 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -274,6 +274,7 @@ struct kvm_gfn_range {
enum kvm_gfn_range_filter attr_filter;
bool may_block;
bool lockless;
+ bool perm_upgrade;
};
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);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index cf4cb586f140..0d0073615fa8 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -532,6 +532,7 @@ struct kvm_mmu_notifier_range {
bool flush_on_ret;
bool may_block;
bool lockless;
+ bool perm_upgrade;
};
/*
@@ -613,6 +614,7 @@ 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.perm_upgrade = range->perm_upgrade;
/*
* HVA-based notifications aren't relevant to private
* mappings as they don't have a userspace mapping.
@@ -738,6 +740,7 @@ 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),
+ .perm_upgrade = !!(range->flags & MMU_NOTIFIER_RANGE_PERM_UPGRADE),
};
trace_kvm_unmap_hva_range(range->start, range->end);
@@ -2552,6 +2555,7 @@ 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.perm_upgrade = range->perm_upgrade;
/*
* If/when KVM supports more attributes beyond private .vs shared, this
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [RFC PATCH 4/4] KVM: x86/mmu: don't zap the secondary MMU on permission upgrade
2026-10-29 14:33 [RFC PATCH 0/4] KVM: x86/mmu: don't zap secondary MMU on permission upgrade Carlos López
` (2 preceding siblings ...)
2026-10-29 14:33 ` [RFC PATCH 3/4] KVM: plumb MMU_NOTIFIER_RANGE_PERM_UPGRADE into kvm_gfn_range Carlos López
@ 2026-10-29 14:33 ` Carlos López
2026-10-29 16:52 ` [RFC PATCH 0/4] KVM: x86/mmu: don't zap " Claudio Imbrenda
4 siblings, 0 replies; 6+ messages in thread
From: Carlos López @ 2026-10-29 14:33 UTC (permalink / raw)
To: kvm, linux-mm, seanjc, pbonzini
Cc: linux-s390, imbrenda, Carlos López, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen,
maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT), H. Peter Anvin,
open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)
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 a held
SPTE is at most as permissive as before. Permissions for the SPTE are
updated on the next fault, so dropping them early gains nothing.
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. Uses of mprotect() that only add
access benefit likewise.
Signed-off-by: Carlos López <clopez@suse.de>
---
arch/x86/kvm/mmu/mmu.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index dc2aef72ffdd..a4258a8c0c89 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -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->perm_upgrade)
+ return false;
+
if (kvm_memslots_have_rmaps(kvm))
flush = __kvm_rmap_zap_gfn_range(kvm, range->slot,
range->start, range->end,
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [RFC PATCH 0/4] KVM: x86/mmu: don't zap secondary MMU on permission upgrade
2026-10-29 14:33 [RFC PATCH 0/4] KVM: x86/mmu: don't zap secondary MMU on permission upgrade Carlos López
` (3 preceding siblings ...)
2026-10-29 14:33 ` [RFC PATCH 4/4] KVM: x86/mmu: don't zap the secondary MMU on permission upgrade Carlos López
@ 2026-10-29 16:52 ` Claudio Imbrenda
4 siblings, 0 replies; 6+ messages in thread
From: Claudio Imbrenda @ 2026-10-29 16:52 UTC (permalink / raw)
To: Carlos López; +Cc: kvm, linux-mm, seanjc, pbonzini, linux-s390
On Thu, 29 Oct 2026 15:33:33 +0100
Carlos López <clopez@suse.de> wrote:
> Prevent x86 KVM from over-zapping its secondary MMU on PTE permission
> upgrades, which leave the PFNs unchanged and do not revoke access.
> Permissions for SPTEs that KVM holds are updated on the next fault, so
> dropping them early gains nothing.
>
> Do this by adding a new MMU notifier flag to signal that a
> MMU_NOTIFY_PROTECTION_VMA strictly upgrades (loosens) permissions on a
> particular range.
>
> See below for benchmark results.
>
> 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 relies on
> uffd-wp to write-protect guest RAM and then restore permissions as each
> page is saved. Without this series, the permission restore causes the
> guest to re-fault read-only memory needlessly.
>
> Note, the reverse situation is already handled for secondary MMUs
> that reuse the CPU page tables (e.g. ARM SMMU, IOMMU/ATS). They cannot
> refault, so they are notified of a permission upgrade on the CPU
> TLB-flush path [1].
>
> KVM, however, owns separate page tables and faults permissions on demand,
> so it needs no such notification (as Sean noted when that signalling
> was first proposed on the fault path [2]). This series applies the same
> principle to the change_protection() path, signalling the permission
> upgrade so KVM can avoid the unnecessary zap.
>
> RFC Considerations
> ==================
>
> 1. Having the zap skip in KVM's arch-independent code does not seem
> obviously correct, as s390 caches additional translation state
> that may not be rebuilt by refaults. Maybe an expert can correct
> me. The skip could be extended to arm64 and RISC-V, but I have not
> benchmarked the benefits and I'd like to get feedback first. Hence,
> x86-only for now.
>
> 2. change_protection() could simply not emit the invalidation at all
> for a pure permission upgrade (shared-page-table MMUs invalidate
> on the CPU TLB-flush path, as noted above, so they do not need it).
> But again, the s390 case could be broken, and even then, that would
> seem too much intrusion for KVM's benefit.
>
> 3. The flag reaches KVM as a bool on struct kvm_gfn_range.
> Alternatively, KVM could forward the notifier event, as Claudio
> Imbrenda suggested at LPC [3] and others have proposed in the past
> for different purposes [4] [5]. That, along with the event flags,
> could serve the same purpose. I'd be happy to take that approach if
> there is agreement that other consumers would want it.
>
> (The LPC talk recording has some audio issues so I am not too sure
> on what the final agreement was)
AFAIR the consensus was:
- adding the reason to the kvm kvm_gfn_range is fine.
- having a return value from the notifier to pass information back is
_not_ fine; I should add a new callback instead.
regarding this series:
on x86 you don't care what the new permissions are, because you're
updating when re-faulting (if I understood correctly).
for s390 it might be beneficial to know what the new permissions are;
of course we could look up the vma and find out, but since you are
already checking the permissions, maybe you can find a way to also
convey the new permission bits?
that would make our life on s390 easier :)
>
> Benchmarks
> ==========
>
> 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. All results are aggregated
> over 5 runs, and compared between the stock kernel and one with this
> patch series. Guest is rebooted across each individual run, and NUMA
> balancing is disabled.
>
> pf/s and tlb/s (per-second deltas of KVM's pf_taken and remote_tlb_flush
> debugfs stats) clearly improve:
>
> 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 and the refault cost shows in the tail,
> where p99 drops ~3.8x.
>
> A separate stress-ng run with write64 and --verify showed no data
> corruption and no livelocks.
>
> References
> ==========
>
> [1] https://lore.kernel.org/linux-mm/cover.1eca029b8603ef4eebe5b41eae51facfc5920c41.1690292440.git-series.apopple@nvidia.com/
> [2] https://lore.kernel.org/linux-mm/ZGu1vsbscdO48V6h@google.com/
> [3] https://lpc.events/event/20/contributions/2552/
> [4] https://lore.kernel.org/all/20240801183453.57199-1-seanjc@google.com
> [5] https://lore.kernel.org/all/20231110003734.1014084-1-jackyli@google.com
>
> Carlos López (4):
> mm/mprotect: add MM_CP_PERM_UPGRADE
> mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE
> KVM: plumb MMU_NOTIFIER_RANGE_PERM_UPGRADE into kvm_gfn_range
> KVM: x86/mmu: don't zap the secondary MMU on permission upgrade
>
> arch/x86/kvm/mmu/mmu.c | 7 +++++++
> include/linux/kvm_host.h | 1 +
> include/linux/mm.h | 8 ++++++++
> include/linux/mmu_notifier.h | 5 +++++
> mm/hugetlb.c | 6 +++++-
> mm/mprotect.c | 20 +++++++++++++++++++-
> virt/kvm/kvm_main.c | 4 ++++
> 7 files changed, 49 insertions(+), 2 deletions(-)
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-10-29 16:52 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-29 14:33 [RFC PATCH 0/4] KVM: x86/mmu: don't zap secondary MMU on permission upgrade Carlos López
2026-10-29 14:33 ` [RFC PATCH 1/4] mm/mprotect: add MM_CP_PERM_UPGRADE Carlos López
2026-10-29 14:33 ` [RFC PATCH 2/4] mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE Carlos López
2026-10-29 14:33 ` [RFC PATCH 3/4] KVM: plumb MMU_NOTIFIER_RANGE_PERM_UPGRADE into kvm_gfn_range Carlos López
2026-10-29 14:33 ` [RFC PATCH 4/4] KVM: x86/mmu: don't zap the secondary MMU on permission upgrade Carlos López
2026-10-29 16:52 ` [RFC PATCH 0/4] KVM: x86/mmu: don't zap " Claudio Imbrenda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox