Linux Perf Users
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Mark Rutland <mark.rutland@arm.com>
Cc: Baisheng Gao <baisheng.gao@unisoc.com>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	"reviewer:PERFORMANCE EVENTS SUBSYSTEM"
	<kan.liang@linux.intel.com>,
	"open list:PERFORMANCE EVENTS SUBSYSTEM"
	<linux-perf-users@vger.kernel.org>,
	"open list:PERFORMANCE EVENTS SUBSYSTEM"
	<linux-kernel@vger.kernel.org>,
	cixi.geng@linux.dev, hao_hao.wang@unisoc.com
Subject: Re: [PATCH] perf/core: Handling the race between exit_mmap and perf sample
Date: Wed, 4 Jun 2025 17:32:19 +0200	[thread overview]
Message-ID: <20250604153219.GJ39944@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <aEBeRfScZKD-7h5u@J2N7QTR9R3>

On Wed, Jun 04, 2025 at 03:55:01PM +0100, Mark Rutland wrote:

> I think we might need something in the perf core for cpu-bound events, assuming
> those can also potentially make samples.
> 
> From a quick scan of perf_event_sample_format:
> 
> 	PERF_SAMPLE_IP			// safe
> 	PERF_SAMPLE_TID			// safe
> 	PERF_SAMPLE_TIME		// safe
> 	PERF_SAMPLE_ADDR		// ???

Safe, set by driver, or 0.

> 	PERF_SAMPLE_READ		// ???

This is basically read(2) on a fd, but in sample format. Only the count
values. This good.

> 	PERF_SAMPLE_CALLCHAIN		// may access mm

Right.

> 	PERF_SAMPLE_ID			// safe
> 	PERF_SAMPLE_CPU			// safe
> 	PERF_SAMPLE_PERIOD		// safe
> 	PERF_SAMPLE_STREAM_ID		// ???
safe

> 	PERF_SAMPLE_RAW			// ???
safe, this is random data returned by the driver

> 	PERF_SAMPLE_BRANCH_STACK	// safe
> 	PERF_SAMPLE_REGS_USER		// safe
> 	PERF_SAMPLE_STACK_USER		// may access mm
> 	PERF_SAMPLE_WEIGHT		// ???
> 	PERF_SAMPLE_DATA_SRC		// ???
Both should be safe, driver sets them.

> 	PERF_SAMPLE_IDENTIFIER		// safe
> 	PERF_SAMPLE_TRANSACTION		// ???
Safe, another random thing the driver can set. This was for
transactional memory stuff.

> 	PERF_SAMPLE_REGS_INTR		// safe
> 	PERF_SAMPLE_PHYS_ADDR		// safe; handles mm==NULL && addr < TASK_SIZE
> 	PERF_SAMPLE_AUX			// ???

Safe, should be driver, PT for Intel, or that CoreSight for ARM.

> 	PERF_SAMPLE_CGROUP		// safe
> 	PERF_SAMPLE_DATA_PAGE_SIZE	// partial; doesn't check addr < TASK_SIZE
> 	PERF_SAMPLE_CODE_PAGE_SIZE	// partial; doesn't check addr < TASK_SIZE

But does use init_mm when !mm, perf_get_page_size().

> 	PERF_SAMPLE_WEIGHT_STRUCT	// ???
Safe, driver bits again.

> 
> ... I think all the dodgy cases use mm somehow, so maybe the perf core
> should check for current->mm?

This then... I suppose.

---
diff --git a/kernel/events/core.c b/kernel/events/core.c
index f34c99f8ce8f..49944e4ec3e7 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7439,6 +7439,10 @@ perf_sample_ustack_size(u16 stack_size, u16 header_size,
 	if (!regs)
 		return 0;
 
+	/* No mm, no stack, no dump. */
+	if (!current->mm)
+		return 0;
+
 	/*
 	 * Check if we fit in with the requested stack size into the:
 	 * - TASK_SIZE
@@ -8153,6 +8157,9 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)
 	if (!kernel && !user)
 		return &__empty_callchain;
 
+	if (!current->mm)
+		user = false;
+
 	callchain = get_perf_callchain(regs, 0, kernel, user,
 				       max_stack, crosstask, true);
 	return callchain ?: &__empty_callchain;

  reply	other threads:[~2025-06-04 15:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-24  2:54 [PATCH] perf/core: Handling the race between exit_mmap and perf sample Baisheng Gao
2025-04-24 10:40 ` Peter Zijlstra
2025-06-04 14:05 ` Mark Rutland
2025-06-04 14:24   ` Peter Zijlstra
2025-06-04 14:55     ` Mark Rutland
2025-06-04 15:32       ` Peter Zijlstra [this message]
2025-06-04 16:08         ` Mark Rutland
2025-06-05 11:08           ` Peter Zijlstra

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=20250604153219.GJ39944@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=baisheng.gao@unisoc.com \
    --cc=cixi.geng@linux.dev \
    --cc=hao_hao.wang@unisoc.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    /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