From d4184b9aee17d7450346817614b39d7ef2561b6a Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 14:05:56 +0200 Subject: [PATCH 01/21] Harden public RAID API argument validation --- raid/check.c | 29 +++++++++++++++++++++++++++-- raid/module.c | 36 ++++++++++++++++++++++++++++++++---- raid/raid.h | 36 ++++++++++++++++++++++++------------ 3 files changed, 83 insertions(+), 18 deletions(-) diff --git a/raid/check.c b/raid/check.c index 6cc11b82..fd519f82 100644 --- a/raid/check.c +++ b/raid/check.c @@ -113,10 +113,20 @@ int raid_check(int nr, int *ir, int nd, int np, size_t size, void **v) /* enforce limit on size */ BUG_ON(size % 64 != 0); - /* enforce limit on number of failures */ - BUG_ON(nr >= np); /* >= because we check with extra parity */ + /* enforce limit on number of data blocks */ + BUG_ON(nd < 1 || nd > RAID_DATA_MAX); + + /* enforce limit on number of parity blocks */ + BUG_ON(np < 1); BUG_ON(np > RAID_PARITY_MAX || (raid_mode_active == RAID_MODE_VANDERMONDE_RAID && np > 3)); + /* enforce limit on number of failures */ + BUG_ON(nr < 0 || nr >= np); /* >= because we check with extra parity */ + + /* enforce valid vectors */ + BUG_ON(v == 0); + BUG_ON(nr > 0 && ir == 0); + /* enforce order in index vector */ BUG_ON(nr >= 2 && ir[0] >= ir[1]); BUG_ON(nr >= 3 && ir[1] >= ir[2]); @@ -125,6 +135,7 @@ int raid_check(int nr, int *ir, int nd, int np, size_t size, void **v) BUG_ON(nr >= 6 && ir[4] >= ir[5]); /* enforce limit on index vector */ + BUG_ON(nr > 0 && ir[0] < 0); BUG_ON(nr > 0 && ir[nr - 1] >= nd + np); /* count failed data disk */ @@ -153,6 +164,20 @@ int raid_scan(int *ir, int nd, int np, size_t size, void **v) { int r; + /* enforce limit on size */ + BUG_ON(size % 64 != 0); + + /* enforce limit on number of data blocks */ + BUG_ON(nd < 1 || nd > RAID_DATA_MAX); + + /* enforce limit on number of parity blocks */ + BUG_ON(np < 0); + BUG_ON(np > RAID_PARITY_MAX || (raid_mode_active == RAID_MODE_VANDERMONDE_RAID && np > 3)); + + /* enforce valid vectors */ + BUG_ON(v == 0); + BUG_ON(np > 1 && ir == 0); + /* check the special case of no failure */ if (np != 0 && raid_check(0, 0, nd, np, size, v) == 0) return 0; diff --git a/raid/module.c b/raid/module.c index 4a0f9807..3007a88c 100644 --- a/raid/module.c +++ b/raid/module.c @@ -78,10 +78,16 @@ void raid_gen(int nd, int np, size_t size, void **v) /* enforce limit on size */ BUG_ON(size % 64 != 0); - /* enforce limit on number of failures */ + /* enforce limit on number of data blocks */ + BUG_ON(nd < 1 || nd > RAID_DATA_MAX); + + /* enforce limit on number of parity blocks */ BUG_ON(np < 1); BUG_ON(np > RAID_PARITY_MAX || (raid_mode_active == RAID_MODE_VANDERMONDE_RAID && np > 3)); + /* enforce valid vector */ + BUG_ON(v == 0); + BUG_ON(raid_gen_ptr[np - 1] == 0); raid_gen_ptr[np - 1](nd, size, v); } @@ -103,10 +109,20 @@ void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v) /* enforce limit on size */ BUG_ON(size % 64 != 0); - /* enforce limit on number of failures */ - BUG_ON(nr > np); + /* enforce limit on number of data blocks */ + BUG_ON(nd < 1 || nd > RAID_DATA_MAX); + + /* enforce limit on number of parity blocks */ + BUG_ON(np < 1); BUG_ON(np > RAID_PARITY_MAX || (raid_mode_active == RAID_MODE_VANDERMONDE_RAID && np > 3)); + /* enforce limit on number of failures */ + BUG_ON(nr < 0 || nr > np); + + /* enforce valid vectors */ + BUG_ON(v == 0); + BUG_ON(nr > 0 && ir == 0); + /* enforce order in index vector */ BUG_ON(nr >= 2 && ir[0] >= ir[1]); BUG_ON(nr >= 3 && ir[1] >= ir[2]); @@ -115,6 +131,7 @@ void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v) BUG_ON(nr >= 6 && ir[4] >= ir[5]); /* enforce limit on index vector */ + BUG_ON(nr > 0 && ir[0] < 0); BUG_ON(nr > 0 && ir[nr - 1] >= nd + np); /* count the number of data blocks to recover */ @@ -150,6 +167,7 @@ void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v) * Recover the nrd data blocks specified in ir[], * using the first nrd parity in ip[] for recovering */ + BUG_ON(raid_rec_ptr[nrd - 1] == 0); raid_rec_ptr[nrd - 1](nrd, ir, ip, nd, size, v); } @@ -163,10 +181,18 @@ void raid_data(int nr, int *id, int *ip, int nd, size_t size, void **v) /* enforce limit on size */ BUG_ON(size % 64 != 0); + /* enforce limit on number of data blocks */ + BUG_ON(nd < 1 || nd > RAID_DATA_MAX); + /* enforce limit on number of failures */ - BUG_ON(nr > nd); + BUG_ON(nr < 0 || nr > nd); BUG_ON(nr > RAID_PARITY_MAX); + /* enforce valid vectors */ + BUG_ON(v == 0); + BUG_ON(nr > 0 && id == 0); + BUG_ON(nr > 0 && ip == 0); + /* enforce order in index vector for data */ BUG_ON(nr >= 2 && id[0] >= id[1]); BUG_ON(nr >= 3 && id[1] >= id[2]); @@ -175,6 +201,7 @@ void raid_data(int nr, int *id, int *ip, int nd, size_t size, void **v) BUG_ON(nr >= 6 && id[4] >= id[5]); /* enforce limit on index vector for data */ + BUG_ON(nr > 0 && id[0] < 0); BUG_ON(nr > 0 && id[nr - 1] >= nd); /* enforce order in index vector for parity */ @@ -185,6 +212,7 @@ void raid_data(int nr, int *id, int *ip, int nd, size_t size, void **v) BUG_ON(nr >= 6 && ip[4] >= ip[5]); /* enforce limit on index vector for parity */ + BUG_ON(nr > 0 && ip[0] < 0); BUG_ON(nr > 0 && (ip[nr - 1] >= RAID_PARITY_MAX || (raid_mode_active == RAID_MODE_VANDERMONDE_RAID && ip[nr - 1] >= 3))); /* if failed data is present */ diff --git a/raid/raid.h b/raid/raid.h index 934085e1..d88fe8bb 100644 --- a/raid/raid.h +++ b/raid/raid.h @@ -198,8 +198,9 @@ void raid_zero(void *zero); * * Each parity block allows to recover one data block. * - * @nd Number of data blocks. - * @np Number of parity blocks to compute. + * @nd Number of data blocks. It must be between 1 and RAID_DATA_MAX. + * @np Number of parity blocks to compute. It must be between 1 and + * RAID_PARITY_MAX. RAID_MODE_VANDERMONDE_RAID supports at most 3. * @size Size of the blocks pointed to by @v. It must be a multiple of 64. * @v Vector of pointers to the blocks of data and parity. * It has (@nd + @np) elements. The starting elements are the blocks for @@ -230,13 +231,16 @@ void raid_gen(int nd, int np, size_t size, void **v); * This happens even in the case you have more parity blocks than needed, * and some form of integrity verification would be possible. * - * @nr Number of failed data and parity blocks to recover. + * @nr Number of failed data and parity blocks to recover. It must be + * between 0 and @np. * @ir[] Vector of @nr indexes of the failed data and parity blocks. * The indexes start from 0. They must be in order. + * All indexes must be nonnegative and smaller than @nd + @np. * The first parity is represented with value @nd, the second with value * @nd + 1, just like positions in the @v vector. - * @nd Number of data blocks. - * @np Number of parity blocks. + * @nd Number of data blocks. It must be between 1 and RAID_DATA_MAX. + * @np Number of parity blocks. It must be between 1 and RAID_PARITY_MAX. + * RAID_MODE_VANDERMONDE_RAID supports at most 3. * @size Size of the blocks pointed by @v. It must be a multiple of 64. * @v Vector of pointers to the blocks of data and parity. * It has (@nd + @np) elements. The starting elements are the blocks @@ -253,12 +257,16 @@ void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v); * This function recovers all the data blocks marked as bad in the @id vector. * The parity blocks are not modified. * - * @nr Number of failed data blocks to recover. + * @nr Number of failed data blocks to recover. It must be nonnegative, + * not greater than @nd, and not greater than RAID_PARITY_MAX. * @id[] Vector of @nr indexes of the data blocks to recover. * The indexes start from 0. They must be in order. + * All indexes must be nonnegative and smaller than @nd. * @ip[] Vector of @nr indexes of the parity blocks to use for recovery. * The indexes start from 0. They must be in order. - * @nd Number of data blocks. + * All indexes must be nonnegative and smaller than RAID_PARITY_MAX. + * In RAID_MODE_VANDERMONDE_RAID they must be smaller than 3. + * @nd Number of data blocks. It must be between 1 and RAID_DATA_MAX. * @size Size of the blocks pointed to by @v. It must be a multiple of 64. * @v Vector of pointers to the blocks of data and parity. * It has (@nd + @ip[@nr - 1] + 1) elements. The starting elements are the @@ -281,13 +289,16 @@ void raid_data(int nr, int *id, int *ip, int nd, size_t size, void **v); * * No data or parity blocks are modified. * - * @nr Number of failed data and parity blocks. + * @nr Number of failed data and parity blocks. It must be nonnegative and + * strictly smaller than @np. * @ir[] Vector of @nr indexes of the failed data and parity blocks. * The indexes start from 0. They must be in order. + * All indexes must be nonnegative and smaller than @nd + @np. * The first parity is represented with value @nd, the second with value * @nd + 1, just like positions in the @v vector. - * @nd Number of data blocks. - * @np Number of parity blocks. + * @nd Number of data blocks. It must be between 1 and RAID_DATA_MAX. + * @np Number of parity blocks. It must be between 1 and RAID_PARITY_MAX. + * RAID_MODE_VANDERMONDE_RAID supports at most 3. * @size Size of the blocks pointed by @v. It must be a multiple of 64. * @v Vector of pointers to the blocks of data and parity. * It has (@nd + @np) elements. The starting elements are the blocks @@ -331,8 +342,9 @@ int raid_check(int nr, int *ir, int nd, int np, size_t size, void **v); * The indexes start from 0 and they are in order. * The first parity is represented with value @nd, the second with value * @nd + 1, just like positions in the @v vector. - * @nd Number of data blocks. - * @np Number of parity blocks. + * @nd Number of data blocks. It must be between 1 and RAID_DATA_MAX. + * @np Number of parity blocks. It must be between 0 and RAID_PARITY_MAX. + * RAID_MODE_VANDERMONDE_RAID supports at most 3. * @size Size of the blocks pointed by @v. It must be a multiple of 64. * @v Vector of pointers to the blocks of data and parity. * It has (@nd + @np) elements. The starting elements are the blocks From 46ebce4e3ca47c7d03024ff4626d81cf38bc9637 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 14:46:18 +0200 Subject: [PATCH 02/21] Add specialized P+Q REC2OF2 recovery to all SIMD backends --- TODO | 5 - raid/avx2.c | 159 +++++++++++++++++++++++++++++++- raid/avx512.c | 80 ++++++++++++++++ raid/gfni.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++++++ raid/neon.c | 123 +++++++++++++++++++++++++ raid/neon32.c | 105 +++++++++++++++++++++ raid/ssse3.c | 172 ++++++++++++++++++++++++++++++++++ 7 files changed, 885 insertions(+), 9 deletions(-) diff --git a/TODO b/TODO index a58b42eb..5c0e630f 100644 --- a/TODO +++ b/TODO @@ -24,11 +24,6 @@ All done ? * Enable MuseAir and disable random seed -* Optimize rec2of2 SIMD recovery for P+Q: add specialized AVX2, AVX512 and GFNI - fast paths for recovering two failed data disks when ip[0] == 0 && ip[1] == 1. - Exploit the RAID6 closed-form solution to reduce recovery from 4 generic GF - multiplications to 2 GF multiplications plus XOR (Dy = C0*Pd ^ C1*Qd, Dx = Pd ^ Dy). - * Check Reliability Test. We can derive Failure probability from MBTF https://github.com/linuxhw/SMART https://github.com/linuxhw/EnterpriseDrive diff --git a/raid/avx2.c b/raid/avx2.c index dfe230ac..be9364cd 100644 --- a/raid/avx2.c +++ b/raid/avx2.c @@ -1039,6 +1039,79 @@ static __always_inline void raid_rec2_avx2_delta(int nr, int *id, int *ip, int n raid_avx_end(); } +/* + * Recover failure of two data blocks using P and Q AVX2 implementation. + */ +static __always_inline void raid_rec2of2_avx2(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + uint8_t C[2]; + size_t i; + + /* get multiplication coefficients */ + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + /* compute delta parity */ + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_avx_begin(); + + asm volatile ("vpbroadcastb %0,%%ymm7" : : "m" (gfconst16.low4[0])); + + for (i = 0; i < size; i += 32) { + /* Pd */ + asm volatile ("vmovdqa %0,%%ymm0" : : "m" (p[i])); + asm volatile ("vpxor %0,%%ymm0,%%ymm0" : : "m" (pa[i])); + + /* Qd */ + asm volatile ("vmovdqa %0,%%ymm1" : : "m" (q[i])); + asm volatile ("vpxor %0,%%ymm1,%%ymm1" : : "m" (qa[i])); + + /* split Pd and Qd */ + asm volatile ("vpsrlw $4,%ymm0,%ymm2"); + asm volatile ("vpsrlw $4,%ymm1,%ymm3"); + asm volatile ("vpand %ymm7,%ymm0,%ymm4"); + asm volatile ("vpand %ymm7,%ymm1,%ymm5"); + asm volatile ("vpand %ymm7,%ymm2,%ymm2"); + asm volatile ("vpand %ymm7,%ymm3,%ymm3"); + + /* C0 * Pd */ + asm volatile ("vbroadcasti128 %0,%%ymm6" : : "m" (raid_gfmulpshufb[C[0]][0][0])); + asm volatile ("vpshufb %ymm4,%ymm6,%ymm6"); + + asm volatile ("vbroadcasti128 %0,%%ymm4" : : "m" (raid_gfmulpshufb[C[0]][1][0])); + asm volatile ("vpshufb %ymm2,%ymm4,%ymm4"); + asm volatile ("vpxor %ymm4,%ymm6,%ymm6"); + + /* C1 * Qd */ + asm volatile ("vbroadcasti128 %0,%%ymm4" : : "m" (raid_gfmulpshufb[C[1]][0][0])); + asm volatile ("vpshufb %ymm5,%ymm4,%ymm4"); + asm volatile ("vpxor %ymm4,%ymm6,%ymm6"); + + asm volatile ("vbroadcasti128 %0,%%ymm4" : : "m" (raid_gfmulpshufb[C[1]][1][0])); + asm volatile ("vpshufb %ymm3,%ymm4,%ymm4"); + asm volatile ("vpxor %ymm4,%ymm6,%ymm6"); + + /* Dy = ymm6, Dx = Pd ^ Dy */ + asm volatile ("vpxor %ymm6,%ymm0,%ymm0"); + + asm volatile ("vmovdqa %%ymm0,%0" : "=m" (pa[i])); + asm volatile ("vmovdqa %%ymm6,%0" : "=m" (qa[i])); + } + + raid_avx_end(); +} + /* * RAID recovering AVX2 implementation optimized for up to four failures. * @@ -1440,7 +1513,7 @@ static __always_inline void raid_recX_avx2(int nr, int *id, int *ip, int nd, siz /* * RAID recovering for two disks AVX2 extended implementation */ -static __always_inline void raid_rec2_avx2ext_delta(int nr, int *id, int *ip, int nd, size_t size, void **vv) +static __always_inline void raid_rec2_avx2ext_delta(int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; const int N = 2; @@ -1451,8 +1524,6 @@ static __always_inline void raid_rec2_avx2ext_delta(int nr, int *id, int *ip, in size_t i; int j, k; - (void)nr; /* unused, it's always 2 */ - /* setup the coefficients matrix */ for (j = 0; j < N; ++j) for (k = 0; k < N; ++k) @@ -1537,6 +1608,79 @@ static __always_inline void raid_rec2_avx2ext_delta(int nr, int *id, int *ip, in raid_avx_end(); } +/* + * RAID recovering for two disks with P and Q AVX2 implementation + */ +static __always_inline void raid_rec2of2_avx2ext(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + const int N = 2; + uint8_t *p[N]; + uint8_t *pa[N]; + uint8_t C[N]; + size_t i; + + /* get multiplication coefficients */ + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + /* compute delta parity */ + raid_delta_gen(N, id, ip, nd, size, vv); + + p[0] = v[nd]; + p[1] = v[nd + 1]; + pa[0] = v[id[0]]; + pa[1] = v[id[1]]; + + raid_avx_begin(); + + asm volatile ("vpbroadcastb %0,%%ymm7" : : "m" (gfconst16.low4[0])); + + /* the multiplication tables are constant for the whole recovery */ + asm volatile ("vbroadcasti128 %0,%%ymm8" : : "m" (raid_gfmulpshufb[C[0]][0][0])); + asm volatile ("vbroadcasti128 %0,%%ymm9" : : "m" (raid_gfmulpshufb[C[0]][1][0])); + asm volatile ("vbroadcasti128 %0,%%ymm10" : : "m" (raid_gfmulpshufb[C[1]][0][0])); + asm volatile ("vbroadcasti128 %0,%%ymm11" : : "m" (raid_gfmulpshufb[C[1]][1][0])); + + for (i = 0; i < size; i += 32) { + /* d0 = p[0] ^ pa[0] */ + asm volatile ("vmovdqa %0,%%ymm0" : : "m" (p[0][i])); + asm volatile ("vmovdqa %0,%%ymm2" : : "m" (pa[0][i])); + asm volatile ("vpxor %ymm2,%ymm0,%ymm0"); + + /* d1 = p[1] ^ pa[1] */ + asm volatile ("vmovdqa %0,%%ymm1" : : "m" (p[1][i])); + asm volatile ("vmovdqa %0,%%ymm3" : : "m" (pa[1][i])); + asm volatile ("vpxor %ymm3,%ymm1,%ymm1"); + + /* C[0] * d0 */ + asm volatile ("vpsrlw $4,%ymm0,%ymm5"); + asm volatile ("vpand %ymm7,%ymm0,%ymm4"); + asm volatile ("vpand %ymm7,%ymm5,%ymm5"); + asm volatile ("vpshufb %ymm4,%ymm8,%ymm2"); + asm volatile ("vpshufb %ymm5,%ymm9,%ymm3"); + asm volatile ("vpxor %ymm3,%ymm2,%ymm6"); + + /* C[1] * d1 */ + asm volatile ("vpsrlw $4,%ymm1,%ymm5"); + asm volatile ("vpand %ymm7,%ymm1,%ymm4"); + asm volatile ("vpand %ymm7,%ymm5,%ymm5"); + asm volatile ("vpshufb %ymm4,%ymm10,%ymm2"); + asm volatile ("vpshufb %ymm5,%ymm11,%ymm3"); + asm volatile ("vpxor %ymm2,%ymm6,%ymm6"); + asm volatile ("vpxor %ymm3,%ymm6,%ymm6"); + + /* pa[1] = C[0] * d0 ^ C[1] * d1 */ + asm volatile ("vmovdqa %%ymm6,%0" : "=m" (pa[1][i])); + + /* pa[0] = d0 ^ pa[1] */ + asm volatile ("vpxor %ymm6,%ymm0,%ymm0"); + asm volatile ("vmovdqa %%ymm0,%0" : "=m" (pa[0][i])); + } + + raid_avx_end(); +} + /* * RAID recovering AVX2 implementation optimized for up to three failures. * @@ -2224,6 +2368,13 @@ void raid_rec1_avx2(int nr, int *id, int *ip, int nd, size_t size, void **vv) void raid_rec2_avx2(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + /* if recovering with P,Q use the specialized function */ + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_avx2(id, ip, nd, size, vv); + return; + } + raid_rec2_avx2_delta(2, id, ip, nd, size, vv); } @@ -2277,7 +2428,7 @@ void raid_rec2_avx2ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) /* if recovering with P,Q uses the delta function */ if (ip[0] == 0 && ip[1] == 1) { - raid_rec2_avx2ext_delta(2, id, ip, nd, size, vv); + raid_rec2of2_avx2ext(id, ip, nd, size, vv); return; } diff --git a/raid/avx512.c b/raid/avx512.c index 621a34bb..56d2b86b 100644 --- a/raid/avx512.c +++ b/raid/avx512.c @@ -887,9 +887,89 @@ void raid_rec1_avx512bw(int nr, int *id, int *ip, int nd, size_t size, void **vv raid_recX_avx512bw(1, id, ip, nd, size, vv); } +/* + * Recover failure of two data blocks using P and Q AVX512BW implementation. + */ +static __always_inline void raid_rec2of2_avx512bw(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + uint8_t C[2]; + size_t i; + + /* get multiplication coefficients */ + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + /* compute delta parity */ + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_avx_begin(); + + asm volatile ("vpbroadcastb %0,%%zmm31" : : "m" (gfconst16.low4[0])); + + /* keep both multiplication-table pairs resident */ + asm volatile ("vbroadcasti32x4 %0,%%zmm16" : : "m" (raid_gfmulpshufb[C[0]][0][0])); + asm volatile ("vbroadcasti32x4 %0,%%zmm17" : : "m" (raid_gfmulpshufb[C[0]][1][0])); + asm volatile ("vbroadcasti32x4 %0,%%zmm18" : : "m" (raid_gfmulpshufb[C[1]][0][0])); + asm volatile ("vbroadcasti32x4 %0,%%zmm19" : : "m" (raid_gfmulpshufb[C[1]][1][0])); + + for (i = 0; i < size; i += 64) { + /* Pd */ + asm volatile ("vmovdqa64 %0,%%zmm0" : : "m" (p[i])); + asm volatile ("vpxord %0,%%zmm0,%%zmm0" : : "m" (pa[i])); + + /* Qd */ + asm volatile ("vmovdqa64 %0,%%zmm1" : : "m" (q[i])); + asm volatile ("vpxord %0,%%zmm1,%%zmm1" : : "m" (qa[i])); + + /* split Pd and Qd */ + asm volatile ("vpsrlw $4,%zmm0,%zmm2"); + asm volatile ("vpsrlw $4,%zmm1,%zmm3"); + asm volatile ("vpandq %zmm31,%zmm0,%zmm4"); + asm volatile ("vpandq %zmm31,%zmm1,%zmm5"); + asm volatile ("vpandq %zmm31,%zmm2,%zmm2"); + asm volatile ("vpandq %zmm31,%zmm3,%zmm3"); + + /* C0 * Pd */ + asm volatile ("vpshufb %zmm4,%zmm16,%zmm6"); + asm volatile ("vpshufb %zmm2,%zmm17,%zmm7"); + asm volatile ("vpxord %zmm7,%zmm6,%zmm6"); + + /* C1 * Qd */ + asm volatile ("vpshufb %zmm5,%zmm18,%zmm7"); + asm volatile ("vpxord %zmm7,%zmm6,%zmm6"); + asm volatile ("vpshufb %zmm3,%zmm19,%zmm7"); + asm volatile ("vpxord %zmm7,%zmm6,%zmm6"); + + /* Dy = zmm6, Dx = Pd ^ Dy */ + asm volatile ("vpxord %zmm6,%zmm0,%zmm0"); + + asm volatile ("vmovdqa64 %%zmm0,%0" : "=m" (pa[i])); + asm volatile ("vmovdqa64 %%zmm6,%0" : "=m" (qa[i])); + } + + raid_avx_end(); +} + void raid_rec2_avx512bw(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + /* if recovering with P,Q use the specialized function */ + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_avx512bw(id, ip, nd, size, vv); + return; + } + raid_recX_avx512bw(2, id, ip, nd, size, vv); } diff --git a/raid/gfni.c b/raid/gfni.c index 0d91dca8..f4717630 100644 --- a/raid/gfni.c +++ b/raid/gfni.c @@ -1264,9 +1264,82 @@ void raid_rec1_avx2gfni_raid(int nr, int *id, int *ip, int nd, size_t size, void raid_recX_avx2gfni_raid(1, id, ip, nd, size, vv); } +/* + * Recover failure of two data blocks using P and Q AVX2 RAID GFNI implementation. + */ +static __always_inline void raid_rec2of2_avx2gfni_raid(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + const uint8_t *T[2]; + uint8_t C[2]; + size_t i; + + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + T[0] = raid_gfaffine_raid[C[0]]; + T[1] = raid_gfaffine_raid[C[1]]; + + /* compute delta parity */ + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_avx_begin(); + + asm volatile ("vpbroadcastq %0,%%ymm14" : : "m" (T[0][0])); + asm volatile ("vpbroadcastq %0,%%ymm15" : : "m" (T[1][0])); + + for (i = 0; i < size; i += 64) { + /* Pd, two 32-byte lanes */ + asm volatile ("vmovdqa %0,%%ymm0" : : "m" (p[i])); + asm volatile ("vmovdqa %0,%%ymm1" : : "m" (p[i + 32])); + asm volatile ("vpxor %0,%%ymm0,%%ymm0" : : "m" (pa[i])); + asm volatile ("vpxor %0,%%ymm1,%%ymm1" : : "m" (pa[i + 32])); + + /* Qd, two 32-byte lanes */ + asm volatile ("vmovdqa %0,%%ymm2" : : "m" (q[i])); + asm volatile ("vmovdqa %0,%%ymm3" : : "m" (q[i + 32])); + asm volatile ("vpxor %0,%%ymm2,%%ymm2" : : "m" (qa[i])); + asm volatile ("vpxor %0,%%ymm3,%%ymm3" : : "m" (qa[i + 32])); + + /* Dy = C0 * Pd ^ C1 * Qd */ + asm volatile ("vgf2p8affineqb $0,%ymm14,%ymm0,%ymm4"); + asm volatile ("vgf2p8affineqb $0,%ymm14,%ymm1,%ymm5"); + asm volatile ("vgf2p8affineqb $0,%ymm15,%ymm2,%ymm6"); + asm volatile ("vgf2p8affineqb $0,%ymm15,%ymm3,%ymm7"); + asm volatile ("vpxor %ymm6,%ymm4,%ymm4"); + asm volatile ("vpxor %ymm7,%ymm5,%ymm5"); + + /* Dx = Pd ^ Dy */ + asm volatile ("vpxor %ymm4,%ymm0,%ymm0"); + asm volatile ("vpxor %ymm5,%ymm1,%ymm1"); + + asm volatile ("vmovdqa %%ymm0,%0" : "=m" (pa[i])); + asm volatile ("vmovdqa %%ymm1,%0" : "=m" (pa[i + 32])); + asm volatile ("vmovdqa %%ymm4,%0" : "=m" (qa[i])); + asm volatile ("vmovdqa %%ymm5,%0" : "=m" (qa[i + 32])); + } + + raid_avx_end(); +} + void raid_rec2_avx2gfni_raid(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_avx2gfni_raid(id, ip, nd, size, vv); + return; + } + raid_recX_avx2gfni_raid(2, id, ip, nd, size, vv); } @@ -1307,9 +1380,68 @@ void raid_rec1_avx512gfni_raid(int nr, int *id, int *ip, int nd, size_t size, vo raid_recX_avx512gfni_raid(1, id, ip, nd, size, vv); } +/* + * Recover failure of two data blocks using P and Q AVX512 RAID GFNI implementation. + */ +static __always_inline void raid_rec2of2_avx512gfni_raid(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + const uint8_t *T[2]; + uint8_t C[2]; + size_t i; + + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + T[0] = raid_gfaffine_raid[C[0]]; + T[1] = raid_gfaffine_raid[C[1]]; + + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_avx_begin(); + + asm volatile ("vpbroadcastq %0,%%zmm14" : : "m" (T[0][0])); + asm volatile ("vpbroadcastq %0,%%zmm15" : : "m" (T[1][0])); + + for (i = 0; i < size; i += 64) { + asm volatile ("vmovdqa64 %0,%%zmm0" : : "m" (p[i])); + asm volatile ("vpxorq %0,%%zmm0,%%zmm0" : : "m" (pa[i])); + + asm volatile ("vmovdqa64 %0,%%zmm1" : : "m" (q[i])); + asm volatile ("vpxorq %0,%%zmm1,%%zmm1" : : "m" (qa[i])); + + asm volatile ("vgf2p8affineqb $0,%zmm14,%zmm0,%zmm2"); + asm volatile ("vgf2p8affineqb $0,%zmm15,%zmm1,%zmm3"); + asm volatile ("vpxorq %zmm3,%zmm2,%zmm2"); + + /* Dx = Pd ^ Dy */ + asm volatile ("vpxorq %zmm2,%zmm0,%zmm0"); + + asm volatile ("vmovdqa64 %%zmm0,%0" : "=m" (pa[i])); + asm volatile ("vmovdqa64 %%zmm2,%0" : "=m" (qa[i])); + } + + raid_avx_end(); +} + void raid_rec2_avx512gfni_raid(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_avx512gfni_raid(id, ip, nd, size, vv); + return; + } + raid_recX_avx512gfni_raid(2, id, ip, nd, size, vv); } @@ -1350,9 +1482,73 @@ void raid_rec1_avx2gfni_aes(int nr, int *id, int *ip, int nd, size_t size, void raid_recX_avx2gfni_aes(1, id, ip, nd, size, vv); } +/* + * Recover failure of two data blocks using P and Q AVX2 AES GFNI implementation. + */ +static __always_inline void raid_rec2of2_avx2gfni_aes(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + uint8_t C[2]; + size_t i; + + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_avx_begin(); + + asm volatile ("vpbroadcastb %0,%%ymm14" : : "m" (C[0])); + asm volatile ("vpbroadcastb %0,%%ymm15" : : "m" (C[1])); + + for (i = 0; i < size; i += 64) { + asm volatile ("vmovdqa %0,%%ymm0" : : "m" (p[i])); + asm volatile ("vmovdqa %0,%%ymm1" : : "m" (p[i + 32])); + asm volatile ("vpxor %0,%%ymm0,%%ymm0" : : "m" (pa[i])); + asm volatile ("vpxor %0,%%ymm1,%%ymm1" : : "m" (pa[i + 32])); + + asm volatile ("vmovdqa %0,%%ymm2" : : "m" (q[i])); + asm volatile ("vmovdqa %0,%%ymm3" : : "m" (q[i + 32])); + asm volatile ("vpxor %0,%%ymm2,%%ymm2" : : "m" (qa[i])); + asm volatile ("vpxor %0,%%ymm3,%%ymm3" : : "m" (qa[i + 32])); + + asm volatile ("vgf2p8mulb %ymm14,%ymm0,%ymm4"); + asm volatile ("vgf2p8mulb %ymm14,%ymm1,%ymm5"); + asm volatile ("vgf2p8mulb %ymm15,%ymm2,%ymm6"); + asm volatile ("vgf2p8mulb %ymm15,%ymm3,%ymm7"); + asm volatile ("vpxor %ymm6,%ymm4,%ymm4"); + asm volatile ("vpxor %ymm7,%ymm5,%ymm5"); + + asm volatile ("vpxor %ymm4,%ymm0,%ymm0"); + asm volatile ("vpxor %ymm5,%ymm1,%ymm1"); + + asm volatile ("vmovdqa %%ymm0,%0" : "=m" (pa[i])); + asm volatile ("vmovdqa %%ymm1,%0" : "=m" (pa[i + 32])); + asm volatile ("vmovdqa %%ymm4,%0" : "=m" (qa[i])); + asm volatile ("vmovdqa %%ymm5,%0" : "=m" (qa[i + 32])); + } + + raid_avx_end(); +} + void raid_rec2_avx2gfni_aes(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_avx2gfni_aes(id, ip, nd, size, vv); + return; + } + raid_recX_avx2gfni_aes(2, id, ip, nd, size, vv); } @@ -1393,9 +1589,63 @@ void raid_rec1_avx512gfni_aes(int nr, int *id, int *ip, int nd, size_t size, voi raid_recX_avx512gfni_aes(1, id, ip, nd, size, vv); } +/* + * Recover failure of two data blocks using P and Q AVX512 AES GFNI implementation. + */ +static __always_inline void raid_rec2of2_avx512gfni_aes(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + uint8_t C[2]; + size_t i; + + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_avx_begin(); + + asm volatile ("vpbroadcastb %0,%%zmm14" : : "m" (C[0])); + asm volatile ("vpbroadcastb %0,%%zmm15" : : "m" (C[1])); + + for (i = 0; i < size; i += 64) { + asm volatile ("vmovdqa64 %0,%%zmm0" : : "m" (p[i])); + asm volatile ("vpxorq %0,%%zmm0,%%zmm0" : : "m" (pa[i])); + + asm volatile ("vmovdqa64 %0,%%zmm1" : : "m" (q[i])); + asm volatile ("vpxorq %0,%%zmm1,%%zmm1" : : "m" (qa[i])); + + asm volatile ("vgf2p8mulb %zmm14,%zmm0,%zmm2"); + asm volatile ("vgf2p8mulb %zmm15,%zmm1,%zmm3"); + asm volatile ("vpxorq %zmm3,%zmm2,%zmm2"); + + asm volatile ("vpxorq %zmm2,%zmm0,%zmm0"); + + asm volatile ("vmovdqa64 %%zmm0,%0" : "=m" (pa[i])); + asm volatile ("vmovdqa64 %%zmm2,%0" : "=m" (qa[i])); + } + + raid_avx_end(); +} + void raid_rec2_avx512gfni_aes(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_avx512gfni_aes(id, ip, nd, size, vv); + return; + } + raid_recX_avx512gfni_aes(2, id, ip, nd, size, vv); } diff --git a/raid/neon.c b/raid/neon.c index 71ed4ecd..419392d7 100644 --- a/raid/neon.c +++ b/raid/neon.c @@ -1045,9 +1045,132 @@ void raid_rec1_neon(int nr, int *id, int *ip, int nd, size_t size, void **vv) raid_recX_neon(1, id, ip, nd, size, vv); } +/* + * Recover failure of two data blocks using P and Q AArch64 NEON implementation. + */ +static __always_inline void raid_rec2of2_neon(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + uint8_t C[2]; + size_t i; + + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_neon_begin(); + + asm volatile ( + "ldr q28, %0\n" + "ldr q20, %1\n" + "ldr q21, %2\n" + "ldr q22, %3\n" + "ldr q23, %4\n" + : + : "m" (gfconst16.low4[0]), + "m" (raid_gfmulpshufb[C[0]][0][0]), + "m" (raid_gfmulpshufb[C[0]][1][0]), + "m" (raid_gfmulpshufb[C[1]][0][0]), + "m" (raid_gfmulpshufb[C[1]][1][0]) + ); + + for (i = 0; i < size; i += 32) { + asm volatile ( + /* Pd, two lanes */ + "ldr q0, %4\n" + "ldr q1, %5\n" + "ldr q12, %8\n" + "ldr q13, %9\n" + "eor v0.16b, v0.16b, v12.16b\n" + "eor v1.16b, v1.16b, v13.16b\n" + + /* preserve Pd for Dx */ + "mov v8.16b, v0.16b\n" + "mov v9.16b, v1.16b\n" + + /* Qd, two lanes */ + "ldr q2, %6\n" + "ldr q3, %7\n" + "ldr q12, %10\n" + "ldr q13, %11\n" + "eor v2.16b, v2.16b, v12.16b\n" + "eor v3.16b, v3.16b, v13.16b\n" + + /* split Pd */ + "ushr v4.16b, v0.16b, #4\n" + "ushr v5.16b, v1.16b, #4\n" + "and v0.16b, v0.16b, v28.16b\n" + "and v1.16b, v1.16b, v28.16b\n" + "and v4.16b, v4.16b, v28.16b\n" + "and v5.16b, v5.16b, v28.16b\n" + + /* split Qd */ + "ushr v6.16b, v2.16b, #4\n" + "ushr v7.16b, v3.16b, #4\n" + "and v2.16b, v2.16b, v28.16b\n" + "and v3.16b, v3.16b, v28.16b\n" + "and v6.16b, v6.16b, v28.16b\n" + "and v7.16b, v7.16b, v28.16b\n" + + /* C0 * Pd */ + "tbl v10.16b, {v20.16b}, v0.16b\n" + "tbl v11.16b, {v20.16b}, v1.16b\n" + "tbl v12.16b, {v21.16b}, v4.16b\n" + "tbl v13.16b, {v21.16b}, v5.16b\n" + "eor v10.16b, v10.16b, v12.16b\n" + "eor v11.16b, v11.16b, v13.16b\n" + + /* C1 * Qd */ + "tbl v12.16b, {v22.16b}, v2.16b\n" + "tbl v13.16b, {v22.16b}, v3.16b\n" + "tbl v14.16b, {v23.16b}, v6.16b\n" + "tbl v15.16b, {v23.16b}, v7.16b\n" + "eor v12.16b, v12.16b, v14.16b\n" + "eor v13.16b, v13.16b, v15.16b\n" + + /* Dy */ + "eor v10.16b, v10.16b, v12.16b\n" + "eor v11.16b, v11.16b, v13.16b\n" + + /* Dx = Pd ^ Dy */ + "eor v8.16b, v8.16b, v10.16b\n" + "eor v9.16b, v9.16b, v11.16b\n" + + "str q8, %0\n" + "str q9, %1\n" + "str q10, %2\n" + "str q11, %3\n" + : "=m" (pa[i]), "=m" (pa[i + 16]), + "=m" (qa[i]), "=m" (qa[i + 16]) + : "m" (p[i]), "m" (p[i + 16]), + "m" (q[i]), "m" (q[i + 16]), + "m" (pa[i]), "m" (pa[i + 16]), + "m" (qa[i]), "m" (qa[i + 16]) + ); + } + + raid_neon_end(); +} + void raid_rec2_neon(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_neon(id, ip, nd, size, vv); + return; + } + raid_recX_neon(2, id, ip, nd, size, vv); } diff --git a/raid/neon32.c b/raid/neon32.c index 04c839b1..d551917e 100644 --- a/raid/neon32.c +++ b/raid/neon32.c @@ -1225,9 +1225,114 @@ void raid_rec1_neon32(int nr, int *id, int *ip, int nd, size_t size, void **vv) raid_recX_neon32_123(1, id, ip, nd, size, vv); } +/* + * Recover failure of two data blocks using P and Q AArch32 NEON implementation. + */ +static __always_inline void raid_rec2of2_neon32(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + uint8_t C[2]; + size_t i; + + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_neon32_begin(); + + /* q15 = nibble mask, q9-q12 = C0/C1 low/high tables */ + asm volatile ( + "vld1.8 {q15}, %0\n" + "vld1.8 {q9}, %1\n" + "vld1.8 {q10}, %2\n" + "vld1.8 {q11}, %3\n" + "vld1.8 {q12}, %4\n" + : + : "Q" (gfconst16.low4[0]), + "Q" (raid_gfmulpshufb[C[0]][0][0]), + "Q" (raid_gfmulpshufb[C[0]][1][0]), + "Q" (raid_gfmulpshufb[C[1]][0][0]), + "Q" (raid_gfmulpshufb[C[1]][1][0]) + ); + + for (i = 0; i < size; i += 16) { + asm volatile ( + /* Pd */ + "vld1.8 {q0}, %2\n" + "vld1.8 {q13}, %4\n" + "veor q0, q0, q13\n" + + /* Qd */ + "vld1.8 {q1}, %3\n" + "vld1.8 {q13}, %5\n" + "veor q1, q1, q13\n" + + /* split Pd */ + "vmov q2, q0\n" + "vshr.u8 q3, q0, #4\n" + "vand q2, q2, q15\n" + "vand q3, q3, q15\n" + + /* split Qd */ + "vmov q4, q1\n" + "vshr.u8 q5, q1, #4\n" + "vand q4, q4, q15\n" + "vand q5, q5, q15\n" + + /* C0 low * Pd low -> q6 */ + "vtbl.8 d12, {d18-d19}, d4\n" + "vtbl.8 d13, {d18-d19}, d5\n" + + /* C0 high * Pd high -> q7 */ + "vtbl.8 d14, {d20-d21}, d6\n" + "vtbl.8 d15, {d20-d21}, d7\n" + "veor q6, q6, q7\n" + + /* C1 low * Qd low -> q7 */ + "vtbl.8 d14, {d22-d23}, d8\n" + "vtbl.8 d15, {d22-d23}, d9\n" + + /* C1 high * Qd high -> q8 */ + "vtbl.8 d16, {d24-d25}, d10\n" + "vtbl.8 d17, {d24-d25}, d11\n" + "veor q7, q7, q8\n" + + /* Dy */ + "veor q6, q6, q7\n" + + /* Dx = Pd ^ Dy */ + "veor q0, q0, q6\n" + + "vst1.8 {q0}, %0\n" + "vst1.8 {q6}, %1\n" + : "=Q" (pa[i]), "=Q" (qa[i]) + : "Q" (p[i]), "Q" (q[i]), + "Q" (pa[i]), "Q" (qa[i]) + ); + } + + raid_neon32_end(); +} + void raid_rec2_neon32(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_neon32(id, ip, nd, size, vv); + return; + } + raid_recX_neon32_123(2, id, ip, nd, size, vv); } diff --git a/raid/ssse3.c b/raid/ssse3.c index 922d9744..a233b55d 100644 --- a/raid/ssse3.c +++ b/raid/ssse3.c @@ -1304,6 +1304,83 @@ static __always_inline void raid_rec2_ssse3_delta(int nr, int *id, int *ip, int raid_sse_end(); } +/* + * Recover failure of two data blocks using P and Q SSSE3 implementation. + */ +static __always_inline void raid_rec2of2_ssse3(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + uint8_t C[2]; + size_t i; + + /* get multiplication coefficients */ + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + /* compute delta parity */ + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_sse_begin(); + + asm volatile ("movdqa %0,%%xmm7" : : "m" (gfconst16.low4[0])); + + for (i = 0; i < size; i += 16) { + /* Pd */ + asm volatile ("movdqa %0,%%xmm0" : : "m" (p[i])); + asm volatile ("pxor %0,%%xmm0" : : "m" (pa[i])); + + /* Qd */ + asm volatile ("movdqa %0,%%xmm1" : : "m" (q[i])); + asm volatile ("pxor %0,%%xmm1" : : "m" (qa[i])); + + /* split Pd and Qd into low/high nibbles */ + asm volatile ("movdqa %xmm0,%xmm4"); + asm volatile ("movdqa %xmm1,%xmm5"); + asm volatile ("movdqa %xmm0,%xmm2"); + asm volatile ("movdqa %xmm1,%xmm3"); + asm volatile ("psrlw $4,%xmm2"); + asm volatile ("psrlw $4,%xmm3"); + asm volatile ("pand %xmm7,%xmm4"); + asm volatile ("pand %xmm7,%xmm5"); + asm volatile ("pand %xmm7,%xmm2"); + asm volatile ("pand %xmm7,%xmm3"); + + /* C0 * Pd */ + asm volatile ("movdqa %0,%%xmm6" : : "m" (raid_gfmulpshufb[C[0]][0][0])); + asm volatile ("pshufb %xmm4,%xmm6"); + + asm volatile ("movdqa %0,%%xmm4" : : "m" (raid_gfmulpshufb[C[0]][1][0])); + asm volatile ("pshufb %xmm2,%xmm4"); + asm volatile ("pxor %xmm4,%xmm6"); + + /* C1 * Qd */ + asm volatile ("movdqa %0,%%xmm4" : : "m" (raid_gfmulpshufb[C[1]][0][0])); + asm volatile ("pshufb %xmm5,%xmm4"); + asm volatile ("pxor %xmm4,%xmm6"); + + asm volatile ("movdqa %0,%%xmm4" : : "m" (raid_gfmulpshufb[C[1]][1][0])); + asm volatile ("pshufb %xmm3,%xmm4"); + asm volatile ("pxor %xmm4,%xmm6"); + + /* xmm6 = Dy, xmm0 = Pd, so Dx = Pd ^ Dy */ + asm volatile ("pxor %xmm6,%xmm0"); + + asm volatile ("movdqa %%xmm0,%0" : "=m" (pa[i])); + asm volatile ("movdqa %%xmm6,%0" : "=m" (qa[i])); + } + + raid_sse_end(); +} + static __always_inline void raid_recX_ssse3_1234(int nr, int *id, int *ip, int nd, size_t size, void **vv) { @@ -1858,6 +1935,87 @@ static __always_inline void raid_rec2_ssse3ext_delta(int nr, int *id, int *ip, i raid_sse_end(); } +/* + * Recover failure of two data blocks using P and Q SSSE3 extended implementation. + */ +static __always_inline void raid_rec2of2_ssse3ext(int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p; + uint8_t *pa; + uint8_t *q; + uint8_t *qa; + uint8_t C[2]; + size_t i; + + /* get multiplication coefficients */ + C[0] = inv(powgen(id[1] - id[0]) ^ 1); + C[1] = inv(powgen(id[0]) ^ powgen(id[1])); + + /* compute delta parity */ + raid_delta_gen(2, id, ip, nd, size, vv); + + p = v[nd]; + q = v[nd + 1]; + pa = v[id[0]]; + qa = v[id[1]]; + + raid_sse_begin(); + + asm volatile ("movdqa %0,%%xmm7" : : "m" (gfconst16.low4[0])); + + /* keep the four multiplication tables resident */ + asm volatile ("movdqa %0,%%xmm8" : : "m" (raid_gfmulpshufb[C[0]][0][0])); + asm volatile ("movdqa %0,%%xmm9" : : "m" (raid_gfmulpshufb[C[0]][1][0])); + asm volatile ("movdqa %0,%%xmm10" : : "m" (raid_gfmulpshufb[C[1]][0][0])); + asm volatile ("movdqa %0,%%xmm11" : : "m" (raid_gfmulpshufb[C[1]][1][0])); + + for (i = 0; i < size; i += 16) { + /* Pd */ + asm volatile ("movdqa %0,%%xmm0" : : "m" (p[i])); + asm volatile ("pxor %0,%%xmm0" : : "m" (pa[i])); + + /* Qd */ + asm volatile ("movdqa %0,%%xmm1" : : "m" (q[i])); + asm volatile ("pxor %0,%%xmm1" : : "m" (qa[i])); + + /* split Pd and Qd */ + asm volatile ("movdqa %xmm0,%xmm4"); + asm volatile ("movdqa %xmm1,%xmm5"); + asm volatile ("movdqa %xmm0,%xmm2"); + asm volatile ("movdqa %xmm1,%xmm3"); + asm volatile ("psrlw $4,%xmm2"); + asm volatile ("psrlw $4,%xmm3"); + asm volatile ("pand %xmm7,%xmm4"); + asm volatile ("pand %xmm7,%xmm5"); + asm volatile ("pand %xmm7,%xmm2"); + asm volatile ("pand %xmm7,%xmm3"); + + /* C0 * Pd */ + asm volatile ("movdqa %xmm8,%xmm6"); + asm volatile ("pshufb %xmm4,%xmm6"); + asm volatile ("movdqa %xmm9,%xmm12"); + asm volatile ("pshufb %xmm2,%xmm12"); + asm volatile ("pxor %xmm12,%xmm6"); + + /* C1 * Qd */ + asm volatile ("movdqa %xmm10,%xmm12"); + asm volatile ("pshufb %xmm5,%xmm12"); + asm volatile ("pxor %xmm12,%xmm6"); + asm volatile ("movdqa %xmm11,%xmm12"); + asm volatile ("pshufb %xmm3,%xmm12"); + asm volatile ("pxor %xmm12,%xmm6"); + + /* Dy = xmm6, Dx = Pd ^ Dy */ + asm volatile ("pxor %xmm6,%xmm0"); + + asm volatile ("movdqa %%xmm0,%0" : "=m" (pa[i])); + asm volatile ("movdqa %%xmm6,%0" : "=m" (qa[i])); + } + + raid_sse_end(); +} + static __always_inline void raid_recX_ssse3ext_123(int nr, int *id, int *ip, int nd, size_t size, void **vv) { @@ -2459,6 +2617,13 @@ void raid_rec1_ssse3(int nr, int *id, int *ip, int nd, size_t size, void **vv) void raid_rec2_ssse3(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + /* if recovering with P,Q use the specialized function */ + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_ssse3(id, ip, nd, size, vv); + return; + } + raid_rec2_ssse3_delta(2, id, ip, nd, size, vv); } @@ -2503,6 +2668,13 @@ void raid_rec1_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv void raid_rec2_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 2); + + /* if recovering with P,Q use the specialized function */ + if (ip[0] == 0 && ip[1] == 1) { + raid_rec2of2_ssse3ext(id, ip, nd, size, vv); + return; + } + raid_rec2_ssse3ext_delta(2, id, ip, nd, size, vv); } From e74b03a062786aea897efaae7870542f00e1f4d8 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 15:26:20 +0200 Subject: [PATCH 03/21] Remove unused argument from recovery functions --- raid/avx2.c | 65 +++++++++++++++++++++++---------------------------- raid/int.c | 15 ++++++------ raid/ssse3.c | 66 ++++++++++++++++++++++------------------------------ 3 files changed, 64 insertions(+), 82 deletions(-) diff --git a/raid/avx2.c b/raid/avx2.c index be9364cd..acb9336d 100644 --- a/raid/avx2.c +++ b/raid/avx2.c @@ -882,7 +882,7 @@ static __always_inline void raid_genX_avx2ext(int nd, size_t size, void **vv, in /* * RAID recovering for one disk AVX2 implementation */ -static __always_inline void raid_rec1_avx2_delta(int nr, int *id, int *ip, int nd, size_t size, void **vv) +static __always_inline void raid_rec1_avx2_delta(int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p; @@ -891,8 +891,6 @@ static __always_inline void raid_rec1_avx2_delta(int nr, int *id, int *ip, int n uint8_t V; size_t i; - (void)nr; /* unused, it's always 1 */ - /* setup the coefficients matrix */ G = A(ip[0], id[0]); @@ -946,31 +944,28 @@ static __always_inline void raid_rec1_avx2_delta(int nr, int *id, int *ip, int n /* * RAID recovering for two disks AVX2 implementation */ -static __always_inline void raid_rec2_avx2_delta(int nr, int *id, int *ip, int nd, size_t size, void **vv) +static __always_inline void raid_rec2_avx2_delta(int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; - const int N = 2; - uint8_t *p[N]; - uint8_t *pa[N]; - uint8_t G[N * N]; - uint8_t V[N * N]; + uint8_t *p[2]; + uint8_t *pa[2]; + uint8_t G[2 * 2]; + uint8_t V[2 * 2]; size_t i; int j, k; - (void)nr; /* unused, it's always 2 */ - /* setup the coefficients matrix */ - for (j = 0; j < N; ++j) - for (k = 0; k < N; ++k) - G[j * N + k] = A(ip[j], id[k]); + for (j = 0; j < 2; ++j) + for (k = 0; k < 2; ++k) + G[j * 2 + k] = A(ip[j], id[k]); /* invert it to solve the system of linear equations */ - raid_invert(G, V, N); + raid_invert(G, V, 2); /* compute delta parity */ - raid_delta_gen(N, id, ip, nd, size, vv); + raid_delta_gen(2, id, ip, nd, size, vv); - for (j = 0; j < N; ++j) { + for (j = 0; j < 2; ++j) { p[j] = v[nd + ip[j]]; pa[j] = v[id[j]]; } @@ -1516,26 +1511,25 @@ static __always_inline void raid_recX_avx2(int nr, int *id, int *ip, int nd, siz static __always_inline void raid_rec2_avx2ext_delta(int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; - const int N = 2; - uint8_t *p[N]; - uint8_t *pa[N]; - uint8_t G[N * N]; - uint8_t V[N * N]; + uint8_t *p[2]; + uint8_t *pa[2]; + uint8_t G[2 * 2]; + uint8_t V[2 * 2]; size_t i; int j, k; /* setup the coefficients matrix */ - for (j = 0; j < N; ++j) - for (k = 0; k < N; ++k) - G[j * N + k] = A(ip[j], id[k]); + for (j = 0; j < 2; ++j) + for (k = 0; k < 2; ++k) + G[j * 2 + k] = A(ip[j], id[k]); /* invert it to solve the system of linear equations */ - raid_invert(G, V, N); + raid_invert(G, V, 2); /* compute delta parity */ - raid_delta_gen(N, id, ip, nd, size, vv); + raid_delta_gen(2, id, ip, nd, size, vv); - for (j = 0; j < N; ++j) { + for (j = 0; j < 2; ++j) { p[j] = v[nd + ip[j]]; pa[j] = v[id[j]]; } @@ -1614,10 +1608,9 @@ static __always_inline void raid_rec2_avx2ext_delta(int *id, int *ip, int nd, si static __always_inline void raid_rec2of2_avx2ext(int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; - const int N = 2; - uint8_t *p[N]; - uint8_t *pa[N]; - uint8_t C[N]; + uint8_t *p[2]; + uint8_t *pa[2]; + uint8_t C[2]; size_t i; /* get multiplication coefficients */ @@ -1625,7 +1618,7 @@ static __always_inline void raid_rec2of2_avx2ext(int *id, int *ip, int nd, size_ C[1] = inv(powgen(id[0]) ^ powgen(id[1])); /* compute delta parity */ - raid_delta_gen(N, id, ip, nd, size, vv); + raid_delta_gen(2, id, ip, nd, size, vv); p[0] = v[nd]; p[1] = v[nd + 1]; @@ -2362,7 +2355,7 @@ void raid_rec1_avx2(int nr, int *id, int *ip, int nd, size_t size, void **vv) return; } - raid_rec1_avx2_delta(1, id, ip, nd, size, vv); + raid_rec1_avx2_delta(id, ip, nd, size, vv); } void raid_rec2_avx2(int nr, int *id, int *ip, int nd, size_t size, void **vv) @@ -2375,7 +2368,7 @@ void raid_rec2_avx2(int nr, int *id, int *ip, int nd, size_t size, void **vv) return; } - raid_rec2_avx2_delta(2, id, ip, nd, size, vv); + raid_rec2_avx2_delta(id, ip, nd, size, vv); } void raid_rec3_avx2(int nr, int *id, int *ip, int nd, size_t size, void **vv) @@ -2415,7 +2408,7 @@ void raid_rec1_avx2ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) /* if recovering with Q uses the delta function */ if (ip[0] == 1) { - raid_rec1_avx2_delta(1, id, ip, nd, size, vv); + raid_rec1_avx2_delta(id, ip, nd, size, vv); return; } diff --git a/raid/int.c b/raid/int.c index 2036f589..7c27e436 100644 --- a/raid/int.c +++ b/raid/int.c @@ -581,7 +581,6 @@ void raid_rec2_int8(int nr, int *id, int *ip, int nd, size_t size, void **vv) uint8_t *pa; uint8_t *q; uint8_t *qa; - const int N = 2; const uint8_t *T[2][2]; uint8_t G[2 * 2]; uint8_t V[2 * 2]; @@ -597,17 +596,17 @@ void raid_rec2_int8(int nr, int *id, int *ip, int nd, size_t size, void **vv) } /* setup the coefficients matrix */ - for (j = 0; j < N; ++j) - for (k = 0; k < N; ++k) - G[j * N + k] = A(ip[j], id[k]); + for (j = 0; j < 2; ++j) + for (k = 0; k < 2; ++k) + G[j * 2 + k] = A(ip[j], id[k]); /* invert it to solve the system of linear equations */ - raid_invert(G, V, N); + raid_invert(G, V, 2); /* get multiplication tables */ - for (j = 0; j < N; ++j) - for (k = 0; k < N; ++k) - T[j][k] = table(V[j * N + k]); + for (j = 0; j < 2; ++j) + for (k = 0; k < 2; ++k) + T[j][k] = table(V[j * 2 + k]); /* compute delta parity */ raid_delta_gen(2, id, ip, nd, size, vv); diff --git a/raid/ssse3.c b/raid/ssse3.c index a233b55d..5384774f 100644 --- a/raid/ssse3.c +++ b/raid/ssse3.c @@ -1152,7 +1152,7 @@ static __always_inline void raid_genX_ssse3ext(int nd, size_t size, void **vv, i /* * RAID recovering for one disk SSSE3 implementation */ -static __always_inline void raid_rec1_ssse3_delta(int nr, int *id, int *ip, int nd, size_t size, void **vv) +static __always_inline void raid_rec1_ssse3_delta(int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p; @@ -1161,8 +1161,6 @@ static __always_inline void raid_rec1_ssse3_delta(int nr, int *id, int *ip, int uint8_t V; size_t i; - (void)nr; /* unused, it's always 1 */ - /* setup the coefficients matrix */ G = A(ip[0], id[0]); @@ -1203,31 +1201,28 @@ static __always_inline void raid_rec1_ssse3_delta(int nr, int *id, int *ip, int /* * RAID recovering for two disks SSSE3 implementation */ -static __always_inline void raid_rec2_ssse3_delta(int nr, int *id, int *ip, int nd, size_t size, void **vv) +static __always_inline void raid_rec2_ssse3_delta(int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; - const int N = 2; - uint8_t *p[N]; - uint8_t *pa[N]; - uint8_t G[N * N]; - uint8_t V[N * N]; + uint8_t *p[2]; + uint8_t *pa[2]; + uint8_t G[2 * 2]; + uint8_t V[2 * 2]; size_t i; int j, k; - (void)nr; /* unused, it's always 2 */ - /* setup the coefficients matrix */ - for (j = 0; j < N; ++j) - for (k = 0; k < N; ++k) - G[j * N + k] = A(ip[j], id[k]); + for (j = 0; j < 2; ++j) + for (k = 0; k < 2; ++k) + G[j * 2 + k] = A(ip[j], id[k]); /* invert it to solve the system of linear equations */ - raid_invert(G, V, N); + raid_invert(G, V, 2); /* compute delta parity */ - raid_delta_gen(N, id, ip, nd, size, vv); + raid_delta_gen(2, id, ip, nd, size, vv); - for (j = 0; j < N; ++j) { + for (j = 0; j < 2; ++j) { p[j] = v[nd + ip[j]]; pa[j] = v[id[j]]; } @@ -1726,7 +1721,7 @@ static __always_inline void raid_recX_ssse3(int nr, int *id, int *ip, /* * RAID recovering for one disk SSSE3 extended implementation */ -static __always_inline void raid_rec1_ssse3ext_delta(int nr, int *id, int *ip, int nd, size_t size, void **vv) +static __always_inline void raid_rec1_ssse3ext_delta(int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p; @@ -1735,8 +1730,6 @@ static __always_inline void raid_rec1_ssse3ext_delta(int nr, int *id, int *ip, i uint8_t V; size_t i; - (void)nr; /* unused, it's always 1 */ - /* setup the coefficients matrix */ G = A(ip[0], id[0]); @@ -1828,31 +1821,28 @@ static __always_inline void raid_rec1_ssse3ext_delta(int nr, int *id, int *ip, i /* * RAID recovering for two disks SSSE3 extended implementation */ -static __always_inline void raid_rec2_ssse3ext_delta(int nr, int *id, int *ip, int nd, size_t size, void **vv) +static __always_inline void raid_rec2_ssse3ext_delta(int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; - const int N = 2; - uint8_t *p[N]; - uint8_t *pa[N]; - uint8_t G[N * N]; - uint8_t V[N * N]; + uint8_t *p[2]; + uint8_t *pa[2]; + uint8_t G[2 * 2]; + uint8_t V[2 * 2]; size_t i; int j, k; - (void)nr; /* unused, it's always 2 */ - /* setup the coefficients matrix */ - for (j = 0; j < N; ++j) - for (k = 0; k < N; ++k) - G[j * N + k] = A(ip[j], id[k]); + for (j = 0; j < 2; ++j) + for (k = 0; k < 2; ++k) + G[j * 2 + k] = A(ip[j], id[k]); /* invert it to solve the system of linear equations */ - raid_invert(G, V, N); + raid_invert(G, V, 2); /* compute delta parity */ - raid_delta_gen(N, id, ip, nd, size, vv); + raid_delta_gen(2, id, ip, nd, size, vv); - for (j = 0; j < N; ++j) { + for (j = 0; j < 2; ++j) { p[j] = v[nd + ip[j]]; pa[j] = v[id[j]]; } @@ -2611,7 +2601,7 @@ void raid_rec1_ssse3(int nr, int *id, int *ip, int nd, size_t size, void **vv) return; } - raid_rec1_ssse3_delta(1, id, ip, nd, size, vv); + raid_rec1_ssse3_delta(id, ip, nd, size, vv); } void raid_rec2_ssse3(int nr, int *id, int *ip, int nd, size_t size, void **vv) @@ -2624,7 +2614,7 @@ void raid_rec2_ssse3(int nr, int *id, int *ip, int nd, size_t size, void **vv) return; } - raid_rec2_ssse3_delta(2, id, ip, nd, size, vv); + raid_rec2_ssse3_delta(id, ip, nd, size, vv); } void raid_rec3_ssse3(int nr, int *id, int *ip, int nd, size_t size, void **vv) @@ -2662,7 +2652,7 @@ void raid_rec1_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv return; } - raid_rec1_ssse3ext_delta(1, id, ip, nd, size, vv); + raid_rec1_ssse3ext_delta(id, ip, nd, size, vv); } void raid_rec2_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) @@ -2675,7 +2665,7 @@ void raid_rec2_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv return; } - raid_rec2_ssse3ext_delta(2, id, ip, nd, size, vv); + raid_rec2_ssse3ext_delta(id, ip, nd, size, vv); } void raid_rec3_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) From d3e67b2fdfd6731cd639829f56e95f38641a9680 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 15:42:05 +0200 Subject: [PATCH 04/21] Extract common recovery function setup --- raid/test.c | 438 +++++++++++++--------------------------------------- 1 file changed, 106 insertions(+), 332 deletions(-) diff --git a/raid/test.c b/raid/test.c index ef4e907b..04b11177 100644 --- a/raid/test.c +++ b/raid/test.c @@ -348,55 +348,11 @@ int raid_test_poly(unsigned mode) #define test_setup(i) f[i - 1][nf[i - 1]++] -int raid_test_rec(int mode, int nd, size_t size) +static void raid_test_setup_rec(int mode, int np, raid_rec_fn *f[RAID_PARITY_MAX][32], int nf[RAID_PARITY_MAX]) { - void (*f[RAID_PARITY_MAX][32])(int nr, int *id, int *ip, int nd, size_t size, void **vbuf); - void *v_alloc; - void **v; - void **data; - void **parity; - void **test; - void *data_save[RAID_PARITY_MAX]; - void *parity_save[RAID_PARITY_MAX]; - void *waste; - int nv; - int id[RAID_PARITY_MAX]; - int ip[RAID_PARITY_MAX]; int i; - int j; - int nr; - int nf[RAID_PARITY_MAX]; - int np; - raid_mode(mode); - if (mode == RAID_MODE_VANDERMONDE_RAID) - np = 3; - else - np = RAID_PARITY_MAX; - - nv = nd + np * 2 + 2; - - v = raid_malloc_vector(nv, size, &v_alloc); - if (!v) { - /* LCOV_EXCL_START */ - return -1; - /* LCOV_EXCL_STOP */ - } - - data = v; - parity = v + nd; - test = v + nd + np; - - for (i = 0; i < np; ++i) - parity_save[i] = parity[i]; - - memset(v[nv - 2], 0, size); - raid_zero(v[nv - 2]); - - waste = v[nv - 1]; - - /* fill with pseudo-random data with the arbitrary seed "1" */ - raid_mrand_vector(1, nd, size, v); + (void)mode; /* counters of functions for each parity level */ for (i = 0; i < np; ++i) @@ -644,6 +600,60 @@ int raid_test_rec(int mode, int nd, size_t size) } #endif } +} + +int raid_test_rec(int mode, int nd, size_t size) +{ + raid_rec_fn *f[RAID_PARITY_MAX][32]; + void *v_alloc; + void **v; + void **data; + void **parity; + void **test; + void *data_save[RAID_PARITY_MAX]; + void *parity_save[RAID_PARITY_MAX]; + void *waste; + int nv; + int id[RAID_PARITY_MAX]; + int ip[RAID_PARITY_MAX]; + int i; + int j; + int nr; + int nf[RAID_PARITY_MAX]; + int np; + + raid_mode(mode); + if (mode == RAID_MODE_VANDERMONDE_RAID) + np = 3; + else + np = RAID_PARITY_MAX; + + nv = nd + np * 2 + 2; + + v = raid_malloc_vector(nv, size, &v_alloc); + if (!v) { + /* LCOV_EXCL_START */ + return -1; + /* LCOV_EXCL_STOP */ + } + + data = v; + parity = v + nd; + test = v + nd + np; + + for (i = 0; i < np; ++i) + parity_save[i] = parity[i]; + + memset(v[nv - 2], 0, size); + raid_zero(v[nv - 2]); + + waste = v[nv - 1]; + + /* fill with pseudo-random data with the arbitrary seed "1" */ + raid_mrand_vector(1, nd, size, v); + + /* setup recov functions */ + raid_test_setup_rec(mode, np, f, nf); /* compute the parity */ raid_gen_ref(nd, np, size, v); @@ -709,7 +719,7 @@ bail: int raid_test_tail(int mode, int nd_max, size_t size) { - void (*f[RAID_PARITY_MAX][32])(int nr, int *id, int *ip, int nd, size_t size, void **vbuf); + raid_rec_fn *f[RAID_PARITY_MAX][32]; void *v_alloc; void **v; void **data; @@ -758,252 +768,8 @@ int raid_test_tail(int mode, int nd_max, size_t size) /* fill with pseudo-random data with the arbitrary seed "1" */ raid_mrand_vector(1, nd_max, size, v); - /* counters of functions for each parity level */ - for (i = 0; i < np; ++i) - nf[i] = 0; - /* setup recov functions */ - for (i = 1; i <= np; ++i) { - switch (i) { - case 1: - test_setup(i) = raid_rec1_int8; - break; - case 2: - test_setup(i) = raid_rec2_int8; - break; - default: - test_setup(i) = raid_recX_int8; - break; - } -#ifdef CONFIG_X86 - if (raid_cpu_has_ssse3()) { - switch (i) { - case 1: - test_setup(i) = raid_rec1_ssse3; - break; - case 2: - test_setup(i) = raid_rec2_ssse3; - break; - case 3: - test_setup(i) = raid_rec3_ssse3; - break; - case 4: - test_setup(i) = raid_rec4_ssse3; - break; - case 5: - test_setup(i) = raid_rec5_ssse3; - break; - case 6: - test_setup(i) = raid_rec6_ssse3; - break; - } -#ifdef CONFIG_X86_64 - switch (i) { - case 1: - test_setup(i) = raid_rec1_ssse3ext; - break; - case 2: - test_setup(i) = raid_rec2_ssse3ext; - break; - case 3: - test_setup(i) = raid_rec3_ssse3ext; - break; - case 4: - test_setup(i) = raid_rec4_ssse3ext; - break; - case 5: - test_setup(i) = raid_rec5_ssse3ext; - break; - case 6: - test_setup(i) = raid_rec6_ssse3ext; - break; - } -#endif - } - if (raid_cpu_has_avx2()) { - switch (i) { - case 1: - test_setup(i) = raid_rec1_avx2; - break; - case 2: - test_setup(i) = raid_rec2_avx2; - break; - case 3: - test_setup(i) = raid_rec3_avx2; - break; - case 4: - test_setup(i) = raid_rec4_avx2; - break; - case 5: - test_setup(i) = raid_rec5_avx2; - break; - case 6: - test_setup(i) = raid_rec6_avx2; - break; - } - } -#ifdef CONFIG_X86_64 - if (raid_cpu_has_avx512bw()) { - switch (i) { - case 1: - test_setup(i) = raid_rec1_avx512bw; - break; - case 2: - test_setup(i) = raid_rec2_avx512bw; - break; - case 3: - test_setup(i) = raid_rec3_avx512bw; - break; - case 4: - test_setup(i) = raid_rec4_avx512bw; - break; - case 5: - test_setup(i) = raid_rec5_avx512bw; - break; - case 6: - test_setup(i) = raid_rec6_avx512bw; - break; - } - } - if (mode == RAID_MODE_CAUCHY_RAID || mode == RAID_MODE_CAUCHY_AES) { - if (raid_cpu_has_avx2gfni()) { - if (mode == RAID_MODE_CAUCHY_AES) { - switch (i) { - case 1: - test_setup(i) = raid_rec1_avx2gfni_aes; - break; - case 2: - test_setup(i) = raid_rec2_avx2gfni_aes; - break; - case 3: - test_setup(i) = raid_rec3_avx2gfni_aes; - break; - case 4: - test_setup(i) = raid_rec4_avx2gfni_aes; - break; - case 5: - test_setup(i) = raid_rec5_avx2gfni_aes; - break; - case 6: - test_setup(i) = raid_rec6_avx2gfni_aes; - break; - } - } else { - switch (i) { - case 1: - test_setup(i) = raid_rec1_avx2gfni_raid; - break; - case 2: - test_setup(i) = raid_rec2_avx2gfni_raid; - break; - case 3: - test_setup(i) = raid_rec3_avx2gfni_raid; - break; - case 4: - test_setup(i) = raid_rec4_avx2gfni_raid; - break; - case 5: - test_setup(i) = raid_rec5_avx2gfni_raid; - break; - case 6: - test_setup(i) = raid_rec6_avx2gfni_raid; - break; - } - } - } - if (raid_cpu_has_avx512gfni()) { - if (mode == RAID_MODE_CAUCHY_AES) { - switch (i) { - case 1: - test_setup(i) = raid_rec1_avx512gfni_aes; - break; - case 2: - test_setup(i) = raid_rec2_avx512gfni_aes; - break; - case 3: - test_setup(i) = raid_rec3_avx512gfni_aes; - break; - case 4: - test_setup(i) = raid_rec4_avx512gfni_aes; - break; - case 5: - test_setup(i) = raid_rec5_avx512gfni_aes; - break; - case 6: - test_setup(i) = raid_rec6_avx512gfni_aes; - break; - } - } else { - switch (i) { - case 1: - test_setup(i) = raid_rec1_avx512gfni_raid; - break; - case 2: - test_setup(i) = raid_rec2_avx512gfni_raid; - break; - case 3: - test_setup(i) = raid_rec3_avx512gfni_raid; - break; - case 4: - test_setup(i) = raid_rec4_avx512gfni_raid; - break; - case 5: - test_setup(i) = raid_rec5_avx512gfni_raid; - break; - case 6: - test_setup(i) = raid_rec6_avx512gfni_raid; - break; - } - } - } - } -#endif -#endif -#ifdef CONFIG_NEON - switch (i) { - case 1: - test_setup(i) = raid_rec1_neon; - break; - case 2: - test_setup(i) = raid_rec2_neon; - break; - case 3: - test_setup(i) = raid_rec3_neon; - break; - case 4: - test_setup(i) = raid_rec4_neon; - break; - case 5: - test_setup(i) = raid_rec5_neon; - break; - case 6: - test_setup(i) = raid_rec6_neon; - break; - } -#endif -#ifdef CONFIG_NEON32 - switch (i) { - case 1: - test_setup(i) = raid_rec1_neon32; - break; - case 2: - test_setup(i) = raid_rec2_neon32; - break; - case 3: - test_setup(i) = raid_rec3_neon32; - break; - case 4: - test_setup(i) = raid_rec4_neon32; - break; - case 5: - test_setup(i) = raid_rec5_neon32; - break; - case 6: - test_setup(i) = raid_rec6_neon32; - break; - } -#endif - } + raid_test_setup_rec(mode, np, f, nf); /* test all disk counts from 1 to nd_max */ for (nd = 1; nd <= nd_max; ++nd) { @@ -1078,47 +844,9 @@ bail: /* LCOV_EXCL_STOP */ } -int raid_test_par(int mode, int nd, size_t size) +static void raid_test_setup_par(int mode, int np, raid_gen_fn *f[RAID_PARITY_MAX][32], int nf[RAID_PARITY_MAX]) { - void (*f[RAID_PARITY_MAX][32])(int nd, size_t size, void **vbuf); - void *v_alloc; - void **v; - int nv; - int i, j, k; - int nf[RAID_PARITY_MAX]; - int np; - - raid_mode(mode); - if (mode == RAID_MODE_VANDERMONDE_RAID) - np = 3; - else - np = RAID_PARITY_MAX; - - nv = nd + np * 2; - - v = raid_malloc_vector(nv, size, &v_alloc); - if (!v) { - /* LCOV_EXCL_START */ - return -1; - /* LCOV_EXCL_STOP */ - } - - /* check memory */ - if (raid_mtest_vector(nv, size, v) != 0) { - /* LCOV_EXCL_START */ - goto bail; - /* LCOV_EXCL_STOP */ - } - - /* fill with pseudo-random data with the arbitrary seed "2" */ - raid_mrand_vector(2, nv, size, v); - - /* compute the parity */ - raid_gen_ref(nd, np, size, v); - - /* copy in back buffers */ - for (i = 0; i < np; ++i) - memcpy(v[nd + np + i], v[nd + i], size); + int i; /* counters of functions for each parity level */ for (i = 0; i < np; ++i) @@ -1328,6 +1056,52 @@ int raid_test_par(int mode, int nd, size_t size) #endif #endif } +} + +int raid_test_par(int mode, int nd, size_t size) +{ + raid_gen_fn *f[RAID_PARITY_MAX][32]; + void *v_alloc; + void **v; + int nv; + int i, j, k; + int nf[RAID_PARITY_MAX]; + int np; + + raid_mode(mode); + if (mode == RAID_MODE_VANDERMONDE_RAID) + np = 3; + else + np = RAID_PARITY_MAX; + + nv = nd + np * 2; + + v = raid_malloc_vector(nv, size, &v_alloc); + if (!v) { + /* LCOV_EXCL_START */ + return -1; + /* LCOV_EXCL_STOP */ + } + + /* check memory */ + if (raid_mtest_vector(nv, size, v) != 0) { + /* LCOV_EXCL_START */ + goto bail; + /* LCOV_EXCL_STOP */ + } + + /* fill with pseudo-random data with the arbitrary seed "2" */ + raid_mrand_vector(2, nv, size, v); + + /* compute the parity */ + raid_gen_ref(nd, np, size, v); + + /* copy in back buffers */ + for (i = 0; i < np; ++i) + memcpy(v[nd + np + i], v[nd + i], size); + + /* setup parity generation functions */ + raid_test_setup_par(mode, np, f, nf); /* check all the functions */ for (k = 0; k < np; ++k) { From 4d97df38ce346653327a3cd72db0f0108daa1372 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 16:12:04 +0200 Subject: [PATCH 05/21] Optimize recovery with P by deriving the final missing block via XOR --- raid/avx2.c | 210 +++++++++++++--------------- raid/avx512.c | 43 +++++- raid/gfni.c | 54 ++++++- raid/neon.c | 3 +- raid/neon32.c | 6 +- raid/ssse3.c | 380 +++++++++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 550 insertions(+), 146 deletions(-) diff --git a/raid/avx2.c b/raid/avx2.c index acb9336d..86cea670 100644 --- a/raid/avx2.c +++ b/raid/avx2.c @@ -1112,6 +1112,10 @@ static __always_inline void raid_rec2of2_avx2(int *id, int *ip, int nd, size_t s * * Compute all selected syndromes in one scan of the surviving data, then * retain only their low/high nibbles while reconstructing the missing data. + * + * If P is available, keep the complete P delta syndrome in ymm6. + * Reconstruct only nr - 1 missing blocks through the inverse matrix and + * obtain the last block by XORing the reconstructed blocks out of Pdelta. */ static __always_inline void raid_recX_avx2_1234(int nr, int *id, int *ip, int nd, size_t size, void **vv) { @@ -1170,7 +1174,7 @@ static __always_inline void raid_recX_avx2_1234(int nr, int *id, int *ip, int nd BUG_ON(ns != nd - nr); /* precompute inverse-matrix multiplication table pointers */ - for (j = 0; j < nr; ++j) + for (j = 0; j < nr - has_p; ++j) for (k = 0; k < nr; ++k) R[j][k] = &raid_gfmulpshufb[V[j * nr + k]][0][0]; @@ -1240,7 +1244,11 @@ static __always_inline void raid_recX_avx2_1234(int nr, int *id, int *ip, int nd } } - /* preserve only aligned low/high nibbles, never full delta blocks */ + /* preserve the complete P delta before splitting syndrome 0 */ + if (has_p) + asm volatile ("vmovdqa %ymm0,%ymm6"); + + /* preserve only aligned low/high nibbles */ asm volatile ("vpsrlw $4,%ymm0,%ymm4"); asm volatile ("vpand %ymm7,%ymm0,%ymm0"); asm volatile ("vpand %ymm7,%ymm4,%ymm4"); @@ -1271,8 +1279,8 @@ static __always_inline void raid_recX_avx2_1234(int nr, int *id, int *ip, int nd asm volatile ("vmovdqa %%ymm4,%0" : "=m" (pd_high[96])); } - /* reconstruct every missing block, initializing from coefficient zero */ - for (j = 0; j < nr; ++j) { + /* reconstruct all but the last missing block when P is available */ + for (j = 0; j < nr - has_p; ++j) { const uint8_t **t = R[j]; asm volatile ("vbroadcasti128 %0,%%ymm0" : : "m" (t[0][0])); @@ -1294,8 +1302,15 @@ static __always_inline void raid_recX_avx2_1234(int nr, int *id, int *ip, int nd } asm volatile ("vpxor %ymm1,%ymm0,%ymm0"); + + if (has_p) + asm volatile ("vpxor %ymm0,%ymm6,%ymm6"); + asm volatile ("vmovdqa %%ymm0,%0" : "=m" (pa[j][i])); } + + if (has_p) + asm volatile ("vmovdqa %%ymm6,%0" : "=m" (pa[nr - 1][i])); } raid_avx_end(); @@ -1306,6 +1321,10 @@ static __always_inline void raid_recX_avx2_1234(int nr, int *id, int *ip, int nd * * Compute all selected syndromes in a single pass over the surviving data. * Syndrome accumulators are kept in memory to minimize register pressure. + * + * If P is available, keep the complete P delta syndrome in ymm6. + * Reconstruct only nr - 1 missing blocks through the inverse matrix and + * obtain the last block by XORing the reconstructed blocks out of Pdelta. */ static __always_inline void raid_recX_avx2(int nr, int *id, int *ip, int nd, size_t size, void **vv) { @@ -1367,7 +1386,7 @@ static __always_inline void raid_recX_avx2(int nr, int *id, int *ip, int nd, siz BUG_ON(ns != nd - nr); /* precompute inverse-matrix multiplication table pointers */ - for (j = 0; j < nr; ++j) + for (j = 0; j < nr - has_p; ++j) for (k = 0; k < nr; ++k) R[j][k] = &raid_gfmulpshufb[V[j * nr + k]][0][0]; @@ -1442,6 +1461,13 @@ static __always_inline void raid_recX_avx2(int nr, int *id, int *ip, int nd, siz } } + /* + * Preserve the complete P delta before pd_low[0] is + * destructively converted to low/high nibble form. + */ + if (has_p) + asm volatile ("vmovdqa %0,%%ymm6" : : "m" (pd_low[0])); + /* * All survivor reads for this chunk are now complete. * @@ -1458,7 +1484,8 @@ static __always_inline void raid_recX_avx2(int nr, int *id, int *ip, int nd, siz } /* - * Reconstruct each missing block. + * Reconstruct all but the last missing block when P is + * available. * * Keep independent low/high accumulation chains: * @@ -1468,8 +1495,9 @@ static __always_inline void raid_recX_avx2(int nr, int *id, int *ip, int nd, siz * ymm3 = high table/result * ymm4 = syndrome low * ymm5 = syndrome high + * ymm6 = remaining P delta */ - for (j = 0; j < nr; ++j) { + for (j = 0; j < nr - has_p; ++j) { const uint8_t **t = R[j]; /* coefficient 0 initializes the accumulators */ @@ -1497,111 +1525,21 @@ static __always_inline void raid_recX_avx2(int nr, int *id, int *ip, int nd, siz } asm volatile ("vpxor %ymm1,%ymm0,%ymm0"); + + if (has_p) + asm volatile ("vpxor %ymm0,%ymm6,%ymm6"); + asm volatile ("vmovdqa %%ymm0,%0" : "=m" (pa[j][i])); } + + if (has_p) + asm volatile ("vmovdqa %%ymm6,%0" : "=m" (pa[nr - 1][i])); } raid_avx_end(); } #ifdef CONFIG_X86_64 -/* - * RAID recovering for two disks AVX2 extended implementation - */ -static __always_inline void raid_rec2_avx2ext_delta(int *id, int *ip, int nd, size_t size, void **vv) -{ - uint8_t **v = (uint8_t **)vv; - uint8_t *p[2]; - uint8_t *pa[2]; - uint8_t G[2 * 2]; - uint8_t V[2 * 2]; - size_t i; - int j, k; - - /* setup the coefficients matrix */ - for (j = 0; j < 2; ++j) - for (k = 0; k < 2; ++k) - G[j * 2 + k] = A(ip[j], id[k]); - - /* invert it to solve the system of linear equations */ - raid_invert(G, V, 2); - - /* compute delta parity */ - raid_delta_gen(2, id, ip, nd, size, vv); - - for (j = 0; j < 2; ++j) { - p[j] = v[nd + ip[j]]; - pa[j] = v[id[j]]; - } - - raid_avx_begin(); - - asm volatile ("vpbroadcastb %0,%%ymm6" : : "m" (gfconst16.low4[0])); - - /* the inverse matrix V[] is constant for the whole recovery */ - asm volatile ("vbroadcasti128 %0,%%ymm8" : : "m" (raid_gfmulpshufb[V[0]][0][0])); - asm volatile ("vbroadcasti128 %0,%%ymm9" : : "m" (raid_gfmulpshufb[V[0]][1][0])); - asm volatile ("vbroadcasti128 %0,%%ymm10" : : "m" (raid_gfmulpshufb[V[1]][0][0])); - asm volatile ("vbroadcasti128 %0,%%ymm11" : : "m" (raid_gfmulpshufb[V[1]][1][0])); - asm volatile ("vbroadcasti128 %0,%%ymm12" : : "m" (raid_gfmulpshufb[V[2]][0][0])); - asm volatile ("vbroadcasti128 %0,%%ymm13" : : "m" (raid_gfmulpshufb[V[2]][1][0])); - asm volatile ("vbroadcasti128 %0,%%ymm14" : : "m" (raid_gfmulpshufb[V[3]][0][0])); - asm volatile ("vbroadcasti128 %0,%%ymm15" : : "m" (raid_gfmulpshufb[V[3]][1][0])); - - for (i = 0; i < size; i += 32) { - /* d0 = p[0] ^ pa[0] */ - asm volatile ("vmovdqa %0,%%ymm0" : : "m" (p[0][i])); - asm volatile ("vmovdqa %0,%%ymm4" : : "m" (pa[0][i])); - asm volatile ("vpxor %ymm4,%ymm0,%ymm0"); - - /* d1 = p[1] ^ pa[1] */ - asm volatile ("vmovdqa %0,%%ymm1" : : "m" (p[1][i])); - asm volatile ("vmovdqa %0,%%ymm4" : : "m" (pa[1][i])); - asm volatile ("vpxor %ymm4,%ymm1,%ymm1"); - - /* - * Split both deltas into low/high nibbles once. - * - * ymm0 = d0 low - * ymm2 = d0 high - * ymm1 = d1 low - * ymm3 = d1 high - */ - asm volatile ("vpsrlw $4,%ymm0,%ymm2"); - asm volatile ("vpsrlw $4,%ymm1,%ymm3"); - asm volatile ("vpand %ymm6,%ymm0,%ymm0"); - asm volatile ("vpand %ymm6,%ymm2,%ymm2"); - asm volatile ("vpand %ymm6,%ymm1,%ymm1"); - asm volatile ("vpand %ymm6,%ymm3,%ymm3"); - - /* pa[0] = V[0] * d0 ^ V[1] * d1 */ - asm volatile ("vpshufb %ymm0,%ymm8,%ymm4"); - asm volatile ("vpshufb %ymm2,%ymm9,%ymm5"); - asm volatile ("vpxor %ymm5,%ymm4,%ymm4"); - - asm volatile ("vpshufb %ymm1,%ymm10,%ymm5"); - asm volatile ("vpxor %ymm5,%ymm4,%ymm4"); - asm volatile ("vpshufb %ymm3,%ymm11,%ymm5"); - asm volatile ("vpxor %ymm5,%ymm4,%ymm4"); - - asm volatile ("vmovdqa %%ymm4,%0" : "=m" (pa[0][i])); - - /* pa[1] = V[2] * d0 ^ V[3] * d1 */ - asm volatile ("vpshufb %ymm0,%ymm12,%ymm4"); - asm volatile ("vpshufb %ymm2,%ymm13,%ymm5"); - asm volatile ("vpxor %ymm5,%ymm4,%ymm4"); - - asm volatile ("vpshufb %ymm1,%ymm14,%ymm5"); - asm volatile ("vpxor %ymm5,%ymm4,%ymm4"); - asm volatile ("vpshufb %ymm3,%ymm15,%ymm5"); - asm volatile ("vpxor %ymm5,%ymm4,%ymm4"); - - asm volatile ("vmovdqa %%ymm4,%0" : "=m" (pa[1][i])); - } - - raid_avx_end(); -} - /* * RAID recovering for two disks with P and Q AVX2 implementation */ @@ -1978,11 +1916,16 @@ static __always_inline void raid_recX_avx2ext_123(int nr, int *id, int *ip, int } /* - * RAID recovering AVX2 implementation. + * RAID recovering AVX2 extended implementation. * * Compute only the selected syndromes, keeping them in registers. * This avoids raid_delta_gen(), temporary syndrome buffers, and the * generation of unused parity rows. + * + * If P is available, preserve the complete P delta syndrome and + * reconstruct only nr - 1 missing blocks through the inverse matrix. + * The last missing block is obtained by XORing the reconstructed blocks + * out of Pdelta. */ static __always_inline void raid_recX_avx2ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) { @@ -1999,6 +1942,8 @@ static __always_inline void raid_recX_avx2ext(int nr, int *id, int *ip, int nd, int ns; int has_p; + BUG_ON(nr < 1 || nr > RAID_PARITY_MAX); + /* setup the coefficients matrix */ for (j = 0; j < nr; ++j) for (k = 0; k < nr; ++k) @@ -2037,16 +1982,18 @@ static __always_inline void raid_recX_avx2ext(int nr, int *id, int *ip, int nd, BUG_ON(k != nr); BUG_ON(ns != nd - nr); - /* precompute inverse-matrix multiplication table pointers */ - for (j = 0; j < nr; ++j) + /* + * If P is available, the last inverse-matrix row is not needed. + * The corresponding missing block is reconstructed by XOR. + */ + for (j = 0; j < nr - has_p; ++j) for (k = 0; k < nr; ++k) R[j][k] = &raid_gfmulpshufb[V[j * nr + k]][0][0]; raid_avx_begin(); - asm volatile ("vpbroadcastb %0,%%ymm15" : : "m" (gfconst16.low4[0])); - for (i = 0; i < size; i += 32) { + asm volatile ("vpbroadcastb %0,%%ymm15" : : "m" (gfconst16.low4[0])); /* * Start each syndrome with the corresponding stored parity. * @@ -2160,6 +2107,16 @@ static __always_inline void raid_recX_avx2ext(int nr, int *id, int *ip, int nd, } } + /* + * Preserve the complete P delta before splitting syndrome 0. + * + * ymm14 is no longer needed by syndrome generation and is not + * used by the split. After the split, Pdelta is moved to ymm15, + * freeing ymm14 again for reconstruction. + */ + if (has_p) + asm volatile ("vmovdqa %ymm0,%ymm14"); + /* * Split every completed syndrome once. * @@ -2206,8 +2163,30 @@ static __always_inline void raid_recX_avx2ext(int nr, int *id, int *ip, int nd, asm volatile ("vpand %ymm15,%ymm11,%ymm11"); } - /* reconstruct every missing data block */ - for (j = 0; j < nr; ++j) { + /* + * The nibble mask is no longer needed. + * + * Move the complete P delta from the temporary register to + * ymm15, which remains the P accumulator for reconstruction. + * ymm14 is now free again as the multiplication temporary. + */ + if (has_p) + asm volatile ("vmovdqa %ymm14,%ymm15"); + + /* + * Reconstruct the missing data blocks. + * + * If P is available, only nr - 1 outputs are reconstructed + * through the inverse matrix. Each reconstructed output is + * XORed out of ymm15. The remaining value is the final + * missing block. + * + * ymm12 = low accumulator + * ymm13 = high accumulator + * ymm14 = multiplication table / result + * ymm15 = remaining Pdelta, if has_p + */ + for (j = 0; j < nr - has_p; ++j) { const uint8_t **t = R[j]; /* first coefficient initializes the low/high accumulators */ @@ -2256,20 +2235,25 @@ static __always_inline void raid_recX_avx2ext(int nr, int *id, int *ip, int nd, asm volatile ("vbroadcasti128 %0,%%ymm14" : : "m" (t[5][0])); asm volatile ("vpshufb %ymm10,%ymm14,%ymm14"); asm volatile ("vpxor %ymm14,%ymm12,%ymm12"); - asm volatile ("vbroadcasti128 %0,%%ymm14" : : "m" (t[5][16])); asm volatile ("vpshufb %ymm11,%ymm14,%ymm14"); asm volatile ("vpxor %ymm14,%ymm13,%ymm13"); } asm volatile ("vpxor %ymm13,%ymm12,%ymm12"); + + if (has_p) + asm volatile ("vpxor %ymm12,%ymm15,%ymm15"); + asm volatile ("vmovdqa %%ymm12,%0" : "=m" (pa[j][i])); } + + if (has_p) + asm volatile ("vmovdqa %%ymm15,%0" : "=m" (pa[nr - 1][i])); } raid_avx_end(); } - #endif void raid_gen2_avx2_raid(int nd, size_t size, void **vv) diff --git a/raid/avx512.c b/raid/avx512.c index 56d2b86b..cd808353 100644 --- a/raid/avx512.c +++ b/raid/avx512.c @@ -544,10 +544,10 @@ static __always_inline void raid_genX_avx512bw(int nd, size_t size, void **vv, i } /* - * RAID recovering AVX512BW implementation. + * Recover multiple data failures using selected parity blocks with AVX512BW. * - * Compute only the selected syndromes directly from the stored parity - * and surviving data. This avoids raid_delta_gen(), temporary delta + * Compute only the selected syndromes, keeping them in registers. + * This avoids raid_delta_gen(), temporary syndrome buffers, recomputation of * parity blocks, and generation of unused parity rows. * * AVX512 provides enough registers to keep all six supported syndromes @@ -611,8 +611,11 @@ static __always_inline void raid_recX_avx512bw(int nr, int *id, int *ip, int nd, BUG_ON(k != nr); BUG_ON(ns != nd - nr); - /* precompute inverse-matrix multiplication table pointers */ - for (j = 0; j < nr; ++j) + /* + * If P is available, the last missing block is reconstructed + * directly from Pdelta, so the last inverse row isn't needed. + */ + for (j = 0; j < nr - has_p; ++j) for (k = 0; k < nr; ++k) R[j][k] = &raid_gfmulpshufb[V[j * nr + k]][0][0]; @@ -632,6 +635,7 @@ static __always_inline void raid_recX_avx512bw(int nr, int *id, int *ip, int nd, * zmm13 source high nibble * zmm14 low multiplication table / result * zmm15 high multiplication table / result + * zmm30 remaining P delta, if has_p * zmm31 low-nibble mask * * After syndrome splitting: @@ -740,6 +744,13 @@ static __always_inline void raid_recX_avx512bw(int nr, int *id, int *ip, int nd, } } + /* + * Preserve the complete P delta before syndrome 0 is + * destructively split into low/high nibbles. + */ + if (has_p) + asm volatile ("vmovdqa64 %zmm0,%zmm30"); + /* split every completed syndrome once into low/high nibbles */ asm volatile ("vpsrlw $4,%zmm0,%zmm1"); asm volatile ("vpandq %zmm31,%zmm0,%zmm0"); @@ -776,7 +787,11 @@ static __always_inline void raid_recX_avx512bw(int nr, int *id, int *ip, int nd, } /* - * Reconstruct every missing data block. + * Reconstruct the missing data blocks. + * + * If P is available, reconstruct only nr - 1 blocks through + * the inverse matrix. XOR each result out of zmm30, leaving + * the final missing block in zmm30. * * Keep independent low/high dependency chains: * @@ -784,11 +799,12 @@ static __always_inline void raid_recX_avx512bw(int nr, int *id, int *ip, int nd, * zmm13 high accumulator * zmm14 low multiplication table/result * zmm15 high multiplication table/result + * zmm30 remaining P delta, if has_p * * This is intentional. Do not serialize low/high products * through a single temporary register. */ - for (j = 0; j < nr; ++j) { + for (j = 0; j < nr - has_p; ++j) { const uint8_t **t = R[j]; /* coefficient 0 initializes both accumulators */ @@ -842,9 +858,22 @@ static __always_inline void raid_recX_avx512bw(int nr, int *id, int *ip, int nd, asm volatile ("vpxorq %zmm15,%zmm13,%zmm13"); } + /* combine low/high products into the reconstructed block */ asm volatile ("vpxorq %zmm13,%zmm12,%zmm12"); + + /* + * Remove the reconstructed block from Pdelta. + * After nr - 1 iterations zmm30 contains the final + * missing data block. + */ + if (has_p) + asm volatile ("vpxorq %zmm12,%zmm30,%zmm30"); + asm volatile ("vmovdqa64 %%zmm12,%0" : "=m" (pa[j][i])); } + + if (has_p) + asm volatile ("vmovdqa64 %%zmm30,%0" : "=m" (pa[nr - 1][i])); } raid_avx_end(); diff --git a/raid/gfni.c b/raid/gfni.c index f4717630..60d6a3be 100644 --- a/raid/gfni.c +++ b/raid/gfni.c @@ -599,6 +599,16 @@ static __always_inline void raid_recX_avx2gfni_raid(int nr, int *id, int *ip, in raid_avx_end(); } +/* + * Recover multiple data failures using selected parity blocks with AVX512 GFNI. + * + * Compute only the selected syndromes, keeping them in registers. + * This avoids raid_delta_gen(), temporary syndrome buffers, recomputation of + * parity blocks, and generation of unused parity rows. + * + * If P is available, reconstruct only nr - 1 missing blocks through the + * inverse matrix and derive the last missing block from the P delta by XOR. + */ static __always_inline void raid_recX_avx512gfni_raid(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; @@ -649,7 +659,8 @@ static __always_inline void raid_recX_avx512gfni_raid(int nr, int *id, int *ip, BUG_ON(k != nr); BUG_ON(ns != nd - nr); - for (j = 0; j < nr; ++j) + /* the last inverse row isn't needed when P is available */ + for (j = 0; j < nr - has_p; ++j) for (k = 0; k < nr; ++k) R[j][k] = raid_gfaffine_raid[V[j * nr + k]]; @@ -712,7 +723,15 @@ static __always_inline void raid_recX_avx512gfni_raid(int nr, int *id, int *ip, } } - for (j = 0; j < nr; ++j) { + /* preserve the complete P delta */ + if (has_p) + asm volatile ("vmovdqa64 %zmm0,%zmm30"); + + /* + * If P is available, reconstruct only nr - 1 blocks through + * the inverse matrix. The final block remains in zmm30. + */ + for (j = 0; j < nr - has_p; ++j) { const uint8_t **t = R[j]; asm volatile ("vpbroadcastq %0,%%zmm6" : : "m" (t[0][0])); @@ -748,13 +767,19 @@ static __always_inline void raid_recX_avx512gfni_raid(int nr, int *id, int *ip, asm volatile ("vpxorq %zmm8,%zmm7,%zmm7"); } + if (has_p) + asm volatile ("vpxorq %zmm7,%zmm30,%zmm30"); + asm volatile ("vmovdqa64 %%zmm7,%0" : "=m" (pa[j][i])); } + + if (has_p) + asm volatile ("vmovdqa64 %%zmm30,%0" : "=m" (pa[nr - 1][i])); } raid_avx_end(); } - + static __always_inline void raid_recX_avx2gfni_aes(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; @@ -944,6 +969,17 @@ static __always_inline void raid_recX_avx2gfni_aes(int nr, int *id, int *ip, int raid_avx_end(); } +/* + * Recover multiple data failures using selected parity blocks with AVX512 GFNI + * and the AES polynomial. + * + * Compute only the selected syndromes, keeping them in registers. + * This avoids raid_delta_gen(), temporary syndrome buffers, recomputation of + * parity blocks, and generation of unused parity rows. + * + * If P is available, reconstruct only nr - 1 missing blocks through the + * inverse matrix and derive the last missing block from the P delta by XOR. + */ static __always_inline void raid_recX_avx512gfni_aes(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; @@ -1050,7 +1086,11 @@ static __always_inline void raid_recX_avx512gfni_aes(int nr, int *id, int *ip, i } } - for (j = 0; j < nr; ++j) { + /* preserve the complete P delta */ + if (has_p) + asm volatile ("vmovdqa64 %zmm0,%zmm30"); + + for (j = 0; j < nr - has_p; ++j) { asm volatile ("vpbroadcastb %0,%%zmm6" : : "m" (V[j * nr])); asm volatile ("vgf2p8mulb %zmm6,%zmm0,%zmm7"); @@ -1084,8 +1124,14 @@ static __always_inline void raid_recX_avx512gfni_aes(int nr, int *id, int *ip, i asm volatile ("vpxorq %zmm8,%zmm7,%zmm7"); } + if (has_p) + asm volatile ("vpxorq %zmm7,%zmm30,%zmm30"); + asm volatile ("vmovdqa64 %%zmm7,%0" : "=m" (pa[j][i])); } + + if (has_p) + asm volatile ("vmovdqa64 %%zmm30,%0" : "=m" (pa[nr - 1][i])); } raid_avx_end(); diff --git a/raid/neon.c b/raid/neon.c index 419392d7..f7ac4740 100644 --- a/raid/neon.c +++ b/raid/neon.c @@ -566,8 +566,7 @@ static __always_inline void raid_genX_neon(int nd, size_t size, void **vv, int n raid_neon_end(); } -static __always_inline void raid_recX_neon(int nr, int *id, int *ip, - int nd, size_t size, void **vv) +static __always_inline void raid_recX_neon(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p[RAID_PARITY_MAX]; diff --git a/raid/neon32.c b/raid/neon32.c index d551917e..c64e541a 100644 --- a/raid/neon32.c +++ b/raid/neon32.c @@ -518,8 +518,7 @@ static __always_inline void raid_genX_neon32(int nd, size_t size, raid_neon32_end(); } -static __always_inline void raid_recX_neon32_123(int nr, int *id, int *ip, - int nd, size_t size, void **vv) +static __always_inline void raid_recX_neon32_123(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p[RAID_PARITY_MAX]; @@ -816,8 +815,7 @@ static __always_inline void raid_recX_neon32_123(int nr, int *id, int *ip, raid_neon32_end(); } -static __always_inline void raid_recX_neon32(int nr, int *id, int *ip, - int nd, size_t size, void **vv) +static __always_inline void raid_recX_neon32(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p[RAID_PARITY_MAX]; diff --git a/raid/ssse3.c b/raid/ssse3.c index 5384774f..c747d096 100644 --- a/raid/ssse3.c +++ b/raid/ssse3.c @@ -1376,8 +1376,17 @@ static __always_inline void raid_rec2of2_ssse3(int *id, int *ip, int nd, size_t raid_sse_end(); } -static __always_inline void raid_recX_ssse3_1234(int nr, int *id, int *ip, - int nd, size_t size, void **vv) +/* + * RAID recovering SSSE3 implementation optimized for up to four failures. + * + * If P is available, keep the complete P delta syndrome in xmm6. + * Reconstruct only nr - 1 missing blocks through the inverse matrix and + * obtain the last block by XORing the reconstructed blocks out of Pdelta. + * + * After the survivor scan xmm6 is no longer needed, leaving it available + * for the P delta accumulator. + */ +static __always_inline void raid_recX_ssse3_1234(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p[RAID_PARITY_MAX]; @@ -1431,7 +1440,7 @@ static __always_inline void raid_recX_ssse3_1234(int nr, int *id, int *ip, BUG_ON(k != nr); BUG_ON(ns != nd - nr); - for (j = 0; j < nr; ++j) + for (j = 0; j < nr - has_p; ++j) for (k = 0; k < nr; ++k) R[j][k] = &raid_gfmulpshufb[V[j * nr + k]][0][0]; @@ -1505,6 +1514,10 @@ static __always_inline void raid_recX_ssse3_1234(int nr, int *id, int *ip, } } + /* preserve the complete P delta before splitting syndrome 0 */ + if (has_p) + asm volatile ("movdqa %xmm0,%xmm6"); + /* * Split completed syndromes once and store the low/high nibbles. * The survivor scan is already complete. @@ -1548,7 +1561,11 @@ static __always_inline void raid_recX_ssse3_1234(int nr, int *id, int *ip, asm volatile ("movdqa %%xmm5,%0" : "=m" (pd_high[48])); } - for (j = 0; j < nr; ++j) { + /* + * Reconstruct all but the last missing block when P is + * available. xmm6 keeps the remaining P delta. + */ + for (j = 0; j < nr - has_p; ++j) { const uint8_t **t = R[j]; asm volatile ("movdqa %0,%%xmm0" : : "m" (t[0][0])); @@ -1570,15 +1587,31 @@ static __always_inline void raid_recX_ssse3_1234(int nr, int *id, int *ip, } asm volatile ("pxor %xmm1,%xmm0"); + + if (has_p) + asm volatile ("pxor %xmm0,%xmm6"); + asm volatile ("movdqa %%xmm0,%0" : "=m" (pa[j][i])); } + + if (has_p) + asm volatile ("movdqa %%xmm6,%0" : "=m" (pa[nr - 1][i])); } raid_sse_end(); } -static __always_inline void raid_recX_ssse3(int nr, int *id, int *ip, - int nd, size_t size, void **vv) +/* + * RAID recovering SSSE3 implementation. + * + * If P is available, keep the complete P delta syndrome in xmm6. + * Reconstruct only nr - 1 missing blocks through the inverse matrix and + * obtain the last block by XORing the reconstructed blocks out of Pdelta. + * + * The completed syndromes are kept in temporary memory, leaving xmm6 + * available for the P delta accumulator during reconstruction. + */ +static __always_inline void raid_recX_ssse3(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p[RAID_PARITY_MAX]; @@ -1632,7 +1665,7 @@ static __always_inline void raid_recX_ssse3(int nr, int *id, int *ip, BUG_ON(k != nr); BUG_ON(ns != nd - nr); - for (j = 0; j < nr; ++j) + for (j = 0; j < nr - has_p; ++j) for (k = 0; k < nr; ++k) R[j][k] = &raid_gfmulpshufb[V[j * nr + k]][0][0]; @@ -1676,6 +1709,13 @@ static __always_inline void raid_recX_ssse3(int nr, int *id, int *ip, } } + /* + * Preserve the complete P delta before pd_low[0] is converted + * to its low-nibble representation. + */ + if (has_p) + asm volatile ("movdqa %0,%%xmm6" : : "m" (pd_low[0])); + /* convert all completed raw syndromes to low/high nibble form */ for (k = 0; k < nr; ++k) { asm volatile ("movdqa %0,%%xmm0" : : "m" (pd_low[k * 16])); @@ -1687,8 +1727,11 @@ static __always_inline void raid_recX_ssse3(int nr, int *id, int *ip, asm volatile ("movdqa %%xmm1,%0" : "=m" (pd_high[k * 16])); } - /* reconstruct */ - for (j = 0; j < nr; ++j) { + /* + * Reconstruct all but the last missing block when P is + * available. xmm6 keeps the remaining P delta. + */ + for (j = 0; j < nr - has_p; ++j) { const uint8_t **t = R[j]; asm volatile ("movdqa %0,%%xmm0" : : "m" (t[0][0])); @@ -1710,8 +1753,15 @@ static __always_inline void raid_recX_ssse3(int nr, int *id, int *ip, } asm volatile ("pxor %xmm1,%xmm0"); + + if (has_p) + asm volatile ("pxor %xmm0,%xmm6"); + asm volatile ("movdqa %%xmm0,%0" : "=m" (pa[j][i])); } + + if (has_p) + asm volatile ("movdqa %%xmm6,%0" : "=m" (pa[nr - 1][i])); } raid_sse_end(); @@ -2006,8 +2056,7 @@ static __always_inline void raid_rec2of2_ssse3ext(int *id, int *ip, int nd, size raid_sse_end(); } -static __always_inline void raid_recX_ssse3ext_123(int nr, int *id, int *ip, - int nd, size_t size, void **vv) +static __always_inline void raid_recX_ssse3ext_123(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p[RAID_PARITY_MAX]; @@ -2256,8 +2305,308 @@ static __always_inline void raid_recX_ssse3ext_123(int nr, int *id, int *ip, raid_sse_end(); } -static __always_inline void raid_recX_ssse3ext(int nr, int *id, int *ip, - int nd, size_t size, void **vv) +/* + * RAID recovering SSSE3 extended implementation optimized for up to five + * failures. + * + * If P is available, keep the complete P delta syndrome in xmm10. + * Reconstruct only nr - 1 missing blocks through the inverse matrix and + * obtain the last block by XORing the reconstructed blocks out of Pdelta. + * + * With at most five failures the low/high syndrome pairs use xmm0..xmm9, + * leaving xmm10 available for the P delta accumulator. + */ +static __always_inline void raid_recX_ssse3ext_12345(int nr, int *id, int *ip, int nd, size_t size, void **vv) +{ + uint8_t **v = (uint8_t **)vv; + uint8_t *p[RAID_PARITY_MAX]; + uint8_t *pa[RAID_PARITY_MAX]; + uint8_t *src[RAID_DATA_MAX]; + uint8_t G[RAID_PARITY_MAX * RAID_PARITY_MAX]; + uint8_t V[RAID_PARITY_MAX * RAID_PARITY_MAX]; + const uint8_t *S[RAID_DATA_MAX][RAID_PARITY_MAX]; + const uint8_t *R[RAID_PARITY_MAX][RAID_PARITY_MAX]; + size_t i; + int d, j, k, s; + int ns; + int has_p; + + BUG_ON(nr < 1 || nr > 5); + + for (j = 0; j < nr; ++j) + for (k = 0; k < nr; ++k) + G[j * nr + k] = A(ip[j], id[k]); + + raid_invert(G, V, nr); + + for (j = 0; j < nr; ++j) { + p[j] = v[nd + ip[j]]; + pa[j] = v[id[j]]; + } + + /* ip[] is ordered. If P is available, it is always ip[0] */ + has_p = ip[0] == 0; + + ns = 0; + k = 0; + + for (d = 0; d < nd; ++d) { + if (k < nr && d == id[k]) { + ++k; + continue; + } + + src[ns] = v[d]; + + for (j = 0; j < nr; ++j) + S[ns][j] = &raid_gfmulpshufb[A(ip[j], d)][0][0]; + + ++ns; + } + + BUG_ON(k != nr); + BUG_ON(ns != nd - nr); + + /* + * If P is available the last inverse-matrix row isn't needed. + * The corresponding missing block is obtained by XOR. + */ + for (j = 0; j < nr - has_p; ++j) + for (k = 0; k < nr; ++k) + R[j][k] = &raid_gfmulpshufb[V[j * nr + k]][0][0]; + + raid_sse_begin(); + + for (i = 0; i < size; i += 16) { + /* + * xmm15 is reused by reconstruction, therefore reload the + * nibble mask on every iteration. + */ + asm volatile ("movdqa %0,%%xmm15" : : "m" (gfconst16.low4[0])); + + /* + * Raw syndrome accumulators: + * + * xmm0 syndrome 0 + * xmm1 syndrome 1 + * xmm2 syndrome 2 + * xmm3 syndrome 3 + * xmm4 syndrome 4 + * + * xmm10 remains free for Pdelta. + */ + asm volatile ("movdqa %0,%%xmm0" : : "m" (p[0][i])); + + if (nr >= 2) + asm volatile ("movdqa %0,%%xmm1" : : "m" (p[1][i])); + + if (nr >= 3) + asm volatile ("movdqa %0,%%xmm2" : : "m" (p[2][i])); + + if (nr >= 4) + asm volatile ("movdqa %0,%%xmm3" : : "m" (p[3][i])); + + if (nr >= 5) + asm volatile ("movdqa %0,%%xmm4" : : "m" (p[4][i])); + + /* add all surviving data contributions */ + for (s = 0; s < ns; ++s) { + const uint8_t **t = S[s]; + + asm volatile ("movdqa %0,%%xmm6" : : "m" (src[s][i])); + + /* + * P has coefficient 1 for every data disk. + * XOR the original source before splitting it. + */ + if (has_p) { + asm volatile ("pxor %xmm6,%xmm0"); + + asm volatile ("movdqa %xmm6,%xmm7"); + asm volatile ("psrlw $4,%xmm7"); + asm volatile ("pand %xmm15,%xmm6"); + asm volatile ("pand %xmm15,%xmm7"); + } else { + asm volatile ("movdqa %xmm6,%xmm7"); + asm volatile ("psrlw $4,%xmm7"); + asm volatile ("pand %xmm15,%xmm6"); + asm volatile ("pand %xmm15,%xmm7"); + + asm volatile ("movdqa %0,%%xmm8" : : "m" (t[0][0])); + asm volatile ("movdqa %0,%%xmm9" : : "m" (t[0][16])); + asm volatile ("pshufb %xmm6,%xmm8"); + asm volatile ("pshufb %xmm7,%xmm9"); + asm volatile ("pxor %xmm8,%xmm0"); + asm volatile ("pxor %xmm9,%xmm0"); + } + + if (nr >= 2) { + asm volatile ("movdqa %0,%%xmm8" : : "m" (t[1][0])); + asm volatile ("movdqa %0,%%xmm9" : : "m" (t[1][16])); + asm volatile ("pshufb %xmm6,%xmm8"); + asm volatile ("pshufb %xmm7,%xmm9"); + asm volatile ("pxor %xmm8,%xmm1"); + asm volatile ("pxor %xmm9,%xmm1"); + } + + if (nr >= 3) { + asm volatile ("movdqa %0,%%xmm8" : : "m" (t[2][0])); + asm volatile ("movdqa %0,%%xmm9" : : "m" (t[2][16])); + asm volatile ("pshufb %xmm6,%xmm8"); + asm volatile ("pshufb %xmm7,%xmm9"); + asm volatile ("pxor %xmm8,%xmm2"); + asm volatile ("pxor %xmm9,%xmm2"); + } + + if (nr >= 4) { + asm volatile ("movdqa %0,%%xmm8" : : "m" (t[3][0])); + asm volatile ("movdqa %0,%%xmm9" : : "m" (t[3][16])); + asm volatile ("pshufb %xmm6,%xmm8"); + asm volatile ("pshufb %xmm7,%xmm9"); + asm volatile ("pxor %xmm8,%xmm3"); + asm volatile ("pxor %xmm9,%xmm3"); + } + + if (nr >= 5) { + asm volatile ("movdqa %0,%%xmm8" : : "m" (t[4][0])); + asm volatile ("movdqa %0,%%xmm9" : : "m" (t[4][16])); + asm volatile ("pshufb %xmm6,%xmm8"); + asm volatile ("pshufb %xmm7,%xmm9"); + asm volatile ("pxor %xmm8,%xmm4"); + asm volatile ("pxor %xmm9,%xmm4"); + } + } + + /* + * Preserve the complete P delta before splitting syndrome 0. + * + * xmm10 = M0 ^ M1 ^ ... ^ M(nr-1) + */ + if (has_p) + asm volatile ("movdqa %xmm0,%xmm10"); + + /* + * Expand backwards to low/high syndrome pairs: + * + * xmm0/xmm1 syndrome 0 low/high + * xmm2/xmm3 syndrome 1 low/high + * xmm4/xmm5 syndrome 2 low/high + * xmm6/xmm7 syndrome 3 low/high + * xmm8/xmm9 syndrome 4 low/high + * + * xmm10 remains the complete remaining P delta. + */ + if (nr >= 5) { + asm volatile ("movdqa %xmm4,%xmm8"); + asm volatile ("movdqa %xmm4,%xmm9"); + asm volatile ("psrlw $4,%xmm9"); + asm volatile ("pand %xmm15,%xmm8"); + asm volatile ("pand %xmm15,%xmm9"); + } + + if (nr >= 4) { + asm volatile ("movdqa %xmm3,%xmm6"); + asm volatile ("movdqa %xmm3,%xmm7"); + asm volatile ("psrlw $4,%xmm7"); + asm volatile ("pand %xmm15,%xmm6"); + asm volatile ("pand %xmm15,%xmm7"); + } + + if (nr >= 3) { + asm volatile ("movdqa %xmm2,%xmm4"); + asm volatile ("movdqa %xmm2,%xmm5"); + asm volatile ("psrlw $4,%xmm5"); + asm volatile ("pand %xmm15,%xmm4"); + asm volatile ("pand %xmm15,%xmm5"); + } + + if (nr >= 2) { + asm volatile ("movdqa %xmm1,%xmm2"); + asm volatile ("movdqa %xmm1,%xmm3"); + asm volatile ("psrlw $4,%xmm3"); + asm volatile ("pand %xmm15,%xmm2"); + asm volatile ("pand %xmm15,%xmm3"); + } + + asm volatile ("movdqa %xmm0,%xmm1"); + asm volatile ("psrlw $4,%xmm1"); + asm volatile ("pand %xmm15,%xmm0"); + asm volatile ("pand %xmm15,%xmm1"); + + /* + * Reconstruct all but the last missing block when P is + * available. + * + * xmm12/xmm13 = low/high output accumulators + * xmm14/xmm15 = multiplication temporaries + * xmm10 = remaining P delta + */ + for (j = 0; j < nr - has_p; ++j) { + const uint8_t **t = R[j]; + + /* coefficient 0 initializes both accumulators */ + asm volatile ("movdqa %0,%%xmm12" : : "m" (t[0][0])); + asm volatile ("movdqa %0,%%xmm13" : : "m" (t[0][16])); + asm volatile ("pshufb %xmm0,%xmm12"); + asm volatile ("pshufb %xmm1,%xmm13"); + + if (nr >= 2) { + asm volatile ("movdqa %0,%%xmm14" : : "m" (t[1][0])); + asm volatile ("movdqa %0,%%xmm15" : : "m" (t[1][16])); + asm volatile ("pshufb %xmm2,%xmm14"); + asm volatile ("pshufb %xmm3,%xmm15"); + asm volatile ("pxor %xmm14,%xmm12"); + asm volatile ("pxor %xmm15,%xmm13"); + } + + if (nr >= 3) { + asm volatile ("movdqa %0,%%xmm14" : : "m" (t[2][0])); + asm volatile ("movdqa %0,%%xmm15" : : "m" (t[2][16])); + asm volatile ("pshufb %xmm4,%xmm14"); + asm volatile ("pshufb %xmm5,%xmm15"); + asm volatile ("pxor %xmm14,%xmm12"); + asm volatile ("pxor %xmm15,%xmm13"); + } + + if (nr >= 4) { + asm volatile ("movdqa %0,%%xmm14" : : "m" (t[3][0])); + asm volatile ("movdqa %0,%%xmm15" : : "m" (t[3][16])); + asm volatile ("pshufb %xmm6,%xmm14"); + asm volatile ("pshufb %xmm7,%xmm15"); + asm volatile ("pxor %xmm14,%xmm12"); + asm volatile ("pxor %xmm15,%xmm13"); + } + + if (nr >= 5) { + asm volatile ("movdqa %0,%%xmm14" : : "m" (t[4][0])); + asm volatile ("movdqa %0,%%xmm15" : : "m" (t[4][16])); + asm volatile ("pshufb %xmm8,%xmm14"); + asm volatile ("pshufb %xmm9,%xmm15"); + asm volatile ("pxor %xmm14,%xmm12"); + asm volatile ("pxor %xmm15,%xmm13"); + } + + asm volatile ("pxor %xmm13,%xmm12"); + + /* + * Remove the reconstructed block from Pdelta. + * After nr - 1 iterations xmm10 contains the final + * missing block. + */ + if (has_p) + asm volatile ("pxor %xmm12,%xmm10"); + + asm volatile ("movdqa %%xmm12,%0" : "=m" (pa[j][i])); + } + + if (has_p) + asm volatile ("movdqa %%xmm10,%0" : "=m" (pa[nr - 1][i])); + } + + raid_sse_end(); +} + +static __always_inline void raid_recX_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; uint8_t *p[RAID_PARITY_MAX]; @@ -2677,13 +3026,13 @@ void raid_rec3_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv void raid_rec4_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 4); - raid_recX_ssse3ext(4, id, ip, nd, size, vv); + raid_recX_ssse3ext_12345(4, id, ip, nd, size, vv); } void raid_rec5_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) { BUG_ON(nr != 5); - raid_recX_ssse3ext(5, id, ip, nd, size, vv); + raid_recX_ssse3ext_12345(5, id, ip, nd, size, vv); } void raid_rec6_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv) @@ -2691,7 +3040,6 @@ void raid_rec6_ssse3ext(int nr, int *id, int *ip, int nd, size_t size, void **vv BUG_ON(nr != 6); raid_recX_ssse3ext(6, id, ip, nd, size, vv); } - #endif void raid_register_ssse3(void) From 048d0c14b138ceb6026f63f01b88a832317d2ee4 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 17:03:52 +0200 Subject: [PATCH 06/21] Make TEST_COUNT and TEST_SIZE parametric in raid fulltest and test Makefile --- raid/test/Makefile | 17 +++++++++----- raid/test/fulltest.c | 56 ++++++++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/raid/test/Makefile b/raid/test/Makefile index c4f29947..0471ad3f 100644 --- a/raid/test/Makefile +++ b/raid/test/Makefile @@ -64,11 +64,16 @@ mktables: mktables.o tables.c: mktables ./mktables > tables.c +TEST_COUNT ?= 32 +TEST_SIZE ?= 256 +SDE_TEST_COUNT ?= 8 +SDE_TEST_SIZE ?= 256 + # Use this target to run a complete test test: ./selftest ./speedtest - ./fulltest + ./fulltest $(TEST_COUNT) $(TEST_SIZE) # Use this target to run a coverage test using lcov covtest: @@ -86,10 +91,10 @@ sdecovtest: $(MAKE) clean $(MAKE) lcov_reset $(MAKE) COVERAGE=1 all - $(SDE) -p4p -- ./fulltest - $(SDE) -mrm -- ./fulltest - $(SDE) -nhm -- ./fulltest - $(SDE) -hsw -- ./fulltest + $(SDE) -p4p -- ./fulltest $(SDE_TEST_COUNT) $(SDE_TEST_SIZE) + $(SDE) -mrm -- ./fulltest $(SDE_TEST_COUNT) $(SDE_TEST_SIZE) + $(SDE) -nhm -- ./fulltest $(SDE_TEST_COUNT) $(SDE_TEST_SIZE) + $(SDE) -hsw -- ./fulltest $(SDE_TEST_COUNT) $(SDE_TEST_SIZE) $(SDE) -p4p -- ./selftest $(SDE) -mrm -- ./selftest $(SDE) -nhm -- ./selftest @@ -109,7 +114,7 @@ sdetest: $(SDE) -hsw -- ./selftest $(SDE) -skx -- ./selftest $(SDE) -icl -- ./selftest - $(SDE) -icl -- ./fulltest + $(SDE) -icl -- ./fulltest $(SDE_TEST_COUNT) $(SDE_TEST_SIZE) # Use this target to run a speed test on Ice Lake simulator sdespeed: diff --git a/raid/test/fulltest.c b/raid/test/fulltest.c index c1611359..011dcaf2 100644 --- a/raid/test/fulltest.c +++ b/raid/test/fulltest.c @@ -21,11 +21,32 @@ #ifdef COVERAGE #define TEST_COUNT 10 #else -#define TEST_COUNT 32 +#define TEST_COUNT 24 #endif -int main(void) +int main(int argc, char *argv[]) { + int test_count = TEST_COUNT; + int test_size = TEST_SIZE; + + if (argc > 1) { + test_count = atoi(argv[1]); + if (test_count < RAID_PARITY_MAX || test_count > RAID_DATA_MAX) { + printf("Invalid TEST_COUNT %d (must be between %d and %d)\n", + test_count, RAID_PARITY_MAX, RAID_DATA_MAX); + return EXIT_FAILURE; + } + } + + if (argc > 2) { + test_size = atoi(argv[2]); + if (test_size <= 0 || (test_size % 64) != 0) { + printf("Invalid TEST_SIZE %d (must be positive and multiple of 64)\n", + test_size); + return EXIT_FAILURE; + } + } + printf("Full sanity test for the RAID Cauchy library\n\n"); raid_init(); @@ -53,7 +74,10 @@ int main(void) #endif #endif - printf("\nPlease wait about 60 seconds...\n\n"); + if (test_count >= 32) + printf("\nPlease wait about 60 seconds...\n\n"); + else + printf("\nPlease wait...\n\n"); printf("Test sorting...\n"); if (raid_test_sort() != 0) { @@ -84,7 +108,7 @@ int main(void) } printf("Test Vandermonde RAID parity generation with %u data disks...\n", RAID_DATA_MAX); - if (raid_test_par(RAID_MODE_VANDERMONDE_RAID, RAID_DATA_MAX, TEST_SIZE) != 0) { + if (raid_test_par(RAID_MODE_VANDERMONDE_RAID, RAID_DATA_MAX, test_size) != 0) { /* LCOV_EXCL_START */ goto bail; /* LCOV_EXCL_STOP */ @@ -92,7 +116,7 @@ int main(void) printf("Test Cauchy RAID parity generation with 1-%u data disks...\n", RAID_DATA_MAX); for (int i = 1; i <= RAID_DATA_MAX; ++i) { - if (raid_test_par(RAID_MODE_CAUCHY_RAID, i, TEST_SIZE) != 0) { + if (raid_test_par(RAID_MODE_CAUCHY_RAID, i, test_size) != 0) { /* LCOV_EXCL_START */ goto bail; /* LCOV_EXCL_STOP */ @@ -100,7 +124,7 @@ int main(void) } printf("Test Cauchy AES parity generation with 1-%u data disks...\n", RAID_DATA_MAX); for (int i = 1; i <= RAID_DATA_MAX; ++i) { - if (raid_test_par(RAID_MODE_CAUCHY_AES, i, TEST_SIZE) != 0) { + if (raid_test_par(RAID_MODE_CAUCHY_AES, i, test_size) != 0) { /* LCOV_EXCL_START */ goto bail; /* LCOV_EXCL_STOP */ @@ -108,44 +132,42 @@ int main(void) } printf("Test Vandermonde RAID tail recovering with 1-%u data disks...\n", RAID_DATA_MAX); - if (raid_test_tail(RAID_MODE_VANDERMONDE_RAID, RAID_DATA_MAX, TEST_SIZE) != 0) { + if (raid_test_tail(RAID_MODE_VANDERMONDE_RAID, RAID_DATA_MAX, test_size) != 0) { /* LCOV_EXCL_START */ goto bail; /* LCOV_EXCL_STOP */ } printf("Test Cauchy RAID tail recovering with 1-%u data disks...\n", RAID_DATA_MAX); - if (raid_test_tail(RAID_MODE_CAUCHY_RAID, RAID_DATA_MAX, TEST_SIZE) != 0) { + if (raid_test_tail(RAID_MODE_CAUCHY_RAID, RAID_DATA_MAX, test_size) != 0) { /* LCOV_EXCL_START */ goto bail; /* LCOV_EXCL_STOP */ } printf("Test Cauchy AES tail recovering with 1-%u data disks...\n", RAID_DATA_MAX); - if (raid_test_tail(RAID_MODE_CAUCHY_AES, RAID_DATA_MAX, TEST_SIZE) != 0) { + if (raid_test_tail(RAID_MODE_CAUCHY_AES, RAID_DATA_MAX, test_size) != 0) { /* LCOV_EXCL_START */ goto bail; /* LCOV_EXCL_STOP */ } - printf("Test Vandermonde RAID recovering with all combinations of %u data and 3 parity blocks...\n", TEST_COUNT); - if (raid_test_rec(RAID_MODE_VANDERMONDE_RAID, TEST_COUNT, TEST_SIZE) != 0) { + printf("Test Vandermonde RAID recovering with all combinations of %u data and 3 parity blocks...\n", test_count); + if (raid_test_rec(RAID_MODE_VANDERMONDE_RAID, test_count, test_size) != 0) { /* LCOV_EXCL_START */ goto bail; /* LCOV_EXCL_STOP */ } - - printf("Test Cauchy RAID recovering with all combinations of %u data and 6 parity blocks...\n", TEST_COUNT); - if (raid_test_rec(RAID_MODE_CAUCHY_RAID, TEST_COUNT, TEST_SIZE) != 0) { + printf("Test Cauchy RAID recovering with all combinations of %u data and 6 parity blocks...\n", test_count); + if (raid_test_rec(RAID_MODE_CAUCHY_RAID, test_count, test_size) != 0) { /* LCOV_EXCL_START */ goto bail; /* LCOV_EXCL_STOP */ } - - printf("Test Cauchy AES recovering with all combinations of %u data and 6 parity blocks...\n", TEST_COUNT); - if (raid_test_rec(RAID_MODE_CAUCHY_AES, TEST_COUNT, TEST_SIZE) != 0) { + printf("Test Cauchy AES recovering with all combinations of %u data and 6 parity blocks...\n", test_count); + if (raid_test_rec(RAID_MODE_CAUCHY_AES, test_count, test_size) != 0) { /* LCOV_EXCL_START */ goto bail; /* LCOV_EXCL_STOP */ From 3f4d7e15fa9ba2d32ef35098086a11d35ed828de Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 17:20:15 +0200 Subject: [PATCH 07/21] Reduce SSSE3ext REC1 unrolling from 64 to 32 bytes for better performance --- raid/ssse3.c | 48 ++++++++++++++---------------------------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/raid/ssse3.c b/raid/ssse3.c index c747d096..464257ca 100644 --- a/raid/ssse3.c +++ b/raid/ssse3.c @@ -1769,7 +1769,13 @@ static __always_inline void raid_recX_ssse3(int nr, int *id, int *ip, int nd, si #ifdef CONFIG_X86_64 /* - * RAID recovering for one disk SSSE3 extended implementation + * RAID recovering for one disk SSSE3 extended implementation. + * + * Process two 16-byte blocks per iteration, grouping equal operations across + * both lanes to expose instruction-level parallelism. + * + * A four-way 64-byte unroll was tested but is slower than this two-way 32-byte + * implementation. */ static __always_inline void raid_rec1_ssse3ext_delta(int *id, int *ip, int nd, size_t size, void **vv) { @@ -1798,71 +1804,45 @@ static __always_inline void raid_rec1_ssse3ext_delta(int *id, int *ip, int nd, s asm volatile ("movdqa %0,%%xmm4" : : "m" (raid_gfmulpshufb[V][0][0])); asm volatile ("movdqa %0,%%xmm5" : : "m" (raid_gfmulpshufb[V][1][0])); - for (i = 0; i < size; i += 64) { + for (i = 0; i < size; i += 32) { asm volatile ("movdqa %0,%%xmm0" : : "m" (p[i])); asm volatile ("movdqa %0,%%xmm1" : : "m" (p[i + 16])); - asm volatile ("movdqa %0,%%xmm2" : : "m" (p[i + 32])); - asm volatile ("movdqa %0,%%xmm3" : : "m" (p[i + 48])); asm volatile ("movdqa %0,%%xmm8" : : "m" (pa[i])); asm volatile ("movdqa %0,%%xmm9" : : "m" (pa[i + 16])); - asm volatile ("movdqa %0,%%xmm10" : : "m" (pa[i + 32])); - asm volatile ("movdqa %0,%%xmm11" : : "m" (pa[i + 48])); asm volatile ("pxor %xmm8,%xmm0"); asm volatile ("pxor %xmm9,%xmm1"); - asm volatile ("pxor %xmm10,%xmm2"); - asm volatile ("pxor %xmm11,%xmm3"); asm volatile ("movdqa %xmm0,%xmm8"); asm volatile ("movdqa %xmm1,%xmm9"); - asm volatile ("movdqa %xmm2,%xmm10"); - asm volatile ("movdqa %xmm3,%xmm11"); asm volatile ("psrlw $4,%xmm8"); asm volatile ("psrlw $4,%xmm9"); - asm volatile ("psrlw $4,%xmm10"); - asm volatile ("psrlw $4,%xmm11"); asm volatile ("pand %xmm7,%xmm0"); asm volatile ("pand %xmm7,%xmm1"); - asm volatile ("pand %xmm7,%xmm2"); - asm volatile ("pand %xmm7,%xmm3"); asm volatile ("pand %xmm7,%xmm8"); asm volatile ("pand %xmm7,%xmm9"); - asm volatile ("pand %xmm7,%xmm10"); - asm volatile ("pand %xmm7,%xmm11"); - asm volatile ("movdqa %xmm4,%xmm12"); - asm volatile ("movdqa %xmm4,%xmm13"); - asm volatile ("movdqa %xmm4,%xmm14"); - asm volatile ("movdqa %xmm4,%xmm15"); + asm volatile ("movdqa %xmm4,%xmm10"); + asm volatile ("movdqa %xmm4,%xmm11"); - asm volatile ("pshufb %xmm0,%xmm12"); - asm volatile ("pshufb %xmm1,%xmm13"); - asm volatile ("pshufb %xmm2,%xmm14"); - asm volatile ("pshufb %xmm3,%xmm15"); + asm volatile ("pshufb %xmm0,%xmm10"); + asm volatile ("pshufb %xmm1,%xmm11"); asm volatile ("movdqa %xmm5,%xmm0"); asm volatile ("movdqa %xmm5,%xmm1"); - asm volatile ("movdqa %xmm5,%xmm2"); - asm volatile ("movdqa %xmm5,%xmm3"); asm volatile ("pshufb %xmm8,%xmm0"); asm volatile ("pshufb %xmm9,%xmm1"); - asm volatile ("pshufb %xmm10,%xmm2"); - asm volatile ("pshufb %xmm11,%xmm3"); - asm volatile ("pxor %xmm12,%xmm0"); - asm volatile ("pxor %xmm13,%xmm1"); - asm volatile ("pxor %xmm14,%xmm2"); - asm volatile ("pxor %xmm15,%xmm3"); + asm volatile ("pxor %xmm10,%xmm0"); + asm volatile ("pxor %xmm11,%xmm1"); asm volatile ("movdqa %%xmm0,%0" : "=m" (pa[i])); asm volatile ("movdqa %%xmm1,%0" : "=m" (pa[i + 16])); - asm volatile ("movdqa %%xmm2,%0" : "=m" (pa[i + 32])); - asm volatile ("movdqa %%xmm3,%0" : "=m" (pa[i + 48])); } raid_sse_end(); From 0ebe2e1e5ee3d71239a2d63d7f8f64b8cb500cac Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Fri, 21 Aug 2026 11:23:36 +0200 Subject: [PATCH 08/21] Add OTHERS --- raid/OTHERS | 1111 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1111 insertions(+) create mode 100644 raid/OTHERS diff --git a/raid/OTHERS b/raid/OTHERS new file mode 100644 index 00000000..d6adc13c --- /dev/null +++ b/raid/OTHERS @@ -0,0 +1,1111 @@ +SnapRAID RAID engine comparison notes +===================================== + + +Scope +----- + +These notes compare the SnapRAID RAID engine with other RAID and erasure-coding +implementations that are useful optimization references. The goal is not to +rank projects by a single benchmark. The goal is to understand which design +choices follow from each workload and which techniques can or cannot be +transferred to SnapRAID. + +The most important rule is the same one used in NOTES: optimize the real +SnapRAID workload, not only isolated inner loops. A technique that reduces +instruction count can still be a loss if it adds another pass over a 256 KiB +block, increases cache footprint, or changes the lifetime of parity data in an +unfavorable way. + +Three different kinds of advantage are distinguished throughout this document: + +* Algorithmic advantage: the coding or recovery algorithm fundamentally + requires less work. + +* Implementation advantage: the same mathematical work is scheduled, + vectorized, or dispatched more efficiently. + +* Workload-specific advantage: a design is faster because its assumptions fit + one application, but the same design may be unsuitable for another + application. + +The third category is especially important. Linux RAID6, OpenZFS RAIDZ, ISA-L, +klauspost/reedsolomon, Jerasure/GF-Complete, and CM256/gf256 do not all +optimize the same problem. + + +Source snapshots +---------------- + +The comparison below was prepared from these source snapshots. Commit hashes +are recorded so that later changes in the external projects do not silently +change the meaning of the notes. + + Linux kernel torvalds/linux + 818bebeb63dd6bf5f4e07e145f6cdbace520a34c + + OpenZFS openzfs/zfs + 7e1cb1374b7156ca1fd1fb6379f899e83c8687ea + + Intel ISA-L intel/isa-l + 8b7e3b28a2b7b816483d3f9e80162e11cebeaa70 + + klauspost/reedsolomon klauspost/reedsolomon + af9e2b1b1bad1889954523347758996aafd9c805 + + Jerasure ceph/jerasure + de1739cc8483696506829b52e7fda4f6bb195e6a + + GF-Complete ceph/gf-complete + a6862d10c9db467148f20eef2c6445ac9afd94d8 + + CM256 catid/cm256 + 634d1e36b095dbba937586a7e9843d828762dae4 + + gf256 catid/gf256 + 2f88f20ec3f80d304ce060c824aac31cb0d684ae + + +Compared implementations +------------------------ + +SnapRAID: A storage-oriented RAID engine specialized for large fixed-size + blocks and a small parity count. The current implementation supports + one to six parity levels and is aggressively specialized by parity + count and CPU backend. + +* Typical data/parity block size is 256 KiB. + +* Generation tries to read a source region once, advance all required parity + outputs together, keep accumulators in SIMD registers, and store final parity + once. + +* P is XOR. Q has generator-power structure. Higher parity rows use the + extended Cauchy construction. + +* Generation and SIMD recovery have parity-count-specific GEN1..GEN6 and + REC1..REC6 entry points, plus RAID-specific fast cases such as rec1of1, + reduced P+Q rec2of2 reconstruction, and deriving the final RECX block from + the P delta when P is available. + +* Current optimized backends include x86 SSSE3, AVX2, AVX512BW and GFNI, plus + AArch64 and AArch32 NEON. + +* Generation uses non-temporal parity stores where appropriate because parity + normally enters a queue before the disk writer consumes it. + +Linux RAID6: A kernel RAID6 P+Q engine. It is much less general than a + Reed-Solomon library and therefore can specialize very + aggressively for exactly two parity equations. + +* P is XOR and Q is the classic RAID6 generator-power syndrome over GF(2^8), + implemented naturally with a Horner multiply-by-2 recurrence. + +* Generation keeps P and Q in registers while walking all data disks for the + current vector position, then writes both parity vectors once. + +* x86 generation contains spatial x1/x2/x4 variants, explicit prefetch, and + non-temporal final stores in optimized paths. + +* Recovery is specifically designed for one or two failures. Although the + public RAID6 recovery interface accepts an arbitrary byte count, it is + typically used with PAGE_SIZE-sized buffers and optimized for that workload. + +* Architecture coverage is broad: x86, ARM NEON, PowerPC, s390, RISC-V vector, + LoongArch SIMD, and generic integer implementations. + +* Generation can be benchmarked at boot and the fastest registered + implementation selected dynamically. + +OpenZFS RAIDZ: A filesystem-integrated RAID engine supporting RAIDZ1, RAIDZ2 + and RAIDZ3. It must handle ZFS RAIDZ geometry, ABD buffers, and + rows whose columns may have different lengths. + +* P, Q and R use coefficients based on 1, 2 and 4 respectively over the + standard RAID GF(2^8) polynomial. + +* Q and R generation use fixed multiply-by-2 and multiply-by-4 Horner + recurrences. + +* Parity generation is blocked in 64 KiB regions for L2 locality. + +* For each data column, the parity region is loaded, updated and stored. Cached + stores are therefore a natural consequence of parity reuse. + +* Recovery has explicit rec_p, rec_q, rec_r, rec_pq, rec_pr, rec_qr and rec_pqr + operations with coefficients prepared ahead of the hot loops. + +* The implementation can benchmark each generation and recovery operation + independently and assemble a per-operation fastest backend. + +Intel ISA-L: A high-throughput general erasure-coding library. The fundamental + operation is a GF(2^8) vector dot product with arbitrary + coefficients, not a fixed RAID P/Q/R layout. + +* The API supports systematic Reed-Solomon matrices, Cauchy matrices, generic + matrix inversion, and user-provided coefficient sets. + +* Fast encode/decode kernels calculate one to six destination vectors in one + pass. More outputs are handled in groups. + +* For a source vector, SIMD kernels can split the source nibbles once and reuse + them while updating several output accumulators. + +* PSHUFB-style paths use 32 bytes of expanded low/high nibble tables per + coefficient. + +* x86 backends include SSE/AVX/AVX2/AVX512 and GFNI variants. Current non-x86 + support includes AArch64 NEON and SVE/SVE2, PPC64LE VSX, and RISC-V RVV. + +* A separate update API supports adding the contribution of one source vector + to one or more existing parity vectors. + +klauspost/reedsolomon: A modern general-purpose Reed-Solomon implementation in + Go with architecture-specific generated kernels, + cache-aware work partitioning, parallel execution, and a + separate Leopard mode for very high shard counts. + +* The normal Reed-Solomon path uses a systematic matrix, with Vandermonde as + the default and optional Cauchy, Jerasure-compatible, PAR1 and custom + matrices. + +* Generated SIMD microkernels cover combinations of multiple inputs and + multiple outputs, up to 10 by 10 on the main generated paths. + +* The implementation chooses a cache-sized amount of data per round instead of + blindly processing an arbitrarily large shard in one unit. + +* x86 paths include AVX2, AVX512 and GFNI. AArch64 includes NEON and SVE; there + is also architecture-specific PPC64LE support. + +* Normal reconstruction builds a decoding matrix from surviving shards, inverts + it, and reuses the same multi-output coding machinery for missing shards. + +* An inversion-tree cache can reuse decoding information for repeated erasure + patterns. + +* For shard counts beyond the normal GF(2^8) matrix regime, Leopard provides a + different O(n log n) coding strategy. + +Jerasure/GF-Complete: A general erasure-coding framework paired with a generic + Galois-field library. Jerasure provides coding matrices, + bitmatrices, schedules and decode orchestration; + GF-Complete provides field arithmetic and region + operations. + +* Jerasure supports Vandermonde and Cauchy coding constructions and can convert + matrices into XOR-oriented bitmatrices. + +* Cauchy good-matrix and smart-schedule techniques primarily reduce XOR work in + bitmatrix/packetized coding, not direct SIMD byte-wise GF multiplication. + +* For w=8, GF-Complete can use low/high nibble split tables with SSSE3 or NEON, + closely resembling the PSHUFB/TBL multiplication technique used by other + libraries. + +* A GF-Complete region multiply keeps the coefficient tables resident while + processing the whole region, but Jerasure matrix encoding naturally invokes + such region work separately for matrix rows/sources. + +* The Ceph mirror snapshots used here are legacy code and should be read as + algorithm/implementation references rather than current optimization targets. + +CM256/gf256: A compact Cauchy MDS codec paired with a GF(2^8) region engine. It + targets generic original/recovery blocks and can support many more + recovery blocks than SnapRAID. + +* CM256 normalizes its Cauchy construction so that the first recovery row is + all ones, giving a simple XOR parity row. + +* Encoding processes each recovery block as a whole-region multiply plus + multiply-add sequence. + +* gf256 uses low/high nibble shuffle multiplication on SSSE3/AVX2 and NEON + paths. + +* The standalone AVX2 gf256 tables are pre-expanded to vector width, trading a + larger table footprint for normal vector loads instead of 128-bit + load-and-broadcast operations. + +* CM256 decoding exploits Cauchy structure directly through a Schur-type LDU + solve rather than treating every decode as an arbitrary matrix inversion. + + +Primary target and workload +--------------------------- + +The primary target is the most important comparison category. It explains many +apparent contradictions between otherwise good implementations. + +SnapRAID: Large storage blocks, normally 256 KiB, and one to six parity levels. + The normal generation workload streams many MiB of source data per + stripe and queues parity before disk output. Full extra passes over + source or parity buffers are expensive enough to influence kernel + design. + +Linux RAID6: Kernel RAID6 with exactly P+Q. Recovery is typically performed on + PAGE_SIZE-sized buffers. The tiny fixed parity count permits + formulas and kernels that would not generalize to many parity + outputs. + +OpenZFS RAIDZ: Filesystem RAID with one to three parity columns, dynamic row + geometry and ABD buffers. Cache-blocked processing and repeated + parity updates are reasonable because the parity working region + is deliberately kept hot. + +ISA-L: General high-throughput erasure coding for storage, networking and other + buffer-oriented applications. It must handle arbitrary coefficient + matrices and parity counts without assuming RAID-specific P/Q semantics. + +klauspost: General Reed-Solomon shards over a wide range of block sizes and + data/parity counts. It combines generic matrix semantics with + cache-aware tiling, parallelism and generated SIMD microkernels. + +Jerasure/GF: General erasure-coding construction and field operations. The + framework also supports packetized bitmatrix/schedule approaches + whose cost model is different from direct byte-wise SIMD GF + multiplication. + +CM256/gf256: Generic Cauchy MDS blocks, including use cases with many recovery + blocks. Whole-region GF operations make coefficient residency + easy, but can require source/output regions to be traversed more + than once. + +The direct consequence for SnapRAID is: + + Saving arithmetic is not enough. + + An optimization should preferably preserve: + + one traversal of each large source region + simultaneous progress of required parity outputs + accumulator residency in SIMD registers + one final parity store + + +Coding scheme and parity organization +------------------------------------- + +SnapRAID: Systematic storage layout with P as XOR, Q using generator powers, + and higher parity rows from an extended Cauchy construction. The + first coefficient properties and the small maximum parity count are + known to the implementation and can be exploited directly. + +Linux RAID6: Exactly P+Q. P is XOR and Q uses powers of generator 2 over the + standard RAID field. Q therefore has a natural Horner recurrence + with a fixed multiply-by-2 operation. + +OpenZFS RAIDZ: RAIDZ1/2/3 use P, Q and R. P uses coefficient 1, Q uses powers + of 2, and R uses powers of 4. The fixed 1/2/4 choice supports + simple XOR, MUL2 and MUL4 operations in the generation kernels. + +ISA-L: Generic systematic Reed-Solomon or Cauchy matrices, plus arbitrary + caller-provided coefficients. The vector kernels do not depend on a + particular RAID matrix. + +klauspost: Systematic matrix coding with Vandermonde by default and multiple + matrix options. Generated kernels treat the matrix coefficients + generically while recognizing selected useful matrix properties in + some paths. + +Jerasure/GF: Generic Vandermonde, Cauchy and bitmatrix constructions. Cauchy + matrix improvement can minimize bitmatrix one-count, which matters + for XOR schedules but not directly for PSHUFB/GFNI byte + multiplication cost. + +CM256/gf256: Normalized Cauchy coding. The normalization deliberately creates + an all-one first recovery row. Remaining rows are generic Cauchy + coefficients rather than a RAID-specific Q power sequence. + +The matrix choice therefore affects more than recoverability. SnapRAID, Linux +RAID6 and OpenZFS can exploit fixed generator recurrences in generation. +Generic matrix libraries instead spend more effort making arbitrary coefficient +multiplication fast. + + +GF(2^8) arithmetic +------------------ + +Most implementations compared here use byte-oriented GF(2^8), but they do not +all use the same coding matrix or even the same primitive polynomial. Similar +SIMD instructions do not imply compatible parity. + +SnapRAID: The normal RAID field uses polynomial 0x11d. Non-GFNI SIMD + multiplication uses compact low/high nibble shuffle tables. RAID GFNI + represents multiplication by each fixed coefficient as an 8-byte + affine matrix because the native GFNI multiply instruction is defined + for the AES field. This affine representation directly implements + multiplication in GF(2^8)/0x11d with one VGF2P8AFFINEQB instruction; + it does not require converting the operand to the AES field, + multiplying there, and converting it back. Native AES-field + VGF2P8MULB therefore does not by itself imply a performance advantage + over the RAID-field GFNI implementation. + +Linux RAID6: Uses polynomial 0x11d. Generation mostly needs multiplication by + the fixed generator 2, implemented by byte shift plus conditional + XOR with 0x1d. Recovery also uses precomputed GF tables and SIMD + shuffle tables. + +OpenZFS RAIDZ: Uses polynomial 0x11d. Generation emphasizes fixed MUL2 and + MUL4. General recovery coefficient multiplication uses + table/shuffle machinery in optimized SIMD backends. + +ISA-L: Uses the standard 0x11d field for its common GF(2^8) erasure-code + functions. Classic SIMD paths expand each coefficient into 16-byte low + and 16-byte high nibble tables. GFNI paths use a GFNI-ready expanded + representation. + +klauspost: The normal Reed-Solomon path uses the same common 0x11d field. + Generated backends include shuffle-based and GFNI multiplication, + allowing a particularly direct microarchitectural comparison with + SnapRAID. + +Jerasure/GF: GF-Complete is intentionally more general and supports multiple + field techniques. Its w=8 split-table SIMD path uses the same + basic low/high nibble decomposition as PSHUFB-based SnapRAID and + ISA-L kernels. + +CM256/gf256: Uses a different GF(2^8) polynomial, 0x14d, in its normal + configuration. The SIMD multiplication technique is still + comparable even though the actual coding coefficients and parity + bytes are not compatible with SnapRAID. + + +Supported architectures +----------------------- + +Architecture lists below describe the RAID/erasure-code engines studied, not +every instruction set used elsewhere in each project. + +SnapRAID: Portable scalar C plus x86 SSSE3, AVX2, AVX512BW and GFNI, and NEON + on both AArch64 and AArch32. + +Linux RAID6: Generic integer plus x86 MMX/SSE/SSE2/AVX2/AVX512 generation and + SSSE3/AVX2/AVX512 recovery. It also has ARM NEON, PowerPC + AltiVec/vpermxor, s390 vector, RISC-V RVV and LoongArch SIMD + support. + +OpenZFS RAIDZ: Scalar, x86-64 SSE2, SSSE3, AVX2, AVX512F and AVX512BW, AArch64 + NEON and NEONx2, and PowerPC AltiVec in the current RAIDZ math + dispatcher. + +ISA-L: x86-64 SSE/AVX/AVX2/AVX512 including GFNI, AArch64 NEON and SVE/SVE2, + PPC64LE VSX, RISC-V64 RVV, and portable baseline C. + +klauspost: Portable Go plus generated x86 SIMD including AVX2/AVX512/GFNI, + AArch64 NEON/SVE, and architecture-specific PPC64LE support. + +Jerasure/GF: Portable C with GF-Complete SIMD support including SSSE3 and NEON, + plus other generic field techniques. It predates the AVX512/GFNI + generation of libraries. + +CM256/gf256: Portable scalar code plus gf256 SIMD implementations centered on + SSSE3, AVX2 and NEON. + +For architecture breadth, Linux RAID6 is the strongest reference. For modern +generic GF kernels, ISA-L and klauspost are more directly comparable with +current SnapRAID x86 and ARM work. + + +Kernel and code specialization strategy +--------------------------------------- + +SnapRAID: Hand-specializes around the small fixed parity domain. GEN1 through + GEN6 and SIMD REC1 through REC6 have separate entry points, with + shared internal kernels where appropriate and specific fast paths. + This allows register allocation and algebra to change with parity + count. + +Linux RAID6: Specializes a single mathematical problem, P+Q, but supplies + several spatial unroll factors and architecture backends. The + x1/x2/x4 variants are alternative implementations of the same + fixed syndrome operation. + +OpenZFS RAIDZ: Specializes by P, PQ and PQR generation and by each supported + recovery combination. Backend macro layers provide the same + algorithm with different SIMD widths and field primitives. + +ISA-L: Provides families of assembly kernels for one through six simultaneous + destinations. The high-level function partitions a larger destination + count into groups supported by those kernels. + +klauspost: Uses extensive code generation for many N-input by M-output + combinations. This moves specialization from RAID semantics to + generic matrix microkernels and creates a large generated assembly + body. + +Jerasure/GF: Keeps coding construction and GF region primitives relatively + separate. The framework is general, while GF-Complete specializes + the field operation selected for a region. + +CM256/gf256: Uses a compact generic Cauchy codec and optimized region + primitives rather than a large family of parity-count-specific + encode kernels. + +SnapRAID and klauspost therefore demonstrate two different kinds of +specialization: + + SnapRAID: + specialize the RAID algorithm + + klauspost/reedsolomon: + specialize generic matrix multiplication microkernels + + +Memory traversal +---------------- + +Memory traversal is the category where workload differences matter most for +SnapRAID. For large blocks, the number of full source and destination passes +can dominate small instruction-count savings. + +SnapRAID: The preferred organization is vector-offset outer, source-disk inner, + with all required parity accumulators live. A source chunk is loaded + once and contributes to all parity outputs before the next source is + loaded. + + for each SIMD chunk: + initialize P/Q/R/... accumulators + for each source disk: + load source chunk once + update all parity accumulators + store each final parity chunk once + +Fused recovery follows the same source-reuse principle. It initializes only the +selected syndromes from stored parity, loads each surviving source chunk once, +updates all selected syndromes, and reconstructs every missing output without a +full-size temporary syndrome buffer. + +Linux RAID6: Very similar for P+Q generation. P and Q stay live while the + kernel walks all data disks for the current SIMD chunk, followed + by one final store of each parity output. + +OpenZFS RAIDZ: Processes 64 KiB cache-blocked regions. Within a region, each + data column updates parity buffers that are loaded and stored + repeatedly. This deliberately trades more parity memory + operations for L2 locality and compatibility with RAIDZ/ABD + geometry. + + for each 64 KiB region: + initialize parity region + for each data column: + load data + load/update/store P + load/update/store Q/R as required + +ISA-L: Multi-output dot-product kernels load a source vector chunk, split it + once, and apply several coefficient tables to several live destination + accumulators. This preserves much of the same source-reuse benefit as + SnapRAID without assuming RAID-specific coefficients. + +klauspost: Uses cache-aware rounds and multi-input/multi-output microkernels. + It does not rely on one global whole-shard traversal policy; it + chooses working-set sizes intended to keep the active inputs and + outputs useful in cache. + +Jerasure/GF: A GF-Complete multiply_region keeps one coefficient table resident + over a complete region. Jerasure matrix encoding naturally invokes + region operations for matrix rows and source contributions, so a + source region can be revisited for different outputs. + +CM256/gf256: Encoding one recovery block at a time also gives excellent + coefficient residency, but producing several recovery blocks + requires separate whole-region work for those outputs. + +For SnapRAID, copying the Jerasure/CM256 whole-region orientation would +normally be the wrong trade. It could improve coefficient residency while +increasing 256 KiB source rereads. Current GEN2/GEN3 table residency shows that +SnapRAID can obtain part of the coefficient-residency benefit without changing +the single-pass multi-output traversal. + + +Generation strategy +------------------- + +SnapRAID P: Pure XOR and a dedicated GEN1 path. + +SnapRAID Q: Generator-power structure. Current GFNI RAID paths use direct + Cauchy coefficients, with precomputed affine representations for + the standard RAID field. Horner GFNI implementations were + investigated but are not used by the current implementation. + Other backends use the strategy that benchmarks best for that + parity count. + +SnapRAID 3..6: Higher extended-Cauchy rows use direct coefficient + multiplication. Current GEN2/GEN3 optimizations preload + coefficient tables or matrices into vector registers when the + register file permits it. + +Linux RAID6: P and Q are fused. Q is always the fixed Horner recurrence. The + result is exceptionally compact because there is no requirement to + support arbitrary third or later parity rows. + +OpenZFS RAIDZ: P, PQ and PQR have separate operations. Q and R exploit fixed + MUL2 and MUL4 recurrences, but parity buffers are updated in + cache as each data column is processed. + +ISA-L: Treats generation as generic matrix dot products. Up to six output rows + are fused, so one source load and one nibble split can feed several + parity accumulators. + +klauspost: Treats generation as generic matrix coding but uses generated NxM + microkernels and cache-sized rounds. This is a more general version + of multi-output fusion than a fixed GEN1..GEN6 family. + +Jerasure/GF: Matrix construction and region arithmetic are separated. This is + flexible and makes each coefficient operation simple, but does not + naturally provide the same fixed small-parity fusion as SnapRAID. + +CM256/gf256: Each recovery block is a Cauchy matrix row evaluated by + whole-region GF operations. The first all-one row can reduce to + XOR; remaining rows use generic coefficient multiply-add. + + +Recovery strategy +----------------- + +Recovery comparisons must separate the common single-failure case from rare +multi-failure cases. A sophisticated general decoder can be useful without +improving the normal SnapRAID recovery path. + +SnapRAID: With one missing data disk and P available, rec1of1 remaps pointers + and uses the XOR generator. No temporary syndrome buffer, GF multiply + or matrix solve is required. Other SIMD recovery uses + parity-count-specific REC1..REC6 entry points. + +* Fused SIMD recovery builds and inverts the small N by N matrix, accumulates + exactly the selected syndromes from stored parity and one scan of the + surviving data, and reconstructs all missing blocks in the same kernel. + +* The fused paths avoid raid_delta_gen(), generation of unused parity-prefix + rows, and full-size temporary syndrome buffers. Scalar and selected + SSSE3/AVX2 REC1/REC2 paths retain delta-based recovery. + +* N is at most six, so scalar matrix inversion setup is negligible compared + with processing 256 KiB blocks. + +Linux RAID6: Uses formulas specific to the RAID6 failure cases. Two-data + recovery temporarily substitutes zero pages, generates delta P/Q + using the normal syndrome engine, then solves the two equations + with precomputed multipliers. Data+P recovery has its own formula. + +* This is an algorithmic specialization made possible by exactly two parity + equations. + +* The typical PAGE_SIZE recovery workload makes additional passes much less + costly than they would be over a full SnapRAID block. + +OpenZFS RAIDZ: Dispatches directly among rec_p, rec_q, rec_r, rec_pq, rec_pr, + rec_qr and rec_pqr according to missing data count and available + parity. Recovery coefficients are calculated before the SIMD + loops. + +* The implementation computes syndromes from available columns and then applies + explicit one-, two- or three-failure equations. + +* Big/short RAIDZ column geometry can split the reconstruction into ranges so + conditional logic is kept out of the SIMD inner loops. + +ISA-L: Provides the GF matrix inversion and fast dot-product primitives needed + for decode, but the core fast path is intentionally generic: construct + decode coefficients and run the same multi-output coding engine on + surviving sources. + +klauspost: Selects surviving shards, constructs and inverts the decoding + matrix, then invokes the same multi-output coding machinery for the + requested missing shards. An inversion-tree cache can reuse previous + erasure patterns. + +Jerasure/GF: Selects surviving rows, constructs a decoding matrix and inverts + it for generic matrix decode. Bitmatrix modes can instead build + XOR schedules, including smart schedules intended to reduce XOR + operations. + +CM256/gf256: Uses the algebraic structure of its Cauchy matrix and a direct + Schur-type LDU solve. This avoids treating the decode matrix as a + completely unstructured large matrix, but the triangular solve + performs region multiply-add operations over the recovery blocks. + +The CM256 direct Cauchy solver is mathematically interesting if SnapRAID ever +grows far beyond six parity levels. For the current N <= 6 domain, reducing +matrix-setup complexity is not valuable enough to compensate for an unfavorable +large-block traversal. + + +SIMD and register strategy +-------------------------- + +SnapRAID: Uses parity-count-specific register allocation. Fused recovery keeps + selected syndromes in registers where possible, reuses pre-split + nibbles, and precomputes syndrome and reconstruction coefficient + state before the hot loop. Current GEN2/GEN3 can keep + multiplication tables resident in vector registers where space + permits. + +* AVX512BW and GFNI keep all one to six selected syndromes in vector registers. + +* Register-constrained non-GFNI paths use small aligned per-chunk scratch space + for syndrome state instead of full-size temporary blocks. + +* AVX512 width/unroll is benchmark-driven; wider 128-byte processing is not + assumed to be faster. + +Linux RAID6: Uses fixed P/Q register state and multiple spatial-unroll + variants. AVX2 and AVX512 implementations explicitly prefetch + source data and use simple fixed MUL2 arithmetic rather than + arbitrary coefficient tables during generation. + +OpenZFS RAIDZ: Backend macro layers operate on multiple vector registers per + primitive and provide fixed MUL2/MUL4 plus general coefficient + multiplication. The surrounding algorithm is designed around 64 + KiB cache chunks and ABD iteration rather than keeping parity + live across every data column. + +ISA-L: The 1..6 output kernels are a strong reference for generic multi-output + fusion. AVX2 can load one source, form low/high nibble indices once, and + apply several coefficient tables to several destination accumulators. + +* GFNI removes the nibble-shuffle multiply itself, but output-count and + register-pressure tradeoffs remain. AVX2 GFNI and AVX512 GFNI therefore do + not necessarily use identical maximum output group sizes. + +* Current SVE/SVE2 code can pre-detect an all-one coefficient output and use + direct XOR instead of table multiplication. + +klauspost: Generated NxM kernels use the register file for a broader generic + matrix tile. The trade is much more generated code in exchange for + specialized input/output combinations and fewer generic loops in the + hot path. + +Jerasure/GF: GF-Complete region kernels are optimized around one coefficient at + a time. The coefficient tables can remain resident for the entire + region, which minimizes setup but provides less simultaneous + output fusion. + +CM256/gf256: gf256 similarly optimizes region operations around one + coefficient. Its AVX2 representation can store already duplicated + vector-width low/high tables so the hot loop uses normal 256-bit + loads instead of broadcasts. + + +Coefficient and table handling +------------------------------ + +Coefficient setup is one of the areas where several projects converge on the +same principle: move address calculation, table expansion and matrix work out +of the byte-processing loop whenever possible. + +SnapRAID non-GFNI recovery: Precomputes direct pointers to compact 2x16-byte + shuffle tables for every surviving-data syndrome + coefficient and every inverse-matrix reconstruction + coefficient. The asm therefore avoids + coefficient-byte index arithmetic in the hot loop. + +SnapRAID RAID GFNI recovery: Precomputes direct pointers to the complete 8-byte + affine matrix representation of every syndrome and + inverse coefficient, removing the + coefficient-to-global-table dependency from the + hot loop. + +ISA-L: ec_init_tables expands every coefficient to a 32-byte representation + before encode/decode. The same high-level idea is used for both classic + shuffle kernels and GFNI-ready constants. + +GF-Complete: A region operation selects the coefficient tables once and reuses + them across the whole region. + +gf256: The standalone AVX2 path keeps 256-bit pre-expanded low/high tables. + Compared with a compact 2x16-byte representation, this removes + broadcasts but doubles bytes fetched per coefficient and increases table + footprint. + +For SnapRAID RECX, pre-expanding every AVX2 coefficient table to 2x32 bytes +remains only a microbenchmark idea, not an assumed improvement. The larger +representation doubles coefficient-table traffic. For AVX512 the footprint +penalty becomes even less attractive. + + +Memory, cache and store behavior +-------------------------------- + +SnapRAID: Large source blocks and queued parity favor streaming behavior. + Generation normally writes parity once with non-temporal stores. + Keeping many generated parity blocks resident in cache is not the + objective. + +Linux RAID6: Optimized generation is very similar: source prefetch and + non-temporal final P/Q stores are used in x86 paths. The separate + xor_syndrome update operation reads and modifies existing parity + and therefore uses normal cached stores. + +OpenZFS RAIDZ: Cached stores are integral to the traversal. P/Q/R are + intentionally loaded and updated again as later data columns are + processed inside the 64 KiB cache block. Non-temporal stores + would fight that reuse pattern. + +ISA-L: The public erasure-code API is a memory-buffer library and is not tied + to a disk-writer queue. x86 assembly includes different + aligned/unaligned load/store configurations, but its store policy should + not be copied into SnapRAID independently of the surrounding traversal + and consumer. + +klauspost: Cache-aware rounds are a first-class part of the generic + implementation. The library attempts to size active input/output + work to cache rather than assuming storage-style streaming parity + output. + +Jerasure/GF: Whole-region operations naturally use cached source/destination + buffers and may revisit them for other matrix rows. + +CM256/gf256: Whole-region multiply and multiply-add operations also assume + normal memory reuse. This is appropriate for a generic codec but + does not match SnapRAID final parity lifetime as closely as Linux + RAID6 does. + +OpenZFS is the clearest demonstration that store policy cannot be judged in +isolation: cached stores are correct for an algorithm that immediately reloads +parity, while SnapRAID MOVNT is appropriate when final parity is not consumed +until later in a queue. + + +Temporary memory and working state +---------------------------------- + +SnapRAID: Common rec1of1 uses no syndrome temporary. Fused recovery has small + N-by-N setup state and coefficient arrays, with N <= 6. + Syndrome values and their nibble splits stay in registers or bounded + aligned per-chunk scratch space; no full-size syndrome buffer is + required. + +Linux RAID6: Two-disk recovery reuses the failed data pages themselves as + temporary delta P and delta Q storage, avoiding a separate large + allocation. + +OpenZFS RAIDZ: Works through ABDs and target/syndrome buffers whose geometry + may differ between big and short columns. Its working unit is + intentionally split to keep the SIMD loops regular. + +ISA-L: Requires caller-provided expanded coefficient tables of 32*k*rows bytes + for fast encode/decode, in addition to the source and destination + buffers. + +klauspost: Maintains matrix/decode setup state and cache-oriented work + partitioning. Recovery can also retain inversion information for + repeated erasure patterns. + +Jerasure/GF: Bitmatrix and schedule modes can introduce schedule data and + packetized intermediate work. Direct matrix/GF paths have + coefficient and matrix setup state plus the data regions. + +CM256/gf256: The direct Cauchy solve primarily transforms recovery blocks with + region operations and coefficient setup rather than allocating a + large generic decoding matrix for each data byte. + + +Special cases and fast paths +---------------------------- + +SnapRAID: P is a dedicated XOR path. rec1of1 with P available reduces to GEN1. + Coefficient 0/1 simplifications are useful when they can be + established without adding harmful hot-loop branches. + +Linux RAID6: The entire implementation is a fixed special case: P+Q only. + Single failure can use RAID5-style reconstruction, while two-data + and data+P failure have explicit formulas. + +OpenZFS RAIDZ: Has explicit recovery functions for every P/Q/R combination + through three missing data columns. The dispatcher selects the + cheapest usable parity set. + +ISA-L: Current SVE/SVE2 recognizes coefficient-one destinations and replaces GF + lookup multiplication with XOR. Other paths benefit from fixed + 1..6-output kernel families rather than RAID-specific formulas. + +klauspost: Can select generated special kernels based on matrix/output shape + and contains matrix-property-aware paths such as all-one first + parity rows on some generated backends. + +Jerasure/GF: GF-Complete handles coefficient 0 and 1 specially once per region. + Jerasure can exploit all-one rows and bitmatrix schedules at a + higher coding level. + +CM256/gf256: The normalized first recovery row is XOR. gf256 region + multiply-add also special-cases trivial coefficients outside the + inner byte loop. + + +Incremental parity and update APIs +---------------------------------- + +SnapRAID: The RAID library hot generation path computes complete parity blocks + for the selected stripe/block. A generic single-source MAD update API + is not the main optimization target. + +Linux RAID6: raid6_xor_syndrome explicitly supports in-place P/Q parity + updates. The old contribution can be removed, data changed, and + the new contribution added using the same syndrome structure. + +OpenZFS RAIDZ: The RAIDZ math engine is integrated with copy-on-write + filesystem row generation/reconstruction rather than exposed as + a generic single-source erasure-code update primitive. + +ISA-L: ec_encode_data_update and the gf_*vect_mad family are explicit + single-source update operations that can update several destinations. + +klauspost: The library supports broader shard update/reconstruction workflows, + but its main optimized coding engine remains generic matrix coding + rather than a storage-specific parity-delta API. + +Jerasure/GF: GF region multiply with add semantics can be used to accumulate a + single source contribution, although the higher-level matrix API + is not built around SnapRAID-style block synchronization. + +CM256/gf256: gf256 multiply-add is naturally usable as an update primitive, + while the CM256 encoding API normally constructs complete recovery + blocks. + + +Runtime dispatch and calibration +-------------------------------- + +SnapRAID: Selects implementations from CPU capabilities and the configured + backend. Broad startup microbenchmark calibration is intentionally + not part of the normal design. + +Linux RAID6: Can benchmark registered generation algorithms at boot using + PAGE_SIZE buffers and select the fastest result. Recovery + selection is more directly tied to architecture capability + priority. + +OpenZFS RAIDZ: Benchmarks generation and recovery operations independently and + builds a fastest implementation whose gen_p, gen_pq, gen_pqr and + recovery functions may come from different SIMD backends. + +ISA-L: Uses multibinary runtime dispatch to choose the best implementation for + the detected instruction-set capabilities. + +klauspost: Detects CPU capabilities and chooses generated SIMD functions, cache + strategy and parallel work according to the active platform/options. + +Jerasure/GF: GF-Complete initializes a field implementation appropriate for the + selected technique and available SIMD capabilities rather than + microbenchmarking all alternatives for each operation. + +CM256/gf256: gf256 selects available SIMD support during initialization. The + codec does not use a Linux/OpenZFS-style per-operation startup + benchmark. + +Linux and OpenZFS are useful references for runtime benchmarking, but their +existence does not by itself justify adding calibration to SnapRAID. SnapRAID +has fewer known kernels and a stable large-block workload; calibration adds +startup work, complexity and another source of platform-dependent behavior. + + +Scalability and implementation constraints +------------------------------------------ + +SnapRAID: One to six parity levels. This small fixed N is a central + optimization assumption: it bounds register pressure, recovery matrix + size and the number of specialized kernels. + +Linux RAID6: Exactly two parity levels. It is the most specialized + implementation in the comparison and therefore the least suitable + model for parity counts above Q. + +OpenZFS RAIDZ: One to three parity levels. The implementation is specialized + around P/Q/R rather than arbitrary Reed-Solomon parity counts. + +ISA-L: Generic matrix coding. Fast functions fuse a bounded number of outputs + at a time, and larger parity counts are decomposed into several kernel + groups. + +klauspost: Normal GF(2^8) Reed-Solomon supports the usual byte-field shard + regime; Leopard provides a different path for much larger shard + counts, up to tens of thousands. + +Jerasure/GF: General matrix/bitmatrix framework with field-width choices beyond + the fixed small-parity RAID engines. Its algorithms prioritize + generality over a six-parity register budget. + +CM256/gf256: Designed for a total original+recovery block count within the + GF(2^8) index space and is comfortable with substantially more + recovery blocks than SnapRAID. + +This explains why structured solvers, schedule caches or O(n log n) coding can +be important elsewhere while being unnecessary for current SnapRAID. With N <= +6, arithmetic setup complexity is tiny relative to moving the data. + + +What each implementation teaches SnapRAID +----------------------------------------- + +Linux RAID6: The strongest reference for P+Q streaming generation. It confirms + single-pass source traversal, live parity accumulators, + fixed-generator Horner arithmetic and non-temporal final stores as + sensible storage techniques. Its x1/x2/x4 unrolling and prefetch + are ideas to benchmark, not assumptions to copy. + +OpenZFS RAIDZ: The strongest counterexample for memory policy. Its 64 KiB L2 + blocking and cached parity stores are coherent because parity is + deliberately reused after every data column. The same technique + would increase SnapRAID parity traffic and conflicts with the + queued final-output lifetime. + +ISA-L: The strongest generic reference for multi-output SIMD coding. It + demonstrates that source reuse, nibble reuse and several live output + accumulators are useful even without RAID-specific matrices. GFNI Horner + was investigated, while coefficient precomputation and + multi-output source reuse have influenced the current SnapRAID + implementation. + +klauspost: Shows how far a generic Reed-Solomon engine can push specialization + through generated NxM kernels, cache-aware rounds and parallelism. + It is a useful contrast to SnapRAID hand-specializing GEN1..GEN6 + around known RAID semantics. + +Jerasure/GF: Provides historical matrix, Cauchy and GF implementation + references. Its coefficient residency is useful to understand, but + packetized smart schedules, good-Cauchy bitmatrix one-count + minimization and repeated whole-region passes do not map directly + to current SnapRAID SIMD coding. + +CM256/gf256: Provides a compact Cauchy codec reference. The direct Cauchy LDU + solver is interesting for a hypothetical much larger parity count. + Its pre-expanded AVX2 shuffle tables are a clean microbenchmark + idea, but their larger footprint is not automatically favorable + for RECX. + + +Ideas already represented in current SnapRAID +--------------------------------------------- + +* Fuse several parity outputs or selected recovery syndromes so a source chunk + can update all of them before it is discarded. + +* Use fixed-generator Horner arithmetic where the matrix structure and backend + make it advantageous. + +* Use low/high nibble shuffle multiplication for general GF(2^8) coefficients + on appropriate SIMD backends. + +* Use GFNI with precomputed coefficient representations when hardware support + makes it profitable. + +* Move coefficient address/matrix setup out of the hot data loop. + +* Compute only the selected recovery syndromes, keep them and reused nibble + splits in registers where pressure permits it, and otherwise use bounded + per-chunk scratch space. + +* Spatially unroll simple kernels where independent chains improve throughput + without creating excessive register pressure. + +* Use a direct XOR fast path for P and for the common one-data recovery case + when P is available. + +* Use non-temporal final parity stores for the normal large-block generation + pipeline. + + +Ideas worth keeping as references +--------------------------------- + +* Linux-style alternative spatial unroll factors are useful benchmark + references for specific kernels, but wider is not assumed faster. + +* Linux/OpenZFS runtime benchmarking is a reference if future CPU diversity + makes static dispatch insufficient. + +* ISA-L and klauspost multi-output kernels remain good references for register + allocation and output grouping, especially as new GFNI/SVE backends evolve. + +* ISA-L coefficient-one detection is worth considering only where the + coefficient can be classified outside the hot loop and the resulting + branching/register cost is favorable. + +* gf256 pre-expanded AVX2 shuffle tables are a small, isolated RECX + microbenchmark candidate. They should be tested against the extra L1 table + traffic, not accepted from instruction count alone. + +* CM256 structured Cauchy LDU is a useful future reference if SnapRAID ever + supports parity counts large enough for decoding-matrix setup to matter + materially. + +* klauspost cache tiling and generated NxM specialization are useful + architectural references if SnapRAID ever moves toward much larger parity + counts or more generic coding matrices. + + +Ideas that do not transfer directly +----------------------------------- + +* Do not adopt a source/output loop orientation that requires another full pass + over each 256 KiB source merely to keep one coefficient table resident + longer. + +* Do not adopt OpenZFS cached parity update loops without also adopting the + cache-blocked repeated-parity-reuse workload that makes them sensible. + +* Do not replace direct byte-wise GF SIMD with Jerasure smart bitmatrix + schedules merely because those schedules reduce XOR count. Their cost model + includes packetization and memory traffic that SnapRAID does not currently + need. + +* Do not change the SnapRAID extended Cauchy matrix to a Jerasure good-Cauchy + matrix just to reduce bitmatrix one-count. That metric does not reduce + PSHUFB/GFNI instruction count for a general coefficient. + +* Do not introduce a large generic field object or generic matrix framework + when fixed <=6 parity specialization provides simpler hot paths. + +* Do not trade register pressure for a second full source pass unless a real + large-block benchmark proves that the saved arithmetic is larger than the + added memory traffic. + +* Do not assume pre-expanded vector-width coefficient tables are faster. They + replace broadcasts with extra table bytes and can consume a large fraction of + L1, especially at AVX512 width. + +* Do not optimize recovery-matrix setup at the expense of extra 256 KiB data + passes while the matrix dimension remains at most six. + + +Overall conclusions +------------------- + +No external implementation is a universally better model for SnapRAID. The +useful comparison is to identify which project is closest for a particular +design question. + + P+Q streaming and final stores: + Linux RAID6 + + Filesystem/cache-blocked RAID with repeated parity reuse: + OpenZFS RAIDZ + + Modern generic multi-output SIMD erasure coding: + Intel ISA-L + + Generated generic NxM kernels and cache-aware Reed-Solomon: + klauspost/reedsolomon + + General coding matrices, bitmatrices and historical GF techniques: + Jerasure/GF-Complete + + Compact Cauchy codec and structured Cauchy decoding: + CM256/gf256 + +The current SnapRAID engine occupies a distinct point in this design space: it +is more general than Linux RAID6 and OpenZFS RAIDZ in parity count, but much +more specialized than ISA-L, klauspost, Jerasure or CM256 in coding matrix and +workload. + +That specialization is valuable because SnapRAID normally processes large 256 +KiB regions. The dominant design objective is therefore not merely to minimize +GF instructions. It is to combine low arithmetic cost with low memory traffic: +read large sources as few times as possible, advance all required parity +together, keep reusable state in registers, and write final parity once. + +When evaluating a new external optimization, first ask whether it preserves +those properties. If it does not, the burden of proof is a full SnapRAID +large-block benchmark, not a smaller microkernel instruction count. From df4667ffd5ac4d743f498a319c70d3b2c5535992 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sun, 23 Aug 2026 21:56:59 +0200 Subject: [PATCH 09/21] Add NOTES --- raid/NOTES | 1045 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1045 insertions(+) create mode 100644 raid/NOTES diff --git a/raid/NOTES b/raid/NOTES new file mode 100644 index 00000000..b6fe2330 --- /dev/null +++ b/raid/NOTES @@ -0,0 +1,1045 @@ +SnapRAID RAID library performance notes +======================================= + + +Scope +----- + +These notes summarize performance experiments, implementation choices, +rejected ideas, recovery optimizations, and non-obvious conclusions +reached while reviewing and tuning the SnapRAID RAID library. + +The focus is the 1..6 parity implementation, especially x86 AVX2, +AVX512BW, GFNI, SSSE3, and AArch64/AArch32 NEON backends. + +This is intentionally a record of what has already been tried and what was +learned from it, so that the same ideas are not repeatedly re-investigated. + +Detailed project-by-project comparisons with other RAID and erasure-coding +engines are kept in OTHERS. This file keeps only conclusions that affect +SnapRAID implementation or optimization policy. + +The main rule is to optimize the real SnapRAID workload, not only isolated +inner loops. + + +Implementation overview +----------------------- + +Generation: + +* For the CPUs, compilers, and SnapRAID workloads tested so far, the 1..6 + parity generation paths have no known major performance bottleneck after + coefficient-residency optimization. + +* Relevant scalar, SSE/SSSE3, AVX2, AVX512BW, AArch64/AArch32 NEON, RAID GFNI + affine, and AES GFNI paths are implemented. + +* Specialized GEN2 and GEN3 paths keep useful multiplication tables or + coefficient representations in vector registers when register pressure + permits it. + +* This residency optimization preserves the preferred chunk-major, + multi-output traversal. It does not add another pass over source blocks. + +* RAID GFNI generation uses direct Cauchy coefficients. Their 8-byte affine + representations are precomputed in a disk-major table, avoiding a dependent + Cauchy-coefficient -> affine-matrix lookup in the hot generation loop. + +* AVX512 128-byte processing, non-GFNI AVX512BW Horner, standalone + VPTERNLOGQ transformations, VPBLENDVB, cached parity stores, and large + pre-expanded GFNI constants have been investigated. Some are rejected, while + VPTERNLOGQ is now used where it is part of a larger profitable GEN2/GEN3 + restructuring. + +Recovery: + +* The normal one-data-disk recovery with P available is already handled by + raid_rec1of1(), which is essentially an XOR generation pass with pointer + remapping. + +* SIMD recovery has parity-count-specific REC1..REC6 entry points. Fused paths + compute exactly the selected syndromes in one scan of the surviving data and + reconstruct the missing blocks without full-size temporary syndrome buffers. + +* AVX512BW, GFNI, AArch64 NEON, and AArch32 NEON use fused syndrome generation + for all non-P REC1 and REC2 cases and for REC3..REC6. SSSE3 and AVX2 use it + for REC3..REC6, with additional fused cases in the extended AVX2 path. + +* Scalar recovery and selected SSSE3/AVX2 REC1/REC2 paths continue to use + raid_delta_gen(). + +* Non-GFNI fused recovery splits every completed syndrome into low/high nibbles + once and reuses the split values across all reconstructed outputs. + +* Non-GFNI fused recovery precomputes survivor coefficient and inverse-matrix + table pointers in C before entering the hot data loop. + +* RAID GFNI fused recovery precomputes direct pointers to the 8-byte affine + representation of every syndrome and inverse-matrix coefficient. + +* A recurring RECX local cost is repeated coefficient table/matrix loading + or broadcasting inside the data loop. + +* Specialized P+Q rec2of2 recovery reduces 4 GF multiplications to 2 GF + multiplications and 1 XOR, implemented across scalar int8 and all SIMD backends. + +* When P is among the selected parity blocks, optimized SIMD RECX paths + reconstruct only nr - 1 missing blocks through the inverse matrix and derive + the final missing block from the raw P delta by XORing the blocks already + reconstructed. + +Architecture and validation: + +* AArch32 has a separate NEON backend for generation and recovery. Cross-build, + QEMU user-mode testing, and CI coverage are available for the ARM32 path. + +* Matrix inversion has dedicated multithreaded stress testing. With at most six + parity levels, inversion setup is not a significant performance target. + + +Real workload assumptions +------------------------- + +* SnapRAID processes large blocks: typically 256 KiB per data/parity block. + +* A normal stripe therefore touches a large amount of memory. With 12 data + disks, one 256 KiB stripe already means about 3 MiB of data input before + considering parity output. + +* This large-block behavior is very important. Optimizations that reread a + source buffer, increase the number of passes, or enlarge lookup tables can be + worse even if the inner instruction sequence looks better. + +* This is also the first filter for ideas taken from other RAID or + erasure-coding engines. A design optimized for page-sized recovery, + cache-resident tiles, or generic small shards may make a different memory + tradeoff and should not be copied mechanically. + +* Prefer reducing work inside the existing single-pass traversal before + considering a traversal change. Saving a broadcast or table load is useful; + saving registers by rereading a complete 256 KiB source block usually is not. + +* SnapRAID uses a queue before parity data is written to disk. In the measured + SnapRAID disk-write workloads used during this tuning, very small queues were + slower. Throughput improved clearly up to about queue depth 32 and continued + to improve beyond that with diminishing returns. These queue-depth results + are workload- and storage-dependent and are not an architectural property of + the RAID kernels. + +* With 256 KiB blocks, queue depth 32 means this much parity output can be in + flight: + + 1 parity = 8 MiB + 2 parity = 16 MiB + 3 parity = 24 MiB + 6 parity = 48 MiB + + Therefore a parity block produced by the RAID kernel is normally not consumed + immediately. + +* A memory read of a 256 KiB block that is no longer cached is still very cheap + compared with real storage I/O. Trying to keep many queued parity blocks hot + in cache is therefore not necessarily useful. + + +MOVNT / non-temporal stores +--------------------------- + +* Generation backends use MOVNTDQ / VMOVNTDQ for parity output where applicable. + +* The synthetic speedtest naturally favors non-temporal stores because generated + parity is not consumed afterwards. This benchmark therefore measures producer + throughput, not the complete producer -> queue -> writer pipeline. + +* However, the real SnapRAID queue makes the workload much closer to the + non-temporal case than to an immediate-consumer case. + +* Cached stores could potentially help only with very short queues, where the + writer consumes parity while it is still hot. Queue depths around 1..4 are the + most plausible area where this could happen. + +* Those very short queues were not representative of the fastest measured + SnapRAID disk pipeline in these tests. In that workload, increasing the queue + depth up to about 32 provided substantial throughput improvements. + +* At queue depths representative of those measurements, cached parity output + can occupy many MiB of cache and can evict useful input data. MOVNT avoids + normal cache allocation/write-allocate traffic for parity output and reduces + this source of cache pressure. + +* Conclusion: for the tested large-block, queued SnapRAID generation workload, + keep MOVNT as the default generation policy. Replacing MOVNT with cached + stores is not currently considered an open optimization. This conclusion + should be revisited if block size, queueing behavior, target CPU, or consumer + locality changes. + + +Typical recovery case +--------------------- + +* The common recovery case is one failed data disk while parity P is still + available. + +* raid_rec() selects the first available parity. Therefore, for a single data + failure with P available, recovery uses P. + +* REC1 implementations special-case ip[0] == 0 and call raid_rec1of1(). + +* raid_rec1of1() remaps pointers so that P is used as an XOR input and the + failed data buffer is used as the GEN1 output, then calls + raid_gen(nd, 1, ...). + +* Therefore the common single-disk recovery path does NOT call + raid_delta_gen(). + +* The data traffic is already close to the minimum: + + read P + read all surviving data blocks + XOR them + write the recovered data block once + +* There are no temporary syndrome buffers, no GF multiplication, and no matrix + inversion in this common path. + +* This means that large architectural recovery changes mostly optimize rare + failure cases, not the usual single-disk recovery. + +* Two simultaneous data disk failures are already an exceptional case in normal + use. Three or more simultaneous data failures are even less important as + optimization targets. + + +Recovery speedtest interpretation +--------------------------------- + +The recovery speed tests contain separate rows for the important fast paths and +for the generic recovery paths. + +REC1OF1: + +* The row uses ip[0] == 0. + +* Every REC1 wrapper therefore enters raid_rec1of1() instead of executing its + normal GF recovery body. + +* The speed test explicitly selects the generation function used by the + raid_rec1of1() call with raid_gen_force(1, ...). + +* There is no GFNI GEN1 because PAR1 is only XOR. Therefore: + + rec1of1 avx2 -> raid_gen1_avx2 + rec1of1 gfni -> raid_gen1_avx2 + + rec1of1 avx512 -> raid_gen1_avx512bw + rec1of1 gfni512 -> raid_gen1_avx512bw + + This is intentional. The GFNI recovery wrapper is selected, but the fast path + naturally reduces to the best available XOR generator. + +REC1OF2 and REC1OF3: + +* The rows use Q and R respectively, avoiding the PAR1 fast path. + +* They measure the backend-specific non-P REC1 path. Depending on the backend + and selected parity, this is either fused recovery or a delta-based kernel. + +REC2OF2: + +* The row uses ip = { 0, 1 }, meaning P and Q. + +* Scalar int8 and all SIMD REC2 implementations have a specialized P+Q fast path + and call raid_rec2of2_*(). + +* This makes the row a direct benchmark for the specialized P+Q recovery + implementation. + +REC2OF3 and REC2OF4: + +* The rows use { Q, R } and { R, S } respectively, avoiding the P+Q fast case. + +* REC2OF3 and REC2OF4 remain useful control rows because they do not match + ip = { 0, 1 } and therefore continue to exercise the generic REC2 paths. + +* They measure generic 2x2 recovery with different selected-parity positions, + exposing the difference between exact selected-syndrome fusion and + prefix-based delta generation. + +REC3OF3 through REC6OF6: + +* These rows measure multi-failure recovery for each supported failure count + using the first N parity levels. SIMD columns use fused recovery; the scalar + column retains raid_delta_gen(). + +Delta generation: + +* A benchmark forces a matching generation backend only when the measured + wrapper can call raid_rec1of1() or raid_delta_gen(). + +* Fused recovery rows do not depend on raid_gen() because they generate the + selected syndromes inside the recovery kernel. + + +raid_delta_gen() fusion +----------------------- + +* Fused SIMD recovery accumulates exactly the parity rows selected by ip[] + directly from the stored parity and surviving data. + +* This avoids the prefix constraint of raid_delta_gen(), which computes: + + np = ip[nr - 1] + 1 + + and raid_gen() can generate only a prefix of parity rows. + +* Example: + + nr = 3 + ip = { 0, 2, 5 } + + makes a delta-based path generate through GEN6, while a fused path calculates + only P, R and U. + +* For each SIMD chunk, the fused path starts the selected syndrome accumulators + from stored parity, scans every surviving data source once, and then applies + the inverse matrix to reconstruct all missing blocks. + +* AVX512BW and GFNI keep all one to six syndromes in registers. Non-GFNI AVX2, + SSSE3, and NEON paths use registers or small aligned per-chunk scratch space + according to register pressure; they do not allocate full-size syndrome + buffers. + +* The common rec1of1/P case remains the direct XOR generation path and does not + enter fused GF recovery. + +* raid_delta_gen() remains in the scalar implementation and selected + SSSE3/AVX2 REC1/REC2 paths. + + +Generation: AVX512 width and unrolling +-------------------------------------- + +* Wider processing is not automatically faster. + +* AVX512 128-byte processing was tested on the tuning systems represented by + these notes and did not consistently outperform the existing 64-byte path; + in some tested cases it was slower. + +* Therefore do not assume that x2/x4 spatial unrolling used by some RAID + engines is beneficial for SnapRAID. + +* The block size is already large, memory traffic is substantial, and wider + unrolling can increase register pressure, front-end work, and AVX512 power or + frequency effects without reducing total memory traffic. + +* Standalone VPTERNLOGQ transformations were tested on Zen 5 and did not improve + performance. + +* This is not a general rejection of VPTERNLOGQ. AVX512BW GEN2/GEN3 + kernels use VPTERNLOGQ where it is part of a larger table-residency + restructuring that reduces other work. Judge the complete kernel, not an + instruction in isolation. + +* Conclusion: keep the conservative AVX512 processing width unless a + specific benchmark proves otherwise. + + +Generation: GFNI direct coefficients +------------------------------------ + +* Classic RAID6 P+Q generation has a natural Horner recurrence for Q with the + standard RAID polynomial/generator. + +* Horner implementations were investigated for GFNI generation, but the final + implementation uses direct Cauchy coefficients for both RAID and AES GFNI + generation. + +* RAID GFNI represents each fixed Cauchy coefficient with an 8-byte affine + matrix consumed by VGF2P8AFFINEQB. + +* The generation-specific RAID Cauchy affine matrices are precomputed in: + + raid_gfcauchyaffine_raid[DISK][PARITY - 1][8] + + with disk-major layout: + + [disk][Q/R/S/T/U][8-byte affine matrix] + +* This avoids the previous dependent lookup sequence: + + load raid_gfcauchy_raid[p][d] + use the coefficient to index raid_gfaffine_raid[] + load/broadcast the affine matrix + + and replaces it with one direct address: + + raid_gfcauchyaffine_raid[d][p - 1] + +* The generic raid_gfaffine_raid[256][8] table remains necessary for + runtime-computed multipliers, especially recovery. + +* AES GFNI generation uses the corresponding direct Cauchy coefficient byte + with VGF2P8MULB. + +* Keeping both RAID and AES generation in direct-coefficient form also makes + their GFNI kernels structurally comparable: the main field-specific + difference is VGF2P8AFFINEQB with an 8-byte RAID matrix versus + VGF2P8MULB with an AES coefficient byte. + + +Generation: coefficient residency +--------------------------------- + +Recent GEN2/GEN3 work established coefficient residency as a practical +generation optimization, not only a theoretical one. + +* Non-GFNI SIMD GEN2/GEN3 kernels preload useful low/high multiplication tables + into vector registers when the register file has enough space. + +* The resident set can be partial. When there are more data disks than can be + covered by spare registers, later disks fall back to loading their + coefficient tables normally. + +* GEN3 has higher register pressure because both Q and R coefficients are + needed. Residency is therefore deliberately bounded by the available + register file rather than forcing spills or another source pass. + +* AVX512BW can use VPTERNLOGQ as part of these resident-table kernels to combine + the accumulator with low/high products. The useful optimization is the whole + scheduling and residency change, not VPTERNLOGQ by itself. + +* The source chunk is still loaded once and contributes to all parity outputs + before moving on. The optimization therefore preserves the most important + large-block memory property. + +General conclusion: + +* First use spare registers to remove repeated coefficient setup while + preserving one-pass multi-output generation. + +* Do not trade coefficient residency for extra 256 KiB source passes. + +* Generation success does not imply that RECX can use the same register map. + RECX must also keep multiple syndromes and reconstruction state live, so its + coefficient-residency opportunity remains narrower. + +Overall generation conclusion: + +* For the CPUs, compilers, and workloads represented by the measurements in + these notes, no major generation optimization is currently known with a + strong reason to be investigated. + +* Further generation changes should be driven by new hardware evidence, a + changed workload, or measurements that identify a concrete bottleneck rather + than by an assumption that an optimization from another implementation will + also improve SnapRAID. + + +GFNI field implementation +------------------------- + +* AES GFNI multiplication maps naturally to VGF2P8MULB because the instruction + uses the AES polynomial. + +* Standard RAID polynomial 0x11d cannot use VGF2P8MULB directly. + +* RAID GFNI therefore uses VGF2P8AFFINEQB with precomputed affine matrices. + +* Multiplication by a fixed coefficient in the RAID 0x11d field does not + require converting the operand to the AES field. Multiplication by a + constant is GF(2)-linear, so it is represented directly by an 8x8 GF(2) + matrix and executed with one VGF2P8AFFINEQB instruction. + +* Native AES VGF2P8MULB is therefore not assumed to be inherently faster + than RAID VGF2P8AFFINEQB for the SnapRAID workload. Relative performance + depends on the CPU, backend, parity count, and workload. + +* The global affine matrix table stores 8 bytes per coefficient. + +* RAID GFNI generation additionally stores the affine representation of each + Cauchy coefficient directly in a disk-major + raid_gfcauchyaffine_raid[251][5][8] table. This removes the dependent + Cauchy-coefficient -> global-affine-table lookup from generation while the + generic affine table remains available for runtime multipliers. + +* A generic exhaustive software test validates all coefficient/input pairs + against the normal RAID GF multiplication table. + +* Dedicated hardware affine-matrix validators were intentionally not added. + Normal generation/recovery tests exercise the hardware path. + +* RAID and AES GFNI recovery/generation have polynomial-specific names: + + *_gfni_raid + *_gfni_aes + + with backend tags: + + gfni + gfni512 + +* This naming and registration split is intentional and should be preserved. + + +GFNI RECX coefficient precomputation +------------------------------------ + +RAID GFNI fused recovery precomputes direct pointers to the complete 8-byte +affine representation for: + + every surviving-data / selected-syndrome coefficient + every inverse-matrix reconstruction coefficient + +The hot loop therefore uses: + + direct load/broadcast through the precomputed pointer + VGF2P8AFFINEQB + +This keeps the coefficient-byte -> affine-table lookup outside the hot loop +while retaining the compact 8-byte representation. + +Important details: + +* All 8 bytes form one affine matrix coefficient; it cannot be treated as a + single-byte coefficient. + +* AVX2 and AVX512 RAID GFNI recovery both use this precomputation. + +* AES GFNI does not need this conversion because VGF2P8MULB consumes the + coefficient byte directly. + +* The remaining repeated operation is the per-source or per-reconstruction-term + broadcast of the 8-byte affine matrix for each data chunk. + + +GFNI pre-expanded affine tables +------------------------------- + +* Some high-performance generic erasure-code engines use pre-expanded GFNI + constants, for example a 32-byte representation ready for vector use. + +* For SnapRAID this would increase the RAID affine table from about 2 KiB to + about 8 KiB. + +* In the tested 256 KiB SnapRAID workloads, the compact 8-byte entries plus + broadcast provided the preferred cache-footprint tradeoff. + +* No measured result has justified trading four times the table footprint for + fewer broadcasts. + +* Decision for the currently tested workloads: keep compact 8-byte affine + matrices. + + +External implementation comparisons +----------------------------------- + +Detailed comparisons are in OTHERS. The implementations studied include +specialized RAID engines, filesystem RAID, and general Reed-Solomon libraries. + +The important conclusions are general rather than project-specific. + +Classify an external advantage before trying to copy it: + + algorithm advantage + the method performs genuinely less mathematical work + + implementation advantage + the same work is scheduled, vectorized, or prepared better + + workload-specific advantage + the method wins because the external workload has different block, + cache, parity, update, or latency assumptions + +This distinction prevents a good optimization for another engine from becoming +an automatic SnapRAID TODO. + +Memory traversal: + +* Several modern high-throughput engines confirm that loading one source chunk + and updating multiple outputs from it is a strong general strategy. + +* Other engines deliberately process cache-resident regions and repeatedly + update cached parity. That can be correct when parity is consumed again + immediately, but it is a different memory model from SnapRAID generation. + +* Whole-region operations that keep one coefficient resident can be efficient + for generic erasure coding. For SnapRAID, the same loop orientation can + require rereading large source blocks and is normally a poor trade. + +* The preferred order remains: preserve the one-pass source traversal, then use + spare registers, hoisting, and precomputation to reduce local coefficient + cost. + +Specialization: + +* SnapRAID can specialize aggressively because parity is limited to 1..6 and + the matrix structure is known. + +* Generic engines may use large generated N-input/M-output kernel families, + general decoding matrices, structured solvers, or broader runtime machinery. + These are valuable for their scale but do not automatically reduce work in + the N <= 6 SnapRAID domain. + +* The most useful external ideas are usually local: fuse multiple outputs, + precompute coefficient representations, reuse source splits, special-case + coefficient 0/1 where simple, and exploit algebraic fast paths. + +Cache and stores: + +* Store policy should be evaluated against output lifetime. Cached stores can + be advantageous when an algorithm immediately reloads parity; non-temporal + stores can be advantageous when final parity is queued and consumed later. + +* Therefore cached stores in another fast implementation are not evidence that + SnapRAID should change its MOVNT policy. + +Recovery: + +* General matrix inversion or structured Cauchy solvers become important at + larger scale. For at most six missing blocks, moving 256 KiB regions is far + more important than reducing the tiny matrix-setup cost. + +* Algorithmic failure-case specializations are different: if a fixed recovery + case provably needs fewer GF multiplications, it remains worth measuring. + The specialized P+Q rec2of2 case is an example. + +Runtime and architecture: + +* Some engines benchmark implementations at startup, while others dispatch only + from CPU capabilities. Runtime calibration remains low priority for SnapRAID + because the kernel set is small and the workload is stable. + +* Other engines support additional SIMD families such as SVE, RVV, or VSX. + These are useful porting references, not hot-path optimization tasks + without a target platform requirement. + + +Memory layout, displacement and stride noise +-------------------------------------------- + +* The memory layout is not a neutral detail. + +* Fixed displacement between buffers was observed to help. + +* STRIDE_NOISE behavior differs by CPU. + +* On Intel, stride noise had little effect in the observed tests. + +* On Zen 5, disabling stride noise caused a large throughput reduction. + +* This indicates that hardware prefetch/cache behavior is already strongly + affected by the chosen buffer layout. + +* Therefore new software-prefetch or traversal changes should not be assumed to + help. They must be benchmarked against the existing displacement/stride + behavior. + +* This is another reason to avoid adding extra passes over 256 KiB blocks. + + +REC1 optimizations +------------------ + +The important normal REC1 case is already raid_rec1of1() with P and is close to +the minimum possible memory traffic. + +For generic REC1, used when P is unavailable: + +* AVX512BW, GFNI, AArch64 NEON, and AArch32 NEON use the fused recovery kernel. + +* AVX2 extended uses its delta kernel for Q and fused recovery for later parity + rows. + +* Scalar, SSSE3, base AVX2, and SSSE3 extended use their delta-based REC1 + kernels. + +REC1 status: + +* For the current large-block SnapRAID workload and implemented backends, + no major rec1of1/P optimization is currently known. + +* Generic non-P REC1 is selected per parity count and backend, so fused and + delta-based implementations are benchmarked independently. + +* No major REC1 algorithmic optimization is currently expected. + + +REC2 optimizations +------------------ + +REC2 has both fused and delta-based implementations. + +* AVX512BW, GFNI, AArch64 NEON, and AArch32 NEON use fused syndrome generation + for every selected parity pair. + +* AVX2 extended uses its delta kernel for P+Q and fused recovery for other + parity pairs. + +* Scalar, SSSE3, base AVX2, and SSSE3 extended use delta-based REC2 kernels. + +* Delta-based non-GFNI kernels split both deltas once per chunk and reuse the + split values for both recovered outputs. + +Important portability result: + +* x86-32 has only the low eight XMM/YMM registers available to these old ISA + paths. + +* Optimizations that depend on xmm8..xmm15 or ymm8..ymm15 must remain restricted + to CONFIG_X86_64. + +REC2 status: + +* Generic REC2 uses the fused path where available and retains local split reuse + in the delta-based paths. + +* Scalar int8 and all SIMD backends implement specialized rec2of2 P+Q recovery. + + +Specialized rec2of2 P+Q +----------------------- + +For failures of two data disks recovered specifically with P and Q, the system +can be reduced to: + + Dy = C0 * Pd ^ C1 * Qd + Dx = Pd ^ Dy + +This needs only two GF multiplications for Dy, followed by one XOR for Dx, +instead of the generic 2x2 reconstruction with four GF multiplications. + +All backends implement this specialization: + +* Scalar int8 (raid_rec2of2_int8) +* SSSE3 and SSSE3 ext (raid_rec2of2_ssse3, raid_rec2of2_ssse3ext) +* AVX2 and AVX2 ext (raid_rec2of2_avx2, raid_rec2of2_avx2ext) +* AVX512BW (raid_rec2of2_avx512bw) +* AVX2 GFNI and AVX512 GFNI (RAID and AES variants) +* AArch64 NEON and AArch32 NEON (raid_rec2of2_neon, raid_rec2of2_neon32) + +The rec2of2 benchmark row directly measures this specialized P+Q fast path. + +REC2OF3 and REC2OF4 remain useful control rows because they do not match +ip = { 0, 1 } and therefore continue to exercise the generic REC2 paths. + + +RECX delta handling +------------------- + +RECX computes the selected syndromes directly from stored parity and one scan of +the surviving data for each SIMD chunk. + +For non-GFNI paths: + +* each completed syndrome is split into low/high nibbles once +* the split values stay in registers or bounded aligned per-chunk scratch space +* the same split values are reused across all reconstructed output rows +* no full-size temporary syndrome block is generated or reloaded + +For GFNI paths: + +* the byte syndromes remain directly in SIMD registers +* no nibble split is needed + +When P is among the selected parity blocks, optimized RECX paths can reconstruct +only nr - 1 missing blocks through the inverse matrix and derive the final block +from the raw P delta by XORing the blocks already reconstructed. This removes +one complete inverse-matrix reconstruction row without adding another +source-data pass. + +On the tested x86 implementations, this improved the affected multi-disk +recovery paths by roughly 8% to 12%, depending on the backend and failure count. + + +RECX shuffle-table pointer precomputation +---------------------------------------- + +Non-GFNI fused recovery precomputes in C: + + S[s][j] = &raid_gfmulpshufb[A(ip[j], d)][0][0]; + R[j][k] = &raid_gfmulpshufb[V[j * N + k]][0][0]; + +The hot assembly then only performs: + + load S[s][j] or R[j][k] + load or broadcast the low table at offset 0 + load or broadcast the high table at offset 16 + +This has two advantages: + +1. It removes coefficient-index address arithmetic from the hot assembly. + +2. It keeps C object layout and address calculation in C, leaving the assembly + focused on SIMD operations. + +Users include: + +* AVX2 REC3..REC6 and selected AVX2 extended REC1/REC2 paths +* AVX512BW RECX +* SSSE3 REC3..REC6 +* AArch64 and AArch32 NEON REC1..REC6 + +The remaining cost is that the low/high tables are still loaded or broadcast for +every coefficient of every data chunk. + + +RECX register state +------------------- + +The fused RECX structure uses registers aggressively and falls back to small +per-chunk scratch space where the register file is constrained. + +AVX512BW: + +* up to six syndrome values are kept as low/high pairs in zmm0..zmm11 +* zmm12..zmm15 are reconstruction temporaries/accumulators +* zmm31 keeps the low-nibble mask +* coefficient shuffle tables are still broadcast through R[][] during + reconstruction + +AVX512 RAID GFNI: + +* up to six byte syndromes are kept in zmm0..zmm5 +* temporary/accumulator registers are used for reconstruction +* coefficient affine matrices come directly through precomputed R[][] pointers +* one 8-byte affine matrix is still broadcast per coefficient per chunk + +AVX2 RAID GFNI: + +* the two 32-byte halves of up to six syndromes occupy most of the available + YMM register file +* S[][] and R[][] remove indexed affine-table lookup +* register pressure leaves less room for keeping many coefficient matrices + resident + +AVX2/SSSE3 non-GFNI: + +* up to four raw syndromes are accumulated in registers on the specialized + paths +* larger failure counts use aligned per-chunk syndrome scratch space +* register pressure is significantly higher than AVX512 +* coefficient table residency is therefore much more constrained + +NEON: + +* selected syndromes are accumulated in one surviving-data scan +* S[][] and R[][] provide direct pointers to low/high multiplication tables +* reconstruction still loads each coefficient table pair as needed + + +RECX coefficient residency +-------------------------- + +The main RECX local optimization question is: + + can repeated coefficient loads/broadcasts be reduced by keeping useful + coefficient tables or affine matrices resident in spare vector registers? + +This is most plausible on AVX512 because of the larger register file. + +Possible experiments: + +* keep one output row of coefficients resident across data chunks + +* keep a useful prefix of coefficient tables resident when N permits it + +* on AVX512 RAID GFNI, keep several 8-byte affine matrices broadcast in ZMM + registers instead of broadcasting them again for every chunk + +* on AVX512BW, keep some low/high shuffle-table pairs resident + +Constraints: + +* Do not add a second pass over the 256 KiB block. + +* Do not spill syndrome values or accumulators merely to save coefficient + broadcasts. + +* Do not add large N-specific kernel families unless measurements justify the + complexity. + +* AVX2 and SSSE3 register pressure is already high, so the likely opportunity is + much smaller there. + +* NEON has more architectural registers than old x86 SIMD but RECX already + uses a significant part of them; any residency experiment should be + measured carefully. + +This is a narrow local problem. Fused syndrome generation, nibble reuse, +coefficient address precomputation, and GFNI affine lookup precomputation are +already implemented. + + +Recovery optimization policy +---------------------------- + +For recovery, avoid new architecture-level changes unless a very strong case +appears. + +Preferred optimizations are basic/local: + +* hoist loop-invariant values +* keep masks/tables/matrices resident when register pressure allows it +* avoid repeated loads or broadcasts +* initialize accumulators from the first useful term +* keep address calculations in C when doing so simplifies inline assembly +* exploit coefficient 0 or 1 only when it stays simple and does not add harmful + branching or complexity +* use limited spatial unrolling only when it exposes ILP without excessive + register pressure; SSSE3ext REC1 was faster with a two-way 32-byte unroll + than with the tested four-way 64-byte version + +Avoid unless new measurements provide a concrete reason: + +* extra full passes over source blocks +* aggressive spatial unrolling +* software prefetch without clear evidence +* changing MOVNT policy +* major loop-orientation changes +* large pre-expanded lookup tables +* broad runtime calibration machinery + + +Recovery matrix setup and inversion +----------------------------------- + +* Recovery matrices are at most 6x6 for the 1..6 parity implementation. + +* The arithmetic cost of constructing and inverting such a matrix is tiny + compared with reading and writing 256 KiB data regions. + +* More elaborate structured Cauchy solvers, inversion caches, or asymptotically + faster coding methods are therefore not useful performance targets for the + 1..6 parity limit. + +* A dedicated multithreaded inversion stress test exists to validate the matrix + code independently of the data-path SIMD kernels. + +* At parity counts far beyond six, matrix construction and structured + solvers should be re-evaluated together with the whole memory traversal. + They should not be optimized in isolation. + + +Inline assembly and portability constraints +------------------------------------------- + +The SIMD code intentionally uses multiple small or large asm volatile blocks and +keeps SIMD state live across them. + +raid_selftest() is intended to run at every library startup after the RAID mode +has been selected. It verifies only the currently selected mode, so changing +the mode requires running the self test again before that mode is used. + +Besides checking the RAID algorithms, it verifies the SIMD implementations +actually selected for that mode on the current CPU, compiler, and ABI, +providing runtime protection against implementation or compiler-related +miscompilation. + +Important project constraints: + +* Assembly blocks that dereference pointers internally must model memory effects + appropriately. RECX pointer-based blocks use a "memory" clobber. + +* The x86 SSE/AVX end helpers declare SIMD clobbers needed by the project ABI + strategy, including the Windows x64 callee-saved XMM registers. + +* The NEON end helper declares v0..v31 clobbered so that GCC/Clang preserve + AAPCS64 callee-saved v8..v15 when required. + +* The end-of-function dummy-clobber mechanism is a documented project technique. + It is not a fully explicit extended-asm dataflow model, so future compiler + portability changes should be reviewed carefully. + +* This is primarily a compiler and ABI portability consideration rather than a + known correctness issue in the current implementation. raid_selftest() + verifies the selected mode and implementation at startup so that failures + affecting actual RAID results are detected before normal use. + +* Do not assume x86-32 has xmm8..xmm15 or ymm8..ymm15. + +* AArch32 NEON is a separate backend. Do not assume the AArch64 register or ABI + model when changing ARM32 assembly. + +* RAID thread-safety requirements are documented. Avoid introducing mutable + global calibration or dispatch state without preserving those requirements. + +* RAID block sizes passed to the SIMD code are multiples of 64, so the + 32-byte and 64-byte unrolled loops do not need tail handling. + +These are established implementation constraints, not correctness findings. + + +Scaling beyond 6 parity levels +------------------------------ + +The implementation deliberately targets at most six parity levels. + +* At this scale, fixed-parity specialization keeps matrix setup small and + enables tight kernels with explicit register allocation. + +* General-purpose erasure-code engines demonstrate other designs, including + generic matrix kernels, generated multi-input/multi-output kernels, and + algorithms intended for much larger shard counts. + +* Those designs solve a different scaling problem. They should not be adopted + merely to generalize a domain where the fixed 1..6 structure is useful. + +* If the parity limit is increased substantially, generic intrinsics or + generated kernels should be compared with handwritten specialization. + Memory traversal + must remain a first-class design constraint. + +* Scaling beyond six parity levels is primarily a functional and architectural + change, not an unresolved performance problem of the 1..6 implementation. + + +Short list of ideas already tried or rejected +--------------------------------------------- + +* AVX512 128-byte generation processing: + tried, can be slower. + +* Non-GFNI AVX512BW Horner: + tried on Zen 5, no improvement. + +* Standalone VPTERNLOGQ substitutions: + tried on Zen 5, no improvement. This does not reject integrated use as part + of the profitable GEN2/GEN3 resident-table kernels. + +* VPBLENDVB x2: + tried, no benefit / worse. + +* MOVNT -> cached stores: + not appropriate for the normal queued disk-write workload; keep MOVNT. + +* GFNI 32-byte pre-expanded affine constants: + rejected due to larger cache footprint. + +* Split outputs into multiple full source passes: + rejected because 256 KiB source blocks would be reread. + +* Whole-region/output-major rewrites intended mainly to keep one coefficient + resident: + poor fit when they add source rereads. Use register residency inside the + existing traversal instead. + +* raid_delta_gen() + REC fusion: + implemented for SIMD REC3..REC6 and for REC1/REC2 on AVX512BW, GFNI, and NEON; + selected SSSE3/AVX2 REC1/REC2 and scalar paths retain raid_delta_gen(). + +* Software prefetch: + no strong reason to add it given the existing long streams and proven + sensitivity to displacement/stride tuning. + +* Runtime backend microbenchmark selection: + possible, but low value relative to complexity. + +* Large generic N-input/M-output kernel families: + useful for general erasure coding, but not justified for the fixed + 1..6 parity domain without a concrete benchmark need. + +* Structured or asymptotically faster matrix solvers: + unnecessary at N <= 6 because matrix setup is tiny compared with data + movement. From 03d82a71f4922e490cd5b8a51a0d03a185b95a38 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 17:51:35 +0200 Subject: [PATCH 10/21] Document raid_selftest startup execution and mode selection requirements --- raid/README | 14 +++++++++++--- raid/raid.h | 19 ++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/raid/README b/raid/README index ca8566fe..c05883a1 100644 --- a/raid/README +++ b/raid/README @@ -16,7 +16,10 @@ Public API Header: #include "raid.h" Before invoking any generation or recovery functions, call raid_init() once to -detect CPU capabilities and initialize mode dispatch. +detect CPU capabilities and initialize mode dispatch. Then select the RAID mode +to use, if different from the default, and call raid_selftest(). raid_selftest() +verifies only the currently selected mode and is intended to be run at every +library startup before normal RAID operations. void raid_init(void) Initializes algorithm tables and detects hardware SIMD instruction sets @@ -71,7 +74,12 @@ detect CPU capabilities and initialize mode dispatch. int raid_selftest(void) Runs startup verification tests for parity generation, recovery, and - scanning across parity levels 1..6. Returns 0 on success, < 0 on failure. + scanning across parity levels supported by the currently selected mode. + It is intended to be called at every library startup after the RAID mode + has been selected, to verify the selected implementation on the current + CPU, compiler, and ABI. If the mode is changed later, the self test should + be run again before using the new mode. Returns 0 on success, < 0 on + failure. Memory, Alignment and Aliasing Requirements @@ -166,7 +174,7 @@ Testing and Benchmarks Standalone test applications located in raid/test/ provide verification and performance evaluation: - raid/test/selftest Quick startup self-test verifying basic correctness. + raid/test/selftest Quick startup self-test exercising raid_selftest(). raid/test/fulltest Exhaustive test suite testing all combinations of data disks (1..251) and single/multi-disk failure recovery. raid/test/invtest Matrix inversion test verifying non-singularity for all diff --git a/raid/raid.h b/raid/raid.h index d88fe8bb..a33e46bd 100644 --- a/raid/raid.h +++ b/raid/raid.h @@ -111,6 +111,9 @@ * * You must call this function before any other RAID function. * + * After selecting the RAID mode to use, raid_selftest() should be called + * before using the RAID system. + * * Initialization modifies global RAID state and must not be called * concurrently with any other RAID function. * @@ -140,12 +143,18 @@ int raid_mode(int mode); * * raid_init() must be called before this function. * - * If a mode different from the default is required, raid_mode() must be - * called before this function. The self test verifies the currently selected - * RAID mode. + * The RAID mode to use must be selected before calling this function. + * raid_init() selects RAID_MODE_CAUCHY_RAID by default; raid_mode() may be + * used to select a different mode. The self test verifies only the currently + * selected RAID mode. * - * The test is immediate, and it's intended to be run at application - * startup to check the integrity of the RAID system. + * The test is immediate and is intended to be run at every application + * startup after the RAID mode has been selected. It verifies the RAID + * implementation selected for the current mode, CPU, compiler, and ABI + * before it is used. + * + * If the RAID mode is changed later, this function should be called again + * before using the new mode. * * The test modifies global RAID state and must not be called concurrently * with any other RAID function. From c1a78092c41f59708c321ebe4cab5daa67de984d Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 20:20:35 +0200 Subject: [PATCH 11/21] Fix RAID documentation accuracy --- raid/internal.h | 4 +++- raid/raid.c | 16 +++++++--------- raid/raid.h | 11 +++++++---- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/raid/internal.h b/raid/internal.h index 1ac13e63..f34f376f 100644 --- a/raid/internal.h +++ b/raid/internal.h @@ -44,7 +44,9 @@ #include /* - * Inverse assert. + * Inverse assert used to check internal invariants and API preconditions. + * It is intentionally based on assert() so that all checks can be disabled + * with NDEBUG. When disabled, callers must satisfy all documented preconditions. */ #define BUG_ON(a) assert(!(a)) diff --git a/raid/raid.c b/raid/raid.c index b5d07407..c2aab876 100644 --- a/raid/raid.c +++ b/raid/raid.c @@ -26,12 +26,10 @@ * and divisions by 2 in GF(2^8) are very fast. It is similar to ZFS RAIDZ3, * which uses powers of 4 instead of 2^-1. * - * Unfortunately, this method does not extend beyond triple parity because - * no choice of power coefficients guarantees solvable equations for all - * combinations of missing disks. This is expected: a Vandermonde matrix, - * used in this method, does not ensure that all submatrices are nonsingular - * [2, Chap. 11, Problem 7], which is required for an MDS (Maximum Distance - * Separable) code [2, Chap. 11, Theorem 8]. + * Unfortunately, this method does not extend reliably beyond triple parity. + * Vandermonde matrices do not guarantee that all square submatrices are + * nonsingular, which is required to recover every possible combination of + * missing disks [2, Chap. 11, Problem 7][2, Chap. 11, Theorem 8]. * * To overcome this limitation, a Cauchy matrix [3][4] is used for parity * computation. A Cauchy matrix ensures that all square submatrices are @@ -121,9 +119,9 @@ * data disks, one for each column. All the 377,342,351,231 square submatrices * are nonsingular, verified also by brute-force testing. * - * This matrix can be extended to support any number of parities by simply - * adding additional rows and removing one column for each new row to maintain - * the condition i + j < 255. + * The construction can be extended to additional parity rows while reducing + * the maximum number of data columns accordingly. In GF(2^8), the Extended + * Cauchy construction requires data + parity <= 257 * (See mktables.c for more details on how the matrix is generated.) * * The downside is that generic multiplications are required to compute the diff --git a/raid/raid.h b/raid/raid.h index a33e46bd..89360018 100644 --- a/raid/raid.h +++ b/raid/raid.h @@ -20,8 +20,8 @@ * * The polynomial used by the original RAID-6 specification and the * Linux kernel RAID implementation, used with primitive generator g=2. - * Choosing this polynomial ensures on-disk compatibility with existing - * RAID arrays and interchange with other RAID implementations. + * For identically ordered data blocks, the generated P/Q syndrome bytes + * match the Linux RAID6 implementation. * * On CPUs supporting Intel GFNI (Galois Field New Instructions), * generation and recovery are accelerated with vgf2p8affineqb. @@ -63,7 +63,7 @@ * * Provides high performance on modern CPUs with SSSE3, AVX2, AVX-512, GFNI, or NEON support. * On GFNI CPUs, the standard RAID field is accelerated using affine transformations. - * Ensures maximum compatibility with standard Linux RAID arrays. + * Uses Linux RAID6-compatible P/Q coefficients. * * This is the default mode set after calling raid_init(). */ @@ -331,7 +331,10 @@ int raid_check(int nr, int *ir, int nd, int np, size_t size, void **v); * first one encountered is returned. This function does not check that the * returned combination is unique. * - * It uses a brute force method, and then the call can be expensive. + * This function is primarily intended as a reference implementation and to + * demonstrate how raid_check() can be used to locate failed blocks. It uses + * an exhaustive brute force search and is not intended for production use. + * * The expected execution time is proportional to the binomial coefficient * @np + @nd choose @np - 1, usually written as: * From 88442cb10c79c2b9d3f342d75d08bf106b560d17 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 21:24:52 +0200 Subject: [PATCH 12/21] Avoid writing to healthy parities in raid_rec() using local vector remapping --- raid/module.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/raid/module.c b/raid/module.c index 3007a88c..3f244ef6 100644 --- a/raid/module.c +++ b/raid/module.c @@ -172,8 +172,35 @@ void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v) } /* recompute all the parities up to the last bad one */ - if (nrp != 0) - raid_gen(nd, ir[nr - 1] - nd + 1, size, v); + if (nrp != 0) { + void *v_gen[RAID_DATA_MAX + RAID_PARITY_MAX]; + int max_p = ir[nr - 1] - nd + 1; + int i, j; + + /* copy data block pointers */ + for (i = 0; i < nd; ++i) + v_gen[i] = v[i]; + + /* + * Setup parity pointers. + * To avoid writing into healthy parity blocks (which may be read-only), + * redirect each healthy parity to the buffer of the next bad parity. + * Because raid_gen() generates and writes parities in increasing order, + * the subsequent bad parity will overwrite any intermediate data. + */ + for (i = 0, j = 0; i < max_p; ++i) { + if (j < nrp && ir[nrd + j] == nd + i) { + /* this parity has to be recovered */ + v_gen[nd + i] = v[nd + i]; + ++j; + } else { + /* this parity is healthy, redirect to the next bad parity */ + v_gen[nd + i] = v[ir[nrd + j]]; + } + } + + raid_gen(nd, max_p, size, v_gen); + } } void raid_data(int nr, int *id, int *ip, int nd, size_t size, void **v) From 453fb20d83220bb55d0a4d37e16485702aeae4a7 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 21:32:35 +0200 Subject: [PATCH 13/21] Avoid UB in raid_gen memcpy when output aliases input with nd == 1 --- raid/avx2.c | 9 ++++++--- raid/avx512.c | 18 ++++++++++++------ raid/gfni.c | 14 +++++++++----- raid/neon.c | 3 ++- raid/neon32.c | 3 ++- raid/ssse3.c | 23 +++++++++++++++-------- 6 files changed, 46 insertions(+), 24 deletions(-) diff --git a/raid/avx2.c b/raid/avx2.c index 86cea670..3354afa0 100644 --- a/raid/avx2.c +++ b/raid/avx2.c @@ -317,7 +317,8 @@ static __always_inline void raid_gen3_avx2ext_gen(int nd, size_t size, void **vv /* special case with only one data disk */ if (l == 0) { for (i = 0; i < 3; ++i) - memcpy(v[1 + i], v[0], size); + if (v[1 + i] != v[0]) + memcpy(v[1 + i], v[0], size); return; } @@ -574,7 +575,8 @@ static __always_inline void raid_gen4_avx2ext_gen(int nd, size_t size, void **vv /* special case with only one data disk */ if (l == 0) { for (i = 0; i < 4; ++i) - memcpy(v[1 + i], v[0], size); + if (v[1 + i] != v[0]) + memcpy(v[1 + i], v[0], size); return; } @@ -736,7 +738,8 @@ static __always_inline void raid_genX_avx2ext(int nd, size_t size, void **vv, in /* special case with only one data disk */ if (l == 0) { for (d = 0; d < np; ++d) - memcpy(v[1 + d], v[0], size); + if (v[1 + d] != v[0]) + memcpy(v[1 + d], v[0], size); return; } diff --git a/raid/avx512.c b/raid/avx512.c index cd808353..d23f67d6 100644 --- a/raid/avx512.c +++ b/raid/avx512.c @@ -57,8 +57,10 @@ void raid_gen2_avx512bw(int nd, size_t size, void **vv) int d; if (nd == 1) { - memcpy(v[1], v[0], size); - memcpy(v[2], v[0], size); + if (v[1] != v[0]) + memcpy(v[1], v[0], size); + if (v[2] != v[0]) + memcpy(v[2], v[0], size); return; } @@ -281,9 +283,12 @@ void raid_gen3_avx512bw(int nd, size_t size, void **vv) int d; if (nd == 1) { - memcpy(v[1], v[0], size); - memcpy(v[2], v[0], size); - memcpy(v[3], v[0], size); + if (v[1] != v[0]) + memcpy(v[1], v[0], size); + if (v[2] != v[0]) + memcpy(v[2], v[0], size); + if (v[3] != v[0]) + memcpy(v[3], v[0], size); return; } @@ -450,7 +455,8 @@ static __always_inline void raid_genX_avx512bw(int nd, size_t size, void **vv, i if (nd == 1) { for (d = 0; d < np; ++d) - memcpy(v[1 + d], v[0], size); + if (v[1 + d] != v[0]) + memcpy(v[1 + d], v[0], size); return; } diff --git a/raid/gfni.c b/raid/gfni.c index 60d6a3be..9fd6fb99 100644 --- a/raid/gfni.c +++ b/raid/gfni.c @@ -18,7 +18,8 @@ static __always_inline void raid_genX_avx2gfni_raid(int nd, size_t size, void ** if (nd == 1) { for (d = 0; d < np; ++d) - memcpy(v[1 + d], v[0], size); + if (v[1 + d] != v[0]) + memcpy(v[1 + d], v[0], size); return; } @@ -129,7 +130,8 @@ static __always_inline void raid_genX_avx512gfni_raid(int nd, size_t size, void if (nd == 1) { for (d = 0; d < np; ++d) - memcpy(v[1 + d], v[0], size); + if (v[1 + d] != v[0]) + memcpy(v[1 + d], v[0], size); return; } @@ -212,7 +214,8 @@ static __always_inline void raid_genX_avx2gfni_aes(int nd, size_t size, void **v if (nd == 1) { for (d = 0; d < np; ++d) - memcpy(v[1 + d], v[0], size); + if (v[1 + d] != v[0]) + memcpy(v[1 + d], v[0], size); return; } @@ -330,7 +333,8 @@ static __always_inline void raid_genX_avx512gfni_aes(int nd, size_t size, void * if (nd == 1) { for (d = 0; d < np; ++d) - memcpy(v[1 + d], v[0], size); + if (v[1 + d] != v[0]) + memcpy(v[1 + d], v[0], size); return; } @@ -779,7 +783,7 @@ static __always_inline void raid_recX_avx512gfni_raid(int nr, int *id, int *ip, raid_avx_end(); } - + static __always_inline void raid_recX_avx2gfni_aes(int nr, int *id, int *ip, int nd, size_t size, void **vv) { uint8_t **v = (uint8_t **)vv; diff --git a/raid/neon.c b/raid/neon.c index f7ac4740..7af6ae34 100644 --- a/raid/neon.c +++ b/raid/neon.c @@ -250,7 +250,8 @@ static __always_inline void raid_genX_neon(int nd, size_t size, void **vv, int n /* special case with only one data disk */ if (l == 0) { for (d = 0; d < np; ++d) - memcpy(v[1 + d], v[0], size); + if (v[1 + d] != v[0]) + memcpy(v[1 + d], v[0], size); return; } diff --git a/raid/neon32.c b/raid/neon32.c index c64e541a..1483a6ff 100644 --- a/raid/neon32.c +++ b/raid/neon32.c @@ -244,7 +244,8 @@ static __always_inline void raid_genX_neon32(int nd, size_t size, /* special case with only one data disk */ if (l == 0) { for (d = 0; d < np; ++d) - memcpy(v[1 + d], v[0], size); + if (v[1 + d] != v[0]) + memcpy(v[1 + d], v[0], size); return; } diff --git a/raid/ssse3.c b/raid/ssse3.c index 464257ca..c5165bf1 100644 --- a/raid/ssse3.c +++ b/raid/ssse3.c @@ -26,7 +26,8 @@ static __always_inline void raid_gen3_ssse3_gen(int nd, size_t size, void **vv, /* special case with only one data disk */ if (l == 0) { for (i = 0; i < 3; ++i) - memcpy(v[1 + i], v[0], size); + if (v[1 + i] != v[0]) + memcpy(v[1 + i], v[0], size); return; } @@ -131,7 +132,8 @@ static __always_inline void raid_gen4_ssse3_gen(int nd, size_t size, void **vv, /* special case with only one data disk */ if (l == 0) { for (i = 0; i < 4; ++i) - memcpy(v[1 + i], v[0], size); + if (v[1 + i] != v[0]) + memcpy(v[1 + i], v[0], size); return; } @@ -257,7 +259,8 @@ void raid_gen5_ssse3_raid(int nd, size_t size, void **vv) /* special case with only one data disk */ if (l == 0) { for (i = 0; i < 5; ++i) - memcpy(v[1 + i], v[0], size); + if (v[1 + i] != v[0]) + memcpy(v[1 + i], v[0], size); return; } @@ -394,7 +397,8 @@ void raid_gen6_ssse3_raid(int nd, size_t size, void **vv) /* special case with only one data disk */ if (l == 0) { for (i = 0; i < 6; ++i) - memcpy(v[1 + i], v[0], size); + if (v[1 + i] != v[0]) + memcpy(v[1 + i], v[0], size); return; } @@ -545,7 +549,8 @@ static __always_inline void raid_gen3_ssse3ext_gen(int nd, size_t size, void **v /* special case with only one data disk */ if (l == 0) { for (i = 0; i < 3; ++i) - memcpy(v[1 + i], v[0], size); + if (v[1 + i] != v[0]) + memcpy(v[1 + i], v[0], size); return; } @@ -805,7 +810,8 @@ static __always_inline void raid_gen4_ssse3ext_gen(int nd, size_t size, void **v /* special case with only one data disk */ if (l == 0) { for (i = 0; i < 4; ++i) - memcpy(v[1 + i], v[0], size); + if (v[1 + i] != v[0]) + memcpy(v[1 + i], v[0], size); return; } @@ -995,7 +1001,8 @@ static __always_inline void raid_genX_ssse3ext(int nd, size_t size, void **vv, i /* special case with only one data disk */ if (l == 0) { for (d = 0; d < np; ++d) - memcpy(v[1 + d], v[0], size); + if (v[1 + d] != v[0]) + memcpy(v[1 + d], v[0], size); return; } @@ -1773,7 +1780,7 @@ static __always_inline void raid_recX_ssse3(int nr, int *id, int *ip, int nd, si * * Process two 16-byte blocks per iteration, grouping equal operations across * both lanes to expose instruction-level parallelism. - * + * * A four-way 64-byte unroll was tested but is slower than this two-way 32-byte * implementation. */ From eba07d2173d460858478fa3494f7c917a34184f5 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 21:37:10 +0200 Subject: [PATCH 14/21] Include in raid.h to make the public header self-contained --- raid/raid.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/raid/raid.h b/raid/raid.h index 89360018..b8012797 100644 --- a/raid/raid.h +++ b/raid/raid.h @@ -4,6 +4,8 @@ #ifndef __RAID_H #define __RAID_H +#include + /* * GF(2^8) reducing polynomial selection. * From 306f84768928d5d8dd83cdbf712a315c9b2182a9 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 21:42:22 +0200 Subject: [PATCH 15/21] Reset global zero buffer with raid_zero(0) in raid_selftest cleanup --- raid/module.c | 6 ++++++ raid/raid.h | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/raid/module.c b/raid/module.c index 3f244ef6..c5fa05b4 100644 --- a/raid/module.c +++ b/raid/module.c @@ -779,6 +779,12 @@ int raid_selftest(void) ret = 0; bail: + /* + * Reset the global zero buffer so that it does not point to + * the deallocated memory. + */ + raid_zero(0); + free(v); free(v_alloc); diff --git a/raid/raid.h b/raid/raid.h index b8012797..459adeba 100644 --- a/raid/raid.h +++ b/raid/raid.h @@ -161,6 +161,10 @@ int raid_mode(int mode); * The test modifies global RAID state and must not be called concurrently * with any other RAID function. * + * The test resets the zero buffer to 0 via raid_zero(0) upon completion. + * A valid zero buffer must be set with raid_zero() afterwards before calling + * recovery functions (raid_rec(), raid_data()). + * * It returns 0 on success. */ int raid_selftest(void); From b9974b7873ebee7df1b848211e6f8c36771c5900 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 21:50:24 +0200 Subject: [PATCH 16/21] Include missing raid docs, mktables, and invtestx in EXTRA_DIST --- Makefile.am | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile.am b/Makefile.am index 1a58fc47..7aa7750a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -195,11 +195,16 @@ EXTRA_DIST = \ configure.windows-x86 configure.windows-x64 snapraid.conf.example.windows \ m4/ax_pthread.m4 \ m4/m4_ax_check_compile_flag.m4 \ + raid/README \ + raid/NOTES \ + raid/OTHERS \ + raid/mktables.c \ raid/test/Makefile \ raid/test/fulltest.c \ raid/test/selftest.c \ raid/test/speedtest.c \ raid/test/invtest.c \ + raid/test/invtestx.c \ makeslackware.sh.in \ makeslackdist.sh.in \ pkg/snapraid.SlackBuild.in \ From 5ed6a29db2cbed5068ca7afd0f25fa9c2bb658b3 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 22:01:40 +0200 Subject: [PATCH 17/21] Document memory helpers, freeing semantics, and design invariants --- raid/memory.h | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/raid/memory.h b/raid/memory.h index 65e63efc..3cc6d768 100644 --- a/raid/memory.h +++ b/raid/memory.h @@ -4,6 +4,19 @@ #ifndef __RAID_MEMORY_H #define __RAID_MEMORY_H +/** + * Memory allocation and benchmark layout helpers. + * + * This module provides optional utility functions to allocate SIMD-aligned and + * cache-displaced memory buffers for test suites, benchmarks, and Direct I/O. + * The core RAID engine (raid.h) remains zero-allocation and operates on + * preallocated buffers. + * + * Like other internal helpers in this library, these functions intentionally do + * not validate against arithmetic overflow. Callers are responsible for passing + * reasonable and bounded arguments. + */ + /** * Memory alignment provided by raid_malloc(). * @@ -64,26 +77,36 @@ unsigned raid_optimal_displacement(int n); /** - * Aligned malloc. - * Use an alignment suitable for the raid functions. + * Aligned malloc with default RAID alignment (256 bytes). + * + * Stores the base allocation address in @freeptr to be passed to free(). */ void *raid_malloc(size_t size, void **freeptr); /** * Arbitrary aligned malloc. + * + * Allocates a buffer of @size bytes aligned to @align_size bytes. + * Stores the base allocation address in @freeptr to be passed to free(). */ void *raid_malloc_align(size_t size, size_t align_size, void **freeptr); /** * Aligned vector allocation. - * Use an alignment suitable for the raid functions. - * Returns a vector of @n pointers, each one pointing to a block of - * the specified @size. + * + * Allocates an array of @n pointers, each one pointing to a block of + * the specified @size with optimal L1 displacement and stride perturbation. + * + * Freeing requires two calls: free(*freeptr) for the data buffer, + * and free(v) for the pointer vector. */ void **raid_malloc_vector(int n, size_t size, void **freeptr); /** * Arbitrary aligned vector allocation. + * + * Freeing requires two calls: free(*freeptr) for the data buffer, + * and free(v) for the pointer vector. */ void **raid_malloc_vector_align(int n, size_t size, size_t align_size, ssize_t displacement_size, ssize_t wrap_size, void **freeptr); From 46a09a380a82c8b3f829193f87f1953f3300ea45 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 22:06:17 +0200 Subject: [PATCH 18/21] Check maximum basic CPUID leaf before querying leaf 7 in AVX probing --- raid/cpu.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/raid/cpu.h b/raid/cpu.h index 7dd19d68..d49c6673 100644 --- a/raid/cpu.h +++ b/raid/cpu.h @@ -91,6 +91,11 @@ static inline int raid_cpu_match_avx(uint32_t leaf_1_ecx, uint32_t leaf_7_ebx, u { uint32_t reg[4]; + /* check that leaf 7 is supported */ + raid_cpuid(0, 0, reg); + if (reg[0] < 7) + return 0; + raid_cpuid(1, 0, reg); if ((reg[2] & leaf_1_ecx) != leaf_1_ecx) return 0; From e43bdda22b64719c142474a54ddbf58c3e3ad702 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 22:11:52 +0200 Subject: [PATCH 19/21] Return -1 in raid_check and raid_scan when block size is 0 --- raid/check.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/raid/check.c b/raid/check.c index fd519f82..89c03c91 100644 --- a/raid/check.c +++ b/raid/check.c @@ -138,6 +138,9 @@ int raid_check(int nr, int *ir, int nd, int np, size_t size, void **v) BUG_ON(nr > 0 && ir[0] < 0); BUG_ON(nr > 0 && ir[nr - 1] >= nd + np); + if (size == 0) + return -1; + /* count failed data disk */ rd = 0; while (rd < nr && ir[rd] < nd) @@ -178,6 +181,9 @@ int raid_scan(int *ir, int nd, int np, size_t size, void **v) BUG_ON(v == 0); BUG_ON(np > 1 && ir == 0); + if (size == 0) + return -1; + /* check the special case of no failure */ if (np != 0 && raid_check(0, 0, nd, np, size, v) == 0) return 0; From 7250521e661a64e23b7939b73ad3cfd301fa626a Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 22:16:52 +0200 Subject: [PATCH 20/21] Clarify fulltest scope and test_count limits in raid README --- raid/README | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/raid/README b/raid/README index c05883a1..b475a360 100644 --- a/raid/README +++ b/raid/README @@ -175,8 +175,10 @@ Standalone test applications located in raid/test/ provide verification and performance evaluation: raid/test/selftest Quick startup self-test exercising raid_selftest(). - raid/test/fulltest Exhaustive test suite testing all combinations of data - disks (1..251) and single/multi-disk failure recovery. + raid/test/fulltest Comprehensive test suite verifying parity generation and + tail recovery for 1..251 data disks, and all multi-disk + failure recovery combinations for up to TEST_COUNT disks + (default 24/32). raid/test/invtest Matrix inversion test verifying non-singularity for all square submatrices of the Cauchy matrix. raid/test/speedtest Benchmark measuring single-thread throughput (MiB/s) From fd65dbd9ead292cdce68100bdd5b1bd284e7ba8f Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 24 Aug 2026 23:03:00 +0200 Subject: [PATCH 21/21] Fix undefined behavior by making TommyDS foreach callback signatures take void* --- cmdline/dup.c | 6 ++++-- cmdline/elem.c | 54 +++++++++++++++++++++++++++++++--------------- cmdline/elem.h | 20 ++++++++--------- cmdline/import.c | 4 +++- cmdline/import.h | 2 +- cmdline/pool.c | 8 ++++--- cmdline/scan.c | 5 +++-- cmdline/search.c | 4 +++- cmdline/search.h | 2 +- cmdline/snapraid.c | 4 ++-- cmdline/state.c | 22 +++++++++---------- cmdline/thermal.c | 4 +++- cmdline/thermal.h | 2 +- 13 files changed, 84 insertions(+), 53 deletions(-) diff --git a/cmdline/dup.c b/cmdline/dup.c index 873be3f8..39e3b66e 100644 --- a/cmdline/dup.c +++ b/cmdline/dup.c @@ -60,8 +60,10 @@ static inline tommy_uint32_t hash_hash(struct snapraid_hash* hash) return tommy_hash_u32(0, hash->hash, HASH_MAX); } -void hash_free(struct snapraid_hash* hash) +static void hash_free(void* void_hash) { + struct snapraid_hash* hash = void_hash; + free(hash); } @@ -122,7 +124,7 @@ void state_dup(struct snapraid_state* state) } } - tommy_hashdyn_foreach(&hashset, (tommy_foreach_func*)hash_free); + tommy_hashdyn_foreach(&hashset, hash_free); tommy_hashdyn_done(&hashset); msg_status("\n"); diff --git a/cmdline/elem.c b/cmdline/elem.c index c5b13d96..ac192970 100644 --- a/cmdline/elem.c +++ b/cmdline/elem.c @@ -24,8 +24,10 @@ struct snapraid_content* content_alloc(const char* path, uint64_t dev) return content; } -void content_free(struct snapraid_content* content) +void content_free(void* void_content) { + struct snapraid_content* content = void_content; + free(content); } @@ -453,8 +455,10 @@ struct snapraid_file* file_dup(struct snapraid_file* copy) return file; } -void file_free(struct snapraid_file* file) +void file_free(void* void_file) { + struct snapraid_file* file = void_file; + free(file->sub); file->sub = 0; free(file->blockvec); @@ -679,8 +683,10 @@ struct snapraid_extent* extent_alloc(block_off_t parity_pos, struct snapraid_fil return extent; } -void extent_free(struct snapraid_extent* extent) +void extent_free(void* void_extent) { + struct snapraid_extent* extent = void_extent; + free(extent); } @@ -730,8 +736,10 @@ struct snapraid_link* link_alloc(const char* sub, const char* linkto, unsigned l return slink; } -void link_free(struct snapraid_link* slink) +void link_free(void* void_link) { + struct snapraid_link* slink = void_link; + free(slink->sub); free(slink->linkto); free(slink); @@ -764,8 +772,10 @@ struct snapraid_dir* dir_alloc(const char* sub) return dir; } -void dir_free(struct snapraid_dir* dir) +void dir_free(void* void_dir) { + struct snapraid_dir* dir = void_dir; + free(dir->sub); free(dir); } @@ -820,8 +830,10 @@ void dealloc_import(struct snapraid_dealloc* dealloc, struct snapraid_file* file } } -void dealloc_free(struct snapraid_dealloc* dealloc) +void dealloc_free(void* void_dealloc) { + struct snapraid_dealloc* dealloc = void_dealloc; + free(dealloc->sub); free(dealloc->blockhash); free(dealloc); @@ -890,19 +902,21 @@ struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev return disk; } -void disk_free(struct snapraid_disk* disk) +void disk_free(void* void_disk) { - tommy_list_foreach(&disk->filelist, (tommy_foreach_func*)file_free); - tommy_list_foreach(&disk->deletedlist, (tommy_foreach_func*)file_free); - tommy_tree_foreach(&disk->fs_file, (tommy_foreach_func*)extent_free); + struct snapraid_disk* disk = void_disk; + + tommy_list_foreach(&disk->filelist, file_free); + tommy_list_foreach(&disk->deletedlist, file_free); + tommy_tree_foreach(&disk->fs_file, extent_free); tommy_hashdyn_done(&disk->inodeset); tommy_hashdyn_done(&disk->pathset); tommy_hashdyn_done(&disk->stampset); - tommy_list_foreach(&disk->linklist, (tommy_foreach_func*)link_free); + tommy_list_foreach(&disk->linklist, link_free); tommy_hashdyn_done(&disk->linkset); - tommy_list_foreach(&disk->dirlist, (tommy_foreach_func*)dir_free); + tommy_list_foreach(&disk->dirlist, dir_free); tommy_hashdyn_done(&disk->dirset); - tommy_list_foreach(&disk->dealloclist, (tommy_foreach_func*)dealloc_free); + tommy_list_foreach(&disk->dealloclist, dealloc_free); #if HAVE_THREAD thread_mutex_destroy(&disk->fs_mutex); @@ -937,8 +951,10 @@ struct snapraid_extra* extra_alloc(const char* name, const char* dir, uint64_t d return extra; } -void extra_free(struct snapraid_extra* extra) +void extra_free(void* void_extra) { + struct snapraid_extra* extra = void_extra; + free(extra); } @@ -1565,8 +1581,10 @@ struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t return map; } -void map_free(struct snapraid_map* map) +void map_free(void* void_map) { + struct snapraid_map* map = void_map; + free(map); } @@ -1584,8 +1602,10 @@ static int bucket_cmp(const void* void_a, const void* void_b) return 0; } -void bucket_free(struct snapraid_bucket* bucket) +void bucket_free(void* void_bucket) { + struct snapraid_bucket* bucket = void_bucket; + free(bucket); } @@ -1621,7 +1641,7 @@ void bucket_insert(tommy_hashdyn* bucket_hash, time_t time_at, block_off_t count void bucket_to_list(tommy_hashdyn* bucket_hash, tommy_list* bucket_list, block_off_t* bucketcount) { /* clear previous list */ - tommy_list_foreach(bucket_list, (tommy_foreach_func*)bucket_free); + tommy_list_foreach(bucket_list, bucket_free); tommy_list_init(bucket_list); log_tag("list:bucket_begin\n"); diff --git a/cmdline/elem.h b/cmdline/elem.h index 73b67468..e4a1f097 100644 --- a/cmdline/elem.h +++ b/cmdline/elem.h @@ -574,7 +574,7 @@ struct snapraid_content* content_alloc(const char* path, uint64_t dev); /** * Deallocate a content. */ -void content_free(struct snapraid_content* content); +void content_free(void* void_content); /** * Allocate a filter pattern for files and directories. @@ -850,7 +850,7 @@ struct snapraid_file* file_dup(struct snapraid_file* copy); /** * Deallocate a file. */ -void file_free(struct snapraid_file* file); +void file_free(void* void_file); /** * Rename a file. @@ -955,7 +955,7 @@ struct snapraid_extent* extent_alloc(block_off_t parity_pos, struct snapraid_fil /** * Deallocate a extent. */ -void extent_free(struct snapraid_extent* extent); +void extent_free(void* void_extent); /** * Compare extent by parity position. @@ -1001,7 +1001,7 @@ struct snapraid_link* link_alloc(const char* name, const char* slink, unsigned l /** * Deallocate a link. */ -void link_free(struct snapraid_link* slink); +void link_free(void* void_link); /** * Compare a link with a name. @@ -1044,7 +1044,7 @@ struct snapraid_dir* dir_alloc(const char* name); /** * Deallocate a dir. */ -void dir_free(struct snapraid_dir* dir); +void dir_free(void* void_dir); /** * Compare a dir with a name. @@ -1067,7 +1067,7 @@ struct snapraid_dealloc* dealloc_alloc(unsigned block_size, const char* sub, dat /** * Deallocate a dealloc. */ -void dealloc_free(struct snapraid_dealloc* dealloc); +void dealloc_free(void* void_dealloc); /** * Import file hash in the dealloc. @@ -1082,7 +1082,7 @@ struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev /** * Deallocate a disk. */ -void disk_free(struct snapraid_disk* disk); +void disk_free(void* void_disk); /** * Allocate a extra disk. @@ -1092,7 +1092,7 @@ struct snapraid_extra* extra_alloc(const char* name, const char* dir, uint64_t d /** * Deallocate a other disk. */ -void extra_free(struct snapraid_extra* other); +void extra_free(void* void_extra); /** * Get the size of the disk in blocks. @@ -1219,7 +1219,7 @@ struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t /** * Deallocate a disk mapping. */ -void map_free(struct snapraid_map* map); +void map_free(void* void_map); /** * Mask used to store additional information in the info bits. @@ -1335,7 +1335,7 @@ struct snapraid_bucket { tommy_node node; }; -void bucket_free(struct snapraid_bucket* bucket); +void bucket_free(void* void_bucket); /** * Insert an bucket record into the hashtable diff --git a/cmdline/import.c b/cmdline/import.c index a50cfa9c..40df5f52 100644 --- a/cmdline/import.c +++ b/cmdline/import.c @@ -204,8 +204,10 @@ static void import_dealloc(struct snapraid_state* state, const char* dir, struct tommy_list_insert_tail(&state->importlist, &file->nodelist, file); } -void import_file_free(struct snapraid_import_file* file) +void import_file_free(void* void_file) { + struct snapraid_import_file* file = void_file; + free(file->path); free(file->blockimp); free(file); diff --git a/cmdline/import.h b/cmdline/import.h index fcc7f34a..8aef9b0d 100644 --- a/cmdline/import.h +++ b/cmdline/import.h @@ -44,7 +44,7 @@ struct snapraid_import_file { /** * Deallocate an import file. */ -void import_file_free(struct snapraid_import_file* file); +void import_file_free(void* void_file); /** * Fetch a block from the specified hash. diff --git a/cmdline/pool.c b/cmdline/pool.c index d3707d92..8e504ce5 100644 --- a/cmdline/pool.c +++ b/cmdline/pool.c @@ -35,8 +35,10 @@ static inline tommy_uint32_t pool_hash(const char* file) return tommy_hash_u32(0, file, strlen(file)); } -void pool_free(struct snapraid_pool* pool) +static void pool_free(void* void_pool) { + struct snapraid_pool* pool = void_pool; + free(pool); } @@ -442,12 +444,12 @@ void state_pool(struct snapraid_state* state) msg_progress("Cleaning...\n"); /* delete all the remaining links */ - tommy_hashdyn_foreach_arg(&poolset, (tommy_foreach_arg_func*)remove_link, pool_dir); + tommy_hashdyn_foreach_arg(&poolset, remove_link, pool_dir); /* delete empty dirs */ clean_dir(pool_dir); - tommy_hashdyn_foreach(&poolset, (tommy_foreach_func*)pool_free); + tommy_hashdyn_foreach(&poolset, pool_free); tommy_hashdyn_done(&poolset); if (count) diff --git a/cmdline/scan.c b/cmdline/scan.c index 2d22d914..998f0c99 100644 --- a/cmdline/scan.c +++ b/cmdline/scan.c @@ -74,8 +74,9 @@ static struct snapraid_scan* scan_alloc(struct snapraid_state* state, struct sna return scan; } -static void scan_free(struct snapraid_scan* scan) +static void scan_free(void* void_scan) { + struct snapraid_scan* scan = void_scan; #if HAVE_THREAD thread_mutex_destroy(&scan->disk->stamp_mutex); #endif @@ -2249,7 +2250,7 @@ static int state_diffscan(struct snapraid_state* state, int is_diff) log_flush(); - tommy_list_foreach(&scanlist, (tommy_foreach_func*)scan_free); + tommy_list_foreach(&scanlist, scan_free); /* check the file-system on all disks */ state_fscheck(state, "after scan"); diff --git a/cmdline/search.c b/cmdline/search.c index 59b01c89..6d614e36 100644 --- a/cmdline/search.c +++ b/cmdline/search.c @@ -25,8 +25,10 @@ static void search_file(struct snapraid_state* state, const char* path, data_off tommy_hashdyn_insert(&state->searchset, &file->node, file, file_hash); } -void search_file_free(struct snapraid_search_file* file) +void search_file_free(void* void_file) { + struct snapraid_search_file* file = void_file; + free(file->path); free(file); } diff --git a/cmdline/search.h b/cmdline/search.h index 6eb8d62c..55c8fef3 100644 --- a/cmdline/search.h +++ b/cmdline/search.h @@ -28,7 +28,7 @@ struct snapraid_search_file { /** * Deallocate a search file. */ -void search_file_free(struct snapraid_search_file* file); +void search_file_free(void* void_file); /** * Fetch a file from the size, timestamp and name. diff --git a/cmdline/snapraid.c b/cmdline/snapraid.c index 90612556..7a8c3196 100644 --- a/cmdline/snapraid.c +++ b/cmdline/snapraid.c @@ -2131,8 +2131,8 @@ int snapraid_main(int argc, char* argv[]) #endif state_done(&state); - tommy_list_foreach(&filterlist_file, (tommy_foreach_func*)filter_free); - tommy_list_foreach(&filterlist_disk, (tommy_foreach_func*)filter_free); + tommy_list_foreach(&filterlist_file, filter_free); + tommy_list_foreach(&filterlist_disk, filter_free); app_done(); os_done(); diff --git a/cmdline/state.c b/cmdline/state.c index 18cb85ab..14946845 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -202,19 +202,19 @@ void state_init(struct snapraid_state* state) void state_done(struct snapraid_state* state) { - tommy_list_foreach(&state->disklist, (tommy_foreach_func*)disk_free); - tommy_list_foreach(&state->extralist, (tommy_foreach_func*)extra_free); - tommy_list_foreach(&state->maplist, (tommy_foreach_func*)map_free); - tommy_list_foreach(&state->contentlist, (tommy_foreach_func*)content_free); - tommy_list_foreach(&state->filterlist, (tommy_foreach_func*)filter_free); - tommy_list_foreach(&state->importlist, (tommy_foreach_func*)import_file_free); - tommy_list_foreach(&state->thermallist, (tommy_foreach_func*)thermal_free); - tommy_hashdyn_foreach(&state->searchset, (tommy_foreach_func*)search_file_free); + tommy_list_foreach(&state->disklist, disk_free); + tommy_list_foreach(&state->extralist, extra_free); + tommy_list_foreach(&state->maplist, map_free); + tommy_list_foreach(&state->contentlist, content_free); + tommy_list_foreach(&state->filterlist, filter_free); + tommy_list_foreach(&state->importlist, import_file_free); + tommy_list_foreach(&state->thermallist, thermal_free); + tommy_hashdyn_foreach(&state->searchset, search_file_free); tommy_hashdyn_done(&state->importset); tommy_hashdyn_done(&state->previmportset); tommy_hashdyn_done(&state->searchset); tommy_arrayblkof_done(&state->infoarr); - tommy_list_foreach(&state->bucketlist, (tommy_foreach_func*)bucket_free); + tommy_list_foreach(&state->bucketlist, bucket_free); } /** @@ -4149,7 +4149,7 @@ static void* state_write_thread(void* arg) return 0; bail: - tommy_hashdyn_foreach(&bucket_hash, (tommy_foreach_func*)bucket_free); + tommy_hashdyn_foreach(&bucket_hash, bucket_free); tommy_hashdyn_done(&bucket_hash); return context; } @@ -5017,7 +5017,7 @@ void state_commit(struct snapraid_state* state) if (!tommy_list_empty(&disk->dealloclist)) { /* clear the dealloc list */ - tommy_list_foreach(&disk->dealloclist, (tommy_foreach_func*)dealloc_free); + tommy_list_foreach(&disk->dealloclist, dealloc_free); tommy_list_init(&disk->dealloclist); /* mark the state as needing write */ diff --git a/cmdline/thermal.c b/cmdline/thermal.c index 53199f01..36ee1926 100644 --- a/cmdline/thermal.c +++ b/cmdline/thermal.c @@ -22,8 +22,10 @@ struct snapraid_thermal* thermal_alloc(uint64_t dev, const char* name) return thermal; } -void thermal_free(struct snapraid_thermal* thermal) +void thermal_free(void* void_thermal) { + struct snapraid_thermal* thermal = void_thermal; + free(thermal); } diff --git a/cmdline/thermal.h b/cmdline/thermal.h index 82fe8f61..5e23ddb3 100644 --- a/cmdline/thermal.h +++ b/cmdline/thermal.h @@ -56,7 +56,7 @@ struct snapraid_thermal* thermal_alloc(uint64_t dev, const char* name); /** * Deallocate a thermal state. */ -void thermal_free(struct snapraid_thermal* thermal); +void thermal_free(void* void_thermal); #endif