mmc: sdio: fixes some leaks
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
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.
--- drivers/mmc/core/sdio_bus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/core/sdio_bus.c +1 -1
@@ -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
PATCH v4 2/2 mmc: sdio: fix possible memory leak in some error path
In reply to Yang YingliangIf 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().
--- 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
@@ -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
@@ -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
@@ -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
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()orsdio_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 callput_device().To fix these leaks, make
sdio_func_present()only control whetherdevice_del()needs to be called or not. Then,put_device()is called to give up the reference which was set indevice_initialize(), the memory can be freed insdio_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 insdio_free_func_cis().Fixes 3d10a1ba0d37 ("sdio: fix reference counting in sdio_remove_func()")Signed-off-by Yang Yingliang <[email protected]>--- 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@@ -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@@ -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@@ -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); }
Hi,
Quoted 14 lines · Yang Yingliang
This patchset fix a refcount leak and two memory leaks about SDIO function.
v3 -> v4:
Drop patch1, keep callingput_device()to free memory, set 'func->card' to NULL to avoid redundant put.v2 -> v3:
Change to callof_node_put()inremove()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 insdio_add_func()
mmc: sdio: fix possible memory leak in some error pathdrivers/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
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 callingput_device()to free memory, set 'func->card' to NULL to avoid redundant put.v2 -> v3:
Change to callof_node_put()inremove()function to fix node refcount leak.v1 -> v2:
Fix compile error in patch #2.Yang Yingliang (2):
mmc: sdio: fix of node refcount leak insdio_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
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 callingput_device()to free memory, set 'func->card' to NULL to avoid redundant put.v2 -> v3:
Change to callof_node_put()inremove()function to fix node refcount leak.v1 -> v2:
Fix compile error in patch #2.Yang Yingliang (2):
mmc: sdio: fix of node refcount leak insdio_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
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 callingput_device()to free memory, set 'func->card' to NULL to avoid redundant put.v2 -> v3:
Change to callof_node_put()inremove()function to fix node refcount leak.v1 -> v2:
Fix compile error in patch #2.Yang Yingliang (2):
mmc: sdio: fix of node refcount leak insdio_add_func()
mmc: sdio: fix possible memory leak in some error pathdrivers/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
.