Skip to contentSkip to conversation

mmc: core: Fix OF node reference leak on card add failure

PATCH In review Zhu Ling 3 participants 3 messages Sep 4 – Sep 10

Subsystems (2)
lore
Share & tools
lore

Zhu Ling

author
#0 lore raw

PATCH 1/1 mmc: core: Fix OF node reference leak on card add failure

mmc_of_find_child_device() returns a device node with its reference count incremented. mmc_add_card() stores the reference before calling device_add(), while the card is marked present only after device_add() succeeds.

If device_add() fails, the callers release the card through mmc_remove_card(). However, mmc_remove_card() only drops the OF node reference for a present card, leaking the reference on this error path.

Move of_node_put() outside the present-card conditional so the reference is released for both registered cards and card-add failures.

Fixes 25185f3f31c9 ("mmc: Add SDIO function devicetree subnode parsing")
---
 drivers/mmc/core/bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/core/bus.c +1 -1
index be5cf338bdeb..92ad39c0cabc 100644
@@ -417,8 +417,8 @@ void mmc_remove_card(struct mmc_card *card)
 				mmc_hostname(card->host), card->rca);
 		}
 		device_del(&card->dev);
-		of_node_put(card->dev.of_node);
 	}
+	of_node_put(card->dev.of_node);
 
 	if (host->cqe_enabled) {
 		host->cqe_ops->cqe_disable(host);
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Zhu Ling

mmc_of_find_child_device() returns a device node with its reference count incremented. mmc_add_card() stores the reference before calling device_add(), while the card is marked present only after device_add() succeeds.

If device_add() fails, the callers release the card through mmc_remove_card(). However, mmc_remove_card() only drops the OF node reference for a present card, leaking the reference on this error path.

Quoted 7 lines

Move of_node_put() outside the present-card conditional so the reference is released for both registered cards and card-add failures.

Fixes 25185f3f31c9 ("mmc: Add SDIO function devicetree subnode parsing")

---

Quoted 19 lines
  drivers/mmc/core/bus.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/core/bus.c +1 -1
index be5cf338bdeb..92ad39c0cabc 100644
@@ -417,8 +417,8 @@ void mmc_remove_card(struct mmc_card *card)
  				mmc_hostname(card->host), card->rca);
  		}
  		device_del(&card->dev);
-		of_node_put(card->dev.of_node);
  	}
+	of_node_put(card->dev.of_node);
 
  	if (host->cqe_enabled) {
  		host->cqe_ops->cqe_disable(host);
#2 lore raw
In reply to Zhu Ling
Quoted 10 lines · Zhu Ling

mmc_of_find_child_device() returns a device node with its reference count incremented. mmc_add_card() stores the reference before calling device_add(), while the card is marked present only after device_add() succeeds.

If device_add() fails, the callers release the card through mmc_remove_card(). However, mmc_remove_card() only drops the OF node reference for a present card, leaking the reference on this error path.

Quoted 6 lines

Move of_node_put() outside the present-card conditional so the reference is released for both registered cards and card-add failures.

Fixes 25185f3f31c9 ("mmc: Add SDIO function devicetree subnode parsing")

Applied for fixes, thanks!

Kind regards
Uffe

Quoted 19 lines
---
 drivers/mmc/core/bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/core/bus.c +1 -1
index be5cf338bdeb..92ad39c0cabc 100644
@@ -417,8 +417,8 @@ void mmc_remove_card(struct mmc_card *card)
                                mmc_hostname(card->host), card->rca);
                }
                device_del(&card->dev);
-               of_node_put(card->dev.of_node);
        }
+       of_node_put(card->dev.of_node);
 
        if (host->cqe_enabled) {
                host->cqe_ops->cqe_disable(host);