linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch V3 00/10] rslib: Cleanup and VLA removal
@ 2018-04-22 16:23 Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 01/10] rslib: Add GFP aware init function Thomas Gleixner
                   ` (10 more replies)
  0 siblings, 11 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck

Kees tried to get rid of the Variable Length Arrays in the Reed-Solomon
library by replacing them with fixed length arrays on stack. Though they
are rather large and Andrew did not fall in love with that solution.

This series addresses that in a different way by splitting the rs control
structure up, so that each invocation of rs_init() returns a new instance
in which the decoder buffers are allocated. The polynom tables which build
the base for the RS codecs are still shareable between the instances to
avoid large allocations and initializations of the same data over and over.

The usage sites have been audited and fixed up where necessary to
accomodate the decoder change which forbids parallel decoder invocation for
a particular rs control instance to prevent buffer corruption.

While at it the patch set tidies up the code and converts the related files
over to use SPDX license identifiers.

Changes since V2:

   Provide a gfp aware variant of init_rs and use it in the alloc callback
   of the dm/verity_fec rs_pool mempool allocations

   Picked up Reviewed/Acked-by tags and fixed the subject line for the MTD
   specific patch.

Changes since V1:

   Simplify error path in the diskonchip code and use the proper
   function to free the decoder.

As this spawns multiple subsystems it should either go through Andrews tree
or Kees can route it with his other hardening stuff.

Thanks,

        tglx

8<---------------
 drivers/md/dm-verity-fec.c        |    2 
 drivers/mtd/nand/raw/cafe_nand.c  |    7 -
 drivers/mtd/nand/raw/diskonchip.c |   67 +++++-----
 include/linux/rslib.h             |   71 +++++++----
 lib/reed_solomon/decode_rs.c      |   34 ++---
 lib/reed_solomon/encode_rs.c      |   15 --
 lib/reed_solomon/reed_solomon.c   |  233 ++++++++++++++++++++++----------------
 7 files changed, 237 insertions(+), 192 deletions(-)

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 01/10] rslib: Add GFP aware init function
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-25  1:16   ` Stephen Rothwell
  2018-04-22 16:23 ` [patch V3 02/10] dm/verity_fec: Use GFP aware reed solomon init Thomas Gleixner
                   ` (9 subsequent siblings)
  10 siblings, 1 reply; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck, Neil Brown

[-- Attachment #1: rslib--Add-GFP-aware-init-function.patch --]
[-- Type: text/plain, Size: 5958 bytes --]

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>
---
 include/linux/rslib.h           |   26 +++++++++++++++++++++++---
 lib/reed_solomon/reed_solomon.c |   36 ++++++++++++++++++++----------------
 2 files changed, 43 insertions(+), 19 deletions(-)

--- a/include/linux/rslib.h
+++ b/include/linux/rslib.h
@@ -77,10 +77,30 @@ int decode_rs16(struct rs_control *rs, u
 #endif
 
 /* Create or get a matching rs control structure */
-struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
-			   int nroots);
+struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
+			       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),
-                                         int fcr, int prim, int nroots);
+					 int fcr, int prim, int nroots);
 
 /* Release a rs control structure */
 void free_rs(struct rs_control *rs);
--- a/lib/reed_solomon/reed_solomon.c
+++ b/lib/reed_solomon/reed_solomon.c
@@ -59,19 +59,20 @@ static DEFINE_MUTEX(rslistlock);
  * @fcr:	first root of RS code generator polynomial, index form
  * @prim:	primitive element to generate polynomial 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
  * en/decoding. Fill the arrays according to the given parameters.
  */
 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;
 	int i, j, sr, root, iprim;
 
 	/* Allocate the control structure */
-	rs = kmalloc(sizeof (struct rs_control), GFP_KERNEL);
-	if (rs == NULL)
+	rs = kmalloc(sizeof(*rs), gfp);
+	if (!rs)
 		return NULL;
 
 	INIT_LIST_HEAD(&rs->list);
@@ -85,15 +86,15 @@ static struct rs_control *rs_init(int sy
 	rs->gffunc = gffunc;
 
 	/* 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)
 		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)
 		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)
 		goto erridx;
 
@@ -195,13 +196,14 @@ void free_rs(struct rs_control *rs)
  *		in index form
  *  @prim:	primitive element to generate polynomial 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,
-                                           int (*gffunc)(int), int fcr,
-                                           int prim, int nroots)
+					   int (*gffunc)(int), int fcr,
+					   int prim, int nroots, gfp_t gfp)
 {
-	struct list_head	*tmp;
-	struct rs_control	*rs;
+	struct list_head *tmp;
+	struct rs_control *rs;
 
 	/* Sanity checks */
 	if (symsize < 1)
@@ -236,7 +238,7 @@ static struct rs_control *init_rs_intern
 	}
 
 	/* 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) {
 		rs->users = 1;
 		list_add(&rs->list, &rslist);
@@ -247,7 +249,7 @@ static struct rs_control *init_rs_intern
 }
 
 /**
- * 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)
  *  @gfpoly:	the extended Galois field generator polynomial coefficients,
  *		with the 0th coefficient in the low order bit. The polynomial
@@ -256,11 +258,12 @@ static struct rs_control *init_rs_intern
  *		in index form
  *  @prim:	primitive element to generate polynomial 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,
-                           int nroots)
+struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
+			       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);
 }
 
 /**
@@ -279,7 +282,8 @@ struct rs_control *init_rs(int symsize,
 struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int),
                                          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);
 }
 
 #ifdef CONFIG_REED_SOLOMON_ENC8

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 02/10] dm/verity_fec: Use GFP aware reed solomon init
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 01/10] rslib: Add GFP aware init function Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 03/10] rslib: Cleanup whitespace damage Thomas Gleixner
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck, Neil Brown

[-- Attachment #1: dm-verity_fec--Use-GFP-aware-reed-solomon-init.patch --]
[-- Type: text/plain, Size: 1249 bytes --]

Allocations from the rs_pool can invoke init_rs() from the mempool
allocation callback. This is problematic in fec_alloc_bufs() which invokes
mempool_alloc() with GFP_NOIO to prevent a swap deadlock because init_rs()
uses GFP_KERNEL allocations.

Switch it to init_rs_gfp() and invoke it with the gfp_t flags which are
handed in from the allocator.

Note: This is not a problem today because the rs control struct is shared
between the instances and its created when the mempool is initialized. But
the upcoming changes which switch to a rs_control struct per instance to
embed decoder buffers will trigger the swap vs. GFP_KERNEL issue.

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>
---
 drivers/md/dm-verity-fec.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/drivers/md/dm-verity-fec.c
+++ b/drivers/md/dm-verity-fec.c
@@ -570,7 +570,7 @@ static void *fec_rs_alloc(gfp_t gfp_mask
 {
 	struct dm_verity *v = (struct dm_verity *)pool_data;
 
-	return init_rs(8, 0x11d, 0, 1, v->fec->roots);
+	return init_rs_gfp(8, 0x11d, 0, 1, v->fec->roots, gfp_mask);
 }
 
 static void fec_rs_free(void *element, void *pool_data)

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 03/10] rslib: Cleanup whitespace damage
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 01/10] rslib: Add GFP aware init function Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 02/10] dm/verity_fec: Use GFP aware reed solomon init Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 04/10] rslib: Cleanup top level comments Thomas Gleixner
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck

[-- Attachment #1: rslib--Cleanup_whitespace_damage.patch --]
[-- Type: text/plain, Size: 3522 bytes --]

From: Thomas Gleixner <tglx@linutronix.de>

Instead of mixing the whitespace cleanup into functional changes, mop it up
first.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Boris Brezillon <boris.brezillon@free-electrons.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Kernel Hardening <kernel-hardening@lists.openwall.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Anton Vorontsov <anton@enomsg.org>
Cc: Colin Cross <ccross@android.com>
Cc: Andrew Morton <akpm@linuxfoundation.org>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Alasdair Kergon <agk@redhat.com>

---
 include/linux/rslib.h           |   12 ++++++------
 lib/reed_solomon/reed_solomon.c |   12 ++++++------
 2 files changed, 12 insertions(+), 12 deletions(-)

--- a/include/linux/rslib.h
+++ b/include/linux/rslib.h
@@ -39,15 +39,15 @@
  * @list:	List entry for the rs control list
 */
 struct rs_control {
-	int 		mm;
-	int 		nn;
+	int		mm;
+	int		nn;
 	uint16_t	*alpha_to;
 	uint16_t	*index_of;
 	uint16_t	*genpoly;
-	int 		nroots;
-	int 		fcr;
-	int 		prim;
-	int 		iprim;
+	int		nroots;
+	int		fcr;
+	int		prim;
+	int		iprim;
 	int		gfpoly;
 	int		(*gffunc)(int);
 	int		users;
--- a/lib/reed_solomon/reed_solomon.c
+++ b/lib/reed_solomon/reed_solomon.c
@@ -192,7 +192,7 @@ void free_rs(struct rs_control *rs)
  *  @gffunc:	pointer to function to generate the next field element,
  *		or the multiplicative identity element if given 0.  Used
  *		instead of gfpoly if gfpoly is 0
- *  @fcr:  	the first consecutive root of the rs code generator polynomial
+ *  @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)
@@ -209,9 +209,9 @@ static struct rs_control *init_rs_intern
 	if (symsize < 1)
 		return NULL;
 	if (fcr < 0 || fcr >= (1<<symsize))
-    		return NULL;
+		return NULL;
 	if (prim <= 0 || prim >= (1<<symsize))
-    		return NULL;
+		return NULL;
 	if (nroots < 0 || nroots >= (1<<symsize))
 		return NULL;
 
@@ -254,7 +254,7 @@ static struct rs_control *init_rs_intern
  *  @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
+ *  @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)
@@ -274,13 +274,13 @@ struct rs_control *init_rs_gfp(int symsi
  *  @gffunc:	pointer to function to generate the next field element,
  *		or the multiplicative identity element if given 0.  Used
  *		instead of gfpoly if gfpoly is 0
- *  @fcr:  	the first consecutive root of the rs code generator polynomial
+ *  @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)
  */
 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,
 				GFP_KERNEL);

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 04/10] rslib: Cleanup top level comments
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
                   ` (2 preceding siblings ...)
  2018-04-22 16:23 ` [patch V3 03/10] rslib: Cleanup whitespace damage Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 05/10] rslib: Add SPDX identifiers Thomas Gleixner
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck

[-- Attachment #1: rslib--Cleanup_top_level_comments.patch --]
[-- Type: text/plain, Size: 4053 bytes --]

From: Thomas Gleixner <tglx@linutronix.de>

File references and stale CVS ids are really not useful.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Boris Brezillon <boris.brezillon@free-electrons.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Kernel Hardening <kernel-hardening@lists.openwall.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Anton Vorontsov <anton@enomsg.org>
Cc: Colin Cross <ccross@android.com>
Cc: Andrew Morton <akpm@linuxfoundation.org>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Alasdair Kergon <agk@redhat.com>

---
 include/linux/rslib.h           |    7 +------
 lib/reed_solomon/decode_rs.c    |   12 ++----------
 lib/reed_solomon/encode_rs.c    |   13 ++-----------
 lib/reed_solomon/reed_solomon.c |    9 +--------
 4 files changed, 6 insertions(+), 35 deletions(-)

--- a/include/linux/rslib.h
+++ b/include/linux/rslib.h
@@ -1,16 +1,11 @@
 /*
- * include/linux/rslib.h
- *
- * Overview:
- *   Generic Reed Solomon encoder / decoder library
+ * Generic Reed Solomon encoder / decoder library
  *
  * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
  *
  * RS code lifted from reed solomon library written by Phil Karn
  * Copyright 2002 Phil Karn, KA9Q
  *
- * $Id: rslib.h,v 1.4 2005/11/07 11:14:52 gleixner Exp $
- *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
--- a/lib/reed_solomon/decode_rs.c
+++ b/lib/reed_solomon/decode_rs.c
@@ -1,20 +1,12 @@
 /*
- * lib/reed_solomon/decode_rs.c
- *
- * Overview:
- *   Generic Reed Solomon encoder / decoder library
+ * Generic Reed Solomon encoder / decoder library
  *
  * Copyright 2002, Phil Karn, KA9Q
  * May be used under the terms of the GNU General Public License (GPL)
  *
  * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
  *
- * $Id: decode_rs.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $
- *
- */
-
-/* Generic data width independent code which is included by the
- * wrappers.
+ * Generic data width independent code which is included by the wrappers.
  */
 {
 	int deg_lambda, el, deg_omega;
--- a/lib/reed_solomon/encode_rs.c
+++ b/lib/reed_solomon/encode_rs.c
@@ -1,21 +1,12 @@
 /*
- * lib/reed_solomon/encode_rs.c
- *
- * Overview:
- *   Generic Reed Solomon encoder / decoder library
+ * Generic Reed Solomon encoder / decoder library
  *
  * Copyright 2002, Phil Karn, KA9Q
  * May be used under the terms of the GNU General Public License (GPL)
  *
  * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
  *
- * $Id: encode_rs.c,v 1.5 2005/11/07 11:14:59 gleixner Exp $
- *
- */
-
-/* Generic data width independent code which is included by the
- * wrappers.
- * int encode_rsX (struct rs_control *rs, uintX_t *data, int len, uintY_t *par)
+ * Generic data width independent code which is included by the wrappers.
  */
 {
 	int i, j, pad;
--- a/lib/reed_solomon/reed_solomon.c
+++ b/lib/reed_solomon/reed_solomon.c
@@ -1,16 +1,11 @@
 /*
- * lib/reed_solomon/reed_solomon.c
- *
- * Overview:
- *   Generic Reed Solomon encoder / decoder library
+ * Generic Reed Solomon encoder / decoder library
  *
  * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
  *
  * Reed Solomon code lifted from reed solomon library written by Phil Karn
  * Copyright 2002 Phil Karn, KA9Q
  *
- * $Id: rslib.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $
- *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
@@ -35,9 +30,7 @@
  * second stage, which does the decoding / error correction itself.
  * Many hw encoders provide a syndrome calculation over the received
  * data + syndrome and can call the second stage directly.
- *
  */
-
 #include <linux/errno.h>
 #include <linux/kernel.h>
 #include <linux/init.h>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 05/10] rslib: Add SPDX identifiers
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
                   ` (3 preceding siblings ...)
  2018-04-22 16:23 ` [patch V3 04/10] rslib: Cleanup top level comments Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 06/10] rslib: Remove GPL boilerplate Thomas Gleixner
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck, Kate Stewart, Greg Kroah-Hartman

[-- Attachment #1: rslib--Add_SPDX_identifiers.patch --]
[-- Type: text/plain, Size: 2421 bytes --]

From: Thomas Gleixner <tglx@linutronix.de>

The Reed-Solomon library is based on code from Phil Karn who granted
permission to import it into the kernel under the GPL V2.

See commit 15b5423757a7 ("Shared Reed-Solomon ECC library") in the history
git tree at: git://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git

  ...
  The encoder/decoder code is lifted from the GPL'd userspace RS-library
  written by Phil Karn. I modified/wrapped it to provide the different
  functions which we need in the MTD/NAND code.
  ...    
  Signed-Off-By: Thomas Gleixner <tglx@linutronix.de>
  Signed-Off-By: David Woodhouse <dwmw2@infradead.org>
  "No objections at all. Just keep the authorship notices." -- Phil Karn

Add the proper SPDX identifiers according to
Documentation/process/license-rules.rst.


Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Boris Brezillon <boris.brezillon@free-electrons.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Kernel Hardening <kernel-hardening@lists.openwall.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Anton Vorontsov <anton@enomsg.org>
Cc: Colin Cross <ccross@android.com>
Cc: Andrew Morton <akpm@linuxfoundation.org>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Alasdair Kergon <agk@redhat.com>

---
 include/linux/rslib.h           |    1 +
 lib/reed_solomon/decode_rs.c    |    1 +
 lib/reed_solomon/encode_rs.c    |    1 +
 lib/reed_solomon/reed_solomon.c |    1 +
 4 files changed, 4 insertions(+)

--- a/include/linux/rslib.h
+++ b/include/linux/rslib.h
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Generic Reed Solomon encoder / decoder library
  *
--- a/lib/reed_solomon/decode_rs.c
+++ b/lib/reed_solomon/decode_rs.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Generic Reed Solomon encoder / decoder library
  *
--- a/lib/reed_solomon/encode_rs.c
+++ b/lib/reed_solomon/encode_rs.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Generic Reed Solomon encoder / decoder library
  *
--- a/lib/reed_solomon/reed_solomon.c
+++ b/lib/reed_solomon/reed_solomon.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Generic Reed Solomon encoder / decoder library
  *

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 06/10] rslib: Remove GPL boilerplate
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
                   ` (4 preceding siblings ...)
  2018-04-22 16:23 ` [patch V3 05/10] rslib: Add SPDX identifiers Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 07/10] rslib: Simplify error path Thomas Gleixner
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck, Kate Stewart, Greg Kroah-Hartman

[-- Attachment #1: rslib--Remove_GPL_boilerplate.patch --]
[-- Type: text/plain, Size: 2011 bytes --]

From: Thomas Gleixner <tglx@linutronix.de>

Now that SPDX identifiers are in place, remove the GPL boiler plate
text. Leave the notices which document that Phil Karn granted permission in
place (encode/decode source code). The modified files are code written for
the kernel by me.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Boris Brezillon <boris.brezillon@free-electrons.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Kernel Hardening <kernel-hardening@lists.openwall.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Anton Vorontsov <anton@enomsg.org>
Cc: Colin Cross <ccross@android.com>
Cc: Andrew Morton <akpm@linuxfoundation.org>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Alasdair Kergon <agk@redhat.com>

---
 include/linux/rslib.h           |    5 -----
 lib/reed_solomon/reed_solomon.c |    4 ----
 2 files changed, 9 deletions(-)

--- a/include/linux/rslib.h
+++ b/include/linux/rslib.h
@@ -6,12 +6,7 @@
  *
  * RS code lifted from reed solomon library written by Phil Karn
  * Copyright 2002 Phil Karn, KA9Q
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
  */
-
 #ifndef _RSLIB_H_
 #define _RSLIB_H_
 
--- a/lib/reed_solomon/reed_solomon.c
+++ b/lib/reed_solomon/reed_solomon.c
@@ -7,10 +7,6 @@
  * Reed Solomon code lifted from reed solomon library written by Phil Karn
  * Copyright 2002 Phil Karn, KA9Q
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
  * Description:
  *
  * The generic Reed Solomon library provides runtime configurable

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 07/10] rslib: Simplify error path
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
                   ` (5 preceding siblings ...)
  2018-04-22 16:23 ` [patch V3 06/10] rslib: Remove GPL boilerplate Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 08/10] rslib: Split rs control struct Thomas Gleixner
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck

[-- Attachment #1: rslib--Simplify-error-path.patch --]
[-- Type: text/plain, Size: 1841 bytes --]

The four error path labels in rs_init() can be reduced to one by allocating
the struct with kzalloc so the pointers in the struct are NULL and can be
unconditionally handed in to kfree() because they either point to an
allocation or are NULL.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 lib/reed_solomon/reed_solomon.c |   17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

--- a/lib/reed_solomon/reed_solomon.c
+++ b/lib/reed_solomon/reed_solomon.c
@@ -60,8 +60,7 @@ static struct rs_control *rs_init(int sy
 	struct rs_control *rs;
 	int i, j, sr, root, iprim;
 
-	/* Allocate the control structure */
-	rs = kmalloc(sizeof(*rs), gfp);
+	rs = kzalloc(sizeof(*rs), gfp);
 	if (!rs)
 		return NULL;
 
@@ -78,15 +77,15 @@ static struct rs_control *rs_init(int sy
 	/* Allocate the arrays */
 	rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp);
 	if (rs->alpha_to == NULL)
-		goto errrs;
+		goto err;
 
 	rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp);
 	if (rs->index_of == NULL)
-		goto erralp;
+		goto err;
 
 	rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), gfp);
 	if(rs->genpoly == NULL)
-		goto erridx;
+		goto err;
 
 	/* Generate Galois field lookup tables */
 	rs->index_of[0] = rs->nn;	/* log(zero) = -inf */
@@ -111,7 +110,7 @@ static struct rs_control *rs_init(int sy
 	}
 	/* If it's not primitive, exit */
 	if(sr != rs->alpha_to[0])
-		goto errpol;
+		goto err;
 
 	/* Find prim-th root of 1, used in decoding */
 	for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn);
@@ -141,14 +140,10 @@ static struct rs_control *rs_init(int sy
 		rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
 	return rs;
 
-	/* Error exit */
-errpol:
+err:
 	kfree(rs->genpoly);
-erridx:
 	kfree(rs->index_of);
-erralp:
 	kfree(rs->alpha_to);
-errrs:
 	kfree(rs);
 	return NULL;
 }

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 08/10] rslib: Split rs control struct
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
                   ` (6 preceding siblings ...)
  2018-04-22 16:23 ` [patch V3 07/10] rslib: Simplify error path Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 09/10] mtd: rawnand: diskonchip: Allocate rs control per instance Thomas Gleixner
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck, Boris Brezillon

[-- Attachment #1: rslib--Split_rs_control_struct.patch --]
[-- Type: text/plain, Size: 16134 bytes --]

From: Thomas Gleixner <tglx@linutronix.de>

The decoder library uses variable length arrays on stack. To get rid of
them it would be simple to allocate fixed length arrays on stack, but those
might become rather large. The other solution is to allocate the buffers in
the rs control structure, but this cannot be done as long as the structure
can be shared by several users. Sharing is desired because the RS polynom
tables are large and initialization is time consuming.

To solve this split the codec information out of the control structure and
have a pointer to a shared codec in it. Instantiate the control structure
for each user, create a new codec if no shareable is avaiable yet.  Adjust
all affected usage sites to the new scheme.

This allows to add per instance decoder buffers to the control structure
later on.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Boris Brezillon <boris.brezillon@bootlin.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Kernel Hardening <kernel-hardening@lists.openwall.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Anton Vorontsov <anton@enomsg.org>
Cc: Colin Cross <ccross@android.com>
Cc: Andrew Morton <akpm@linuxfoundation.org>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Alasdair Kergon <agk@redhat.com>

---
 drivers/mtd/nand/raw/cafe_nand.c  |    7 +
 drivers/mtd/nand/raw/diskonchip.c |    7 +
 include/linux/rslib.h             |   19 +++--
 lib/reed_solomon/decode_rs.c      |    1 
 lib/reed_solomon/encode_rs.c      |    1 
 lib/reed_solomon/reed_solomon.c   |  135 +++++++++++++++++++++-----------------
 6 files changed, 100 insertions(+), 70 deletions(-)

--- a/drivers/mtd/nand/raw/cafe_nand.c
+++ b/drivers/mtd/nand/raw/cafe_nand.c
@@ -394,12 +394,13 @@ static int cafe_nand_read_page(struct mt
 
 		for (i=0; i<8; i+=2) {
 			uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
-			syn[i] = cafe->rs->index_of[tmp & 0xfff];
-			syn[i+1] = cafe->rs->index_of[(tmp >> 16) & 0xfff];
+
+			syn[i] = cafe->rs->codec->index_of[tmp & 0xfff];
+			syn[i+1] = cafe->rs->codec->index_of[(tmp >> 16) & 0xfff];
 		}
 
 		n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0,
-		                pat);
+				pat);
 
 		for (i = 0; i < n; i++) {
 			int p = pos[i];
--- a/drivers/mtd/nand/raw/diskonchip.c
+++ b/drivers/mtd/nand/raw/diskonchip.c
@@ -140,6 +140,7 @@ static int doc_ecc_decode(struct rs_cont
 	int i, j, nerr, errpos[8];
 	uint8_t parity;
 	uint16_t ds[4], s[5], tmp, errval[8], syn[4];
+	struct rs_codec *cd = rs->codec;
 
 	memset(syn, 0, sizeof(syn));
 	/* Convert the ecc bytes into words */
@@ -160,15 +161,15 @@ static int doc_ecc_decode(struct rs_cont
 	for (j = 1; j < NROOTS; j++) {
 		if (ds[j] == 0)
 			continue;
-		tmp = rs->index_of[ds[j]];
+		tmp = cd->index_of[ds[j]];
 		for (i = 0; i < NROOTS; i++)
-			s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
+			s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)];
 	}
 
 	/* Calc syn[i] = s[i] / alpha^(v + i) */
 	for (i = 0; i < NROOTS; i++) {
 		if (s[i])
-			syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
+			syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i));
 	}
 	/* Call the decoder library */
 	nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
--- a/include/linux/rslib.h
+++ b/include/linux/rslib.h
@@ -13,7 +13,7 @@
 #include <linux/list.h>
 
 /**
- * struct rs_control - rs control structure
+ * struct rs_codec - rs codec data
  *
  * @mm:		Bits per symbol
  * @nn:		Symbols per block (= (1<<mm)-1)
@@ -27,9 +27,9 @@
  * @gfpoly:	The primitive generator polynominal
  * @gffunc:	Function to generate the field, if non-canonical representation
  * @users:	Users of this structure
- * @list:	List entry for the rs control list
+ * @list:	List entry for the rs codec list
 */
-struct rs_control {
+struct rs_codec {
 	int		mm;
 	int		nn;
 	uint16_t	*alpha_to;
@@ -45,6 +45,14 @@ struct rs_control {
 	struct list_head list;
 };
 
+/**
+ * struct rs_control - rs control structure per instance
+ * @codec:	The codec used for this instance
+ */
+struct rs_control {
+	struct rs_codec	*codec;
+};
+
 /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit  */
 #ifdef CONFIG_REED_SOLOMON_ENC8
 int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
@@ -67,7 +75,6 @@ int decode_rs16(struct rs_control *rs, u
 		uint16_t *corr);
 #endif
 
-/* Create or get a matching rs control structure */
 struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
 			       int nroots, gfp_t gfp);
 
@@ -98,7 +105,7 @@ void free_rs(struct rs_control *rs);
 
 /** modulo replacement for galois field arithmetics
  *
- *  @rs:	the rs control structure
+ *  @rs:	Pointer to the RS codec
  *  @x:		the value to reduce
  *
  *  where
@@ -108,7 +115,7 @@ void free_rs(struct rs_control *rs);
  *  Simple arithmetic modulo would return a wrong result for values
  *  >= 3 * rs->nn
 */
-static inline int rs_modnn(struct rs_control *rs, int x)
+static inline int rs_modnn(struct rs_codec *rs, int x)
 {
 	while (x >= rs->nn) {
 		x -= rs->nn;
--- a/lib/reed_solomon/decode_rs.c
+++ b/lib/reed_solomon/decode_rs.c
@@ -10,6 +10,7 @@
  * Generic data width independent code which is included by the wrappers.
  */
 {
+	struct rs_codec *rs = rsc->codec;
 	int deg_lambda, el, deg_omega;
 	int i, j, r, k, pad;
 	int nn = rs->nn;
--- a/lib/reed_solomon/encode_rs.c
+++ b/lib/reed_solomon/encode_rs.c
@@ -10,6 +10,7 @@
  * Generic data width independent code which is included by the wrappers.
  */
 {
+	struct rs_codec *rs = rsc->codec;
 	int i, j, pad;
 	int nn = rs->nn;
 	int nroots = rs->nroots;
--- a/lib/reed_solomon/reed_solomon.c
+++ b/lib/reed_solomon/reed_solomon.c
@@ -11,22 +11,23 @@
  *
  * The generic Reed Solomon library provides runtime configurable
  * encoding / decoding of RS codes.
- * Each user must call init_rs to get a pointer to a rs_control
- * structure for the given rs parameters. This structure is either
- * generated or a already available matching control structure is used.
- * If a structure is generated then the polynomial arrays for
- * fast encoding / decoding are built. This can take some time so
- * make sure not to call this function from a time critical path.
- * Usually a module / driver should initialize the necessary
- * rs_control structure on module / driver init and release it
- * on exit.
- * The encoding puts the calculated syndrome into a given syndrome
- * buffer.
- * The decoding is a two step process. The first step calculates
- * the syndrome over the received (data + syndrome) and calls the
- * second stage, which does the decoding / error correction itself.
- * Many hw encoders provide a syndrome calculation over the received
- * data + syndrome and can call the second stage directly.
+ *
+ * Each user must call init_rs to get a pointer to a rs_control structure
+ * for the given rs parameters. The control struct is unique per instance.
+ * It points to a codec which can be shared by multiple control structures.
+ * If a codec is newly allocated then the polynomial arrays for fast
+ * encoding / decoding are built. This can take some time so make sure not
+ * to call this function from a time critical path.  Usually a module /
+ * driver should initialize the necessary rs_control structure on module /
+ * driver init and release it on exit.
+ *
+ * The encoding puts the calculated syndrome into a given syndrome buffer.
+ *
+ * The decoding is a two step process. The first step calculates the
+ * syndrome over the received (data + syndrome) and calls the second stage,
+ * which does the decoding / error correction itself.  Many hw encoders
+ * provide a syndrome calculation over the received data + syndrome and can
+ * call the second stage directly.
  */
 #include <linux/errno.h>
 #include <linux/kernel.h>
@@ -36,13 +37,13 @@
 #include <linux/slab.h>
 #include <linux/mutex.h>
 
-/* This list holds all currently allocated rs control structures */
-static LIST_HEAD (rslist);
+/* This list holds all currently allocated rs codec structures */
+static LIST_HEAD(codec_list);
 /* Protection for the list */
 static DEFINE_MUTEX(rslistlock);
 
 /**
- * rs_init - Initialize a Reed-Solomon codec
+ * codec_init - Initialize a Reed-Solomon codec
  * @symsize:	symbol size, bits (1-8)
  * @gfpoly:	Field generator polynomial coefficients
  * @gffunc:	Field generator function
@@ -51,14 +52,14 @@ static DEFINE_MUTEX(rslistlock);
  * @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 codec structure and the polynom arrays for faster
  * en/decoding. Fill the arrays according to the given parameters.
  */
-static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
-				  int fcr, int prim, int nroots, gfp_t gfp)
+static struct rs_codec *codec_init(int symsize, int gfpoly, int (*gffunc)(int),
+				   int fcr, int prim, int nroots, gfp_t gfp)
 {
-	struct rs_control *rs;
 	int i, j, sr, root, iprim;
+	struct rs_codec *rs;
 
 	rs = kzalloc(sizeof(*rs), gfp);
 	if (!rs)
@@ -138,6 +139,9 @@ static struct rs_control *rs_init(int sy
 	/* convert rs->genpoly[] to index form for quicker encoding */
 	for (i = 0; i <= nroots; i++)
 		rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
+
+	rs->users = 1;
+	list_add(&rs->list, &codec_list);
 	return rs;
 
 err:
@@ -150,26 +154,36 @@ static struct rs_control *rs_init(int sy
 
 
 /**
- *  free_rs - Free the rs control structure, if it is no longer used
- *  @rs:	the control structure which is not longer used by the
+ *  free_rs - Free the rs control structure
+ *  @rs:	The control structure which is not longer used by the
  *		caller
+ *
+ * Free the control structure. If @rs is the last user of the associated
+ * codec, free the codec as well.
  */
 void free_rs(struct rs_control *rs)
 {
+	struct rs_codec *cd;
+
+	if (!rs)
+		return;
+
+	cd = rs->codec;
 	mutex_lock(&rslistlock);
-	rs->users--;
-	if(!rs->users) {
-		list_del(&rs->list);
-		kfree(rs->alpha_to);
-		kfree(rs->index_of);
-		kfree(rs->genpoly);
-		kfree(rs);
+	cd->users--;
+	if(!cd->users) {
+		list_del(&cd->list);
+		kfree(cd->alpha_to);
+		kfree(cd->index_of);
+		kfree(cd->genpoly);
+		kfree(cd);
 	}
 	mutex_unlock(&rslistlock);
+	kfree(rs);
 }
 
 /**
- * init_rs_internal - Find a matching or allocate a new rs control structure
+ * init_rs_internal - Allocate rs control, find a matching codec or allocate a new one
  *  @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
@@ -200,33 +214,39 @@ static struct rs_control *init_rs_intern
 	if (nroots < 0 || nroots >= (1<<symsize))
 		return NULL;
 
+	rs = kzalloc(sizeof(*rs), GFP_KERNEL);
+	if (!rs)
+		return NULL;
+
 	mutex_lock(&rslistlock);
 
 	/* Walk through the list and look for a matching entry */
-	list_for_each(tmp, &rslist) {
-		rs = list_entry(tmp, struct rs_control, list);
-		if (symsize != rs->mm)
+	list_for_each(tmp, &codec_list) {
+		struct rs_codec *cd = list_entry(tmp, struct rs_codec, list);
+
+		if (symsize != cd->mm)
 			continue;
-		if (gfpoly != rs->gfpoly)
+		if (gfpoly != cd->gfpoly)
 			continue;
-		if (gffunc != rs->gffunc)
+		if (gffunc != cd->gffunc)
 			continue;
-		if (fcr != rs->fcr)
+		if (fcr != cd->fcr)
 			continue;
-		if (prim != rs->prim)
+		if (prim != cd->prim)
 			continue;
-		if (nroots != rs->nroots)
+		if (nroots != cd->nroots)
 			continue;
 		/* We have a matching one already */
-		rs->users++;
+		cd->users++;
+		rs->codec = cd;
 		goto out;
 	}
 
 	/* Create a new one */
-	rs = rs_init(symsize, gfpoly, gffunc, fcr, prim, nroots, gfp);
-	if (rs) {
-		rs->users = 1;
-		list_add(&rs->list, &rslist);
+	rs->codec = codec_init(symsize, gfpoly, gffunc, fcr, prim, nroots, gfp);
+	if (!rs->codec) {
+		kfree(rs);
+		rs = NULL;
 	}
 out:
 	mutex_unlock(&rslistlock);
@@ -234,7 +254,7 @@ static struct rs_control *init_rs_intern
 }
 
 /**
- * init_rs_gfp - Find a matching or allocate a new rs control structure
+ * init_rs_gfp - 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
@@ -252,9 +272,8 @@ struct rs_control *init_rs_gfp(int symsi
 }
 
 /**
- * init_rs_non_canonical - Find a matching or allocate a new rs control
- *                         structure, for fields with non-canonical
- *                         representation
+ * init_rs_non_canonical - Allocate rs control struct for fields with
+ *                         non-canonical representation
  *  @symsize:	the symbol size (number of bits)
  *  @gffunc:	pointer to function to generate the next field element,
  *		or the multiplicative identity element if given 0.  Used
@@ -274,7 +293,7 @@ struct rs_control *init_rs_non_canonical
 #ifdef CONFIG_REED_SOLOMON_ENC8
 /**
  *  encode_rs8 - Calculate the parity for data values (8bit data width)
- *  @rs:	the rs control structure
+ *  @rsc:	the rs control structure
  *  @data:	data field of a given type
  *  @len:	data length
  *  @par:	parity data, must be initialized by caller (usually all 0)
@@ -284,7 +303,7 @@ struct rs_control *init_rs_non_canonical
  *  symbol size > 8. The calling code must take care of encoding of the
  *  syndrome result for storage itself.
  */
-int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
+int encode_rs8(struct rs_control *rsc, uint8_t *data, int len, uint16_t *par,
 	       uint16_t invmsk)
 {
 #include "encode_rs.c"
@@ -295,7 +314,7 @@ EXPORT_SYMBOL_GPL(encode_rs8);
 #ifdef CONFIG_REED_SOLOMON_DEC8
 /**
  *  decode_rs8 - Decode codeword (8bit data width)
- *  @rs:	the rs control structure
+ *  @rsc:	the rs control structure
  *  @data:	data field of a given type
  *  @par:	received parity data field
  *  @len:	data length
@@ -310,7 +329,7 @@ EXPORT_SYMBOL_GPL(encode_rs8);
  *  syndrome result and the received parity before calling this code.
  *  Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
  */
-int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
+int decode_rs8(struct rs_control *rsc, uint8_t *data, uint16_t *par, int len,
 	       uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
 	       uint16_t *corr)
 {
@@ -322,7 +341,7 @@ EXPORT_SYMBOL_GPL(decode_rs8);
 #ifdef CONFIG_REED_SOLOMON_ENC16
 /**
  *  encode_rs16 - Calculate the parity for data values (16bit data width)
- *  @rs:	the rs control structure
+ *  @rsc:	the rs control structure
  *  @data:	data field of a given type
  *  @len:	data length
  *  @par:	parity data, must be initialized by caller (usually all 0)
@@ -330,7 +349,7 @@ EXPORT_SYMBOL_GPL(decode_rs8);
  *
  *  Each field in the data array contains up to symbol size bits of valid data.
  */
-int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
+int encode_rs16(struct rs_control *rsc, uint16_t *data, int len, uint16_t *par,
 	uint16_t invmsk)
 {
 #include "encode_rs.c"
@@ -341,7 +360,7 @@ EXPORT_SYMBOL_GPL(encode_rs16);
 #ifdef CONFIG_REED_SOLOMON_DEC16
 /**
  *  decode_rs16 - Decode codeword (16bit data width)
- *  @rs:	the rs control structure
+ *  @rsc:	the rs control structure
  *  @data:	data field of a given type
  *  @par:	received parity data field
  *  @len:	data length
@@ -354,7 +373,7 @@ EXPORT_SYMBOL_GPL(encode_rs16);
  *  Each field in the data array contains up to symbol size bits of valid data.
  *  Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
  */
-int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
+int decode_rs16(struct rs_control *rsc, uint16_t *data, uint16_t *par, int len,
 		uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
 		uint16_t *corr)
 {

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 09/10] mtd: rawnand: diskonchip: Allocate rs control per instance
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
                   ` (7 preceding siblings ...)
  2018-04-22 16:23 ` [patch V3 08/10] rslib: Split rs control struct Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-22 16:23 ` [patch V3 10/10] rslib: Allocate decoder buffers to avoid VLAs Thomas Gleixner
  2018-04-24 16:42 ` [patch V3 00/10] rslib: Cleanup and VLA removal Kees Cook
  10 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck, Boris Brezillon

[-- Attachment #1: mtd-diskonchip--Allocate_rs_control_per_instance.patch --]
[-- Type: text/plain, Size: 5232 bytes --]

From: Thomas Gleixner <tglx@linutronix.de>

The reed solomon library is moving the on stack decoder buffers into the rs
control structure. That would break the DoC driver because multiple
instances share the same control structure and can operate in parallel. At
least in theory....

Instantiate a rs control instance per DoC device to avoid that. The per
instance buffer is fine as the operation on a single DoC instance is
serialized by the MTD/NAND core.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Boris Brezillon <boris.brezillon@bootlin.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Kernel Hardening <kernel-hardening@lists.openwall.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Anton Vorontsov <anton@enomsg.org>
Cc: Colin Cross <ccross@android.com>
Cc: Andrew Morton <akpm@linuxfoundation.org>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Alasdair Kergon <agk@redhat.com>

---
 drivers/mtd/nand/raw/diskonchip.c |   60 +++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 33 deletions(-)

--- a/drivers/mtd/nand/raw/diskonchip.c
+++ b/drivers/mtd/nand/raw/diskonchip.c
@@ -66,6 +66,7 @@ struct doc_priv {
 	int curchip;
 	int mh0_page;
 	int mh1_page;
+	struct rs_control *rs_decoder;
 	struct mtd_info *nextdoc;
 
 	/* Handle the last stage of initialization (BBT scan, partitioning) */
@@ -123,9 +124,6 @@ MODULE_PARM_DESC(doc_config_location, "P
 /* Number of symbols */
 #define NN 1023
 
-/* the Reed Solomon control structure */
-static struct rs_control *rs_decoder;
-
 /*
  * The HW decoder in the DoC ASIC's provides us a error syndrome,
  * which we must convert to a standard syndrome usable by the generic
@@ -931,7 +929,7 @@ static int doc200x_correct_data(struct m
 				calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
 		}
 
-		ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
+		ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc);
 		if (ret > 0)
 			pr_err("doc200x_correct_data corrected %d errors\n",
 			       ret);
@@ -1422,10 +1420,10 @@ static inline int __init doc2001plus_ini
 
 static int __init doc_probe(unsigned long physadr)
 {
+	struct nand_chip *nand = NULL;
+	struct doc_priv *doc = NULL;
 	unsigned char ChipID;
 	struct mtd_info *mtd;
-	struct nand_chip *nand;
-	struct doc_priv *doc;
 	void __iomem *virtadr;
 	unsigned char save_control;
 	unsigned char tmp, tmpb, tmpc;
@@ -1562,8 +1560,25 @@ static int __init doc_probe(unsigned lon
 		goto fail;
 	}
 
+
+	/*
+	 * Allocate a RS codec instance
+	 *
+	 * Symbolsize is 10 (bits)
+	 * Primitve polynomial is x^10+x^3+1
+	 * First consecutive root is 510
+	 * Primitve element to generate roots = 1
+	 * Generator polinomial degree = 4
+	 */
+	doc = (struct doc_priv *) (nand + 1);
+	doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
+	if (!doc->rs_decoder) {
+		pr_err("DiskOnChip: Could not create a RS codec\n");
+		ret = -ENOMEM;
+		goto fail;
+	}
+
 	mtd			= nand_to_mtd(nand);
-	doc			= (struct doc_priv *) (nand + 1);
 	nand->bbt_td		= (struct nand_bbt_descr *) (doc + 1);
 	nand->bbt_md		= nand->bbt_td + 1;
 
@@ -1613,7 +1628,6 @@ static int __init doc_probe(unsigned lon
 		   haven't yet added it.  This is handled without incident by
 		   mtd_device_unregister, as far as I can tell. */
 		nand_release(mtd);
-		kfree(nand);
 		goto fail;
 	}
 
@@ -1626,6 +1640,9 @@ static int __init doc_probe(unsigned lon
 	   actually a DiskOnChip.  */
 	WriteDOC(save_control, virtadr, DOCControl);
  fail:
+	if (doc)
+		free_rs(doc->rs_decoder);
+	kfree(nand);
 	iounmap(virtadr);
 
 error_ioremap:
@@ -1648,6 +1665,7 @@ static void release_nanddoc(void)
 		nand_release(mtd);
 		iounmap(doc->virtadr);
 		release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
+		free_rs(doc->rs_decoder);
 		kfree(nand);
 	}
 }
@@ -1656,27 +1674,12 @@ static int __init init_nanddoc(void)
 {
 	int i, ret = 0;
 
-	/* We could create the decoder on demand, if memory is a concern.
-	 * This way we have it handy, if an error happens
-	 *
-	 * Symbolsize is 10 (bits)
-	 * Primitve polynomial is x^10+x^3+1
-	 * first consecutive root is 510
-	 * primitve element to generate roots = 1
-	 * generator polinomial degree = 4
-	 */
-	rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
-	if (!rs_decoder) {
-		pr_err("DiskOnChip: Could not create a RS decoder\n");
-		return -ENOMEM;
-	}
-
 	if (doc_config_location) {
 		pr_info("Using configured DiskOnChip probe address 0x%lx\n",
 			doc_config_location);
 		ret = doc_probe(doc_config_location);
 		if (ret < 0)
-			goto outerr;
+			return ret;
 	} else {
 		for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
 			doc_probe(doc_locations[i]);
@@ -1687,11 +1690,7 @@ static int __init init_nanddoc(void)
 	if (!doclist) {
 		pr_info("No valid DiskOnChip devices found\n");
 		ret = -ENODEV;
-		goto outerr;
 	}
-	return 0;
- outerr:
-	free_rs(rs_decoder);
 	return ret;
 }
 
@@ -1699,11 +1698,6 @@ static void __exit cleanup_nanddoc(void)
 {
 	/* Cleanup the nand/DoC resources */
 	release_nanddoc();
-
-	/* Free the reed solomon resources */
-	if (rs_decoder) {
-		free_rs(rs_decoder);
-	}
 }
 
 module_init(init_nanddoc);

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [patch V3 10/10] rslib: Allocate decoder buffers to avoid VLAs
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
                   ` (8 preceding siblings ...)
  2018-04-22 16:23 ` [patch V3 09/10] mtd: rawnand: diskonchip: Allocate rs control per instance Thomas Gleixner
@ 2018-04-22 16:23 ` Thomas Gleixner
  2018-04-24 16:42 ` [patch V3 00/10] rslib: Cleanup and VLA removal Kees Cook
  10 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-22 16:23 UTC (permalink / raw)
  To: LKML
  Cc: Kees Cook, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck

[-- Attachment #1: rslib--Allocate_decoder_buffers_to_avoid_VLAs.patch --]
[-- Type: text/plain, Size: 5392 bytes --]

From: Thomas Gleixner <tglx@linutronix.de>

To get rid of the variable length arrays on stack in the RS decoder it's
necessary to allocate the decoder buffers per control structure instance.

All usage sites have been checked for potential parallel decoder usage and
fixed where necessary. Kees confirmed that the pstore decoding is strictly
single threaded so there should be no surprises.

Allocate them in the rs control structure sized depending on the number of
roots for the chosen codec and adapt the decoder code to make use of them.

Document the fact that decode operations based on a particular rs control
instance cannot run in parallel and the caller has to ensure that as it's
not possible to provide a proper locking construct which fits all use
cases.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Kees Cook <keescook@chromium.org>
Cc: Boris Brezillon <boris.brezillon@free-electrons.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Kernel Hardening <kernel-hardening@lists.openwall.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Anton Vorontsov <anton@enomsg.org>
Cc: Colin Cross <ccross@android.com>
Cc: Andrew Morton <akpm@linuxfoundation.org>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Alasdair Kergon <agk@redhat.com>

---
 include/linux/rslib.h           |    1 +
 lib/reed_solomon/decode_rs.c    |   20 +++++++++++++-------
 lib/reed_solomon/reed_solomon.c |   31 ++++++++++++++++++++++++++++++-
 3 files changed, 44 insertions(+), 8 deletions(-)

--- a/include/linux/rslib.h
+++ b/include/linux/rslib.h
@@ -51,6 +51,7 @@ struct rs_codec {
  */
 struct rs_control {
 	struct rs_codec	*codec;
+	uint16_t	buffers[0];
 };
 
 /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit  */
--- a/lib/reed_solomon/decode_rs.c
+++ b/lib/reed_solomon/decode_rs.c
@@ -21,16 +21,22 @@
 	uint16_t *alpha_to = rs->alpha_to;
 	uint16_t *index_of = rs->index_of;
 	uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error;
-	/* Err+Eras Locator poly and syndrome poly The maximum value
-	 * of nroots is 8. So the necessary stack size will be about
-	 * 220 bytes max.
-	 */
-	uint16_t lambda[nroots + 1], syn[nroots];
-	uint16_t b[nroots + 1], t[nroots + 1], omega[nroots + 1];
-	uint16_t root[nroots], reg[nroots + 1], loc[nroots];
 	int count = 0;
 	uint16_t msk = (uint16_t) rs->nn;
 
+	/*
+	 * The decoder buffers are in the rs control struct. They are
+	 * arrays sized [nroots + 1]
+	 */
+	uint16_t *lambda = rsc->buffers + RS_DECODE_LAMBDA * (nroots + 1);
+	uint16_t *syn = rsc->buffers + RS_DECODE_SYN * (nroots + 1);
+	uint16_t *b = rsc->buffers + RS_DECODE_B * (nroots + 1);
+	uint16_t *t = rsc->buffers + RS_DECODE_T * (nroots + 1);
+	uint16_t *omega = rsc->buffers + RS_DECODE_OMEGA * (nroots + 1);
+	uint16_t *root = rsc->buffers + RS_DECODE_ROOT * (nroots + 1);
+	uint16_t *reg = rsc->buffers + RS_DECODE_REG * (nroots + 1);
+	uint16_t *loc = rsc->buffers + RS_DECODE_LOC * (nroots + 1);
+
 	/* Check length parameter for validity */
 	pad = nn - nroots - len;
 	BUG_ON(pad < 0 || pad >= nn);
--- a/lib/reed_solomon/reed_solomon.c
+++ b/lib/reed_solomon/reed_solomon.c
@@ -37,6 +37,18 @@
 #include <linux/slab.h>
 #include <linux/mutex.h>
 
+enum {
+	RS_DECODE_LAMBDA,
+	RS_DECODE_SYN,
+	RS_DECODE_B,
+	RS_DECODE_T,
+	RS_DECODE_OMEGA,
+	RS_DECODE_ROOT,
+	RS_DECODE_REG,
+	RS_DECODE_LOC,
+	RS_DECODE_NUM_BUFFERS
+};
+
 /* This list holds all currently allocated rs codec structures */
 static LIST_HEAD(codec_list);
 /* Protection for the list */
@@ -203,6 +215,7 @@ static struct rs_control *init_rs_intern
 {
 	struct list_head *tmp;
 	struct rs_control *rs;
+	unsigned int bsize;
 
 	/* Sanity checks */
 	if (symsize < 1)
@@ -214,7 +227,13 @@ static struct rs_control *init_rs_intern
 	if (nroots < 0 || nroots >= (1<<symsize))
 		return NULL;
 
-	rs = kzalloc(sizeof(*rs), GFP_KERNEL);
+	/*
+	 * The decoder needs buffers in each control struct instance to
+	 * avoid variable size or large fixed size allocations on
+	 * stack. Size the buffers to arrays of [nroots + 1].
+	 */
+	bsize = sizeof(uint16_t) * RS_DECODE_NUM_BUFFERS * (nroots + 1);
+	rs = kzalloc(sizeof(*rs) + bsize, gfp);
 	if (!rs)
 		return NULL;
 
@@ -327,6 +346,11 @@ EXPORT_SYMBOL_GPL(encode_rs8);
  *  The syndrome and parity uses a uint16_t data type to enable
  *  symbol size > 8. The calling code must take care of decoding of the
  *  syndrome result and the received parity before calling this code.
+ *
+ *  Note: The rs_control struct @rsc contains buffers which are used for
+ *  decoding, so the caller has to ensure that decoder invocations are
+ *  serialized.
+ *
  *  Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
  */
 int decode_rs8(struct rs_control *rsc, uint8_t *data, uint16_t *par, int len,
@@ -371,6 +395,11 @@ EXPORT_SYMBOL_GPL(encode_rs16);
  *  @corr:	buffer to store correction bitmask on eras_pos
  *
  *  Each field in the data array contains up to symbol size bits of valid data.
+ *
+ *  Note: The rc_control struct @rsc contains buffers which are used for
+ *  decoding, so the caller has to ensure that decoder invocations are
+ *  serialized.
+ *
  *  Returns the number of corrected bits or -EBADMSG for uncorrectable errors.
  */
 int decode_rs16(struct rs_control *rsc, uint16_t *data, uint16_t *par, int len,

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [patch V3 00/10] rslib: Cleanup and VLA removal
  2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
                   ` (9 preceding siblings ...)
  2018-04-22 16:23 ` [patch V3 10/10] rslib: Allocate decoder buffers to avoid VLAs Thomas Gleixner
@ 2018-04-24 16:42 ` Kees Cook
  10 siblings, 0 replies; 15+ messages in thread
From: Kees Cook @ 2018-04-24 16:42 UTC (permalink / raw)
  To: Thomas Gleixner
  Cc: LKML, Segher Boessenkool, Kernel Hardening, Andrew Morton,
	Boris Brezillon, Richard Weinberger, David Woodhouse,
	Alasdair Kergon, Mike Snitzer, Anton Vorontsov, Colin Cross,
	Tony Luck

On Sun, Apr 22, 2018 at 9:23 AM, Thomas Gleixner <tglx@linutronix.de> wrote:
> Kees tried to get rid of the Variable Length Arrays in the Reed-Solomon
> library by replacing them with fixed length arrays on stack. Though they
> are rather large and Andrew did not fall in love with that solution.
>
> This series addresses that in a different way by splitting the rs control
> structure up, so that each invocation of rs_init() returns a new instance
> in which the decoder buffers are allocated. The polynom tables which build
> the base for the RS codecs are still shareable between the instances to
> avoid large allocations and initializations of the same data over and over.
>
> The usage sites have been audited and fixed up where necessary to
> accomodate the decoder change which forbids parallel decoder invocation for
> a particular rs control instance to prevent buffer corruption.
>
> While at it the patch set tidies up the code and converts the related files
> over to use SPDX license identifiers.
>
> Changes since V2:
>
>    Provide a gfp aware variant of init_rs and use it in the alloc callback
>    of the dm/verity_fec rs_pool mempool allocations
>
>    Picked up Reviewed/Acked-by tags and fixed the subject line for the MTD
>    specific patch.

Excellent, thanks!

> Changes since V1:
>
>    Simplify error path in the diskonchip code and use the proper
>    function to free the decoder.
>
> As this spawns multiple subsystems it should either go through Andrews tree
> or Kees can route it with his other hardening stuff.

I'll create a VLA sub-tree to my KSPP stuff and start collecting this
and other VLA fixes that maintainers haven't taken yet. Once
build-testing finishes, I'll push it out for -next.

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [patch V3 01/10] rslib: Add GFP aware init function
  2018-04-22 16:23 ` [patch V3 01/10] rslib: Add GFP aware init function Thomas Gleixner
@ 2018-04-25  1:16   ` Stephen Rothwell
  2018-04-25  2:45     ` Kees Cook
  0 siblings, 1 reply; 15+ messages in thread
From: Stephen Rothwell @ 2018-04-25  1:16 UTC (permalink / raw)
  To: Thomas Gleixner
  Cc: LKML, Kees Cook, Segher Boessenkool, Kernel Hardening,
	Andrew Morton, Boris Brezillon, Richard Weinberger,
	David Woodhouse, Alasdair Kergon, Mike Snitzer, Anton Vorontsov,
	Colin Cross, Tony Luck, Neil Brown

[-- Attachment #1: Type: text/plain, Size: 2173 bytes --]

Hi Thomas,

On Sun, 22 Apr 2018 18:23:46 +0200 Thomas Gleixner <tglx@linutronix.de> wrote:
>
> 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>
> ---
>  include/linux/rslib.h           |   26 +++++++++++++++++++++++---
>  lib/reed_solomon/reed_solomon.c |   36 ++++++++++++++++++++----------------
>  2 files changed, 43 insertions(+), 19 deletions(-)
> 
> --- a/include/linux/rslib.h
> +++ b/include/linux/rslib.h
> @@ -77,10 +77,30 @@ int decode_rs16(struct rs_control *rs, u
>  #endif
>  
>  /* Create or get a matching rs control structure */
> -struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
> -			   int nroots);
> +struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
> +			       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);
> +}
> +

The version of this patch that Kees has committed to his kspp tree in linux-next has

#include <linux/slab.h>

why not just

#include <linux/types.h>	/* for gpf_t */
#include <linux/gpf.h>		/* for GFP_KERNEL */

?
-- 
Cheers,
Stephen Rothwell

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [patch V3 01/10] rslib: Add GFP aware init function
  2018-04-25  1:16   ` Stephen Rothwell
@ 2018-04-25  2:45     ` Kees Cook
  2018-04-25  8:59       ` Thomas Gleixner
  0 siblings, 1 reply; 15+ messages in thread
From: Kees Cook @ 2018-04-25  2:45 UTC (permalink / raw)
  To: Stephen Rothwell
  Cc: Thomas Gleixner, LKML, Segher Boessenkool, Kernel Hardening,
	Andrew Morton, Boris Brezillon, Richard Weinberger,
	David Woodhouse, Alasdair Kergon, Mike Snitzer, Anton Vorontsov,
	Colin Cross, Tony Luck, Neil Brown

On Tue, Apr 24, 2018 at 6:16 PM, Stephen Rothwell <sfr@canb.auug.org.au> wrote:
> Hi Thomas,
>
> On Sun, 22 Apr 2018 18:23:46 +0200 Thomas Gleixner <tglx@linutronix.de> wrote:
>>
>> 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>
>> ---
>>  include/linux/rslib.h           |   26 +++++++++++++++++++++++---
>>  lib/reed_solomon/reed_solomon.c |   36 ++++++++++++++++++++----------------
>>  2 files changed, 43 insertions(+), 19 deletions(-)
>>
>> --- a/include/linux/rslib.h
>> +++ b/include/linux/rslib.h
>> @@ -77,10 +77,30 @@ int decode_rs16(struct rs_control *rs, u
>>  #endif
>>
>>  /* Create or get a matching rs control structure */
>> -struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
>> -                        int nroots);
>> +struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
>> +                            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);
>> +}
>> +
>
> The version of this patch that Kees has committed to his kspp tree in linux-next has
>
> #include <linux/slab.h>
>
> why not just
>
> #include <linux/types.h>        /* for gpf_t */
> #include <linux/gpf.h>          /* for GFP_KERNEL */
>
> ?

We could certainly switch to that. (Why doesn't gfp.h include types.h?)

-Kees


-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [patch V3 01/10] rslib: Add GFP aware init function
  2018-04-25  2:45     ` Kees Cook
@ 2018-04-25  8:59       ` Thomas Gleixner
  0 siblings, 0 replies; 15+ messages in thread
From: Thomas Gleixner @ 2018-04-25  8:59 UTC (permalink / raw)
  To: Kees Cook
  Cc: Stephen Rothwell, LKML, Segher Boessenkool, Kernel Hardening,
	Andrew Morton, Boris Brezillon, Richard Weinberger,
	David Woodhouse, Alasdair Kergon, Mike Snitzer, Anton Vorontsov,
	Colin Cross, Tony Luck, Neil Brown

On Tue, 24 Apr 2018, Kees Cook wrote:
> On Tue, Apr 24, 2018 at 6:16 PM, Stephen Rothwell <sfr@canb.auug.org.au> wrote:
> > On Sun, 22 Apr 2018 18:23:46 +0200 Thomas Gleixner <tglx@linutronix.de> wrote:
> >> +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);
> >> +}
> >> +
> >
> > The version of this patch that Kees has committed to his kspp tree in linux-next has
> >
> > #include <linux/slab.h>
> >
> > why not just
> >
> > #include <linux/types.h>        /* for gpf_t */
> > #include <linux/gpf.h>          /* for GFP_KERNEL */
> >
> > ?

Sure

> We could certainly switch to that. (Why doesn't gfp.h include types.h?)

Excellent question.

Thanks,

	tglx

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2018-04-25  8:59 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-22 16:23 [patch V3 00/10] rslib: Cleanup and VLA removal Thomas Gleixner
2018-04-22 16:23 ` [patch V3 01/10] rslib: Add GFP aware init function Thomas Gleixner
2018-04-25  1:16   ` Stephen Rothwell
2018-04-25  2:45     ` Kees Cook
2018-04-25  8:59       ` Thomas Gleixner
2018-04-22 16:23 ` [patch V3 02/10] dm/verity_fec: Use GFP aware reed solomon init Thomas Gleixner
2018-04-22 16:23 ` [patch V3 03/10] rslib: Cleanup whitespace damage Thomas Gleixner
2018-04-22 16:23 ` [patch V3 04/10] rslib: Cleanup top level comments Thomas Gleixner
2018-04-22 16:23 ` [patch V3 05/10] rslib: Add SPDX identifiers Thomas Gleixner
2018-04-22 16:23 ` [patch V3 06/10] rslib: Remove GPL boilerplate Thomas Gleixner
2018-04-22 16:23 ` [patch V3 07/10] rslib: Simplify error path Thomas Gleixner
2018-04-22 16:23 ` [patch V3 08/10] rslib: Split rs control struct Thomas Gleixner
2018-04-22 16:23 ` [patch V3 09/10] mtd: rawnand: diskonchip: Allocate rs control per instance Thomas Gleixner
2018-04-22 16:23 ` [patch V3 10/10] rslib: Allocate decoder buffers to avoid VLAs Thomas Gleixner
2018-04-24 16:42 ` [patch V3 00/10] rslib: Cleanup and VLA removal Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).