bpf: Add support for sleepable raw tracepoint programs
Subsystems (8)
v2 cover bpf: Add support for sleepable raw tracepoint programs
This series adds support for sleepable BPF programs attached to raw tracepoints (tp_btf). The motivation is to allow BPF programs on syscall tracepoints to use sleepable helpers such as bpf_copy_from_user(), enabling reliable user memory reads that can page-fault.
Currently, raw tracepoint BPF programs always run with RCU read lock held and preemption disabled, which prevents calling any helper that might sleep. Faultable tracepoints (__DECLARE_TRACE_SYSCALL) already run under rcu_tasks_trace protection in process context where sleeping is safe.
This series removes that restriction for faultable tracepoints:
Patch 1 adds an attach-time check to reject sleepable programs on non-faultable tracepoints (e.g., sched_switch) that may run in NMI or other non-sleepable contexts. This is a no-op until the verifier allows sleepable raw_tp programs.
Patch 2 modifies __bpf_trace_run() to support sleepable programs:
use migrate_disable() instead of rcu_read_lock() for sleepable programs, call might_fault() to annotate faultable context, and rely on the outer rcu_tasks_trace lock from the faultable tracepoint callback for program lifetime protection.
Patch 3 removes preempt_disable from the faultable tracepoint BPF callback wrapper, since preemption management is now handled per-program inside __bpf_trace_run().
Patch 4 allows BPF_TRACE_RAW_TP programs to be loaded as sleepable. All runtime infrastructure is in place at this point, ensuring no bisectability issues.
Patch 5 adds the tp_btf.s section handler in libbpf, following the existing pattern of fentry.s/fexit.s/lsm.s.
Patch 6 adds selftests covering both the positive case (sleepable program on sys_enter using bpf_copy_from_user() to read user memory) and the negative case (sleepable program rejected on sched_switch).
---
Changes in v2:
- Address AI review points - modified the order of the patches
- Link to v1: https://lore.kernel.org/bpf/[email protected]/
---
Mykyta Yatsenko (6):
bpf: Reject sleepable raw_tp programs on non-faultable tracepoints
bpf: Add sleepable execution path for raw tracepoint programs
bpf: Remove preempt_disable from faultable tracepoint BPF callbacks
bpf: Allow sleepable programs for BPF_TRACE_RAW_TP attach type
libbpf: Add tp_btf.s section handler for sleepable raw tracepoints selftests/bpf: Add tests for sleepable raw tracepoint programs
include/trace/bpf_probe.h | 2 - kernel/bpf/syscall.c | 5 ++ kernel/bpf/verifier.c | 3 +- kernel/trace/bpf_trace.c | 20 ++++++-- tools/lib/bpf/libbpf.c | 1 + .../selftests/bpf/prog_tests/sleepable_raw_tp.c | 56 ++++++++++++++++++++++ .../selftests/bpf/progs/test_sleepable_raw_tp.c | 43 +++++++++++++++++ .../bpf/progs/test_sleepable_raw_tp_fail.c | 16 +++++++ tools/testing/selftests/bpf/verifier/sleepable.c | 5 +- 9 files changed, 142 insertions(+), 9 deletions(-)
---
base-commit: f620af11c27b8ec9994a39fe968aa778112d1566
change-id: 20260216-sleepable_tracepoints-381ae1410550
Best regards,
Signature · 1 line
Mykyta Yatsenko <[email protected]>
PATCH v2 1/6 bpf: Reject sleepable raw_tp programs on non-faultable tracepoints
From: Mykyta Yatsenko <[email protected]>
Add an attach-time check in bpf_raw_tp_link_attach() to ensure that sleepable BPF programs can only attach to faultable tracepoints. Faultable tracepoints (e.g., sys_enter, sys_exit) are guaranteed to run in a context where sleeping is safe, using rcu_tasks_trace for protection. Non-faultable tracepoints may run in NMI or other non-sleepable contexts.
This complements the verifier-side change that allows BPF_TRACE_RAW_TP programs to be loaded as sleepable.
--- kernel/bpf/syscall.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/bpf/syscall.c +5 -0
@@ -4261,6 +4261,11 @@ static int bpf_raw_tp_link_attach(struct bpf_prog *prog,if (!btp)return -ENOENT ; +if (prog->sleepable && !tracepoint_is_faultable (btp->tp)) { + err = -EINVAL ; + goto out_put_btp; + } + link =kzalloc_obj (*link,GFP_USER );if (!link) { err = -ENOMEM ;
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v2 1/6] bpf: Reject sleepable raw_tp programs on non-faultable tracepoints
Quoted 10 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Add an attach-time check in
bpf_raw_tp_link_attach()to ensure that sleepable BPF programs can only attach to faultable tracepoints. Faultable tracepoints (e.g.,sys_enter,sys_exit) are guaranteed to run in a context where sleeping is safe, usingrcu_tasks_tracefor protection. Non-faultable tracepoints may run in NMI or other non-sleepable contexts.
Quoted 5 lines
This complements the verifier-side change that allows
BPF_TRACE_RAW_TPprograms to be loaded as sleepable.Signed-off-by Mykyta Yatsenko <[email protected]>---
Quoted 1 line
[...]
PATCH v2 2/6 bpf: Add sleepable execution path for raw tracepoint programs
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Modify __bpf_trace_run() to support both sleepable and non-sleepable BPF programs. When the program is sleepable:
- Skip cant_sleep() and instead call might_fault() to annotate the faultable context
- Use migrate_disable()/migrate_enable() instead of rcu_read_lock()/rcu_read_unlock() to allow sleeping while still protecting percpu data access
- The outer rcu_tasks_trace lock is already held by the faultable tracepoint callback (__DECLARE_TRACE_SYSCALL), providing lifetime protection for the BPF program
For non-sleepable programs, behavior is unchanged: cant_sleep() check, rcu_read_lock() protection.
This allows multiple BPF programs with different sleepable settings to coexist on the same faultable tracepoint, since __bpf_trace_run() is invoked per-link.
--- kernel/trace/bpf_trace.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
kernel/trace/bpf_trace.c +17 -3
@@ -2076,7 +2076,7 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)struct bpf_run_ctx *old_run_ctx;struct bpf_trace_run_ctx run_ctx; -rcu_read_lock_dont_migrate (); +migrate_disable ();if (unlikely (!bpf_prog_get_recursion_context (prog))) {bpf_prog_inc_misses_counter (prog); goto out;
@@ -2085,12 +2085,26 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) run_ctx.bpf_cookie = link->cookie; old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx); - (void )bpf_prog_run (prog, args); +if (prog->sleepable) { +might_fault (); + (void )bpf_prog_run (prog, args); + }else { +/* +* Non-sleepable programs may run in the faultable context, +* do cant_sleep() only if program is non-sleepable and context +* is non-faultable. +*/ +if (!link->link.sleepable) +cant_sleep (); +rcu_read_lock (); + (void )bpf_prog_run (prog, args); +rcu_read_unlock (); + }bpf_reset_run_ctx (old_run_ctx); out:bpf_prog_put_recursion_context (prog); -rcu_read_unlock_migrate (); +migrate_enable (); }#define UNPACK (...) __VA_ARGS__
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH bpf-next v2 2/6] bpf: Add sleepable execution path for raw tracepoint programs
At this commit, __BPF_DECLARE_TRACE_SYSCALL in include/trace/bpf_probe.h still wraps the BPF callback in preempt_disable_notrace() / preempt_enable_notrace():
Quoted 4 lines
kernel/trace/bpf_trace.c +0
-0
[ ... ]
kernel/trace/bpf_trace.c void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) · line 2090
Quoted 8 lines
kernel/trace/bpf_trace.c +3
-1@@ -2085,12 +2085,26 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) run_ctx.bpf_cookie = link->cookie; old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx); - (void )bpf_prog_run (prog, args); +if (prog->sleepable) { +might_fault (); + (void )bpf_prog_run (prog, args);
At this commit, __BPF_DECLARE_TRACE_SYSCALL in include/trace/bpf_probe.h still wraps the BPF callback in preempt_disable_notrace() / preempt_enable_notrace():
include/trace/bpf_probe.h:__BPF_DECLARE_TRACE_SYSCALL() {
might_fault();
preempt_disable_notrace();
bpf_trace_run##x(__data, ...);
preempt_enable_notrace();
}Since __bpf_trace_run() is inlined into bpf_trace_run##x(), which is called between the preempt_disable_notrace() and preempt_enable_notrace(), the might_fault() here will execute with preempt_count > 0, producing a warning on CONFIG_DEBUG_ATOMIC_SLEEP kernels.
More importantly, if the sleepable program calls a helper like bpf_copy_from_user(), the page fault handler will see in_atomic() and return -EFAULT for any non-resident page instead of faulting it in, defeating the purpose of sleepable programs.
The v1 RFC series included a separate patch to remove preempt_disable_notrace() from __BPF_DECLARE_TRACE_SYSCALL. Should that change be folded into this commit so the sleepable path works at each bisect point?
kernel/trace/bpf_trace.c void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) · line 2102
Quoted 12 lines
kernel/trace/bpf_trace.c +12
-0+ }else { +/* +* Non-sleepable programs may run in the faultable context, +* do cant_sleep() only if program is non-sleepable and context +* is non-faultable. +*/ +if (!link->link.sleepable) +cant_sleep (); +rcu_read_lock (); + (void )bpf_prog_run (prog, args); +rcu_read_unlock (); + }
[ ... ]
The commit message states:
Quoted 2 lines
kernel/trace/bpf_trace.c +0
-0For non-sleepable programs, behavior is unchanged: cant_sleep() check,
rcu_read_lock()protection.
The parent commit does not have a cant_sleep() call in __bpf_trace_run() -- it uses rcu_read_lock_dont_migrate(). The conditional cant_sleep() is new in this commit. Could this sentence be updated to reflect the actual change?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22406522761
AI-authorship-score: low
AI-authorship-explanation: Natural, domain-specific language with a small inaccuracy in the commit message that is more characteristic of human writing; code shows deep subsystem expertise.
issues-found: 2
issue-severity-score: medium
issue-severity-explanation: Sleepable BPF programs on faultable tracepoints cannot actually sleep because __BPF_DECLARE_TRACE_SYSCALL still wraps execution in preempt_disable_notrace; this defeats the purpose of the feature and produces debug warnings, but does not crash the system.
Re: [PATCH bpf-next v2 2/6] bpf: Add sleepable execution path for raw tracepoint programs
Quoted 39 lines · On Wed, 25 Feb 2026 at 18:14, <[email protected]> wrote:
Quoted 5 lines
kernel/trace/bpf_trace.c +0
-0[ ... ]
kernel/trace/bpf_trace.c void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) · line 2090
Quoted 8 lines
kernel/trace/bpf_trace.c +3
-1@@ -2085,12 +2085,26 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) run_ctx.bpf_cookie = link->cookie; old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx); - (void )bpf_prog_run (prog, args); +if (prog->sleepable) { +might_fault (); + (void )bpf_prog_run (prog, args);At this commit,
__BPF_DECLARE_TRACE_SYSCALLininclude/trace/bpf_probe.hstill wraps the BPF callback inpreempt_disable_notrace()/preempt_enable_notrace():include/trace/bpf_probe.h:__BPF_DECLARE_TRACE_SYSCALL() { might_fault(); preempt_disable_notrace(); bpf_trace_run##x(__data, ...); preempt_enable_notrace(); }Since
__bpf_trace_run()is inlined into bpf_trace_run##x(), which is called between thepreempt_disable_notrace()andpreempt_enable_notrace(), themight_fault()here will execute withpreempt_count> 0, producing a warning onCONFIG_DEBUG_ATOMIC_SLEEPkernels.More importantly, if the sleepable program calls a helper like
bpf_copy_from_user(), the page fault handler will seein_atomic()and return-EFAULTfor any non-resident page instead of faulting it in, defeating the purpose of sleepable programs.
Quoted 4 lines
The v1 RFC series included a separate patch to remove
preempt_disable_notrace()from__BPF_DECLARE_TRACE_SYSCALL. Should that change be folded into this commit so the sleepable path works at each bisect point?
But sleepable programs are not yet permitted, so I think this is not needed for bisection-friendliness.
Quoted 18 lines
kernel/trace/bpf_trace.c void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) · line 2102
Quoted 13 lines
kernel/trace/bpf_trace.c +12
-0+ }else { +/* +* Non-sleepable programs may run in the faultable context, +* do cant_sleep() only if program is non-sleepable and context +* is non-faultable. +*/ +if (!link->link.sleepable) +cant_sleep (); +rcu_read_lock (); + (void )bpf_prog_run (prog, args); +rcu_read_unlock (); + }[ ... ]
The commit message states:
Quoted 7 lines
Quoted 2 lines
kernel/trace/bpf_trace.c +0
-0For non-sleepable programs, behavior is unchanged: cant_sleep() check,
rcu_read_lock()protection.The parent commit does not have a
cant_sleep()call in__bpf_trace_run()-- it usesrcu_read_lock_dont_migrate(). The conditionalcant_sleep()is new in this commit. Could this sentence be updated to reflect the actual change?
This seems worth adjusting.
Quoted 2 lines
Other than that LGTM.
Quoted 1 line
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 2/6] bpf: Add sleepable execution path for raw tracepoint programs
Quoted 70 lines · Kumar Kartikeya Dwivedi
Quoted 39 lines · On Wed, 25 Feb 2026 at 18:14, <[email protected]> wrote:
Quoted 5 lines
kernel/trace/bpf_trace.c +0
-0[ ... ]
kernel/trace/bpf_trace.c void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) · line 2090
Quoted 8 lines
kernel/trace/bpf_trace.c +3
-1@@ -2085,12 +2085,26 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) run_ctx.bpf_cookie = link->cookie; old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx); - (void )bpf_prog_run (prog, args); +if (prog->sleepable) { +might_fault (); + (void )bpf_prog_run (prog, args);At this commit,
__BPF_DECLARE_TRACE_SYSCALLininclude/trace/bpf_probe.hstill wraps the BPF callback inpreempt_disable_notrace()/preempt_enable_notrace():include/trace/bpf_probe.h:__BPF_DECLARE_TRACE_SYSCALL() { might_fault(); preempt_disable_notrace(); bpf_trace_run##x(__data, ...); preempt_enable_notrace(); }Since
__bpf_trace_run()is inlined into bpf_trace_run##x(), which is called between thepreempt_disable_notrace()andpreempt_enable_notrace(), themight_fault()here will execute withpreempt_count> 0, producing a warning onCONFIG_DEBUG_ATOMIC_SLEEPkernels.More importantly, if the sleepable program calls a helper like
bpf_copy_from_user(), the page fault handler will seein_atomic()and return-EFAULTfor any non-resident page instead of faulting it in, defeating the purpose of sleepable programs.Quoted 4 lines
The v1 RFC series included a separate patch to remove
preempt_disable_notrace()from__BPF_DECLARE_TRACE_SYSCALL. Should that change be folded into this commit so the sleepable path works at each bisect point?But sleepable programs are not yet permitted, so I think this is not needed for bisection-friendliness.
Quoted 21 lines
kernel/trace/bpf_trace.c void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) · line 2102
Quoted 13 lines
kernel/trace/bpf_trace.c +12
-0+ }else { +/* +* Non-sleepable programs may run in the faultable context, +* do cant_sleep() only if program is non-sleepable and context +* is non-faultable. +*/ +if (!link->link.sleepable) +cant_sleep (); +rcu_read_lock (); + (void )bpf_prog_run (prog, args); +rcu_read_unlock (); + }[ ... ]
The commit message states:
Quoted 2 lines
kernel/trace/bpf_trace.c +0
-0For non-sleepable programs, behavior is unchanged: cant_sleep() check,
rcu_read_lock()protection.
Quoted 6 lines
Quoted 4 lines
The parent commit does not have a
cant_sleep()call in__bpf_trace_run()-- it usesrcu_read_lock_dont_migrate(). The conditionalcant_sleep()is new in this commit. Could this sentence be updated to reflect the actual change?This seems worth adjusting.
Actually, with the extra cant_sleep() check, we get this splat when running normal raw_tp tests.
E.g.
...
[ 295.211949] BUG: assuming atomic context at kernel/trace/bpf_trace.c:2098 [ 295.212166] in_atomic(): 0, irqs_disabled(): 0, pid: 587, name: test_progs [ 295.212316] 3 locks held by test_progs/587: ... [ 295.213044] Call Trace: [ 295.213046] <TASK> [ 295.213048] dump_stack_lvl+0x54/0x70 [ 295.213055] __cant_sleep+0xb7/0xd0 [ 295.213060] bpf_trace_run1+0xce/0x340 [ 295.213072] bpf_testmod_test_read+0x433/0x5e0 [bpf_testmod] [ 295.213080] ? srso_return_thunk+0x5/0x5f [ 295.213093] kernfs_fop_read_iter+0x166/0x220
...
It means if the tracepoint is not marked faultable but is invoked in a non-atomic context, we will get a warning.
It might be better to just drop this, since I don't think this adds much value.
Quoted 9 lines
Quoted 3 lines
Other than that LGTM.
Quoted 1 line
[...]
Yonghong Song
reviewerRe: [PATCH bpf-next v2 2/6] bpf: Add sleepable execution path for raw tracepoint programs
Quoted 85 lines · Kumar Kartikeya Dwivedi
Quoted 60 lines · Kumar Kartikeya Dwivedi
Quoted 36 lines · On Wed, 25 Feb 2026 at 18:14, <[email protected]> wrote:
Quoted 4 lines
kernel/trace/bpf_trace.c +0
-0[ ... ]
kernel/trace/bpf_trace.c void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) · line 2090
Quoted 8 lines
kernel/trace/bpf_trace.c +3
-1@@ -2085,12 +2085,26 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) run_ctx.bpf_cookie = link->cookie; old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx); - (void )bpf_prog_run (prog, args); +if (prog->sleepable) { +might_fault (); + (void )bpf_prog_run (prog, args);At this commit,
__BPF_DECLARE_TRACE_SYSCALLininclude/trace/bpf_probe.hstill wraps the BPF callback inpreempt_disable_notrace()/preempt_enable_notrace():include/trace/bpf_probe.h:__BPF_DECLARE_TRACE_SYSCALL() { might_fault(); preempt_disable_notrace(); bpf_trace_run##x(__data, ...); preempt_enable_notrace(); }Since
__bpf_trace_run()is inlined into bpf_trace_run##x(), which is called between thepreempt_disable_notrace()andpreempt_enable_notrace(), themight_fault()here will execute withpreempt_count> 0, producing a warning onCONFIG_DEBUG_ATOMIC_SLEEPkernels.More importantly, if the sleepable program calls a helper like
bpf_copy_from_user(), the page fault handler will seein_atomic()and return-EFAULTfor any non-resident page instead of faulting it in, defeating the purpose of sleepable programs.Quoted 4 lines
The v1 RFC series included a separate patch to remove
preempt_disable_notrace()from__BPF_DECLARE_TRACE_SYSCALL. Should that change be folded into this commit so the sleepable path works at each bisect point?But sleepable programs are not yet permitted, so I think this is not needed for bisection-friendliness.
Quoted 16 lines
kernel/trace/bpf_trace.c void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) · line 2102
Quoted 12 lines
kernel/trace/bpf_trace.c +12
-0+ }else { +/* +* Non-sleepable programs may run in the faultable context, +* do cant_sleep() only if program is non-sleepable and context +* is non-faultable. +*/ +if (!link->link.sleepable) +cant_sleep (); +rcu_read_lock (); + (void )bpf_prog_run (prog, args); +rcu_read_unlock (); + }[ ... ]
The commit message states:
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
kernel/trace/bpf_trace.c +0
-0For non-sleepable programs, behavior is unchanged: cant_sleep() check,
rcu_read_lock()protection.The parent commit does not have a
cant_sleep()call in__bpf_trace_run()-- it usesrcu_read_lock_dont_migrate(). The conditionalcant_sleep()is new in this commit. Could this sentence be updated to reflect the actual change?This seems worth adjusting.
Actually, with the extra
cant_sleep()check, we get this splat when running normalraw_tptests.
E.g....
[ 295.211949] BUG: assuming atomic context at kernel/trace/bpf_trace.c:2098 [ 295.212166] in_atomic(): 0, irqs_disabled(): 0, pid: 587, name: test_progs [ 295.212316] 3 locks held by test_progs/587: ... [ 295.213044] Call Trace: [ 295.213046] <TASK> [ 295.213048] dump_stack_lvl+0x54/0x70 [ 295.213055] __cant_sleep+0xb7/0xd0 [ 295.213060] bpf_trace_run1+0xce/0x340 [ 295.213072] bpf_testmod_test_read+0x433/0x5e0 [bpf_testmod] [ 295.213080] ? srso_return_thunk+0x5/0x5f [ 295.213093] kernfs_fop_read_iter+0x166/0x220
Quoted 5 lines
...
It means if the tracepoint is not marked faultable but is invoked in a non-atomic context, we will get a warning.
It might be better to just drop this, since I don't think this adds much value.
Agree. This is due to the tracepoints defined in test_kmods where tracepoints have default non-sleepable. So we have
prog: non-sleepable
test_kmods assumed context: non-sleepable
actual context: sleepable
This caused the warning.
BTW, can we merge patches 2 and 3 together? I know the sleepable tp_btf is not enabled yet in verifier. But just looking at patch 2 is a little bit confusing as its caller still has preempt_{disable,enable}_notrace().
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Other than that LGTM.
Quoted 1 line
[...]
PATCH v2 3/6 bpf: Remove preempt_disable from faultable tracepoint BPF callbacks
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Remove preempt_disable_notrace()/preempt_enable_notrace() from __BPF_DECLARE_TRACE_SYSCALL, the BPF probe callback wrapper for faultable (syscall) tracepoints.
The preemption management is now handled inside __bpf_trace_run() on a per-program basis: migrate_disable() for sleepable programs, rcu_read_lock() (which implies preempt-off in non-PREEMPT_RCU configs) for non-sleepable programs. This allows sleepable BPF programs to actually sleep when attached to faultable tracepoints.
--- include/trace/bpf_probe.h | 2 -- 1 file changed, 2 deletions(-)
include/trace/bpf_probe.h +0 -2
@@ -58,9 +58,7 @@ static notrace void \ __bpf_trace_##call(void *__data, proto) \ { \ might_fault(); \ - preempt_disable_notrace(); \ CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args)); \ - preempt_enable_notrace(); \ }#undef DECLARE_EVENT_SYSCALL_CLASS
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v2 3/6] bpf: Remove preempt_disable from faultable tracepoint BPF callbacks
Quoted 7 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Remove preempt_disable_notrace()/preempt_enable_notrace() from
__BPF_DECLARE_TRACE_SYSCALL, the BPF probe callback wrapper for faultable (syscall) tracepoints.
Quoted 8 lines
The preemption management is now handled inside
__bpf_trace_run()on a per-program basis:migrate_disable()for sleepable programs,rcu_read_lock()(which implies preempt-off in non-PREEMPT_RCU configs) for non-sleepable programs. This allows sleepable BPF programs to actually sleep when attached to faultable tracepoints.Signed-off-by Mykyta Yatsenko <[email protected]>---
Quoted 1 line
[...]
PATCH v2 4/6 bpf: Allow sleepable programs for BPF_TRACE_RAW_TP attach type
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Add BPF_TRACE_RAW_TP to the set of tracing program attach types that can be loaded as sleepable in can_be_sleepable(). The actual enforcement that the target tracepoint supports sleepable execution (i.e., is faultable) is deferred to attach time, since the target tracepoint is not known at program load time.
--- kernel/bpf/verifier.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
@@ -25199,6 +25199,7 @@ static bool can_be_sleepable(struct bpf_prog *prog)case BPF_MODIFY_RETURN :case BPF_TRACE_ITER :case BPF_TRACE_FSESSION : +case BPF_TRACE_RAW_TP :return true;default :return false;
@@ -25228,7 +25229,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) }if (prog->sleepable && !can_be_sleepable (prog)) { -verbose (env,"Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n" ); +verbose (env,"Only fentry/fexit/fmod_ret, lsm, iter, uprobe, struct_ops, and raw_tp programs can be sleepable\n" );return -EINVAL ; }
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v2 4/6] bpf: Allow sleepable programs for BPF_TRACE_RAW_TP attach type
Quoted 11 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Add
BPF_TRACE_RAW_TPto the set of tracing program attach types that can be loaded as sleepable incan_be_sleepable(). The actual enforcement that the target tracepoint supports sleepable execution (i.e., is faultable) is deferred to attach time, since the target tracepoint is not known at program load time.Signed-off-by Mykyta Yatsenko <[email protected]>---
Quoted 1 line
[...]
PATCH v2 5/6 libbpf: Add tp_btf.s section handler for sleepable raw tracepoints
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Add SEC_DEF for "tp_btf.s+" section prefix, enabling userspace BPF programs to use SEC("tp_btf.s/<tracepoint>") to load sleepable raw tracepoint programs. This follows the existing pattern used for fentry.s, fexit.s, fmod_ret.s, and lsm.s section definitions.
--- tools/lib/bpf/libbpf.c | 1 + 1 file changed, 1 insertion(+)
tools/lib/bpf/libbpf.c +1 -0
@@ -9863,6 +9863,7 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("raw_tracepoint.w+" ,RAW_TRACEPOINT_WRITABLE ,0 ,SEC_NONE , attach_raw_tp),SEC_DEF ("raw_tp.w+" ,RAW_TRACEPOINT_WRITABLE ,0 ,SEC_NONE , attach_raw_tp),SEC_DEF ("tp_btf+" ,TRACING ,BPF_TRACE_RAW_TP ,SEC_ATTACH_BTF , attach_trace), +SEC_DEF ("tp_btf.s+" ,TRACING ,BPF_TRACE_RAW_TP ,SEC_ATTACH_BTF |SEC_SLEEPABLE , attach_trace),SEC_DEF ("fentry+" ,TRACING ,BPF_TRACE_FENTRY ,SEC_ATTACH_BTF , attach_trace),SEC_DEF ("fmod_ret+" ,TRACING ,BPF_MODIFY_RETURN ,SEC_ATTACH_BTF , attach_trace), SEC_DEF("fexit+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v2 5/6] libbpf: Add tp_btf.s section handler for sleepable raw tracepoints
Quoted 10 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Add
SEC_DEFfor "tp_btf.s+" section prefix, enabling userspace BPF programs to useSEC("tp_btf.s/<tracepoint>")to load sleepable raw tracepoint programs. This follows the existing pattern used for fentry.s, fexit.s, fmod_ret.s, and lsm.s section definitions.Signed-off-by Mykyta Yatsenko <[email protected]>---
Quoted 1 line
[...]
PATCH v2 6/6 selftests/bpf: Add tests for sleepable raw tracepoint programs
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Add two subtests:
- success: Attach a sleepable BPF program to the faultable sys_enter tracepoint (tp_btf.s/sys_enter). Verify the program is triggered by a syscall.
- reject_non_faultable: Attempt to attach a sleepable BPF program to a non-faultable tracepoint (tp_btf.s/sched_switch). Verify that attachment is rejected.
--- .../selftests/bpf/prog_tests/sleepable_raw_tp.c | 56 ++++++++++++++++++++++ .../selftests/bpf/progs/test_sleepable_raw_tp.c | 43 +++++++++++++++++ .../bpf/progs/test_sleepable_raw_tp_fail.c | 16 +++++++ tools/testing/selftests/bpf/verifier/sleepable.c | 5 +- 4 files changed, 117 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +56 -0
@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <test_progs.h> +#include <time.h> +#include "test_sleepable_raw_tp.skel.h" +#include "test_sleepable_raw_tp_fail.skel.h" + +static void test_sleepable_raw_tp_success (void ) +{ +struct test_sleepable_raw_tp *skel; +int err; + + skel =test_sleepable_raw_tp__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_load" )) +return ; + + skel->bss->target_pid =getpid (); + + err =test_sleepable_raw_tp__attach (skel); +if (!ASSERT_OK (err,"skel_attach" )) + goto cleanup; + +syscall (__NR_nanosleep, &(struct timespec ){ .tv_nsec =555 },NULL ); + +ASSERT_EQ (skel->bss->triggered,1 ,"triggered" ); +ASSERT_EQ (skel->bss->err,0 ,"err" ); +ASSERT_EQ (skel->bss->copied_tv_nsec,555 ,"copied_tv_nsec" ); + +cleanup: +test_sleepable_raw_tp__destroy (skel); +} + +static void test_sleepable_raw_tp_reject (void ) +{ +struct test_sleepable_raw_tp_fail *skel; +int err; + + skel =test_sleepable_raw_tp_fail__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_load" )) + goto cleanup; + + err =test_sleepable_raw_tp_fail__attach (skel); +ASSERT_ERR (err,"skel_attach_should_fail" ); + +cleanup: +test_sleepable_raw_tp_fail__destroy (skel); +} + +void test_sleepable_raw_tp (void ) +{ +if (test__start_subtest ("success" )) +test_sleepable_raw_tp_success (); +if (test__start_subtest ("reject_non_faultable" )) +test_sleepable_raw_tp_reject (); +}
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +43 -0
43 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp_fail.c new file +16 -0
@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +/* Sleepable program on a non-faultable tracepoint should fail at attach */ +SEC("tp_btf.s/sched_switch") +int BPF_PROG (test_sleepable_sched_switch ,bool preempt, +struct task_struct *prev,struct task_struct *next) +{ +return 0 ; +}
tools/testing/selftests/bpf/verifier/sleepable.c +2 -3
@@ -76,7 +76,7 @@ .runs = -1, }, { - "sleepable raw tracepoint reject", + "sleepable raw tracepoint accept", .insns = {BPF_MOV64_IMM (BPF_REG_0 ,0 ), BPF_EXIT_INSN(),
@@ -84,8 +84,7 @@ .prog_type = BPF_PROG_TYPE_TRACING, .expected_attach_type = BPF_TRACE_RAW_TP, .kfunc = "sched_switch", - .result = REJECT, - .errstr = "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable", + .result = ACCEPT, .flags = BPF_F_SLEEPABLE, .runs = -1, },
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v2 6/6] selftests/bpf: Add tests for sleepable raw tracepoint programs
Quoted 11 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Add two subtests:
- success: Attach a sleepable BPF program to the faultablesys_entertracepoint (tp_btf.s/sys_enter). Verify the program is triggered by a syscall.
-reject_non_faultable: Attempt to attach a sleepable BPF program to a non-faultable tracepoint (tp_btf.s/sched_switch). Verify that attachment is rejected.
Quoted 2 lines
Signed-off-by Mykyta Yatsenko <[email protected]>---
... but see nit below.
Quoted 20 lines
.../selftests/bpf/prog_tests/sleepable_raw_tp.c | 56 ++++++++++++++++++++++ .../selftests/bpf/progs/test_sleepable_raw_tp.c | 43 +++++++++++++++++ .../bpf/progs/test_sleepable_raw_tp_fail.c | 16 +++++++ tools/testing/selftests/bpf/verifier/sleepable.c | 5 +- 4 files changed, 117 insertions(+), 3 deletions(-)tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +8
-0@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <test_progs.h> +#include <time.h> +#include "test_sleepable_raw_tp.skel.h" +#include "test_sleepable_raw_tp_fail.skel.h" +
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c line 18
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +10
-0+static void test_sleepable_raw_tp_success (void ) +{ +struct test_sleepable_raw_tp *skel; +int err; + + skel =test_sleepable_raw_tp__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_load" )) +return ; + + skel->bss->target_pid =getpid ();
Shouldn't this be sys_gettid()? Otherwise the filter in prog only works for the main thread (pid == tgid) in parallel test_progs mode.
Quoted 137 lines
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +38
-0+ + err =test_sleepable_raw_tp__attach (skel); +if (!ASSERT_OK (err,"skel_attach" )) + goto cleanup; + +syscall (__NR_nanosleep, &(struct timespec ){ .tv_nsec =555 },NULL ); + +ASSERT_EQ (skel->bss->triggered,1 ,"triggered" ); +ASSERT_EQ (skel->bss->err,0 ,"err" ); +ASSERT_EQ (skel->bss->copied_tv_nsec,555 ,"copied_tv_nsec" ); + +cleanup: +test_sleepable_raw_tp__destroy (skel); +} + +static void test_sleepable_raw_tp_reject (void ) +{ +struct test_sleepable_raw_tp_fail *skel; +int err; + + skel =test_sleepable_raw_tp_fail__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_load" )) + goto cleanup; + + err =test_sleepable_raw_tp_fail__attach (skel); +ASSERT_ERR (err,"skel_attach_should_fail" ); + +cleanup: +test_sleepable_raw_tp_fail__destroy (skel); +} + +void test_sleepable_raw_tp (void ) +{ +if (test__start_subtest ("success" )) +test_sleepable_raw_tp_success (); +if (test__start_subtest ("reject_non_faultable" )) +test_sleepable_raw_tp_reject (); +}tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +43
-0@@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <asm/unistd.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_core_read.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +int target_pid; +int triggered; +long err; +long copied_tv_nsec; + +SEC("tp_btf.s/sys_enter") +int BPF_PROG (test_sleepable_sys_enter ,struct pt_regs *regs,long id) +{ +struct task_struct *task =bpf_get_current_task_btf (); +struct __kernel_timespec *ts; +long tv_nsec; + +if (task->pid != target_pid) +return 0 ; + +if (id != __NR_nanosleep) +return 0 ; + + ts = (void *)PT_REGS_PARM1_CORE_SYSCALL (regs); + +/* +* Use bpf_copy_from_user() - a sleepable helper - to read user memory. +* This exercises the sleepable execution path of raw tracepoints. +*/ + err =bpf_copy_from_user (&tv_nsec,sizeof (tv_nsec), &ts->tv_nsec); +if (err) +return err; + + copied_tv_nsec = tv_nsec; + triggered =1 ; +return 0 ; +}tools/testing/selftests/bpf/progs/test_sleepable_raw_tp_fail.c new file +16
-0@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +/* Sleepable program on a non-faultable tracepoint should fail at attach */ +SEC("tp_btf.s/sched_switch") +int BPF_PROG (test_sleepable_sched_switch ,bool preempt, +struct task_struct *prev,struct task_struct *next) +{ +return 0 ; +}tools/testing/selftests/bpf/verifier/sleepable.c +2
-4@@ -76,7 +76,7 @@ .runs = -1, }, { - "sleepable raw tracepoint reject", + "sleepable raw tracepoint accept", .insns = {BPF_MOV64_IMM (BPF_REG_0 ,0 ), BPF_EXIT_INSN(),@@ -84,8 +84,7 @@ .prog_type = BPF_PROG_TYPE_TRACING, .expected_attach_type = BPF_TRACE_RAW_TP, .kfunc = "sched_switch", - .result = REJECT, - .errstr = "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable", + .result = ACCEPT, .flags = BPF_F_SLEEPABLE, .runs = -1, }, --2.47.3
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 6/6] selftests/bpf: Add tests for sleepable raw tracepoint programs
Quoted 46 lines · Kumar Kartikeya Dwivedi
Quoted 11 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Add two subtests:
- success: Attach a sleepable BPF program to the faultablesys_entertracepoint (tp_btf.s/sys_enter). Verify the program is triggered by a syscall.
-reject_non_faultable: Attempt to attach a sleepable BPF program to a non-faultable tracepoint (tp_btf.s/sched_switch). Verify that attachment is rejected.Quoted 2 lines
Signed-off-by Mykyta Yatsenko <[email protected]>---
... but see nit below.
Quoted 25 lines
.../selftests/bpf/prog_tests/sleepable_raw_tp.c | 56 ++++++++++++++++++++++ .../selftests/bpf/progs/test_sleepable_raw_tp.c | 43 +++++++++++++++++ .../bpf/progs/test_sleepable_raw_tp_fail.c | 16 +++++++ tools/testing/selftests/bpf/verifier/sleepable.c | 5 +- 4 files changed, 117 insertions(+), 3 deletions(-)tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +13
-0@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <test_progs.h> +#include <time.h> +#include "test_sleepable_raw_tp.skel.h" +#include "test_sleepable_raw_tp_fail.skel.h" + +static void test_sleepable_raw_tp_success (void ) +{ +struct test_sleepable_raw_tp *skel; +int err; +
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c line 18
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +5
-0+ skel =test_sleepable_raw_tp__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_load" )) +return ; + + skel->bss->target_pid =getpid ();Shouldn't this be
sys_gettid()? Otherwise the filter in prog only works for the main thread (pid == tgid) in paralleltest_progsmode.
Answering myself, this is fine, since parallel mode uses fork() vs pthread_create().
Quoted 1 line
Quoted 1 line
[...]
Re: [PATCH bpf-next v2 6/6] selftests/bpf: Add tests for sleepable raw tracepoint programs
In reply to Mykyta YatsenkoQuoted 58 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Add two subtests:
- success: Attach a sleepable BPF program to the faultablesys_entertracepoint (tp_btf.s/sys_enter). Verify the program is triggered by a syscall.
-reject_non_faultable: Attempt to attach a sleepable BPF program to a non-faultable tracepoint (tp_btf.s/sched_switch). Verify that attachment is rejected.Signed-off-by Mykyta Yatsenko <[email protected]>--- .../selftests/bpf/prog_tests/sleepable_raw_tp.c | 56 ++++++++++++++++++++++ .../selftests/bpf/progs/test_sleepable_raw_tp.c | 43 +++++++++++++++++ .../bpf/progs/test_sleepable_raw_tp_fail.c | 16 +++++++ tools/testing/selftests/bpf/verifier/sleepable.c | 5 +- 4 files changed, 117 insertions(+), 3 deletions(-)tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +34
-0@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <test_progs.h> +#include <time.h> +#include "test_sleepable_raw_tp.skel.h" +#include "test_sleepable_raw_tp_fail.skel.h" + +static void test_sleepable_raw_tp_success (void ) +{ +struct test_sleepable_raw_tp *skel; +int err; + + skel =test_sleepable_raw_tp__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_load" )) +return ; + + skel->bss->target_pid =getpid (); + + err =test_sleepable_raw_tp__attach (skel); +if (!ASSERT_OK (err,"skel_attach" )) + goto cleanup; + +syscall (__NR_nanosleep, &(struct timespec ){ .tv_nsec =555 },NULL ); + +ASSERT_EQ (skel->bss->triggered,1 ,"triggered" ); +ASSERT_EQ (skel->bss->err,0 ,"err" ); +ASSERT_EQ (skel->bss->copied_tv_nsec,555 ,"copied_tv_nsec" ); + +cleanup: +test_sleepable_raw_tp__destroy (skel); +} + +static void test_sleepable_raw_tp_reject (void )
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c line 44
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +10
-0+{ +struct test_sleepable_raw_tp_fail *skel; +int err; + + skel =test_sleepable_raw_tp_fail__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_load" )) + goto cleanup; + + err =test_sleepable_raw_tp_fail__attach (skel); +ASSERT_ERR (err,"skel_attach_should_fail" );
would it be better to call RUN_TESTS(test_sleepable_raw_tp_fail) instead? you could also check the verifier output
jirka
Quoted 111 lines
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +12
-0+ +cleanup: +test_sleepable_raw_tp_fail__destroy (skel); +} + +void test_sleepable_raw_tp (void ) +{ +if (test__start_subtest ("success" )) +test_sleepable_raw_tp_success (); +if (test__start_subtest ("reject_non_faultable" )) +test_sleepable_raw_tp_reject (); +}tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +43
-0@@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <asm/unistd.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_core_read.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +int target_pid; +int triggered; +long err; +long copied_tv_nsec; + +SEC("tp_btf.s/sys_enter") +int BPF_PROG (test_sleepable_sys_enter ,struct pt_regs *regs,long id) +{ +struct task_struct *task =bpf_get_current_task_btf (); +struct __kernel_timespec *ts; +long tv_nsec; + +if (task->pid != target_pid) +return 0 ; + +if (id != __NR_nanosleep) +return 0 ; + + ts = (void *)PT_REGS_PARM1_CORE_SYSCALL (regs); + +/* +* Use bpf_copy_from_user() - a sleepable helper - to read user memory. +* This exercises the sleepable execution path of raw tracepoints. +*/ + err =bpf_copy_from_user (&tv_nsec,sizeof (tv_nsec), &ts->tv_nsec); +if (err) +return err; + + copied_tv_nsec = tv_nsec; + triggered =1 ; +return 0 ; +}tools/testing/selftests/bpf/progs/test_sleepable_raw_tp_fail.c new file +16
-0@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +/* Sleepable program on a non-faultable tracepoint should fail at attach */ +SEC("tp_btf.s/sched_switch") +int BPF_PROG (test_sleepable_sched_switch ,bool preempt, +struct task_struct *prev,struct task_struct *next) +{ +return 0 ; +}tools/testing/selftests/bpf/verifier/sleepable.c +2
-3@@ -76,7 +76,7 @@ .runs = -1, }, { - "sleepable raw tracepoint reject", + "sleepable raw tracepoint accept", .insns = {BPF_MOV64_IMM (BPF_REG_0 ,0 ), BPF_EXIT_INSN(),@@ -84,8 +84,7 @@ .prog_type = BPF_PROG_TYPE_TRACING, .expected_attach_type = BPF_TRACE_RAW_TP, .kfunc = "sched_switch", - .result = REJECT, - .errstr = "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable", + .result = ACCEPT, .flags = BPF_F_SLEEPABLE, .runs = -1, },Signature · 2 lines
-- 2.47.3
Re: [PATCH bpf-next v2 6/6] selftests/bpf: Add tests for sleepable raw tracepoint programs
Quoted 73 lines · Jiri Olsa
Quoted 58 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Add two subtests:
- success: Attach a sleepable BPF program to the faultablesys_entertracepoint (tp_btf.s/sys_enter). Verify the program is triggered by a syscall.
-reject_non_faultable: Attempt to attach a sleepable BPF program to a non-faultable tracepoint (tp_btf.s/sched_switch). Verify that attachment is rejected.Signed-off-by Mykyta Yatsenko <[email protected]>--- .../selftests/bpf/prog_tests/sleepable_raw_tp.c | 56 ++++++++++++++++++++++ .../selftests/bpf/progs/test_sleepable_raw_tp.c | 43 +++++++++++++++++ .../bpf/progs/test_sleepable_raw_tp_fail.c | 16 +++++++ tools/testing/selftests/bpf/verifier/sleepable.c | 5 +- 4 files changed, 117 insertions(+), 3 deletions(-)tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +34
-0@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <test_progs.h> +#include <time.h> +#include "test_sleepable_raw_tp.skel.h" +#include "test_sleepable_raw_tp_fail.skel.h" + +static void test_sleepable_raw_tp_success (void ) +{ +struct test_sleepable_raw_tp *skel; +int err; + + skel =test_sleepable_raw_tp__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_load" )) +return ; + + skel->bss->target_pid =getpid (); + + err =test_sleepable_raw_tp__attach (skel); +if (!ASSERT_OK (err,"skel_attach" )) + goto cleanup; + +syscall (__NR_nanosleep, &(struct timespec ){ .tv_nsec =555 },NULL ); + +ASSERT_EQ (skel->bss->triggered,1 ,"triggered" ); +ASSERT_EQ (skel->bss->err,0 ,"err" ); +ASSERT_EQ (skel->bss->copied_tv_nsec,555 ,"copied_tv_nsec" ); + +cleanup: +test_sleepable_raw_tp__destroy (skel); +} + +static void test_sleepable_raw_tp_reject (void )tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c line 44
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +10
-0+{ +struct test_sleepable_raw_tp_fail *skel; +int err; + + skel =test_sleepable_raw_tp_fail__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_load" )) + goto cleanup; + + err =test_sleepable_raw_tp_fail__attach (skel); +ASSERT_ERR (err,"skel_attach_should_fail" );would it be better to call
RUN_TESTS(test_sleepable_raw_tp_fail)instead? you could also check the verifier output
Quoted 1 line
jirka
yes, definitely, thanks!
Quoted 113 lines
Quoted 113 lines
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +12
-0+ +cleanup: +test_sleepable_raw_tp_fail__destroy (skel); +} + +void test_sleepable_raw_tp (void ) +{ +if (test__start_subtest ("success" )) +test_sleepable_raw_tp_success (); +if (test__start_subtest ("reject_non_faultable" )) +test_sleepable_raw_tp_reject (); +}tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +43
-0@@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <asm/unistd.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_core_read.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +int target_pid; +int triggered; +long err; +long copied_tv_nsec; + +SEC("tp_btf.s/sys_enter") +int BPF_PROG (test_sleepable_sys_enter ,struct pt_regs *regs,long id) +{ +struct task_struct *task =bpf_get_current_task_btf (); +struct __kernel_timespec *ts; +long tv_nsec; + +if (task->pid != target_pid) +return 0 ; + +if (id != __NR_nanosleep) +return 0 ; + + ts = (void *)PT_REGS_PARM1_CORE_SYSCALL (regs); + +/* +* Use bpf_copy_from_user() - a sleepable helper - to read user memory. +* This exercises the sleepable execution path of raw tracepoints. +*/ + err =bpf_copy_from_user (&tv_nsec,sizeof (tv_nsec), &ts->tv_nsec); +if (err) +return err; + + copied_tv_nsec = tv_nsec; + triggered =1 ; +return 0 ; +}tools/testing/selftests/bpf/progs/test_sleepable_raw_tp_fail.c new file +16
-0@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +/* Sleepable program on a non-faultable tracepoint should fail at attach */ +SEC("tp_btf.s/sched_switch") +int BPF_PROG (test_sleepable_sched_switch ,bool preempt, +struct task_struct *prev,struct task_struct *next) +{ +return 0 ; +}tools/testing/selftests/bpf/verifier/sleepable.c +2
-3@@ -76,7 +76,7 @@ .runs = -1, }, { - "sleepable raw tracepoint reject", + "sleepable raw tracepoint accept", .insns = {BPF_MOV64_IMM (BPF_REG_0 ,0 ), BPF_EXIT_INSN(),@@ -84,8 +84,7 @@ .prog_type = BPF_PROG_TYPE_TRACING, .expected_attach_type = BPF_TRACE_RAW_TP, .kfunc = "sched_switch", - .result = REJECT, - .errstr = "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable", + .result = ACCEPT, .flags = BPF_F_SLEEPABLE, .runs = -1, },Signature · 2 lines
-- 2.47.3