Skip to content

Commit 8a20655

Browse files
mykyta5kkdwvd
authored andcommitted
selftests/bpf: Add tests for sleepable tracepoint programs
Cover all three sleepable tracepoint types (tp_btf.s, raw_tp.s, tp.s) and sys_exit (via bpf_task_pt_regs) with functional tests using bpf_copy_from_user() on getcwd. Verify alias and bare SEC variants, bpf_prog_test_run_raw_tp() with BPF_F_TEST_RUN_ON_CPU rejection, attach-time rejection on non-faultable tracepoints, and load-time rejection for sleepable tp_btf on non-faultable tracepoints. Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com> Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/bpf/20260422-sleepable_tracepoints-v13-6-99005dff21ef@meta.com Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
1 parent 0cd420a commit 8a20655

4 files changed

Lines changed: 287 additions & 2 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <test_progs.h>
5+
#include <unistd.h>
6+
#include "test_sleepable_tracepoints.skel.h"
7+
#include "test_sleepable_tracepoints_fail.skel.h"
8+
9+
static void run_test(struct test_sleepable_tracepoints *skel)
10+
{
11+
char buf[PATH_MAX] = "/";
12+
13+
skel->bss->target_pid = getpid();
14+
skel->bss->prog_triggered = 0;
15+
skel->bss->err = 0;
16+
skel->bss->copied_byte = 0;
17+
18+
syscall(__NR_getcwd, buf, sizeof(buf));
19+
20+
ASSERT_EQ(skel->bss->prog_triggered, 1, "prog_triggered");
21+
ASSERT_EQ(skel->bss->err, 0, "err");
22+
ASSERT_EQ(skel->bss->copied_byte, '/', "copied_byte");
23+
}
24+
25+
static void run_auto_attach_test(struct bpf_program *prog,
26+
struct test_sleepable_tracepoints *skel)
27+
{
28+
struct bpf_link *link;
29+
30+
link = bpf_program__attach(prog);
31+
if (!ASSERT_OK_PTR(link, "prog_attach"))
32+
return;
33+
34+
run_test(skel);
35+
bpf_link__destroy(link);
36+
}
37+
38+
static void test_attach_only(struct bpf_program *prog)
39+
{
40+
struct bpf_link *link;
41+
42+
link = bpf_program__attach(prog);
43+
if (ASSERT_OK_PTR(link, "attach"))
44+
bpf_link__destroy(link);
45+
}
46+
47+
static void test_attach_reject(struct bpf_program *prog)
48+
{
49+
struct bpf_link *link;
50+
51+
link = bpf_program__attach(prog);
52+
if (!ASSERT_ERR_PTR(link, "attach_should_fail"))
53+
bpf_link__destroy(link);
54+
}
55+
56+
static void test_raw_tp_bare(struct test_sleepable_tracepoints *skel)
57+
{
58+
struct bpf_link *link;
59+
60+
link = bpf_program__attach_raw_tracepoint(skel->progs.handle_raw_tp_bare,
61+
"sys_enter");
62+
if (ASSERT_OK_PTR(link, "attach"))
63+
bpf_link__destroy(link);
64+
}
65+
66+
static void test_tp_bare(struct test_sleepable_tracepoints *skel)
67+
{
68+
struct bpf_link *link;
69+
70+
link = bpf_program__attach_tracepoint(skel->progs.handle_tp_bare,
71+
"syscalls", "sys_enter_getcwd");
72+
if (ASSERT_OK_PTR(link, "attach"))
73+
bpf_link__destroy(link);
74+
}
75+
76+
static void test_test_run(struct test_sleepable_tracepoints *skel)
77+
{
78+
__u64 args[2] = {0x1234ULL, 0x5678ULL};
79+
LIBBPF_OPTS(bpf_test_run_opts, topts,
80+
.ctx_in = args,
81+
.ctx_size_in = sizeof(args),
82+
);
83+
int fd, err;
84+
85+
fd = bpf_program__fd(skel->progs.handle_test_run);
86+
err = bpf_prog_test_run_opts(fd, &topts);
87+
ASSERT_OK(err, "test_run");
88+
ASSERT_EQ(topts.retval, args[0] + args[1], "test_run_retval");
89+
}
90+
91+
static void test_test_run_on_cpu_reject(struct test_sleepable_tracepoints *skel)
92+
{
93+
__u64 args[2] = {};
94+
LIBBPF_OPTS(bpf_test_run_opts, topts,
95+
.ctx_in = args,
96+
.ctx_size_in = sizeof(args),
97+
.flags = BPF_F_TEST_RUN_ON_CPU,
98+
);
99+
int fd, err;
100+
101+
fd = bpf_program__fd(skel->progs.handle_test_run);
102+
err = bpf_prog_test_run_opts(fd, &topts);
103+
ASSERT_ERR(err, "test_run_on_cpu_reject");
104+
}
105+
106+
void test_sleepable_tracepoints(void)
107+
{
108+
struct test_sleepable_tracepoints *skel;
109+
110+
skel = test_sleepable_tracepoints__open_and_load();
111+
if (!ASSERT_OK_PTR(skel, "open_and_load"))
112+
return;
113+
114+
if (test__start_subtest("tp_btf"))
115+
run_auto_attach_test(skel->progs.handle_sys_enter_tp_btf, skel);
116+
if (test__start_subtest("raw_tp"))
117+
run_auto_attach_test(skel->progs.handle_sys_enter_raw_tp, skel);
118+
if (test__start_subtest("tracepoint"))
119+
run_auto_attach_test(skel->progs.handle_sys_enter_tp, skel);
120+
if (test__start_subtest("sys_exit"))
121+
run_auto_attach_test(skel->progs.handle_sys_exit_tp, skel);
122+
if (test__start_subtest("tracepoint_alias"))
123+
test_attach_only(skel->progs.handle_sys_enter_tp_alias);
124+
if (test__start_subtest("raw_tracepoint_alias"))
125+
test_attach_only(skel->progs.handle_sys_enter_raw_tp_alias);
126+
if (test__start_subtest("raw_tp_bare"))
127+
test_raw_tp_bare(skel);
128+
if (test__start_subtest("tp_bare"))
129+
test_tp_bare(skel);
130+
if (test__start_subtest("test_run"))
131+
test_test_run(skel);
132+
if (test__start_subtest("test_run_on_cpu_reject"))
133+
test_test_run_on_cpu_reject(skel);
134+
if (test__start_subtest("raw_tp_non_faultable"))
135+
test_attach_reject(skel->progs.handle_raw_tp_non_faultable);
136+
if (test__start_subtest("tp_non_syscall"))
137+
test_attach_reject(skel->progs.handle_tp_non_syscall);
138+
if (test__start_subtest("tp_btf_non_faultable_reject"))
139+
RUN_TESTS(test_sleepable_tracepoints_fail);
140+
141+
test_sleepable_tracepoints__destroy(skel);
142+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <vmlinux.h>
5+
#include <asm/unistd.h>
6+
#include <bpf/bpf_tracing.h>
7+
#include <bpf/bpf_core_read.h>
8+
#include <bpf/bpf_helpers.h>
9+
10+
char _license[] SEC("license") = "GPL";
11+
12+
int target_pid;
13+
int prog_triggered;
14+
long err;
15+
char copied_byte;
16+
17+
static int copy_getcwd_arg(char *ubuf)
18+
{
19+
err = bpf_copy_from_user(&copied_byte, sizeof(copied_byte), ubuf);
20+
if (err)
21+
return err;
22+
23+
prog_triggered = 1;
24+
return 0;
25+
}
26+
27+
SEC("tp_btf.s/sys_enter")
28+
int BPF_PROG(handle_sys_enter_tp_btf, struct pt_regs *regs, long id)
29+
{
30+
if ((bpf_get_current_pid_tgid() >> 32) != target_pid ||
31+
id != __NR_getcwd)
32+
return 0;
33+
34+
return copy_getcwd_arg((void *)PT_REGS_PARM1_SYSCALL(regs));
35+
}
36+
37+
SEC("raw_tp.s/sys_enter")
38+
int BPF_PROG(handle_sys_enter_raw_tp, struct pt_regs *regs, long id)
39+
{
40+
if ((bpf_get_current_pid_tgid() >> 32) != target_pid ||
41+
id != __NR_getcwd)
42+
return 0;
43+
44+
return copy_getcwd_arg((void *)PT_REGS_PARM1_CORE_SYSCALL(regs));
45+
}
46+
47+
SEC("tp.s/syscalls/sys_enter_getcwd")
48+
int handle_sys_enter_tp(struct syscall_trace_enter *args)
49+
{
50+
if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
51+
return 0;
52+
53+
return copy_getcwd_arg((void *)args->args[0]);
54+
}
55+
56+
SEC("tp.s/syscalls/sys_exit_getcwd")
57+
int handle_sys_exit_tp(struct syscall_trace_exit *args)
58+
{
59+
struct pt_regs *regs;
60+
61+
if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
62+
return 0;
63+
64+
regs = (struct pt_regs *)bpf_task_pt_regs(bpf_get_current_task_btf());
65+
return copy_getcwd_arg((void *)PT_REGS_PARM1_CORE_SYSCALL(regs));
66+
}
67+
68+
SEC("raw_tp.s")
69+
int BPF_PROG(handle_raw_tp_bare, struct pt_regs *regs, long id)
70+
{
71+
return 0;
72+
}
73+
74+
SEC("tp.s")
75+
int handle_tp_bare(void *ctx)
76+
{
77+
return 0;
78+
}
79+
80+
SEC("tracepoint.s/syscalls/sys_enter_getcwd")
81+
int handle_sys_enter_tp_alias(struct syscall_trace_enter *args)
82+
{
83+
return 0;
84+
}
85+
86+
SEC("raw_tracepoint.s/sys_enter")
87+
int BPF_PROG(handle_sys_enter_raw_tp_alias, struct pt_regs *regs, long id)
88+
{
89+
return 0;
90+
}
91+
92+
SEC("raw_tp.s/sys_enter")
93+
int BPF_PROG(handle_test_run, struct pt_regs *regs, long id)
94+
{
95+
if ((__u64)regs == 0x1234ULL && (__u64)id == 0x5678ULL)
96+
return (__u64)regs + (__u64)id;
97+
98+
return 0;
99+
}
100+
101+
SEC("raw_tp.s/sched_switch")
102+
int BPF_PROG(handle_raw_tp_non_faultable, bool preempt,
103+
struct task_struct *prev, struct task_struct *next)
104+
{
105+
return 0;
106+
}
107+
108+
SEC("tp.s/sched/sched_switch")
109+
int handle_tp_non_syscall(void *ctx)
110+
{
111+
return 0;
112+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include <bpf/bpf_helpers.h>
7+
#include "bpf_misc.h"
8+
9+
char _license[] SEC("license") = "GPL";
10+
11+
/* Sleepable program on a non-faultable tracepoint should fail to load */
12+
SEC("tp_btf.s/sched_switch")
13+
__failure __msg("Sleepable program cannot attach to non-faultable tracepoint")
14+
int BPF_PROG(handle_sched_switch, bool preempt,
15+
struct task_struct *prev, struct task_struct *next)
16+
{
17+
return 0;
18+
}

tools/testing/selftests/bpf/verifier/sleepable.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,20 @@
7676
.runs = -1,
7777
},
7878
{
79-
"sleepable raw tracepoint reject",
79+
"sleepable raw tracepoint accept",
80+
.insns = {
81+
BPF_MOV64_IMM(BPF_REG_0, 0),
82+
BPF_EXIT_INSN(),
83+
},
84+
.prog_type = BPF_PROG_TYPE_TRACING,
85+
.expected_attach_type = BPF_TRACE_RAW_TP,
86+
.kfunc = "sys_enter",
87+
.result = ACCEPT,
88+
.flags = BPF_F_SLEEPABLE,
89+
.runs = -1,
90+
},
91+
{
92+
"sleepable raw tracepoint reject non-faultable",
8093
.insns = {
8194
BPF_MOV64_IMM(BPF_REG_0, 0),
8295
BPF_EXIT_INSN(),
@@ -85,7 +98,7 @@
8598
.expected_attach_type = BPF_TRACE_RAW_TP,
8699
.kfunc = "sched_switch",
87100
.result = REJECT,
88-
.errstr = "Only fentry/fexit/fsession/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable",
101+
.errstr = "Sleepable program cannot attach to non-faultable tracepoint",
89102
.flags = BPF_F_SLEEPABLE,
90103
.runs = -1,
91104
},

0 commit comments

Comments
 (0)