All of lore.kernel.org
 help / color / mirror / Atom feed
From: Russell King - ARM Linux admin <linux@armlinux.org.uk>
To: Stephen Boyd <sboyd@kernel.org>
Cc: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>,
	"mturquette@baylibre.com" <mturquette@baylibre.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"wens@csie.org" <wens@csie.org>,
	"miquel.raynal@bootlin.com" <miquel.raynal@bootlin.com>,
	"jhugo@codeaurora.org" <jhugo@codeaurora.org>,
	"linux-clk@vger.kernel.org" <linux-clk@vger.kernel.org>,
	"jbrunet@baylibre.com" <jbrunet@baylibre.com>
Subject: Re: [PATCH v3 1/7] clkdev: Hold clocks_mutex while iterating clocks list
Date: Mon, 8 Apr 2019 23:21:06 +0100	[thread overview]
Message-ID: <20190408222106.ect7nwjsklzhmdwj@shell.armlinux.org.uk> (raw)
In-Reply-To: <155474280295.20095.182612336816713159@swboyd.mtv.corp.google.com>

On Mon, Apr 08, 2019 at 10:00:02AM -0700, Stephen Boyd wrote:
> Quoting Matti Vaittinen (2019-04-08 03:49:41)
> > On Fri, Apr 05, 2019 at 01:37:24PM -0700, Stephen Boyd wrote:
> > > Quoting Vaittinen, Matti (2019-04-04 23:51:43)
> > > > On Thu, 2019-04-04 at 14:53 -0700, Stephen Boyd wrote:
> > > > > We recently introduced a change to support devm clk lookups. That
> > > > > change
> > > > > introduced a code-path that used clk_find() without holding the
> > > > > 'clocks_mutex'. Unfortunately, clk_find() iterates over the 'clocks'
> > > > > list and so we need to prevent the list from being modified while
> > > > > iterating over it by holding the mutex. Similarly, we don't need to
> > > > > hold
> > > > > the 'clocks_mutex' besides when we're dereferencing the clk_lookup
> > > > > pointer
> > > > 
> > > > /// Snip
> > > > 
> > > > > -out:
> > > > > +static struct clk_lookup *clk_find(const char *dev_id, const char
> > > > > *con_id)
> > > > > +{
> > > > > +     struct clk_lookup *cl;
> > > > > +
> > > > > +     mutex_lock(&clocks_mutex);
> > > > > +     cl = __clk_find(dev_id, con_id);
> > > > >       mutex_unlock(&clocks_mutex);
> > > > >  
> > > > > -     return cl ? clk : ERR_PTR(-ENOENT);
> > > > > +     return cl;
> > > > > +}
> > > > 
> > > > I am not an expert on this but reading commit message abowe and seeing
> > > > the code for clk_find() looks a bit scary. If I understand it
> > > > correctly, the clocks_mutex should be held when dereferencing the
> > > > clk_lookup returned by clk_find. The clk_find implementation drops the
> > > > lock before returning - which makes me think I miss something here. How
> > > > can the caller ever safely dereference returned clk_lookup pointer?
> > > > Just reading abowe makes me think that lock should be taken by whoever
> > > > is calling the clk_find, and dropped only after caller has used the
> > > > found clk_lookup for whatever caller intends to use it. Maybe I am
> > > > missing something?
> > > > 
> > > 
> > > The only user after this patch (devm) is doing a pointer comparison so
> > > it looks OK. But yes, in general there shouldn't be users of clk_find()
> > > that dereference the pointer because there isn't any protection besides
> > > the mutex.
> > 
> > If the only (intended) user for clk_find is devm_clk_release_clkdev,
> > then we might want to write it in devm_clk_release_clkdev - just to
> > avoid similar errors (as I did with devm) in the future? I might even
> > consider renaming __clk_find to clk_find or to clk_find_unsafe - but
> > that's all just nitpicking :) Go with what you like to maintain :D
> > 
> 
> Sure. I was thinking along the same lines after you asked.

This is rubbish.  The reason clk_find() doesn't take the lock is that
you _need_ to hold the lock while you dereference the clk_lookup data.
The lock isn't protecting just the lookup, it protects what you do with
the result of the lookup as well.

So, as I say, adding locking inside clk_find() is completely
misunderstanding the locking here.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up
According to speedtest.net: 11.9Mbps down 500kbps up

  reply	other threads:[~2019-04-08 22:21 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-04 21:53 [PATCH v3 0/7] Rewrite clk parent handling Stephen Boyd
2019-04-04 21:53 ` [PATCH v3 1/7] clkdev: Hold clocks_mutex while iterating clocks list Stephen Boyd
2019-04-05  6:51   ` Vaittinen, Matti
2019-04-05 20:37     ` Stephen Boyd
2019-04-08 10:49       ` Matti Vaittinen
2019-04-08 17:00         ` Stephen Boyd
2019-04-08 22:21           ` Russell King - ARM Linux admin [this message]
2019-04-09  5:31             ` Vaittinen, Matti
2019-04-09  8:57               ` Russell King - ARM Linux admin
2019-04-10 16:45                 ` Stephen Boyd
2019-04-04 21:53 ` [PATCH v3 2/7] clk: Prepare for clk registration API that uses DT nodes Stephen Boyd
2019-04-04 21:53 ` [PATCH v3 3/7] clk: Add of_clk_hw_register() API for early clk drivers Stephen Boyd
2019-04-08 21:46   ` Jeffrey Hugo
2019-04-10 16:49     ` Stephen Boyd
2019-04-10 16:53     ` Stephen Boyd
2019-04-10 19:39       ` Jeffrey Hugo
2019-04-10 21:45         ` Stephen Boyd
2019-04-10 22:18           ` Jeffrey Hugo
2019-04-11 17:32             ` Jeffrey Hugo
2019-04-04 21:53 ` [PATCH v3 4/7] clk: Allow parents to be specified without string names Stephen Boyd
2019-04-04 21:53 ` [PATCH v3 5/7] clk: Look for parents with clkdev based clk_lookups Stephen Boyd
2019-04-04 21:53 ` [PATCH v3 6/7] clk: Allow parents to be specified via clkspec index Stephen Boyd
2019-04-04 21:53 ` [PATCH v3 7/7] clk: fixed-factor: Let clk framework find parent Stephen Boyd

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=20190408222106.ect7nwjsklzhmdwj@shell.armlinux.org.uk \
    --to=linux@armlinux.org.uk \
    --cc=jbrunet@baylibre.com \
    --cc=jhugo@codeaurora.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matti.vaittinen@fi.rohmeurope.com \
    --cc=miquel.raynal@bootlin.com \
    --cc=mturquette@baylibre.com \
    --cc=sboyd@kernel.org \
    --cc=wens@csie.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.