Commit 98a69b96 authored by Sean Christopherson's avatar Sean Christopherson

KVM: x86/mmu: WARN on MMIO cache hit when emulating write-protected gfn

WARN if KVM gets an MMIO cache hit on a RET_PF_WRITE_PROTECTED fault, as
KVM should return RET_PF_WRITE_PROTECTED if and only if there is a memslot,
and creating a memslot is supposed to invalidate the MMIO cache by virtue
of changing the memslot generation.

Keep the code around mainly to provide a convenient location to document
why emulated MMIO should be impossible.
Suggested-by: default avatarYuan Yao <yuan.yao@linux.intel.com>
Link: https://lore.kernel.org/r/20240831001538.336683-23-seanjc@google.comSigned-off-by: default avatarSean Christopherson <seanjc@google.com>
parent d859b161
......@@ -5988,6 +5988,18 @@ static int kvm_mmu_write_protect_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
vcpu->arch.last_retry_eip = 0;
vcpu->arch.last_retry_addr = 0;
/*
* It should be impossible to reach this point with an MMIO cache hit,
* as RET_PF_WRITE_PROTECTED is returned if and only if there's a valid,
* writable memslot, and creating a memslot should invalidate the MMIO
* cache by way of changing the memslot generation. WARN and disallow
* retry if MMIO is detected, as retrying MMIO emulation is pointless
* and could put the vCPU into an infinite loop because the processor
* will keep faulting on the non-existent MMIO address.
*/
if (WARN_ON_ONCE(mmio_info_in_cache(vcpu, cr2_or_gpa, direct)))
return RET_PF_EMULATE;
/*
* Before emulating the instruction, check to see if the access was due
* to a read-only violation while the CPU was walking non-nested NPT
......@@ -6029,17 +6041,15 @@ static int kvm_mmu_write_protect_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
return RET_PF_RETRY;
/*
* The gfn is write-protected, but if emulation fails we can still
* optimistically try to just unprotect the page and let the processor
* The gfn is write-protected, but if KVM detects its emulating an
* instruction that is unlikely to be used to modify page tables, or if
* emulation fails, KVM can try to unprotect the gfn and let the CPU
* re-execute the instruction that caused the page fault. Do not allow
* retrying MMIO emulation, as it's not only pointless but could also
* cause us to enter an infinite loop because the processor will keep
* faulting on the non-existent MMIO address. Retrying an instruction
* from a nested guest is also pointless and dangerous as we are only
* explicitly shadowing L1's page tables, i.e. unprotecting something
* for L1 isn't going to magically fix whatever issue cause L2 to fail.
*/
if (!mmio_info_in_cache(vcpu, cr2_or_gpa, direct) && !is_guest_mode(vcpu))
* retrying an instruction from a nested guest as KVM is only explicitly
* shadowing L1's page tables, i.e. unprotecting something for L1 isn't
* going to magically fix whatever issue caused L2 to fail.
*/
if (!is_guest_mode(vcpu))
*emulation_type |= EMULTYPE_ALLOW_RETRY_PF;
return RET_PF_EMULATE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment