Patchwork [v2,3/7] dt/powerpc/powernv: Use of_get_child_by_name to get a named child.

login
register
mail settings
Submitter Srinivas KANDAGATLA
Date Sept. 14, 2012, 8:19 a.m.
Message ID <1347610743-20189-1-git-send-email-srinivas.kandagatla@st.com>
Download mbox | patch
Permalink /patch/183882/
State Superseded
Headers show

Comments

Srinivas KANDAGATLA - Sept. 14, 2012, 8:19 a.m.
From: Srinivas Kandagatla <srinivas.kandagatla@st.com>

As follow-up to "dt: introduce of_get_child_by_name to get child node by
name." patch, This patch removes some of the code duplication in the
driver by replacing it with of_get_child_by_name instead.

Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@st.com>
---
 arch/powerpc/platforms/powernv/opal.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
Michael Ellerman - Sept. 17, 2012, 2:07 a.m.
On Fri, 2012-09-14 at 09:19 +0100, Srinivas KANDAGATLA wrote:
> From: Srinivas Kandagatla <srinivas.kandagatla@st.com>
> 
> As follow-up to "dt: introduce of_get_child_by_name to get child node by
> name." patch, This patch removes some of the code duplication in the
> driver by replacing it with of_get_child_by_name instead.
> 
> Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@st.com>
> ---
>  arch/powerpc/platforms/powernv/opal.c |    6 ++----
>  1 files changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/powerpc/platforms/powernv/opal.c b/arch/powerpc/platforms/powernv/opal.c
> index aaa0dba..6dfb8af 100644
> --- a/arch/powerpc/platforms/powernv/opal.c
> +++ b/arch/powerpc/platforms/powernv/opal.c
> @@ -294,11 +294,9 @@ static int __init opal_init(void)
>  		consoles = of_node_get(opal_node);
>  
>  	/* Register serial ports */
> -	for_each_child_of_node(consoles, np) {
> -		if (strcmp(np->name, "serial"))
> -			continue;
> +	np = of_get_child_by_name(consoles, "serial");
> +	if (np)
>  		of_platform_device_create(np, NULL, NULL);
> -	}
>  	of_node_put(consoles);

You've introduced a refcounting bug here.

The return value of of_get_child_by_name() has its refcount elevated
(via of_node_get()), so you have to drop that reference with
of_node_put().

The old code was safe because it only used np inside the loop, and the
loop logic deals with dropping the refcount for you.

And yes of_platform_device_create() takes a reference for itself.

cheers
Srinivas KANDAGATLA - Sept. 17, 2012, 7:31 a.m.
On 17/09/12 03:07, Michael Ellerman wrote:
> On Fri, 2012-09-14 at 09:19 +0100, Srinivas KANDAGATLA wrote:
>> From: Srinivas Kandagatla <srinivas.kandagatla@st.com>
>>
>> As follow-up to "dt: introduce of_get_child_by_name to get child node by
>> name." patch, This patch removes some of the code duplication in the
>> driver by replacing it with of_get_child_by_name instead.
>>
>> Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@st.com>
>> ---
>>  arch/powerpc/platforms/powernv/opal.c |    6 ++----
>>  1 files changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/powerpc/platforms/powernv/opal.c b/arch/powerpc/platforms/powernv/opal.c
>> index aaa0dba..6dfb8af 100644
>> --- a/arch/powerpc/platforms/powernv/opal.c
>> +++ b/arch/powerpc/platforms/powernv/opal.c
>> @@ -294,11 +294,9 @@ static int __init opal_init(void)
>>  		consoles = of_node_get(opal_node);
>>  
>>  	/* Register serial ports */
>> -	for_each_child_of_node(consoles, np) {
>> -		if (strcmp(np->name, "serial"))
>> -			continue;
>> +	np = of_get_child_by_name(consoles, "serial");
>> +	if (np)
>>  		of_platform_device_create(np, NULL, NULL);
>> -	}
>>  	of_node_put(consoles);
> You've introduced a refcounting bug here.
>
> The return value of of_get_child_by_name() has its refcount elevated
> (via of_node_get()), so you have to drop that reference with
> of_node_put().
I agree..
Will take care of it in v3 patch-series.
> The old code was safe because it only used np inside the loop, and the
> loop logic deals with dropping the refcount for you.
>
> And yes of_platform_device_create() takes a reference for itself.
>
> cheers
>
>
>

Patch

diff --git a/arch/powerpc/platforms/powernv/opal.c b/arch/powerpc/platforms/powernv/opal.c
index aaa0dba..6dfb8af 100644
--- a/arch/powerpc/platforms/powernv/opal.c
+++ b/arch/powerpc/platforms/powernv/opal.c
@@ -294,11 +294,9 @@  static int __init opal_init(void)
 		consoles = of_node_get(opal_node);
 
 	/* Register serial ports */
-	for_each_child_of_node(consoles, np) {
-		if (strcmp(np->name, "serial"))
-			continue;
+	np = of_get_child_by_name(consoles, "serial");
+	if (np)
 		of_platform_device_create(np, NULL, NULL);
-	}
 	of_node_put(consoles);
 
 	/* Find all OPAL interrupts and request them */