1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2016 Google, Inc.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "../kselftest.h"
void child(int cpu)
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);
if (sched_setaffinity(0, sizeof(set), &set) != 0) {
ksft_print_msg("sched_setaffinity() failed: %s\n",
strerror(errno));
_exit(1);
}
if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) {
ksft_print_msg("ptrace(PTRACE_TRACEME) failed: %s\n",
strerror(errno));
_exit(1);
}
if (raise(SIGSTOP) != 0) {
ksft_print_msg("raise(SIGSTOP) failed: %s\n", strerror(errno));
_exit(1);
}
_exit(0);
}
int run_test(int cpu)
{
int status;
pid_t pid = fork();
pid_t wpid;
if (pid < 0) {
ksft_print_msg("fork() failed: %s\n", strerror(errno));
return KSFT_FAIL;
}
if (pid == 0)
child(cpu);
wpid = waitpid(pid, &status, __WALL);
if (wpid != pid) {
ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
return KSFT_FAIL;
}
if (!WIFSTOPPED(status)) {
ksft_print_msg("child did not stop: %s\n", strerror(errno));
return KSFT_FAIL;
}
if (WSTOPSIG(status) != SIGSTOP) {
ksft_print_msg("child did not stop with SIGSTOP: %s\n",
strerror(errno));
return KSFT_FAIL;
}
if (ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) < 0) {
if (errno == EIO) {
ksft_print_msg(
"ptrace(PTRACE_SINGLESTEP) not supported on this architecture: %s\n",
strerror(errno));
return KSFT_SKIP;
}
ksft_print_msg("ptrace(PTRACE_SINGLESTEP) failed: %s\n",
strerror(errno));
return KSFT_FAIL;
}
wpid = waitpid(pid, &status, __WALL);
if (wpid != pid) {
ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
return KSFT_FAIL;
}
if (WIFEXITED(status)) {
ksft_print_msg("child did not single-step: %s\n",
strerror(errno));
return KSFT_FAIL;
}
if (!WIFSTOPPED(status)) {
ksft_print_msg("child did not stop: %s\n", strerror(errno));
return KSFT_FAIL;
}
if (WSTOPSIG(status) != SIGTRAP) {
ksft_print_msg("child did not stop with SIGTRAP: %s\n",
strerror(errno));
return KSFT_FAIL;
}
if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) {
ksft_print_msg("ptrace(PTRACE_CONT) failed: %s\n",
strerror(errno));
return KSFT_FAIL;
}
wpid = waitpid(pid, &status, __WALL);
if (wpid != pid) {
ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
return KSFT_FAIL;
}
if (!WIFEXITED(status)) {
ksft_print_msg("child did not exit after PTRACE_CONT: %s\n",
strerror(errno));
return KSFT_FAIL;
}
return KSFT_PASS;
}
void suspend(void)
{
int power_state_fd;
struct sigevent event = {};
int timerfd;
int err;
struct itimerspec spec = {};
if (getuid() != 0)
ksft_exit_skip("Please run the test as root - Exiting.\n");
power_state_fd = open("/sys/power/state", O_RDWR);
if (power_state_fd < 0)
ksft_exit_fail_msg(
"open(\"/sys/power/state\") failed %s)\n",
strerror(errno));
timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
if (timerfd < 0)
ksft_exit_fail_msg("timerfd_create() failed\n");
spec.it_value.tv_sec = 5;
err = timerfd_settime(timerfd, 0, &spec, NULL);
if (err < 0)
ksft_exit_fail_msg("timerfd_settime() failed\n");
if (write(power_state_fd, "mem", strlen("mem")) != strlen("mem"))
ksft_exit_fail_msg("Failed to enter Suspend state\n");
close(timerfd);
close(power_state_fd);
}
int main(int argc, char **argv)
{
int opt;
bool do_suspend = true;
bool succeeded = true;
unsigned int tests = 0;
cpu_set_t available_cpus;
int err;
int cpu;
ksft_print_header();
while ((opt = getopt(argc, argv, "n")) != -1) {
switch (opt) {
case 'n':
do_suspend = false;
break;
default:
printf("Usage: %s [-n]\n", argv[0]);
printf(" -n: do not trigger a suspend/resume cycle before the test\n");
return -1;
}
}
err = sched_getaffinity(0, sizeof(available_cpus), &available_cpus);
if (err < 0)
ksft_exit_fail_msg("sched_getaffinity() failed\n");
for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
if (!CPU_ISSET(cpu, &available_cpus))
continue;
tests++;
}
if (do_suspend)
suspend();
ksft_set_plan(tests);
for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
int test_success;
if (!CPU_ISSET(cpu, &available_cpus))
continue;
test_success = run_test(cpu);
switch (test_success) {
case KSFT_PASS:
ksft_test_result_pass("CPU %d\n", cpu);
break;
case KSFT_SKIP:
ksft_test_result_skip("CPU %d\n", cpu);
break;
case KSFT_FAIL:
ksft_test_result_fail("CPU %d\n", cpu);
succeeded = false;
break;
}
}
if (succeeded)
ksft_exit_pass();
else
ksft_exit_fail();
}
|