aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/x86/kernel/nmi.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
index a15a8880066..f84f5c57de3 100644
--- a/arch/x86/kernel/nmi.c
+++ b/arch/x86/kernel/nmi.c
@@ -395,6 +395,14 @@ static __kprobes void default_do_nmi(struct pt_regs *regs)
395 * thus there is no race between the first check of state for NOT_RUNNING 395 * thus there is no race between the first check of state for NOT_RUNNING
396 * and setting it to NMI_EXECUTING. The HW will prevent nested NMIs 396 * and setting it to NMI_EXECUTING. The HW will prevent nested NMIs
397 * at this point. 397 * at this point.
398 *
399 * In case the NMI takes a page fault, we need to save off the CR2
400 * because the NMI could have preempted another page fault and corrupt
401 * the CR2 that is about to be read. As nested NMIs must be restarted
402 * and they can not take breakpoints or page faults, the update of the
403 * CR2 must be done before converting the nmi state back to NOT_RUNNING.
404 * Otherwise, there would be a race of another nested NMI coming in
405 * after setting state to NOT_RUNNING but before updating the nmi_cr2.
398 */ 406 */
399enum nmi_states { 407enum nmi_states {
400 NMI_NOT_RUNNING = 0, 408 NMI_NOT_RUNNING = 0,
@@ -402,6 +410,7 @@ enum nmi_states {
402 NMI_LATCHED, 410 NMI_LATCHED,
403}; 411};
404static DEFINE_PER_CPU(enum nmi_states, nmi_state); 412static DEFINE_PER_CPU(enum nmi_states, nmi_state);
413static DEFINE_PER_CPU(unsigned long, nmi_cr2);
405 414
406#define nmi_nesting_preprocess(regs) \ 415#define nmi_nesting_preprocess(regs) \
407 do { \ 416 do { \
@@ -410,11 +419,14 @@ static DEFINE_PER_CPU(enum nmi_states, nmi_state);
410 return; \ 419 return; \
411 } \ 420 } \
412 this_cpu_write(nmi_state, NMI_EXECUTING); \ 421 this_cpu_write(nmi_state, NMI_EXECUTING); \
422 this_cpu_write(nmi_cr2, read_cr2()); \
413 } while (0); \ 423 } while (0); \
414 nmi_restart: 424 nmi_restart:
415 425
416#define nmi_nesting_postprocess() \ 426#define nmi_nesting_postprocess() \
417 do { \ 427 do { \
428 if (unlikely(this_cpu_read(nmi_cr2) != read_cr2())) \
429 write_cr2(this_cpu_read(nmi_cr2)); \
418 if (this_cpu_dec_return(nmi_state)) \ 430 if (this_cpu_dec_return(nmi_state)) \
419 goto nmi_restart; \ 431 goto nmi_restart; \
420 } while (0) 432 } while (0)