Skip to contentSkip to conversation

perf: arm_spe: Probe PMSNEVFR and PMSDSFR filter bits independently

PATCH New Ping Li 2 participants 2 messages Nov 23 Arm PMU profiling and debugging

lore
Share & tools
lore

Ping Li

author
#0 lore raw

PATCH 1/1 perf: arm_spe: Probe PMSNEVFR and PMSDSFR filter bits independently

PMSEVFR_EL1, PMSNEVFR_EL1 and PMSDSFR_EL1 each have their own implementation-defined RES0 bit sets, with the remaining bits being RAZ/WI. The driver discovers the supported event filter bits by probing PMSEVFR_EL1, but then reuses the resulting mask to validate the inverse event filter (config3) and never validates the data source filter (config4) at all beyond checking for the presence of the FDS feature.

This has two consequences:

* The inverse event filter is written verbatim to PMSNEVFR_EL1, so if PMSNEVFR_EL1 implements a different set of bits to PMSEVFR_EL1 then a filter using bits that are RAZ/WI is accepted and silently has no effect, while a filter using bits that are writable in PMSNEVFR_EL1 but not PMSEVFR_EL1 is wrongly rejected with -EOPNOTSUPP.

* PMSDSFR_EL1 holds an inclusion mask which the driver exposes inverted, so a set config4 bit requests that a data source is excluded and a cleared bit requests that it is included. Bits that are RAZ/WI are stuck at 0, so their data sources cannot be included while the data source filter is active: exclusion requests are always honoured, but inclusion requests for such sources are silently dropped.

Discover the supported filter bits of PMSNEVFR_EL1 and PMSDSFR_EL1 separately, probing each register only when the corresponding feature (FnE / FDS) is implemented. Validate the inverse event filter against the PMSNEVFR_EL1 mask, and reject data source filter configs which ask to include a data source whose PMSDSFR_EL1 bit is not implemented. For missing features, keep a full mask so that filter requests are still rejected alongside the pre-existing feature checks.

Runtime-tested on Cortex-A78AE (SPEv1.0, which implements neither FnE nor FDS) and on an SPEv1.2 CPU implementing FnE (but not FDS):
the PMU probes cleanly with the per-register discovery in place, event filter configs are validated per-bit against the probed masks with unimplemented bits rejected using -EOPNOTSUPP, and sampling is unaffected. The PMSNEVFR_EL1 probe path itself is exercised on the SPEv1.2 CPU, where the register happens to implement the same bits as PMSEVFR_EL1. Only the FDS path lacks coverage on hardware implementing the feature.

Fixes 8d9190f00a975 ("perf: arm_spe: Add support for SPEv1.2 inverted event filtering")
Fixes e6a27290d800 ("perf: arm_spe: Add support for filtering on data source")
---
 drivers/perf/arm_spe_pmu.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
drivers/perf/arm_spe_pmu.c +26 -1
index 707778c67dab..00d54202c59b 100644
@@ -92,6 +92,8 @@ struct arm_spe_pmu {
 	u64					features;
 
 	u64					pmsevfr_res0;
+	u64					pmsnevfr_res0;
+	u64					pmsdsfr_res0;
 	u16					max_record_sz;
 	u16					align;
 	struct perf_output_handle __percpu	*handle;
@@ -818,13 +820,22 @@ static int arm_spe_pmu_event_init(struct perf_event *event)
 	if (arm_spe_event_to_pmsevfr(event) & spe_pmu->pmsevfr_res0)
 		return -EOPNOTSUPP;
 
-	if (arm_spe_event_to_pmsnevfr(event) & spe_pmu->pmsevfr_res0)
+	if (arm_spe_event_to_pmsnevfr(event) & spe_pmu->pmsnevfr_res0)
 		return -EOPNOTSUPP;
 
 	if (arm_spe_event_to_pmsdsfr(event) != U64_MAX &&
 	    !(spe_pmu->features & SPE_PMU_FEAT_FDS))
 		return -EOPNOTSUPP;
 
+	/*
+	 * The data source filter is exposed inverted, so validate the
+	 * value written to PMSDSFR_EL1 (~config4) rather than config4
+	 * itself.
+	 */
+	if (ATTR_CFG_GET_FLD(attr, inv_data_src_filter) &&
+	    (arm_spe_event_to_pmsdsfr(event) & spe_pmu->pmsdsfr_res0))
+		return -EOPNOTSUPP;
+
 	if (attr->exclude_idle)
 		return -EOPNOTSUPP;
 
@@ -1228,6 +1239,20 @@ static void __arm_spe_pmu_dev_probe(void *info)
 	write_sysreg_s(U64_MAX, SYS_PMSEVFR_EL1);
 	spe_pmu->pmsevfr_res0 = ~read_sysreg_s(SYS_PMSEVFR_EL1);
 
+	if (spe_pmu->features & SPE_PMU_FEAT_INV_FILT_EVT) {
+		write_sysreg_s(U64_MAX, SYS_PMSNEVFR_EL1);
+		spe_pmu->pmsnevfr_res0 = ~read_sysreg_s(SYS_PMSNEVFR_EL1);
+	} else {
+		spe_pmu->pmsnevfr_res0 = U64_MAX;
+	}
+
+	if (spe_pmu->features & SPE_PMU_FEAT_FDS) {
+		write_sysreg_s(U64_MAX, SYS_PMSDSFR_EL1);
+		spe_pmu->pmsdsfr_res0 = ~read_sysreg_s(SYS_PMSDSFR_EL1);
+	} else {
+		spe_pmu->pmsdsfr_res0 = U64_MAX;
+	}
+
 	dev_info(dev,
 		 "probed SPEv1.%d for CPUs %*pbl [max_record_sz %u, align %u, features 0x%llx]\n",
 		 spe_pmu->pmsver - 1, cpumask_pr_args(&spe_pmu->supported_cpus),

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] perf: arm_spe: inverted pmsdsfr_res0 check in arm_spe_pmu_event_init() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] perf: arm_spe: inverted pmsdsfr_res0 check in arm_spe_pmu_event_init()
--

commit 3f0a782ec38224bd78fe15f8095fc1100ecbb217
Author: Ping Li <[email protected]>

perf: arm_spe: Probe PMSNEVFR and PMSDSFR filter bits independently

This commit probes the supported filter bits of PMSNEVFR_EL1 and PMSDSFR_EL1 independently and validates inverse event filters and data source filters against their respective RES0 masks.

Quoted 4 lines
drivers/perf/arm_spe_pmu.c +0 -0
index 707778c67dab5..00d54202c59bc 100644

[ ... ]

Quoted 10 lines
drivers/perf/arm_spe_pmu.c +1 -1
@@ -818,13 +820,22 @@ static int arm_spe_pmu_event_init(struct perf_event *event)
 	if (arm_spe_event_to_pmsevfr(event) & spe_pmu->pmsevfr_res0)
 		return -EOPNOTSUPP;
 
-	if (arm_spe_event_to_pmsnevfr(event) & spe_pmu->pmsevfr_res0)
+	if (arm_spe_event_to_pmsnevfr(event) & spe_pmu->pmsnevfr_res0)
 		return -EOPNOTSUPP;
 
 	if (arm_spe_event_to_pmsdsfr(event) != U64_MAX &&
 	    !(spe_pmu->features & SPE_PMU_FEAT_FDS))

drivers/perf/arm_spe_pmu.c static int arm_spe_pmu_event_init(struct perf_event *event) · line 837

Quoted 10 lines
drivers/perf/arm_spe_pmu.c +8 -0
 		return -EOPNOTSUPP;
 
+	/*
+	 * The data source filter is exposed inverted, so validate the
+	 * value written to PMSDSFR_EL1 (~config4) rather than config4
+	 * itself.
+	 */
+	if (ATTR_CFG_GET_FLD(attr, inv_data_src_filter) &&
+	    (arm_spe_event_to_pmsdsfr(event) & spe_pmu->pmsdsfr_res0))
+		return -EOPNOTSUPP;

[Severity: High]
Does validating arm_spe_event_to_pmsdsfr(event) (~config4) against spe_pmu->pmsdsfr_res0 reject valid exclusion masks whenever PMSDSFR_EL1 has any unimplemented RAZ/WI bits?

Because arm_spe_event_to_pmsdsfr() bitwise-inverts inv_data_src_filter, every bit left at its default of 0 in config4 becomes 1 in arm_spe_event_to_pmsdsfr(event). If a user passes inv_data_src_filter=0x1 to exclude data source 0 while leaving all other bits at 0, any unimplemented bit position in spe_pmu->pmsdsfr_res0 is 1 in both arm_spe_event_to_pmsdsfr(event) and spe_pmu->pmsdsfr_res0, so arm_spe_pmu_event_init() fails with -EOPNOTSUPP unless userspace sets every unimplemented RES0 bit in config4 (which is not exposed in sysfs caps/). Conversely, a config4 value that sets only unimplemented bits in spe_pmu->pmsdsfr_res0 clears those bits in ~config4 and is accepted.

Should this check ATTR_CFG_GET_FLD(attr, inv_data_src_filter) & spe_pmu->pmsdsfr_res0 instead, or expose the supported mask in caps/ if userspace is expected to clear unimplemented bits?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1