From nobody Fri May  9 11:08:51 2025
Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com
 [209.85.214.201])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D9A1F507
	for <linux-kernel@vger.kernel.org>; Mon, 24 Mar 2025 16:46:02 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=209.85.214.201
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1742834764; cv=none;
 b=JEn8Yi6DmLu5+diB8m+2jmlI9fReb+XHTAuPQK7AldZV24wl1W2kU1QZegNH+jxLfBh3uTCOGWdz4ZvuQ3Yay7Sd78NaC/EUmHO6Og9gPqpOH2+TAK6p1TV5KubmuO4XWn/kSDXIAFY9u29nDBIEvoyHPcwGAzjGrGMbIF//V/g=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1742834764; c=relaxed/simple;
	bh=kxWIhqI5kQgSrfWnFj1SXB+Qsrz3NdTDGFIs7hJn0dk=;
	h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From:
	 To:Cc:Content-Type;
 b=gQTL2xU4iXczAFt/evsY5qrv57savF6DvRdzRk2pB8eLCivKo+/HmL9ixfVZIwCmktZQNPLNfT2VamVCQasst3Yb7c+ODcmFPNgRJwmOy4nn4z1Ws2ZRwW7ojh43O0MNJs1FF5koUqqBiofxyJV1Jt4/p/d7k/096dPRFKLwfvI=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=reject dis=none) header.from=google.com;
 spf=pass smtp.mailfrom=flex--irogers.bounces.google.com;
 dkim=pass (2048-bit key) header.d=google.com header.i=@google.com
 header.b=oOVNY9z5; arc=none smtp.client-ip=209.85.214.201
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=reject dis=none) header.from=google.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=flex--irogers.bounces.google.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=google.com header.i=@google.com
 header.b="oOVNY9z5"
Received: by mail-pl1-f201.google.com with SMTP id
 d9443c01a7336-224192ff68bso69801005ad.1
        for <linux-kernel@vger.kernel.org>;
 Mon, 24 Mar 2025 09:46:02 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=google.com; s=20230601; t=1742834762; x=1743439562;
 darn=vger.kernel.org;
        h=cc:to:from:subject:message-id:references:mime-version:in-reply-to
         :date:from:to:cc:subject:date:message-id:reply-to;
        bh=aOKv+RE9GVYPUsIbet19o8Ujd1At3Uyf2EUgP1dN0LA=;
        b=oOVNY9z57JzRUlbZ4eGOdsG+azCc4jdxDTfnkCnrfWcIyc+vBaOlevyDTFgHvfobl1
         ttoR365ee2zfZOZF5bcK13KnVorni9L6kKBA6o7GVEtDW7yP2eVEw7oT3WBX6FIwKAtN
         ddrViPKUt88SmTac9zSAu7IftgHym6meUBoguzmx9SAJ9PHITMkkEDSEArWs5bl3a+46
         mqBU2MX8G8jm4MRIpfD8sjXKDCmCvnwfH0L9V+XUElL0xYsydDeY0b/TDDe9r6c5TBt4
         sPx3AAe4auo74d3R6tGtQJicSuVOOqW2ChQihfEcGm3kF+NaypROl5nrkRSfAMVJ6prt
         rNqw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20230601; t=1742834762; x=1743439562;
        h=cc:to:from:subject:message-id:references:mime-version:in-reply-to
         :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
        bh=aOKv+RE9GVYPUsIbet19o8Ujd1At3Uyf2EUgP1dN0LA=;
        b=w/BWu+QZgVRJ5mMYgnEjW8kc8HqnSHpAYfXaeZLKyOtNOCPRDO47OJ9MnpZSZGhbY9
         retuJ6HA2cQOiUNvJKuY+yzCqxdqaIGUofgsoZJa6OKPBUVarcRMzB/Bn98IyeLN/U9Q
         XzCU+P39FY3VDTW5ZPypLQtlKIDnFibnmqTOD8JDLiy2ekA8vyl4Qb4rkvBKbgnKzQI1
         qAM9GnMAW0byfuaNSp01pQXC77xNVBoq6vZrPmzX2hsuq5uONSmjcFZ+JOqMET43MetP
         pNMuEI1/JFMEXrPxUncfv/YUIRwOjsY56Dx5uD3FtzMZdpSpdcJVNZm3Wr/b61q7IYH7
         WE7Q==
X-Forwarded-Encrypted: i=1;
 AJvYcCXQgwLgeId5hHFqZ3rskcHuNpJMJ2Mxr7Fm+4lKcE1lJEKq/qGbHMDmqPuXUn9ikX3t6TUsLAS0mcehXi8=@vger.kernel.org
X-Gm-Message-State: AOJu0Yz5Gv6uRbqVlGXgoL6/gsyeShRYCHeIeuyIciDm49Ye3OobISGG
	jQX+2AUza+xOXcr/EJ3fj+TrV+hD5zp914b25VxV1kihhxiBOgT8d9YfInT+ekrS1ZECNClzSh+
	l5abZog==
X-Google-Smtp-Source: 
 AGHT+IGBHhsPQI40bxF8e5SZUalli4eDczXVHBI0ne3gaUoq80rI6UrqnCCns2/QyBvGAVbiwmVw9Lw3aojp
X-Received: from plblc15.prod.google.com
 ([2002:a17:902:fa8f:b0:223:67ac:e082])
 (user=irogers job=prod-delivery.src-stubby-dispatcher) by
 2002:a17:903:1a2f:b0:210:fce4:11ec
 with SMTP id d9443c01a7336-22780c69ce3mr210455165ad.1.1742834762326; Mon, 24
 Mar 2025 09:46:02 -0700 (PDT)
Date: Mon, 24 Mar 2025 09:45:41 -0700
In-Reply-To: <20250324164543.62574-1-irogers@google.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
Mime-Version: 1.0
References: <20250324164543.62574-1-irogers@google.com>
X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog
Message-ID: <20250324164543.62574-2-irogers@google.com>
Subject: [PATCH v6 1/2] perf record: Skip don't fail for events that don't
 open
From: Ian Rogers <irogers@google.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>,
	Kan Liang <kan.liang@linux.intel.com>, James Clark <james.clark@linaro.org>,
	Ze Gao <zegao2021@gmail.com>, Weilin Wang <weilin.wang@intel.com>,
	Dominique Martinet <asmadeus@codewreck.org>,
	Jean-Philippe Romain <jean-philippe.romain@foss.st.com>,
 Junhao He <hejunhao3@huawei.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	Aditya Bodkhe <Aditya.Bodkhe1@ibm.com>
Cc: Leo Yan <leo.yan@arm.com>, Atish Patra <atishp@rivosinc.com>
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

Whilst for many tools it is an expected behavior that failure to open
a perf event is a failure, ARM decided to name PMU events the same as
legacy events and then failed to rename such events on a server uncore
SLC PMU. As perf's default behavior when no PMU is specified is to
open the event on all PMUs that advertise/"have" the event, this
yielded failures when trying to make the priority of legacy and
sysfs/json events uniform - something requested by RISC-V and ARM. A
legacy event user on ARM hardware may find their event opened on an
uncore PMU which for perf record will fail. Arnaldo suggested skipping
such events which this patch implements. Rather than have the skipping
conditional on running on ARM, the skipping is done on all
architectures as such a fundamental behavioral difference could lead
to problems with tools built/depending on perf.

An example of perf record failing to open events on x86 is:
```
$ perf record -e data_read,cycles,LLC-prefetch-read -a sleep 0.1
Error:
Failure to open event 'data_read' on PMU 'uncore_imc_free_running_0' which =
will be removed.
The sys_perf_event_open() syscall returned with 22 (Invalid argument) for e=
vent (data_read).
"dmesg | grep -i perf" may provide additional information.

Error:
Failure to open event 'data_read' on PMU 'uncore_imc_free_running_1' which =
will be removed.
The sys_perf_event_open() syscall returned with 22 (Invalid argument) for e=
vent (data_read).
"dmesg | grep -i perf" may provide additional information.

Error:
Failure to open event 'LLC-prefetch-read' on PMU 'cpu' which will be remove=
d.
The LLC-prefetch-read event is not supported.
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 2.188 MB perf.data (87 samples) ]

$ perf report --stats
Aggregated stats:
               TOTAL events:      17255
                MMAP events:        284  ( 1.6%)
                COMM events:       1961  (11.4%)
                EXIT events:          1  ( 0.0%)
                FORK events:       1960  (11.4%)
              SAMPLE events:         87  ( 0.5%)
               MMAP2 events:      12836  (74.4%)
             KSYMBOL events:         83  ( 0.5%)
           BPF_EVENT events:         36  ( 0.2%)
      FINISHED_ROUND events:          2  ( 0.0%)
            ID_INDEX events:          1  ( 0.0%)
          THREAD_MAP events:          1  ( 0.0%)
             CPU_MAP events:          1  ( 0.0%)
           TIME_CONV events:          1  ( 0.0%)
       FINISHED_INIT events:          1  ( 0.0%)
cycles stats:
              SAMPLE events:         87
```

If all events fail to open then the perf record will fail:
```
$ perf record -e LLC-prefetch-read true
Error:
Failure to open event 'LLC-prefetch-read' on PMU 'cpu' which will be remove=
d.
The LLC-prefetch-read event is not supported.
Error:
Failure to open any events for recording
```

As an evlist may have dummy events that open when all command line
events fail we ignore dummy events when detecting if at least some
events open. This still permits the dummy event on its own to be used
as a permission check:
```
$ perf record -e dummy true
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.046 MB perf.data ]
```
but allows failure when a dummy event is implicilty inserted or when
there are insufficient permissions to open it:
```
$ perf record -e LLC-prefetch-read -a true
Error:
Failure to open event 'LLC-prefetch-read' on PMU 'cpu' which will be remove=
d.
The LLC-prefetch-read event is not supported.
Error:
Failure to open any events for recording
```

The issue with legacy events is that on RISC-V they want the driver to
not have mappings from legacy to non-legacy config encodings for each
vendor/model due to size, complexity and difficulty to update. It was
reported that on ARM Apple-M? CPUs the legacy mapping in the driver
was broken and the sysfs/json events should always take precedent,
however, it isn't clear this is still the case. It is the case that
without working around this issue a legacy event like cycles without a
PMU can encode differently than when specified with a PMU - the
non-PMU version favoring legacy encodings, the PMU one avoiding legacy
encodings.

The patch removes events and then adjusts the idx value for each
evsel. This is done so that the dense xyarrays used for file
descriptors, etc. don't contain broken entries. As event opening
happens relatively late in the record process, use of the idx value
before the open will have become corrupted, so it is expected there
are latent bugs hidden behind this change - the change is best
effort. As the only vendor that has broken event names is ARM, this
will principally effect ARM users. They will also experience warning
messages like those above because of the uncore PMU advertising legacy
event names.

Suggested-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: James Clark <james.clark@linaro.org>
Tested-by: Leo Yan <leo.yan@arm.com>
Tested-by: Atish Patra <atishp@rivosinc.com>
---
 tools/perf/builtin-record.c | 47 ++++++++++++++++++++++++++++++++-----
 1 file changed, 41 insertions(+), 6 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index ba20bf7c011d..00f6a1c23254 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -962,7 +962,6 @@ static int record__config_tracking_events(struct record=
 *rec)
 	 */
 	if (opts->target.initial_delay || target__has_cpu(&opts->target) ||
 	    perf_pmus__num_core_pmus() > 1) {
-
 		/*
 		 * User space tasks can migrate between CPUs, so when tracing
 		 * selected CPUs, sideband for all CPUs is still needed.
@@ -1367,6 +1366,7 @@ static int record__open(struct record *rec)
 	struct perf_session *session =3D rec->session;
 	struct record_opts *opts =3D &rec->opts;
 	int rc =3D 0;
+	bool skipped =3D false;
=20
 	evlist__for_each_entry(evlist, pos) {
 try_again:
@@ -1382,15 +1382,50 @@ static int record__open(struct record *rec)
 			        pos =3D evlist__reset_weak_group(evlist, pos, true);
 				goto try_again;
 			}
-			rc =3D -errno;
 			evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg));
-			ui__error("%s\n", msg);
-			goto out;
+			ui__error("Failure to open event '%s' on PMU '%s' which will be removed=
.\n%s\n",
+				  evsel__name(pos), evsel__pmu_name(pos), msg);
+			pos->skippable =3D true;
+			skipped =3D true;
+		} else {
+			pos->supported =3D true;
 		}
-
-		pos->supported =3D true;
 	}
=20
+	if (skipped) {
+		struct evsel *tmp;
+		int idx =3D 0;
+		bool evlist_empty =3D true;
+
+		/* Remove evsels that failed to open and update indices. */
+		evlist__for_each_entry_safe(evlist, tmp, pos) {
+			if (pos->skippable) {
+				evlist__remove(evlist, pos);
+				continue;
+			}
+
+			/*
+			 * Note, dummy events may be command line parsed or
+			 * added by the tool. We care about supporting `perf
+			 * record -e dummy` which may be used as a permission
+			 * check. Dummy events that are added to the command
+			 * line and opened along with other events that fail,
+			 * will still fail as if the dummy events were tool
+			 * added events for the sake of code simplicity.
+			 */
+			if (!evsel__is_dummy_event(pos))
+				evlist_empty =3D false;
+		}
+		evlist__for_each_entry(evlist, pos) {
+			pos->core.idx =3D idx++;
+		}
+		/* If list is empty then fail. */
+		if (evlist_empty) {
+			ui__error("Failure to open any events for recording.\n");
+			rc =3D -1;
+			goto out;
+		}
+	}
 	if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) {
 		pr_warning(
 "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
--=20
2.49.0.395.g12beb8f557-goog