[PATCH 17/31] KVM: Take memory protections into account for __kvm_vcpu_map

From: Paolo Bonzini

Date: Fri Sep 18 2026 - 10:08:45 EST


Take into account memory attributes when establishing a long term map
through kvm_vcpu_map()/kvm_vcpu_map_readonly().

Unlike the gfn-to-pfn cache, changes to the attributes will not be
propagated, so that it will be possible to place a nested VMCS, VMCB,
or APIC access page on a non-readable/writable page. This will be
fixed separately by using a gfn-to-pfn cache for nested virtualization
data structures, in the meanwhile apply a stopgap fix.

Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
virt/kvm/kvm_main.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index acf09059a1cd..1d111502b8c1 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3193,6 +3193,11 @@ int __kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map,
if (WARN_ON_ONCE(map->hva))
kvm_vcpu_unmap(vcpu, map);

+ if (!kvm_mem_attributes_may_read_gfn(vcpu->kvm, gfn))
+ return -EFAULT;
+ if (writable && !kvm_mem_attributes_may_write_gfn(vcpu->kvm, gfn))
+ return -EFAULT;
+
map->pinned_page = NULL;
map->page = NULL;
map->hva = NULL;
--
2.52.0