From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Ingo Molnar <mingo@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
"H. Peter Anvin" <hpa@zytor.com>,
Andy Lutomirski <luto@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Andy Lutomirski <luto@amacapital.net>
Subject: [PATCH 2/2] x86/nmi: Fix and optimize the NMI stack check code
Date: Thu, 09 Mar 2017 17:42:06 -0500 [thread overview]
Message-ID: <20170309224447.852428776@goodmis.org> (raw)
In-Reply-To: 20170309224204.066497548@goodmis.org
[-- Attachment #1: 0002-x86-nmi-Fix-and-optimize-the-NMI-stack-check-code.patch --]
[-- Type: text/plain, Size: 2282 bytes --]
From: "Steven Rostedt (Red Hat)" <rostedt@goodmis.org>
Andy Lutomirski reported an off by one in the NMI stack check
for the nested NMI code, where if the stack pointer was one above
the actual stack (stack start + STACK_SIZE) it would trigger a false
positive. This is not that big of a deal because the stack pointer
should never be that. Even if a stack was using the pages just
above the NMI stack, it would require the stack about to overflow
for this to trigger, which is a much bigger bug than this is fixing.
Also, Linus Torvalds pointed out that doing two compares can be
accomplish with a single compare. That is:
("reg" is top of stack we are comparing "stack" to)
cmpq reg, stack
jae label // note, code had one off "ja" instead of "jae"
subq size, reg
cmpq reg, stack
jb label
Is the same as:
subq $1, reg
subq stack, reg
cmpq size, reg
jae label
The subq $1 was added into the leaq by doing:
leaq 5*8+7(%rsp), %rdx
Added more comments as well.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Inspired-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
arch/x86/entry/entry_64.S | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index 3aad759aace2..1e6ca3740762 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -1355,16 +1355,17 @@ ENTRY(nmi)
* if it controls the kernel's RSP. We set DF before we clear
* "NMI executing".
*/
- lea 6*8(%rsp), %rdx
- /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
- cmpq %rdx, 4*8(%rsp)
- /* If the stack pointer is above the NMI stack, this is a normal NMI */
- ja first_nmi
-
- subq $EXCEPTION_STKSZ, %rdx
- cmpq %rdx, 4*8(%rsp)
- /* If it is below the NMI stack, it is a normal NMI */
- jb first_nmi
+
+ /* Load address of the top of this stack into rdx */
+ lea 5*8+7(%rsp), %rdx
+ /* Subtract the return stack pointer from it */
+ subq 4*8(%rsp), %rdx
+ /*
+ * If the result is greater or equal to the stack size,
+ * then the return stack was not on this stack.
+ */
+ cmpq $EXCEPTION_STKSZ, %rdx
+ jae first_nmi
/* Ah, it is within the NMI stack. */
--
2.10.2
next prev parent reply other threads:[~2017-03-09 22:44 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-09 22:42 [PATCH 0/2] x86/nmi: Optimize address compares with better jump algorithm Steven Rostedt
2017-03-09 22:42 ` [PATCH 1/2] x86/nmi: Optimize the check for being in the repeat_nmi code Steven Rostedt
2017-03-10 2:42 ` Andy Lutomirski
2017-03-10 3:49 ` Steven Rostedt
2017-03-10 3:50 ` Andy Lutomirski
2017-03-10 7:20 ` Ingo Molnar
2017-03-10 19:00 ` Linus Torvalds
2017-03-10 19:03 ` Andy Lutomirski
2017-03-09 22:42 ` Steven Rostedt [this message]
2017-03-10 2:43 ` [PATCH 2/2] x86/nmi: Fix and optimize the NMI stack check code Andy Lutomirski
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=20170309224447.852428776@goodmis.org \
--to=rostedt@goodmis.org \
--cc=akpm@linux-foundation.org \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=luto@kernel.org \
--cc=mingo@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