raid6: x86: asm constraints, sfence and prefetch in xor_syndrome
cover raid6: x86: asm constraints, sfence and prefetch in xor_syndrome
This follows up on the review of "raid6: use normal stores in the x86 x4 xor_syndrome functions" [1]. With that patch, none of the x86 xor_syndrome() functions uses non-temporal stores, so their sfence orders nothing. Christoph suggested removing it [2]. Eric pointed out that the AVX-512 functions list P and Q as asm inputs only and rely on the "memory" clobber of that sfence, and suggested prefetcht0 for Q [3]. 1/3 fixes the constraints, 2/3 removes the sfence, and 3/3 changes the prefetch of Q.
Based on mm-nonmm-unstable with [1] applied.
Testing:
- Builds without warnings with W=1, gcc 15.2 and clang 21.1.
- With 1/3, clang generates the same code as before. gcc reloads the data pointers inside the loop of the AVX-512 functions (up to two more instructions per function). With 2/3, the sfence instructions are gone, and apart from alignment padding the code is the same. 3/3 only changes the six prefetch instructions.
- The raid6 KUnit test passes in QEMU with -cpu max under KVM on a Haswell host (avx2x1..x4) and with -cpu Westmere (sse2x1..x4). All 8 test cases pass in each run.
- In userspace, gen_syndrome() and xor_syndrome() of all nine sse2, avx2 and avx512 variants match a scalar reference (gen_syndrome for 2-24 data disks, xor_syndrome for every start/stop): sse2 natively on a Core i5-2520M, avx2 natively on a Xeon E5-2699 v3, all variants natively on a Xeon Gold 6130, and avx512 also with Intel SDE (-skx). The AVX-512 functions were not run in a kernel. I have no AVX-512 machine with root access.
The patches and the tests were prepared with the help of an LLM. I reviewed everything.
[1] https://lore.kernel.org/linux-raid/[email protected]/
[2] https://lore.kernel.org/linux-raid/[email protected]/
[3] https://lore.kernel.org/linux-raid/[email protected]/
Miroslav Cimerman (3):
raid6: declare the AVX-512 P/Q stores as asm outputs
raid6: remove the sfence from the x86 xor_syndrome functions
raid6: prefetch Q with prefetcht0 in the x86 x4 xor_syndrome functions
lib/raid/raid6/x86/avx2.c | 7 ++----- lib/raid/raid6/x86/avx512.c | 37 ++++++++++++++----------------------- lib/raid/raid6/x86/sse2.c | 7 ++----- 3 files changed, 18 insertions(+), 33 deletions(-)
base-commit: 609cd6a5e6138620c16d8818bb585e8766064c23
prerequisite-patch-id: 0242f328222b644b2b329d90c8d50aad322b7655
prerequisite-message-id: <[email protected]>
Signature · 1 line
2.53.0
PATCH 1/3 raid6: declare the AVX-512 P/Q stores as asm outputs
The AVX-512 gen_syndrome() and xor_syndrome() functions store P and Q in inline asm that lists these memory operands as inputs only. The compiler may then assume that the asm does not change P and Q. Each function ends with an sfence whose asm has a "memory" clobber, and the constraints technically rely on it.
Declare the stored P and Q as outputs: "=m" where the asm only writes them, and "+m" for Q in xor_syndrome(), which the same asm reads and writes back. The SSE2 and AVX2 functions already declare their stores as outputs.
With clang the generated code does not change: it already loads the data pointers from ptrs[] inside the loop over the block. gcc 15.2 now does the same, because a byte store may alias the pointer array. Five of the six functions get one or two more instructions.
--- lib/raid/raid6/x86/avx512.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-)
lib/raid/raid6/x86/avx512.c +12 -18
@@ -73,8 +73,7 @@ static void raid6_avx5121_gen_syndrome(int disks, size_t bytes, void **ptrs) "vpxorq %%zmm2,%%zmm2,%%zmm2\n\t" "vmovntdq %%zmm4,%1\n\t" "vpxorq %%zmm4,%%zmm4,%%zmm4" - : - : "m" (p[d]), "m" (q[d])); + : "=m" (p[d]), "=m" (q[d])); } asmvolatile ("sfence" : : :"memory" );
@@ -133,8 +132,7 @@ static void raid6_avx5121_xor_syndrome(int disks, int start, int stop,/* Don't use movntdq for r/w memory area < cache line */ "vmovdqa64 %%zmm4,%0\n\t" "vmovdqa64 %%zmm2,%1" - : - : "m" (q[d]), "m" (p[d])); + : "+m" (q[d]), "=m" (p[d])); } asmvolatile ("sfence" : : :"memory" );
@@ -204,9 +202,8 @@ static void raid6_avx5122_gen_syndrome(int disks, size_t bytes, void **ptrs) "vmovntdq %%zmm3,%1\n\t" "vmovntdq %%zmm4,%2\n\t" "vmovntdq %%zmm6,%3" - : - : "m" (p[d]), "m" (p[d+64]), "m" (q[d]), - "m" (q[d+64])); + : "=m" (p[d]), "=m" (p[d+64]), "=m" (q[d]), + "=m" (q[d+64])); } asmvolatile ("sfence" : : :"memory" );
@@ -289,9 +286,8 @@ static void raid6_avx5122_xor_syndrome(int disks, int start, int stop,"vmovdqa64 %%zmm6,%1\n\t" "vmovdqa64 %%zmm2,%2\n\t" "vmovdqa64 %%zmm3,%3" - : - : "m" (q[d]), "m" (q[d+64]), "m" (p[d]), - "m" (p[d+64])); + : "+m" (q[d]), "+m" (q[d+64]), "=m" (p[d]), + "=m" (p[d+64])); } asmvolatile ("sfence" : : :"memory" );
@@ -393,10 +389,9 @@ static void raid6_avx5124_gen_syndrome(int disks, size_t bytes, void **ptrs) "vpxorq %%zmm12,%%zmm12,%%zmm12\n\t" "vmovntdq %%zmm14,%7\n\t" "vpxorq %%zmm14,%%zmm14,%%zmm14" - : - : "m" (p[d]), "m" (p[d+64]), "m" (p[d+128]), -"m" (p[d+192 ]),"m" (q[d]),"m" (q[d+64 ]), - "m" (q[d+128]), "m" (q[d+192])); + : "=m" (p[d]), "=m" (p[d+64]), "=m" (p[d+128]), +"=m" (p[d+192 ]),"=m" (q[d]),"=m" (q[d+64 ]), + "=m" (q[d+128]), "=m" (q[d+192])); } asmvolatile ("sfence" : : :"memory" );
@@ -528,10 +523,9 @@ static void raid6_avx5124_xor_syndrome(int disks, int start, int stop, "vmovdqa64 %%zmm6,%5\n\t" "vmovdqa64 %%zmm12,%6\n\t" "vmovdqa64 %%zmm14,%7" - : - : "m" (p[d]), "m" (p[d+64]), "m" (p[d+128]), -"m" (p[d+192 ]),"m" (q[d]),"m" (q[d+64 ]), - "m" (q[d+128]), "m" (q[d+192])); + : "=m" (p[d]), "=m" (p[d+64]), "=m" (p[d+128]), +"=m" (p[d+192 ]),"+m" (q[d]),"+m" (q[d+64 ]), + "+m" (q[d+128]), "+m" (q[d+192])); } asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );
Signature · 1 line
2.53.0
PATCH 2/3 raid6: remove the sfence from the x86 xor_syndrome functions
In reply to Miroslav CimermanThe SSE2, AVX2 and AVX-512 xor_syndrome() functions end with an sfence. It orders non-temporal stores, but none of these functions uses them anymore. The x4 versions stopped with "raid6: use normal stores in the x86 x4 xor_syndrome functions", and the x1 and x2 versions never used them.
Remove the sfence from all nine functions. The gen_syndrome() functions still write P and Q with non-temporal stores and keep their sfence.
The asm of the AVX-512 functions relied on the "memory" clobber of the sfence. "raid6: declare the AVX-512 P/Q stores as asm outputs" declares their stores as outputs instead.
The memory operands are still declared as single bytes while the asm accesses 16 to 64 bytes. This is unchanged. No C code in these functions accesses the buffers.
--- lib/raid/raid6/x86/avx2.c | 3 --- lib/raid/raid6/x86/avx512.c | 3 --- lib/raid/raid6/x86/sse2.c | 3 --- 3 files changed, 9 deletions(-)
lib/raid/raid6/x86/avx2.c +0 -3
@@ -115,7 +115,6 @@ static void raid6_avx21_xor_syndrome(int disks, int start, int stop, asmvolatile ("vmovdqa %%ymm2,%0" :"=m" (p[d])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -245,7 +244,6 @@ static void raid6_avx22_xor_syndrome(int disks, int start, int stop, asmvolatile ("vmovdqa %%ymm3,%0" :"=m" (p[d+32 ])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -448,7 +446,6 @@ static void raid6_avx24_xor_syndrome(int disks, int start, int stop, asmvolatile ("vmovdqa %%ymm12,%0" :"=m" (q[d+64 ])); asmvolatile ("vmovdqa %%ymm14,%0" :"=m" (q[d+96 ])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
lib/raid/raid6/x86/avx512.c +0 -3
@@ -135,7 +135,6 @@ static void raid6_avx5121_xor_syndrome(int disks, int start, int stop, : "+m" (q[d]), "=m" (p[d])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -290,7 +289,6 @@ static void raid6_avx5122_xor_syndrome(int disks, int start, int stop, "=m" (p[d+64])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -527,7 +525,6 @@ static void raid6_avx5124_xor_syndrome(int disks, int start, int stop,"=m" (p[d+192 ]),"+m" (q[d]),"+m" (q[d+64 ]), "+m" (q[d+128]), "+m" (q[d+192])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
lib/raid/raid6/x86/sse2.c +0 -3
@@ -113,7 +113,6 @@ static void raid6_sse21_xor_syndrome(int disks, int start, int stop, asmvolatile ("movdqa %%xmm2,%0" :"=m" (p[d])); } - asmvolatile ("sfence" : : :"memory" );kernel_fpu_end (); }
@@ -241,7 +240,6 @@ static void raid6_sse22_xor_syndrome(int disks, int start, int stop, asmvolatile ("movdqa %%xmm3,%0" :"=m" (p[d+16 ])); } - asmvolatile ("sfence" : : :"memory" );kernel_fpu_end (); }
@@ -445,7 +443,6 @@ static void raid6_sse24_xor_syndrome(int disks, int start, int stop, asmvolatile ("movdqa %%xmm12,%0" :"=m" (q[d+32 ])); asmvolatile ("movdqa %%xmm14,%0" :"=m" (q[d+48 ])); } - asmvolatile ("sfence" : : :"memory" );kernel_fpu_end (); }
Signature · 1 line
2.53.0
PATCH 3/3 raid6: prefetch Q with prefetcht0 in the x86 x4 xor_syndrome functions
In reply to Miroslav CimermanThe x4 xor_syndrome() functions for SSE2, AVX2 and AVX-512 prefetch Q with prefetchnta. That hint is meant for data that is used once. Q is read, updated and written back with normal stores, and md calls xor_syndrome() twice on the same P/Q page for each write.
Prefetch Q with prefetcht0, which keeps it in all cache levels. The data blocks are read once and keep prefetchnta.
Time per write measured in a userspace harness in md's read-modify-write pattern (two xor_syndrome() calls on the same P/Q page), avx512x4 on a Xeon Gold 6130, 2-24 data disks, write to the first, middle and last data disk, median of 10 passes:
P and Q in DRAM: -2.2% (geometric mean), up to -5.7% P and Q in L3: +0.2% P and Q in L1/L2: +0.3%
The gain comes with 2-8 data disks, where Q is a larger part of the work. The SSE2 and AVX2 versions were not measured.
--- lib/raid/raid6/x86/avx2.c | 4 ++-- lib/raid/raid6/x86/avx512.c | 4 ++-- lib/raid/raid6/x86/sse2.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
lib/raid/raid6/x86/avx2.c +2 -2
@@ -408,8 +408,8 @@ static void raid6_avx24_xor_syndrome(int disks, int start, int stop, asmvolatile ("vpxor %ymm13,%ymm12,%ymm12" ); asmvolatile ("vpxor %ymm15,%ymm14,%ymm14" ); } - asmvolatile ("prefetchnta %0" ::"m" (q[d])); - asmvolatile ("prefetchnta %0" ::"m" (q[d+64 ])); + asmvolatile ("prefetcht0 %0" ::"m" (q[d])); + asmvolatile ("prefetcht0 %0" ::"m" (q[d+64 ]));/* P/Q left side optimization */ for (z = start-1 ; z >=0 ; z--) { asmvolatile ("vpxor %ymm5,%ymm5,%ymm5" );
lib/raid/raid6/x86/avx512.c +2 -2
@@ -476,8 +476,8 @@ static void raid6_avx5124_xor_syndrome(int disks, int start, int stop,"m" (dptr[z][d+128 ]), "m" (dptr[z][d+192])); } - asmvolatile ("prefetchnta %0\n\t" -"prefetchnta %1\n\t" + asmvolatile ("prefetcht0 %0\n\t" +"prefetcht0 %1\n\t" : :"m" (q[d]),"m" (q[d+128 ]));/* P/Q left side optimization */
lib/raid/raid6/x86/sse2.c +2 -2
@@ -405,8 +405,8 @@ static void raid6_sse24_xor_syndrome(int disks, int start, int stop, asmvolatile ("pxor %xmm13,%xmm12" ); asmvolatile ("pxor %xmm15,%xmm14" ); } - asmvolatile ("prefetchnta %0" ::"m" (q[d])); - asmvolatile ("prefetchnta %0" ::"m" (q[d+32 ])); + asmvolatile ("prefetcht0 %0" ::"m" (q[d])); + asmvolatile ("prefetcht0 %0" ::"m" (q[d+32 ]));/* P/Q left side optimization */ for ( z = start-1 ; z >=0 ; z-- ) { asmvolatile ("pxor %xmm5,%xmm5" );
Signature · 1 line
2.53.0
Quoted 41 lines · Miroslav Cimerman
This follows up on the review of "raid6: use normal stores in the x86 x4
xor_syndromefunctions" [1]. With that patch, none of the x86xor_syndrome()functions uses non-temporal stores, so their sfence orders nothing. Christoph suggested removing it [2]. Eric pointed out that the AVX-512 functions list P and Q as asm inputs only and rely on the "memory" clobber of that sfence, and suggested prefetcht0 for Q [3]. 1/3 fixes the constraints, 2/3 removes the sfence, and 3/3 changes the prefetch of Q.Based on mm-nonmm-unstable with [1] applied.
Testing:
- Builds without warnings with W=1, gcc 15.2 and clang 21.1.
- With 1/3, clang generates the same code as before. gcc reloads the data pointers inside the loop of the AVX-512 functions (up to two more instructions per function). With 2/3, the sfence instructions are gone, and apart from alignment padding the code is the same. 3/3 only changes the six prefetch instructions.
- The raid6 KUnit test passes in QEMU with -cpu max under KVM on a Haswell host (avx2x1..x4) and with -cpu Westmere (sse2x1..x4). All 8 test cases pass in each run.
- In userspace,gen_syndrome()andxor_syndrome()of all nine sse2, avx2 and avx512 variants match a scalar reference (gen_syndromefor 2-24 data disks,xor_syndromefor every start/stop): sse2 natively on a Core i5-2520M, avx2 natively on a Xeon E5-2699 v3, all variants natively on a Xeon Gold 6130, and avx512 also with Intel SDE (-skx). The AVX-512 functions were not run in a kernel. I have no AVX-512 machine with root access.The patches and the tests were prepared with the help of an LLM. I reviewed everything.
[1] https://lore.kernel.org/linux-raid/[email protected]/
[2] https://lore.kernel.org/linux-raid/[email protected]/
[3] https://lore.kernel.org/linux-raid/[email protected]/Miroslav Cimerman (3):
raid6: declare the AVX-512 P/Q stores as asm outputs
raid6: remove the sfence from the x86xor_syndromefunctions
raid6: prefetch Q with prefetcht0 in the x86 x4xor_syndromefunctions
Quoted 4 lines
lib/raid/raid6/x86/avx2.c | 7 ++----- lib/raid/raid6/x86/avx512.c | 37 ++++++++++++++----------------------- lib/raid/raid6/x86/sse2.c | 7 ++----- 3 files changed, 18 insertions(+), 33 deletions(-)
- Eric
Andrew Morton
tree notice+ raid6-prefetch-q-with-prefetcht0-in-the-x86-x4-xor_syndrome-functions.patch added to mm-nonmm-unstable branch
In reply to Miroslav CimermanThe patch titled Subject: raid6: prefetch Q with prefetcht0 in the x86 x4 xor_syndrome functions has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: raid6: prefetch Q with prefetcht0 in the x86 x4 xor_syndrome functions
has been added to the -mm mm-nonmm-unstable branch. Its filename is
raid6-prefetch-q-with-prefetcht0-in-the-x86-x4-xor_syndrome-functions.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/raid6-prefetch-q-with-prefetcht0-in-the-x86-x4-xor_syndrome-functions.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Miroslav Cimerman <[email protected]>
Subject: raid6: prefetch Q with prefetcht0 in the x86 x4 xor_syndrome functions
Date: Sun, 22 Nov 2026 03:29:33 +0000
The x4 xor_syndrome() functions for SSE2, AVX2 and AVX-512 prefetch Q with prefetchnta. That hint is meant for data that is used once. Q is read, updated and written back with normal stores, and md calls xor_syndrome() twice on the same P/Q page for each write.
Prefetch Q with prefetcht0, which keeps it in all cache levels. The data blocks are read once and keep prefetchnta.
Time per write measured in a userspace harness in md's read-modify-write pattern (two xor_syndrome() calls on the same P/Q page), avx512x4 on a Xeon Gold 6130, 2-24 data disks, write to the first, middle and last data disk, median of 10 passes:
P and Q in DRAM: -2.2% (geometric mean), up to -5.7% P and Q in L3: +0.2% P and Q in L1/L2: +0.3%
The gain comes with 2-8 data disks, where Q is a larger part of the work. The SSE2 and AVX2 versions were not measured.
lib/raid/raid6/x86/avx2.c | 4 ++-- lib/raid/raid6/x86/avx512.c | 4 ++-- lib/raid/raid6/x86/sse2.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
lib/raid/raid6/x86/avx2.c renamed from lib/raid/raid6/x86/avx2.c~raid6-prefetch-q-with-prefetcht0-in-the-x86-x4-xor_syndrome-functions +2 -2
@@ -408,8 +408,8 @@ static void raid6_avx24_xor_syndrome(int asmvolatile ("vpxor %ymm13,%ymm12,%ymm12" ); asmvolatile ("vpxor %ymm15,%ymm14,%ymm14" ); } - asmvolatile ("prefetchnta %0" ::"m" (q[d])); - asmvolatile ("prefetchnta %0" ::"m" (q[d+64 ])); + asmvolatile ("prefetcht0 %0" ::"m" (q[d])); + asmvolatile ("prefetcht0 %0" ::"m" (q[d+64 ]));/* P/Q left side optimization */ for (z = start-1 ; z >=0 ; z--) { asmvolatile ("vpxor %ymm5,%ymm5,%ymm5" );
lib/raid/raid6/x86/avx512.c renamed from lib/raid/raid6/x86/avx512.c~raid6-prefetch-q-with-prefetcht0-in-the-x86-x4-xor_syndrome-functions +2 -2
@@ -476,8 +476,8 @@ static void raid6_avx5124_xor_syndrome(i"m" (dptr[z][d+128 ]), "m" (dptr[z][d+192])); } - asmvolatile ("prefetchnta %0\n\t" -"prefetchnta %1\n\t" + asmvolatile ("prefetcht0 %0\n\t" +"prefetcht0 %1\n\t" : :"m" (q[d]),"m" (q[d+128 ]));/* P/Q left side optimization */
lib/raid/raid6/x86/sse2.c renamed from lib/raid/raid6/x86/sse2.c~raid6-prefetch-q-with-prefetcht0-in-the-x86-x4-xor_syndrome-functions +2 -2
@@ -405,8 +405,8 @@ static void raid6_sse24_xor_syndrome(int asmvolatile ("pxor %xmm13,%xmm12" ); asmvolatile ("pxor %xmm15,%xmm14" ); } - asmvolatile ("prefetchnta %0" ::"m" (q[d])); - asmvolatile ("prefetchnta %0" ::"m" (q[d+32 ])); + asmvolatile ("prefetcht0 %0" ::"m" (q[d])); + asmvolatile ("prefetcht0 %0" ::"m" (q[d+32 ]));/* P/Q left side optimization */ for ( z = start-1 ; z >=0 ; z-- ) { asmvolatile ("pxor %xmm5,%xmm5" );
_
Patches currently in -mm which might be from [email protected] are
raid6-use-normal-stores-in-the-x86-x4-xor_syndrome-functions.patch raid6-declare-the-avx-512-p-q-stores-as-asm-outputs.patch raid6-remove-the-sfence-from-the-x86-xor_syndrome-functions.patch raid6-prefetch-q-with-prefetcht0-in-the-x86-x4-xor_syndrome-functions.patch
Andrew Morton
tree notice+ raid6-declare-the-avx-512-p-q-stores-as-asm-outputs.patch added to mm-nonmm-unstable branch
In reply to Miroslav CimermanThe patch titled Subject: raid6: declare the AVX-512 P/Q stores as asm outputs has been added to the -mm mm-nonmm-unstable branch. Its filename is
The patch titled
Subject: raid6: declare the AVX-512 P/Q stores as asm outputs
has been added to the -mm mm-nonmm-unstable branch. Its filename is
raid6-declare-the-avx-512-p-q-stores-as-asm-outputs.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/raid6-declare-the-avx-512-p-q-stores-as-asm-outputs.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Miroslav Cimerman <[email protected]>
Subject: raid6: declare the AVX-512 P/Q stores as asm outputs
Date: Sun, 22 Nov 2026 03:29:31 +0000
Patch series "raid6: x86: asm constraints, sfence and prefetch in xor_syndrome".
This follows up on the review of "raid6: use normal stores in the x86 x4 xor_syndrome functions" [1]. With that patch, none of the x86 xor_syndrome() functions uses non-temporal stores, so their sfence orders nothing. Christoph suggested removing it [2]. Eric pointed out that the AVX-512 functions list P and Q as asm inputs only and rely on the "memory" clobber of that sfence, and suggested prefetcht0 for Q [3]. 1/3 fixes the constraints, 2/3 removes the sfence, and 3/3 changes the prefetch of Q.
The patches and the tests were prepared with the help of an LLM. I reviewed everything.
This patch (of 3):
The AVX-512 gen_syndrome() and xor_syndrome() functions store P and Q in inline asm that lists these memory operands as inputs only. The compiler may then assume that the asm does not change P and Q. Each function ends with an sfence whose asm has a "memory" clobber, and the constraints technically rely on it.
Declare the stored P and Q as outputs: "=m" where the asm only writes them, and "+m" for Q in xor_syndrome(), which the same asm reads and writes back. The SSE2 and AVX2 functions already declare their stores as outputs.
With clang the generated code does not change: it already loads the data pointers from ptrs[] inside the loop over the block. gcc 15.2 now does the same, because a byte store may alias the pointer array. Five of the six functions get one or two more instructions.
lib/raid/raid6/x86/avx512.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-)
lib/raid/raid6/x86/avx512.c renamed from lib/raid/raid6/x86/avx512.c~raid6-declare-the-avx-512-p-q-stores-as-asm-outputs +12 -18
@@ -73,8 +73,7 @@ static void raid6_avx5121_gen_syndrome(i "vpxorq %%zmm2,%%zmm2,%%zmm2\n\t" "vmovntdq %%zmm4,%1\n\t" "vpxorq %%zmm4,%%zmm4,%%zmm4" - : - : "m" (p[d]), "m" (q[d])); + : "=m" (p[d]), "=m" (q[d])); } asmvolatile ("sfence" : : :"memory" );
@@ -133,8 +132,7 @@ static void raid6_avx5121_xor_syndrome(i/* Don't use movntdq for r/w memory area < cache line */ "vmovdqa64 %%zmm4,%0\n\t" "vmovdqa64 %%zmm2,%1" - : - : "m" (q[d]), "m" (p[d])); + : "+m" (q[d]), "=m" (p[d])); } asmvolatile ("sfence" : : :"memory" );
@@ -204,9 +202,8 @@ static void raid6_avx5122_gen_syndrome(i "vmovntdq %%zmm3,%1\n\t" "vmovntdq %%zmm4,%2\n\t" "vmovntdq %%zmm6,%3" - : - : "m" (p[d]), "m" (p[d+64]), "m" (q[d]), - "m" (q[d+64])); + : "=m" (p[d]), "=m" (p[d+64]), "=m" (q[d]), + "=m" (q[d+64])); } asmvolatile ("sfence" : : :"memory" );
@@ -289,9 +286,8 @@ static void raid6_avx5122_xor_syndrome(i"vmovdqa64 %%zmm6,%1\n\t" "vmovdqa64 %%zmm2,%2\n\t" "vmovdqa64 %%zmm3,%3" - : - : "m" (q[d]), "m" (q[d+64]), "m" (p[d]), - "m" (p[d+64])); + : "+m" (q[d]), "+m" (q[d+64]), "=m" (p[d]), + "=m" (p[d+64])); } asmvolatile ("sfence" : : :"memory" );
@@ -393,10 +389,9 @@ static void raid6_avx5124_gen_syndrome(i "vpxorq %%zmm12,%%zmm12,%%zmm12\n\t" "vmovntdq %%zmm14,%7\n\t" "vpxorq %%zmm14,%%zmm14,%%zmm14" - : - : "m" (p[d]), "m" (p[d+64]), "m" (p[d+128]), -"m" (p[d+192 ]),"m" (q[d]),"m" (q[d+64 ]), - "m" (q[d+128]), "m" (q[d+192])); + : "=m" (p[d]), "=m" (p[d+64]), "=m" (p[d+128]), +"=m" (p[d+192 ]),"=m" (q[d]),"=m" (q[d+64 ]), + "=m" (q[d+128]), "=m" (q[d+192])); } asmvolatile ("sfence" : : :"memory" );
@@ -528,10 +523,9 @@ static void raid6_avx5124_xor_syndrome(i "vmovdqa64 %%zmm6,%5\n\t" "vmovdqa64 %%zmm12,%6\n\t" "vmovdqa64 %%zmm14,%7" - : - : "m" (p[d]), "m" (p[d+64]), "m" (p[d+128]), -"m" (p[d+192 ]),"m" (q[d]),"m" (q[d+64 ]), - "m" (q[d+128]), "m" (q[d+192])); + : "=m" (p[d]), "=m" (p[d+64]), "=m" (p[d+128]), +"=m" (p[d+192 ]),"+m" (q[d]),"+m" (q[d+64 ]), + "+m" (q[d+128]), "+m" (q[d+192])); } asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );
_
Patches currently in -mm which might be from [email protected] are
raid6-use-normal-stores-in-the-x86-x4-xor_syndrome-functions.patch raid6-declare-the-avx-512-p-q-stores-as-asm-outputs.patch raid6-remove-the-sfence-from-the-x86-xor_syndrome-functions.patch raid6-prefetch-q-with-prefetcht0-in-the-x86-x4-xor_syndrome-functions.patch
Andrew Morton
tree notice+ raid6-remove-the-sfence-from-the-x86-xor_syndrome-functions.patch added to mm-nonmm-unstable branch
In reply to Miroslav CimermanThe patch titled Subject: raid6: remove the sfence from the x86 xor_syndrome functions has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: raid6: remove the sfence from the x86 xor_syndrome functions
has been added to the -mm mm-nonmm-unstable branch. Its filename is
raid6-remove-the-sfence-from-the-x86-xor_syndrome-functions.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/raid6-remove-the-sfence-from-the-x86-xor_syndrome-functions.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Miroslav Cimerman <[email protected]>
Subject: raid6: remove the sfence from the x86 xor_syndrome functions
Date: Sun, 22 Nov 2026 03:29:32 +0000
The SSE2, AVX2 and AVX-512 xor_syndrome() functions end with an sfence. It orders non-temporal stores, but none of these functions uses them anymore. The x4 versions stopped with "raid6: use normal stores in the x86 x4 xor_syndrome functions", and the x1 and x2 versions never used them.
Remove the sfence from all nine functions. The gen_syndrome() functions still write P and Q with non-temporal stores and keep their sfence.
The asm of the AVX-512 functions relied on the "memory" clobber of the sfence. "raid6: declare the AVX-512 P/Q stores as asm outputs" declares their stores as outputs instead.
The memory operands are still declared as single bytes while the asm accesses 16 to 64 bytes. This is unchanged. No C code in these functions accesses the buffers.
lib/raid/raid6/x86/avx2.c | 3 --- lib/raid/raid6/x86/avx512.c | 3 --- lib/raid/raid6/x86/sse2.c | 3 --- 3 files changed, 9 deletions(-)
lib/raid/raid6/x86/avx2.c renamed from lib/raid/raid6/x86/avx2.c~raid6-remove-the-sfence-from-the-x86-xor_syndrome-functions +0 -3
@@ -115,7 +115,6 @@ static void raid6_avx21_xor_syndrome(int asmvolatile ("vmovdqa %%ymm2,%0" :"=m" (p[d])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -245,7 +244,6 @@ static void raid6_avx22_xor_syndrome(int asmvolatile ("vmovdqa %%ymm3,%0" :"=m" (p[d+32 ])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -448,7 +446,6 @@ static void raid6_avx24_xor_syndrome(int asmvolatile ("vmovdqa %%ymm12,%0" :"=m" (q[d+64 ])); asmvolatile ("vmovdqa %%ymm14,%0" :"=m" (q[d+96 ])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
lib/raid/raid6/x86/avx512.c renamed from lib/raid/raid6/x86/avx512.c~raid6-remove-the-sfence-from-the-x86-xor_syndrome-functions +0 -3
@@ -135,7 +135,6 @@ static void raid6_avx5121_xor_syndrome(i : "+m" (q[d]), "=m" (p[d])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -290,7 +289,6 @@ static void raid6_avx5122_xor_syndrome(i "=m" (p[d+64])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
@@ -527,7 +525,6 @@ static void raid6_avx5124_xor_syndrome(i"=m" (p[d+192 ]),"+m" (q[d]),"+m" (q[d+64 ]), "+m" (q[d+128]), "+m" (q[d+192])); } - asmvolatile ("sfence" : : :"memory" ); asmvolatile ("vzeroupper" );kernel_fpu_end (); }
lib/raid/raid6/x86/sse2.c renamed from lib/raid/raid6/x86/sse2.c~raid6-remove-the-sfence-from-the-x86-xor_syndrome-functions +0 -3
@@ -113,7 +113,6 @@ static void raid6_sse21_xor_syndrome(int asmvolatile ("movdqa %%xmm2,%0" :"=m" (p[d])); } - asmvolatile ("sfence" : : :"memory" );kernel_fpu_end (); }
@@ -241,7 +240,6 @@ static void raid6_sse22_xor_syndrome(int asmvolatile ("movdqa %%xmm3,%0" :"=m" (p[d+16 ])); } - asmvolatile ("sfence" : : :"memory" );kernel_fpu_end (); }
@@ -445,7 +443,6 @@ static void raid6_sse24_xor_syndrome(int asmvolatile ("movdqa %%xmm12,%0" :"=m" (q[d+32 ])); asmvolatile ("movdqa %%xmm14,%0" :"=m" (q[d+48 ])); } - asmvolatile ("sfence" : : :"memory" );kernel_fpu_end (); }
_
Patches currently in -mm which might be from [email protected] are
raid6-use-normal-stores-in-the-x86-x4-xor_syndrome-functions.patch raid6-declare-the-avx-512-p-q-stores-as-asm-outputs.patch raid6-remove-the-sfence-from-the-x86-xor_syndrome-functions.patch raid6-prefetch-q-with-prefetcht0-in-the-x86-x4-xor_syndrome-functions.patch
Re: [PATCH 1/3] raid6: declare the AVX-512 P/Q stores as asm outputs
In reply to Miroslav CimermanLooks good:
Re: [PATCH 2/3] raid6: remove the sfence from the x86 xor_syndrome functions
In reply to Miroslav CimermanLooks good:
Re: [PATCH 3/3] raid6: prefetch Q with prefetcht0 in the x86 x4 xor_syndrome functions
In reply to Miroslav CimermanLooks good: