* [PATCH v2 0/3] mmc: sdio: fixes some leaks
@ 2022-11-02 1:25 Yang Yingliang
2022-11-02 1:25 ` [PATCH v2 1/3] mmc: sdio: fix of node refcount leak in sdio_add_func() Yang Yingliang
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Yang Yingliang @ 2022-11-02 1:25 UTC (permalink / raw)
To: linux-mmc; +Cc: ulf.hansson, yangyingliang
This patch a refcount leak and two memory leaks about
SDIO function.
v1 -> v2:
Fix compile error in patch #2.
Yang Yingliang (3):
mmc: sdio: fix of node refcount leak in sdio_add_func()
mmc: sdio: fix possible memory leak in sdio_init_func()
mmc: sdio: fix possible memory leak in mmc_attach_sdio()
drivers/mmc/core/sdio.c | 7 ++-----
drivers/mmc/core/sdio_bus.c | 11 ++++++-----
2 files changed, 8 insertions(+), 10 deletions(-)
--
2.25.1
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v2 1/3] mmc: sdio: fix of node refcount leak in sdio_add_func()
2022-11-02 1:25 [PATCH v2 0/3] mmc: sdio: fixes some leaks Yang Yingliang
@ 2022-11-02 1:25 ` Yang Yingliang
2022-11-08 16:04 ` Ulf Hansson
2022-11-02 1:25 ` [PATCH v2 2/3] mmc: sdio: fix possible memory leak in sdio_init_func() Yang Yingliang
2022-11-02 1:25 ` [PATCH v2 3/3] mmc: sdio: fix possible memory leak in mmc_attach_sdio() Yang Yingliang
2 siblings, 1 reply; 5+ messages in thread
From: Yang Yingliang @ 2022-11-02 1:25 UTC (permalink / raw)
To: linux-mmc; +Cc: ulf.hansson, yangyingliang
If device_add() returns error in sdio_add_func(), sdio function is not
presented, so the node refcount that hold in sdio_set_of_node() can not
be put in sdio_remove_func() which is called from error path. Fix this
by calling of_node_put() if device_add() fails.
Fixes: 25185f3f31c9 ("mmc: Add SDIO function devicetree subnode parsing")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
drivers/mmc/core/sdio_bus.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/mmc/core/sdio_bus.c b/drivers/mmc/core/sdio_bus.c
index babf21a0adeb..b9308813a226 100644
--- a/drivers/mmc/core/sdio_bus.c
+++ b/drivers/mmc/core/sdio_bus.c
@@ -365,6 +365,8 @@ int sdio_add_func(struct sdio_func *func)
ret = device_add(&func->dev);
if (ret == 0)
sdio_func_set_present(func);
+ else
+ of_node_put(func->dev.of_node);
return ret;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v2 1/3] mmc: sdio: fix of node refcount leak in sdio_add_func()
2022-11-02 1:25 ` [PATCH v2 1/3] mmc: sdio: fix of node refcount leak in sdio_add_func() Yang Yingliang
@ 2022-11-08 16:04 ` Ulf Hansson
0 siblings, 0 replies; 5+ messages in thread
From: Ulf Hansson @ 2022-11-08 16:04 UTC (permalink / raw)
To: Yang Yingliang; +Cc: linux-mmc
On Wed, 2 Nov 2022 at 02:27, Yang Yingliang <yangyingliang@huawei.com> wrote:
>
> If device_add() returns error in sdio_add_func(), sdio function is not
> presented, so the node refcount that hold in sdio_set_of_node() can not
> be put in sdio_remove_func() which is called from error path. Fix this
> by calling of_node_put() if device_add() fails.
>
> Fixes: 25185f3f31c9 ("mmc: Add SDIO function devicetree subnode parsing")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> ---
> drivers/mmc/core/sdio_bus.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/mmc/core/sdio_bus.c b/drivers/mmc/core/sdio_bus.c
> index babf21a0adeb..b9308813a226 100644
> --- a/drivers/mmc/core/sdio_bus.c
> +++ b/drivers/mmc/core/sdio_bus.c
> @@ -365,6 +365,8 @@ int sdio_add_func(struct sdio_func *func)
> ret = device_add(&func->dev);
> if (ret == 0)
> sdio_func_set_present(func);
> + else
> + of_node_put(func->dev.of_node);
I think it would be better to change the sdio_remove_func() to manage
this. In principle sdio_func_present() should not make
sdio_remove_func() to return immediately, but rather just control
whether device_del() needs to be called or not.
>
> return ret;
> }
Kind regards
Uffe
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/3] mmc: sdio: fix possible memory leak in sdio_init_func()
2022-11-02 1:25 [PATCH v2 0/3] mmc: sdio: fixes some leaks Yang Yingliang
2022-11-02 1:25 ` [PATCH v2 1/3] mmc: sdio: fix of node refcount leak in sdio_add_func() Yang Yingliang
@ 2022-11-02 1:25 ` Yang Yingliang
2022-11-02 1:25 ` [PATCH v2 3/3] mmc: sdio: fix possible memory leak in mmc_attach_sdio() Yang Yingliang
2 siblings, 0 replies; 5+ messages in thread
From: Yang Yingliang @ 2022-11-02 1:25 UTC (permalink / raw)
To: linux-mmc; +Cc: ulf.hansson, yangyingliang
If it fails in sdio_init_func(), sdio_remove_func() can not
free the memory that allocated in sdio_alloc_func(), because
sdio_add_func() is not called yet, the sdio function is not
presented and sdio_remove_func() will return directly.
In this error path, we can not call put_device() to free the
memory in sdio_release_func(), because sdio_read_func_cis()
may fail, then sdio_free_func_cis() is called in release()
funtion which could cause put the reference that has not
been got.
So fix these leaks with calling kfree() instead of sdio_remove_func()
in error path.
Fixes: 3d10a1ba0d37 ("sdio: fix reference counting in sdio_remove_func()")
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
drivers/mmc/core/sdio.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/drivers/mmc/core/sdio.c b/drivers/mmc/core/sdio.c
index f64b9ac76a5c..f314224b362b 100644
--- a/drivers/mmc/core/sdio.c
+++ b/drivers/mmc/core/sdio.c
@@ -133,11 +133,8 @@ static int sdio_init_func(struct mmc_card *card, unsigned int fn)
return 0;
fail:
- /*
- * It is okay to remove the function here even though we hold
- * the host lock as we haven't registered the device yet.
- */
- sdio_remove_func(func);
+ kfree(func->tmpbuf);
+ kfree(func);
return ret;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 3/3] mmc: sdio: fix possible memory leak in mmc_attach_sdio()
2022-11-02 1:25 [PATCH v2 0/3] mmc: sdio: fixes some leaks Yang Yingliang
2022-11-02 1:25 ` [PATCH v2 1/3] mmc: sdio: fix of node refcount leak in sdio_add_func() Yang Yingliang
2022-11-02 1:25 ` [PATCH v2 2/3] mmc: sdio: fix possible memory leak in sdio_init_func() Yang Yingliang
@ 2022-11-02 1:25 ` Yang Yingliang
2 siblings, 0 replies; 5+ messages in thread
From: Yang Yingliang @ 2022-11-02 1:25 UTC (permalink / raw)
To: linux-mmc; +Cc: ulf.hansson, yangyingliang
If sdio_add_func() returns error in mmc_attach_sdio(),
sdio_remove_func() can not free the memory that allocated
in sdio_init_func(), because the sdio function is not
presented and sdio_remove_func() will return directly.
To fix these leaks, we can call put_device() to give up
the reference which was set in device_initialize(), then
the memory can be freed in sdio_release_func().
Fixes: 3d10a1ba0d37 ("sdio: fix reference counting in sdio_remove_func()")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
drivers/mmc/core/sdio_bus.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/drivers/mmc/core/sdio_bus.c b/drivers/mmc/core/sdio_bus.c
index b9308813a226..c4d3f721567e 100644
--- a/drivers/mmc/core/sdio_bus.c
+++ b/drivers/mmc/core/sdio_bus.c
@@ -379,11 +379,10 @@ int sdio_add_func(struct sdio_func *func)
*/
void sdio_remove_func(struct sdio_func *func)
{
- if (!sdio_func_present(func))
- return;
-
- device_del(&func->dev);
- of_node_put(func->dev.of_node);
+ if (sdio_func_present(func)) {
+ device_del(&func->dev);
+ of_node_put(func->dev.of_node);
+ }
put_device(&func->dev);
}
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-11-08 16:05 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-02 1:25 [PATCH v2 0/3] mmc: sdio: fixes some leaks Yang Yingliang
2022-11-02 1:25 ` [PATCH v2 1/3] mmc: sdio: fix of node refcount leak in sdio_add_func() Yang Yingliang
2022-11-08 16:04 ` Ulf Hansson
2022-11-02 1:25 ` [PATCH v2 2/3] mmc: sdio: fix possible memory leak in sdio_init_func() Yang Yingliang
2022-11-02 1:25 ` [PATCH v2 3/3] mmc: sdio: fix possible memory leak in mmc_attach_sdio() Yang Yingliang
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).