public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Thierry Reding <thierry.reding@gmail.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Rob Herring <robherring2@gmail.com>,
	"devicetree@vger.kernel.org" <devicetree@vger.kernel.org>,
	Grant Likely <grant.likely@linaro.org>,
	Rob Herring <rob.herring@calxeda.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: Usage of for_each_child_of_node()
Date: Thu, 24 Oct 2013 09:50:58 +0200	[thread overview]
Message-ID: <20131024075058.GD9403@ulmo.nvidia.com> (raw)
In-Reply-To: <20131023161644.GB20675@roeck-us.net>

[-- Attachment #1: Type: text/plain, Size: 2779 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@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
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 --]

  reply	other threads:[~2013-10-24  7:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-12 20:54 Usage of for_each_child_of_node() Guenter Roeck
2013-10-13  3:15 ` Rob Herring
2013-10-23  7:10   ` Thierry Reding
2013-10-23 16:16     ` Guenter Roeck
2013-10-24  7:50       ` Thierry Reding [this message]
2013-10-24 13:31         ` Guenter Roeck
2013-10-24 14:21           ` Thierry Reding

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=20131024075058.GD9403@ulmo.nvidia.com \
    --to=thierry.reding@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=grant.likely@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=rob.herring@calxeda.com \
    --cc=robherring2@gmail.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox