Re: [PATCH 2/3] KVM: VMX: Treat partial support of an entry/exit pair as inconsistent

From: Sean Christopherson

Date: Wed Dec 02 2026 - 16:42:08 EST


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.