Skip to contentSkip to conversation

x86, MCE: MCE event ring management

PATCH In review Chen, Gong 4 participants 19 messages Aug 14, 2014 – Nov 12, 2014

Subsystems (7)
x86 ACPI Scheduler EDAC-core +3 more subsystems
lore
Share & tools
lore

Chen, Gong

author
#0 lore raw

We have too many rings for different H/W error events management. All of them can be merged into one kind of unified mechanism. Furthermore, this management mechanism should be reliable enough even in MCE context to avoid deadlock like calling printk in MCE context. This patch series is used for this purpose.

[PATCH v2 1/5] x86, MCE: Provide a lock-less memory pool to save [PATCH v2 2/5] x86, MCE: Don't use percpu for MCE workqueue/irq_work [PATCH v2 3/5] x86, MCE: Remove mce_ring for SRAO error [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable [PATCH v2 5/5] x86, MCE: Avoid potential deadlock in MCE context

author
#1 lore raw

PATCH v2 1/5 x86, MCE: Provide a lock-less memory pool to save error record

printk is not safe to use in MCE context. Add a lockless memory allocator pool to save error records in MCE context. Issuing those records will be delayed to a context safe to do printk. This idea is inspired by APEI/GHES driver.

We're very conservative and allocate only two pages for it but since we're going to use those pages throughout the system's lifetime, we allocate them statically to avoid early boot time allocation woes.

---
 arch/x86/Kconfig                          |   1 +
 arch/x86/include/uapi/asm/mce.h           |   3 +-
 arch/x86/kernel/cpu/mcheck/Makefile       |   2 +-
 arch/x86/kernel/cpu/mcheck/mce-genpool.c  | 102 ++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/mcheck/mce-internal.h |  11 ++++
 arch/x86/kernel/cpu/mcheck/mce.c          |   6 ++
 6 files changed, 123 insertions(+), 2 deletions(-)
 create mode 100644 arch/x86/kernel/cpu/mcheck/mce-genpool.c
arch/x86/Kconfig +1 -0
index 4aafd322e21e..706dc3c8f6d6 100644
@@ -866,6 +866,7 @@ config X86_REROUTE_FOR_BROKEN_BOOT_IRQS
 
 config X86_MCE
 	bool "Machine Check / overheating reporting"
+	select GENERIC_ALLOCATOR
 	default y
 	---help---
 	  Machine Check support allows the processor to notify the
arch/x86/include/uapi/asm/mce.h +2 -1
index a0eab85ce7b8..577acacaf515 100644
@@ -15,7 +15,8 @@ struct mce {
 	__u64 time;	/* wall time_t when error was detected */
 	__u8  cpuvendor;	/* cpu vendor as encoded in system.h */
 	__u8  inject_flags;	/* software inject flags */
-	__u16  pad;
+	__u8  severity;
+	__u8  usable_addr;	/* addr is usable */
 	__u32 cpuid;	/* CPUID 1 EAX */
 	__u8  cs;		/* code segment */
 	__u8  bank;	/* machine check bank */
arch/x86/kernel/cpu/mcheck/Makefile +1 -1
index bb34b03af252..a3311c886194 100644
@@ -1,4 +1,4 @@
-obj-y				=  mce.o mce-severity.o
+obj-y				=  mce.o mce-severity.o mce-genpool.o
 
 obj-$(CONFIG_X86_ANCIENT_MCE)	+= winchip.o p5.o
 obj-$(CONFIG_X86_MCE_INTEL)	+= mce_intel.o
arch/x86/kernel/cpu/mcheck/mce-genpool.c new file +102 -0

102 lines not on this page · raw message

arch/x86/kernel/cpu/mcheck/mce-internal.h +11 -0
index 09edd0b65fef..6f0ea3db02c8 100644
@@ -11,6 +11,8 @@ enum severity_level {
 	MCE_PANIC_SEVERITY,
 };
 
+extern struct atomic_notifier_head x86_mce_decoder_chain;
+
 #define ATTR_LEN		16
 
 /* One object for each MCE bank, shared by all CPUs */
@@ -21,6 +23,15 @@ struct mce_bank {
 	char			attrname[ATTR_LEN];	/* attribute name */
 };
 
+struct mce_evt_llist {
+	struct llist_node llnode;
+	struct mce mce;
+};
+
+void mce_genpool_process(void);
+bool mce_genpool_empty(void);
+bool mce_genpool_add(struct mce *mce);
+int mce_genpool_init(void);
 int mce_severity(struct mce *a, int tolerant, char **msg);
 struct dentry *mce_get_debugfs_dir(void);
 
arch/x86/kernel/cpu/mcheck/mce.c +6 -0
index bd9ccda8087f..b2c8bb83672a 100644
@@ -1686,6 +1686,12 @@ void mcheck_cpu_init(struct cpuinfo_x86 *c)
 	if (mca_cfg.disabled)
 		return;
 
+	if (mce_genpool_init()) {
+		mca_cfg.disabled = true;
+		pr_emerg("Couldn't allocate MCE records pool!\n");
+		return;
+	}
+
 	if (__mcheck_cpu_ancient_init(c))
 		return;
 
Signature · 1 line
2.0.0.rc2
author
#2 lore raw

PATCH v2 2/5 x86, MCE: Don't use percpu for MCE workqueue/irq_work

In reply to Chen, Gong

An MCE is considered a rare event. Therefore, there's no need to have per-CPU instances of both normal and IRQ workqueues. Make them both global.

---
 arch/x86/kernel/cpu/mcheck/mce.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/mcheck/mce.c +6 -7
index b2c8bb83672a..7534cfce43e9 100644
@@ -107,7 +107,8 @@ DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
  */
 mce_banks_t mce_banks_ce_disabled;
 
-static DEFINE_PER_CPU(struct work_struct, mce_work);
+static struct work_struct mce_work;
+static struct irq_work mce_irq_work;
 
 static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
 
@@ -526,11 +527,9 @@ int mce_available(struct cpuinfo_x86 *c)
 static void mce_schedule_work(void)
 {
 	if (!mce_ring_empty())
-		schedule_work(&__get_cpu_var(mce_work));
+		schedule_work(&mce_work);
 }
 
-DEFINE_PER_CPU(struct irq_work, mce_irq_work);
-
 static void mce_irq_work_cb(struct irq_work *entry)
 {
 	mce_notify_irq();
@@ -551,7 +550,7 @@ static void mce_report_event(struct pt_regs *regs)
 		return;
 	}
 
-	irq_work_queue(&__get_cpu_var(mce_irq_work));
+	irq_work_queue(&mce_irq_work);
 }
 
 /*
@@ -1708,8 +1707,8 @@ void mcheck_cpu_init(struct cpuinfo_x86 *c)
 	__mcheck_cpu_init_generic();
 	__mcheck_cpu_init_vendor(c);
 	__mcheck_cpu_init_timer();
-	INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
-	init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
+	INIT_WORK(&mce_work, mce_process_work);
+	init_irq_work(&mce_irq_work, mce_irq_work_cb);
 }
 
 /*
Signature · 1 line
2.0.0.rc2
author
#3 lore raw

PATCH v2 3/5 x86, MCE: Remove mce_ring for SRAO error

In reply to Chen, Gong

Use unified genpool to save SRAO error events and put AO error handling in the same notification chain with mce error decoding.

---
 arch/x86/include/asm/mce.h       |   2 +-
 arch/x86/kernel/cpu/mcheck/mce.c | 110 ++++++++++++++-------------------------
 drivers/acpi/acpi_extlog.c       |   2 +-
 drivers/edac/i7core_edac.c       |   2 +-
 drivers/edac/mce_amd.c           |   2 +-
 drivers/edac/sb_edac.c           |   2 +-
 6 files changed, 43 insertions(+), 77 deletions(-)
arch/x86/include/asm/mce.h +1 -1
index 958b90f761e5..a713dabd000b 100644
@@ -114,7 +114,7 @@ struct mca_config {
 };
 
 extern struct mca_config mca_cfg;
-extern void mce_register_decode_chain(struct notifier_block *nb);
+extern void mce_register_decode_chain(struct notifier_block *nb, bool drain);
 extern void mce_unregister_decode_chain(struct notifier_block *nb);
 
 #include <linux/percpu.h>
arch/x86/kernel/cpu/mcheck/mce.c +38 -72

158 lines not on this page · raw message

drivers/acpi/acpi_extlog.c +1 -1
index b3842ffc19ba..07e012e74c1b 100644
@@ -286,7 +286,7 @@ static int __init extlog_init(void)
 	 */
 	old_edac_report_status = get_edac_report_status();
 	set_edac_report_status(EDAC_REPORTING_DISABLED);
-	mce_register_decode_chain(&extlog_mce_dec);
+	mce_register_decode_chain(&extlog_mce_dec, true);
 	/* enable OS to be involved to take over management from BIOS */
 	((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN;
 
drivers/edac/i7core_edac.c +1 -1
index 9cd0b301f81b..7e347c4ce641 100644
@@ -2432,7 +2432,7 @@ static int __init i7core_init(void)
 	pci_rc = pci_register_driver(&i7core_driver);
 
 	if (pci_rc >= 0) {
-		mce_register_decode_chain(&i7_mce_dec);
+		mce_register_decode_chain(&i7_mce_dec, true);
 		return 0;
 	}
 
drivers/edac/mce_amd.c +1 -1
index f78c1c54dbd5..718c41c41f07 100644
@@ -914,7 +914,7 @@ static int __init mce_amd_init(void)
 
 	pr_info("MCE: In-kernel MCE decoding enabled.\n");
 
-	mce_register_decode_chain(&amd_mce_dec_nb);
+	mce_register_decode_chain(&amd_mce_dec_nb, true);
 
 	return 0;
 }
drivers/edac/sb_edac.c +1 -1
index deea0dc9999b..d2dc2929568d 100644
@@ -2153,7 +2153,7 @@ static int __init sbridge_init(void)
 
 	pci_rc = pci_register_driver(&sbridge_driver);
 	if (pci_rc >= 0) {
-		mce_register_decode_chain(&sbridge_mce_dec);
+		mce_register_decode_chain(&sbridge_mce_dec, true);
 		if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
 			sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
 		return 0;
Signature · 1 line
2.0.0.rc2
author
#4 lore raw

PATCH v2 4/5 x86/mce: Simplify flow when handling recoverable memory errors

In reply to Chen, Gong

From: Tony Luck <[email protected]>

Handling of uncorrected, but s/w recoverable, errors is a two phase process:

1) In the machine check handler we identify the signature of a recoverable error and use TIF_MCE_NOTIFY to mark the process so that we will not return to ring3 and re-execute the instruction that accessed the failed memory location.
2) In process context we see TIF_MCE_NOTIFY set and call mce_notify_process() to do all the recovery work that we could not do in machine check context.

We need to pass the memory address and a flag to say whether the faulting instruction can be continued from stage "1" to stage "2". The existing code to do this is baroque. We hunt through a fixed sized array of "mce_info" structures and use atomic_cmpxchg to claim a free one (we panic if all are busy). Then in phase 2 we scan back through the mce_info array looking for the one that matches the current process.

We can do much better by admitting that the failing address found in phase 1 is a property of the current task. We can only recover from errors detected while trying to retire an instruction in ring3 (s/w limitation). So we know that MCE_AR_SEVERITY means that we are running on the cpu that was executing user code and hit an error that triggered this machine check. This is the reason why TIF_MCE_NOTIFY has been set, and the memory error must be dealt with before we can either allow this process to continue (if we can replace the page) or not (signalling the process if the memory has been lost).

So we add two fields to the task structure to be saved in phase 1 and retrieved in phase 2. The mce_info[] array and routines to add/find/clear entried from it can all be deleted.

Gong: Add missed CONFIG_MEMORY_FAILURE & '\n' to the end of printk message, while at it.

---
 arch/x86/include/asm/mce.h       |  2 ++
 arch/x86/kernel/cpu/mcheck/mce.c | 67 +++++++---------------------------------
 arch/x86/kernel/signal.c         |  2 +-
 include/linux/sched.h            |  4 +++
 4 files changed, 19 insertions(+), 56 deletions(-)
arch/x86/include/asm/mce.h +2 -0
index a713dabd000b..76e706767655 100644
@@ -187,7 +187,9 @@ enum mcp_flags {
 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b);
 
 int mce_notify_irq(void);
+#ifdef CONFIG_MEMORY_FAILURE
 void mce_notify_process(void);
+#endif
 
 DECLARE_PER_CPU(struct mce, injectm);
 
arch/x86/kernel/cpu/mcheck/mce.c +12 -55

107 lines not on this page · raw message

arch/x86/kernel/signal.c +1 -1
index 2851d63c1202..28fd70560b96 100644
@@ -735,7 +735,7 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
 {
 	user_exit();
 
-#ifdef CONFIG_X86_MCE
+#ifdef CONFIG_MEMORY_FAILURE
 	/* notify userspace of pending MCEs */
 	if (thread_info_flags & _TIF_MCE_NOTIFY)
 		mce_notify_process();
include/linux/sched.h +4 -0
index 857ba40426ba..b41e839b9e1e 100644
@@ -1645,6 +1645,10 @@ struct task_struct {
 	unsigned int	sequential_io;
 	unsigned int	sequential_io_avg;
 #endif
+#ifdef CONFIG_MEMORY_FAILURE
+	__u64	paddr;
+	int	restartable;
+#endif
 };
 
 /* Future-safe accessor for struct task_struct's cpus_allowed. */
Signature · 1 line
2.0.0.rc2
maintainer
#5 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Let me CC some more people:

So guys, we would like to mark a task as to-be-killed if it has encountered an uncorrectable error during processing.

The current code is nasty (see Tony's patch and commit message below) and we would like to be able to say:

	current->restartable = true/false;

in the machine check handler and then, when we get back to userspace but *before* we sched in the task, to check whether it is restartable and if not, to kill it and poison the memory it has been utilizing so that we can contain the error instead of killing the whole system.

Currently, we're piggybacking on do_notify_resume()->mce_notify_process() but I hear this is not that clean and nice so I'd be open to suggestions on how to do this in a nicer/cleaner way.

Roughly speaking, we want to be able to mark a task with the sign of death and to kill it, if needed. The important part is *before* it gets to run again. And the approach with adding two-more fields to task_struct seems cleaner than what we have currently.

Any suggestion is appreciated,
thanks!

Oh, and btw, sorry for the top-posting - leaving in the whole mail for reference.

Quoted 205 lines · Gong

From: Tony Luck <[email protected]>

Handling of uncorrected, but s/w recoverable, errors is a two phase process:

1) In the machine check handler we identify the signature of a recoverable error and use TIF_MCE_NOTIFY to mark the process so that we will not return to ring3 and re-execute the instruction that accessed the failed memory location.
2) In process context we see TIF_MCE_NOTIFY set and call mce_notify_process() to do all the recovery work that we could not do in machine check context.

We need to pass the memory address and a flag to say whether the faulting instruction can be continued from stage "1" to stage "2". The existing code to do this is baroque. We hunt through a fixed sized array of "mce_info" structures and use atomic_cmpxchg to claim a free one (we panic if all are busy). Then in phase 2 we scan back through the mce_info array looking for the one that matches the current process.

We can do much better by admitting that the failing address found in phase 1 is a property of the current task. We can only recover from errors detected while trying to retire an instruction in ring3 (s/w limitation). So we know that MCE_AR_SEVERITY means that we are running on the cpu that was executing user code and hit an error that triggered this machine check. This is the reason why TIF_MCE_NOTIFY has been set, and the memory error must be dealt with before we can either allow this process to continue (if we can replace the page) or not (signalling the process if the memory has been lost).

So we add two fields to the task structure to be saved in phase 1 and retrieved in phase 2. The mce_info[] array and routines to add/find/clear entried from it can all be deleted.

Gong: Add missed CONFIG_MEMORY_FAILURE & '\n' to the end of printk message, while at it.

---
 arch/x86/include/asm/mce.h       |  2 ++
 arch/x86/kernel/cpu/mcheck/mce.c | 67 +++++++---------------------------------
 arch/x86/kernel/signal.c         |  2 +-
 include/linux/sched.h            |  4 +++
 4 files changed, 19 insertions(+), 56 deletions(-)
arch/x86/include/asm/mce.h +2 -0
index a713dabd000b..76e706767655 100644
@@ -187,7 +187,9 @@ enum mcp_flags {
 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b);
 
 int mce_notify_irq(void);
+#ifdef CONFIG_MEMORY_FAILURE
 void mce_notify_process(void);
+#endif
 
 DECLARE_PER_CPU(struct mce, injectm);
 
arch/x86/kernel/cpu/mcheck/mce.c +12 -55
index 0e64a3c69b2b..b0a25d5f7185 100644
@@ -928,51 +928,6 @@ static void mce_clear_state(unsigned long *toclear)
 }
 
 /*
- * Need to save faulting physical address associated with a process
- * in the machine check handler some place where we can grab it back
- * later in mce_notify_process()
- */
-#define	MCE_INFO_MAX	16
-
-struct mce_info {
-	atomic_t		inuse;
-	struct task_struct	*t;
-	__u64			paddr;
-	int			restartable;
-} mce_info[MCE_INFO_MAX];
-
-static void mce_save_info(__u64 addr, int c)
-{
-	struct mce_info *mi;
-
-	for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
-		if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
-			mi->t = current;
-			mi->paddr = addr;
-			mi->restartable = c;
-			return;
-		}
-	}
-
-	mce_panic("Too many concurrent recoverable errors", NULL, NULL);
-}
-
-static struct mce_info *mce_find_info(void)
-{
-	struct mce_info *mi;
-
-	for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
-		if (atomic_read(&mi->inuse) && mi->t == current)
-			return mi;
-	return NULL;
-}
-
-static void mce_clear_info(struct mce_info *mi)
-{
-	atomic_set(&mi->inuse, 0);
-}
-
-/*
  * The actual machine check handler. This only handles real
  * exceptions when something got corrupted coming in through int 18.
  *
@@ -1121,11 +1076,15 @@ void do_machine_check(struct pt_regs *regs, long error_code)
 	if (cfg->tolerant < 3) {
 		if (no_way_out)
 			mce_panic("Fatal machine check on current CPU", &m, msg);
+#ifdef CONFIG_MEMORY_FAILURE
 		if (worst == MCE_AR_SEVERITY) {
 			/* schedule action before return to userland */
-			mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
+			current->paddr = m.addr;
+			current->restartable = !!(m.mcgstatus & MCG_STATUS_RIPV);
 			set_thread_flag(TIF_MCE_NOTIFY);
-		} else if (kill_it) {
+		} else
+#endif
+		if (kill_it) {
 			force_sig(SIGBUS, current);
 		}
 	}
@@ -1159,33 +1118,31 @@ int memory_failure(unsigned long pfn, int vector, int flags)
  * process any corrupted pages, and kill/signal current process if required.
  * Action required errors are handled here.
  */
+#ifdef CONFIG_MEMORY_FAILURE
 void mce_notify_process(void)
 {
 	unsigned long pfn;
-	struct mce_info *mi = mce_find_info();
 	int flags = MF_ACTION_REQUIRED;
 
-	if (!mi)
-		mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
-	pfn = mi->paddr >> PAGE_SHIFT;
+	pfn = current->paddr >> PAGE_SHIFT;
 
 	clear_thread_flag(TIF_MCE_NOTIFY);
 
-	pr_err("Uncorrected hardware memory error in user-access at %llx",
-		 mi->paddr);
+	pr_err("Uncorrected hardware memory error in user-access at %llx\n",
+		 current->paddr);
 	/*
 	 * We must call memory_failure() here even if the current process is
 	 * doomed. We still need to mark the page as poisoned and alert any
 	 * other users of the page.
 	 */
-	if (!mi->restartable)
+	if (!current->restartable)
 		flags |= MF_MUST_KILL;
 	if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {
 		pr_err("Memory error not recovered");
 		force_sig(SIGBUS, current);
 	}
-	mce_clear_info(mi);
 }
+#endif
 
 /*
  * Action optional processing happens here (picking up
arch/x86/kernel/signal.c +1 -1
index 2851d63c1202..28fd70560b96 100644
@@ -735,7 +735,7 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
 {
 	user_exit();
 
-#ifdef CONFIG_X86_MCE
+#ifdef CONFIG_MEMORY_FAILURE
 	/* notify userspace of pending MCEs */
 	if (thread_info_flags & _TIF_MCE_NOTIFY)
 		mce_notify_process();
include/linux/sched.h +4 -0
index 857ba40426ba..b41e839b9e1e 100644
@@ -1645,6 +1645,10 @@ struct task_struct {
 	unsigned int	sequential_io;
 	unsigned int	sequential_io_avg;
 #endif
+#ifdef CONFIG_MEMORY_FAILURE
+	__u64	paddr;
+	int	restartable;
+#endif
 };
 
 /* Future-safe accessor for struct task_struct's cpus_allowed. */
Signature · 1 line
2.0.0.rc2
Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#6 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Quoted 21 lines · Borislav Petkov

Let me CC some more people:

So guys, we would like to mark a task as to-be-killed if it has encountered an uncorrectable error during processing.

The current code is nasty (see Tony's patch and commit message below) and we would like to be able to say:

        current->restartable = true/false;

in the machine check handler and then, when we get back to userspace but *before* we sched in the task, to check whether it is restartable and if not, to kill it and poison the memory it has been utilizing so that we can contain the error instead of killing the whole system.

Currently, we're piggybacking on do_notify_resume()->mce_notify_process() but I hear this is not that clean and nice so I'd be open to suggestions on how to do this in a nicer/cleaner way.

Quoted 4 lines

Roughly speaking, we want to be able to mark a task with the sign of death and to kill it, if needed. The important part is *before* it gets to run again. And the approach with adding two-more fields to task_struct seems cleaner than what we have currently.

The last time I looked at the MCE code, I got a bit lost in the control flow. Is there ever a userspace-killing MCE that's delivered from kernel mode? By that, I mean that I think that all userspace-killing MCEs go have user_mode_vm(regs) and go through paranoid_exit.

If so, why do you need to jump through hoops at all? You can't call do_exit, but it should be completely safe to force a fatal signal and let the scheduler and signal code take care of killing the process, right? For that matter, you should also be able to poke at vm structures, etc.

Or is there a meaningful case where mce_notify_process needs to help with recovery but the original MCE happened with !user_mode_vm(regs)?

--Andy

Quoted 220 lines

Any suggestion is appreciated,
thanks!

Oh, and btw, sorry for the top-posting - leaving in the whole mail for reference.

Quoted 202 lines · Gong

From: Tony Luck <[email protected]>

Handling of uncorrected, but s/w recoverable, errors is a two phase process:

1) In the machine check handler we identify the signature of a recoverable error and use TIF_MCE_NOTIFY to mark the process so that we will not return to ring3 and re-execute the instruction that accessed the failed memory location.
2) In process context we see TIF_MCE_NOTIFY set and call mce_notify_process() to do all the recovery work that we could not do in machine check context.

We need to pass the memory address and a flag to say whether the faulting instruction can be continued from stage "1" to stage "2". The existing code to do this is baroque. We hunt through a fixed sized array of "mce_info" structures and use atomic_cmpxchg to claim a free one (we panic if all are busy). Then in phase 2 we scan back through the mce_info array looking for the one that matches the current process.

We can do much better by admitting that the failing address found in phase 1 is a property of the current task. We can only recover from errors detected while trying to retire an instruction in ring3 (s/w limitation). So we know that MCE_AR_SEVERITY means that we are running on the cpu that was executing user code and hit an error that triggered this machine check. This is the reason why TIF_MCE_NOTIFY has been set, and the memory error must be dealt with before we can either allow this process to continue (if we can replace the page) or not (signalling the process if the memory has been lost).

So we add two fields to the task structure to be saved in phase 1 and retrieved in phase 2. The mce_info[] array and routines to add/find/clear entried from it can all be deleted.

Gong: Add missed CONFIG_MEMORY_FAILURE & '\n' to the end of printk message, while at it.

---
 arch/x86/include/asm/mce.h       |  2 ++
 arch/x86/kernel/cpu/mcheck/mce.c | 67 +++++++---------------------------------
 arch/x86/kernel/signal.c         |  2 +-
 include/linux/sched.h            |  4 +++
 4 files changed, 19 insertions(+), 56 deletions(-)
arch/x86/include/asm/mce.h +2 -0
index a713dabd000b..76e706767655 100644
@@ -187,7 +187,9 @@ enum mcp_flags {
 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b);
 
 int mce_notify_irq(void);
+#ifdef CONFIG_MEMORY_FAILURE
 void mce_notify_process(void);
+#endif
 
 DECLARE_PER_CPU(struct mce, injectm);
 
arch/x86/kernel/cpu/mcheck/mce.c +12 -55
index 0e64a3c69b2b..b0a25d5f7185 100644
@@ -928,51 +928,6 @@ static void mce_clear_state(unsigned long *toclear)
 }
 
 /*
- * Need to save faulting physical address associated with a process
- * in the machine check handler some place where we can grab it back
- * later in mce_notify_process()
- */
-#define      MCE_INFO_MAX    16
-
-struct mce_info {
-     atomic_t                inuse;
-     struct task_struct      *t;
-     __u64                   paddr;
-     int                     restartable;
-} mce_info[MCE_INFO_MAX];
-
-static void mce_save_info(__u64 addr, int c)
-{
-     struct mce_info *mi;
-
-     for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
-             if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
-                     mi->t = current;
-                     mi->paddr = addr;
-                     mi->restartable = c;
-                     return;
-             }
-     }
-
-     mce_panic("Too many concurrent recoverable errors", NULL, NULL);
-}
-
-static struct mce_info *mce_find_info(void)
-{
-     struct mce_info *mi;
-
-     for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
-             if (atomic_read(&mi->inuse) && mi->t == current)
-                     return mi;
-     return NULL;
-}
-
-static void mce_clear_info(struct mce_info *mi)
-{
-     atomic_set(&mi->inuse, 0);
-}
-
-/*
  * The actual machine check handler. This only handles real
  * exceptions when something got corrupted coming in through int 18.
  *
@@ -1121,11 +1076,15 @@ void do_machine_check(struct pt_regs *regs, long error_code)
      if (cfg->tolerant < 3) {
              if (no_way_out)
                      mce_panic("Fatal machine check on current CPU", &m, msg);
+#ifdef CONFIG_MEMORY_FAILURE
              if (worst == MCE_AR_SEVERITY) {
                      /* schedule action before return to userland */
-                     mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
+                     current->paddr = m.addr;
+                     current->restartable = !!(m.mcgstatus & MCG_STATUS_RIPV);
                      set_thread_flag(TIF_MCE_NOTIFY);
-             } else if (kill_it) {
+             } else
+#endif
+             if (kill_it) {
                      force_sig(SIGBUS, current);
              }
      }
@@ -1159,33 +1118,31 @@ int memory_failure(unsigned long pfn, int vector, int flags)
  * process any corrupted pages, and kill/signal current process if required.
  * Action required errors are handled here.
  */
+#ifdef CONFIG_MEMORY_FAILURE
 void mce_notify_process(void)
 {
      unsigned long pfn;
-     struct mce_info *mi = mce_find_info();
      int flags = MF_ACTION_REQUIRED;
 
-     if (!mi)
-             mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
-     pfn = mi->paddr >> PAGE_SHIFT;
+     pfn = current->paddr >> PAGE_SHIFT;
 
      clear_thread_flag(TIF_MCE_NOTIFY);
 
-     pr_err("Uncorrected hardware memory error in user-access at %llx",
-              mi->paddr);
+     pr_err("Uncorrected hardware memory error in user-access at %llx\n",
+              current->paddr);
      /*
       * We must call memory_failure() here even if the current process is
       * doomed. We still need to mark the page as poisoned and alert any
       * other users of the page.
       */
-     if (!mi->restartable)
+     if (!current->restartable)
              flags |= MF_MUST_KILL;
      if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {
              pr_err("Memory error not recovered");
              force_sig(SIGBUS, current);
      }
-     mce_clear_info(mi);
 }
+#endif
 
 /*
  * Action optional processing happens here (picking up
arch/x86/kernel/signal.c +1 -1
index 2851d63c1202..28fd70560b96 100644
@@ -735,7 +735,7 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
 {
      user_exit();
 
-#ifdef CONFIG_X86_MCE
+#ifdef CONFIG_MEMORY_FAILURE
      /* notify userspace of pending MCEs */
      if (thread_info_flags & _TIF_MCE_NOTIFY)
              mce_notify_process();
include/linux/sched.h +4 -1
index 857ba40426ba..b41e839b9e1e 100644
@@ -1645,6 +1645,10 @@ struct task_struct {
      unsigned int    sequential_io;
      unsigned int    sequential_io_avg;
 #endif
+#ifdef CONFIG_MEMORY_FAILURE
+     __u64   paddr;
+     int     restartable;
+#endif
 };
 
 /* Future-safe accessor for struct task_struct's cpus_allowed. */
--
Quoted 3 lines

2.0.0.rc2

--
Regards/Gruss,
Boris.

Sent from a fat crate under my desk. Formatting is fine. --

maintainer
#7 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Quoted 3 lines · Andy Lutomirski

The last time I looked at the MCE code, I got a bit lost in the control flow. Is there ever a userspace-killing MCE that's delivered from kernel mode?

Yep, so while you're executing a userspace process, you get an #MC raised which reports an error for which action is required, i.e. look at all those MCE_AR_SEVERITY errors in arch/x86/kernel/cpu/mcheck/mce-severity.c.

It happened within the context of current so we go and run the #MC handler which decides that the process needs to be killed in order to contain the error. So after we exit the handler and before we return to try to sched in the process again on any core, we want to actually kill it and poison all its memory.

Quoted 2 lines

By that, I mean that I think that all userspace-killing MCEs go have user_mode_vm(regs) and go through paranoid_exit.

Yes.

Quoted 5 lines

If so, why do you need to jump through hoops at all? You can't call do_exit, but it should be completely safe to force a fatal signal and let the scheduler and signal code take care of killing the process, right? For that matter, you should also be able to poke at vm structures, etc.

Well, we do that already. memory-failure.c does kill the processes when it decides to.

The only question is whether adding two new members to task_struct is ok. It is nicely convenient and it all falls into place.

In the #MC handler we do:

if (worst == MCE_AR_SEVERITY) {
/* schedule action before return to userland */
+ current->paddr = m.addr;

+			current->restartable = !!(m.mcgstatus & MCG_STATUS_RIPV);
			set_thread_flag(TIF_MCE_NOTIFY);
		}

and then before we return to userspace we do:

+ if (!current->restartable)
flags |= MF_MUST_KILL;
if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {

and the MF_MUST_KILL makes sure memory_failure() does a force_sig().

So I think this is ok, I only think that people might oppose the two new members to task_struct but it looks clean to me this way. IMHO at least.

Quoted 2 lines

Or is there a meaningful case where mce_notify_process needs to help with recovery but the original MCE happened with !user_mode_vm(regs)?

Well, for the !user_mode_vm(regs) case we panic anyway.

Thanks Andy.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#8 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Quoted 44 lines · Borislav Petkov
Quoted 3 lines · Andy Lutomirski

The last time I looked at the MCE code, I got a bit lost in the control flow. Is there ever a userspace-killing MCE that's delivered from kernel mode?

Yep, so while you're executing a userspace process, you get an #MC raised which reports an error for which action is required, i.e. look at all those MCE_AR_SEVERITY errors in arch/x86/kernel/cpu/mcheck/mce-severity.c.

It happened within the context of current so we go and run the #MC handler which decides that the process needs to be killed in order to contain the error. So after we exit the handler and before we return to try to sched in the process again on any core, we want to actually kill it and poison all its memory.

Quoted 2 lines

By that, I mean that I think that all userspace-killing MCEs go have user_mode_vm(regs) and go through paranoid_exit.

Yes.

Quoted 5 lines

If so, why do you need to jump through hoops at all? You can't call do_exit, but it should be completely safe to force a fatal signal and let the scheduler and signal code take care of killing the process, right? For that matter, you should also be able to poke at vm structures, etc.

Well, we do that already. memory-failure.c does kill the processes when it decides to.

The only question is whether adding two new members to task_struct is ok. It is nicely convenient and it all falls into place.

In the #MC handler we do:

                if (worst == MCE_AR_SEVERITY) {
                        /* schedule action before return to userland */
arch/x86/kernel/cpu/mcheck/mce.c +2 -0
+                       current->paddr = m.addr;
+                       current->restartable = !!(m.mcgstatus & MCG_STATUS_RIPV);
                        set_thread_flag(TIF_MCE_NOTIFY);
                }

and then before we return to userspace we do:

Quoted 9 lines

+ if (!current->restartable)

                flags |= MF_MUST_KILL;
        if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {

and the MF_MUST_KILL makes sure memory_failure() does a force_sig().

So I think this is ok, I only think that people might oppose the two new members to task_struct but it looks clean to me this way. IMHO at least.

I think it's okay-ish, but only if it's necessary, and I still don't see why it's necessary.

Can't you just remove TIF_MCE_NOTIFY entirely and just do all the mce_notify_process work directly in do_machine_check? IOW, why do you need to store any state per-task when it's already on the stack anyway.

Or am I missing something here?

--Andy

Quoted 13 lines
Quoted 2 lines

Or is there a meaningful case where mce_notify_process needs to help with recovery but the original MCE happened with !user_mode_vm(regs)?

Well, for the !user_mode_vm(regs) case we panic anyway.

Thanks Andy.

--
Regards/Gruss,
Boris.

Sent from a fat crate under my desk. Formatting is fine. --

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
maintainer
#9 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Quoted 7 lines · Andy Lutomirski

I think it's okay-ish, but only if it's necessary, and I still don't see why it's necessary.

Can't you just remove TIF_MCE_NOTIFY entirely and just do all the mce_notify_process work directly in do_machine_check? IOW, why do you need to store any state per-task when it's already on the stack anyway.

I wish but memory_failure() can't run in #MC context as it noodles quite a lot and grabs all kinds of locks and does a bunch of other atomit-context-unsafe things.

And it needs to run *before* the process is killed as it looks at its pages.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#10 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Quoted 4 lines · Borislav Petkov
Quoted 3 lines · Andy Lutomirski

I think it's okay-ish, but only if it's necessary, and I still don't see why it's necessary.

Quoted 8 lines
Quoted 4 lines

Can't you just remove TIF_MCE_NOTIFY entirely and just do all the mce_notify_process work directly in do_machine_check? IOW, why do you need to store any state per-task when it's already on the stack anyway.

I wish but memory_failure() can't run in #MC context as it noodles quite a lot and grabs all kinds of locks and does a bunch of other atomit-context-unsafe things.

Oh -- does it need to sleep?

I find myself wondering whether a much cleaner solution might be to sync regs and switch stacks before invoking do_machine_check rather than afterwards. Then do_machine_check would really be completely non-atomic. It would add a few lines of asm, though.

--Andy

Quoted 10 lines

And it needs to run *before* the process is killed as it looks at its pages.

--
Regards/Gruss,
Boris.

Sent from a fat crate under my desk. Formatting is fine. --

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
maintainer
#11 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Quoted 1 line · Andy Lutomirski

Oh -- does it need to sleep?

Oh yeah, iterates over all tasks and generally async access to VM stuff. You can have a look if bored: mm/memory-failure.c

:-)

Quoted 4 lines

I find myself wondering whether a much cleaner solution might be to sync regs and switch stacks before invoking do_machine_check rather than afterwards. Then do_machine_check would really be completely non-atomic. It would add a few lines of asm, though.

That's like wagging the dog.

I'd prefer much more to actually have a mechanism to do something to tasks before scheduling them in.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#12 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Quoted 6 lines · Borislav Petkov
Quoted 1 line · Andy Lutomirski

Oh -- does it need to sleep?

Oh yeah, iterates over all tasks and generally async access to VM stuff. You can have a look if bored: mm/memory-failure.c

Quoted 8 lines

:-)

Quoted 4 lines

I find myself wondering whether a much cleaner solution might be to sync regs and switch stacks before invoking do_machine_check rather than afterwards. Then do_machine_check would really be completely non-atomic. It would add a few lines of asm, though.

That's like wagging the dog.

Wagging this particular dog might shake some loose fur off. If we did this for all paranoid entries, then paranoid_userspace could just be deleted. :)

Quoted 3 lines

I'd prefer much more to actually have a mechanism to do something to tasks before scheduling them in.

Like task_work_add?

--Andy

maintainer
#13 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Quoted 3 lines · Andy Lutomirski

Wagging this particular dog might shake some loose fur off. If we did this for all paranoid entries, then paranoid_userspace could just be deleted. :)

:-)

Quoted 4 lines
Quoted 2 lines

I'd prefer much more to actually have a mechanism to do something to tasks before scheduling them in.

Like task_work_add?

Ah, there *is* something like that! I knew we would have something - we always do :-)

Thanks.

/me goes to stare at it.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#14 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

In reply to Andy Lutomirski
Quoted 14 lines · Andy Lutomirski
Quoted 13 lines · Borislav Petkov
Quoted 1 line · Andy Lutomirski

Oh -- does it need to sleep?

Oh yeah, iterates over all tasks and generally async access to VM stuff. You can have a look if bored: mm/memory-failure.c

:-)

Quoted 4 lines

I find myself wondering whether a much cleaner solution might be to sync regs and switch stacks before invoking do_machine_check rather than afterwards. Then do_machine_check would really be completely non-atomic. It would add a few lines of asm, though.

Quoted 5 lines
Quoted 1 line

That's like wagging the dog.

Wagging this particular dog might shake some loose fur off. If we did this for all paranoid entries, then paranoid_userspace could just be deleted. :)

Andi is about to make this dog even messier. I think I like my suggestion more and more.

--Andy

#15 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

In reply to Borislav Petkov

Sorry, I am a bit confused...

Quoted 3 lines · Borislav Petkov

Roughly speaking, we want to be able to mark a task with the sign of death and to kill it, if needed.

"it" is current, yes?

So I agree with Andy, task_work_add() can work and you can also pass paddr/restartable to the handler.

But,

Quoted 2 lines

The important part is *before* it
gets to run again.

But it is already running? Perhaps you meant "before it returns to user-mode" ?

Oleg.

#16 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

Quoted 8 lines · Oleg Nesterov

Sorry, I am a bit confused...

Quoted 3 lines · Borislav Petkov

Roughly speaking, we want to be able to mark a task with the sign of death and to kill it, if needed.

"it" is current, yes?

This part I'm not sure about due to MCE broadcast. "It" is current for exactly one cpu that's in do_machine_check.

Quoted 4 lines

So I agree with Andy, task_work_add() can work and you can also pass paddr/restartable to the handler.

Quoted 8 lines

But,

Quoted 2 lines

The important part is *before* it
gets to run again.

But it is already running? Perhaps you meant "before it returns to user-mode" ?

Right. But killing it in the do_exit sense from do_machine_check is currently impossible because do_machine_check runs on an IST stack. With my patch, if do_machine_check sees user_mode_vm(regs) and CONFIG_X86_64, then it will be running on the real task stack, so it can enable interrupts, kill the task however it wants, etc. This was the original motivation for my patch.

--Andy

maintainer
#17 lore raw

Re: [PATCH v2 4/5] x86/mce: Simplify flow when handling recoverable memory errors

In reply to Oleg Nesterov
Quoted 8 lines · Oleg Nesterov

Sorry, I am a bit confused...

Quoted 3 lines · Borislav Petkov

Roughly speaking, we want to be able to mark a task with the sign of death and to kill it, if needed.

"it" is current, yes?

Yes.

Quoted 2 lines

So I agree with Andy, task_work_add() can work and you can also pass paddr/restartable to the handler.

Yes, and it is much simpler than changing the entry.S glue to switch to the kernel stack for our purposes.

Quoted 7 lines

But,

Quoted 2 lines

The important part is *before* it
gets to run again.

But it is already running? Perhaps you meant "before it returns to user-mode" ?

Right, it runs, while it does so, it triggers an MCE because it touches some corrupted memory, we jump to the #MC handler and before we return to user space, we kill that process.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#18 lore raw

PATCH v2 5/5 x86, MCE: Avoid potential deadlock in MCE context

In reply to Chen, Gong

Printing in MCE context is a no-no, currently, as printk is not NMI-safe. If some of the notifiers on the MCE chain call *printk*, we may deadlock. In order to avoid that, delay printk into process context to fix it.

Background info at: https://lkml.org/lkml/2014/6/27/26

---
 arch/x86/include/asm/mce.h               |  1 +
 arch/x86/kernel/cpu/mcheck/mce-apei.c    |  2 +-
 arch/x86/kernel/cpu/mcheck/mce.c         | 11 ++++++-----
 arch/x86/kernel/cpu/mcheck/mce_intel.c   |  2 +-
 arch/x86/kernel/cpu/mcheck/therm_throt.c |  1 +
 arch/x86/kernel/cpu/mcheck/threshold.c   |  1 +
 6 files changed, 11 insertions(+), 7 deletions(-)
arch/x86/include/asm/mce.h +1 -0
index 76e706767655..be2ec2902830 100644
@@ -187,6 +187,7 @@ enum mcp_flags {
 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b);
 
 int mce_notify_irq(void);
+void mce_queue_irq_work(void);
 #ifdef CONFIG_MEMORY_FAILURE
 void mce_notify_process(void);
 #endif
arch/x86/kernel/cpu/mcheck/mce-apei.c +1 -1
index a1aef9533154..380e3ac8fb62 100644
@@ -57,7 +57,7 @@ void apei_mce_report_mem_error(int severity, struct cper_sec_mem_err *mem_err)
 
 	m.addr = mem_err->physical_addr;
 	mce_log(&m);
-	mce_notify_irq();
+	mce_queue_irq_work();
 }
 EXPORT_SYMBOL_GPL(apei_mce_report_mem_error);
 
arch/x86/kernel/cpu/mcheck/mce.c +6 -5
index b0a25d5f7185..c7ac50d8c4b5 100644
@@ -151,14 +151,11 @@ static struct mce_log mcelog = {
 void mce_log(struct mce *mce)
 {
 	unsigned next, entry;
-	int ret = 0;
 
 	/* Emit the trace record: */
 	trace_mce_record(mce);
 
-	ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
-	if (ret == NOTIFY_STOP)
-		return;
+	mce_genpool_add(mce);
 
 	mce->finished = 0;
 	wmb();
@@ -488,6 +485,11 @@ static void mce_irq_work_cb(struct irq_work *entry)
 	mce_schedule_work();
 }
 
+void mce_queue_irq_work(void)
+{
+	irq_work_queue(&mce_irq_work);
+}
+
 static void mce_report_event(struct pt_regs *regs)
 {
 	if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
@@ -1044,7 +1046,6 @@ void do_machine_check(struct pt_regs *regs, long error_code)
 		/* assuming valid severity level != 0 */
 		m.severity = severity;
 		m.usable_addr = mce_usable_address(&m);
-		mce_genpool_add(&m);
 
 		mce_log(&m);
 
arch/x86/kernel/cpu/mcheck/mce_intel.c +1 -1
index 3bdb95ae8c43..ec2cf614152e 100644
@@ -195,8 +195,8 @@ static void intel_threshold_interrupt(void)
 {
 	if (cmci_storm_detect())
 		return;
+
 	machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
-	mce_notify_irq();
 }
 
 /*
arch/x86/kernel/cpu/mcheck/therm_throt.c +1 -0
index 36a1bb6d1ee0..d100e2bb8ed7 100644
@@ -427,6 +427,7 @@ static inline void __smp_thermal_interrupt(void)
 {
 	inc_irq_stat(irq_thermal_count);
 	smp_thermal_vector();
+	mce_queue_irq_work();
 }
 
 asmlinkage __visible void smp_thermal_interrupt(struct pt_regs *regs)
arch/x86/kernel/cpu/mcheck/threshold.c +1 -0
index 7245980186ee..d695faa234eb 100644
@@ -22,6 +22,7 @@ static inline void __smp_threshold_interrupt(void)
 {
 	inc_irq_stat(irq_threshold_count);
 	mce_threshold_vector();
+	mce_queue_irq_work();
 }
 
 asmlinkage __visible void smp_threshold_interrupt(void)
Signature · 1 line
2.0.0.rc2