Re: [PATCH 2/2] x86/nmi: Fix and optimize the NMI stack check code

From: Andy Lutomirski
Date: Thu Mar 09 2017 - 21:44:26 EST


On Thu, Mar 9, 2017 at 2:42 PM, Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:
> From: "Steven Rostedt (Red Hat)" <rostedt@xxxxxxxxxxx>
>
> 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.

Nice.