1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3
4 #include <test_progs.h>
5 #include "test_perf_skip.skel.h"
6 #include <linux/compiler.h>
7 #include <linux/hw_breakpoint.h>
8 #include <sys/mman.h>
9
10 #ifndef TRAP_PERF
11 #define TRAP_PERF 6
12 #endif
13
14 int sigio_count, sigtrap_count;
15
handle_sigio(int sig __always_unused)16 static void handle_sigio(int sig __always_unused)
17 {
18 ++sigio_count;
19 }
20
handle_sigtrap(int signum __always_unused,siginfo_t * info,void * ucontext __always_unused)21 static void handle_sigtrap(int signum __always_unused,
22 siginfo_t *info,
23 void *ucontext __always_unused)
24 {
25 ASSERT_EQ(info->si_code, TRAP_PERF, "si_code");
26 ++sigtrap_count;
27 }
28
test_function(void)29 static noinline int test_function(void)
30 {
31 asm volatile ("");
32 return 0;
33 }
34
serial_test_perf_skip(void)35 void serial_test_perf_skip(void)
36 {
37 struct sigaction action = {};
38 struct sigaction previous_sigtrap;
39 sighandler_t previous_sigio = SIG_ERR;
40 struct test_perf_skip *skel = NULL;
41 struct perf_event_attr attr = {};
42 int perf_fd = -1;
43 int err;
44 struct f_owner_ex owner;
45 struct bpf_link *prog_link = NULL;
46
47 action.sa_flags = SA_SIGINFO | SA_NODEFER;
48 action.sa_sigaction = handle_sigtrap;
49 sigemptyset(&action.sa_mask);
50 if (!ASSERT_OK(sigaction(SIGTRAP, &action, &previous_sigtrap), "sigaction"))
51 return;
52
53 previous_sigio = signal(SIGIO, handle_sigio);
54 if (!ASSERT_NEQ(previous_sigio, SIG_ERR, "signal"))
55 goto cleanup;
56
57 skel = test_perf_skip__open_and_load();
58 if (!ASSERT_OK_PTR(skel, "skel_load"))
59 goto cleanup;
60
61 attr.type = PERF_TYPE_BREAKPOINT;
62 attr.size = sizeof(attr);
63 attr.bp_type = HW_BREAKPOINT_X;
64 attr.bp_addr = (uintptr_t)test_function;
65 attr.bp_len = sizeof(long);
66 attr.sample_period = 1;
67 attr.sample_type = PERF_SAMPLE_IP;
68 attr.pinned = 1;
69 attr.exclude_kernel = 1;
70 attr.exclude_hv = 1;
71 attr.precise_ip = 3;
72 attr.sigtrap = 1;
73 attr.remove_on_exec = 1;
74
75 perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
76 if (perf_fd < 0 && (errno == ENOENT || errno == EOPNOTSUPP)) {
77 printf("SKIP:no PERF_TYPE_BREAKPOINT/HW_BREAKPOINT_X\n");
78 test__skip();
79 goto cleanup;
80 }
81 if (!ASSERT_OK(perf_fd < 0, "perf_event_open"))
82 goto cleanup;
83
84 /* Configure the perf event to signal on sample. */
85 err = fcntl(perf_fd, F_SETFL, O_ASYNC);
86 if (!ASSERT_OK(err, "fcntl(F_SETFL, O_ASYNC)"))
87 goto cleanup;
88
89 owner.type = F_OWNER_TID;
90 owner.pid = syscall(__NR_gettid);
91 err = fcntl(perf_fd, F_SETOWN_EX, &owner);
92 if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)"))
93 goto cleanup;
94
95 /* Allow at most one sample. A sample rejected by bpf should
96 * not count against this.
97 */
98 err = ioctl(perf_fd, PERF_EVENT_IOC_REFRESH, 1);
99 if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_REFRESH)"))
100 goto cleanup;
101
102 prog_link = bpf_program__attach_perf_event(skel->progs.handler, perf_fd);
103 if (!ASSERT_OK_PTR(prog_link, "bpf_program__attach_perf_event"))
104 goto cleanup;
105
106 /* Configure the bpf program to suppress the sample. */
107 skel->bss->ip = (uintptr_t)test_function;
108 test_function();
109
110 ASSERT_EQ(sigio_count, 0, "sigio_count");
111 ASSERT_EQ(sigtrap_count, 0, "sigtrap_count");
112
113 /* Configure the bpf program to allow the sample. */
114 skel->bss->ip = 0;
115 test_function();
116
117 ASSERT_EQ(sigio_count, 1, "sigio_count");
118 ASSERT_EQ(sigtrap_count, 1, "sigtrap_count");
119
120 /* Test that the sample above is the only one allowed (by perf, not
121 * by bpf)
122 */
123 test_function();
124
125 ASSERT_EQ(sigio_count, 1, "sigio_count");
126 ASSERT_EQ(sigtrap_count, 1, "sigtrap_count");
127
128 cleanup:
129 bpf_link__destroy(prog_link);
130 if (perf_fd >= 0)
131 close(perf_fd);
132 test_perf_skip__destroy(skel);
133
134 if (previous_sigio != SIG_ERR)
135 signal(SIGIO, previous_sigio);
136 sigaction(SIGTRAP, &previous_sigtrap, NULL);
137 }
138