devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] of: property: fix unbalanced of node refcount in of_fwnode_get_reference_args()
@ 2022-11-17 14:56 Yang Yingliang
  2022-11-18  7:48 ` Sakari Ailus
  2022-11-21  1:20 ` Frank Rowand
  0 siblings, 2 replies; 3+ messages in thread
From: Yang Yingliang @ 2022-11-17 14:56 UTC (permalink / raw)
  To: devicetree
  Cc: sakari.ailus, rafael.j.wysocki, robh+dt, frowand.list,
	yangyingliang

I got the the following report:

  OF: ERROR: memory leak, expected refcount 1 instead of 4,
  of_node_get()/of_node_put() unbalanced - destroy cset entry:
  attach overlay node /i2c/pmic@34/adc

In of_fwnode_get_reference_args(), the 'of_args.np' returned
from of_parse_phandle_with_args(), its refcount is increased
in of_phandle_iterator_next(), if args is null, the refcount
of 'of_args.np' need be put to avoid leak.

Fixes: 3e3119d3088f ("device property: Introduce fwnode_property_get_reference_args")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/of/property.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/of/property.c b/drivers/of/property.c
index 967f79b59016..134cfc980b70 100644
--- a/drivers/of/property.c
+++ b/drivers/of/property.c
@@ -993,8 +993,10 @@ of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
 						       nargs, index, &of_args);
 	if (ret < 0)
 		return ret;
-	if (!args)
+	if (!args) {
+		of_node_put(of_args.np);
 		return 0;
+	}
 
 	args->nargs = of_args.args_count;
 	args->fwnode = of_fwnode_handle(of_args.np);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] of: property: fix unbalanced of node refcount in of_fwnode_get_reference_args()
  2022-11-17 14:56 [PATCH] of: property: fix unbalanced of node refcount in of_fwnode_get_reference_args() Yang Yingliang
@ 2022-11-18  7:48 ` Sakari Ailus
  2022-11-21  1:20 ` Frank Rowand
  1 sibling, 0 replies; 3+ messages in thread
From: Sakari Ailus @ 2022-11-18  7:48 UTC (permalink / raw)
  To: Yang Yingliang; +Cc: devicetree, rafael.j.wysocki, robh+dt, frowand.list

Hi Yang,

On Thu, Nov 17, 2022 at 10:56:56PM +0800, Yang Yingliang wrote:
> I got the the following report:
> 
>   OF: ERROR: memory leak, expected refcount 1 instead of 4,
>   of_node_get()/of_node_put() unbalanced - destroy cset entry:
>   attach overlay node /i2c/pmic@34/adc
> 
> In of_fwnode_get_reference_args(), the 'of_args.np' returned
> from of_parse_phandle_with_args(), its refcount is increased
> in of_phandle_iterator_next(), if args is null, the refcount
> of 'of_args.np' need be put to avoid leak.
> 
> Fixes: 3e3119d3088f ("device property: Introduce fwnode_property_get_reference_args")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>

Thanks for the patch!

Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>

-- 
Sakari Ailus

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] of: property: fix unbalanced of node refcount in of_fwnode_get_reference_args()
  2022-11-17 14:56 [PATCH] of: property: fix unbalanced of node refcount in of_fwnode_get_reference_args() Yang Yingliang
  2022-11-18  7:48 ` Sakari Ailus
@ 2022-11-21  1:20 ` Frank Rowand
  1 sibling, 0 replies; 3+ messages in thread
From: Frank Rowand @ 2022-11-21  1:20 UTC (permalink / raw)
  To: Yang Yingliang, devicetree; +Cc: sakari.ailus, rafael.j.wysocki, robh+dt

On 11/17/22 08:56, Yang Yingliang wrote:

Change the subject from:

of: property: fix unbalanced of node refcount in of_fwnode_get_reference_args()

to:

of: property: decrement node refcount in of_fwnode_get_reference_args()


Change patch comment:

> I got the the following report:
> 
>   OF: ERROR: memory leak, expected refcount 1 instead of 4,
>   of_node_get()/of_node_put() unbalanced - destroy cset entry:
>   attach overlay node /i2c/pmic@34/adc
> 
> In of_fwnode_get_reference_args(), the 'of_args.np' returned
> from of_parse_phandle_with_args(), its refcount is increased
> in of_phandle_iterator_next(), if args is null, the refcount
> of 'of_args.np' need be put to avoid leak.

to:

In of_fwnode_get_reference_args(), the refcount of of_args.np has been
incremented in the case of successful return from
of_parse_phandle_with_args() or of_parse_phandle_with_fixed_args().

Decrement the refcount if of_args is not returned to the caller of
of_fwnode_get_reference_args().


> 
> Fixes: 3e3119d3088f ("device property: Introduce fwnode_property_get_reference_args")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> ---
>  drivers/of/property.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/of/property.c b/drivers/of/property.c
> index 967f79b59016..134cfc980b70 100644
> --- a/drivers/of/property.c
> +++ b/drivers/of/property.c
> @@ -993,8 +993,10 @@ of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
>  						       nargs, index, &of_args);
>  	if (ret < 0)
>  		return ret;
> -	if (!args)
> +	if (!args) {
> +		of_node_put(of_args.np);
>  		return 0;
> +	}
>  
>  	args->nargs = of_args.args_count;
>  	args->fwnode = of_fwnode_handle(of_args.np);

Reviewed by visual inspection.  Did not do my normal build, test, check for
warnings.

Reviewed-by: Frank Rowand <frowand.list@gmail.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-21  1:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-17 14:56 [PATCH] of: property: fix unbalanced of node refcount in of_fwnode_get_reference_args() Yang Yingliang
2022-11-18  7:48 ` Sakari Ailus
2022-11-21  1:20 ` Frank Rowand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).