Skip to content

Commit f7f04d1

Browse files
masahir0yChristoph Hellwig
authored andcommitted
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 9b1d6c8 ("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 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 fragile. 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) Link: https://lore.kernel.org/all/20220921043946.GA1355561@roeck-us.net/ Link: https://lore.kernel.org/all/8e70837d-d859-dfb2-bf7f-83f8b31467bc@samsung.com/ Fixes: 9b1d6c8 ("lib: scatterlist: move SG pool code from SCSI driver to lib/sg_pool.c") Reported-by: Guenter Roeck <linux@roeck-us.net> Reported-by: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org> Reviewed-by: Robin Murphy <robin.murphy@arm.com> Tested-by: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent 91fd38e commit f7f04d1

File tree

1 file changed

+2
-14
lines changed

1 file changed

+2
-14
lines changed

lib/sg_pool.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: GPL-2.0-only
2-
#include <linux/module.h>
2+
#include <linux/init.h>
33
#include <linux/scatterlist.h>
44
#include <linux/mempool.h>
55
#include <linux/slab.h>
@@ -177,16 +177,4 @@ static __init int sg_pool_init(void)
177177
return -ENOMEM;
178178
}
179179

180-
static __exit void sg_pool_exit(void)
181-
{
182-
int i;
183-
184-
for (i = 0; i < SG_MEMPOOL_NR; i++) {
185-
struct sg_pool *sgp = sg_pools + i;
186-
mempool_destroy(sgp->pool);
187-
kmem_cache_destroy(sgp->slab);
188-
}
189-
}
190-
191-
module_init(sg_pool_init);
192-
module_exit(sg_pool_exit);
180+
subsys_initcall(sg_pool_init);

0 commit comments

Comments
 (0)