Skip to content

Commit 568521d

Browse files
committed
Merge branch 'core-rslib-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull Reed-Solomon library updates from Thomas Gleixner: "A cleanup and fixes series from Ferdinand Blomqvist who analyzed the original Reed-Solomon library from Phil Karn on which the kernel implementation is based on. This comes with a test module which verifies all the various corner cases for correctness" * 'core-rslib-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: rslib: Make some functions static rslib: Fix remaining decoder flaws rslib: Update documentation rslib: Fix handling of of caller provided syndrome rslib: decode_rs: Code cleanup rslib: decode_rs: Fix length parameter check rslib: Fix decoding of shortened codes rslib: Add tests for the encoder and decoder
2 parents 6b37754 + ede7c24 commit 568521d

5 files changed

Lines changed: 624 additions & 35 deletions

File tree

‎lib/Kconfig.debug‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,18 @@ config RBTREE_TEST
17541754
A benchmark measuring the performance of the rbtree library.
17551755
Also includes rbtree invariant checks.
17561756

1757+
config REED_SOLOMON_TEST
1758+
tristate "Reed-Solomon library test"
1759+
depends on DEBUG_KERNEL || m
1760+
select REED_SOLOMON
1761+
select REED_SOLOMON_ENC16
1762+
select REED_SOLOMON_DEC16
1763+
help
1764+
This option enables the self-test function of rslib at boot,
1765+
or at module load time.
1766+
1767+
If unsure, say N.
1768+
17571769
config INTERVAL_TREE_TEST
17581770
tristate "Interval tree test"
17591771
depends on DEBUG_KERNEL

‎lib/reed_solomon/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
#
55

66
obj-$(CONFIG_REED_SOLOMON) += reed_solomon.o
7-
7+
obj-$(CONFIG_REED_SOLOMON_TEST) += test_rslib.o

‎lib/reed_solomon/decode_rs.c‎

Lines changed: 85 additions & 30 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
/*
@@ -39,11 +40,21 @@
3940

4041
/* Check length parameter for validity */
4142
pad = nn - nroots - len;
42-
BUG_ON(pad < 0 || pad >= nn);
43+
BUG_ON(pad < 0 || pad >= nn - nroots);
4344

4445
/* Does the caller provide the syndrome ? */
45-
if (s != NULL)
46-
goto decode;
46+
if (s != NULL) {
47+
for (i = 0; i < nroots; i++) {
48+
/* The syndrome is in index form,
49+
* so nn represents zero
50+
*/
51+
if (s[i] != nn)
52+
goto decode;
53+
}
54+
55+
/* syndrome is zero, no errors to correct */
56+
return 0;
57+
}
4758

4859
/* form the syndromes; i.e., evaluate data(x) at roots of
4960
* g(x) */
@@ -88,8 +99,7 @@
8899
/* if syndrome is zero, data[] is a codeword and there are no
89100
* errors to correct. So return data[] unmodified
90101
*/
91-
count = 0;
92-
goto finish;
102+
return 0;
93103
}
94104

95105
decode:
@@ -99,9 +109,9 @@
99109
if (no_eras > 0) {
100110
/* Init lambda to be the erasure locator polynomial */
101111
lambda[1] = alpha_to[rs_modnn(rs,
102-
prim * (nn - 1 - eras_pos[0]))];
112+
prim * (nn - 1 - (eras_pos[0] + pad)))];
103113
for (i = 1; i < no_eras; i++) {
104-
u = rs_modnn(rs, prim * (nn - 1 - eras_pos[i]));
114+
u = rs_modnn(rs, prim * (nn - 1 - (eras_pos[i] + pad)));
105115
for (j = i + 1; j > 0; j--) {
106116
tmp = index_of[lambda[j - 1]];
107117
if (tmp != nn) {
@@ -175,6 +185,15 @@
175185
if (lambda[i] != nn)
176186
deg_lambda = i;
177187
}
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+
178197
/* Find roots of error+erasure locator polynomial by Chien search */
179198
memcpy(&reg[1], &lambda[1], nroots * sizeof(reg[0]));
180199
count = 0; /* Number of roots of lambda(x) */
@@ -188,6 +207,12 @@
188207
}
189208
if (q != 0)
190209
continue; /* Not a root */
210+
211+
if (k < pad) {
212+
/* Impossible error location. Uncorrectable error. */
213+
return -EBADMSG;
214+
}
215+
191216
/* store root (index-form) and error location number */
192217
root[count] = i;
193218
loc[count] = k;
@@ -202,8 +227,7 @@
202227
* deg(lambda) unequal to number of roots => uncorrectable
203228
* error detected
204229
*/
205-
count = -EBADMSG;
206-
goto finish;
230+
return -EBADMSG;
207231
}
208232
/*
209233
* Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
@@ -223,14 +247,23 @@
223247
/*
224248
* Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
225249
* 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
226251
*/
252+
num_corrected = 0;
227253
for (j = count - 1; j >= 0; j--) {
228254
num1 = 0;
229255
for (i = deg_omega; i >= 0; i--) {
230256
if (omega[i] != nn)
231257
num1 ^= alpha_to[rs_modnn(rs, omega[i] +
232258
i * root[j])];
233259
}
260+
261+
if (num1 == 0) {
262+
/* Nothing to correct at this position */
263+
b[j] = 0;
264+
continue;
265+
}
266+
234267
num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)];
235268
den = 0;
236269

@@ -242,30 +275,52 @@
242275
i * root[j])];
243276
}
244277
}
245-
/* Apply error to data */
246-
if (num1 != 0 && loc[j] >= pad) {
247-
uint16_t cor = alpha_to[rs_modnn(rs,index_of[num1] +
248-
index_of[num2] +
249-
nn - index_of[den])];
250-
/* Store the error correction pattern, if a
251-
* correction buffer is available */
252-
if (corr) {
253-
corr[j] = cor;
254-
} else {
255-
/* If a data buffer is given and the
256-
* error is inside the message,
257-
* correct it */
258-
if (data && (loc[j] < (nn - nroots)))
259-
data[loc[j] - pad] ^= cor;
260-
}
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)];
261297
}
298+
299+
if (tmp != alpha_to[s[i]])
300+
return -EBADMSG;
262301
}
263302

264-
finish:
265-
if (eras_pos != NULL) {
266-
for (i = 0; i < count; i++)
267-
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+
}
268323
}
269-
return count;
270324

325+
return num_corrected;
271326
}

‎lib/reed_solomon/reed_solomon.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ EXPORT_SYMBOL_GPL(encode_rs8);
340340
* @data: data field of a given type
341341
* @par: received parity data field
342342
* @len: data length
343-
* @s: syndrome data field (if NULL, syndrome is calculated)
343+
* @s: syndrome data field, must be in index form
344+
* (if NULL, syndrome is calculated)
344345
* @no_eras: number of erasures
345346
* @eras_pos: position of erasures, can be NULL
346347
* @invmsk: invert data mask (will be xored on data, not on parity!)
@@ -354,7 +355,8 @@ EXPORT_SYMBOL_GPL(encode_rs8);
354355
* decoding, so the caller has to ensure that decoder invocations are
355356
* serialized.
356357
*
357-
* Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
358+
* Returns the number of corrected symbols or -EBADMSG for uncorrectable
359+
* errors. The count includes errors in the parity.
358360
*/
359361
int decode_rs8(struct rs_control *rsc, uint8_t *data, uint16_t *par, int len,
360362
uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
@@ -391,7 +393,8 @@ EXPORT_SYMBOL_GPL(encode_rs16);
391393
* @data: data field of a given type
392394
* @par: received parity data field
393395
* @len: data length
394-
* @s: syndrome data field (if NULL, syndrome is calculated)
396+
* @s: syndrome data field, must be in index form
397+
* (if NULL, syndrome is calculated)
395398
* @no_eras: number of erasures
396399
* @eras_pos: position of erasures, can be NULL
397400
* @invmsk: invert data mask (will be xored on data, not on parity!)
@@ -403,7 +406,8 @@ EXPORT_SYMBOL_GPL(encode_rs16);
403406
* decoding, so the caller has to ensure that decoder invocations are
404407
* serialized.
405408
*
406-
* Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
409+
* Returns the number of corrected symbols or -EBADMSG for uncorrectable
410+
* errors. The count includes errors in the parity.
407411
*/
408412
int decode_rs16(struct rs_control *rsc, uint16_t *data, uint16_t *par, int len,
409413
uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,

0 commit comments

Comments
 (0)