Skip to content

Commit b4733cd

Browse files
committed
Merge tag 'kvm-x86-selftests-6.17' of https://github.com/kvm-x86/linux into HEAD
KVM selftests changes for 6.17 - Fix a comment typo. - Verify KVM is loaded when getting any KVM module param so that attempting to run a selftest without kvm.ko loaded results in a SKIP message about KVM not being loaded/enabled, versus some random parameter not existing. - SKIP tests that hit EACCES when attempting to access a file, with a "Root required?" help message. In most cases, the test just needs to be run with elevated permissions.
2 parents 89400f0 + 7144321 commit b4733cd

File tree

7 files changed

+30
-23
lines changed

7 files changed

+30
-23
lines changed

tools/testing/selftests/kvm/access_tracking_perf_test.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,8 @@ int main(int argc, char *argv[])
596596
if (ret)
597597
return ret;
598598
} else {
599-
page_idle_fd = open("/sys/kernel/mm/page_idle/bitmap", O_RDWR);
600-
__TEST_REQUIRE(page_idle_fd >= 0,
601-
"Couldn't open /sys/kernel/mm/page_idle/bitmap. "
602-
"Is CONFIG_IDLE_PAGE_TRACKING enabled?");
603-
599+
page_idle_fd = __open_path_or_exit("/sys/kernel/mm/page_idle/bitmap", O_RDWR,
600+
"Is CONFIG_IDLE_PAGE_TRACKING enabled?");
604601
close(page_idle_fd);
605602

606603
puts("Using page_idle for aging");

tools/testing/selftests/kvm/include/kvm_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ struct vm_guest_mode_params {
256256
};
257257
extern const struct vm_guest_mode_params vm_guest_mode_params[];
258258

259+
int __open_path_or_exit(const char *path, int flags, const char *enoent_help);
259260
int open_path_or_exit(const char *path, int flags);
260261
int open_kvm_dev_path_or_exit(void);
261262

tools/testing/selftests/kvm/include/x86/processor.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,6 @@ do { \
11501150

11511151
void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits);
11521152
void kvm_init_vm_address_properties(struct kvm_vm *vm);
1153-
bool vm_is_unrestricted_guest(struct kvm_vm *vm);
11541153

11551154
struct ex_regs {
11561155
uint64_t rax, rcx, rdx, rbx;
@@ -1325,6 +1324,11 @@ static inline bool kvm_is_forced_emulation_enabled(void)
13251324
return !!get_kvm_param_integer("force_emulation_prefix");
13261325
}
13271326

1327+
static inline bool kvm_is_unrestricted_guest_enabled(void)
1328+
{
1329+
return get_kvm_intel_param_bool("unrestricted_guest");
1330+
}
1331+
13281332
uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
13291333
int *level);
13301334
uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr);

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,27 @@ static uint32_t last_guest_seed;
2626

2727
static int vcpu_mmap_sz(void);
2828

29-
int open_path_or_exit(const char *path, int flags)
29+
int __open_path_or_exit(const char *path, int flags, const char *enoent_help)
3030
{
3131
int fd;
3232

3333
fd = open(path, flags);
34-
__TEST_REQUIRE(fd >= 0 || errno != ENOENT, "Cannot open %s: %s", path, strerror(errno));
35-
TEST_ASSERT(fd >= 0, "Failed to open '%s'", path);
34+
if (fd < 0)
35+
goto error;
3636

3737
return fd;
38+
39+
error:
40+
if (errno == EACCES || errno == ENOENT)
41+
ksft_exit_skip("- Cannot open '%s': %s. %s\n",
42+
path, strerror(errno),
43+
errno == EACCES ? "Root required?" : enoent_help);
44+
TEST_FAIL("Failed to open '%s'", path);
45+
}
46+
47+
int open_path_or_exit(const char *path, int flags)
48+
{
49+
return __open_path_or_exit(path, flags, "");
3850
}
3951

4052
/*
@@ -48,7 +60,7 @@ int open_path_or_exit(const char *path, int flags)
4860
*/
4961
static int _open_kvm_dev_path_or_exit(int flags)
5062
{
51-
return open_path_or_exit(KVM_DEV_PATH, flags);
63+
return __open_path_or_exit(KVM_DEV_PATH, flags, "Is KVM loaded and enabled?");
5264
}
5365

5466
int open_kvm_dev_path_or_exit(void)
@@ -64,6 +76,9 @@ static ssize_t get_module_param(const char *module_name, const char *param,
6476
ssize_t bytes_read;
6577
int fd, r;
6678

79+
/* Verify KVM is loaded, to provide a more helpful SKIP message. */
80+
close(open_kvm_dev_path_or_exit());
81+
6782
r = snprintf(path, path_size, "/sys/module/%s/parameters/%s",
6883
module_name, param);
6984
TEST_ASSERT(r < path_size,

tools/testing/selftests/kvm/lib/sparsebit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
*
117117
* + A node with all mask bits set only occurs when the last bit
118118
* described by the previous node is not equal to this nodes
119-
* starting index - 1. All such occurences of this condition are
119+
* starting index - 1. All such occurrences of this condition are
120120
* avoided by moving the setting of the nodes mask bits into
121121
* the previous nodes num_after setting.
122122
*
@@ -592,7 +592,7 @@ static struct node *node_split(struct sparsebit *s, sparsebit_idx_t idx)
592592
*
593593
* + A node with all mask bits set only occurs when the last bit
594594
* described by the previous node is not equal to this nodes
595-
* starting index - 1. All such occurences of this condition are
595+
* starting index - 1. All such occurrences of this condition are
596596
* avoided by moving the setting of the nodes mask bits into
597597
* the previous nodes num_after setting.
598598
*/

tools/testing/selftests/kvm/lib/x86/processor.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,16 +1264,6 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
12641264
return min(max_gfn, ht_gfn - 1);
12651265
}
12661266

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-
12771267
void kvm_selftest_arch_init(void)
12781268
{
12791269
host_cpu_is_intel = this_cpu_is_intel();

tools/testing/selftests/kvm/x86/vmx_exception_with_invalid_guest_state.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int main(int argc, char *argv[])
110110
struct kvm_vm *vm;
111111

112112
TEST_REQUIRE(host_cpu_is_intel);
113-
TEST_REQUIRE(!vm_is_unrestricted_guest(NULL));
113+
TEST_REQUIRE(!kvm_is_unrestricted_guest_enabled());
114114

115115
vm = vm_create_with_one_vcpu(&vcpu, guest_code);
116116
get_set_sigalrm_vcpu(vcpu);

0 commit comments

Comments
 (0)