From: Jiri Slaby <jslaby@suse.cz>
To: stable@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Andy Lutomirski <luto@kernel.org>,
Borislav Petkov <bp@suse.de>,
Linus Torvalds <torvalds@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@kernel.org>, Jiri Slaby <jslaby@suse.cz>
Subject: [PATCH 3.12 57/82] x86/nmi: Enable nested do_nmi() handling for 64-bit kernels
Date: Mon, 24 Aug 2015 11:09:17 +0200 [thread overview]
Message-ID: <c5fbd6a116a9b3476c75eef1153fd231cc96ebc2.1440407339.git.jslaby@suse.cz> (raw)
In-Reply-To: <be9382caaa4c843d86ce5d107bd41dfcc722d395.1440407339.git.jslaby@suse.cz>
In-Reply-To: <cover.1440407339.git.jslaby@suse.cz>
From: Andy Lutomirski <luto@kernel.org>
3.12-stable review patch. If anyone has any objections, please let me know.
===============
commit 9d05041679904b12c12421cbcf9cb5f4860a8d7b upstream.
32-bit kernels handle nested NMIs in C. Enable the exact same
handling on 64-bit kernels as well. This isn't currently
necessary, but it will become necessary once the asm code starts
allowing limited nesting.
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Reviewed-by: Steven Rostedt <rostedt@goodmis.org>
Cc: Borislav Petkov <bp@suse.de>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
---
arch/x86/kernel/nmi.c | 123 +++++++++++++++++++++-----------------------------
1 file changed, 52 insertions(+), 71 deletions(-)
diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
index 6fcb49ce50a1..b82e0fdc7edb 100644
--- a/arch/x86/kernel/nmi.c
+++ b/arch/x86/kernel/nmi.c
@@ -392,15 +392,15 @@ static __kprobes void default_do_nmi(struct pt_regs *regs)
}
/*
- * NMIs can hit breakpoints which will cause it to lose its
- * NMI context with the CPU when the breakpoint does an iret.
- */
-#ifdef CONFIG_X86_32
-/*
- * For i386, NMIs use the same stack as the kernel, and we can
- * add a workaround to the iret problem in C (preventing nested
- * NMIs if an NMI takes a trap). Simply have 3 states the NMI
- * can be in:
+ * NMIs can hit breakpoints which will cause it to lose its NMI context
+ * with the CPU when the breakpoint or page fault does an IRET.
+ *
+ * As a result, NMIs can nest if NMIs get unmasked due an IRET during
+ * NMI processing. On x86_64, the asm glue protects us from nested NMIs
+ * if the outer NMI came from kernel mode, but we can still nest if the
+ * outer NMI came from user mode.
+ *
+ * To handle these nested NMIs, we have three states:
*
* 1) not running
* 2) executing
@@ -414,15 +414,14 @@ static __kprobes void default_do_nmi(struct pt_regs *regs)
* (Note, the latch is binary, thus multiple NMIs triggering,
* when one is running, are ignored. Only one NMI is restarted.)
*
- * If an NMI hits a breakpoint that executes an iret, another
- * NMI can preempt it. We do not want to allow this new NMI
- * to run, but we want to execute it when the first one finishes.
- * We set the state to "latched", and the exit of the first NMI will
- * perform a dec_return, if the result is zero (NOT_RUNNING), then
- * it will simply exit the NMI handler. If not, the dec_return
- * would have set the state to NMI_EXECUTING (what we want it to
- * be when we are running). In this case, we simply jump back
- * to rerun the NMI handler again, and restart the 'latched' NMI.
+ * If an NMI executes an iret, another NMI can preempt it. We do not
+ * want to allow this new NMI to run, but we want to execute it when the
+ * first one finishes. We set the state to "latched", and the exit of
+ * the first NMI will perform a dec_return, if the result is zero
+ * (NOT_RUNNING), then it will simply exit the NMI handler. If not, the
+ * dec_return would have set the state to NMI_EXECUTING (what we want it
+ * to be when we are running). In this case, we simply jump back to
+ * rerun the NMI handler again, and restart the 'latched' NMI.
*
* No trap (breakpoint or page fault) should be hit before nmi_restart,
* thus there is no race between the first check of state for NOT_RUNNING
@@ -445,49 +444,36 @@ enum nmi_states {
static DEFINE_PER_CPU(enum nmi_states, nmi_state);
static DEFINE_PER_CPU(unsigned long, nmi_cr2);
-#define nmi_nesting_preprocess(regs) \
- do { \
- if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) { \
- this_cpu_write(nmi_state, NMI_LATCHED); \
- return; \
- } \
- this_cpu_write(nmi_state, NMI_EXECUTING); \
- this_cpu_write(nmi_cr2, read_cr2()); \
- } while (0); \
- nmi_restart:
-
-#define nmi_nesting_postprocess() \
- do { \
- if (unlikely(this_cpu_read(nmi_cr2) != read_cr2())) \
- write_cr2(this_cpu_read(nmi_cr2)); \
- if (this_cpu_dec_return(nmi_state)) \
- goto nmi_restart; \
- } while (0)
-#else /* x86_64 */
+#ifdef CONFIG_X86_64
/*
- * In x86_64 things are a bit more difficult. This has the same problem
- * where an NMI hitting a breakpoint that calls iret will remove the
- * NMI context, allowing a nested NMI to enter. What makes this more
- * difficult is that both NMIs and breakpoints have their own stack.
- * When a new NMI or breakpoint is executed, the stack is set to a fixed
- * point. If an NMI is nested, it will have its stack set at that same
- * fixed address that the first NMI had, and will start corrupting the
- * stack. This is handled in entry_64.S, but the same problem exists with
- * the breakpoint stack.
+ * In x86_64, we need to handle breakpoint -> NMI -> breakpoint. Without
+ * some care, the inner breakpoint will clobber the outer breakpoint's
+ * stack.
*
- * If a breakpoint is being processed, and the debug stack is being used,
- * if an NMI comes in and also hits a breakpoint, the stack pointer
- * will be set to the same fixed address as the breakpoint that was
- * interrupted, causing that stack to be corrupted. To handle this case,
- * check if the stack that was interrupted is the debug stack, and if
- * so, change the IDT so that new breakpoints will use the current stack
- * and not switch to the fixed address. On return of the NMI, switch back
- * to the original IDT.
+ * If a breakpoint is being processed, and the debug stack is being
+ * used, if an NMI comes in and also hits a breakpoint, the stack
+ * pointer will be set to the same fixed address as the breakpoint that
+ * was interrupted, causing that stack to be corrupted. To handle this
+ * case, check if the stack that was interrupted is the debug stack, and
+ * if so, change the IDT so that new breakpoints will use the current
+ * stack and not switch to the fixed address. On return of the NMI,
+ * switch back to the original IDT.
*/
static DEFINE_PER_CPU(int, update_debug_stack);
+#endif
-static inline void nmi_nesting_preprocess(struct pt_regs *regs)
+dotraplinkage notrace __kprobes void
+do_nmi(struct pt_regs *regs, long error_code)
{
+ if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) {
+ this_cpu_write(nmi_state, NMI_LATCHED);
+ return;
+ }
+ this_cpu_write(nmi_state, NMI_EXECUTING);
+ this_cpu_write(nmi_cr2, read_cr2());
+nmi_restart:
+
+#ifdef CONFIG_X86_64
/*
* If we interrupted a breakpoint, it is possible that
* the nmi handler will have breakpoints too. We need to
@@ -498,22 +484,8 @@ static inline void nmi_nesting_preprocess(struct pt_regs *regs)
debug_stack_set_zero();
this_cpu_write(update_debug_stack, 1);
}
-}
-
-static inline void nmi_nesting_postprocess(void)
-{
- if (unlikely(this_cpu_read(update_debug_stack))) {
- debug_stack_reset();
- this_cpu_write(update_debug_stack, 0);
- }
-}
#endif
-dotraplinkage notrace __kprobes void
-do_nmi(struct pt_regs *regs, long error_code)
-{
- nmi_nesting_preprocess(regs);
-
nmi_enter();
inc_irq_stat(__nmi_count);
@@ -523,8 +495,17 @@ do_nmi(struct pt_regs *regs, long error_code)
nmi_exit();
- /* On i386, may loop back to preprocess */
- nmi_nesting_postprocess();
+#ifdef CONFIG_X86_64
+ if (unlikely(this_cpu_read(update_debug_stack))) {
+ debug_stack_reset();
+ this_cpu_write(update_debug_stack, 0);
+ }
+#endif
+
+ if (unlikely(this_cpu_read(nmi_cr2) != read_cr2()))
+ write_cr2(this_cpu_read(nmi_cr2));
+ if (this_cpu_dec_return(nmi_state))
+ goto nmi_restart;
}
void stop_nmi(void)
--
2.5.0
next prev parent reply other threads:[~2015-08-24 9:17 UTC|newest]
Thread overview: 91+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-24 9:09 [PATCH 3.12 00/82] 3.12.47-stable review Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 01/82] efi: fix 32bit kernel boot failed problem using efi Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 02/82] futex: Fix a race condition between REQUEUE_PI and task death Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 03/82] HID: usbhid: add Chicony/Pixart usb optical mouse that needs QUIRK_ALWAYS_POLL Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 04/82] mm: avoid setting up anonymous pages into file mapping Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 05/82] freeing unlinked file indefinitely delayed Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 06/82] s390/sclp: clear upper register halves in _sclp_print_early Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 07/82] ARC: make sure instruction_pointer() returns unsigned value Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 08/82] genirq: Prevent resend to interrupts marked IRQ_NESTED_THREAD Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 09/82] ALSA: usb-audio: Add MIDI support for Steinberg MI2/MI4 Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 10/82] ALSA: usb-audio: add dB range mapping for some devices Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 11/82] ALSA: hda - Fix MacBook Pro 5,2 quirk Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 12/82] st: null pointer dereference panic caused by use after kref_put by st_open Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 13/82] mac80211: clear subdir_stations when removing debugfs Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 14/82] mmc: sdhci-esdhc: Make 8BIT bus work Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 15/82] mmc: sdhci-pxav3: fix platform_data is not initialized Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 16/82] md/raid1: fix test for 'was read error from last working device' Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 17/82] tile: use free_bootmem_late() for initrd Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 18/82] Input: usbtouchscreen - avoid unresponsive TSC-30 touch screen Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 19/82] blkcg: fix gendisk reference leak in blkg_conf_prep() Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 20/82] ata: pmp: add quirk for Marvell 4140 SATA PMP Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 21/82] usb-storage: ignore ZTE MF 823 card reader in mode 0x1225 Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 22/82] xhci: Calculate old endpoints correctly on device reset Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 23/82] xhci: report U3 when link is in resume state Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 24/82] xhci: prevent bus_suspend if SS port resuming in phase 1 Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 25/82] xhci: do not report PLC when link is in internal resume state Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 26/82] rds: rds_ib_device.refcount overflow Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 27/82] vhost: actually track log eventfd file Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 28/82] iscsi-target: Fix use-after-free during TPG session shutdown Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 29/82] iscsi-target: Fix iser explicit logout TX kthread leak Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 30/82] 3w-xxxx: fix mis-aligned struct accesses Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 31/82] hwrng: via-rng - Mark device ID table as __maybe_unused Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 32/82] ARM: realview: fix sparsemem build Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 33/82] MIPS: Fix sched_getaffinity with MT FPAFF enabled Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 34/82] MIPS: Make set_pte() SMP safe Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 35/82] fsnotify: fix oops in fsnotify_clear_marks_by_group_flags() Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 36/82] drm/radeon/combios: add some validation of lvds values Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 37/82] ipr: Fix locking for unit attention handling Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 38/82] ipr: Fix incorrect trace indexing Jiri Slaby
2015-08-24 9:08 ` [PATCH 3.12 39/82] ipr: Fix invalid array indexing for HRRQ Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 40/82] xhci: fix off by one error in TRB DMA address boundary check Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 41/82] USB: sierra: add 1199:68AB device ID Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 42/82] ima: add support for new "euid" policy condition Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 43/82] ima: extend "mask" policy matching support Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 44/82] ipmi: fix timeout calculation when bmc is disconnected Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 45/82] sparc64: Fix userspace FPU register corruptions Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 46/82] md: use kzalloc() when bitmap is disabled Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 47/82] ASoC: pcm1681: Fix setting de-emphasis sampling rate selection Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 48/82] x86/xen: Probe target addresses in set_aliased_prot() before the hypercall Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 49/82] xen/gntdevt: Fix race condition in gntdev_release() Jiri Slaby
2015-08-25 11:35 ` Luis Henriques
2015-08-25 11:52 ` Marek Marczykowski-Górecki
2015-08-25 13:18 ` Jiri Slaby
2015-08-25 14:08 ` Marek Marczykowski-Górecki
2015-08-27 7:59 ` Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 50/82] crypto: ixp4xx - Remove bogus BUG_ON on scattered dst buffer Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 51/82] ARM: OMAP2+: hwmod: Fix _wait_target_ready() for hwmods without sysc Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 52/82] iscsi-target: Fix iscsit_start_kthreads failure OOPs Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 53/82] ALSA: hda - fix cs4210_spdif_automute() Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 54/82] ipc: modify message queue accounting to not take kernel data structures into account Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 55/82] ocfs2: fix BUG in ocfs2_downconvert_thread_do_work() Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 56/82] md/raid1: extend spinlock to protect raid1_end_read_request against inconsistencies Jiri Slaby
2015-08-24 9:09 ` Jiri Slaby [this message]
2015-08-24 9:09 ` [PATCH 3.12 58/82] x86/nmi/64: Remove asm code that saves CR2 Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 59/82] x86/nmi/64: Switch stacks on userspace NMI entry Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 60/82] arch: Introduce smp_load_acquire(), smp_store_release() Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 61/82] rcu: Provide counterpart to rcu_dereference() for non-RCU situations Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 62/82] rcu: Move lockless_dereference() out of rcupdate.h Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 63/82] x86/ldt: Make modify_ldt synchronous Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 64/82] x86/ldt: Correct LDT access in single stepping logic Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 65/82] x86/ldt: Correct FPU emulation access to LDT Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 66/82] x86/ldt: Further fix FPU emulation Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 67/82] signalfd: fix information leak in signalfd_copyinfo Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 68/82] signal: fix information leak in copy_siginfo_to_user Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 69/82] signal: fix information leak in copy_siginfo_from_user32 Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 70/82] path_openat(): fix double fput() Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 71/82] md/bitmap: return an error when bitmap superblock is corrupt Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 72/82] mm, vmscan: Do not wait for page writeback for GFP_NOFS allocations Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 73/82] ipc/sem.c: update/correct memory barriers Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 74/82] ipc,sem: fix use after free on IPC_RMID after a task using same semaphore set exits Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 75/82] mm/hwpoison: fix page refcount of unknown non LRU page Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 76/82] xen-blkfront: don't add indirect pages to list when !feature_persistent Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 77/82] perf: Fix fasync handling on inherited events Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 78/82] dm thin metadata: delete btrees when releasing metadata snapshot Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 79/82] localmodconfig: Use Kbuild files too Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 80/82] EDAC, ppc4xx: Access mci->csrows array elements properly Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 81/82] drm/radeon: add new OLAND pci id Jiri Slaby
2015-08-24 9:09 ` [PATCH 3.12 82/82] rbd: fix copyup completion race Jiri Slaby
2015-08-24 16:09 ` [PATCH 3.12 00/82] 3.12.47-stable review Guenter Roeck
2015-08-27 8:10 ` Jiri Slaby
2015-08-24 23:36 ` Shuah Khan
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=c5fbd6a116a9b3476c75eef1153fd231cc96ebc2.1440407339.git.jslaby@suse.cz \
--to=jslaby@suse.cz \
--cc=bp@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=stable@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.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
Powered by JetHome