Skip to content

Commit dee7ea4

Browse files
committed
Merge tag 'kvm-x86-selftests_utils-6.10' of https://github.com/kvm-x86/linux into HEAD
KVM selftests treewide updates for 6.10: - Define _GNU_SOURCE for all selftests to fix a warning that was introduced by a change to kselftest_harness.h late in the 6.9 cycle, and because forcing every test to #define _GNU_SOURCE is painful. - Provide a global psuedo-RNG instance for all tests, so that library code can generate random, but determinstic numbers. - Use the global pRNG to randomly force emulation of select writes from guest code on x86, e.g. to help validate KVM's emulation of locked accesses. - Rename kvm_util_base.h back to kvm_util.h, as the weird layer of indirection was added purely to avoid manually #including ucall_common.h in a handful of locations. - Allocate and initialize x86's GDT, IDT, TSS, segments, and default exception handlers at VM creation, instead of forcing tests to manually trigger the related setup.
2 parents 31a6cd7 + b093f87 commit dee7ea4

File tree

86 files changed

+1420
-447
lines changed

Some content is hidden

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

86 files changed

+1420
-447
lines changed

tools/testing/selftests/kvm/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
230230
endif
231231
CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
232232
-Wno-gnu-variable-sized-type-not-at-end -MD -MP -DCONFIG_64BIT \
233-
-fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \
234-
-fno-builtin-strnlen \
233+
-D_GNU_SOURCE -fno-builtin-memcmp -fno-builtin-memcpy \
234+
-fno-builtin-memset -fno-builtin-strnlen \
235235
-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
236236
-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
237237
-I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
*
66
* Copyright (c) 2021, Google LLC.
77
*/
8-
#define _GNU_SOURCE
9-
108
#include "arch_timer.h"
119
#include "delay.h"
1210
#include "gic.h"
1311
#include "processor.h"
1412
#include "timer_test.h"
13+
#include "ucall_common.h"
1514
#include "vgic.h"
1615

1716
enum guest_stage {

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

Lines changed: 0 additions & 1 deletion
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-
#define _GNU_SOURCE
1110
#include <linux/bitmap.h>
1211
#include <fcntl.h>
1312
#include <test_util.h>

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
* KVM_SYSTEM_EVENT_SUSPEND UAPI.
1212
*/
1313

14-
#define _GNU_SOURCE
15-
1614
#include <linux/kernel.h>
1715
#include <linux/psci.h>
1816
#include <asm/cputype.h>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*
55
* Copyright (C) 2020, Red Hat, Inc.
66
*/
7-
#define _GNU_SOURCE
87
#include <linux/kernel.h>
98
#include <sys/syscall.h>
109
#include <asm/kvm.h>

tools/testing/selftests/kvm/arch_timer.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@
1919
*
2020
* Copyright (c) 2021, Google LLC.
2121
*/
22-
23-
#define _GNU_SOURCE
24-
2522
#include <stdlib.h>
2623
#include <pthread.h>
2724
#include <linux/sizes.h>
2825
#include <linux/bitmap.h>
2926
#include <sys/sysinfo.h>
3027

3128
#include "timer_test.h"
29+
#include "ucall_common.h"
3230

3331
struct test_args test_args = {
3432
.nr_vcpus = NR_VCPUS_DEF,

tools/testing/selftests/kvm/demand_paging_test.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
* Copyright (C) 2018, Red Hat, Inc.
77
* Copyright (C) 2019, Google, Inc.
88
*/
9-
10-
#define _GNU_SOURCE /* for pipe2 */
11-
129
#include <inttypes.h>
1310
#include <stdio.h>
1411
#include <stdlib.h>
@@ -21,6 +18,7 @@
2118
#include "test_util.h"
2219
#include "memstress.h"
2320
#include "guest_modes.h"
21+
#include "ucall_common.h"
2422
#include "userfaultfd_util.h"
2523

2624
#ifdef __NR_userfaultfd

tools/testing/selftests/kvm/dirty_log_perf_test.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "test_util.h"
1919
#include "memstress.h"
2020
#include "guest_modes.h"
21+
#include "ucall_common.h"
2122

2223
#ifdef __aarch64__
2324
#include "aarch64/vgic.h"
@@ -129,7 +130,6 @@ struct test_params {
129130
enum vm_mem_backing_src_type backing_src;
130131
int slots;
131132
uint32_t write_percent;
132-
uint32_t random_seed;
133133
bool random_access;
134134
};
135135

@@ -153,8 +153,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
153153
p->slots, p->backing_src,
154154
p->partition_vcpu_memory_access);
155155

156-
pr_info("Random seed: %u\n", p->random_seed);
157-
memstress_set_random_seed(vm, p->random_seed);
158156
memstress_set_write_percent(vm, p->write_percent);
159157

160158
guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm->page_shift;
@@ -343,11 +341,13 @@ int main(int argc, char *argv[])
343341
.partition_vcpu_memory_access = true,
344342
.backing_src = DEFAULT_VM_MEM_SRC,
345343
.slots = 1,
346-
.random_seed = 1,
347344
.write_percent = 100,
348345
};
349346
int opt;
350347

348+
/* Override the seed to be deterministic by default. */
349+
guest_random_seed = 1;
350+
351351
dirty_log_manual_caps =
352352
kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
353353
dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
@@ -392,7 +392,7 @@ int main(int argc, char *argv[])
392392
p.phys_offset = strtoull(optarg, NULL, 0);
393393
break;
394394
case 'r':
395-
p.random_seed = atoi_positive("Random seed", optarg);
395+
guest_random_seed = atoi_positive("Random seed", optarg);
396396
break;
397397
case 's':
398398
p.backing_src = parse_backing_src_type(optarg);

tools/testing/selftests/kvm/dirty_log_test.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
*
55
* Copyright (C) 2018, Red Hat, Inc.
66
*/
7-
8-
#define _GNU_SOURCE /* for program_invocation_name */
9-
107
#include <stdio.h>
118
#include <stdlib.h>
129
#include <pthread.h>
@@ -23,6 +20,7 @@
2320
#include "test_util.h"
2421
#include "guest_modes.h"
2522
#include "processor.h"
23+
#include "ucall_common.h"
2624

2725
#define DIRTY_MEM_BITS 30 /* 1G */
2826
#define PAGE_SHIFT_4K 12
@@ -76,7 +74,6 @@
7674
static uint64_t host_page_size;
7775
static uint64_t guest_page_size;
7876
static uint64_t guest_num_pages;
79-
static uint64_t random_array[TEST_PAGES_PER_LOOP];
8077
static uint64_t iteration;
8178

8279
/*
@@ -109,19 +106,19 @@ static void guest_code(void)
109106
*/
110107
for (i = 0; i < guest_num_pages; i++) {
111108
addr = guest_test_virt_mem + i * guest_page_size;
112-
*(uint64_t *)addr = READ_ONCE(iteration);
109+
vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration));
113110
}
114111

115112
while (true) {
116113
for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
117114
addr = guest_test_virt_mem;
118-
addr += (READ_ONCE(random_array[i]) % guest_num_pages)
115+
addr += (guest_random_u64(&guest_rng) % guest_num_pages)
119116
* guest_page_size;
120117
addr = align_down(addr, host_page_size);
121-
*(uint64_t *)addr = READ_ONCE(iteration);
118+
119+
vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration));
122120
}
123121

124-
/* Tell the host that we need more random numbers */
125122
GUEST_SYNC(1);
126123
}
127124
}
@@ -508,20 +505,10 @@ static void log_mode_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err)
508505
mode->after_vcpu_run(vcpu, ret, err);
509506
}
510507

511-
static void generate_random_array(uint64_t *guest_array, uint64_t size)
512-
{
513-
uint64_t i;
514-
515-
for (i = 0; i < size; i++)
516-
guest_array[i] = random();
517-
}
518-
519508
static void *vcpu_worker(void *data)
520509
{
521510
int ret;
522511
struct kvm_vcpu *vcpu = data;
523-
struct kvm_vm *vm = vcpu->vm;
524-
uint64_t *guest_array;
525512
uint64_t pages_count = 0;
526513
struct kvm_signal_mask *sigmask = alloca(offsetof(struct kvm_signal_mask, sigset)
527514
+ sizeof(sigset_t));
@@ -540,11 +527,8 @@ static void *vcpu_worker(void *data)
540527
sigemptyset(sigset);
541528
sigaddset(sigset, SIG_IPI);
542529

543-
guest_array = addr_gva2hva(vm, (vm_vaddr_t)random_array);
544-
545530
while (!READ_ONCE(host_quit)) {
546531
/* Clear any existing kick signals */
547-
generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
548532
pages_count += TEST_PAGES_PER_LOOP;
549533
/* Let the guest dirty the random pages */
550534
ret = __vcpu_run(vcpu);

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*
55
* Author: Chao Peng <[email protected]>
66
*/
7-
8-
#define _GNU_SOURCE
97
#include <stdlib.h>
108
#include <string.h>
119
#include <unistd.h>
@@ -19,8 +17,8 @@
1917
#include <sys/types.h>
2018
#include <sys/stat.h>
2119

20+
#include "kvm_util.h"
2221
#include "test_util.h"
23-
#include "kvm_util_base.h"
2422

2523
static void test_file_read_write(int fd)
2624
{

0 commit comments

Comments
 (0)