Skip to content

Commit a265c9f

Browse files
khueyIngo Molnar
authored andcommitted
selftest/bpf: Test a perf BPF program that suppresses side effects
The test sets a hardware breakpoint and uses a BPF program to suppress the side effects of a perf event sample, including I/O availability signals, SIGTRAPs, and decrementing the event counter limit, if the IP matches the expected value. Then the function with the breakpoint is executed multiple times to test that all effects behave as expected. Signed-off-by: Kyle Huey <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Song Liu <[email protected]> Acked-by: Jiri Olsa <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent c4fcc7d commit a265c9f

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
16+
static void handle_sigio(int sig __always_unused)
17+
{
18+
++sigio_count;
19+
}
20+
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+
29+
static noinline int test_function(void)
30+
{
31+
asm volatile ("");
32+
return 0;
33+
}
34+
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "vmlinux.h"
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
6+
uintptr_t ip;
7+
8+
SEC("perf_event")
9+
int handler(struct bpf_perf_event_data *data)
10+
{
11+
/* Skip events that have the correct ip. */
12+
return ip != PT_REGS_IP(&data->regs);
13+
}
14+
15+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)