From 83a530e1610ab996e59c0941db6cc72f763dddbd Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Sun, 22 Apr 2018 18:23:46 +0200 Subject: 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 Cc: Mike Snitzer Cc: Alasdair Kergon Cc: Neil Brown Signed-off-by: Kees Cook --- include/linux/rslib.h | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/linux/rslib.h b/include/linux/rslib.h index 746580c1939c..2aae7ef987eb 100644 --- a/include/linux/rslib.h +++ b/include/linux/rslib.h @@ -20,6 +20,8 @@ #define _RSLIB_H_ #include +#include /* for gfp_t */ +#include /* for GFP_KERNEL */ /** * 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 /* 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); -- cgit From cc4b86e496beb6f3f499e1be260f51b4026aba84 Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Sun, 22 Apr 2018 18:23:48 +0200 Subject: rslib: Cleanup whitespace damage Instead of mixing the whitespace cleanup into functional changes, mop it up first. Signed-off-by: Thomas Gleixner Cc: Boris Brezillon Cc: Tony Luck Cc: Kees Cook Cc: Segher Boessenkool Cc: Kernel Hardening Cc: Richard Weinberger Cc: Mike Snitzer Cc: Anton Vorontsov Cc: Colin Cross Cc: Andrew Morton Cc: David Woodhouse Cc: Alasdair Kergon Signed-off-by: Kees Cook --- include/linux/rslib.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/include/linux/rslib.h b/include/linux/rslib.h index 2aae7ef987eb..d7bc66931032 100644 --- a/include/linux/rslib.h +++ b/include/linux/rslib.h @@ -41,15 +41,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; -- cgit From 3413e1891d443b9e936b4b6fb0ba4cdc38ff71b6 Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Sun, 22 Apr 2018 18:23:49 +0200 Subject: rslib: Cleanup top level comments File references and stale CVS ids are really not useful. Signed-off-by: Thomas Gleixner Cc: Boris Brezillon Cc: Tony Luck Cc: Kees Cook Cc: Segher Boessenkool Cc: Kernel Hardening Cc: Richard Weinberger Cc: Mike Snitzer Cc: Anton Vorontsov Cc: Colin Cross Cc: Andrew Morton Cc: David Woodhouse Cc: Alasdair Kergon Signed-off-by: Kees Cook --- include/linux/rslib.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'include') diff --git a/include/linux/rslib.h b/include/linux/rslib.h index d7bc66931032..a596abaf2e56 100644 --- 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. -- cgit From dc8f923eaee24061c557cb3221abeea3b95072c4 Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Sun, 22 Apr 2018 18:23:50 +0200 Subject: rslib: Add SPDX identifiers 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 Signed-Off-By: David Woodhouse "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 Reviewed-by: Kate Stewart Reviewed-by: Greg Kroah-Hartman Cc: Boris Brezillon Cc: Tony Luck Cc: Kees Cook Cc: Segher Boessenkool Cc: Kernel Hardening Cc: Richard Weinberger Cc: Mike Snitzer Cc: Anton Vorontsov Cc: Colin Cross Cc: Andrew Morton Cc: David Woodhouse Cc: Alasdair Kergon Signed-off-by: Kees Cook --- include/linux/rslib.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/linux/rslib.h b/include/linux/rslib.h index a596abaf2e56..7ace7eec3028 100644 --- 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 * -- cgit From 689c6efdfb58d7b21c375f515349e4091e08100b Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Sun, 22 Apr 2018 18:23:51 +0200 Subject: rslib: Remove GPL boilerplate 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 Reviewed-by: Kate Stewart Reviewed-by: Greg Kroah-Hartman Cc: Boris Brezillon Cc: Tony Luck Cc: Kees Cook Cc: Segher Boessenkool Cc: Kernel Hardening Cc: Richard Weinberger Cc: Mike Snitzer Cc: Anton Vorontsov Cc: Colin Cross Cc: Andrew Morton Cc: David Woodhouse Cc: Alasdair Kergon Signed-off-by: Kees Cook --- include/linux/rslib.h | 5 ----- 1 file changed, 5 deletions(-) (limited to 'include') diff --git a/include/linux/rslib.h b/include/linux/rslib.h index 7ace7eec3028..27652c18bf8c 100644 --- 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_ -- cgit From 2163398192f6a53e84765cfe3e5a2088437c3d2d Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Sun, 22 Apr 2018 18:23:53 +0200 Subject: rslib: Split rs control struct 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 Acked-by: Boris Brezillon Cc: Tony Luck Cc: Kees Cook Cc: Segher Boessenkool Cc: Kernel Hardening Cc: Richard Weinberger Cc: Mike Snitzer Cc: Anton Vorontsov Cc: Colin Cross Cc: Andrew Morton Cc: David Woodhouse Cc: Alasdair Kergon Signed-off-by: Kees Cook --- include/linux/rslib.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/include/linux/rslib.h b/include/linux/rslib.h index 27652c18bf8c..6703311beea3 100644 --- a/include/linux/rslib.h +++ b/include/linux/rslib.h @@ -15,7 +15,7 @@ #include /* for GFP_KERNEL */ /** - * struct rs_control - rs control structure + * struct rs_codec - rs codec data * * @mm: Bits per symbol * @nn: Symbols per block (= (1<= 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; -- cgit From 45888b40d2a6221d46bb69959e2600ddba71cc1f Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Sun, 22 Apr 2018 18:23:55 +0200 Subject: rslib: Allocate decoder buffers to avoid VLAs 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 Acked-by: Kees Cook Cc: Boris Brezillon Cc: Tony Luck Cc: Segher Boessenkool Cc: Kernel Hardening Cc: Richard Weinberger Cc: Mike Snitzer Cc: Anton Vorontsov Cc: Colin Cross Cc: Andrew Morton Cc: David Woodhouse Cc: Alasdair Kergon Signed-off-by: Kees Cook --- include/linux/rslib.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include') diff --git a/include/linux/rslib.h b/include/linux/rslib.h index 6703311beea3..5974cedd008c 100644 --- a/include/linux/rslib.h +++ b/include/linux/rslib.h @@ -50,9 +50,11 @@ struct rs_codec { /** * struct rs_control - rs control structure per instance * @codec: The codec used for this instance + * @buffers: Internal scratch buffers used in calls to decode_rs() */ struct rs_control { struct rs_codec *codec; + uint16_t buffers[0]; }; /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */ -- cgit