Skip to contentSkip to conversation

KVM: VMX: Add support for the secondary VM-exit controls

PATCH Superseded Sohil Mehta 3 participants 11 messages Nov 26 – Dec 2

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover KVM: VMX: Add support for the secondary VM-exit controls

Sean, Paolo,

These patches started out as part of the FRED KVM series [1]. However, the series is getting bulky (30+ patches) and hard to manage as well as review. I am working on splitting it into smaller parts [2] for easier review and merge.

The overall goal is to get the infrastructure pieces ready first and then the FRED guest support followed by the nested FRED support. Some core x86 patches that were part of the FRED KVM series are already in place [3] with more on the way [4].

This series focusses on getting the secondary VM-exit controls support in place independent of FRED. Please let me know if you prefer a different approach or have any suggestions.

Background
----------
The secondary VM-exit controls are a 64-bit VMCS field, activated by bit 31 of the primary VM-exit controls. The SDM defines four of them so far: "save FRED", "load FRED", "load IA32_SPEC_CTRL" and "prematurely busy shadow stack".

With this series, KVM discovers the field and programs it in vmcs01 and vmcs02, so that FRED or any other user of these controls only has to set its own bits later on.

Patches
-------
Patch 1 adds the support for the secondary VM-exit controls.

Changes from FRED KVM v10:
- Reword the commit message and add some minor changes to match the name and placement with neighboring code.
- Copy the secondary controls into vmcs02 to fix the issue described below. In v10 this lived in the nested patches [5] which tied the FRED guest support to the nested support.

Binbin, I have kept your review tag since the changes are minor. Please let me know if you have any concerns.

Patch 2 fixes an issue reported by sashiko related to the entry/exit pair checking [6] and patch 3 adds the new field to the VMCS dump. The last two patches are optional, but they help complete the support.

The patches are based on the kvm-x86/next branch (kvm-x86-next-2026.11.11).

vmcs02 issue
------------
KVM seeds vmcs02's VM-exit controls from vmcs01's, so vmcs02 inherits bit 31 but not the secondary controls themselves. vmcs02 then runs with the secondary controls active but empty, which is harmless only while every bit is clear.

Once FRED sets its controls in vmcs01, vmcs02 also inherits FRED's VM-entry control. Entering any L2 loads FRED state, but the exit from L2 doesn't restore the host's. Until the next VM entry the host runs with a zeroed FRED configuration, and any event in that window hangs the CPU.

This isn't specific to FRED, any future secondary VM-exit control would likewise be silently off whenever an L2 runs.

Testing
-------
I tested it on a CWF system with FRED enabled as well as fred=off. The CPU enumerates all four secondary VM-exit controls (IA32_VMX_EXIT_CTLS2 = 0xf), so KVM activates the field in vmcs01 and copies it into vmcs02, with every bit clear.

A debug dump_vmcs() on first VM exit from vmcs01 and from vmcs02 shows the same controls in both:

    ExitControls=0x902befff SecondaryExitControls=0x0000000000000000

KVM selftests and kvm-unit-tests show no regressions.

Links
-----
[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/lkml/179545148741.92412.5844266518161867015.tip-bot2@tip-bot2/
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/lkml/[email protected]/
[6]: https://lore.kernel.org/lkml/[email protected]/

Sohil Mehta (1):
KVM: VMX: Treat partial support of an entry/exit pair as inconsistent

Xin Li (Intel) (2):
KVM: VMX: Add support for the secondary VM-exit controls
KVM: VMX: Dump the secondary VM-exit controls in dump_vmcs()

 arch/x86/include/asm/msr-index.h |  1 +
 arch/x86/include/asm/vmx.h       |  3 +++
 arch/x86/kvm/vmx/capabilities.h  |  6 +++++
 arch/x86/kvm/vmx/nested.c        |  3 +++
 arch/x86/kvm/vmx/vmcs.h          |  1 +
 arch/x86/kvm/vmx/vmx.c           | 46 +++++++++++++++++++++++++++++---
 arch/x86/kvm/vmx/vmx.h           |  7 ++++-
 7 files changed, 62 insertions(+), 5 deletions(-)

base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 KVM: VMX: Add support for the secondary VM-exit controls

From: "Xin Li (Intel)" <[email protected]>

The secondary VM-exit controls are a 64-bit VMCS field for controls that no longer fit in the 32-bit primary field. Bit 31 of the primary VM-exit controls activates them. No feature KVM supports uses any of these controls yet, but the upcoming FRED KVM support is planning to use them.

Add the VMCS field, its 64-bit controls shadow and the vmcs_config plumbing, and activate the controls in vmcs01 whenever hardware supports them. With every control bit clear, KVM behaves exactly as before.

Check the secondary controls through their own entry/exit pair table. Avoid using a shared table because a failed check would clear the activation bit and turn off every secondary control, not just the inconsistent one. The new table stays empty until the first feature adds a pair.

Copy the controls into vmcs02 as well. vmcs02 inherits the activation bit from vmcs01, so without the copy it would run with the secondary controls active but empty. That is harmless while every bit is clear. Once a feature sets one, though, L2 would run with it silently off, leaving the host with L2's state after a VM exit.

---
v1:
(Changes from FRED KVM v10):
- Set the secondary VM-exit controls in vmcs02 too, so that no commit leaves vmcs02 with the controls activated but empty. (Prevents a CPU hang on exits from L2 once FRED guest support lands without the nested patches.)
- Match the names and placement of the new variables and code to their neighbours.
- Reword the changelog and also say why the secondary entry/exit pair table is separate from the primary one.

---
 arch/x86/include/asm/msr-index.h |  1 +
 arch/x86/include/asm/vmx.h       |  3 +++
 arch/x86/kvm/vmx/capabilities.h  |  6 ++++++
 arch/x86/kvm/vmx/nested.c        |  3 +++
 arch/x86/kvm/vmx/vmcs.h          |  1 +
 arch/x86/kvm/vmx/vmx.c           | 25 +++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.h           |  7 ++++++-
 7 files changed, 45 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +1 -0
index c0914829a925..ba26b8eccb01 100644
@@ -1288,6 +1288,7 @@
 #define MSR_IA32_VMX_TRUE_ENTRY_CTLS     0x00000490
 #define MSR_IA32_VMX_VMFUNC             0x00000491
 #define MSR_IA32_VMX_PROCBASED_CTLS3	0x00000492
+#define MSR_IA32_VMX_EXIT_CTLS2		0x00000493
 
 #define MSR_IA32_MCU_STAGING_MBOX_ADDR	0x000007a5
 
arch/x86/include/asm/vmx.h +3 -0
index e49b836c7313..a0802af63dc3 100644
@@ -127,6 +127,7 @@ struct vmcs {
 #define VM_EXIT_CLEAR_IA32_RTIT_CTL		0x02000000
 #define VM_EXIT_LOAD_CET_STATE                  0x10000000
 #define VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL	0x40000000
+#define VM_EXIT_ACTIVATE_SECONDARY_CONTROLS	0x80000000
 
 #define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR	0x00036dff
 
@@ -283,6 +284,8 @@ enum vmcs_field {
 	SHARED_EPT_POINTER		= 0x0000203C,
 	PID_POINTER_TABLE		= 0x00002042,
 	PID_POINTER_TABLE_HIGH		= 0x00002043,
+	SECONDARY_VM_EXIT_CONTROLS	= 0x00002044,
+	SECONDARY_VM_EXIT_CONTROLS_HIGH	= 0x00002045,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/vmx/capabilities.h +6 -0
index 810119167f79..5737e79bf2da 100644
@@ -58,6 +58,7 @@ struct vmcs_config {
 	u32 cpu_based_2nd_exec_ctrl;
 	u64 cpu_based_3rd_exec_ctrl;
 	u32 vmexit_ctrl;
+	u64 vmexit_2nd_ctrl;
 	u32 vmentry_ctrl;
 	u64 misc;
 	struct nested_vmx_msrs nested;
@@ -149,6 +150,11 @@ static inline bool cpu_has_tertiary_exec_ctrls(void)
 		CPU_BASED_ACTIVATE_TERTIARY_CONTROLS;
 }
 
+static inline bool cpu_has_secondary_vmexit_ctrls(void)
+{
+	return vmcs_config.vmexit_ctrl & VM_EXIT_ACTIVATE_SECONDARY_CONTROLS;
+}
+
 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
 {
 	return vmcs_config.cpu_based_2nd_exec_ctrl &
arch/x86/kvm/vmx/nested.c +3 -0
index 1a98f28651f7..0f39309cc7f3 100644
@@ -2559,6 +2559,9 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0
 		exec_control &= ~VM_EXIT_LOAD_IA32_EFER;
 	vm_exit_controls_set(vmx, exec_control);
 
+	if (exec_control & VM_EXIT_ACTIVATE_SECONDARY_CONTROLS)
+		secondary_vm_exit_controls_set(vmx, __secondary_vm_exit_controls_get(vmcs01));
+
 	/*
 	 * Interrupt/Exception Fields
 	 */
arch/x86/kvm/vmx/vmcs.h +1 -0
index 1f16ddeae9cb..ca341c59bab1 100644
@@ -45,6 +45,7 @@ struct vmcs_host_state {
 struct vmcs_controls_shadow {
 	u32 vm_entry;
 	u32 vm_exit;
+	u64 secondary_vm_exit;
 	u32 pin;
 	u32 exec;
 	u32 secondary_exec;
arch/x86/kvm/vmx/vmx.c +25 -0
index 68fa251dda60..3752a86c3fca 100644
@@ -2752,6 +2752,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
 	u32 _cpu_based_2nd_exec_control = 0;
 	u64 _cpu_based_3rd_exec_control = 0;
 	u32 _vmexit_control = 0;
+	u64 _vmexit_2nd_control = 0;
 	u32 _vmentry_control = 0;
 	struct msr val;
 	u64 basic_msr;
@@ -2774,6 +2775,12 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
 		{ VM_ENTRY_LOAD_CET_STATE,		VM_EXIT_LOAD_CET_STATE },
 	};
 
+	struct {
+		u32 entry_control;
+		u64 exit_control;
+	} const vmcs_entry_exit_2nd_pairs[] = {
+	};
+
 	memset(vmcs_conf, 0, sizeof(*vmcs_conf));
 
 	if (adjust_vmx_controls(KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL,
@@ -2864,6 +2871,11 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
 				&_vmexit_control))
 		return -EIO;
 
+	if (_vmexit_control & VM_EXIT_ACTIVATE_SECONDARY_CONTROLS)
+		_vmexit_2nd_control =
+			adjust_vmx_controls64(KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS,
+					      MSR_IA32_VMX_EXIT_CTLS2);
+
 	if (adjust_vmx_controls(KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL,
 				KVM_OPTIONAL_VMX_PIN_BASED_VM_EXEC_CONTROL,
 				MSR_IA32_VMX_PINBASED_CTLS,
@@ -2886,6 +2898,10 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
 				       _vmentry_control, _vmexit_control))
 		return -EIO;
 
+	if (vmx_check_entry_exit_pairs(vmcs_entry_exit_2nd_pairs,
+				       _vmentry_control, _vmexit_2nd_control))
+		return -EIO;
+
 	/*
 	 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
 	 * can't be used due to an errata where VM Exit may incorrectly clear
@@ -2935,6 +2951,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
 	vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
 	vmcs_conf->cpu_based_3rd_exec_ctrl = _cpu_based_3rd_exec_control;
 	vmcs_conf->vmexit_ctrl         = _vmexit_control;
+	vmcs_conf->vmexit_2nd_ctrl     = _vmexit_2nd_control;
 	vmcs_conf->vmentry_ctrl        = _vmentry_control;
 	vmcs_conf->misc	= misc_msr;
 
@@ -4629,6 +4646,11 @@ static u32 vmx_get_initial_vmexit_ctrl(void)
 		  VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL);
 }
 
+static u64 vmx_get_initial_secondary_vmexit_ctrl(void)
+{
+	return vmcs_config.vmexit_2nd_ctrl;
+}
+
 void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -4973,6 +4995,9 @@ static void init_vmcs(struct kvm_vcpu *vcpu)
 
 	vm_exit_controls_set(vmx, vmx_get_initial_vmexit_ctrl());
 
+	if (cpu_has_secondary_vmexit_ctrls())
+		secondary_vm_exit_controls_set(vmx, vmx_get_initial_secondary_vmexit_ctrl());
+
 	/* 22.2.1, 20.8.1 */
 	vm_entry_controls_set(vmx, vmx_get_initial_vmentry_ctrl());
 
arch/x86/kvm/vmx/vmx.h +6 -1
index 74cd50f29fa4..e8c6ce23168a 100644
@@ -457,7 +457,11 @@ static inline u8 vmx_get_rvi(void)
 	       VM_EXIT_PT_CONCEAL_PIP |					\
 	       VM_EXIT_CLEAR_IA32_RTIT_CTL |				\
 	       VM_EXIT_LOAD_CET_STATE |					\
-	       VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL)
+	       VM_EXIT_SAVE_IA32_PERF_GLOBAL_CTRL |			\
+	       VM_EXIT_ACTIVATE_SECONDARY_CONTROLS)
+
+#define KVM_REQUIRED_VMX_SECONDARY_VM_EXIT_CONTROLS 0
+#define KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS 0
 
 #define KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL			\
 	(PIN_BASED_EXT_INTR_MASK |					\
@@ -571,6 +575,7 @@ static __always_inline void lname##_controls_changebit(struct vcpu_vmx *vmx, u##
 }
 BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS, 32)
 BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS, 32)
+BUILD_CONTROLS_SHADOW(secondary_vm_exit, SECONDARY_VM_EXIT_CONTROLS, 64)
 BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL, 32)
 BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL, 32)
 BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL, 32)
Signature · 1 line
2.43.0
#2 lore raw
Quoted 6 lines · Sohil Mehta

From: "Xin Li (Intel)" <[email protected]>

The secondary VM-exit controls are a 64-bit VMCS field for controls that no longer fit in the 32-bit primary field. Bit 31 of the primary VM-exit controls activates them. No feature KVM supports uses any of these controls yet, but the upcoming FRED KVM support is planning to use them.

This reads more like background explaining what the secondary controls are than a description of what the patch actually does, so looks misaligned with the guidline in:

https://docs.kernel.org/process/maintainer-kvm-x86.html

Changelog
... lead with a short blurb on the actual changes, and then follow up with the context and background.

Based on what I understood, perhaps something like:

Support the new 64-bit secondary VM-exit controls in VMCS configuration and its shadow data. Activate the controls in vmcs01 whenever hardware supports them, and copy them into vmcs02.

I also think the subject could be simply:

KVM: nVMX: Support the secondary VM-exit controls

...

Quoted 5 lines

Copy the controls into vmcs02 as well. vmcs02 inherits the activation bit from vmcs01, so without the copy it would run with the secondary controls active but empty. That is harmless while every bit is clear. Once a feature sets one, though, L2 would run with it silently off, leaving the host with L2's state after a VM exit.

Just to clarify: so, you resolved by copying into vmcs02; otherwise, the issue there, right?

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +5 -0
+static u64 vmx_get_initial_secondary_vmexit_ctrl(void)
+{
+	return vmcs_config.vmexit_2nd_ctrl;
+}
+

...

arch/x86/kvm/vmx/vmx.c static void init_vmcs(struct kvm_vcpu *vcpu) · line 4999

Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2 -0
+	if (cpu_has_secondary_vmexit_ctrls())
+		secondary_vm_exit_controls_set(vmx, vmx_get_initial_secondary_vmexit_ctrl());

I was wondering back in V10 why the wrapper is necessary without any gating there. This seems readable to me:

  secondary_vm_exit_controls_set(vmx, vmcs_config.vmexit_2nd_ctrl);

Thanks,
Chang

#3 lore raw
Quoted 8 lines · Chang S. Bae
Quoted 6 lines · Sohil Mehta

From: "Xin Li (Intel)" <[email protected]>

The secondary VM-exit controls are a 64-bit VMCS field for controls that no longer fit in the 32-bit primary field. Bit 31 of the primary VM-exit controls activates them. No feature KVM supports uses any of these controls yet, but the upcoming FRED KVM support is planning to use them.

Quoted 6 lines

This reads more like background explaining what the secondary controls are than a description of what the patch actually does, so looks misaligned with the guidline in:

  https://docs.kernel.org/process/maintainer-kvm-x86.html

Yeah, core x86 habits :) I thought the intro paragraph was short enough that it can be easily skimmed.

Quoted 10 lines

  Changelog
  ... lead with a short blurb on the actual changes, and then follow up with the context and background.

Based on what I understood, perhaps something like:

  Support the new 64-bit secondary VM-exit controls in VMCS configuration and its shadow data. Activate the controls in vmcs01 whenever hardware supports them, and copy them into vmcs02.

Sure, I can add a small blurb in the beginning.

Quoted 4 lines

I also think the subject could be simply:

  KVM: nVMX: Support the secondary VM-exit controls

Why the nVMX? Most of the patch is VMX. It does touch vmcs02, but only so that L0's own controls stay in effect while an L2 runs. L1 doesn't see the field yet. So I think this would be more appropriate:

KVM: VMX: Support the secondary VM-exit controls

Quoted 10 lines

...

Quoted 5 lines

Copy the controls into vmcs02 as well. vmcs02 inherits the activation bit from vmcs01, so without the copy it would run with the secondary controls active but empty. That is harmless while every bit is clear. Once a feature sets one, though, L2 would run with it silently off, leaving the host with L2's state after a VM exit.

Just to clarify: so, you resolved by copying into vmcs02; otherwise, the issue there, right?

Yes, that's right. The issue would only happen once a feature sets a control.

Quoted 6 lines
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +5 -0
+static u64 vmx_get_initial_secondary_vmexit_ctrl(void)
+{
+    return vmcs_config.vmexit_2nd_ctrl;
+}
+

...

Quoted 6 lines
Quoted 3 lines

+    if (cpu_has_secondary_vmexit_ctrls())
+        secondary_vm_exit_controls_set(vmx,
vmx_get_initial_secondary_vmexit_ctrl());

I was wondering back in V10 why the wrapper is necessary without any gating there.

This is mainly to keep it consistent with vmx_get_initial_vmexit_ctrl(). Also, similar to the primary function, the secondary will likely mask features out in the future. So, adding the wrapper now would avoid the churn.

Quoted 4 lines

This seems readable to me:

  secondary_vm_exit_controls_set(vmx, vmcs_config.vmexit_2nd_ctrl);

#4 lore raw

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

In reply to Sohil Mehta

The VM-entry/VM-exit pair check only requires one of a pair's exit controls to be supported along with the entry control. That is fine while every pair has a single exit control.

However, a feature can have multiple exit controls paired with a single entry control. For example, PAT and EFER each have two exit controls, but KVM doesn't use their save controls. In future, FRED will use both:
its state is loaded on VM-entry, and both saved and restored on VM-exit.

If hardware supports only one of those exit controls, the existing check passes and the feature would be enabled with part of its state left unswitched. Require all of a pair's controls to be supported, or none of them. The existing pairs are unaffected.

---
v1:
- New patch. It addresses a gap Sashiko found in v10 patch 2:
https://lore.kernel.org/all/[email protected]/
- Doesn't include a fixes tag because all existing pairs have a single exit control.

---
 arch/x86/kvm/vmx/vmx.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +7 -1
index 3752a86c3fca..d35bad8b29a7 100644
@@ -2716,6 +2716,11 @@ static u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr)
 	return  ctl_opt & allowed;
 }
 
+/*
+ * 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)	\
 ({										\
 	int i, r = 0;								\
@@ -2727,7 +2732,8 @@ 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 ((exit_controls & x_ctrl) ==					\
+		    ((entry_controls & n_ctrl) ? x_ctrl : 0))			\
 			continue;						\
 										\
 		pr_warn_once("Inconsistent VM-Entry/VM-Exit pair, "		\
Signature · 1 line
2.43.0
#5 lore raw

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

Quoted 8 lines · Sohil Mehta

+/*
+ * 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) \
({ \

...

arch/x86/kvm/vmx/vmx.c static u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) · line 2737

Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +2 -1
-		if (!(entry_controls & n_ctrl) == !(exit_controls & x_ctrl))	\
+		if ((exit_controls & x_ctrl) ==					\
+		    ((entry_controls & n_ctrl) ? x_ctrl : 0))			\
  			continue;						\

Nit: Since this condition is a little difficult to follow without explanation nearby, perhaps better to move the comment down right there.

Thanks,
Chang

#6 lore raw

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

Quoted 11 lines · Chang S. Bae
Quoted 9 lines · Sohil Mehta

  +/*
+ * 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)    \
  ({                                        \

...

Quoted 9 lines
Quoted 5 lines

-        if (!(entry_controls & n_ctrl) == !(exit_controls & x_ctrl))    \
+        if ((exit_controls & x_ctrl) ==                    \
+            ((entry_controls & n_ctrl) ? x_ctrl : 0))            \
              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.

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.

#7 lore raw

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

Quoted 11 lines · Sohil Mehta
Quoted 10 lines · Chang S. Bae
Quoted 9 lines · Sohil Mehta

  +/*
+ * 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)    \
  ({                                        \

Quoted 5 lines
Quoted 5 lines

...

Quoted 4 lines

-        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:

arch/x86/kvm/vmx/vmx.c +14 -1
index b341af4c6057..dd7b9b67aaec 100644
@@ -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, "		\
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

              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.

Quoted 3 lines

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.

#8 lore raw

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

Quoted 6 lines · Sean Christopherson

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?

I tested that this works for FRED's 1:2 (VM-entry control: VM-exit control) scenario. I don't think the SDM has a M:1 counterpart but future-proofing this seems fine to me.

Quoted 16 lines
arch/x86/kvm/vmx/vmx.c +9 -0
+
+/*
+ * 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;		\

arch/x86/kvm/vmx/vmx.c static u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) · line 2746

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +5 -1
 										\
-		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, "		\

This looks good. I'll incorporate it in the next version.

Quoted 3 lines

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

I wouldn't say it can never work :) call_on_irqstack_cond() does it twice with \ at the end of each comment line. But in this case there is no compelling reason to do it.

#9 lore raw

PATCH 3/3 KVM: VMX: Dump the secondary VM-exit controls in dump_vmcs()

In reply to Sohil Mehta

From: "Xin Li (Intel)" <[email protected]>

KVM now activates the secondary VM-exit controls whenever hardware supports them. Print them next to the primary VM-exit controls in the VMCS dump to help debug failed VM-entries.

While at it, give the entry and exit controls the 0x prefix that the execution control fields already have.

---
v1:
- New patch, split out of FRED KVM v10 series so that the dump covers the field this series adds.

---
 arch/x86/kvm/vmx/vmx.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +10 -3
index d35bad8b29a7..7291c2916ff6 100644
@@ -6493,7 +6493,7 @@ void dump_vmcs(struct kvm_vcpu *vcpu)
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	u32 vmentry_ctl, vmexit_ctl;
 	u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
-	u64 tertiary_exec_control;
+	u64 tertiary_exec_control, secondary_vmexit_ctl;
 	unsigned long cr4;
 	int efer_slot;
 
@@ -6518,6 +6518,11 @@ void dump_vmcs(struct kvm_vcpu *vcpu)
 	else
 		tertiary_exec_control = 0;
 
+	if (cpu_has_secondary_vmexit_ctrls())
+		secondary_vmexit_ctl = vmcs_read64(SECONDARY_VM_EXIT_CONTROLS);
+	else
+		secondary_vmexit_ctl = 0;
+
 	pr_err("VMCS %p, last attempted VM-entry on CPU %d\n",
 	       vmx->loaded_vmcs->vmcs, vcpu->arch.last_vmentry_cpu);
 	pr_err("*** Guest State ***\n");
@@ -6626,8 +6631,10 @@ void dump_vmcs(struct kvm_vcpu *vcpu)
 	pr_err("*** Control State ***\n");
 	pr_err("CPUBased=0x%08x SecondaryExec=0x%08x TertiaryExec=0x%016llx\n",
 	       cpu_based_exec_ctrl, secondary_exec_control, tertiary_exec_control);
-	pr_err("PinBased=0x%08x EntryControls=%08x ExitControls=%08x\n",
-	       pin_based_exec_ctrl, vmentry_ctl, vmexit_ctl);
+	pr_err("PinBased=0x%08x EntryControls=0x%08x\n",
+	       pin_based_exec_ctrl, vmentry_ctl);
+	pr_err("ExitControls=0x%08x SecondaryExitControls=0x%016llx\n",
+	       vmexit_ctl, secondary_vmexit_ctl);
 	pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
 	       vmcs_read32(EXCEPTION_BITMAP),
 	       vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
Signature · 1 line
2.43.0
Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] KVM: VMX: Dump the secondary VM-exit controls in dump_vmcs()

Quoted 4 lines · Sohil Mehta

KVM now activates the secondary VM-exit controls whenever hardware supports them. Print them next to the primary VM-exit controls in the VMCS dump to help debug failed VM-entries.

...

Quoted 2 lines

- New patch, split out of FRED KVM v10 series so that the dump covers the field this series adds.

Yeah, this looks nice to have in this series as taking the secondary control part from patch18 there:

Thanks,
Chang