]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: selftests: Verify KVM is loaded when getting a KVM module param
authorSean Christopherson <seanjc@google.com>
Fri, 16 May 2025 21:59:06 +0000 (14:59 -0700)
committerSean Christopherson <seanjc@google.com>
Fri, 20 Jun 2025 20:39:10 +0000 (13:39 -0700)
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>
tools/testing/selftests/kvm/include/x86/processor.h
tools/testing/selftests/kvm/lib/kvm_util.c
tools/testing/selftests/kvm/lib/x86/processor.c
tools/testing/selftests/kvm/x86/vmx_exception_with_invalid_guest_state.c

index b11b5a53ebd59742d37018d4d222629ceaaceb08..2efb05c2f2fb20da21a05cbaf7731f130698859b 100644 (file)
@@ -1150,7 +1150,6 @@ do {                                                                                      \
 
 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits);
 void kvm_init_vm_address_properties(struct kvm_vm *vm);
-bool vm_is_unrestricted_guest(struct kvm_vm *vm);
 
 struct ex_regs {
        uint64_t rax, rcx, rdx, rbx;
@@ -1325,6 +1324,11 @@ static inline bool kvm_is_forced_emulation_enabled(void)
        return !!get_kvm_param_integer("force_emulation_prefix");
 }
 
+static inline bool kvm_is_unrestricted_guest_enabled(void)
+{
+       return get_kvm_intel_param_bool("unrestricted_guest");
+}
+
 uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
                                    int *level);
 uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr);
index a055343a7bf7586a2f43d9e5072b1a032d3093fc..7573771a6146267258747722674a3c4533041f13 100644 (file)
@@ -64,6 +64,9 @@ static ssize_t get_module_param(const char *module_name, const char *param,
        ssize_t bytes_read;
        int fd, r;
 
+       /* Verify KVM is loaded, to provide a more helpful SKIP message. */
+       close(open_kvm_dev_path_or_exit());
+
        r = snprintf(path, path_size, "/sys/module/%s/parameters/%s",
                     module_name, param);
        TEST_ASSERT(r < path_size,
index a92dc1dad085158f0f18ff1c149d68058124ee62..d4c19ac885a95a92da2816d62ee4dcf26a983308 100644 (file)
@@ -1264,16 +1264,6 @@ done:
        return min(max_gfn, ht_gfn - 1);
 }
 
-/* Returns true if kvm_intel was loaded with unrestricted_guest=1. */
-bool vm_is_unrestricted_guest(struct kvm_vm *vm)
-{
-       /* Ensure that a KVM vendor-specific module is loaded. */
-       if (vm == NULL)
-               close(open_kvm_dev_path_or_exit());
-
-       return get_kvm_intel_param_bool("unrestricted_guest");
-}
-
 void kvm_selftest_arch_init(void)
 {
        host_cpu_is_intel = this_cpu_is_intel();
index 3fd6eceab46f5d320e0f1a12206889e8a9dbf0a1..2cae86d9d5e2ee677e64057f7d3d91bf58351c4a 100644 (file)
@@ -110,7 +110,7 @@ int main(int argc, char *argv[])
        struct kvm_vm *vm;
 
        TEST_REQUIRE(host_cpu_is_intel);
-       TEST_REQUIRE(!vm_is_unrestricted_guest(NULL));
+       TEST_REQUIRE(!kvm_is_unrestricted_guest_enabled());
 
        vm = vm_create_with_one_vcpu(&vcpu, guest_code);
        get_set_sigalrm_vcpu(vcpu);