[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 2/3] x86/alternatives: Rework get_ideal_nops()


  • To: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Mon, 2 Jun 2025 11:57:32 +0200
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Roger Pau Monné <roger.pau@xxxxxxxxxx>, Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Mon, 02 Jun 2025 09:57:33 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 22.05.2025 17:00, Andrew Cooper wrote:
> The {k8,p6}_nops[] arrays are both 80-byte structures indexing 45-byte
> structures.  Furthermore, perhaps unusually for C, the source layout is an
> obvious hint about the trangular nature of the structure.
> 
> Therefore, we can replace the pointer chase with some simple arithmatic.
> 
> No functional change.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
> ---
> CC: Jan Beulich <JBeulich@xxxxxxxx>
> CC: Roger Pau Monné <roger.pau@xxxxxxxxxx>
> 
> The implemenation of get_ideal_nops() changes from:
> 
>     mov    0x19bc41(%rip),%rax        # <ideal_nops>
>     mov    %edi,%edi
>     mov    (%rax,%rdi,8),%rax
>     jmp    <__x86_return_thunk>
> 
> to:
> 
>     lea    -0x1(%rdi),%eax
>     imul   %edi,%eax
>     shr    %eax
>     add    0x67fc1(%rip),%rax        # <ideal_nops>
>     jmp    <__x86_return_thunk>
> 
> The imul has a latency of 3 cycles on all CPUs back to the K8 and Nehalem.
> It's better than an extra deference on all CPUs, even the older ones.

While this is all good, what we're losing is ...

> --- a/xen/arch/x86/alternative.c
> +++ b/xen/arch/x86/alternative.c
> @@ -20,7 +20,7 @@
>  #define MAX_PATCH_LEN (255-1)
>  
>  #ifdef K8_NOP1
> -static const unsigned char k8nops[] init_or_livepatch_const = {
> +static const unsigned char k8_nops[] init_or_livepatch_const = {
>      K8_NOP1,
>      K8_NOP2,
>      K8_NOP3,
> @@ -31,22 +31,10 @@ static const unsigned char k8nops[] 
> init_or_livepatch_const = {
>      K8_NOP8,
>      K8_NOP9,
>  };
> -static const unsigned char * const k8_nops[ASM_NOP_MAX+1] 
> init_or_livepatch_constrel = {

... the (at least visual) connection to ASM_NOP_MAX. Could I talk you into
adding build time array-size checks for both arrays, to restore the
connection?

> -    NULL,
> -    k8nops,
> -    k8nops + 1,
> -    k8nops + 1 + 2,
> -    k8nops + 1 + 2 + 3,
> -    k8nops + 1 + 2 + 3 + 4,
> -    k8nops + 1 + 2 + 3 + 4 + 5,
> -    k8nops + 1 + 2 + 3 + 4 + 5 + 6,
> -    k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
> -    k8nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
> -};
>  #endif
>  
>  #ifdef P6_NOP1
> -static const unsigned char p6nops[] init_or_livepatch_const = {
> +static const unsigned char p6_nops[] init_or_livepatch_const = {
>      P6_NOP1,
>      P6_NOP2,
>      P6_NOP3,
> @@ -57,21 +45,9 @@ static const unsigned char p6nops[] 
> init_or_livepatch_const = {
>      P6_NOP8,
>      P6_NOP9,
>  };
> -static const unsigned char * const p6_nops[ASM_NOP_MAX+1] 
> init_or_livepatch_constrel = {
> -    NULL,
> -    p6nops,
> -    p6nops + 1,
> -    p6nops + 1 + 2,
> -    p6nops + 1 + 2 + 3,
> -    p6nops + 1 + 2 + 3 + 4,
> -    p6nops + 1 + 2 + 3 + 4 + 5,
> -    p6nops + 1 + 2 + 3 + 4 + 5 + 6,
> -    p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
> -    p6nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
> -};
>  #endif
>  
> -static const unsigned char * const *ideal_nops init_or_livepatch_data = 
> p6_nops;
> +static const unsigned char *ideal_nops init_or_livepatch_data = p6_nops;
>  
>  #ifdef HAVE_AS_NOPS_DIRECTIVE
>  
> @@ -86,9 +62,17 @@ static bool init_or_livepatch_read_mostly 
> toolchain_nops_are_ideal;
>  # define toolchain_nops_are_ideal false
>  #endif
>  
> +/*
> + * Both k8_nops[] and p6_nops[] are flattened triangular data structures,
> + * making the offsets easy to calculate.
> + *
> + * To get the start of NOP $N, we want to calculate T($N - 1)
> + */
>  static const unsigned char *get_ideal_nops(unsigned int noplen)
>  {
> -    return ideal_nops[noplen];
> +    unsigned int offset = ((noplen - 1) * noplen) / 2;
> +
> +    return &ideal_nops[offset];
>  }
>  
>  static void __init arch_init_ideal_nops(void)




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.