diff options
Diffstat (limited to 'tools/testing')
-rwxr-xr-x | tools/testing/ktest/ktest.pl | 14 | ||||
-rw-r--r-- | tools/testing/selftests/Makefile | 8 | ||||
-rw-r--r-- | tools/testing/selftests/ptrace/Makefile | 10 | ||||
-rw-r--r-- | tools/testing/selftests/ptrace/peeksiginfo.c | 214 | ||||
-rw-r--r-- | tools/testing/selftests/soft-dirty/Makefile | 10 | ||||
-rw-r--r-- | tools/testing/selftests/soft-dirty/soft-dirty.c | 114 |
6 files changed, 365 insertions, 5 deletions
diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl index 4e67d52eb3a2..0d7fd8b51544 100755 --- a/tools/testing/ktest/ktest.pl +++ b/tools/testing/ktest/ktest.pl | |||
@@ -73,6 +73,7 @@ my $ktest_config; | |||
73 | my $version; | 73 | my $version; |
74 | my $have_version = 0; | 74 | my $have_version = 0; |
75 | my $machine; | 75 | my $machine; |
76 | my $last_machine; | ||
76 | my $ssh_user; | 77 | my $ssh_user; |
77 | my $tmpdir; | 78 | my $tmpdir; |
78 | my $builddir; | 79 | my $builddir; |
@@ -108,6 +109,7 @@ my $scp_to_target; | |||
108 | my $scp_to_target_install; | 109 | my $scp_to_target_install; |
109 | my $power_off; | 110 | my $power_off; |
110 | my $grub_menu; | 111 | my $grub_menu; |
112 | my $last_grub_menu; | ||
111 | my $grub_file; | 113 | my $grub_file; |
112 | my $grub_number; | 114 | my $grub_number; |
113 | my $grub_reboot; | 115 | my $grub_reboot; |
@@ -1538,7 +1540,9 @@ sub run_scp_mod { | |||
1538 | 1540 | ||
1539 | sub get_grub2_index { | 1541 | sub get_grub2_index { |
1540 | 1542 | ||
1541 | return if (defined($grub_number)); | 1543 | return if (defined($grub_number) && defined($last_grub_menu) && |
1544 | $last_grub_menu eq $grub_menu && defined($last_machine) && | ||
1545 | $last_machine eq $machine); | ||
1542 | 1546 | ||
1543 | doprint "Find grub2 menu ... "; | 1547 | doprint "Find grub2 menu ... "; |
1544 | $grub_number = -1; | 1548 | $grub_number = -1; |
@@ -1565,6 +1569,8 @@ sub get_grub2_index { | |||
1565 | die "Could not find '$grub_menu' in $grub_file on $machine" | 1569 | die "Could not find '$grub_menu' in $grub_file on $machine" |
1566 | if (!$found); | 1570 | if (!$found); |
1567 | doprint "$grub_number\n"; | 1571 | doprint "$grub_number\n"; |
1572 | $last_grub_menu = $grub_menu; | ||
1573 | $last_machine = $machine; | ||
1568 | } | 1574 | } |
1569 | 1575 | ||
1570 | sub get_grub_index { | 1576 | sub get_grub_index { |
@@ -1577,7 +1583,9 @@ sub get_grub_index { | |||
1577 | if ($reboot_type ne "grub") { | 1583 | if ($reboot_type ne "grub") { |
1578 | return; | 1584 | return; |
1579 | } | 1585 | } |
1580 | return if (defined($grub_number)); | 1586 | return if (defined($grub_number) && defined($last_grub_menu) && |
1587 | $last_grub_menu eq $grub_menu && defined($last_machine) && | ||
1588 | $last_machine eq $machine); | ||
1581 | 1589 | ||
1582 | doprint "Find grub menu ... "; | 1590 | doprint "Find grub menu ... "; |
1583 | $grub_number = -1; | 1591 | $grub_number = -1; |
@@ -1604,6 +1612,8 @@ sub get_grub_index { | |||
1604 | die "Could not find '$grub_menu' in /boot/grub/menu on $machine" | 1612 | die "Could not find '$grub_menu' in /boot/grub/menu on $machine" |
1605 | if (!$found); | 1613 | if (!$found); |
1606 | doprint "$grub_number\n"; | 1614 | doprint "$grub_number\n"; |
1615 | $last_grub_menu = $grub_menu; | ||
1616 | $last_machine = $machine; | ||
1607 | } | 1617 | } |
1608 | 1618 | ||
1609 | sub wait_for_input | 1619 | sub wait_for_input |
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 3cc0ad7ae863..fa6ea69f2e48 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile | |||
@@ -1,10 +1,12 @@ | |||
1 | TARGETS = breakpoints | 1 | TARGETS = breakpoints |
2 | TARGETS += cpu-hotplug | ||
3 | TARGETS += efivarfs | ||
2 | TARGETS += kcmp | 4 | TARGETS += kcmp |
5 | TARGETS += memory-hotplug | ||
3 | TARGETS += mqueue | 6 | TARGETS += mqueue |
7 | TARGETS += ptrace | ||
8 | TARGETS += soft-dirty | ||
4 | TARGETS += vm | 9 | TARGETS += vm |
5 | TARGETS += cpu-hotplug | ||
6 | TARGETS += memory-hotplug | ||
7 | TARGETS += efivarfs | ||
8 | 10 | ||
9 | all: | 11 | all: |
10 | for TARGET in $(TARGETS); do \ | 12 | for TARGET in $(TARGETS); do \ |
diff --git a/tools/testing/selftests/ptrace/Makefile b/tools/testing/selftests/ptrace/Makefile new file mode 100644 index 000000000000..47ae2d385ce8 --- /dev/null +++ b/tools/testing/selftests/ptrace/Makefile | |||
@@ -0,0 +1,10 @@ | |||
1 | CFLAGS += -iquote../../../../include/uapi -Wall | ||
2 | peeksiginfo: peeksiginfo.c | ||
3 | |||
4 | all: peeksiginfo | ||
5 | |||
6 | clean: | ||
7 | rm -f peeksiginfo | ||
8 | |||
9 | run_tests: all | ||
10 | @./peeksiginfo || echo "peeksiginfo selftests: [FAIL]" | ||
diff --git a/tools/testing/selftests/ptrace/peeksiginfo.c b/tools/testing/selftests/ptrace/peeksiginfo.c new file mode 100644 index 000000000000..d46558b1f58d --- /dev/null +++ b/tools/testing/selftests/ptrace/peeksiginfo.c | |||
@@ -0,0 +1,214 @@ | |||
1 | #define _GNU_SOURCE | ||
2 | #include <stdio.h> | ||
3 | #include <signal.h> | ||
4 | #include <unistd.h> | ||
5 | #include <errno.h> | ||
6 | #include <linux/types.h> | ||
7 | #include <sys/wait.h> | ||
8 | #include <sys/syscall.h> | ||
9 | #include <sys/user.h> | ||
10 | #include <sys/mman.h> | ||
11 | |||
12 | #include "linux/ptrace.h" | ||
13 | |||
14 | static int sys_rt_sigqueueinfo(pid_t tgid, int sig, siginfo_t *uinfo) | ||
15 | { | ||
16 | return syscall(SYS_rt_sigqueueinfo, tgid, sig, uinfo); | ||
17 | } | ||
18 | |||
19 | static int sys_rt_tgsigqueueinfo(pid_t tgid, pid_t tid, | ||
20 | int sig, siginfo_t *uinfo) | ||
21 | { | ||
22 | return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, uinfo); | ||
23 | } | ||
24 | |||
25 | static int sys_ptrace(int request, pid_t pid, void *addr, void *data) | ||
26 | { | ||
27 | return syscall(SYS_ptrace, request, pid, addr, data); | ||
28 | } | ||
29 | |||
30 | #define SIGNR 10 | ||
31 | #define TEST_SICODE_PRIV -1 | ||
32 | #define TEST_SICODE_SHARE -2 | ||
33 | |||
34 | #define err(fmt, ...) \ | ||
35 | fprintf(stderr, \ | ||
36 | "Error (%s:%d): " fmt, \ | ||
37 | __FILE__, __LINE__, ##__VA_ARGS__) | ||
38 | |||
39 | static int check_error_paths(pid_t child) | ||
40 | { | ||
41 | struct ptrace_peeksiginfo_args arg; | ||
42 | int ret, exit_code = -1; | ||
43 | void *addr_rw, *addr_ro; | ||
44 | |||
45 | /* | ||
46 | * Allocate two contiguous pages. The first one is for read-write, | ||
47 | * another is for read-only. | ||
48 | */ | ||
49 | addr_rw = mmap(NULL, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE, | ||
50 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
51 | if (addr_rw == MAP_FAILED) { | ||
52 | err("mmap() failed: %m\n"); | ||
53 | return 1; | ||
54 | } | ||
55 | |||
56 | addr_ro = mmap(addr_rw + PAGE_SIZE, PAGE_SIZE, PROT_READ, | ||
57 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); | ||
58 | if (addr_ro == MAP_FAILED) { | ||
59 | err("mmap() failed: %m\n"); | ||
60 | goto out; | ||
61 | } | ||
62 | |||
63 | arg.nr = SIGNR; | ||
64 | arg.off = 0; | ||
65 | |||
66 | /* Unsupported flags */ | ||
67 | arg.flags = ~0; | ||
68 | ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, addr_rw); | ||
69 | if (ret != -1 || errno != EINVAL) { | ||
70 | err("sys_ptrace() returns %d (expected -1)," | ||
71 | " errno %d (expected %d): %m\n", | ||
72 | ret, errno, EINVAL); | ||
73 | goto out; | ||
74 | } | ||
75 | arg.flags = 0; | ||
76 | |||
77 | /* A part of the buffer is read-only */ | ||
78 | ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, | ||
79 | addr_ro - sizeof(siginfo_t) * 2); | ||
80 | if (ret != 2) { | ||
81 | err("sys_ptrace() returns %d (expected 2): %m\n", ret); | ||
82 | goto out; | ||
83 | } | ||
84 | |||
85 | /* Read-only buffer */ | ||
86 | ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, addr_ro); | ||
87 | if (ret != -1 && errno != EFAULT) { | ||
88 | err("sys_ptrace() returns %d (expected -1)," | ||
89 | " errno %d (expected %d): %m\n", | ||
90 | ret, errno, EFAULT); | ||
91 | goto out; | ||
92 | } | ||
93 | |||
94 | exit_code = 0; | ||
95 | out: | ||
96 | munmap(addr_rw, 2 * PAGE_SIZE); | ||
97 | return exit_code; | ||
98 | } | ||
99 | |||
100 | int check_direct_path(pid_t child, int shared, int nr) | ||
101 | { | ||
102 | struct ptrace_peeksiginfo_args arg = {.flags = 0, .nr = nr, .off = 0}; | ||
103 | int i, j, ret, exit_code = -1; | ||
104 | siginfo_t siginfo[SIGNR]; | ||
105 | int si_code; | ||
106 | |||
107 | if (shared == 1) { | ||
108 | arg.flags = PTRACE_PEEKSIGINFO_SHARED; | ||
109 | si_code = TEST_SICODE_SHARE; | ||
110 | } else { | ||
111 | arg.flags = 0; | ||
112 | si_code = TEST_SICODE_PRIV; | ||
113 | } | ||
114 | |||
115 | for (i = 0; i < SIGNR; ) { | ||
116 | arg.off = i; | ||
117 | ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, siginfo); | ||
118 | if (ret == -1) { | ||
119 | err("ptrace() failed: %m\n"); | ||
120 | goto out; | ||
121 | } | ||
122 | |||
123 | if (ret == 0) | ||
124 | break; | ||
125 | |||
126 | for (j = 0; j < ret; j++, i++) { | ||
127 | if (siginfo[j].si_code == si_code && | ||
128 | siginfo[j].si_int == i) | ||
129 | continue; | ||
130 | |||
131 | err("%d: Wrong siginfo i=%d si_code=%d si_int=%d\n", | ||
132 | shared, i, siginfo[j].si_code, siginfo[j].si_int); | ||
133 | goto out; | ||
134 | } | ||
135 | } | ||
136 | |||
137 | if (i != SIGNR) { | ||
138 | err("Only %d signals were read\n", i); | ||
139 | goto out; | ||
140 | } | ||
141 | |||
142 | exit_code = 0; | ||
143 | out: | ||
144 | return exit_code; | ||
145 | } | ||
146 | |||
147 | int main(int argc, char *argv[]) | ||
148 | { | ||
149 | siginfo_t siginfo[SIGNR]; | ||
150 | int i, exit_code = 1; | ||
151 | sigset_t blockmask; | ||
152 | pid_t child; | ||
153 | |||
154 | sigemptyset(&blockmask); | ||
155 | sigaddset(&blockmask, SIGRTMIN); | ||
156 | sigprocmask(SIG_BLOCK, &blockmask, NULL); | ||
157 | |||
158 | child = fork(); | ||
159 | if (child == -1) { | ||
160 | err("fork() failed: %m"); | ||
161 | return 1; | ||
162 | } else if (child == 0) { | ||
163 | pid_t ppid = getppid(); | ||
164 | while (1) { | ||
165 | if (ppid != getppid()) | ||
166 | break; | ||
167 | sleep(1); | ||
168 | } | ||
169 | return 1; | ||
170 | } | ||
171 | |||
172 | /* Send signals in process-wide and per-thread queues */ | ||
173 | for (i = 0; i < SIGNR; i++) { | ||
174 | siginfo->si_code = TEST_SICODE_SHARE; | ||
175 | siginfo->si_int = i; | ||
176 | sys_rt_sigqueueinfo(child, SIGRTMIN, siginfo); | ||
177 | |||
178 | siginfo->si_code = TEST_SICODE_PRIV; | ||
179 | siginfo->si_int = i; | ||
180 | sys_rt_tgsigqueueinfo(child, child, SIGRTMIN, siginfo); | ||
181 | } | ||
182 | |||
183 | if (sys_ptrace(PTRACE_ATTACH, child, NULL, NULL) == -1) | ||
184 | return 1; | ||
185 | |||
186 | waitpid(child, NULL, 0); | ||
187 | |||
188 | /* Dump signals one by one*/ | ||
189 | if (check_direct_path(child, 0, 1)) | ||
190 | goto out; | ||
191 | /* Dump all signals for one call */ | ||
192 | if (check_direct_path(child, 0, SIGNR)) | ||
193 | goto out; | ||
194 | |||
195 | /* | ||
196 | * Dump signal from the process-wide queue. | ||
197 | * The number of signals is not multible to the buffer size | ||
198 | */ | ||
199 | if (check_direct_path(child, 1, 3)) | ||
200 | goto out; | ||
201 | |||
202 | if (check_error_paths(child)) | ||
203 | goto out; | ||
204 | |||
205 | printf("PASS\n"); | ||
206 | exit_code = 0; | ||
207 | out: | ||
208 | if (sys_ptrace(PTRACE_KILL, child, NULL, NULL) == -1) | ||
209 | return 1; | ||
210 | |||
211 | waitpid(child, NULL, 0); | ||
212 | |||
213 | return exit_code; | ||
214 | } | ||
diff --git a/tools/testing/selftests/soft-dirty/Makefile b/tools/testing/selftests/soft-dirty/Makefile new file mode 100644 index 000000000000..a9cdc823d6e0 --- /dev/null +++ b/tools/testing/selftests/soft-dirty/Makefile | |||
@@ -0,0 +1,10 @@ | |||
1 | CFLAGS += -iquote../../../../include/uapi -Wall | ||
2 | soft-dirty: soft-dirty.c | ||
3 | |||
4 | all: soft-dirty | ||
5 | |||
6 | clean: | ||
7 | rm -f soft-dirty | ||
8 | |||
9 | run_tests: all | ||
10 | @./soft-dirty || echo "soft-dirty selftests: [FAIL]" | ||
diff --git a/tools/testing/selftests/soft-dirty/soft-dirty.c b/tools/testing/selftests/soft-dirty/soft-dirty.c new file mode 100644 index 000000000000..aba4f87f87f0 --- /dev/null +++ b/tools/testing/selftests/soft-dirty/soft-dirty.c | |||
@@ -0,0 +1,114 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <stdio.h> | ||
3 | #include <sys/mman.h> | ||
4 | #include <unistd.h> | ||
5 | #include <fcntl.h> | ||
6 | #include <sys/types.h> | ||
7 | |||
8 | typedef unsigned long long u64; | ||
9 | |||
10 | #define PME_PRESENT (1ULL << 63) | ||
11 | #define PME_SOFT_DIRTY (1Ull << 55) | ||
12 | |||
13 | #define PAGES_TO_TEST 3 | ||
14 | #ifndef PAGE_SIZE | ||
15 | #define PAGE_SIZE 4096 | ||
16 | #endif | ||
17 | |||
18 | static void get_pagemap2(char *mem, u64 *map) | ||
19 | { | ||
20 | int fd; | ||
21 | |||
22 | fd = open("/proc/self/pagemap2", O_RDONLY); | ||
23 | if (fd < 0) { | ||
24 | perror("Can't open pagemap2"); | ||
25 | exit(1); | ||
26 | } | ||
27 | |||
28 | lseek(fd, (unsigned long)mem / PAGE_SIZE * sizeof(u64), SEEK_SET); | ||
29 | read(fd, map, sizeof(u64) * PAGES_TO_TEST); | ||
30 | close(fd); | ||
31 | } | ||
32 | |||
33 | static inline char map_p(u64 map) | ||
34 | { | ||
35 | return map & PME_PRESENT ? 'p' : '-'; | ||
36 | } | ||
37 | |||
38 | static inline char map_sd(u64 map) | ||
39 | { | ||
40 | return map & PME_SOFT_DIRTY ? 'd' : '-'; | ||
41 | } | ||
42 | |||
43 | static int check_pte(int step, int page, u64 *map, u64 want) | ||
44 | { | ||
45 | if ((map[page] & want) != want) { | ||
46 | printf("Step %d Page %d has %c%c, want %c%c\n", | ||
47 | step, page, | ||
48 | map_p(map[page]), map_sd(map[page]), | ||
49 | map_p(want), map_sd(want)); | ||
50 | return 1; | ||
51 | } | ||
52 | |||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | static void clear_refs(void) | ||
57 | { | ||
58 | int fd; | ||
59 | char *v = "4"; | ||
60 | |||
61 | fd = open("/proc/self/clear_refs", O_WRONLY); | ||
62 | if (write(fd, v, 3) < 3) { | ||
63 | perror("Can't clear soft-dirty bit"); | ||
64 | exit(1); | ||
65 | } | ||
66 | close(fd); | ||
67 | } | ||
68 | |||
69 | int main(void) | ||
70 | { | ||
71 | char *mem, x; | ||
72 | u64 map[PAGES_TO_TEST]; | ||
73 | |||
74 | mem = mmap(NULL, PAGES_TO_TEST * PAGE_SIZE, | ||
75 | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, 0, 0); | ||
76 | |||
77 | x = mem[0]; | ||
78 | mem[2 * PAGE_SIZE] = 'c'; | ||
79 | get_pagemap2(mem, map); | ||
80 | |||
81 | if (check_pte(1, 0, map, PME_PRESENT)) | ||
82 | return 1; | ||
83 | if (check_pte(1, 1, map, 0)) | ||
84 | return 1; | ||
85 | if (check_pte(1, 2, map, PME_PRESENT | PME_SOFT_DIRTY)) | ||
86 | return 1; | ||
87 | |||
88 | clear_refs(); | ||
89 | get_pagemap2(mem, map); | ||
90 | |||
91 | if (check_pte(2, 0, map, PME_PRESENT)) | ||
92 | return 1; | ||
93 | if (check_pte(2, 1, map, 0)) | ||
94 | return 1; | ||
95 | if (check_pte(2, 2, map, PME_PRESENT)) | ||
96 | return 1; | ||
97 | |||
98 | mem[0] = 'a'; | ||
99 | mem[PAGE_SIZE] = 'b'; | ||
100 | x = mem[2 * PAGE_SIZE]; | ||
101 | get_pagemap2(mem, map); | ||
102 | |||
103 | if (check_pte(3, 0, map, PME_PRESENT | PME_SOFT_DIRTY)) | ||
104 | return 1; | ||
105 | if (check_pte(3, 1, map, PME_PRESENT | PME_SOFT_DIRTY)) | ||
106 | return 1; | ||
107 | if (check_pte(3, 2, map, PME_PRESENT)) | ||
108 | return 1; | ||
109 | |||
110 | (void)x; /* gcc warn */ | ||
111 | |||
112 | printf("PASS\n"); | ||
113 | return 0; | ||
114 | } | ||