* [PATCH perf/core] perf: Save callchain->nr in perf_sample_data
@ 2026-12-05 17:09 Weiming Shi
2026-12-05 17:29 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Weiming Shi @ 2026-12-05 17:09 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: linux-perf-users, linux-kernel, Markus Metzger,
co+a3fa2401636b2e85, Xiang Mei, Weiming Shi, stable
get_perf_callchain() releases the per-CPU recursion slot via
put_callchain_entry() before returning, so a same-context callchain taker
(e.g. a kprobe or BPF tracing program) on the same CPU can reuse the
entry and overwrite entry->nr.
perf_sample_save_callchain() folds data->callchain->nr into data->dyn_size
for ring-buffer reservation and discards the count, while
perf_output_sample() re-reads data->callchain->nr without READ_ONCE() to
size __output_copy(). A rewrite between the two reads either writes past
header->size into neighboring ring-buffer records or under-writes and
corrupts record framing. For example, with a kprobe perf event on
perf_output_copy() taking a callchain in the same hardirq context:
header->size = 48 bytes (reserved nr = 2)
copied = 120 bytes (copied nr = 11, +72 bytes past header->size)
Read data->callchain->nr once with READ_ONCE() in
perf_sample_save_callchain(), store it in data->callchain_nr (which fits
in existing trailing padding of struct perf_sample_data), and use that
cached count in perf_output_sample() to emit nr and copy
data->callchain->ip.
Fixes: 5622f295b53f ("x86, perf_counter, bts: Optimize BTS overflow handling")
Cc: stable@vger.kernel.org
Reported-by: <co+a3fa2401636b2e85@bugs.sh>
Assisted-by: LLM
Signed-off-by: Weiming Shi <bestswngs@gmail.com>
---
include/linux/perf_event.h | 8 +++++---
kernel/events/core.c | 9 +++++----
2 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index b52348eca6ef..db9a8fa947cd 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -1330,6 +1330,7 @@ struct perf_sample_data {
*/
u64 ip;
struct perf_callchain_entry *callchain;
+ u64 callchain_nr;
struct perf_raw_record *raw;
struct perf_branch_stack *br_stack;
u64 *br_stack_cntr;
@@ -1376,7 +1377,7 @@ static inline void perf_sample_save_callchain(struct perf_sample_data *data,
struct perf_event *event,
struct pt_regs *regs)
{
- int size = 1;
+ u64 nr;
if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN))
return;
@@ -1384,9 +1385,10 @@ static inline void perf_sample_save_callchain(struct perf_sample_data *data,
return;
data->callchain = perf_callchain(event, regs);
- size += data->callchain->nr;
+ nr = READ_ONCE(data->callchain->nr);
+ data->callchain_nr = nr;
- data->dyn_size += size * sizeof(u64);
+ data->dyn_size += (1 + nr) * sizeof(u64);
data->sample_flags |= PERF_SAMPLE_CALLCHAIN;
}
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4ab3919ac78e..f2e8d1e60720 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -8451,11 +8451,12 @@ void perf_output_sample(struct perf_output_handle *handle,
perf_output_read(handle, event);
if (sample_type & PERF_SAMPLE_CALLCHAIN) {
- int size = 1;
+ u64 nr = data->callchain_nr;
- size += data->callchain->nr;
- size *= sizeof(u64);
- __output_copy(handle, data->callchain, size);
+ __output_copy(handle, &nr, sizeof(nr));
+ if (nr)
+ __output_copy(handle, data->callchain->ip,
+ nr * sizeof(u64));
}
if (sample_type & PERF_SAMPLE_RAW) {
--
2.56.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH perf/core] perf: Save callchain->nr in perf_sample_data
2026-12-05 17:09 [PATCH perf/core] perf: Save callchain->nr in perf_sample_data Weiming Shi
@ 2026-12-05 17:29 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 17:29 UTC (permalink / raw)
To: Weiming Shi; +Cc: linux-perf-users
> get_perf_callchain() releases the per-CPU recursion slot via
> put_callchain_entry() before returning, so a same-context callchain taker
> (e.g. a kprobe or BPF tracing program) on the same CPU can reuse the
> entry and overwrite entry->nr.
>
> perf_sample_save_callchain() folds data->callchain->nr into data->dyn_size
> for ring-buffer reservation and discards the count, while
> perf_output_sample() re-reads data->callchain->nr without READ_ONCE() to
> size __output_copy(). A rewrite between the two reads either writes past
> header->size into neighboring ring-buffer records or under-writes and
> corrupts record framing. For example, with a kprobe perf event on
> perf_output_copy() taking a callchain in the same hardirq context:
>
> header->size = 48 bytes (reserved nr = 2)
> copied = 120 bytes (copied nr = 11, +72 bytes past header->size)
>
> Read data->callchain->nr once with READ_ONCE() in
> perf_sample_save_callchain(), store it in data->callchain_nr (which fits
> in existing trailing padding of struct perf_sample_data), and use that
> cached count in perf_output_sample() to emit nr and copy
> data->callchain->ip.
>
> Fixes: 5622f295b53f ("x86, perf_counter, bts: Optimize BTS overflow handling")
> Cc: stable@vger.kernel.org
> Reported-by: <co+a3fa2401636b2e85@bugs.sh>
> Assisted-by: LLM
> Signed-off-by: Weiming Shi <bestswngs@gmail.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205170903.4178905-2-bestswngs@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-05 17:29 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 17:09 [PATCH perf/core] perf: Save callchain->nr in perf_sample_data Weiming Shi
2026-12-05 17:29 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.