Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'rslib-v4.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux

Pull reed-salomon library updates from Kees Cook:
"Refactors rslib and callers to provide a per-instance allocation area
instead of performing VLAs on the stack"

* tag 'rslib-v4.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux:
rslib: Allocate decoder buffers to avoid VLAs
mtd: rawnand: diskonchip: Allocate rs control per instance
rslib: Split rs control struct
rslib: Simplify error path
rslib: Remove GPL boilerplate
rslib: Add SPDX identifiers
rslib: Cleanup top level comments
rslib: Cleanup whitespace damage
dm/verity_fec: Use GFP aware reed solomon init
rslib: Add GFP aware init function

+243 -196
+1 -1
drivers/md/dm-verity-fec.c
··· 570 570 { 571 571 struct dm_verity *v = (struct dm_verity *)pool_data; 572 572 573 - return init_rs(8, 0x11d, 0, 1, v->fec->roots); 573 + return init_rs_gfp(8, 0x11d, 0, 1, v->fec->roots, gfp_mask); 574 574 } 575 575 576 576 static void fec_rs_free(void *element, void *pool_data)
+4 -3
drivers/mtd/nand/raw/cafe_nand.c
··· 394 394 395 395 for (i=0; i<8; i+=2) { 396 396 uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2)); 397 - syn[i] = cafe->rs->index_of[tmp & 0xfff]; 398 - syn[i+1] = cafe->rs->index_of[(tmp >> 16) & 0xfff]; 397 + 398 + syn[i] = cafe->rs->codec->index_of[tmp & 0xfff]; 399 + syn[i+1] = cafe->rs->codec->index_of[(tmp >> 16) & 0xfff]; 399 400 } 400 401 401 402 n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0, 402 - pat); 403 + pat); 403 404 404 405 for (i = 0; i < n; i++) { 405 406 int p = pos[i];
+31 -36
drivers/mtd/nand/raw/diskonchip.c
··· 66 66 int curchip; 67 67 int mh0_page; 68 68 int mh1_page; 69 + struct rs_control *rs_decoder; 69 70 struct mtd_info *nextdoc; 70 71 71 72 /* Handle the last stage of initialization (BBT scan, partitioning) */ ··· 124 123 /* Number of symbols */ 125 124 #define NN 1023 126 125 127 - /* the Reed Solomon control structure */ 128 - static struct rs_control *rs_decoder; 129 - 130 126 /* 131 127 * The HW decoder in the DoC ASIC's provides us a error syndrome, 132 128 * which we must convert to a standard syndrome usable by the generic ··· 138 140 int i, j, nerr, errpos[8]; 139 141 uint8_t parity; 140 142 uint16_t ds[4], s[5], tmp, errval[8], syn[4]; 143 + struct rs_codec *cd = rs->codec; 141 144 142 145 memset(syn, 0, sizeof(syn)); 143 146 /* Convert the ecc bytes into words */ ··· 159 160 for (j = 1; j < NROOTS; j++) { 160 161 if (ds[j] == 0) 161 162 continue; 162 - tmp = rs->index_of[ds[j]]; 163 + tmp = cd->index_of[ds[j]]; 163 164 for (i = 0; i < NROOTS; i++) 164 - s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)]; 165 + s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)]; 165 166 } 166 167 167 168 /* Calc syn[i] = s[i] / alpha^(v + i) */ 168 169 for (i = 0; i < NROOTS; i++) { 169 170 if (s[i]) 170 - syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i)); 171 + syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i)); 171 172 } 172 173 /* Call the decoder library */ 173 174 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval); ··· 929 930 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i); 930 931 } 931 932 932 - ret = doc_ecc_decode(rs_decoder, dat, calc_ecc); 933 + ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc); 933 934 if (ret > 0) 934 935 pr_err("doc200x_correct_data corrected %d errors\n", 935 936 ret); ··· 1420 1421 1421 1422 static int __init doc_probe(unsigned long physadr) 1422 1423 { 1424 + struct nand_chip *nand = NULL; 1425 + struct doc_priv *doc = NULL; 1423 1426 unsigned char ChipID; 1424 1427 struct mtd_info *mtd; 1425 - struct nand_chip *nand; 1426 - struct doc_priv *doc; 1427 1428 void __iomem *virtadr; 1428 1429 unsigned char save_control; 1429 1430 unsigned char tmp, tmpb, tmpc; ··· 1560 1561 goto fail; 1561 1562 } 1562 1563 1564 + 1565 + /* 1566 + * Allocate a RS codec instance 1567 + * 1568 + * Symbolsize is 10 (bits) 1569 + * Primitve polynomial is x^10+x^3+1 1570 + * First consecutive root is 510 1571 + * Primitve element to generate roots = 1 1572 + * Generator polinomial degree = 4 1573 + */ 1574 + doc = (struct doc_priv *) (nand + 1); 1575 + doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS); 1576 + if (!doc->rs_decoder) { 1577 + pr_err("DiskOnChip: Could not create a RS codec\n"); 1578 + ret = -ENOMEM; 1579 + goto fail; 1580 + } 1581 + 1563 1582 mtd = nand_to_mtd(nand); 1564 - doc = (struct doc_priv *) (nand + 1); 1565 1583 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1); 1566 1584 nand->bbt_md = nand->bbt_td + 1; 1567 1585 ··· 1628 1612 haven't yet added it. This is handled without incident by 1629 1613 mtd_device_unregister, as far as I can tell. */ 1630 1614 nand_release(mtd); 1631 - kfree(nand); 1632 1615 goto fail; 1633 1616 } 1634 1617 ··· 1640 1625 actually a DiskOnChip. */ 1641 1626 WriteDOC(save_control, virtadr, DOCControl); 1642 1627 fail: 1628 + if (doc) 1629 + free_rs(doc->rs_decoder); 1630 + kfree(nand); 1643 1631 iounmap(virtadr); 1644 1632 1645 1633 error_ioremap: ··· 1665 1647 nand_release(mtd); 1666 1648 iounmap(doc->virtadr); 1667 1649 release_mem_region(doc->physadr, DOC_IOREMAP_LEN); 1650 + free_rs(doc->rs_decoder); 1668 1651 kfree(nand); 1669 1652 } 1670 1653 } ··· 1674 1655 { 1675 1656 int i, ret = 0; 1676 1657 1677 - /* We could create the decoder on demand, if memory is a concern. 1678 - * This way we have it handy, if an error happens 1679 - * 1680 - * Symbolsize is 10 (bits) 1681 - * Primitve polynomial is x^10+x^3+1 1682 - * first consecutive root is 510 1683 - * primitve element to generate roots = 1 1684 - * generator polinomial degree = 4 1685 - */ 1686 - rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS); 1687 - if (!rs_decoder) { 1688 - pr_err("DiskOnChip: Could not create a RS decoder\n"); 1689 - return -ENOMEM; 1690 - } 1691 - 1692 1658 if (doc_config_location) { 1693 1659 pr_info("Using configured DiskOnChip probe address 0x%lx\n", 1694 1660 doc_config_location); 1695 1661 ret = doc_probe(doc_config_location); 1696 1662 if (ret < 0) 1697 - goto outerr; 1663 + return ret; 1698 1664 } else { 1699 1665 for (i = 0; (doc_locations[i] != 0xffffffff); i++) { 1700 1666 doc_probe(doc_locations[i]); ··· 1690 1686 if (!doclist) { 1691 1687 pr_info("No valid DiskOnChip devices found\n"); 1692 1688 ret = -ENODEV; 1693 - goto outerr; 1694 1689 } 1695 - return 0; 1696 - outerr: 1697 - free_rs(rs_decoder); 1698 1690 return ret; 1699 1691 } 1700 1692 ··· 1698 1698 { 1699 1699 /* Cleanup the nand/DoC resources */ 1700 1700 release_nanddoc(); 1701 - 1702 - /* Free the reed solomon resources */ 1703 - if (rs_decoder) { 1704 - free_rs(rs_decoder); 1705 - } 1706 1701 } 1707 1702 1708 1703 module_init(init_nanddoc);
+48 -26
include/linux/rslib.h
··· 1 + // SPDX-License-Identifier: GPL-2.0 1 2 /* 2 - * include/linux/rslib.h 3 - * 4 - * Overview: 5 - * Generic Reed Solomon encoder / decoder library 3 + * Generic Reed Solomon encoder / decoder library 6 4 * 7 5 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de) 8 6 * 9 7 * RS code lifted from reed solomon library written by Phil Karn 10 8 * Copyright 2002 Phil Karn, KA9Q 11 - * 12 - * $Id: rslib.h,v 1.4 2005/11/07 11:14:52 gleixner Exp $ 13 - * 14 - * This program is free software; you can redistribute it and/or modify 15 - * it under the terms of the GNU General Public License version 2 as 16 - * published by the Free Software Foundation. 17 9 */ 18 - 19 10 #ifndef _RSLIB_H_ 20 11 #define _RSLIB_H_ 21 12 22 13 #include <linux/list.h> 14 + #include <linux/types.h> /* for gfp_t */ 15 + #include <linux/gfp.h> /* for GFP_KERNEL */ 23 16 24 17 /** 25 - * struct rs_control - rs control structure 18 + * struct rs_codec - rs codec data 26 19 * 27 20 * @mm: Bits per symbol 28 21 * @nn: Symbols per block (= (1<<mm)-1) ··· 29 36 * @gfpoly: The primitive generator polynominal 30 37 * @gffunc: Function to generate the field, if non-canonical representation 31 38 * @users: Users of this structure 32 - * @list: List entry for the rs control list 39 + * @list: List entry for the rs codec list 33 40 */ 34 - struct rs_control { 35 - int mm; 36 - int nn; 41 + struct rs_codec { 42 + int mm; 43 + int nn; 37 44 uint16_t *alpha_to; 38 45 uint16_t *index_of; 39 46 uint16_t *genpoly; 40 - int nroots; 41 - int fcr; 42 - int prim; 43 - int iprim; 47 + int nroots; 48 + int fcr; 49 + int prim; 50 + int iprim; 44 51 int gfpoly; 45 52 int (*gffunc)(int); 46 53 int users; 47 54 struct list_head list; 55 + }; 56 + 57 + /** 58 + * struct rs_control - rs control structure per instance 59 + * @codec: The codec used for this instance 60 + * @buffers: Internal scratch buffers used in calls to decode_rs() 61 + */ 62 + struct rs_control { 63 + struct rs_codec *codec; 64 + uint16_t buffers[0]; 48 65 }; 49 66 50 67 /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */ ··· 79 76 uint16_t *corr); 80 77 #endif 81 78 82 - /* Create or get a matching rs control structure */ 83 - struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim, 84 - int nroots); 79 + struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim, 80 + int nroots, gfp_t gfp); 81 + 82 + /** 83 + * init_rs - Create a RS control struct and initialize it 84 + * @symsize: the symbol size (number of bits) 85 + * @gfpoly: the extended Galois field generator polynomial coefficients, 86 + * with the 0th coefficient in the low order bit. The polynomial 87 + * must be primitive; 88 + * @fcr: the first consecutive root of the rs code generator polynomial 89 + * in index form 90 + * @prim: primitive element to generate polynomial roots 91 + * @nroots: RS code generator polynomial degree (number of roots) 92 + * 93 + * Allocations use GFP_KERNEL. 94 + */ 95 + static inline struct rs_control *init_rs(int symsize, int gfpoly, int fcr, 96 + int prim, int nroots) 97 + { 98 + return init_rs_gfp(symsize, gfpoly, fcr, prim, nroots, GFP_KERNEL); 99 + } 100 + 85 101 struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int), 86 - int fcr, int prim, int nroots); 102 + int fcr, int prim, int nroots); 87 103 88 104 /* Release a rs control structure */ 89 105 void free_rs(struct rs_control *rs); 90 106 91 107 /** modulo replacement for galois field arithmetics 92 108 * 93 - * @rs: the rs control structure 109 + * @rs: Pointer to the RS codec 94 110 * @x: the value to reduce 95 111 * 96 112 * where ··· 119 97 * Simple arithmetic modulo would return a wrong result for values 120 98 * >= 3 * rs->nn 121 99 */ 122 - static inline int rs_modnn(struct rs_control *rs, int x) 100 + static inline int rs_modnn(struct rs_codec *rs, int x) 123 101 { 124 102 while (x >= rs->nn) { 125 103 x -= rs->nn;
+17 -17
lib/reed_solomon/decode_rs.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 1 2 /* 2 - * lib/reed_solomon/decode_rs.c 3 - * 4 - * Overview: 5 - * Generic Reed Solomon encoder / decoder library 3 + * Generic Reed Solomon encoder / decoder library 6 4 * 7 5 * Copyright 2002, Phil Karn, KA9Q 8 6 * May be used under the terms of the GNU General Public License (GPL) 9 7 * 10 8 * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de) 11 9 * 12 - * $Id: decode_rs.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $ 13 - * 14 - */ 15 - 16 - /* Generic data width independent code which is included by the 17 - * wrappers. 10 + * Generic data width independent code which is included by the wrappers. 18 11 */ 19 12 { 13 + struct rs_codec *rs = rsc->codec; 20 14 int deg_lambda, el, deg_omega; 21 15 int i, j, r, k, pad; 22 16 int nn = rs->nn; ··· 21 27 uint16_t *alpha_to = rs->alpha_to; 22 28 uint16_t *index_of = rs->index_of; 23 29 uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error; 24 - /* Err+Eras Locator poly and syndrome poly The maximum value 25 - * of nroots is 8. So the necessary stack size will be about 26 - * 220 bytes max. 27 - */ 28 - uint16_t lambda[nroots + 1], syn[nroots]; 29 - uint16_t b[nroots + 1], t[nroots + 1], omega[nroots + 1]; 30 - uint16_t root[nroots], reg[nroots + 1], loc[nroots]; 31 30 int count = 0; 32 31 uint16_t msk = (uint16_t) rs->nn; 32 + 33 + /* 34 + * The decoder buffers are in the rs control struct. They are 35 + * arrays sized [nroots + 1] 36 + */ 37 + uint16_t *lambda = rsc->buffers + RS_DECODE_LAMBDA * (nroots + 1); 38 + uint16_t *syn = rsc->buffers + RS_DECODE_SYN * (nroots + 1); 39 + uint16_t *b = rsc->buffers + RS_DECODE_B * (nroots + 1); 40 + uint16_t *t = rsc->buffers + RS_DECODE_T * (nroots + 1); 41 + uint16_t *omega = rsc->buffers + RS_DECODE_OMEGA * (nroots + 1); 42 + uint16_t *root = rsc->buffers + RS_DECODE_ROOT * (nroots + 1); 43 + uint16_t *reg = rsc->buffers + RS_DECODE_REG * (nroots + 1); 44 + uint16_t *loc = rsc->buffers + RS_DECODE_LOC * (nroots + 1); 33 45 34 46 /* Check length parameter for validity */ 35 47 pad = nn - nroots - len;
+4 -11
lib/reed_solomon/encode_rs.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 1 2 /* 2 - * lib/reed_solomon/encode_rs.c 3 - * 4 - * Overview: 5 - * Generic Reed Solomon encoder / decoder library 3 + * Generic Reed Solomon encoder / decoder library 6 4 * 7 5 * Copyright 2002, Phil Karn, KA9Q 8 6 * May be used under the terms of the GNU General Public License (GPL) 9 7 * 10 8 * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de) 11 9 * 12 - * $Id: encode_rs.c,v 1.5 2005/11/07 11:14:59 gleixner Exp $ 13 - * 14 - */ 15 - 16 - /* Generic data width independent code which is included by the 17 - * wrappers. 18 - * int encode_rsX (struct rs_control *rs, uintX_t *data, int len, uintY_t *par) 10 + * Generic data width independent code which is included by the wrappers. 19 11 */ 20 12 { 13 + struct rs_codec *rs = rsc->codec; 21 14 int i, j, pad; 22 15 int nn = rs->nn; 23 16 int nroots = rs->nroots;
+138 -102
lib/reed_solomon/reed_solomon.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 1 2 /* 2 - * lib/reed_solomon/reed_solomon.c 3 - * 4 - * Overview: 5 - * Generic Reed Solomon encoder / decoder library 3 + * Generic Reed Solomon encoder / decoder library 6 4 * 7 5 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de) 8 6 * 9 7 * Reed Solomon code lifted from reed solomon library written by Phil Karn 10 8 * Copyright 2002 Phil Karn, KA9Q 11 9 * 12 - * $Id: rslib.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $ 13 - * 14 - * This program is free software; you can redistribute it and/or modify 15 - * it under the terms of the GNU General Public License version 2 as 16 - * published by the Free Software Foundation. 17 - * 18 10 * Description: 19 11 * 20 12 * The generic Reed Solomon library provides runtime configurable 21 13 * encoding / decoding of RS codes. 22 - * Each user must call init_rs to get a pointer to a rs_control 23 - * structure for the given rs parameters. This structure is either 24 - * generated or a already available matching control structure is used. 25 - * If a structure is generated then the polynomial arrays for 26 - * fast encoding / decoding are built. This can take some time so 27 - * make sure not to call this function from a time critical path. 28 - * Usually a module / driver should initialize the necessary 29 - * rs_control structure on module / driver init and release it 30 - * on exit. 31 - * The encoding puts the calculated syndrome into a given syndrome 32 - * buffer. 33 - * The decoding is a two step process. The first step calculates 34 - * the syndrome over the received (data + syndrome) and calls the 35 - * second stage, which does the decoding / error correction itself. 36 - * Many hw encoders provide a syndrome calculation over the received 37 - * data + syndrome and can call the second stage directly. 38 14 * 15 + * Each user must call init_rs to get a pointer to a rs_control structure 16 + * for the given rs parameters. The control struct is unique per instance. 17 + * It points to a codec which can be shared by multiple control structures. 18 + * If a codec is newly allocated then the polynomial arrays for fast 19 + * encoding / decoding are built. This can take some time so make sure not 20 + * to call this function from a time critical path. Usually a module / 21 + * driver should initialize the necessary rs_control structure on module / 22 + * driver init and release it on exit. 23 + * 24 + * The encoding puts the calculated syndrome into a given syndrome buffer. 25 + * 26 + * The decoding is a two step process. The first step calculates the 27 + * syndrome over the received (data + syndrome) and calls the second stage, 28 + * which does the decoding / error correction itself. Many hw encoders 29 + * provide a syndrome calculation over the received data + syndrome and can 30 + * call the second stage directly. 39 31 */ 40 - 41 32 #include <linux/errno.h> 42 33 #include <linux/kernel.h> 43 34 #include <linux/init.h> ··· 37 46 #include <linux/slab.h> 38 47 #include <linux/mutex.h> 39 48 40 - /* This list holds all currently allocated rs control structures */ 41 - static LIST_HEAD (rslist); 49 + enum { 50 + RS_DECODE_LAMBDA, 51 + RS_DECODE_SYN, 52 + RS_DECODE_B, 53 + RS_DECODE_T, 54 + RS_DECODE_OMEGA, 55 + RS_DECODE_ROOT, 56 + RS_DECODE_REG, 57 + RS_DECODE_LOC, 58 + RS_DECODE_NUM_BUFFERS 59 + }; 60 + 61 + /* This list holds all currently allocated rs codec structures */ 62 + static LIST_HEAD(codec_list); 42 63 /* Protection for the list */ 43 64 static DEFINE_MUTEX(rslistlock); 44 65 45 66 /** 46 - * rs_init - Initialize a Reed-Solomon codec 67 + * codec_init - Initialize a Reed-Solomon codec 47 68 * @symsize: symbol size, bits (1-8) 48 69 * @gfpoly: Field generator polynomial coefficients 49 70 * @gffunc: Field generator function 50 71 * @fcr: first root of RS code generator polynomial, index form 51 72 * @prim: primitive element to generate polynomial roots 52 73 * @nroots: RS code generator polynomial degree (number of roots) 74 + * @gfp: GFP_ flags for allocations 53 75 * 54 - * Allocate a control structure and the polynom arrays for faster 76 + * Allocate a codec structure and the polynom arrays for faster 55 77 * en/decoding. Fill the arrays according to the given parameters. 56 78 */ 57 - static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int), 58 - int fcr, int prim, int nroots) 79 + static struct rs_codec *codec_init(int symsize, int gfpoly, int (*gffunc)(int), 80 + int fcr, int prim, int nroots, gfp_t gfp) 59 81 { 60 - struct rs_control *rs; 61 82 int i, j, sr, root, iprim; 83 + struct rs_codec *rs; 62 84 63 - /* Allocate the control structure */ 64 - rs = kmalloc(sizeof (struct rs_control), GFP_KERNEL); 65 - if (rs == NULL) 85 + rs = kzalloc(sizeof(*rs), gfp); 86 + if (!rs) 66 87 return NULL; 67 88 68 89 INIT_LIST_HEAD(&rs->list); ··· 88 85 rs->gffunc = gffunc; 89 86 90 87 /* Allocate the arrays */ 91 - rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL); 88 + rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp); 92 89 if (rs->alpha_to == NULL) 93 - goto errrs; 90 + goto err; 94 91 95 - rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL); 92 + rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp); 96 93 if (rs->index_of == NULL) 97 - goto erralp; 94 + goto err; 98 95 99 - rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), GFP_KERNEL); 96 + rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), gfp); 100 97 if(rs->genpoly == NULL) 101 - goto erridx; 98 + goto err; 102 99 103 100 /* Generate Galois field lookup tables */ 104 101 rs->index_of[0] = rs->nn; /* log(zero) = -inf */ ··· 123 120 } 124 121 /* If it's not primitive, exit */ 125 122 if(sr != rs->alpha_to[0]) 126 - goto errpol; 123 + goto err; 127 124 128 125 /* Find prim-th root of 1, used in decoding */ 129 126 for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn); ··· 151 148 /* convert rs->genpoly[] to index form for quicker encoding */ 152 149 for (i = 0; i <= nroots; i++) 153 150 rs->genpoly[i] = rs->index_of[rs->genpoly[i]]; 151 + 152 + rs->users = 1; 153 + list_add(&rs->list, &codec_list); 154 154 return rs; 155 155 156 - /* Error exit */ 157 - errpol: 156 + err: 158 157 kfree(rs->genpoly); 159 - erridx: 160 158 kfree(rs->index_of); 161 - erralp: 162 159 kfree(rs->alpha_to); 163 - errrs: 164 160 kfree(rs); 165 161 return NULL; 166 162 } 167 163 168 164 169 165 /** 170 - * free_rs - Free the rs control structure, if it is no longer used 171 - * @rs: the control structure which is not longer used by the 166 + * free_rs - Free the rs control structure 167 + * @rs: The control structure which is not longer used by the 172 168 * caller 169 + * 170 + * Free the control structure. If @rs is the last user of the associated 171 + * codec, free the codec as well. 173 172 */ 174 173 void free_rs(struct rs_control *rs) 175 174 { 175 + struct rs_codec *cd; 176 + 177 + if (!rs) 178 + return; 179 + 180 + cd = rs->codec; 176 181 mutex_lock(&rslistlock); 177 - rs->users--; 178 - if(!rs->users) { 179 - list_del(&rs->list); 180 - kfree(rs->alpha_to); 181 - kfree(rs->index_of); 182 - kfree(rs->genpoly); 183 - kfree(rs); 182 + cd->users--; 183 + if(!cd->users) { 184 + list_del(&cd->list); 185 + kfree(cd->alpha_to); 186 + kfree(cd->index_of); 187 + kfree(cd->genpoly); 188 + kfree(cd); 184 189 } 185 190 mutex_unlock(&rslistlock); 191 + kfree(rs); 186 192 } 193 + EXPORT_SYMBOL_GPL(free_rs); 187 194 188 195 /** 189 - * init_rs_internal - Find a matching or allocate a new rs control structure 196 + * init_rs_internal - Allocate rs control, find a matching codec or allocate a new one 190 197 * @symsize: the symbol size (number of bits) 191 198 * @gfpoly: the extended Galois field generator polynomial coefficients, 192 199 * with the 0th coefficient in the low order bit. The polynomial ··· 204 191 * @gffunc: pointer to function to generate the next field element, 205 192 * or the multiplicative identity element if given 0. Used 206 193 * instead of gfpoly if gfpoly is 0 207 - * @fcr: the first consecutive root of the rs code generator polynomial 194 + * @fcr: the first consecutive root of the rs code generator polynomial 208 195 * in index form 209 196 * @prim: primitive element to generate polynomial roots 210 197 * @nroots: RS code generator polynomial degree (number of roots) 198 + * @gfp: GFP_ flags for allocations 211 199 */ 212 200 static struct rs_control *init_rs_internal(int symsize, int gfpoly, 213 - int (*gffunc)(int), int fcr, 214 - int prim, int nroots) 201 + int (*gffunc)(int), int fcr, 202 + int prim, int nroots, gfp_t gfp) 215 203 { 216 - struct list_head *tmp; 217 - struct rs_control *rs; 204 + struct list_head *tmp; 205 + struct rs_control *rs; 206 + unsigned int bsize; 218 207 219 208 /* Sanity checks */ 220 209 if (symsize < 1) 221 210 return NULL; 222 211 if (fcr < 0 || fcr >= (1<<symsize)) 223 - return NULL; 212 + return NULL; 224 213 if (prim <= 0 || prim >= (1<<symsize)) 225 - return NULL; 214 + return NULL; 226 215 if (nroots < 0 || nroots >= (1<<symsize)) 216 + return NULL; 217 + 218 + /* 219 + * The decoder needs buffers in each control struct instance to 220 + * avoid variable size or large fixed size allocations on 221 + * stack. Size the buffers to arrays of [nroots + 1]. 222 + */ 223 + bsize = sizeof(uint16_t) * RS_DECODE_NUM_BUFFERS * (nroots + 1); 224 + rs = kzalloc(sizeof(*rs) + bsize, gfp); 225 + if (!rs) 227 226 return NULL; 228 227 229 228 mutex_lock(&rslistlock); 230 229 231 230 /* Walk through the list and look for a matching entry */ 232 - list_for_each(tmp, &rslist) { 233 - rs = list_entry(tmp, struct rs_control, list); 234 - if (symsize != rs->mm) 231 + list_for_each(tmp, &codec_list) { 232 + struct rs_codec *cd = list_entry(tmp, struct rs_codec, list); 233 + 234 + if (symsize != cd->mm) 235 235 continue; 236 - if (gfpoly != rs->gfpoly) 236 + if (gfpoly != cd->gfpoly) 237 237 continue; 238 - if (gffunc != rs->gffunc) 238 + if (gffunc != cd->gffunc) 239 239 continue; 240 - if (fcr != rs->fcr) 240 + if (fcr != cd->fcr) 241 241 continue; 242 - if (prim != rs->prim) 242 + if (prim != cd->prim) 243 243 continue; 244 - if (nroots != rs->nroots) 244 + if (nroots != cd->nroots) 245 245 continue; 246 246 /* We have a matching one already */ 247 - rs->users++; 247 + cd->users++; 248 + rs->codec = cd; 248 249 goto out; 249 250 } 250 251 251 252 /* Create a new one */ 252 - rs = rs_init(symsize, gfpoly, gffunc, fcr, prim, nroots); 253 - if (rs) { 254 - rs->users = 1; 255 - list_add(&rs->list, &rslist); 253 + rs->codec = codec_init(symsize, gfpoly, gffunc, fcr, prim, nroots, gfp); 254 + if (!rs->codec) { 255 + kfree(rs); 256 + rs = NULL; 256 257 } 257 258 out: 258 259 mutex_unlock(&rslistlock); ··· 274 247 } 275 248 276 249 /** 277 - * init_rs - Find a matching or allocate a new rs control structure 250 + * init_rs_gfp - Create a RS control struct and initialize it 278 251 * @symsize: the symbol size (number of bits) 279 252 * @gfpoly: the extended Galois field generator polynomial coefficients, 280 253 * with the 0th coefficient in the low order bit. The polynomial 281 254 * must be primitive; 282 - * @fcr: the first consecutive root of the rs code generator polynomial 255 + * @fcr: the first consecutive root of the rs code generator polynomial 283 256 * in index form 284 257 * @prim: primitive element to generate polynomial roots 285 258 * @nroots: RS code generator polynomial degree (number of roots) 259 + * @gfp: GFP_ flags for allocations 286 260 */ 287 - struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim, 288 - int nroots) 261 + struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim, 262 + int nroots, gfp_t gfp) 289 263 { 290 - return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots); 264 + return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots, gfp); 291 265 } 266 + EXPORT_SYMBOL_GPL(init_rs_gfp); 292 267 293 268 /** 294 - * init_rs_non_canonical - Find a matching or allocate a new rs control 295 - * structure, for fields with non-canonical 296 - * representation 269 + * init_rs_non_canonical - Allocate rs control struct for fields with 270 + * non-canonical representation 297 271 * @symsize: the symbol size (number of bits) 298 272 * @gffunc: pointer to function to generate the next field element, 299 273 * or the multiplicative identity element if given 0. Used 300 274 * instead of gfpoly if gfpoly is 0 301 - * @fcr: the first consecutive root of the rs code generator polynomial 275 + * @fcr: the first consecutive root of the rs code generator polynomial 302 276 * in index form 303 277 * @prim: primitive element to generate polynomial roots 304 278 * @nroots: RS code generator polynomial degree (number of roots) 305 279 */ 306 280 struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int), 307 - int fcr, int prim, int nroots) 281 + int fcr, int prim, int nroots) 308 282 { 309 - return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots); 283 + return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots, 284 + GFP_KERNEL); 310 285 } 286 + EXPORT_SYMBOL_GPL(init_rs_non_canonical); 311 287 312 288 #ifdef CONFIG_REED_SOLOMON_ENC8 313 289 /** 314 290 * encode_rs8 - Calculate the parity for data values (8bit data width) 315 - * @rs: the rs control structure 291 + * @rsc: the rs control structure 316 292 * @data: data field of a given type 317 293 * @len: data length 318 294 * @par: parity data, must be initialized by caller (usually all 0) ··· 325 295 * symbol size > 8. The calling code must take care of encoding of the 326 296 * syndrome result for storage itself. 327 297 */ 328 - int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par, 298 + int encode_rs8(struct rs_control *rsc, uint8_t *data, int len, uint16_t *par, 329 299 uint16_t invmsk) 330 300 { 331 301 #include "encode_rs.c" ··· 336 306 #ifdef CONFIG_REED_SOLOMON_DEC8 337 307 /** 338 308 * decode_rs8 - Decode codeword (8bit data width) 339 - * @rs: the rs control structure 309 + * @rsc: the rs control structure 340 310 * @data: data field of a given type 341 311 * @par: received parity data field 342 312 * @len: data length ··· 349 319 * The syndrome and parity uses a uint16_t data type to enable 350 320 * symbol size > 8. The calling code must take care of decoding of the 351 321 * syndrome result and the received parity before calling this code. 322 + * 323 + * Note: The rs_control struct @rsc contains buffers which are used for 324 + * decoding, so the caller has to ensure that decoder invocations are 325 + * serialized. 326 + * 352 327 * Returns the number of corrected bits or -EBADMSG for uncorrectable errors. 353 328 */ 354 - int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len, 329 + int decode_rs8(struct rs_control *rsc, uint8_t *data, uint16_t *par, int len, 355 330 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, 356 331 uint16_t *corr) 357 332 { ··· 368 333 #ifdef CONFIG_REED_SOLOMON_ENC16 369 334 /** 370 335 * encode_rs16 - Calculate the parity for data values (16bit data width) 371 - * @rs: the rs control structure 336 + * @rsc: the rs control structure 372 337 * @data: data field of a given type 373 338 * @len: data length 374 339 * @par: parity data, must be initialized by caller (usually all 0) ··· 376 341 * 377 342 * Each field in the data array contains up to symbol size bits of valid data. 378 343 */ 379 - int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par, 344 + int encode_rs16(struct rs_control *rsc, uint16_t *data, int len, uint16_t *par, 380 345 uint16_t invmsk) 381 346 { 382 347 #include "encode_rs.c" ··· 387 352 #ifdef CONFIG_REED_SOLOMON_DEC16 388 353 /** 389 354 * decode_rs16 - Decode codeword (16bit data width) 390 - * @rs: the rs control structure 355 + * @rsc: the rs control structure 391 356 * @data: data field of a given type 392 357 * @par: received parity data field 393 358 * @len: data length ··· 398 363 * @corr: buffer to store correction bitmask on eras_pos 399 364 * 400 365 * Each field in the data array contains up to symbol size bits of valid data. 366 + * 367 + * Note: The rc_control struct @rsc contains buffers which are used for 368 + * decoding, so the caller has to ensure that decoder invocations are 369 + * serialized. 370 + * 401 371 * Returns the number of corrected bits or -EBADMSG for uncorrectable errors. 402 372 */ 403 - int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len, 373 + int decode_rs16(struct rs_control *rsc, uint16_t *data, uint16_t *par, int len, 404 374 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, 405 375 uint16_t *corr) 406 376 { ··· 413 373 } 414 374 EXPORT_SYMBOL_GPL(decode_rs16); 415 375 #endif 416 - 417 - EXPORT_SYMBOL_GPL(init_rs); 418 - EXPORT_SYMBOL_GPL(init_rs_non_canonical); 419 - EXPORT_SYMBOL_GPL(free_rs); 420 376 421 377 MODULE_LICENSE("GPL"); 422 378 MODULE_DESCRIPTION("Reed Solomon encoder/decoder");