x86: Stop MCEs and NMIs during code patching

When a machine check or NMI occurs while multiple byte code is patched
the CPU could theoretically see an inconsistent instruction and crash.
Prevent this by temporarily disabling MCEs and returning early in the
NMI handler.

Based on discussion with Mathieu Desnoyers.

Cc: Mathieu Desnoyers <compudj@krystal.dyndns.org>
Cc: Jeremy Fitzhardinge <jeremy@goop.org>
Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Andi Kleen 2007-07-22 11:12:32 +02:00 committed by Linus Torvalds
parent 19d36ccdc3
commit 8f4e956b31
9 changed files with 86 additions and 2 deletions

View File

@ -8,6 +8,8 @@
#include <asm/alternative.h> #include <asm/alternative.h>
#include <asm/sections.h> #include <asm/sections.h>
#include <asm/pgtable.h> #include <asm/pgtable.h>
#include <asm/mce.h>
#include <asm/nmi.h>
#ifdef CONFIG_HOTPLUG_CPU #ifdef CONFIG_HOTPLUG_CPU
static int smp_alt_once; static int smp_alt_once;
@ -373,6 +375,14 @@ void __init alternative_instructions(void)
{ {
unsigned long flags; unsigned long flags;
/* The patching is not fully atomic, so try to avoid local interruptions
that might execute the to be patched code.
Other CPUs are not running. */
stop_nmi();
#ifdef CONFIG_MCE
stop_mce();
#endif
local_irq_save(flags); local_irq_save(flags);
apply_alternatives(__alt_instructions, __alt_instructions_end); apply_alternatives(__alt_instructions, __alt_instructions_end);
@ -405,6 +415,11 @@ void __init alternative_instructions(void)
#endif #endif
apply_paravirt(__parainstructions, __parainstructions_end); apply_paravirt(__parainstructions, __parainstructions_end);
local_irq_restore(flags); local_irq_restore(flags);
restart_nmi();
#ifdef CONFIG_MCE
restart_mce();
#endif
} }
/* /*

View File

@ -60,6 +60,20 @@ void mcheck_init(struct cpuinfo_x86 *c)
} }
} }
static unsigned long old_cr4 __initdata;
void __init stop_mce(void)
{
old_cr4 = read_cr4();
clear_in_cr4(X86_CR4_MCE);
}
void __init restart_mce(void)
{
if (old_cr4 & X86_CR4_MCE)
set_in_cr4(X86_CR4_MCE);
}
static int __init mcheck_disable(char *str) static int __init mcheck_disable(char *str)
{ {
mce_disabled = 1; mce_disabled = 1;

View File

@ -775,6 +775,8 @@ static __kprobes void default_do_nmi(struct pt_regs * regs)
reassert_nmi(); reassert_nmi();
} }
static int ignore_nmis;
fastcall __kprobes void do_nmi(struct pt_regs * regs, long error_code) fastcall __kprobes void do_nmi(struct pt_regs * regs, long error_code)
{ {
int cpu; int cpu;
@ -785,11 +787,24 @@ fastcall __kprobes void do_nmi(struct pt_regs * regs, long error_code)
++nmi_count(cpu); ++nmi_count(cpu);
default_do_nmi(regs); if (!ignore_nmis)
default_do_nmi(regs);
nmi_exit(); nmi_exit();
} }
void stop_nmi(void)
{
acpi_nmi_disable();
ignore_nmis++;
}
void restart_nmi(void)
{
ignore_nmis--;
acpi_nmi_enable();
}
#ifdef CONFIG_KPROBES #ifdef CONFIG_KPROBES
fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code) fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
{ {

View File

@ -667,6 +667,20 @@ static struct miscdevice mce_log_device = {
&mce_chrdev_ops, &mce_chrdev_ops,
}; };
static unsigned long old_cr4 __initdata;
void __init stop_mce(void)
{
old_cr4 = read_cr4();
clear_in_cr4(X86_CR4_MCE);
}
void __init restart_mce(void)
{
if (old_cr4 & X86_CR4_MCE)
set_in_cr4(X86_CR4_MCE);
}
/* /*
* Old style boot options parsing. Only for compatibility. * Old style boot options parsing. Only for compatibility.
*/ */

View File

@ -384,11 +384,14 @@ int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
return rc; return rc;
} }
static unsigned ignore_nmis;
asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code) asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
{ {
nmi_enter(); nmi_enter();
add_pda(__nmi_count,1); add_pda(__nmi_count,1);
default_do_nmi(regs); if (!ignore_nmis)
default_do_nmi(regs);
nmi_exit(); nmi_exit();
} }
@ -401,6 +404,18 @@ int do_nmi_callback(struct pt_regs * regs, int cpu)
return 0; return 0;
} }
void stop_nmi(void)
{
acpi_nmi_disable();
ignore_nmis++;
}
void restart_nmi(void)
{
ignore_nmis--;
acpi_nmi_enable();
}
#ifdef CONFIG_SYSCTL #ifdef CONFIG_SYSCTL
static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu) static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)

View File

@ -5,3 +5,7 @@ extern void mcheck_init(struct cpuinfo_x86 *c);
#endif #endif
extern int mce_disabled; extern int mce_disabled;
extern void stop_mce(void);
extern void restart_mce(void);

View File

@ -57,5 +57,7 @@ unsigned lapic_adjust_nmi_hz(unsigned hz);
int lapic_watchdog_ok(void); int lapic_watchdog_ok(void);
void disable_lapic_nmi_watchdog(void); void disable_lapic_nmi_watchdog(void);
void enable_lapic_nmi_watchdog(void); void enable_lapic_nmi_watchdog(void);
void stop_nmi(void);
void restart_nmi(void);
#endif /* ASM_NMI_H */ #endif /* ASM_NMI_H */

View File

@ -107,6 +107,9 @@ extern void do_machine_check(struct pt_regs *, long);
extern int mce_notify_user(void); extern int mce_notify_user(void);
extern void stop_mce(void);
extern void restart_mce(void);
#endif #endif
#endif #endif

View File

@ -88,5 +88,7 @@ unsigned lapic_adjust_nmi_hz(unsigned hz);
int lapic_watchdog_ok(void); int lapic_watchdog_ok(void);
void disable_lapic_nmi_watchdog(void); void disable_lapic_nmi_watchdog(void);
void enable_lapic_nmi_watchdog(void); void enable_lapic_nmi_watchdog(void);
void stop_nmi(void);
void restart_nmi(void);
#endif /* ASM_NMI_H */ #endif /* ASM_NMI_H */