Skip to contentSkip to conversation

KVM: VMX: Support the secondary VM-exit controls

PATCH Superseded Sohil Mehta 4 participants 13 messages Dec 3

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM: VMX: Support the secondary VM-exit controls

Some of these patches started out as part of the FRED KVM series [1]. I am working towards getting the core infrastructure pieces ready first then the FRED guest support followed by the nested FRED support.

This series focusses on getting the secondary VM-exit controls support in place independent of FRED. Preferably, it can be merged into the KVM tree separately. Otherwise, I'll place the patches at the beginning of the FRED series for a combined merge.

Changes in v2
-------------
- Split the pair check into "all supported" and "none supported" cases, which also covers a pair with several entry controls (Sean) [patch 2]
- Lead the changelogs with a small summary of changes (Chang) [patches 1, 2]
- Pick up Chang's review tag [patch 3]

v1: https://lore.kernel.org/lkml/[email protected]/

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. Binbin, I have kept your review tag since most of the changes are minor. The only significant change on top of FRED KVM v10 is the vmcs02 handling described below.

Patch 2 fixes an issue reported by sashiko related to the entry/exit pair checking [2] 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
-------
This was tested on a FRED capable 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.

Patch 2's new check was also tested with the upcoming FRED guest support, whose pair has two exit controls. KVM keeps the pair when all three controls are supported and rejects it when any one is forced off.

Links
-----
[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: 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: Support 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           | 52 +++++++++++++++++++++++++++++---
 arch/x86/kvm/vmx/vmx.h           |  7 ++++-
 7 files changed, 68 insertions(+), 5 deletions(-)

base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 KVM: VMX: Support the secondary VM-exit controls

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

Add the VMCS field for the secondary VM-exit controls, its controls shadow and the vmcs_config plumbing. Activate the controls in vmcs01 whenever hardware supports them, and copy them into vmcs02.

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

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.

---
v2:
- Lead the changelog with the changes, and shorten the subject (Chang).

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
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] KVM: VMX: evmcs_sanitize_exec_ctrls() omits vmcs_conf->vmexit_2nd_ctrl --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] KVM: VMX: evmcs_sanitize_exec_ctrls() omits vmcs_conf->vmexit_2nd_ctrl
--

commit c3fad2255d445005d76a89541e2fce2975f5ba30
Author: Xin Li (Intel) <[email protected]>

KVM: VMX: Support the secondary VM-exit controls

This commit adds the VMCS field, controls shadow, and vmcs_config plumbing for the 64-bit secondary VM-exit controls, activating them in vmcs01 when supported and copying them into vmcs02.

Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +0 -0
index 68fa251dda600..3752a86c3fca6 100644

[ ... ]

arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2956

Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +1 -0
@@ -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;

[Severity: Low]
Should evmcs_sanitize_exec_ctrls() also sanitize vmcs_conf->vmexit_2nd_ctrl?

setup_vmcs_config() calls evmcs_sanitize_exec_ctrls() right after this when enlightened_vmcs is enabled. In arch/x86/kvm/vmx/vmx_onhyperv.c, it strips VM_EXIT_ACTIVATE_SECONDARY_CONTROLS from vmcs_conf->vmexit_ctrl, but leaves vmcs_conf->vmexit_2nd_ctrl untouched.

KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS is 0 today, so this is latent. Once a feature sets a bit there, Hyper-V eVMCSv1 leaves vmexit_2nd_ctrl non-zero despite lacking SECONDARY_VM_EXIT_CONTROLS, so capability checks on vmcs_config.vmexit_2nd_ctrl would falsely report support.

Adding a 0ULL mask in arch/x86/kvm/vmx/hyperv_evmcs.h and sanitizing vmexit_2nd_ctrl in evmcs_sanitize_exec_ctrls(), matching EVMCS1_SUPPORTED_3RDEXEC for cpu_based_3rd_exec_ctrl, would keep vmcs_config consistent under eVMCS.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
In reply to Sohil Mehta
Quoted 12 lines · Sohil Mehta

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

Add the VMCS field for the secondary VM-exit controls, its controls shadow and the vmcs_config plumbing. Activate the controls in vmcs01 whenever hardware supports them, and copy them into vmcs02.

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. With every control bit clear, KVM behaves exactly as before.

Quoted 5 lines

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.

This only matters with error_on_inconsistent_vmcs_config=0, because kvm_intel refuses to load on a failed check by default. Sean's reason in [1] is the stronger one:

"two tables provides better separation, which makes it easier to follow what's going on, and avoids "polluting" every entry with empty fields."

I would use it here instead and add a link to that discussion.

[1]: https://lore.kernel.org/all/[email protected]/

#4 lore raw
Quoted 11 lines
Quoted 6 lines

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.

This only matters with error_on_inconsistent_vmcs_config=0, because kvm_intel refuses to load on a failed check by default. Sean's reason in [1] is the stronger one:

Good point.

Quoted 4 lines

"two tables provides better separation, which makes it easier to follow what's going on, and avoids "polluting" every entry with empty fields."

The "polluting" part is hard to decipher without going through the link. I am thinking of rewording the above paragraph as:

Check the secondary controls through their own entry/exit pair table, which stays empty until the first feature adds a pair. A shared table would need both a primary and a secondary exit control in every row, leaving most rows with an empty field. Two tables keep the rows simple and easier to follow [1].

Quoted 3 lines

I would use it here instead and add a link to that discussion.

[1]: https://lore.kernel.org/all/[email protected]/

#5 lore raw
In reply to Sohil Mehta

+Vitaly

Quoted 9 lines
 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(-)

Sashiko identified a sanitization item in kvm/vmx/ related to Hyper-V's eVMCS.

https://lore.kernel.org/all/[email protected]/

The change is just two additional lines and avoids a future pitfall. So, I plan to add the below diff to this patch. I don't think this warrants a separate patch, especially since vmexit_2nd_ctrl is defined here. Please let me know if anyone prefers otherwise.

Changelog update:

Like the tertiary controls, clear the secondary VM-exit controls when KVM is running on Hyper-V, as eVMCS has no field for them.

(quoted diff) +0 -0

b/arch/x86/kvm/vmx/hyperv_evmcs.h
index 052b62b8ed8e..99a4be8a2a40 100644

arch/x86/kvm/vmx/hyperv_evmcs.h +2 -0
@@ -106,6 +106,8 @@
 	 VM_EXIT_PT_CONCEAL_PIP |					\
 	 VM_EXIT_CLEAR_IA32_RTIT_CTL)
 
+#define EVMCS1_SUPPORTED_VMEXIT_2ND_CTRL (0ULL)
+
 #define EVMCS1_SUPPORTED_VMENTRY_CTRL					\
 	(VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |				\
 	 VM_ENTRY_LOAD_DEBUG_CONTROLS |					\
(quoted diff) +0 -0

b/arch/x86/kvm/vmx/vmx_onhyperv.c
index b9a8b91166d0..56afb372e50d 100644

arch/x86/kvm/vmx/vmx_onhyperv.c +0 -0
@@ -33,4 +33,5 @@ void evmcs_sanitize_exec_ctrls(struct vmcs_config

*vmcs_conf)
evmcs_check_vmcs_conf(cpu_based_3rd_exec_ctrl, 3RDEXEC); evmcs_check_vmcs_conf(vmentry_ctrl, VMENTRY_CTRL); evmcs_check_vmcs_conf(vmexit_ctrl, VMEXIT_CTRL);
+ evmcs_check_vmcs_conf(vmexit_2nd_ctrl, VMEXIT_2ND_CTRL);
}

#6 lore raw

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

In reply to Sohil Mehta

Make the VM-entry/VM-exit pair check require all of a pair's controls to be supported, or none of them. The existing pairs are unaffected.

Currently, the 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. Check the entry side the same way, which also covers a pair with several entry controls, although no such pair exists today.

---
v2:
- Split the check into "all supported" and "none supported" cases, which also covers a pair with several entry controls (Sean).
- Lead the changelog with the change.

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 | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +13 -1
index 3752a86c3fca..1387d400fded 100644
@@ -2716,6 +2716,14 @@ 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
+ * versa, 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 exists,
+ * 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 +2735,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, "		\
Signature · 1 line
2.43.0
Reviewed-by on v2 2/3
#7 lore raw

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

Quoted 11 lines · Sohil Mehta

Make the VM-entry/VM-exit pair check require all of a pair's controls to be supported, or none of them. The existing pairs are unaffected.

Currently, the 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.

Nit:

I feel this sentence is a bit loose.
If there is a new version,  how about:
In the future, FRED will use both exit controls: VM-entry loads the guest FRED state, and VM-exit saves the guest state and loads the host state.

Quoted 7 lines

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. Check the entry side the same way, which also covers a pair with several entry controls, although no such pair exists today.

#8 lore raw

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

Quoted 5 lines · Binbin Wu

I feel this sentence is a bit loose.
If there is a new version,  how about:
In the future, FRED will use both exit controls: VM-entry loads the guest FRED state, and VM-exit saves the guest state and loads the host state.

You are right. To avoid giving the impression that this patch is unique to FRED, I did not include the details. Though a couple of extra words won't hurt. Will include them in the next version.

Quoted 1 line

Thanks!

#9 lore raw

PATCH v2 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.

---
v2:
- Pick up Chang's review tag.

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 1387d400fded..7899ec885084 100644
@@ -6499,7 +6499,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;
 
@@ -6524,6 +6524,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");
@@ -6632,8 +6637,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 v2 3/3
#10 lore raw

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

Quoted 6 lines · 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.

Quoted 6 lines

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

Reviewed-by on the v2 series
#11 lore raw
In reply to Sohil Mehta
Quoted 8 lines · Sohil Mehta

Some of these patches started out as part of the FRED KVM series [1]. I am working towards getting the core infrastructure pieces ready first then the FRED guest support followed by the nested FRED support.

This series focusses on getting the secondary VM-exit controls support in place independent of FRED. Preferably, it can be merged into the KVM tree separately. Otherwise, I'll place the patches at the beginning of the FRED series for a combined merge.

The series looks good to me. I just have one nit on the changelog of patch 1. For the series:

#12 lore raw
In reply to Sohil Mehta

Hi Sean,

Quoted 9 lines · Sohil Mehta

Some of these patches started out as part of the FRED KVM series [1]. I am working towards getting the core infrastructure pieces ready first then the FRED guest support followed by the nested FRED support.

This series focusses on getting the secondary VM-exit controls support in place independent of FRED. Preferably, it can be merged into the KVM tree separately. Otherwise, I'll place the patches at the beginning of the FRED series for a combined merge.

Are you considering this series for the upcoming merge cycle? It seems ready for your review.

There are a couple of changelog nits and a minor update to patch 1. But all the patches have review tags from Chao and Binbin. I am thinking of sending out an updated version later today/tomorrow.

No KVM behavior changes with the series, so the risk is limited, but there are no gains either. Mainly this would help reduce the load on the FRED KVM series.

Thanks!