Skip to content

Commit 44f42ef

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86/mmu: Move private vs. shared check above slot validity checks
Prioritize private vs. shared gfn attribute checks above slot validity checks to ensure a consistent userspace ABI. E.g. as is, KVM will exit to userspace if there is no memslot, but emulate accesses to the APIC access page even if the attributes mismatch. Fixes: 8dd2eee ("KVM: x86/mmu: Handle page fault for private memory") Cc: Yu Zhang <yu.c.zhang@linux.intel.com> Cc: Chao Peng <chao.p.peng@linux.intel.com> Cc: Fuad Tabba <tabba@google.com> Cc: Michael Roth <michael.roth@amd.com> Cc: Isaku Yamahata <isaku.yamahata@intel.com> Signed-off-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Kai Huang <kai.huang@intel.com> Message-ID: <20240228024147.41573-10-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 07702e5 commit 44f42ef

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

arch/x86/kvm/mmu/mmu.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4316,11 +4316,6 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
43164316
return RET_PF_EMULATE;
43174317
}
43184318

4319-
if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) {
4320-
kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
4321-
return -EFAULT;
4322-
}
4323-
43244319
if (fault->is_private)
43254320
return kvm_faultin_pfn_private(vcpu, fault);
43264321

@@ -4358,9 +4353,24 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
43584353
{
43594354
int ret;
43604355

4356+
/*
4357+
* Note that the mmu_invalidate_seq also serves to detect a concurrent
4358+
* change in attributes. is_page_fault_stale() will detect an
4359+
* invalidation relate to fault->fn and resume the guest without
4360+
* installing a mapping in the page tables.
4361+
*/
43614362
fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq;
43624363
smp_rmb();
43634364

4365+
/*
4366+
* Now that we have a snapshot of mmu_invalidate_seq we can check for a
4367+
* private vs. shared mismatch.
4368+
*/
4369+
if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) {
4370+
kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
4371+
return -EFAULT;
4372+
}
4373+
43644374
/*
43654375
* Check for a relevant mmu_notifier invalidation event before getting
43664376
* the pfn from the primary MMU, and before acquiring mmu_lock.

0 commit comments

Comments
 (0)