Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / perf_events / remove_on_exec.c
blob5814611a1dc72e91dcc2eea8bbd5de8a7e8c98f3
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Test for remove_on_exec.
5 * Copyright (C) 2021, Google LLC.
6 */
8 #define _GNU_SOURCE
10 /* We need the latest siginfo from the kernel repo. */
11 #include <sys/types.h>
12 #include <asm/siginfo.h>
13 #define __have_siginfo_t 1
14 #define __have_sigval_t 1
15 #define __have_sigevent_t 1
16 #define __siginfo_t_defined
17 #define __sigval_t_defined
18 #define __sigevent_t_defined
19 #define _BITS_SIGINFO_CONSTS_H 1
20 #define _BITS_SIGEVENT_CONSTS_H 1
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <linux/perf_event.h>
27 #include <pthread.h>
28 #include <signal.h>
29 #include <sys/ioctl.h>
30 #include <sys/syscall.h>
31 #include <unistd.h>
33 #include "../kselftest_harness.h"
35 static volatile int signal_count;
37 static struct perf_event_attr make_event_attr(void)
39 struct perf_event_attr attr = {
40 .type = PERF_TYPE_HARDWARE,
41 .size = sizeof(attr),
42 .config = PERF_COUNT_HW_INSTRUCTIONS,
43 .sample_period = 1000,
44 .exclude_kernel = 1,
45 .exclude_hv = 1,
46 .disabled = 1,
47 .inherit = 1,
49 * Children normally retain their inherited event on exec; with
50 * remove_on_exec, we'll remove their event, but the parent and
51 * any other non-exec'd children will keep their events.
53 .remove_on_exec = 1,
54 .sigtrap = 1,
56 return attr;
59 static void sigtrap_handler(int signum, siginfo_t *info, void *ucontext)
61 if (info->si_code != TRAP_PERF) {
62 fprintf(stderr, "%s: unexpected si_code %d\n", __func__, info->si_code);
63 return;
66 signal_count++;
69 FIXTURE(remove_on_exec)
71 struct sigaction oldact;
72 int fd;
75 FIXTURE_SETUP(remove_on_exec)
77 struct perf_event_attr attr = make_event_attr();
78 struct sigaction action = {};
80 signal_count = 0;
82 /* Initialize sigtrap handler. */
83 action.sa_flags = SA_SIGINFO | SA_NODEFER;
84 action.sa_sigaction = sigtrap_handler;
85 sigemptyset(&action.sa_mask);
86 ASSERT_EQ(sigaction(SIGTRAP, &action, &self->oldact), 0);
88 /* Initialize perf event. */
89 self->fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, PERF_FLAG_FD_CLOEXEC);
90 ASSERT_NE(self->fd, -1);
93 FIXTURE_TEARDOWN(remove_on_exec)
95 close(self->fd);
96 sigaction(SIGTRAP, &self->oldact, NULL);
99 /* Verify event propagates to fork'd child. */
100 TEST_F(remove_on_exec, fork_only)
102 int status;
103 pid_t pid = fork();
105 if (pid == 0) {
106 ASSERT_EQ(signal_count, 0);
107 ASSERT_EQ(ioctl(self->fd, PERF_EVENT_IOC_ENABLE, 0), 0);
108 while (!signal_count);
109 _exit(42);
112 while (!signal_count); /* Child enables event. */
113 EXPECT_EQ(waitpid(pid, &status, 0), pid);
114 EXPECT_EQ(WEXITSTATUS(status), 42);
118 * Verify that event does _not_ propagate to fork+exec'd child; event enabled
119 * after fork+exec.
121 TEST_F(remove_on_exec, fork_exec_then_enable)
123 pid_t pid_exec, pid_only_fork;
124 int pipefd[2];
125 int tmp;
128 * Non-exec child, to ensure exec does not affect inherited events of
129 * other children.
131 pid_only_fork = fork();
132 if (pid_only_fork == 0) {
133 /* Block until parent enables event. */
134 while (!signal_count);
135 _exit(42);
138 ASSERT_NE(pipe(pipefd), -1);
139 pid_exec = fork();
140 if (pid_exec == 0) {
141 ASSERT_NE(dup2(pipefd[1], STDOUT_FILENO), -1);
142 close(pipefd[0]);
143 execl("/proc/self/exe", "exec_child", NULL);
144 _exit((perror("exec failed"), 1));
146 close(pipefd[1]);
148 ASSERT_EQ(waitpid(pid_exec, &tmp, WNOHANG), 0); /* Child is running. */
149 /* Wait for exec'd child to start spinning. */
150 EXPECT_EQ(read(pipefd[0], &tmp, sizeof(int)), sizeof(int));
151 EXPECT_EQ(tmp, 42);
152 close(pipefd[0]);
153 /* Now we can enable the event, knowing the child is doing work. */
154 EXPECT_EQ(ioctl(self->fd, PERF_EVENT_IOC_ENABLE, 0), 0);
155 /* If the event propagated to the exec'd child, it will exit normally... */
156 usleep(100000); /* ... give time for event to trigger (in case of bug). */
157 EXPECT_EQ(waitpid(pid_exec, &tmp, WNOHANG), 0); /* Should still be running. */
158 EXPECT_EQ(kill(pid_exec, SIGKILL), 0);
160 /* Verify removal from child did not affect this task's event. */
161 tmp = signal_count;
162 while (signal_count == tmp); /* Should not hang! */
163 /* Nor should it have affected the first child. */
164 EXPECT_EQ(waitpid(pid_only_fork, &tmp, 0), pid_only_fork);
165 EXPECT_EQ(WEXITSTATUS(tmp), 42);
169 * Verify that event does _not_ propagate to fork+exec'd child; event enabled
170 * before fork+exec.
172 TEST_F(remove_on_exec, enable_then_fork_exec)
174 pid_t pid_exec;
175 int tmp;
177 EXPECT_EQ(ioctl(self->fd, PERF_EVENT_IOC_ENABLE, 0), 0);
179 pid_exec = fork();
180 if (pid_exec == 0) {
181 execl("/proc/self/exe", "exec_child", NULL);
182 _exit((perror("exec failed"), 1));
186 * The child may exit abnormally at any time if the event propagated and
187 * a SIGTRAP is sent before the handler was set up.
189 usleep(100000); /* ... give time for event to trigger (in case of bug). */
190 EXPECT_EQ(waitpid(pid_exec, &tmp, WNOHANG), 0); /* Should still be running. */
191 EXPECT_EQ(kill(pid_exec, SIGKILL), 0);
193 /* Verify removal from child did not affect this task's event. */
194 tmp = signal_count;
195 while (signal_count == tmp); /* Should not hang! */
198 TEST_F(remove_on_exec, exec_stress)
200 pid_t pids[30];
201 int i, tmp;
203 for (i = 0; i < sizeof(pids) / sizeof(pids[0]); i++) {
204 pids[i] = fork();
205 if (pids[i] == 0) {
206 execl("/proc/self/exe", "exec_child", NULL);
207 _exit((perror("exec failed"), 1));
210 /* Some forked with event disabled, rest with enabled. */
211 if (i > 10)
212 EXPECT_EQ(ioctl(self->fd, PERF_EVENT_IOC_ENABLE, 0), 0);
215 usleep(100000); /* ... give time for event to trigger (in case of bug). */
217 for (i = 0; i < sizeof(pids) / sizeof(pids[0]); i++) {
218 /* All children should still be running. */
219 EXPECT_EQ(waitpid(pids[i], &tmp, WNOHANG), 0);
220 EXPECT_EQ(kill(pids[i], SIGKILL), 0);
223 /* Verify event is still alive. */
224 tmp = signal_count;
225 while (signal_count == tmp);
228 /* For exec'd child. */
229 static void exec_child(void)
231 struct sigaction action = {};
232 const int val = 42;
234 /* Set up sigtrap handler in case we erroneously receive a trap. */
235 action.sa_flags = SA_SIGINFO | SA_NODEFER;
236 action.sa_sigaction = sigtrap_handler;
237 sigemptyset(&action.sa_mask);
238 if (sigaction(SIGTRAP, &action, NULL))
239 _exit((perror("sigaction failed"), 1));
241 /* Signal parent that we're starting to spin. */
242 if (write(STDOUT_FILENO, &val, sizeof(int)) == -1)
243 _exit((perror("write failed"), 1));
245 /* Should hang here until killed. */
246 while (!signal_count);
249 #define main test_main
250 TEST_HARNESS_MAIN
251 #undef main
252 int main(int argc, char *argv[])
254 if (!strcmp(argv[0], "exec_child")) {
255 exec_child();
256 return 1;
259 return test_main(argc, argv);