Skip to contentSkip to conversation

bpf: Add support for sleepable tracepoint programs

PATCH Superseded Mykyta Yatsenko 3 participants 17 messages Mar 30 – Apr 1

Subsystems (10)
Networking Kselftest BPF perf events +6 more subsystems

v8 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v8 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.

This series removes 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 migration protection and RCU locking are now managed per-program inside __bpf_trace_run().

Patch 2 renames bpf_prog_run_array_uprobe() to bpf_prog_run_array_sleepable() to support new usecase.

Patch 3 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. trace_call_bpf_faultable() uses rcu_tasks_trace for lifetime protection, following the uprobe pattern. This adds rcu_tasks_trace overhead for all classic tracepoint BPF programs on syscall tracepoints, not just sleepable ones.

Patch 4 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 5 adds libbpf SEC_DEF handlers: tp_btf.s, raw_tp.s, raw_tracepoint.s, tp.s, and tracepoint.s.

Patch 6 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 v8:
- Fix sleepable tracepoint support in bpf_prog_test_run() (Kumar, sashiko)
- Link to v7: https://patch.msgid.link/[email protected]

Changes in v7:
- Add recursion check (bpf_prog_get_recursion_context()) to make sure
private stack is safe when sleepable program is preempted by itself
(Alexei, Kumar)
- Use combined rcu_read_lock_dont_migrate() instead of separate rcu_read_lock()/migrate_disable() calls for non-sleepable path (Alexei)
- Link to v6: https://lore.kernel.org/bpf/[email protected]/

Changes in v6:
- Remove recursion check from trace_call_bpf_faultable(), sleepable
tracepoints are called from syscall enter/exit, no recursion is
possible.(Kumar)
- Refactor bpf_prog_run_array_uprobe() to support tracepoints
usecase cleanly (Kumar)
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Addressed AI review: zero initialize struct pt_regs in
perf_call_bpf_enter(); changed handling tp.s and tracepoint.s in
attach_tp() in libbpf.
- Updated commit messages
- Link to v4: https://lore.kernel.org/r/[email protected]

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
- 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 (6):
bpf: Add sleepable support for raw tracepoint programs
bpf: Rename bpf_prog_run_array_uprobe() to bpf_prog_run_array_sleepable()
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/bpf.h                                |  25 +++-
 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                           |  49 ++++++-
 kernel/trace/trace_syscalls.c                      | 110 ++++++++-------
 kernel/trace/trace_uprobe.c                        |   2 +-
 net/bpf/test_run.c                                 |  55 ++++++--
 tools/lib/bpf/libbpf.c                             |  39 +++++-
 .../bpf/prog_tests/sleepable_tracepoints.c         | 153 +++++++++++++++++++++
 .../bpf/progs/test_sleepable_tracepoints.c         | 128 +++++++++++++++++
 .../bpf/progs/test_sleepable_tracepoints_fail.c    |  18 +++
 tools/testing/selftests/bpf/verifier/sleepable.c   |  17 ++-
 15 files changed, 543 insertions(+), 88 deletions(-)

---
base-commit: 596817673dc2d226bdacb72d7a03a328966f563b
change-id: 20260216-sleepable_tracepoints-381ae1410550

Best regards,

Signature · 1 line
Mykyta Yatsenko <[email protected]>
#1 lore raw

PATCH v8 1/6 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_tasks_trace() for lifetime protection with migrate_disable(). For non-sleepable programs, use the regular rcu_read_lock_dont_migrate().

Remove the preempt_disable_notrace/preempt_enable_notrace pair from the faultable tracepoint BPF probe wrapper in bpf_probe.h, since migration protection and RCU locking are now handled per-program inside __bpf_trace_run().

Adapt bpf_prog_test_run_raw_tp() for sleepable programs: select RCU flavor per-program and reject BPF_F_TEST_RUN_ON_CPU since sleepable programs cannot run in hardirq or preempt-disabled context.

This prepares the runtime execution path for both BTF-based raw tracepoints (tp_btf) and classic raw tracepoints (raw_tp) to support sleepable BPF programs on faultable tracepoints (e.g. syscall tracepoints). The verifier changes to allow loading sleepable programs are in a subsequent patch.

---
 include/trace/bpf_probe.h |  2 --
 kernel/trace/bpf_trace.c  | 20 ++++++++++++++---
 net/bpf/test_run.c        | 55 ++++++++++++++++++++++++++++++++++-------------
 3 files changed, 57 insertions(+), 20 deletions(-)
include/trace/bpf_probe.h +0 -2
index 9391d54d3f12..d1de8f9aa07f 100644
@@ -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
index 0b040a417442..8066249e4ba2 100644
@@ -2072,11 +2072,19 @@ void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
 static __always_inline
 void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 {
+	struct srcu_ctr __percpu *scp = NULL;
 	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) {
+		scp = rcu_read_lock_tasks_trace();
+		migrate_disable();
+	} else {
+		rcu_read_lock_dont_migrate();
+	}
+
 	if (unlikely(!bpf_prog_get_recursion_context(prog))) {
 		bpf_prog_inc_misses_counter(prog);
 		goto out;
@@ -2085,12 +2093,18 @@ 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);
+	(void)bpf_prog_run(prog, args);
 
 	bpf_reset_run_ctx(old_run_ctx);
 out:
 	bpf_prog_put_recursion_context(prog);
-	rcu_read_unlock_migrate();
+
+	if (sleepable) {
+		migrate_enable();
+		rcu_read_unlock_tasks_trace(scp);
+	} else {
+		rcu_read_unlock_migrate();
+	}
 }
 
 #define UNPACK(...)			__VA_ARGS__
net/bpf/test_run.c +40 -15

82 lines not on this page · raw message

Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH bpf-next v8 1/6] bpf: Add sleepable support for raw tracepoint programs

Quoted 59 lines · Mykyta Yatsenko

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_tasks_trace() for lifetime protection with migrate_disable(). For non-sleepable programs, use the regular rcu_read_lock_dont_migrate().

Remove the preempt_disable_notrace/preempt_enable_notrace pair from the faultable tracepoint BPF probe wrapper in bpf_probe.h, since migration protection and RCU locking are now handled per-program inside __bpf_trace_run().

Adapt bpf_prog_test_run_raw_tp() for sleepable programs: select RCU flavor per-program and reject BPF_F_TEST_RUN_ON_CPU since sleepable programs cannot run in hardirq or preempt-disabled context.

This prepares the runtime execution path for both BTF-based raw tracepoints (tp_btf) and classic raw tracepoints (raw_tp) to support sleepable BPF programs on faultable tracepoints (e.g. syscall tracepoints). The verifier changes to allow loading sleepable programs are in a subsequent patch.

---
 include/trace/bpf_probe.h |  2 --
 kernel/trace/bpf_trace.c  | 20 ++++++++++++++---
 net/bpf/test_run.c        | 55 ++++++++++++++++++++++++++++++++++-------------
 3 files changed, 57 insertions(+), 20 deletions(-)
include/trace/bpf_probe.h +0 -2
index 9391d54d3f12..d1de8f9aa07f 100644
@@ -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 +2 -0
index 0b040a417442..8066249e4ba2 100644
@@ -2072,11 +2072,19 @@ void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
 static __always_inline
 void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 {
+       struct srcu_ctr __percpu *scp = NULL;
        struct bpf_prog *prog = link->link.prog;
+       bool sleepable = prog->sleepable;
        struct bpf_run_ctx *old_run_ctx;

kernel/trace/bpf_trace.c void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) · line 2087

Quoted 10 lines
kernel/trace/bpf_trace.c +7 -1
        struct bpf_trace_run_ctx run_ctx;
 
-       rcu_read_lock_dont_migrate();
+       if (sleepable) {
+               scp = rcu_read_lock_tasks_trace();
+               migrate_disable();
+       } else {
+               rcu_read_lock_dont_migrate();
+       }
+

At first, looking at this repeating pattern I was going to suggest adding some macro, e.g., bpf_prog_run_rcu_pre(prog) and bpf_prog_run_rcu_post(prog) (whatever the name) which would do this dance of sleepable/non-sleepable rcu lock and migration. But then I thought why not have a scoped guard for this? I'd even look into hiding run_ctx manipulation inside it.

Curious what others think, whether it would make sense?

[...]

#3 lore raw

Re: [PATCH bpf-next v8 1/6] bpf: Add sleepable support for raw tracepoint programs

Quoted 80 lines · Andrii Nakryiko
Quoted 59 lines · Mykyta Yatsenko

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_tasks_trace() for lifetime protection with migrate_disable(). For non-sleepable programs, use the regular rcu_read_lock_dont_migrate().

Remove the preempt_disable_notrace/preempt_enable_notrace pair from the faultable tracepoint BPF probe wrapper in bpf_probe.h, since migration protection and RCU locking are now handled per-program inside __bpf_trace_run().

Adapt bpf_prog_test_run_raw_tp() for sleepable programs: select RCU flavor per-program and reject BPF_F_TEST_RUN_ON_CPU since sleepable programs cannot run in hardirq or preempt-disabled context.

This prepares the runtime execution path for both BTF-based raw tracepoints (tp_btf) and classic raw tracepoints (raw_tp) to support sleepable BPF programs on faultable tracepoints (e.g. syscall tracepoints). The verifier changes to allow loading sleepable programs are in a subsequent patch.

---
 include/trace/bpf_probe.h |  2 --
 kernel/trace/bpf_trace.c  | 20 ++++++++++++++---
 net/bpf/test_run.c        | 55 ++++++++++++++++++++++++++++++++++-------------
 3 files changed, 57 insertions(+), 20 deletions(-)
include/trace/bpf_probe.h +0 -2
index 9391d54d3f12..d1de8f9aa07f 100644
@@ -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 +2 -0
index 0b040a417442..8066249e4ba2 100644
@@ -2072,11 +2072,19 @@ void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
 static __always_inline
 void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 {
+       struct srcu_ctr __percpu *scp = NULL;
        struct bpf_prog *prog = link->link.prog;
+       bool sleepable = prog->sleepable;
        struct bpf_run_ctx *old_run_ctx;

kernel/trace/bpf_trace.c void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) · line 2087

Quoted 10 lines
kernel/trace/bpf_trace.c +7 -1
        struct bpf_trace_run_ctx run_ctx;
 
-       rcu_read_lock_dont_migrate();
+       if (sleepable) {
+               scp = rcu_read_lock_tasks_trace();
+               migrate_disable();
+       } else {
+               rcu_read_lock_dont_migrate();
+       }
+

At first, looking at this repeating pattern I was going to suggest adding some macro, e.g., bpf_prog_run_rcu_pre(prog) and bpf_prog_run_rcu_post(prog) (whatever the name) which would do this dance of sleepable/non-sleepable rcu lock and migration. But then I thought why not have a scoped guard for this? I'd even look into hiding run_ctx manipulation inside it.

Quoted 1 line

Curious what others think, whether it would make sense?

I feel copy pasting it in these two places is fine for now. If it spreads then yeah the helper would be needed.

#4 lore raw

PATCH v8 2/6 bpf: Rename bpf_prog_run_array_uprobe() to bpf_prog_run_array_sleepable()

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

bpf_prog_run_array_uprobe() will be used by both uprobes and faultable classic tracepoints. Rename it to bpf_prog_run_array_sleepable() to reflect its general purpose:
per-program RCU flavor selection for sleepable program support.

Add a bool is_uprobe parameter instead of hardcoding run_ctx.is_uprobe = true. This flag controls bpf_get_func_ip_kprobe() address resolution and should only be set for actual uprobe callers.

---
 include/linux/bpf.h         | 7 ++++---
 kernel/trace/trace_uprobe.c | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
include/linux/bpf.h +4 -3
index 05b34a6355b0..9fe19ab21cfc 100644
@@ -2487,8 +2487,9 @@ bpf_prog_run_array(const struct bpf_prog_array *array,
  * rcu-protected dynamically sized maps.
  */
 static __always_inline u32
-bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
-			  const void *ctx, bpf_prog_run_fn run_prog)
+bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
+			     const void *ctx, bpf_prog_run_fn run_prog,
+			     bool is_uprobe)
 {
 	const struct bpf_prog_array_item *item;
 	const struct bpf_prog *prog;
@@ -2504,7 +2505,7 @@ bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
 
 	migrate_disable();
 
-	run_ctx.is_uprobe = true;
+	run_ctx.is_uprobe = is_uprobe;
 
 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
 	item = &array->items[0];
kernel/trace/trace_uprobe.c +1 -1
index 2cabf8a23ec5..eb4d78e689ac 100644
@@ -1402,7 +1402,7 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
 
 		rcu_read_lock_trace();
 		array = rcu_dereference_check(call->prog_array, rcu_read_lock_trace_held());
-		ret = bpf_prog_run_array_uprobe(array, regs, bpf_prog_run);
+		ret = bpf_prog_run_array_sleepable(array, regs, bpf_prog_run, true);
 		rcu_read_unlock_trace();
 		if (!ret)
 			return;
Signature · 1 line
2.52.0
#5 lore raw

Re: [PATCH bpf-next v8 2/6] bpf: Rename bpf_prog_run_array_uprobe() to bpf_prog_run_array_sleepable()

Quoted 36 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

bpf_prog_run_array_uprobe() will be used by both uprobes and faultable classic tracepoints. Rename it to bpf_prog_run_array_sleepable() to reflect its general purpose:
per-program RCU flavor selection for sleepable program support.

Add a bool is_uprobe parameter instead of hardcoding run_ctx.is_uprobe = true. This flag controls bpf_get_func_ip_kprobe() address resolution and should only be set for actual uprobe callers.

---
 include/linux/bpf.h         | 7 ++++---
 kernel/trace/trace_uprobe.c | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
include/linux/bpf.h +3 -2
index 05b34a6355b0..9fe19ab21cfc 100644
@@ -2487,8 +2487,9 @@ bpf_prog_run_array(const struct bpf_prog_array *array,
  * rcu-protected dynamically sized maps.
  */
 static __always_inline u32
-bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
-                         const void *ctx, bpf_prog_run_fn run_prog)
+bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
+                            const void *ctx, bpf_prog_run_fn run_prog,
+                            bool is_uprobe)
 {
        const struct bpf_prog_array_item *item;
        const struct bpf_prog *prog;

include/linux/bpf.h bpf_prog_run_array_uprobe(const struct bpf_prog_array *array, · line 2509

Quoted 6 lines
include/linux/bpf.h +1 -1
@@ -2504,7 +2505,7 @@ bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
 
        migrate_disable();
 
-       run_ctx.is_uprobe = true;
+       run_ctx.is_uprobe = is_uprobe;
 

nah, we can't make it into generic "run_array_sleepable", it's clearly uprobe-specific due to this

Quoted 19 lines
include/linux/bpf.h +0 -0
        old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
        item = &array->items[0];
kernel/trace/trace_uprobe.c +1 -2
index 2cabf8a23ec5..eb4d78e689ac 100644
@@ -1402,7 +1402,7 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
 
                rcu_read_lock_trace();
                array = rcu_dereference_check(call->prog_array, rcu_read_lock_trace_held());
-               ret = bpf_prog_run_array_uprobe(array, regs, bpf_prog_run);
+               ret = bpf_prog_run_array_sleepable(array, regs, bpf_prog_run, true);
                rcu_read_unlock_trace();
                if (!ret)
                        return;
 
--

2.52.0

#6 lore raw

Re: [PATCH bpf-next v8 2/6] bpf: Rename bpf_prog_run_array_uprobe() to bpf_prog_run_array_sleepable()

Quoted 39 lines · Andrii Nakryiko
Quoted 37 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

bpf_prog_run_array_uprobe() will be used by both uprobes and faultable classic tracepoints. Rename it to bpf_prog_run_array_sleepable() to reflect its general purpose:
per-program RCU flavor selection for sleepable program support.

Add a bool is_uprobe parameter instead of hardcoding run_ctx.is_uprobe = true. This flag controls bpf_get_func_ip_kprobe() address resolution and should only be set for actual uprobe callers.

---
  include/linux/bpf.h         | 7 ++++---
  kernel/trace/trace_uprobe.c | 2 +-
  2 files changed, 5 insertions(+), 4 deletions(-)
include/linux/bpf.h +3 -2
index 05b34a6355b0..9fe19ab21cfc 100644
@@ -2487,8 +2487,9 @@ bpf_prog_run_array(const struct bpf_prog_array *array,
   * rcu-protected dynamically sized maps.
   */
  static __always_inline u32
-bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
-                         const void *ctx, bpf_prog_run_fn run_prog)
+bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
+                            const void *ctx, bpf_prog_run_fn run_prog,
+                            bool is_uprobe)
  {
         const struct bpf_prog_array_item *item;
         const struct bpf_prog *prog;
@@ -2504,7 +2505,7 @@ bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
 
Quoted 9 lines

include/linux/bpf.h bpf_prog_run_array_uprobe(const struct bpf_prog_array *array, · line 2509

Quoted 5 lines
include/linux/bpf.h +1 -1
         migrate_disable();
 
-       run_ctx.is_uprobe = true;
+       run_ctx.is_uprobe = is_uprobe;
 

nah, we can't make it into generic "run_array_sleepable", it's clearly uprobe-specific due to this

What's the alternative, though, just make a new bpf_prog_run_array_tracepoint() hardcoding tracepoint-specific implementation into it?

Quoted 19 lines
Quoted 19 lines
include/linux/bpf.h +0 -0
         old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
         item = &array->items[0];
kernel/trace/trace_uprobe.c +1 -2
index 2cabf8a23ec5..eb4d78e689ac 100644
@@ -1402,7 +1402,7 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
 
                 rcu_read_lock_trace();
                 array = rcu_dereference_check(call->prog_array, rcu_read_lock_trace_held());
-               ret = bpf_prog_run_array_uprobe(array, regs, bpf_prog_run);
+               ret = bpf_prog_run_array_sleepable(array, regs, bpf_prog_run, true);
                 rcu_read_unlock_trace();
                 if (!ret)
                         return;
 
--

2.52.0

#7 lore raw

Re: [PATCH bpf-next v8 2/6] bpf: Rename bpf_prog_run_array_uprobe() to bpf_prog_run_array_sleepable()

Quoted 51 lines · Mykyta Yatsenko
Quoted 39 lines · Andrii Nakryiko
Quoted 37 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

bpf_prog_run_array_uprobe() will be used by both uprobes and faultable classic tracepoints. Rename it to bpf_prog_run_array_sleepable() to reflect its general purpose:
per-program RCU flavor selection for sleepable program support.

Add a bool is_uprobe parameter instead of hardcoding run_ctx.is_uprobe = true. This flag controls bpf_get_func_ip_kprobe() address resolution and should only be set for actual uprobe callers.

---
  include/linux/bpf.h         | 7 ++++---
  kernel/trace/trace_uprobe.c | 2 +-
  2 files changed, 5 insertions(+), 4 deletions(-)
include/linux/bpf.h +3 -2
index 05b34a6355b0..9fe19ab21cfc 100644
@@ -2487,8 +2487,9 @@ bpf_prog_run_array(const struct bpf_prog_array *array,
   * rcu-protected dynamically sized maps.
   */
  static __always_inline u32
-bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
-                         const void *ctx, bpf_prog_run_fn run_prog)
+bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
+                            const void *ctx, bpf_prog_run_fn run_prog,
+                            bool is_uprobe)
  {
         const struct bpf_prog_array_item *item;
         const struct bpf_prog *prog;
@@ -2504,7 +2505,7 @@ bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
 
Quoted 9 lines

include/linux/bpf.h bpf_prog_run_array_uprobe(const struct bpf_prog_array *array, · line 2509

Quoted 5 lines
include/linux/bpf.h +1 -1
         migrate_disable();
 
-       run_ctx.is_uprobe = true;
+       run_ctx.is_uprobe = is_uprobe;
 

nah, we can't make it into generic "run_array_sleepable", it's clearly uprobe-specific due to this

Quoted 3 lines

What's the alternative, though, just make a new bpf_prog_run_array_tracepoint() hardcoding tracepoint-specific implementation into it?

you know what, I checked bpf_prog_run_array(), and it turns out we do have .is_uprobe = false bit there, so I guess we baked that field at some point there.

So what you are doing here with configurable is_uprobe flag is fine, let's keep it

Quoted 21 lines
Quoted 20 lines
Quoted 20 lines
include/linux/bpf.h +0 -0
         old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
         item = &array->items[0];
kernel/trace/trace_uprobe.c +1 -2
index 2cabf8a23ec5..eb4d78e689ac 100644
@@ -1402,7 +1402,7 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
 
                 rcu_read_lock_trace();
                 array = rcu_dereference_check(call->prog_array, rcu_read_lock_trace_held());
-               ret = bpf_prog_run_array_uprobe(array, regs, bpf_prog_run);
+               ret = bpf_prog_run_array_sleepable(array, regs, bpf_prog_run, true);
                 rcu_read_unlock_trace();
                 if (!ret)
                         return;
 
--

2.52.0

#8 lore raw

PATCH v8 3/6 bpf: Add sleepable support for classic tracepoint programs

In reply to Mykyta Yatsenko

From: 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_tasks_trace for lifetime protection and bpf_prog_run_array_sleepable() for per-program RCU flavor selection, following the uprobe_prog_run() pattern.

Add per-prog recursion check to bpf_prog_run_array_sleepable(), skipped for uprobes via the is_uprobe flag, to allow sleepable tracepoints to use private stack safely.

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. Since BPF no longer runs within the buffer allocation context, the fake_regs output parameter to perf_trace_buf_alloc() is no longer needed and is replaced with NULL.

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.

This prepares the classic tracepoint runtime and attach paths for sleepable programs. The verifier changes to allow loading sleepable BPF_PROG_TYPE_TRACEPOINT programs are in a subsequent patch.

To: Peter Zijlstra <[email protected]>
To: Steven Rostedt <[email protected]>

---
 include/linux/bpf.h           |  18 ++++++-
 include/linux/trace_events.h  |   6 +++
 kernel/events/core.c          |   9 ++++
 kernel/trace/bpf_trace.c      |  29 +++++++++++
 kernel/trace/trace_syscalls.c | 110 ++++++++++++++++++++++--------------------
 5 files changed, 119 insertions(+), 53 deletions(-)
include/linux/bpf.h +17 -1
index 9fe19ab21cfc..7547c830fcb8 100644
@@ -2486,13 +2486,15 @@ bpf_prog_run_array(const struct bpf_prog_array *array,
  * section and disable preemption for that program alone, so it can access
  * rcu-protected dynamically sized maps.
  */
+void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
+
 static __always_inline u32
 bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
 			     const void *ctx, bpf_prog_run_fn run_prog,
 			     bool is_uprobe)
 {
 	const struct bpf_prog_array_item *item;
-	const struct bpf_prog *prog;
+	struct bpf_prog *prog;
 	struct bpf_run_ctx *old_run_ctx;
 	struct bpf_trace_run_ctx run_ctx;
 	u32 ret = 1;
@@ -2513,8 +2515,22 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
 		if (!prog->sleepable)
 			rcu_read_lock();
 
+		/* Per-prog recursion check to enable private stack. */
+		if (!is_uprobe &&
+		    unlikely(!bpf_prog_get_recursion_context(prog))) {
+			bpf_prog_inc_misses_counter(prog);
+			bpf_prog_put_recursion_context(prog);
+			if (!prog->sleepable)
+				rcu_read_unlock();
+			item++;
+			continue;
+		}
+
 		run_ctx.bpf_cookie = item->bpf_cookie;
 		ret &= run_prog(prog, ctx);
+
+		if (!is_uprobe)
+			bpf_prog_put_recursion_context(prog);
 		item++;
 
 		if (!prog->sleepable)
include/linux/trace_events.h +6 -0
index 37eb2f0f3dd8..5fbbeb9ec4b9 100644
@@ -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 *c
 	return 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
index 89b40e439717..054127525376 100644
@@ -11644,6 +11644,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 +29 -0
index 8066249e4ba2..efe13995c38a 100644
@@ -152,6 +152,35 @@ 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 (syscall
+ * tracepoints). Supports sleepable BPF programs by using rcu_tasks_trace
+ * for lifetime protection and bpf_prog_run_array_sleepable() for per-program
+ * RCU flavor selection, following the uprobe pattern.
+ *
+ * Recursion protection is unnecessary here (hence not touching bpf_prog_active
+ * compared to trace_call_bpf()), because syscall tracepoints fire only at
+ * syscall entry/exit boundaries, self-recursion is impossible.
+ *
+ * Must be called from a faultable/preemptible context.
+ */
+unsigned int trace_call_bpf_faultable(struct trace_event_call *call, void *ctx)
+{
+	struct bpf_prog_array *prog_array;
+
+	might_fault();
+	guard(rcu_tasks_trace)();
+
+	prog_array = rcu_dereference_check(call->prog_array,
+					   rcu_read_lock_trace_held());
+	return bpf_prog_run_array_sleepable(prog_array, ctx, bpf_prog_run,
+					    false);
+}
+
 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
 {
kernel/trace/trace_syscalls.c +58 -52

208 lines not on this page · raw message

Signature · 1 line
2.52.0
#9 lore raw

Re: [PATCH bpf-next v8 3/6] bpf: Add sleepable support for classic tracepoint programs

Quoted 39 lines · Mykyta Yatsenko

From: 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_tasks_trace for lifetime protection and bpf_prog_run_array_sleepable() for per-program RCU flavor selection, following the uprobe_prog_run() pattern.

Add per-prog recursion check to bpf_prog_run_array_sleepable(), skipped for uprobes via the is_uprobe flag, to allow sleepable tracepoints to use private stack safely.

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. Since BPF no longer runs within the buffer allocation context, the fake_regs output parameter to perf_trace_buf_alloc() is no longer needed and is replaced with NULL.

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.

This prepares the classic tracepoint runtime and attach paths for sleepable programs. The verifier changes to allow loading sleepable BPF_PROG_TYPE_TRACEPOINT programs are in a subsequent patch.

To: Peter Zijlstra <[email protected]>
To: Steven Rostedt <[email protected]>

Quoted 8 lines
---
 include/linux/bpf.h           |  18 ++++++-
 include/linux/trace_events.h  |   6 +++
 kernel/events/core.c          |   9 ++++
 kernel/trace/bpf_trace.c      |  29 +++++++++++
 kernel/trace/trace_syscalls.c | 110 ++++++++++++++++++++++--------------------
 5 files changed, 119 insertions(+), 53 deletions(-)

lgtm, we'll have a bit of slowdown due to memzeroing fake pt_regs every single time, but that's probably acceptable

[...]

Quoted 10 lines
include/linux/bpf.h +6 -0
@@ -2513,8 +2515,22 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
                if (!prog->sleepable)
                        rcu_read_lock();
 
+               /* Per-prog recursion check to enable private stack. */
+               if (!is_uprobe &&
+                   unlikely(!bpf_prog_get_recursion_context(prog))) {
+                       bpf_prog_inc_misses_counter(prog);
+                       bpf_prog_put_recursion_context(prog);
+                       if (!prog->sleepable)

include/linux/bpf.h bpf_prog_run_array_sleepable(const struct bpf_prog_array *array, · line 2533

Quoted 10 lines
include/linux/bpf.h +8 -0
+                               rcu_read_unlock();
+                       item++;
+                       continue;
+               }
+
                run_ctx.bpf_cookie = item->bpf_cookie;
                ret &= run_prog(prog, ctx);
+
+               if (!is_uprobe)
+                       bpf_prog_put_recursion_context(prog);

looking at all this is_uprobe special casing now I do feel like a separate bpf_prog_run_array_sleepable vs bpf_prog_run_array_uprobe makes more sense

Quoted 7 lines
include/linux/bpf.h +0 -0
                item++;
 
                if (!prog->sleepable)
include/linux/trace_events.h +0 -0
index 37eb2f0f3dd8..5fbbeb9ec4b9 100644

[...]

Quoted 14 lines
include/linux/trace_events.h +0 -5
@@ -1411,12 +1411,7 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
        int size = 0;
        int uargs = 0;
 
-       /*
-        * Syscall probe called with preemption enabled, but the ring
-        * buffer and per-cpu data require preemption to be disabled.
-        */
        might_fault();
-       guard(preempt_notrace)();
 
        syscall_nr = trace_get_syscall_nr(current, regs);
        if (syscall_nr < 0 || syscall_nr >= NR_syscalls)

include/linux/trace_events.h static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id) · line 1429

Quoted 5 lines
include/linux/trace_events.h +2 -0
@@ -1430,6 +1425,26 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
 
        syscall_get_arguments(current, regs, args);
 
+       /*
+        * Run BPF filter in faultable context before per-cpu buffer

BPF program, it's not a filter

Quoted 4 lines
include/linux/trace_events.h +4 -0
+        * allocation, allowing sleepable BPF programs to execute.
+        */
+       valid_prog_array = bpf_prog_array_valid(sys_data->enter_event);
+       if (valid_prog_array &&

[...]

Quoted 10 lines
include/linux/trace_events.h +0 -5
-       /*
-        * Syscall probe called with preemption enabled, but the ring
-        * buffer and per-cpu data require preemption to be disabled.
-        */
        might_fault();
-       guard(preempt_notrace)();
 
        syscall_nr = trace_get_syscall_nr(current, regs);
        if (syscall_nr < 0 || syscall_nr >= NR_syscalls)

include/linux/trace_events.h static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret) · line 1562

Quoted 6 lines
include/linux/trace_events.h +2 -1
@@ -1560,29 +1558,37 @@ static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
        if (!sys_data)
                return;
 
-       head = this_cpu_ptr(sys_data->exit_event->perf_events);
+       /*
+        * Run BPF filter in faultable context before per-cpu buffer

again, BPF program, not filter

Quoted 9 lines
include/linux/trace_events.h +7 -1
+        * allocation, allowing sleepable BPF programs to execute.
+        */
        valid_prog_array = bpf_prog_array_valid(sys_data->exit_event);
-       if (!valid_prog_array && hlist_empty(head))
+       if (valid_prog_array &&
+           !perf_call_bpf_exit(sys_data->exit_event, syscall_nr,
+                               syscall_get_return_value(current, regs)))
+               return;
+

[...]

#10 lore raw

PATCH v8 4/6 bpf: Verifier support for sleepable tracepoint programs

In reply to Mykyta Yatsenko

From: 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.

The attach-time check for classic tracepoints (tp) in __perf_event_set_bpf_prog() was added in the previous patch.

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
index 51ade3cde8bb..047fafaad4b1 100644
@@ -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
index 8c1cf2eb6cbb..ddf74b9fb5c6 100644
@@ -25339,6 +25339,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);
@@ -25555,6 +25561,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;
@@ -25562,7 +25569,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)
@@ -25584,7 +25593,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
#11 lore raw

PATCH v8 5/6 libbpf: Add section handlers for sleepable tracepoints

In reply to Mykyta Yatsenko

From: 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 | 39 ++++++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf.c +32 -7
index 9ea41f40dc82..a353dc7a4d41 100644
@@ -9982,11 +9982,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),
@@ -13109,23 +13114,41 @@ 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;
 
-	/* no auto-attach for SEC("tp") or SEC("tracepoint") */
-	if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0)
+	/* no auto-attach for bare SEC("tp"), SEC("tracepoint") and .s variants */
+	if (strcmp(prog->sec_name, "tp") == 0 ||
+	    strcmp(prog->sec_name, "tracepoint") == 0 ||
+	    strcmp(prog->sec_name, "tp.s") == 0 ||
+	    strcmp(prog->sec_name, "tracepoint.s") == 0)
 		return 0;
 
 	sec_name = strdup(prog->sec_name);
 	if (!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);
@@ -13189,6 +13212,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
#12 lore raw

Re: [PATCH bpf-next v8 5/6] libbpf: Add section handlers for sleepable tracepoints

Quoted 56 lines · Mykyta Yatsenko

From: 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 | 39 ++++++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf.c +12 -0
index 9ea41f40dc82..a353dc7a4d41 100644
@@ -9982,11 +9982,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),
@@ -13109,23 +13114,41 @@ 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;
 

tools/lib/bpf/libbpf.c struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog, · line 13133

Quoted 10 lines
tools/lib/bpf/libbpf.c +5 -2
        *link = NULL;
 
-       /* no auto-attach for SEC("tp") or SEC("tracepoint") */
-       if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0)
+       /* no auto-attach for bare SEC("tp"), SEC("tracepoint") and .s variants */
+       if (strcmp(prog->sec_name, "tp") == 0 ||
+           strcmp(prog->sec_name, "tracepoint") == 0 ||
+           strcmp(prog->sec_name, "tp.s") == 0 ||
+           strcmp(prog->sec_name, "tracepoint.s") == 0)
                return 0;

it makes no sense to hard-code explicit strcmp() literal strings here while having prefixes array. why not do what attach_raw_tp() does and use just prefixes uniformly?

pw-bot: cr

Quoted 39 lines
tools/lib/bpf/libbpf.c +15 -6
        sec_name = strdup(prog->sec_name);
        if (!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);
@@ -13189,6 +13212,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;
 
--

2.52.0

#13 lore raw

Re: [PATCH bpf-next v8 5/6] libbpf: Add section handlers for sleepable tracepoints

Quoted 73 lines · Andrii Nakryiko
Quoted 56 lines · Mykyta Yatsenko

From: 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 | 39 ++++++++++++++++++++++++++++++++-------
  1 file changed, 32 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf.c +12 -0
index 9ea41f40dc82..a353dc7a4d41 100644
@@ -9982,11 +9982,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),
@@ -13109,23 +13114,41 @@ 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;
 

tools/lib/bpf/libbpf.c struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog, · line 13133

Quoted 10 lines
tools/lib/bpf/libbpf.c +5 -2
         *link = NULL;
 
-       /* no auto-attach for SEC("tp") or SEC("tracepoint") */
-       if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0)
+       /* no auto-attach for bare SEC("tp"), SEC("tracepoint") and .s variants */
+       if (strcmp(prog->sec_name, "tp") == 0 ||
+           strcmp(prog->sec_name, "tracepoint") == 0 ||
+           strcmp(prog->sec_name, "tp.s") == 0 ||
+           strcmp(prog->sec_name, "tracepoint.s") == 0)
                 return 0;

it makes no sense to hard-code explicit strcmp() literal strings here while having prefixes array. why not do what attach_raw_tp() does and use just prefixes uniformly?

Quoted 3 lines

pw-bot: cr

Thanks for taking a look! I'll fix this and other comments for the next version.

Quoted 39 lines
Quoted 39 lines
tools/lib/bpf/libbpf.c +15 -6
         sec_name = strdup(prog->sec_name);
         if (!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);
@@ -13189,6 +13212,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;
 
--

2.52.0

#14 lore raw

PATCH v8 6/6 selftests/bpf: Add tests for sleepable tracepoint programs

In reply to Mykyta Yatsenko

From: 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:
* BTF-based raw tracepoint using SEC("tp_btf.s/sys_enter")
* Classic raw tracepoint using SEC("raw_tp.s/sys_enter")
* Classic tracepoint using SEC("tp.s/syscalls/sys_enter_nanosleep") receiving
struct syscall_trace_enter with direct access to args[].
* Sleepable raw_tp executed via bpf_prog_test_run_raw_tp()

Add a negative test (handle_sched_switch) 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         | 153 +++++++++++++++++++++
 .../bpf/progs/test_sleepable_tracepoints.c         | 128 +++++++++++++++++
 .../bpf/progs/test_sleepable_tracepoints_fail.c    |  18 +++
 tools/testing/selftests/bpf/verifier/sleepable.c   |  17 ++-
 4 files changed, 314 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sleepable_tracepoints.c new file +153 -0
new file mode 100644
index 000000000000..66d5146b88fa
@@ -0,0 +1,153 @@
+// 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"
+#include "test_sleepable_tracepoints_fail.skel.h"
+
+static void trigger_nanosleep(void)
+{
+	syscall(__NR_nanosleep, &(struct timespec){ .tv_nsec = 555 }, NULL);
+}
+
+static void run_test(struct test_sleepable_tracepoints *skel)
+{
+	skel->bss->target_pid = getpid();
+	skel->bss->triggered = 0;
+	skel->bss->err = 0;
+	skel->bss->copied_tv_nsec = 0;
+
+	trigger_nanosleep();
+
+	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");
+}
+
+static void run_auto_attach_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;
+
+	run_test(skel);
+	bpf_link__destroy(link);
+}
+
+void test_sleepable_tracepoints(void)
+{
+	struct test_sleepable_tracepoints *skel;
+	struct bpf_link *link;
+	int err;
+
+	skel = test_sleepable_tracepoints__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+		return;
+
+	/* Primary functional tests: full bpf_copy_from_user exercise */
+
+	if (test__start_subtest("tp_btf"))
+		run_auto_attach_test(skel->progs.handle_sys_enter_tp_btf, skel);
+
+	if (test__start_subtest("raw_tp"))
+		run_auto_attach_test(skel->progs.handle_sys_enter_raw_tp, skel);
+
+	if (test__start_subtest("tracepoint"))
+		run_auto_attach_test(skel->progs.handle_sys_enter_tp, skel);
+
+	/* Alias SEC variants: verify libbpf prefix parsing */
+
+	if (test__start_subtest("tracepoint_alias")) {
+		link = bpf_program__attach(skel->progs.handle_sys_enter_tp_alias);
+		if (ASSERT_OK_PTR(link, "tp_alias_attach"))
+			bpf_link__destroy(link);
+	}
+
+	if (test__start_subtest("raw_tracepoint_alias")) {
+		link = bpf_program__attach(skel->progs.handle_sys_enter_raw_tp_alias);
+		if (ASSERT_OK_PTR(link, "raw_tp_alias_attach"))
+			bpf_link__destroy(link);
+	}
+
+	/* Bare SEC variants: verify manual attach */
+
+	if (test__start_subtest("raw_tp_bare")) {
+		link = bpf_program__attach_raw_tracepoint(skel->progs.handle_raw_tp_bare,
+							  "sys_enter");
+		if (ASSERT_OK_PTR(link, "raw_tp_bare_attach"))
+			bpf_link__destroy(link);
+	}
+
+	if (test__start_subtest("tp_bare")) {
+		link = bpf_program__attach_tracepoint(skel->progs.handle_tp_bare, "syscalls",
+						      "sys_enter_nanosleep");
+		if (ASSERT_OK_PTR(link, "tp_bare_attach"))
+			bpf_link__destroy(link);
+	}
+
+	/* Sys exit test */
+
+	if (test__start_subtest("sys_exit")) {
+		link = bpf_program__attach(skel->progs.handle_sys_exit_tp);
+		if (ASSERT_OK_PTR(link, "sys_exit_attach")) {
+			skel->bss->target_pid = getpid();
+			skel->bss->exit_triggered = 0;
+
+			trigger_nanosleep();
+
+			ASSERT_EQ(skel->bss->exit_triggered, 1, "exit_triggered");
+			bpf_link__destroy(link);
+		}
+	}
+
+	/* BPF_PROG_TEST_RUN: exercise bpf_prog_test_run_raw_tp() */
+
+	if (test__start_subtest("test_run")) {
+		__u64 args[2] = {0x1234ULL, 0x5678ULL};
+		int prog_fd;
+		LIBBPF_OPTS(bpf_test_run_opts, topts,
+			.ctx_in = args,
+			.ctx_size_in = sizeof(args),
+		);
+
+		prog_fd = bpf_program__fd(skel->progs.handle_test_run);
+		err = bpf_prog_test_run_opts(prog_fd, &topts);
+		ASSERT_OK(err, "test_run");
+		ASSERT_EQ(topts.retval, 0x1234 + 0x5678, "test_run_retval");
+	}
+
+	if (test__start_subtest("test_run_on_cpu_reject")) {
+		__u64 args[2] = {0x1234ULL, 0x5678ULL};
+		int prog_fd;
+		LIBBPF_OPTS(bpf_test_run_opts, topts,
+			.ctx_in = args,
+			.ctx_size_in = sizeof(args),
+			.flags = BPF_F_TEST_RUN_ON_CPU,
+		);
+
+		prog_fd = bpf_program__fd(skel->progs.handle_test_run);
+		err = bpf_prog_test_run_opts(prog_fd, &topts);
+		ASSERT_ERR(err, "test_run_on_cpu_should_fail");
+		ASSERT_EQ(errno, EINVAL, "test_run_on_cpu_errno");
+	}
+
+	/* Negative: attach-time rejection on non-faultable tracepoints */
+
+	if (test__start_subtest("raw_tp_non_faultable")) {
+		link = bpf_program__attach(skel->progs.handle_raw_tp_non_faultable);
+		ASSERT_ERR_PTR(link, "raw_tp_non_faultable_attach");
+	}
+
+	if (test__start_subtest("tp_non_syscall")) {
+		link = bpf_program__attach(skel->progs.handle_tp_non_syscall);
+		ASSERT_ERR_PTR(link, "tp_non_syscall_attach");
+	}
+
+	test_sleepable_tracepoints__destroy(skel);
+
+	/* Negative: load-time rejection (separate BPF object) */
+	RUN_TESTS(test_sleepable_tracepoints_fail);
+}
tools/testing/selftests/bpf/progs/test_sleepable_tracepoints.c new file +128 -0

128 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_sleepable_tracepoints_fail.c new file +18 -0
new file mode 100644
index 000000000000..1a0748a9520b
@@ -0,0 +1,18 @@
+// 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>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+/* Sleepable program on a non-faultable tracepoint should fail to load */
+SEC("tp_btf.s/sched_switch")
+__failure __msg("Sleepable program cannot attach to non-faultable tracepoint")
+int BPF_PROG(handle_sched_switch, bool preempt,
+	     struct task_struct *prev, struct task_struct *next)
+{
+	return 0;
+}
tools/testing/selftests/bpf/verifier/sleepable.c +15 -2
index 1f0d2bdc673f..6dabc5522945 100644
@@ -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
#15 lore raw

Re: [PATCH bpf-next v8 6/6] selftests/bpf: Add tests for sleepable tracepoint programs

Quoted 30 lines · Mykyta Yatsenko

From: 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:
* BTF-based raw tracepoint using SEC("tp_btf.s/sys_enter")
* Classic raw tracepoint using SEC("raw_tp.s/sys_enter")
* Classic tracepoint using SEC("tp.s/syscalls/sys_enter_nanosleep") receiving
struct syscall_trace_enter with direct access to args[].
* Sleepable raw_tp executed via bpf_prog_test_run_raw_tp()

Add a negative test (handle_sched_switch) 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         | 153 +++++++++++++++++++++
 .../bpf/progs/test_sleepable_tracepoints.c         | 128 +++++++++++++++++
 .../bpf/progs/test_sleepable_tracepoints_fail.c    |  18 +++
 tools/testing/selftests/bpf/verifier/sleepable.c   |  17 ++-
 4 files changed, 314 insertions(+), 2 deletions(-)

[...]

Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/sleepable_tracepoints.c new file +24 -0
+void test_sleepable_tracepoints(void)
+{
+       struct test_sleepable_tracepoints *skel;
+       struct bpf_link *link;
+       int err;
+
+       skel = test_sleepable_tracepoints__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+               return;
+
+       /* Primary functional tests: full bpf_copy_from_user exercise */
+
+       if (test__start_subtest("tp_btf"))
+               run_auto_attach_test(skel->progs.handle_sys_enter_tp_btf, skel);
+
+       if (test__start_subtest("raw_tp"))
+               run_auto_attach_test(skel->progs.handle_sys_enter_raw_tp, skel);
+
+       if (test__start_subtest("tracepoint"))
+               run_auto_attach_test(skel->progs.handle_sys_enter_tp, skel);
+
+       /* Alias SEC variants: verify libbpf prefix parsing */
+
+       if (test__start_subtest("tracepoint_alias")) {

tools/testing/selftests/bpf/prog_tests/sleepable_tracepoints.c line 73

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/sleepable_tracepoints.c new file +10 -0
+               link = bpf_program__attach(skel->progs.handle_sys_enter_tp_alias);
+               if (ASSERT_OK_PTR(link, "tp_alias_attach"))
+                       bpf_link__destroy(link);
+       }
+
+       if (test__start_subtest("raw_tracepoint_alias")) {
+               link = bpf_program__attach(skel->progs.handle_sys_enter_raw_tp_alias);
+               if (ASSERT_OK_PTR(link, "raw_tp_alias_attach"))
+                       bpf_link__destroy(link);
+       }

why alias tests are different? and why not have an array

struct { const char *name; struct bpf_program *prog } cases[] = {
{ "tp_btf", skel->progs.handle_sys_enter_tp_btf},

    ...
}

and then do the tests in a loop?

Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/sleepable_tracepoints.c new file +17 -0
+
+       /* Bare SEC variants: verify manual attach */
+
+       if (test__start_subtest("raw_tp_bare")) {
+               link = bpf_program__attach_raw_tracepoint(skel->progs.handle_raw_tp_bare,
+                                                         "sys_enter");
+               if (ASSERT_OK_PTR(link, "raw_tp_bare_attach"))
+                       bpf_link__destroy(link);
+       }
+
+       if (test__start_subtest("tp_bare")) {
+               link = bpf_program__attach_tracepoint(skel->progs.handle_tp_bare, "syscalls",
+                                                     "sys_enter_nanosleep");
+               if (ASSERT_OK_PTR(link, "tp_bare_attach"))
+                       bpf_link__destroy(link);
+       }
+

[...]

tools/testing/selftests/bpf/progs/test_sleepable_tracepoints.c line 42

Quoted 13 lines
tools/testing/selftests/bpf/progs/test_sleepable_tracepoints.c new file +13 -0
+
+static __always_inline bool is_target_nanosleep(long id)
+{
+       return (bpf_get_current_pid_tgid() >> 32) == target_pid &&
+              id == __NR_nanosleep;
+}
+
+/* Primary functional tests: full bpf_copy_from_user exercise */
+
+SEC("tp_btf.s/sys_enter")
+int BPF_PROG(handle_sys_enter_tp_btf, struct pt_regs *regs, long id)
+{
+       if (!is_target_nanosleep(id))

inline the check

tools/testing/selftests/bpf/progs/test_sleepable_tracepoints.c line 51

Quoted 9 lines
tools/testing/selftests/bpf/progs/test_sleepable_tracepoints.c new file +9 -0
+               return 0;
+
+       return copy_nanosleep_arg((void *)PT_REGS_PARM1_SYSCALL(regs));
+}
+
+SEC("raw_tp.s/sys_enter")
+int BPF_PROG(handle_sys_enter_raw_tp, struct pt_regs *regs, long id)
+{
+       if (!is_target_nanosleep(id))

same, just takes longer to find what is_target_nanosleep() does for no added benefit

Quoted 5 lines
tools/testing/selftests/bpf/progs/test_sleepable_tracepoints.c new file +5 -0
+               return 0;
+
+       return copy_nanosleep_arg((void *)PT_REGS_PARM1_CORE_SYSCALL(regs));
+}
+

[...]

#16 lore raw
In reply to Mykyta Yatsenko
Quoted 3 lines · Mykyta Yatsenko

This series adds support for sleepable BPF programs attached to raw tracepoints (tp_btf), classic raw tracepoints (raw_tp), and classic

"classic raw tracepoints" is confusing, let's keep "tp" as "classic". tp_btf vs raw_tp is just about BTF-awareness. I'd just say that you add support for sleepable BPF program for raw tracepoints (tp_btf, raw_tp) and classic tracepoints (tp).

Quoted 126 lines

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.

This series removes 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 migration protection and RCU locking are now managed per-program inside __bpf_trace_run().

Patch 2 renames bpf_prog_run_array_uprobe() to bpf_prog_run_array_sleepable() to support new usecase.

Patch 3 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. trace_call_bpf_faultable() uses rcu_tasks_trace for lifetime protection, following the uprobe pattern. This adds rcu_tasks_trace overhead for all classic tracepoint BPF programs on syscall tracepoints, not just sleepable ones.

Patch 4 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 5 adds libbpf SEC_DEF handlers: tp_btf.s, raw_tp.s, raw_tracepoint.s, tp.s, and tracepoint.s.

Patch 6 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 v8:
- Fix sleepable tracepoint support in bpf_prog_test_run() (Kumar, sashiko)
- Link to v7: https://patch.msgid.link/[email protected]

Changes in v7:
- Add recursion check (bpf_prog_get_recursion_context()) to make sure
private stack is safe when sleepable program is preempted by itself
(Alexei, Kumar)
- Use combined rcu_read_lock_dont_migrate() instead of separate rcu_read_lock()/migrate_disable() calls for non-sleepable path (Alexei)
- Link to v6: https://lore.kernel.org/bpf/[email protected]/

Changes in v6:
- Remove recursion check from trace_call_bpf_faultable(), sleepable
tracepoints are called from syscall enter/exit, no recursion is
possible.(Kumar)
- Refactor bpf_prog_run_array_uprobe() to support tracepoints
usecase cleanly (Kumar)
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Addressed AI review: zero initialize struct pt_regs in
perf_call_bpf_enter(); changed handling tp.s and tracepoint.s in
attach_tp() in libbpf.
- Updated commit messages
- Link to v4: https://lore.kernel.org/r/[email protected]

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
- 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 (6):
bpf: Add sleepable support for raw tracepoint programs
bpf: Rename bpf_prog_run_array_uprobe() to bpf_prog_run_array_sleepable()
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/bpf.h                                |  25 +++-
 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                           |  49 ++++++-
 kernel/trace/trace_syscalls.c                      | 110 ++++++++-------
 kernel/trace/trace_uprobe.c                        |   2 +-
 net/bpf/test_run.c                                 |  55 ++++++--
 tools/lib/bpf/libbpf.c                             |  39 +++++-
 .../bpf/prog_tests/sleepable_tracepoints.c         | 153 +++++++++++++++++++++
 .../bpf/progs/test_sleepable_tracepoints.c         | 128 +++++++++++++++++
 .../bpf/progs/test_sleepable_tracepoints_fail.c    |  18 +++
 tools/testing/selftests/bpf/verifier/sleepable.c   |  17 ++-
 15 files changed, 543 insertions(+), 88 deletions(-)

---
base-commit: 596817673dc2d226bdacb72d7a03a328966f563b
change-id: 20260216-sleepable_tracepoints-381ae1410550

Best regards,
--
Mykyta Yatsenko <[email protected]>