KVM: x86: Ensure vcpu->mode is loaded from memory in kvm_vcpu_exit_request()
authorSean Christopherson <seanjc@google.com>
Wed, 28 Aug 2024 23:20:13 +0000 (16:20 -0700)
committerSean Christopherson <seanjc@google.com>
Fri, 1 Nov 2024 16:21:46 +0000 (09:21 -0700)
Wrap kvm_vcpu_exit_request()'s load of vcpu->mode with READ_ONCE() to
ensure the variable is re-loaded from memory, as there is no guarantee the
caller provides the necessary annotations to ensure KVM sees a fresh value,
e.g. the VM-Exit fastpath could theoretically reuse the pre-VM-Enter value.

Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Link: https://lore.kernel.org/r/20240828232013.768446-1-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/x86.c

index 7b347e564d10f56e22fd6b03b2d8f2763e36dbe3..312d6e08c76b1e311467282c867586b8cd5d571b 100644 (file)
@@ -2144,8 +2144,9 @@ EXPORT_SYMBOL_GPL(kvm_emulate_monitor);
 static inline bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)
 {
        xfer_to_guest_mode_prepare();
-       return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) ||
-               xfer_to_guest_mode_work_pending();
+
+       return READ_ONCE(vcpu->mode) == EXITING_GUEST_MODE ||
+              kvm_request_pending(vcpu) || xfer_to_guest_mode_work_pending();
 }
 
 /*