From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Emil Tsalapatis <emil@etsalapatis.com>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Barret Rhoden <brho@google.com>,
Matt Bobrowski <mattbobrowski@google.com>,
kkd@meta.com, kernel-team@meta.com
Subject: [PATCH bpf-next v5 06/12] bpf: Add dump_stack() analogue to print to BPF stderr
Date: Thu, 3 Jul 2025 13:48:12 -0700 [thread overview]
Message-ID: <20250703204818.925464-7-memxor@gmail.com> (raw)
In-Reply-To: <20250703204818.925464-1-memxor@gmail.com>
Introduce a kernel function which is the analogue of dump_stack()
printing some useful information and the stack trace. This is not
exposed to BPF programs yet, but can be made available in the future.
When we have a program counter for a BPF program in the stack trace,
also additionally output the filename and line number to make the trace
helpful. The rest of the trace can be passed into ./decode_stacktrace.sh
to obtain the line numbers for kernel symbols.
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
include/linux/bpf.h | 2 ++
kernel/bpf/stream.c | 48 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4d577352f3e6..18f8e4066e20 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3615,8 +3615,10 @@ __printf(2, 3)
int bpf_stream_stage_printk(struct bpf_stream_stage *ss, const char *fmt, ...);
int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog,
enum bpf_stream_id stream_id);
+int bpf_stream_stage_dump_stack(struct bpf_stream_stage *ss);
#define bpf_stream_printk(ss, ...) bpf_stream_stage_printk(&ss, __VA_ARGS__)
+#define bpf_stream_dump_stack(ss) bpf_stream_stage_dump_stack(&ss)
#define bpf_stream_stage(ss, prog, stream_id, expr) \
({ \
diff --git a/kernel/bpf/stream.c b/kernel/bpf/stream.c
index e434541358db..8c842f845245 100644
--- a/kernel/bpf/stream.c
+++ b/kernel/bpf/stream.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
#include <linux/bpf.h>
+#include <linux/filter.h>
#include <linux/bpf_mem_alloc.h>
#include <linux/percpu.h>
#include <linux/refcount.h>
@@ -476,3 +477,50 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog,
llist_add_batch(head, tail, &stream->log);
return 0;
}
+
+struct dump_stack_ctx {
+ struct bpf_stream_stage *ss;
+ int err;
+};
+
+static bool dump_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp)
+{
+ struct dump_stack_ctx *ctxp = cookie;
+ const char *file = "", *line = "";
+ struct bpf_prog *prog;
+ int num, ret;
+
+ rcu_read_lock();
+ prog = bpf_prog_ksym_find(ip);
+ rcu_read_unlock();
+ if (prog) {
+ ret = bpf_prog_get_file_line(prog, ip, &file, &line, &num);
+ if (ret < 0)
+ goto end;
+ ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n %s @ %s:%d\n",
+ (void *)ip, line, file, num);
+ return !ctxp->err;
+ }
+end:
+ ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)ip);
+ return !ctxp->err;
+}
+
+int bpf_stream_stage_dump_stack(struct bpf_stream_stage *ss)
+{
+ struct dump_stack_ctx ctx = { .ss = ss };
+ int ret;
+
+ ret = bpf_stream_stage_printk(ss, "CPU: %d UID: %d PID: %d Comm: %s\n",
+ raw_smp_processor_id(), __kuid_val(current_real_cred()->euid),
+ current->pid, current->comm);
+ if (ret)
+ return ret;
+ ret = bpf_stream_stage_printk(ss, "Call trace:\n");
+ if (ret)
+ return ret;
+ arch_bpf_stack_walk(dump_stack_cb, &ctx);
+ if (ctx.err)
+ return ctx.err;
+ return bpf_stream_stage_printk(ss, "\n");
+}
--
2.47.1
next prev parent reply other threads:[~2025-07-03 20:48 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-03 20:48 [PATCH bpf-next v5 00/12] BPF Standard Streams Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` [PATCH bpf-next v5 01/12] bpf: Refactor bprintf buffer support Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` [PATCH bpf-next v5 02/12] bpf: Introduce BPF standard streams Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` [PATCH bpf-next v5 03/12] bpf: Add function to extract program source info Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` [PATCH bpf-next v5 04/12] bpf: Ensure RCU lock is held around bpf_prog_ksym_find Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` [PATCH bpf-next v5 05/12] bpf: Add function to find program from stack trace Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` Kumar Kartikeya Dwivedi [this message]
2025-07-03 20:48 ` [PATCH bpf-next v5 07/12] bpf: Report may_goto timeout to BPF stderr Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` [PATCH bpf-next v5 08/12] bpf: Report rqspinlock deadlocks/timeout " Kumar Kartikeya Dwivedi
2025-07-30 23:02 ` Kees Cook
2025-07-30 23:07 ` Alexei Starovoitov
2025-07-30 23:09 ` Kees Cook
2025-07-30 23:13 ` Alexei Starovoitov
2025-07-30 23:19 ` Kees Cook
2025-07-03 20:48 ` [PATCH bpf-next v5 09/12] libbpf: Add bpf_stream_printk() macro Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` [PATCH bpf-next v5 10/12] libbpf: Introduce bpf_prog_stream_read() API Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` [PATCH bpf-next v5 11/12] bpftool: Add support for dumping streams Kumar Kartikeya Dwivedi
2025-07-03 20:48 ` [PATCH bpf-next v5 12/12] selftests/bpf: Add tests for prog streams Kumar Kartikeya Dwivedi
2025-07-04 2:40 ` [PATCH bpf-next v5 00/12] BPF Standard Streams patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250703204818.925464-7-memxor@gmail.com \
--to=memxor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brho@google.com \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=kernel-team@meta.com \
--cc=kkd@meta.com \
--cc=martin.lau@kernel.org \
--cc=mattbobrowski@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).