mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chengdong Li <chengdongli@optimatist.com>
To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	namhyung@kernel.org, mark.rutland@arm.com,
	alexander.shishkin@linux.intel.com, jolsa@kernel.org,
	irogers@google.com, adrian.hunter@intel.com,
	kan.liang@linux.intel.com
Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	Chengdong Li <chengdongli@optimatist.com>
Subject: [PATCH] perf/core: Improve arguments checking of inherited per-task counters when sampling.
Date: Thu, 14 Aug 2025 19:06:25 +0800	[thread overview]
Message-ID: <20250814110625.84622-1-chengdongli@optimatist.com> (raw)

It's not allowed to mmap() of inherited per-task counters with CPU ==
-1, this would create a performance issue. But it is not friendly to
developers as current implementation postponed the arguments checking to
perf_mmap(), developer can get an -EINVAL from mmap() but without
any previous error returned from perf_event_open().

This patch improves it by moving the arguments checking from perf_mmap()
to perf_event_open().

Signed-off-by: Chengdong Li <chengdongli@optimatist.com>
---
 kernel/events/core.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 8060c2857bb2..f102adb395ec 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6941,14 +6941,6 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
 	int ret, flags = 0;
 	mapped_f mapped;
 
-	/*
-	 * Don't allow mmap() of inherited per-task counters. This would
-	 * create a performance issue due to all children writing to the
-	 * same rb.
-	 */
-	if (event->cpu == -1 && event->attr.inherit)
-		return -EINVAL;
-
 	if (!(vma->vm_flags & VM_SHARED))
 		return -EINVAL;
 
@@ -13392,6 +13384,18 @@ SYSCALL_DEFINE5(perf_event_open,
 			return -EACCES;
 	}
 
+	/*
+	 * Don't allow perf_event_open() of inherited per-task counters
+	 * with cpu == 1 when sampling. Otherwise, this would create a
+	 * performance issue due to all children writing to the same mmap()
+	 * created ring buffer.
+	 *
+	 * We recommend to call perf_event_open() for all cpus when sampling on
+	 * inherited per-task counters.
+	 */
+	if (attr.sample_freq && attr.inherit && cpu == -1)
+		return -EINVAL;
+
 	if (attr.freq) {
 		if (attr.sample_freq > sysctl_perf_event_sample_rate)
 			return -EINVAL;
-- 
2.43.0


             reply	other threads:[~2025-08-14 11:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-14 11:06 Chengdong Li [this message]
2025-08-19 10:26 ` Mark Rutland
2025-08-19 13:31   ` Chengdong Li(李成栋)
2025-08-27 10:44     ` Mark Rutland
2025-08-20  7:17 ` kernel test robot

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=20250814110625.84622-1-chengdongli@optimatist.com \
    --to=chengdongli@optimatist.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.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 \
    --cc=peterz@infradead.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

all inboxes | Powered by JetHome®