stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] perf/x86: Fix low freq setting issue
@ 2025-01-17 15:19 kan.liang
  2025-01-17 15:19 ` [PATCH 2/3] perf: Fix low freq setting via IOC_PERIOD kan.liang
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: kan.liang @ 2025-01-17 15:19 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, irogers, linux-kernel,
	linux-perf-users
  Cc: ak, ravi.bangoria, jolsa, Kan Liang, stable

From: Kan Liang <kan.liang@linux.intel.com>

Perf doesn't work with a low freq.

perf record -e cpu_core/instructions/ppp -F 120
Error:
The sys_perf_event_open() syscall returned with 22 (Invalid argument)
for event (cpu_core/instructions/ppp).
"dmesg | grep -i perf" may provide additional information.

The limit_period() check avoids a low sampling period on a counter. It
doesn't intend to limit the frequency.
The check in the x86_pmu_hw_config() should be limited to non-freq mode.
The attr.sample_period and attr.sample_freq are union. The
attr.sample_period should not be used to indicate the freq mode.

Fixes: c46e665f0377 ("perf/x86: Add INST_RETIRED.ALL workarounds")
Closes: https://lore.kernel.org/lkml/20250115154949.3147-1-ravi.bangoria@amd.com/
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: stable@vger.kernel.org
---
 arch/x86/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 7b6430e5a77b..20ad5cca6ad2 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -630,7 +630,7 @@ int x86_pmu_hw_config(struct perf_event *event)
 	if (event->attr.type == event->pmu->type)
 		event->hw.config |= x86_pmu_get_event_config(event);
 
-	if (event->attr.sample_period && x86_pmu.limit_period) {
+	if (!event->attr.freq && x86_pmu.limit_period) {
 		s64 left = event->attr.sample_period;
 		x86_pmu.limit_period(event, &left);
 		if (left > event->attr.sample_period)
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] perf: Fix low freq setting via IOC_PERIOD
  2025-01-17 15:19 [PATCH 1/3] perf/x86: Fix low freq setting issue kan.liang
@ 2025-01-17 15:19 ` kan.liang
  2025-01-20  4:59   ` Ravi Bangoria
  2025-02-25 14:14   ` [tip: perf/urgent] perf/core: " tip-bot2 for Kan Liang
  2025-01-20  5:00 ` [PATCH 1/3] perf/x86: Fix low freq setting issue Ravi Bangoria
  2025-02-25 14:14 ` [tip: perf/urgent] perf/x86: Fix low freqency " tip-bot2 for Kan Liang
  2 siblings, 2 replies; 6+ messages in thread
From: kan.liang @ 2025-01-17 15:19 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, irogers, linux-kernel,
	linux-perf-users
  Cc: ak, ravi.bangoria, jolsa, Kan Liang, stable

From: Kan Liang <kan.liang@linux.intel.com>

A low freq cannot be set via IOC_PERIOD on some platforms.

The perf_event_check_period() introduced in commit 81ec3f3c4c4d
("perf/x86: Add check_period PMU callback") intended to check the
period, rather than the freq. A low freq may be mistakenly rejected by
the limit_period().

Fixes: 81ec3f3c4c4d ("perf/x86: Add check_period PMU callback")
Closes: https://lore.kernel.org/lkml/20250115154949.3147-1-ravi.bangoria@amd.com/
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: stable@vger.kernel.org
---
 kernel/events/core.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index f91ba29048ce..a9a04d4f3619 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5960,14 +5960,15 @@ static int _perf_event_period(struct perf_event *event, u64 value)
 	if (!value)
 		return -EINVAL;
 
-	if (event->attr.freq && value > sysctl_perf_event_sample_rate)
-		return -EINVAL;
-
-	if (perf_event_check_period(event, value))
-		return -EINVAL;
-
-	if (!event->attr.freq && (value & (1ULL << 63)))
-		return -EINVAL;
+	if (event->attr.freq) {
+		if (value > sysctl_perf_event_sample_rate)
+			return -EINVAL;
+	} else {
+		if (perf_event_check_period(event, value))
+			return -EINVAL;
+		if (value & (1ULL << 63))
+			return -EINVAL;
+	}
 
 	event_function_call(event, __perf_event_period, &value);
 
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/3] perf: Fix low freq setting via IOC_PERIOD
  2025-01-17 15:19 ` [PATCH 2/3] perf: Fix low freq setting via IOC_PERIOD kan.liang
@ 2025-01-20  4:59   ` Ravi Bangoria
  2025-02-25 14:14   ` [tip: perf/urgent] perf/core: " tip-bot2 for Kan Liang
  1 sibling, 0 replies; 6+ messages in thread
From: Ravi Bangoria @ 2025-01-20  4:59 UTC (permalink / raw)
  To: kan.liang, peterz, mingo, acme, namhyung, irogers, linux-kernel,
	linux-perf-users
  Cc: ak, jolsa, stable, Ravi Bangoria

On 17-Jan-25 8:49 PM, kan.liang@linux.intel.com wrote:
> From: Kan Liang <kan.liang@linux.intel.com>
> 
> A low freq cannot be set via IOC_PERIOD on some platforms.
> 
> The perf_event_check_period() introduced in commit 81ec3f3c4c4d
> ("perf/x86: Add check_period PMU callback") intended to check the
> period, rather than the freq. A low freq may be mistakenly rejected by
> the limit_period().
> 
> Fixes: 81ec3f3c4c4d ("perf/x86: Add check_period PMU callback")
> Closes: https://lore.kernel.org/lkml/20250115154949.3147-1-ravi.bangoria@amd.com/
> Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
> Cc: Jiri Olsa <jolsa@redhat.com>
> Cc: Ravi Bangoria <ravi.bangoria@amd.com>
> Cc: stable@vger.kernel.org

Reviewed-by: Ravi Bangoria <ravi.bangoria@amd.com>

Thanks,
Ravi

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] perf/x86: Fix low freq setting issue
  2025-01-17 15:19 [PATCH 1/3] perf/x86: Fix low freq setting issue kan.liang
  2025-01-17 15:19 ` [PATCH 2/3] perf: Fix low freq setting via IOC_PERIOD kan.liang
@ 2025-01-20  5:00 ` Ravi Bangoria
  2025-02-25 14:14 ` [tip: perf/urgent] perf/x86: Fix low freqency " tip-bot2 for Kan Liang
  2 siblings, 0 replies; 6+ messages in thread
From: Ravi Bangoria @ 2025-01-20  5:00 UTC (permalink / raw)
  To: kan.liang, peterz, mingo, acme, namhyung, irogers, linux-kernel,
	linux-perf-users
  Cc: ak, jolsa, stable, Ravi Bangoria

On 17-Jan-25 8:49 PM, kan.liang@linux.intel.com wrote:
> From: Kan Liang <kan.liang@linux.intel.com>
> 
> Perf doesn't work with a low freq.
> 
> perf record -e cpu_core/instructions/ppp -F 120
> Error:
> The sys_perf_event_open() syscall returned with 22 (Invalid argument)
> for event (cpu_core/instructions/ppp).
> "dmesg | grep -i perf" may provide additional information.
> 
> The limit_period() check avoids a low sampling period on a counter. It
> doesn't intend to limit the frequency.
> The check in the x86_pmu_hw_config() should be limited to non-freq mode.
> The attr.sample_period and attr.sample_freq are union. The
> attr.sample_period should not be used to indicate the freq mode.
> 
> Fixes: c46e665f0377 ("perf/x86: Add INST_RETIRED.ALL workarounds")
> Closes: https://lore.kernel.org/lkml/20250115154949.3147-1-ravi.bangoria@amd.com/
> Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
> Cc: Andi Kleen <ak@linux.intel.com>
> Cc: Ravi Bangoria <ravi.bangoria@amd.com>
> Cc: stable@vger.kernel.org

Reviewed-by: Ravi Bangoria <ravi.bangoria@amd.com>

Thanks,
Ravi

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [tip: perf/urgent] perf/core: Fix low freq setting via IOC_PERIOD
  2025-01-17 15:19 ` [PATCH 2/3] perf: Fix low freq setting via IOC_PERIOD kan.liang
  2025-01-20  4:59   ` Ravi Bangoria
@ 2025-02-25 14:14   ` tip-bot2 for Kan Liang
  1 sibling, 0 replies; 6+ messages in thread
From: tip-bot2 for Kan Liang @ 2025-02-25 14:14 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Kan Liang, Ingo Molnar, Ravi Bangoria, Peter Zijlstra, stable,
	x86, linux-kernel

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     0d39844150546fa1415127c5fbae26db64070dd3
Gitweb:        https://git.kernel.org/tip/0d39844150546fa1415127c5fbae26db64070dd3
Author:        Kan Liang <kan.liang@linux.intel.com>
AuthorDate:    Fri, 17 Jan 2025 07:19:12 -08:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Tue, 25 Feb 2025 14:54:14 +01:00

perf/core: Fix low freq setting via IOC_PERIOD

A low attr::freq value cannot be set via IOC_PERIOD on some platforms.

The perf_event_check_period() introduced in:

  81ec3f3c4c4d ("perf/x86: Add check_period PMU callback")

was intended to check the period, rather than the frequency.
A low frequency may be mistakenly rejected by limit_period().

Fix it.

Fixes: 81ec3f3c4c4d ("perf/x86: Add check_period PMU callback")
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20250117151913.3043942-2-kan.liang@linux.intel.com
Closes: https://lore.kernel.org/lkml/20250115154949.3147-1-ravi.bangoria@amd.com/
---
 kernel/events/core.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 086d46d..6364319 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5969,14 +5969,15 @@ static int _perf_event_period(struct perf_event *event, u64 value)
 	if (!value)
 		return -EINVAL;
 
-	if (event->attr.freq && value > sysctl_perf_event_sample_rate)
-		return -EINVAL;
-
-	if (perf_event_check_period(event, value))
-		return -EINVAL;
-
-	if (!event->attr.freq && (value & (1ULL << 63)))
-		return -EINVAL;
+	if (event->attr.freq) {
+		if (value > sysctl_perf_event_sample_rate)
+			return -EINVAL;
+	} else {
+		if (perf_event_check_period(event, value))
+			return -EINVAL;
+		if (value & (1ULL << 63))
+			return -EINVAL;
+	}
 
 	event_function_call(event, __perf_event_period, &value);
 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [tip: perf/urgent] perf/x86: Fix low freqency setting issue
  2025-01-17 15:19 [PATCH 1/3] perf/x86: Fix low freq setting issue kan.liang
  2025-01-17 15:19 ` [PATCH 2/3] perf: Fix low freq setting via IOC_PERIOD kan.liang
  2025-01-20  5:00 ` [PATCH 1/3] perf/x86: Fix low freq setting issue Ravi Bangoria
@ 2025-02-25 14:14 ` tip-bot2 for Kan Liang
  2 siblings, 0 replies; 6+ messages in thread
From: tip-bot2 for Kan Liang @ 2025-02-25 14:14 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Kan Liang, Ingo Molnar, Ravi Bangoria, Peter Zijlstra, stable,
	x86, linux-kernel

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     88ec7eedbbd21cad38707620ad6c48a4e9a87c18
Gitweb:        https://git.kernel.org/tip/88ec7eedbbd21cad38707620ad6c48a4e9a87c18
Author:        Kan Liang <kan.liang@linux.intel.com>
AuthorDate:    Fri, 17 Jan 2025 07:19:11 -08:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Tue, 25 Feb 2025 14:54:14 +01:00

perf/x86: Fix low freqency setting issue

Perf doesn't work at low frequencies:

  $ perf record -e cpu_core/instructions/ppp -F 120
  Error:
  The sys_perf_event_open() syscall returned with 22 (Invalid argument)
  for event (cpu_core/instructions/ppp).
  "dmesg | grep -i perf" may provide additional information.

The limit_period() check avoids a low sampling period on a counter. It
doesn't intend to limit the frequency.

The check in the x86_pmu_hw_config() should be limited to non-freq mode.
The attr.sample_period and attr.sample_freq are union. The
attr.sample_period should not be used to indicate the frequency mode.

Fixes: c46e665f0377 ("perf/x86: Add INST_RETIRED.ALL workarounds")
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20250117151913.3043942-1-kan.liang@linux.intel.com
Closes: https://lore.kernel.org/lkml/20250115154949.3147-1-ravi.bangoria@amd.com/
---
 arch/x86/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 8f218ac..2092d61 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -628,7 +628,7 @@ int x86_pmu_hw_config(struct perf_event *event)
 	if (event->attr.type == event->pmu->type)
 		event->hw.config |= x86_pmu_get_event_config(event);
 
-	if (event->attr.sample_period && x86_pmu.limit_period) {
+	if (!event->attr.freq && x86_pmu.limit_period) {
 		s64 left = event->attr.sample_period;
 		x86_pmu.limit_period(event, &left);
 		if (left > event->attr.sample_period)

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-02-25 14:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-17 15:19 [PATCH 1/3] perf/x86: Fix low freq setting issue kan.liang
2025-01-17 15:19 ` [PATCH 2/3] perf: Fix low freq setting via IOC_PERIOD kan.liang
2025-01-20  4:59   ` Ravi Bangoria
2025-02-25 14:14   ` [tip: perf/urgent] perf/core: " tip-bot2 for Kan Liang
2025-01-20  5:00 ` [PATCH 1/3] perf/x86: Fix low freq setting issue Ravi Bangoria
2025-02-25 14:14 ` [tip: perf/urgent] perf/x86: Fix low freqency " tip-bot2 for Kan Liang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).