All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Vetter <daniel@ffwll.ch>
To: Julia Lawall <Julia.Lawall@inria.fr>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	kernel-janitors@vger.kernel.org,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	David Airlie <airlied@linux.ie>, Daniel Vetter <daniel@ffwll.ch>,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] drm/of: free the right object
Date: Mon, 12 Jul 2021 16:25:41 +0200	[thread overview]
Message-ID: <YOxQ5TbkNrqCGBDJ@phenom.ffwll.local> (raw)
In-Reply-To: <20210709200717.3676376-1-Julia.Lawall@inria.fr>

On Fri, Jul 09, 2021 at 10:07:17PM +0200, Julia Lawall wrote:
> There is no need to free a NULL value.  Instead, free the object
> that is leaking due to the iterator.
> 
> The semantic patch that finds this problem is as follows:
> 
> // <smpl>
> @@
> expression x,e;
> identifier f;
> @@
>  x = f(...);
>  if (x == NULL) {
> 	... when any
> 	    when != x = e
> *	of_node_put(x);
> 	...
>  }
> // </smpl>
> 
> Fixes: 6529007522de ("drm: of: Add drm_of_lvds_get_dual_link_pixel_order")
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

Applied to drm-misc-next, thanks for your patch.

Random rant about this for_each_child_of_node():
- not documented with kerneldoc
- very dangerous since the seemingly correct usage leaks

What we've done here for similar refcounted iterators is a 3 step process:
- iter_init()
- for_each_iter() macro
- iter_fini(), which has to be always called after iter_init and cleans up
  the last reference.

Then it's a lot more obvious that you call iter_fini() when you break out
of a loop. Trying to hide that in the for_each macro is a bit much.

Cheers, Daniel

> 
> ---
>  drivers/gpu/drm/drm_of.c |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/drm_of.c b/drivers/gpu/drm/drm_of.c
> index ca04c34e8251..197c57477344 100644
> --- a/drivers/gpu/drm/drm_of.c
> +++ b/drivers/gpu/drm/drm_of.c
> @@ -315,7 +315,7 @@ static int drm_of_lvds_get_remote_pixels_type(
>  
>  		remote_port = of_graph_get_remote_port(endpoint);
>  		if (!remote_port) {
> -			of_node_put(remote_port);
> +			of_node_put(endpoint);
>  			return -EPIPE;
>  		}
>  
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

WARNING: multiple messages have this Message-ID
From: Daniel Vetter <daniel@ffwll.ch>
To: Julia Lawall <Julia.Lawall@inria.fr>
Cc: Thomas Zimmermann <tzimmermann@suse.de>,
	David Airlie <airlied@linux.ie>,
	kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org,
	dri-devel@lists.freedesktop.org
Subject: Re: [PATCH] drm/of: free the right object
Date: Mon, 12 Jul 2021 16:25:41 +0200	[thread overview]
Message-ID: <YOxQ5TbkNrqCGBDJ@phenom.ffwll.local> (raw)
In-Reply-To: <20210709200717.3676376-1-Julia.Lawall@inria.fr>

On Fri, Jul 09, 2021 at 10:07:17PM +0200, Julia Lawall wrote:
> There is no need to free a NULL value.  Instead, free the object
> that is leaking due to the iterator.
> 
> The semantic patch that finds this problem is as follows:
> 
> // <smpl>
> @@
> expression x,e;
> identifier f;
> @@
>  x = f(...);
>  if (x == NULL) {
> 	... when any
> 	    when != x = e
> *	of_node_put(x);
> 	...
>  }
> // </smpl>
> 
> Fixes: 6529007522de ("drm: of: Add drm_of_lvds_get_dual_link_pixel_order")
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

Applied to drm-misc-next, thanks for your patch.

Random rant about this for_each_child_of_node():
- not documented with kerneldoc
- very dangerous since the seemingly correct usage leaks

What we've done here for similar refcounted iterators is a 3 step process:
- iter_init()
- for_each_iter() macro
- iter_fini(), which has to be always called after iter_init and cleans up
  the last reference.

Then it's a lot more obvious that you call iter_fini() when you break out
of a loop. Trying to hide that in the for_each macro is a bit much.

Cheers, Daniel

> 
> ---
>  drivers/gpu/drm/drm_of.c |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/drm_of.c b/drivers/gpu/drm/drm_of.c
> index ca04c34e8251..197c57477344 100644
> --- a/drivers/gpu/drm/drm_of.c
> +++ b/drivers/gpu/drm/drm_of.c
> @@ -315,7 +315,7 @@ static int drm_of_lvds_get_remote_pixels_type(
>  
>  		remote_port = of_graph_get_remote_port(endpoint);
>  		if (!remote_port) {
> -			of_node_put(remote_port);
> +			of_node_put(endpoint);
>  			return -EPIPE;
>  		}
>  
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

  reply	other threads:[~2021-07-12 14:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-09 20:07 Julia Lawall
2021-07-09 20:07 ` Julia Lawall
2021-07-12 14:25 ` Daniel Vetter [this message]
2021-07-12 14:25   ` Daniel Vetter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YOxQ5TbkNrqCGBDJ@phenom.ffwll.local \
    --to=daniel@ffwll.ch \
    --cc=Julia.Lawall@inria.fr \
    --cc=airlied@linux.ie \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=tzimmermann@suse.de \
    --subject='Re: [PATCH] drm/of: free the right object' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.