Skip to content

Commit

Permalink
x86: disable preemption in delay_tsc()
Browse files Browse the repository at this point in the history
Marin Mitov points out that delay_tsc() can misbehave if it is preempted and
rescheduled on a different CPU which has a skewed TSC.  Fix it by disabling
preemption.

(I assume that the worst-case behaviour here is a stall of 2^32 cycles)

Cc: Andi Kleen <ak@suse.de>
Cc: Marin Mitov <mitov@issp.bas.bg>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Andrew Morton authored and Linus Torvalds committed Nov 15, 2007
1 parent 7eea436 commit 35d5d08
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 3 additions & 0 deletions arch/x86/lib/delay_32.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/preempt.h>
#include <linux/delay.h>

#include <asm/processor.h>
Expand Down Expand Up @@ -42,11 +43,13 @@ static void delay_tsc(unsigned long loops)
{
unsigned long bclock, now;

preempt_disable(); /* TSC's are per-cpu */
rdtscl(bclock);
do {
rep_nop();
rdtscl(now);
} while ((now-bclock) < loops);
preempt_enable();
}

/*
Expand Down
11 changes: 7 additions & 4 deletions arch/x86/lib/delay_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/preempt.h>
#include <linux/delay.h>

#include <asm/delay.h>
#include <asm/msr.h>

Expand All @@ -27,14 +29,15 @@ int read_current_timer(unsigned long *timer_value)
void __delay(unsigned long loops)
{
unsigned bclock, now;


preempt_disable(); /* TSC's are pre-cpu */
rdtscl(bclock);
do
{
do {
rep_nop();
rdtscl(now);
}
while((now-bclock) < loops);
while ((now-bclock) < loops);
preempt_enable();
}
EXPORT_SYMBOL(__delay);

Expand Down

0 comments on commit 35d5d08

Please sign in to comment.