x86: add missing vzeroupper instructions
Subsystems (6)
cover x86: add missing vzeroupper instructions
Assembly code using YMM or ZMM registers is supposed to end with the vzeroupper instruction in order to avoid degrading the performance of any later SSE code. Since this only affects performance and not correctness, it is sometimes overlooked. Most kernel code does it correctly, but a few cases were missed. This series fixes them.
It should be easiest to take the full series through the x86 tree.
Eric Biggers (6):
xor: add missing vzeroupper to AVX code
raid6: add missing vzeroupper to AVX2 code
raid6: add missing vzeroupper to AVX-512 code
crypto: x86/aria - add missing vzeroupper in AVX2 code
crypto: x86/aria - add missing vzeroupper in AVX-512 code
netfilter: nft_set_pipapo_avx2: add missing vzeroupper
arch/x86/crypto/aria-aesni-avx2-asm_64.S | 6 ++++++ arch/x86/crypto/aria-gfni-avx512-asm_64.S | 3 +++ lib/raid/raid6/x86/avx2.c | 6 ++++++ lib/raid/raid6/x86/avx512.c | 6 ++++++ lib/raid/raid6/x86/recov_avx2.c | 2 ++ lib/raid/raid6/x86/recov_avx512.c | 2 ++ lib/raid/xor/x86/xor-avx.c | 1 + net/netfilter/nft_set_pipapo_avx2.c | 17 ++++++++--------- 8 files changed, 34 insertions(+), 9 deletions(-)
base-commit: db2ddb87143519e20a95aa36c60b36107b736a58
Signature · 1 line
2.55.0
PATCH 1/6 xor: add missing vzeroupper to AVX code
Since the AVX optimized XOR code uses YMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
--- lib/raid/xor/x86/xor-avx.c | 1 + 1 file changed, 1 insertion(+)
lib/raid/xor/x86/xor-avx.c +1 -0
@@ -147,6 +147,7 @@ static void xor_gen_avx(void *dest, void **srcs, unsigned int src_cnt, {kernel_fpu_begin ();xor_gen_avx_inner (dest, srcs, src_cnt, bytes); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
Signature · 1 line
2.55.0
PATCH 2/6 raid6: add missing vzeroupper to AVX2 code
In reply to Eric BiggersSince the AVX2 optimized RAID6 code uses YMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
--- lib/raid/raid6/x86/avx2.c | 6 ++++++ lib/raid/raid6/x86/recov_avx2.c | 2 ++ 2 files changed, 8 insertions(+)
lib/raid/raid6/x86/avx2.c +6 -0
@@ -67,6 +67,7 @@ static void raid6_avx21_gen_syndrome(int disks, size_t bytes, void **ptrs) } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -115,6 +116,7 @@ static void raid6_avx21_xor_syndrome(int disks, int start, int stop, } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -175,6 +177,7 @@ static void raid6_avx22_gen_syndrome(int disks, size_t bytes, void **ptrs) } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -243,6 +246,7 @@ static void raid6_avx22_xor_syndrome(int disks, int start, int stop, } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -334,6 +338,7 @@ static void raid6_avx24_gen_syndrome(int disks, size_t bytes, void **ptrs) } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -444,6 +449,7 @@ static void raid6_avx24_xor_syndrome(int disks, int start, int stop, asmvolatile ("vmovntdq %%ymm14,%0" :"=m" (q[d+96 ])); } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
lib/raid/raid6/x86/recov_avx2.c +2 -0
@@ -176,6 +176,7 @@ static void raid6_2data_recov_avx2(int disks, size_t bytes, int faila,#endif } + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -293,6 +294,7 @@ static void raid6_datap_recov_avx2(int disks, size_t bytes, int faila,#endif } + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
Signature · 1 line
2.55.0
PATCH 3/6 raid6: add missing vzeroupper to AVX-512 code
In reply to Eric BiggersSince the AVX-512 optimized RAID6 code uses ZMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
--- lib/raid/raid6/x86/avx512.c | 6 ++++++ lib/raid/raid6/x86/recov_avx512.c | 2 ++ 2 files changed, 8 insertions(+)
lib/raid/raid6/x86/avx512.c +6 -0
@@ -78,6 +78,7 @@ static void raid6_avx5121_gen_syndrome(int disks, size_t bytes, void **ptrs) } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -137,6 +138,7 @@ static void raid6_avx5121_xor_syndrome(int disks, int start, int stop, } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -208,6 +210,7 @@ static void raid6_avx5122_gen_syndrome(int disks, size_t bytes, void **ptrs) } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -292,6 +295,7 @@ static void raid6_avx5122_xor_syndrome(int disks, int start, int stop, } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -396,6 +400,7 @@ static void raid6_avx5124_gen_syndrome(int disks, size_t bytes, void **ptrs) } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -529,6 +534,7 @@ static void raid6_avx5124_xor_syndrome(int disks, int start, int stop, "m" (q[d+128]), "m" (q[d+192])); } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );kernel_fpu_end (); }const struct raid6_calls raid6_avx512x4 = {
lib/raid/raid6/x86/recov_avx512.c +2 -0
@@ -211,6 +211,7 @@ static void raid6_2data_recov_avx512(int disks, size_t bytes, int faila,#endif } + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -353,6 +354,7 @@ static void raid6_datap_recov_avx512(int disks, size_t bytes, int faila,#endif } + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
Signature · 1 line
2.55.0
PATCH 4/6 crypto: x86/aria - add missing vzeroupper in AVX2 code
In reply to Eric BiggersSince the AVX2 optimized ARIA code uses YMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
--- arch/x86/crypto/aria-aesni-avx2-asm_64.S | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/crypto/aria-aesni-avx2-asm_64.S +6 -0
@@ -982,6 +982,7 @@ SYM_TYPED_FUNC_START(aria_aesni_avx2_encrypt_32way) %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14, %ymm15, %rax); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_aesni_avx2_encrypt_32way)
@@ -1007,6 +1008,7 @@ SYM_TYPED_FUNC_START(aria_aesni_avx2_decrypt_32way) %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14, %ymm15, %rax); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_aesni_avx2_decrypt_32way)
@@ -1209,6 +1211,7 @@ SYM_TYPED_FUNC_START(aria_aesni_avx2_ctr_crypt_32way) %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14, %ymm15, %r10); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_aesni_avx2_ctr_crypt_32way)
@@ -1359,6 +1362,7 @@ SYM_TYPED_FUNC_START(aria_aesni_avx2_gfni_encrypt_32way) %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14, %ymm15, %rax); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_aesni_avx2_gfni_encrypt_32way)
@@ -1384,6 +1388,7 @@ SYM_TYPED_FUNC_START(aria_aesni_avx2_gfni_decrypt_32way) %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14, %ymm15, %rax); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_aesni_avx2_gfni_decrypt_32way)
@@ -1428,6 +1433,7 @@ SYM_TYPED_FUNC_START(aria_aesni_avx2_gfni_ctr_crypt_32way) %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14, %ymm15, %r10); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_aesni_avx2_gfni_ctr_crypt_32way)
Signature · 1 line
2.55.0
PATCH 5/6 crypto: x86/aria - add missing vzeroupper in AVX-512 code
In reply to Eric BiggersSince the AVX-512 optimized ARIA code uses ZMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
--- arch/x86/crypto/aria-gfni-avx512-asm_64.S | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/crypto/aria-gfni-avx512-asm_64.S +3 -0
@@ -800,6 +800,7 @@ SYM_TYPED_FUNC_START(aria_gfni_avx512_encrypt_64way) %zmm9, %zmm8, %zmm11, %zmm10, %zmm12, %zmm13, %zmm14, %zmm15, %rax); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_gfni_avx512_encrypt_64way)
@@ -825,6 +826,7 @@ SYM_TYPED_FUNC_START(aria_gfni_avx512_decrypt_64way) %zmm9, %zmm8, %zmm11, %zmm10, %zmm12, %zmm13, %zmm14, %zmm15, %rax); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_gfni_avx512_decrypt_64way)
@@ -966,6 +968,7 @@ SYM_TYPED_FUNC_START(aria_gfni_avx512_ctr_crypt_64way) %zmm9, %zmm8, %zmm11, %zmm10, %zmm12, %zmm13, %zmm14, %zmm15, %r10); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_gfni_avx512_ctr_crypt_64way)
Signature · 1 line
2.55.0
PATCH 6/6 netfilter: nft_set_pipapo_avx2: add missing vzeroupper
In reply to Eric BiggersSince pipapo_get_avx2() uses YMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
--- net/netfilter/nft_set_pipapo_avx2.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
net/netfilter/nft_set_pipapo_avx2.c +8 -9
@@ -1134,6 +1134,7 @@ struct nft_pipapo_elem *pipapo_get_avx2(const struct nft_pipapo_match *m,struct nft_pipapo_scratch *scratch;const struct nft_pipapo_field *f;unsigned long *res, *fill, *map; +struct nft_pipapo_elem *e;bool map_index;int ret =0 ;int i;
@@ -1207,14 +1208,11 @@ struct nft_pipapo_elem *pipapo_get_avx2(const struct nft_pipapo_match *m, next_match:if (ret <0 ) { scratch->map_index = map_index; -kernel_fpu_end (); -__local_unlock_nested_bh (&scratch->bh_lock); -return NULL ; + e =NULL ; + goto out; }if (last) { -struct nft_pipapo_elem *e; - e = f->mt[ret].e;if (unlikely (__nft_set_elem_expired (&e->ext, tstamp) || !nft_set_elem_active (&e->ext, genmask))) {
@@ -1224,9 +1222,7 @@ struct nft_pipapo_elem *pipapo_get_avx2(const struct nft_pipapo_match *m, } scratch->map_index = map_index; -kernel_fpu_end (); -__local_unlock_nested_bh (&scratch->bh_lock); -return e; + goto out; } map_index = !map_index;
@@ -1234,9 +1230,12 @@ struct nft_pipapo_elem *pipapo_get_avx2(const struct nft_pipapo_match *m, data +=NFT_PIPAPO_GROUPS_PADDED_SIZE (f); } + e =NULL ; +out: + asmvolatile ("vzeroupper" );kernel_fpu_end ();__local_unlock_nested_bh (&scratch->bh_lock); -return NULL ; +return e; }/**
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 3/6] raid6: add missing vzeroupper to AVX-512 code
In reply to Eric BiggersThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Undefined behavior in inline assembly due to writing to input operands. --
--- Patch [3]: [PATCH 3/6] raid6: add missing vzeroupper to AVX-512 code --- commit c5b5d02d83bf2abe1380d3036a95bcf38db79ff8
Author: Eric Biggers <[email protected]>
raid6: add missing vzeroupper to AVX-512 code
Since the AVX-512 optimized RAID6 code uses ZMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
lib/raid/raid6/x86/recov_avx512.c static void raid6_2data_recov_avx512(int disks, size_t bytes, int faila,
Quoted 5 lines
lib/raid/raid6/x86/recov_avx512.c +0
-0@@ -211,6 +211,7 @@ static void raid6_2data_recov_avx512(int disks, size_t bytes, int faila,
[Severity: High]
This is a pre-existing issue, but while reviewing this code, I noticed that earlier in this function and in raid6_datap_recov_avx512(), the inline assembly writes to memory operands that are listed only in the input constraints list.
For example, in raid6_2data_recov_avx512():
asm volatile("vmovdqa64 %%zmm1, %0\n\t"
"vmovdqa64 %%zmm13,%1\n\t"
"vpxorq %%zmm1, %%zmm0, %%zmm0\n\t"
"vpxorq %%zmm13, %%zmm8, %%zmm8"
:
: "m" (dq[0]), "m" (dq[64]));And in raid6_datap_recov_avx512():
asm volatile("vmovdqa64 %%zmm1, %0\n\t"
"vmovdqa64 %%zmm14, %1\n\t"
"vmovdqa64 %%zmm2, %2\n\t"
"vmovdqa64 %%zmm12,%3"
:
: "m" (dq[0]), "m" (dq[64]), "m" (p[0]),
"m" (p[64]));Could this cause data corruption or miscompilation?
GCC documentation forbids writing to input operands. If GCC assumes input operands are unmodified, might it optimize away the writes, cache stale values, or reorder accesses during RAID6 recovery?
Quoted 6 lines
lib/raid/raid6/x86/recov_avx512.c +1
-0#endif } + asmvolatile ("vzeroupper" );kernel_fpu_end (); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH 2/6] raid6: add missing vzeroupper to AVX2 code
In reply to Eric BiggersThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing vzeroupper in AVX-512 RAID6 code leading to severe AVX-SSE transition penalties --
--- Patch [2]: [PATCH 2/6] raid6: add missing vzeroupper to AVX2 code --- commit 053e34bcf70317a18a7c9c774075bd7eb1c95140
Author: Eric Biggers <[email protected]>
raid6: add missing vzeroupper to AVX2 code
Since the AVX2 optimized RAID6 code uses YMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
lib/raid/raid6/x86/avx2.c static void raid6_avx21_gen_syndrome(int disks, size_t bytes, void **ptrs) · line 70
Quoted 9 lines
lib/raid/raid6/x86/avx2.c +1
-0@@ -67,6 +67,7 @@ static void raid6_avx21_gen_syndrome(int disks, size_t bytes, void **ptrs) } asmvolatile ("sfence" : : :"memory" ); + asmvolatile ("vzeroupper" );
[Severity: High]
This is a pre-existing issue, but does the AVX-512 RAID6 code also need this fix?
Like the AVX2 code, the AVX-512 code uses ZMM registers but returns without executing vzeroupper before kernel_fpu_end().
For example, in lib/raid/raid6/x86/avx512.c:raid6_avx5121_gen_syndrome():
asm volatile("sfence" : : : "memory");
kernel_fpu_end();
}And similarly in lib/raid/raid6/x86/recov_avx512.c:raid6_2data_recov_avx512().
Would omitting vzeroupper here leave the upper bits of ZMM registers dirty and incur severe AVX-SSE transition penalties if the CPU subsequently executes legacy SSE code, causing a performance regression?
Quoted 2 lines
lib/raid/raid6/x86/avx2.c +0
-0kernel_fpu_end (); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH 4/6] crypto: x86/aria - add missing vzeroupper in AVX2 code
In reply to Eric BiggersThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing vzeroupper in the AVX-512 optimized ARIA code (aria-gfni-avx512-asm_64.S), causing a significant performance regression due to AVX-SSE transition penalties. This problem wasn't introduced by this patch, but the patch fixes the exact same issue in the AVX2 implementation while missing the AVX-512 code. --
--- Patch [4]: [PATCH 4/6] crypto: x86/aria - add missing vzeroupper in AVX2 code --- commit 92a2e79950d33a63291c62ee57a893ce808d68d2
Author: Eric Biggers <[email protected]>
crypto: x86/aria - add missing vzeroupper in AVX2 code
This commit adds missing vzeroupper instructions to the AVX2 ARIA assembly code. This prevents performance degradation for any later SSE code that might be executed by avoiding transition penalties from the YMM registers being left in a dirty state.
Quoted 4 lines
arch/x86/crypto/aria-aesni-avx2-asm_64.S +0
-0
[ ... ]
arch/x86/crypto/aria-aesni-avx2-asm_64.S SYM_TYPED_FUNC_START(aria_aesni_avx2_gfni_ctr_crypt_32way) · line 1439
Quoted 8 lines
arch/x86/crypto/aria-aesni-avx2-asm_64.S +1
-0@@ -1428,6 +1433,7 @@ SYM_TYPED_FUNC_START(aria_aesni_avx2_gfni_ctr_crypt_32way) %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14, %ymm15, %r10); + vzeroupper; FRAME_END RET; SYM_FUNC_END(aria_aesni_avx2_gfni_ctr_crypt_32way)
[Severity: High]
This is a pre-existing issue, but does the AVX-512 ARIA code also need the same fix?
Looking at arch/x86/crypto/aria-gfni-avx512-asm_64.S, the AVX-512 optimized functions like aria_gfni_avx512_encrypt_64way(), aria_gfni_avx512_decrypt_64way(), and aria_gfni_avx512_ctr_crypt_64way() return without executing vzeroupper:
arch/x86/crypto/aria-gfni-avx512-asm_64.S:aria_gfni_avx512_encrypt_64way() {
...
FRAME_END
RET;SYM_FUNC_END(aria_gfni_avx512_encrypt_64way)
}
Since zmm registers are used, wouldn't returning without clearing the EVEX dirty state cause the same performance transition penalties for subsequent legacy SSE code?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH 6/6] netfilter: nft_set_pipapo_avx2: add missing vzeroupper
In reply to Eric BiggersEric, thanks for taking care of this.
The patch looks good to me, I just have two questions:
Quoted 3 lines · On Sat, 15 Aug 2026 13:57:50 -0700 Eric Biggers <[email protected]> wrote:
Since
pipapo_get_avx2()uses YMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
Out of curiosity: was this prompted by some observed latency spike in execution of SSE code, or it's just meant to satisfy the recommendation from AMD and Intel to use it while transitioning from AVX to SSE modes?
Quoted 2 lines
Fixes 7400b063969b ("nft_set_pipapo: Introduce AVX2-based lookup implementation")
Is this really stable material? Skipping vzeroupper might have a performance impact, but it's not an issue for correctness.
The main reason why I'm asking is that, while vzeroupper might look harmless and obviously safe, it actually caused CVE-2023-20593 ("ZenBleed") on AMD Zen 2.
I expect systems receiving stable kernel updates to also run the patched microcode by now, so I'm not overly concerned in any case.
Signature · 1 line
Stefano
Quoted 7 lines · On Sat, 15 Aug 2026 13:57:44 -0700 Eric Biggers <[email protected]> wrote:
Assembly code using YMM or ZMM registers is supposed to end with the vzeroupper instruction in order to avoid degrading the performance of any later SSE code. Since this only affects performance and not correctness, it is sometimes overlooked. Most kernel code does it correctly, but a few cases were missed. This series fixes them.
It should be easiest to take the full series through the x86 tree.
Would it be better to an an unconditional vzeroupper in kernel_fpu_end()? It could go in kernel_fpu_start() but that might have a bigger effect on latency.
(I assume there is one in kernel_fpu_start() if it actually saves the user registers?)
Looking the latency/uops seems reasonably on everything 'recent' except zen-1 and knights-landing.
Although the microcode patch for zen-2 might make that a lot worse.
David
Quoted 21 lines
Eric Biggers (6):
xor: add missing vzeroupper to AVX code
raid6: add missing vzeroupper to AVX2 code
raid6: add missing vzeroupper to AVX-512 code
crypto: x86/aria - add missing vzeroupper in AVX2 code
crypto: x86/aria - add missing vzeroupper in AVX-512 code
netfilter:nft_set_pipapo_avx2: add missing vzeroupperarch/x86/crypto/aria-aesni-avx2-asm_64.S | 6 ++++++ arch/x86/crypto/aria-gfni-avx512-asm_64.S | 3 +++ lib/raid/raid6/x86/avx2.c | 6 ++++++ lib/raid/raid6/x86/avx512.c | 6 ++++++ lib/raid/raid6/x86/recov_avx2.c | 2 ++ lib/raid/raid6/x86/recov_avx512.c | 2 ++ lib/raid/xor/x86/xor-avx.c | 1 + net/netfilter/nft_set_pipapo_avx2.c | 17 ++++++++--------- 8 files changed, 34 insertions(+), 9 deletions(-)base-commit:
db2ddb87143519e20a95aa36c60b36107b736a58
Re: [PATCH 6/6] netfilter: nft_set_pipapo_avx2: add missing vzeroupper
In reply to Stefano BrivioQuoted 7 lines · Stefano Brivio
Eric, thanks for taking care of this.
The patch looks good to me, I just have two questions:
On Sat, 15 Aug 2026 13:57:50 -0700
Eric Biggers <[email protected]> wrote:
Quoted 7 lines
Quoted 3 lines
Since
pipapo_get_avx2()uses YMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.Out of curiosity: was this prompted by some observed latency spike in execution of SSE code, or it's just meant to satisfy the recommendation from AMD and Intel to use it while transitioning from AVX to SSE modes?
This one was found by code review. But the latency spike has been observed in other cases with missing vzeroupper, so it's definitely a real effect at least on some CPUs.
Quoted 6 lines
Quoted 2 lines
Fixes 7400b063969b ("nft_set_pipapo: Introduce AVX2-based lookup implementation")Is this really stable material? Skipping vzeroupper might have a performance impact, but it's not an issue for correctness.
Quoted 6 lines
The main reason why I'm asking is that, while vzeroupper might look harmless and obviously safe, it actually caused CVE-2023-20593 ("ZenBleed") on AMD Zen 2.
I expect systems receiving stable kernel updates to also run the patched microcode by now, so I'm not overly concerned in any case.
It's awkward to mark something as a fix, then not actually want it to be fixed everywhere. The stable maintainers know this, and they often apply fixes anyway regardless of 'Cc stable'.
And if vzeroupper is really not safe due to ZenBleed, then why is it used everywhere else in the kernel? This is just one of the few exceptions that doesn't have it. If it's not safe then they should all be alternatives that patch them out to no-ops on affected CPUs.
But since that was never implemented, and userspace almost always uses vzeroupper too, it seems the only real solution is the microcode. I don't think it makes sense to have a middle ground where almost all of userspace uses vzeroupper, ~97% of the kernel uses vzeroupper, and a random 3% of the kernel doesn't use it.
- Eric
Quoted 17 lines · David Laight
Quoted 7 lines · On Sat, 15 Aug 2026 13:57:44 -0700 Eric Biggers <[email protected]> wrote:
Assembly code using YMM or ZMM registers is supposed to end with the vzeroupper instruction in order to avoid degrading the performance of any later SSE code. Since this only affects performance and not correctness, it is sometimes overlooked. Most kernel code does it correctly, but a few cases were missed. This series fixes them.
It should be easiest to take the full series through the x86 tree.
Would it be better to an an unconditional vzeroupper in
kernel_fpu_end()? It could go inkernel_fpu_start()but that might have a bigger effect on latency.
(I assume there is one inkernel_fpu_start()if it actually saves the user registers?)
Quoted 5 lines
Looking the latency/uops seems reasonably on everything 'recent' except zen-1 and knights-landing.
Although the microcode patch for zen-2 might make that a lot worse.David
I would like to do that, but there are some issues:
- In some cases, within a single kernel-mode FPU section the kernel executes AVX instructions, then SSE instructions afterwards. It typically occurs when large input lengths are optimized specially with AVX and then shorter lengths fall back to SSE code. chacha_dosimd() in lib/crypto/x86/chacha.h is an example of this.
In these cases the internal vzeroupper is definitely needed.
- Unnecessary overhead if only SSE instructions are used, which is still frequent since we don't provide both SSE and AVX versions of the same code when the AVX doesn't provide a notable performance benefit. lib/crypto/x86/sha256-ni-asm.S is an example of this.
- Further divergence from the userspace ABI, which can be annoying when importing assembly code to or from userspace projects, or developing or testing the assembly files in userspace.
As for kernel_fpu_begin(), no, it doesn't do vzeroupper.
I do think that some years down the line, we'll drop the use of SSE in the kernel entirely. At that point, vzeroupper in kernel_fpu_end() would make sense.
But until then, I think we should stick with the existing, standard convention of having the vzeroupper at the end of the assembly routines.
- Eric
Quoted 5 lines · Eric Biggers
As for
kernel_fpu_begin(), no, it doesn't do vzeroupper.I do think that some years down the line, we'll drop the use of SSE in the kernel entirely. At that point, vzeroupper in
kernel_fpu_end()would make sense.
Or add kernel_avx_{begin,end} wrappers that include the vzeroupper in kernel_avx_end. That would be a lot easier to use than the manual vzeroupper in every modern user of in-kernel AVX.
Quoted 11 lines · On Mon, 17 Aug 2026 02:15:19 -0700 Christoph Hellwig <[email protected]> wrote:
Quoted 5 lines · Eric Biggers
As for
kernel_fpu_begin(), no, it doesn't do vzeroupper.I do think that some years down the line, we'll drop the use of SSE in the kernel entirely. At that point, vzeroupper in
kernel_fpu_end()would make sense.Or add kernel_avx_{begin,end} wrappers that include the vzeroupper in
kernel_avx_end. That would be a lot easier to use than the manual vzeroupper in every modern user of in-kernel AVX.
You might want one in the start as well.
I have a theory that the avx512 logic was added as a completely separate block. This meant it could be included in cpu for testing but disabled in any released to customers.
(Or maybe the it is the original avx logic that used latches not in the normal register file.)
A side effect is that different latches are used for the low bits of the registers - so when you change to/from avx512 the register contents have to be transferred between the blocks - adding latency.
So if the wrong registers are live for the code you are going to execute the data has to be transferred across.
There are also other effects as well.
I found this link: https://travisdowns.github.io/blog/2020/01/17/avxfreq1.html
It is a few years old now (2020) but probably still relevant.
A quick summary is that the first 256 or 512 bit instruction starts a 9us window where the cpu runs at 1/4 speed, for 512 bit that is followed by 11us where nothing happens at all.
David
Re: [PATCH 6/6] netfilter: nft_set_pipapo_avx2: add missing vzeroupper
In reply to Eric BiggersQuoted 12 lines · On Sun, 16 Aug 2026 10:15:18 -0700 Eric Biggers <[email protected]> wrote:
Quoted 11 lines · Stefano Brivio
Eric, thanks for taking care of this.
The patch looks good to me, I just have two questions:
Quoted 3 lines · On Sat, 15 Aug 2026 13:57:50 -0700 Eric Biggers <[email protected]> wrote:
Since
pipapo_get_avx2()uses YMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.
Quoted 7 lines
Quoted 3 lines
Out of curiosity: was this prompted by some observed latency spike in execution of SSE code, or it's just meant to satisfy the recommendation from AMD and Intel to use it while transitioning from AVX to SSE modes?
This one was found by code review. But the latency spike has been observed in other cases with missing vzeroupper, so it's definitely a real effect at least on some CPUs.
Ah, interesting, I've been wondering about that.
Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
Fixes 7400b063969b ("nft_set_pipapo: Introduce AVX2-based lookup implementation")Is this really stable material? Skipping vzeroupper might have a performance impact, but it's not an issue for correctness.
The main reason why I'm asking is that, while vzeroupper might look harmless and obviously safe, it actually caused CVE-2023-20593 ("ZenBleed") on AMD Zen 2.
Quoted 6 lines
Quoted 2 lines
I expect systems receiving stable kernel updates to also run the patched microcode by now, so I'm not overly concerned in any case.
It's awkward to mark something as a fix, then not actually want it to be fixed everywhere. The stable maintainers know this, and they often apply fixes anyway regardless of 'Cc stable'.
Right, I guess you have point there.
Quoted 10 lines
And if vzeroupper is really not safe due to ZenBleed, then why is it used everywhere else in the kernel? This is just one of the few exceptions that doesn't have it. If it's not safe then they should all be alternatives that patch them out to no-ops on affected CPUs.
But since that was never implemented, and userspace almost always uses vzeroupper too, it seems the only real solution is the microcode. I don't think it makes sense to have a middle ground where almost all of userspace uses vzeroupper, ~97% of the kernel uses vzeroupper, and a random 3% of the kernel doesn't use it.
Don't get me wrong, I wasn't advocating against "fixing" this for specific paths, rather just pointing out that something seemingly harmless such as vzeroupper can have nasty side effects.
I used ZenBleed as a mere example, not as a current reason to do or not do anything specific.
Signature · 1 line
Stefano
Re: [PATCH 6/6] netfilter: nft_set_pipapo_avx2: add missing vzeroupper
In reply to Eric BiggersQuoted 9 lines · On Sat, 15 Aug 2026 13:57:50 -0700 Eric Biggers <[email protected]> wrote:
Since
pipapo_get_avx2()uses YMM registers, execute vzeroupper before returning from it. This is needed to avoid degrading the performance of any later SSE code that may happen to be executed.Fixes 7400b063969b ("nft_set_pipapo: Introduce AVX2-based lookup implementation")Cc Florian Westphal <[email protected]>Cc Stefano Brivio <[email protected]>Signed-off-by Eric Biggers <[email protected]>
Signature · 1 line
Stefano
Quoted 27 lines · David Laight
Quoted 11 lines · On Mon, 17 Aug 2026 02:15:19 -0700 Christoph Hellwig <[email protected]> wrote:
Quoted 5 lines · Eric Biggers
As for
kernel_fpu_begin(), no, it doesn't do vzeroupper.I do think that some years down the line, we'll drop the use of SSE in the kernel entirely. At that point, vzeroupper in
kernel_fpu_end()would make sense.Or add kernel_avx_{begin,end} wrappers that include the vzeroupper in
kernel_avx_end. That would be a lot easier to use than the manual vzeroupper in every modern user of in-kernel AVX.You might want one in the start as well.
I have a theory that the avx512 logic was added as a completely separate block. This meant it could be included in cpu for testing but disabled in any released to customers.
(Or maybe the it is the original avx logic that used latches not in the normal register file.)
A side effect is that different latches are used for the low bits of the registers - so when you change to/from avx512 the register contents have to be transferred between the blocks - adding latency.
So if the wrong registers are live for the code you are going to execute the data has to be transferred across.
Quoted 6 lines
There are also other effects as well.
I found this link: https://travisdowns.github.io/blog/2020/01/17/avxfreq1.html
It is a few years old now (2020) but probably still relevant.
A quick summary is that the first 256 or 512 bit instruction starts a 9us window where the cpu runs at 1/4 speed, for 512 bit that is followed by 11us where nothing happens at all.
The linked article is about Skylake, which is an older Intel CPU that has a bad AVX-512 implementation with overly-eager downclocking. Later Intel CPUs improved the implementation. And of course, AMD just implemented it properly from the start without the downclocking issues. Information about AMD Zen 5's AVX-512 implementation can be found here:
https://www.numberworld.org/blogs/2024_8_7_zen5_avx512_teardown/
Most of the AVX-512 optimized code in the kernel already requires !X86_FEATURE_PREFER_YMM, excluding Skylake as well as Ice Lake.
That being said, if I recall correctly, even with Intel's improved implementation on Sapphire Rapids and Emerald Rapids, Intel does still have some start-up latency for accessing ZMM registers. AMD doesn't. I don't believe vzeroupper helps, unfortunately.
I've considered setting X86_FEATURE_PREFER_YMM on all Intel CPUs, but then even workloads that would benefit from ZMM registers wouldn't use them. And I suspect the Intel folks wouldn't agree with that either.
- Eric