rslib: Add GFP aware init function

The rslib usage in dm/verity_fec is broken because init_rs() can nest in
GFP_NOIO mempool allocations as init_rs() is invoked from the mempool alloc
callback.

Provide a variant which takes gfp_t flags as argument.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Alasdair Kergon <agk@redhat.com>
Cc: Neil Brown <neilb@suse.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
This commit is contained in:
Thomas Gleixner 2018-04-22 18:23:46 +02:00 committed by Kees Cook
parent 6d08b06e67
commit 83a530e161
2 changed files with 48 additions and 23 deletions

View File

@ -20,6 +20,8 @@
#define _RSLIB_H_ #define _RSLIB_H_
#include <linux/list.h> #include <linux/list.h>
#include <linux/types.h> /* for gfp_t */
#include <linux/gfp.h> /* for GFP_KERNEL */
/** /**
* struct rs_control - rs control structure * struct rs_control - rs control structure
@ -77,10 +79,30 @@ int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
#endif #endif
/* Create or get a matching rs control structure */ /* Create or get a matching rs control structure */
struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim, struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
int nroots); int nroots, gfp_t gfp);
/**
* init_rs - Create a RS control struct and initialize it
* @symsize: the symbol size (number of bits)
* @gfpoly: the extended Galois field generator polynomial coefficients,
* with the 0th coefficient in the low order bit. The polynomial
* must be primitive;
* @fcr: the first consecutive root of the rs code generator polynomial
* in index form
* @prim: primitive element to generate polynomial roots
* @nroots: RS code generator polynomial degree (number of roots)
*
* Allocations use GFP_KERNEL.
*/
static inline struct rs_control *init_rs(int symsize, int gfpoly, int fcr,
int prim, int nroots)
{
return init_rs_gfp(symsize, gfpoly, fcr, prim, nroots, GFP_KERNEL);
}
struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int), struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
int fcr, int prim, int nroots); int fcr, int prim, int nroots);
/* Release a rs control structure */ /* Release a rs control structure */
void free_rs(struct rs_control *rs); void free_rs(struct rs_control *rs);

View File

@ -59,19 +59,20 @@ static DEFINE_MUTEX(rslistlock);
* @fcr: first root of RS code generator polynomial, index form * @fcr: first root of RS code generator polynomial, index form
* @prim: primitive element to generate polynomial roots * @prim: primitive element to generate polynomial roots
* @nroots: RS code generator polynomial degree (number of roots) * @nroots: RS code generator polynomial degree (number of roots)
* @gfp: GFP_ flags for allocations
* *
* Allocate a control structure and the polynom arrays for faster * Allocate a control structure and the polynom arrays for faster
* en/decoding. Fill the arrays according to the given parameters. * en/decoding. Fill the arrays according to the given parameters.
*/ */
static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int), static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
int fcr, int prim, int nroots) int fcr, int prim, int nroots, gfp_t gfp)
{ {
struct rs_control *rs; struct rs_control *rs;
int i, j, sr, root, iprim; int i, j, sr, root, iprim;
/* Allocate the control structure */ /* Allocate the control structure */
rs = kmalloc(sizeof (struct rs_control), GFP_KERNEL); rs = kmalloc(sizeof(*rs), gfp);
if (rs == NULL) if (!rs)
return NULL; return NULL;
INIT_LIST_HEAD(&rs->list); INIT_LIST_HEAD(&rs->list);
@ -85,15 +86,15 @@ static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
rs->gffunc = gffunc; rs->gffunc = gffunc;
/* Allocate the arrays */ /* Allocate the arrays */
rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL); rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp);
if (rs->alpha_to == NULL) if (rs->alpha_to == NULL)
goto errrs; goto errrs;
rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL); rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp);
if (rs->index_of == NULL) if (rs->index_of == NULL)
goto erralp; goto erralp;
rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), GFP_KERNEL); rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), gfp);
if(rs->genpoly == NULL) if(rs->genpoly == NULL)
goto erridx; goto erridx;
@ -181,6 +182,7 @@ void free_rs(struct rs_control *rs)
} }
mutex_unlock(&rslistlock); mutex_unlock(&rslistlock);
} }
EXPORT_SYMBOL_GPL(free_rs);
/** /**
* init_rs_internal - Find a matching or allocate a new rs control structure * init_rs_internal - Find a matching or allocate a new rs control structure
@ -195,13 +197,14 @@ void free_rs(struct rs_control *rs)
* in index form * in index form
* @prim: primitive element to generate polynomial roots * @prim: primitive element to generate polynomial roots
* @nroots: RS code generator polynomial degree (number of roots) * @nroots: RS code generator polynomial degree (number of roots)
* @gfp: GFP_ flags for allocations
*/ */
static struct rs_control *init_rs_internal(int symsize, int gfpoly, static struct rs_control *init_rs_internal(int symsize, int gfpoly,
int (*gffunc)(int), int fcr, int (*gffunc)(int), int fcr,
int prim, int nroots) int prim, int nroots, gfp_t gfp)
{ {
struct list_head *tmp; struct list_head *tmp;
struct rs_control *rs; struct rs_control *rs;
/* Sanity checks */ /* Sanity checks */
if (symsize < 1) if (symsize < 1)
@ -236,7 +239,7 @@ static struct rs_control *init_rs_internal(int symsize, int gfpoly,
} }
/* Create a new one */ /* Create a new one */
rs = rs_init(symsize, gfpoly, gffunc, fcr, prim, nroots); rs = rs_init(symsize, gfpoly, gffunc, fcr, prim, nroots, gfp);
if (rs) { if (rs) {
rs->users = 1; rs->users = 1;
list_add(&rs->list, &rslist); list_add(&rs->list, &rslist);
@ -247,7 +250,7 @@ static struct rs_control *init_rs_internal(int symsize, int gfpoly,
} }
/** /**
* init_rs - Find a matching or allocate a new rs control structure * init_rs_gfp - Find a matching or allocate a new rs control structure
* @symsize: the symbol size (number of bits) * @symsize: the symbol size (number of bits)
* @gfpoly: the extended Galois field generator polynomial coefficients, * @gfpoly: the extended Galois field generator polynomial coefficients,
* with the 0th coefficient in the low order bit. The polynomial * with the 0th coefficient in the low order bit. The polynomial
@ -256,12 +259,14 @@ static struct rs_control *init_rs_internal(int symsize, int gfpoly,
* in index form * in index form
* @prim: primitive element to generate polynomial roots * @prim: primitive element to generate polynomial roots
* @nroots: RS code generator polynomial degree (number of roots) * @nroots: RS code generator polynomial degree (number of roots)
* @gfp: GFP_ flags for allocations
*/ */
struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim, struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
int nroots) int nroots, gfp_t gfp)
{ {
return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots); return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots, gfp);
} }
EXPORT_SYMBOL_GPL(init_rs_gfp);
/** /**
* init_rs_non_canonical - Find a matching or allocate a new rs control * init_rs_non_canonical - Find a matching or allocate a new rs control
@ -279,8 +284,10 @@ struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int), struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int),
int fcr, int prim, int nroots) int fcr, int prim, int nroots)
{ {
return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots); return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots,
GFP_KERNEL);
} }
EXPORT_SYMBOL_GPL(init_rs_non_canonical);
#ifdef CONFIG_REED_SOLOMON_ENC8 #ifdef CONFIG_REED_SOLOMON_ENC8
/** /**
@ -374,10 +381,6 @@ int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
EXPORT_SYMBOL_GPL(decode_rs16); EXPORT_SYMBOL_GPL(decode_rs16);
#endif #endif
EXPORT_SYMBOL_GPL(init_rs);
EXPORT_SYMBOL_GPL(init_rs_non_canonical);
EXPORT_SYMBOL_GPL(free_rs);
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Reed Solomon encoder/decoder"); MODULE_DESCRIPTION("Reed Solomon encoder/decoder");
MODULE_AUTHOR("Phil Karn, Thomas Gleixner"); MODULE_AUTHOR("Phil Karn, Thomas Gleixner");