Skip to content

Commit 3e0797f

Browse files
committed
Merge tag 'kvm-x86-selftests-6.16' of https://github.com/kvm-x86/linux into HEAD
KVM selftests changes for 6.16: - Add support for SNP to the various SEV selftests. - Add a selftest to verify fastops instructions via forced emulation. - Add MGLRU support to the access tracking perf test.
2 parents db44dcb + d166453 commit 3e0797f

File tree

23 files changed

+1273
-209
lines changed

23 files changed

+1273
-209
lines changed

arch/x86/include/uapi/asm/kvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ struct kvm_sev_snp_launch_start {
845845
};
846846

847847
/* Kept in sync with firmware values for simplicity. */
848+
#define KVM_SEV_PAGE_TYPE_INVALID 0x0
848849
#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1
849850
#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3
850851
#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4

tools/arch/x86/include/uapi/asm/kvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ struct kvm_sev_snp_launch_start {
844844
};
845845

846846
/* Kept in sync with firmware values for simplicity. */
847+
#define KVM_SEV_PAGE_TYPE_INVALID 0x0
847848
#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1
848849
#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3
849850
#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4

tools/testing/selftests/cgroup/Makefile

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ TEST_GEN_PROGS += test_zswap
2121
LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
2222

2323
include ../lib.mk
24+
include lib/libcgroup.mk
2425

25-
$(OUTPUT)/test_core: cgroup_util.c
26-
$(OUTPUT)/test_cpu: cgroup_util.c
27-
$(OUTPUT)/test_cpuset: cgroup_util.c
28-
$(OUTPUT)/test_freezer: cgroup_util.c
29-
$(OUTPUT)/test_hugetlb_memcg: cgroup_util.c
30-
$(OUTPUT)/test_kill: cgroup_util.c
31-
$(OUTPUT)/test_kmem: cgroup_util.c
32-
$(OUTPUT)/test_memcontrol: cgroup_util.c
33-
$(OUTPUT)/test_pids: cgroup_util.c
34-
$(OUTPUT)/test_zswap: cgroup_util.c
26+
$(OUTPUT)/test_core: $(LIBCGROUP_O)
27+
$(OUTPUT)/test_cpu: $(LIBCGROUP_O)
28+
$(OUTPUT)/test_cpuset: $(LIBCGROUP_O)
29+
$(OUTPUT)/test_freezer: $(LIBCGROUP_O)
30+
$(OUTPUT)/test_hugetlb_memcg: $(LIBCGROUP_O)
31+
$(OUTPUT)/test_kill: $(LIBCGROUP_O)
32+
$(OUTPUT)/test_kmem: $(LIBCGROUP_O)
33+
$(OUTPUT)/test_memcontrol: $(LIBCGROUP_O)
34+
$(OUTPUT)/test_pids: $(LIBCGROUP_O)
35+
$(OUTPUT)/test_zswap: $(LIBCGROUP_O)

tools/testing/selftests/cgroup/cgroup_util.c renamed to tools/testing/selftests/cgroup/lib/cgroup_util.c

Lines changed: 30 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#include <unistd.h>
1818

1919
#include "cgroup_util.h"
20-
#include "../clone3/clone3_selftests.h"
20+
#include "../../clone3/clone3_selftests.h"
2121

2222
/* Returns read len on success, or -errno on failure. */
23-
static ssize_t read_text(const char *path, char *buf, size_t max_len)
23+
ssize_t read_text(const char *path, char *buf, size_t max_len)
2424
{
2525
ssize_t len;
2626
int fd;
@@ -39,7 +39,7 @@ static ssize_t read_text(const char *path, char *buf, size_t max_len)
3939
}
4040

4141
/* Returns written len on success, or -errno on failure. */
42-
static ssize_t write_text(const char *path, char *buf, ssize_t len)
42+
ssize_t write_text(const char *path, char *buf, ssize_t len)
4343
{
4444
int fd;
4545

@@ -217,7 +217,8 @@ int cg_write_numeric(const char *cgroup, const char *control, long value)
217217
return cg_write(cgroup, control, buf);
218218
}
219219

220-
int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
220+
static int cg_find_root(char *root, size_t len, const char *controller,
221+
bool *nsdelegate)
221222
{
222223
char buf[10 * PAGE_SIZE];
223224
char *fs, *mount, *type, *options;
@@ -236,18 +237,37 @@ int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
236237
options = strtok(NULL, delim);
237238
strtok(NULL, delim);
238239
strtok(NULL, delim);
239-
240-
if (strcmp(type, "cgroup2") == 0) {
241-
strncpy(root, mount, len);
242-
if (nsdelegate)
243-
*nsdelegate = !!strstr(options, "nsdelegate");
244-
return 0;
240+
if (strcmp(type, "cgroup") == 0) {
241+
if (!controller || !strstr(options, controller))
242+
continue;
243+
} else if (strcmp(type, "cgroup2") == 0) {
244+
if (controller &&
245+
cg_read_strstr(mount, "cgroup.controllers", controller))
246+
continue;
247+
} else {
248+
continue;
245249
}
250+
strncpy(root, mount, len);
251+
252+
if (nsdelegate)
253+
*nsdelegate = !!strstr(options, "nsdelegate");
254+
return 0;
255+
246256
}
247257

248258
return -1;
249259
}
250260

261+
int cg_find_controller_root(char *root, size_t len, const char *controller)
262+
{
263+
return cg_find_root(root, len, controller, NULL);
264+
}
265+
266+
int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
267+
{
268+
return cg_find_root(root, len, NULL, nsdelegate);
269+
}
270+
251271
int cg_create(const char *cgroup)
252272
{
253273
return mkdir(cgroup, 0755);
@@ -488,84 +508,6 @@ int cg_run_nowait(const char *cgroup,
488508
return pid;
489509
}
490510

491-
int get_temp_fd(void)
492-
{
493-
return open(".", O_TMPFILE | O_RDWR | O_EXCL);
494-
}
495-
496-
int alloc_pagecache(int fd, size_t size)
497-
{
498-
char buf[PAGE_SIZE];
499-
struct stat st;
500-
int i;
501-
502-
if (fstat(fd, &st))
503-
goto cleanup;
504-
505-
size += st.st_size;
506-
507-
if (ftruncate(fd, size))
508-
goto cleanup;
509-
510-
for (i = 0; i < size; i += sizeof(buf))
511-
read(fd, buf, sizeof(buf));
512-
513-
return 0;
514-
515-
cleanup:
516-
return -1;
517-
}
518-
519-
int alloc_anon(const char *cgroup, void *arg)
520-
{
521-
size_t size = (unsigned long)arg;
522-
char *buf, *ptr;
523-
524-
buf = malloc(size);
525-
for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
526-
*ptr = 0;
527-
528-
free(buf);
529-
return 0;
530-
}
531-
532-
int is_swap_enabled(void)
533-
{
534-
char buf[PAGE_SIZE];
535-
const char delim[] = "\n";
536-
int cnt = 0;
537-
char *line;
538-
539-
if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
540-
return -1;
541-
542-
for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
543-
cnt++;
544-
545-
return cnt > 1;
546-
}
547-
548-
int set_oom_adj_score(int pid, int score)
549-
{
550-
char path[PATH_MAX];
551-
int fd, len;
552-
553-
sprintf(path, "/proc/%d/oom_score_adj", pid);
554-
555-
fd = open(path, O_WRONLY | O_APPEND);
556-
if (fd < 0)
557-
return fd;
558-
559-
len = dprintf(fd, "%d", score);
560-
if (len < 0) {
561-
close(fd);
562-
return len;
563-
}
564-
565-
close(fd);
566-
return 0;
567-
}
568-
569511
int proc_mount_contains(const char *option)
570512
{
571513
char buf[4 * PAGE_SIZE];

tools/testing/selftests/cgroup/cgroup_util.h renamed to tools/testing/selftests/cgroup/lib/include/cgroup_util.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#include <stdbool.h>
33
#include <stdlib.h>
44

5-
#include "../kselftest.h"
6-
5+
#ifndef PAGE_SIZE
76
#define PAGE_SIZE 4096
7+
#endif
88

99
#define MB(x) (x << 20)
1010

@@ -21,6 +21,10 @@ static inline int values_close(long a, long b, int err)
2121
return labs(a - b) <= (a + b) / 100 * err;
2222
}
2323

24+
extern ssize_t read_text(const char *path, char *buf, size_t max_len);
25+
extern ssize_t write_text(const char *path, char *buf, ssize_t len);
26+
27+
extern int cg_find_controller_root(char *root, size_t len, const char *controller);
2428
extern int cg_find_unified_root(char *root, size_t len, bool *nsdelegate);
2529
extern char *cg_name(const char *root, const char *name);
2630
extern char *cg_name_indexed(const char *root, const char *name, int index);
@@ -49,11 +53,6 @@ extern int cg_enter_current_thread(const char *cgroup);
4953
extern int cg_run_nowait(const char *cgroup,
5054
int (*fn)(const char *cgroup, void *arg),
5155
void *arg);
52-
extern int get_temp_fd(void);
53-
extern int alloc_pagecache(int fd, size_t size);
54-
extern int alloc_anon(const char *cgroup, void *arg);
55-
extern int is_swap_enabled(void);
56-
extern int set_oom_adj_score(int pid, int score);
5756
extern int cg_wait_for_proc_count(const char *cgroup, int count);
5857
extern int cg_killall(const char *cgroup);
5958
int proc_mount_contains(const char *option);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CGROUP_DIR := $(selfdir)/cgroup
2+
3+
LIBCGROUP_C := lib/cgroup_util.c
4+
5+
LIBCGROUP_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBCGROUP_C))
6+
7+
LIBCGROUP_O_DIRS := $(shell dirname $(LIBCGROUP_O) | uniq)
8+
9+
CFLAGS += -I$(CGROUP_DIR)/lib/include
10+
11+
EXTRA_HDRS := $(selfdir)/clone3/clone3_selftests.h
12+
13+
$(LIBCGROUP_O_DIRS):
14+
mkdir -p $@
15+
16+
$(LIBCGROUP_O): $(OUTPUT)/%.o : $(CGROUP_DIR)/%.c $(EXTRA_HDRS) $(LIBCGROUP_O_DIRS)
17+
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
18+
19+
EXTRA_CLEAN += $(LIBCGROUP_O)

tools/testing/selftests/cgroup/test_memcontrol.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,84 @@
2424
static bool has_localevents;
2525
static bool has_recursiveprot;
2626

27+
int get_temp_fd(void)
28+
{
29+
return open(".", O_TMPFILE | O_RDWR | O_EXCL);
30+
}
31+
32+
int alloc_pagecache(int fd, size_t size)
33+
{
34+
char buf[PAGE_SIZE];
35+
struct stat st;
36+
int i;
37+
38+
if (fstat(fd, &st))
39+
goto cleanup;
40+
41+
size += st.st_size;
42+
43+
if (ftruncate(fd, size))
44+
goto cleanup;
45+
46+
for (i = 0; i < size; i += sizeof(buf))
47+
read(fd, buf, sizeof(buf));
48+
49+
return 0;
50+
51+
cleanup:
52+
return -1;
53+
}
54+
55+
int alloc_anon(const char *cgroup, void *arg)
56+
{
57+
size_t size = (unsigned long)arg;
58+
char *buf, *ptr;
59+
60+
buf = malloc(size);
61+
for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
62+
*ptr = 0;
63+
64+
free(buf);
65+
return 0;
66+
}
67+
68+
int is_swap_enabled(void)
69+
{
70+
char buf[PAGE_SIZE];
71+
const char delim[] = "\n";
72+
int cnt = 0;
73+
char *line;
74+
75+
if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
76+
return -1;
77+
78+
for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
79+
cnt++;
80+
81+
return cnt > 1;
82+
}
83+
84+
int set_oom_adj_score(int pid, int score)
85+
{
86+
char path[PATH_MAX];
87+
int fd, len;
88+
89+
sprintf(path, "/proc/%d/oom_score_adj", pid);
90+
91+
fd = open(path, O_WRONLY | O_APPEND);
92+
if (fd < 0)
93+
return fd;
94+
95+
len = dprintf(fd, "%d", score);
96+
if (len < 0) {
97+
close(fd);
98+
return len;
99+
}
100+
101+
close(fd);
102+
return 0;
103+
}
104+
27105
/*
28106
* This test creates two nested cgroups with and without enabling
29107
* the memory controller.

tools/testing/selftests/kvm/Makefile.kvm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LIBKVM += lib/elf.c
88
LIBKVM += lib/guest_modes.c
99
LIBKVM += lib/io.c
1010
LIBKVM += lib/kvm_util.c
11+
LIBKVM += lib/lru_gen_util.c
1112
LIBKVM += lib/memstress.c
1213
LIBKVM += lib/guest_sprintf.c
1314
LIBKVM += lib/rbtree.c
@@ -70,6 +71,7 @@ TEST_GEN_PROGS_x86 += x86/cr4_cpuid_sync_test
7071
TEST_GEN_PROGS_x86 += x86/dirty_log_page_splitting_test
7172
TEST_GEN_PROGS_x86 += x86/feature_msrs_test
7273
TEST_GEN_PROGS_x86 += x86/exit_on_emulation_failure_test
74+
TEST_GEN_PROGS_x86 += x86/fastops_test
7375
TEST_GEN_PROGS_x86 += x86/fix_hypercall_test
7476
TEST_GEN_PROGS_x86 += x86/hwcr_msr_test
7577
TEST_GEN_PROGS_x86 += x86/hyperv_clock
@@ -222,6 +224,7 @@ OVERRIDE_TARGETS = 1
222224
# importantly defines, i.e. overwrites, $(CC) (unless `make -e` or `make CC=`,
223225
# which causes the environment variable to override the makefile).
224226
include ../lib.mk
227+
include ../cgroup/lib/libcgroup.mk
225228

226229
INSTALL_HDR_PATH = $(top_srcdir)/usr
227230
LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
@@ -275,7 +278,7 @@ LIBKVM_S := $(filter %.S,$(LIBKVM))
275278
LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))
276279
LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
277280
LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING))
278-
LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ)
281+
LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) $(LIBCGROUP_O)
279282
SPLIT_TEST_GEN_PROGS := $(patsubst %, $(OUTPUT)/%, $(SPLIT_TESTS))
280283
SPLIT_TEST_GEN_OBJ := $(patsubst %, $(OUTPUT)/$(ARCH)/%.o, $(SPLIT_TESTS))
281284

0 commit comments

Comments
 (0)