mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@poochiereds.net>
To: Oleg Drokin <green@linuxhacker.ru>,
	Peter Zijlstra <peterz@infradead.org>
Cc: Mailing List <linux-kernel@vger.kernel.org>,
	mingo@redhat.com, arjan@linux.intel.com,
	"J . Bruce Fields" <bfields@fieldses.org>
Subject: Re: initialize a mutex into locked state?
Date: Fri, 17 Jun 2016 10:42:20 -0400	[thread overview]
Message-ID: <1466174540.3596.16.camel@poochiereds.net> (raw)
In-Reply-To: <17E58758-CA23-420D-89B4-50E71F8A6428@linuxhacker.ru>

On Fri, 2016-06-17 at 10:40 -0400, Oleg Drokin wrote:
> On Jun 17, 2016, at 10:32 AM, Peter Zijlstra wrote:
> 
> > 
> > On Fri, Jun 17, 2016 at 10:24:32AM -0400, Oleg Drokin wrote:
> > > 
> > > 
> > > On Jun 17, 2016, at 10:19 AM, Peter Zijlstra wrote:
> > > 
> > > > 
> > > > On Fri, Jun 17, 2016 at 10:14:10AM -0400, Oleg Drokin wrote:
> > > > > 
> > > > > 
> > > > > On Jun 17, 2016, at 4:25 AM, Peter Zijlstra wrote:
> > > > > 
> > > > > > 
> > > > > > On Wed, Jun 15, 2016 at 02:23:35PM -0400, Oleg Drokin
> > > > > > wrote:
> > > > > > > 
> > > > > > > Hello!
> > > > > > > 
> > > > > > > To my surprise I found out that it's not possible to
> > > > > > > initialise a mutex into
> > > > > > > a locked state.
> > > > > > > I discussed it with Arjan and apparently there's no
> > > > > > > fundamental reason
> > > > > > > not to allow this.
> > > > > > There is. A mutex _must_ have an owner. If you can
> > > > > > initialize it in
> > > > > > locked state, you could do so statically, ie. outside of
> > > > > > the context of
> > > > > > a task.
> > > > > What's wrong with disallowing only static initializers, but
> > > > > allowing dynamic ones?
> > > > > Then there is a clear owner.
> > > > At which point, what wrong with the simple:
> > > > 
> > > > 	mutex_init(&m);
> > > > 	mutex_lock(&m);
> > > > 
> > > > Sequence? Its obvious, has clear semantics and doesn't extend
> > > > the API.
> > > The problem is:
> > > 
> > > spin_lock(somelock);
> > > structure = some_internal_list_lookup(list);
> > > if (structure)
> > > 	goto out;
> > > 
> > > init_new_structure(new_structure);
> > > mutex_init(&new_structure->s_mutex);
> > > mutex_lock(&new_structure->s_mutex);  // XXX CANNOT DO THIS UNDER
> > > SPINLOCK!
> > 	mutex_trylock(&new_structure->s_mutex);
> > 
> > should work, since you know it cannot be acquired yet by anybody
> > else,
> > since you've not published it yet.
> This does work, but suddenly does not look so obvious anymore, does
> it?
> I got some feedback that doing this is not really preferred.
> 
> Also once __must_check is added to mutex_try_lock() (surprised it's
> not yet),
> we'll need to also have the useless "but what if it did fail to lock"
> path?
> 

Maybe just BUG in that case, and add a comment that says something
along the lines of "this should always work since it's not hashed yet"
?
 
> > 
> > And a trylock does not sleep, so is perfectly fine under a
> > spinlock.
> > 
> > > 
> > > 
> > > list_add(list, new_structure->s_list);
> > > structure = new_structure;
> > > out:
> > > spin_unlock(somelock);
> > > return structure;
> > > 

-- 
Jeff Layton <jlayton@poochiereds.net>

  reply	other threads:[~2016-06-17 14:42 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-15 18:23 Oleg Drokin
2016-06-17  8:25 ` Peter Zijlstra
2016-06-17 14:14   ` Oleg Drokin
2016-06-17 14:19     ` Peter Zijlstra
2016-06-17 14:24       ` Oleg Drokin
2016-06-17 14:32         ` Peter Zijlstra
2016-06-17 14:40           ` Oleg Drokin
2016-06-17 14:42             ` Jeff Layton [this message]
2016-06-17 14:54               ` Oleg Drokin
2016-06-17 14:59                 ` Arjan van de Ven
2016-06-17 14:59             ` Peter Zijlstra
     [not found]         ` <CAC0gvwFhyahkP9M7Ktfd0GOv8CJbkeVegSfc57XpEUk8qAGA1w@mail.gmail.com>
2016-06-17 14:46           ` Oleg Drokin

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=1466174540.3596.16.camel@poochiereds.net \
    --to=jlayton@poochiereds.net \
    --cc=arjan@linux.intel.com \
    --cc=bfields@fieldses.org \
    --cc=green@linuxhacker.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®