Commit fcab107a authored by Sean Christopherson's avatar Sean Christopherson
Browse files

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: default avatarSean Christopherson <seanjc@google.com>
parent 30142a93
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -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);
+3 −0
Original line number Diff line number Diff line
@@ -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,
+0 −10
Original line number Diff line number Diff line
@@ -1264,16 +1264,6 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
	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();
+1 −1
Original line number Diff line number Diff line
@@ -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);