Subject: [PATCH 5.15 768/846] perf evsel: Override attr->sample_period for non-libpfm4 events

From: Greg Kroah-Hartman <[email-masked]>
Date: Mon, 24 Jan 2022 19:44:45 +0100

From: German Gomez <[email-masked]>

commit 3606c0e1a1050d397ad759a62607e419fd8b0ccb upstream.

A previous patch preventing "attr->sample_period" values from being
overridden in pfm events changed a related behaviour in arm-spe.

Before said patch:

  perf record -c 10000 -e arm_spe_0// -- sleep 1

Would yield an SPE event with period=10000. After the patch, the period
in "-c 10000" was being ignored because the arm-spe code initializes
sample_period to a non-zero value.

This patch restores the previous behaviour for non-libpfm4 events.

Fixes: ae5dcc8abe31 (“perf record: Prevent override of attr->sample_period for libpfm4 events”)
Reported-by: Chase Conklin <[email-masked]>
Signed-off-by: German Gomez <[email-masked]>
Cc: Alexander Shishkin <[email-masked]>
Cc: Ian Rogers <[email-masked]>
Cc: Jiri Olsa <[email-masked]>
Cc: John Fastabend <[email-masked]>
Cc: KP Singh <[email-masked]>
Cc: Mark Rutland <[email-masked]>
Cc: Martin KaFai Lau <[email-masked]>
Cc: Namhyung Kim <[email-masked]>
Cc: Song Liu <[email-masked]>
Cc: Stephane Eranian <[email-masked]>
Cc: Yonghong Song <[email-masked]>
Cc: [email-masked]
Cc: [email-masked]
Link: http://lore.kernel.org/lkml/[email-masked]
Signed-off-by: Arnaldo Carvalho de Melo <[email-masked]>
Signed-off-by: Greg Kroah-Hartman <[email-masked]>
---
 tools/perf/util/evsel.c |   25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1047,6 +1047,17 @@ void __weak arch_evsel__set_sample_weigh
 	evsel__set_sample_bit(evsel, WEIGHT);
 }
 
+static void evsel__set_default_freq_period(struct record_opts *opts,
+					   struct perf_event_attr *attr)
+{
+	if (opts->freq) {
+		attr->freq = 1;
+		attr->sample_freq = opts->freq;
+	} else {
+		attr->sample_period = opts->default_interval;
+	}
+}
+
 /*
  * The enable_on_exec/disabled value strategy:
  *
@@ -1113,14 +1124,12 @@ void evsel__config(struct evsel *evsel,
 	 * We default some events to have a default interval. But keep
 	 * it a weak assumption overridable by the user.
 	 */
-	if (!attr->sample_period) {
-		if (opts->freq) {
-			attr->freq		= 1;
-			attr->sample_freq	= opts->freq;
-		} else {
-			attr->sample_period = opts->default_interval;
-		}
-	}
+	if ((evsel->is_libpfm_event && !attr->sample_period) ||
+	    (!evsel->is_libpfm_event && (!attr->sample_period ||
+					 opts->user_freq != UINT_MAX ||
+					 opts->user_interval != ULLONG_MAX)))
+		evsel__set_default_freq_period(opts, attr);
+
 	/*
 	 * If attr->freq was set (here or earlier), ask for period
 	 * to be sampled.