* [PATCH 1/4] bpf: Add bpf_stream_print_stack stack dumping kfunc
2026-02-02 19:33 [PATCH 0/4] bpf: Add bpf_stream_print_stack kfunc Emil Tsalapatis
@ 2026-02-02 19:33 ` Emil Tsalapatis
2026-02-03 17:30 ` Alexei Starovoitov
2026-02-02 19:33 ` [PATCH 2/4] selftests/bpf: Add selftests for bpf_stream_print_stack Emil Tsalapatis
` (3 subsequent siblings)
4 siblings, 1 reply; 8+ messages in thread
From: Emil Tsalapatis @ 2026-02-02 19:33 UTC (permalink / raw)
To: bpf
Cc: andrii, ast, daniel, eddyz87, martin.lau, memxor, song,
yonghong.song, Emil Tsalapatis, Emil Tsalapatis (Meta)
From: Emil Tsalapatis <etsal@meta.com>
Add a new kfunc called bpf_stream_print_stack to be used by programs
that need to print out their current BPF stack. The kfunc is essentially
a wrapper around the existing bpf_stream_dump_stack functionality used
to generate stack traces for error events like may_goto violations and
BPF-side arena page faults.
Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
---
kernel/bpf/helpers.c | 1 +
kernel/bpf/stream.c | 19 +++++++++++++++++++
2 files changed, 20 insertions(+)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index b54ec0e945aa..c30a9f68af6b 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -4562,6 +4562,7 @@ BTF_ID_FLAGS(func, bpf_strncasestr);
BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)
#endif
BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, bpf_stream_print_stack, KF_IMPLICIT_ARGS)
BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS)
BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS)
BTF_ID_FLAGS(func, bpf_dynptr_from_file)
diff --git a/kernel/bpf/stream.c b/kernel/bpf/stream.c
index 24730df55e69..be9ce98e9469 100644
--- a/kernel/bpf/stream.c
+++ b/kernel/bpf/stream.c
@@ -245,6 +245,25 @@ __bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const vo
return ret;
}
+/* Directly trigger a stack dump from the program. */
+__bpf_kfunc int bpf_stream_print_stack(int stream_id, struct bpf_prog_aux *aux)
+{
+ struct bpf_stream_stage ss;
+ struct bpf_prog *prog;
+
+ /* Make sure the stream ID is valid. */
+ if (!bpf_stream_get(stream_id, aux))
+ return -ENOENT;
+
+ prog = aux->main_prog_aux->prog;
+
+ bpf_stream_stage(ss, prog, stream_id, ({
+ bpf_stream_dump_stack(ss);
+ }));
+
+ return 0;
+}
+
__bpf_kfunc_end_defs();
/* Added kfunc to common_btf_ids */
--
2.49.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 1/4] bpf: Add bpf_stream_print_stack stack dumping kfunc
2026-02-02 19:33 ` [PATCH 1/4] bpf: Add bpf_stream_print_stack stack dumping kfunc Emil Tsalapatis
@ 2026-02-03 17:30 ` Alexei Starovoitov
2026-02-03 17:39 ` Emil Tsalapatis
0 siblings, 1 reply; 8+ messages in thread
From: Alexei Starovoitov @ 2026-02-03 17:30 UTC (permalink / raw)
To: Emil Tsalapatis
Cc: bpf, Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann, Eduard,
Martin KaFai Lau, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Emil Tsalapatis
On Mon, Feb 2, 2026 at 11:33 AM Emil Tsalapatis <emil@etsalapatis.com> wrote:
>
> From: Emil Tsalapatis <etsal@meta.com>
>
> Add a new kfunc called bpf_stream_print_stack to be used by programs
> that need to print out their current BPF stack. The kfunc is essentially
> a wrapper around the existing bpf_stream_dump_stack functionality used
> to generate stack traces for error events like may_goto violations and
> BPF-side arena page faults.
>
> Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
Pls respin, since it fails check:
Commit 69310da6192e ("bpf: Add bpf_stream_print_stack stack dumping kfunc")
author Signed-off-by missing
author email: etsal@meta.com
committer email: ast@kernel.org
Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Author: must be the same as SOB:
pw-bot: cr
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH 1/4] bpf: Add bpf_stream_print_stack stack dumping kfunc
2026-02-03 17:30 ` Alexei Starovoitov
@ 2026-02-03 17:39 ` Emil Tsalapatis
0 siblings, 0 replies; 8+ messages in thread
From: Emil Tsalapatis @ 2026-02-03 17:39 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: bpf, Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann, Eduard,
Martin KaFai Lau, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Emil Tsalapatis
On Tue Feb 3, 2026 at 12:30 PM EST, Alexei Starovoitov wrote:
> On Mon, Feb 2, 2026 at 11:33 AM Emil Tsalapatis <emil@etsalapatis.com> wrote:
>>
>> From: Emil Tsalapatis <etsal@meta.com>
>>
>> Add a new kfunc called bpf_stream_print_stack to be used by programs
>> that need to print out their current BPF stack. The kfunc is essentially
>> a wrapper around the existing bpf_stream_dump_stack functionality used
>> to generate stack traces for error events like may_goto violations and
>> BPF-side arena page faults.
>>
>> Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
>
> Pls respin, since it fails check:
>
> Commit 69310da6192e ("bpf: Add bpf_stream_print_stack stack dumping kfunc")
> author Signed-off-by missing
> author email: etsal@meta.com
> committer email: ast@kernel.org
> Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
>
> Author: must be the same as SOB:
Will do, sorry about that. I will also update the Signed-off-by for consistency.
>
> pw-bot: cr
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/4] selftests/bpf: Add selftests for bpf_stream_print_stack
2026-02-02 19:33 [PATCH 0/4] bpf: Add bpf_stream_print_stack kfunc Emil Tsalapatis
2026-02-02 19:33 ` [PATCH 1/4] bpf: Add bpf_stream_print_stack stack dumping kfunc Emil Tsalapatis
@ 2026-02-02 19:33 ` Emil Tsalapatis
2026-02-02 19:33 ` [PATCH 3/4] bpf: Allow BPF stream kfuncs while holding a lock Emil Tsalapatis
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Emil Tsalapatis @ 2026-02-02 19:33 UTC (permalink / raw)
To: bpf
Cc: andrii, ast, daniel, eddyz87, martin.lau, memxor, song,
yonghong.song, Emil Tsalapatis
Add selftests for the new bpf_stream_print_stack kfunc.
Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
---
tools/testing/selftests/bpf/progs/stream.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/tools/testing/selftests/bpf/progs/stream.c b/tools/testing/selftests/bpf/progs/stream.c
index 4a5bd852f10c..f63b378de090 100644
--- a/tools/testing/selftests/bpf/progs/stream.c
+++ b/tools/testing/selftests/bpf/progs/stream.c
@@ -234,4 +234,25 @@ int stream_arena_callback_fault(void *ctx)
return 0;
}
+SEC("syscall")
+__arch_x86_64
+__arch_arm64
+__success __retval(0)
+__stderr("CPU: {{[0-9]+}} UID: 0 PID: {{[0-9]+}} Comm: {{.*}}")
+__stderr("Call trace:\n"
+"{{([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n"
+"|[ \t]+[^\n]+\n)*}}")
+int stream_print_stack_kfunc(void *ctx)
+{
+ return bpf_stream_print_stack(BPF_STDERR);
+}
+
+SEC("syscall")
+__success __retval(-2)
+int stream_print_stack_invalid_id(void *ctx)
+{
+ /* Try to pass an invalid stream ID. */
+ return bpf_stream_print_stack((enum bpf_stream_id)0xbadcafe);
+}
+
char _license[] SEC("license") = "GPL";
--
2.49.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 3/4] bpf: Allow BPF stream kfuncs while holding a lock
2026-02-02 19:33 [PATCH 0/4] bpf: Add bpf_stream_print_stack kfunc Emil Tsalapatis
2026-02-02 19:33 ` [PATCH 1/4] bpf: Add bpf_stream_print_stack stack dumping kfunc Emil Tsalapatis
2026-02-02 19:33 ` [PATCH 2/4] selftests/bpf: Add selftests for bpf_stream_print_stack Emil Tsalapatis
@ 2026-02-02 19:33 ` Emil Tsalapatis
2026-02-02 19:33 ` [PATCH 4/4] selftests/bpf: Add selftests for stream functions under lock Emil Tsalapatis
2026-02-03 16:16 ` [PATCH 0/4] bpf: Add bpf_stream_print_stack kfunc Kumar Kartikeya Dwivedi
4 siblings, 0 replies; 8+ messages in thread
From: Emil Tsalapatis @ 2026-02-02 19:33 UTC (permalink / raw)
To: bpf
Cc: andrii, ast, daniel, eddyz87, martin.lau, memxor, song,
yonghong.song, Emil Tsalapatis, Emil Tsalapatis (Meta)
From: Emil Tsalapatis <etsal@meta.com>
The BPF stream kfuncs bpf_stream_vprintk and bpf_stream_print_stack
do not sleep and so are safe to call while holding a lock. Amend
the verifier to allow that.
Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
kernel/bpf/verifier.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index c2f2650db9fd..b7970537eb41 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -12485,6 +12485,8 @@ enum special_kfunc_type {
KF_bpf_arena_free_pages,
KF_bpf_arena_reserve_pages,
KF_bpf_session_is_return,
+ KF_bpf_stream_vprintk,
+ KF_bpf_stream_print_stack,
};
BTF_ID_LIST(special_kfunc_list)
@@ -12563,6 +12565,8 @@ BTF_ID(func, bpf_arena_alloc_pages)
BTF_ID(func, bpf_arena_free_pages)
BTF_ID(func, bpf_arena_reserve_pages)
BTF_ID(func, bpf_session_is_return)
+BTF_ID(func, bpf_stream_vprintk)
+BTF_ID(func, bpf_stream_print_stack)
static bool is_task_work_add_kfunc(u32 func_id)
{
@@ -13007,10 +13011,17 @@ static bool is_bpf_arena_kfunc(u32 btf_id)
btf_id == special_kfunc_list[KF_bpf_arena_reserve_pages];
}
+static bool is_bpf_stream_kfunc(u32 btf_id)
+{
+ return btf_id == special_kfunc_list[KF_bpf_stream_vprintk] ||
+ btf_id == special_kfunc_list[KF_bpf_stream_print_stack];
+}
+
static bool kfunc_spin_allowed(u32 btf_id)
{
return is_bpf_graph_api_kfunc(btf_id) || is_bpf_iter_num_api_kfunc(btf_id) ||
- is_bpf_res_spin_lock_kfunc(btf_id) || is_bpf_arena_kfunc(btf_id);
+ is_bpf_res_spin_lock_kfunc(btf_id) || is_bpf_arena_kfunc(btf_id) ||
+ is_bpf_stream_kfunc(btf_id);
}
static bool is_sync_callback_calling_kfunc(u32 btf_id)
--
2.49.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 4/4] selftests/bpf: Add selftests for stream functions under lock
2026-02-02 19:33 [PATCH 0/4] bpf: Add bpf_stream_print_stack kfunc Emil Tsalapatis
` (2 preceding siblings ...)
2026-02-02 19:33 ` [PATCH 3/4] bpf: Allow BPF stream kfuncs while holding a lock Emil Tsalapatis
@ 2026-02-02 19:33 ` Emil Tsalapatis
2026-02-03 16:16 ` [PATCH 0/4] bpf: Add bpf_stream_print_stack kfunc Kumar Kartikeya Dwivedi
4 siblings, 0 replies; 8+ messages in thread
From: Emil Tsalapatis @ 2026-02-02 19:33 UTC (permalink / raw)
To: bpf
Cc: andrii, ast, daniel, eddyz87, martin.lau, memxor, song,
yonghong.song, Emil Tsalapatis
Add a selftest to ensure BPF stream functions can now be called
while holding a lock.
Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
---
tools/testing/selftests/bpf/progs/stream.c | 32 ++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/tools/testing/selftests/bpf/progs/stream.c b/tools/testing/selftests/bpf/progs/stream.c
index f63b378de090..6f999ba951a3 100644
--- a/tools/testing/selftests/bpf/progs/stream.c
+++ b/tools/testing/selftests/bpf/progs/stream.c
@@ -42,6 +42,10 @@ int size;
u64 fault_addr;
void *arena_ptr;
+#define private(name) SEC(".bss." #name) __hidden __attribute__((aligned(8)))
+
+private(STREAM) struct bpf_spin_lock block;
+
SEC("syscall")
__success __retval(0)
int stream_exhaust(void *ctx)
@@ -255,4 +259,32 @@ int stream_print_stack_invalid_id(void *ctx)
return bpf_stream_print_stack((enum bpf_stream_id)0xbadcafe);
}
+SEC("syscall")
+__arch_x86_64
+__arch_arm64
+__success __retval(0)
+__stdout(_STR)
+__stderr("CPU: {{[0-9]+}} UID: 0 PID: {{[0-9]+}} Comm: {{.*}}")
+__stderr("Call trace:\n"
+"{{([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n"
+"|[ \t]+[^\n]+\n)*}}")
+int stream_print_kfuncs_locked(void *ctx)
+{
+ int ret;
+
+ bpf_spin_lock(&block);
+
+ ret = bpf_stream_printk(BPF_STDOUT, _STR);
+ if (ret)
+ goto out;
+
+ ret = bpf_stream_print_stack(BPF_STDERR);
+
+out:
+ bpf_spin_unlock(&block);
+
+ return ret;
+}
+
+
char _license[] SEC("license") = "GPL";
--
2.49.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 0/4] bpf: Add bpf_stream_print_stack kfunc
2026-02-02 19:33 [PATCH 0/4] bpf: Add bpf_stream_print_stack kfunc Emil Tsalapatis
` (3 preceding siblings ...)
2026-02-02 19:33 ` [PATCH 4/4] selftests/bpf: Add selftests for stream functions under lock Emil Tsalapatis
@ 2026-02-03 16:16 ` Kumar Kartikeya Dwivedi
4 siblings, 0 replies; 8+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-02-03 16:16 UTC (permalink / raw)
To: Emil Tsalapatis
Cc: bpf, andrii, ast, daniel, eddyz87, martin.lau, song,
yonghong.song
On Mon, 2 Feb 2026 at 20:33, Emil Tsalapatis <emil@etsalapatis.com> wrote:
>
> Add a new bpf_stream_print_stack kfunc for printing a BPF program stack
> into a BPF stream. Update the verifier to allow the new kfunc to be
> called with BPF spinlocks held, along with bpf_stream_vprintk.
>
> Patchset spun out of the larger libarena + ASAN patchset.
> (https://lore.kernel.org/bpf/20260127181610.86376-1-emil@etsalapatis.com/)
>
> Changeset:
> - Update bpf_stream_print_stack to take stream_id arg (Kumar)
> - Added selftest for the bpf_stream_print_stack
> - Add selftest for calling the streams kfuncs under lock
>
> Cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
>
> Emil Tsalapatis (4):
> bpf: Add bpf_stream_print_stack stack dumping kfunc
> selftests/bpf: Add selftests for bpf_stream_print_stack
> bpf: Allow BPF stream kfuncs while holding a lock
> selftests/bpf: Add selftests for stream functions under lock
>
For the set:
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> kernel/bpf/helpers.c | 1 +
> kernel/bpf/stream.c | 19 ++++++++
> kernel/bpf/verifier.c | 13 +++++-
> tools/testing/selftests/bpf/progs/stream.c | 53 ++++++++++++++++++++++
> 4 files changed, 85 insertions(+), 1 deletion(-)
>
> --
> 2.49.0
>
^ permalink raw reply [flat|nested] 8+ messages in thread