mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Prashant Malani <pmalani@google.com>
To: Ben Segall <bsegall@google.com>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	 Ingo Molnar <mingo@redhat.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	 open list <linux-kernel@vger.kernel.org>,
	 "open list:CPU FREQUENCY SCALING FRAMEWORK"
	<linux-pm@vger.kernel.org>, Mel Gorman <mgorman@suse.de>,
	 Peter Zijlstra <peterz@infradead.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	 Steven Rostedt <rostedt@goodmis.org>,
	Valentin Schneider <vschneid@redhat.com>,
	 Vincent Guittot <vincent.guittot@linaro.org>,
	Viresh Kumar <viresh.kumar@linaro.org>
Cc: Prashant Malani <pmalani@google.com>
Subject: [PATCH v2 2/2] cpufreq: CPPC: Dont read counters for idle CPUs
Date: Thu, 19 Jun 2025 00:09:17 +0000	[thread overview]
Message-ID: <20250619000925.415528-3-pmalani@google.com> (raw)
In-Reply-To: <20250619000925.415528-1-pmalani@google.com>

AMU performance counters tend to be inaccurate when measured on idle CPUs.
On an idle CPU which is programmed to 3.4 GHz (verified through firmware),
here is a measurement and calculation of operating frequency:

t0: ref=899127636, del=3012458473
t1: ref=899129626, del=3012466509
perf=40

For reference, when we measure the same CPU with stress-ng running, we have
a more accurate result:
t0: ref=30751756418, del=104490567689
t1: ref=30751760628, del=104490582296
perf=34

(t0 and t1 are 2 microseconds apart)

In the above, the prescribed method[1] of calculating frequency from CPPC
counters was used.

The follow-on effect is that the inaccurate frequency is stashed in the
cpufreq policy struct when the CPU is brought online. Since CPUs are mostly
idle when they are brought online, this means cpufreq has an inaccurate
view of the programmed clock rate.

Consequently, if userspace tries to actually set the frequency to the
previously erroneous rate (4 GHz in the above example), cpufreq returns
early without calling in to the CPPC driver to send the relevant PCC
command; it thinks the CPU is already at that frequency.

Update the CPPC get_rate() code to skip sampling counters if we know a CPU
is idle, and go directly to the fallback response of returning the
“desired” frequency. The code intends to do that anyway if the counters
happen to return an “idle” reading.

[1] https://docs.kernel.org/admin-guide/acpi/cppc_sysfs.html#computing-average-delivered-performance

Signed-off-by: Prashant Malani <pmalani@google.com>
---

Changes in v2:
- Add sched.h header for usage when compiled as module.

 drivers/cpufreq/cppc_cpufreq.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
index b7c688a5659c..5ed04774e569 100644
--- a/drivers/cpufreq/cppc_cpufreq.c
+++ b/drivers/cpufreq/cppc_cpufreq.c
@@ -18,6 +18,7 @@
 #include <linux/cpufreq.h>
 #include <linux/irq_work.h>
 #include <linux/kthread.h>
+#include <linux/sched.h>
 #include <linux/time.h>
 #include <linux/vmalloc.h>
 #include <uapi/linux/sched/types.h>
@@ -753,6 +754,10 @@ static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
 
 	cpufreq_cpu_put(policy);
 
+	/* Idle CPUs have unreliable counters, so skip to the end. */
+	if (idle_cpu(cpu))
+		goto out_invalid_counters;
+
 	ret = cppc_get_perf_ctrs_sample(cpu, &fb_ctrs_t0, &fb_ctrs_t1);
 	if (ret) {
 		if (ret == -EFAULT)
-- 
2.50.0.rc2.701.gf1e915cc24-goog


  parent reply	other threads:[~2025-06-19  0:09 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-19  0:09 [PATCH v2 0/2] cpufreq: CPPC: idle cpu perf handling Prashant Malani
2025-06-19  0:09 ` [PATCH v2 1/2] sched: Expose idle_cpu() to modules Prashant Malani
2025-06-19  0:09 ` Prashant Malani [this message]
2025-06-20  3:53   ` [PATCH v2 2/2] cpufreq: CPPC: Dont read counters for idle CPUs Jie Zhan
2025-06-20  5:07     ` Prashant Malani
2025-06-26 18:42       ` Prashant Malani
2025-06-27  7:54       ` Jie Zhan
2025-06-27 17:07         ` Prashant Malani
2025-07-02 18:38           ` Prashant Malani
2025-07-03  9:29             ` Beata Michalska
2025-07-07  8:32             ` Beata Michalska
2025-07-09 17:25               ` Prashant Malani
2025-07-09 22:49                 ` Prashant Malani
2025-07-14  9:30                   ` Beata Michalska
2025-07-15  6:28                     ` Prashant Malani
2025-07-21 17:00                       ` Rafael J. Wysocki
2025-07-21 19:40                         ` Prashant Malani
2025-07-22  3:27                           ` Viresh Kumar
2025-07-22  6:02                             ` Prashant Malani
2025-07-30  7:31                               ` Prashant Malani
2025-07-31  8:27                                 ` Beata Michalska
2025-07-31 11:13                                   ` Viresh Kumar
2025-07-31 20:23                                     ` Beata Michalska
2025-08-01  4:43                                       ` Viresh Kumar
2025-08-07  0:19                                         ` Prashant Malani
2025-08-11  6:05                                           ` Viresh Kumar
2025-08-11 18:43                                             ` Prashant Malani
2025-08-11 19:19                                               ` Rafael J. Wysocki
2025-08-11 20:01                                                 ` Prashant Malani
2025-08-14 11:48                                                   ` Rafael J. Wysocki
2025-08-15  5:12                                                     ` Prashant Malani
2025-08-16  8:25                                                       ` Prashant Malani
2025-08-13 10:12                                               ` Beata Michalska
2025-07-31 16:51                                   ` Prashant Malani
2025-07-31 20:30                                     ` Beata Michalska
2025-08-01  9:16                                       ` Prashant Malani
2025-08-04 20:55                                         ` Prashant Malani
2025-08-06  7:21                                           ` Beata Michalska
2025-08-07  0:01                                             ` Prashant Malani
2025-08-07 10:24                                               ` Beata Michalska
2025-08-08  2:14                                                 ` Prashant Malani
2025-08-13 10:15                                                   ` Beata Michalska
2025-08-13 22:25                                                     ` Prashant Malani
2025-07-07  8:35         ` Beata Michalska

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=20250619000925.415528-3-pmalani@google.com \
    --to=pmalani@google.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rafael@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=vincent.guittot@linaro.org \
    --cc=viresh.kumar@linaro.org \
    --cc=vschneid@redhat.com \
    /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

all inboxes | Powered by JetHome®