Skip to contentSkip to conversation

KVM: selftest: Move XFD CPUID checking out of __vm_xsave_require_permission()

PATCH In review Wang, Lei 2 participants 3 messages Nov 25, 2022 – Nov 29, 2022

Subsystems (2)
lore
Share & tools
lore

Wang, Lei

author
#0 lore raw

PATCH 1/1 KVM: selftest: Move XFD CPUID checking out of __vm_xsave_require_permission()

kvm_cpu_has(X86_FEATURE_XFD) will call kvm_get_supported_cpuid() which will cache the cpuid information when it is firstly called. Move this line out of __vm_xsave_require_permission() and check it afterwards so that the CPUID change will not be veiled by the cached CPUID information.

---
 tools/testing/selftests/kvm/lib/x86_64/processor.c | 2 --
 tools/testing/selftests/kvm/x86_64/amx_test.c      | 1 +
 2 files changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -2
index 39c4409ef56a..5686eacd4700 100644
@@ -616,8 +616,6 @@ void __vm_xsave_require_permission(int bit, const char *name)
 		.addr = (unsigned long) &bitmask
 	};
 
-	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XFD));
-
 	kvm_fd = open_kvm_dev_path_or_exit();
 	rc = __kvm_ioctl(kvm_fd, KVM_GET_DEVICE_ATTR, &attr);
 	close(kvm_fd);
tools/testing/selftests/kvm/x86_64/amx_test.c +1 -0
index dadcbad10a1d..1e3457ff304b 100644
@@ -312,6 +312,7 @@ int main(int argc, char *argv[])
 	/* Create VM */
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XFD));
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVE));
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_AMX_TILE));
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XTILECFG));
Signature · 1 line
2.34.1
#1 lore raw
Quoted 4 lines · Lei

kvm_cpu_has(X86_FEATURE_XFD) will call kvm_get_supported_cpuid() which will cache the cpuid information when it is firstly called. Move this line out of __vm_xsave_require_permission() and check it afterwards so that the CPUID change will not be veiled by the cached CPUID information.

Please call out exactly what CPUID change is being referred to. Someone that doesn't already know about ARCH_REQ_XCOMP_GUEST_PERM and it's interaction with KVM_GET_SUPPORTED_CPUID will have zero clue what this fixes.

E.g.

Move the kvm_cpu_has() check on X86_FEATURE_XFD out of the helper to enable off-by-default XSAVE-managed features and into the one test that
currenty requires XFD (XFeature Disable) support. kvm_cpu_has() uses
kvm_get_supported_cpuid() and thus caches KVM_GET_SUPPORTED_CPUID, and so using kvm_cpu_has() before ARCH_REQ_XCOMP_GUEST_PERM effectively results in the test caching stale values, e.g. subsequent checks on AMX_TILE will get false negatives.

Although off-by-default features are nonsensical without XFD, checking for XFD virtualization prior to enabling such features isn't strictly required.

Fixes 7fbb653e01fd ("KVM: selftests: Check KVM's supported CPUID, not host CPUID, for XFD")
Quoted 24 lines
---
 tools/testing/selftests/kvm/lib/x86_64/processor.c | 2 --
 tools/testing/selftests/kvm/x86_64/amx_test.c      | 1 +
 2 files changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -2
index 39c4409ef56a..5686eacd4700 100644
@@ -616,8 +616,6 @@ void __vm_xsave_require_permission(int bit, const char *name)
 		.addr = (unsigned long) &bitmask
 	};
 
-	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XFD));
-
 	kvm_fd = open_kvm_dev_path_or_exit();
 	rc = __kvm_ioctl(kvm_fd, KVM_GET_DEVICE_ATTR, &attr);
 	close(kvm_fd);
tools/testing/selftests/kvm/x86_64/amx_test.c +0 -0
index dadcbad10a1d..1e3457ff304b 100644

tools/testing/selftests/kvm/x86_64/amx_test.c int main(int argc, char *argv[]) · line 315

Quoted 4 lines
tools/testing/selftests/kvm/x86_64/amx_test.c +1 -0
@@ -312,6 +312,7 @@ int main(int argc, char *argv[])
 	/* Create VM */
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XFD));

I think we should disallow kvm_get_supported_cpuid() before __vm_xsave_require_permission(), otherwise we'll reintroduce a similar bug in the future.

tools/testing/selftests/kvm/x86_64/amx_test.c int main(int argc, char *argv[]) · line 318

Quoted 3 lines
tools/testing/selftests/kvm/x86_64/amx_test.c +0 -0
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVE));
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_AMX_TILE));
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XTILECFG));

And then as a follow-up, we should move these above vm_create_with_one_vcpu(), checking them after vm_create_with_one_vcpu() is odd.

I'll send a v2 with the reworded changelog and additional patches to assert that __vm_xsave_require_permission() isn't used after kvm_get_supported_cpuid().

author
#2 lore raw
Quoted 20 lines · Sean Christopherson
Quoted 4 lines · Lei

kvm_cpu_has(X86_FEATURE_XFD) will call kvm_get_supported_cpuid() which will cache the cpuid information when it is firstly called. Move this line out of __vm_xsave_require_permission() and check it afterwards so that the CPUID change will not be veiled by the cached CPUID information.

Please call out exactly what CPUID change is being referred to. Someone that doesn't already know about ARCH_REQ_XCOMP_GUEST_PERM and it's interaction with KVM_GET_SUPPORTED_CPUID will have zero clue what this fixes.

E.g.

Move the kvm_cpu_has() check on X86_FEATURE_XFD out of the helper to enable off-by-default XSAVE-managed features and into the one test that
currenty requires XFD (XFeature Disable) support. kvm_cpu_has() uses
kvm_get_supported_cpuid() and thus caches KVM_GET_SUPPORTED_CPUID, and so using kvm_cpu_has() before ARCH_REQ_XCOMP_GUEST_PERM effectively results in the test caching stale values, e.g. subsequent checks on AMX_TILE will get false negatives.

Quoted 5 lines

Although off-by-default features are nonsensical without XFD, checking for XFD virtualization prior to enabling such features isn't strictly required.

Fixes 7fbb653e01fd ("KVM: selftests: Check KVM's supported CPUID, not host CPUID, for XFD")

Makes sense.

Quoted 27 lines
Quoted 27 lines
---
 tools/testing/selftests/kvm/lib/x86_64/processor.c | 2 --
 tools/testing/selftests/kvm/x86_64/amx_test.c      | 1 +
 2 files changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -2
index 39c4409ef56a..5686eacd4700 100644
@@ -616,8 +616,6 @@ void __vm_xsave_require_permission(int bit, const char *name)
 		.addr = (unsigned long) &bitmask
 	};
 
-	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XFD));
-
 	kvm_fd = open_kvm_dev_path_or_exit();
 	rc = __kvm_ioctl(kvm_fd, KVM_GET_DEVICE_ATTR, &attr);
 	close(kvm_fd);
tools/testing/selftests/kvm/x86_64/amx_test.c +0 -0
index dadcbad10a1d..1e3457ff304b 100644
@@ -312,6 +312,7 @@ int main(int argc, char *argv[])
 	/* Create VM */
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 
Quoted 5 lines

tools/testing/selftests/kvm/x86_64/amx_test.c int main(int argc, char *argv[]) · line 315

Quoted 1 line
tools/testing/selftests/kvm/x86_64/amx_test.c +1 -0
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XFD));

I think we should disallow kvm_get_supported_cpuid() before __vm_xsave_require_permission(), otherwise we'll reintroduce a similar bug in the future.

Agree.

Quoted 9 lines

tools/testing/selftests/kvm/x86_64/amx_test.c int main(int argc, char *argv[]) · line 318

Quoted 3 lines
tools/testing/selftests/kvm/x86_64/amx_test.c +0 -0
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVE));
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_AMX_TILE));
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XTILECFG));

And then as a follow-up, we should move these above vm_create_with_one_vcpu(), checking them after vm_create_with_one_vcpu() is odd.

I'll send a v2 with the reworded changelog and additional patches to assert that __vm_xsave_require_permission() isn't used after kvm_get_supported_cpuid().

Already seen your patches, thanks Sean!