Skip to contentSkip to conversation

mmc: sdio: fixes some leaks

PATCH Superseded Yang Yingliang 2 participants 5 messages Nov 2, 2022 – Nov 8, 2022 MMC, SD and SDIO

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover mmc: sdio: fixes some leaks

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(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/3 mmc: sdio: fix of node refcount leak in sdio_add_func()

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")
---
 drivers/mmc/core/sdio_bus.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/mmc/core/sdio_bus.c +2 -0
index babf21a0adeb..b9308813a226 100644
@@ -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;
 }
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 1/3] mmc: sdio: fix of node refcount leak in sdio_add_func()

Quoted 17 lines · Yang Yingliang

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")
---
 drivers/mmc/core/sdio_bus.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/mmc/core/sdio_bus.c +0 -0
index babf21a0adeb..b9308813a226 100644

drivers/mmc/core/sdio_bus.c int sdio_add_func(struct sdio_func *func) · line 369

Quoted 5 lines
drivers/mmc/core/sdio_bus.c +2 -0
@@ -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.

drivers/mmc/core/sdio_bus.c int sdio_add_func(struct sdio_func *func) · line 372

Quoted 3 lines
drivers/mmc/core/sdio_bus.c +0 -0
        return ret;
 }

Kind regards
Uffe

#3 lore raw

PATCH v2 2/3 mmc: sdio: fix possible memory leak in sdio_init_func()

In reply to Yang Yingliang

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()")
---
 drivers/mmc/core/sdio.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/mmc/core/sdio.c +2 -5
index f64b9ac76a5c..f314224b362b 100644
@@ -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;
 }
 
Signature · 1 line
2.25.1
#4 lore raw

PATCH v2 3/3 mmc: sdio: fix possible memory leak in mmc_attach_sdio()

In reply to Yang Yingliang

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()")
---
 drivers/mmc/core/sdio_bus.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/mmc/core/sdio_bus.c +4 -5
index b9308813a226..c4d3f721567e 100644
@@ -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);
 }
 
Signature · 1 line
2.25.1