Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Weiming Shi <bestswngs@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>
Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	Markus Metzger <markus.t.metzger@intel.com>,
	co+a3fa2401636b2e85@bugs.sh, Xiang Mei <xmei5@asu.edu>,
	Weiming Shi <bestswngs@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH perf/core] perf: Save callchain->nr in perf_sample_data
Date: Sat,  5 Dec 2026 09:09:04 -0800	[thread overview]
Message-ID: <20261205170903.4178905-2-bestswngs@gmail.com> (raw)

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


                 reply	other threads:[~2026-12-05 17:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261205170903.4178905-2-bestswngs@gmail.com \
    --to=bestswngs@gmail.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=co+a3fa2401636b2e85@bugs.sh \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=markus.t.metzger@intel.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=stable@vger.kernel.org \
    --cc=xmei5@asu.edu \
    /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