Skip to content

Commit 991305d

Browse files
fblomqviKAGA-KOKO
authored andcommitted
rslib: Fix remaining decoder flaws
The decoder is flawed in the following ways: - The decoder sometimes fails silently, i.e. it announces success but returns a word that is not a codeword. - The return value of the decoder is incoherent with respect to how fixed erasures are counted. If the word to be decoded is a codeword, then the decoder always returns zero even if some erasures are given. On the other hand, if the word to be decoded contains errors, then the number of erasures is always included in the count of corrected symbols. So the decoder handles erasures without symbol corruption inconsistently. This inconsistency probably doesn't affect anyone using the decoder, but it is inconsistent with the documentation. - The error positions returned in eras_pos include all erasures, but the corrections are only set in the correction buffer if there actually is a symbol error. So if there are erasures without symbol corruption, then the correction buffer will contain errors (unless initialized to zero before calling the decoder) or some values will be unset (if the correction buffer is uninitialized). - When correcting data in-place the decoder does not correct errors in the parity. On the other hand, when returning the errors in correction buffers, errors in the parity are included. The respective fixed are: - The syndrome of a codeword is always zero, and the syndrome is linear, .i.e, S(x+e) = S(x) + S(e). So compute the syndrome for the error and check whether it equals the syndrome of the received word. If it does, then we have decoded to a valid codeword, otherwise we know that we have an uncorrectable error. Fortunately, some unrecoverable error conditions can be detected earlier in the decoding, which saves some processing power. - Simply count and return the number of symbols actually corrected. - Make sure to only return positions where symbols were corrected. - Also fix errors in parity when correcting in-place. Another option would be to completely disregard errors in the parity, but then the interface makes it impossible to write tests that test for silent failures. Other changes: - Only fill the correction buffer and error position buffer if both of them are provided. Otherwise correct in place. Previously the error position buffer was always populated with the positions of the corrected errors, irrespective of whether a correction buffer was supplied or not. The rationale for this change is that there seems to be two use cases for the decoder; correct in-place or use the correction buffers. The caller does not need the positions of the corrected errors when in-place correction is used. If in-place correction is not used, then both the correction buffer and error position buffer need to be populated. Signed-off-by: Ferdinand Blomqvist <ferdinand.blomqvist@gmail.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Link: https://lkml.kernel.org/r/20190620141039.9874-8-ferdinand.blomqvist@gmail.com
1 parent 38cbae1 commit 991305d

1 file changed

Lines changed: 68 additions & 20 deletions

File tree

‎lib/reed_solomon/decode_rs.c‎

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
uint16_t *index_of = rs->index_of;
2323
uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error;
2424
int count = 0;
25+
int num_corrected;
2526
uint16_t msk = (uint16_t) rs->nn;
2627

2728
/*
@@ -184,6 +185,15 @@
184185
if (lambda[i] != nn)
185186
deg_lambda = i;
186187
}
188+
189+
if (deg_lambda == 0) {
190+
/*
191+
* deg(lambda) is zero even though the syndrome is non-zero
192+
* => uncorrectable error detected
193+
*/
194+
return -EBADMSG;
195+
}
196+
187197
/* Find roots of error+erasure locator polynomial by Chien search */
188198
memcpy(&reg[1], &lambda[1], nroots * sizeof(reg[0]));
189199
count = 0; /* Number of roots of lambda(x) */
@@ -197,6 +207,12 @@
197207
}
198208
if (q != 0)
199209
continue; /* Not a root */
210+
211+
if (k < pad) {
212+
/* Impossible error location. Uncorrectable error. */
213+
return -EBADMSG;
214+
}
215+
200216
/* store root (index-form) and error location number */
201217
root[count] = i;
202218
loc[count] = k;
@@ -231,14 +247,23 @@
231247
/*
232248
* Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
233249
* inv(X(l))**(fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form
250+
* Note: we reuse the buffer for b to store the correction pattern
234251
*/
252+
num_corrected = 0;
235253
for (j = count - 1; j >= 0; j--) {
236254
num1 = 0;
237255
for (i = deg_omega; i >= 0; i--) {
238256
if (omega[i] != nn)
239257
num1 ^= alpha_to[rs_modnn(rs, omega[i] +
240258
i * root[j])];
241259
}
260+
261+
if (num1 == 0) {
262+
/* Nothing to correct at this position */
263+
b[j] = 0;
264+
continue;
265+
}
266+
242267
num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)];
243268
den = 0;
244269

@@ -250,29 +275,52 @@
250275
i * root[j])];
251276
}
252277
}
253-
/* Apply error to data */
254-
if (num1 != 0 && loc[j] >= pad) {
255-
uint16_t cor = alpha_to[rs_modnn(rs,index_of[num1] +
256-
index_of[num2] +
257-
nn - index_of[den])];
258-
/* Store the error correction pattern, if a
259-
* correction buffer is available */
260-
if (corr) {
261-
corr[j] = cor;
262-
} else {
263-
/* If a data buffer is given and the
264-
* error is inside the message,
265-
* correct it */
266-
if (data && (loc[j] < (nn - nroots)))
267-
data[loc[j] - pad] ^= cor;
268-
}
278+
279+
b[j] = alpha_to[rs_modnn(rs, index_of[num1] +
280+
index_of[num2] +
281+
nn - index_of[den])];
282+
num_corrected++;
283+
}
284+
285+
/*
286+
* We compute the syndrome of the 'error' and check that it matches
287+
* the syndrome of the received word
288+
*/
289+
for (i = 0; i < nroots; i++) {
290+
tmp = 0;
291+
for (j = 0; j < count; j++) {
292+
if (b[j] == 0)
293+
continue;
294+
295+
k = (fcr + i) * prim * (nn-loc[j]-1);
296+
tmp ^= alpha_to[rs_modnn(rs, index_of[b[j]] + k)];
269297
}
298+
299+
if (tmp != alpha_to[s[i]])
300+
return -EBADMSG;
270301
}
271302

272-
if (eras_pos != NULL) {
273-
for (i = 0; i < count; i++)
274-
eras_pos[i] = loc[i] - pad;
303+
/*
304+
* Store the error correction pattern, if a
305+
* correction buffer is available
306+
*/
307+
if (corr && eras_pos) {
308+
j = 0;
309+
for (i = 0; i < count; i++) {
310+
if (b[i]) {
311+
corr[j] = b[i];
312+
eras_pos[j++] = loc[i] - pad;
313+
}
314+
}
315+
} else if (data && par) {
316+
/* Apply error to data and parity */
317+
for (i = 0; i < count; i++) {
318+
if (loc[i] < (nn - nroots))
319+
data[loc[i] - pad] ^= b[i];
320+
else
321+
par[loc[i] - pad - len] ^= b[i];
322+
}
275323
}
276-
return count;
277324

325+
return num_corrected;
278326
}

0 commit comments

Comments
 (0)