[PATCH] KVM: selftests: Use __vmread() for the optional HOST_IA32_PERF_GLOBAL_CTRL field

From: Javid Rzayev

Date: Sun Nov 22 2026 - 19:34:20 EST


The HOST_IA32_PERF_GLOBAL_CTRL VMCS field exists only if the CPU supports
the "load IA32_PERF_GLOBAL_CTRL" VM-exit control, which is not always the
case. E.g. when running on an Intel VM in Google Cloud with nested
virtualization enabled and no vPMU, VMREAD of this field fails.

init_vmcs_guest_state() copies the field into GUEST_IA32_PERF_GLOBAL_CTRL.
The write already uses __vmwrite() because the field is optional, but the
read uses vmread(), which asserts on failure since commit 4e824a04d4fa
("KVM: selftests: Always assert that vmreadz() succeeds"). As a result,
nested VMX tests (e.g. state_test, smm_test, nested_close_kvm_test) fail
with:

vmread[0x2c04] hit VM-Fail

Read the field with __vmread() and copy it only if the read succeeds.

Fixes: 4e824a04d4fa ("KVM: selftests: Always assert that vmreadz() succeeds")
Signed-off-by: Javid Rzayev <javid.rzyv@xxxxxxxxx>
---
Tested on a Google Cloud n2-standard-128 VM (Intel, nested
virtualization enabled, no vPMU) running next-20261120:

before: 15 nested VMX tests fail with "vmread[0x2c04] hit VM-Fail"
after: all of them pass

mmu_stress_test hits the 120s kselftest timeout both before and after
the patch; that is unrelated (the test scales with the host size).

tools/testing/selftests/kvm/lib/x86/vmx.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/x86/vmx.c b/tools/testing/selftests/kvm/lib/x86/vmx.c
index 5a06746eb..4b3e439ee 100644
--- a/tools/testing/selftests/kvm/lib/x86/vmx.c
+++ b/tools/testing/selftests/kvm/lib/x86/vmx.c
@@ -348,6 +348,8 @@ static inline void init_vmcs_host_state(void)
*/
static inline void init_vmcs_guest_state(void *rip, void *rsp)
{
+ u64 val;
+
vmwrite(GUEST_ES_SELECTOR, vmread(HOST_ES_SELECTOR));
vmwrite(GUEST_CS_SELECTOR, vmread(HOST_CS_SELECTOR));
vmwrite(GUEST_SS_SELECTOR, vmread(HOST_SS_SELECTOR));
@@ -363,8 +365,8 @@ static inline void init_vmcs_guest_state(void *rip, void *rsp)
vmwrite(GUEST_IA32_DEBUGCTL, 0);
__vmwrite(GUEST_IA32_PAT, vmread(HOST_IA32_PAT));
__vmwrite(GUEST_IA32_EFER, vmread(HOST_IA32_EFER));
- __vmwrite(GUEST_IA32_PERF_GLOBAL_CTRL,
- vmread(HOST_IA32_PERF_GLOBAL_CTRL));
+ if (!__vmread(HOST_IA32_PERF_GLOBAL_CTRL, &val))
+ __vmwrite(GUEST_IA32_PERF_GLOBAL_CTRL, val);

vmwrite(GUEST_ES_LIMIT, -1);
vmwrite(GUEST_CS_LIMIT, -1);
--
2.47.3