Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Maxim Levitsky <mlevitsk@redhat.com>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
	x86@kernel.org, Sean Christopherson <seanjc@google.com>,
	Maxim Levitsky <mlevitsk@redhat.com>
Subject: [PATCH v3 2/5] KVM: x86: split vcpu->arch.hflags into nested_guest_mode and smm_mode
Date: Mon, 23 Nov 2026 12:55:09 -0500	[thread overview]
Message-ID: <20261123175512.80438-3-mlevitsk@redhat.com> (raw)
In-Reply-To: <20261123175512.80438-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              | 11 ++++++-----
 5 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index ab30f418e526..d891dc678e9b 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 another 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..6114a099d177 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 != 0;
 }
 
 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..821411291f48 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,8 @@ 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;
+	vcpu->arch.smm_mode = 0;
+	vcpu->arch.nested_guest_state = 0;
 
 	vcpu->arch.smi_pending = 0;
 	vcpu->arch.smi_count = 0;
-- 
2.56.1


  parent reply	other threads:[~2026-11-23 17:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 17:55 [PATCH v3 0/5] KVM: nVMX: fix nested APICv emulation for windows guests Maxim Levitsky
2026-11-23 17:55 ` [PATCH v3 1/5] KVM: nVMX: don't check PIR.ON when processing nested posted interrupts Maxim Levitsky
2026-11-23 18:02   ` sashiko-bot
2026-11-23 17:55 ` Maxim Levitsky [this message]
2026-11-23 18:03   ` [PATCH v3 2/5] KVM: x86: split vcpu->arch.hflags into nested_guest_mode and smm_mode sashiko-bot
2026-11-23 17:55 ` [PATCH v3 3/5] KVM: x86: convert nested_guest_state into an atomic variable Maxim Levitsky
2026-11-23 17:55 ` [PATCH v3 4/5] KVM: x86: nVMX: move nested APICv state to nested_guest_state Maxim Levitsky
2026-11-23 17:55 ` [PATCH v3 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts Maxim Levitsky

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=20261123175512.80438-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox