lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall
Subsystems (2)
PATCH 1/1 lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall
sg_alloc_table_chained() is called by several drivers, but if it is called before sg_pool_init(), it results in a NULL pointer dereference in sg_pool_alloc().
Since commit 9b1d6c895002 ("lib: scatterlist: move SG pool code from SCSI driver to lib/sg_pool.c"), we rely on module_init(sg_pool_init) is invoked before other module_init calls but this assumption is fragile.
I slightly changed the link order while Kbuild refactoring Kbuild, then uncovered this issue. I should keep the current link order, but depending on a specific call order among module_init is so fragine.
We usually define the init order by specifying *_initcall correctly, or delay the driver probing by returning -EPROBE_DEFER.
Change module_initcall() to subsys_initcall(), and also delete the pointless module_exit() because lib/sg_pool.c is always compiled as built-in. (CONFIG_SG_POOL is bool)
---
I am sending this to DMA subsystem because I did not find a corresponding one for lib/sg_pool.c
lib/sg_pool.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
lib/sg_pool.c +2 -14
@@ -1,5 +1,5 @@// SPDX-License-Identifier: GPL-2.0-only -#include <linux/module.h> +#include <linux/init.h> #include <linux/scatterlist.h> #include <linux/mempool.h> #include <linux/slab.h>
@@ -177,16 +177,4 @@ static __init int sg_pool_init(void)return -ENOMEM ; } -static __exit void sg_pool_exit(void) -{ -int i; - -for (i =0 ; i <SG_MEMPOOL_NR ; i++) { -struct sg_pool *sgp = sg_pools + i; -mempool_destroy (sgp->pool); -kmem_cache_destroy (sgp->slab); - } -} - -module_init (sg_pool_init); -module_exit (sg_pool_exit); +subsys_initcall (sg_pool_init);
Signature · 1 line
2.34.1
Quoted 11 lines · Masahiro Yamada
sg_alloc_table_chained()is called by several drivers, but if it is called beforesg_pool_init(), it results in a NULL pointer dereference insg_pool_alloc().Since commit
9b1d6c895002("lib: scatterlist: move SG pool code from SCSI driver tolib/sg_pool.c"), we rely onmodule_init(sg_pool_init)is invoked before othermodule_initcalls but this assumption is fragile.I slightly changed the link order while
KbuildrefactoringKbuild,
A nit.
while Kbuild refactoring Kbuild -> while refactoring Kbuild
Quoted 2 lines
then uncovered this issue. I should keep the current link order, but depending on a specific call order among
module_initis so fragine.
fragine -> fragile.
Quoted 53 lines
We usually define the init order by specifying
*_initcallcorrectly, or delay the driver probing by returning -EPROBE_DEFER.Change
module_initcall()tosubsys_initcall(), and also delete the pointlessmodule_exit()becauselib/sg_pool.cis always compiled as built-in. (CONFIG_SG_POOLis bool)Fixes 9b1d6c895002 ("lib: scatterlist: move SG pool code from SCSI driver to lib/sg_pool.c")Reported-by Guenter Roeck <[email protected]>Reported-by Marek Szyprowski <[email protected]>Signed-off-by Masahiro Yamada <[email protected]>---
I am sending this to DMA subsystem because I did not find a corresponding one for
lib/sg_pool.clib/sg_pool.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)lib/sg_pool.c +2
-15@@ -1,5 +1,5 @@// SPDX-License-Identifier: GPL-2.0-only -#include <linux/module.h> +#include <linux/init.h> #include <linux/scatterlist.h> #include <linux/mempool.h> #include <linux/slab.h> @@ -177,16 +177,4 @@ static __init int sg_pool_init(void)return -ENOMEM ; } -static __exit void sg_pool_exit(void) -{ -int i; - -for (i =0 ; i <SG_MEMPOOL_NR ; i++) { -struct sg_pool *sgp = sg_pools + i; -mempool_destroy (sgp->pool); -kmem_cache_destroy (sgp->slab); - } -} - -module_init (sg_pool_init); -module_exit (sg_pool_exit); +subsys_initcall (sg_pool_init); --2.34.1
Signature · 2 lines
Best Regards Masahiro Yamada
Quoted 13 lines · Masahiro Yamada
sg_alloc_table_chained()is called by several drivers, but if it is called beforesg_pool_init(), it results in a NULL pointer dereference insg_pool_alloc().Since commit
9b1d6c895002("lib: scatterlist: move SG pool code from SCSI driver tolib/sg_pool.c"), we rely onmodule_init(sg_pool_init)is invoked before othermodule_initcalls but this assumption is fragile.I slightly changed the link order while
KbuildrefactoringKbuild, then uncovered this issue. I should keep the current link order, but depending on a specific call order amongmodule_initis so fragine.
Quoted 6 lines
We usually define the init order by specifying
*_initcallcorrectly, or delay the driver probing by returning -EPROBE_DEFER.Change
module_initcall()tosubsys_initcall(), and also delete the pointlessmodule_exit()becauselib/sg_pool.cis always compiled as built-in. (CONFIG_SG_POOLis bool)
Makes sense to me. Short of having some cool-but-impractically-complex system to derive a dependency graph from the config and compute an initcall order from that, initialising helper library code at an earlier step than drivers certainly seems like the next-best option, and subsys doesn't seem inappropriate for the nature of this code. FWIW,
Quoted 43 lines
Fixes 9b1d6c895002 ("lib: scatterlist: move SG pool code from SCSI driver to lib/sg_pool.c")Reported-by Guenter Roeck <[email protected]>Reported-by Marek Szyprowski <[email protected]>Signed-off-by Masahiro Yamada <[email protected]>---
I am sending this to DMA subsystem because I did not find a corresponding one for
lib/sg_pool.clib/sg_pool.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)lib/sg_pool.c +2
-14@@ -1,5 +1,5 @@// SPDX-License-Identifier: GPL-2.0-only -#include <linux/module.h> +#include <linux/init.h> #include <linux/scatterlist.h> #include <linux/mempool.h> #include <linux/slab.h> @@ -177,16 +177,4 @@ static __init int sg_pool_init(void)return -ENOMEM ; } -static __exit void sg_pool_exit(void) -{ -int i; - -for (i =0 ; i <SG_MEMPOOL_NR ; i++) { -struct sg_pool *sgp = sg_pools + i; -mempool_destroy (sgp->pool); -kmem_cache_destroy (sgp->slab); - } -} - -module_init (sg_pool_init); -module_exit (sg_pool_exit); +subsys_initcall (sg_pool_init);
Quoted 26 lines · Masahiro Yamada
sg_alloc_table_chained()is called by several drivers, but if it is called beforesg_pool_init(), it results in a NULL pointer dereference insg_pool_alloc().Since commit
9b1d6c895002("lib: scatterlist: move SG pool code from SCSI driver tolib/sg_pool.c"), we rely onmodule_init(sg_pool_init)is invoked before othermodule_initcalls but this assumption is fragile.I slightly changed the link order while
KbuildrefactoringKbuild, then uncovered this issue. I should keep the current link order, but depending on a specific call order amongmodule_initis so fragine.We usually define the init order by specifying
*_initcallcorrectly, or delay the driver probing by returning -EPROBE_DEFER.Change
module_initcall()tosubsys_initcall(), and also delete the pointlessmodule_exit()becauselib/sg_pool.cis always compiled as built-in. (CONFIG_SG_POOLis bool)Fixes 9b1d6c895002 ("lib: scatterlist: move SG pool code from SCSI driver to lib/sg_pool.c")Reported-by Guenter Roeck <[email protected]>Reported-by Marek Szyprowski <[email protected]>Signed-off-by Masahiro Yamada <[email protected]>
Quoted 4 lines
---
I am sending this to DMA subsystem because I did not find a corresponding one for
lib/sg_pool.c
Feel free to add:
Quoted 22 lines
lib/sg_pool.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)lib/sg_pool.c +1
-5@@ -1,5 +1,5 @@// SPDX-License-Identifier: GPL-2.0-only -#include <linux/module.h> +#include <linux/init.h> #include <linux/scatterlist.h> #include <linux/mempool.h> #include <linux/slab.h> @@ -177,16 +177,4 @@ static __init int sg_pool_init(void)return -ENOMEM ; } -static __exit void sg_pool_exit(void) -{ -int i; -
lib/sg_pool.c static __init int sg_pool_init(void) · line 180
Quoted 10 lines
lib/sg_pool.c +1
-9-for (i =0 ; i <SG_MEMPOOL_NR ; i++) { -struct sg_pool *sgp = sg_pools + i; -mempool_destroy (sgp->pool); -kmem_cache_destroy (sgp->slab); - } -} - -module_init (sg_pool_init); -module_exit (sg_pool_exit); +subsys_initcall (sg_pool_init);
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Christoph Hellwig
AppliedThanks, applied with the typo fixes included.