Platform SMIs and their interferance with tsc based delay calibration
Issue:
Current tsc based delay_calibration can result in significant errors in loops_per_jiffy count when the platform events like SMIs (System Management Interrupts that are non-maskable) are present. This could lead to potential kernel panic(). This issue is becoming more visible with 2.6 kernel (as default HZ is 1000) and on platforms with higher SMI handling latencies. During the boot time, SMIs are mostly used by BIOS (for things like legacy keyboard emulation).
Description:
The psuedocode for current delay calibration with tsc based delay looks like
(0) Estimate a value for loops_per_jiffy
(1) While (loops_per_jiffy estimate is accurate enough)
(2) wait for jiffy transition (jiffy1) (3) Note down current tsc (tsc1) (4) loop until tsc becomes tsc1 + loops_per_jiffy (5) check whether jiffy changed since jiffy1 or not and refine loops_per_jiffy estimate
Consider the following cases
Case 1:
If SMIs happen between (2) and (3) above, we can end up with a loops_per_jiffy value that is too low. This results in shorted delays and kernel can panic () during boot (Mostly at IOAPIC timer initialization timer_irq_works() as we don't have enough timer interrupts in a specified interval).
Case 2:
If SMIs happen between (3) and (4) above, then we can end up with a loops_per_jiffy value that is too high. And with current i386 code, too high lpj value (greater than 17M) can result in a overflow in delay.c:__const_udelay() again resulting in shorter delay and panic().
Solution:
The patch below makes the calibration routine aware of asynchronous events like SMIs. We increase the delay calibration time and also identify any significant errors (greater than 12.5%) in the calibration and notify it to user. Like to know your comments on this.
Thanks,
Venki
./arch/i386/kernel/timers/timer_tsc.c renamed from ./arch/i386/kernel/timers/timer_tsc.c.org +1 -0
@@ -552,6 +552,7 @@ static struct timer_opts timer_tsc = { .get_offset = get_offset_tsc, .monotonic_clock = monotonic_clock_tsc, .delay = delay_tsc, + .calibrate_delay = calibrate_delay_tsc, };struct init_timer_opts __initdata timer_tsc_init = {
./arch/i386/kernel/timers/common.c renamed from ./arch/i386/kernel/timers/common.c.org +46 -0
49 lines not on this page · raw message
./arch/i386/kernel/timers/timer_hpet.c renamed from ./arch/i386/kernel/timers/timer_hpet.c.org +1 -0
@@ -183,6 +183,7 @@ static struct timer_opts timer_hpet = { .get_offset = get_offset_hpet, .monotonic_clock = monotonic_clock_hpet, .delay = delay_hpet, + .calibrate_delay = calibrate_delay_tsc, };struct init_timer_opts __initdata timer_hpet_init = {
./arch/i386/kernel/timers/timer_pm.c renamed from ./arch/i386/kernel/timers/timer_pm.c.org +1 -0
@@ -246,6 +246,7 @@ static struct timer_opts timer_pmtmr = { .get_offset = get_offset_pmtmr, .monotonic_clock = monotonic_clock_pmtmr, .delay = delay_pmtmr, + .calibrate_delay = calibrate_delay_tsc, };struct init_timer_opts __initdata timer_pmtmr_init = {
./arch/i386/kernel/timers/calibrate.c renamed from ./arch/i386/kernel/timers/calibrate.c.org +96 -0
96 lines not on this page · raw message
./arch/i386/kernel/timers/Makefile renamed from ./arch/i386/kernel/timers/Makefile.org +1 -1
@@ -2,7 +2,7 @@ # Makefile for x86 timers # -obj-y := timer.o timer_none.o timer_tsc.o timer_pit.o common.o +obj-y :=calibrate.o timer.o timer_none.o timer_tsc.o timer_pit.o common.o obj-$(CONFIG_X86_CYCLONE_TIMER) += timer_cyclone.o obj-$(CONFIG_HPET_TIMER) += timer_hpet.o
./arch/i386/Kconfig renamed from ./arch/i386/Kconfig.org +0 -4
@@ -373,10 +373,6 @@ config RWSEM_XCHGADD_ALGORITHM depends on !M386 default y -config GENERIC_CALIBRATE_DELAY - bool - default y - config X86_PPRO_FENCE bool depends on M686 || M586MMX || M586TSC || M586 || M486 || M386
./include/asm-i386/timer.h renamed from ./include/asm-i386/timer.h.org +2 -0
@@ -22,6 +22,7 @@ struct timer_opts {unsigned long (*get_offset)(void );unsigned long long (*monotonic_clock)(void );void (*delay)(unsigned long ); +unsigned long (*calibrate_delay)(void ); };struct init_timer_opts {
@@ -52,6 +53,7 @@ extern struct init_timer_opts timer_cycl#endif extern unsigned long calibrate_tsc (void ); +extern unsigned long calibrate_delay_tsc (void );extern void init_cpu_khz (void );#ifdef CONFIG_HPET_TIMER extern struct init_timer_opts timer_hpet_init;
Please don't send emails which contain 500-column lines?
Quoted 4 lines · Venkatesh Pallipadi
Current tsc based
delay_calibrationcan result in significant errors inloops_per_jiffycount when the platform events like SMIs (System Management Interrupts that are non-maskable) are present.
This seems like an unsolveable problem.
Quoted 5 lines
Solution:
The patch below makes the calibration routine aware of asynchronous events
like SMIs. We increase the delay calibration time and also identify any significant errors (greater than 12.5%) in the calibration and notify it to user. Like to know your comments on this.
I find calibrate_delay_tsc() quite confusing. Are you sure that the variable names are correct?
+ tsc_rate_max = (post_end - pre_start) / DELAY_CALIBRATION_TICKS;
+ tsc_rate_min = (pre_end - post_start) / DELAY_CALIBRATION_TICKS;
that looks strange. I'm sure it all makes sense if one understands the algorithm, but it shouldn't be this hard. Please reissue the patch with adequate comments which describe what the code is doing.
Shouldn't calibrate_delay_tsc() be __devinit? (That may generate warnings from reference_discarded.pl, but they're false positives)
Quoted 1 line
From a maintainability POV it's not good that x86 is no longer using the
generic calibrate_delay() code. Can you rework the code so that all architectures must implement arch_calibrate_delay(), then provide stubs for all except x86? After all, other architectures/platforms may have the same problem.
./arch/i386/kernel/timers/common.c void __init init_cpu_khz(void) · line 202
Quoted 12 lines · Venkatesh Pallipadi
./arch/i386/kernel/timers/common.c renamed from ./arch/i386/kernel/timers/common.c.org +12
-0+ +/* +* If the upper limit and lower limit of the tsc_rate is more than +* 12.5% apart. +*/ +if (pre_start ==0 || pre_end ==0 || + (tsc_rate_max - tsc_rate_min) > (tsc_rate_max >>3 )) { +printk (KERN_WARNING "TSC calibration may not be precise. " +"Too many SMIs? " +"Consider running with \"lpj=\" boot option\n" ); +return 0 ; + }
I think it would be better to rerun it a few times automatically before giving up. This way it would hopefully work transparently but slower for most users. The message is too obscure too to be usable and needs more explanation.
And also in case the platforms in questions support EM64T x86-64 would need to be changed too :)
-Andi
Quoted 8 lines · Venkatesh Pallipadi
Current tsc based
delay_calibrationcan result in significant errors inloops_per_jiffycount when the platform events like SMIs (System Management Interrupts that are non-maskable) are present. This could lead to potential kernelpanic(). This issue is becoming more visible with 2.6 kernel (as default HZ is 1000) and on platforms with higher SMI handling latencies. During the boot time, SMIs are mostly used by BIOS (for things like legacy keyboard emulation).
Hmm. I see the problem, but I don't know that I'm 100% happy with this patch, though.
In particular, I don't see why you didn't just put this in the generic calibrate_delay() routine. You seem to have basically duplicated it, and added the "guess from an external timer" code - and I don't see what's non-generic about that, except for some trivial "what's the current timer" lookup.
Linus