Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	x86@kernel.org, Antonio Quartulli <antonio@mandelbit.com>,
	Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 07/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with VPCLMULQDQ and AVX512
Date: Sun, 29 Nov 2026 12:02:41 -0800	[thread overview]
Message-ID: <20261129200245.146503-8-ebiggers@kernel.org> (raw)
In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org>

Add a VPCLMULQDQ + AVX512 implementation of GHASH and POLYVAL for x86_64
in lib/crypto/x86/gf128hash-vpclmul-avx512.S, providing:

- {ghash,polyval}_preparekey_vpclmul_avx512()
- {ghash,polyval}_blocks_vpclmul_avx512()

The file is derived from arch/x86/crypto/aes-gcm-vaes-avx512.S, so it
inherits its copyright and license.  This speeds up both algorithms on
AVX512 CPUs by hashing four 128-bit blocks per ZMM vector (16 blocks per
stride), and prepares for migrating the VAES+AVX512 AES-GCM code into
lib/crypto/.

Key implementation choices for AVX512:

- The 16-block main loop uses schoolbook multiplication with 3-input
  vpternlogd rather than Karatsuba (matching aes-gcm-vaes-avx512.S and
  avoiding 128 bytes of extra key-struct storage).

- Key preparation stores H^1 * x^-64 mod G(x) in key->h_times_xm64 and
  computes H^16 through H^1 in parallel via squaring (H^1 -> H^2),
  2-wide YMM multiplication ([H^4, H^3]), and 4-wide ZMM multiplications
  by [H^4, H^4, H^4, H^4], precomputing x^-64 multiples so each
  multiplication needs only a single reduction step.

- Trailing 1..15 blocks are processed in a single masked loop that sums
  unreduced products across all remaining vectors and performs one
  deferred reduction at the end; on the final vector (with 1 <= r <= 4
  blocks), BMI2 bzhi + a zero-masked input load is paired with an
  unmasked 64-byte load of [H^r..H^1, 0..].  Single-block calls (nblocks
  == 1) use a strictly 128-bit XMM fast path with key->h_times_xm64 to
  avoid ZMM overhead and vzeroupper.

To accommodate the 16 precomputed key powers and three trailing zeroized
padding blocks needed for the unmasked 64-byte key-power load on 1..3
trailing blocks, extend 'struct gf128hash_key_x86' into a union of the
8-power and 16-power layouts (growing it from 208 to 320 bytes).

To keep H^1 and H^1 * x^-64 at the same offsets in both layouts, place
h_powers[8] at the end of the union and h_times_xm64 after the padding.
This allows single-block and generic fallback code to access them
unconditionally.

Benchmark results on AMD Ryzen 9 9950X (Zen 5) using ghash_kunit and
polyval_kunit:

    Length   GHASH before  GHASH after   POLYVAL before  POLYVAL after
    ------   ------------  -----------   --------------  -------------
         1       129 MB/s     129 MB/s         131 MB/s       128 MB/s
        16      2157 MB/s    2122 MB/s        2232 MB/s      2298 MB/s
        64      7286 MB/s    6353 MB/s        7156 MB/s      6530 MB/s
       127      6615 MB/s    6896 MB/s        6785 MB/s      6962 MB/s
       128     11769 MB/s   11907 MB/s       11663 MB/s     12245 MB/s
       200      9112 MB/s   11263 MB/s        8863 MB/s     10913 MB/s
       256     16585 MB/s   21921 MB/s       16529 MB/s     22183 MB/s
       511     14640 MB/s   20077 MB/s       14630 MB/s     20459 MB/s
       512     17740 MB/s   28403 MB/s       20235 MB/s     28070 MB/s
      1024     20218 MB/s   33497 MB/s       22206 MB/s     31997 MB/s
      3173     20521 MB/s   34829 MB/s       20545 MB/s     35064 MB/s
      4096     21706 MB/s   37833 MB/s       20717 MB/s     37368 MB/s
     16384     21577 MB/s   39348 MB/s       22174 MB/s     39020 MB/s

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 include/crypto/gf128hash.h                |  32 +-
 lib/crypto/Makefile                       |   3 +-
 lib/crypto/x86/gf128hash-cpu.h            |   9 +
 lib/crypto/x86/gf128hash-pclmul.S         |   8 +-
 lib/crypto/x86/gf128hash-vpclmul-avx2.S   |   8 +-
 lib/crypto/x86/gf128hash-vpclmul-avx512.S | 516 ++++++++++++++++++++++
 lib/crypto/x86/gf128hash.h                |  90 +++-
 7 files changed, 631 insertions(+), 35 deletions(-)
 create mode 100644 lib/crypto/x86/gf128hash-vpclmul-avx512.S

diff --git a/include/crypto/gf128hash.h b/include/crypto/gf128hash.h
index 4d2109a3362c0..5c38fc38fd9c4 100644
--- a/include/crypto/gf128hash.h
+++ b/include/crypto/gf128hash.h
@@ -36,14 +36,34 @@ struct polyval_elem {
 
 /* Format of struct ghash_key and struct polyval_key on x86_64 */
 struct gf128hash_key_x86 {
-	/* Powers of the hash key H^8 through H^1, in POLYVAL format */
-	struct polyval_elem h_powers[8];
 	/*
-	 * Halves of each entry of @h_powers XOR'd together.  If
-	 * gf128hash_use_vpclmul_avx2(), they're in the order H^8, H^6, H^7,
-	 * H^5, H^4, H^2, H^3, H^1.  Otherwise they're in the order H^8..H^1.
+	 * If gf128hash_use_vpclmul_avx512(), then h_powers16 is used.
+	 * Otherwise the other member of the union is used.  In either case, H^1
+	 * is in the same place (h_powers16[15] or equivalently h_powers[7]).
 	 */
-	__le64 h_powers_xored[8];
+	union {
+		/* Powers of the hash key H^16 through H^1, in POLYVAL format */
+		struct polyval_elem h_powers16[16];
+
+		struct {
+			/*
+			 * Halves of each entry of @h_powers XOR'd together.  If
+			 * gf128hash_use_vpclmul_avx2(), they're in the order
+			 * H^8, H^6, H^7, H^5, H^4, H^2, H^3, H^1.  Otherwise
+			 * they're in the order H^8..H^1.
+			 */
+			__le64 h_powers_xored[8];
+			/* Unused space (set to zero) */
+			u8 unused[64];
+			/*
+			 * Powers of the hash key H^8 through H^1, in POLYVAL
+			 * format
+			 */
+			struct polyval_elem h_powers[8];
+		};
+	};
+	/* Padding for AVX512 loads (set to zero) */
+	struct polyval_elem padding[3];
 	/*
 	 * H^1 * x^-64 mod G(x) in POLYVAL format.  This is used to optimize
 	 * single-block multiplications using 1-step reduction.
diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index 9d3ecc643a844..c057c0d896a4f 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -43,7 +43,8 @@ endif
 
 libgf128hash-$(CONFIG_RISCV) += riscv/ghash-riscv64-zvkg.o
 libgf128hash-$(CONFIG_X86_64) += x86/gf128hash-pclmul.o \
-				 x86/gf128hash-vpclmul-avx2.o
+				 x86/gf128hash-vpclmul-avx2.o \
+				 x86/gf128hash-vpclmul-avx512.o
 endif # CONFIG_CRYPTO_LIB_GF128HASH_ARCH
 
 # clean-files must be defined unconditionally
diff --git a/lib/crypto/x86/gf128hash-cpu.h b/lib/crypto/x86/gf128hash-cpu.h
index 5045b41bf8d73..ed7500e665d8d 100644
--- a/lib/crypto/x86/gf128hash-cpu.h
+++ b/lib/crypto/x86/gf128hash-cpu.h
@@ -36,4 +36,13 @@ static inline bool gf128hash_use_vpclmul_avx2(void)
 	       cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ);
 }
 
+static inline bool gf128hash_use_vpclmul_avx512(void)
+{
+	return gf128hash_use_vpclmul_avx2() &&
+	       cpu_feature_enabled(X86_FEATURE_AVX512BW) &&
+	       cpu_feature_enabled(X86_FEATURE_AVX512VL) &&
+	       cpu_feature_enabled(X86_FEATURE_BMI2) &&
+	       !cpu_feature_enabled(X86_FEATURE_PREFER_YMM);
+}
+
 #endif /* _LIB_CRYPTO_X86_GF128HASH_CPU_H */
diff --git a/lib/crypto/x86/gf128hash-pclmul.S b/lib/crypto/x86/gf128hash-pclmul.S
index 7bb0a25b9df02..5814a6aa1f1ce 100644
--- a/lib/crypto/x86/gf128hash-pclmul.S
+++ b/lib/crypto/x86/gf128hash-pclmul.S
@@ -187,10 +187,10 @@
 // Offsets in struct gf128hash_key_x86, using the 8-power non-permuted layout.
 // h_powers[0..7] holds H^8..H^1, h_powers_xored[i] holds the XOR of the two
 // 64-bit halves of h_powers[i], and h_times_xm64 holds H^1 * x^-64 mod G(x).
-#define OFFSETOF_H_POWERS	0
-#define OFFSETOFEND_H_POWERS	128
-#define OFFSETOF_H_POWERS_XORED	128
-#define OFFSETOF_H_TIMES_XM64	192
+#define OFFSETOF_H_POWERS_XORED	0
+#define OFFSETOF_H_POWERS	128
+#define OFFSETOFEND_H_POWERS	256
+#define OFFSETOF_H_TIMES_XM64	304
 
 .text
 
diff --git a/lib/crypto/x86/gf128hash-vpclmul-avx2.S b/lib/crypto/x86/gf128hash-vpclmul-avx2.S
index 9c38148445aa3..57585f54a8f64 100644
--- a/lib/crypto/x86/gf128hash-vpclmul-avx2.S
+++ b/lib/crypto/x86/gf128hash-vpclmul-avx2.S
@@ -116,10 +116,10 @@
 // h_powers[0..7] holds H^8..H^1, h_powers_xored[0..7] holds the XORs of the two
 // 64-bit halves of H^8, H^6, H^7, H^5, H^4, H^2, H^3, H^1 (in that order), and
 // h_times_xm64 holds H^1 * x^-64 mod G(x).
-#define OFFSETOF_H_POWERS	0
-#define OFFSETOFEND_H_POWERS	128
-#define OFFSETOF_H_POWERS_XORED	128
-#define OFFSETOF_H_TIMES_XM64	192
+#define OFFSETOF_H_POWERS_XORED	0
+#define OFFSETOF_H_POWERS	128
+#define OFFSETOFEND_H_POWERS	256
+#define OFFSETOF_H_TIMES_XM64	304
 
 .text
 
diff --git a/lib/crypto/x86/gf128hash-vpclmul-avx512.S b/lib/crypto/x86/gf128hash-vpclmul-avx512.S
new file mode 100644
index 0000000000000..af7c2d05ff989
--- /dev/null
+++ b/lib/crypto/x86/gf128hash-vpclmul-avx512.S
@@ -0,0 +1,516 @@
+/* SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause */
+//
+// Accelerated GHASH and POLYVAL implementation with VPCLMULQDQ and AVX512
+//
+// Copyright 2024 Google LLC
+//
+// Author: Eric Biggers <ebiggers@google.com>
+//
+//------------------------------------------------------------------------------
+//
+// This file is dual-licensed, meaning that you can use it under your choice of
+// either of the following two licenses:
+//
+// Licensed under the Apache License 2.0 (the "License").  You may obtain a copy
+// of the License at
+//
+//	http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// or
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice,
+//    this list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright
+//    notice, this list of conditions and the following disclaimer in the
+//    documentation and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+//
+//------------------------------------------------------------------------------
+//
+// This file implements GHASH and POLYVAL using the VPCLMULQDQ instruction on
+// 512-bit vectors (zmm registers), each holding four 128-bit blocks.  It also
+// requires AVX512BW (for vmovdqu8, 512-bit vpshufb, and kmovq), AVX512VL (for
+// EVEX-coded instructions such as vpternlogd on xmm and ymm registers), and
+// BMI2 (for bzhi).  The math is documented in gf128hash-pclmul.S.  The design
+// is similar to gf128hash-vpclmul-avx2.S, with the following differences:
+//
+// - The main loop processes 16 blocks per stride, as 4 vectors.  Lanes 0..3 of
+//   each vector hold 4 consecutive blocks in order, so the data vectors are
+//   multiplied by the key power vectors [H^16..H^13], [H^12..H^9], [H^8..H^5],
+//   and [H^4..H^1] (written [lane 0..lane 3]), which stay in registers.  As in
+//   the AVX2 code, the unreduced products are summed in each lane, all lanes
+//   are reduced at once, and the reduced lanes are XOR'd together.
+//
+// - The main loop uses schoolbook multiplication rather than Karatsuba.  The
+//   3-input XOR provided by vpternlogd ($0x96: dst ^= src1 ^ src2) helps
+//   schoolbook more than Karatsuba, storing the XOR'd halves of all 16 key
+//   powers for Karatsuba would add 128 bytes to struct gf128hash_key_x86, and
+//   using schoolbook also matches aes-gcm-vaes-avx512.S (which shares the key
+//   struct).  As in gf128hash-vpclmul-avx2.S, vectors 1..3 are processed before
+//   vector 0 so the loop-carried dependency on the accumulator enters as late
+//   as possible.
+//
+// - The key uses the 16-power layout of struct gf128hash_key_x86:
+//   h_powers16[0..15] holds H^16..H^1 and is followed by 3 zeroized padding
+//   blocks, and then h_times_xm64 (H^1 * x^-64 mod G(x)).  h_powers16[15] and
+//   h_times_xm64 are at the same offsets as in the 8-power layout.  The padding
+//   allows any remaining 1..15 blocks after the 16-block loop to be processed
+//   in one masked loop (with a single deferred reduction at the end): on the
+//   last vector (with 1 <= r <= 4 blocks), a zero-masked load avoids reading
+//   past the end of the data, and an unmasked load reads [H^r..H^1, 0, ...].
+//
+// - The nblocks == 1 fast path uses only xmm registers (avoiding the overhead
+//   of zmm registers and of vzeroupper) and uses key->h_times_xm64 so that only
+//   one reduction step is needed, matching gf128hash-pclmul.S.
+//
+// - preparekey stores H^1 * x^-64 mod G(x) in key->h_times_xm64, squares H^1 to
+//   get H^2, computes [H^4, H^3] by multiplying [H^2, H^1] by [H^2, H^2], and
+//   then computes [H^8..H^5], [H^12..H^9], and [H^16..H^13] by repeatedly
+//   multiplying by [H^4, H^4, H^4, H^4], precomputing x^-64 multiples so each
+//   multiplication needs only one reduction step.
+//
+// - Only zmm0..zmm15 are used (never zmm16..zmm31), so vzeroupper resets all
+//   dirty upper vector state (avoiding Hi16_ZMM XSAVE overhead) and 128/256-bit
+//   operations can use shorter VEX encodings where applicable.
+
+#include <linux/linkage.h>
+
+.section .rodata
+.p2align 4
+
+	// vpshufb mask that reverses the order of the bytes in a 16-byte block
+.Lbswap_mask:
+	.octa	0x000102030405060708090a0b0c0d0e0f
+
+	// Mask for multiplying by x mod G(x): G(x) - x^128 = x^127 + x^126 +
+	// x^121 + 1 (bits 127, 126, 121, and 0), and bit 64 for the carry from
+	// bit 63 that vpaddq doesn't propagate
+.Lgfpoly_and_internal_carrybit:
+	.octa	0xc2000000000000010000000000000001
+
+	// (G(x) - x^128 - 1) / x^64 = x^63 + x^62 + x^57, the constant used to
+	// multiply by x^-64 mod G(x)
+.Lgfpoly:
+	.quad	0xc200000000000000
+
+// Offsets in struct gf128hash_key_x86, using the 16-power layout.
+// h_powers16[0..15] holds H^16..H^1, followed by 3 zeroized padding blocks at
+// OFFSETOFEND_H_POWERS, and then h_times_xm64 (H^1 * x^-64 mod G(x)).
+#define OFFSETOF_H_POWERS	0
+#define OFFSETOFEND_H_POWERS	256
+#define OFFSETOF_H_TIMES_XM64	304
+
+.text
+
+// Set each 128-bit lane of \dst to dot(lane of \a, lane of \b), given
+// \a_times_xm64 = \a * x^-64 mod G(x) in each lane (see gf128hash-pclmul.S for
+// the math).  Works on xmm, ymm, and zmm registers.  \dst may alias \a,
+// \a_times_xm64, or \b.  \gfpoly must contain .Lgfpoly in the low qword of each
+// lane.  Clobbers \t0 and \t1.  \gfpoly, \t0, and \t1 must be distinct from
+// each other and from the other operands.
+.macro	_gf128hash_mul	a, a_times_xm64, b, dst, gfpoly, t0, t1
+	vpclmulqdq	$0x01, \a, \b, \t0		// a_L * b_H
+	vpclmulqdq	$0x00, \a_times_xm64, \b, \t1	// (a*x^-64)_L * b_L
+	vpxord		\t1, \t0, \t0			// MI
+	vpclmulqdq	$0x11, \a, \b, \t1		// a_H * b_H
+	vpclmulqdq	$0x10, \a_times_xm64, \b, \dst	// (a*x^-64)_H * b_L
+	vpxord		\t1, \dst, \dst			// HI
+	vpclmulqdq	$0x00, \gfpoly, \t0, \t1	// MI_L*(x^63+x^62+x^57)
+	vpshufd		$0x4e, \t0, \t0			// MI_H + MI_L * x^64
+	vpternlogd	$0x96, \t1, \t0, \dst		// Fold MI into HI
+.endm
+
+// Like _gf128hash_mul, but compute dot(\a, \a) in each 128-bit lane.  In
+// GF(2)[x], the cross terms cancel: (a_L + a_H * x^64)^2 = (a_L)^2 +
+// (a_H)^2 * x^128.  So only 2 vpclmulqdq are needed for the product, and the
+// initial MI is 0, so the first reduction step sets MI = LO * x^-64.  \dst may
+// alias \a.  Clobbers \t0 and \t1.  \gfpoly, \t0, and \t1 must be distinct from
+// each other and from \a and \dst.
+.macro	_gf128hash_square	a, dst, gfpoly, t0, t1
+	vpclmulqdq	$0x00, \a, \a, \t0		// LO = (a_L)^2
+	vpclmulqdq	$0x11, \a, \a, \dst		// HI = (a_H)^2
+	vpclmulqdq	$0x00, \gfpoly, \t0, \t1	// LO_L*(x^63+x^62+x^57)
+	vpshufd		$0x4e, \t0, \t0			// LO_H + LO_L * x^64
+	vpxord		\t0, \t1, \t1			// MI = LO * x^-64
+	vpclmulqdq	$0x00, \gfpoly, \t1, \t0	// MI_L*(x^63+x^62+x^57)
+	vpshufd		$0x4e, \t1, \t1			// MI_H + MI_L * x^64
+	vpternlogd	$0x96, \t0, \t1, \dst		// Fold MI into HI
+.endm
+
+// Reduce the 256-bit products LO + MI * x^64 + HI * x^128 given in each 128-bit
+// lane of \lo, \mi, and \hi: multiply them by x^-128 mod G(x) and store the
+// 128-bit results in \hi.  This is done in two steps, MI += LO * x^-64 then
+// HI += MI * x^-64.  Works on xmm, ymm, and zmm registers.  \gfpoly must
+// contain .Lgfpoly in the low qword of each lane.  Clobbers \lo, \mi, and \tmp.
+// All operands must be distinct registers.
+.macro	_gf128hash_reduce	lo, mi, hi, gfpoly, tmp
+	vpclmulqdq	$0x00, \gfpoly, \lo, \tmp	// LO_L*(x^63+x^62+x^57)
+	vpshufd		$0x4e, \lo, \lo			// LO_H + LO_L * x^64
+	vpternlogd	$0x96, \tmp, \lo, \mi		// Fold LO into MI
+	vpclmulqdq	$0x00, \gfpoly, \mi, \tmp	// MI_L*(x^63+x^62+x^57)
+	vpshufd		$0x4e, \mi, \mi			// MI_H + MI_L * x^64
+	vpternlogd	$0x96, \tmp, \mi, \hi		// Fold MI into HI
+.endm
+
+// void {ghash,polyval}_preparekey_vpclmul_avx512(struct gf128hash_key_x86 *key,
+//						  const u8 raw_key[16]);
+//
+// Given the raw GHASH key (\is_ghash=1) or POLYVAL key (\is_ghash=0), set:
+// - key->h_powers16[0..15] to H^16..H^1 in POLYVAL format
+// - key->padding[0..2] to zero
+// - key->h_times_xm64 to H^1 * x^-64 mod G(x)
+.macro	_gf128hash_preparekey	is_ghash
+	.set	KEY,		%rdi
+	.set	RAW_KEY,	%rsi
+	.set	POWERS_PTR,	%rdx
+	// %eax is used as a loop counter.
+	.set	TMP0,		%zmm0
+	.set	TMP0_YMM,	%ymm0
+	.set	TMP0_XMM,	%xmm0
+	.set	TMP1,		%zmm1
+	.set	TMP1_YMM,	%ymm1
+	.set	TMP1_XMM,	%xmm1
+	.set	H_CUR,		%zmm2
+	.set	H_CUR_YMM,	%ymm2
+	.set	H_CUR_XMM,	%xmm2
+	.set	H_INC,		%zmm3
+	.set	H_INC_YMM,	%ymm3
+	.set	H_INC_XMM,	%xmm3
+	.set	H_INC_XM64,	%zmm4
+	.set	H_INC_XM64_YMM,	%ymm4
+	.set	H_INC_XM64_XMM,	%xmm4
+	.set	GFPOLY,		%zmm5
+	.set	GFPOLY_YMM,	%ymm5
+	.set	GFPOLY_XMM,	%xmm5
+
+	vmovdqu		(RAW_KEY), H_CUR_XMM
+.if \is_ghash
+	// Convert the GHASH key to POLYVAL format: byte-reverse it, then
+	// multiply it by x mod G(x).  See gf128hash-pclmul.S for details.  Here
+	// vpternlogd $0x78 does the AND and XOR: dst ^= src1 & src2.
+	vpshufb		.Lbswap_mask(%rip), H_CUR_XMM, H_CUR_XMM
+	vpshufd		$0xd3, H_CUR_XMM, TMP0_XMM
+	vpsrad		$31, TMP0_XMM, TMP0_XMM
+	vpaddq		H_CUR_XMM, H_CUR_XMM, H_CUR_XMM
+	vpternlogd	$0x78, .Lgfpoly_and_internal_carrybit(%rip), \
+			TMP0_XMM, H_CUR_XMM
+.endif
+
+	// Point POWERS_PTR to h_powers16[12], and zeroize the 3 padding blocks
+	// (addressed via POWERS_PTR for compact disp8 encoding).
+	lea		OFFSETOFEND_H_POWERS-64(KEY), POWERS_PTR
+	vpxor		TMP0_XMM, TMP0_XMM, TMP0_XMM
+	vmovdqu		TMP0_YMM, 64(POWERS_PTR)	// padding[0..1]
+	vmovdqu		TMP0_XMM, 64+32(POWERS_PTR)	// padding[2]
+
+	vpbroadcastq	.Lgfpoly(%rip), GFPOLY
+
+	// Compute and store H^1 * x^-64 mod G(x) in h_times_xm64.
+	vpclmulqdq	$0x00, GFPOLY_XMM, H_CUR_XMM, TMP1_XMM
+	vpshufd		$0x4e, H_CUR_XMM, TMP0_XMM
+	vpxor		TMP0_XMM, TMP1_XMM, TMP1_XMM
+	vmovdqu		TMP1_XMM, OFFSETOF_H_TIMES_XM64(KEY)
+
+	// Square H^1 to get H^2, and compute H^2 * x^-64 mod G(x).
+	_gf128hash_square H_CUR_XMM, H_INC_XMM, GFPOLY_XMM, TMP0_XMM, TMP1_XMM
+	vpclmulqdq	$0x00, GFPOLY_XMM, H_INC_XMM, H_INC_XM64_XMM
+	vpshufd		$0x4e, H_INC_XMM, TMP0_XMM
+	vpxor		TMP0_XMM, H_INC_XM64_XMM, H_INC_XM64_XMM
+
+	// Create H_CUR_YMM = [H^2, H^1], H_INC_YMM = [H^2, H^2], and
+	// H_INC_XM64_YMM = [H^2 * x^-64, H^2 * x^-64].
+	vinserti128	$1, H_CUR_XMM, H_INC_YMM, H_CUR_YMM
+	vinserti128	$1, H_INC_XMM, H_INC_YMM, H_INC_YMM
+	vinserti128	$1, H_INC_XM64_XMM, H_INC_XM64_YMM, H_INC_XM64_YMM
+
+	// Compute H_INC_YMM = [H^4, H^3], then create
+	// H_CUR = [H^4, H^3, H^2, H^1], H_INC = [H^4, H^4, H^4, H^4], and
+	// H_INC_XM64 = [H^4 * x^-64, ...].
+	_gf128hash_mul	H_INC_YMM, H_INC_XM64_YMM, H_CUR_YMM, H_INC_YMM, \
+			GFPOLY_YMM, TMP0_YMM, TMP1_YMM
+	vinserti64x4	$1, H_CUR_YMM, H_INC, H_CUR
+	vshufi64x2	$0, H_INC, H_INC, H_INC
+	vpclmulqdq	$0x00, GFPOLY, H_INC, H_INC_XM64
+	vpshufd		$0x4e, H_INC, TMP0
+	vpxord		TMP0, H_INC_XM64, H_INC_XM64
+
+	// Store [H^4, H^3, H^2, H^1] in h_powers16[12..15].
+	vmovdqu8	H_CUR, (POWERS_PTR)
+
+	// Compute [H^8..H^5], [H^12..H^9], and [H^16..H^13], and store them in
+	// h_powers16[8..11], [4..7], and [0..3] respectively.
+	mov		$3, %eax
+.Lpreparekey_next\@:
+	sub		$64, POWERS_PTR
+	_gf128hash_mul	H_INC, H_INC_XM64, H_CUR, H_CUR, GFPOLY, TMP0, TMP1
+	vmovdqu8	H_CUR, (POWERS_PTR)
+	dec		%eax
+	jnz		.Lpreparekey_next\@
+
+	vzeroupper
+	RET
+.endm
+
+// XOR together the four 128-bit lanes of \src (whose low lane is \src_xmm), and
+// store the result in \dst_xmm.  Writing \dst_xmm zeroes the upper lanes of the
+// corresponding zmm register.  \dst_xmm may alias \src_xmm; \t0_xmm, \t1_xmm,
+// and \t2_xmm are temporaries and must be distinct from each other and from
+// \src_xmm and \dst_xmm.  \src_xmm, \dst_xmm, and \t0_xmm must be in
+// %xmm0..%xmm15 (for VEX-encoded vpxor).
+.macro	_horizontal_xor	src, src_xmm, dst_xmm, t0_xmm, t1_xmm, t2_xmm
+	vextracti32x4	$1, \src, \t0_xmm
+	vextracti32x4	$2, \src, \t1_xmm
+	vextracti32x4	$3, \src, \t2_xmm
+	vpxor		\t0_xmm, \src_xmm, \dst_xmm
+	vpternlogd	$0x96, \t1_xmm, \t2_xmm, \dst_xmm
+.endm
+
+// Update ACC with 16 blocks (four 64-byte vectors) of data from DATA, using
+// aggregated schoolbook multiplication.  ACC must hold the accumulator in lane
+// 0 and zero in lanes 1..3, and this property is preserved.  After
+// byte-reversing (if \bswap_data=1) and adding ACC to DATA0, the products are
+// summed per lane for i = 0..3 as:
+//
+//	LO = sum LO_i,            where LO_i  = (H_POW{4-i})_L * (DATAi)_L
+//	MI = sum (MI_ia + MI_ib), where MI_ia = (H_POW{4-i})_L * (DATAi)_H and
+//					MI_ib = (H_POW{4-i})_H * (DATAi)_L
+//	HI = sum HI_i,            where HI_i  = (H_POW{4-i})_H * (DATAi)_H
+//
+// Vectors 1..3 are processed before vector 0 so that the loop-carried
+// dependency on ACC enters as late as possible.
+.macro	_gf128hash_4x	bswap_data
+	vmovdqu8	1*64(DATA), DATA1
+	vmovdqu8	2*64(DATA), DATA2
+	vmovdqu8	3*64(DATA), DATA3
+	vmovdqu8	0*64(DATA), DATA0
+.if \bswap_data
+	vpshufb		BSWAP_MASK, DATA1, DATA1
+	vpshufb		BSWAP_MASK, DATA2, DATA2
+	vpshufb		BSWAP_MASK, DATA3, DATA3
+	vpshufb		BSWAP_MASK, DATA0, DATA0
+.endif
+
+	// Vectors 1..3: [M_4..M_7] * [H^12..H^9], [M_8..M_11] * [H^8..H^5], and
+	// [M_12..M_15] * [H^4..H^1].  Initialize (LO, MI, HI) with their sum.
+	vpclmulqdq	$0x00, H_POW3, DATA1, LO	// LO_1
+	vpclmulqdq	$0x00, H_POW2, DATA2, TMP0	// LO_2
+	vpclmulqdq	$0x00, H_POW1, DATA3, TMP1	// LO_3
+	vpternlogd	$0x96, TMP1, TMP0, LO		// sum(LO_{1..3})
+
+	vpclmulqdq	$0x01, H_POW3, DATA1, MI	// MI_1a
+	vpclmulqdq	$0x01, H_POW2, DATA2, TMP0	// MI_2a
+	vpclmulqdq	$0x01, H_POW1, DATA3, TMP1	// MI_3a
+	vpternlogd	$0x96, TMP1, TMP0, MI		// sum(MI_{1..3}a)
+	vpclmulqdq	$0x10, H_POW3, DATA1, TMP0	// MI_1b
+	vpclmulqdq	$0x10, H_POW2, DATA2, TMP1	// MI_2b
+	vpternlogd	$0x96, TMP1, TMP0, MI		// + MI_1b + MI_2b
+	vpclmulqdq	$0x10, H_POW1, DATA3, TMP0	// MI_3b
+	vpxord		TMP0, MI, MI			// sum(MI_{1..3}{a,b})
+
+	vpclmulqdq	$0x11, H_POW3, DATA1, HI	// HI_1
+	vpclmulqdq	$0x11, H_POW2, DATA2, DATA2	// HI_2
+	vpclmulqdq	$0x11, H_POW1, DATA3, DATA3	// HI_3
+	vpternlogd	$0x96, DATA3, DATA2, HI		// sum(HI_{1..3})
+
+	// Vector 0: [M_0 + ACC, M_1..M_3] * [H^16..H^13].  Add to (LO, MI, HI),
+	// then reduce all 4 lanes and XOR them together into ACC_XMM.  Writing
+	// ACC_XMM zeroes lanes 1..3 of ACC.
+	vpxord		ACC, DATA0, DATA0
+	vpclmulqdq	$0x00, H_POW4, DATA0, TMP0	// LO_0
+	vpclmulqdq	$0x01, H_POW4, DATA0, TMP1	// MI_0a
+	vpclmulqdq	$0x10, H_POW4, DATA0, DATA2	// MI_0b
+	vpclmulqdq	$0x11, H_POW4, DATA0, DATA0	// HI_0
+	vpxord		TMP0, LO, LO		// LO = sum(LO_{0..3})
+	vpternlogd	$0x96, DATA2, TMP1, MI	// MI = sum(MI_{0..3}{a,b})
+	vpxord		DATA0, HI, HI		// HI = sum(HI_{0..3})
+	_gf128hash_reduce LO, MI, HI, GFPOLY, TMP0
+	_horizontal_xor	HI, HI_XMM, ACC_XMM, DATA0_XMM, DATA1_XMM, DATA2_XMM
+.endm
+
+// void
+// {ghash,polyval}_blocks_vpclmul_avx512(struct polyval_elem *acc,
+//					 const struct gf128hash_key_x86 *key,
+//					 const u8 *data, size_t nblocks);
+//
+// Update the POLYVAL-format accumulator @acc with @nblocks >= 1 full 16-byte
+// blocks from @data.  If \bswap_data=1 (GHASH), each block is byte-reversed to
+// convert it to POLYVAL format.
+.macro	_gf128hash_blocks	bswap_data
+	.set	ACC_PTR,	%rdi
+	.set	KEY,		%rsi
+	.set	DATA,		%rdx
+	.set	NBLOCKS,	%rcx
+	.set	NBYTES,		%rcx	// Reused as byte count in the tail loop
+	.set	POWERS_PTR,	%rax
+	.set	ALL_ONES,	%r8
+
+	// %r9 and %k1 are used as temporary registers.
+	.set	DATA0,		%zmm0
+	.set	DATA0_XMM,	%xmm0
+	.set	DATA1,		%zmm1
+	.set	DATA1_XMM,	%xmm1
+	.set	DATA2,		%zmm2
+	.set	DATA2_XMM,	%xmm2
+	.set	DATA3,		%zmm3
+	.set	DATA3_XMM,	%xmm3
+	.set	BSWAP_MASK,	%zmm4
+	.set	ACC,		%zmm5
+	.set	ACC_XMM,	%xmm5
+	.set	H_POW4,		%zmm6
+	.set	H_POW3,		%zmm7
+	.set	H_POW2,		%zmm8
+	.set	H_POW1,		%zmm9
+	.set	GFPOLY,		%zmm10
+	.set	GFPOLY_XMM,	%xmm10
+	.set	TMP0,		%zmm11
+	.set	TMP1,		%zmm12
+	.set	LO,		%zmm13
+	.set	MI,		%zmm14
+	.set	HI,		%zmm15
+	.set	HI_XMM,		%xmm15
+
+	// Load the accumulator into lane 0 of ACC.  This zeroes lanes 1..3.
+	vmovdqu		(ACC_PTR), ACC_XMM
+
+	// Handle nblocks == 1 specially, using only xmm registers.  This case
+	// is common (e.g. for 1-block associated data, the incremental AES-GCM
+	// lengths block, and the HCTR2 length and tweak blocks).
+	cmp		$1, NBLOCKS
+	jne		.Lblocks_more_than_1block\@
+
+	vmovq		.Lgfpoly(%rip), GFPOLY_XMM
+	vmovdqu		(DATA), DATA0_XMM
+.if \bswap_data
+	vpshufb		.Lbswap_mask(%rip), DATA0_XMM, DATA0_XMM
+.endif
+	vpxor		DATA0_XMM, ACC_XMM, ACC_XMM
+	vmovdqu		OFFSETOFEND_H_POWERS-16(KEY), DATA0_XMM	// H^1
+	vmovdqu		OFFSETOF_H_TIMES_XM64(KEY), DATA1_XMM	// H^1 * x^-64
+	_gf128hash_mul	DATA0_XMM, DATA1_XMM, ACC_XMM, ACC_XMM, GFPOLY_XMM, \
+			DATA2_XMM, DATA3_XMM
+	vmovdqu		ACC_XMM, (ACC_PTR)
+	// No vzeroupper is needed since only xmm registers were used.
+	RET
+
+.Lblocks_more_than_1block\@:
+.if \bswap_data
+	vbroadcasti32x4	.Lbswap_mask(%rip), BSWAP_MASK
+.endif
+	vpbroadcastq	.Lgfpoly(%rip), GFPOLY
+
+	// Process 16 blocks (256 bytes) at a time.
+	sub		$16, NBLOCKS
+	jb		.Lblocks_tail\@
+	vmovdqu8	OFFSETOF_H_POWERS+0*64(KEY), H_POW4	// [H^16..H^13]
+	vmovdqu8	OFFSETOF_H_POWERS+1*64(KEY), H_POW3	// [H^12..H^9]
+	vmovdqu8	OFFSETOF_H_POWERS+2*64(KEY), H_POW2	// [H^8..H^5]
+	vmovdqu8	OFFSETOF_H_POWERS+3*64(KEY), H_POW1	// [H^4..H^1]
+.Lblocks_loop_4x\@:
+	_gf128hash_4x	\bswap_data
+	add		$256, DATA
+	sub		$16, NBLOCKS
+	jae		.Lblocks_loop_4x\@
+
+.Lblocks_tail\@:
+	add		$16, NBLOCKS
+	jz		.Lblocks_done\@
+
+	// 1 <= n = NBLOCKS <= 15 blocks remain.  Multiply them by H^n..H^1,
+	// starting at h_powers16[16-n], using schoolbook multiplication.  Sum
+	// the unreduced products across all remaining vectors, and reduce once
+	// at the end.  After converting NBLOCKS to NBYTES (16*n <= 240 <= 255,
+	// so it fits in bzhi's 8-bit index), on each iteration bzhi builds a
+	// byte mask of all 1s if >= 64 bytes remain, or one bit per remaining
+	// data byte otherwise.  When fewer than 4 blocks remain, the
+	// zero-masked load doesn't access masked-out bytes, and the unmasked
+	// key-power load extends into the zeroized padding blocks; unused lanes
+	// of DATA0 are zero (vpshufb keeps them zero) and contribute 0.
+	shl		$4, NBLOCKS			// NBYTES = 16*n
+	mov		NBYTES, POWERS_PTR
+	neg		POWERS_PTR
+	lea		OFFSETOFEND_H_POWERS(KEY,POWERS_PTR), POWERS_PTR
+	mov		$-1, ALL_ONES
+
+	// First vector (up to 4 blocks): XOR ACC into block 0 (lanes 1..3 of
+	// ACC are zero) and initialize LO, MI, and HI.
+	bzhi		NBYTES, ALL_ONES, %r9
+	kmovq		%r9, %k1
+	vmovdqu8	(DATA), DATA0{%k1}{z}
+	vmovdqu8	(POWERS_PTR), H_POW1
+.if \bswap_data
+	vpshufb		BSWAP_MASK, DATA0, DATA0
+.endif
+	vpxord		ACC, DATA0, DATA0
+	vpclmulqdq	$0x00, H_POW1, DATA0, LO
+	vpclmulqdq	$0x01, H_POW1, DATA0, MI
+	vpclmulqdq	$0x10, H_POW1, DATA0, TMP0
+	vpclmulqdq	$0x11, H_POW1, DATA0, HI
+	vpxord		TMP0, MI, MI
+	sub		$64, NBYTES
+	jle		.Lblocks_tail_reduce\@
+
+	// Remaining vectors (1..3 iterations).
+.Lblocks_tail_loop\@:
+	add		$64, DATA
+	add		$64, POWERS_PTR
+	bzhi		NBYTES, ALL_ONES, %r9
+	kmovq		%r9, %k1
+	vmovdqu8	(DATA), DATA0{%k1}{z}
+	vmovdqu8	(POWERS_PTR), H_POW1
+.if \bswap_data
+	vpshufb		BSWAP_MASK, DATA0, DATA0
+.endif
+	vpclmulqdq	$0x00, H_POW1, DATA0, TMP0
+	vpclmulqdq	$0x01, H_POW1, DATA0, TMP1
+	vpclmulqdq	$0x10, H_POW1, DATA0, DATA2
+	vpclmulqdq	$0x11, H_POW1, DATA0, DATA1
+	vpxord		TMP0, LO, LO
+	vpternlogd	$0x96, DATA2, TMP1, MI
+	vpxord		DATA1, HI, HI
+	sub		$64, NBYTES
+	jg		.Lblocks_tail_loop\@
+
+.Lblocks_tail_reduce\@:
+	// Reduce all 4 lanes, and XOR them together into ACC_XMM.
+	_gf128hash_reduce LO, MI, HI, GFPOLY, TMP0
+	_horizontal_xor	HI, HI_XMM, ACC_XMM, DATA0_XMM, DATA1_XMM, DATA2_XMM
+
+.Lblocks_done\@:
+	vmovdqu		ACC_XMM, (ACC_PTR)
+	vzeroupper
+	RET
+.endm
+
+SYM_FUNC_START(ghash_preparekey_vpclmul_avx512)
+	_gf128hash_preparekey	1
+SYM_FUNC_END(ghash_preparekey_vpclmul_avx512)
+SYM_FUNC_START(polyval_preparekey_vpclmul_avx512)
+	_gf128hash_preparekey	0
+SYM_FUNC_END(polyval_preparekey_vpclmul_avx512)
+SYM_FUNC_START(ghash_blocks_vpclmul_avx512)
+	_gf128hash_blocks	1
+SYM_FUNC_END(ghash_blocks_vpclmul_avx512)
+SYM_FUNC_START(polyval_blocks_vpclmul_avx512)
+	_gf128hash_blocks	0
+SYM_FUNC_END(polyval_blocks_vpclmul_avx512)
diff --git a/lib/crypto/x86/gf128hash.h b/lib/crypto/x86/gf128hash.h
index 4470be6dfea2d..9fb826d62e829 100644
--- a/lib/crypto/x86/gf128hash.h
+++ b/lib/crypto/x86/gf128hash.h
@@ -8,18 +8,24 @@
 #include "gf128hash-cpu.h"
 
 #define NUM_H_POWERS 8
+#define NUM_H_POWERS_AVX512 16
 
 /* The assembly code assumes the following offsets. */
 static_assert(sizeof_field(struct gf128hash_key_x86, h_powers) ==
 	      NUM_H_POWERS * sizeof(struct polyval_elem));
-static_assert(offsetof(struct gf128hash_key_x86, h_powers) == 0);
-static_assert(offsetof(struct gf128hash_key_x86, h_powers_xored) == 128);
-static_assert(offsetof(struct gf128hash_key_x86, h_times_xm64) == 192);
-static_assert(sizeof(struct gf128hash_key_x86) == 208);
+static_assert(sizeof_field(struct gf128hash_key_x86, h_powers16) ==
+	      NUM_H_POWERS_AVX512 * sizeof(struct polyval_elem));
+static_assert(offsetof(struct gf128hash_key_x86, h_powers_xored) == 0);
+static_assert(offsetof(struct gf128hash_key_x86, h_powers) == 128);
+static_assert(offsetof(struct gf128hash_key_x86, h_powers16) == 0);
+static_assert(offsetof(struct gf128hash_key_x86, padding) == 256);
+static_assert(offsetof(struct gf128hash_key_x86, h_times_xm64) == 304);
+static_assert(sizeof(struct gf128hash_key_x86) == 320);
 
 static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_pclmul);
 static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_pclmul_avx);
 static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_vpclmul_avx2);
+static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_vpclmul_avx512);
 
 void gf128hash_mul_pclmul(struct polyval_elem *acc,
 			  const struct gf128hash_key_x86 *key);
@@ -32,12 +38,16 @@ void ghash_preparekey_pclmul_avx(struct gf128hash_key_x86 *key,
 				 const u8 raw_key[GHASH_BLOCK_SIZE]);
 void ghash_preparekey_vpclmul_avx2(struct gf128hash_key_x86 *key,
 				   const u8 raw_key[GHASH_BLOCK_SIZE]);
+void ghash_preparekey_vpclmul_avx512(struct gf128hash_key_x86 *key,
+				     const u8 raw_key[GHASH_BLOCK_SIZE]);
 void polyval_preparekey_pclmul(struct gf128hash_key_x86 *key,
 			       const u8 raw_key[POLYVAL_BLOCK_SIZE]);
 void polyval_preparekey_pclmul_avx(struct gf128hash_key_x86 *key,
 				   const u8 raw_key[POLYVAL_BLOCK_SIZE]);
 void polyval_preparekey_vpclmul_avx2(struct gf128hash_key_x86 *key,
 				     const u8 raw_key[POLYVAL_BLOCK_SIZE]);
+void polyval_preparekey_vpclmul_avx512(struct gf128hash_key_x86 *key,
+				       const u8 raw_key[POLYVAL_BLOCK_SIZE]);
 
 void ghash_blocks_pclmul(struct polyval_elem *acc,
 			 const struct gf128hash_key_x86 *key, const u8 *data,
@@ -48,6 +58,9 @@ void ghash_blocks_pclmul_avx(struct polyval_elem *acc,
 void ghash_blocks_vpclmul_avx2(struct polyval_elem *acc,
 			       const struct gf128hash_key_x86 *key,
 			       const u8 *data, size_t nblocks);
+void ghash_blocks_vpclmul_avx512(struct polyval_elem *acc,
+				 const struct gf128hash_key_x86 *key,
+				 const u8 *data, size_t nblocks);
 void polyval_blocks_pclmul(struct polyval_elem *acc,
 			   const struct gf128hash_key_x86 *key, const u8 *data,
 			   size_t nblocks);
@@ -57,6 +70,9 @@ void polyval_blocks_pclmul_avx(struct polyval_elem *acc,
 void polyval_blocks_vpclmul_avx2(struct polyval_elem *acc,
 				 const struct gf128hash_key_x86 *key,
 				 const u8 *data, size_t nblocks);
+void polyval_blocks_vpclmul_avx512(struct polyval_elem *acc,
+				   const struct gf128hash_key_x86 *key,
+				   const u8 *data, size_t nblocks);
 
 static __always_inline void
 __gf128hash_mul_x86_fpu(struct polyval_elem *acc,
@@ -73,7 +89,9 @@ __ghash_blocks_x86_fpu(struct polyval_elem *acc,
 		       const struct gf128hash_key_x86 *key, const u8 *data,
 		       size_t nblocks)
 {
-	if (static_branch_likely(&have_vpclmul_avx2))
+	if (static_branch_likely(&have_vpclmul_avx512))
+		ghash_blocks_vpclmul_avx512(acc, key, data, nblocks);
+	else if (static_branch_likely(&have_vpclmul_avx2))
 		ghash_blocks_vpclmul_avx2(acc, key, data, nblocks);
 	else if (static_branch_likely(&have_pclmul_avx))
 		ghash_blocks_pclmul_avx(acc, key, data, nblocks);
@@ -101,6 +119,14 @@ gf128hash_preparekey_x86(struct gf128hash_key_x86 *key, const u8 raw_key[16],
 {
 	if (static_branch_likely(&have_pclmul) && likely(irq_fpu_usable())) {
 		kernel_fpu_begin_mask(0);
+		if (static_branch_likely(&have_vpclmul_avx512)) {
+			if (is_ghash)
+				ghash_preparekey_vpclmul_avx512(key, raw_key);
+			else
+				polyval_preparekey_vpclmul_avx512(key, raw_key);
+			kernel_fpu_end();
+			return;
+		}
 		if (static_branch_likely(&have_vpclmul_avx2)) {
 			if (is_ghash)
 				ghash_preparekey_vpclmul_avx2(key, raw_key);
@@ -118,6 +144,8 @@ gf128hash_preparekey_x86(struct gf128hash_key_x86 *key, const u8 raw_key[16],
 				polyval_preparekey_pclmul(key, raw_key);
 		}
 		kernel_fpu_end();
+		memset(key->unused, 0, sizeof(key->unused));
+		memset(key->padding, 0, sizeof(key->padding));
 		return;
 	}
 	if (is_ghash)
@@ -125,11 +153,10 @@ gf128hash_preparekey_x86(struct gf128hash_key_x86 *key, const u8 raw_key[16],
 	else
 		memcpy(&key->h_powers[NUM_H_POWERS - 1], raw_key,
 		       POLYVAL_BLOCK_SIZE);
-	for (int i = NUM_H_POWERS - 2; i >= 0; i--) {
-		key->h_powers[i] = key->h_powers[i + 1];
-		polyval_mul_generic(&key->h_powers[i],
-				    &key->h_powers[NUM_H_POWERS - 1]);
-	}
+	static_assert(offsetof(struct gf128hash_key_x86,
+			       h_powers[NUM_H_POWERS - 1]) ==
+		      offsetof(struct gf128hash_key_x86,
+			       h_powers16[NUM_H_POWERS_AVX512 - 1]));
 	/*
 	 * Decide which key layout to use.  This fallback code is reached when
 	 * the static keys aren't set, which might be because
@@ -137,17 +164,34 @@ gf128hash_preparekey_x86(struct gf128hash_key_x86 *key, const u8 raw_key[16],
 	 * here so that keys prepared before gf128hash_mod_init() have the same
 	 * layout as keys prepared afterwards.
 	 */
-	if (gf128hash_use_vpclmul_avx2()) {
-		static const u8 indices[8] = { 0, 2, 1, 3, 4, 6, 5, 7 };
-
-		for (int i = 0; i < NUM_H_POWERS; i++)
-			key->h_powers_xored[i] = key->h_powers[indices[i]].lo ^
-						 key->h_powers[indices[i]].hi;
+	if (gf128hash_use_vpclmul_avx512()) {
+		for (int i = NUM_H_POWERS_AVX512 - 2; i >= 0; i--) {
+			key->h_powers16[i] = key->h_powers16[i + 1];
+			polyval_mul_generic(
+				&key->h_powers16[i],
+				&key->h_powers16[NUM_H_POWERS_AVX512 - 1]);
+		}
 	} else {
-		for (int i = 0; i < NUM_H_POWERS; i++)
-			key->h_powers_xored[i] = key->h_powers[i].lo ^
-						 key->h_powers[i].hi;
+		for (int i = NUM_H_POWERS - 2; i >= 0; i--) {
+			key->h_powers[i] = key->h_powers[i + 1];
+			polyval_mul_generic(&key->h_powers[i],
+					    &key->h_powers[NUM_H_POWERS - 1]);
+		}
+		if (gf128hash_use_vpclmul_avx2()) {
+			static const u8 indices[8] = { 0, 2, 1, 3, 4, 6, 5, 7 };
+
+			for (int i = 0; i < NUM_H_POWERS; i++)
+				key->h_powers_xored[i] =
+					key->h_powers[indices[i]].lo ^
+					key->h_powers[indices[i]].hi;
+		} else {
+			for (int i = 0; i < NUM_H_POWERS; i++)
+				key->h_powers_xored[i] = key->h_powers[i].lo ^
+							 key->h_powers[i].hi;
+		}
+		memset(key->unused, 0, sizeof(key->unused));
 	}
+	memset(key->padding, 0, sizeof(key->padding));
 	polyval_mul_xm64_generic(&key->h_times_xm64,
 				 &key->h_powers[NUM_H_POWERS - 1]);
 }
@@ -207,6 +251,9 @@ gf128hash_blocks_x86(struct polyval_elem *acc,
 			kernel_fpu_begin_mask(0);
 			if (is_ghash)
 				__ghash_blocks_x86_fpu(acc, key, data, n);
+			else if (static_branch_likely(&have_vpclmul_avx512))
+				polyval_blocks_vpclmul_avx512(acc, key, data,
+							      n);
 			else if (static_branch_likely(&have_vpclmul_avx2))
 				polyval_blocks_vpclmul_avx2(acc, key, data, n);
 			else if (static_branch_likely(&have_pclmul_avx))
@@ -253,8 +300,11 @@ static void gf128hash_mod_init_arch(void)
 
 	if (gf128hash_use_pclmul_avx()) {
 		static_branch_enable(&have_pclmul_avx);
-		if (gf128hash_use_vpclmul_avx2())
+		if (gf128hash_use_vpclmul_avx2()) {
 			static_branch_enable(&have_vpclmul_avx2);
+			if (gf128hash_use_vpclmul_avx512())
+				static_branch_enable(&have_vpclmul_avx512);
+		}
 	}
 	static_branch_enable(&have_pclmul);
 }
-- 
2.56.1


  parent reply	other threads:[~2026-11-29 20:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 20:02 [PATCH 00/11] Migrate x86_64 AES-GCM code into library Eric Biggers
2026-11-29 20:02 ` [PATCH 01/11] crypto: aes - Flush dcache in CBC-CTS and XTS linear fast paths Eric Biggers
2026-11-29 20:02 ` [PATCH 02/11] lib/crypto: gf128hash: Link libgf128hash before libaes Eric Biggers
2026-11-29 20:02 ` [PATCH 03/11] lib/crypto: aes-gcm: Add one-shot AES-GCM arch hooks Eric Biggers
2026-11-29 20:02 ` [PATCH 04/11] crypto: aes - Add linear fast path for AES-GCM Eric Biggers
2026-11-29 20:02 ` [PATCH 05/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with PCLMULQDQ and AVX Eric Biggers
2026-11-29 20:02 ` [PATCH 06/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with VPCLMULQDQ and AVX2 Eric Biggers
2026-11-29 20:02 ` Eric Biggers [this message]
2026-11-29 20:02 ` [PATCH 08/11] lib/crypto: x86/aes-gcm: Migrate AES-NI optimized code into library Eric Biggers
2026-11-29 20:02 ` [PATCH 09/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX2 " Eric Biggers
2026-11-29 20:02 ` [PATCH 10/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX512 " Eric Biggers
2026-11-29 20:02 ` [PATCH 11/11] crypto: x86/aesni-intel - Remove empty module Eric Biggers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129200245.146503-8-ebiggers@kernel.org \
    --to=ebiggers@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=antonio@mandelbit.com \
    --cc=ardb@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox