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.")
Signed-off-by: Navid Emamdoost <[email protected]>
---
drivers/gpu/drm/tiny/hx8357d.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/drivers/gpu/drm/tiny/hx8357d.c b/drivers/gpu/drm/tiny/hx8357d.c
index 9af8ff84974f..da5ec944f47e 100644
--- a/drivers/gpu/drm/tiny/hx8357d.c
+++ b/drivers/gpu/drm/tiny/hx8357d.c
@@ -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)
--
2.17.1
…
> +++ b/drivers/gpu/drm/tiny/hx8357d.c
> @@ -232,44 +232,49 @@ static int hx8357d_probe(struct spi_device *spi)
…
> + 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”?
…
> +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
Please avoid also another typo in the (previous) patch subject.
Regards,
Markus
> …
>> +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
Hi Navid,
Den 27.10.2019 18.32, skrev Navid Emamdoost:
> 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.")
> Signed-off-by: Navid Emamdoost <[email protected]>
> ---
> drivers/gpu/drm/tiny/hx8357d.c | 25 +++++++++++++++----------
> 1 file changed, 15 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/gpu/drm/tiny/hx8357d.c b/drivers/gpu/drm/tiny/hx8357d.c
> index 9af8ff84974f..da5ec944f47e 100644
> --- a/drivers/gpu/drm/tiny/hx8357d.c
> +++ b/drivers/gpu/drm/tiny/hx8357d.c
> @@ -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.
> - 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)
>