Skip to contentSkip to conversation

bpf: Add support for sleepable raw tracepoint programs

PATCH Superseded Mykyta Yatsenko 8 participants 23 messages Mar 11 – Mar 12

Subsystems (8)
Kselftest BPF Tracing BPF selftests +4 more subsystems

v3 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v3 cover bpf: Add support for sleepable raw tracepoint programs

This series adds support for sleepable BPF programs attached to raw tracepoints (tp_btf). The motivation is to allow BPF programs on syscall tracepoints to use sleepable helpers such as bpf_copy_from_user(), enabling reliable user memory reads that can page-fault.

Currently, raw tracepoint BPF programs always run with RCU read lock held 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:
use migrate_disable() for per-CPU data protection on both paths, adding rcu_read_lock() only for non-sleepable programs. For sleepable programs, call might_fault() to annotate faultable context and rely on the outer rcu_tasks_trace lock from the faultable tracepoint callback for program lifetime protection. 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 allows BPF_TRACE_RAW_TP programs to be loaded as sleepable, and adds a load-time check in bpf_check_attach_target() to reject sleepable programs targeting non-faultable tracepoints with a verifier error message.

Patch 3 adds the tp_btf.s section handler in libbpf, following the existing pattern of fentry.s/fexit.s/lsm.s.

Patch 4 adds selftests covering both the positive case (sleepable program on sys_enter using bpf_copy_from_user() to read user memory) and the negative case (sleepable program rejected on sched_switch at load time via RUN_TESTS).

---
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 (4):
bpf: Add sleepable execution path for raw tracepoint programs
bpf: Verifier support for sleepable raw tracepoint programs
libbpf: Add tp_btf.s section handler for sleepable raw tracepoints selftests/bpf: Add tests for sleepable raw tracepoint programs

 include/trace/bpf_probe.h                          |  2 -
 kernel/bpf/verifier.c                              |  9 ++++-
 kernel/trace/bpf_trace.c                           | 13 +++++--
 tools/lib/bpf/libbpf.c                             |  1 +
 .../selftests/bpf/prog_tests/sleepable_raw_tp.c    | 40 ++++++++++++++++++++
 .../selftests/bpf/progs/test_sleepable_raw_tp.c    | 43 ++++++++++++++++++++++
 .../bpf/progs/test_sleepable_raw_tp_fail.c         | 18 +++++++++
 tools/testing/selftests/bpf/verifier/sleepable.c   | 17 ++++++++-
 8 files changed, 135 insertions(+), 8 deletions(-)

---
base-commit: bd2e02e3c9215305dfa344c050d5822f19929cf7
change-id: 20260216-sleepable_tracepoints-381ae1410550

Best regards,

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

PATCH v3 1/4 bpf: Add sleepable execution path for raw tracepoint programs

From: Mykyta Yatsenko <[email protected]>

Modify __bpf_trace_run() to support both sleepable and non-sleepable BPF programs. When the program is sleepable:

- Call might_fault() to annotate the faultable context
- Use migrate_disable()/migrate_enable() instead of rcu_read_lock()/rcu_read_unlock() to allow sleeping while still protecting percpu data access
- The outer rcu_tasks_trace lock is already held by the faultable tracepoint callback (__DECLARE_TRACE_SYSCALL), providing lifetime protection for the BPF program

For non-sleepable programs, rcu_read_lock_dont_migrate() is replaced with explicit migrate_disable()/rcu_read_lock() pairing.

Remove preempt_disable_notrace()/preempt_enable_notrace() from __BPF_DECLARE_TRACE_SYSCALL. Per-CPU protection and RCU locking are now managed per-program inside __bpf_trace_run().

---
 include/trace/bpf_probe.h |  2 --
 kernel/trace/bpf_trace.c  | 13 ++++++++++---
 2 files changed, 10 insertions(+), 5 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 +10 -3
index 0b040a417442..3688a7e115d1 100644
@@ -2076,7 +2076,7 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 	struct bpf_run_ctx *old_run_ctx;
 	struct bpf_trace_run_ctx run_ctx;
 
-	rcu_read_lock_dont_migrate();
+	migrate_disable();
 	if (unlikely(!bpf_prog_get_recursion_context(prog))) {
 		bpf_prog_inc_misses_counter(prog);
 		goto out;
@@ -2085,12 +2085,19 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 	run_ctx.bpf_cookie = link->cookie;
 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
 
-	(void) bpf_prog_run(prog, args);
+	if (prog->sleepable) {
+		might_fault();
+		(void)bpf_prog_run(prog, args);
+	} else {
+		rcu_read_lock();
+		(void)bpf_prog_run(prog, args);
+		rcu_read_unlock();
+	}
 
 	bpf_reset_run_ctx(old_run_ctx);
 out:
 	bpf_prog_put_recursion_context(prog);
-	rcu_read_unlock_migrate();
+	migrate_enable();
 }
 
 #define UNPACK(...)			__VA_ARGS__
Signature · 1 line
2.52.0
reviewer Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH bpf-next v3 1/4] bpf: Add sleepable execution path for raw tracepoint programs

Quoted 16 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Modify __bpf_trace_run() to support both sleepable and non-sleepable BPF programs. When the program is sleepable:

- Call might_fault() to annotate the faultable context
- Use migrate_disable()/migrate_enable() instead of rcu_read_lock()/rcu_read_unlock() to allow sleeping while still protecting percpu data access
- The outer rcu_tasks_trace lock is already held by the faultable tracepoint callback (__DECLARE_TRACE_SYSCALL), providing lifetime protection for the BPF program

For non-sleepable programs, rcu_read_lock_dont_migrate() is replaced with explicit migrate_disable()/rcu_read_lock() pairing.

Quoted 6 lines

Remove preempt_disable_notrace()/preempt_enable_notrace() from __BPF_DECLARE_TRACE_SYSCALL. Per-CPU protection and RCU locking are now managed per-program inside __bpf_trace_run().

Quoted 54 lines
---
 include/trace/bpf_probe.h |  2 --
 kernel/trace/bpf_trace.c  | 13 ++++++++++---
 2 files changed, 10 insertions(+), 5 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 +10 -3
index 0b040a417442..3688a7e115d1 100644
@@ -2076,7 +2076,7 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 	struct bpf_run_ctx *old_run_ctx;
 	struct bpf_trace_run_ctx run_ctx;
 
-	rcu_read_lock_dont_migrate();
+	migrate_disable();
 	if (unlikely(!bpf_prog_get_recursion_context(prog))) {
 		bpf_prog_inc_misses_counter(prog);
 		goto out;
@@ -2085,12 +2085,19 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 	run_ctx.bpf_cookie = link->cookie;
 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
 
-	(void) bpf_prog_run(prog, args);
+	if (prog->sleepable) {
+		might_fault();
+		(void)bpf_prog_run(prog, args);
+	} else {
+		rcu_read_lock();
+		(void)bpf_prog_run(prog, args);
+		rcu_read_unlock();
+	}
 
 	bpf_reset_run_ctx(old_run_ctx);
 out:
 	bpf_prog_put_recursion_context(prog);
-	rcu_read_unlock_migrate();
+	migrate_enable();
 }
 
 #define UNPACK(...)			__VA_ARGS__
Acked-by on v3 1/4
#3 lore raw

Re: [PATCH bpf-next v3 1/4] bpf: Add sleepable execution path for raw tracepoint programs

In reply to Mykyta Yatsenko
Quoted 16 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Modify __bpf_trace_run() to support both sleepable and non-sleepable BPF programs. When the program is sleepable:

- Call might_fault() to annotate the faultable context
- Use migrate_disable()/migrate_enable() instead of rcu_read_lock()/rcu_read_unlock() to allow sleeping while still protecting percpu data access
- The outer rcu_tasks_trace lock is already held by the faultable tracepoint callback (__DECLARE_TRACE_SYSCALL), providing lifetime protection for the BPF program

For non-sleepable programs, rcu_read_lock_dont_migrate() is replaced with explicit migrate_disable()/rcu_read_lock() pairing.

Quoted 6 lines

Remove preempt_disable_notrace()/preempt_enable_notrace() from __BPF_DECLARE_TRACE_SYSCALL. Per-CPU protection and RCU locking are now managed per-program inside __bpf_trace_run().

Quoted 57 lines
---
 include/trace/bpf_probe.h |  2 --
 kernel/trace/bpf_trace.c  | 13 ++++++++++---
 2 files changed, 10 insertions(+), 5 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 +10 -3
index 0b040a417442..3688a7e115d1 100644
@@ -2076,7 +2076,7 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 	struct bpf_run_ctx *old_run_ctx;
 	struct bpf_trace_run_ctx run_ctx;
 
-	rcu_read_lock_dont_migrate();
+	migrate_disable();
 	if (unlikely(!bpf_prog_get_recursion_context(prog))) {
 		bpf_prog_inc_misses_counter(prog);
 		goto out;
@@ -2085,12 +2085,19 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
 	run_ctx.bpf_cookie = link->cookie;
 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
 
-	(void) bpf_prog_run(prog, args);
+	if (prog->sleepable) {
+		might_fault();
+		(void)bpf_prog_run(prog, args);
+	} else {
+		rcu_read_lock();
+		(void)bpf_prog_run(prog, args);
+		rcu_read_unlock();
+	}
 
 	bpf_reset_run_ctx(old_run_ctx);
 out:
 	bpf_prog_put_recursion_context(prog);
-	rcu_read_unlock_migrate();
+	migrate_enable();
 }
 
 #define UNPACK(...)			__VA_ARGS__
Signature · 2 lines
-- 
2.52.0
#4 lore raw

Re: [PATCH bpf-next v3 1/4] bpf: Add sleepable execution path for raw tracepoint programs

In reply to Mykyta Yatsenko
Quoted 6 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Modify __bpf_trace_run() to support both sleepable and non-sleepable BPF programs. When the program is sleepable:

Quoted 7 lines

- Call might_fault() to annotate the faultable context
- Use migrate_disable()/migrate_enable() instead of rcu_read_lock()/rcu_read_unlock() to allow sleeping while still protecting percpu data access
- The outer rcu_tasks_trace lock is already held by the faultable tracepoint callback (__DECLARE_TRACE_SYSCALL), providing lifetime protection for the BPF program

we should not rely on this, see uprobe_prog_run(), we should do something like that here, explicitly calling correct RCU flavor depending on sleepable/non-sleepable program

Quoted 68 lines

For non-sleepable programs, rcu_read_lock_dont_migrate() is replaced with explicit migrate_disable()/rcu_read_lock() pairing.

Remove preempt_disable_notrace()/preempt_enable_notrace() from __BPF_DECLARE_TRACE_SYSCALL. Per-CPU protection and RCU locking are now managed per-program inside __bpf_trace_run().

---
 include/trace/bpf_probe.h |  2 --
 kernel/trace/bpf_trace.c  | 13 ++++++++++---
 2 files changed, 10 insertions(+), 5 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 +10 -4
index 0b040a417442..3688a7e115d1 100644
@@ -2076,7 +2076,7 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
        struct bpf_run_ctx *old_run_ctx;
        struct bpf_trace_run_ctx run_ctx;
 
-       rcu_read_lock_dont_migrate();
+       migrate_disable();
        if (unlikely(!bpf_prog_get_recursion_context(prog))) {
                bpf_prog_inc_misses_counter(prog);
                goto out;
@@ -2085,12 +2085,19 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
        run_ctx.bpf_cookie = link->cookie;
        old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
 
-       (void) bpf_prog_run(prog, args);
+       if (prog->sleepable) {
+               might_fault();
+               (void)bpf_prog_run(prog, args);
+       } else {
+               rcu_read_lock();
+               (void)bpf_prog_run(prog, args);
+               rcu_read_unlock();
+       }
 
        bpf_reset_run_ctx(old_run_ctx);
 out:
        bpf_prog_put_recursion_context(prog);
-       rcu_read_unlock_migrate();
+       migrate_enable();
 }
 
 #define UNPACK(...)                    __VA_ARGS__
 
--

2.52.0

#5 lore raw

PATCH v3 2/4 bpf: Verifier support for sleepable raw tracepoint programs

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add BPF_TRACE_RAW_TP to the set of tracing program attach types that can be loaded as sleepable in can_be_sleepable().

Reject sleepable programs that target non-faultable tracepoints at load time in bpf_check_attach_target(), providing a clear verifier error message.

---
 kernel/bpf/verifier.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +8 -1
index 8e4f69918693..95b33b85698f 100644
@@ -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;
@@ -25461,7 +25468,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 	}
 
 	if (prog->sleepable && !can_be_sleepable(prog)) {
-		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, struct_ops, and raw_tp programs can be sleepable\n");
 		return -EINVAL;
 	}
 
Signature · 1 line
2.52.0
reviewer Reviewed-by on v3 2/4
#6 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: Verifier support for sleepable raw tracepoint programs

Quoted 10 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add BPF_TRACE_RAW_TP to the set of tracing program attach types that can be loaded as sleepable in can_be_sleepable().

Reject sleepable programs that target non-faultable tracepoints at load time in bpf_check_attach_target(), providing a clear verifier error message.

Quoted 38 lines
---
 kernel/bpf/verifier.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +8 -1
index 8e4f69918693..95b33b85698f 100644
@@ -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;
@@ -25461,7 +25468,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 	}
 
 	if (prog->sleepable && !can_be_sleepable(prog)) {
-		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, struct_ops, and raw_tp programs can be sleepable\n");
 		return -EINVAL;
 	}
 
#7 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: Verifier support for sleepable raw tracepoint programs

In reply to Mykyta Yatsenko
The updated error message adds raw_tp but still omits fsession, which is listed in can_be_sleepable() just above. Should this read "fentry/fexit/fmod_ret/fsession, lsm, iter, uprobe, struct_ops, and…
Quoted 3 lines
kernel/bpf/verifier.c +0 -0

[ ... ]

Quoted 9 lines
kernel/bpf/verifier.c +1 -0
@@ -25435,6 +25441,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;

kernel/bpf/verifier.c struct bpf_attach_target_info tgt_info = {}; · line 25474

Quoted 5 lines
kernel/bpf/verifier.c +1 -1
@@ -25464,7 +25471,7 @@ struct bpf_attach_target_info tgt_info = {};
 	}
 
 	if (prog->sleepable && !can_be_sleepable(prog)) {
-		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, struct_ops, and raw_tp programs can be sleepable\n");

The updated error message adds raw_tp but still omits fsession, which is listed in can_be_sleepable() just above. Should this read "fentry/fexit/fmod_ret/fsession, lsm, iter, uprobe, struct_ops, and raw_tp"?

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 25473

Quoted 2 lines
kernel/bpf/verifier.c +0 -0
 		return -EINVAL;
 	}

---
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/22968403755

#8 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: Verifier support for sleepable raw tracepoint programs

Quoted 18 lines · On Wed, 11 Mar 2026 at 19:53, <[email protected]> wrote:
Quoted 4 lines
kernel/bpf/verifier.c +0 -0

[ ... ]

Quoted 11 lines
kernel/bpf/verifier.c +1 -0
@@ -25435,6 +25441,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;
@@ -25464,7 +25471,7 @@ struct bpf_attach_target_info tgt_info = {};
      }
 
Quoted 9 lines

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 25471

Quoted 3 lines
kernel/bpf/verifier.c +1 -1
      if (prog->sleepable && !can_be_sleepable(prog)) {
-             verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+             verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, struct_ops, and raw_tp programs can be sleepable\n");

The updated error message adds raw_tp but still omits fsession, which is listed in can_be_sleepable() just above. Should this read "fentry/fexit/fmod_ret/fsession, lsm, iter, uprobe, struct_ops, and raw_tp"?

We can ignore AI for now, Leon has a change for this.

Quoted 9 lines

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 25473

Quoted 2 lines
kernel/bpf/verifier.c +0 -0
              return -EINVAL;
      }

---
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/22968403755

#9 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: Verifier support for sleepable raw tracepoint programs

Quoted 29 lines · Kumar Kartikeya Dwivedi
Quoted 18 lines · On Wed, 11 Mar 2026 at 19:53, <[email protected]> wrote:
Quoted 4 lines
kernel/bpf/verifier.c +0 -0

[ ... ]

Quoted 11 lines
kernel/bpf/verifier.c +1 -0
@@ -25435,6 +25441,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;
@@ -25464,7 +25471,7 @@ struct bpf_attach_target_info tgt_info = {};
      }
 
Quoted 9 lines

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 25471

Quoted 3 lines
kernel/bpf/verifier.c +1 -1
      if (prog->sleepable && !can_be_sleepable(prog)) {
-             verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+             verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, struct_ops, and raw_tp programs can be sleepable\n");

The updated error message adds raw_tp but still omits fsession, which is listed in can_be_sleepable() just above. Should this read "fentry/fexit/fmod_ret/fsession, lsm, iter, uprobe, struct_ops, and raw_tp"?

Quoted 2 lines

We can ignore AI for now, Leon has a change for this.

Just to confirm the change: is it
https://lore.kernel.org/bpf/[email protected]/?

If yes, it is to add "fsession" to the verbose log message in check_get_func_ip(). Then, the AI review is correct: "fsession" was missing here.

After searching for "fexit" in verifier.c, "FENTRY/FEXIT" can be updated to "FENTRY/FEXIT/FSESSION" in bpf_check_attach_target(), and "fsession" is missing in some comments. I'm not sure whether it is worth a patch to do such cleanup work.

cc Menglong

Thanks,
Leon

#10 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: Verifier support for sleepable raw tracepoint programs

On 2026/3/12 13:50 Leon Hwang <[email protected]> write:

Quoted 35 lines · Kumar Kartikeya Dwivedi
Quoted 29 lines · On Wed, 11 Mar 2026 at 19:53, <[email protected]> wrote:
Quoted 18 lines
Quoted 4 lines
kernel/bpf/verifier.c +0 -0

[ ... ]

Quoted 11 lines
kernel/bpf/verifier.c +1 -0
@@ -25435,6 +25441,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;
@@ -25464,7 +25471,7 @@ struct bpf_attach_target_info tgt_info = {};
      }
 
Quoted 9 lines

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 25471

Quoted 3 lines
kernel/bpf/verifier.c +1 -1
      if (prog->sleepable && !can_be_sleepable(prog)) {
-             verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+             verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, struct_ops, and raw_tp programs can be sleepable\n");

The updated error message adds raw_tp but still omits fsession, which is listed in can_be_sleepable() just above. Should this read "fentry/fexit/fmod_ret/fsession, lsm, iter, uprobe, struct_ops, and raw_tp"?

Quoted 2 lines

We can ignore AI for now, Leon has a change for this.

Just to confirm the change: is it
https://lore.kernel.org/bpf/[email protected]/?

Quoted 8 lines

If yes, it is to add "fsession" to the verbose log message in check_get_func_ip(). Then, the AI review is correct: "fsession" was missing here.

After searching for "fexit" in verifier.c, "FENTRY/FEXIT" can be updated to "FENTRY/FEXIT/FSESSION" in bpf_check_attach_target(), and "fsession" is missing in some comments. I'm not sure whether it is worth a patch to do such cleanup work.

Ah, when I search the "fentry.*fexit", I found that there do have some missed things in:
- the comments
- the log message in check_get_func_ip() and check_attach_btf_id()
- the document in bpftool-prog.rst
- the help info in bpftool
- bash-completion of bpftool

I can send a series to fix these things, except the log message in check_get_func_ip(), which is already in Leon's patch.

Thanks!
Menglong Dong

Quoted 8 lines

cc Menglong

Thanks,
Leon

#11 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: Verifier support for sleepable raw tracepoint programs

Quoted 3 lines · Menglong Dong

On 2026/3/12 13:50 Leon Hwang <[email protected]> write:

Quoted 2 lines · Kumar Kartikeya Dwivedi
Quoted 1 line · On Wed, 11 Mar 2026 at 19:53, <[email protected]> wrote:

[...]

Quoted 23 lines
Quoted 6 lines
Quoted 3 lines

We can ignore AI for now, Leon has a change for this.

Just to confirm the change: is it
https://lore.kernel.org/bpf/[email protected]/?

Quoted 8 lines

If yes, it is to add "fsession" to the verbose log message in check_get_func_ip(). Then, the AI review is correct: "fsession" was missing here.

After searching for "fexit" in verifier.c, "FENTRY/FEXIT" can be updated to "FENTRY/FEXIT/FSESSION" in bpf_check_attach_target(), and "fsession" is missing in some comments. I'm not sure whether it is worth a patch to do such cleanup work.

Ah, when I search the "fentry.*fexit", I found that there do have some missed things in:
- the comments
- the log message in check_get_func_ip() and check_attach_btf_id()
- the document in bpftool-prog.rst
- the help info in bpftool
- bash-completion of bpftool

Quoted 3 lines

I can send a series to fix these things, except the log message in check_get_func_ip(), which is already in Leon's patch.

More appropriate to include it in your series, I think.

Thanks,
Leon

maintainer Acked-by on v3 2/4
#12 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: Verifier support for sleepable raw tracepoint programs

In reply to Mykyta Yatsenko
Quoted 6 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add BPF_TRACE_RAW_TP to the set of tracing program attach types that can be loaded as sleepable in can_be_sleepable().

Quoted 6 lines

Reject sleepable programs that target non-faultable tracepoints at load time in bpf_check_attach_target(), providing a clear verifier error message.

---

Quoted 1 line

[...]

Acked-by on v3 2/4
#13 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: Verifier support for sleepable raw tracepoint programs

In reply to Mykyta Yatsenko
Quoted 10 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add BPF_TRACE_RAW_TP to the set of tracing program attach types that can be loaded as sleepable in can_be_sleepable().

Reject sleepable programs that target non-faultable tracepoints at load time in bpf_check_attach_target(), providing a clear verifier error message.

Quoted 41 lines
---
 kernel/bpf/verifier.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +8 -1
index 8e4f69918693..95b33b85698f 100644
@@ -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;
@@ -25461,7 +25468,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 	}
 
 	if (prog->sleepable && !can_be_sleepable(prog)) {
-		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, struct_ops, and raw_tp programs can be sleepable\n");
 		return -EINVAL;
 	}
 
Signature · 2 lines
-- 
2.52.0
#14 lore raw

Re: [PATCH bpf-next v3 2/4] bpf: Verifier support for sleepable raw tracepoint programs

In reply to Mykyta Yatsenko
Quoted 4 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add BPF_TRACE_RAW_TP to the set of tracing program attach types

for completeness, we should probably make classic tracepoints sleepable as well, no?

Quoted 37 lines

that can be loaded as sleepable in can_be_sleepable().

Reject sleepable programs that target non-faultable tracepoints at load time in bpf_check_attach_target(), providing a clear verifier error message.

---
 kernel/bpf/verifier.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +7 -0
index 8e4f69918693..95b33b85698f 100644
@@ -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;

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 25471

Quoted 5 lines
kernel/bpf/verifier.c +1 -1
@@ -25461,7 +25468,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
        }
 
        if (prog->sleepable && !can_be_sleepable(prog)) {
-               verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+               verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, struct_ops, and raw_tp programs can be sleepable\n");

this is becoming ridiculous, can't we just say "Program type XXX cannot be sleepable"?

Quoted 8 lines
kernel/bpf/verifier.c +0 -1
                return -EINVAL;
        }
 
 
 
--

2.52.0

#15 lore raw

PATCH v3 3/4 libbpf: Add tp_btf.s section handler for sleepable raw tracepoints

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add SEC_DEF for "tp_btf.s+" section prefix, enabling userspace BPF programs to use SEC("tp_btf.s/<tracepoint>") to load sleepable raw tracepoint programs. This follows the existing pattern used for fentry.s, fexit.s, fmod_ret.s, and lsm.s section definitions.

---
 tools/lib/bpf/libbpf.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/libbpf.c +1 -0
index 0662d72bad20..7b65869f5c8e 100644
@@ -9863,6 +9863,7 @@ static const struct bpf_sec_def section_defs[] = {
 	SEC_DEF("raw_tracepoint.w+",	RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
 	SEC_DEF("raw_tp.w+",		RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
 	SEC_DEF("tp_btf+",		TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
+	SEC_DEF("tp_btf.s+",		TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
 	SEC_DEF("fentry+",		TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fmod_ret+",		TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fexit+",		TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
Signature · 1 line
2.52.0
reviewer Reviewed-by on v3 3/4
#16 lore raw

Re: [PATCH bpf-next v3 3/4] libbpf: Add tp_btf.s section handler for sleepable raw tracepoints

Quoted 9 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add SEC_DEF for "tp_btf.s+" section prefix, enabling userspace BPF programs to use SEC("tp_btf.s/<tracepoint>") to load sleepable raw tracepoint programs. This follows the existing pattern used for fentry.s, fexit.s, fmod_ret.s, and lsm.s section definitions.

Quoted 16 lines
---
 tools/lib/bpf/libbpf.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/libbpf.c +1 -0
index 0662d72bad20..7b65869f5c8e 100644
@@ -9863,6 +9863,7 @@ static const struct bpf_sec_def section_defs[] = {
 	SEC_DEF("raw_tracepoint.w+",	RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
 	SEC_DEF("raw_tp.w+",		RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
 	SEC_DEF("tp_btf+",		TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
+	SEC_DEF("tp_btf.s+",		TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
 	SEC_DEF("fentry+",		TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fmod_ret+",		TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fexit+",		TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
Acked-by on v3 3/4
#17 lore raw

Re: [PATCH bpf-next v3 3/4] libbpf: Add tp_btf.s section handler for sleepable raw tracepoints

In reply to Mykyta Yatsenko
Quoted 9 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add SEC_DEF for "tp_btf.s+" section prefix, enabling userspace BPF programs to use SEC("tp_btf.s/<tracepoint>") to load sleepable raw tracepoint programs. This follows the existing pattern used for fentry.s, fexit.s, fmod_ret.s, and lsm.s section definitions.

Quoted 19 lines
---
 tools/lib/bpf/libbpf.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/libbpf.c +1 -0
index 0662d72bad20..7b65869f5c8e 100644
@@ -9863,6 +9863,7 @@ static const struct bpf_sec_def section_defs[] = {
 	SEC_DEF("raw_tracepoint.w+",	RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
 	SEC_DEF("raw_tp.w+",		RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
 	SEC_DEF("tp_btf+",		TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
+	SEC_DEF("tp_btf.s+",		TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
 	SEC_DEF("fentry+",		TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fmod_ret+",		TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fexit+",		TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
Signature · 2 lines
-- 
2.52.0
#18 lore raw

Re: [PATCH bpf-next v3 3/4] libbpf: Add tp_btf.s section handler for sleepable raw tracepoints

In reply to Mykyta Yatsenko
Quoted 19 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add SEC_DEF for "tp_btf.s+" section prefix, enabling userspace BPF programs to use SEC("tp_btf.s/<tracepoint>") to load sleepable raw tracepoint programs. This follows the existing pattern used for fentry.s, fexit.s, fmod_ret.s, and lsm.s section definitions.

---
 tools/lib/bpf/libbpf.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/libbpf.c +0 -0
index 0662d72bad20..7b65869f5c8e 100644

tools/lib/bpf/libbpf.c static const struct bpf_sec_def section_defs[] = { · line 9866

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -9863,6 +9863,7 @@ static const struct bpf_sec_def section_defs[] = {
        SEC_DEF("raw_tracepoint.w+",    RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
        SEC_DEF("raw_tp.w+",            RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
        SEC_DEF("tp_btf+",              TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
+       SEC_DEF("tp_btf.s+",            TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),

what about raw_tp.s+ ?

pw-bot: cr

Quoted 7 lines
tools/lib/bpf/libbpf.c +0 -1
        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),
 
--

2.52.0

#19 lore raw

PATCH v3 4/4 selftests/bpf: Add tests for sleepable raw tracepoint programs

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add tests for sleepable raw tracepoint programs:
- success: Attach a sleepable BPF program to the faultable sys_enter tracepoint (tp_btf.s/sys_enter). Verify the program is triggered by a syscall.
- reject_non_faultable: Verify that loading a sleepable BPF program targeting a non-faultable tracepoint (tp_btf.s/sched_switch) is rejected by the verifier.

---
 .../selftests/bpf/prog_tests/sleepable_raw_tp.c    | 40 ++++++++++++++++++++
 .../selftests/bpf/progs/test_sleepable_raw_tp.c    | 43 ++++++++++++++++++++++
 .../bpf/progs/test_sleepable_raw_tp_fail.c         | 18 +++++++++
 tools/testing/selftests/bpf/verifier/sleepable.c   | 17 ++++++++-
 4 files changed, 116 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +40 -0
new file mode 100644
index 000000000000..e902b41591e7
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <time.h>
+#include "test_sleepable_raw_tp.skel.h"
+#include "test_sleepable_raw_tp_fail.skel.h"
+
+static void test_sleepable_raw_tp_success(void)
+{
+	struct test_sleepable_raw_tp *skel;
+	int err;
+
+	skel = test_sleepable_raw_tp__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+		return;
+
+	skel->bss->target_pid = getpid();
+
+	err = test_sleepable_raw_tp__attach(skel);
+	if (!ASSERT_OK(err, "skel_attach"))
+		goto cleanup;
+
+	syscall(__NR_nanosleep, &(struct timespec){ .tv_nsec = 555 }, NULL);
+
+	ASSERT_EQ(skel->bss->triggered, 1, "triggered");
+	ASSERT_EQ(skel->bss->err, 0, "err");
+	ASSERT_EQ(skel->bss->copied_tv_nsec, 555, "copied_tv_nsec");
+
+cleanup:
+	test_sleepable_raw_tp__destroy(skel);
+}
+
+void test_sleepable_raw_tp(void)
+{
+	if (test__start_subtest("success"))
+		test_sleepable_raw_tp_success();
+
+	RUN_TESTS(test_sleepable_raw_tp_fail);
+}
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +43 -0

43 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_sleepable_raw_tp_fail.c new file +18 -0
new file mode 100644
index 000000000000..54100899f728
@@ -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(test_sleepable_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
reviewer Reviewed-by on v3 4/4
#20 lore raw

Re: [PATCH bpf-next v3 4/4] selftests/bpf: Add tests for sleepable raw tracepoint programs

Quoted 10 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add tests for sleepable raw tracepoint programs:
- success: Attach a sleepable BPF program to the faultable sys_enter tracepoint (tp_btf.s/sys_enter). Verify the program is triggered by a syscall.
- reject_non_faultable: Verify that loading a sleepable BPF program targeting a non-faultable tracepoint (tp_btf.s/sched_switch) is rejected by the verifier.

Quoted 2 lines
Quoted 161 lines
---
 .../selftests/bpf/prog_tests/sleepable_raw_tp.c    | 40 ++++++++++++++++++++
 .../selftests/bpf/progs/test_sleepable_raw_tp.c    | 43 ++++++++++++++++++++++
 .../bpf/progs/test_sleepable_raw_tp_fail.c         | 18 +++++++++
 tools/testing/selftests/bpf/verifier/sleepable.c   | 17 ++++++++-
 4 files changed, 116 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +40 -0
new file mode 100644
index 000000000000..e902b41591e7
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <time.h>
+#include "test_sleepable_raw_tp.skel.h"
+#include "test_sleepable_raw_tp_fail.skel.h"
+
+static void test_sleepable_raw_tp_success(void)
+{
+	struct test_sleepable_raw_tp *skel;
+	int err;
+
+	skel = test_sleepable_raw_tp__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+		return;
+
+	skel->bss->target_pid = getpid();
+
+	err = test_sleepable_raw_tp__attach(skel);
+	if (!ASSERT_OK(err, "skel_attach"))
+		goto cleanup;
+
+	syscall(__NR_nanosleep, &(struct timespec){ .tv_nsec = 555 }, NULL);
+
+	ASSERT_EQ(skel->bss->triggered, 1, "triggered");
+	ASSERT_EQ(skel->bss->err, 0, "err");
+	ASSERT_EQ(skel->bss->copied_tv_nsec, 555, "copied_tv_nsec");
+
+cleanup:
+	test_sleepable_raw_tp__destroy(skel);
+}
+
+void test_sleepable_raw_tp(void)
+{
+	if (test__start_subtest("success"))
+		test_sleepable_raw_tp_success();
+
+	RUN_TESTS(test_sleepable_raw_tp_fail);
+}
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +43 -0
new file mode 100644
index 000000000000..ebacc766df57
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <asm/unistd.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+int target_pid;
+int triggered;
+long err;
+long copied_tv_nsec;
+
+SEC("tp_btf.s/sys_enter")
+int BPF_PROG(test_sleepable_sys_enter, struct pt_regs *regs, long id)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+	struct __kernel_timespec *ts;
+	long tv_nsec;
+
+	if (task->pid != target_pid)
+		return 0;
+
+	if (id != __NR_nanosleep)
+		return 0;
+
+	ts = (void *)PT_REGS_PARM1_CORE_SYSCALL(regs);
+
+	/*
+	 * Use bpf_copy_from_user() - a sleepable helper - to read user memory.
+	 * This exercises the sleepable execution path of raw tracepoints.
+	 */
+	err = bpf_copy_from_user(&tv_nsec, sizeof(tv_nsec), &ts->tv_nsec);
+	if (err)
+		return err;
+
+	copied_tv_nsec = tv_nsec;
+	triggered = 1;
+	return 0;
+}
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp_fail.c new file +18 -0
new file mode 100644
index 000000000000..54100899f728
@@ -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(test_sleepable_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,
 },
Acked-by on v3 4/4
#21 lore raw

Re: [PATCH bpf-next v3 4/4] selftests/bpf: Add tests for sleepable raw tracepoint programs

In reply to Mykyta Yatsenko
Quoted 10 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add tests for sleepable raw tracepoint programs:
- success: Attach a sleepable BPF program to the faultable sys_enter tracepoint (tp_btf.s/sys_enter). Verify the program is triggered by a syscall.
- reject_non_faultable: Verify that loading a sleepable BPF program targeting a non-faultable tracepoint (tp_btf.s/sched_switch) is rejected by the verifier.

Quoted 2 lines
Quoted 164 lines
---
 .../selftests/bpf/prog_tests/sleepable_raw_tp.c    | 40 ++++++++++++++++++++
 .../selftests/bpf/progs/test_sleepable_raw_tp.c    | 43 ++++++++++++++++++++++
 .../bpf/progs/test_sleepable_raw_tp_fail.c         | 18 +++++++++
 tools/testing/selftests/bpf/verifier/sleepable.c   | 17 ++++++++-
 4 files changed, 116 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +40 -0
new file mode 100644
index 000000000000..e902b41591e7
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <time.h>
+#include "test_sleepable_raw_tp.skel.h"
+#include "test_sleepable_raw_tp_fail.skel.h"
+
+static void test_sleepable_raw_tp_success(void)
+{
+	struct test_sleepable_raw_tp *skel;
+	int err;
+
+	skel = test_sleepable_raw_tp__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+		return;
+
+	skel->bss->target_pid = getpid();
+
+	err = test_sleepable_raw_tp__attach(skel);
+	if (!ASSERT_OK(err, "skel_attach"))
+		goto cleanup;
+
+	syscall(__NR_nanosleep, &(struct timespec){ .tv_nsec = 555 }, NULL);
+
+	ASSERT_EQ(skel->bss->triggered, 1, "triggered");
+	ASSERT_EQ(skel->bss->err, 0, "err");
+	ASSERT_EQ(skel->bss->copied_tv_nsec, 555, "copied_tv_nsec");
+
+cleanup:
+	test_sleepable_raw_tp__destroy(skel);
+}
+
+void test_sleepable_raw_tp(void)
+{
+	if (test__start_subtest("success"))
+		test_sleepable_raw_tp_success();
+
+	RUN_TESTS(test_sleepable_raw_tp_fail);
+}
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +43 -0
new file mode 100644
index 000000000000..ebacc766df57
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <asm/unistd.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+int target_pid;
+int triggered;
+long err;
+long copied_tv_nsec;
+
+SEC("tp_btf.s/sys_enter")
+int BPF_PROG(test_sleepable_sys_enter, struct pt_regs *regs, long id)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+	struct __kernel_timespec *ts;
+	long tv_nsec;
+
+	if (task->pid != target_pid)
+		return 0;
+
+	if (id != __NR_nanosleep)
+		return 0;
+
+	ts = (void *)PT_REGS_PARM1_CORE_SYSCALL(regs);
+
+	/*
+	 * Use bpf_copy_from_user() - a sleepable helper - to read user memory.
+	 * This exercises the sleepable execution path of raw tracepoints.
+	 */
+	err = bpf_copy_from_user(&tv_nsec, sizeof(tv_nsec), &ts->tv_nsec);
+	if (err)
+		return err;
+
+	copied_tv_nsec = tv_nsec;
+	triggered = 1;
+	return 0;
+}
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp_fail.c new file +18 -0
new file mode 100644
index 000000000000..54100899f728
@@ -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(test_sleepable_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 · 2 lines
-- 
2.52.0
#22 lore raw

Re: [PATCH bpf-next v3 4/4] selftests/bpf: Add tests for sleepable raw tracepoint programs

In reply to Mykyta Yatsenko
Quoted 92 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add tests for sleepable raw tracepoint programs:
- success: Attach a sleepable BPF program to the faultable sys_enter tracepoint (tp_btf.s/sys_enter). Verify the program is triggered by a syscall.
- reject_non_faultable: Verify that loading a sleepable BPF program targeting a non-faultable tracepoint (tp_btf.s/sched_switch) is rejected by the verifier.

---
 .../selftests/bpf/prog_tests/sleepable_raw_tp.c    | 40 ++++++++++++++++++++
 .../selftests/bpf/progs/test_sleepable_raw_tp.c    | 43 ++++++++++++++++++++++
 .../bpf/progs/test_sleepable_raw_tp_fail.c         | 18 +++++++++
 tools/testing/selftests/bpf/verifier/sleepable.c   | 17 ++++++++-
 4 files changed, 116 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sleepable_raw_tp.c new file +40 -0
new file mode 100644
index 000000000000..e902b41591e7
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <time.h>
+#include "test_sleepable_raw_tp.skel.h"
+#include "test_sleepable_raw_tp_fail.skel.h"
+
+static void test_sleepable_raw_tp_success(void)
+{
+       struct test_sleepable_raw_tp *skel;
+       int err;
+
+       skel = test_sleepable_raw_tp__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+               return;
+
+       skel->bss->target_pid = getpid();
+
+       err = test_sleepable_raw_tp__attach(skel);
+       if (!ASSERT_OK(err, "skel_attach"))
+               goto cleanup;
+
+       syscall(__NR_nanosleep, &(struct timespec){ .tv_nsec = 555 }, NULL);
+
+       ASSERT_EQ(skel->bss->triggered, 1, "triggered");
+       ASSERT_EQ(skel->bss->err, 0, "err");
+       ASSERT_EQ(skel->bss->copied_tv_nsec, 555, "copied_tv_nsec");
+
+cleanup:
+       test_sleepable_raw_tp__destroy(skel);
+}
+
+void test_sleepable_raw_tp(void)
+{
+       if (test__start_subtest("success"))
+               test_sleepable_raw_tp_success();
+
+       RUN_TESTS(test_sleepable_raw_tp_fail);
+}
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +20 -0
new file mode 100644
index 000000000000..ebacc766df57
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <asm/unistd.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+int target_pid;
+int triggered;
+long err;
+long copied_tv_nsec;
+
+SEC("tp_btf.s/sys_enter")
+int BPF_PROG(test_sleepable_sys_enter, struct pt_regs *regs, long id)
+{
+       struct task_struct *task = bpf_get_current_task_btf();

tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c line 30

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +10 -0
+       struct __kernel_timespec *ts;
+       long tv_nsec;
+
+       if (task->pid != target_pid)
+               return 0;
+
+       if (id != __NR_nanosleep)
+               return 0;
+
+       ts = (void *)PT_REGS_PARM1_CORE_SYSCALL(regs);

this is BTF-aware raw tracepoint, so let's use non-CO-RE variant of this macro (PT_REGS_PARM1_SYSCALL), which will perform direct regs->di access

but then let's also have non-BTF-aware (SEC("raw_tp")) test, where you'll have to use PT_REGS_PARM1_CORE_SYSCALL().

And let's also make sure that SEC("tp.s/syscalls/sys_enter_nanosleep") also works?

Quoted 76 lines
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp.c new file +13 -0
+
+       /*
+        * Use bpf_copy_from_user() - a sleepable helper - to read user memory.
+        * This exercises the sleepable execution path of raw tracepoints.
+        */
+       err = bpf_copy_from_user(&tv_nsec, sizeof(tv_nsec), &ts->tv_nsec);
+       if (err)
+               return err;
+
+       copied_tv_nsec = tv_nsec;
+       triggered = 1;
+       return 0;
+}
tools/testing/selftests/bpf/progs/test_sleepable_raw_tp_fail.c new file +18 -0
new file mode 100644
index 000000000000..54100899f728
@@ -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(test_sleepable_sched_switch, bool preempt,
+            struct task_struct *prev, struct task_struct *next)
+{
+       return 0;
+}
tools/testing/selftests/bpf/verifier/sleepable.c +15 -3
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,
 },
 
--

2.52.0