Skip to content

Commit bcb2b94

Browse files
committed
KVM: selftests: exit with 0 status code when tests cannot be run
Right now, skipped tests are returning a failure exit code if /dev/kvm does not exists. Consistently return a zero status code so that various scripts over the interwebs do not complain. Also return a zero status code if the KVM_CAP_SYNC_REGS capability is not present, and hardcode in the test the register kinds that are covered (rather than just using whatever value of KVM_SYNC_X86_VALID_FIELDS is provided by the kernel headers). Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 452a68d commit bcb2b94

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
lines changed

tools/testing/selftests/kvm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LIBKVM += $(LIBKVM_$(UNAME_M))
1515

1616
INSTALL_HDR_PATH = $(top_srcdir)/usr
1717
LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
18-
CFLAGS += -O2 -g -std=gnu99 -I$(LINUX_HDR_PATH) -Iinclude -I$(<D)
18+
CFLAGS += -O2 -g -std=gnu99 -I$(LINUX_HDR_PATH) -Iinclude -I$(<D) -I..
1919

2020
# After inclusion, $(OUTPUT) is defined and
2121
# $(TEST_GEN_PROGS) starts with $(OUTPUT)/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <errno.h>
2020
#include <unistd.h>
2121
#include <fcntl.h>
22+
#include "kselftest.h"
2223

2324
ssize_t test_write(int fd, const void *buf, size_t count);
2425
ssize_t test_read(int fd, void *buf, size_t count);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ int kvm_check_cap(long cap)
5050
int kvm_fd;
5151

5252
kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
53-
TEST_ASSERT(kvm_fd >= 0, "open %s failed, rc: %i errno: %i",
54-
KVM_DEV_PATH, kvm_fd, errno);
53+
if (kvm_fd < 0)
54+
exit(KSFT_SKIP);
5555

5656
ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap);
5757
TEST_ASSERT(ret != -1, "KVM_CHECK_EXTENSION IOCTL failed,\n"
@@ -91,8 +91,8 @@ struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
9191

9292
vm->mode = mode;
9393
kvm_fd = open(KVM_DEV_PATH, perm);
94-
TEST_ASSERT(kvm_fd >= 0, "open %s failed, rc: %i errno: %i",
95-
KVM_DEV_PATH, kvm_fd, errno);
94+
if (kvm_fd < 0)
95+
exit(KSFT_SKIP);
9696

9797
/* Create VM. */
9898
vm->fd = ioctl(kvm_fd, KVM_CREATE_VM, NULL);
@@ -418,8 +418,8 @@ struct kvm_cpuid2 *kvm_get_supported_cpuid(void)
418418

419419
cpuid = allocate_kvm_cpuid2();
420420
kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
421-
TEST_ASSERT(kvm_fd >= 0, "open %s failed, rc: %i errno: %i",
422-
KVM_DEV_PATH, kvm_fd, errno);
421+
if (kvm_fd < 0)
422+
exit(KSFT_SKIP);
423423

424424
ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid);
425425
TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_CPUID failed %d %d\n",
@@ -675,8 +675,8 @@ static int vcpu_mmap_sz(void)
675675
int dev_fd, ret;
676676

677677
dev_fd = open(KVM_DEV_PATH, O_RDONLY);
678-
TEST_ASSERT(dev_fd >= 0, "%s open %s failed, rc: %i errno: %i",
679-
__func__, KVM_DEV_PATH, dev_fd, errno);
678+
if (dev_fd < 0)
679+
exit(KSFT_SKIP);
680680

681681
ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
682682
TEST_ASSERT(ret >= sizeof(struct kvm_run),

tools/testing/selftests/kvm/sync_regs_test.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ static void compare_vcpu_events(struct kvm_vcpu_events *left,
8585
{
8686
}
8787

88+
#define TEST_SYNC_FIELDS (KVM_SYNC_X86_REGS|KVM_SYNC_X86_SREGS|KVM_SYNC_X86_EVENTS)
89+
#define INVALID_SYNC_FIELD 0x80000000
90+
8891
int main(int argc, char *argv[])
8992
{
9093
struct kvm_vm *vm;
@@ -98,25 +101,44 @@ int main(int argc, char *argv[])
98101
setbuf(stdout, NULL);
99102

100103
cap = kvm_check_cap(KVM_CAP_SYNC_REGS);
101-
TEST_ASSERT((unsigned long)cap == KVM_SYNC_X86_VALID_FIELDS,
102-
"KVM_CAP_SYNC_REGS (0x%x) != KVM_SYNC_X86_VALID_FIELDS (0x%lx)\n",
103-
cap, KVM_SYNC_X86_VALID_FIELDS);
104+
if ((cap & TEST_SYNC_FIELDS) != TEST_SYNC_FIELDS) {
105+
fprintf(stderr, "KVM_CAP_SYNC_REGS not supported, skipping test\n");
106+
exit(KSFT_SKIP);
107+
}
108+
if ((cap & INVALID_SYNC_FIELD) != 0) {
109+
fprintf(stderr, "The \"invalid\" field is not invalid, skipping test\n");
110+
exit(KSFT_SKIP);
111+
}
104112

105113
/* Create VM */
106114
vm = vm_create_default(VCPU_ID, guest_code);
107115

108116
run = vcpu_state(vm, VCPU_ID);
109117

110118
/* Request reading invalid register set from VCPU. */
111-
run->kvm_valid_regs = KVM_SYNC_X86_VALID_FIELDS << 1;
119+
run->kvm_valid_regs = INVALID_SYNC_FIELD;
120+
rv = _vcpu_run(vm, VCPU_ID);
121+
TEST_ASSERT(rv < 0 && errno == EINVAL,
122+
"Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
123+
rv);
124+
vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
125+
126+
run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
112127
rv = _vcpu_run(vm, VCPU_ID);
113128
TEST_ASSERT(rv < 0 && errno == EINVAL,
114129
"Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
115130
rv);
116131
vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
117132

118133
/* Request setting invalid register set into VCPU. */
119-
run->kvm_dirty_regs = KVM_SYNC_X86_VALID_FIELDS << 1;
134+
run->kvm_dirty_regs = INVALID_SYNC_FIELD;
135+
rv = _vcpu_run(vm, VCPU_ID);
136+
TEST_ASSERT(rv < 0 && errno == EINVAL,
137+
"Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
138+
rv);
139+
vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0;
140+
141+
run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
120142
rv = _vcpu_run(vm, VCPU_ID);
121143
TEST_ASSERT(rv < 0 && errno == EINVAL,
122144
"Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
@@ -125,7 +147,7 @@ int main(int argc, char *argv[])
125147

126148
/* Request and verify all valid register sets. */
127149
/* TODO: BUILD TIME CHECK: TEST_ASSERT(KVM_SYNC_X86_NUM_FIELDS != 3); */
128-
run->kvm_valid_regs = KVM_SYNC_X86_VALID_FIELDS;
150+
run->kvm_valid_regs = TEST_SYNC_FIELDS;
129151
rv = _vcpu_run(vm, VCPU_ID);
130152
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
131153
"Unexpected exit reason: %u (%s),\n",
@@ -146,7 +168,7 @@ int main(int argc, char *argv[])
146168
run->s.regs.sregs.apic_base = 1 << 11;
147169
/* TODO run->s.regs.events.XYZ = ABC; */
148170

149-
run->kvm_valid_regs = KVM_SYNC_X86_VALID_FIELDS;
171+
run->kvm_valid_regs = TEST_SYNC_FIELDS;
150172
run->kvm_dirty_regs = KVM_SYNC_X86_REGS | KVM_SYNC_X86_SREGS;
151173
rv = _vcpu_run(vm, VCPU_ID);
152174
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
@@ -172,7 +194,7 @@ int main(int argc, char *argv[])
172194
/* Clear kvm_dirty_regs bits, verify new s.regs values are
173195
* overwritten with existing guest values.
174196
*/
175-
run->kvm_valid_regs = KVM_SYNC_X86_VALID_FIELDS;
197+
run->kvm_valid_regs = TEST_SYNC_FIELDS;
176198
run->kvm_dirty_regs = 0;
177199
run->s.regs.regs.r11 = 0xDEADBEEF;
178200
rv = _vcpu_run(vm, VCPU_ID);
@@ -211,7 +233,7 @@ int main(int argc, char *argv[])
211233
* with kvm_sync_regs values.
212234
*/
213235
run->kvm_valid_regs = 0;
214-
run->kvm_dirty_regs = KVM_SYNC_X86_VALID_FIELDS;
236+
run->kvm_dirty_regs = TEST_SYNC_FIELDS;
215237
run->s.regs.regs.r11 = 0xBBBB;
216238
rv = _vcpu_run(vm, VCPU_ID);
217239
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,

tools/testing/selftests/kvm/vmx_tsc_adjust_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ int main(int argc, char *argv[])
189189
struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
190190

191191
if (!(entry->ecx & CPUID_VMX)) {
192-
printf("nested VMX not enabled, skipping test");
193-
return 0;
192+
fprintf(stderr, "nested VMX not enabled, skipping test\n");
193+
exit(KSFT_SKIP);
194194
}
195195

196196
vm = vm_create_default_vmx(VCPU_ID, (void *) l1_guest_code);

0 commit comments

Comments
 (0)