Skip to contentSkip to conversation

tegra_wm9712: Fix a memory leaking bug in tegra_wm9712_driver_probe()

PATCH Superseded Gen Zhang 2 participants 7 messages May 24, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

PATCH 1/1 tegra_wm9712: Fix a memory leaking bug in tegra_wm9712_driver_probe()

In tegra_wm9712_driver_probe(), 'machine->codec' is allocated by platform_device_alloc(). When it is NULL, function returns ENOMEM. However, 'machine' is allocated by devm_kzalloc() before this site. Thus we should free 'machine' before function ends to prevent memory leaking.

Further, we should free 'machine->util_data', 'machine->codec' and 'machine' before this function normally ends to prevent memory leaking.

---

sound/soc/tegra/tegra_wm9712.c +8 -1
index 864a334..295c41d 100644
@@ -86,7 +86,8 @@ static int tegra_wm9712_driver_probe(struct platform_device *pdev)
 	machine->codec = platform_device_alloc("wm9712-codec", -1);
 	if (!machine->codec) {
 		dev_err(&pdev->dev, "Can't allocate wm9712 platform device\n");
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto codec_free;
 	}
 
 	ret = platform_device_add(machine->codec);
@@ -127,6 +128,10 @@ static int tegra_wm9712_driver_probe(struct platform_device *pdev)
 		goto asoc_utils_fini;
 	}
 
+	tegra_asoc_utils_fini(&machine->util_data);
+	platform_device_del(machine->codec);
+	platform_device_put(machine->codec);
+	devm_kfree(&pdev->dev, machine);
 	return 0;
 
 asoc_utils_fini:
@@ -135,6 +140,8 @@ static int tegra_wm9712_driver_probe(struct platform_device *pdev)
 	platform_device_del(machine->codec);
 codec_put:
 	platform_device_put(machine->codec);
+codec_free:
+	devm_kfree(&pdev->dev, machine);
 	return ret;
 }
 

---

maintainer
#1 lore raw
Quoted 5 lines · Gen Zhang

In tegra_wm9712_driver_probe(), 'machine->codec' is allocated by platform_device_alloc(). When it is NULL, function returns ENOMEM. However, 'machine' is allocated by devm_kzalloc() before this site. Thus we should free 'machine' before function ends to prevent memory leaking.

Memory allocated by devm_xxx() is automatically freed on failure so this is not correct.

Quoted 2 lines

Further, we should free 'machine->util_data', 'machine->codec' and 'machine' before this function normally ends to prevent memory leaking.

This is also incorrect. Why would we free all resources after successfully initialising the driver?

Quoted 17 lines

---

sound/soc/tegra/tegra_wm9712.c +2 -1
index 864a334..295c41d 100644
@@ -86,7 +86,8 @@ static int tegra_wm9712_driver_probe(struct platform_device *pdev)
 	machine->codec = platform_device_alloc("wm9712-codec", -1);
 	if (!machine->codec) {
 		dev_err(&pdev->dev, "Can't allocate wm9712 platform device\n");
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto codec_free;
 	}
 
 	ret = platform_device_add(machine->codec);

sound/soc/tegra/tegra_wm9712.c static int tegra_wm9712_driver_probe(struct platform_device *pdev) · line 135

Quoted 8 lines
sound/soc/tegra/tegra_wm9712.c +4 -0
@@ -127,6 +128,10 @@ static int tegra_wm9712_driver_probe(struct platform_device *pdev)
 		goto asoc_utils_fini;
 	}
 
+	tegra_asoc_utils_fini(&machine->util_data);
+	platform_device_del(machine->codec);
+	platform_device_put(machine->codec);
+	devm_kfree(&pdev->dev, machine);
 	return 0;

As stated above, this is incorrect.

Did you actually test this? I think you would find this would break the driver.

Jon

Signature · 1 line
nvpublic
author
#2 lore raw
Quoted 10 lines · Jon Hunter
Quoted 5 lines · Gen Zhang

In tegra_wm9712_driver_probe(), 'machine->codec' is allocated by platform_device_alloc(). When it is NULL, function returns ENOMEM. However, 'machine' is allocated by devm_kzalloc() before this site. Thus we should free 'machine' before function ends to prevent memory leaking.

Memory allocated by devm_xxx() is automatically freed on failure so this is not correct.

Thanks for your comments, Jon. But after I examined the code, I am still confused about the usage of devm_kmalloc(). You can kindly refer to hisi_sas_debugfs_init() in drivers/scsi/hisi_sas/hisi_sas_main.c. And devm_kfree() is used to free a memory allocated by devm_kmalloc(). And I found other situations similar to this in other files.

So, I hope you can give me some guidance on this. Thanks!

Quoted 6 lines
Quoted 3 lines

Further, we should free 'machine->util_data', 'machine->codec' and 'machine' before this function normally ends to prevent memory leaking.

This is also incorrect. Why would we free all resources after successfully initialising the driver?

I re-checked this part, and it is totally incorrect. It should be deleted.

Thanks
Gen

maintainer
#3 lore raw
Quoted 11 lines · Gen Zhang
Quoted 10 lines · Jon Hunter
Quoted 5 lines · Gen Zhang

In tegra_wm9712_driver_probe(), 'machine->codec' is allocated by platform_device_alloc(). When it is NULL, function returns ENOMEM. However, 'machine' is allocated by devm_kzalloc() before this site. Thus we should free 'machine' before function ends to prevent memory leaking.

Memory allocated by devm_xxx() is automatically freed on failure so this is not correct.

Quoted 7 lines

Thanks for your comments, Jon. But after I examined the code, I am still confused about the usage of devm_kmalloc(). You can kindly refer to hisi_sas_debugfs_init() in drivers/scsi/hisi_sas/hisi_sas_main.c. And devm_kfree() is used to free a memory allocated by devm_kmalloc(). And I found other situations similar to this in other files.

So, I hope you can give me some guidance on this. Thanks!

Please refer to the devres documentation [0].

Cheers,
Jon

[0] https://www.kernel.org/doc/Documentation/driver-model/devres.txt

Signature · 1 line
nvpublic
author
#4 lore raw
Quoted 19 lines · Jon Hunter
Quoted 17 lines · Gen Zhang
Quoted 10 lines · Jon Hunter
Quoted 5 lines · Gen Zhang

In tegra_wm9712_driver_probe(), 'machine->codec' is allocated by platform_device_alloc(). When it is NULL, function returns ENOMEM. However, 'machine' is allocated by devm_kzalloc() before this site. Thus we should free 'machine' before function ends to prevent memory leaking.

Memory allocated by devm_xxx() is automatically freed on failure so this is not correct.

Thanks for your comments, Jon. But after I examined the code, I am still confused about the usage of devm_kmalloc(). You can kindly refer to hisi_sas_debugfs_init() in drivers/scsi/hisi_sas/hisi_sas_main.c. And devm_kfree() is used to free a memory allocated by devm_kmalloc(). And I found other situations similar to this in other files.

Quoted 11 lines
Quoted 1 line

So, I hope you can give me some guidance on this. Thanks!

Please refer to the devres documentation [0].

Cheers,
Jon

[0] https://www.kernel.org/doc/Documentation/driver-model/devres.txt

Signature · 1 line
nvpublic

Thanks for your reply. I figured out that devm_kmalloc will free the memory no matter fail or not. But I still want to ask why other codes as I above mentioned use devm_kfree() to free memory allocated by devm_kmalloc(). If the memory is automatically freed, is this devm_kfee() redundant codes that should be removed? Am I misunderstanding this again or it is something else?

Thanks
Gen

maintainer
#5 lore raw
Quoted 29 lines · Gen Zhang
Quoted 28 lines · Jon Hunter
Quoted 11 lines · Gen Zhang
Quoted 10 lines · Jon Hunter
Quoted 5 lines · Gen Zhang

In tegra_wm9712_driver_probe(), 'machine->codec' is allocated by platform_device_alloc(). When it is NULL, function returns ENOMEM. However, 'machine' is allocated by devm_kzalloc() before this site. Thus we should free 'machine' before function ends to prevent memory leaking.

Memory allocated by devm_xxx() is automatically freed on failure so this is not correct.

Quoted 7 lines

Thanks for your comments, Jon. But after I examined the code, I am still confused about the usage of devm_kmalloc(). You can kindly refer to hisi_sas_debugfs_init() in drivers/scsi/hisi_sas/hisi_sas_main.c. And devm_kfree() is used to free a memory allocated by devm_kmalloc(). And I found other situations similar to this in other files.

So, I hope you can give me some guidance on this. Thanks!

Please refer to the devres documentation [0].

Cheers,
Jon

[0] https://www.kernel.org/doc/Documentation/driver-model/devres.txt

Quoted 8 lines
Quoted 2 lines
Signature · 1 line
nvpublic

Thanks for your reply. I figured out that devm_kmalloc will free the memory no matter fail or not. But I still want to ask why other codes as I above mentioned use devm_kfree() to free memory allocated by devm_kmalloc(). If the memory is automatically freed, is this devm_kfee() redundant codes that should be removed? Am I misunderstanding this again or it is something else?

There could well be cases where you need to explicitly call devm_kfree(), but having a quick glance at the example above, I don't see why you would call devm_kfree() here and yes looks like that code could be simplified significantly. Notice that hisi_sas_debugfs_exit() does not free any memory as it is not necessary to explicitly do so.

Cheers
Jon

Signature · 1 line
nvpublic
author
#6 lore raw
Quoted 11 lines · Jon Hunter

There could well be cases where you need to explicitly call devm_kfree(), but having a quick glance at the example above, I don't see why you would call devm_kfree() here and yes looks like that code could be simplified significantly. Notice that hisi_sas_debugfs_exit() does not free any memory as it is not necessary to explicitly do so.

Cheers
Jon

Signature · 1 line
nvpublic

Thanks for your suggestions, Jon! I think I need to e-mail to those maintainers about this issue.

Thanks
Gen