linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Andi Kleen <andi@firstfloor.org>
To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org
Subject: [PATCH] [15/16] x86: MCE: Support action-optional machine checks
Date: Tue,  7 Apr 2009 17:10:13 +0200 (CEST)	[thread overview]
Message-ID: <20090407151013.17B2B1D046F@basil.firstfloor.org> (raw)
In-Reply-To: <20090407509.382219156@firstfloor.org>


Newer Intel CPUs support a new class of machine checks called recoverable
action optional.

Action Optional means that the CPU detected some form of corruption in
the background and tells the OS about using a machine check
exception. The OS can then take appropiate action, like killing the
process with the corrupted data or logging the event properly to disk.

This is done by the new generic high level memory failure handler added in a
earlier patch. The high level handler takes the address with the failed
memory and does the appropiate action, like killing the process.

The high level handler cannot be directly called from the machine check 
exception though, because it has to run in a defined process context to be able
to sleep when taking VM locks (it is not expected to sleep for a long time,
just do so in some exceptional cases like lock contention) 

Thus the MCE handler has to queue a work item for process context,
trigger process context and then call the high level handler from there.

This patch adds two path to process context: through a per thread kernel exit
notify_user() callback or through a high priority work item.  The first
runs when the process exits back to user space, the other when it goes
to sleep and there is no higher priority process. 

The machine check handler will schedule both, and whoever runs first
will grab the event. This is done because quick reaction to this 
event is critical to avoid a potential more fatal machine check
when the corruption is consumed.

There is a simple lock less ring buffer to queue the corrupted
addresses between the exception handler and the process context handler.
Then in process context it just calls the high level VM code with 
the corrupted PFNs.

The code adds the required code to extract the failed address from
the CPU's machine check registers. It doesn't try to handle all 
possible cases -- the specification has 6 different ways to specify
memory address -- but only the linear address.

Most of the required checking has been already done earlier in the
mce_severity rule checking engine.  Following the Intel
recommendations Action Optional errors are only enabled for known
situations (encoded in MCACODs). The errors are ignored otherwise,
because they are action optional.

Signed-off-by: Andi Kleen <ak@linux.intel.com>

---
 arch/x86/Kconfig                          |    1 
 arch/x86/include/asm/irq_vectors.h        |    1 
 arch/x86/include/asm/mce.h                |    1 
 arch/x86/kernel/cpu/mcheck/mce-severity.c |    8 +-
 arch/x86/kernel/cpu/mcheck/mce_64.c       |  114 ++++++++++++++++++++++++++++++
 arch/x86/kernel/signal.c                  |    2 
 6 files changed, 125 insertions(+), 2 deletions(-)

Index: linux/arch/x86/kernel/cpu/mcheck/mce_64.c
===================================================================
--- linux.orig/arch/x86/kernel/cpu/mcheck/mce_64.c	2009-04-07 16:39:39.000000000 +0200
+++ linux/arch/x86/kernel/cpu/mcheck/mce_64.c	2009-04-07 16:39:39.000000000 +0200
@@ -14,6 +14,7 @@
 #include <linux/sched.h>
 #include <linux/string.h>
 #include <linux/rcupdate.h>
+#include <linux/mm.h>
 #include <linux/kallsyms.h>
 #include <linux/sysdev.h>
 #include <linux/miscdevice.h>
@@ -79,6 +80,8 @@
 	[0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
 };
 
+static DEFINE_PER_CPU(struct work_struct, mce_work);
+
 /* Do initial initialization of a struct mce */
 void mce_setup(struct mce *m)
 {
@@ -273,6 +276,52 @@
 	wrmsrl(msr, v);
 }
 
+/*
+ * Simple lockless ring to communicate PFNs from the exception handler with the
+ * process context work function. This is vastly simplified because there's
+ * only a single reader and a single writer.
+ */
+#define MCE_RING_SIZE 16	/* we use one entry less */
+
+struct mce_ring {
+	unsigned short start;
+	unsigned short end;
+	unsigned long ring[MCE_RING_SIZE];
+};
+static DEFINE_PER_CPU(struct mce_ring, mce_ring);
+
+static int mce_ring_empty(void)
+{
+	struct mce_ring *r = &__get_cpu_var(mce_ring);
+
+	return r->start == r->end;
+}
+
+static int mce_ring_get(unsigned long *pfn)
+{
+	struct mce_ring *r = &__get_cpu_var(mce_ring);
+
+	if (r->start == r->end)
+		return 0;
+	*pfn = r->ring[r->start];
+	r->start = (r->start + 1) % MCE_RING_SIZE;
+	return 1;
+}
+
+static int mce_ring_add(unsigned long pfn)
+{
+	struct mce_ring *r = &__get_cpu_var(mce_ring);
+	unsigned next;
+
+	next = (r->end + 1) % MCE_RING_SIZE;
+	if (next == r->start)
+		return -1;
+	r->ring[r->end] = pfn;
+	wmb();
+	r->end = next;
+	return 0;
+}
+
 int mce_available(struct cpuinfo_x86 *c)
 {
 	if (mce_dont_init)
@@ -293,6 +342,15 @@
 		m->ip = mce_rdmsrl(rip_msr);
 }
 
+static void mce_schedule_work(void)
+{
+	if (!mce_ring_empty()) {
+		struct work_struct *work = &__get_cpu_var(mce_work);
+		if (!work_pending(work))
+			schedule_work(work);
+	}
+}
+
 /*
  * Called after interrupts have been reenabled again
  * when a MCE happened during an interrupts off region
@@ -304,6 +362,7 @@
 	exit_idle();
 	irq_enter();
 	mce_notify_irq();
+	mce_schedule_work();
 	irq_exit();
 }
 
@@ -311,6 +370,13 @@
 {
 	if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
 		mce_notify_irq();
+		/*
+		 * Triggering the work queue here is just an insurance
+		 * policy in case the syscall exit notify handler
+		 * doesn't run soon enough or ends up running on the
+		 * wrong CPU (can happen when audit sleeps)
+		 */
+		mce_schedule_work();
 		return;
 	}
 
@@ -669,6 +735,23 @@
 	return ret;
 }
 
+/*
+ * Check if the address reported by the CPU is in a format we can parse.
+ * It would be possible to add code for most other cases, but all would
+ * be somewhat complicated (e.g. segment offset would require an instruction
+ * parser). So only support physical addresses upto page granuality for now.
+ */
+static int mce_usable_address(struct mce *m)
+{
+	if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
+		return 0;
+	if ((m->misc & 0x3f) > PAGE_SHIFT)
+		return 0;
+	if (((m->misc >> 6) & 7) != MCM_ADDR_PHYS)
+		return 0;
+	return 1;
+}
+
 static void mce_clear_state(unsigned long *toclear)
 {
 	int i;
@@ -802,6 +885,16 @@
 		if (m.status & MCI_STATUS_ADDRV)
 			m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
 
+		/*
+		 * Action optional error. Queue address for later processing.
+		 * When the ring overflows we just ignore the AO error.
+		 * RED-PEN add some logging mechanism when
+		 * usable_address or mce_add_ring fails.
+		 * RED-PEN don't ignore overflow for tolerant == 0
+		 */
+		if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
+			mce_ring_add(m.addr >> PAGE_SHIFT);
+
 		mce_get_rip(&m, regs);
 		mce_log(&m);
 
@@ -852,6 +945,26 @@
 }
 EXPORT_SYMBOL_GPL(do_machine_check);
 
+/*
+ * Called after mce notification in process context. This code
+ * is allowed to sleep. Call the high level VM handler to process
+ * any corrupted pages.
+ * Assume that the work queue code only calls this one at a time
+ * per CPU.
+ */
+void mce_notify_process(void)
+{
+	unsigned long pfn;
+	mce_notify_irq();
+	while (mce_ring_get(&pfn))
+		memory_failure(pfn, MCE_VECTOR);
+}
+
+static void mce_process_work(struct work_struct *dummy)
+{
+	mce_notify_process();
+}
+
 #ifdef CONFIG_X86_MCE_INTEL
 /***
  * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
@@ -1088,6 +1201,7 @@
 	mce_init();
 	mce_cpu_features(c);
 	mce_init_timer();
+	INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
 }
 
 /*
Index: linux/arch/x86/include/asm/mce.h
===================================================================
--- linux.orig/arch/x86/include/asm/mce.h	2009-04-07 16:39:39.000000000 +0200
+++ linux/arch/x86/include/asm/mce.h	2009-04-07 16:39:39.000000000 +0200
@@ -163,6 +163,7 @@
 extern void machine_check_poll(enum mcp_flags flags, mce_banks_t *b);
 
 extern int mce_notify_irq(void);
+extern void mce_notify_process(void);
 
 #endif /* !CONFIG_X86_32 */
 
Index: linux/arch/x86/kernel/signal.c
===================================================================
--- linux.orig/arch/x86/kernel/signal.c	2009-04-07 16:39:39.000000000 +0200
+++ linux/arch/x86/kernel/signal.c	2009-04-07 16:39:39.000000000 +0200
@@ -860,7 +860,7 @@
 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
 	/* notify userspace of pending MCEs */
 	if (thread_info_flags & _TIF_MCE_NOTIFY)
-		mce_notify_irq();
+		mce_notify_process();
 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
 
 	/* deal with pending signal delivery */
Index: linux/arch/x86/kernel/cpu/mcheck/mce-severity.c
===================================================================
--- linux.orig/arch/x86/kernel/cpu/mcheck/mce-severity.c	2009-04-07 16:39:00.000000000 +0200
+++ linux/arch/x86/kernel/cpu/mcheck/mce-severity.c	2009-04-07 16:39:39.000000000 +0200
@@ -67,7 +67,13 @@
 	     "Action required; unknown MCACOD", SER),
 	MASK(MCI_STATUS_OVER|MCI_UC_SAR, MCI_STATUS_OVER|MCI_UC_SAR, PANIC,
 	     "Action required with lost events", SER),
-	/* AO add known MCACODs here */
+
+	/* known AO MCACODs: handle by calling high level handler */
+	MASK(MCI_UC_SAR|0xfff0, MCI_UC_S|0xc0, AO,
+	     "Action optional: memory scrubbing error", SER),
+	MASK(MCI_UC_SAR|MCACOD, MCI_UC_S|0x17a, AO,
+	     "Action optional: last level cache writeback error", SER),
+
 	MASK(MCI_STATUS_OVER|MCI_UC_SAR, MCI_UC_S, SOME,
 	     "Action optional unknown MCACOD", SER),
 	MASK(MCI_STATUS_OVER|MCI_UC_SAR, MCI_UC_S|MCI_STATUS_OVER, SOME,
Index: linux/arch/x86/include/asm/irq_vectors.h
===================================================================
--- linux.orig/arch/x86/include/asm/irq_vectors.h	2009-04-07 16:39:00.000000000 +0200
+++ linux/arch/x86/include/asm/irq_vectors.h	2009-04-07 16:39:39.000000000 +0200
@@ -25,6 +25,7 @@
  */
 
 #define NMI_VECTOR			0x02
+#define MCE_VECTOR			0x12
 
 /*
  * IDT vectors usable for external interrupt sources start
Index: linux/arch/x86/Kconfig
===================================================================
--- linux.orig/arch/x86/Kconfig	2009-04-07 16:39:00.000000000 +0200
+++ linux/arch/x86/Kconfig	2009-04-07 16:39:39.000000000 +0200
@@ -760,6 +760,7 @@
 
 config X86_MCE
 	bool "Machine Check Exception"
+	select MEMORY_FAILURE
 	---help---
 	  Machine Check Exception support allows the processor to notify the
 	  kernel if it detects a problem (e.g. overheating, component failure).

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  parent reply	other threads:[~2009-04-07 15:10 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-07 15:09 [PATCH] [0/16] POISON: Intro Andi Kleen
2009-04-07 15:09 ` [PATCH] [1/16] POISON: Add support for high priority work items Andi Kleen
2009-04-07 15:09 ` [PATCH] [2/16] POISON: Add page flag for poisoned pages Andi Kleen
2009-04-07 21:07   ` Christoph Lameter
2009-04-08  0:29   ` Russ Anderson
2009-04-08  6:26     ` Andi Kleen
2009-04-08  5:14   ` Andrew Morton
2009-04-08  6:24     ` Andi Kleen
2009-04-08  7:00       ` Andrew Morton
2009-04-08  9:38         ` Andi Kleen
2009-04-07 15:09 ` [PATCH] [3/16] POISON: Handle poisoned pages in page free Andi Kleen
2009-04-07 23:21   ` Minchan Kim
2009-04-08  6:51     ` Andi Kleen
2009-04-08  7:39       ` Minchan Kim
2009-04-08  9:41         ` Andi Kleen
2009-04-08 10:05           ` Minchan Kim
2009-04-07 15:10 ` [PATCH] [4/16] POISON: Export some rmap vma locking to outside world Andi Kleen
2009-04-07 15:10 ` [PATCH] [5/16] POISON: Add support for poison swap entries Andi Kleen
2009-04-07 21:11   ` Christoph Lameter
2009-04-07 21:56     ` Andi Kleen
2009-04-07 21:56       ` Christoph Lameter
2009-04-07 22:25         ` Andi Kleen
2009-04-07 15:10 ` [PATCH] [6/16] POISON: Add new SIGBUS error codes for poison signals Andi Kleen
2009-04-07 15:10 ` [PATCH] [7/16] POISON: Add basic support for poisoned pages in fault handler Andi Kleen
2009-05-26 12:55   ` Hidehiro Kawai
2009-05-26 13:18     ` Andi Kleen
2009-04-07 15:10 ` [PATCH] [8/16] POISON: Add various poison checks in mm/memory.c Andi Kleen
2009-04-07 19:03   ` Johannes Weiner
2009-04-07 19:31     ` Andi Kleen
2009-04-07 20:17       ` Johannes Weiner
2009-04-07 20:24         ` Andi Kleen
2009-04-07 20:36           ` Johannes Weiner
2009-04-07 15:10 ` [PATCH] [9/16] POISON: x86: Add VM_FAULT_POISON handling to x86 page fault handler Andi Kleen
2009-04-07 15:10 ` [PATCH] [10/16] POISON: Use bitmask/action code for try_to_unmap behaviour Andi Kleen
2009-04-07 21:19   ` Christoph Lameter
2009-04-07 21:59     ` Andi Kleen
2009-04-07 22:04       ` Christoph Lameter
2009-04-07 22:35         ` Andi Kleen
2009-04-07 15:10 ` [PATCH] [11/16] POISON: Handle poisoned pages in try_to_unmap Andi Kleen
2009-04-07 15:10 ` [PATCH] [12/16] POISON: Handle poisoned pages in set_page_dirty() Andi Kleen
2009-04-07 15:10 ` [PATCH] [13/16] POISON: The high level memory error handler in the VM Andi Kleen
2009-04-07 16:03   ` Rik van Riel
2009-04-07 16:30     ` Andi Kleen
2009-04-07 18:51   ` Johannes Weiner
2009-04-07 19:40     ` Andi Kleen
2009-04-08 17:03   ` Chris Mason
2009-04-09  7:29     ` Andi Kleen
2009-04-09  7:58       ` [PATCH] [13/16] POISON: The high level memory error handler in the VM II Andi Kleen
2009-04-09 13:30         ` Chris Mason
2009-04-09 14:02           ` Andi Kleen
2009-04-09 14:37             ` Chris Mason
2009-04-09 14:57               ` Andi Kleen
2009-04-29  8:16               ` Wu Fengguang
2009-04-29  8:21                 ` btrfs BUG on creating huge sparse file Wu Fengguang
2009-04-29 11:40                   ` Chris Mason
2009-04-29 11:45                     ` Wu Fengguang
2009-04-29  8:36                 ` [PATCH] [13/16] POISON: The high level memory error handler in the VM II Andi Kleen
2009-04-29  9:05                   ` Wu Fengguang
2009-04-29 11:27                     ` Chris Mason
2009-04-07 15:10 ` [PATCH] [14/16] x86: MCE: Rename mce_notify_user to mce_notify_irq Andi Kleen
2009-04-07 15:10 ` Andi Kleen [this message]
2009-04-07 15:10 ` [PATCH] [16/16] POISON: Add madvise() based injector for poisoned data Andi Kleen
2009-04-07 19:13 ` [PATCH] [0/16] POISON: Intro Robin Holt
2009-04-07 19:38   ` Andi Kleen
2009-04-08  5:15 ` Andrew Morton
2009-04-08  6:15   ` Andi Kleen
2009-04-08 17:29     ` Roland Dreier
2009-04-09  7:22       ` Andi Kleen
2009-04-08  5:47 ` Andrew Morton
2009-04-08  6:21   ` Andi Kleen
2009-04-13 13:18   ` Wu Fengguang
2009-05-26 12:50 ` Hidehiro Kawai
2009-05-26 13:29   ` Andi Kleen
2009-05-28  4:37     ` Hidehiro Kawai
2009-05-28  8:00       ` Andi Kleen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090407151013.17B2B1D046F@basil.firstfloor.org \
    --to=andi@firstfloor.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).