]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: x86/mmu: WARN on MMIO cache hit when emulating write-protected gfn
authorSean Christopherson <seanjc@google.com>
Sat, 31 Aug 2024 00:15:37 +0000 (17:15 -0700)
committerSean Christopherson <seanjc@google.com>
Tue, 10 Sep 2024 03:16:36 +0000 (20:16 -0700)
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 <yuan.yao@linux.intel.com>
Link: https://lore.kernel.org/r/20240831001538.336683-23-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/mmu/mmu.c

index ebbdc979a069446347843e41249beff6a4619a73..330b87a1c80ad7a4d0ba27f49f7f5cc799e84c03 100644 (file)
@@ -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;