Skip to contentSkip to conversation

drm/tinydrm: Fix memroy leak in hx8357d_probe

PATCH In review Navid Emamdoost 3 participants 5 messages Oct 27, 2019 – Oct 29, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/tinydrm: Fix memroy leak in hx8357d_probe

In the implementation of hx8357d_probe() the allocated memory for dbidev is leaked when an error happens. Release dbidev if any of the following calls fail: devm_gpiod_get(), devm_of_find_backlight(), mipi_dbi_spi_init(), mipi_dbi_init(), drm_dev_register().

Fixes f300c86e33a6 ("drm: Add an hx8367d tinydrm driver.")
---
 drivers/gpu/drm/tiny/hx8357d.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/gpu/drm/tiny/hx8357d.c +15 -10
index 9af8ff84974f..da5ec944f47e 100644
@@ -232,44 +232,49 @@ static int hx8357d_probe(struct spi_device *spi)
 
 	drm = &dbidev->drm;
 	ret = devm_drm_dev_init(dev, drm, &hx8357d_driver);
-	if (ret) {
-		kfree(dbidev);
-		return ret;
-	}
+	if (ret)
+		goto free_dbidev;
 
 	drm_mode_config_init(drm);
 
 	dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
 	if (IS_ERR(dc)) {
 		DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
-		return PTR_ERR(dc);
+		ret = PTR_ERR(dc);
+		goto free_dbidev;
 	}
 
 	dbidev->backlight = devm_of_find_backlight(dev);
-	if (IS_ERR(dbidev->backlight))
-		return PTR_ERR(dbidev->backlight);
+	if (IS_ERR(dbidev->backlight)) {
+		ret = PTR_ERR(dbidev->backlight);
+		goto free_dbidev;
+	}
 
 	device_property_read_u32(dev, "rotation", &rotation);
 
 	ret = mipi_dbi_spi_init(spi, &dbidev->dbi, dc);
 	if (ret)
-		return ret;
+		goto free_dbidev;
 
 	ret = mipi_dbi_dev_init(dbidev, &hx8357d_pipe_funcs, &yx350hv15_mode, rotation);
 	if (ret)
-		return ret;
+		goto free_dbidev;
 
 	drm_mode_config_reset(drm);
 
 	ret = drm_dev_register(drm, 0);
 	if (ret)
-		return ret;
+		goto free_dbidev;
 
 	spi_set_drvdata(spi, drm);
 
 	drm_fbdev_generic_setup(drm, 0);
 
 	return 0;
+
+free_dbidev:
+	kfree(dbidev);
+	return ret;
 }
 
 static int hx8357d_remove(struct spi_device *spi)
Signature · 1 line
2.17.1
#1 lore raw

…

Quoted 2 lines

+++ b/drivers/gpu/drm/tiny/hx8357d.c

(quoted diff) +0 -0
@@ -232,44 +232,49 @@ static int hx8357d_probe(struct spi_device *spi)

…

drivers/gpu/drm/tiny/hx8357d.c static int hx8357d_probe(struct spi_device *spi) · line 269

Quoted 3 lines
drivers/gpu/drm/tiny/hx8357d.c +1 -0
+		goto free_dbidev;
 
 	spi_set_drvdata(spi, drm);

I got another development concern here.
Can it make sense to pass the variable “dbidev” instead of “drm”?

…

drivers/gpu/drm/tiny/hx8357d.c static int hx8357d_probe(struct spi_device *spi) · line 276

Quoted 2 lines
drivers/gpu/drm/tiny/hx8357d.c +2 -0
+free_dbidev:
+	kfree(dbidev);

…

I became curious if there is a need for such a memory release at another place. How do you think about to add this function call also to the implementation of the function “hx8357d_remove”?

Regards,
Markus

#2 lore raw

Re: drm/tinydrm: Fix memory leak in hx8357d_probe

Quoted 6 lines

…

drivers/gpu/drm/tiny/hx8357d.c static int hx8357d_probe(struct spi_device *spi) · line 276

Quoted 2 lines
drivers/gpu/drm/tiny/hx8357d.c +2 -0
+free_dbidev:
+	kfree(dbidev);

…

I became curious if there is a need for such a memory release at another place.

I have taken another look also at a related implementation detail. Now I have realised that the desired clean-up should usually be achieved by the callback function “mipi_dbi_release” for this software module.
https://elixir.bootlin.com/linux/v5.4-rc2/source/drivers/gpu/drm/drm_mipi_dbi.c#L581
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/gpu/drm/drm_mipi_dbi.c?id=0365fb6baeb1ebefbbdad9e3f48bab9b3ccb8df3#n581

Regards,
Markus

#4 lore raw
In reply to Navid Emamdoost

Hi Navid,

Den 27.10.2019 18.32, skrev Navid Emamdoost:

Quoted 16 lines

In the implementation of hx8357d_probe() the allocated memory for dbidev is leaked when an error happens. Release dbidev if any of the following calls fail: devm_gpiod_get(), devm_of_find_backlight(), mipi_dbi_spi_init(), mipi_dbi_init(), drm_dev_register().

Fixes f300c86e33a6 ("drm: Add an hx8367d tinydrm driver.")
---
 drivers/gpu/drm/tiny/hx8357d.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/gpu/drm/tiny/hx8357d.c +0 -0
index 9af8ff84974f..da5ec944f47e 100644

drivers/gpu/drm/tiny/hx8357d.c static int hx8357d_probe(struct spi_device *spi) · line 234

Quoted 3 lines
drivers/gpu/drm/tiny/hx8357d.c +0 -0
@@ -232,44 +232,49 @@ static int hx8357d_probe(struct spi_device *spi)
 
 	drm = &dbidev->drm;
 	ret = devm_drm_dev_init(dev, drm, &hx8357d_driver);

This is a device managed function that releases its resource(s) on device:driver unbind like the other devm_ functions (it can't free dbidev if it fails hence the kfree in the error path here).

This is the release call path:

devm_drm_dev_init_release -> drm_dev_put -> drm_dev_release -> dev->driver->release : mipi_dbi_release.

Noralf.

Quoted 57 lines
drivers/gpu/drm/tiny/hx8357d.c +15 -10
-	if (ret) {
-		kfree(dbidev);
-		return ret;
-	}
+	if (ret)
+		goto free_dbidev;
 
 	drm_mode_config_init(drm);
 
 	dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
 	if (IS_ERR(dc)) {
 		DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
-		return PTR_ERR(dc);
+		ret = PTR_ERR(dc);
+		goto free_dbidev;
 	}
 
 	dbidev->backlight = devm_of_find_backlight(dev);
-	if (IS_ERR(dbidev->backlight))
-		return PTR_ERR(dbidev->backlight);
+	if (IS_ERR(dbidev->backlight)) {
+		ret = PTR_ERR(dbidev->backlight);
+		goto free_dbidev;
+	}
 
 	device_property_read_u32(dev, "rotation", &rotation);
 
 	ret = mipi_dbi_spi_init(spi, &dbidev->dbi, dc);
 	if (ret)
-		return ret;
+		goto free_dbidev;
 
 	ret = mipi_dbi_dev_init(dbidev, &hx8357d_pipe_funcs, &yx350hv15_mode, rotation);
 	if (ret)
-		return ret;
+		goto free_dbidev;
 
 	drm_mode_config_reset(drm);
 
 	ret = drm_dev_register(drm, 0);
 	if (ret)
-		return ret;
+		goto free_dbidev;
 
 	spi_set_drvdata(spi, drm);
 
 	drm_fbdev_generic_setup(drm, 0);
 
 	return 0;
+
+free_dbidev:
+	kfree(dbidev);
+	return ret;
 }
 
 static int hx8357d_remove(struct spi_device *spi)