[PATCH v2 23/28] KVM: x86/mmu: Do not update accessed/dirty if guest PTE is read-only

From: Paolo Bonzini

Date: Fri Sep 18 2026 - 04:22:46 EST


From: Nicolas Saenz Julienne <nsaenz@xxxxxxxxxx>

In this case do not go all the way out to userspace and just stomp out
the write. This is consistent with the way KVM handles read-only
memory slots during emulation, though admittedly inconsistent with
everything else: NPT for example always wants nested page table entries
for the guest page tables to be writable, and will fault.

Signed-off-by: Nicolas Saenz Julienne <nsaenz@xxxxxxxxxx>
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/x86/kvm/mmu/paging_tmpl.h | 3 +++
1 file changed, 3 insertions(+)

diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h
index d2836a327bc9..2ff655edd57c 100644
--- a/arch/x86/kvm/mmu/paging_tmpl.h
+++ b/arch/x86/kvm/mmu/paging_tmpl.h
@@ -419,6 +419,9 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
goto error;
}

+ if (!kvm_mem_attributes_may_write_gfn(vcpu->kvm, gpa_to_gfn(real_gpa)))
+ walker->pte_writable[walker->level - 1] = false;
+
ptep_user = (pt_element_t __user *)((void *)host_addr + offset);
if (unlikely(get_user(pte, ptep_user)))
goto error;
--
2.52.0