diff mbox series

[v2] drm: rcar-du: Fix leak of CMM platform device reference

Message ID 20201216140836.31328-1-laurent.pinchart+renesas@ideasonboard.com (mailing list archive)
State Mainlined
Commit 9fa120458da142da0d1d3eaf6f6a3a2c2c91d27b
Delegated to: Kieran Bingham
Headers show
Series [v2] drm: rcar-du: Fix leak of CMM platform device reference | expand

Commit Message

Laurent Pinchart Dec. 16, 2020, 2:08 p.m. UTC
The device references acquired by of_find_device_by_node() are not
released by the driver. Fix this by registering a cleanup action.

Fixes: 8de707aeb452 ("drm: rcar-du: kms: Initialize CMM instances")
Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
---
Changes since v1:

- Only set rcdu->cmms[] if the CMM config option is enabled
- Use platform_device_put()
---
 drivers/gpu/drm/rcar-du/rcar_du_kms.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

Comments

Jacopo Mondi Dec. 16, 2020, 2:16 p.m. UTC | #1
Hi Laurent,

On Wed, Dec 16, 2020 at 04:08:36PM +0200, Laurent Pinchart wrote:
> The device references acquired by of_find_device_by_node() are not
> released by the driver. Fix this by registering a cleanup action.
>
> Fixes: 8de707aeb452 ("drm: rcar-du: kms: Initialize CMM instances")
> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> ---
> Changes since v1:
>
> - Only set rcdu->cmms[] if the CMM config option is enabled
> - Use platform_device_put()
> ---
>  drivers/gpu/drm/rcar-du/rcar_du_kms.c | 22 +++++++++++++++++++---
>  1 file changed, 19 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/rcar-du/rcar_du_kms.c b/drivers/gpu/drm/rcar-du/rcar_du_kms.c
> index 92dfa3d4c011..fdb8a0d127ad 100644
> --- a/drivers/gpu/drm/rcar-du/rcar_du_kms.c
> +++ b/drivers/gpu/drm/rcar-du/rcar_du_kms.c
> @@ -14,6 +14,7 @@
>  #include <drm/drm_fb_cma_helper.h>
>  #include <drm/drm_gem_cma_helper.h>
>  #include <drm/drm_gem_framebuffer_helper.h>
> +#include <drm/drm_managed.h>
>  #include <drm/drm_probe_helper.h>
>  #include <drm/drm_vblank.h>
>
> @@ -726,8 +727,12 @@ static int rcar_du_cmm_init(struct rcar_du_device *rcdu)
>  		 * disabled: return 0 and let the DU continue probing.
>  		 */
>  		ret = rcar_cmm_init(pdev);
> -		if (ret)
> +		if (ret) {
> +			platform_device_put(pdev);
>  			return ret == -ENODEV ? 0 : ret;
> +		}
> +
> +		rcdu->cmms[i] = pdev;
>
>  		/*
>  		 * Enforce suspend/resume ordering by making the CMM a provider

Sorry but don't we have an error path here below too, and if it fails
-EINVAL is returned and the whole modeset_init() bails out without
having put the platform device.

> @@ -739,13 +744,20 @@ static int rcar_du_cmm_init(struct rcar_du_device *rcdu)
>  				"Failed to create device link to CMM%u\n", i);
>  			return -EINVAL;
>  		}
> -
> -		rcdu->cmms[i] = pdev;
>  	}
>
>  	return 0;
>  }
>
> +static void rcar_du_modeset_cleanup(struct drm_device *dev, void *res)
> +{
> +	struct rcar_du_device *rcdu = to_rcar_du_device(dev);
> +	unsigned int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(rcdu->cmms); ++i)
> +		platform_device_put(rcdu->cmms[i]);
> +}
> +
>  int rcar_du_modeset_init(struct rcar_du_device *rcdu)
>  {
>  	static const unsigned int mmio_offsets[] = {
> @@ -766,6 +778,10 @@ int rcar_du_modeset_init(struct rcar_du_device *rcdu)
>  	if (ret)
>  		return ret;
>
> +	ret = drmm_add_action(&rcdu->ddev, rcar_du_modeset_cleanup, NULL);
> +	if (ret)
> +		return ret;
> +
>  	dev->mode_config.min_width = 0;
>  	dev->mode_config.min_height = 0;
>  	dev->mode_config.normalize_zpos = true;
> --
> Regards,
>
> Laurent Pinchart
>
Laurent Pinchart Dec. 16, 2020, 2:24 p.m. UTC | #2
Hi Jacopo,

On Wed, Dec 16, 2020 at 03:16:28PM +0100, Jacopo Mondi wrote:
> On Wed, Dec 16, 2020 at 04:08:36PM +0200, Laurent Pinchart wrote:
> > The device references acquired by of_find_device_by_node() are not
> > released by the driver. Fix this by registering a cleanup action.
> >
> > Fixes: 8de707aeb452 ("drm: rcar-du: kms: Initialize CMM instances")
> > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > ---
> > Changes since v1:
> >
> > - Only set rcdu->cmms[] if the CMM config option is enabled
> > - Use platform_device_put()
> > ---
> >  drivers/gpu/drm/rcar-du/rcar_du_kms.c | 22 +++++++++++++++++++---
> >  1 file changed, 19 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/rcar-du/rcar_du_kms.c b/drivers/gpu/drm/rcar-du/rcar_du_kms.c
> > index 92dfa3d4c011..fdb8a0d127ad 100644
> > --- a/drivers/gpu/drm/rcar-du/rcar_du_kms.c
> > +++ b/drivers/gpu/drm/rcar-du/rcar_du_kms.c
> > @@ -14,6 +14,7 @@
> >  #include <drm/drm_fb_cma_helper.h>
> >  #include <drm/drm_gem_cma_helper.h>
> >  #include <drm/drm_gem_framebuffer_helper.h>
> > +#include <drm/drm_managed.h>
> >  #include <drm/drm_probe_helper.h>
> >  #include <drm/drm_vblank.h>
> >
> > @@ -726,8 +727,12 @@ static int rcar_du_cmm_init(struct rcar_du_device *rcdu)
> >  		 * disabled: return 0 and let the DU continue probing.
> >  		 */
> >  		ret = rcar_cmm_init(pdev);
> > -		if (ret)
> > +		if (ret) {
> > +			platform_device_put(pdev);
> >  			return ret == -ENODEV ? 0 : ret;
> > +		}
> > +
> > +		rcdu->cmms[i] = pdev;
> >
> >  		/*
> >  		 * Enforce suspend/resume ordering by making the CMM a provider
> 
> Sorry but don't we have an error path here below too, and if it fails
> -EINVAL is returned and the whole modeset_init() bails out without
> having put the platform device.

There's an error path below, but in that case rcdu->cmms[i] will be set
and the cleanup action will take care of it.

> > @@ -739,13 +744,20 @@ static int rcar_du_cmm_init(struct rcar_du_device *rcdu)
> >  				"Failed to create device link to CMM%u\n", i);
> >  			return -EINVAL;
> >  		}
> > -
> > -		rcdu->cmms[i] = pdev;
> >  	}
> >
> >  	return 0;
> >  }
> >
> > +static void rcar_du_modeset_cleanup(struct drm_device *dev, void *res)
> > +{
> > +	struct rcar_du_device *rcdu = to_rcar_du_device(dev);
> > +	unsigned int i;
> > +
> > +	for (i = 0; i < ARRAY_SIZE(rcdu->cmms); ++i)
> > +		platform_device_put(rcdu->cmms[i]);
> > +}
> > +
> >  int rcar_du_modeset_init(struct rcar_du_device *rcdu)
> >  {
> >  	static const unsigned int mmio_offsets[] = {
> > @@ -766,6 +778,10 @@ int rcar_du_modeset_init(struct rcar_du_device *rcdu)
> >  	if (ret)
> >  		return ret;
> >
> > +	ret = drmm_add_action(&rcdu->ddev, rcar_du_modeset_cleanup, NULL);
> > +	if (ret)
> > +		return ret;
> > +
> >  	dev->mode_config.min_width = 0;
> >  	dev->mode_config.min_height = 0;
> >  	dev->mode_config.normalize_zpos = true;
Jacopo Mondi Dec. 16, 2020, 2:32 p.m. UTC | #3
Hi Laurent
On Wed, Dec 16, 2020 at 04:24:11PM +0200, Laurent Pinchart wrote:
> Hi Jacopo,
>
> On Wed, Dec 16, 2020 at 03:16:28PM +0100, Jacopo Mondi wrote:
> > On Wed, Dec 16, 2020 at 04:08:36PM +0200, Laurent Pinchart wrote:
> > > The device references acquired by of_find_device_by_node() are not
> > > released by the driver. Fix this by registering a cleanup action.
> > >
> > > Fixes: 8de707aeb452 ("drm: rcar-du: kms: Initialize CMM instances")
> > > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > > ---
> > > Changes since v1:
> > >
> > > - Only set rcdu->cmms[] if the CMM config option is enabled
> > > - Use platform_device_put()
> > > ---
> > >  drivers/gpu/drm/rcar-du/rcar_du_kms.c | 22 +++++++++++++++++++---
> > >  1 file changed, 19 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/drivers/gpu/drm/rcar-du/rcar_du_kms.c b/drivers/gpu/drm/rcar-du/rcar_du_kms.c
> > > index 92dfa3d4c011..fdb8a0d127ad 100644
> > > --- a/drivers/gpu/drm/rcar-du/rcar_du_kms.c
> > > +++ b/drivers/gpu/drm/rcar-du/rcar_du_kms.c
> > > @@ -14,6 +14,7 @@
> > >  #include <drm/drm_fb_cma_helper.h>
> > >  #include <drm/drm_gem_cma_helper.h>
> > >  #include <drm/drm_gem_framebuffer_helper.h>
> > > +#include <drm/drm_managed.h>
> > >  #include <drm/drm_probe_helper.h>
> > >  #include <drm/drm_vblank.h>
> > >
> > > @@ -726,8 +727,12 @@ static int rcar_du_cmm_init(struct rcar_du_device *rcdu)
> > >  		 * disabled: return 0 and let the DU continue probing.
> > >  		 */
> > >  		ret = rcar_cmm_init(pdev);
> > > -		if (ret)
> > > +		if (ret) {
> > > +			platform_device_put(pdev);
> > >  			return ret == -ENODEV ? 0 : ret;
> > > +		}
> > > +
> > > +		rcdu->cmms[i] = pdev;
> > >
> > >  		/*
> > >  		 * Enforce suspend/resume ordering by making the CMM a provider
> >
> > Sorry but don't we have an error path here below too, and if it fails
> > -EINVAL is returned and the whole modeset_init() bails out without
> > having put the platform device.
>
> There's an error path below, but in that case rcdu->cmms[i] will be set
> and the cleanup action will take care of it.
>

Right, the helper is registered before the init() function eventually
bails out. Sorry for being unnecessarily picky.

Reviewed-by: Jacopo Mondi <jacopo+renesas@jmondi.org>

Thanks
  j


> > > @@ -739,13 +744,20 @@ static int rcar_du_cmm_init(struct rcar_du_device *rcdu)
> > >  				"Failed to create device link to CMM%u\n", i);
> > >  			return -EINVAL;
> > >  		}
> > > -
> > > -		rcdu->cmms[i] = pdev;
> > >  	}
> > >
> > >  	return 0;
> > >  }
> > >
> > > +static void rcar_du_modeset_cleanup(struct drm_device *dev, void *res)
> > > +{
> > > +	struct rcar_du_device *rcdu = to_rcar_du_device(dev);
> > > +	unsigned int i;
> > > +
> > > +	for (i = 0; i < ARRAY_SIZE(rcdu->cmms); ++i)
> > > +		platform_device_put(rcdu->cmms[i]);
> > > +}
> > > +
> > >  int rcar_du_modeset_init(struct rcar_du_device *rcdu)
> > >  {
> > >  	static const unsigned int mmio_offsets[] = {
> > > @@ -766,6 +778,10 @@ int rcar_du_modeset_init(struct rcar_du_device *rcdu)
> > >  	if (ret)
> > >  		return ret;
> > >
> > > +	ret = drmm_add_action(&rcdu->ddev, rcar_du_modeset_cleanup, NULL);
> > > +	if (ret)
> > > +		return ret;
> > > +
> > >  	dev->mode_config.min_width = 0;
> > >  	dev->mode_config.min_height = 0;
> > >  	dev->mode_config.normalize_zpos = true;
>
> --
> Regards,
>
> Laurent Pinchart
diff mbox series

Patch

diff --git a/drivers/gpu/drm/rcar-du/rcar_du_kms.c b/drivers/gpu/drm/rcar-du/rcar_du_kms.c
index 92dfa3d4c011..fdb8a0d127ad 100644
--- a/drivers/gpu/drm/rcar-du/rcar_du_kms.c
+++ b/drivers/gpu/drm/rcar-du/rcar_du_kms.c
@@ -14,6 +14,7 @@ 
 #include <drm/drm_fb_cma_helper.h>
 #include <drm/drm_gem_cma_helper.h>
 #include <drm/drm_gem_framebuffer_helper.h>
+#include <drm/drm_managed.h>
 #include <drm/drm_probe_helper.h>
 #include <drm/drm_vblank.h>
 
@@ -726,8 +727,12 @@  static int rcar_du_cmm_init(struct rcar_du_device *rcdu)
 		 * disabled: return 0 and let the DU continue probing.
 		 */
 		ret = rcar_cmm_init(pdev);
-		if (ret)
+		if (ret) {
+			platform_device_put(pdev);
 			return ret == -ENODEV ? 0 : ret;
+		}
+
+		rcdu->cmms[i] = pdev;
 
 		/*
 		 * Enforce suspend/resume ordering by making the CMM a provider
@@ -739,13 +744,20 @@  static int rcar_du_cmm_init(struct rcar_du_device *rcdu)
 				"Failed to create device link to CMM%u\n", i);
 			return -EINVAL;
 		}
-
-		rcdu->cmms[i] = pdev;
 	}
 
 	return 0;
 }
 
+static void rcar_du_modeset_cleanup(struct drm_device *dev, void *res)
+{
+	struct rcar_du_device *rcdu = to_rcar_du_device(dev);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(rcdu->cmms); ++i)
+		platform_device_put(rcdu->cmms[i]);
+}
+
 int rcar_du_modeset_init(struct rcar_du_device *rcdu)
 {
 	static const unsigned int mmio_offsets[] = {
@@ -766,6 +778,10 @@  int rcar_du_modeset_init(struct rcar_du_device *rcdu)
 	if (ret)
 		return ret;
 
+	ret = drmm_add_action(&rcdu->ddev, rcar_du_modeset_cleanup, NULL);
+	if (ret)
+		return ret;
+
 	dev->mode_config.min_width = 0;
 	dev->mode_config.min_height = 0;
 	dev->mode_config.normalize_zpos = true;