From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796500290; b=1C4suqRT1SM+gL8kU7p46H2eceWELLo4OOsFf2ygAmcOisB3gnkjX7Zy2DVGB3Q2FdCG VgPfFY2szixM4stsrErBiJF6Jjxg0uGePh5GlcoErcMku02Uq1oI5brNokAzWaXeq5mpT /cJuShQlmfl770a6YDmLB2/pl4wIvPP852f4xBShgBOxOdMjy/TOcBmdVcjDu+wkorNmq aWe0h5Y37LFYjTeOSTpBEbFAA4iMD9SiOWFPJ1gM8Ir6DhhgCdfRl4q4qqoPUGBj8vqLS YcV6hF8PEYdRSrCWK/wCi1AnGYv8ox0GKcMaCRhwzna9PFjwwMlqpFQPu6XUoZVlgOQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796500290; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Rspamd-Queue-Id; bh=Out5h3ZFrznOMpYONq6PPk7CaRaMHB8xdr7KmgJSnS8=; b=R7BCdR1zvoJPlwDIk0kPOGzBsc135EqD3C5Twfg/vsaDvKGZuzYCDJmohTg5yG4fchJc uzJBjRhkhJHU0ByrqjQsIvqT06fHQ0IijH6gSYHiPxrSTnYG4FgQAHwSThtiFraZ0uh67 NZGmQtFVm7SBHs4QGjddcdoX4B/M1xmve8Xjovl0QMumrSjysJhLrp3Ow0Hsa1mh1lrwQ 9aC8kKtWQMgJtSLxuKoimKCBrDbVtSIy+96GRMgQaLS2WUyGq+2jEmYKsIQbNW7BFxBPw kggTXgyyq2TUBRhA8DVrvxIlQBLWXy4wdY9Rq84NY+hXiY959T/1/QPxrNUQjNnU6BQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=mandelbit.com; spf=pass smtp.mailfrom=mandelbit.com; dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b="x6Bx/Xis"; arc=none smtp.remote-ip=195.10.208.47 Received: from mout-b-107.mailbox.org (mout-b-107.mailbox.org [195.10.208.47]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F27A606A31F for ; Sat, 5 Dec 2026 19:51:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4F27A606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=x6Bx/Xis Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-b-107.mailbox.org (Postfix) with ESMTPS id 4jhfBG67Yxz3wsP; Sat, 05 Dec 2026 20:51:18 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com; s=MBO0001; t=1796500278; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Out5h3ZFrznOMpYONq6PPk7CaRaMHB8xdr7KmgJSnS8=; b=x6Bx/XislgKVCo1XsNnpkpaTzbYrmAK8auO4KHBnpCgqCQx90pTRVN9Z6884YlVUQ22gmF dsJxxzMVlF0dBXRys41kSdEhpw2r4Wgvcw6jdLcu13TiIAjgm4pGFKWzjs/8xjMOAMYCvf pj0AmveyOVYYNRQIyKsDmVuhpLeEe2N04cVRL+S4wirrTlRoyzG45hdTXBPpfr84TJU200 EYcWcFJQDpWedsb9S26dkHuLQG+gXOw4O8+eP+OolE++R8dmOsF7C0OLEOlCs8Pr2R//iI pisyAt38Z6BrxiBne2rHhcz/qLv9Dv1vFGdESfQUCvoTT2uLn4IoGHcNVmcRhA== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of antonio@mandelbit.com designates 2001:67c:2050:b231:465::102 as permitted sender) smtp.mailfrom=antonio@mandelbit.com From: Antonio Quartulli To: Eric Biggers , linux-crypto@vger.kernel.org Cc: Antonio Quartulli , Ard Biesheuvel , "Jason A . Donenfeld" , Herbert Xu , x86@kernel.org Subject: [RFC PATCH] lib/crypto: x86/aes-gcm: Process short associated data in one asm call Date: Sat, 5 Dec 2026 20:51:06 +0100 Message-ID: <20261205195107.181324-1-antonio@mandelbit.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org> References: <20261129200245.146503-1-ebiggers@kernel.org> X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4jhfBG67Yxz3wsP The one-shot AES-GCM path XORs associated data shorter than one block byte by byte into the zeroed GHASH accumulator in memory and then calls the GHASH multiplication, which reloads it. Network protocols commonly use 8 to 13 bytes of associated data, so this runs once per packet. Add aes_gcm_ad_short_vaes_avx512(), which loads 1 to 15 bytes of associated data with a masked load straight from the caller's buffer, multiplies the zero-padded block by H in registers and stores the accumulator once, without reading it. Use it from aes_gcm_crypt_x86() on the VAES+AVX512 tier, and zero the accumulator only on the other paths. Signed-off-by: Antonio Quartulli --- Hi Eric, this patch applies on top of your series (v1). I converted ovpn to aes_gcm_encrypt()/aes_gcm_decrypt() for linear packets (8 bytes of associated data per record) and compared it with what we had been testing before: the pre-migration VAES+AVX512 code called directly from ovpn in one FPU section, as mentioned in one of my earlier mails. The library was about 2% slower on the receiver. Most of the difference was the sub-block associated data path, which this patch replaces on the VAES+AVX512 tier. Single ovpn UDP tunnel, AMD Ryzen 9 9950X, 100G mlx5, receiver-bound, 3 rounds each; library vs the direct calls, on the same boot: your series + this patch UDP inside tunnel -2.1% +0.2% TCP inside tunnel -2.3% -2.0% Receiver AES-GCM cost per packet (receiver cycles with AES-GCM minus AEGIS on the same variant), library minus direct calls: UDP inside tunnel +30 cycles +5 cycles TCP inside tunnel +25 cycles +15 cycles TCP is noisier; what is left there is mostly the decrypt residual below. For records of 32 to 128 bytes (e.g. the TCP ACKs) the library is within a few cycles of the direct calls. Per operation in a pinned VM (AES-256, 1420 bytes of data, 8 bytes of associated data; TSC ticks, library vs direct calls): your series + this patch encrypt 410 vs 400 406 vs 403 decrypt 417 vs 398 409 vs 400 The remaining decrypt difference sits in the C glue around the asm calls (about 40 more instructions per operation); the asm itself is not slower. Only the VAES+AVX512 tier is covered, since masked loads make this a very small function. The VAES+AVX2 tier could do the same with a partial-block load; I can follow up with that if you think it is worth it..... If it makes sense to you, feel free to fold this into patch 10. Tested with the aes_gcm, ghash and polyval KUnit suites, both on VAES+AVX512 and booted with clearcpuid=avx512bw (VAES+AVX2 tier), and by cross-checking encryption, decryption and tag rejection against the pre-migration code for every associated data length from 0 to 40 bytes combined with data lengths 0, 1, 15, 16, 17, 255 and 1420. **Only tested on AMD Zen 5** Thanks! Antonio lib/crypto/x86/aes-gcm-vaes-avx512.S | 39 ++++++++++++++++++++++++++++ lib/crypto/x86/aes.h | 19 +++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/crypto/x86/aes-gcm-vaes-avx512.S b/lib/crypto/x86/aes-gcm-vaes-avx512.S index 70b9adc02b41..085cff5fdfef 100644 --- a/lib/crypto/x86/aes-gcm-vaes-avx512.S +++ b/lib/crypto/x86/aes-gcm-vaes-avx512.S @@ -896,6 +896,45 @@ SYM_FUNC_START(aes_gcm_dec_update_vaes_avx512) _aes_gcm_update 0 SYM_FUNC_END(aes_gcm_dec_update_vaes_avx512) +// void aes_gcm_ad_short_vaes_avx512(const struct aes_gcm_key *key, +// struct polyval_elem *ghash_acc, +// const u8 *ad, size_t ad_len); +// +// Process associated data that is shorter than one block and is the whole +// associated data of a message: 1 <= |ad_len| <= 15. The GHASH accumulator +// would be zero, so it is not read: *|ghash_acc| is set to the zero-padded +// block multiplied by H. The block is loaded with a masked load straight from +// |ad|, reading exactly |ad_len| bytes. This is the common case for network +// protocols (8 to 13 bytes), and doing it in one short leaf function avoids +// the generic path's byte-wise update of the accumulator in memory followed by +// a vector load of it. +SYM_FUNC_START(aes_gcm_ad_short_vaes_avx512) + // Function arguments + .set KEY, %rdi + .set GHASH_ACC_PTR, %rsi + .set AD, %rdx + .set ADLEN, %ecx + // %eax, %xmm1-%xmm3, and %k1 are used as temporary registers. + .set GFPOLY, %xmm4 + .set GHASH_ACC, %xmm6 + .set H_POW1, %xmm7 + + vmovdqa .Lgfpoly(%rip), GFPOLY + mov $-1, %eax + bzhi ADLEN, %eax, %eax + kmovd %eax, %k1 + vmovdqu8 (AD), GHASH_ACC{%k1}{z} + vpshufb .Lbswap_mask(%rip), GHASH_ACC, GHASH_ACC + vmovdqu8 OFFSETOFEND_H_POWERS-16(KEY), H_POW1 +.irp i, 0,1,2,3,4,5,6,7,8,9 + _ghash_mul_step \i, H_POW1, GHASH_ACC, GHASH_ACC, GFPOLY, \ + %xmm1, %xmm2, %xmm3 +.endr + vmovdqu GHASH_ACC, (GHASH_ACC_PTR) + // No need for vzeroupper, since only xmm registers were used. + RET +SYM_FUNC_END(aes_gcm_ad_short_vaes_avx512) + SYM_FUNC_START(aes_gcm_enc_final_vaes_avx512) _aes_gcm_final 1 SYM_FUNC_END(aes_gcm_enc_final_vaes_avx512) diff --git a/lib/crypto/x86/aes.h b/lib/crypto/x86/aes.h index e0c65904eef0..5076cfd16fa4 100644 --- a/lib/crypto/x86/aes.h +++ b/lib/crypto/x86/aes.h @@ -486,6 +486,9 @@ bool aes_gcm_dec_final_vaes_avx2(const struct aes_gcm_key *key, const struct polyval_elem *ghash_acc, u64 total_aadlen, u64 total_datalen, const u8 *authtag); +void aes_gcm_ad_short_vaes_avx512(const struct aes_gcm_key *key, + struct polyval_elem *ghash_acc, + const u8 *ad, size_t ad_len); void aes_gcm_enc_final_vaes_avx512(const struct aes_gcm_key *key, const u8 nonce[12], const struct polyval_elem *ghash_acc, @@ -621,7 +624,7 @@ aes_gcm_crypt_x86(u8 *dst, const u8 *src, size_t data_len, u8 *authtag, const u8 *ad, size_t ad_len, const u8 nonce[12], const struct aes_gcm_key *key, bool enc, int *err) { - struct polyval_elem ghash_acc = {}; + struct polyval_elem ghash_acc; __be32 ctr32[4]; if (!static_branch_likely(&have_aesni_pclmul) || @@ -633,8 +636,18 @@ aes_gcm_crypt_x86(u8 *dst, const u8 *src, size_t data_len, u8 *authtag, kernel_fpu_begin_mask(0); - if (ad_len) - aes_gcm_process_ad_x86(key, &ghash_acc, ad, ad_len); + /* + * Associated data shorter than one block (common for network protocols) + * initializes the accumulator directly, without reading it. + */ + if (ad_len && ad_len < GHASH_BLOCK_SIZE && + static_branch_likely(&have_vaes_avx512)) { + aes_gcm_ad_short_vaes_avx512(key, &ghash_acc, ad, ad_len); + } else { + memset(&ghash_acc, 0, sizeof(ghash_acc)); + if (ad_len) + aes_gcm_process_ad_x86(key, &ghash_acc, ad, ad_len); + } if (likely(data_len)) aes_gcm_update_x86_fpu(dst, src, data_len, &ghash_acc, ctr32, -- 2.56.1