Skip to contentSkip to conversation

memory: emif: fix device tree node reference leak

PATCH In review Weigang He 2 participants 2 messages Jan 15 – Jan 17

Subsystems (2)
lore
Share & tools
lore

Weigang He

author
#0 lore raw

PATCH 1/1 memory: emif: fix device tree node reference leak

The EMIF driver acquires a device tree node reference via of_parse_phandle() but never releases it with of_node_put(), causing a reference count leak.

This affects:
1. Error paths in of_get_memory_device_details() - if allocation or validation fails after of_parse_phandle() succeeds, the reference is leaked
2. Normal driver removal - emif_remove() never releases the reference stored in emif->np_ddr

Fix by adding an err_put_node label to properly release the node reference on error paths, and adding of_node_put() in emif_remove() to release the reference during normal driver unload.

Fixes e6b42eb6a66c ("memory: emif: add device tree support to emif driver")
---
 drivers/memory/emif.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/memory/emif.c +5 -2
index 2fadad0666b1f..e4f7547efbff4 100644
@@ -922,7 +922,7 @@ static struct emif_data *of_get_memory_device_details(
 	if (!emif || !pd || !dev_info) {
 		dev_err(dev, "%s: Out of memory!!\n",
 			__func__);
-		goto error;
+		goto err_put_node;
 	}
 
 	emif->plat_data		= pd;
@@ -946,7 +946,7 @@ static struct emif_data *of_get_memory_device_details(
 			pd->device_info->io_width, pd->phy_type, pd->ip_rev,
 			emif->dev)) {
 		dev_err(dev, "%s: invalid device data!!\n", __func__);
-		goto error;
+		goto err_put_node;
 	}
 	/*
 	 * For EMIF instances other than EMIF1 see if the devices connected
@@ -970,6 +970,8 @@ static struct emif_data *of_get_memory_device_details(
 	emif->plat_data->min_tck = of_get_min_tck(np_ddr, emif->dev);
 	goto out;
 
+err_put_node:
+	of_node_put(np_ddr);
 error:
 	return NULL;
 out:
@@ -1139,6 +1141,7 @@ static void emif_remove(struct platform_device *pdev)
 {
 	struct emif_data *emif = platform_get_drvdata(pdev);
 
+	of_node_put(emif->np_ddr);
 	emif_debugfs_exit(emif);
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 11 lines · Weigang He

The EMIF driver acquires a device tree node reference via of_parse_phandle() but never releases it with of_node_put(), causing a reference count leak.

This affects:
1. Error paths in of_get_memory_device_details() - if allocation or validation fails after of_parse_phandle() succeeds, the reference is leaked
2. Normal driver removal - emif_remove() never releases the reference stored in emif->np_ddr

Quoted 5 lines

Fix by adding an err_put_node label to properly release the node reference on error paths, and adding of_node_put() in emif_remove() to release the reference during normal driver unload.

Fixes e6b42eb6a66c ("memory: emif: add device tree support to emif driver")

Missing Cc stable.

Quoted 28 lines
---
 drivers/memory/emif.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/memory/emif.c +2 -2
index 2fadad0666b1f..e4f7547efbff4 100644
@@ -922,7 +922,7 @@ static struct emif_data *of_get_memory_device_details(
 	if (!emif || !pd || !dev_info) {
 		dev_err(dev, "%s: Out of memory!!\n",
 			__func__);
-		goto error;
+		goto err_put_node;
 	}
 
 	emif->plat_data		= pd;
@@ -946,7 +946,7 @@ static struct emif_data *of_get_memory_device_details(
 			pd->device_info->io_width, pd->phy_type, pd->ip_rev,
 			emif->dev)) {
 		dev_err(dev, "%s: invalid device data!!\n", __func__);
-		goto error;
+		goto err_put_node;
 	}
 	/*
 	 * For EMIF instances other than EMIF1 see if the devices connected

drivers/memory/emif.c static struct emif_data *of_get_memory_device_details( · line 975

Quoted 6 lines
drivers/memory/emif.c +2 -0
@@ -970,6 +970,8 @@ static struct emif_data *of_get_memory_device_details(
 	emif->plat_data->min_tck = of_get_min_tck(np_ddr, emif->dev);
 	goto out;
 
+err_put_node:
+	of_node_put(np_ddr);
 error:

This is unused now. You do not need new label, just use existing one.

Best regards,
Krzysztof