Re: [PATCH v2 10/11] x86/alternatives: Simplify ALTERNATIVE_n()

From: Peter Zijlstra
Date: Thu Sep 07 2023 - 11:39:17 EST


On Thu, Sep 07, 2023 at 01:09:17PM +0200, Peter Zijlstra wrote:

> Anyway, the 1,3,2 variant spelled out reads like:
>
> #APP
> # 1563 "../arch/x86/kernel/alternative.c" 1
> # ALT: oldnstr
> 661:
> # ALT: oldnstr
> 661:
> push %rbp
> 662:
> # ALT: padding
> .skip -(((665f-664f)-(662b-661b)) > 0) * ((665f-664f)-(662b-661b)),0x90
>
> # Which evaluates like:
> # 665f-664f = 3
> # 662b-661b = 1
> # 3-1 > 0 = -1
> # --1 * (3-1) = 2
> #
> # so two single byte nops get emitted here.
>
> 663:
> .pushsection .altinstructions,"a"
> .long 661b - .
> .long 664f - .
> .4byte ( 3*32+21)
> .byte 663b-661b
> .byte 665f-664f
> .popsection
> .pushsection .altinstr_replacement, "ax"
> # ALT: replacement
> 664:
> mov %rsp,%rbp
> 665:
> .popsection
>
> 662:
> # ALT: padding
> .skip -(((665f-664f)-(662b-661b)) > 0) * ((665f-664f)-(662b-661b)),0x90
>
> # And this evaluates to:
> # 665f-664f = 2
> # 662b-661b = 3 (because it includes the original 1 byte instruction and 2 bytes padding)
> # 3-1 > 0 = 0
> # 0 * (3-1) = 0

copy-paste fail, that needs to read:

3-3 > 0 = 0
0 * (3-3) = 0

> #
> # so no extra padding
>
> 663:
> .pushsection .altinstructions,"a"
> .long 661b - .
> .long 664f - .
> .4byte ( 3*32+21)
> .byte 663b-661b
> .byte 665f-664f
> .popsection
> .pushsection .altinstr_replacement, "ax"
> # ALT: replacement
> 664:
> push %r12
> 665:
> .popsection
>
> # 0 "" 2
> # ../arch/x86/kernel/alternative.c:1569: int3_selftest();
> #NO_APP