linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-22  5:02 [PATCH v2 0/6] " Ai Chao
@ 2025-05-22  5:02 ` Ai Chao
  2025-05-22 10:04   ` Johannes Berg
  2025-05-23 10:51   ` Christophe Leroy
  0 siblings, 2 replies; 11+ messages in thread
From: Ai Chao @ 2025-05-22  5:02 UTC (permalink / raw)
  To: perex, tiwai, johannes, kuninori.morimoto.gx, lgirdwood, broonie,
	jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm,
	Ai Chao

The for_each_child_of_node_scoped() helper provides a scope-based
clean-up functionality to put the device_node automatically, and
as such, there is no need to call of_node_put() directly.

Thus, use this helper to simplify the code.

Signed-off-by: Ai Chao <aichao@kylinos.cn>
---
 sound/aoa/soundbus/i2sbus/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/sound/aoa/soundbus/i2sbus/core.c b/sound/aoa/soundbus/i2sbus/core.c
index ce84288168e4..20a4c5891afc 100644
--- a/sound/aoa/soundbus/i2sbus/core.c
+++ b/sound/aoa/soundbus/i2sbus/core.c
@@ -207,6 +207,8 @@ static int i2sbus_add_dev(struct macio_dev *macio,
 			}
 		}
 	}
+	of_node_put(sound);
+
 	/* for the time being, until we can handle non-layout-id
 	 * things in some fabric, refuse to attach if there is no
 	 * layout-id property or we haven't been forced to attach.
@@ -335,7 +337,6 @@ static int i2sbus_add_dev(struct macio_dev *macio,
 
 static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
 {
-	struct device_node *np;
 	int got = 0, err;
 	struct i2sbus_control *control = NULL;
 
@@ -347,7 +348,7 @@ static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
 		return -ENODEV;
 	}
 
-	for_each_child_of_node(dev->ofdev.dev.of_node, np) {
+	for_each_child_of_node_scoped(dev->ofdev.dev.of_node, np) {
 		if (of_device_is_compatible(np, "i2sbus") ||
 		    of_device_is_compatible(np, "i2s-modem")) {
 			got += i2sbus_add_dev(dev, control, np);
-- 
2.47.1


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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-22  5:02 ` [PATCH v2 2/6] ASoC: aoa: " Ai Chao
@ 2025-05-22 10:04   ` Johannes Berg
  2025-05-23 10:51   ` Christophe Leroy
  1 sibling, 0 replies; 11+ messages in thread
From: Johannes Berg @ 2025-05-22 10:04 UTC (permalink / raw)
  To: Ai Chao, perex, tiwai, kuninori.morimoto.gx, lgirdwood, broonie,
	jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm

On Thu, 2025-05-22 at 13:02 +0800, Ai Chao wrote:
> The for_each_child_of_node_scoped() helper provides a scope-based
> clean-up functionality to put the device_node automatically, and
> as such, there is no need to call of_node_put() directly.
> 
> Thus, use this helper to simplify the code.

> -	for_each_child_of_node(dev->ofdev.dev.of_node, np) {
> +	for_each_child_of_node_scoped(dev->ofdev.dev.of_node, np) {
>  		if (of_device_is_compatible(np, "i2sbus") ||
>  		    of_device_is_compatible(np, "i2s-modem")) {
>  			got += i2sbus_add_dev(dev, control, np);

Given the structure of this code, this either fixes a leak, or is wrong.
I really don't know which of those two it is, it's been decades, but
either way it's not a change that simply "simplif[ies] the code".

johannes

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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
       [not found] <2aq0nyvyf7t-2aq4hsc7kp6@nsmail7.0.0--kylin--1>
@ 2025-05-23 10:40 ` Johannes Berg
  2025-05-26  8:12   ` Ai Chao
  0 siblings, 1 reply; 11+ messages in thread
From: Johannes Berg @ 2025-05-23 10:40 UTC (permalink / raw)
  To: 艾超, perex, tiwai, kuninori.morimoto.gx, lgirdwood,
	broonie, jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm

Hi,

[you should avoid HTML]

> "simplifies the code" is no need to callof_node_put() .

Fair. Except that's not what you _actually_ changed here. Like I said,
either it's buggy before or after.

johannes

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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-22  5:02 ` [PATCH v2 2/6] ASoC: aoa: " Ai Chao
  2025-05-22 10:04   ` Johannes Berg
@ 2025-05-23 10:51   ` Christophe Leroy
  2025-05-26  8:53     ` Ai Chao
  1 sibling, 1 reply; 11+ messages in thread
From: Christophe Leroy @ 2025-05-23 10:51 UTC (permalink / raw)
  To: Ai Chao, perex, tiwai, johannes, kuninori.morimoto.gx, lgirdwood,
	broonie, jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm



Le 22/05/2025 à 07:02, Ai Chao a écrit :
> The for_each_child_of_node_scoped() helper provides a scope-based
> clean-up functionality to put the device_node automatically, and
> as such, there is no need to call of_node_put() directly.

I don't understand this explanation.

You say "no need to call of_node_put()" and the only thing you do in 
addition to changing from for_each_child_of_node() to 
for_each_child_of_node_scoped() is to _add_ a new call to of_node_put().

I would expect to see a _removal_ of some of_node_put() when I read your 
description.

Christophe

> 
> Thus, use this helper to simplify the code.
> 
> Signed-off-by: Ai Chao <aichao@kylinos.cn>
> ---
>   sound/aoa/soundbus/i2sbus/core.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/sound/aoa/soundbus/i2sbus/core.c b/sound/aoa/soundbus/i2sbus/core.c
> index ce84288168e4..20a4c5891afc 100644
> --- a/sound/aoa/soundbus/i2sbus/core.c
> +++ b/sound/aoa/soundbus/i2sbus/core.c
> @@ -207,6 +207,8 @@ static int i2sbus_add_dev(struct macio_dev *macio,
>   			}
>   		}
>   	}
> +	of_node_put(sound);
> +
>   	/* for the time being, until we can handle non-layout-id
>   	 * things in some fabric, refuse to attach if there is no
>   	 * layout-id property or we haven't been forced to attach.
> @@ -335,7 +337,6 @@ static int i2sbus_add_dev(struct macio_dev *macio,
>   
>   static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
>   {
> -	struct device_node *np;
>   	int got = 0, err;
>   	struct i2sbus_control *control = NULL;
>   
> @@ -347,7 +348,7 @@ static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
>   		return -ENODEV;
>   	}
>   
> -	for_each_child_of_node(dev->ofdev.dev.of_node, np) {
> +	for_each_child_of_node_scoped(dev->ofdev.dev.of_node, np) {
>   		if (of_device_is_compatible(np, "i2sbus") ||
>   		    of_device_is_compatible(np, "i2s-modem")) {
>   			got += i2sbus_add_dev(dev, control, np);


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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-23 10:40 ` [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped() Johannes Berg
@ 2025-05-26  8:12   ` Ai Chao
  2025-05-26  8:13     ` Johannes Berg
  0 siblings, 1 reply; 11+ messages in thread
From: Ai Chao @ 2025-05-26  8:12 UTC (permalink / raw)
  To: Johannes Berg, perex, tiwai, kuninori.morimoto.gx, lgirdwood,
	broonie, jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm

Hi Johannes:

>> "simplifies the code" is no need to callof_node_put() .
> Fair. Except that's not what you _actually_ changed here. Like I said,
> either it's buggy before or after.
>
In the function i2sbus_probe, it not return a struct device_node, so , I 
think function for_each_child_of_node_scoped is better than 
for_each_child_of_node.

Best regards,
Ai Chao

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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-26  8:12   ` Ai Chao
@ 2025-05-26  8:13     ` Johannes Berg
  2025-05-26  8:20       ` Ai Chao
  0 siblings, 1 reply; 11+ messages in thread
From: Johannes Berg @ 2025-05-26  8:13 UTC (permalink / raw)
  To: Ai Chao, perex, tiwai, kuninori.morimoto.gx, lgirdwood, broonie,
	jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm

On Mon, 2025-05-26 at 16:12 +0800, Ai Chao wrote:
> Hi Johannes:
> 
> > > "simplifies the code" is no need to callof_node_put() .
> > Fair. Except that's not what you _actually_ changed here. Like I said,
> > either it's buggy before or after.
> > 
> In the function i2sbus_probe, it not return a struct device_node, so , I 
> think function for_each_child_of_node_scoped is better than 
> for_each_child_of_node.

You still haven't explained why it's even correct.

johannes

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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-26  8:13     ` Johannes Berg
@ 2025-05-26  8:20       ` Ai Chao
  2025-05-26  8:21         ` Johannes Berg
  2025-05-28 19:23         ` Dmitry Baryshkov
  0 siblings, 2 replies; 11+ messages in thread
From: Ai Chao @ 2025-05-26  8:20 UTC (permalink / raw)
  To: Johannes Berg, perex, tiwai, kuninori.morimoto.gx, lgirdwood,
	broonie, jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm


Hi Johannes:
>> Hi Johannes:
>>
>>>> "simplifies the code" is no need to callof_node_put() .
>>> Fair. Except that's not what you _actually_ changed here. Like I said,
>>> either it's buggy before or after.
>>>
>> In the function i2sbus_probe, it not return a struct device_node, so , I
>> think function for_each_child_of_node_scoped is better than
>> for_each_child_of_node.
> You still haven't explained why it's even correct.
>
> johannes

The for_each_child_of_node() function is used to iterate over all child 
nodes of a device tree node.
During each iteration, it retrieves a pointer to the child node via 
of_get_next_child() and automatically increments the node's reference 
count (of_node_get()).
Each call to of_get_next_child() increases the reference count 
(refcount) of the returned child node, ensuring that the node is not 
freed while in use.
for_each_child_of_node() increments the child node's reference count in 
each iteration but does not decrement it automatically.
If of_node_put() is not called manually, the reference count will never 
reach zero, resulting in a memory leak of the node.

Best regards,
Ai Chao

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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-26  8:20       ` Ai Chao
@ 2025-05-26  8:21         ` Johannes Berg
  2025-05-30  9:30           ` Ai Chao
  2025-05-28 19:23         ` Dmitry Baryshkov
  1 sibling, 1 reply; 11+ messages in thread
From: Johannes Berg @ 2025-05-26  8:21 UTC (permalink / raw)
  To: Ai Chao, perex, tiwai, kuninori.morimoto.gx, lgirdwood, broonie,
	jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm

On Mon, 2025-05-26 at 16:20 +0800, Ai Chao wrote:
> Hi Johannes:
> > > Hi Johannes:
> > > 
> > > > > "simplifies the code" is no need to callof_node_put() .
> > > > Fair. Except that's not what you _actually_ changed here. Like I said,
> > > > either it's buggy before or after.
> > > > 
> > > In the function i2sbus_probe, it not return a struct device_node, so , I
> > > think function for_each_child_of_node_scoped is better than
> > > for_each_child_of_node.
> > You still haven't explained why it's even correct.
> > 
> > johannes
> 
> The for_each_child_of_node() function is used to iterate over all child 
> nodes of a device tree node.
> During each iteration, it retrieves a pointer to the child node via 
> of_get_next_child() and automatically increments the node's reference 
> count (of_node_get()).
> Each call to of_get_next_child() increases the reference count 
> (refcount) of the returned child node, ensuring that the node is not 
> freed while in use.
> for_each_child_of_node() increments the child node's reference count in 
> each iteration but does not decrement it automatically.
> If of_node_put() is not called manually, the reference count will never 
> reach zero, resulting in a memory leak of the node.

Yes, good! Now show that you can apply what you've learned to the
specific code (and changes) at hand.

johannes

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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-23 10:51   ` Christophe Leroy
@ 2025-05-26  8:53     ` Ai Chao
  0 siblings, 0 replies; 11+ messages in thread
From: Ai Chao @ 2025-05-26  8:53 UTC (permalink / raw)
  To: Christophe Leroy, perex, tiwai, johannes, kuninori.morimoto.gx,
	lgirdwood, broonie, jbrunet, neil.armstrong, khilman,
	martin.blumenstingl, shengjiu.wang, Xiubo.Lee, festevam,
	nicoleotsuka, shawnguo, s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm

Hi Christophe:

>> The for_each_child_of_node_scoped() helper provides a scope-based
>> clean-up functionality to put the device_node automatically, and
>> as such, there is no need to call of_node_put() directly.
>
> I don't understand this explanation.
>
> You say "no need to call of_node_put()" and the only thing you do in 
> addition to changing from for_each_child_of_node() to 
> for_each_child_of_node_scoped() is to _add_ a new call to of_node_put().
>
> I would expect to see a _removal_ of some of_node_put() when I read 
> your description.
>
>>       }
>> +    of_node_put(sound);
>> +

The for_each_child_of_node() function is used to iterate over all child 
nodes of a device tree node.  During each iteration, it retrieves a 
pointer to the child node via of_get_next_child() and automatically 
increments the node's reference count (of_node_get()). Each call to 
of_get_next_child() increases the reference count (refcount) of the 
returned child node, ensuring that the node is not freed while in use.
for_each_child_of_node() increments the child node's reference count in 
each iteration but does not decrement it automatically.
If of_node_put() is not called manually, the reference count will never 
reach zero, resulting in a memory leak of the node.

In function i2sbus_add_dev, it used device_node out of 
for_each_child_of_node(){},  it need to add a new call to 
of_node_put(sound) to reference count.

In function i2cbus_probe, it used device_node in 
for_each_child_of_node(){}, used for_each_child_of_node_scoped() is 
better than for_each_child_of_node().

Best regards,
Ai Chao

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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-26  8:20       ` Ai Chao
  2025-05-26  8:21         ` Johannes Berg
@ 2025-05-28 19:23         ` Dmitry Baryshkov
  1 sibling, 0 replies; 11+ messages in thread
From: Dmitry Baryshkov @ 2025-05-28 19:23 UTC (permalink / raw)
  To: Ai Chao
  Cc: Johannes Berg, perex, tiwai, kuninori.morimoto.gx, lgirdwood,
	broonie, jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla, linux-sound, linux-kernel,
	linuxppc-dev, linux-renesas-soc, linux-arm-kernel, linux-amlogic,
	imx, kernel, linux-arm-msm

On Mon, May 26, 2025 at 04:20:37PM +0800, Ai Chao wrote:
> 
> Hi Johannes:
> > > Hi Johannes:
> > > 
> > > > > "simplifies the code" is no need to callof_node_put() .
> > > > Fair. Except that's not what you _actually_ changed here. Like I said,
> > > > either it's buggy before or after.
> > > > 
> > > In the function i2sbus_probe, it not return a struct device_node, so , I
> > > think function for_each_child_of_node_scoped is better than
> > > for_each_child_of_node.
> > You still haven't explained why it's even correct.
> > 
> > johannes
> 
> The for_each_child_of_node() function is used to iterate over all child
> nodes of a device tree node.
> During each iteration, it retrieves a pointer to the child node via
> of_get_next_child() and automatically increments the node's reference count
> (of_node_get()).

This is not complete story, you missed the second part of it.

> Each call to of_get_next_child() increases the reference count (refcount) of
> the returned child node, ensuring that the node is not freed while in use.
> for_each_child_of_node() increments the child node's reference count in each
> iteration but does not decrement it automatically.
> If of_node_put() is not called manually, the reference count will never
> reach zero, resulting in a memory leak of the node.
> 
> Best regards,
> Ai Chao

-- 
With best wishes
Dmitry

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

* Re: [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped()
  2025-05-26  8:21         ` Johannes Berg
@ 2025-05-30  9:30           ` Ai Chao
  0 siblings, 0 replies; 11+ messages in thread
From: Ai Chao @ 2025-05-30  9:30 UTC (permalink / raw)
  To: Johannes Berg, perex, tiwai, kuninori.morimoto.gx, lgirdwood,
	broonie, jbrunet, neil.armstrong, khilman, martin.blumenstingl,
	shengjiu.wang, Xiubo.Lee, festevam, nicoleotsuka, shawnguo,
	s.hauer, srinivas.kandagatla
  Cc: linux-sound, linux-kernel, linuxppc-dev, linux-renesas-soc,
	linux-arm-kernel, linux-amlogic, imx, kernel, linux-arm-msm

Hi Johannes:
     Thanks for your feedback.  I will drop it.

> On Mon, 2025-05-26 at 16:20 +0800, Ai Chao wrote:
>> Hi Johannes:
>>>> for_each_child_of_node.
>>> You still haven't explained why it's even correct.
>>>
>>> johannes
>> The for_each_child_of_node() function is used to iterate over all child
>> nodes of a device tree node.
>> During each iteration, it retrieves a pointer to the child node via
>> of_get_next_child() and automatically increments the node's reference
>> count (of_node_get()).
>> Each call to of_get_next_child() increases the reference count
>> (refcount) of the returned child node, ensuring that the node is not
>> freed while in use.
>> for_each_child_of_node() increments the child node's reference count in
>> each iteration but does not decrement it automatically.
>> If of_node_put() is not called manually, the reference count will never
>> reach zero, resulting in a memory leak of the node.
> Yes, good! Now show that you can apply what you've learned to the
> specific code (and changes) at hand.
>
> johannes

-- 
Best regards,
Ai Chao


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

end of thread, other threads:[~2025-05-30  9:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <2aq0nyvyf7t-2aq4hsc7kp6@nsmail7.0.0--kylin--1>
2025-05-23 10:40 ` [PATCH v2 2/6] ASoC: aoa: Use helper function for_each_child_of_node_scoped() Johannes Berg
2025-05-26  8:12   ` Ai Chao
2025-05-26  8:13     ` Johannes Berg
2025-05-26  8:20       ` Ai Chao
2025-05-26  8:21         ` Johannes Berg
2025-05-30  9:30           ` Ai Chao
2025-05-28 19:23         ` Dmitry Baryshkov
2025-05-22  5:02 [PATCH v2 0/6] " Ai Chao
2025-05-22  5:02 ` [PATCH v2 2/6] ASoC: aoa: " Ai Chao
2025-05-22 10:04   ` Johannes Berg
2025-05-23 10:51   ` Christophe Leroy
2025-05-26  8:53     ` Ai Chao

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).