Skip to content

Commit 98a69b9

Browse files
committed
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: Yuan Yao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent d859b16 commit 98a69b9

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5988,6 +5988,18 @@ static int kvm_mmu_write_protect_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
59885988
vcpu->arch.last_retry_eip = 0;
59895989
vcpu->arch.last_retry_addr = 0;
59905990

5991+
/*
5992+
* It should be impossible to reach this point with an MMIO cache hit,
5993+
* as RET_PF_WRITE_PROTECTED is returned if and only if there's a valid,
5994+
* writable memslot, and creating a memslot should invalidate the MMIO
5995+
* cache by way of changing the memslot generation. WARN and disallow
5996+
* retry if MMIO is detected, as retrying MMIO emulation is pointless
5997+
* and could put the vCPU into an infinite loop because the processor
5998+
* will keep faulting on the non-existent MMIO address.
5999+
*/
6000+
if (WARN_ON_ONCE(mmio_info_in_cache(vcpu, cr2_or_gpa, direct)))
6001+
return RET_PF_EMULATE;
6002+
59916003
/*
59926004
* Before emulating the instruction, check to see if the access was due
59936005
* 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,
60296041
return RET_PF_RETRY;
60306042

60316043
/*
6032-
* The gfn is write-protected, but if emulation fails we can still
6033-
* optimistically try to just unprotect the page and let the processor
6044+
* The gfn is write-protected, but if KVM detects its emulating an
6045+
* instruction that is unlikely to be used to modify page tables, or if
6046+
* emulation fails, KVM can try to unprotect the gfn and let the CPU
60346047
* re-execute the instruction that caused the page fault. Do not allow
6035-
* retrying MMIO emulation, as it's not only pointless but could also
6036-
* cause us to enter an infinite loop because the processor will keep
6037-
* faulting on the non-existent MMIO address. Retrying an instruction
6038-
* from a nested guest is also pointless and dangerous as we are only
6039-
* explicitly shadowing L1's page tables, i.e. unprotecting something
6040-
* for L1 isn't going to magically fix whatever issue cause L2 to fail.
6041-
*/
6042-
if (!mmio_info_in_cache(vcpu, cr2_or_gpa, direct) && !is_guest_mode(vcpu))
6048+
* retrying an instruction from a nested guest as KVM is only explicitly
6049+
* shadowing L1's page tables, i.e. unprotecting something for L1 isn't
6050+
* going to magically fix whatever issue caused L2 to fail.
6051+
*/
6052+
if (!is_guest_mode(vcpu))
60436053
*emulation_type |= EMULTYPE_ALLOW_RETRY_PF;
60446054

60456055
return RET_PF_EMULATE;

0 commit comments

Comments
 (0)