From: Peter Zijlstra <peterz@infradead.org>
To: tglx@linutronix.de, luto@amacapital.net, peterz@infradead.org
Cc: linux-kernel@vger.kernel.org, x86@kernel.org,
Lai Jiangshan <laijs@linux.alibaba.com>,
sean.j.christopherson@intel.com, andrew.cooper3@citrix.com,
daniel.thompson@linaro.org
Subject: [PATCH 2/6] x86/entry, nmi: Disable #DB
Date: Thu, 28 May 2020 22:19:39 +0200 [thread overview]
Message-ID: <20200528202328.475098073@infradead.org> (raw)
In-Reply-To: 20200528201937.038455891@infradead.org
Instead of playing stupid games with IST stacks, fully disallow #DB
during NMIs. There is absolutely no reason to allow them, and killing
this saves a heap of trouble.
We already disallow #DB on noinstr and CEA, so we can't get #DB before
this, and this ensures we can't get it after this either.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
arch/x86/kernel/nmi.c | 55 ++------------------------------------------------
1 file changed, 3 insertions(+), 52 deletions(-)
--- a/arch/x86/kernel/nmi.c
+++ b/arch/x86/kernel/nmi.c
@@ -478,40 +478,7 @@ enum nmi_states {
};
static DEFINE_PER_CPU(enum nmi_states, nmi_state);
static DEFINE_PER_CPU(unsigned long, nmi_cr2);
-
-#ifdef CONFIG_X86_64
-/*
- * 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.
- */
-static DEFINE_PER_CPU(int, update_debug_stack);
-
-static noinstr bool is_debug_stack(unsigned long addr)
-{
- struct cea_exception_stacks *cs = __this_cpu_read(cea_exception_stacks);
- unsigned long top = CEA_ESTACK_TOP(cs, DB);
- unsigned long bot = CEA_ESTACK_BOT(cs, DB1);
-
- if (__this_cpu_read(debug_stack_usage))
- return true;
- /*
- * Note, this covers the guard page between DB and DB1 as well to
- * avoid two checks. But by all means @addr can never point into
- * the guard page.
- */
- return addr >= bot && addr < top;
-}
-#endif
+static DEFINE_PER_CPU(unsigned long, nmi_dr7);
DEFINE_IDTENTRY_NMI(exc_nmi)
{
@@ -526,18 +493,7 @@ DEFINE_IDTENTRY_NMI(exc_nmi)
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
- * change the IDT such that breakpoints that happen here
- * continue to use the NMI stack.
- */
- if (unlikely(is_debug_stack(regs->sp))) {
- debug_stack_set_zero();
- this_cpu_write(update_debug_stack, 1);
- }
-#endif
+ local_db_save(this_cpu_ptr(&nmi_dr7));
nmi_enter();
@@ -548,12 +504,7 @@ DEFINE_IDTENTRY_NMI(exc_nmi)
nmi_exit();
-#ifdef CONFIG_X86_64
- if (unlikely(this_cpu_read(update_debug_stack))) {
- debug_stack_reset();
- this_cpu_write(update_debug_stack, 0);
- }
-#endif
+ local_db_restore(*this_cpu_ptr(&nmi_dr7));
if (unlikely(this_cpu_read(nmi_cr2) != read_cr2()))
write_cr2(this_cpu_read(nmi_cr2));
next prev parent reply other threads:[~2020-05-28 20:27 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-28 20:19 [PATCH 0/6] x86/entry: disallow #DB more Peter Zijlstra
2020-05-28 20:19 ` [PATCH 1/6] x86/entry: Introduce local_db_{save,restore}() Peter Zijlstra
2020-05-28 20:52 ` Andrew Cooper
2020-05-28 21:15 ` Peter Zijlstra
2020-05-28 21:33 ` Peter Zijlstra
2020-05-29 17:28 ` Andy Lutomirski
2020-05-29 19:02 ` Peter Zijlstra
2020-05-28 21:36 ` Andrew Cooper
2020-05-28 20:19 ` Peter Zijlstra [this message]
2020-05-28 20:19 ` [PATCH 3/6] x86/entry, mce: Disallow #DB during #MC Peter Zijlstra
2020-05-28 20:19 ` [PATCH 4/6] x86/entry: Optimize local_db_save() for virt Peter Zijlstra
2020-05-29 17:24 ` Sean Christopherson
2020-05-28 20:19 ` [PATCH 5/6] x86/entry: Remove debug IDT frobbing Peter Zijlstra
2020-05-28 20:19 ` [RFC][PATCH 6/6] x86/entry: Remove DBn stacks Peter Zijlstra
2020-05-28 22:35 ` Lai Jiangshan
2020-05-28 22:37 ` Peter Zijlstra
2020-05-28 22:42 ` [PATCH 0/6] x86/entry: disallow #DB more Lai Jiangshan
2020-05-28 22:48 ` Peter Zijlstra
2020-05-28 23:05 ` Lai Jiangshan
2020-05-29 8:00 ` Peter Zijlstra
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=20200528202328.475098073@infradead.org \
--to=peterz@infradead.org \
--cc=andrew.cooper3@citrix.com \
--cc=daniel.thompson@linaro.org \
--cc=laijs@linux.alibaba.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=sean.j.christopherson@intel.com \
--cc=tglx@linutronix.de \
--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).