public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Ingo Molnar <mingo@elte.hu>
To: Oleg Nesterov <oleg@tv-sign.ru>
Cc: lkml <linux-kernel@vger.kernel.org>,
	Linus Torvalds <torvalds@osdl.org>, Andrew Morton <akpm@osdl.org>,
	Arjan van de Ven <arjanv@infradead.org>,
	Nicolas Pitre <nico@cam.org>,
	Jes Sorensen <jes@trained-monkey.org>,
	Zwane Mwaikambo <zwane@arm.linux.org.uk>,
	David Howells <dhowells@redhat.com>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Benjamin LaHaise <bcrl@kvack.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Christoph Hellwig <hch@infradead.org>, Andi Kleen <ak@suse.de>,
	Russell King <rmk+lkml@arm.linux.org.uk>
Subject: Re: [patch 5/9] mutex subsystem, core
Date: Thu, 22 Dec 2005 15:46:26 +0100	[thread overview]
Message-ID: <20051222144626.GA31939@elte.hu> (raw)
In-Reply-To: <43AAAC6F.17CC646@tv-sign.ru>


* Oleg Nesterov <oleg@tv-sign.ru> wrote:

> > +	/*
> > +	 * Lets try to take the lock again - this is needed even if
> > +	 * we get here for the first time (shortly after failing to
> > +	 * acquire the lock), to make sure that we get a wakeup once
> > +	 * it's unlocked. Later on this is the operation that gives
> > +	 * us the lock. If there are other waiters we need to xchg it
> > +	 * to -1, so that when we release the lock, we properly wake
> > +	 * up the other waiters:
> > +	 */
> > +	old_val = atomic_xchg(&lock->count, -1);
> > +
> > +	if (unlikely(old_val == 1)) {
> > +		/*
> > +		 * Got the lock - rejoice! But there's one small
> > +		 * detail to fix up: above we have set the lock to -1,
> > +		 * unconditionally. But what if there are no waiters?
> > +		 * While it would work with -1 too, 0 is a better value
> > +		 * in that case, because we wont hit the slowpath when
> > +		 * we release the lock. We can simply use atomic_set()
> > +		 * for this, because we are the owners of the lock now,
> > +		 * and are still holding the wait_lock:
> > +		 */
> > +		if (likely(list_empty(&lock->wait_list)))
> > +			atomic_set(&lock->count, 0);
> 
> This is a minor issue, but still I think it makes sense to optimize
> for uncontended case:
> 
> 	old_val = atomic_xchg(&lock->count, 0); // no sleepers
> 
> 	if (old_val == 1) {
> 		// sleepers ?
> 		if (!list_empty(&lock->wait_list))
> 			// need to wakeup them
> 			atomic_set(&lock->count, -1);
> 		...
> 	}
>       [*]

but then we'd have to set it to -1 again, at [*], because we are now 
about to become a waiter. So i'm not sure it's worth switching this 
around.

Also, there are two uses of this codepath: first it's the 'did we race 
with an unlocker', in which case the lock is almost likely still 
contended. The second pass comes after we have woken up, in which case 
it's likely uncontended.

while we could split up the two cases and optimize each for its own 
situation, i think it makes more sense to have then unified, and thus to 
have more compact code. It's the slowpath after all.

	Ingo

  reply	other threads:[~2005-12-22 14:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-22 11:42 [patch 5/9] mutex subsystem, core Ingo Molnar
2005-12-22 11:57 ` Christoph Hellwig
2005-12-22 12:52   ` Ingo Molnar
2005-12-22 13:38 ` Oleg Nesterov
2005-12-22 14:46   ` Ingo Molnar [this message]
2005-12-22 16:12     ` Oleg Nesterov

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=20051222144626.GA31939@elte.hu \
    --to=mingo@elte.hu \
    --cc=ak@suse.de \
    --cc=akpm@osdl.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=arjanv@infradead.org \
    --cc=bcrl@kvack.org \
    --cc=dhowells@redhat.com \
    --cc=hch@infradead.org \
    --cc=jes@trained-monkey.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nico@cam.org \
    --cc=oleg@tv-sign.ru \
    --cc=rmk+lkml@arm.linux.org.uk \
    --cc=rostedt@goodmis.org \
    --cc=torvalds@osdl.org \
    --cc=zwane@arm.linux.org.uk \
    /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