]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: x86: Ensure vendor's exit handler runs before fastpath userspace exits
authorSean Christopherson <seanjc@google.com>
Thu, 23 Apr 2026 16:26:27 +0000 (09:26 -0700)
committerSean Christopherson <seanjc@google.com>
Wed, 13 May 2026 16:53:55 +0000 (09:53 -0700)
Move the handling of fastpath userspace exits into vendor code to ensure
KVM runs vendor specific operations that need to run before userspace gains
control of the vCPU.  E.g. for VMX (and soon to be for SVM as well), KVM
needs to flush the PML buffer prior to exiting to userspace, otherwise any
memory written by the final KVM_RUN might never be flagged as dirty.

Note, waiting to snapshot CR0 and CR3 until svm_handle_exit() is flawed in
general, as that risks consuming stale state in a fastpath handler.  That
will be addressed in a future change.

Fixes: f7f39c50edb9 ("KVM: x86: Exit to userspace if fastpath triggers one on instruction skip")
Cc: stable@vger.kernel.org
Cc: Nikunj A. Dadhania <nikunj@amd.com>
Reviewed-by: Nikunj A. Dadhania <nikunj@amd.com>
Reviewed-by: Kai Huang <kai.huang@intel.com>
Link: https://patch.msgid.link/20260423162628.490962-2-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/svm/svm.c
arch/x86/kvm/vmx/vmx.c
arch/x86/kvm/x86.c

index b78dd8805ebb8c4e646753da82fa39ad768b3e74..fd0362874756e2e13b817976c86b462bd2dd5655 100644 (file)
@@ -3671,6 +3671,9 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
                        vcpu->arch.cr3 = svm->vmcb->save.cr3;
        }
 
+       if (unlikely(exit_fastpath == EXIT_FASTPATH_EXIT_USERSPACE))
+               return 0;
+
        if (is_guest_mode(vcpu)) {
                int vmexit;
 
index 1701db1b2e1825760b443aadf50a2cea963468b8..d81b22359918aa52fcc03aca9c596881d40c3879 100644 (file)
@@ -6705,6 +6705,9 @@ static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
        if (enable_pml && !is_guest_mode(vcpu))
                vmx_flush_pml_buffer(vcpu);
 
+       if (unlikely(exit_fastpath == EXIT_FASTPATH_EXIT_USERSPACE))
+               return 0;
+
        /*
         * KVM should never reach this point with a pending nested VM-Enter.
         * More specifically, short-circuiting VM-Entry to emulate L2 due to
index 48f259015ce4421e06425b3baa1b4b7d4865897d..810ff08780d1e4a291000fc0473ff48e30f71c43 100644 (file)
@@ -11588,9 +11588,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
        if (vcpu->arch.apic_attention)
                kvm_lapic_sync_from_vapic(vcpu);
 
-       if (unlikely(exit_fastpath == EXIT_FASTPATH_EXIT_USERSPACE))
-               return 0;
-
        r = kvm_x86_call(handle_exit)(vcpu, exit_fastpath);
        return r;