From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>
Cc: bpf@vger.kernel.org, Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>,
Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
Quentin Monnet <qmo@kernel.org>, Tao Chen <chen.dylane@linux.dev>,
STAR Labs SG <info@starlabs.sg>,
Arnaud Lecomte <contact@arnaud-lcm.com>
Subject: [PATCHv3 bpf-next 12/12] bpf: Avoid changing callchain in bpf_get_stackid_pe
Date: Mon, 3 Aug 2026 23:01:49 +0200 [thread overview]
Message-ID: <20260803210149.296496-13-jolsa@kernel.org> (raw)
In-Reply-To: <20260803210149.296496-1-jolsa@kernel.org>
There's no need to modify the trace object bpf_get_stackid_pe, we just
need to pass the needed callchain length in separate argument.
This way we can have callchain pointers const and remove the trace->nr
modification and restoration.
Assisted-by: Codex:GPT-5.5
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
kernel/bpf/stackmap.c | 39 +++++++++++++++++----------------------
1 file changed, 17 insertions(+), 22 deletions(-)
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index 19858e1562ce..da72efc8b774 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -506,7 +506,7 @@ get_callchain_entry_for_task(struct task_struct *task, u32 max_depth)
struct stackid {
struct stack_map_bucket *bucket;
- u64 *ips;
+ const u64 *ips;
u32 nr;
u32 len;
u32 hash;
@@ -515,21 +515,21 @@ struct stackid {
};
static int stackid_init(struct stackid *stackid, struct bpf_map *map,
- struct perf_callchain_entry *trace, u64 flags)
+ const struct perf_callchain_entry *trace, u32 trace_nr, u64 flags)
{
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
u32 max_depth;
- if (trace->nr <= skip)
+ if (trace_nr <= skip)
/* skipping more than usable stack trace */
return -EFAULT;
max_depth = stack_map_calculate_max_depth(map->value_size, stack_map_data_size(map), flags);
- stackid->nr = min_t(u32, trace->nr - skip, max_depth - skip);
+ stackid->nr = min_t(u32, trace_nr - skip, max_depth - skip);
stackid->len = stackid->nr * sizeof(u64);
stackid->ips = trace->ip + skip;
- stackid->hash = jhash2((u32 *)stackid->ips, stackid->len / sizeof(u32), 0);
+ stackid->hash = jhash2((const u32 *)stackid->ips, stackid->len / sizeof(u32), 0);
stackid->id = stackid->hash & (smap->n_buckets - 1);
stackid->bucket = READ_ONCE(smap->buckets[stackid->id]);
stackid->hash_matches = stackid->bucket && stackid->bucket->hash == stackid->hash;
@@ -537,11 +537,12 @@ static int stackid_init(struct stackid *stackid, struct bpf_map *map,
}
static int stackid_fastpath(struct stackid *stackid, struct bpf_map *map,
- struct perf_callchain_entry *trace, u64 flags)
+ const struct perf_callchain_entry *trace, u32 trace_nr,
+ u64 flags)
{
int err;
- err = stackid_init(stackid, map, trace, flags);
+ err = stackid_init(stackid, map, trace, trace_nr, flags);
if (err)
return err;
@@ -640,7 +641,7 @@ BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
/* couldn't fetch the stack trace */
return -EFAULT;
- err = stackid_fastpath(&stackid, map, trace, flags);
+ err = stackid_fastpath(&stackid, map, trace, trace->nr, flags);
if (err != -ENOENT)
return err;
@@ -676,12 +677,13 @@ static __u64 count_kernel_ip(const struct perf_callchain_entry *trace)
BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
struct bpf_map *, map, u64, flags)
{
+ const struct perf_callchain_entry *trace;
struct perf_event *event = ctx->event;
struct stack_map_bucket *new_bucket;
- struct perf_callchain_entry *trace;
struct stackid stackid;
bool kernel, user;
__u64 nr_kernel;
+ u32 trace_nr;
int ret;
/* perf_sample_data doesn't have callchain, use bpf_get_stackid */
@@ -701,13 +703,13 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
return -EFAULT;
nr_kernel = count_kernel_ip(trace);
- __u64 nr = trace->nr; /* save original */
if (kernel) {
- trace->nr = nr_kernel;
+ trace_nr = nr_kernel;
} else { /* user */
u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
+ trace_nr = trace->nr;
skip += nr_kernel;
if (skip > BPF_F_SKIP_FIELD_MASK)
return -EFAULT;
@@ -715,21 +717,14 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
}
- ret = stackid_fastpath(&stackid, map, trace, flags);
+ ret = stackid_fastpath(&stackid, map, trace, trace_nr, flags);
if (ret != -ENOENT)
- goto out;
+ return ret;
new_bucket = stackid_new_bucket(&stackid, map);
- if (new_bucket) {
- trace->nr = nr;
+ if (new_bucket)
return stackid_install(&stackid, map, new_bucket, flags);
- }
- ret = -ENOMEM;
-
-out:
- /* restore nr */
- trace->nr = nr;
- return ret;
+ return -ENOMEM;
}
const struct bpf_func_proto bpf_get_stackid_proto_pe = {
--
2.54.0
next prev parent reply other threads:[~2026-08-03 21:03 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-03 21:01 [PATCHv3 bpf-next 00/12] bpf: Disable preemption in stack map code Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 01/12] bpf: Factor stackid_init function from __bpf_get_stackid Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 02/12] bpf: Factor stackid_fastpath " Jiri Olsa
2026-08-03 22:21 ` bot+bpf-ci
2026-08-03 21:01 ` [PATCHv3 bpf-next 03/12] bpf: Factor stackid_new_bucket " Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 04/12] bpf: Use stack id functions instead of __bpf_get_stackid Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 05/12] bpf: Disable preemption in bpf_get_stackid Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 06/12] bpf: Factor callchain_store function from __bpf_get_stack Jiri Olsa
2026-08-03 21:30 ` sashiko-bot
2026-08-04 12:02 ` Jiri Olsa
2026-08-03 22:06 ` bot+bpf-ci
2026-08-03 21:01 ` [PATCHv3 bpf-next 07/12] bpf: Factor callchain_finalize " Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 08/12] bpf: Remove trace_in argument " Jiri Olsa
2026-08-03 22:21 ` bot+bpf-ci
2026-08-04 20:05 ` Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 09/12] bpf: Clear buf on error in __bpf_get_task_stack Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 10/12] bpf: Disable preemption in __bpf_get_stack Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 11/12] bpf: Avoid changing callchain in bpf_get_stack_pe Jiri Olsa
2026-08-03 21:01 ` Jiri Olsa [this message]
2026-08-03 22:06 ` [PATCHv3 bpf-next 12/12] bpf: Avoid changing callchain in bpf_get_stackid_pe bot+bpf-ci
2026-08-04 20:05 ` Jiri Olsa
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=20260803210149.296496-13-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=chen.dylane@linux.dev \
--cc=contact@arnaud-lcm.com \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=info@starlabs.sg \
--cc=martin.lau@linux.dev \
--cc=qmo@kernel.org \
--cc=songliubraving@fb.com \
--cc=yhs@fb.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