devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Usage of for_each_child_of_node()
@ 2013-10-12 20:54 Guenter Roeck
       [not found] ` <5259B6F8.3070701-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: Guenter Roeck @ 2013-10-12 20:54 UTC (permalink / raw)
  To: devicetree-u79uwXL29TY76Z2rM5mHXA
  Cc: Grant Likely, Rob Herring, linux-kernel-u79uwXL29TY76Z2rM5mHXA

Hi all,

for_each_child_of_node() and similar functions increase the refcount
on each returned node and expect the caller to release the node by
calling of_node_put() when done.

Looking through the kernel code, it appears this is hardly ever done,
if at all. Some code even calls of_node_get() on returned nodes again.

I guess this doesn't matter in cases where devicetree is a static entity.
However, this is not (or no longer) the case with devicetree overlays,
or more generically in cases where devicetree nodes are added and
removed dynamically.

Fundamental question: Would patches to fix this problem be accepted upstream ?

Or, of course, stepping a bit back: Am I missing something essential ?

Thanks,
Guenter
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: Usage of for_each_child_of_node()
       [not found] ` <5259B6F8.3070701-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
@ 2013-10-13  3:15   ` Rob Herring
  2013-10-23  7:10     ` Thierry Reding
  0 siblings, 1 reply; 7+ messages in thread
From: Rob Herring @ 2013-10-13  3:15 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: devicetree-u79uwXL29TY76Z2rM5mHXA, Grant Likely, Rob Herring,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA

On Sat, Oct 12, 2013 at 3:54 PM, Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org> wrote:
> Hi all,
>
> for_each_child_of_node() and similar functions increase the refcount
> on each returned node and expect the caller to release the node by
> calling of_node_put() when done.
>
> Looking through the kernel code, it appears this is hardly ever done,
> if at all. Some code even calls of_node_get() on returned nodes again.
>
> I guess this doesn't matter in cases where devicetree is a static entity.
> However, this is not (or no longer) the case with devicetree overlays,
> or more generically in cases where devicetree nodes are added and
> removed dynamically.
>
> Fundamental question: Would patches to fix this problem be accepted upstream
> ?

Certainly.

> Or, of course, stepping a bit back: Am I missing something essential ?

No. I think this is frequently wrong since it typically doesn't matter
for static entries as you mention.

Rob
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: Usage of for_each_child_of_node()
  2013-10-13  3:15   ` Rob Herring
@ 2013-10-23  7:10     ` Thierry Reding
       [not found]       ` <20131023071006.GA7708-AwZRO8vwLAwmlAP/+Wk3EA@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: Thierry Reding @ 2013-10-23  7:10 UTC (permalink / raw)
  To: Rob Herring
  Cc: Guenter Roeck, devicetree, Grant Likely, Rob Herring, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1765 bytes --]

On Sat, Oct 12, 2013 at 10:15:03PM -0500, Rob Herring wrote:
> On Sat, Oct 12, 2013 at 3:54 PM, Guenter Roeck <linux@roeck-us.net> wrote:
> > Hi all,
> >
> > for_each_child_of_node() and similar functions increase the refcount
> > on each returned node and expect the caller to release the node by
> > calling of_node_put() when done.
> >
> > Looking through the kernel code, it appears this is hardly ever done,
> > if at all. Some code even calls of_node_get() on returned nodes again.
> >
> > I guess this doesn't matter in cases where devicetree is a static entity.
> > However, this is not (or no longer) the case with devicetree overlays,
> > or more generically in cases where devicetree nodes are added and
> > removed dynamically.
> >
> > Fundamental question: Would patches to fix this problem be accepted upstream
> > ?
> 
> Certainly.
> 
> > Or, of course, stepping a bit back: Am I missing something essential ?
> 
> No. I think this is frequently wrong since it typically doesn't matter
> for static entries as you mention.

Actually, I think it actually happens to be correct most of the time.
The reason is that for_each_child_of_node() internally calls the
of_get_next_child() to iterate over all children. And that function
already calls of_node_put() on the "previous" node. So if all the code
does is to iterate over all nodes to query them, then all should be
fine.

The only case where you actually need to drop the reference on a node is
if you break out of the loop (so that of_get_next_child() will not be
called). But that's usually the case when you need to perform some
operation on the node, in which case it is the right thing to hold on to
a reference until you're done with the node.

Thierry

[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]

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

* Re: Usage of for_each_child_of_node()
       [not found]       ` <20131023071006.GA7708-AwZRO8vwLAwmlAP/+Wk3EA@public.gmane.org>
@ 2013-10-23 16:16         ` Guenter Roeck
       [not found]           ` <20131023161644.GB20675-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: Guenter Roeck @ 2013-10-23 16:16 UTC (permalink / raw)
  To: Thierry Reding
  Cc: Rob Herring, devicetree-u79uwXL29TY76Z2rM5mHXA, Grant Likely,
	Rob Herring, linux-kernel-u79uwXL29TY76Z2rM5mHXA

On Wed, Oct 23, 2013 at 09:10:07AM +0200, Thierry Reding wrote:
> On Sat, Oct 12, 2013 at 10:15:03PM -0500, Rob Herring wrote:
> > On Sat, Oct 12, 2013 at 3:54 PM, Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org> wrote:
> > > Hi all,
> > >
> > > for_each_child_of_node() and similar functions increase the refcount
> > > on each returned node and expect the caller to release the node by
> > > calling of_node_put() when done.
> > >
> > > Looking through the kernel code, it appears this is hardly ever done,
> > > if at all. Some code even calls of_node_get() on returned nodes again.
> > >
> > > I guess this doesn't matter in cases where devicetree is a static entity.
> > > However, this is not (or no longer) the case with devicetree overlays,
> > > or more generically in cases where devicetree nodes are added and
> > > removed dynamically.
> > >
> > > Fundamental question: Would patches to fix this problem be accepted upstream
> > > ?
> > 
> > Certainly.
> > 
> > > Or, of course, stepping a bit back: Am I missing something essential ?
> > 
> > No. I think this is frequently wrong since it typically doesn't matter
> > for static entries as you mention.
> 
> Actually, I think it actually happens to be correct most of the time.
> The reason is that for_each_child_of_node() internally calls the
> of_get_next_child() to iterate over all children. And that function
> already calls of_node_put() on the "previous" node. So if all the code
> does is to iterate over all nodes to query them, then all should be
> fine.
> 
Good, that reduces the scope of the problem significantly.

> The only case where you actually need to drop the reference on a node is
> if you break out of the loop (so that of_get_next_child() will not be
> called). But that's usually the case when you need to perform some
> operation on the node, in which case it is the right thing to hold on to
> a reference until you're done with the node.
> 
Unfortunately, there are many cases with code such as

	if (error)
		return;	/* or break; */

or even
	if (found node)
		return of_node_get(node);

in the loop.

Guenter
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: Usage of for_each_child_of_node()
       [not found]           ` <20131023161644.GB20675-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
@ 2013-10-24  7:50             ` Thierry Reding
  2013-10-24 13:31               ` Guenter Roeck
  0 siblings, 1 reply; 7+ messages in thread
From: Thierry Reding @ 2013-10-24  7:50 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Rob Herring, devicetree-u79uwXL29TY76Z2rM5mHXA, Grant Likely,
	Rob Herring, linux-kernel-u79uwXL29TY76Z2rM5mHXA

[-- Attachment #1: Type: text/plain, Size: 2806 bytes --]

On Wed, Oct 23, 2013 at 09:16:44AM -0700, Guenter Roeck wrote:
> On Wed, Oct 23, 2013 at 09:10:07AM +0200, Thierry Reding wrote:
> > On Sat, Oct 12, 2013 at 10:15:03PM -0500, Rob Herring wrote:
> > > On Sat, Oct 12, 2013 at 3:54 PM, Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org> wrote:
> > > > Hi all,
> > > >
> > > > for_each_child_of_node() and similar functions increase the refcount
> > > > on each returned node and expect the caller to release the node by
> > > > calling of_node_put() when done.
> > > >
> > > > Looking through the kernel code, it appears this is hardly ever done,
> > > > if at all. Some code even calls of_node_get() on returned nodes again.
> > > >
> > > > I guess this doesn't matter in cases where devicetree is a static entity.
> > > > However, this is not (or no longer) the case with devicetree overlays,
> > > > or more generically in cases where devicetree nodes are added and
> > > > removed dynamically.
> > > >
> > > > Fundamental question: Would patches to fix this problem be accepted upstream
> > > > ?
> > > 
> > > Certainly.
> > > 
> > > > Or, of course, stepping a bit back: Am I missing something essential ?
> > > 
> > > No. I think this is frequently wrong since it typically doesn't matter
> > > for static entries as you mention.
> > 
> > Actually, I think it actually happens to be correct most of the time.
> > The reason is that for_each_child_of_node() internally calls the
> > of_get_next_child() to iterate over all children. And that function
> > already calls of_node_put() on the "previous" node. So if all the code
> > does is to iterate over all nodes to query them, then all should be
> > fine.
> > 
> Good, that reduces the scope of the problem significantly.
> 
> > The only case where you actually need to drop the reference on a node is
> > if you break out of the loop (so that of_get_next_child() will not be
> > called). But that's usually the case when you need to perform some
> > operation on the node, in which case it is the right thing to hold on to
> > a reference until you're done with the node.
> > 
> Unfortunately, there are many cases with code such as
> 
> 	if (error)
> 		return;	/* or break; */

Well, a break isn't necessarily bad, since you could be using the node
subsequently. I imagine that depending on the exact block following the
if statement the node could also be assigned to some field within a
structure or similar, in which case this might still be valid. So it
really needs to be evaluated on a case by case basis.

If the above is actually verbatim, then yes, that's certainly an error.

> or even
> 	if (found node)
> 		return of_node_get(node);
> 
> in the loop.

Yeah, I think all of those are probably wrong too.

Thierry

[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]

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

* Re: Usage of for_each_child_of_node()
  2013-10-24  7:50             ` Thierry Reding
@ 2013-10-24 13:31               ` Guenter Roeck
       [not found]                 ` <52692129.3070207-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: Guenter Roeck @ 2013-10-24 13:31 UTC (permalink / raw)
  To: Thierry Reding
  Cc: Rob Herring, devicetree, Grant Likely, Rob Herring, linux-kernel

On 10/24/2013 12:50 AM, Thierry Reding wrote:
> On Wed, Oct 23, 2013 at 09:16:44AM -0700, Guenter Roeck wrote:
>> On Wed, Oct 23, 2013 at 09:10:07AM +0200, Thierry Reding wrote:
>>> On Sat, Oct 12, 2013 at 10:15:03PM -0500, Rob Herring wrote:
>>>> On Sat, Oct 12, 2013 at 3:54 PM, Guenter Roeck <linux@roeck-us.net> wrote:
>>>>> Hi all,
>>>>>
>>>>> for_each_child_of_node() and similar functions increase the refcount
>>>>> on each returned node and expect the caller to release the node by
>>>>> calling of_node_put() when done.
>>>>>
>>>>> Looking through the kernel code, it appears this is hardly ever done,
>>>>> if at all. Some code even calls of_node_get() on returned nodes again.
>>>>>
>>>>> I guess this doesn't matter in cases where devicetree is a static entity.
>>>>> However, this is not (or no longer) the case with devicetree overlays,
>>>>> or more generically in cases where devicetree nodes are added and
>>>>> removed dynamically.
>>>>>
>>>>> Fundamental question: Would patches to fix this problem be accepted upstream
>>>>> ?
>>>>
>>>> Certainly.
>>>>
>>>>> Or, of course, stepping a bit back: Am I missing something essential ?
>>>>
>>>> No. I think this is frequently wrong since it typically doesn't matter
>>>> for static entries as you mention.
>>>
>>> Actually, I think it actually happens to be correct most of the time.
>>> The reason is that for_each_child_of_node() internally calls the
>>> of_get_next_child() to iterate over all children. And that function
>>> already calls of_node_put() on the "previous" node. So if all the code
>>> does is to iterate over all nodes to query them, then all should be
>>> fine.
>>>
>> Good, that reduces the scope of the problem significantly.
>>
>>> The only case where you actually need to drop the reference on a node is
>>> if you break out of the loop (so that of_get_next_child() will not be
>>> called). But that's usually the case when you need to perform some
>>> operation on the node, in which case it is the right thing to hold on to
>>> a reference until you're done with the node.
>>>
>> Unfortunately, there are many cases with code such as
>>
>> 	if (error)
>> 		return;	/* or break; */
>
> Well, a break isn't necessarily bad, since you could be using the node
> subsequently. I imagine that depending on the exact block following the

Correct, but I meant the error case. Randomly looking through several
drivers, most of them get error return handling wrong. "Winner" so far
is of_regulator_match(), which doesn't release the node on error return,
but does not acquire references for use afterwards either.

Something to do with my non-existing free time ;-).

Guenter

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

* Re: Usage of for_each_child_of_node()
       [not found]                 ` <52692129.3070207-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
@ 2013-10-24 14:21                   ` Thierry Reding
  0 siblings, 0 replies; 7+ messages in thread
From: Thierry Reding @ 2013-10-24 14:21 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Rob Herring, devicetree-u79uwXL29TY76Z2rM5mHXA, Grant Likely,
	Rob Herring, linux-kernel-u79uwXL29TY76Z2rM5mHXA

[-- Attachment #1: Type: text/plain, Size: 2937 bytes --]

On Thu, Oct 24, 2013 at 06:31:21AM -0700, Guenter Roeck wrote:
> On 10/24/2013 12:50 AM, Thierry Reding wrote:
> >On Wed, Oct 23, 2013 at 09:16:44AM -0700, Guenter Roeck wrote:
> >>On Wed, Oct 23, 2013 at 09:10:07AM +0200, Thierry Reding wrote:
> >>>On Sat, Oct 12, 2013 at 10:15:03PM -0500, Rob Herring wrote:
> >>>>On Sat, Oct 12, 2013 at 3:54 PM, Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org> wrote:
> >>>>>Hi all,
> >>>>>
> >>>>>for_each_child_of_node() and similar functions increase the refcount
> >>>>>on each returned node and expect the caller to release the node by
> >>>>>calling of_node_put() when done.
> >>>>>
> >>>>>Looking through the kernel code, it appears this is hardly ever done,
> >>>>>if at all. Some code even calls of_node_get() on returned nodes again.
> >>>>>
> >>>>>I guess this doesn't matter in cases where devicetree is a static entity.
> >>>>>However, this is not (or no longer) the case with devicetree overlays,
> >>>>>or more generically in cases where devicetree nodes are added and
> >>>>>removed dynamically.
> >>>>>
> >>>>>Fundamental question: Would patches to fix this problem be accepted upstream
> >>>>>?
> >>>>
> >>>>Certainly.
> >>>>
> >>>>>Or, of course, stepping a bit back: Am I missing something essential ?
> >>>>
> >>>>No. I think this is frequently wrong since it typically doesn't matter
> >>>>for static entries as you mention.
> >>>
> >>>Actually, I think it actually happens to be correct most of the time.
> >>>The reason is that for_each_child_of_node() internally calls the
> >>>of_get_next_child() to iterate over all children. And that function
> >>>already calls of_node_put() on the "previous" node. So if all the code
> >>>does is to iterate over all nodes to query them, then all should be
> >>>fine.
> >>>
> >>Good, that reduces the scope of the problem significantly.
> >>
> >>>The only case where you actually need to drop the reference on a node is
> >>>if you break out of the loop (so that of_get_next_child() will not be
> >>>called). But that's usually the case when you need to perform some
> >>>operation on the node, in which case it is the right thing to hold on to
> >>>a reference until you're done with the node.
> >>>
> >>Unfortunately, there are many cases with code such as
> >>
> >>	if (error)
> >>		return;	/* or break; */
> >
> >Well, a break isn't necessarily bad, since you could be using the node
> >subsequently. I imagine that depending on the exact block following the
> 
> Correct, but I meant the error case. Randomly looking through several
> drivers, most of them get error return handling wrong. "Winner" so far
> is of_regulator_match(), which doesn't release the node on error return,
> but does not acquire references for use afterwards either.
> 
> Something to do with my non-existing free time ;-).

Well, that's better than boring, isn't it? =)

Thierry

[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]

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

end of thread, other threads:[~2013-10-24 14:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-12 20:54 Usage of for_each_child_of_node() Guenter Roeck
     [not found] ` <5259B6F8.3070701-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
2013-10-13  3:15   ` Rob Herring
2013-10-23  7:10     ` Thierry Reding
     [not found]       ` <20131023071006.GA7708-AwZRO8vwLAwmlAP/+Wk3EA@public.gmane.org>
2013-10-23 16:16         ` Guenter Roeck
     [not found]           ` <20131023161644.GB20675-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
2013-10-24  7:50             ` Thierry Reding
2013-10-24 13:31               ` Guenter Roeck
     [not found]                 ` <52692129.3070207-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
2013-10-24 14:21                   ` Thierry Reding

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