Skip to contentSkip to conversation

Patch to fix race between the NMI code and the CMOS clock

PATCH Corey Minyard 2 participants 4 messages Jan 21, 2005 – Jan 24, 2005

lore
Share & tools
lore
#0 lore raw

This patch fixes a race between the CMOS clock setting and the NMI code. The NMI code indiscriminatly sets index registers and values in the same place the CMOS clock is set. If you are setting the CMOS clock and an NMI occurs, Bad values could be written to or read from the CMOS RAM, or the NMI operation might not occur correctly.

Fixing this requires creating a special lock so the NMI code can know its CPU owns the lock an "do the right thing" in that case.

This was discovered and the fix has been tested by a very demanding customer who tests the heck of out the software we deliver.

arch/i386/kernel/time.c +8 -0
Index: linux-2.6.11-rc1/arch/i386/kernel/time.c
===================================================================
@@ -83,6 +83,14 @@
 
 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
 
+/*
+ * This is a special lock that is owned by the CPU and holds the index
+ * register we are working with.  It is required for NMI access to the
+ * CMOS/RTC registers.  See include/asm-i386/mc146818rtc.h for details.
+ */
+volatile unsigned long cmos_lock = 0;
+EXPORT_SYMBOL(cmos_lock);
+
 spinlock_t i8253_lock = SPIN_LOCK_UNLOCKED;
 EXPORT_SYMBOL(i8253_lock);
 
include/asm-i386/mach-default/mach_traps.h +12 -0
Index: linux-2.6.11-rc1/include/asm-i386/mach-default/mach_traps.h
===================================================================
@@ -7,6 +7,8 @@
 #ifndef _MACH_TRAPS_H
 #define _MACH_TRAPS_H
 
+#include <asm/mc146818rtc.h>
+
 static inline void clear_mem_error(unsigned char reason)
 {
 	reason = (reason & 0xf) | 4;
@@ -20,10 +22,20 @@
 
 static inline void reassert_nmi(void)
 {
+	int old_reg = -1;
+
+	if (do_i_have_lock_cmos())
+		old_reg = current_lock_cmos_reg();
+	else
+		lock_cmos(0); /* register doesn't matter here */
 	outb(0x8f, 0x70);
 	inb(0x71);		/* dummy */
 	outb(0x0f, 0x70);
 	inb(0x71);		/* dummy */
+	if (old_reg >= 0)
+		outb(old_reg, 0x70);
+	else
+		unlock_cmos();
 }
 
 #endif /* !_MACH_TRAPS_H */
include/asm-i386/mc146818rtc.h +86 -8

110 lines not on this page · raw message

#1 lore raw
Quoted 10 lines · Corey Minyard

This patch fixes a race between the CMOS clock setting and the NMI
code. The NMI code indiscriminatly sets index registers and values in the same place the CMOS clock is set. If you are setting the CMOS clock and an NMI occurs, Bad values could be written to or read from the CMOS RAM, or the NMI operation might not occur correctly.

Fixing this requires creating a special lock so the NMI code can know its CPU owns the lock an "do the right thing" in that case.

hm, tricky patch. I can't see any holes in it. The volatile variable is awkward but should be OK on x86 and I can see the need for it.

There's a preposterous amount of inlining happening in this code. Hence your patch took the size of drivers/char/rtc.o from

text data bss dec hex filename
3657 540 8 4205 106d drivers/char/rtc.o
to
5419 540 8 5967 174f drivers/char/rtc.o

Do you think you could take a look at uninlining everything sometime?

#2 lore raw
Quoted 10 lines · Andrew Morton
Quoted 7 lines · Corey Minyard

This patch fixes a race between the CMOS clock setting and the NMI code. The NMI code indiscriminatly sets index registers and values in the same place the CMOS clock is set. If you are setting the CMOS clock and an NMI occurs, Bad values could be written to or read from the CMOS RAM, or the NMI operation might not occur correctly.

Quoted 9 lines
Quoted 4 lines

Fixing this requires creating a special lock so the NMI code can know its CPU owns the lock an "do the right thing" in that case.

hm, tricky patch. I can't see any holes in it. The volatile variable is awkward but should be OK on x86 and I can see the need for it.

It took some thought and I couldn't think of anything simpler. I posted in on lkml a little while ago, and some people tried but nobody else found a simpler solution that worked with SMP.

Quoted 11 lines

There's a preposterous amount of inlining happening in this code. Hence your patch took the size of drivers/char/rtc.o from

text data bss dec hex filename
3657 540 8 4205 106d drivers/char/rtc.o
to
5419 540 8 5967 174f drivers/char/rtc.o

Do you think you could take a look at uninlining everything sometime?

Certainly. I'll try to have it sometime today.

Thanks,

-Corey

#3 lore raw
In reply to Andrew Morton
Quoted 21 lines · Andrew Morton
Quoted 11 lines · Corey Minyard

This patch fixes a race between the CMOS clock setting and the NMI code. The NMI code indiscriminatly sets index registers and values in the same place the CMOS clock is set. If you are setting the CMOS clock and an NMI occurs, Bad values could be written to or read from the CMOS RAM, or the NMI operation might not occur correctly.

Fixing this requires creating a special lock so the NMI code can know its CPU owns the lock an "do the right thing" in that case.

hm, tricky patch. I can't see any holes in it. The volatile variable is awkward but should be OK on x86 and I can see the need for it.

There's a preposterous amount of inlining happening in this code. Hence your patch took the size of drivers/char/rtc.o from

Quoted 8 lines

text data bss dec hex filename
3657 540 8 4205 106d drivers/char/rtc.o
to
5419 540 8 5967 174f drivers/char/rtc.o

Do you think you could take a look at uninlining everything sometime?

Ok, done. With the attached patch I get ~2300 bytes less text in that file. It makes CMOS_READ and CMOS_WRITE functions. They were used all over the place in drivers/char/rtc.o, and the extra locking stuff took a lot of space. I also fixed some comment grammer and I discovered that linux/smp.h needed to be included, since smp_processor_id() was being called.

-Corey

This patch fixes a race between the CMOS clock setting and the NMI code. The NMI code indiscriminatly sets index registers and values in the same place the CMOS clock is set. If you are setting the CMOS clock and an NMI occurs, Bad values could be written to or read from the CMOS RAM, or the NMI operation might not occur correctly.

Fixing this requires creating a special lock so the NMI code can know its CPU owns the lock an "do the right thing" in that case.

This was discovered and the fix has been tested by a very demanding customer who tests the heck of out the software we deliver.

arch/i386/kernel/time.c +28 -0
Index: linux-2.6.11-rc1/arch/i386/kernel/time.c
===================================================================
@@ -83,6 +83,34 @@
 
 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
 
+/*
+ * This is a special lock that is owned by the CPU and holds the index
+ * register we are working with.  It is required for NMI access to the
+ * CMOS/RTC registers.  See include/asm-i386/mc146818rtc.h for details.
+ */
+volatile unsigned long cmos_lock = 0;
+EXPORT_SYMBOL(cmos_lock);
+
+/* Routines for accessing the CMOS RAM/RTC. */
+unsigned char rtc_cmos_read(unsigned char addr)
+{
+	unsigned char val;
+	lock_cmos_prefix(addr);
+	outb_p(addr, RTC_PORT(0));
+	val = inb_p(RTC_PORT(1));
+	lock_cmos_suffix(addr);
+	return val;
+}
+EXPORT_SYMBOL(rtc_cmos_read);
+void rtc_cmos_write(unsigned char val, unsigned char addr)
+{
+	lock_cmos_prefix(addr);
+	outb_p(addr, RTC_PORT(0));
+	outb_p(val, RTC_PORT(1));
+	lock_cmos_suffix(addr);
+}
+EXPORT_SYMBOL(rtc_cmos_write);
+
 spinlock_t i8253_lock = SPIN_LOCK_UNLOCKED;
 EXPORT_SYMBOL(i8253_lock);
 
include/asm-i386/mach-default/mach_traps.h +12 -0
Index: linux-2.6.11-rc1/include/asm-i386/mach-default/mach_traps.h
===================================================================
@@ -7,6 +7,8 @@
 #ifndef _MACH_TRAPS_H
 #define _MACH_TRAPS_H
 
+#include <asm/mc146818rtc.h>
+
 static inline void clear_mem_error(unsigned char reason)
 {
 	reason = (reason & 0xf) | 4;
@@ -20,10 +22,20 @@
 
 static inline void reassert_nmi(void)
 {
+	int old_reg = -1;
+
+	if (do_i_have_lock_cmos())
+		old_reg = current_lock_cmos_reg();
+	else
+		lock_cmos(0); /* register doesn't matter here */
 	outb(0x8f, 0x70);
 	inb(0x71);		/* dummy */
 	outb(0x0f, 0x70);
 	inb(0x71);		/* dummy */
+	if (old_reg >= 0)
+		outb(old_reg, 0x70);
+	else
+		unlock_cmos();
 }
 
 #endif /* !_MACH_TRAPS_H */
include/asm-i386/mc146818rtc.h +73 -8

97 lines not on this page · raw message