Skip to contentSkip to conversation

Warn of incorrect cpu_khz on AMD systems

PATCH Applied Prarit Bhargava 2 participants 5 messages Nov 4, 2008 – Nov 12, 2008 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Warn of incorrect cpu_khz on AMD systems

If none of the perfctrs are free when calculating cpu_khz we default to using ctr 3 (ie, we just choose 3). This may lead to an incorrect tsc freq value which can cause the system to be unstable.

To aid in future debugging, WARN the user of a potential problem.

arch/x86/kernel/time_64.c +2 -0
index cb19d65..86d71b3 100644
@@ -80,6 +80,8 @@ unsigned long __init calibrate_cpu(void)
 			break;
 	no_ctr_free = (i == 4);
 	if (no_ctr_free) {
+		printk(KERN_WARN "Warning: AMD perfctrs busy ... "
+		       "cpu_khz value may be incorrect.\n");
 		i = 3;
 		rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
 		wrmsrl(MSR_K7_EVNTSEL3, 0);
#1 lore raw
Quoted 5 lines · Prarit Bhargava

If none of the perfctrs are free when calculating cpu_khz we default to using ctr 3 (ie, we just choose 3). This may lead to an incorrect tsc freq value which can cause the system to be unstable.

To aid in future debugging, WARN the user of a potential problem.

oh, nasty... when can this happen - are you using nmi_watchdog=2?

Cannot we avoid this situation somehow? The calibrate_cpu() function is quite ugly and does a dangerous thing by ignoring the reservation.

This whole sequence is sloppy:

        for (i = 0; i < 4; i++)
                if (avail_to_resrv_perfctr_nmi_bit(i))
                        break;
        no_ctr_free = (i == 4);
        if (no_ctr_free) {
                i = 3;
                rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
                wrmsrl(MSR_K7_EVNTSEL3, 0);
                rdmsrl(MSR_K7_PERFCTR3, pmc3);
        } else {
                reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
                reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
        }

arch/x86/kernel/time_64.c unsigned long __init calibrate_cpu(void) · line 84

Quoted 4 lines
arch/x86/kernel/time_64.c +2 -0
 	no_ctr_free = (i == 4);
 	if (no_ctr_free) {
+		printk(KERN_WARN "Warning: AMD perfctrs busy ... "
+		       "cpu_khz value may be incorrect.\n");

also, please use a WARN() instead so that kerneloops.org picks it up.

Ingo

#2 lore raw
Quoted 4 lines

Cannot we avoid this situation somehow? The calibrate_cpu() function is quite ugly and does a dangerous thing by ignoring the reservation.

Yes, I noticed that too -- it's really the crux of the problem. If no_ctr_free is a last resort boot option. But I wonder if it should exist at all.

I was originally thinking the system should just stop booting if no_ctr_free and panic() ... and I'm willing to make that patch.

The likelihood of hitting this is low, below .0125% of the time. But when it does hit, it is nasty and difficult to diagnose. The last thing that I thought could be wrong was that cpu_khz was busted.

Quoted 17 lines

This whole sequence is sloppy:

        for (i = 0; i < 4; i++)
                if (avail_to_resrv_perfctr_nmi_bit(i))
                        break;
        no_ctr_free = (i == 4);
        if (no_ctr_free) {
                i = 3;
                rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
                wrmsrl(MSR_K7_EVNTSEL3, 0);
                rdmsrl(MSR_K7_PERFCTR3, pmc3);
        } else {
                reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
                reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
        }
Quoted 9 lines

arch/x86/kernel/time_64.c unsigned long __init calibrate_cpu(void) · line 84

Quoted 5 lines
arch/x86/kernel/time_64.c +2 -0
 	no_ctr_free = (i == 4);
 	if (no_ctr_free) {
+		printk(KERN_WARN "Warning: AMD perfctrs busy ... "
+		       "cpu_khz value may be incorrect.\n");

also, please use a WARN() instead so that kerneloops.org picks it up.

Will do -- but do you think a panic() is more appropriate?

P.

Quoted 2 lines

Ingo

#3 lore raw
In reply to Ingo Molnar

New patch replacing printk with WARN() based on Ingo's suggestion...

P.

If none of the perfctrs are free when calculating cpu_khz we default to using ctr 3 (ie, we just choose 3). This may lead to an incorrect tsc freq value which can cause the system to be unstable.

To aid in future debugging, WARN the user of a potential problem.

arch/x86/kernel/time_64.c +2 -0
index cb19d65..e71d1ba 100644
@@ -80,6 +80,8 @@ unsigned long __init calibrate_cpu(void)
 			break;
 	no_ctr_free = (i == 4);
 	if (no_ctr_free) {
+		WARN(1, KERN_WARN "Warning: AMD perfctrs busy ... "
+		     "cpu_khz value may be incorrect.\n");
 		i = 3;
 		rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
 		wrmsrl(MSR_K7_EVNTSEL3, 0);
Applied
#4 lore raw
Quoted 1 line · Prarit Bhargava

New patch replacing printk with WARN() based on Ingo's suggestion...

applied to tip/x86/debug, thanks Prarit!

Ingo