bpf: Add support for sleepable tracepoint programs
Subsystems (9)
v4 cover bpf: Add support for sleepable tracepoint programs
This series adds support for sleepable BPF programs attached to raw tracepoints (tp_btf), classic raw tracepoints (raw_tp), and classic tracepoints (tp). 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 inside __bpf_trace_run(), 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 modifies __bpf_trace_run() to support sleepable programs following the uprobe_prog_run() pattern: use explicit rcu_read_lock_trace() for sleepable programs and rcu_read_lock() for non-sleepable programs. Also removes preempt_disable from the faultable tracepoint BPF callback wrapper, since per-CPU protection and RCU locking are now managed per-program inside __bpf_trace_run().
Patch 2 adds sleepable support for classic tracepoints (BPF_PROG_TYPE_TRACEPOINT) by introducing trace_call_bpf_faultable() and restructuring perf_syscall_enter/exit() to run BPF programs in faultable context before the preempt-disabled per-cpu buffer allocation.
Patch 3 allows BPF_TRACE_RAW_TP, BPF_PROG_TYPE_RAW_TRACEPOINT, and BPF_PROG_TYPE_TRACEPOINT programs to be loaded as sleepable, with load-time and attach-time checks to reject sleepable programs on non-faultable tracepoints.
Patch 4 adds libbpf SEC_DEF handlers: tp_btf.s, raw_tp.s, raw_tracepoint.s, tp.s, and tracepoint.s.
Patch 5 adds selftests covering tp_btf.s, raw_tp.s, and tp.s positive cases using bpf_copy_from_user() on sys_enter nanosleep, plus negative tests for non-faultable tracepoints.
---
Changes in v4:
- Follow uprobe_prog_run() pattern with explicit rcu_read_lock_trace()
instead of relying on outer rcu_tasks_trace lock
- Add sleepable support for classic raw tracepoints (raw_tp.s)
- Add sleepable support for classic tracepoints (tp.s) with new trace_call_bpf_faultable() and restructured perf_syscall_enter/exit()
- Add raw_tp.s, raw_tracepoint.s, tp.s, tracepoint.s SEC_DEF handlers
- Replace growing type enumeration in error message with generic
"program of this type cannot be sleepable"
- Use PT_REGS_PARM1_SYSCALL (non-CO-RE) in BTF test
- Add classic raw_tp and classic tracepoint sleepable tests
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Moved faultable tracepoint check from attach time to load time in bpf_check_attach_target(), providing a clear verifier error message
- Removed cant_sleep() call from non-sleepable path in
__bpf_trace_run()
- Folded preempt_disable removal into the sleepable execution path
patch
- Used RUN_TESTS() with __failure/__msg for negative test case instead
of explicit userspace program
- Reduced series from 6 patches to 4
- Link to v2: https://lore.kernel.org/r/[email protected]
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 (5):
bpf: Add sleepable support for raw tracepoint programs
bpf: Add sleepable support for classic tracepoint programs
bpf: Verifier support for sleepable tracepoint programs
libbpf: Add section handlers for sleepable tracepoints selftests/bpf: Add tests for sleepable tracepoint programs
include/linux/trace_events.h | 6 ++ include/trace/bpf_probe.h | 2 - kernel/bpf/syscall.c | 5 + kernel/bpf/verifier.c | 13 ++- kernel/events/core.c | 9 ++ kernel/trace/bpf_trace.c | 59 +++++++++++- kernel/trace/trace_syscalls.c | 104 +++++++++++---------- tools/lib/bpf/libbpf.c | 32 ++++++- .../bpf/prog_tests/sleepable_tracepoints.c | 57 +++++++++++ .../bpf/progs/test_sleepable_tracepoints.c | 75 +++++++++++++++ tools/testing/selftests/bpf/verifier/sleepable.c | 17 +++- 11 files changed, 317 insertions(+), 62 deletions(-)
---
base-commit: bd2e02e3c9215305dfa344c050d5822f19929cf7
change-id: 20260216-sleepable_tracepoints-381ae1410550
Best regards,
Signature · 1 line
Mykyta Yatsenko <[email protected]>
PATCH v4 1/5 bpf: Add sleepable support for raw tracepoint programs
From: Mykyta Yatsenko <[email protected]>
Rework __bpf_trace_run() to support sleepable BPF programs by using explicit RCU flavor selection, following the uprobe_prog_run() pattern.
For sleepable programs, use rcu_read_lock_trace() for lifetime protection and add a might_fault() annotation. For non-sleepable programs, use the regular rcu_read_lock(). Replace the combined rcu_read_lock_dont_migrate() with separate rcu_read_lock()/ migrate_disable() calls, since sleepable programs need rcu_read_lock_trace() instead of rcu_read_lock().
Remove the preempt_disable_notrace/preempt_enable_notrace pair from the faultable tracepoint BPF probe wrapper in bpf_probe.h, since preemption management is now handled inside __bpf_trace_run().
This enables both BTF-based raw tracepoints (tp_btf.s) and classic raw tracepoints (raw_tp.s) to run sleepable BPF programs when attached to faultable tracepoints (e.g. syscall tracepoints).
--- include/trace/bpf_probe.h | 2 -- kernel/trace/bpf_trace.c | 20 +++++++++++++++++--- 2 files changed, 17 insertions(+), 5 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
kernel/trace/bpf_trace.c +17 -3
@@ -2073,10 +2073,16 @@ static __always_inlinevoid __bpf_trace_run (struct bpf_raw_tp_link *link,u64 *args) {struct bpf_prog *prog = link->link.prog; +bool sleepable = prog->sleepable;struct bpf_run_ctx *old_run_ctx;struct bpf_trace_run_ctx run_ctx; -rcu_read_lock_dont_migrate (); +if (sleepable) +rcu_read_lock_trace (); +else +rcu_read_lock (); + +migrate_disable ();if (unlikely (!bpf_prog_get_recursion_context (prog))) {bpf_prog_inc_misses_counter (prog); goto out;
@@ -2085,12 +2091,20 @@ 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 (sleepable) +might_fault (); + + (void )bpf_prog_run (prog, args);bpf_reset_run_ctx (old_run_ctx); out:bpf_prog_put_recursion_context (prog); -rcu_read_unlock_migrate (); +migrate_enable (); + +if (sleepable) +rcu_read_unlock_trace (); +else +rcu_read_unlock (); }#define UNPACK (...) __VA_ARGS__
Signature · 1 line
2.52.0
PATCH v4 2/5 bpf: Add sleepable support for classic tracepoint programs
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Add trace_call_bpf_faultable(), a variant of trace_call_bpf() for faultable tracepoints that supports sleepable BPF programs. It uses rcu_read_lock_trace() for lifetime protection and bpf_prog_run_array_uprobe() for per-program RCU flavor selection, following the uprobe_prog_run() pattern. Uses preempt-safe this_cpu_inc_return/this_cpu_dec for the bpf_prog_active recursion counter since preemption is enabled in this context.
Restructure perf_syscall_enter() and perf_syscall_exit() to run BPF filter before perf event processing. Previously, BPF ran after the per-cpu perf trace buffer was allocated under preempt_disable, requiring cleanup via perf_swevent_put_recursion_context() on filter. Now BPF runs in faultable context before preempt_disable, reading syscall arguments from local variables instead of the per-cpu trace record, removing the dependency on buffer allocation. This allows sleepable BPF programs to execute and avoids unnecessary buffer allocation when BPF filters the event. The perf event submission path (buffer allocation, fill, submit) remains under preempt_disable as before.
Add an attach-time check in __perf_event_set_bpf_prog() to reject sleepable BPF_PROG_TYPE_TRACEPOINT programs on non-syscall tracepoints, since only syscall tracepoints run in faultable context.
--- include/linux/trace_events.h | 6 +++ kernel/events/core.c | 9 ++++ kernel/trace/bpf_trace.c | 39 ++++++++++++++++ kernel/trace/trace_syscalls.c | 104 +++++++++++++++++++++++------------------- 4 files changed, 110 insertions(+), 48 deletions(-)
include/linux/trace_events.h +6 -0
@@ -767,6 +767,7 @@ trace_trigger_soft_disabled(struct trace_event_file *file)#ifdef CONFIG_BPF_EVENTS unsigned int trace_call_bpf (struct trace_event_call *call,void *ctx); +unsigned int trace_call_bpf_faultable (struct trace_event_call *call,void *ctx);int perf_event_attach_bpf_prog (struct perf_event *event,struct bpf_prog *prog,u64 bpf_cookie);void perf_event_detach_bpf_prog (struct perf_event *event);int perf_event_query_prog_array (struct perf_event *event,void __user *info);
@@ -789,6 +790,11 @@ static inline unsigned int trace_call_bpf(struct trace_event_call *call, void *creturn 1 ; } +static inline unsigned int trace_call_bpf_faultable (struct trace_event_call *call,void *ctx) +{ +return 1 ; +} +static inline int perf_event_attach_bpf_prog (struct perf_event *event,struct bpf_prog *prog,u64 bpf_cookie) {
kernel/events/core.c +9 -0
@@ -11647,6 +11647,15 @@ static int __perf_event_set_bpf_prog(struct perf_event *event,/* only uprobe programs are allowed to be sleepable */ return -EINVAL ; +if (prog->type ==BPF_PROG_TYPE_TRACEPOINT && prog->sleepable) { +/* +* Sleepable tracepoint programs can only attach to faultable +* tracepoints. Currently only syscall tracepoints are faultable. +*/ +if (!is_syscall_tp) +return -EINVAL ; + } +/* Kprobe override only works for kprobes, not uprobes. */ if (prog->kprobe_override && !is_kprobe)return -EINVAL ;
kernel/trace/bpf_trace.c +39 -0
@@ -152,6 +152,45 @@ unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)return ret; } +/** +* trace_call_bpf_faultable - invoke BPF program in faultable context +* @call: tracepoint event +* @ctx: opaque context pointer +* +* Variant of trace_call_bpf() for faultable tracepoints (e.g. syscall +* tracepoints). Supports sleepable BPF programs by using rcu_tasks_trace +* for lifetime protection and per-program rcu_read_lock for non-sleepable +* programs, following the uprobe_prog_run() pattern. +* +* Must be called from a faultable/preemptible context. +*/ +unsigned int trace_call_bpf_faultable (struct trace_event_call *call,void *ctx) +{ +unsigned int ret; + +might_fault (); + +guard (rcu_tasks_trace)(); +guard (migrate)(); + +if (unlikely (this_cpu_inc_return (bpf_prog_active) !=1 )) { +scoped_guard (rcu) { +bpf_prog_inc_misses_counters (rcu_dereference (call->prog_array)); + } +this_cpu_dec (bpf_prog_active); +return 0 ; + } + + ret =bpf_prog_run_array_uprobe ( +rcu_dereference_check (call->prog_array, +rcu_read_lock_trace_held ()), + ctx, bpf_prog_run); + +this_cpu_dec (bpf_prog_active); + +return ret; +} +#ifdef CONFIG_BPF_KPROBE_OVERRIDE BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc) {
kernel/trace/trace_syscalls.c +56 -48
179 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 2/5] bpf: Add sleepable support for classic tracepoint programs
Should regs be zero-initialized here? The perf_fetch_caller_regs() comment in include/linux/perf_event.h states:
Quoted 18 lines
kernel/trace/trace_syscalls.c +2
-2@@ -1372,26 +1372,27 @@ static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);static int sys_perf_refcount_enter;static int sys_perf_refcount_exit; -static int perf_call_bpf_enter (struct trace_event_call *call,struct pt_regs *regs, +static int perf_call_bpf_enter (struct trace_event_call *call,struct syscall_metadata *sys_data, -struct syscall_trace_enter *rec) +int syscall_nr,unsigned long *args) {struct syscall_tp_t {struct trace_entry ent;int syscall_nr;unsigned long args[SYSCALL_DEFINE_MAXARGS ];
kernel/trace/trace_syscalls.c static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls); · line 1390
Quoted 10 lines
kernel/trace/trace_syscalls.c +3
-2}__aligned(8) param; +struct pt_regs regs;int i;BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *)); -/* bpf prog requires 'regs' to be the first member in the ctx (a.k.a. ¶m) */ -perf_fetch_caller_regs (regs); +/* bpf prog requires 'regs' to be the first member in the ctx */ +perf_fetch_caller_regs (®s);
Should regs be zero-initialized here? The perf_fetch_caller_regs() comment in include/linux/perf_event.h states:
NOTE: assumes @regs is otherwise already 0 filled; this is important for things like PERF_SAMPLE_REGS_INTR.
The function only sets ip, sp, cs, and flags, leaving all other register fields at whatever happened to be on the stack.
Previously, the regs pointer came from perf_trace_buf_alloc() which returned this_cpu_ptr(&__perf_regs[rctx]), a per-cpu variable that is zero-initialized at boot. The new stack-local struct does not get that zeroing.
If a BPF program calls bpf_perf_event_output(), bpf_perf_event_output_tp() extracts this pointer:
struct pt_regs *regs = *(struct pt_regs **)tp_buff;
and passes it through to perf_event_output(), where uninitialized register fields could end up in perf samples.
Would initializing to zero fix this?
struct pt_regs regs = {};Quoted 1 line
kernel/trace/trace_syscalls.c +1
-0+ *(struct pt_regs **)¶m = ®s;
[ ... ]
Quoted 11 lines
kernel/trace/trace_syscalls.c +2
-2@@ -1515,21 +1519,22 @@ static void perf_sysenter_disable(struct trace_event_call *call)syscall_fault_buffer_disable (); } -static int perf_call_bpf_exit (struct trace_event_call *call,struct pt_regs *regs, -struct syscall_trace_exit *rec) +static int perf_call_bpf_exit (struct trace_event_call *call, +int syscall_nr,long ret_val) {struct syscall_tp_t {struct trace_entry ent;
kernel/trace/trace_syscalls.c static void perf_sysenter_disable(struct trace_event_call *call) · line 1533
Quoted 10 lines
kernel/trace/trace_syscalls.c +4
-3int syscall_nr;unsigned long ret; }__aligned(8) param; - -/* bpf prog requires 'regs' to be the first member in the ctx (a.k.a. ¶m) */ -perf_fetch_caller_regs (regs); +struct pt_regs regs; + +/* bpf prog requires 'regs' to be the first member in the ctx */ +perf_fetch_caller_regs (®s);
Same uninitialized regs concern as perf_call_bpf_enter() above.
kernel/trace/trace_syscalls.c static void perf_sysenter_disable(struct trace_event_call *call) · line 1534
Quoted 1 line
kernel/trace/trace_syscalls.c +1
-0+ *(struct pt_regs **)¶m = ®s;
[ ... ]
---
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/23070834076
PATCH v4 3/5 bpf: Verifier support for sleepable tracepoint programs
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Allow BPF_PROG_TYPE_RAW_TRACEPOINT, BPF_PROG_TYPE_TRACEPOINT, and BPF_TRACE_RAW_TP (tp_btf) programs to be sleepable by adding them to can_be_sleepable().
For BTF-based raw tracepoints (tp_btf), add a load-time check in bpf_check_attach_target() that rejects sleepable programs attaching to non-faultable tracepoints with a descriptive error message.
For classic raw tracepoints (raw_tp), add an attach-time check in bpf_raw_tp_link_attach() that rejects sleepable programs on non-faultable tracepoints. The attach-time check is needed because the tracepoint name is not known at load time for classic raw_tp.
Replace the verbose error message that enumerates allowed program types with a generic "program of this type cannot be sleepable" message, since the list of sleepable-capable types keeps growing.
--- kernel/bpf/syscall.c | 5 +++++ kernel/bpf/verifier.c | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-)
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)) { +bpf_put_raw_tracepoint (btp); +return -EINVAL ; + } + link =kzalloc_obj (*link,GFP_USER );if (!link) { err = -ENOMEM ;
kernel/bpf/verifier.c +11 -2
@@ -25216,6 +25216,12 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, btp =bpf_get_raw_tracepoint (tname);if (!btp)return -EINVAL ; +if (prog->sleepable && !tracepoint_is_faultable (btp->tp)) { +bpf_log (log,"Sleepable program cannot attach to non-faultable tracepoint %s\n" , + tname); +bpf_put_raw_tracepoint (btp); +return -EINVAL ; + } fname =kallsyms_lookup ((unsigned long )btp->bpf_func,NULL ,NULL ,NULL , trace_symbol);bpf_put_raw_tracepoint (btp);
@@ -25432,6 +25438,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;
@@ -25439,7 +25446,9 @@ static bool can_be_sleepable(struct bpf_prog *prog) }return prog->type ==BPF_PROG_TYPE_LSM || prog->type ==BPF_PROG_TYPE_KPROBE /* only for uprobes */ || - prog->type ==BPF_PROG_TYPE_STRUCT_OPS ; + prog->type ==BPF_PROG_TYPE_STRUCT_OPS || + prog->type ==BPF_PROG_TYPE_RAW_TRACEPOINT || + prog->type ==BPF_PROG_TYPE_TRACEPOINT ; }static int check_attach_btf_id (struct bpf_verifier_env *env)
@@ -25461,7 +25470,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,"Program of this type cannot be sleepable\n" );return -EINVAL ; }
Signature · 1 line
2.52.0
PATCH v4 4/5 libbpf: Add section handlers for sleepable tracepoints
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Add SEC_DEF entries for sleepable tracepoint variants:
- "tp_btf.s+" for sleepable BTF-based raw tracepoints
- "raw_tp.s+" for sleepable classic raw tracepoints
- "raw_tracepoint.s+" (alias)
- "tp.s+" for sleepable classic tracepoints
- "tracepoint.s+" (alias)
Update attach_raw_tp() to recognize "raw_tp.s" and "raw_tracepoint.s" prefixes when extracting the tracepoint name.
Rewrite attach_tp() to use a prefix array including "tp.s/" and "tracepoint.s/" variants for proper section name parsing.
--- tools/lib/bpf/libbpf.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +27 -5
@@ -9858,11 +9858,16 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("netkit/peer" ,SCHED_CLS ,BPF_NETKIT_PEER ,SEC_NONE ),SEC_DEF ("tracepoint+" ,TRACEPOINT ,0 ,SEC_NONE , attach_tp),SEC_DEF ("tp+" ,TRACEPOINT ,0 ,SEC_NONE , attach_tp), +SEC_DEF ("tracepoint.s+" ,TRACEPOINT ,0 ,SEC_SLEEPABLE , attach_tp), +SEC_DEF ("tp.s+" ,TRACEPOINT ,0 ,SEC_SLEEPABLE , attach_tp),SEC_DEF ("raw_tracepoint+" ,RAW_TRACEPOINT ,0 ,SEC_NONE , attach_raw_tp),SEC_DEF ("raw_tp+" ,RAW_TRACEPOINT ,0 ,SEC_NONE , attach_raw_tp), +SEC_DEF ("raw_tracepoint.s+" ,RAW_TRACEPOINT ,0 ,SEC_SLEEPABLE , attach_raw_tp), +SEC_DEF ("raw_tp.s+" ,RAW_TRACEPOINT ,0 ,SEC_SLEEPABLE , attach_raw_tp),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),
@@ -12985,7 +12990,14 @@ struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog,static int attach_tp (const struct bpf_program *prog,long cookie,struct bpf_link **link) { +static const char *const prefixes[] = { +"tp.s/" , +"tp/" , +"tracepoint.s/" , +"tracepoint/" , + };char *sec_name, *tp_cat, *tp_name; +size_t i; *link =NULL ;
@@ -12997,11 +13009,19 @@ static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_linif (!sec_name)return -ENOMEM ; -/* extract "tp/<category>/<name>" or "tracepoint/<category>/<name>" */ -if (str_has_pfx (prog->sec_name,"tp/" )) - tp_cat = sec_name +sizeof ("tp/" ) -1 ; -else - tp_cat = sec_name +sizeof ("tracepoint/" ) -1 ; +/* extract "<prefix><category>/<name>" */ + tp_cat =NULL ; +for (i =0 ; i <ARRAY_SIZE (prefixes); i++) { +if (str_has_pfx (prog->sec_name, prefixes[i])) { + tp_cat = sec_name +strlen (prefixes[i]); +break ; + } + } +if (!tp_cat) { +free (sec_name); +return -EINVAL ; + } + tp_name =strchr (tp_cat,'/' );if (!tp_name) {free (sec_name);
@@ -13065,6 +13085,8 @@ static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf"raw_tracepoint" ,"raw_tp.w" ,"raw_tracepoint.w" , +"raw_tp.s" , + "raw_tracepoint.s", };size_t i;const char *tp_name =NULL ;
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 4/5] libbpf: Add section handlers for sleepable tracepoints
Should attach_tp() handle bare SEC("tp.s") and SEC("tracepoint.s") the same way it handles bare SEC("tp") and SEC("tracepoint")?
Quoted 4 lines
tools/lib/bpf/libbpf.c +0
-0
[ ... ]
Quoted 11 lines
tools/lib/bpf/libbpf.c +2
-5@@ -12997,11 +13009,19 @@ static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_linif (!sec_name)return -ENOMEM ; -/* extract "tp/<category>/<name>" or "tracepoint/<category>/<name>" */ -if (str_has_pfx (prog->sec_name,"tp/" )) - tp_cat = sec_name +sizeof ("tp/" ) -1 ; -else - tp_cat = sec_name +sizeof ("tracepoint/" ) -1 ; +/* extract "<prefix><category>/<name>" */ + tp_cat =NULL ;
tools/lib/bpf/libbpf.c static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_lin · line 13023
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+for (i =0 ; i <ARRAY_SIZE (prefixes); i++) { +if (str_has_pfx (prog->sec_name, prefixes[i])) { + tp_cat = sec_name +strlen (prefixes[i]); +break ; + } + } +if (!tp_cat) { +free (sec_name); +return -EINVAL ; + }
Should attach_tp() handle bare SEC("tp.s") and SEC("tracepoint.s") the same way it handles bare SEC("tp") and SEC("tracepoint")?
sec_def_matches() allows bare section names for "+" suffixed SEC_DEF entries -- when sec_name[len] == '\0' it returns true. So SEC("tp.s") matches SEC_DEF("tp.s+") and reaches attach_tp().
The existing bare-name guard only checks for "tp" and "tracepoint":
if (strcmp(prog->sec_name, "tp") == 0 ||
strcmp(prog->sec_name, "tracepoint") == 0)
return 0;Since none of the prefixes ("tp.s/", "tp/", "tracepoint.s/", "tracepoint/") match the bare string "tp.s", tp_cat stays NULL and the function returns -EINVAL. In bpf_object__attach_skeleton() this becomes a fatal error that stops the entire skeleton attach.
attach_raw_tp() in the same patch handles the equivalent bare case correctly -- when sec_name[pfx_len] == '\0' it returns 0:
for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
...
if (prog->sec_name[pfx_len] == '\0')
return 0;
...
}The convention across libbpf attach handlers (attach_kprobe, attach_raw_tp) is to return 0 with *link = NULL for bare section names, signaling "no auto-attach" without error.
---
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/23070834076
PATCH v4 5/5 selftests/bpf: Add tests for sleepable tracepoint programs
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Add functional tests for sleepable tracepoint programs that attach to the nanosleep syscall and use bpf_copy_from_user() to read user memory:
- tp_btf: BTF-based raw tracepoint using SEC("tp_btf.s/sys_enter") with PT_REGS_PARM1_SYSCALL (non-CO-RE macro for BTF programs).
- classic: Classic raw tracepoint using SEC("raw_tp.s/sys_enter") with PT_REGS_PARM1_CORE_SYSCALL (CO-RE macro needed for classic).
- tracepoint: Classic tracepoint using
SEC("tp.s/syscalls/sys_enter_nanosleep") receiving
struct syscall_trace_enter with direct access to args[].Add a negative test (test_sleepable_raw_tp_fail) that verifies sleepable programs are rejected on non-faultable tracepoints (sched_switch).
Update verifier/sleepable.c tests:
- Add "sleepable raw tracepoint accept" test for sys_enter.
- Rename reject test and update error message to match the new descriptive "Sleepable program cannot attach to non-faultable tracepoint" message.
--- .../bpf/prog_tests/sleepable_tracepoints.c | 57 ++++++++++++++++ .../bpf/progs/test_sleepable_tracepoints.c | 75 ++++++++++++++++++++++ tools/testing/selftests/bpf/verifier/sleepable.c | 17 ++++- 3 files changed, 147 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sleepable_tracepoints.c new file +57 -0
@@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <test_progs.h> +#include <time.h> +#include "test_sleepable_tracepoints.skel.h" + +static void run_test (struct bpf_program *prog, +struct test_sleepable_tracepoints *skel) +{ +struct bpf_link *link; + + link =bpf_program__attach (prog); +if (!ASSERT_OK_PTR (link,"prog_attach" )) +return ; + + skel->bss->target_pid =getpid (); + skel->bss->triggered =0 ; + skel->bss->err =0 ; + skel->bss->copied_tv_nsec =0 ; + +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" ); + +bpf_link__destroy (link); +} + +void test_sleepable_tracepoints (void ) +{ +struct test_sleepable_tracepoints *skel; + + skel =test_sleepable_tracepoints__open (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) +return ; + +bpf_program__set_autoload (skel->progs.handle_sched_switch, false); + +if (!ASSERT_OK (test_sleepable_tracepoints__load (skel),"skel_load" )) + goto cleanup; + +if (test__start_subtest ("tp_btf" )) +run_test (skel->progs.handle_sys_enter_tp_btf, skel); + +if (test__start_subtest ("classic" )) +run_test (skel->progs.handle_sys_enter_raw_tp, skel); + +if (test__start_subtest ("tracepoint" )) +run_test (skel->progs.handle_sys_enter_tp, skel); + +cleanup: +test_sleepable_tracepoints__destroy (skel); + +RUN_TESTS (test_sleepable_tracepoints); +}
tools/testing/selftests/bpf/progs/test_sleepable_tracepoints.c new file +75 -0
75 lines not on this page · raw message
tools/testing/selftests/bpf/verifier/sleepable.c +15 -2
@@ -76,7 +76,20 @@ .runs = -1, }, { - "sleepable raw tracepoint reject", + "sleepable raw tracepoint accept", + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_TRACING , + .expected_attach_type = BPF_TRACE_RAW_TP, + .kfunc = "sys_enter", + .result = ACCEPT, + .flags = BPF_F_SLEEPABLE, + .runs = -1, +}, +{ +"sleepable raw tracepoint reject non-faultable" , .insns = {BPF_MOV64_IMM (BPF_REG_0 ,0 ), BPF_EXIT_INSN(),
@@ -85,7 +98,7 @@ .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", + .errstr = "Sleepable program cannot attach to non-faultable tracepoint", .flags = BPF_F_SLEEPABLE, .runs = -1, },
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 5/5] selftests/bpf: Add tests for sleepable tracepoint programs
The commit message references test_sleepable_raw_tp_fail, but this name doesn't appear anywhere in the committed code. It looks like a leftover from v2 of the series, where the negative test lived…
Quoted 3 lines
Add a negative test (
test_sleepable_raw_tp_fail) that verifies sleepable programs are rejected on non-faultable tracepoints (sched_switch).
The commit message references test_sleepable_raw_tp_fail, but this name doesn't appear anywhere in the committed code. It looks like a leftover from v2 of the series, where the negative test lived in a separate file (progs/test_sleepable_raw_tp_fail.c).
In this version the negative test is handle_sched_switch in progs/test_sleepable_tracepoints.c, tested via the RUN_TESTS macro. Should the commit message be updated to reflect the current naming?
---
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/23070834076