Re: [PATCH 1/1] x86/entry: Use one cmpq in NMI entry to check RIP for nested NMIs

From: Petr Tesarik
Date: Tue Mar 12 2024 - 11:04:26 EST


On 2/29/2024 9:37 AM, Petr Tesarik wrote:
> From: Petr Tesarik <petr.tesarik1@xxxxxxxxxxxxxxxxxxx>
>
> Optimize the check whether a nested NMI occurred between repeat_nmi and
> end_repeat_nmi. Although this is not a hot path, this is standard code to
> check whether a value is within a given range; it is slightly faster, takes
> up less bytes of code and saves one entry in the branch predictor.
>
> This patch also removes the only relocation for end_repeat_nmi, removing
> the need for ANNOTATE_NOENDBR.

Any comment on this?

Kind regards
Petr T

> Signed-off-by: Petr Tesarik <petr.tesarik1@xxxxxxxxxxxxxxxxxxx>
> ---
> arch/x86/entry/entry_64.S | 13 ++++---------
> 1 file changed, 4 insertions(+), 9 deletions(-)
>
> diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
> index 9bb485977629..cae40076e109 100644
> --- a/arch/x86/entry/entry_64.S
> +++ b/arch/x86/entry/entry_64.S
> @@ -1251,13 +1251,10 @@ SYM_CODE_START(asm_exc_nmi)
> * the outer NMI.
> */
>
> - movq $repeat_nmi, %rdx
> - cmpq 8(%rsp), %rdx
> - ja 1f
> - movq $end_repeat_nmi, %rdx
> - cmpq 8(%rsp), %rdx
> - ja nested_nmi_out
> -1:
> + movq 8(%rsp), %rdx
> + subq $repeat_nmi, %rdx
> + cmpq $(end_repeat_nmi - repeat_nmi), %rdx
> + jb nested_nmi_out
>
> /*
> * Now check "NMI executing". If it's set, then we're nested.
> @@ -1383,8 +1380,6 @@ repeat_nmi:
> .endr
> subq $(5*8), %rsp
> end_repeat_nmi:
> - ANNOTATE_NOENDBR // this code
> -
> /*
> * Everything below this point can be preempted by a nested NMI.
> * If this happens, then the inner NMI will change the "iret"