Skip to content

Commit 1814db8

Browse files
committed
Merge tag 'kvm-x86-selftests-6.6' of https://github.com/kvm-x86/linux into HEAD
KVM: x86: Selftests changes for 6.6: - Add testcases to x86's sync_regs_test for detecting KVM TOCTOU bugs - Add support for printf() in guest code and covert all guest asserts to use printf-based reporting - Clean up the PMU event filter test and add new testcases - Include x86 selftests in the KVM x86 MAINTAINERS entry
2 parents 0d15bf9 + c92b922 commit 1814db8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1389
-468
lines changed

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11500,6 +11500,8 @@ F: arch/x86/include/uapi/asm/svm.h
1150011500
F: arch/x86/include/uapi/asm/vmx.h
1150111501
F: arch/x86/kvm/
1150211502
F: arch/x86/kvm/*/
11503+
F: tools/testing/selftests/kvm/*/x86_64/
11504+
F: tools/testing/selftests/kvm/x86_64/
1150311505

1150411506
KERNFS
1150511507
M: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

arch/x86/kvm/x86.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11798,15 +11798,22 @@ static int sync_regs(struct kvm_vcpu *vcpu)
1179811798
__set_regs(vcpu, &vcpu->run->s.regs.regs);
1179911799
vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
1180011800
}
11801+
1180111802
if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
11802-
if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
11803+
struct kvm_sregs sregs = vcpu->run->s.regs.sregs;
11804+
11805+
if (__set_sregs(vcpu, &sregs))
1180311806
return -EINVAL;
11807+
1180411808
vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
1180511809
}
11810+
1180611811
if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
11807-
if (kvm_vcpu_ioctl_x86_set_vcpu_events(
11808-
vcpu, &vcpu->run->s.regs.events))
11812+
struct kvm_vcpu_events events = vcpu->run->s.regs.events;
11813+
11814+
if (kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events))
1180911815
return -EINVAL;
11816+
1181011817
vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
1181111818
}
1181211819

tools/testing/selftests/kvm/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ LIBKVM += lib/guest_modes.c
2323
LIBKVM += lib/io.c
2424
LIBKVM += lib/kvm_util.c
2525
LIBKVM += lib/memstress.c
26+
LIBKVM += lib/guest_sprintf.c
2627
LIBKVM += lib/rbtree.c
2728
LIBKVM += lib/sparsebit.c
2829
LIBKVM += lib/test_util.c
@@ -122,6 +123,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test
122123
TEST_GEN_PROGS_x86_64 += demand_paging_test
123124
TEST_GEN_PROGS_x86_64 += dirty_log_test
124125
TEST_GEN_PROGS_x86_64 += dirty_log_perf_test
126+
TEST_GEN_PROGS_x86_64 += guest_print_test
125127
TEST_GEN_PROGS_x86_64 += hardware_disable_test
126128
TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
127129
TEST_GEN_PROGS_x86_64 += kvm_page_table_test
@@ -152,6 +154,7 @@ TEST_GEN_PROGS_aarch64 += access_tracking_perf_test
152154
TEST_GEN_PROGS_aarch64 += demand_paging_test
153155
TEST_GEN_PROGS_aarch64 += dirty_log_test
154156
TEST_GEN_PROGS_aarch64 += dirty_log_perf_test
157+
TEST_GEN_PROGS_aarch64 += guest_print_test
155158
TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
156159
TEST_GEN_PROGS_aarch64 += kvm_page_table_test
157160
TEST_GEN_PROGS_aarch64 += memslot_modification_stress_test
@@ -168,6 +171,7 @@ TEST_GEN_PROGS_s390x += s390x/tprot
168171
TEST_GEN_PROGS_s390x += s390x/cmma_test
169172
TEST_GEN_PROGS_s390x += demand_paging_test
170173
TEST_GEN_PROGS_s390x += dirty_log_test
174+
TEST_GEN_PROGS_s390x += guest_print_test
171175
TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
172176
TEST_GEN_PROGS_s390x += kvm_page_table_test
173177
TEST_GEN_PROGS_s390x += rseq_test
@@ -176,6 +180,7 @@ TEST_GEN_PROGS_s390x += kvm_binary_stats_test
176180

177181
TEST_GEN_PROGS_riscv += demand_paging_test
178182
TEST_GEN_PROGS_riscv += dirty_log_test
183+
TEST_GEN_PROGS_riscv += guest_print_test
179184
TEST_GEN_PROGS_riscv += kvm_create_max_vcpus
180185
TEST_GEN_PROGS_riscv += kvm_page_table_test
181186
TEST_GEN_PROGS_riscv += set_memory_region_test
@@ -204,6 +209,7 @@ endif
204209
CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
205210
-Wno-gnu-variable-sized-type-not-at-end -MD\
206211
-fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \
212+
-fno-builtin-strnlen \
207213
-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
208214
-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
209215
-I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \

tools/testing/selftests/kvm/aarch64/aarch32_id_regs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static void test_user_raz_wi(struct kvm_vcpu *vcpu)
9898
uint64_t val;
9999

100100
vcpu_get_reg(vcpu, reg_id, &val);
101-
ASSERT_EQ(val, 0);
101+
TEST_ASSERT_EQ(val, 0);
102102

103103
/*
104104
* Expect the ioctl to succeed with no effect on the register
@@ -107,7 +107,7 @@ static void test_user_raz_wi(struct kvm_vcpu *vcpu)
107107
vcpu_set_reg(vcpu, reg_id, BAD_ID_REG_VAL);
108108

109109
vcpu_get_reg(vcpu, reg_id, &val);
110-
ASSERT_EQ(val, 0);
110+
TEST_ASSERT_EQ(val, 0);
111111
}
112112
}
113113

@@ -127,14 +127,14 @@ static void test_user_raz_invariant(struct kvm_vcpu *vcpu)
127127
uint64_t val;
128128

129129
vcpu_get_reg(vcpu, reg_id, &val);
130-
ASSERT_EQ(val, 0);
130+
TEST_ASSERT_EQ(val, 0);
131131

132132
r = __vcpu_set_reg(vcpu, reg_id, BAD_ID_REG_VAL);
133133
TEST_ASSERT(r < 0 && errno == EINVAL,
134134
"unexpected KVM_SET_ONE_REG error: r=%d, errno=%d", r, errno);
135135

136136
vcpu_get_reg(vcpu, reg_id, &val);
137-
ASSERT_EQ(val, 0);
137+
TEST_ASSERT_EQ(val, 0);
138138
}
139139
}
140140

tools/testing/selftests/kvm/aarch64/arch_timer.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*
2020
* Copyright (c) 2021, Google LLC.
2121
*/
22-
2322
#define _GNU_SOURCE
2423

2524
#include <stdlib.h>
@@ -155,11 +154,13 @@ static void guest_validate_irq(unsigned int intid,
155154
xcnt_diff_us = cycles_to_usec(xcnt - shared_data->xcnt);
156155

157156
/* Make sure we are dealing with the correct timer IRQ */
158-
GUEST_ASSERT_2(intid == timer_irq, intid, timer_irq);
157+
GUEST_ASSERT_EQ(intid, timer_irq);
159158

160159
/* Basic 'timer condition met' check */
161-
GUEST_ASSERT_3(xcnt >= cval, xcnt, cval, xcnt_diff_us);
162-
GUEST_ASSERT_1(xctl & CTL_ISTATUS, xctl);
160+
__GUEST_ASSERT(xcnt >= cval,
161+
"xcnt = 0x%llx, cval = 0x%llx, xcnt_diff_us = 0x%llx",
162+
xcnt, cval, xcnt_diff_us);
163+
__GUEST_ASSERT(xctl & CTL_ISTATUS, "xcnt = 0x%llx", xcnt);
163164

164165
WRITE_ONCE(shared_data->nr_iter, shared_data->nr_iter + 1);
165166
}
@@ -192,8 +193,7 @@ static void guest_run_stage(struct test_vcpu_shared_data *shared_data,
192193
TIMER_TEST_ERR_MARGIN_US);
193194

194195
irq_iter = READ_ONCE(shared_data->nr_iter);
195-
GUEST_ASSERT_2(config_iter + 1 == irq_iter,
196-
config_iter + 1, irq_iter);
196+
GUEST_ASSERT_EQ(config_iter + 1, irq_iter);
197197
}
198198
}
199199

@@ -243,13 +243,9 @@ static void *test_vcpu_run(void *arg)
243243
break;
244244
case UCALL_ABORT:
245245
sync_global_from_guest(vm, *shared_data);
246-
REPORT_GUEST_ASSERT_N(uc, "values: %lu, %lu; %lu, vcpu %u; stage; %u; iter: %u",
247-
GUEST_ASSERT_ARG(uc, 0),
248-
GUEST_ASSERT_ARG(uc, 1),
249-
GUEST_ASSERT_ARG(uc, 2),
250-
vcpu_idx,
251-
shared_data->guest_stage,
252-
shared_data->nr_iter);
246+
fprintf(stderr, "Guest assert failed, vcpu %u; stage; %u; iter: %u\n",
247+
vcpu_idx, shared_data->guest_stage, shared_data->nr_iter);
248+
REPORT_GUEST_ASSERT(uc);
253249
break;
254250
default:
255251
TEST_FAIL("Unexpected guest exit\n");

tools/testing/selftests/kvm/aarch64/debug-exceptions.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ static void guest_wp_handler(struct ex_regs *regs)
365365

366366
static void guest_ss_handler(struct ex_regs *regs)
367367
{
368-
GUEST_ASSERT_1(ss_idx < 4, ss_idx);
368+
__GUEST_ASSERT(ss_idx < 4, "Expected index < 4, got '%u'", ss_idx);
369369
ss_addr[ss_idx++] = regs->pc;
370370
regs->pstate |= SPSR_SS;
371371
}
@@ -410,8 +410,8 @@ static void guest_code_ss(int test_cnt)
410410
/* Userspace disables Single Step when the end is nigh. */
411411
asm volatile("iter_ss_end:\n");
412412

413-
GUEST_ASSERT(bvr == w_bvr);
414-
GUEST_ASSERT(wvr == w_wvr);
413+
GUEST_ASSERT_EQ(bvr, w_bvr);
414+
GUEST_ASSERT_EQ(wvr, w_wvr);
415415
}
416416
GUEST_DONE();
417417
}
@@ -450,7 +450,7 @@ static void test_guest_debug_exceptions(uint8_t bpn, uint8_t wpn, uint8_t ctx_bp
450450
vcpu_run(vcpu);
451451
switch (get_ucall(vcpu, &uc)) {
452452
case UCALL_ABORT:
453-
REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
453+
REPORT_GUEST_ASSERT(uc);
454454
break;
455455
case UCALL_DONE:
456456
goto done;

tools/testing/selftests/kvm/aarch64/hypercalls.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* hypercalls are properly masked or unmasked to the guest when disabled or
99
* enabled from the KVM userspace, respectively.
1010
*/
11-
1211
#include <errno.h>
1312
#include <linux/arm-smccc.h>
1413
#include <asm/kvm.h>
@@ -105,15 +104,17 @@ static void guest_test_hvc(const struct test_hvc_info *hc_info)
105104
switch (stage) {
106105
case TEST_STAGE_HVC_IFACE_FEAT_DISABLED:
107106
case TEST_STAGE_HVC_IFACE_FALSE_INFO:
108-
GUEST_ASSERT_3(res.a0 == SMCCC_RET_NOT_SUPPORTED,
109-
res.a0, hc_info->func_id, hc_info->arg1);
107+
__GUEST_ASSERT(res.a0 == SMCCC_RET_NOT_SUPPORTED,
108+
"a0 = 0x%lx, func_id = 0x%x, arg1 = 0x%llx, stage = %u",
109+
res.a0, hc_info->func_id, hc_info->arg1, stage);
110110
break;
111111
case TEST_STAGE_HVC_IFACE_FEAT_ENABLED:
112-
GUEST_ASSERT_3(res.a0 != SMCCC_RET_NOT_SUPPORTED,
113-
res.a0, hc_info->func_id, hc_info->arg1);
112+
__GUEST_ASSERT(res.a0 != SMCCC_RET_NOT_SUPPORTED,
113+
"a0 = 0x%lx, func_id = 0x%x, arg1 = 0x%llx, stage = %u",
114+
res.a0, hc_info->func_id, hc_info->arg1, stage);
114115
break;
115116
default:
116-
GUEST_ASSERT_1(0, stage);
117+
GUEST_FAIL("Unexpected stage = %u", stage);
117118
}
118119
}
119120
}
@@ -132,7 +133,7 @@ static void guest_code(void)
132133
guest_test_hvc(false_hvc_info);
133134
break;
134135
default:
135-
GUEST_ASSERT_1(0, stage);
136+
GUEST_FAIL("Unexpected stage = %u", stage);
136137
}
137138

138139
GUEST_SYNC(stage);
@@ -290,10 +291,7 @@ static void test_run(void)
290291
guest_done = true;
291292
break;
292293
case UCALL_ABORT:
293-
REPORT_GUEST_ASSERT_N(uc, "values: 0x%lx, 0x%lx; 0x%lx, stage: %u",
294-
GUEST_ASSERT_ARG(uc, 0),
295-
GUEST_ASSERT_ARG(uc, 1),
296-
GUEST_ASSERT_ARG(uc, 2), stage);
294+
REPORT_GUEST_ASSERT(uc);
297295
break;
298296
default:
299297
TEST_FAIL("Unexpected guest exit\n");

tools/testing/selftests/kvm/aarch64/page_fault_test.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* hugetlbfs with a hole). It checks that the expected handling method is
88
* called (e.g., uffd faults with the right address and write/read flag).
99
*/
10-
1110
#define _GNU_SOURCE
1211
#include <linux/bitmap.h>
1312
#include <fcntl.h>
@@ -293,12 +292,12 @@ static void guest_code(struct test_desc *test)
293292

294293
static void no_dabt_handler(struct ex_regs *regs)
295294
{
296-
GUEST_ASSERT_1(false, read_sysreg(far_el1));
295+
GUEST_FAIL("Unexpected dabt, far_el1 = 0x%llx", read_sysreg(far_el1));
297296
}
298297

299298
static void no_iabt_handler(struct ex_regs *regs)
300299
{
301-
GUEST_ASSERT_1(false, regs->pc);
300+
GUEST_FAIL("Unexpected iabt, pc = 0x%lx", regs->pc);
302301
}
303302

304303
static struct uffd_args {
@@ -318,7 +317,7 @@ static int uffd_generic_handler(int uffd_mode, int uffd, struct uffd_msg *msg,
318317

319318
TEST_ASSERT(uffd_mode == UFFDIO_REGISTER_MODE_MISSING,
320319
"The only expected UFFD mode is MISSING");
321-
ASSERT_EQ(addr, (uint64_t)args->hva);
320+
TEST_ASSERT_EQ(addr, (uint64_t)args->hva);
322321

323322
pr_debug("uffd fault: addr=%p write=%d\n",
324323
(void *)addr, !!(flags & UFFD_PAGEFAULT_FLAG_WRITE));
@@ -432,7 +431,7 @@ static void mmio_on_test_gpa_handler(struct kvm_vm *vm, struct kvm_run *run)
432431
region = vm_get_mem_region(vm, MEM_REGION_TEST_DATA);
433432
hva = (void *)region->region.userspace_addr;
434433

435-
ASSERT_EQ(run->mmio.phys_addr, region->region.guest_phys_addr);
434+
TEST_ASSERT_EQ(run->mmio.phys_addr, region->region.guest_phys_addr);
436435

437436
memcpy(hva, run->mmio.data, run->mmio.len);
438437
events.mmio_exits += 1;
@@ -631,9 +630,9 @@ static void setup_default_handlers(struct test_desc *test)
631630

632631
static void check_event_counts(struct test_desc *test)
633632
{
634-
ASSERT_EQ(test->expected_events.uffd_faults, events.uffd_faults);
635-
ASSERT_EQ(test->expected_events.mmio_exits, events.mmio_exits);
636-
ASSERT_EQ(test->expected_events.fail_vcpu_runs, events.fail_vcpu_runs);
633+
TEST_ASSERT_EQ(test->expected_events.uffd_faults, events.uffd_faults);
634+
TEST_ASSERT_EQ(test->expected_events.mmio_exits, events.mmio_exits);
635+
TEST_ASSERT_EQ(test->expected_events.fail_vcpu_runs, events.fail_vcpu_runs);
637636
}
638637

639638
static void print_test_banner(enum vm_guest_mode mode, struct test_params *p)
@@ -679,7 +678,7 @@ static void vcpu_run_loop(struct kvm_vm *vm, struct kvm_vcpu *vcpu,
679678
}
680679
break;
681680
case UCALL_ABORT:
682-
REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
681+
REPORT_GUEST_ASSERT(uc);
683682
break;
684683
case UCALL_DONE:
685684
goto done;

tools/testing/selftests/kvm/aarch64/vgic_irq.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* host to inject a specific intid via a GUEST_SYNC call, and then checks that
88
* it received it.
99
*/
10-
1110
#include <asm/kvm.h>
1211
#include <asm/kvm_para.h>
1312
#include <sys/eventfd.h>
@@ -781,7 +780,7 @@ static void test_vgic(uint32_t nr_irqs, bool level_sensitive, bool eoi_split)
781780
run_guest_cmd(vcpu, gic_fd, &inject_args, &args);
782781
break;
783782
case UCALL_ABORT:
784-
REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
783+
REPORT_GUEST_ASSERT(uc);
785784
break;
786785
case UCALL_DONE:
787786
goto done;

0 commit comments

Comments
 (0)