Commit 4815d3c5 authored by Rafael J. Wysocki's avatar Rafael J. Wysocki

cpufreq: x86: Make scaling_cur_freq behave more as expected

After commit f8475cef "x86: use common aperfmperf_khz_on_cpu() to
calculate KHz using APERF/MPERF" the scaling_cur_freq policy attribute
in sysfs only behaves as expected on x86 with APERF/MPERF registers
available when it is read from at least twice in a row.  The value
returned by the first read may not be meaningful, because the
computations in there use cached values from the previous iteration
of aperfmperf_snapshot_khz() which may be stale.

To prevent that from happening, modify arch_freq_get_on_cpu() to
call aperfmperf_snapshot_khz() twice, with a short delay between
these calls, if the previous invocation of aperfmperf_snapshot_khz()
was too far back in the past (specifically, more that 1s ago).

Also, as pointed out by Doug Smythies, aperf_delta is limited now
and the multiplication of it by cpu_khz won't overflow, so simplify
the s->khz computations too.

Fixes: f8475cef "x86: use common aperfmperf_khz_on_cpu() to calculate KHz using APERF/MPERF"
Reported-by: default avatarDoug Smythies <dsmythies@telus.net>
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent 520eccdf
...@@ -8,20 +8,25 @@ ...@@ -8,20 +8,25 @@
* This file is licensed under GPLv2. * This file is licensed under GPLv2.
*/ */
#include <linux/jiffies.h> #include <linux/delay.h>
#include <linux/ktime.h>
#include <linux/math64.h> #include <linux/math64.h>
#include <linux/percpu.h> #include <linux/percpu.h>
#include <linux/smp.h> #include <linux/smp.h>
struct aperfmperf_sample { struct aperfmperf_sample {
unsigned int khz; unsigned int khz;
unsigned long jiffies; ktime_t time;
u64 aperf; u64 aperf;
u64 mperf; u64 mperf;
}; };
static DEFINE_PER_CPU(struct aperfmperf_sample, samples); static DEFINE_PER_CPU(struct aperfmperf_sample, samples);
#define APERFMPERF_CACHE_THRESHOLD_MS 10
#define APERFMPERF_REFRESH_DELAY_MS 20
#define APERFMPERF_STALE_THRESHOLD_MS 1000
/* /*
* aperfmperf_snapshot_khz() * aperfmperf_snapshot_khz()
* On the current CPU, snapshot APERF, MPERF, and jiffies * On the current CPU, snapshot APERF, MPERF, and jiffies
...@@ -33,9 +38,11 @@ static void aperfmperf_snapshot_khz(void *dummy) ...@@ -33,9 +38,11 @@ static void aperfmperf_snapshot_khz(void *dummy)
u64 aperf, aperf_delta; u64 aperf, aperf_delta;
u64 mperf, mperf_delta; u64 mperf, mperf_delta;
struct aperfmperf_sample *s = this_cpu_ptr(&samples); struct aperfmperf_sample *s = this_cpu_ptr(&samples);
ktime_t now = ktime_get();
s64 time_delta = ktime_ms_delta(now, s->time);
/* Don't bother re-computing within 10 ms */ /* Don't bother re-computing within the cache threshold time. */
if (time_before(jiffies, s->jiffies + HZ/100)) if (time_delta < APERFMPERF_CACHE_THRESHOLD_MS)
return; return;
rdmsrl(MSR_IA32_APERF, aperf); rdmsrl(MSR_IA32_APERF, aperf);
...@@ -51,22 +58,21 @@ static void aperfmperf_snapshot_khz(void *dummy) ...@@ -51,22 +58,21 @@ static void aperfmperf_snapshot_khz(void *dummy)
if (mperf_delta == 0) if (mperf_delta == 0)
return; return;
/* s->time = now;
* if (cpu_khz * aperf_delta) fits into ULLONG_MAX, then
* khz = (cpu_khz * aperf_delta) / mperf_delta
*/
if (div64_u64(ULLONG_MAX, cpu_khz) > aperf_delta)
s->khz = div64_u64((cpu_khz * aperf_delta), mperf_delta);
else /* khz = aperf_delta / (mperf_delta / cpu_khz) */
s->khz = div64_u64(aperf_delta,
div64_u64(mperf_delta, cpu_khz));
s->jiffies = jiffies;
s->aperf = aperf; s->aperf = aperf;
s->mperf = mperf; s->mperf = mperf;
/* If the previous iteration was too long ago, discard it. */
if (time_delta > APERFMPERF_STALE_THRESHOLD_MS)
s->khz = 0;
else
s->khz = div64_u64((cpu_khz * aperf_delta), mperf_delta);
} }
unsigned int arch_freq_get_on_cpu(int cpu) unsigned int arch_freq_get_on_cpu(int cpu)
{ {
unsigned int khz;
if (!cpu_khz) if (!cpu_khz)
return 0; return 0;
...@@ -74,6 +80,12 @@ unsigned int arch_freq_get_on_cpu(int cpu) ...@@ -74,6 +80,12 @@ unsigned int arch_freq_get_on_cpu(int cpu)
return 0; return 0;
smp_call_function_single(cpu, aperfmperf_snapshot_khz, NULL, 1); smp_call_function_single(cpu, aperfmperf_snapshot_khz, NULL, 1);
khz = per_cpu(samples.khz, cpu);
if (khz)
return khz;
msleep(APERFMPERF_REFRESH_DELAY_MS);
smp_call_function_single(cpu, aperfmperf_snapshot_khz, NULL, 1);
return per_cpu(samples.khz, cpu); return per_cpu(samples.khz, cpu);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment