[PATCH] arm64: crypto: Add an option to assume NEON XOR is the fastest

Ard Biesheuvel ardb at kernel.org
Tue Sep 22 02:25:09 EDT 2020


On Tue, 22 Sep 2020 at 02:27, Douglas Anderson <dianders at chromium.org> wrote:
>
> On every boot time we see messages like this:
>
> [    0.025360] calling  calibrate_xor_blocks+0x0/0x134 @ 1
> [    0.025363] xor: measuring software checksum speed
> [    0.035351]    8regs     :  3952.000 MB/sec
> [    0.045384]    32regs    :  4860.000 MB/sec
> [    0.055418]    arm64_neon:  5900.000 MB/sec
> [    0.055423] xor: using function: arm64_neon (5900.000 MB/sec)
> [    0.055433] initcall calibrate_xor_blocks+0x0/0x134 returned 0 after 29296 usecs
>
> As you can see, we spend 30 ms on every boot re-confirming that, yet
> again, the arm64_neon implementation is the fastest way to do XOR.
> ...and the above is on a system with HZ=1000.  Due to the way the
> testing happens, if we have HZ defined to something slower it'll take
> much longer.  HZ=100 means we spend 300 ms on every boot re-confirming
> a fact that will be the same for every bootup.
>
> Trying to super-optimize the xor operation makes a lot of sense if
> you're using software RAID, but the above is probably not worth it for
> most Linux users because:
> 1. Quite a few arm64 kernels are built for embedded systems where
>    software raid isn't common.  That means we're spending lots of time
>    on every boot trying to optimize something we don't use.
> 2. Presumably, if we have neon, it's faster than alternatives.  If
>    it's not, it's not expected to be tons slower.
> 3. Quite a lot of arm64 systems are big.LITTLE.  This means that the
>    existing test is somewhat misguided because it's assuming that test
>    results on the boot CPU apply to the other CPUs in the system.
>    This is not necessarily the case.
>
> Let's add a new config option that allows us to just use the neon
> functions (if present) without benchmarking.
>
> NOTE: One small side effect is that on an arm64 system _without_ neon
> we'll end up testing the xor_block_8regs_p and xor_block_32regs_p
> versions of the function.  That's presumably OK since we already test
> all those when KERNEL_MODE_NEON is disabled.
>
> ALSO NOTE: presumably the way to do better than this is to add some
> sort of per-CPU-core lookup table and jump to a per-CPU-core-specific
> XOR function each time xor is called.  Without seeing evidence that
> this would really help someone, though, that doesn't seem worth it.
>
> Signed-off-by: Douglas Anderson <dianders at chromium.org>

On the two arm64 machines that I happen to have running right now, I get

SynQuacer (Cortex-A53)

    8regs     :  1917.000 MB/sec
    32regs    :  2270.000 MB/sec
    arm64_neon:  2053.000 MB/sec

ThunderX2

    8regs     : 10170.000 MB/sec
    32regs    : 12051.000 MB/sec
    arm64_neon: 10948.000 MB/sec

so your assertion is not entirely valid.

If the system does not need XOR, it is free not to load the module, so
there is no reason it has to affect the boot time.

What we /can/ do is remove 8regs - arm64 has plenty of registers so I
don't think it will ever be the fastest.



> ---
>
>  arch/arm64/Kconfig           | 15 +++++++++++++++
>  arch/arm64/include/asm/xor.h |  5 +++++
>  2 files changed, 20 insertions(+)
>
> diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
> index 64ae5e4eb814..fc18df45a5f8 100644
> --- a/arch/arm64/Kconfig
> +++ b/arch/arm64/Kconfig
> @@ -306,6 +306,21 @@ config SMP
>  config KERNEL_MODE_NEON
>         def_bool y
>
> +menuconfig FORCE_NEON_XOR_IF_AVAILABLE
> +       bool "Assume neon is fastest for xor if the CPU supports it"
> +       default y
> +       depends on KERNEL_MODE_NEON
> +       help
> +         Normally the kernel will run through several different XOR
> +         algorithms at boot, timing them on the boot processor to see
> +         which is fastest. This can take quite some time. On many
> +         machines it's expected that, if NEON is available, it's going
> +         to provide the fastest implementation. If you set this option
> +         we'll skip testing this every boot and just assume NEON is the
> +         fastest if present. Setting this option will speed up your
> +         boot but you might end up with a less-optimal xor
> +         implementation.
> +
>  config FIX_EARLYCON_MEM
>         def_bool y
>
> diff --git a/arch/arm64/include/asm/xor.h b/arch/arm64/include/asm/xor.h
> index 947f6a4f1aa0..1acb290866ab 100644
> --- a/arch/arm64/include/asm/xor.h
> +++ b/arch/arm64/include/asm/xor.h
> @@ -57,6 +57,10 @@ static struct xor_block_template xor_block_arm64 = {
>         .do_4   = xor_neon_4,
>         .do_5   = xor_neon_5
>  };
> +#ifdef CONFIG_FORCE_NEON_XOR_IF_AVAILABLE
> +#define XOR_SELECT_TEMPLATE(FASTEST) \
> +       (cpu_has_neon() ? &xor_block_arm64 : FASTEST)
> +#else /* ! CONFIG_FORCE_NEON_XOR_IF_AVAILABLE */
>  #undef XOR_TRY_TEMPLATES
>  #define XOR_TRY_TEMPLATES           \
>         do {        \
> @@ -66,5 +70,6 @@ static struct xor_block_template xor_block_arm64 = {
>                         xor_speed(&xor_block_arm64);\
>                 } \
>         } while (0)
> +#endif /* ! CONFIG_FORCE_NEON_XOR_IF_AVAILABLE */
>
>  #endif /* ! CONFIG_KERNEL_MODE_NEON */
> --
> 2.28.0.681.g6f77f65b4e-goog
>
>
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel



More information about the linux-arm-kernel mailing list