* [PATCH 1/4] KVM: selftests: Verify KVM is loaded when getting a KVM module param
2025-05-16 21:59 [PATCH 0/4] KVM: selftests: Improve error handling when opening files Sean Christopherson
@ 2025-05-16 21:59 ` Sean Christopherson
2025-05-16 21:59 ` [PATCH 2/4] KVM: selftests: Add __open_path_or_exit() variant to provide extra help info Sean Christopherson
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-05-16 21:59 UTC (permalink / raw)
To: Paolo Bonzini, Sean Christopherson; +Cc: kvm, linux-kernel, James Houghton
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.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
tools/testing/selftests/kvm/include/x86/processor.h | 6 +++++-
tools/testing/selftests/kvm/lib/kvm_util.c | 3 +++
tools/testing/selftests/kvm/lib/x86/processor.c | 10 ----------
.../kvm/x86/vmx_exception_with_invalid_guest_state.c | 2 +-
4 files changed, 9 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index b11b5a53ebd5..2efb05c2f2fb 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -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);
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 50edc59cc0ca..b21ba80e3015 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -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,
diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index a92dc1dad085..d4c19ac885a9 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -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();
diff --git a/tools/testing/selftests/kvm/x86/vmx_exception_with_invalid_guest_state.c b/tools/testing/selftests/kvm/x86/vmx_exception_with_invalid_guest_state.c
index 3fd6eceab46f..2cae86d9d5e2 100644
--- a/tools/testing/selftests/kvm/x86/vmx_exception_with_invalid_guest_state.c
+++ b/tools/testing/selftests/kvm/x86/vmx_exception_with_invalid_guest_state.c
@@ -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);
--
2.49.0.1112.g889b7c5bd8-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 2/4] KVM: selftests: Add __open_path_or_exit() variant to provide extra help info
2025-05-16 21:59 [PATCH 0/4] KVM: selftests: Improve error handling when opening files Sean Christopherson
2025-05-16 21:59 ` [PATCH 1/4] KVM: selftests: Verify KVM is loaded when getting a KVM module param Sean Christopherson
@ 2025-05-16 21:59 ` Sean Christopherson
2025-05-16 21:59 ` [PATCH 3/4] KVM: selftests: Play nice with EACCES errors in open_path_or_exit() Sean Christopherson
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-05-16 21:59 UTC (permalink / raw)
To: Paolo Bonzini, Sean Christopherson; +Cc: kvm, linux-kernel, James Houghton
Add an inner __open_path_or_exit() API to let the caller provide additional
information on ENOENT to try and help the user figure out why the test is
being skipped, e.g. for files like the page_idle bitmap needed by the
access tracking perf, which is dependent on a Kconfig.
Immediately convert /dev/kvm to the new API, both as an example and because
users might not know that some architectures/setups require loading KVM.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
.../testing/selftests/kvm/include/kvm_util.h | 1 +
tools/testing/selftests/kvm/lib/kvm_util.c | 19 +++++++++++++++----
2 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index a1fc52bbdf7a..ee29e5d0c5ad 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -247,6 +247,7 @@ struct vm_guest_mode_params {
};
extern const struct vm_guest_mode_params vm_guest_mode_params[];
+int __open_path_or_exit(const char *path, int flags, const char *enoent_help);
int open_path_or_exit(const char *path, int flags);
int open_kvm_dev_path_or_exit(void);
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index b21ba80e3015..b133f2b1dbf7 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -26,15 +26,26 @@ static uint32_t last_guest_seed;
static int vcpu_mmap_sz(void);
-int open_path_or_exit(const char *path, int flags)
+int __open_path_or_exit(const char *path, int flags, const char *enoent_help)
{
int fd;
fd = open(path, flags);
- __TEST_REQUIRE(fd >= 0 || errno != ENOENT, "Cannot open %s: %s", path, strerror(errno));
- TEST_ASSERT(fd >= 0, "Failed to open '%s'", path);
+ if (fd < 0)
+ goto error;
return fd;
+
+error:
+ if (errno == ENOENT)
+ ksft_exit_skip("- Cannot open '%s': %s. %s\n",
+ path, strerror(errno), enoent_help);
+ TEST_FAIL("Failed to open '%s'", path);
+}
+
+int open_path_or_exit(const char *path, int flags)
+{
+ return __open_path_or_exit(path, flags, "");
}
/*
@@ -48,7 +59,7 @@ int open_path_or_exit(const char *path, int flags)
*/
static int _open_kvm_dev_path_or_exit(int flags)
{
- return open_path_or_exit(KVM_DEV_PATH, flags);
+ return __open_path_or_exit(KVM_DEV_PATH, flags, "Is KVM loaded and enabled?");
}
int open_kvm_dev_path_or_exit(void)
--
2.49.0.1112.g889b7c5bd8-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 3/4] KVM: selftests: Play nice with EACCES errors in open_path_or_exit()
2025-05-16 21:59 [PATCH 0/4] KVM: selftests: Improve error handling when opening files Sean Christopherson
2025-05-16 21:59 ` [PATCH 1/4] KVM: selftests: Verify KVM is loaded when getting a KVM module param Sean Christopherson
2025-05-16 21:59 ` [PATCH 2/4] KVM: selftests: Add __open_path_or_exit() variant to provide extra help info Sean Christopherson
@ 2025-05-16 21:59 ` Sean Christopherson
2025-05-16 21:59 ` [PATCH 4/4] KVM: selftests: Print a more helpful message for EACCESS in access tracking test Sean Christopherson
2025-06-24 19:38 ` [PATCH 0/4] KVM: selftests: Improve error handling when opening files Sean Christopherson
4 siblings, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-05-16 21:59 UTC (permalink / raw)
To: Paolo Bonzini, Sean Christopherson; +Cc: kvm, linux-kernel, James Houghton
Expand the SKIP conditions of the open_path_or_exit() helper to skip on
EACCES as well as ENOENT. Most often, lack of permissions to a file
needed by a KVM selftests is due to a file being root-only by default,
not because of any bug/misconfiguration that warrants failing a test.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index b133f2b1dbf7..cd415f70de98 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -37,9 +37,10 @@ int __open_path_or_exit(const char *path, int flags, const char *enoent_help)
return fd;
error:
- if (errno == ENOENT)
+ if (errno == EACCES || errno == ENOENT)
ksft_exit_skip("- Cannot open '%s': %s. %s\n",
- path, strerror(errno), enoent_help);
+ path, strerror(errno),
+ errno == EACCES ? "Root required?" : enoent_help);
TEST_FAIL("Failed to open '%s'", path);
}
--
2.49.0.1112.g889b7c5bd8-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 4/4] KVM: selftests: Print a more helpful message for EACCESS in access tracking test
2025-05-16 21:59 [PATCH 0/4] KVM: selftests: Improve error handling when opening files Sean Christopherson
` (2 preceding siblings ...)
2025-05-16 21:59 ` [PATCH 3/4] KVM: selftests: Play nice with EACCES errors in open_path_or_exit() Sean Christopherson
@ 2025-05-16 21:59 ` Sean Christopherson
2025-06-24 19:38 ` [PATCH 0/4] KVM: selftests: Improve error handling when opening files Sean Christopherson
4 siblings, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-05-16 21:59 UTC (permalink / raw)
To: Paolo Bonzini, Sean Christopherson; +Cc: kvm, linux-kernel, James Houghton
Use open_path_or_exit() helper to probe /sys/kernel/mm/page_idle/bitmap in
the access tracking perf test so that a helpful/pertinent SKIP message is
printed if the file exists but is inaccessible, e.g. because the file has
the kernel's default 0600 permissions.
Cc: James Houghton <jthoughton@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
tools/testing/selftests/kvm/access_tracking_perf_test.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/kvm/access_tracking_perf_test.c b/tools/testing/selftests/kvm/access_tracking_perf_test.c
index da7196fd1b23..c9de66537ec3 100644
--- a/tools/testing/selftests/kvm/access_tracking_perf_test.c
+++ b/tools/testing/selftests/kvm/access_tracking_perf_test.c
@@ -596,11 +596,8 @@ int main(int argc, char *argv[])
if (ret)
return ret;
} else {
- page_idle_fd = open("/sys/kernel/mm/page_idle/bitmap", O_RDWR);
- __TEST_REQUIRE(page_idle_fd >= 0,
- "Couldn't open /sys/kernel/mm/page_idle/bitmap. "
- "Is CONFIG_IDLE_PAGE_TRACKING enabled?");
-
+ page_idle_fd = __open_path_or_exit("/sys/kernel/mm/page_idle/bitmap", O_RDWR,
+ "Is CONFIG_IDLE_PAGE_TRACKING enabled?");
close(page_idle_fd);
puts("Using page_idle for aging");
--
2.49.0.1112.g889b7c5bd8-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH 0/4] KVM: selftests: Improve error handling when opening files
2025-05-16 21:59 [PATCH 0/4] KVM: selftests: Improve error handling when opening files Sean Christopherson
` (3 preceding siblings ...)
2025-05-16 21:59 ` [PATCH 4/4] KVM: selftests: Print a more helpful message for EACCESS in access tracking test Sean Christopherson
@ 2025-06-24 19:38 ` Sean Christopherson
4 siblings, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-06-24 19:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, James Houghton
On Fri, 16 May 2025 14:59:05 -0700, Sean Christopherson wrote:
> Improve selftests' error reporting when opening a file fails, e.g. so that
> failure to access a module param spits out a message about KVM not being
> loaded, instead of a cryptic message about a param not being supported.
>
> Sean Christopherson (4):
> KVM: selftests: Verify KVM is loaded when getting a KVM module param
> KVM: selftests: Add __open_path_or_exit() variant to provide extra
> help info
> KVM: selftests: Play nice with EACCES errors in open_path_or_exit()
> KVM: selftests: Print a more helpful message for EACCESS in access
> tracking test
>
> [...]
Applied to kvm-x86 selftests, thanks!
[1/4] KVM: selftests: Verify KVM is loaded when getting a KVM module param
https://github.com/kvm-x86/linux/commit/fcab107abe1a
[2/4] KVM: selftests: Add __open_path_or_exit() variant to provide extra help info
https://github.com/kvm-x86/linux/commit/6e1cce7cda1b
[3/4] KVM: selftests: Play nice with EACCES errors in open_path_or_exit()
https://github.com/kvm-x86/linux/commit/ba300a728f6f
[4/4] KVM: selftests: Print a more helpful message for EACCESS in access tracking test
https://github.com/kvm-x86/linux/commit/71443210e26d
--
https://github.com/kvm-x86/kvm-unit-tests/tree/next
^ permalink raw reply [flat|nested] 6+ messages in thread