Skip to contentSkip to conversation

#MC mess

RFC In review Borislav Petkov 7 participants 28 messages Feb 18, 2020 – Feb 19, 2020

Subsystems (6)
x86 PowerPC arm64 Scheduler +2 more subsystems
lore
Share & tools
lore

Borislav Petkov

author maintainer
#0 lore raw

RFC 1/1 #MC mess

Ok,

so Peter raised this question on IRC today, that the #MC handler needs to disable all kinds of tracing/kprobing and etc exceptions happening while handling an #MC. And I guess we can talk about supporting some exceptions but #MC is usually nasty enough to not care about tracing when former happens.

So how about this trivial first stab of using the big hammer and simply turning off stuff? The nmi_enter()/nmi_exit() thing still needs debating because ist_enter() already does rcu_nmi_enter() and I'm not sure whether any of the context tracking would still be ok with that.

Anything else I'm missing? It is likely...

Thx.

---

arch/x86/kernel/cpu/mce/core.c +6 -1
index 2c4f949611e4..6dff97c53310 100644
@@ -1214,7 +1214,7 @@ static void __mc_scan_banks(struct mce *m, struct mce *final,
  * MCE broadcast. However some CPUs might be broken beyond repair,
  * so be always careful when synchronizing with others.
  */
-void do_machine_check(struct pt_regs *regs, long error_code)
+void notrace do_machine_check(struct pt_regs *regs, long error_code)
 {
 	DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
 	DECLARE_BITMAP(toclear, MAX_NR_BANKS);
@@ -1251,6 +1251,10 @@ void do_machine_check(struct pt_regs *regs, long error_code)
 	if (__mc_check_crashing_cpu(cpu))
 		return;
 
+	hw_breakpoint_disable();
+	static_key_disable(&__tracepoint_read_msr.key);
+	tracing_off();
+
 	ist_enter(regs);
 
 	this_cpu_inc(mce_exception_count);
@@ -1360,6 +1364,7 @@ void do_machine_check(struct pt_regs *regs, long error_code)
 	ist_exit(regs);
 }
 EXPORT_SYMBOL_GPL(do_machine_check);
+NOKPROBE_SYMBOL(do_machine_check);
 
 #ifndef CONFIG_MEMORY_FAILURE
 int memory_failure(unsigned long pfn, int flags)
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#1 lore raw
Quoted 7 lines · On Tue, 18 Feb 2020 18:31:50 +0100 Borislav Petkov <[email protected]> wrote:

Ok,

so Peter raised this question on IRC today, that the #MC handler needs to disable all kinds of tracing/kprobing and etc exceptions happening while handling an #MC. And I guess we can talk about supporting some exceptions but #MC is usually nasty enough to not care about tracing when former happens.

What's the issue with tracing? Does this affect the tracing done by the edac_mc_handle_error code?

It has a trace event in it, that the rasdaemon uses.

Quoted 25 lines

So how about this trivial first stab of using the big hammer and simply turning off stuff? The nmi_enter()/nmi_exit() thing still needs debating because ist_enter() already does rcu_nmi_enter() and I'm not sure whether any of the context tracking would still be ok with that.

Anything else I'm missing? It is likely...

Thx.

---

arch/x86/kernel/cpu/mce/core.c +1 -1
index 2c4f949611e4..6dff97c53310 100644
@@ -1214,7 +1214,7 @@ static void __mc_scan_banks(struct mce *m, struct mce *final,
  * MCE broadcast. However some CPUs might be broken beyond repair,
  * so be always careful when synchronizing with others.
  */
-void do_machine_check(struct pt_regs *regs, long error_code)
+void notrace do_machine_check(struct pt_regs *regs, long error_code)
 {
 	DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
 	DECLARE_BITMAP(toclear, MAX_NR_BANKS);

arch/x86/kernel/cpu/mce/core.c void do_machine_check(struct pt_regs *regs, long error_code) · line 1255

Quoted 5 lines
arch/x86/kernel/cpu/mce/core.c +2 -0
@@ -1251,6 +1251,10 @@ void do_machine_check(struct pt_regs *regs, long error_code)
 	if (__mc_check_crashing_cpu(cpu))
 		return;
 
+	hw_breakpoint_disable();
+	static_key_disable(&__tracepoint_read_msr.key);

I believe static_key_disable() sleeps, and does all kinds of crazing things (like update the code).

-- Steve

Quoted 14 lines
arch/x86/kernel/cpu/mce/core.c +3 -0
+	tracing_off();
+
 	ist_enter(regs);
 
 	this_cpu_inc(mce_exception_count);
@@ -1360,6 +1364,7 @@ void do_machine_check(struct pt_regs *regs, long error_code)
 	ist_exit(regs);
 }
 EXPORT_SYMBOL_GPL(do_machine_check);
+NOKPROBE_SYMBOL(do_machine_check);
 
 #ifndef CONFIG_MEMORY_FAILURE
 int memory_failure(unsigned long pfn, int flags)
author maintainer
#2 lore raw
Quoted 4 lines · Steven Rostedt

What's the issue with tracing? Does this affect the tracing done by the edac_mc_handle_error code?

It has a trace event in it, that the rasdaemon uses.

Nah, that code is called from process context.

The problem with tracing the #MC handler is the same as tracing the NMI handler. And the NMI handler does all kinds of dancing wrt breakpoints and nested NMIs and the #MC handler doesn't do any of that. Not sure if it should at all, btw.

Quoted 2 lines

I believe static_key_disable() sleeps, and does all kinds of crazing things (like update the code).

True story, thanks for that hint!

static_key_disable()

|-> cpus_read_lock()
|-> percpu_down_read(&cpu_hotplug_lock)
|->might_sleep()

Yuck. Which means, the #MC handler must switch to __rdmsr()/__wrmsr() now.

I wish I could travel back in time and NAK the hell of that MSR tracepoint crap.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#3 lore raw
Quoted 7 lines · On Tue, 18 Feb 2020 20:50:35 +0100 Borislav Petkov <[email protected]> wrote:

True story, thanks for that hint!

static_key_disable()

|-> cpus_read_lock()
|-> percpu_down_read(&cpu_hotplug_lock)
|->might_sleep()
Quoted 5 lines

Yuck. Which means, the #MC handler must switch to __rdmsr()/__wrmsr() now.

I wish I could travel back in time and NAK the hell of that MSR tracepoint crap.

Can we create a per_cpu variable that gets set when we enter the MC handler, and not call the msr trace points when that is set?

Now, is jump labels bad in these cases (note, it is possible to trigger a breakpoint in them, does an iret disable the MC as well, which means we could get nested MC handlers corrupting the IST stack?).

You could have the msr_tracepoint_active() check this per cpu variable?

msr reading and writing is rather slow, and I'm sure reading a per_cpu variable is going to be in the noise of it.

-- Steve

maintainer
#4 lore raw
Quoted 3 lines · Steven Rostedt

Now, is jump labels bad in these cases (note, it is possible to trigger a breakpoint in them, does an iret disable the MC as well, which means we could get nested MC handlers corrupting the IST stack?).

That is what I expect, and would be consistent with the whole NMI situation.

author maintainer
#5 lore raw
In reply to Steven Rostedt
Quoted 4 lines · Steven Rostedt

You could have the msr_tracepoint_active() check this per cpu variable?

msr reading and writing is rather slow, and I'm sure reading a per_cpu variable is going to be in the noise of it.

Yeah, I was worrying about using the tracing MSR variants in NMI context but Peter says tracing should do in_nmi() if it isn't doing so.

Same with #MC: we hold a subsequent #MC from getting raised with MCIP - thanks Tony - but we can have other exceptions raised while in the #MC handler. That too should be taken care of with the in_nmi() thing.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#6 lore raw
In reply to Borislav Petkov
Quoted 1 line

Anything else I'm missing? It is likely...

+ hw_breakpoint_disable();
+ static_key_disable(&__tracepoint_read_msr.key);
+ tracing_off();
+
ist_enter(regs);

How about some code to turn all those back on for a recoverable (where we actually recovered) #MC?

-Tony

author maintainer
#7 lore raw
Quoted 10 lines · Tony
Quoted 1 line

Anything else I'm missing? It is likely...

arch/x86/kernel/cpu/mce/core.c +4 -0
+	hw_breakpoint_disable();
+	static_key_disable(&__tracepoint_read_msr.key);
+	tracing_off();
+
 	ist_enter(regs);

How about some code to turn all those back on for a recoverable (where we actually recovered) #MC?

The use case being?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#8 lore raw
Quoted 4 lines
Quoted 2 lines

How about some code to turn all those back on for a recoverable (where we actually recovered) #MC?

The use case being?

Recoverable machine checks are a normal (hopefully rare) event on a server. But you wouldn't want to lose tracing capability just because we took a page offline and killed a process to recover.

-Tony

author maintainer
#9 lore raw
Quoted 2 lines · Tony

Recoverable machine checks are a normal (hopefully rare) event on a server. But you wouldn't want to lose tracing capability just because we took a page offline and killed a process to recover.

Yeah, ok. How do you want to select which ones? What mce_no_way_out() says or severity or...?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#10 lore raw
Quoted 2 lines

Yeah, ok. How do you want to select which ones? What mce_no_way_out() says or severity or...?

We only return from do_machine_check() in the recoverable case. So down at the end just here:

out_ist:

	ist_exit(regs);
}

Though that does include cases where we returned just because cfg->tolerant was set to some value saying ignore this #MC.

-Tony

maintainer
#11 lore raw
In reply to Luck, Tony
Quoted 9 lines · Tony
Quoted 1 line

Anything else I'm missing? It is likely...

arch/x86/kernel/cpu/mce/core.c +4 -0
+	hw_breakpoint_disable();
+	static_key_disable(&__tracepoint_read_msr.key);
+	tracing_off();
+
 	ist_enter(regs);

How about some code to turn all those back on for a recoverable (where we actually recovered) #MC?

Then please rewrite the #MC entry code to deal with nested exceptions unmasking the MCE, very similr to NMI.

The moment you allow tracing, jump_labels or anything else you can expect #PF, #BP and probably #DB while inside #MC, those will then IRET and re-enable the #MC.

The current situation is completely and utterly buggered.

author maintainer
#12 lore raw
Quoted 6 lines · Peter Zijlstra

Then please rewrite the #MC entry code to deal with nested exceptions unmasking the MCE, very similr to NMI.

The moment you allow tracing, jump_labels or anything else you can expect #PF, #BP and probably #DB while inside #MC, those will then IRET and re-enable the #MC.

Yeah, I'd like to keep it simple and reenable all the crap we disabled only on exit from the handler.

Dunno if we care about losing tracing samples when an MCE happened...

Quoted 1 line

The current situation is completely and utterly buggered.

Lovely. ;-(

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#13 lore raw
In reply to Peter Zijlstra
Quoted 2 lines

Then please rewrite the #MC entry code to deal with nested exceptions unmasking the MCE, very similr to NMI.

#MC doesn't work like NMI. It isn't enabled by IRET. Nested #MC cause an immediate reset. Detection of nested case is by IA32_MCG_STATUS.MCIP. We don't clear MCG_STATUS until we are ready to return from the machine check handler.

-Tony

maintainer
#14 lore raw
Quoted 7 lines · Tony
Quoted 2 lines

Then please rewrite the #MC entry code to deal with nested exceptions unmasking the MCE, very similr to NMI.

#MC doesn't work like NMI. It isn't enabled by IRET. Nested #MC cause an immediate reset. Detection of nested case is by IA32_MCG_STATUS.MCIP. We don't clear MCG_STATUS until we are ready to return from the machine check handler.

Ooh, excellent! That saves a whole heap of problems.

Then I suppose we should work at getting in_nmi() set. Let me have another look at things.

maintainer
#15 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Tony
Quoted 2 lines

Then please rewrite the #MC entry code to deal with nested exceptions unmasking the MCE, very similr to NMI.

#MC doesn't work like NMI. It isn't enabled by IRET. Nested #MC cause an immediate reset. Detection of nested case is by IA32_MCG_STATUS.MCIP. We don't clear MCG_STATUS until we are ready to return from the machine check handler.

Quoted 4 lines

Ooh, excellent! That saves a whole heap of problems.

Then I suppose we should work at getting in_nmi() set. Let me have another look at things.

How's something like the -- completely untested -- below?

---
 arch/arm64/kernel/sdei.c          | 14 +-----
 arch/arm64/kernel/traps.c         |  8 +---
 arch/powerpc/kernel/traps.c       | 19 +++-----
 arch/x86/include/asm/traps.h      |  5 ---
 arch/x86/kernel/cpu/mce/core.c    | 60 ++++++++++++++-----------
 arch/x86/kernel/cpu/mce/p5.c      |  7 +--
 arch/x86/kernel/cpu/mce/winchip.c |  7 +--
 arch/x86/kernel/traps.c           | 94 ++++-----------------------------------
 include/linux/hardirq.h           |  2 +-
 include/linux/preempt.h           |  4 +-
 include/linux/sched.h             |  6 +++
 kernel/printk/printk_safe.c       |  6 ++-
 12 files changed, 72 insertions(+), 160 deletions(-)
arch/arm64/kernel/sdei.c +2 -12
index d6259dac62b6..e396e69e33a1 100644
@@ -251,22 +251,12 @@ asmlinkage __kprobes notrace unsigned long
 __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
 {
 	unsigned long ret;
-	bool do_nmi_exit = false;
 
-	/*
-	 * nmi_enter() deals with printk() re-entrance and use of RCU when
-	 * RCU believed this CPU was idle. Because critical events can
-	 * interrupt normal events, we may already be in_nmi().
-	 */
-	if (!in_nmi()) {
-		nmi_enter();
-		do_nmi_exit = true;
-	}
+	nmi_enter();
 
 	ret = _sdei_handler(regs, arg);
 
-	if (do_nmi_exit)
-		nmi_exit();
+	nmi_exit();
 
 	return ret;
 }
arch/arm64/kernel/traps.c +2 -6
index cf402be5c573..c728f163f329 100644
@@ -906,17 +906,13 @@ bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
 
 asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
 {
-	const bool was_in_nmi = in_nmi();
-
-	if (!was_in_nmi)
-		nmi_enter();
+	nmi_enter();
 
 	/* non-RAS errors are not containable */
 	if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
 		arm64_serror_panic(regs, esr);
 
-	if (!was_in_nmi)
-		nmi_exit();
+	nmi_exit();
 }
 
 asmlinkage void enter_from_user_mode(void)
arch/powerpc/kernel/traps.c +5 -14
index 014ff0701f24..aaaeb2884f59 100644
@@ -441,15 +441,9 @@ void hv_nmi_check_nonrecoverable(struct pt_regs *regs)
 void system_reset_exception(struct pt_regs *regs)
 {
 	unsigned long hsrr0, hsrr1;
-	bool nested = in_nmi();
 	bool saved_hsrrs = false;
 
-	/*
-	 * Avoid crashes in case of nested NMI exceptions. Recoverability
-	 * is determined by RI and in_nmi
-	 */
-	if (!nested)
-		nmi_enter();
+	nmi_enter();
 
 	/*
 	 * System reset can interrupt code where HSRRs are live and MSR[RI]=1.
@@ -521,8 +515,7 @@ void system_reset_exception(struct pt_regs *regs)
 		mtspr(SPRN_HSRR1, hsrr1);
 	}
 
-	if (!nested)
-		nmi_exit();
+	nmi_exit();
 
 	/* What should we do here? We could issue a shutdown or hard reset. */
 }
@@ -823,9 +816,8 @@ int machine_check_generic(struct pt_regs *regs)
 void machine_check_exception(struct pt_regs *regs)
 {
 	int recover = 0;
-	bool nested = in_nmi();
-	if (!nested)
-		nmi_enter();
+
+	nmi_enter();
 
 	__this_cpu_inc(irq_stat.mce_exceptions);
 
@@ -863,8 +855,7 @@ void machine_check_exception(struct pt_regs *regs)
 	return;
 
 bail:
-	if (!nested)
-		nmi_exit();
+	nmi_exit();
 }
 
 void SMIException(struct pt_regs *regs)
arch/x86/include/asm/traps.h +0 -5
index ffa0dc8a535e..6aa9da422d23 100644
@@ -121,11 +121,6 @@ void smp_spurious_interrupt(struct pt_regs *regs);
 void smp_error_interrupt(struct pt_regs *regs);
 asmlinkage void smp_irq_move_cleanup_interrupt(void);
 
-extern void ist_enter(struct pt_regs *regs);
-extern void ist_exit(struct pt_regs *regs);
-extern void ist_begin_non_atomic(struct pt_regs *regs);
-extern void ist_end_non_atomic(void);
-
 #ifdef CONFIG_VMAP_STACK
 void __noreturn handle_stack_overflow(const char *message,
 				      struct pt_regs *regs,
arch/x86/kernel/cpu/mce/core.c +34 -26

99 lines not on this page · raw message

arch/x86/kernel/cpu/mce/p5.c +4 -3
index 4ae6df556526..135f6d5df3db 100644
@@ -20,11 +20,11 @@
 int mce_p5_enabled __read_mostly;
 
 /* Machine check handler for Pentium class Intel CPUs: */
-static void pentium_machine_check(struct pt_regs *regs, long error_code)
+static notrace void pentium_machine_check(struct pt_regs *regs, long error_code)
 {
 	u32 loaddr, hi, lotype;
 
-	ist_enter(regs);
+	nmi_enter();
 
 	rdmsr(MSR_IA32_P5_MC_ADDR, loaddr, hi);
 	rdmsr(MSR_IA32_P5_MC_TYPE, lotype, hi);
@@ -39,8 +39,9 @@ static void pentium_machine_check(struct pt_regs *regs, long error_code)
 
 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
 
-	ist_exit(regs);
+	nmi_exit();
 }
+NOKPROBE_SYMBOL(pentium_machine_check);
 
 /* Set up machine check reporting for processors with Intel style MCE: */
 void intel_p5_mcheck_init(struct cpuinfo_x86 *c)
arch/x86/kernel/cpu/mce/winchip.c +4 -3
index a30ea13cccc2..fbc5216c72f7 100644
@@ -16,15 +16,16 @@
 #include "internal.h"
 
 /* Machine check handler for WinChip C6: */
-static void winchip_machine_check(struct pt_regs *regs, long error_code)
+static notrace void winchip_machine_check(struct pt_regs *regs, long error_code)
 {
-	ist_enter(regs);
+	nmi_enter();
 
 	pr_emerg("CPU0: Machine Check Exception.\n");
 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
 
-	ist_exit(regs);
+	nmi_exit();
 }
+NOKPROBE_SYMBOL(winchip_machine_check);
 
 /* Set up machine check reporting on the Winchip C6 series */
 void winchip_mcheck_init(struct cpuinfo_x86 *c)
arch/x86/kernel/traps.c +8 -86

148 lines not on this page · raw message

include/linux/hardirq.h +1 -1
index da0af631ded5..146332764673 100644
@@ -71,7 +71,7 @@ extern void irq_exit(void);
 		printk_nmi_enter();				\
 		lockdep_off();					\
 		ftrace_nmi_enter();				\
-		BUG_ON(in_nmi());				\
+		BUG_ON(in_nmi() == 0xf);			\
 		preempt_count_add(NMI_OFFSET + HARDIRQ_OFFSET);	\
 		rcu_nmi_enter();				\
 		trace_hardirq_enter();				\
include/linux/preempt.h +2 -2
index bbb68dba37cc..9b26069531de 100644
@@ -26,13 +26,13 @@
  *         PREEMPT_MASK:	0x000000ff
  *         SOFTIRQ_MASK:	0x0000ff00
  *         HARDIRQ_MASK:	0x000f0000
- *             NMI_MASK:	0x00100000
+ *             NMI_MASK:	0x00f00000
  * PREEMPT_NEED_RESCHED:	0x80000000
  */
 #define PREEMPT_BITS	8
 #define SOFTIRQ_BITS	8
 #define HARDIRQ_BITS	4
-#define NMI_BITS	1
+#define NMI_BITS	4
 
 #define PREEMPT_SHIFT	0
 #define SOFTIRQ_SHIFT	(PREEMPT_SHIFT + PREEMPT_BITS)
include/linux/sched.h +6 -0
index b49901f90ef5..ad2aed16b74b 100644
@@ -1285,6 +1285,12 @@ struct task_struct {
 	unsigned long			prev_lowest_stack;
 #endif
 
+#ifdef CONFIG_MEMORY_FAILURE
+	u64				mce_addr;
+	u64				mce_status;
+	struct callback_head		mce_kill_me;
+#endif
+
 	/*
 	 * New fields for task_struct should be added above here, so that
 	 * they are included in the randomized portion of task_struct.
kernel/printk/printk_safe.c +4 -2
index b4045e782743..a029393a8541 100644
@@ -296,12 +296,14 @@ static __printf(1, 0) int vprintk_nmi(const char *fmt, va_list args)
 
 void notrace printk_nmi_enter(void)
 {
-	this_cpu_or(printk_context, PRINTK_NMI_CONTEXT_MASK);
+	if (!in_nmi())
+		this_cpu_or(printk_context, PRINTK_NMI_CONTEXT_MASK);
 }
 
 void notrace printk_nmi_exit(void)
 {
-	this_cpu_and(printk_context, ~PRINTK_NMI_CONTEXT_MASK);
+	if (!in_nmi())
+		this_cpu_and(printk_context, ~PRINTK_NMI_CONTEXT_MASK);
 }
 
 /*
maintainer
#16 lore raw

include/linux/hardirq.h extern void irq_exit(void); · line 74

Quoted 10 lines · Peter Zijlstra
include/linux/hardirq.h +1 -1
index da0af631ded5..146332764673 100644
@@ -71,7 +71,7 @@ extern void irq_exit(void);
 		printk_nmi_enter();				\
 		lockdep_off();					\
 		ftrace_nmi_enter();				\
-		BUG_ON(in_nmi());				\
+		BUG_ON(in_nmi() == 0xf);			\

That wants to be:

		BUG_ON(in_nmi() == NMI_MASK);			\
Quoted 3 lines
include/linux/hardirq.h +0 -0
 		preempt_count_add(NMI_OFFSET + HARDIRQ_OFFSET);	\
 		rcu_nmi_enter();				\
 		trace_hardirq_enter();				\
#17 lore raw
Quoted 14 lines · Peter Zijlstra

include/linux/hardirq.h extern void irq_exit(void); · line 74

Quoted 10 lines · Peter Zijlstra
include/linux/hardirq.h +1 -1
index da0af631ded5..146332764673 100644
@@ -71,7 +71,7 @@ extern void irq_exit(void);
 		printk_nmi_enter();				\
 		lockdep_off();					\
 		ftrace_nmi_enter();				\
-		BUG_ON(in_nmi());				\
+		BUG_ON(in_nmi() == 0xf);			\

That wants to be:

Quoted 1 line
		BUG_ON(in_nmi() == NMI_MASK);			\

Ah that's the email I didn't read...
Sorry for the noise.

#18 lore raw
In reply to Peter Zijlstra

include/linux/hardirq.h extern void irq_exit(void); · line 74

Quoted 11 lines · Peter Zijlstra

On Tue, Feb 18, 2020 at 09:34:04PM +0100, Peter Zijlstra wrote:

include/linux/hardirq.h +1 -1
index da0af631ded5..146332764673 100644
@@ -71,7 +71,7 @@ extern void irq_exit(void);
 		printk_nmi_enter();				\
 		lockdep_off();					\
 		ftrace_nmi_enter();				\
-		BUG_ON(in_nmi());				\
+		BUG_ON(in_nmi() == 0xf);			\

I guess you meant:

  BUG_ON(in_nmi() == NMI_MASK);

Thanks.

#19 lore raw
In reply to Peter Zijlstra
Quoted 13 lines · Peter Zijlstra
Quoted 8 lines · Tony
Quoted 1 line

Anything else I'm missing? It is likely...

arch/x86/kernel/cpu/mce/core.c +4 -0
+    hw_breakpoint_disable();
+    static_key_disable(&__tracepoint_read_msr.key);
+    tracing_off();
+
   ist_enter(regs);
Quoted 8 lines
Quoted 1 line

How about some code to turn all those back on for a recoverable (where we actually recovered) #MC?

Then please rewrite the #MC entry code to deal with nested exceptions unmasking the MCE, very similr to NMI.

The moment you allow tracing, jump_labels or anything else you can expect #PF, #BP and probably #DB while inside #MC, those will then IRET and re-enable the #MC.

Huh? As I understand it, there is no such thing as MCE masking. There are two states:

CR4.MCE=1: MCE is delivered when it occurs.

CR4.MCE=0: MCE causes shutdown

MC delivery sets MCE=0.

So, basically, without LMCE, we are irredeemably screwed. With LMCE, we are still hosed if we nest an MCE inside a recoverable MCE. We can play some games to make the OOPS more reliable, but we are still mostly screwed.

The x86 MCE architecture sucks.

Quoted 2 lines

The current situation is completely and utterly buggered.

#20 lore raw
In reply to Luck, Tony
Quoted 14 lines · Tony



Quoted 2 lines

Anything else I'm missing? It is likely...

arch/x86/kernel/cpu/mce/core.c +4 -0
+    hw_breakpoint_disable();
+    static_key_disable(&__tracepoint_read_msr.key);
+    tracing_off();
+
   ist_enter(regs);
Quoted 3 lines

How about some code to turn all those back on for a recoverable (where we actually recovered) #MC?

At the very least, in the user_mode(regs) case, tracing is fine.

#21 lore raw
Quoted 12 lines · On Tue, 18 Feb 2020 15:10:17 -0800 Andy Lutomirski <[email protected]> wrote:
Quoted 12 lines · Tony



Quoted 2 lines

Anything else I'm missing? It is likely...

arch/x86/kernel/cpu/mce/core.c +4 -0
+    hw_breakpoint_disable();
+    static_key_disable(&__tracepoint_read_msr.key);
+    tracing_off();
+
   ist_enter(regs);
Quoted 6 lines
Quoted 3 lines

How about some code to turn all those back on for a recoverable (where we actually recovered) #MC?

At the very least, in the user_mode(regs) case, tracing is fine.

Also, I don't think "tracing_off()" is what is wanted here. That just disables writing to the ring buffer, which can be called in pretty much any context (if it's before in_nmi() get's set, the worse thing that happens is that events will get dropped due to the recursion protection that checks to make sure there's no re-entrant events at the same level of context).

The only issue with having function tracing enabled, is that it may add a breakpoint when it gets turned on or off. And that tracing_off() doesn't prevent that.

tracepoints still use RCU of some kind, and the protection there has nothing to do with whether a trace point does recording or not.

-- Steve

#22 lore raw
In reply to Borislav Petkov
Quoted 9 lines · Borislav Petkov

Ok,

so Peter raised this question on IRC today, that the #MC handler needs to disable all kinds of tracing/kprobing and etc exceptions happening while handling an #MC. And I guess we can talk about supporting some exceptions but #MC is usually nasty enough to not care about tracing when former happens.

It's worth noting that MCE is utterly, terminally screwed under high load. In particular:

Step 1: NMI (due to perf).

immediately thereafter (before any of the entry asm runs)

Step 2: MCE (due to recoverable memory failure or remote CPU MCE)

Step 3: MCE does its thing and does IRET

Step 4: NMI

We are toast.

Tony, etc, can you ask your Intel contacts who care about this kind of thing to stop twiddling their thumbs and FIX IT? The easy fix is utterly trivial. Add a new instruction IRET_NON_NMI. It does *exactly* the same thing as IRET except that it does not unmask NMIs. (It also doesn't unmask NMIs if it faults.) No fancy design work. Future improvements can still happen on top of this.

(One other improvement that may or may not have happened: the CPU should be configurable so that it never even sends #MC unless it literally cannot continue executing without OS help. No remote MCE triggering #MC, no notifications of corrected errors, no nothing. If the CPU *cannot* continue execution in its current context, for example because a load could not be satisfied, send #MC. If a cache line cannot be written back, then *which* CPU should get the MCE is an interesting question.)

If Intel cares about memory failure recovery, then this design problem needs to be fixed. Without a fix, we're just duct taping little holes and ignoring the giant gaping hole in front of our faces.

maintainer
#23 lore raw
Quoted 24 lines · Andy Lutomirski
Quoted 9 lines · Borislav Petkov

Ok,

so Peter raised this question on IRC today, that the #MC handler needs to disable all kinds of tracing/kprobing and etc exceptions happening while handling an #MC. And I guess we can talk about supporting some exceptions but #MC is usually nasty enough to not care about tracing when former happens.

It's worth noting that MCE is utterly, terminally screwed under high load. In particular:

Step 1: NMI (due to perf).

immediately thereafter (before any of the entry asm runs)

Step 2: MCE (due to recoverable memory failure or remote CPU MCE)

Step 3: MCE does its thing and does IRET

Step 4: NMI

Quoted 8 lines

We are toast.

Tony, etc, can you ask your Intel contacts who care about this kind of thing to stop twiddling their thumbs and FIX IT? The easy fix is utterly trivial. Add a new instruction IRET_NON_NMI. It does *exactly* the same thing as IRET except that it does not unmask NMIs. (It also doesn't unmask NMIs if it faults.) No fancy design work. Future improvements can still happen on top of this.

Yes please! Of course, we're stuck with the existing NMI entry crap forever because legacy, but it would make all things NMI so much saner.

#24 lore raw
Quoted 10 lines · On Wed, 19 Feb 2020 09:15:41 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 7 lines

Tony, etc, can you ask your Intel contacts who care about this kind of thing to stop twiddling their thumbs and FIX IT? The easy fix is utterly trivial. Add a new instruction IRET_NON_NMI. It does *exactly* the same thing as IRET except that it does not unmask NMIs. (It also doesn't unmask NMIs if it faults.) No fancy design work. Future improvements can still happen on top of this.

Yes please! Of course, we're stuck with the existing NMI entry crap forever because legacy, but it would make all things NMI so much saner.

What would be nice is to have a NMI_IRET, that is defined as something that wont break legacy CPUs. Where it could be just a nop iret, or maybe if possible a "lock iret"? That is, not have a IRET_NON_NMI, as that would be all over the place, but just the iret for NMI itself. As that's in one place.

-- Steve

author maintainer
#25 lore raw
Quoted 5 lines · Steven Rostedt

What would be nice is to have a NMI_IRET, that is defined as something that wont break legacy CPUs. Where it could be just a nop iret, or maybe if possible a "lock iret"? That is, not have a IRET_NON_NMI, as that would be all over the place, but just the iret for NMI itself. As that's in one place.

You mean, we could keep the nested NMI thing but it won't practically get executed on NMI_IRET CPUs because there won't be any NMI nesting anymore?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#26 lore raw
In reply to Steven Rostedt
Quoted 11 lines · Steven Rostedt
Quoted 8 lines · On Wed, 19 Feb 2020 09:15:41 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 7 lines

Tony, etc, can you ask your Intel contacts who care about this kind of thing to stop twiddling their thumbs and FIX IT? The easy fix is utterly trivial. Add a new instruction IRET_NON_NMI. It does *exactly* the same thing as IRET except that it does not unmask NMIs. (It also doesn't unmask NMIs if it faults.) No fancy design work. Future improvements can still happen on top of this.

Quoted 8 lines
Quoted 2 lines

Yes please! Of course, we're stuck with the existing NMI entry crap forever because legacy, but it would make all things NMI so much saner.

What would be nice is to have a NMI_IRET, that is defined as something that wont break legacy CPUs. Where it could be just a nop iret, or maybe if possible a "lock iret"? That is, not have a IRET_NON_NMI, as that would be all over the place, but just the iret for NMI itself. As that's in one place.

I don't think that matters much; alternatives should be able to deal with all that either which way around.

#27 lore raw
Quoted 19 lines · Peter Zijlstra
Quoted 14 lines · Steven Rostedt
Quoted 13 lines
Quoted 7 lines · On Wed, 19 Feb 2020 09:15:41 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 7 lines

Tony, etc, can you ask your Intel contacts who care about this kind of thing to stop twiddling their thumbs and FIX IT? The easy fix is utterly trivial. Add a new instruction IRET_NON_NMI. It does *exactly* the same thing as IRET except that it does not unmask NMIs. (It also doesn't unmask NMIs if it faults.) No fancy design work. Future improvements can still happen on top of this.

Yes please! Of course, we're stuck with the existing NMI entry crap forever because legacy, but it would make all things NMI so much saner.

Quoted 8 lines
Quoted 5 lines

What would be nice is to have a NMI_IRET, that is defined as something that wont break legacy CPUs. Where it could be just a nop iret, or maybe if possible a "lock iret"? That is, not have a IRET_NON_NMI, as that would be all over the place, but just the iret for NMI itself. As that's in one place.

I don't think that matters much; alternatives should be able to deal with all that either which way around.

Agreed. That being said, kernels without alternatives could prefer the variant where a CR4 bit makes regular IRET leave NMIs masked and a new IRET instruction (or LOCK IRET, I suppose) unmasks them.