mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Sohil Mehta <sohil.mehta@intel.com>
Cc: "Chang S. Bae" <chang.seok.bae@intel.com>,
	kvm@vger.kernel.org,  Paolo Bonzini <pbonzini@redhat.com>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	 Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	 "H . Peter Anvin" <hpa@zytor.com>, Xin Li <xin@zytor.com>,
	Shivansh Dhiman <shivansh.dhiman@amd.com>,
	 David Woodhouse <dwmw@amazon.co.uk>,
	Chao Gao <chao.gao@intel.com>,
	 Binbin Wu <binbin.wu@linux.intel.com>,
	Zhao Liu <zhao1.liu@intel.com>,  Jon Kohler <jon@nutanix.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/3] KVM: VMX: Treat partial support of an entry/exit pair as inconsistent
Date: Wed, 2 Dec 2026 13:42:01 -0800	[thread overview]
Message-ID: <axCQqf_PEixzRKwn@seanjc-mac.roam.internal> (raw)
In-Reply-To: <154d3685-bf48-4d18-a2d9-40f2425fa03c@intel.com>

On Wed, Dec 02, 2026, Sohil Mehta wrote:
> On 12/2/2026 10:12 AM, Chang S. Bae wrote:
> > On 11/25/2026 6:27 PM, Sohil Mehta wrote:
> >>   +/*
> >> + * A pair may consist of one entry control and several exit controls. It
> >> + * is consistent only if all of its controls are supported, or none
> >> of them
> >> + * are.
> >> + */
> >>   #define vmx_check_entry_exit_pairs(pairs, entry_controls,
> >> exit_controls)    \
> >>   ({                                        \
> > ...
> >> -        if (!(entry_controls & n_ctrl) == !(exit_controls &
> >> x_ctrl))    \
> >> +        if ((exit_controls & x_ctrl) ==                    \
> >> +            ((entry_controls & n_ctrl) ? x_ctrl : 0))            \

Oof, this is impressively difficult to read.  It took me a *long* time to tease
out that the "? x_ctrl" was the exit control, not the entry control.  Rather than
get cute with a ternary check, what if we separate the two cases (all vs. nothing)?
That would allow us to "fix" the M:N case entirely, i.e. to also handle the case
where multiple entry controls are bound to one exit control.  E.g. untested, but
this?

		if (((entry_controls & n_ctrl) == n_ctrl) &&			\
		    ((exit_controls  & x_ctrl) == x_ctrl))			\
			continue;						\
										\
		if (!(entry_controls & n_ctrl) && !(exit_controls & x_ctrl))	\
			continue;						\

With an updated comment:

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b341af4c6057..dd7b9b67aaec 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -2716,6 +2716,15 @@ static u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr)
 	return  ctl_opt & allowed;
 }

+
+/*
+ * Note, one entry control may be paired with multiple exit controls, and vice
+ * versua, e.g. if there's one entry control to load guest state, and then two
+ * exit controls to save guest state and then load/clear host state.  For pairs
+ * of controls, KVM expects all controls to exist if one of the controls exist,
+ * so treat a "pair" as consistent if and only if all of its controls are
+ * supported, or none are supported.
+ */
 #define vmx_check_entry_exit_pairs(pairs, entry_controls, exit_controls)	\
 ({										\
 	int i, r = 0;								\
@@ -2727,7 +2736,11 @@ static u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr)
 		typeof(entry_controls) n_ctrl = pairs[i].entry_control;		\
 		typeof(exit_controls) x_ctrl = pairs[i].exit_control;		\
 										\
-		if (!(entry_controls & n_ctrl) == !(exit_controls & x_ctrl))	\
+		if (((entry_controls & n_ctrl) == n_ctrl) &&			\
+		    ((exit_controls  & x_ctrl) == x_ctrl))			\
+			continue;						\
+										\
+		if (!(entry_controls & n_ctrl) && !(exit_controls & x_ctrl))	\
 			continue;						\
 										\
 		pr_warn_once("Inconsistent VM-Entry/VM-Exit pair, "		\

> >>               continue;                        \
> > 
> > Nit: Since this condition is a little difficult to follow without
> > explanation nearby, perhaps better to move the comment down right there.
> 
> I considered it but a multi-line comment inside a macro body felt odd. I
> don't think any KVM macro or other arch/x86 code typically does it.

Heh, yeah, because multi-line block comments inside of macros straight up don't
work.

> The comment describes what makes a pair consistent, which is what the
> whole macro checks as well. So, I think it looks okay above the macro.
> I'd prefer to keep it there unless you feel strongly about it.

Yes, leave it exactly where it is.

  reply	other threads:[~2026-12-02 21:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  2:27 [PATCH 0/3] KVM: VMX: Add support for the secondary VM-exit controls Sohil Mehta
2026-11-26  2:27 ` [PATCH 1/3] " Sohil Mehta
2026-12-02 18:12   ` Chang S. Bae
2026-12-02 20:45     ` Sohil Mehta
2026-11-26  2:27 ` [PATCH 2/3] KVM: VMX: Treat partial support of an entry/exit pair as inconsistent Sohil Mehta
2026-12-02 18:12   ` Chang S. Bae
2026-12-02 20:46     ` Sohil Mehta
2026-12-02 21:42       ` Sean Christopherson [this message]
2026-12-02 22:36         ` Sohil Mehta
2026-11-26  2:27 ` [PATCH 3/3] KVM: VMX: Dump the secondary VM-exit controls in dump_vmcs() Sohil Mehta
2026-12-02 18:12   ` Chang S. Bae

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=axCQqf_PEixzRKwn@seanjc-mac.roam.internal \
    --to=seanjc@google.com \
    --cc=binbin.wu@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=chang.seok.bae@intel.com \
    --cc=chao.gao@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=dwmw@amazon.co.uk \
    --cc=hpa@zytor.com \
    --cc=jon@nutanix.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=shivansh.dhiman@amd.com \
    --cc=sohil.mehta@intel.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    --cc=xin@zytor.com \
    --cc=zhao1.liu@intel.com \
    /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

all inboxes | Powered by JetHome®