Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Verify KVM is loaded when getting a KVM module param

Probe /dev/kvm when getting a KVM module param so that attempting to load
a module param super early in a selftest generates a SKIP message about
KVM not being loaded/enabled, versus some random parameter not existing.

E.g. KVM x86's unconditional retrieval of force_emulation_prefix during
kvm_selftest_arch_init() generates a rather confusing error message that
takes far too much triage to understand.

Link: https://lore.kernel.org/r/20250516215909.2551628-2-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

+9 -12
+5 -1
tools/testing/selftests/kvm/include/x86/processor.h
··· 1150 1150 1151 1151 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits); 1152 1152 void kvm_init_vm_address_properties(struct kvm_vm *vm); 1153 - bool vm_is_unrestricted_guest(struct kvm_vm *vm); 1154 1153 1155 1154 struct ex_regs { 1156 1155 uint64_t rax, rcx, rdx, rbx; ··· 1322 1323 static inline bool kvm_is_forced_emulation_enabled(void) 1323 1324 { 1324 1325 return !!get_kvm_param_integer("force_emulation_prefix"); 1326 + } 1327 + 1328 + static inline bool kvm_is_unrestricted_guest_enabled(void) 1329 + { 1330 + return get_kvm_intel_param_bool("unrestricted_guest"); 1325 1331 } 1326 1332 1327 1333 uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
+3
tools/testing/selftests/kvm/lib/kvm_util.c
··· 64 64 ssize_t bytes_read; 65 65 int fd, r; 66 66 67 + /* Verify KVM is loaded, to provide a more helpful SKIP message. */ 68 + close(open_kvm_dev_path_or_exit()); 69 + 67 70 r = snprintf(path, path_size, "/sys/module/%s/parameters/%s", 68 71 module_name, param); 69 72 TEST_ASSERT(r < path_size,
-10
tools/testing/selftests/kvm/lib/x86/processor.c
··· 1264 1264 return min(max_gfn, ht_gfn - 1); 1265 1265 } 1266 1266 1267 - /* Returns true if kvm_intel was loaded with unrestricted_guest=1. */ 1268 - bool vm_is_unrestricted_guest(struct kvm_vm *vm) 1269 - { 1270 - /* Ensure that a KVM vendor-specific module is loaded. */ 1271 - if (vm == NULL) 1272 - close(open_kvm_dev_path_or_exit()); 1273 - 1274 - return get_kvm_intel_param_bool("unrestricted_guest"); 1275 - } 1276 - 1277 1267 void kvm_selftest_arch_init(void) 1278 1268 { 1279 1269 host_cpu_is_intel = this_cpu_is_intel();
+1 -1
tools/testing/selftests/kvm/x86/vmx_exception_with_invalid_guest_state.c
··· 110 110 struct kvm_vm *vm; 111 111 112 112 TEST_REQUIRE(host_cpu_is_intel); 113 - TEST_REQUIRE(!vm_is_unrestricted_guest(NULL)); 113 + TEST_REQUIRE(!kvm_is_unrestricted_guest_enabled()); 114 114 115 115 vm = vm_create_with_one_vcpu(&vcpu, guest_code); 116 116 get_set_sigalrm_vcpu(vcpu);