Skip to contentSkip to conversation

mmc: sdio: fixes some leaks

PATCH In review Yang Yingliang 2 participants 8 messages Nov 10, 2022 – Dec 13, 2022 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

v4 cover mmc: sdio: fixes some leaks

This patchset fix a refcount leak and two memory leaks about SDIO function.

v3 -> v4:
Drop patch1, keep calling put_device() to free memory, set 'func->card' to NULL to avoid redundant put.

v2 -> v3:
Change to call of_node_put() in remove() function to fix node refcount leak.

v1 -> v2:
Fix compile error in patch #2.

Yang Yingliang (2):
mmc: sdio: fix of node refcount leak in sdio_add_func()
mmc: sdio: fix possible memory leak in some error path

 drivers/mmc/core/sdio.c     | 1 +
 drivers/mmc/core/sdio_bus.c | 6 +++---
 drivers/mmc/core/sdio_cis.c | 3 ++-
 3 files changed, 6 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v4 1/2 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 moving of_node_put() before present check in remove() function.

Fixes 25185f3f31c9 ("mmc: Add SDIO function devicetree subnode parsing")
---
 drivers/mmc/core/sdio_bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/core/sdio_bus.c +1 -1
index babf21a0adeb..266639504a94 100644
@@ -377,11 +377,11 @@ int sdio_add_func(struct sdio_func *func)
  */
 void sdio_remove_func(struct sdio_func *func)
 {
+	of_node_put(func->dev.of_node);
 	if (!sdio_func_present(func))
 		return;
 
 	device_del(&func->dev);
-	of_node_put(func->dev.of_node);
 	put_device(&func->dev);
 }
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH v4 2/2 mmc: sdio: fix possible memory leak in some error path

In reply to Yang Yingliang

If sdio_add_func() or sdio_init_func() fails, sdio_remove_func() can not free the memory, because the sdio function is not presented in these two cases, it won't call put_device().

To fix these leaks, make sdio_func_present() only control whether device_del() needs to be called or not. Then, put_device() is called to give up the reference which was set in device_initialize(), the memory can be freed in sdio_release_func().

In error case in sdio_init_func(), the reference of 'card->dev' is not get, so set 'func->card' to NULL to avoid redundant put in sdio_free_func_cis().

Fixes 3d10a1ba0d37 ("sdio: fix reference counting in sdio_remove_func()")
---
 drivers/mmc/core/sdio.c     | 1 +
 drivers/mmc/core/sdio_bus.c | 8 ++++----
 drivers/mmc/core/sdio_cis.c | 3 ++-
 3 files changed, 7 insertions(+), 5 deletions(-)
drivers/mmc/core/sdio.c +1 -0
index f64b9ac76a5c..ff95fb6aa741 100644
@@ -137,6 +137,7 @@ static int sdio_init_func(struct mmc_card *card, unsigned int fn)
 	 * It is okay to remove the function here even though we hold
 	 * the host lock as we haven't registered the device yet.
 	 */
+	func->card = NULL;
 	sdio_remove_func(func);
 	return ret;
 }
drivers/mmc/core/sdio_bus.c +4 -4
index 266639504a94..da561658bdae 100644
@@ -377,11 +377,11 @@ int sdio_add_func(struct sdio_func *func)
  */
 void sdio_remove_func(struct sdio_func *func)
 {
-	of_node_put(func->dev.of_node);
-	if (!sdio_func_present(func))
-		return;
+	if (!sdio_func_present(func)) {
+		device_del(&func->dev);
+	}
 
-	device_del(&func->dev);
+	of_node_put(func->dev.of_node);
 	put_device(&func->dev);
 }
 
drivers/mmc/core/sdio_cis.c +2 -1
index a705ba6eff5b..d374460fba9b 100644
@@ -439,6 +439,7 @@ void sdio_free_func_cis(struct sdio_func *func)
 	 * We have now removed the link to the tuples in the
 	 * card structure, so remove the reference.
 	 */
-	put_device(&func->card->dev);
+	if (func->card)
+		put_device(&func->card->dev);
 }
 
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v4 2/2] mmc: sdio: fix possible memory leak in some error path

Sorry, this patch is wrong, please ignore this patchset.

Quoted 66 lines · Yang Yingliang

If sdio_add_func() or sdio_init_func() fails, sdio_remove_func() can not free the memory, because the sdio function is not presented in these two cases, it won't call put_device().

To fix these leaks, make sdio_func_present() only control whether device_del() needs to be called or not. Then, put_device() is called to give up the reference which was set in device_initialize(), the memory can be freed in sdio_release_func().

In error case in sdio_init_func(), the reference of 'card->dev' is not get, so set 'func->card' to NULL to avoid redundant put in sdio_free_func_cis().

Fixes 3d10a1ba0d37 ("sdio: fix reference counting in sdio_remove_func()")
---
  drivers/mmc/core/sdio.c     | 1 +
  drivers/mmc/core/sdio_bus.c | 8 ++++----
  drivers/mmc/core/sdio_cis.c | 3 ++-
  3 files changed, 7 insertions(+), 5 deletions(-)
drivers/mmc/core/sdio.c +1 -0
index f64b9ac76a5c..ff95fb6aa741 100644
@@ -137,6 +137,7 @@ static int sdio_init_func(struct mmc_card *card, unsigned int fn)
  	 * It is okay to remove the function here even though we hold
  	 * the host lock as we haven't registered the device yet.
  	 */
+	func->card = NULL;
  	sdio_remove_func(func);
  	return ret;
  }
drivers/mmc/core/sdio_bus.c +4 -4
index 266639504a94..da561658bdae 100644
@@ -377,11 +377,11 @@ int sdio_add_func(struct sdio_func *func)
   */
  void sdio_remove_func(struct sdio_func *func)
  {
-	of_node_put(func->dev.of_node);
-	if (!sdio_func_present(func))
-		return;
+	if (!sdio_func_present(func)) {
+		device_del(&func->dev);
+	}
 
-	device_del(&func->dev);
+	of_node_put(func->dev.of_node);
  	put_device(&func->dev);
  }
 
drivers/mmc/core/sdio_cis.c +2 -1
index a705ba6eff5b..d374460fba9b 100644
@@ -439,6 +439,7 @@ void sdio_free_func_cis(struct sdio_func *func)
  	 * We have now removed the link to the tuples in the
  	 * card structure, so remove the reference.
  	 */
-	put_device(&func->card->dev);
+	if (func->card)
+		put_device(&func->card->dev);
  }
 
#4 lore raw
In reply to Yang Yingliang

Hi,

Quoted 14 lines · Yang Yingliang

This patchset fix a refcount leak and two memory leaks about SDIO function.

v3 -> v4:
Drop patch1, keep calling put_device() to free memory, set 'func->card' to NULL to avoid redundant put.

v2 -> v3:
Change to call of_node_put() in remove() function to fix node refcount leak.

v1 -> v2:
Fix compile error in patch #2.

Quoted 8 lines

Yang Yingliang (2):
mmc: sdio: fix of node refcount leak in sdio_add_func()
mmc: sdio: fix possible memory leak in some error path

  drivers/mmc/core/sdio.c     | 1 +
  drivers/mmc/core/sdio_bus.c | 6 +++---
  drivers/mmc/core/sdio_cis.c | 3 ++-
  3 files changed, 6 insertions(+), 4 deletions(-)

Is this look good to you, or any suggestions?

Thanks,
Yang

#5 lore raw
Quoted 22 lines · Yang Yingliang

Hi,

Quoted 18 lines · Yang Yingliang

This patchset fix a refcount leak and two memory leaks about SDIO function.

v3 -> v4:
Drop patch1, keep calling put_device() to free memory, set 'func->card' to NULL to avoid redundant put.

v2 -> v3:
Change to call of_node_put() in remove() function to fix node refcount leak.

v1 -> v2:
Fix compile error in patch #2.

Yang Yingliang (2):
mmc: sdio: fix of node refcount leak in sdio_add_func()
mmc: sdio: fix possible memory leak in some error path

Quoted 5 lines
Quoted 4 lines
  drivers/mmc/core/sdio.c     | 1 +
  drivers/mmc/core/sdio_bus.c | 6 +++---
  drivers/mmc/core/sdio_cis.c | 3 ++-
  3 files changed, 6 insertions(+), 4 deletions(-)

Is this look good to you, or any suggestions?

I need some more time to review them, so I will get back to this early next week.

Kind regards
Uffe

#6 lore raw
Quoted 24 lines · Ulf Hansson
Quoted 22 lines · Yang Yingliang

Hi,

Quoted 18 lines · Yang Yingliang

This patchset fix a refcount leak and two memory leaks about SDIO function.

v3 -> v4:
Drop patch1, keep calling put_device() to free memory, set 'func->card' to NULL to avoid redundant put.

v2 -> v3:
Change to call of_node_put() in remove() function to fix node refcount leak.

v1 -> v2:
Fix compile error in patch #2.

Yang Yingliang (2):
mmc: sdio: fix of node refcount leak in sdio_add_func()
mmc: sdio: fix possible memory leak in some error path

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
  drivers/mmc/core/sdio.c     | 1 +
  drivers/mmc/core/sdio_bus.c | 6 +++---
  drivers/mmc/core/sdio_cis.c | 3 ++-
  3 files changed, 6 insertions(+), 4 deletions(-)

Is this look good to you, or any suggestions?

I need some more time to review them, so I will get back to this early next week.

I have re-started to look into these patches, so I will provide you with some comments very soon.

Although, may I ask how you have tested these changes? Or are the changes done from a pure code inspection point of view?

Kind regards
Uffe

#7 lore raw
Quoted 27 lines · Ulf Hansson
Quoted 26 lines · Ulf Hansson
Quoted 25 lines · Yang Yingliang

Hi,

Quoted 22 lines · Yang Yingliang

This patchset fix a refcount leak and two memory leaks about SDIO function.

v3 -> v4:
Drop patch1, keep calling put_device() to free memory, set 'func->card' to NULL to avoid redundant put.

v2 -> v3:
Change to call of_node_put() in remove() function to fix node refcount leak.

v1 -> v2:
Fix compile error in patch #2.

Yang Yingliang (2):
mmc: sdio: fix of node refcount leak in sdio_add_func()
mmc: sdio: fix possible memory leak in some error path

   drivers/mmc/core/sdio.c     | 1 +
   drivers/mmc/core/sdio_bus.c | 6 +++---
   drivers/mmc/core/sdio_cis.c | 3 ++-
   3 files changed, 6 insertions(+), 4 deletions(-)
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Is this look good to you, or any suggestions?

I need some more time to review them, so I will get back to this early next week.

I have re-started to look into these patches, so I will provide you with some comments very soon.

Although, may I ask how you have tested these changes? Or are the changes done from a pure code inspection point of view?

Compiled test only.

Thanks,
Yang

Quoted 4 lines

Kind regards
Uffe
.