Skip to contentSkip to conversation

Introduce AVX512 optimized crypto algorithms

RFC Superseded Megha Dey 4 participants 28 messages Dec 18, 2020 – Jan 21, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Megha Dey

author
#0 lore raw

cover Introduce AVX512 optimized crypto algorithms

Optimize crypto algorithms using VPCLMULQDQ and VAES AVX512 instructions (first implemented on Intel's Icelake client and Xeon CPUs).

These algorithms take advantage of the AVX512 registers to keep the CPU busy and increase memory bandwidth utilization. They provide substantial (2-10x) improvements over existing crypto algorithms when update data size is greater than 128 bytes and do not have any significant impact when used on small amounts of data.

However, these algorithms may also incur a frequency penalty and cause collateral damage to other workloads running on the same core(co-scheduled threads). These frequency drops are also known as bin drops where 1 bin drop is around 100MHz. With the SpecCPU and ffmpeg benchmark, a 0-1 bin drop(0-100MHz) is observed on Icelake desktop and 0-2 bin drops (0-200Mhz) are observed on the Icelake server.

The AVX512 optimization are disabled by default to avoid impact on other workloads. In order to use these optimized algorithms:
1. At compile time:
a. User must enable CONFIG_CRYPTO_AVX512 option
b. Toolchain(assembler) must support VAES or VPCLMULQDQ instructions
2. At run time:
a. User must set module parameter use_avx512 at boot time (<module_name>.use_avx512 = 1) or post boot using sysfs (echo 1 > /sys/module/<module_name>/parameters/use_avx512). (except for aesni ctr and gcm which require boot time initialization because of commit 0fbafd06bdde ("crypto: aesni - fix failing setkey for rfc4106-gcm-aesni")
b. Platform must support VPCLMULQDQ or VAES features

N.B. It is unclear whether these coarse grain controls(global module parameter) would meet all user needs. Perhaps some per-thread control might be useful? Looking for guidance here.

Other implementations of these crypto algorithms are possible, which would result in lower crypto performance but would not cause collateral damage from frequency drops (AVX512L vs AVX512VL).

The following crypto algorithms are optimized using AVX512 registers:

1. "by16" implementation of T10 Data Integrity Field CRC16 (CRC T10 DIF) The "by16" means the main loop processes 256 bytes (16 * 16 bytes) at a time in CRC T10 DIF calculation. This algorithm is optimized using the VPCLMULQDQ instruction which is the encoded 512 bit version of PCLMULQDQ instruction. On an Icelake desktop, with constant frequency set, the "by16" CRC T10 DIF AVX512 optimization shows about 1.5X improvement when the bytes per update size is 1KB or above as measured by the tcrypt module.

2. GHASH computations with vectorized instruction.
VPCLMULQDQ instruction is used to accelerate the most time-consuming part of GHASH, carry-less multiplication. For best parallelism and deeper out of order execution, the main loop of the code works on 16x16 byte blocks at a time and performs reduction every 48 x 16 byte blocks. Optimized GHASH computations show a 4x to 10x speedup when the bytes per update is 256B or above.

3. "by16" implementation of the AES CTR mode using VAES instructions "by16" means that 16 independent blocks (each 128 bits) can be ciphered simultaneously. On an Icelake desktop, with constant frequency set, the "by16" AES CTR mode shows about 2X improvement when the bytes per update size is 256B or above as measured by the tcrypt module.

4. AES GCM using VPCLMULQDQ instructions
Using AVX 512 registers, an average increase of 2X is observed when the bytes per update size is 256B or above as measured by tcrypt module.

Patch 1 checks for assembler support for VPCLMULQDQ instruction Patch 2 introduces CRC T10 DIF calculation with VPCLMULQDQ instructions Patch 3 introduces optimized GHASH computation with VPCLMULQDQ instructions Patch 4 adds new speed test for optimized GHASH computations Patch 5 introduces "by 16" version of AES CTR mode using VAES instructions Patch 6 fixes coding style in existing if else block
Patch 7 introduces the AES GCM mode using VPCLMULQDQ instructions

Complex sign off chain in patches 2 and 3. Original implementation (non kernel) was done by Intel's IPsec team. Kyung Min Park is the author of Patch 2 and co-author of patch 3 along with me.

Also, most of this code is related to crypto subsystem. X86 mailing list is copied here because of Patch 1.

Kyung Min Park (3):
crypto: crct10dif - Accelerated CRC T10 DIF with vectorized
instruction
crypto: ghash - Optimized GHASH computations
crypto: tcrypt - Add speed test for optimized GHASH computations

Megha Dey (4):
x86: Probe assembler capabilities for VAES and VPLCMULQDQ support
crypto: aesni - AES CTR x86_64 "by16" AVX512 optimization
crypto: aesni - fix coding style for if/else block
crypto: aesni - AVX512 version of AESNI-GCM using VPCLMULQDQ

 arch/x86/Kconfig.assembler                   |   10 +
 arch/x86/crypto/Makefile                     |    4 +
 arch/x86/crypto/aes_ctrby16_avx512-x86_64.S  |  856 ++++++++++++
 arch/x86/crypto/aesni-intel_avx512-x86_64.S  | 1788 ++++++++++++++++++++++++++
 arch/x86/crypto/aesni-intel_glue.c           |  122 +-
 arch/x86/crypto/avx512_vaes_common.S         | 1633 +++++++++++++++++++++++
 arch/x86/crypto/crct10dif-avx512-asm_64.S    |  482 +++++++
 arch/x86/crypto/crct10dif-pclmul_glue.c      |   24 +-
 arch/x86/crypto/ghash-clmulni-intel_avx512.S |   68 +
 arch/x86/crypto/ghash-clmulni-intel_glue.c   |   39 +-
 arch/x86/include/asm/disabled-features.h     |   14 +-
 crypto/Kconfig                               |   59 +
 crypto/tcrypt.c                              |    5 +
 13 files changed, 5091 insertions(+), 13 deletions(-)
 create mode 100644 arch/x86/crypto/aes_ctrby16_avx512-x86_64.S
 create mode 100644 arch/x86/crypto/aesni-intel_avx512-x86_64.S
 create mode 100644 arch/x86/crypto/avx512_vaes_common.S
 create mode 100644 arch/x86/crypto/crct10dif-avx512-asm_64.S
 create mode 100644 arch/x86/crypto/ghash-clmulni-intel_avx512.S
Signature · 1 line
2.7.4
author
#1 lore raw

RFC 1/7 x86: Probe assembler capabilities for VAES and VPLCMULQDQ support

This is a preparatory patch to introduce the optimized crypto algorithms using AVX512 instructions which would require VAES and VPLCMULQDQ support.

Check for VAES and VPCLMULQDQ assembler support using AVX512 registers.

---
 arch/x86/Kconfig.assembler | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/Kconfig.assembler +10 -0
index 26b8c08..9ea0bc8 100644
@@ -1,6 +1,16 @@
 # SPDX-License-Identifier: GPL-2.0
 # Copyright (C) 2020 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
 
+config AS_VAES_AVX512
+	def_bool $(as-instr,vaesenc %zmm0$(comma)%zmm1$(comma)%zmm1) && 64BIT
+	help
+	  Supported by binutils >= 2.30 and LLVM integrated assembler
+
+config AS_VPCLMULQDQ
+	def_bool $(as-instr,vpclmulqdq \$0$(comma)%zmm2$(comma)%zmm6$(comma)%zmm4)
+	help
+	  Supported by binutils >= 2.30 and LLVM integrated assembler
+
 config AS_AVX512
 	def_bool $(as-instr,vpmovm2b %k1$(comma)%zmm5)
 	help
Signature · 1 line
2.7.4
author
#4 lore raw

RFC 2/7 crypto: crct10dif - Accelerated CRC T10 DIF with vectorized instruction

In reply to Megha Dey

From: Kyung Min Park <[email protected]>

Update the crc_pcl function that calculates T10 Data Integrity Field CRC16 (CRC T10 DIF) using VPCLMULQDQ instruction. VPCLMULQDQ instruction with AVX-512F adds EVEX encoded 512 bit version of PCLMULQDQ instruction. The advantage comes from packing multiples of 4 * 128 bit data into AVX512 reducing instruction latency.

The glue code in crct10diff module overrides the existing PCLMULQDQ version with the VPCLMULQDQ version when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VPCLMULQDQ instructions
At runtime:
1. VPCLMULQDQ and AVX512VL features are supported on a platform (currently only Icelake)
2. If compiled as built-in module, crct10dif_pclmul.use_avx512 is set at boot time or /sys/module/crct10dif_pclmul/parameters/use_avx512 is set to 1 after boot.
If compiled as loadable module, use_avx512 module parameter must be set:
modprobe crct10dif_pclmul use_avx512=1

A typical run of tcrypt with CRC T10 DIF calculation with PCLMULQDQ instruction and VPCLMULQDQ instruction shows the following results:
For bytes per update >= 1KB, we see the average improvement of 46%(~1.4x) For bytes per update < 1KB, we see the average improvement of 13%. Test was performed on an Icelake based platform with constant frequency set for CPU.

Detailed results for a variety of block sizes and update sizes are in the table below.

---------------------------------------------------------------------------

|            |            |         cycles/operation         |            |
|            |            |       (the lower the better)     |            |
|    byte    |   bytes    |----------------------------------| percentage |
|   blocks   | per update |   CRC T10 DIF  |  CRC T10 DIF    | loss/gain  |
|            |            | with PCLMULQDQ | with VPCLMULQDQ |            |
|------------|------------|----------------|-----------------|------------|
|      16    |     16     |        77      |        106      |   -27.0    |
|      64    |     16     |       411      |        390      |     5.4    |
|      64    |     64     |        71      |         85      |   -16.0    |
|     256    |     16     |      1224      |       1308      |    -6.4    |
|     256    |     64     |       393      |        407      |    -3.4    |
|     256    |    256     |        93      |         86      |     8.1    |
|    1024    |     16     |      4564      |       5020      |    -9.0    |
|    1024    |    256     |       486      |        475      |     2.3    |
|    1024    |   1024     |       221      |        148      |    49.3    |
|    2048    |     16     |      8945      |       9851      |    -9.1    |
|    2048    |    256     |       982      |        951      |     3.3    |
|    2048    |   1024     |       500      |        369      |    35.5    |
|    2048    |   2048     |       413      |        265      |    55.8    |
|    4096    |     16     |     17885      |      19351      |    -7.5    |
|    4096    |    256     |      1828      |       1713      |     6.7    |
|    4096    |   1024     |       968      |        805      |    20.0    |
|    4096    |   4096     |       739      |        475      |    55.6    |
|    8192    |     16     |     48339      |      41556      |    16.3    |
|    8192    |    256     |      3494      |       3342      |     4.5    |
|    8192    |   1024     |      1959      |       1462      |    34.0    |
|    8192    |   4096     |      1561      |       1036      |    50.7    |
|    8192    |   8192     |      1540      |       1004      |    53.4    |

---------------------------------------------------------------------------

This work was inspired by the CRC T10 DIF AVX512 optimization published in Intel Intelligent Storage Acceleration Library.
https://github.com/intel/isa-l/blob/master/crc/crc16_t10dif_by16_10.asm

---
 arch/x86/crypto/Makefile                  |   1 +
 arch/x86/crypto/crct10dif-avx512-asm_64.S | 482 ++++++++++++++++++++++++++++++
 arch/x86/crypto/crct10dif-pclmul_glue.c   |  24 +-
 arch/x86/include/asm/disabled-features.h  |   8 +-
 crypto/Kconfig                            |  23 ++
 5 files changed, 535 insertions(+), 3 deletions(-)
 create mode 100644 arch/x86/crypto/crct10dif-avx512-asm_64.S
arch/x86/crypto/Makefile +1 -0
index a31de0c..bf0b0fc 100644
@@ -80,6 +80,7 @@ crc32-pclmul-y := crc32-pclmul_asm.o crc32-pclmul_glue.o
 
 obj-$(CONFIG_CRYPTO_CRCT10DIF_PCLMUL) += crct10dif-pclmul.o
 crct10dif-pclmul-y := crct10dif-pcl-asm_64.o crct10dif-pclmul_glue.o
+crct10dif-pclmul-$(CONFIG_CRYPTO_CRCT10DIF_AVX512) += crct10dif-avx512-asm_64.o
 
 obj-$(CONFIG_CRYPTO_POLY1305_X86_64) += poly1305-x86_64.o
 poly1305-x86_64-y := poly1305-x86_64-cryptogams.o poly1305_glue.o
arch/x86/crypto/crct10dif-avx512-asm_64.S new file +482 -0

482 lines not on this page · raw message

arch/x86/crypto/crct10dif-pclmul_glue.c +22 -2
index 71291d5a..26a6350 100644
@@ -35,6 +35,16 @@
 #include <asm/simd.h>
 
 asmlinkage u16 crc_t10dif_pcl(u16 init_crc, const u8 *buf, size_t len);
+#ifdef CONFIG_CRYPTO_CRCT10DIF_AVX512
+asmlinkage u16 crct10dif_pcl_avx512(u16 init_crc, const u8 *buf, size_t len);
+#else
+static u16 crct10dif_pcl_avx512(u16 init_crc, const u8 *buf, size_t len)
+{ return 0; }
+#endif
+
+static bool use_avx512;
+module_param(use_avx512, bool, 0644);
+MODULE_PARM_DESC(use_avx512, "Use AVX512 optimized algorithm, if available");
 
 struct chksum_desc_ctx {
 	__u16 crc;
@@ -56,7 +66,12 @@ static int chksum_update(struct shash_desc *desc, const u8 *data,
 
 	if (length >= 16 && crypto_simd_usable()) {
 		kernel_fpu_begin();
-		ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);
+		if (IS_ENABLED(CONFIG_CRYPTO_CRCT10DIF_AVX512) &&
+		    cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ) &&
+		    use_avx512)
+			ctx->crc = crct10dif_pcl_avx512(ctx->crc, data, length);
+		else
+			ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);
 		kernel_fpu_end();
 	} else
 		ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
@@ -75,7 +90,12 @@ static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out)
 {
 	if (len >= 16 && crypto_simd_usable()) {
 		kernel_fpu_begin();
-		*(__u16 *)out = crc_t10dif_pcl(crc, data, len);
+		if (IS_ENABLED(CONFIG_CRYPTO_CRCT10DIF_AVX512) &&
+		    cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ) &&
+		    use_avx512)
+			*(__u16 *)out = crct10dif_pcl_avx512(crc, data, len);
+		else
+			*(__u16 *)out = crc_t10dif_pcl(crc, data, len);
 		kernel_fpu_end();
 	} else
 		*(__u16 *)out = crc_t10dif_generic(crc, data, len);
arch/x86/include/asm/disabled-features.h +7 -1
index 5861d34..1192dea 100644
@@ -56,6 +56,12 @@
 # define DISABLE_PTI		(1 << (X86_FEATURE_PTI & 31))
 #endif
 
+#if defined(CONFIG_AS_VPCLMULQDQ)
+# define DISABLE_VPCLMULQDQ	0
+#else
+# define DISABLE_VPCLMULQDQ	(1 << (X86_FEATURE_VPCLMULQDQ & 31))
+#endif
+
 #ifdef CONFIG_IOMMU_SUPPORT
 # define DISABLE_ENQCMD	0
 #else
@@ -82,7 +88,7 @@
 #define DISABLED_MASK14	0
 #define DISABLED_MASK15	0
 #define DISABLED_MASK16	(DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \
-			 DISABLE_ENQCMD)
+			 DISABLE_ENQCMD|DISABLE_VPCLMULQDQ)
 #define DISABLED_MASK17	0
 #define DISABLED_MASK18	0
 #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)
crypto/Kconfig +23 -0
index a367fcf..b090f14 100644
@@ -613,6 +613,29 @@ config CRYPTO_CRC32C_VPMSUM
 	  (vpmsum) instructions, introduced in POWER8. Enable on POWER8
 	  and newer processors for improved performance.
 
+config CRYPTO_AVX512
+	bool "AVX512 hardware acceleration for crypto algorithms"
+	depends on X86
+	depends on 64BIT
+	help
+	  This option will compile in AVX512 hardware accelerated crypto
+	  algorithms. These optimized algorithms provide substantial(2-10x)
+	  improvements over existing crypto algorithms for large data size.
+	  However, it may also incur a frequency penalty (aka. "bin drops")
+	  and cause collateral damage to other workloads running on the
+	  same core.
+
+# We default CRYPTO_CRCT10DIF_AVX512 to Y but depend on CRYPTO_AVX512 in
+# order to have a singular option (CRYPTO_AVX512) select multiple algorithms
+# when supported. Specifically, if the platform and/or toolset does not
+# support VPLMULQDQ. Then this algorithm should not be supported as part of
+# the set that CRYPTO_AVX512 selects.
+config CRYPTO_CRCT10DIF_AVX512
+	bool
+	default y
+	depends on CRYPTO_AVX512
+	depends on CRYPTO_CRCT10DIF_PCLMUL
+	depends on AS_VPCLMULQDQ
 
 config CRYPTO_CRC32C_SPARC64
 	tristate "CRC32c CRC algorithm (SPARC64)"
Signature · 1 line
2.7.4
author
#7 lore raw

RFC 3/7 crypto: ghash - Optimized GHASH computations

In reply to Megha Dey

From: Kyung Min Park <[email protected]>

Optimize GHASH computations with the 512 bit wide VPCLMULQDQ instructions. The new instruction allows to work on 4 x 16 byte blocks at the time. For best parallelism and deeper out of order execution, the main loop of the code works on 16 x 16 byte blocks at the time and performs reduction every 48 x 16 byte blocks. Such approach needs 48 precomputed GHASH subkeys and the precompute operation has been optimized as well to leverage 512 bit registers, parallel carry less multiply and reduction.

VPCLMULQDQ instruction is used to accelerate the most time-consuming part of GHASH, carry-less multiplication. VPCLMULQDQ instruction with AVX-512F adds EVEX encoded 512 bit version of PCLMULQDQ instruction.

The glue code in ghash_clmulni_intel module overrides existing PCLMULQDQ version with the VPCLMULQDQ version when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VPCLMULQDQ instructions
At runtime:
1. VPCLMULQDQ and AVX512VL features are supported on a platform (currently only Icelake)
2. If compiled as built-in module, ghash_clmulni_intel.use_avx512 is set at boot time or /sys/module/ghash_clmulni_intel/parameters/use_avx512 is set to 1 after boot.
If compiled as loadable module, use_avx512 module parameter must be set:
modprobe ghash_clmulni_intel use_avx512=1

With new implementation, tcrypt ghash speed test shows about 4x to 10x speedup improvement for GHASH calculation compared to the original implementation with PCLMULQDQ when the bytes per update size is 256 Bytes or above. Detailed results for a variety of block sizes and update sizes are in the table below. The test was performed on Icelake based platform with constant frequency set for CPU.

The average performance improvement of the AVX512 version over the current implementation is as follows:
For bytes per update >= 1KB, we see the average improvement of 882%(~8.8x). For bytes per update < 1KB, we see the average improvement of 370%(~3.7x).

A typical run of tcrypt with GHASH calculation with PCLMULQDQ instruction and VPCLMULQDQ instruction shows the following results.

---------------------------------------------------------------------------

|            |            |         cycles/operation         |            |
|            |            |       (the lower the better)     |            |
|    byte    |   bytes    |----------------------------------| percentage |
|   blocks   | per update |   GHASH test   |   GHASH test    | loss/gain  |
|            |            | with PCLMULQDQ | with VPCLMULQDQ |            |
|------------|------------|----------------|-----------------|------------|
|      16    |     16     |       144      |        233      |   -38.0    |
|      64    |     16     |       535      |        709      |   -24.5    |
|      64    |     64     |       210      |        146      |    43.8    |
|     256    |     16     |      1808      |       1911      |    -5.4    |
|     256    |     64     |       865      |        581      |    48.9    |
|     256    |    256     |       682      |        170      |   301.0    |
|    1024    |     16     |      6746      |       6935      |    -2.7    |
|    1024    |    256     |      2829      |        714      |   296.0    |
|    1024    |   1024     |      2543      |        341      |   645.0    |
|    2048    |     16     |     13219      |      13403      |    -1.3    |
|    2048    |    256     |      5435      |       1408      |   286.0    |
|    2048    |   1024     |      5218      |        685      |   661.0    |
|    2048    |   2048     |      5061      |        565      |   796.0    |
|    4096    |     16     |     40793      |      27615      |    47.8    |
|    4096    |    256     |     10662      |       2689      |   297.0    |
|    4096    |   1024     |     10196      |       1333      |   665.0    |
|    4096    |   4096     |     10049      |       1011      |   894.0    |
|    8192    |     16     |     51672      |      54599      |    -5.3    |
|    8192    |    256     |     21228      |       5284      |   301.0    |
|    8192    |   1024     |     20306      |       2556      |   694.0    |
|    8192    |   4096     |     20076      |       2044      |   882.0    |
|    8192    |   8192     |     20071      |       2017      |   895.0    |

---------------------------------------------------------------------------

This work was inspired by the AES GCM mode optimization published in Intel Optimized IPSEC Cryptographic library.
https://github.com/intel/intel-ipsec-mb/lib/avx512/gcm_vaes_avx512.asm

---
 arch/x86/crypto/Makefile                     |    1 +
 arch/x86/crypto/avx512_vaes_common.S         | 1211 ++++++++++++++++++++++++++
 arch/x86/crypto/ghash-clmulni-intel_avx512.S |   68 ++
 arch/x86/crypto/ghash-clmulni-intel_glue.c   |   39 +-
 crypto/Kconfig                               |   12 +
 5 files changed, 1329 insertions(+), 2 deletions(-)
 create mode 100644 arch/x86/crypto/avx512_vaes_common.S
 create mode 100644 arch/x86/crypto/ghash-clmulni-intel_avx512.S
arch/x86/crypto/Makefile +1 -0
index bf0b0fc..0a86cfb 100644
@@ -70,6 +70,7 @@ blake2s-x86_64-y := blake2s-core.o blake2s-glue.o
 
 obj-$(CONFIG_CRYPTO_GHASH_CLMUL_NI_INTEL) += ghash-clmulni-intel.o
 ghash-clmulni-intel-y := ghash-clmulni-intel_asm.o ghash-clmulni-intel_glue.o
+ghash-clmulni-intel-$(CONFIG_CRYPTO_GHASH_CLMUL_NI_AVX512) += ghash-clmulni-intel_avx512.o
 
 obj-$(CONFIG_CRYPTO_CRC32C_INTEL) += crc32c-intel.o
 crc32c-intel-y := crc32c-intel_glue.o
arch/x86/crypto/avx512_vaes_common.S new file +1211 -0

1,211 lines not on this page · raw message

arch/x86/crypto/ghash-clmulni-intel_avx512.S new file +68 -0

68 lines not on this page · raw message

arch/x86/crypto/ghash-clmulni-intel_glue.c +37 -2
index 1f1a95f..3a3e8ea 100644
@@ -22,18 +22,39 @@
 
 #define GHASH_BLOCK_SIZE	16
 #define GHASH_DIGEST_SIZE	16
+#define GHASH_KEY_LEN		16
+
+static bool use_avx512;
+module_param(use_avx512, bool, 0644);
+MODULE_PARM_DESC(use_avx512, "Use AVX512 optimized algorithm, if available");
 
 void clmul_ghash_mul(char *dst, const u128 *shash);
 
 void clmul_ghash_update(char *dst, const char *src, unsigned int srclen,
 			const u128 *shash);
 
+extern void ghash_precomp_avx512(u8 *key_data);
+#ifdef CONFIG_CRYPTO_GHASH_CLMUL_NI_AVX512
+extern void clmul_ghash_update_avx512(char *dst, const char *src, unsigned int srclen,
+				      u8 *shash);
+#else
+static void clmul_ghash_update_avx512(char *dst, const char *src, unsigned int srclen,
+			       u8 *shash)
+{}
+#endif
+
 struct ghash_async_ctx {
 	struct cryptd_ahash *cryptd_tfm;
 };
 
+/*
+ * This is needed for schoolbook multiply purposes.
+ * (HashKey << 1 mod poly), (HashKey^2 << 1 mod poly), ...,
+ * (Hashkey^48 << 1 mod poly)
+ */
 struct ghash_ctx {
 	u128 shash;
+	u8 hkey[GHASH_KEY_LEN * 48];
 };
 
 struct ghash_desc_ctx {
@@ -56,6 +77,15 @@ static int ghash_setkey(struct crypto_shash *tfm,
 	struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
 	be128 *x = (be128 *)key;
 	u64 a, b;
+	int i;
+
+	if (IS_ENABLED(CONFIG_CRYPTO_GHASH_CLMUL_NI_AVX512) &&
+	    cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ) && use_avx512) {
+		for (i = 0; i < 16; i++)
+			ctx->hkey[(16 * 47) + i] = key[i];
+
+		ghash_precomp_avx512(ctx->hkey);
+	}
 
 	if (keylen != GHASH_BLOCK_SIZE)
 		return -EINVAL;
@@ -94,8 +124,13 @@ static int ghash_update(struct shash_desc *desc,
 		if (!dctx->bytes)
 			clmul_ghash_mul(dst, &ctx->shash);
 	}
-
-	clmul_ghash_update(dst, src, srclen, &ctx->shash);
+	if (IS_ENABLED(CONFIG_CRYPTO_GHASH_CLMUL_NI_AVX512) &&
+	    cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ) && use_avx512) {
+		/* Assembly code handles fragments in 16 byte multiples */
+		srclen = ALIGN_DOWN(srclen, 16);
+		clmul_ghash_update_avx512(dst, src, srclen, ctx->hkey);
+	} else
+		clmul_ghash_update(dst, src, srclen, &ctx->shash);
 	kernel_fpu_end();
 
 	if (srclen & 0xf) {
crypto/Kconfig +12 -0
index b090f14..70d1d35 100644
@@ -637,6 +637,18 @@ config CRYPTO_CRCT10DIF_AVX512
 	depends on CRYPTO_CRCT10DIF_PCLMUL
 	depends on AS_VPCLMULQDQ
 
+# We default CRYPTO_GHASH_CLMUL_NI_AVX512 to Y but depend on CRYPTO_AVX512 in
+# order to have a singular option (CRYPTO_AVX512) select multiple algorithms
+# when supported. Specifically, if the platform and/or toolset does not
+# support VPLMULQDQ. Then this algorithm should not be supported as part of
+# the set that CRYPTO_AVX512 selects.
+config CRYPTO_GHASH_CLMUL_NI_AVX512
+	bool
+	default y
+	depends on CRYPTO_AVX512
+	depends on CRYPTO_GHASH_CLMUL_NI_INTEL
+	depends on AS_VPCLMULQDQ
+
 config CRYPTO_CRC32C_SPARC64
 	tristate "CRC32c CRC algorithm (SPARC64)"
 	depends on SPARC64
Signature · 1 line
2.7.4
author
#16 lore raw

RFC 4/7 crypto: tcrypt - Add speed test for optimized GHASH computations

In reply to Megha Dey

From: Kyung Min Park <[email protected]>

Add speed test for optimized GHASH computations with vectorized instructions. Introduce a new test suite to calculate the speed for this algorithm.

---
 crypto/tcrypt.c | 5 +++++
 1 file changed, 5 insertions(+)
crypto/tcrypt.c +5 -0
index a647bb2..6e2d74c6 100644
@@ -2595,6 +2595,11 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)
 				    generic_hash_speed_template, num_mb);
 		if (mode > 400 && mode < 500) break;
 		fallthrough;
+	case 428:
+		klen = 16;
+		test_ahash_speed("ghash", sec, generic_hash_speed_template);
+		if (mode > 400 && mode < 500) break;
+		fallthrough;
 	case 499:
 		break;
 
Signature · 1 line
2.7.4
author
#17 lore raw

RFC 5/7 crypto: aesni - AES CTR x86_64 "by16" AVX512 optimization

In reply to Megha Dey

Introduce the "by16" implementation of the AES CTR mode using AVX512 optimizations. "by16" means that 16 independent blocks (each block being 128 bits) can be ciphered simultaneously as opposed to the current 8 blocks.

The glue code in AESNI module overrides the existing "by8" CTR mode encryption/decryption routines with the "by16" ones when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VAES instructions
At runtime:
1. VAES and AVX512VL features are supported on platform (currently only Icelake)
2. aesni_intel.use_avx512 module parameter is set at boot time. For this algorithm, switching from AVX512 optimized version is not possible once set at boot time because of how the code is structured today.(Can be changed later if required)

The functions aes_ctr_enc_128_avx512_by16(), aes_ctr_enc_192_avx512_by16() and aes_ctr_enc_256_avx512_by16() are adapted from Intel Optimized IPSEC Cryptographic library.

On a Icelake desktop, with turbo disabled and all CPUs running at maximum frequency, the "by16" CTR mode optimization shows better performance across data & key sizes as measured by tcrypt.

The average performance improvement of the "by16" version over the "by8" version is as follows:
For all key sizes(128/192/256 bits),
data sizes < 128 bytes/block, negligible improvement(~3% loss) data sizes > 128 bytes/block, there is an average improvement of
48% for both encryption and decryption.

A typical run of tcrypt with AES CTR mode encryption/decryption of the "by8" and "by16" optimization on a Icelake desktop shows the following results:

--------------------------------------------------------------

|  key   | bytes | cycles/op (lower is better)| percentage   |
| length |  per  |  encryption  |  decryption |  loss/gain   |
| (bits) | block |-------------------------------------------|
|        |       | by8  | by16  | by8  | by16 |  enc | dec   |
|------------------------------------------------------------|
|  128   |  16   | 156  | 168   | 164  | 168  | -7.7 |  -2.5 |
|  128   |  64   | 180  | 190   | 157  | 146  | -5.6 |   7.1 |
|  128   |  256  | 248  | 158   | 251  | 161  | 36.3 |  35.9 |
|  128   |  1024 | 633  | 316   | 642  | 319  | 50.1 |  50.4 |
|  128   |  1472 | 853  | 411   | 877  | 407  | 51.9 |  53.6 |
|  128   |  8192 | 4463 | 1959  | 4447 | 1940 | 56.2 |  56.4 |
|  192   |  16   | 136  | 145   | 149  | 166  | -6.7 | -11.5 |
|  192   |  64   | 159  | 154   | 157  | 160  |  3.2 |  -2   |
|  192   |  256  | 268  | 172   | 274  | 177  | 35.9 |  35.5 |
|  192   |  1024 | 710  | 358   | 720  | 355  | 49.6 |  50.7 |
|  192   |  1472 | 989  | 468   | 983  | 469  | 52.7 |  52.3 |
|  192   |  8192 | 6326 | 3551  | 6301 | 3567 | 43.9 |  43.4 |
|  256   |  16   | 153  | 165   | 139  | 156  | -7.9 | -12.3 |
|  256   |  64   | 158  | 152   | 174  | 161  |  3.8 |   7.5 |
|  256   |  256  | 283  | 176   | 287  | 202  | 37.9 |  29.7 |
|  256   |  1024 | 797  | 393   | 807  | 395  | 50.7 |  51.1 |
|  256   |  1472 | 1108 | 534   | 1107 | 527  | 51.9 |  52.4 |
|  256   |  8192 | 5763 | 2616  | 5773 | 2617 | 54.7 |  54.7 |

--------------------------------------------------------------

This work was inspired by the AES CTR mode optimization published in Intel Optimized IPSEC Cryptographic library.
https://github.com/intel/intel-ipsec-mb/blob/master/lib/avx512/cntr_vaes_avx512.asm

---
 arch/x86/crypto/Makefile                    |   1 +
 arch/x86/crypto/aes_ctrby16_avx512-x86_64.S | 856 ++++++++++++++++++++++++++++
 arch/x86/crypto/aesni-intel_glue.c          |  57 +-
 arch/x86/crypto/avx512_vaes_common.S        | 422 ++++++++++++++
 arch/x86/include/asm/disabled-features.h    |   8 +-
 crypto/Kconfig                              |  12 +
 6 files changed, 1354 insertions(+), 2 deletions(-)
 create mode 100644 arch/x86/crypto/aes_ctrby16_avx512-x86_64.S
arch/x86/crypto/Makefile +1 -0
index 0a86cfb..5fd9b35 100644
@@ -53,6 +53,7 @@ chacha-x86_64-$(CONFIG_AS_AVX512) += chacha-avx512vl-x86_64.o
 obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o
 aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o
 aesni-intel-$(CONFIG_64BIT) += aesni-intel_avx-x86_64.o aes_ctrby8_avx-x86_64.o
+aesni-intel-$(CONFIG_CRYPTO_AES_CTR_AVX512) += aes_ctrby16_avx512-x86_64.o
 
 obj-$(CONFIG_CRYPTO_SHA1_SSSE3) += sha1-ssse3.o
 sha1-ssse3-y := sha1_avx2_x86_64_asm.o sha1_ssse3_asm.o sha1_ssse3_glue.o
arch/x86/crypto/aes_ctrby16_avx512-x86_64.S new file +856 -0

856 lines not on this page · raw message

arch/x86/crypto/aesni-intel_glue.c +56 -1

81 lines not on this page · raw message

arch/x86/crypto/avx512_vaes_common.S +422 -0

431 lines not on this page · raw message

arch/x86/include/asm/disabled-features.h +7 -1
index 1192dea..251c652 100644
@@ -62,6 +62,12 @@
 # define DISABLE_VPCLMULQDQ	(1 << (X86_FEATURE_VPCLMULQDQ & 31))
 #endif
 
+#if defined(CONFIG_AS_VAES_AVX512)
+# define DISABLE_VAES		0
+#else
+# define DISABLE_VAES		(1 << (X86_FEATURE_VAES & 31))
+#endif
+
 #ifdef CONFIG_IOMMU_SUPPORT
 # define DISABLE_ENQCMD	0
 #else
@@ -88,7 +94,7 @@
 #define DISABLED_MASK14	0
 #define DISABLED_MASK15	0
 #define DISABLED_MASK16	(DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \
-			 DISABLE_ENQCMD|DISABLE_VPCLMULQDQ)
+			 DISABLE_ENQCMD|DISABLE_VPCLMULQDQ|DISABLE_VAES)
 #define DISABLED_MASK17	0
 #define DISABLED_MASK18	0
 #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)
crypto/Kconfig +12 -0
index 70d1d35..3043849 100644
@@ -649,6 +649,18 @@ config CRYPTO_GHASH_CLMUL_NI_AVX512
 	depends on CRYPTO_GHASH_CLMUL_NI_INTEL
 	depends on AS_VPCLMULQDQ
 
+# We default CRYPTO_AES_CTR_AVX512 to Y but depend on CRYPTO_AVX512 in
+# order to have a singular option (CRYPTO_AVX512) select multiple algorithms
+# when supported. Specifically, if the platform and/or toolset does not
+# support VPLMULQDQ. Then this algorithm should not be supported as part of
+# the set that CRYPTO_AVX512 selects.
+config CRYPTO_AES_CTR_AVX512
+	bool
+	default y
+	depends on CRYPTO_AVX512
+	depends on CRYPTO_AES_NI_INTEL
+	depends on AS_VAES_AVX512
+
 config CRYPTO_CRC32C_SPARC64
 	tristate "CRC32c CRC algorithm (SPARC64)"
 	depends on SPARC64
Signature · 1 line
2.7.4
author
#20 lore raw

RFC 6/7 crypto: aesni - fix coding style for if/else block

In reply to Megha Dey

The if else block in aesni_init does not follow required coding conventions. If other conditionals are added to the block, it becomes very difficult to parse. Use the correct coding style instead.

---
 arch/x86/crypto/aesni-intel_glue.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/crypto/aesni-intel_glue.c +1 -2
index f45059e..9e56cdf 100644
@@ -1117,8 +1117,7 @@ static int __init aesni_init(void)
 	if (boot_cpu_has(X86_FEATURE_AVX2)) {
 		pr_info("AVX2 version of gcm_enc/dec engaged.\n");
 		aesni_gcm_tfm = &aesni_gcm_tfm_avx_gen4;
-	} else
-	if (boot_cpu_has(X86_FEATURE_AVX)) {
+	} else if (boot_cpu_has(X86_FEATURE_AVX)) {
 		pr_info("AVX version of gcm_enc/dec engaged.\n");
 		aesni_gcm_tfm = &aesni_gcm_tfm_avx_gen2;
 	} else {
Signature · 1 line
2.7.4
author
#21 lore raw

RFC 7/7 crypto: aesni - AVX512 version of AESNI-GCM using VPCLMULQDQ

In reply to Megha Dey

Introduce the AVX512 implementation that optimizes the AESNI-GCM encode and decode routines using VPCLMULQDQ.

The glue code in AESNI module overrides the existing AVX2 GCM mode encryption/decryption routines with the AX512 AES GCM mode ones when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VPCLMULQDQ instructions
At runtime:
1. VPCLMULQDQ and AVX512VL features are supported on a platform (currently only Icelake)
2. aesni_intel.use_avx512 module parameter is set at boot time. For this algorithm, switching from AVX512 optimized version is not possible once set at boot time because of how the code is structured today.(Can be changed later if required)

The functions aesni_gcm_init_avx_512, aesni_gcm_enc_update_avx_512, aesni_gcm_dec_update_avx_512 and aesni_gcm_finalize_avx_512 are adapted from the Intel Optimized IPSEC Cryptographic library.

On a Icelake desktop, with turbo disabled and all CPUs running at maximum frequency, the AVX512 GCM mode optimization shows better performance across data & key sizes as measured by tcrypt.

The average performance improvement of the AVX512 version over the AVX2 version is as follows:
For all key sizes(128/192/256 bits),
data sizes < 128 bytes/block, negligible improvement (~7.5%) data sizes > 128 bytes/block, there is an average improvement of 40% for both encryption and decryption.

A typical run of tcrypt with AES GCM mode encryption/decryption of the AVX2 and AVX512 optimization on a Icelake desktop shows the following results:

----------------------------------------------------------------------

  |   key  | bytes | cycles/op (lower is better)   | Percentage gain/  |
  | length |   per |   encryption  |  decryption   |      loss         |
  | (bits) | block |-------------------------------|-------------------|
  |        |       | avx2 | avx512 | avx2 | avx512 | Encrypt | Decrypt |
  |---------------------------------------------------------------------
  |  128   | 16    | 689  |  701   | 689  |  707   |  -1.7   |  -2.61  |
  |  128   | 64    | 731  |  660   | 771  |  649   |   9.7   |  15.82  |
  |  128   | 256   | 911  |  750   | 900  |  721   |  17.67  |  19.88  |
  |  128   | 512   | 1181 |  814   | 1161 |  782   |  31.07  |  32.64  |
  |  128   | 1024  | 1676 |  1052  | 1685 |  1030  |  37.23  |  38.87  |
  |  128   | 2048  | 2475 |  1447  | 2456 |  1419  |  41.53  |  42.22  |
  |  128   | 4096  | 3806 |  2154  | 3820 |  2119  |  43.41  |  44.53  |
  |  128   | 8192  | 9169 |  3806  | 6997 |  3718  |  58.49  |  46.86  |
  |  192   | 16    | 754  |  683   | 737  |  672   |   9.42  |   8.82  |
  |  192   | 64    | 735  |  686   | 715  |  640   |   6.66  |  10.49  |
  |  192   | 256   | 949  |  738   | 2435 |  729   |  22.23  |  70     |
  |  192   | 512   | 1235 |  854   | 1200 |  833   |  30.85  |  30.58  |
  |  192   | 1024  | 1777 |  1084  | 1763 |  1051  |  38.99  |  40.39  |
  |  192   | 2048  | 2574 |  1497  | 2592 |  1459  |  41.84  |  43.71  |
  |  192   | 4096  | 4086 |  2317  | 4091 |  2244  |  43.29  |  45.14  |
  |  192   | 8192  | 7481 |  4054  | 7505 |  3953  |  45.81  |  47.32  |
  |  256   | 16    | 755  |  682   | 720  |  683   |   9.68  |   5.14  |
  |  256   | 64    | 744  |  677   | 719  |  658   |   9     |   8.48  |
  |  256   | 256   | 962  |  758   | 948  |  749   |  21.21  |  21     |
  |  256   | 512   | 1297 |  862   | 1276 |  836   |  33.54  |  34.48  |
  |  256   | 1024  | 1831 |  1114  | 1819 |  1095  |  39.16  |  39.8   |
  |  256   | 2048  | 2767 |  1566  | 2715 |  1524  |  43.4   |  43.87  |
  |  256   | 4096  | 4378 |  2382  | 4368 |  2354  |  45.6   |  46.11  |
  |  256   | 8192  | 8075 |  4262  | 8080 |  4186  |  47.22  |  48.19  |

----------------------------------------------------------------------

This work was inspired by the AES GCM mode optimization published in Intel Optimized IPSEC Cryptographic library.
https://github.com/intel/intel-ipsec-mb/blob/master/lib/avx512/gcm_vaes_avx512.asm

---
 arch/x86/crypto/Makefile                    |    1 +
 arch/x86/crypto/aesni-intel_avx512-x86_64.S | 1788 +++++++++++++++++++++++++++
 arch/x86/crypto/aesni-intel_glue.c          |   62 +-
 crypto/Kconfig                              |   12 +
 4 files changed, 1858 insertions(+), 5 deletions(-)
 create mode 100644 arch/x86/crypto/aesni-intel_avx512-x86_64.S
arch/x86/crypto/Makefile +1 -0
index 5fd9b35..320d4cc 100644
@@ -54,6 +54,7 @@ obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o
 aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o
 aesni-intel-$(CONFIG_64BIT) += aesni-intel_avx-x86_64.o aes_ctrby8_avx-x86_64.o
 aesni-intel-$(CONFIG_CRYPTO_AES_CTR_AVX512) += aes_ctrby16_avx512-x86_64.o
+aesni-intel-$(CONFIG_CRYPTO_AES_GCM_AVX512) += aesni-intel_avx512-x86_64.o
 
 obj-$(CONFIG_CRYPTO_SHA1_SSSE3) += sha1-ssse3.o
 sha1-ssse3-y := sha1_avx2_x86_64_asm.o sha1_ssse3_asm.o sha1_ssse3_glue.o
arch/x86/crypto/aesni-intel_avx512-x86_64.S new file +1788 -0

1,788 lines not on this page · raw message

arch/x86/crypto/aesni-intel_glue.c +57 -5

97 lines not on this page · raw message

crypto/Kconfig +12 -0
index 3043849..8c8a68d 100644
@@ -661,6 +661,18 @@ config CRYPTO_AES_CTR_AVX512
 	depends on CRYPTO_AES_NI_INTEL
 	depends on AS_VAES_AVX512
 
+# We default CRYPTO_AES_GCM_AVX512 to Y but depend on CRYPTO_AVX512 in
+# order to have a singular option (CRYPTO_AVX512) select multiple algorithms
+# when supported. Specifically, if the platform and/or toolset does not
+# support VPLMULQDQ. Then this algorithm should not be supported as part of
+# the set that CRYPTO_AVX512 selects.
+config CRYPTO_AES_GCM_AVX512
+	bool
+	default y
+	depends on CRYPTO_AVX512
+	depends on CRYPTO_AES_NI_INTEL
+	depends on AS_VPCLMULQDQ
+
 config CRYPTO_CRC32C_SPARC64
 	tristate "CRC32c CRC algorithm (SPARC64)"
 	depends on SPARC64
Signature · 1 line
2.7.4
#8 lore raw

Re: [RFC V1 3/7] crypto: ghash - Optimized GHASH computations

In reply to Megha Dey
Quoted 79 lines · Megha Dey

From: Kyung Min Park <[email protected]>

Optimize GHASH computations with the 512 bit wide VPCLMULQDQ instructions. The new instruction allows to work on 4 x 16 byte blocks at the time. For best parallelism and deeper out of order execution, the main loop of the code works on 16 x 16 byte blocks at the time and performs reduction every 48 x 16 byte blocks. Such approach needs 48 precomputed GHASH subkeys and the precompute operation has been optimized as well to leverage 512 bit registers, parallel carry less multiply and reduction.

VPCLMULQDQ instruction is used to accelerate the most time-consuming part of GHASH, carry-less multiplication. VPCLMULQDQ instruction with AVX-512F adds EVEX encoded 512 bit version of PCLMULQDQ instruction.

The glue code in ghash_clmulni_intel module overrides existing PCLMULQDQ version with the VPCLMULQDQ version when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VPCLMULQDQ instructions
At runtime:
1. VPCLMULQDQ and AVX512VL features are supported on a platform (currently only Icelake)
2. If compiled as built-in module, ghash_clmulni_intel.use_avx512 is set at boot time or /sys/module/ghash_clmulni_intel/parameters/use_avx512 is set to 1 after boot.
If compiled as loadable module, use_avx512 module parameter must be set:
modprobe ghash_clmulni_intel use_avx512=1

With new implementation, tcrypt ghash speed test shows about 4x to 10x speedup improvement for GHASH calculation compared to the original implementation with PCLMULQDQ when the bytes per update size is 256 Bytes or above. Detailed results for a variety of block sizes and update sizes are in the table below. The test was performed on Icelake based platform with constant frequency set for CPU.

The average performance improvement of the AVX512 version over the current implementation is as follows:
For bytes per update >= 1KB, we see the average improvement of 882%(~8.8x). For bytes per update < 1KB, we see the average improvement of 370%(~3.7x).

A typical run of tcrypt with GHASH calculation with PCLMULQDQ instruction and VPCLMULQDQ instruction shows the following results.

---------------------------------------------------------------------------

|            |            |         cycles/operation         |            |
|            |            |       (the lower the better)     |            |
|    byte    |   bytes    |----------------------------------| percentage |
|   blocks   | per update |   GHASH test   |   GHASH test    | loss/gain  |
|            |            | with PCLMULQDQ | with VPCLMULQDQ |            |
|------------|------------|----------------|-----------------|------------|
|      16    |     16     |       144      |        233      |   -38.0    |
|      64    |     16     |       535      |        709      |   -24.5    |
|      64    |     64     |       210      |        146      |    43.8    |
|     256    |     16     |      1808      |       1911      |    -5.4    |
|     256    |     64     |       865      |        581      |    48.9    |
|     256    |    256     |       682      |        170      |   301.0    |
|    1024    |     16     |      6746      |       6935      |    -2.7    |
|    1024    |    256     |      2829      |        714      |   296.0    |
|    1024    |   1024     |      2543      |        341      |   645.0    |
|    2048    |     16     |     13219      |      13403      |    -1.3    |
|    2048    |    256     |      5435      |       1408      |   286.0    |
|    2048    |   1024     |      5218      |        685      |   661.0    |
|    2048    |   2048     |      5061      |        565      |   796.0    |
|    4096    |     16     |     40793      |      27615      |    47.8    |
|    4096    |    256     |     10662      |       2689      |   297.0    |
|    4096    |   1024     |     10196      |       1333      |   665.0    |
|    4096    |   4096     |     10049      |       1011      |   894.0    |
|    8192    |     16     |     51672      |      54599      |    -5.3    |
|    8192    |    256     |     21228      |       5284      |   301.0    |
|    8192    |   1024     |     20306      |       2556      |   694.0    |
|    8192    |   4096     |     20076      |       2044      |   882.0    |
|    8192    |   8192     |     20071      |       2017      |   895.0    |

---------------------------------------------------------------------------

This work was inspired by the AES GCM mode optimization published in Intel Optimized IPSEC Cryptographic library.
https://github.com/intel/intel-ipsec-mb/lib/avx512/gcm_vaes_avx512.asm

Quoted 7 lines

Hello Megha,

What is the purpose of this separate GHASH module? GHASH is only used in combination with AES-CTR to produce GCM, and this series already contains a GCM driver.

Do cores exist that implement PCLMULQDQ but not AES-NI?

If not, I think we should be able to drop this patch (and remove the existing PCLMULQDQ GHASH driver as well)

#24 lore raw
In reply to Megha Dey
Quoted 9 lines · Megha Dey

Optimize crypto algorithms using VPCLMULQDQ and VAES AVX512 instructions (first implemented on Intel's Icelake client and Xeon CPUs).

These algorithms take advantage of the AVX512 registers to keep the CPU busy and increase memory bandwidth utilization. They provide substantial (2-10x) improvements over existing crypto algorithms when update data size is greater than 128 bytes and do not have any significant impact when used on small amounts of data.

Quoted 7 lines

However, these algorithms may also incur a frequency penalty and cause collateral damage to other workloads running on the same core(co-scheduled threads). These frequency drops are also known as bin drops where 1 bin drop is around 100MHz. With the SpecCPU and ffmpeg benchmark, a 0-1 bin drop(0-100MHz) is observed on Icelake desktop and 0-2 bin drops (0-200Mhz) are observed on the Icelake server.

Do these new algorithms all pass the self-tests, including the fuzz tests that are enabled when CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y?

- Eric

author
#25 lore raw

Hi Eric,

Quoted 17 lines · Eric Biggers
Quoted 9 lines · Megha Dey

Optimize crypto algorithms using VPCLMULQDQ and VAES AVX512 instructions (first implemented on Intel's Icelake client and Xeon CPUs).

These algorithms take advantage of the AVX512 registers to keep the CPU busy and increase memory bandwidth utilization. They provide substantial (2-10x) improvements over existing crypto algorithms when update data size is greater than 128 bytes and do not have any significant impact when used on small amounts of data.

Quoted 7 lines

However, these algorithms may also incur a frequency penalty and cause collateral damage to other workloads running on the same core(co-scheduled threads). These frequency drops are also known as bin drops where 1 bin drop is around 100MHz. With the SpecCPU and ffmpeg benchmark, a 0-1 bin drop(0-100MHz) is observed on Icelake desktop and 0-2 bin drops (0-200Mhz) are observed on the Icelake server.

Quoted 2 lines

Do these new algorithms all pass the self-tests, including the fuzz tests that are enabled when CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y?

I had tested these algorithms with CRYPTO_MANAGER_DISABLE_TESTS=n and tcrypt, not with CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y (I wasn't aware this existed, my bad). I see a couple of errors after enabling it and am working on fixing those.

Megha

Quoted 2 lines

- Eric

author
#9 lore raw

Re: [RFC V1 3/7] crypto: ghash - Optimized GHASH computations

In reply to Ard Biesheuvel

Hi Ard,

Quoted 88 lines · Ard Biesheuvel
Quoted 78 lines · Megha Dey

From: Kyung Min Park <[email protected]>

Optimize GHASH computations with the 512 bit wide VPCLMULQDQ instructions. The new instruction allows to work on 4 x 16 byte blocks at the time. For best parallelism and deeper out of order execution, the main loop of the code works on 16 x 16 byte blocks at the time and performs reduction every 48 x 16 byte blocks. Such approach needs 48 precomputed GHASH subkeys and the precompute operation has been optimized as well to leverage 512 bit registers, parallel carry less multiply and reduction.

VPCLMULQDQ instruction is used to accelerate the most time-consuming part of GHASH, carry-less multiplication. VPCLMULQDQ instruction with AVX-512F adds EVEX encoded 512 bit version of PCLMULQDQ instruction.

The glue code in ghash_clmulni_intel module overrides existing PCLMULQDQ version with the VPCLMULQDQ version when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VPCLMULQDQ instructions
At runtime:
1. VPCLMULQDQ and AVX512VL features are supported on a platform (currently only Icelake)
2. If compiled as built-in module, ghash_clmulni_intel.use_avx512 is set at boot time or /sys/module/ghash_clmulni_intel/parameters/use_avx512 is set to 1 after boot.
If compiled as loadable module, use_avx512 module parameter must be set:
modprobe ghash_clmulni_intel use_avx512=1

With new implementation, tcrypt ghash speed test shows about 4x to 10x speedup improvement for GHASH calculation compared to the original implementation with PCLMULQDQ when the bytes per update size is 256 Bytes or above. Detailed results for a variety of block sizes and update sizes are in the table below. The test was performed on Icelake based platform with constant frequency set for CPU.

The average performance improvement of the AVX512 version over the current implementation is as follows:
For bytes per update >= 1KB, we see the average improvement of 882%(~8.8x). For bytes per update < 1KB, we see the average improvement of 370%(~3.7x).

A typical run of tcrypt with GHASH calculation with PCLMULQDQ instruction and VPCLMULQDQ instruction shows the following results.

---------------------------------------------------------------------------

|            |            |         cycles/operation         |            |
|            |            |       (the lower the better)     |            |
|    byte    |   bytes    |----------------------------------| percentage |
|   blocks   | per update |   GHASH test   |   GHASH test    | loss/gain  |
|            |            | with PCLMULQDQ | with VPCLMULQDQ |            |
|------------|------------|----------------|-----------------|------------|
|      16    |     16     |       144      |        233      |   -38.0    |
|      64    |     16     |       535      |        709      |   -24.5    |
|      64    |     64     |       210      |        146      |    43.8    |
|     256    |     16     |      1808      |       1911      |    -5.4    |
|     256    |     64     |       865      |        581      |    48.9    |
|     256    |    256     |       682      |        170      |   301.0    |
|    1024    |     16     |      6746      |       6935      |    -2.7    |
|    1024    |    256     |      2829      |        714      |   296.0    |
|    1024    |   1024     |      2543      |        341      |   645.0    |
|    2048    |     16     |     13219      |      13403      |    -1.3    |
|    2048    |    256     |      5435      |       1408      |   286.0    |
|    2048    |   1024     |      5218      |        685      |   661.0    |
|    2048    |   2048     |      5061      |        565      |   796.0    |
|    4096    |     16     |     40793      |      27615      |    47.8    |
|    4096    |    256     |     10662      |       2689      |   297.0    |
|    4096    |   1024     |     10196      |       1333      |   665.0    |
|    4096    |   4096     |     10049      |       1011      |   894.0    |
|    8192    |     16     |     51672      |      54599      |    -5.3    |
|    8192    |    256     |     21228      |       5284      |   301.0    |
|    8192    |   1024     |     20306      |       2556      |   694.0    |
|    8192    |   4096     |     20076      |       2044      |   882.0    |
|    8192    |   8192     |     20071      |       2017      |   895.0    |

---------------------------------------------------------------------------

This work was inspired by the AES GCM mode optimization published in Intel Optimized IPSEC Cryptographic library.
https://github.com/intel/intel-ipsec-mb/lib/avx512/gcm_vaes_avx512.asm

Quoted 7 lines

Hello Megha,

Quoted 8 lines

What is the purpose of this separate GHASH module? GHASH is only used in combination with AES-CTR to produce GCM, and this series already contains a GCM driver.

Do cores exist that implement PCLMULQDQ but not AES-NI?

If not, I think we should be able to drop this patch (and remove the existing PCLMULQDQ GHASH driver as well)

AFAIK, dm-verity (authenticated but not encrypted file system) is one use case for authentication only.

Although I am not sure if GHASH is specifically used for this or SHA?

Also, I do not know of any cores that implement PCLMULQDQ and not AES-NI.

Megha

#10 lore raw

Re: [RFC V1 3/7] crypto: ghash - Optimized GHASH computations

Quoted 1 line · Megha

Also, I do not know of any cores that implement PCLMULQDQ and not AES-NI.

That's true, bit it's also possible that a hypervisor could enumerate support for PCLMULQDQ and not AES-NI. In general, we've tried to implement x86 CPU features independently, even if they never show up in a real CPU independently.

#14 lore raw

Re: [RFC V1 3/7] crypto: ghash - Optimized GHASH computations

In reply to Dey, Megha
Quoted 11 lines · Megha
Quoted 10 lines

Hello Megha,

What is the purpose of this separate GHASH module? GHASH is only used in combination with AES-CTR to produce GCM, and this series already contains a GCM driver.

Do cores exist that implement PCLMULQDQ but not AES-NI?

If not, I think we should be able to drop this patch (and remove the existing PCLMULQDQ GHASH driver as well)

Quoted 7 lines

AFAIK, dm-verity (authenticated but not encrypted file system) is one use case for authentication only.

Although I am not sure if GHASH is specifically used for this or SHA?

Also, I do not know of any cores that implement PCLMULQDQ and not AES-NI.

dm-verity only uses unkeyed hash algorithms. So no, it doesn't use GHASH.

- Eric

#11 lore raw

Re: [RFC V1 3/7] crypto: ghash - Optimized GHASH computations

In reply to Dave Hansen
Quoted 7 lines · Dave Hansen
Quoted 1 line · Megha

Also, I do not know of any cores that implement PCLMULQDQ and not AES-NI.

That's true, bit it's also possible that a hypervisor could enumerate support for PCLMULQDQ and not AES-NI. In general, we've tried to implement x86 CPU features independently, even if they never show up in a real CPU independently.

We only add optimized implementations of crypto algorithms if they are actually useful, though. If they would never be used in practice, that's not useful.

- Eric

author
#15 lore raw

Re: [RFC V1 3/7] crypto: ghash - Optimized GHASH computations

In reply to Eric Biggers
Quoted 11 lines · Eric Biggers
Quoted 10 lines · Megha
Quoted 10 lines

Hello Megha,

What is the purpose of this separate GHASH module? GHASH is only used in combination with AES-CTR to produce GCM, and this series already contains a GCM driver.

Do cores exist that implement PCLMULQDQ but not AES-NI?

If not, I think we should be able to drop this patch (and remove the existing PCLMULQDQ GHASH driver as well)

Quoted 8 lines
Quoted 7 lines

AFAIK, dm-verity (authenticated but not encrypted file system) is one use case for authentication only.

Although I am not sure if GHASH is specifically used for this or SHA?

Also, I do not know of any cores that implement PCLMULQDQ and not AES-NI.

dm-verity only uses unkeyed hash algorithms. So no, it doesn't use GHASH.

Hmm, I see. If that is the case, I am not aware of any other use case apart from GCM.

I see that the existing GHASH module in the kernel from 2009. I am not sure if there was a use case then, which now is no longer valid.

There many be out-of-tree kernel modules which may be using it but again its only speculation.

So, in the next version should I remove the existing GHASH module? (And of course remove this patch as well?)

-Megha

Quoted 2 lines

- Eric

#12 lore raw

Re: [RFC V1 3/7] crypto: ghash - Optimized GHASH computations

In reply to Eric Biggers
Quoted 9 lines · Eric Biggers
Quoted 6 lines · Dave Hansen
Quoted 1 line · Megha

Also, I do not know of any cores that implement PCLMULQDQ and not AES-NI.

That's true, bit it's also possible that a hypervisor could enumerate support for PCLMULQDQ and not AES-NI. In general, we've tried to implement x86 CPU features independently, even if they never show up in a real CPU independently.

We only add optimized implementations of crypto algorithms if they are actually useful, though. If they would never be used in practice, that's not useful.

Yes, totally agree. If it's not of practical use, it doesn't get merged.

I just wanted to share what we do for other related but independent CPU features.

#5 lore raw

Re: [RFC V1 2/7] crypto: crct10dif - Accelerated CRC T10 DIF with vectorized instruction

In reply to Megha Dey
Quoted 83 lines · Megha Dey

From: Kyung Min Park <[email protected]>

Update the crc_pcl function that calculates T10 Data Integrity Field CRC16 (CRC T10 DIF) using VPCLMULQDQ instruction. VPCLMULQDQ instruction with AVX-512F adds EVEX encoded 512 bit version of PCLMULQDQ instruction. The advantage comes from packing multiples of 4 * 128 bit data into AVX512 reducing instruction latency.

The glue code in crct10diff module overrides the existing PCLMULQDQ version with the VPCLMULQDQ version when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VPCLMULQDQ instructions
At runtime:
1. VPCLMULQDQ and AVX512VL features are supported on a platform (currently only Icelake)
2. If compiled as built-in module, crct10dif_pclmul.use_avx512 is set at boot time or /sys/module/crct10dif_pclmul/parameters/use_avx512 is set to 1 after boot.
If compiled as loadable module, use_avx512 module parameter must be set:
modprobe crct10dif_pclmul use_avx512=1

A typical run of tcrypt with CRC T10 DIF calculation with PCLMULQDQ instruction and VPCLMULQDQ instruction shows the following results:
For bytes per update >= 1KB, we see the average improvement of 46%(~1.4x) For bytes per update < 1KB, we see the average improvement of 13%. Test was performed on an Icelake based platform with constant frequency set for CPU.

Detailed results for a variety of block sizes and update sizes are in the table below.

---------------------------------------------------------------------------

|            |            |         cycles/operation         |            |
|            |            |       (the lower the better)     |            |
|    byte    |   bytes    |----------------------------------| percentage |
|   blocks   | per update |   CRC T10 DIF  |  CRC T10 DIF    | loss/gain  |
|            |            | with PCLMULQDQ | with VPCLMULQDQ |            |
|------------|------------|----------------|-----------------|------------|
|      16    |     16     |        77      |        106      |   -27.0    |
|      64    |     16     |       411      |        390      |     5.4    |
|      64    |     64     |        71      |         85      |   -16.0    |
|     256    |     16     |      1224      |       1308      |    -6.4    |
|     256    |     64     |       393      |        407      |    -3.4    |
|     256    |    256     |        93      |         86      |     8.1    |
|    1024    |     16     |      4564      |       5020      |    -9.0    |
|    1024    |    256     |       486      |        475      |     2.3    |
|    1024    |   1024     |       221      |        148      |    49.3    |
|    2048    |     16     |      8945      |       9851      |    -9.1    |
|    2048    |    256     |       982      |        951      |     3.3    |
|    2048    |   1024     |       500      |        369      |    35.5    |
|    2048    |   2048     |       413      |        265      |    55.8    |
|    4096    |     16     |     17885      |      19351      |    -7.5    |
|    4096    |    256     |      1828      |       1713      |     6.7    |
|    4096    |   1024     |       968      |        805      |    20.0    |
|    4096    |   4096     |       739      |        475      |    55.6    |
|    8192    |     16     |     48339      |      41556      |    16.3    |
|    8192    |    256     |      3494      |       3342      |     4.5    |
|    8192    |   1024     |      1959      |       1462      |    34.0    |
|    8192    |   4096     |      1561      |       1036      |    50.7    |
|    8192    |   8192     |      1540      |       1004      |    53.4    |

---------------------------------------------------------------------------

This work was inspired by the CRC T10 DIF AVX512 optimization published in Intel Intelligent Storage Acceleration Library.
https://github.com/intel/isa-l/blob/master/crc/crc16_t10dif_by16_10.asm

---
 arch/x86/crypto/Makefile                  |   1 +
 arch/x86/crypto/crct10dif-avx512-asm_64.S | 482 ++++++++++++++++++++++++++++++
 arch/x86/crypto/crct10dif-pclmul_glue.c   |  24 +-
 arch/x86/include/asm/disabled-features.h  |   8 +-
 crypto/Kconfig                            |  23 ++
 5 files changed, 535 insertions(+), 3 deletions(-)
 create mode 100644 arch/x86/crypto/crct10dif-avx512-asm_64.S

...

Quoted 5 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +0 -0
index 71291d5a..26a6350 100644

arch/x86/crypto/crct10dif-pclmul_glue.c line 44

Quoted 10 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +7 -0
@@ -35,6 +35,16 @@
 #include <asm/simd.h>
 
 asmlinkage u16 crc_t10dif_pcl(u16 init_crc, const u8 *buf, size_t len);
+#ifdef CONFIG_CRYPTO_CRCT10DIF_AVX512
+asmlinkage u16 crct10dif_pcl_avx512(u16 init_crc, const u8 *buf, size_t len);
+#else
+static u16 crct10dif_pcl_avx512(u16 init_crc, const u8 *buf, size_t len)
+{ return 0; }
+#endif
+

Please drop the alternative definition. If you code the references correctly, the alternative is never called.

Quoted 7 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +3 -0
+static bool use_avx512;
+module_param(use_avx512, bool, 0644);
+MODULE_PARM_DESC(use_avx512, "Use AVX512 optimized algorithm, if available");
 
 struct chksum_desc_ctx {
        __u16 crc;

arch/x86/crypto/crct10dif-pclmul_glue.c static int chksum_update(struct shash_desc *desc, const u8 *data, · line 74

Quoted 10 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +6 -1
@@ -56,7 +66,12 @@ static int chksum_update(struct shash_desc *desc, const u8 *data,
 
        if (length >= 16 && crypto_simd_usable()) {
                kernel_fpu_begin();
-               ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);
+               if (IS_ENABLED(CONFIG_CRYPTO_CRCT10DIF_AVX512) &&
+                   cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ) &&
+                   use_avx512)
+                       ctx->crc = crct10dif_pcl_avx512(ctx->crc, data, length);
+               else
+                       ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);

Please use a static call or static key here, and initialize its value in the init code.

Quoted 4 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +0 -0
                kernel_fpu_end();
        } else
                ctx->crc = crc_t10dif_generic(ctx->crc, data, length);

arch/x86/crypto/crct10dif-pclmul_glue.c static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out) · line 98

Quoted 10 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +6 -1
@@ -75,7 +90,12 @@ static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out)
 {
        if (len >= 16 && crypto_simd_usable()) {
                kernel_fpu_begin();
-               *(__u16 *)out = crc_t10dif_pcl(crc, data, len);
+               if (IS_ENABLED(CONFIG_CRYPTO_CRCT10DIF_AVX512) &&
+                   cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ) &&
+                   use_avx512)
+                       *(__u16 *)out = crct10dif_pcl_avx512(crc, data, len);
+               else
+                       *(__u16 *)out = crc_t10dif_pcl(crc, data, len);

Same here.

Quoted 66 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +0 -0
                kernel_fpu_end();
        } else
                *(__u16 *)out = crc_t10dif_generic(crc, data, len);
arch/x86/include/asm/disabled-features.h +7 -1
index 5861d34..1192dea 100644
@@ -56,6 +56,12 @@
 # define DISABLE_PTI           (1 << (X86_FEATURE_PTI & 31))
 #endif
 
+#if defined(CONFIG_AS_VPCLMULQDQ)
+# define DISABLE_VPCLMULQDQ    0
+#else
+# define DISABLE_VPCLMULQDQ    (1 << (X86_FEATURE_VPCLMULQDQ & 31))
+#endif
+
 #ifdef CONFIG_IOMMU_SUPPORT
 # define DISABLE_ENQCMD        0
 #else
@@ -82,7 +88,7 @@
 #define DISABLED_MASK14        0
 #define DISABLED_MASK15        0
 #define DISABLED_MASK16        (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \
-                        DISABLE_ENQCMD)
+                        DISABLE_ENQCMD|DISABLE_VPCLMULQDQ)
 #define DISABLED_MASK17        0
 #define DISABLED_MASK18        0
 #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)
crypto/Kconfig +23 -1
index a367fcf..b090f14 100644
@@ -613,6 +613,29 @@ config CRYPTO_CRC32C_VPMSUM
          (vpmsum) instructions, introduced in POWER8. Enable on POWER8
          and newer processors for improved performance.
 
+config CRYPTO_AVX512
+       bool "AVX512 hardware acceleration for crypto algorithms"
+       depends on X86
+       depends on 64BIT
+       help
+         This option will compile in AVX512 hardware accelerated crypto
+         algorithms. These optimized algorithms provide substantial(2-10x)
+         improvements over existing crypto algorithms for large data size.
+         However, it may also incur a frequency penalty (aka. "bin drops")
+         and cause collateral damage to other workloads running on the
+         same core.
+
+# We default CRYPTO_CRCT10DIF_AVX512 to Y but depend on CRYPTO_AVX512 in
+# order to have a singular option (CRYPTO_AVX512) select multiple algorithms
+# when supported. Specifically, if the platform and/or toolset does not
+# support VPLMULQDQ. Then this algorithm should not be supported as part of
+# the set that CRYPTO_AVX512 selects.
+config CRYPTO_CRCT10DIF_AVX512
+       bool
+       default y
+       depends on CRYPTO_AVX512
+       depends on CRYPTO_CRCT10DIF_PCLMUL
+       depends on AS_VPCLMULQDQ
 
 config CRYPTO_CRC32C_SPARC64
        tristate "CRC32c CRC algorithm (SPARC64)"
--

2.7.4

#18 lore raw

Re: [RFC V1 5/7] crypto: aesni - AES CTR x86_64 "by16" AVX512 optimization

In reply to Megha Dey
Quoted 82 lines · Megha Dey

Introduce the "by16" implementation of the AES CTR mode using AVX512 optimizations. "by16" means that 16 independent blocks (each block being 128 bits) can be ciphered simultaneously as opposed to the current 8 blocks.

The glue code in AESNI module overrides the existing "by8" CTR mode encryption/decryption routines with the "by16" ones when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VAES instructions
At runtime:
1. VAES and AVX512VL features are supported on platform (currently only Icelake)
2. aesni_intel.use_avx512 module parameter is set at boot time. For this algorithm, switching from AVX512 optimized version is not possible once set at boot time because of how the code is structured today.(Can be changed later if required)

The functions aes_ctr_enc_128_avx512_by16(), aes_ctr_enc_192_avx512_by16() and aes_ctr_enc_256_avx512_by16() are adapted from Intel Optimized IPSEC Cryptographic library.

On a Icelake desktop, with turbo disabled and all CPUs running at maximum frequency, the "by16" CTR mode optimization shows better performance across data & key sizes as measured by tcrypt.

The average performance improvement of the "by16" version over the "by8" version is as follows:
For all key sizes(128/192/256 bits),
data sizes < 128 bytes/block, negligible improvement(~3% loss) data sizes > 128 bytes/block, there is an average improvement of
48% for both encryption and decryption.

A typical run of tcrypt with AES CTR mode encryption/decryption of the "by8" and "by16" optimization on a Icelake desktop shows the following results:

--------------------------------------------------------------

|  key   | bytes | cycles/op (lower is better)| percentage   |
| length |  per  |  encryption  |  decryption |  loss/gain   |
| (bits) | block |-------------------------------------------|
|        |       | by8  | by16  | by8  | by16 |  enc | dec   |
|------------------------------------------------------------|
|  128   |  16   | 156  | 168   | 164  | 168  | -7.7 |  -2.5 |
|  128   |  64   | 180  | 190   | 157  | 146  | -5.6 |   7.1 |
|  128   |  256  | 248  | 158   | 251  | 161  | 36.3 |  35.9 |
|  128   |  1024 | 633  | 316   | 642  | 319  | 50.1 |  50.4 |
|  128   |  1472 | 853  | 411   | 877  | 407  | 51.9 |  53.6 |
|  128   |  8192 | 4463 | 1959  | 4447 | 1940 | 56.2 |  56.4 |
|  192   |  16   | 136  | 145   | 149  | 166  | -6.7 | -11.5 |
|  192   |  64   | 159  | 154   | 157  | 160  |  3.2 |  -2   |
|  192   |  256  | 268  | 172   | 274  | 177  | 35.9 |  35.5 |
|  192   |  1024 | 710  | 358   | 720  | 355  | 49.6 |  50.7 |
|  192   |  1472 | 989  | 468   | 983  | 469  | 52.7 |  52.3 |
|  192   |  8192 | 6326 | 3551  | 6301 | 3567 | 43.9 |  43.4 |
|  256   |  16   | 153  | 165   | 139  | 156  | -7.9 | -12.3 |
|  256   |  64   | 158  | 152   | 174  | 161  |  3.8 |   7.5 |
|  256   |  256  | 283  | 176   | 287  | 202  | 37.9 |  29.7 |
|  256   |  1024 | 797  | 393   | 807  | 395  | 50.7 |  51.1 |
|  256   |  1472 | 1108 | 534   | 1107 | 527  | 51.9 |  52.4 |
|  256   |  8192 | 5763 | 2616  | 5773 | 2617 | 54.7 |  54.7 |

--------------------------------------------------------------

This work was inspired by the AES CTR mode optimization published in Intel Optimized IPSEC Cryptographic library.
https://github.com/intel/intel-ipsec-mb/blob/master/lib/avx512/cntr_vaes_avx512.asm

---
 arch/x86/crypto/Makefile                    |   1 +
 arch/x86/crypto/aes_ctrby16_avx512-x86_64.S | 856 ++++++++++++++++++++++++++++
 arch/x86/crypto/aesni-intel_glue.c          |  57 +-
 arch/x86/crypto/avx512_vaes_common.S        | 422 ++++++++++++++
 arch/x86/include/asm/disabled-features.h    |   8 +-
 crypto/Kconfig                              |  12 +
 6 files changed, 1354 insertions(+), 2 deletions(-)
 create mode 100644 arch/x86/crypto/aes_ctrby16_avx512-x86_64.S

...

Quoted 38 lines
arch/x86/crypto/aesni-intel_glue.c +23 -0
index ad8a718..f45059e 100644
@@ -46,6 +46,10 @@
 #define CRYPTO_AES_CTX_SIZE (sizeof(struct crypto_aes_ctx) + AESNI_ALIGN_EXTRA)
 #define XTS_AES_CTX_SIZE (sizeof(struct aesni_xts_ctx) + AESNI_ALIGN_EXTRA)
 
+static bool use_avx512;
+module_param(use_avx512, bool, 0644);
+MODULE_PARM_DESC(use_avx512, "Use AVX512 optimized algorithm, if available");
+
 /* This data is stored at the end of the crypto_tfm struct.
  * It's a type of per "session" data storage location.
  * This needs to be 16 byte aligned.
@@ -191,6 +195,35 @@ asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv,
                void *keys, u8 *out, unsigned int num_bytes);
 asmlinkage void aes_ctr_enc_256_avx_by8(const u8 *in, u8 *iv,
                void *keys, u8 *out, unsigned int num_bytes);
+
+#ifdef CONFIG_CRYPTO_AES_CTR_AVX512
+asmlinkage void aes_ctr_enc_128_avx512_by16(void *keys, u8 *out,
+                                           const u8 *in,
+                                           unsigned int num_bytes,
+                                           u8 *iv);
+asmlinkage void aes_ctr_enc_192_avx512_by16(void *keys, u8 *out,
+                                           const u8 *in,
+                                           unsigned int num_bytes,
+                                           u8 *iv);
+asmlinkage void aes_ctr_enc_256_avx512_by16(void *keys, u8 *out,
+                                           const u8 *in,
+                                           unsigned int num_bytes,
+                                           u8 *iv);
+#else
+static inline void aes_ctr_enc_128_avx512_by16(void *keys, u8 *out,
+                                              const u8 *in,
+                                              unsigned int num_bytes,
+                                              u8 *iv) {}

arch/x86/crypto/aesni-intel_glue.c asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv, · line 226

Quoted 10 lines
arch/x86/crypto/aesni-intel_glue.c +10 -0
+static inline void aes_ctr_enc_192_avx512_by16(void *keys, u8 *out,
+                                              const u8 *in,
+                                              unsigned int num_bytes,
+                                              u8 *iv) {}
+static inline void aes_ctr_enc_256_avx512_by16(void *keys, u8 *out,
+                                              const u8 *in,
+                                              unsigned int num_bytes,
+                                              u8 *iv) {}
+#endif
+

Please drop these alternatives.

Quoted 28 lines
arch/x86/crypto/aesni-intel_glue.c +17 -0
 /*
  * asmlinkage void aesni_gcm_init_avx_gen2()
  * gcm_data *my_ctx_data, context data
@@ -487,6 +520,23 @@ static void aesni_ctr_enc_avx_tfm(struct crypto_aes_ctx *ctx, u8 *out,
                aes_ctr_enc_256_avx_by8(in, iv, (void *)ctx, out, len);
 }
 
+static void aesni_ctr_enc_avx512_tfm(struct crypto_aes_ctx *ctx, u8 *out,
+                                    const u8 *in, unsigned int len, u8 *iv)
+{
+       /*
+        * based on key length, override with the by16 version
+        * of ctr mode encryption/decryption for improved performance.
+        * aes_set_key_common() ensures that key length is one of
+        * {128,192,256}
+        */
+       if (ctx->key_length == AES_KEYSIZE_128)
+               aes_ctr_enc_128_avx512_by16((void *)ctx, out, in, len, iv);
+       else if (ctx->key_length == AES_KEYSIZE_192)
+               aes_ctr_enc_192_avx512_by16((void *)ctx, out, in, len, iv);
+       else
+               aes_ctr_enc_256_avx512_by16((void *)ctx, out, in, len, iv);
+}
+
 static int ctr_crypt(struct skcipher_request *req)
 {
        struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);

arch/x86/crypto/aesni-intel_glue.c static int __init aesni_init(void) · line 1133

Quoted 9 lines
arch/x86/crypto/aesni-intel_glue.c +5 -1
@@ -1076,7 +1126,12 @@ static int __init aesni_init(void)
                aesni_gcm_tfm = &aesni_gcm_tfm_sse;
        }
        aesni_ctr_enc_tfm = aesni_ctr_enc;
-       if (boot_cpu_has(X86_FEATURE_AVX)) {
+       if (use_avx512 && IS_ENABLED(CONFIG_CRYPTO_AES_CTR_AVX512) &&
+           cpu_feature_enabled(X86_FEATURE_VAES)) {
+               /* Ctr mode performance optimization using AVX512 */
+               aesni_ctr_enc_tfm = aesni_ctr_enc_avx512_tfm;
+               pr_info("AES CTR mode by16 optimization enabled\n");

This will need to be changed to a static_call_update() once my outstanding patch is merged.

Quoted 4 lines
arch/x86/crypto/aesni-intel_glue.c +1 -0
+       } else if (boot_cpu_has(X86_FEATURE_AVX)) {
                /* optimize performance of ctr mode encryption transform */
                aesni_ctr_enc_tfm = aesni_ctr_enc_avx_tfm;
                pr_info("AES CTR mode by8 optimization enabled\n");
#22 lore raw

Re: [RFC V1 7/7] crypto: aesni - AVX512 version of AESNI-GCM using VPCLMULQDQ

In reply to Megha Dey
Quoted 84 lines · Megha Dey

Introduce the AVX512 implementation that optimizes the AESNI-GCM encode and decode routines using VPCLMULQDQ.

The glue code in AESNI module overrides the existing AVX2 GCM mode encryption/decryption routines with the AX512 AES GCM mode ones when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VPCLMULQDQ instructions
At runtime:
1. VPCLMULQDQ and AVX512VL features are supported on a platform (currently only Icelake)
2. aesni_intel.use_avx512 module parameter is set at boot time. For this algorithm, switching from AVX512 optimized version is not possible once set at boot time because of how the code is structured today.(Can be changed later if required)

The functions aesni_gcm_init_avx_512, aesni_gcm_enc_update_avx_512, aesni_gcm_dec_update_avx_512 and aesni_gcm_finalize_avx_512 are adapted from the Intel Optimized IPSEC Cryptographic library.

On a Icelake desktop, with turbo disabled and all CPUs running at maximum frequency, the AVX512 GCM mode optimization shows better performance across data & key sizes as measured by tcrypt.

The average performance improvement of the AVX512 version over the AVX2 version is as follows:
For all key sizes(128/192/256 bits),
data sizes < 128 bytes/block, negligible improvement (~7.5%) data sizes > 128 bytes/block, there is an average improvement of 40% for both encryption and decryption.

A typical run of tcrypt with AES GCM mode encryption/decryption of the AVX2 and AVX512 optimization on a Icelake desktop shows the following results:

----------------------------------------------------------------------

  |   key  | bytes | cycles/op (lower is better)   | Percentage gain/  |
  | length |   per |   encryption  |  decryption   |      loss         |
  | (bits) | block |-------------------------------|-------------------|
  |        |       | avx2 | avx512 | avx2 | avx512 | Encrypt | Decrypt |
  |---------------------------------------------------------------------
  |  128   | 16    | 689  |  701   | 689  |  707   |  -1.7   |  -2.61  |
  |  128   | 64    | 731  |  660   | 771  |  649   |   9.7   |  15.82  |
  |  128   | 256   | 911  |  750   | 900  |  721   |  17.67  |  19.88  |
  |  128   | 512   | 1181 |  814   | 1161 |  782   |  31.07  |  32.64  |
  |  128   | 1024  | 1676 |  1052  | 1685 |  1030  |  37.23  |  38.87  |
  |  128   | 2048  | 2475 |  1447  | 2456 |  1419  |  41.53  |  42.22  |
  |  128   | 4096  | 3806 |  2154  | 3820 |  2119  |  43.41  |  44.53  |
  |  128   | 8192  | 9169 |  3806  | 6997 |  3718  |  58.49  |  46.86  |
  |  192   | 16    | 754  |  683   | 737  |  672   |   9.42  |   8.82  |
  |  192   | 64    | 735  |  686   | 715  |  640   |   6.66  |  10.49  |
  |  192   | 256   | 949  |  738   | 2435 |  729   |  22.23  |  70     |
  |  192   | 512   | 1235 |  854   | 1200 |  833   |  30.85  |  30.58  |
  |  192   | 1024  | 1777 |  1084  | 1763 |  1051  |  38.99  |  40.39  |
  |  192   | 2048  | 2574 |  1497  | 2592 |  1459  |  41.84  |  43.71  |
  |  192   | 4096  | 4086 |  2317  | 4091 |  2244  |  43.29  |  45.14  |
  |  192   | 8192  | 7481 |  4054  | 7505 |  3953  |  45.81  |  47.32  |
  |  256   | 16    | 755  |  682   | 720  |  683   |   9.68  |   5.14  |
  |  256   | 64    | 744  |  677   | 719  |  658   |   9     |   8.48  |
  |  256   | 256   | 962  |  758   | 948  |  749   |  21.21  |  21     |
  |  256   | 512   | 1297 |  862   | 1276 |  836   |  33.54  |  34.48  |
  |  256   | 1024  | 1831 |  1114  | 1819 |  1095  |  39.16  |  39.8   |
  |  256   | 2048  | 2767 |  1566  | 2715 |  1524  |  43.4   |  43.87  |
  |  256   | 4096  | 4378 |  2382  | 4368 |  2354  |  45.6   |  46.11  |
  |  256   | 8192  | 8075 |  4262  | 8080 |  4186  |  47.22  |  48.19  |

----------------------------------------------------------------------

This work was inspired by the AES GCM mode optimization published in Intel Optimized IPSEC Cryptographic library.
https://github.com/intel/intel-ipsec-mb/blob/master/lib/avx512/gcm_vaes_avx512.asm

---
 arch/x86/crypto/Makefile                    |    1 +
 arch/x86/crypto/aesni-intel_avx512-x86_64.S | 1788 +++++++++++++++++++++++++++
 arch/x86/crypto/aesni-intel_glue.c          |   62 +-
 crypto/Kconfig                              |   12 +
 4 files changed, 1858 insertions(+), 5 deletions(-)
 create mode 100644 arch/x86/crypto/aesni-intel_avx512-x86_64.S

...

Quoted 24 lines
arch/x86/crypto/aesni-intel_glue.c +5 -2
index 9e56cdf..8fc5bac 100644
@@ -55,13 +55,16 @@ MODULE_PARM_DESC(use_avx512, "Use AVX512 optimized algorithm, if available");
  * This needs to be 16 byte aligned.
  */
 struct aesni_rfc4106_gcm_ctx {
-       u8 hash_subkey[16] AESNI_ALIGN_ATTR;
+       /* AVX512 optimized algorithms use 48 hash keys to conduct
+        * multiple PCLMULQDQ operations in parallel
+        */
+       u8 hash_subkey[16 * 48] AESNI_ALIGN_ATTR;
        struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR;
        u8 nonce[4];
 };
 
 struct generic_gcmaes_ctx {
-       u8 hash_subkey[16] AESNI_ALIGN_ATTR;
+       u8 hash_subkey[16 * 48] AESNI_ALIGN_ATTR;
        struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR;
 };
 

arch/x86/crypto/aesni-intel_glue.c struct gcm_context_data { · line 90

Quoted 7 lines
arch/x86/crypto/aesni-intel_glue.c +1 -1
@@ -82,7 +85,7 @@ struct gcm_context_data {
        u8 current_counter[GCM_BLOCK_LEN];
        u64 partial_block_len;
        u64 unused;
-       u8 hash_keys[GCM_BLOCK_LEN * 16];
+       u8 hash_keys[48 * 16];
 };
 

This structure gets allocated on the stack, and gets inflated significantly by this change, even though the code is not enabled by default, and not even supported for most users.

Is it really necessary for this to be per-request data? If these are precomputed powers of H, they can be moved into the TFM context structure instead, which lives on the heap (and can be shared by all concurrent users of the TFM)

Quoted 36 lines
arch/x86/crypto/aesni-intel_glue.c +31 -0
 asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
@@ -266,6 +269,47 @@ static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_gen2 = {
        .finalize = &aesni_gcm_finalize_avx_gen2,
 };
 
+#ifdef CONFIG_CRYPTO_AES_GCM_AVX512
+/*
+ * asmlinkage void aesni_gcm_init_avx_512()
+ * gcm_data *my_ctx_data, context data
+ * u8 *hash_subkey,  the Hash sub key input. Data starts on a 16-byte boundary.
+ */
+asmlinkage void aesni_gcm_init_avx_512(void *my_ctx_data,
+                                      struct gcm_context_data *gdata,
+                                      u8 *iv,
+                                      u8 *hash_subkey,
+                                      const u8 *aad,
+                                      unsigned long aad_len);
+asmlinkage void aesni_gcm_enc_update_avx_512(void *ctx,
+                                            struct gcm_context_data *gdata,
+                                            u8 *out,
+                                            const u8 *in,
+                                            unsigned long plaintext_len);
+asmlinkage void aesni_gcm_dec_update_avx_512(void *ctx,
+                                            struct gcm_context_data *gdata,
+                                            u8 *out,
+                                            const u8 *in,
+                                            unsigned long ciphertext_len);
+asmlinkage void aesni_gcm_finalize_avx_512(void *ctx,
+                                          struct gcm_context_data *gdata,
+                                          u8 *auth_tag,
+                                          unsigned long auth_tag_len);
+
+asmlinkage void aes_gcm_precomp_avx_512(struct crypto_aes_ctx *ctx, u8 *hash_subkey);
+
+static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_512 = {
+       .init = &aesni_gcm_init_avx_512,

arch/x86/crypto/aesni-intel_glue.c static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_gen2 = { · line 312

Quoted 10 lines
arch/x86/crypto/aesni-intel_glue.c +10 -0
+       .enc_update = &aesni_gcm_enc_update_avx_512,
+       .dec_update = &aesni_gcm_dec_update_avx_512,
+       .finalize = &aesni_gcm_finalize_avx_512,
+};
+#else
+static void aes_gcm_precomp_avx_512(struct crypto_aes_ctx *ctx, u8 *hash_subkey)
+{}
+static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_512 = {};
+#endif
+

Please drop the alternative dummy definitions.

Quoted 4 lines
arch/x86/crypto/aesni-intel_glue.c +0 -0
 /*
  * asmlinkage void aesni_gcm_init_avx_gen4()
  * gcm_data *my_ctx_data, context data

arch/x86/crypto/aesni-intel_glue.c rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len) · line 721

Quoted 10 lines
arch/x86/crypto/aesni-intel_glue.c +5 -1
@@ -669,7 +713,11 @@ rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
        /* We want to cipher all zeros to create the hash sub key. */
        memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
 
-       aes_encrypt(&ctx, hash_subkey, hash_subkey);
+       if (IS_ENABLED(CONFIG_CRYPTO_AES_GCM_AVX512) && use_avx512 &&
+           cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ))
+               aes_gcm_precomp_avx_512(&ctx, hash_subkey);
+       else
+               aes_encrypt(&ctx, hash_subkey, hash_subkey);
 

I suppose this answers my question about the subkeys. Please find a way to move these out of struct gcm_context_data so they don't need to be copied to the stack for each request.

arch/x86/crypto/aesni-intel_glue.c static int __init aesni_init(void) · line 1168

Quoted 11 lines
arch/x86/crypto/aesni-intel_glue.c +4 -1
        memzero_explicit(&ctx, sizeof(ctx));
        return 0;
@@ -1114,7 +1162,11 @@ static int __init aesni_init(void)
        if (!x86_match_cpu(aesni_cpu_id))
                return -ENODEV;
 #ifdef CONFIG_X86_64
-       if (boot_cpu_has(X86_FEATURE_AVX2)) {
+       if (use_avx512 && IS_ENABLED(CONFIG_CRYPTO_AES_GCM_AVX512) &&
+           cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ)) {
+               pr_info("AVX512 version of gcm_enc/dec engaged.\n");
+               aesni_gcm_tfm = &aesni_gcm_tfm_avx_512;

This was changed in the cryptodev tree to use static keys.

Quoted 30 lines
arch/x86/crypto/aesni-intel_glue.c +1 -0
+       } else if (boot_cpu_has(X86_FEATURE_AVX2)) {
                pr_info("AVX2 version of gcm_enc/dec engaged.\n");
                aesni_gcm_tfm = &aesni_gcm_tfm_avx_gen4;
        } else if (boot_cpu_has(X86_FEATURE_AVX)) {
crypto/Kconfig +12 -1
index 3043849..8c8a68d 100644
@@ -661,6 +661,18 @@ config CRYPTO_AES_CTR_AVX512
        depends on CRYPTO_AES_NI_INTEL
        depends on AS_VAES_AVX512
 
+# We default CRYPTO_AES_GCM_AVX512 to Y but depend on CRYPTO_AVX512 in
+# order to have a singular option (CRYPTO_AVX512) select multiple algorithms
+# when supported. Specifically, if the platform and/or toolset does not
+# support VPLMULQDQ. Then this algorithm should not be supported as part of
+# the set that CRYPTO_AVX512 selects.
+config CRYPTO_AES_GCM_AVX512
+       bool
+       default y
+       depends on CRYPTO_AVX512
+       depends on CRYPTO_AES_NI_INTEL
+       depends on AS_VPCLMULQDQ
+
 config CRYPTO_CRC32C_SPARC64
        tristate "CRC32c CRC algorithm (SPARC64)"
        depends on SPARC64
--

2.7.4

#2 lore raw

Re: [RFC V1 1/7] x86: Probe assembler capabilities for VAES and VPLCMULQDQ support

In reply to Megha Dey
Quoted 17 lines · Megha Dey

This is a preparatory patch to introduce the optimized crypto algorithms using AVX512 instructions which would require VAES and VPLCMULQDQ support.

Check for VAES and VPCLMULQDQ assembler support using AVX512 registers.

---
 arch/x86/Kconfig.assembler | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/Kconfig.assembler +0 -0
index 26b8c08..9ea0bc8 100644

arch/x86/Kconfig.assembler line 5

Quoted 5 lines
arch/x86/Kconfig.assembler +2 -0
@@ -1,6 +1,16 @@
 # SPDX-License-Identifier: GPL-2.0
 # Copyright (C) 2020 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
 
+config AS_VAES_AVX512
+       def_bool $(as-instr,vaesenc %zmm0$(comma)%zmm1$(comma)%zmm1) && 64BIT

Is the '&& 64BIT' necessary here, but not below?

In any case, better to use a separate 'depends on' line, for legibility

Quoted 14 lines
arch/x86/Kconfig.assembler +8 -1
+       help
+         Supported by binutils >= 2.30 and LLVM integrated assembler
+
+config AS_VPCLMULQDQ
+       def_bool $(as-instr,vpclmulqdq \$0$(comma)%zmm2$(comma)%zmm6$(comma)%zmm4)
+       help
+         Supported by binutils >= 2.30 and LLVM integrated assembler
+
 config AS_AVX512
        def_bool $(as-instr,vpmovm2b %k1$(comma)%zmm5)
        help
--

2.7.4

#13 lore raw

Re: [RFC V1 3/7] crypto: ghash - Optimized GHASH computations

In reply to Dave Hansen
Quoted 9 lines · Dave Hansen
Quoted 7 lines · Eric Biggers
Quoted 6 lines · Dave Hansen
Quoted 1 line · Megha

Also, I do not know of any cores that implement PCLMULQDQ and not AES-NI.

That's true, bit it's also possible that a hypervisor could enumerate support for PCLMULQDQ and not AES-NI. In general, we've tried to implement x86 CPU features independently, even if they never show up in a real CPU independently.

Quoted 7 lines
Quoted 2 lines

We only add optimized implementations of crypto algorithms if they are actually useful, though. If they would never be used in practice, that's not useful.

Yes, totally agree. If it's not of practical use, it doesn't get merged.

I just wanted to share what we do for other related but independent CPU features.

Thanks for the insight.

The issue with the current GHASH driver is that it uses infrastructure that we may decide to remove (the async cryptd helper [0]). So adding more dependencies on that without any proven benefit should obviously be avoided at this time as well.

[0] https://lore.kernel.org/linux-arm-kernel/[email protected]/

#26 lore raw
In reply to Dey, Megha
Quoted 21 lines · Megha

Hi Eric,

Quoted 17 lines · Eric Biggers
Quoted 16 lines · Megha Dey

Optimize crypto algorithms using VPCLMULQDQ and VAES AVX512 instructions (first implemented on Intel's Icelake client and Xeon CPUs).

These algorithms take advantage of the AVX512 registers to keep the CPU busy and increase memory bandwidth utilization. They provide substantial (2-10x) improvements over existing crypto algorithms when update data size is greater than 128 bytes and do not have any significant impact when used on small amounts of data.

However, these algorithms may also incur a frequency penalty and cause collateral damage to other workloads running on the same core(co-scheduled threads). These frequency drops are also known as bin drops where 1 bin drop is around 100MHz. With the SpecCPU and ffmpeg benchmark, a 0-1 bin drop(0-100MHz) is observed on Icelake desktop and 0-2 bin drops (0-200Mhz) are observed on the Icelake server.

Quoted 8 lines
Quoted 2 lines

Do these new algorithms all pass the self-tests, including the fuzz tests that are enabled when CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y?

I had tested these algorithms with CRYPTO_MANAGER_DISABLE_TESTS=n and tcrypt, not with
CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y (I wasn't aware this existed, my bad). I see a couple of errors after enabling it and am working on fixing those.

Hello Megha,

I think the GHASH changes can be dropped (as discussed in the other thread), given the lack of a use case. The existing GHASH driver could also be removed in the future, but I don't think it needs to be part of this series.

Could you please rebase this onto the latest AES-NI changes that are in Herbert's tree? (as well as the ones I sent out today) They address some issues with indirect calls and excessive disabling of preemption, and your GCM and CTR changes are definitely going to be affected by this as well.

author
#27 lore raw

Hi Ard,

Quoted 28 lines · Ard Biesheuvel
Quoted 20 lines · Megha

Hi Eric,

Quoted 17 lines · Eric Biggers
Quoted 16 lines · Megha Dey

Optimize crypto algorithms using VPCLMULQDQ and VAES AVX512 instructions (first implemented on Intel's Icelake client and Xeon CPUs).

These algorithms take advantage of the AVX512 registers to keep the CPU busy and increase memory bandwidth utilization. They provide substantial (2-10x) improvements over existing crypto algorithms when update data size is greater than 128 bytes and do not have any significant impact when used on small amounts of data.

However, these algorithms may also incur a frequency penalty and cause collateral damage to other workloads running on the same core(co-scheduled threads). These frequency drops are also known as bin drops where 1 bin drop is around 100MHz. With the SpecCPU and ffmpeg benchmark, a 0-1 bin drop(0-100MHz) is observed on Icelake desktop and 0-2 bin drops (0-200Mhz) are observed on the Icelake server.

Quoted 7 lines
Quoted 2 lines

Do these new algorithms all pass the self-tests, including the fuzz tests that are enabled when CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y?

I had tested these algorithms with CRYPTO_MANAGER_DISABLE_TESTS=n and tcrypt, not with
CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y (I wasn't aware this existed, my bad). I see a couple of errors after enabling it and am working on fixing those.

Quoted 6 lines

Hello Megha,

I think the GHASH changes can be dropped (as discussed in the other thread), given the lack of a use case. The existing GHASH driver could also be removed in the future, but I don't think it needs to be part of this series.

Ok, I will remove the GHASH patch from the next series.

Quoted 6 lines

Could you please rebase this onto the latest AES-NI changes that are in Herbert's tree? (as well as the ones I sent out today) They address some issues with indirect calls and excessive disabling of preemption, and your GCM and CTR changes are definitely going to be affected by this as well.

Yeah sure, will do, thanks for the headsup!

author
#6 lore raw

Re: [RFC V1 2/7] crypto: crct10dif - Accelerated CRC T10 DIF with vectorized instruction

In reply to Ard Biesheuvel

Hi Ard,

Quoted 91 lines · Ard Biesheuvel
Quoted 82 lines · Megha Dey

From: Kyung Min Park <[email protected]>

Update the crc_pcl function that calculates T10 Data Integrity Field CRC16 (CRC T10 DIF) using VPCLMULQDQ instruction. VPCLMULQDQ instruction with AVX-512F adds EVEX encoded 512 bit version of PCLMULQDQ instruction. The advantage comes from packing multiples of 4 * 128 bit data into AVX512 reducing instruction latency.

The glue code in crct10diff module overrides the existing PCLMULQDQ version with the VPCLMULQDQ version when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VPCLMULQDQ instructions
At runtime:
1. VPCLMULQDQ and AVX512VL features are supported on a platform (currently only Icelake)
2. If compiled as built-in module, crct10dif_pclmul.use_avx512 is set at boot time or /sys/module/crct10dif_pclmul/parameters/use_avx512 is set to 1 after boot.
If compiled as loadable module, use_avx512 module parameter must be set:
modprobe crct10dif_pclmul use_avx512=1

A typical run of tcrypt with CRC T10 DIF calculation with PCLMULQDQ instruction and VPCLMULQDQ instruction shows the following results:
For bytes per update >= 1KB, we see the average improvement of 46%(~1.4x) For bytes per update < 1KB, we see the average improvement of 13%. Test was performed on an Icelake based platform with constant frequency set for CPU.

Detailed results for a variety of block sizes and update sizes are in the table below.

---------------------------------------------------------------------------

|            |            |         cycles/operation         |            |
|            |            |       (the lower the better)     |            |
|    byte    |   bytes    |----------------------------------| percentage |
|   blocks   | per update |   CRC T10 DIF  |  CRC T10 DIF    | loss/gain  |
|            |            | with PCLMULQDQ | with VPCLMULQDQ |            |
|------------|------------|----------------|-----------------|------------|
|      16    |     16     |        77      |        106      |   -27.0    |
|      64    |     16     |       411      |        390      |     5.4    |
|      64    |     64     |        71      |         85      |   -16.0    |
|     256    |     16     |      1224      |       1308      |    -6.4    |
|     256    |     64     |       393      |        407      |    -3.4    |
|     256    |    256     |        93      |         86      |     8.1    |
|    1024    |     16     |      4564      |       5020      |    -9.0    |
|    1024    |    256     |       486      |        475      |     2.3    |
|    1024    |   1024     |       221      |        148      |    49.3    |
|    2048    |     16     |      8945      |       9851      |    -9.1    |
|    2048    |    256     |       982      |        951      |     3.3    |
|    2048    |   1024     |       500      |        369      |    35.5    |
|    2048    |   2048     |       413      |        265      |    55.8    |
|    4096    |     16     |     17885      |      19351      |    -7.5    |
|    4096    |    256     |      1828      |       1713      |     6.7    |
|    4096    |   1024     |       968      |        805      |    20.0    |
|    4096    |   4096     |       739      |        475      |    55.6    |
|    8192    |     16     |     48339      |      41556      |    16.3    |
|    8192    |    256     |      3494      |       3342      |     4.5    |
|    8192    |   1024     |      1959      |       1462      |    34.0    |
|    8192    |   4096     |      1561      |       1036      |    50.7    |
|    8192    |   8192     |      1540      |       1004      |    53.4    |

---------------------------------------------------------------------------

This work was inspired by the CRC T10 DIF AVX512 optimization published in Intel Intelligent Storage Acceleration Library.
https://github.com/intel/isa-l/blob/master/crc/crc16_t10dif_by16_10.asm

---
  arch/x86/crypto/Makefile                  |   1 +
  arch/x86/crypto/crct10dif-avx512-asm_64.S | 482 ++++++++++++++++++++++++++++++
  arch/x86/crypto/crct10dif-pclmul_glue.c   |  24 +-
  arch/x86/include/asm/disabled-features.h  |   8 +-
  crypto/Kconfig                            |  23 ++
  5 files changed, 535 insertions(+), 3 deletions(-)

create mode 100644 arch/x86/crypto/crct10dif-avx512-asm_64.S

...

Quoted 7 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +0 -0
index 71291d5a..26a6350 100644
@@ -35,6 +35,16 @@
  #include <asm/simd.h>
 
Quoted 10 lines

arch/x86/crypto/crct10dif-pclmul_glue.c line 44

Quoted 8 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +7 -0
  asmlinkage u16 crc_t10dif_pcl(u16 init_crc, const u8 *buf, size_t len);
+#ifdef CONFIG_CRYPTO_CRCT10DIF_AVX512
+asmlinkage u16 crct10dif_pcl_avx512(u16 init_crc, const u8 *buf, size_t len);
+#else
+static u16 crct10dif_pcl_avx512(u16 init_crc, const u8 *buf, size_t len)
+{ return 0; }
+#endif
+

Please drop the alternative definition. If you code the references correctly, the alternative is never called.

ok.

Quoted 9 lines
Quoted 9 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +3 -0
+static bool use_avx512;
+module_param(use_avx512, bool, 0644);
+MODULE_PARM_DESC(use_avx512, "Use AVX512 optimized algorithm, if available");
 
  struct chksum_desc_ctx {
         __u16 crc;
@@ -56,7 +66,12 @@ static int chksum_update(struct shash_desc *desc, const u8 *data,
 
Quoted 11 lines

arch/x86/crypto/crct10dif-pclmul_glue.c static int chksum_update(struct shash_desc *desc, const u8 *data, · line 74

Quoted 9 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +6 -1
         if (length >= 16 && crypto_simd_usable()) {
                 kernel_fpu_begin();
-               ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);
+               if (IS_ENABLED(CONFIG_CRYPTO_CRCT10DIF_AVX512) &&
+                   cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ) &&
+                   use_avx512)
+                       ctx->crc = crct10dif_pcl_avx512(ctx->crc, data, length);
+               else
+                       ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);

Please use a static call or static key here, and initialize its value in the init code.

Yeah, Ill make the change.

Quoted 16 lines
Quoted 5 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +0 -0
                 kernel_fpu_end();
         } else
                 ctx->crc = crc_t10dif_generic(ctx->crc, data, length);

arch/x86/crypto/crct10dif-pclmul_glue.c static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out) · line 98

Quoted 10 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +6 -1
@@ -75,7 +90,12 @@ static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out)
  {
         if (len >= 16 && crypto_simd_usable()) {
                 kernel_fpu_begin();
-               *(__u16 *)out = crc_t10dif_pcl(crc, data, len);
+               if (IS_ENABLED(CONFIG_CRYPTO_CRCT10DIF_AVX512) &&
+                   cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ) &&
+                   use_avx512)
+                       *(__u16 *)out = crct10dif_pcl_avx512(crc, data, len);
+               else
+                       *(__u16 *)out = crc_t10dif_pcl(crc, data, len);

Same here.

will do

-Megha

Quoted 67 lines
Quoted 67 lines
arch/x86/crypto/crct10dif-pclmul_glue.c +0 -0
                 kernel_fpu_end();
         } else
                 *(__u16 *)out = crc_t10dif_generic(crc, data, len);
arch/x86/include/asm/disabled-features.h +7 -1
index 5861d34..1192dea 100644
@@ -56,6 +56,12 @@
  # define DISABLE_PTI           (1 << (X86_FEATURE_PTI & 31))
  #endif
 
+#if defined(CONFIG_AS_VPCLMULQDQ)
+# define DISABLE_VPCLMULQDQ    0
+#else
+# define DISABLE_VPCLMULQDQ    (1 << (X86_FEATURE_VPCLMULQDQ & 31))
+#endif
+
  #ifdef CONFIG_IOMMU_SUPPORT
  # define DISABLE_ENQCMD        0
  #else
@@ -82,7 +88,7 @@
  #define DISABLED_MASK14        0
  #define DISABLED_MASK15        0
  #define DISABLED_MASK16        (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \
-                        DISABLE_ENQCMD)
+                        DISABLE_ENQCMD|DISABLE_VPCLMULQDQ)
  #define DISABLED_MASK17        0
  #define DISABLED_MASK18        0
  #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)
crypto/Kconfig +23 -1
index a367fcf..b090f14 100644
@@ -613,6 +613,29 @@ config CRYPTO_CRC32C_VPMSUM
           (vpmsum) instructions, introduced in POWER8. Enable on POWER8
           and newer processors for improved performance.
 
+config CRYPTO_AVX512
+       bool "AVX512 hardware acceleration for crypto algorithms"
+       depends on X86
+       depends on 64BIT
+       help
+         This option will compile in AVX512 hardware accelerated crypto
+         algorithms. These optimized algorithms provide substantial(2-10x)
+         improvements over existing crypto algorithms for large data size.
+         However, it may also incur a frequency penalty (aka. "bin drops")
+         and cause collateral damage to other workloads running on the
+         same core.
+
+# We default CRYPTO_CRCT10DIF_AVX512 to Y but depend on CRYPTO_AVX512 in
+# order to have a singular option (CRYPTO_AVX512) select multiple algorithms
+# when supported. Specifically, if the platform and/or toolset does not
+# support VPLMULQDQ. Then this algorithm should not be supported as part of
+# the set that CRYPTO_AVX512 selects.
+config CRYPTO_CRCT10DIF_AVX512
+       bool
+       default y
+       depends on CRYPTO_AVX512
+       depends on CRYPTO_CRCT10DIF_PCLMUL
+       depends on AS_VPCLMULQDQ
 
  config CRYPTO_CRC32C_SPARC64
         tristate "CRC32c CRC algorithm (SPARC64)"
--

2.7.4

author
#19 lore raw

Re: [RFC V1 5/7] crypto: aesni - AES CTR x86_64 "by16" AVX512 optimization

In reply to Ard Biesheuvel

Hi Ard,

Quoted 124 lines · Ard Biesheuvel
Quoted 81 lines · Megha Dey

Introduce the "by16" implementation of the AES CTR mode using AVX512 optimizations. "by16" means that 16 independent blocks (each block being 128 bits) can be ciphered simultaneously as opposed to the current 8 blocks.

The glue code in AESNI module overrides the existing "by8" CTR mode encryption/decryption routines with the "by16" ones when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VAES instructions
At runtime:
1. VAES and AVX512VL features are supported on platform (currently only Icelake)
2. aesni_intel.use_avx512 module parameter is set at boot time. For this algorithm, switching from AVX512 optimized version is not possible once set at boot time because of how the code is structured today.(Can be changed later if required)

The functions aes_ctr_enc_128_avx512_by16(), aes_ctr_enc_192_avx512_by16() and aes_ctr_enc_256_avx512_by16() are adapted from Intel Optimized IPSEC Cryptographic library.

On a Icelake desktop, with turbo disabled and all CPUs running at maximum frequency, the "by16" CTR mode optimization shows better performance across data & key sizes as measured by tcrypt.

The average performance improvement of the "by16" version over the "by8" version is as follows:
For all key sizes(128/192/256 bits),
data sizes < 128 bytes/block, negligible improvement(~3% loss) data sizes > 128 bytes/block, there is an average improvement of
48% for both encryption and decryption.

A typical run of tcrypt with AES CTR mode encryption/decryption of the "by8" and "by16" optimization on a Icelake desktop shows the following results:

--------------------------------------------------------------

|  key   | bytes | cycles/op (lower is better)| percentage   |
| length |  per  |  encryption  |  decryption |  loss/gain   |
| (bits) | block |-------------------------------------------|
|        |       | by8  | by16  | by8  | by16 |  enc | dec   |
|------------------------------------------------------------|
|  128   |  16   | 156  | 168   | 164  | 168  | -7.7 |  -2.5 |
|  128   |  64   | 180  | 190   | 157  | 146  | -5.6 |   7.1 |
|  128   |  256  | 248  | 158   | 251  | 161  | 36.3 |  35.9 |
|  128   |  1024 | 633  | 316   | 642  | 319  | 50.1 |  50.4 |
|  128   |  1472 | 853  | 411   | 877  | 407  | 51.9 |  53.6 |
|  128   |  8192 | 4463 | 1959  | 4447 | 1940 | 56.2 |  56.4 |
|  192   |  16   | 136  | 145   | 149  | 166  | -6.7 | -11.5 |
|  192   |  64   | 159  | 154   | 157  | 160  |  3.2 |  -2   |
|  192   |  256  | 268  | 172   | 274  | 177  | 35.9 |  35.5 |
|  192   |  1024 | 710  | 358   | 720  | 355  | 49.6 |  50.7 |
|  192   |  1472 | 989  | 468   | 983  | 469  | 52.7 |  52.3 |
|  192   |  8192 | 6326 | 3551  | 6301 | 3567 | 43.9 |  43.4 |
|  256   |  16   | 153  | 165   | 139  | 156  | -7.9 | -12.3 |
|  256   |  64   | 158  | 152   | 174  | 161  |  3.8 |   7.5 |
|  256   |  256  | 283  | 176   | 287  | 202  | 37.9 |  29.7 |
|  256   |  1024 | 797  | 393   | 807  | 395  | 50.7 |  51.1 |
|  256   |  1472 | 1108 | 534   | 1107 | 527  | 51.9 |  52.4 |
|  256   |  8192 | 5763 | 2616  | 5773 | 2617 | 54.7 |  54.7 |

--------------------------------------------------------------

This work was inspired by the AES CTR mode optimization published in Intel Optimized IPSEC Cryptographic library.
https://github.com/intel/intel-ipsec-mb/blob/master/lib/avx512/cntr_vaes_avx512.asm

---
  arch/x86/crypto/Makefile                    |   1 +
  arch/x86/crypto/aes_ctrby16_avx512-x86_64.S | 856 ++++++++++++++++++++++++++++
  arch/x86/crypto/aesni-intel_glue.c          |  57 +-
  arch/x86/crypto/avx512_vaes_common.S        | 422 ++++++++++++++
  arch/x86/include/asm/disabled-features.h    |   8 +-
  crypto/Kconfig                              |  12 +
  6 files changed, 1354 insertions(+), 2 deletions(-)

create mode 100644 arch/x86/crypto/aes_ctrby16_avx512-x86_64.S

...

Quoted 41 lines
arch/x86/crypto/aesni-intel_glue.c +26 -0
index ad8a718..f45059e 100644
@@ -46,6 +46,10 @@
  #define CRYPTO_AES_CTX_SIZE (sizeof(struct crypto_aes_ctx) + AESNI_ALIGN_EXTRA)
  #define XTS_AES_CTX_SIZE (sizeof(struct aesni_xts_ctx) + AESNI_ALIGN_EXTRA)
 
+static bool use_avx512;
+module_param(use_avx512, bool, 0644);
+MODULE_PARM_DESC(use_avx512, "Use AVX512 optimized algorithm, if available");
+
  /* This data is stored at the end of the crypto_tfm struct.
   * It's a type of per "session" data storage location.
   * This needs to be 16 byte aligned.
@@ -191,6 +195,35 @@ asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv,
                 void *keys, u8 *out, unsigned int num_bytes);
  asmlinkage void aes_ctr_enc_256_avx_by8(const u8 *in, u8 *iv,
                 void *keys, u8 *out, unsigned int num_bytes);
+
+#ifdef CONFIG_CRYPTO_AES_CTR_AVX512
+asmlinkage void aes_ctr_enc_128_avx512_by16(void *keys, u8 *out,
+                                           const u8 *in,
+                                           unsigned int num_bytes,
+                                           u8 *iv);
+asmlinkage void aes_ctr_enc_192_avx512_by16(void *keys, u8 *out,
+                                           const u8 *in,
+                                           unsigned int num_bytes,
+                                           u8 *iv);
+asmlinkage void aes_ctr_enc_256_avx512_by16(void *keys, u8 *out,
+                                           const u8 *in,
+                                           unsigned int num_bytes,
+                                           u8 *iv);
+#else
+static inline void aes_ctr_enc_128_avx512_by16(void *keys, u8 *out,
+                                              const u8 *in,
+                                              unsigned int num_bytes,
+                                              u8 *iv) {}
+static inline void aes_ctr_enc_192_avx512_by16(void *keys, u8 *out,
+                                              const u8 *in,
+                                              unsigned int num_bytes,
Quoted 8 lines

arch/x86/crypto/aesni-intel_glue.c asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv, · line 226

Quoted 7 lines
arch/x86/crypto/aesni-intel_glue.c +7 -0
+                                              u8 *iv) {}
+static inline void aes_ctr_enc_256_avx512_by16(void *keys, u8 *out,
+                                              const u8 *in,
+                                              unsigned int num_bytes,
+                                              u8 *iv) {}
+#endif
+

Please drop these alternatives.

ok

Quoted 32 lines
Quoted 32 lines
arch/x86/crypto/aesni-intel_glue.c +17 -0
  /*
   * asmlinkage void aesni_gcm_init_avx_gen2()
   * gcm_data *my_ctx_data, context data
@@ -487,6 +520,23 @@ static void aesni_ctr_enc_avx_tfm(struct crypto_aes_ctx *ctx, u8 *out,
                 aes_ctr_enc_256_avx_by8(in, iv, (void *)ctx, out, len);
  }
 
+static void aesni_ctr_enc_avx512_tfm(struct crypto_aes_ctx *ctx, u8 *out,
+                                    const u8 *in, unsigned int len, u8 *iv)
+{
+       /*
+        * based on key length, override with the by16 version
+        * of ctr mode encryption/decryption for improved performance.
+        * aes_set_key_common() ensures that key length is one of
+        * {128,192,256}
+        */
+       if (ctx->key_length == AES_KEYSIZE_128)
+               aes_ctr_enc_128_avx512_by16((void *)ctx, out, in, len, iv);
+       else if (ctx->key_length == AES_KEYSIZE_192)
+               aes_ctr_enc_192_avx512_by16((void *)ctx, out, in, len, iv);
+       else
+               aes_ctr_enc_256_avx512_by16((void *)ctx, out, in, len, iv);
+}
+
  static int ctr_crypt(struct skcipher_request *req)
  {
         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
@@ -1076,7 +1126,12 @@ static int __init aesni_init(void)
                 aesni_gcm_tfm = &aesni_gcm_tfm_sse;
         }
         aesni_ctr_enc_tfm = aesni_ctr_enc;
Quoted 8 lines

arch/x86/crypto/aesni-intel_glue.c static int __init aesni_init(void) · line 1133

Quoted 6 lines
arch/x86/crypto/aesni-intel_glue.c +5 -1
-       if (boot_cpu_has(X86_FEATURE_AVX)) {
+       if (use_avx512 && IS_ENABLED(CONFIG_CRYPTO_AES_CTR_AVX512) &&
+           cpu_feature_enabled(X86_FEATURE_VAES)) {
+               /* Ctr mode performance optimization using AVX512 */
+               aesni_ctr_enc_tfm = aesni_ctr_enc_avx512_tfm;
+               pr_info("AES CTR mode by16 optimization enabled\n");

This will need to be changed to a static_call_update() once my outstanding patch is merged.

yeah will do!

Quoted 5 lines
Quoted 5 lines
arch/x86/crypto/aesni-intel_glue.c +1 -0
+       } else if (boot_cpu_has(X86_FEATURE_AVX)) {
                 /* optimize performance of ctr mode encryption transform */
                 aesni_ctr_enc_tfm = aesni_ctr_enc_avx_tfm;
                 pr_info("AES CTR mode by8 optimization enabled\n");
author
#23 lore raw

Re: [RFC V1 7/7] crypto: aesni - AVX512 version of AESNI-GCM using VPCLMULQDQ

In reply to Ard Biesheuvel

Hi Ard,

Quoted 108 lines · Ard Biesheuvel
Quoted 83 lines · Megha Dey

Introduce the AVX512 implementation that optimizes the AESNI-GCM encode and decode routines using VPCLMULQDQ.

The glue code in AESNI module overrides the existing AVX2 GCM mode encryption/decryption routines with the AX512 AES GCM mode ones when the following criteria are met:
At compile time:
1. CONFIG_CRYPTO_AVX512 is enabled
2. toolchain(assembler) supports VPCLMULQDQ instructions
At runtime:
1. VPCLMULQDQ and AVX512VL features are supported on a platform (currently only Icelake)
2. aesni_intel.use_avx512 module parameter is set at boot time. For this algorithm, switching from AVX512 optimized version is not possible once set at boot time because of how the code is structured today.(Can be changed later if required)

The functions aesni_gcm_init_avx_512, aesni_gcm_enc_update_avx_512, aesni_gcm_dec_update_avx_512 and aesni_gcm_finalize_avx_512 are adapted from the Intel Optimized IPSEC Cryptographic library.

On a Icelake desktop, with turbo disabled and all CPUs running at maximum frequency, the AVX512 GCM mode optimization shows better performance across data & key sizes as measured by tcrypt.

The average performance improvement of the AVX512 version over the AVX2 version is as follows:
For all key sizes(128/192/256 bits),
data sizes < 128 bytes/block, negligible improvement (~7.5%) data sizes > 128 bytes/block, there is an average improvement of 40% for both encryption and decryption.

A typical run of tcrypt with AES GCM mode encryption/decryption of the AVX2 and AVX512 optimization on a Icelake desktop shows the following results:

   ----------------------------------------------------------------------
   |   key  | bytes | cycles/op (lower is better)   | Percentage gain/  |
   | length |   per |   encryption  |  decryption   |      loss         |
   | (bits) | block |-------------------------------|-------------------|
   |        |       | avx2 | avx512 | avx2 | avx512 | Encrypt | Decrypt |
   |---------------------------------------------------------------------
   |  128   | 16    | 689  |  701   | 689  |  707   |  -1.7   |  -2.61  |
   |  128   | 64    | 731  |  660   | 771  |  649   |   9.7   |  15.82  |
   |  128   | 256   | 911  |  750   | 900  |  721   |  17.67  |  19.88  |
   |  128   | 512   | 1181 |  814   | 1161 |  782   |  31.07  |  32.64  |
   |  128   | 1024  | 1676 |  1052  | 1685 |  1030  |  37.23  |  38.87  |
   |  128   | 2048  | 2475 |  1447  | 2456 |  1419  |  41.53  |  42.22  |
   |  128   | 4096  | 3806 |  2154  | 3820 |  2119  |  43.41  |  44.53  |
   |  128   | 8192  | 9169 |  3806  | 6997 |  3718  |  58.49  |  46.86  |
   |  192   | 16    | 754  |  683   | 737  |  672   |   9.42  |   8.82  |
   |  192   | 64    | 735  |  686   | 715  |  640   |   6.66  |  10.49  |
   |  192   | 256   | 949  |  738   | 2435 |  729   |  22.23  |  70     |
   |  192   | 512   | 1235 |  854   | 1200 |  833   |  30.85  |  30.58  |
   |  192   | 1024  | 1777 |  1084  | 1763 |  1051  |  38.99  |  40.39  |
   |  192   | 2048  | 2574 |  1497  | 2592 |  1459  |  41.84  |  43.71  |
   |  192   | 4096  | 4086 |  2317  | 4091 |  2244  |  43.29  |  45.14  |
   |  192   | 8192  | 7481 |  4054  | 7505 |  3953  |  45.81  |  47.32  |
   |  256   | 16    | 755  |  682   | 720  |  683   |   9.68  |   5.14  |
   |  256   | 64    | 744  |  677   | 719  |  658   |   9     |   8.48  |
   |  256   | 256   | 962  |  758   | 948  |  749   |  21.21  |  21     |
   |  256   | 512   | 1297 |  862   | 1276 |  836   |  33.54  |  34.48  |
   |  256   | 1024  | 1831 |  1114  | 1819 |  1095  |  39.16  |  39.8   |
   |  256   | 2048  | 2767 |  1566  | 2715 |  1524  |  43.4   |  43.87  |
   |  256   | 4096  | 4378 |  2382  | 4368 |  2354  |  45.6   |  46.11  |
   |  256   | 8192  | 8075 |  4262  | 8080 |  4186  |  47.22  |  48.19  |
   ----------------------------------------------------------------------

This work was inspired by the AES GCM mode optimization published in Intel Optimized IPSEC Cryptographic library.
https://github.com/intel/intel-ipsec-mb/blob/master/lib/avx512/gcm_vaes_avx512.asm

---
  arch/x86/crypto/Makefile                    |    1 +
  arch/x86/crypto/aesni-intel_avx512-x86_64.S | 1788 +++++++++++++++++++++++++++
  arch/x86/crypto/aesni-intel_glue.c          |   62 +-
  crypto/Kconfig                              |   12 +
  4 files changed, 1858 insertions(+), 5 deletions(-)

create mode 100644 arch/x86/crypto/aesni-intel_avx512-x86_64.S

...

Quoted 23 lines
arch/x86/crypto/aesni-intel_glue.c +5 -2
index 9e56cdf..8fc5bac 100644
@@ -55,13 +55,16 @@ MODULE_PARM_DESC(use_avx512, "Use AVX512 optimized algorithm, if available");
   * This needs to be 16 byte aligned.
   */
  struct aesni_rfc4106_gcm_ctx {
-       u8 hash_subkey[16] AESNI_ALIGN_ATTR;
+       /* AVX512 optimized algorithms use 48 hash keys to conduct
+        * multiple PCLMULQDQ operations in parallel
+        */
+       u8 hash_subkey[16 * 48] AESNI_ALIGN_ATTR;
         struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR;
         u8 nonce[4];
  };
 
  struct generic_gcmaes_ctx {
-       u8 hash_subkey[16] AESNI_ALIGN_ATTR;
+       u8 hash_subkey[16 * 48] AESNI_ALIGN_ATTR;
         struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR;
  };
 
Quoted 11 lines

arch/x86/crypto/aesni-intel_glue.c struct gcm_context_data { · line 90

Quoted 8 lines
arch/x86/crypto/aesni-intel_glue.c +1 -1
@@ -82,7 +85,7 @@ struct gcm_context_data {
         u8 current_counter[GCM_BLOCK_LEN];
         u64 partial_block_len;
         u64 unused;
-       u8 hash_keys[GCM_BLOCK_LEN * 16];
+       u8 hash_keys[48 * 16];
  };
 

This structure gets allocated on the stack, and gets inflated significantly by this change, even though the code is not enabled by default, and not even supported for most users.

Hmm yeah, this makes sense. I will look into it.

Quoted 5 lines

Is it really necessary for this to be per-request data? If these are precomputed powers of H, they can be moved into the TFM context structure instead, which lives on the heap (and can be shared by all concurrent users of the TFM)

Yeah, this is a per request data.

Quoted 40 lines
Quoted 40 lines
arch/x86/crypto/aesni-intel_glue.c +34 -0
  asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
@@ -266,6 +269,47 @@ static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_gen2 = {
         .finalize = &aesni_gcm_finalize_avx_gen2,
  };
 
+#ifdef CONFIG_CRYPTO_AES_GCM_AVX512
+/*
+ * asmlinkage void aesni_gcm_init_avx_512()
+ * gcm_data *my_ctx_data, context data
+ * u8 *hash_subkey,  the Hash sub key input. Data starts on a 16-byte boundary.
+ */
+asmlinkage void aesni_gcm_init_avx_512(void *my_ctx_data,
+                                      struct gcm_context_data *gdata,
+                                      u8 *iv,
+                                      u8 *hash_subkey,
+                                      const u8 *aad,
+                                      unsigned long aad_len);
+asmlinkage void aesni_gcm_enc_update_avx_512(void *ctx,
+                                            struct gcm_context_data *gdata,
+                                            u8 *out,
+                                            const u8 *in,
+                                            unsigned long plaintext_len);
+asmlinkage void aesni_gcm_dec_update_avx_512(void *ctx,
+                                            struct gcm_context_data *gdata,
+                                            u8 *out,
+                                            const u8 *in,
+                                            unsigned long ciphertext_len);
+asmlinkage void aesni_gcm_finalize_avx_512(void *ctx,
+                                          struct gcm_context_data *gdata,
+                                          u8 *auth_tag,
+                                          unsigned long auth_tag_len);
+
+asmlinkage void aes_gcm_precomp_avx_512(struct crypto_aes_ctx *ctx, u8 *hash_subkey);
+
+static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_512 = {
+       .init = &aesni_gcm_init_avx_512,
+       .enc_update = &aesni_gcm_enc_update_avx_512,
+       .dec_update = &aesni_gcm_dec_update_avx_512,
+       .finalize = &aesni_gcm_finalize_avx_512,
Quoted 8 lines

arch/x86/crypto/aesni-intel_glue.c static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_gen2 = { · line 312

Quoted 7 lines
arch/x86/crypto/aesni-intel_glue.c +7 -0
+};
+#else
+static void aes_gcm_precomp_avx_512(struct crypto_aes_ctx *ctx, u8 *hash_subkey)
+{}
+static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_512 = {};
+#endif
+

Please drop the alternative dummy definitions.

ok

Quoted 8 lines
Quoted 8 lines
arch/x86/crypto/aesni-intel_glue.c +0 -0
  /*
   * asmlinkage void aesni_gcm_init_avx_gen4()
   * gcm_data *my_ctx_data, context data
@@ -669,7 +713,11 @@ rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
         /* We want to cipher all zeros to create the hash sub key. */
         memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
 
Quoted 10 lines

arch/x86/crypto/aesni-intel_glue.c rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len) · line 721

Quoted 7 lines
arch/x86/crypto/aesni-intel_glue.c +5 -1
-       aes_encrypt(&ctx, hash_subkey, hash_subkey);
+       if (IS_ENABLED(CONFIG_CRYPTO_AES_GCM_AVX512) && use_avx512 &&
+           cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ))
+               aes_gcm_precomp_avx_512(&ctx, hash_subkey);
+       else
+               aes_encrypt(&ctx, hash_subkey, hash_subkey);
 

I suppose this answers my question about the subkeys. Please find a way to move these out of struct gcm_context_data so they don't need to be copied to the stack for each request.

Hmm yeah. I will move this allocation to the heap instead.

Quoted 14 lines

arch/x86/crypto/aesni-intel_glue.c static int __init aesni_init(void) · line 1168

Quoted 13 lines
arch/x86/crypto/aesni-intel_glue.c +4 -1
         memzero_explicit(&ctx, sizeof(ctx));
         return 0;
@@ -1114,7 +1162,11 @@ static int __init aesni_init(void)
         if (!x86_match_cpu(aesni_cpu_id))
                 return -ENODEV;
  #ifdef CONFIG_X86_64
-       if (boot_cpu_has(X86_FEATURE_AVX2)) {
+       if (use_avx512 && IS_ENABLED(CONFIG_CRYPTO_AES_GCM_AVX512) &&
+           cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ)) {
+               pr_info("AVX512 version of gcm_enc/dec engaged.\n");
+               aesni_gcm_tfm = &aesni_gcm_tfm_avx_512;

This was changed in the cryptodev tree to use static keys.

yep, will make the necessary changes.

-Megha

Quoted 31 lines
Quoted 31 lines
arch/x86/crypto/aesni-intel_glue.c +1 -0
+       } else if (boot_cpu_has(X86_FEATURE_AVX2)) {
                 pr_info("AVX2 version of gcm_enc/dec engaged.\n");
                 aesni_gcm_tfm = &aesni_gcm_tfm_avx_gen4;
         } else if (boot_cpu_has(X86_FEATURE_AVX)) {
crypto/Kconfig +12 -1
index 3043849..8c8a68d 100644
@@ -661,6 +661,18 @@ config CRYPTO_AES_CTR_AVX512
         depends on CRYPTO_AES_NI_INTEL
         depends on AS_VAES_AVX512
 
+# We default CRYPTO_AES_GCM_AVX512 to Y but depend on CRYPTO_AVX512 in
+# order to have a singular option (CRYPTO_AVX512) select multiple algorithms
+# when supported. Specifically, if the platform and/or toolset does not
+# support VPLMULQDQ. Then this algorithm should not be supported as part of
+# the set that CRYPTO_AVX512 selects.
+config CRYPTO_AES_GCM_AVX512
+       bool
+       default y
+       depends on CRYPTO_AVX512
+       depends on CRYPTO_AES_NI_INTEL
+       depends on AS_VPCLMULQDQ
+
  config CRYPTO_CRC32C_SPARC64
         tristate "CRC32c CRC algorithm (SPARC64)"
         depends on SPARC64
--

2.7.4

author
#3 lore raw

Re: [RFC V1 1/7] x86: Probe assembler capabilities for VAES and VPLCMULQDQ support

In reply to Ard Biesheuvel

Hi Ard,

Quoted 20 lines · Ard Biesheuvel
Quoted 19 lines · Megha Dey

This is a preparatory patch to introduce the optimized crypto algorithms using AVX512 instructions which would require VAES and VPLCMULQDQ support.

Check for VAES and VPCLMULQDQ assembler support using AVX512 registers.

---
  arch/x86/Kconfig.assembler | 10 ++++++++++
  1 file changed, 10 insertions(+)
arch/x86/Kconfig.assembler +0 -0
index 26b8c08..9ea0bc8 100644
@@ -1,6 +1,16 @@
  # SPDX-License-Identifier: GPL-2.0
  # Copyright (C) 2020 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
 
Quoted 5 lines

arch/x86/Kconfig.assembler line 5

Quoted 2 lines
arch/x86/Kconfig.assembler +2 -0
+config AS_VAES_AVX512
+       def_bool $(as-instr,vaesenc %zmm0$(comma)%zmm1$(comma)%zmm1) && 64BIT

Is the '&& 64BIT' necessary here, but not below?

In any case, better to use a separate 'depends on' line, for legibility

yeah , I think the '&& 64 BIT' is not required. I will remove it in the next version.

-Megha

Quoted 15 lines
Quoted 15 lines
arch/x86/Kconfig.assembler +8 -1
+       help
+         Supported by binutils >= 2.30 and LLVM integrated assembler
+
+config AS_VPCLMULQDQ
+       def_bool $(as-instr,vpclmulqdq \$0$(comma)%zmm2$(comma)%zmm6$(comma)%zmm4)
+       help
+         Supported by binutils >= 2.30 and LLVM integrated assembler
+
  config AS_AVX512
         def_bool $(as-instr,vpmovm2b %k1$(comma)%zmm5)
         help
--

2.7.4