Skip to contentSkip to conversation

cpufreq: mediatek: Fix resource leaks on mtk_cpu_create_freq_table() failure

PATCH Superseded Riyan Dhiman 2 participants 4 messages Sep 7, 2024 – Oct 3, 2024 CPU frequency scaling framework

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cpufreq: mediatek: Fix resource leaks on mtk_cpu_create_freq_table() failure

If mtk_cpu_create_freq_table() fails then there is a potential resource leak because memory region is not released and IO memory is not unmapped.
Added error handling to ensure proper cleanup of all resources on failure, preventing potential leaks.

Fixes d776790a5536 (cpufreq: mediatek-hw: Fix double devm_remap in hotplug case)

---
It is more of a extension of the above commit as this error handling was missing in that commit.

v2: Fix commit message.
v1: Added error handling.

 drivers/cpufreq/mediatek-cpufreq-hw.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cpufreq/mediatek-cpufreq-hw.c +3 -1
index 8925e096d5b9..3b1303f350ec 100644
@@ -207,13 +207,15 @@ static int mtk_cpu_resources_init(struct platform_device *pdev,
 	ret = mtk_cpu_create_freq_table(pdev, data);
 	if (ret) {
 		dev_info(dev, "Domain-%d failed to create freq table\n", index);
-		return ret;
+		goto unmap_region;
 	}
 
 	policy->freq_table = data->table;
 	policy->driver_data = data;
 
 	return 0;
+unmap_region:
+	iounmap(base);
 release_region:
 	release_mem_region(res->start, resource_size(res));
 	return ret;
Signature · 1 line
2.46.0
maintainer
#1 lore raw
Quoted 24 lines · Riyan Dhiman

If mtk_cpu_create_freq_table() fails then there is a potential resource leak because memory region is not released and IO memory is not unmapped.
Added error handling to ensure proper cleanup of all resources on failure, preventing potential leaks.

Fixes d776790a5536 (cpufreq: mediatek-hw: Fix double devm_remap in hotplug case)

---
It is more of a extension of the above commit as this error handling was missing in that commit.

v2: Fix commit message.
v1: Added error handling.

 drivers/cpufreq/mediatek-cpufreq-hw.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cpufreq/mediatek-cpufreq-hw.c +0 -0
index 8925e096d5b9..3b1303f350ec 100644
@@ -207,13 +207,15 @@ static int mtk_cpu_resources_init(struct platform_device *pdev,
 	ret = mtk_cpu_create_freq_table(pdev, data);
 	if (ret) {
 		dev_info(dev, "Domain-%d failed to create freq table\n", index);

drivers/cpufreq/mediatek-cpufreq-hw.c static int mtk_cpu_resources_init(struct platform_device *pdev, · line 218

Quoted 10 lines
drivers/cpufreq/mediatek-cpufreq-hw.c +3 -1
-		return ret;
+		goto unmap_region;
 	}
 
 	policy->freq_table = data->table;
 	policy->driver_data = data;
 
 	return 0;
+unmap_region:
+	iounmap(base);

Since this driver already uses devm_* APIs, what about using them instead ?

Signature · 1 line
viresh
#2 lore raw

PATCH v2 1/1 cpufreq: mediatek: Fix resource leaks on mtk_cpu_create_freq_table() failure

Quoted 2 lines

Since this driver already uses devm_* APIs, what about using them instead ?

Since ioremap was called that's why I added a goto statement to handle the cleanup.
In this file, devm_kzalloc and devm_regulator_get are called. Do I need to call this devm_platform_ioremap_resource instead of ioremap if devm_* api need to be used?

Regards,
Riyan Dhiman

maintainer
#3 lore raw
Quoted 3 lines · Riyan Dhiman

In this file, devm_kzalloc and devm_regulator_get are called. Do I need to call this devm_platform_ioremap_resource instead of ioremap if devm_* api need to be used?

Yes, that would do.

Signature · 1 line
viresh