Linux Perf Users
 help / color / mirror / Atom feed
From: Ravi Bangoria <ravi.bangoria@amd.com>
To: <peterz@infradead.org>, <kan.liang@linux.intel.com>
Cc: <ravi.bangoria@amd.com>, <mingo@redhat.com>, <acme@kernel.org>,
	<namhyung@kernel.org>, <eranian@google.com>, <irogers@google.com>,
	<bp@alien8.de>, <x86@kernel.org>,
	<linux-perf-users@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <santosh.shukla@amd.com>,
	<ananth.narayan@amd.com>, <sandipan.das@amd.com>
Subject: [UNTESTED][PATCH] perf/x86: Fix limit_period() for 'freq mode events'
Date: Wed, 15 Jan 2025 15:49:49 +0000	[thread overview]
Message-ID: <20250115154949.3147-1-ravi.bangoria@amd.com> (raw)

For the freq mode event ...

event->attr.sample_period contains sampling freq, not the sample period.
So, use actual sample period (event->hw.sample_period) while calling
limit_period().

Kernel dynamically adjusts event sample period after every sample to meet
the desire sampling freq. For this, kernel starts with sample period = 1
and gradually increase it. Instead of simply returning error, start
calibrating freq with the minimum sample period provided by limit_period().

Similarly, value provided along with ioctl(PERF_EVENT_IOC_PERIOD) contains
new freq not the sample period. Avoid calling limit_period() for freq mode
event in the ioctl() code path.

Signed-off-by: Ravi Bangoria <ravi.bangoria@amd.com>
---
UNTESTED: limit_period() is mostly defined by Intel PMUs and I don't have
any of those test machines.

 arch/x86/events/core.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index c75c482d4c52..924aa35676d3 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -629,10 +629,22 @@ int x86_pmu_hw_config(struct perf_event *event)
 		event->hw.config |= x86_pmu_get_event_config(event);
 
 	if (event->attr.sample_period && x86_pmu.limit_period) {
-		s64 left = event->attr.sample_period;
-		x86_pmu.limit_period(event, &left);
-		if (left > event->attr.sample_period)
-			return -EINVAL;
+		if (event->attr.freq) {
+			s64 left = event->hw.sample_period;
+
+			x86_pmu.limit_period(event, &left);
+			if (left != event->hw.sample_period) {
+				event->hw.sample_period = left;
+				event->hw.last_period = left;
+				local64_set(&event->hw.period_left, left);
+			}
+		} else {
+			s64 left = event->attr.sample_period;
+
+			x86_pmu.limit_period(event, &left);
+			if (left > event->attr.sample_period)
+				return -EINVAL;
+		}
 	}
 
 	/* sample_regs_user never support XMM registers */
@@ -2648,6 +2660,9 @@ static int x86_pmu_check_period(struct perf_event *event, u64 value)
 	if (x86_pmu.check_period && x86_pmu.check_period(event, value))
 		return -EINVAL;
 
+	if (event->attr.freq)
+		return 0;
+
 	if (value && x86_pmu.limit_period) {
 		s64 left = value;
 		x86_pmu.limit_period(event, &left);
-- 
2.34.1


             reply	other threads:[~2025-01-15 15:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-15 15:49 Ravi Bangoria [this message]
2025-01-15 19:13 ` [UNTESTED][PATCH] perf/x86: Fix limit_period() for 'freq mode events' Liang, Kan
2025-01-16  4:21   ` Ravi Bangoria
2025-01-16  4:28     ` Ravi Bangoria
2025-01-17 15:20     ` Liang, Kan

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=20250115154949.3147-1-ravi.bangoria@amd.com \
    --to=ravi.bangoria@amd.com \
    --cc=acme@kernel.org \
    --cc=ananth.narayan@amd.com \
    --cc=bp@alien8.de \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=sandipan.das@amd.com \
    --cc=santosh.shukla@amd.com \
    --cc=x86@kernel.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