Skip to contentSkip to conversation

x86/aperfmperf: Don't disable scheduler APERF/MPERF on bad samples

PATCH New Andi Kleen 1 participant 1 message Mar 3 x86

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH v3 1/1 x86/aperfmperf: Don't disable scheduler APERF/MPERF on bad samples

The APERF and MPERF MSRs get read together and the ratio between the two is used to scale the scheduler capacity with frequency.

Since e2b0d619b400 when there is ever an over/underflow of the APERF/MPERF computation the sampling gets completely disabled, under the assumption that there is a problem with the hardware.

However this can happen without any malfunction when there is a long enough interruption between the two MSR reads, for example due to an unlucky NMI or SMI or other system event causing delays. We saw it when a delay resulted in Acnt_Delta << Mcnt_Delta (about ~4k for acnt_delta and 2M for MCnt_Delta).

In this case the ratio computation underflows, which is detected, but then APERF/MPERF usage gets incorrectly disabled forever.

Remove the code to completely disable APERF/MPERF on a bad sample. Instead when any over/underflow happens return the fallback full capacity.

In theory could have a threshold to disable, but since delays could happen randomly it's unclear what a good threshold would be. If the hardware is truly broken this will result in using a few more cycles to read the bogus samples, but they will be all still rejected.

Cc Peter Zijlstra <[email protected]>
Fixes e2b0d619b400 ("x86, sched: check for counters overflow ...")

---

v2: Move freq_scale initialization to cover all cases (thanks 0day bot!)
v3: Rebased/reposted.

---
 arch/x86/kernel/cpu/aperfmperf.c | 35 +++++++++-----------------------
 1 file changed, 10 insertions(+), 25 deletions(-)
arch/x86/kernel/cpu/aperfmperf.c +10 -25
index 7ffc78d5ebf2..99ebbda53d10 100644
@@ -334,23 +334,6 @@ static void __init bp_init_freq_invariance(void)
 	}
 }
 
-static void disable_freq_invariance_workfn(struct work_struct *work)
-{
-	int cpu;
-
-	static_branch_disable(&arch_scale_freq_key);
-
-	/*
-	 * Set arch_freq_scale to a default value on all cpus
-	 * This negates the effect of scaling
-	 */
-	for_each_possible_cpu(cpu)
-		per_cpu(arch_freq_scale, cpu) = SCHED_CAPACITY_SCALE;
-}
-
-static DECLARE_WORK(disable_freq_invariance_work,
-		    disable_freq_invariance_workfn);
-
 DEFINE_PER_CPU(unsigned long, arch_freq_scale) = SCHED_CAPACITY_SCALE;
 EXPORT_PER_CPU_SYMBOL_GPL(arch_freq_scale);
 
@@ -441,8 +424,14 @@ static void scale_freq_tick(u64 acnt, u64 mcnt)
 	if (!arch_scale_freq_invariant())
 		return;
 
+	/*
+	 * On any over/underflow just ignore the sample. It could
+	 * be due to an unlucky NMI or similar between the
+	 * APERF and MPERF reads.
+	 */
+	freq_scale = SCHED_CAPACITY_SCALE;
 	if (check_shl_overflow(acnt, 2*SCHED_CAPACITY_SHIFT, &acnt))
-		goto error;
+		goto out;
 
 	if (static_branch_unlikely(&arch_hybrid_cap_scale_key))
 		freq_ratio = READ_ONCE(this_cpu_ptr(arch_cpu_scale)->freq_ratio);
@@ -450,21 +439,17 @@ static void scale_freq_tick(u64 acnt, u64 mcnt)
 		freq_ratio = arch_max_freq_ratio;
 
 	if (check_mul_overflow(mcnt, freq_ratio, &mcnt) || !mcnt)
-		goto error;
+		goto out;
 
 	freq_scale = div64_u64(acnt, mcnt);
 	if (!freq_scale)
-		goto error;
+		goto out;
 
 	if (freq_scale > SCHED_CAPACITY_SCALE)
 		freq_scale = SCHED_CAPACITY_SCALE;
 
+out:
 	this_cpu_write(arch_freq_scale, freq_scale);
-	return;
-
-error:
-	pr_warn("Scheduler frequency invariance went wobbly, disabling!\n");
-	schedule_work(&disable_freq_invariance_work);
 }
 #else
 static inline void bp_init_freq_invariance(void) { }
Signature · 1 line
2.53.0