From: Maxim Levitsky <mlevitsk@redhat.com>
To: kvm@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
linux-kernel@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
x86@kernel.org, Maxim Levitsky <mlevitsk@redhat.com>
Subject: [PATCH v4 2/5] KVM: x86: split vcpu->arch.hflags into nested_guest_state and smm_mode
Date: Wed, 25 Nov 2026 00:21:36 -0500 [thread overview]
Message-ID: <20261125052139.362046-3-mlevitsk@redhat.com> (raw)
In-Reply-To: <20261125052139.362046-1-mlevitsk@redhat.com>
Split vcpu->arch.hflags into nested_guest_mode and smm_mode to prepare
for converting nested_guest_mode into an atomic variable that will contain
nested guest state that is exposed locklessly for other vCPUs.
No functional change expected.
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
arch/x86/include/asm/kvm_host.h | 18 +++++++++++-------
arch/x86/kvm/regs.h | 6 +++---
arch/x86/kvm/smm.c | 8 ++++----
arch/x86/kvm/smm.h | 2 +-
arch/x86/kvm/x86.c | 13 ++++++++-----
5 files changed, 27 insertions(+), 20 deletions(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index ab30f418e526..226091feb44a 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -738,7 +738,6 @@ struct kvm_vcpu_arch {
unsigned long cr8;
u32 host_pkru;
u32 pkru;
- u32 hflags;
u64 efer;
u64 host_debugctl;
u64 apic_base;
@@ -758,6 +757,16 @@ struct kvm_vcpu_arch {
u64 arch_capabilities;
u64 perf_capabilities;
+#define KVM_NESTED_IN_GUEST BIT(8)
+ /* Non zero when the guest is executing a nested guest */
+ u16 nested_guest_state;
+
+#ifdef CONFIG_KVM_SMM
+#define SMM_MASK BIT(0)
+#define SMM_INSIDE_NMI_MASK BIT(1)
+ /* Non zero when the guest is in SMM */
+ u16 smm_mode;
+#endif
/*
* Paging state of the vcpu
*
@@ -1875,16 +1884,11 @@ static inline unsigned long read_msr(unsigned long msr)
}
#endif
-#define HF_GUEST_MASK (1 << 0) /* VCPU is in guest-mode */
-
#ifdef CONFIG_KVM_SMM
-#define HF_SMM_MASK (1 << 1)
-#define HF_SMM_INSIDE_NMI_MASK (1 << 2)
-
# define KVM_MAX_NR_ADDRESS_SPACES 2
/* SMM is currently unsupported for guests with private memory. */
# define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_has_private_mem(kvm) ? 1 : 2)
-# define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)
+# define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.smm_mode & SMM_MASK ? 1 : 0)
# define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm)
#else
# define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, 0)
diff --git a/arch/x86/kvm/regs.h b/arch/x86/kvm/regs.h
index 447f0ec3e63e..ec9d4a8fbe15 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.hflags |= HF_GUEST_MASK;
+ 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.hflags &= ~HF_GUEST_MASK;
+ 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.hflags & HF_GUEST_MASK;
+ return vcpu->arch.nested_guest_state & KVM_NESTED_IN_GUEST;
}
static inline unsigned long kvm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
diff --git a/arch/x86/kvm/smm.c b/arch/x86/kvm/smm.c
index 656a38dad7e7..60af64a6209f 100644
--- a/arch/x86/kvm/smm.c
+++ b/arch/x86/kvm/smm.c
@@ -114,9 +114,9 @@ void kvm_smm_changed(struct kvm_vcpu *vcpu, bool entering_smm)
trace_kvm_smm_transition(vcpu->vcpu_id, vcpu->arch.smbase, entering_smm);
if (entering_smm) {
- vcpu->arch.hflags |= HF_SMM_MASK;
+ vcpu->arch.smm_mode |= SMM_MASK;
} else {
- vcpu->arch.hflags &= ~(HF_SMM_MASK | HF_SMM_INSIDE_NMI_MASK);
+ vcpu->arch.smm_mode &= ~(SMM_MASK | SMM_INSIDE_NMI_MASK);
/* Process a latched INIT or SMI, if any. */
kvm_make_request(KVM_REQ_EVENT, vcpu);
@@ -311,7 +311,7 @@ void enter_smm(struct kvm_vcpu *vcpu)
goto error;
if (kvm_x86_call(get_nmi_mask)(vcpu))
- vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
+ vcpu->arch.smm_mode |= SMM_INSIDE_NMI_MASK;
else
kvm_x86_call(set_nmi_mask)(vcpu, true);
@@ -584,7 +584,7 @@ int emulator_leave_smm(struct x86_emulate_ctxt *ctxt)
if (ret < 0)
return X86EMUL_UNHANDLEABLE;
- if ((vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK) == 0)
+ if ((vcpu->arch.smm_mode & SMM_INSIDE_NMI_MASK) == 0)
kvm_x86_call(set_nmi_mask)(vcpu, false);
kvm_smm_changed(vcpu, false);
diff --git a/arch/x86/kvm/smm.h b/arch/x86/kvm/smm.h
index db3c88f16138..323c03827d28 100644
--- a/arch/x86/kvm/smm.h
+++ b/arch/x86/kvm/smm.h
@@ -151,7 +151,7 @@ static inline int kvm_inject_smi(struct kvm_vcpu *vcpu)
static inline bool is_smm(struct kvm_vcpu *vcpu)
{
- return vcpu->arch.hflags & HF_SMM_MASK;
+ return vcpu->arch.smm_mode & SMM_MASK;
}
void kvm_smm_changed(struct kvm_vcpu *vcpu, bool in_smm);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 79468ddfe473..0d8854bcbc4f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3013,7 +3013,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
events->smi.smm = is_smm(vcpu);
events->smi.pending = vcpu->arch.smi_pending;
events->smi.smm_inside_nmi =
- !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
+ !!(vcpu->arch.smm_mode & SMM_INSIDE_NMI_MASK);
#endif
events->smi.latched_init = kvm_lapic_latched_init(vcpu);
@@ -3099,7 +3099,7 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
#ifdef CONFIG_KVM_SMM
- if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
+ if (!!(vcpu->arch.smm_mode & SMM_MASK) != events->smi.smm) {
kvm_leave_nested(vcpu);
kvm_smm_changed(vcpu, events->smi.smm);
}
@@ -3108,9 +3108,9 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
if (events->smi.smm) {
if (events->smi.smm_inside_nmi)
- vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
+ vcpu->arch.smm_mode |= SMM_INSIDE_NMI_MASK;
else
- vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
+ vcpu->arch.smm_mode &= ~SMM_INSIDE_NMI_MASK;
}
#else
@@ -9554,7 +9554,10 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
kvm_lapic_reset(vcpu, init_event);
WARN_ON_ONCE(is_guest_mode(vcpu) || is_smm(vcpu));
- vcpu->arch.hflags = 0;
+#ifdef CONFIG_KVM_SMM
+ vcpu->arch.smm_mode = 0;
+#endif
+ vcpu->arch.nested_guest_state = 0;
vcpu->arch.smi_pending = 0;
vcpu->arch.smi_count = 0;
--
2.56.1
next prev parent reply other threads:[~2026-11-25 5:21 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 5:21 [PATCH v4 0/5] KVM: nVMX: fix nested APICv emulation for windows guests Maxim Levitsky
2026-11-25 5:21 ` [PATCH v4 1/5] KVM: nVMX: don't check PID.ON when processing nested posted interrupts Maxim Levitsky
2026-11-25 5:21 ` Maxim Levitsky [this message]
2026-11-25 5:21 ` [PATCH v4 3/5] KVM: x86: convert nested_guest_state into an atomic variable Maxim Levitsky
2026-11-25 5:21 ` [PATCH v4 4/5] KVM: x86: nVMX: move nested APICv state to nested_guest_state Maxim Levitsky
2026-11-25 5:21 ` [PATCH v4 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts Maxim Levitsky
2026-11-25 5:52 ` sashiko-bot
2026-11-25 15:28 ` mlevitsk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125052139.362046-3-mlevitsk@redhat.com \
--to=mlevitsk@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.