All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall
@ 2022-09-23 11:38 ` Masahiro Yamada
  2022-09-23 11:43   ` Masahiro Yamada
                     ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Masahiro Yamada @ 2022-09-23 11:38 UTC (permalink / raw)
  To: linux-kernel, Christoph Hellwig, Marek Szyprowski, Robin Murphy, iommu
  Cc: Guenter Roeck, linux-kbuild, Masahiro Yamada, Martin K. Petersen,
	Ming Lin, Sagi Grimberg

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)

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: 9b1d6c895002 ("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>
---

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(-)

diff --git a/lib/sg_pool.c b/lib/sg_pool.c
index a0b1a52cd6f7..9bfe60ca3f37 100644
--- a/lib/sg_pool.c
+++ b/lib/sg_pool.c
@@ -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


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

* Re: [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall
  2022-09-23 11:38 ` [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall Masahiro Yamada
@ 2022-09-23 11:43   ` Masahiro Yamada
  2022-09-23 12:09   ` Robin Murphy
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Masahiro Yamada @ 2022-09-23 11:43 UTC (permalink / raw)
  To: Linux Kernel Mailing List, Christoph Hellwig, Marek Szyprowski,
	Robin Murphy, iommu
  Cc: Guenter Roeck, Linux Kbuild mailing list, Martin K. Petersen,
	Ming Lin, Sagi Grimberg

On Fri, Sep 23, 2022 at 8:40 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> 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,


A nit.

while Kbuild refactoring Kbuild -> 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 fragine.


fragine -> 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: 9b1d6c895002 ("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>
> ---
>
> 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(-)
>
> diff --git a/lib/sg_pool.c b/lib/sg_pool.c
> index a0b1a52cd6f7..9bfe60ca3f37 100644
> --- a/lib/sg_pool.c
> +++ b/lib/sg_pool.c
> @@ -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
>


-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall
  2022-09-23 11:38 ` [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall Masahiro Yamada
  2022-09-23 11:43   ` Masahiro Yamada
@ 2022-09-23 12:09   ` Robin Murphy
  2022-09-23 12:20   ` Marek Szyprowski
  2022-09-23 14:49   ` Christoph Hellwig
  3 siblings, 0 replies; 6+ messages in thread
From: Robin Murphy @ 2022-09-23 12:09 UTC (permalink / raw)
  To: Masahiro Yamada, linux-kernel, Christoph Hellwig,
	Marek Szyprowski, iommu
  Cc: Guenter Roeck, linux-kbuild, Martin K. Petersen, Ming Lin, Sagi Grimberg

On 2022-09-23 12:38, Masahiro Yamada wrote:
> 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)

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,

Reviewed-by: Robin Murphy <robin.murphy@arm.com>

> 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: 9b1d6c895002 ("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>
> ---
> 
> 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(-)
> 
> diff --git a/lib/sg_pool.c b/lib/sg_pool.c
> index a0b1a52cd6f7..9bfe60ca3f37 100644
> --- a/lib/sg_pool.c
> +++ b/lib/sg_pool.c
> @@ -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);

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

* Re: [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall
  2022-09-23 11:38 ` [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall Masahiro Yamada
  2022-09-23 11:43   ` Masahiro Yamada
  2022-09-23 12:09   ` Robin Murphy
@ 2022-09-23 12:20   ` Marek Szyprowski
  2022-09-23 14:49   ` Christoph Hellwig
  3 siblings, 0 replies; 6+ messages in thread
From: Marek Szyprowski @ 2022-09-23 12:20 UTC (permalink / raw)
  To: Masahiro Yamada, linux-kernel, Christoph Hellwig, Robin Murphy, iommu
  Cc: Guenter Roeck, linux-kbuild, Martin K. Petersen, Ming Lin, Sagi Grimberg

On 23.09.2022 13:38, Masahiro Yamada wrote:
> 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)
>
> 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: 9b1d6c895002 ("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>
> ---
>
> I am sending this to DMA subsystem because I did not find
> a corresponding one for lib/sg_pool.c

Feel free to add:

Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>

>   lib/sg_pool.c | 16 ++--------------
>   1 file changed, 2 insertions(+), 14 deletions(-)
>
> diff --git a/lib/sg_pool.c b/lib/sg_pool.c
> index a0b1a52cd6f7..9bfe60ca3f37 100644
> --- a/lib/sg_pool.c
> +++ b/lib/sg_pool.c
> @@ -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);

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland


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

* Re: [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall
  2022-09-23 11:38 ` [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall Masahiro Yamada
                     ` (2 preceding siblings ...)
  2022-09-23 12:20   ` Marek Szyprowski
@ 2022-09-23 14:49   ` Christoph Hellwig
  3 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2022-09-23 14:49 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kernel, Christoph Hellwig, Marek Szyprowski, Robin Murphy,
	iommu, Guenter Roeck, linux-kbuild, Martin K. Petersen, Ming Lin,
	Sagi Grimberg

Thanks, applied with the typo fixes included.

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

* Re: [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall
@ 2022-09-23 15:28 Guenter Roeck
  0 siblings, 0 replies; 6+ messages in thread
From: Guenter Roeck @ 2022-09-23 15:28 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kernel, Christoph Hellwig, Marek Szyprowski, Robin Murphy,
	iommu, linux-kbuild, Martin K. Petersen, Ming Lin, Sagi Grimberg

On Fri, Sep 23, 2022 at 08:38:35PM +0900, Masahiro Yamada wrote:
> 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)
> 
> 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: 9b1d6c895002 ("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>

Tested-by: Guenter Roeck <linux@roeck-us.net>

Guenter

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

end of thread, other threads:[~2022-09-23 15:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20220923113941eucas1p2e5f7b9bc7316114e69ab6348d40d3b6d@eucas1p2.samsung.com>
2022-09-23 11:38 ` [PATCH] lib/sg_pool: change module_init(sg_pool_init) to subsys_initcall Masahiro Yamada
2022-09-23 11:43   ` Masahiro Yamada
2022-09-23 12:09   ` Robin Murphy
2022-09-23 12:20   ` Marek Szyprowski
2022-09-23 14:49   ` Christoph Hellwig
2022-09-23 15:28 Guenter Roeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.