Skip to content

Commit b29c6ef

Browse files
rafaeljwtorvalds
authored andcommitted
x86 / CPU: Avoid unnecessary IPIs in arch_freq_get_on_cpu()
Even though aperfmperf_snapshot_khz() caches the samples.khz value to return if called again in a sufficiently short time, its caller, arch_freq_get_on_cpu(), still uses smp_call_function_single() to run it which may allow user space to trigger an IPI storm by reading from the scaling_cur_freq cpufreq sysfs file in a tight loop. To avoid that, move the decision on whether or not to return the cached samples.khz value to arch_freq_get_on_cpu(). This change was part of commit 941f5f0 ("x86: CPU: Fix up "cpu MHz" in /proc/cpuinfo"), but it was not the reason for the revert and it remains applicable. Fixes: 4815d3c (cpufreq: x86: Make scaling_cur_freq behave more as expected) Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Reviewed-by: WANG Chao <chao.wang@ucloud.cn> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 99306df commit b29c6ef

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

arch/x86/kernel/cpu/aperfmperf.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ static void aperfmperf_snapshot_khz(void *dummy)
4242
s64 time_delta = ktime_ms_delta(now, s->time);
4343
unsigned long flags;
4444

45-
/* Don't bother re-computing within the cache threshold time. */
46-
if (time_delta < APERFMPERF_CACHE_THRESHOLD_MS)
47-
return;
48-
4945
local_irq_save(flags);
5046
rdmsrl(MSR_IA32_APERF, aperf);
5147
rdmsrl(MSR_IA32_MPERF, mperf);
@@ -74,6 +70,7 @@ static void aperfmperf_snapshot_khz(void *dummy)
7470

7571
unsigned int arch_freq_get_on_cpu(int cpu)
7672
{
73+
s64 time_delta;
7774
unsigned int khz;
7875

7976
if (!cpu_khz)
@@ -82,6 +79,12 @@ unsigned int arch_freq_get_on_cpu(int cpu)
8279
if (!static_cpu_has(X86_FEATURE_APERFMPERF))
8380
return 0;
8481

82+
/* Don't bother re-computing within the cache threshold time. */
83+
time_delta = ktime_ms_delta(ktime_get(), per_cpu(samples.time, cpu));
84+
khz = per_cpu(samples.khz, cpu);
85+
if (khz && time_delta < APERFMPERF_CACHE_THRESHOLD_MS)
86+
return khz;
87+
8588
smp_call_function_single(cpu, aperfmperf_snapshot_khz, NULL, 1);
8689
khz = per_cpu(samples.khz, cpu);
8790
if (khz)

0 commit comments

Comments
 (0)