[PATCH v3 3/5] KVM: x86: convert nested_guest_state into an atomic variable
From: Maxim Levitsky
Date: Mon Nov 23 2026 - 12:55:59 EST
Convert nested_guest_state into an atomic variable, to prepare
to fix nested APICv delivery races.
No functional change expected.
Suggested-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Maxim Levitsky <mlevitsk@xxxxxxxxxx>
---
arch/x86/include/asm/kvm_host.h | 2 +-
arch/x86/kvm/regs.h | 6 +++---
arch/x86/kvm/x86.c | 2 +-
3 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index d891dc678e9b..562042d5589b 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -759,7 +759,7 @@ struct kvm_vcpu_arch {
#define KVM_NESTED_IN_GUEST BIT(8)
/* Non zero when the guest is executing another guest */
- u16 nested_guest_state;
+ atomic_t nested_guest_state;
#ifdef CONFIG_KVM_SMM
#define SMM_MASK BIT(0)
diff --git a/arch/x86/kvm/regs.h b/arch/x86/kvm/regs.h
index 6114a099d177..58950f7d4713 100644
--- a/arch/x86/kvm/regs.h
+++ b/arch/x86/kvm/regs.h
@@ -457,13 +457,13 @@ static inline unsigned long kvm_get_effective_dr7(struct kvm_vcpu *vcpu)
static inline void enter_guest_mode(struct kvm_vcpu *vcpu)
{
- vcpu->arch.nested_guest_state = KVM_NESTED_IN_GUEST;
+ atomic_set(&vcpu->arch.nested_guest_state, KVM_NESTED_IN_GUEST);
vcpu->stat.guest_mode = 1;
}
static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
{
- vcpu->arch.nested_guest_state = 0;
+ atomic_set(&vcpu->arch.nested_guest_state, 0);
if (vcpu->arch.load_eoi_exitmap_pending) {
vcpu->arch.load_eoi_exitmap_pending = false;
@@ -475,7 +475,7 @@ static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
{
- return vcpu->arch.nested_guest_state != 0;
+ return atomic_read(&vcpu->arch.nested_guest_state) != 0;
}
static inline unsigned long kvm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 821411291f48..a28d263e936a 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9555,7 +9555,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
WARN_ON_ONCE(is_guest_mode(vcpu) || is_smm(vcpu));
vcpu->arch.smm_mode = 0;
- vcpu->arch.nested_guest_state = 0;
+ atomic_set(&vcpu->arch.nested_guest_state, 0);
vcpu->arch.smi_pending = 0;
vcpu->arch.smi_count = 0;
--
2.56.1