diff mbox series

[07/11] drm/tinydrm/repaper: Use devm_drm_dev_*()

Message ID 20190120114318.49199-8-noralf@tronnes.org (mailing list archive)
State New, archived
Headers show
Series drm/tinydrm: Remove tinydrm_device | expand

Commit Message

Noralf Trønnes Jan. 20, 2019, 11:43 a.m. UTC
Use devm_drm_dev_init(), devm_drm_dev_register_with_fbdev() and drop
using tinydrm_device.

Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
---
 drivers/gpu/drm/tinydrm/repaper.c | 63 ++++++++++++++++++++-----------
 1 file changed, 40 insertions(+), 23 deletions(-)

Comments

Sam Ravnborg Jan. 20, 2019, 10:22 p.m. UTC | #1
Hi Noralf.

On Sun, Jan 20, 2019 at 12:43:14PM +0100, Noralf Trønnes wrote:
> Use devm_drm_dev_init(), devm_drm_dev_register_with_fbdev() and drop
> using tinydrm_device.
> 
> Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
> ---
>  drivers/gpu/drm/tinydrm/repaper.c | 63 ++++++++++++++++++++-----------
>  1 file changed, 40 insertions(+), 23 deletions(-)
> 
> diff --git a/drivers/gpu/drm/tinydrm/repaper.c b/drivers/gpu/drm/tinydrm/repaper.c
> index 3141241ca8f0..4c8205565668 100644
> --- a/drivers/gpu/drm/tinydrm/repaper.c
> +++ b/drivers/gpu/drm/tinydrm/repaper.c
> @@ -34,7 +34,7 @@
>  #include <drm/drm_gem_framebuffer_helper.h>
>  #include <drm/drm_rect.h>
>  #include <drm/drm_vblank.h>
> -#include <drm/tinydrm/tinydrm.h>
> +#include <drm/drm_simple_kms_helper.h>
>  #include <drm/tinydrm/tinydrm-helpers.h>
>  
>  #define REPAPER_RID_G2_COG_ID	0x12
> @@ -60,7 +60,8 @@ enum repaper_epd_border_byte {
>  };
>  
>  struct repaper_epd {
> -	struct tinydrm_device tinydrm;
> +	struct drm_device base;
> +	struct drm_simple_display_pipe pipe;
>  	struct spi_device *spi;
>  
>  	struct gpio_desc *panel_on;
> @@ -89,10 +90,9 @@ struct repaper_epd {
>  	bool partial;
>  };
>  
> -static inline struct repaper_epd *
> -epd_from_tinydrm(struct tinydrm_device *tdev)
> +static inline struct repaper_epd *drm_to_epd(struct drm_device *drm)
>  {
> -	return container_of(tdev, struct repaper_epd, tinydrm);
> +	return container_of(drm, struct repaper_epd, base);
>  }
>  
>  static int repaper_spi_transfer(struct spi_device *spi, u8 header,
> @@ -530,8 +530,7 @@ static int repaper_fb_dirty(struct drm_framebuffer *fb)
>  {
>  	struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
>  	struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
> -	struct tinydrm_device *tdev = fb->dev->dev_private;
> -	struct repaper_epd *epd = epd_from_tinydrm(tdev);
> +	struct repaper_epd *epd = drm_to_epd(fb->dev);
>  	struct drm_rect clip;
>  	u8 *buf = NULL;
>  	int ret = 0;
> @@ -646,8 +645,7 @@ static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
>  				struct drm_crtc_state *crtc_state,
>  				struct drm_plane_state *plane_state)
>  {
> -	struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
> -	struct repaper_epd *epd = epd_from_tinydrm(tdev);
> +	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
>  	struct spi_device *spi = epd->spi;
>  	struct device *dev = &spi->dev;
>  	bool dc_ok = false;
> @@ -781,8 +779,7 @@ static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
>  
>  static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe)
>  {
> -	struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
> -	struct repaper_epd *epd = epd_from_tinydrm(tdev);
> +	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
>  	struct spi_device *spi = epd->spi;
>  	unsigned int line;
>  
> @@ -856,6 +853,23 @@ static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = {
>  	.prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
>  };
>  
> +static const struct drm_mode_config_funcs repaper_mode_config_funcs = {
> +	.fb_create = drm_gem_fb_create_with_dirty,
> +	.atomic_check = drm_atomic_helper_check,
> +	.atomic_commit = drm_atomic_helper_commit,
> +};
> +
> +static void repaper_release(struct drm_device *drm)
> +{
> +	struct repaper_epd *epd = drm_to_epd(drm);
> +
> +	DRM_DEBUG_DRIVER("\n");
> +
> +	drm_mode_config_cleanup(drm);
> +	drm_dev_fini(drm);
> +	kfree(epd);
> +}
> +
>  static const uint32_t repaper_formats[] = {
>  	DRM_FORMAT_XRGB8888,
>  };
> @@ -894,6 +908,7 @@ static struct drm_driver repaper_driver = {
>  	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
>  				  DRIVER_ATOMIC,
>  	.fops			= &repaper_fops,
> +	.release		= repaper_release,
>  	DRM_GEM_CMA_VMAP_DRIVER_OPS,
>  	.name			= "repaper",
>  	.desc			= "Pervasive Displays RePaper e-ink panels",
> @@ -927,7 +942,6 @@ static int repaper_probe(struct spi_device *spi)
>  	const struct of_device_id *match;
>  	struct drm_connector *connector;
>  	struct device *dev = &spi->dev;
> -	struct tinydrm_device *tdev;
>  	enum repaper_model model;
>  	const char *thermal_zone;
>  	struct repaper_epd *epd;
> @@ -952,10 +966,20 @@ static int repaper_probe(struct spi_device *spi)
>  		}
>  	}
>  
> -	epd = devm_kzalloc(dev, sizeof(*epd), GFP_KERNEL);
> +	epd = kzalloc(sizeof(*epd), GFP_KERNEL);
>  	if (!epd)
>  		return -ENOMEM;
>  
> +	drm = &epd->base;
> +	ret = devm_drm_dev_init(dev, drm, &repaper_driver);
> +	if (ret) {
> +		kfree(epd);
> +		return ret;
> +	}
Here you go to the trouble and free epd on error.

> +
> +	drm_mode_config_init(drm);
> +	drm->mode_config.funcs = &repaper_mode_config_funcs;
> +
>  	epd->spi = spi;
>  
>  	epd->panel_on = devm_gpiod_get(dev, "panel-on", GPIOD_OUT_LOW);
> @@ -1066,32 +1090,25 @@ static int repaper_probe(struct spi_device *spi)
>  	if (!epd->current_frame)
>  		return -ENOMEM;
>  
> -	tdev = &epd->tinydrm;
> -
> -	ret = devm_tinydrm_init(dev, tdev, &repaper_driver);
> -	if (ret)
> -		return ret;
> -
> -	drm = tdev->drm;
>  
>  	connector = drm_simple_connector_create(drm, DRM_MODE_CONNECTOR_VIRTUAL, mode, 0);
>  	if (IS_ERR(connector))
>  		return PTR_ERR(connector);
>  
> -	ret = drm_simple_display_pipe_init(drm, &tdev->pipe, &repaper_pipe_funcs,
> +	ret = drm_simple_display_pipe_init(drm, &epd->pipe, &repaper_pipe_funcs,
>  					   repaper_formats, ARRAY_SIZE(repaper_formats),
>  					   NULL, connector);
>  	if (ret)
>  		return ret;
But later in the same function epd is not freed.
Looks like a leak?
Nothing new if this is correct but just spotted it.

>  
>  	drm_simple_connector_set_mode_config(connector);
> -	drm_mode_config_reset(tdev->drm);
> +	drm_mode_config_reset(drm);
>  
>  	spi_set_drvdata(spi, drm);
>  
>  	DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
>  
> -	return devm_tinydrm_register(tdev);
> +	return devm_drm_dev_register_with_fbdev(drm, 0);
>  }
>  
>  static void repaper_shutdown(struct spi_device *spi)


	Sam
Sam Ravnborg Jan. 20, 2019, 10:25 p.m. UTC | #2
On Sun, Jan 20, 2019 at 11:22:36PM +0100, Sam Ravnborg wrote:
> Hi Noralf.
> 
> On Sun, Jan 20, 2019 at 12:43:14PM +0100, Noralf Trønnes wrote:
> > Use devm_drm_dev_init(), devm_drm_dev_register_with_fbdev() and drop
> > using tinydrm_device.
> > 
> > Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
> > ---
> >  drivers/gpu/drm/tinydrm/repaper.c | 63 ++++++++++++++++++++-----------
> >  1 file changed, 40 insertions(+), 23 deletions(-)
> > 
> > diff --git a/drivers/gpu/drm/tinydrm/repaper.c b/drivers/gpu/drm/tinydrm/repaper.c
> > index 3141241ca8f0..4c8205565668 100644
> > --- a/drivers/gpu/drm/tinydrm/repaper.c
> > +++ b/drivers/gpu/drm/tinydrm/repaper.c
> > @@ -34,7 +34,7 @@
> >  #include <drm/drm_gem_framebuffer_helper.h>
> >  #include <drm/drm_rect.h>
> >  #include <drm/drm_vblank.h>
> > -#include <drm/tinydrm/tinydrm.h>
> > +#include <drm/drm_simple_kms_helper.h>
> >  #include <drm/tinydrm/tinydrm-helpers.h>
> >  
> >  #define REPAPER_RID_G2_COG_ID	0x12
> > @@ -60,7 +60,8 @@ enum repaper_epd_border_byte {
> >  };
> >  
> >  struct repaper_epd {
> > -	struct tinydrm_device tinydrm;
> > +	struct drm_device base;
> > +	struct drm_simple_display_pipe pipe;
> >  	struct spi_device *spi;
> >  
> >  	struct gpio_desc *panel_on;
> > @@ -89,10 +90,9 @@ struct repaper_epd {
> >  	bool partial;
> >  };
> >  
> > -static inline struct repaper_epd *
> > -epd_from_tinydrm(struct tinydrm_device *tdev)
> > +static inline struct repaper_epd *drm_to_epd(struct drm_device *drm)
> >  {
> > -	return container_of(tdev, struct repaper_epd, tinydrm);
> > +	return container_of(drm, struct repaper_epd, base);
> >  }
> >  
> >  static int repaper_spi_transfer(struct spi_device *spi, u8 header,
> > @@ -530,8 +530,7 @@ static int repaper_fb_dirty(struct drm_framebuffer *fb)
> >  {
> >  	struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
> >  	struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
> > -	struct tinydrm_device *tdev = fb->dev->dev_private;
> > -	struct repaper_epd *epd = epd_from_tinydrm(tdev);
> > +	struct repaper_epd *epd = drm_to_epd(fb->dev);
> >  	struct drm_rect clip;
> >  	u8 *buf = NULL;
> >  	int ret = 0;
> > @@ -646,8 +645,7 @@ static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
> >  				struct drm_crtc_state *crtc_state,
> >  				struct drm_plane_state *plane_state)
> >  {
> > -	struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
> > -	struct repaper_epd *epd = epd_from_tinydrm(tdev);
> > +	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
> >  	struct spi_device *spi = epd->spi;
> >  	struct device *dev = &spi->dev;
> >  	bool dc_ok = false;
> > @@ -781,8 +779,7 @@ static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
> >  
> >  static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe)
> >  {
> > -	struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
> > -	struct repaper_epd *epd = epd_from_tinydrm(tdev);
> > +	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
> >  	struct spi_device *spi = epd->spi;
> >  	unsigned int line;
> >  
> > @@ -856,6 +853,23 @@ static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = {
> >  	.prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
> >  };
> >  
> > +static const struct drm_mode_config_funcs repaper_mode_config_funcs = {
> > +	.fb_create = drm_gem_fb_create_with_dirty,
> > +	.atomic_check = drm_atomic_helper_check,
> > +	.atomic_commit = drm_atomic_helper_commit,
> > +};
> > +
> > +static void repaper_release(struct drm_device *drm)
> > +{
> > +	struct repaper_epd *epd = drm_to_epd(drm);
> > +
> > +	DRM_DEBUG_DRIVER("\n");
> > +
> > +	drm_mode_config_cleanup(drm);
> > +	drm_dev_fini(drm);
> > +	kfree(epd);
> > +}
> > +
> >  static const uint32_t repaper_formats[] = {
> >  	DRM_FORMAT_XRGB8888,
> >  };
> > @@ -894,6 +908,7 @@ static struct drm_driver repaper_driver = {
> >  	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
> >  				  DRIVER_ATOMIC,
> >  	.fops			= &repaper_fops,
> > +	.release		= repaper_release,
> >  	DRM_GEM_CMA_VMAP_DRIVER_OPS,
> >  	.name			= "repaper",
> >  	.desc			= "Pervasive Displays RePaper e-ink panels",
> > @@ -927,7 +942,6 @@ static int repaper_probe(struct spi_device *spi)
> >  	const struct of_device_id *match;
> >  	struct drm_connector *connector;
> >  	struct device *dev = &spi->dev;
> > -	struct tinydrm_device *tdev;
> >  	enum repaper_model model;
> >  	const char *thermal_zone;
> >  	struct repaper_epd *epd;
> > @@ -952,10 +966,20 @@ static int repaper_probe(struct spi_device *spi)
> >  		}
> >  	}
> >  
> > -	epd = devm_kzalloc(dev, sizeof(*epd), GFP_KERNEL);
> > +	epd = kzalloc(sizeof(*epd), GFP_KERNEL);
> >  	if (!epd)
> >  		return -ENOMEM;
> >  
> > +	drm = &epd->base;
> > +	ret = devm_drm_dev_init(dev, drm, &repaper_driver);
> > +	if (ret) {
> > +		kfree(epd);
> > +		return ret;
> > +	}
> Here you go to the trouble and free epd on error.
> 
> > +
> > +	drm_mode_config_init(drm);
> > +	drm->mode_config.funcs = &repaper_mode_config_funcs;
> > +
> >  	epd->spi = spi;
> >  
> >  	epd->panel_on = devm_gpiod_get(dev, "panel-on", GPIOD_OUT_LOW);
> > @@ -1066,32 +1090,25 @@ static int repaper_probe(struct spi_device *spi)
> >  	if (!epd->current_frame)
> >  		return -ENOMEM;
> >  
> > -	tdev = &epd->tinydrm;
> > -
> > -	ret = devm_tinydrm_init(dev, tdev, &repaper_driver);
> > -	if (ret)
> > -		return ret;
> > -
> > -	drm = tdev->drm;
> >  
> >  	connector = drm_simple_connector_create(drm, DRM_MODE_CONNECTOR_VIRTUAL, mode, 0);
> >  	if (IS_ERR(connector))
> >  		return PTR_ERR(connector);
> >  
> > -	ret = drm_simple_display_pipe_init(drm, &tdev->pipe, &repaper_pipe_funcs,
> > +	ret = drm_simple_display_pipe_init(drm, &epd->pipe, &repaper_pipe_funcs,
> >  					   repaper_formats, ARRAY_SIZE(repaper_formats),
> >  					   NULL, connector);
> >  	if (ret)
> >  		return ret;
> But later in the same function epd is not freed.
> Looks like a leak?
> Nothing new if this is correct but just spotted it.

The part with "nothing new" I take back. This code converts
devm_kzalloc() to the unmanaged variant.
So the leak is new - and also present in
drivers in next patch (if there is a leak).

Why is the conversion to an unmanaged variant of kzalloc() required?

	Sam
Noralf Trønnes Jan. 21, 2019, 1:15 p.m. UTC | #3
Den 20.01.2019 23.25, skrev Sam Ravnborg:
> On Sun, Jan 20, 2019 at 11:22:36PM +0100, Sam Ravnborg wrote:
>> Hi Noralf.
>>
>> On Sun, Jan 20, 2019 at 12:43:14PM +0100, Noralf Trønnes wrote:
>>> Use devm_drm_dev_init(), devm_drm_dev_register_with_fbdev() and drop
>>> using tinydrm_device.
>>>
>>> Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
>>> ---
>>>  drivers/gpu/drm/tinydrm/repaper.c | 63 ++++++++++++++++++++-----------
>>>  1 file changed, 40 insertions(+), 23 deletions(-)
>>>
>>> diff --git a/drivers/gpu/drm/tinydrm/repaper.c b/drivers/gpu/drm/tinydrm/repaper.c
>>> index 3141241ca8f0..4c8205565668 100644
>>> --- a/drivers/gpu/drm/tinydrm/repaper.c
>>> +++ b/drivers/gpu/drm/tinydrm/repaper.c
>>> @@ -34,7 +34,7 @@
>>>  #include <drm/drm_gem_framebuffer_helper.h>
>>>  #include <drm/drm_rect.h>
>>>  #include <drm/drm_vblank.h>
>>> -#include <drm/tinydrm/tinydrm.h>
>>> +#include <drm/drm_simple_kms_helper.h>
>>>  #include <drm/tinydrm/tinydrm-helpers.h>
>>>  
>>>  #define REPAPER_RID_G2_COG_ID	0x12
>>> @@ -60,7 +60,8 @@ enum repaper_epd_border_byte {
>>>  };
>>>  
>>>  struct repaper_epd {
>>> -	struct tinydrm_device tinydrm;
>>> +	struct drm_device base;
>>> +	struct drm_simple_display_pipe pipe;
>>>  	struct spi_device *spi;
>>>  
>>>  	struct gpio_desc *panel_on;
>>> @@ -89,10 +90,9 @@ struct repaper_epd {
>>>  	bool partial;
>>>  };
>>>  
>>> -static inline struct repaper_epd *
>>> -epd_from_tinydrm(struct tinydrm_device *tdev)
>>> +static inline struct repaper_epd *drm_to_epd(struct drm_device *drm)
>>>  {
>>> -	return container_of(tdev, struct repaper_epd, tinydrm);
>>> +	return container_of(drm, struct repaper_epd, base);
>>>  }
>>>  
>>>  static int repaper_spi_transfer(struct spi_device *spi, u8 header,
>>> @@ -530,8 +530,7 @@ static int repaper_fb_dirty(struct drm_framebuffer *fb)
>>>  {
>>>  	struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
>>>  	struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
>>> -	struct tinydrm_device *tdev = fb->dev->dev_private;
>>> -	struct repaper_epd *epd = epd_from_tinydrm(tdev);
>>> +	struct repaper_epd *epd = drm_to_epd(fb->dev);
>>>  	struct drm_rect clip;
>>>  	u8 *buf = NULL;
>>>  	int ret = 0;
>>> @@ -646,8 +645,7 @@ static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
>>>  				struct drm_crtc_state *crtc_state,
>>>  				struct drm_plane_state *plane_state)
>>>  {
>>> -	struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
>>> -	struct repaper_epd *epd = epd_from_tinydrm(tdev);
>>> +	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
>>>  	struct spi_device *spi = epd->spi;
>>>  	struct device *dev = &spi->dev;
>>>  	bool dc_ok = false;
>>> @@ -781,8 +779,7 @@ static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
>>>  
>>>  static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe)
>>>  {
>>> -	struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
>>> -	struct repaper_epd *epd = epd_from_tinydrm(tdev);
>>> +	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
>>>  	struct spi_device *spi = epd->spi;
>>>  	unsigned int line;
>>>  
>>> @@ -856,6 +853,23 @@ static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = {
>>>  	.prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
>>>  };
>>>  
>>> +static const struct drm_mode_config_funcs repaper_mode_config_funcs = {
>>> +	.fb_create = drm_gem_fb_create_with_dirty,
>>> +	.atomic_check = drm_atomic_helper_check,
>>> +	.atomic_commit = drm_atomic_helper_commit,
>>> +};
>>> +
>>> +static void repaper_release(struct drm_device *drm)
>>> +{
>>> +	struct repaper_epd *epd = drm_to_epd(drm);
>>> +
>>> +	DRM_DEBUG_DRIVER("\n");
>>> +
>>> +	drm_mode_config_cleanup(drm);
>>> +	drm_dev_fini(drm);
>>> +	kfree(epd);
>>> +}
>>> +
>>>  static const uint32_t repaper_formats[] = {
>>>  	DRM_FORMAT_XRGB8888,
>>>  };
>>> @@ -894,6 +908,7 @@ static struct drm_driver repaper_driver = {
>>>  	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
>>>  				  DRIVER_ATOMIC,
>>>  	.fops			= &repaper_fops,
>>> +	.release		= repaper_release,
>>>  	DRM_GEM_CMA_VMAP_DRIVER_OPS,
>>>  	.name			= "repaper",
>>>  	.desc			= "Pervasive Displays RePaper e-ink panels",
>>> @@ -927,7 +942,6 @@ static int repaper_probe(struct spi_device *spi)
>>>  	const struct of_device_id *match;
>>>  	struct drm_connector *connector;
>>>  	struct device *dev = &spi->dev;
>>> -	struct tinydrm_device *tdev;
>>>  	enum repaper_model model;
>>>  	const char *thermal_zone;
>>>  	struct repaper_epd *epd;
>>> @@ -952,10 +966,20 @@ static int repaper_probe(struct spi_device *spi)
>>>  		}
>>>  	}
>>>  
>>> -	epd = devm_kzalloc(dev, sizeof(*epd), GFP_KERNEL);
>>> +	epd = kzalloc(sizeof(*epd), GFP_KERNEL);
>>>  	if (!epd)
>>>  		return -ENOMEM;
>>>  
>>> +	drm = &epd->base;
>>> +	ret = devm_drm_dev_init(dev, drm, &repaper_driver);
>>> +	if (ret) {
>>> +		kfree(epd);
>>> +		return ret;
>>> +	}
>> Here you go to the trouble and free epd on error.
>>
>>> +
>>> +	drm_mode_config_init(drm);
>>> +	drm->mode_config.funcs = &repaper_mode_config_funcs;
>>> +
>>>  	epd->spi = spi;
>>>  
>>>  	epd->panel_on = devm_gpiod_get(dev, "panel-on", GPIOD_OUT_LOW);
>>> @@ -1066,32 +1090,25 @@ static int repaper_probe(struct spi_device *spi)
>>>  	if (!epd->current_frame)
>>>  		return -ENOMEM;
>>>  
>>> -	tdev = &epd->tinydrm;
>>> -
>>> -	ret = devm_tinydrm_init(dev, tdev, &repaper_driver);
>>> -	if (ret)
>>> -		return ret;
>>> -
>>> -	drm = tdev->drm;
>>>  
>>>  	connector = drm_simple_connector_create(drm, DRM_MODE_CONNECTOR_VIRTUAL, mode, 0);
>>>  	if (IS_ERR(connector))
>>>  		return PTR_ERR(connector);
>>>  
>>> -	ret = drm_simple_display_pipe_init(drm, &tdev->pipe, &repaper_pipe_funcs,
>>> +	ret = drm_simple_display_pipe_init(drm, &epd->pipe, &repaper_pipe_funcs,
>>>  					   repaper_formats, ARRAY_SIZE(repaper_formats),
>>>  					   NULL, connector);
>>>  	if (ret)
>>>  		return ret;
>> But later in the same function epd is not freed.
>> Looks like a leak?
>> Nothing new if this is correct but just spotted it.
> 
> The part with "nothing new" I take back. This code converts
> devm_kzalloc() to the unmanaged variant.
> So the leak is new - and also present in
> drivers in next patch (if there is a leak).
> 
> Why is the conversion to an unmanaged variant of kzalloc() required?
> 

epd is freed in repaper_release(), but if devm_drm_dev_init() fails it
won't get called so it's freed in that particular error path.

Noralf.
diff mbox series

Patch

diff --git a/drivers/gpu/drm/tinydrm/repaper.c b/drivers/gpu/drm/tinydrm/repaper.c
index 3141241ca8f0..4c8205565668 100644
--- a/drivers/gpu/drm/tinydrm/repaper.c
+++ b/drivers/gpu/drm/tinydrm/repaper.c
@@ -34,7 +34,7 @@ 
 #include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_rect.h>
 #include <drm/drm_vblank.h>
-#include <drm/tinydrm/tinydrm.h>
+#include <drm/drm_simple_kms_helper.h>
 #include <drm/tinydrm/tinydrm-helpers.h>
 
 #define REPAPER_RID_G2_COG_ID	0x12
@@ -60,7 +60,8 @@  enum repaper_epd_border_byte {
 };
 
 struct repaper_epd {
-	struct tinydrm_device tinydrm;
+	struct drm_device base;
+	struct drm_simple_display_pipe pipe;
 	struct spi_device *spi;
 
 	struct gpio_desc *panel_on;
@@ -89,10 +90,9 @@  struct repaper_epd {
 	bool partial;
 };
 
-static inline struct repaper_epd *
-epd_from_tinydrm(struct tinydrm_device *tdev)
+static inline struct repaper_epd *drm_to_epd(struct drm_device *drm)
 {
-	return container_of(tdev, struct repaper_epd, tinydrm);
+	return container_of(drm, struct repaper_epd, base);
 }
 
 static int repaper_spi_transfer(struct spi_device *spi, u8 header,
@@ -530,8 +530,7 @@  static int repaper_fb_dirty(struct drm_framebuffer *fb)
 {
 	struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
 	struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
-	struct tinydrm_device *tdev = fb->dev->dev_private;
-	struct repaper_epd *epd = epd_from_tinydrm(tdev);
+	struct repaper_epd *epd = drm_to_epd(fb->dev);
 	struct drm_rect clip;
 	u8 *buf = NULL;
 	int ret = 0;
@@ -646,8 +645,7 @@  static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
 				struct drm_crtc_state *crtc_state,
 				struct drm_plane_state *plane_state)
 {
-	struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
-	struct repaper_epd *epd = epd_from_tinydrm(tdev);
+	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
 	struct spi_device *spi = epd->spi;
 	struct device *dev = &spi->dev;
 	bool dc_ok = false;
@@ -781,8 +779,7 @@  static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
 
 static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe)
 {
-	struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
-	struct repaper_epd *epd = epd_from_tinydrm(tdev);
+	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
 	struct spi_device *spi = epd->spi;
 	unsigned int line;
 
@@ -856,6 +853,23 @@  static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = {
 	.prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
 };
 
+static const struct drm_mode_config_funcs repaper_mode_config_funcs = {
+	.fb_create = drm_gem_fb_create_with_dirty,
+	.atomic_check = drm_atomic_helper_check,
+	.atomic_commit = drm_atomic_helper_commit,
+};
+
+static void repaper_release(struct drm_device *drm)
+{
+	struct repaper_epd *epd = drm_to_epd(drm);
+
+	DRM_DEBUG_DRIVER("\n");
+
+	drm_mode_config_cleanup(drm);
+	drm_dev_fini(drm);
+	kfree(epd);
+}
+
 static const uint32_t repaper_formats[] = {
 	DRM_FORMAT_XRGB8888,
 };
@@ -894,6 +908,7 @@  static struct drm_driver repaper_driver = {
 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
 				  DRIVER_ATOMIC,
 	.fops			= &repaper_fops,
+	.release		= repaper_release,
 	DRM_GEM_CMA_VMAP_DRIVER_OPS,
 	.name			= "repaper",
 	.desc			= "Pervasive Displays RePaper e-ink panels",
@@ -927,7 +942,6 @@  static int repaper_probe(struct spi_device *spi)
 	const struct of_device_id *match;
 	struct drm_connector *connector;
 	struct device *dev = &spi->dev;
-	struct tinydrm_device *tdev;
 	enum repaper_model model;
 	const char *thermal_zone;
 	struct repaper_epd *epd;
@@ -952,10 +966,20 @@  static int repaper_probe(struct spi_device *spi)
 		}
 	}
 
-	epd = devm_kzalloc(dev, sizeof(*epd), GFP_KERNEL);
+	epd = kzalloc(sizeof(*epd), GFP_KERNEL);
 	if (!epd)
 		return -ENOMEM;
 
+	drm = &epd->base;
+	ret = devm_drm_dev_init(dev, drm, &repaper_driver);
+	if (ret) {
+		kfree(epd);
+		return ret;
+	}
+
+	drm_mode_config_init(drm);
+	drm->mode_config.funcs = &repaper_mode_config_funcs;
+
 	epd->spi = spi;
 
 	epd->panel_on = devm_gpiod_get(dev, "panel-on", GPIOD_OUT_LOW);
@@ -1066,32 +1090,25 @@  static int repaper_probe(struct spi_device *spi)
 	if (!epd->current_frame)
 		return -ENOMEM;
 
-	tdev = &epd->tinydrm;
-
-	ret = devm_tinydrm_init(dev, tdev, &repaper_driver);
-	if (ret)
-		return ret;
-
-	drm = tdev->drm;
 
 	connector = drm_simple_connector_create(drm, DRM_MODE_CONNECTOR_VIRTUAL, mode, 0);
 	if (IS_ERR(connector))
 		return PTR_ERR(connector);
 
-	ret = drm_simple_display_pipe_init(drm, &tdev->pipe, &repaper_pipe_funcs,
+	ret = drm_simple_display_pipe_init(drm, &epd->pipe, &repaper_pipe_funcs,
 					   repaper_formats, ARRAY_SIZE(repaper_formats),
 					   NULL, connector);
 	if (ret)
 		return ret;
 
 	drm_simple_connector_set_mode_config(connector);
-	drm_mode_config_reset(tdev->drm);
+	drm_mode_config_reset(drm);
 
 	spi_set_drvdata(spi, drm);
 
 	DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
 
-	return devm_tinydrm_register(tdev);
+	return devm_drm_dev_register_with_fbdev(drm, 0);
 }
 
 static void repaper_shutdown(struct spi_device *spi)