public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Balbir Singh <balbir@in.ibm.com>
To: Oleg Nesterov <oleg@tv-sign.ru>
Cc: Linus Torvalds <torvalds@osdl.org>,
	Benjamin LaHaise <bcrl@kvack.org>, Ingo Molnar <mingo@elte.hu>,
	Andi Kleen <ak@suse.de>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@osdl.org>,
	Arjan van de Ven <arjanv@infradead.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Christoph Hellwig <hch@infradead.org>,
	David Howells <dhowells@redhat.com>,
	Alexander Viro <viro@ftp.linux.org.uk>
Subject: Re: [patch 00/15] Generic Mutex Subsystem
Date: Wed, 11 Jan 2006 12:03:22 +0530	[thread overview]
Message-ID: <20060111063322.GA9261@in.ibm.com> (raw)
In-Reply-To: <43C3F6DB.FEFDA101@tv-sign.ru>

On Tue, Jan 10, 2006 at 09:03:07PM +0300, Oleg Nesterov wrote:
> Balbir Singh wrote:
> >
> > On Wed, Dec 21, 2005 at 07:42:14PM +0300, Oleg Nesterov wrote:
> > >
> > > Note that subsequent up() will not call wakeup(): ->count == 0,
> > > it just increment it. That is why we are waking the next waiter
> > > in advance. When it gets cpu, it will decrement ->count by 1,
> > > because ->sleepers == 0. If up() (++->count) was already called,
> > > it takes semaphore. If not - goes to sleep again.
> > >
> > > Or my understanding is completely broken?
> >
> > [ ... long snip ... ]
> >
> > The question now remains as to why we have the atomic_add_negative()? Why do
> > we change the count in __down(), when down() has already decremented it
> > for us?
> 
> ... and why __down() always sets ->sleepers = 0 on return.
>

I think sem->sleepers initially started as a counter, but was later converted
to a boolean value (0 or 1). The only possible values of sem->sleepers is 0, 1
or 2 and we always use sem->sleepers - 1 and set the value to either 0 or 1.

sem->sleepers is set to 0, so that when the double wakeup is called on the
wait queue, the task that wakes up (P2) corrects the count to 
(sem->sleepers - 1) = -1. This ensures that other tasks do not acquire 
the semaphore with down() (count is -1) and P2 sets sem->sleepers back to 1 
and sleeps.

 
> I don't have an answer, only a wild guess.
> 
> Note that if P1 releases this semaphore before pre-woken P2 actually
> gets cpu what happens is:
> 
> 	P1->up() just increments ->count, no wake_up() (fastpath)
> 
> 	P2 takes the semaphore without schedule.
> 
> So *may be* it was designed this way as some form of optimization,
> in this scenario P2 has some chances to run with sem held earlier.
>

P1->up() will do a wake_up() only if count < 0. For no wake_up()
the count >=0 before the increment. This means that there is no one
sleeping on the semaphore.
 
Feel free to correct me,
Balbir

  reply	other threads:[~2006-01-11  6:33 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-19  1:34 [patch 00/15] Generic Mutex Subsystem Ingo Molnar
2005-12-19  4:22 ` Andi Kleen
2005-12-19  4:28   ` Steven Rostedt
2005-12-19  4:31     ` Andi Kleen
2005-12-19  6:24   ` Linus Torvalds
2005-12-19 12:56     ` Steven Rostedt
2005-12-19 16:55       ` Ingo Molnar
2005-12-19 15:50     ` Ingo Molnar
2005-12-19 19:11       ` Linus Torvalds
2005-12-19 19:25         ` Benjamin LaHaise
2005-12-19 19:55           ` Linus Torvalds
2005-12-21 16:42             ` Oleg Nesterov
2006-01-10 10:28               ` Balbir Singh
2006-01-10 18:03                 ` Oleg Nesterov
2006-01-11  6:33                   ` Balbir Singh [this message]
2006-01-11  9:22                     ` Oleg Nesterov
2005-12-19 20:11           ` Ingo Molnar
2005-12-19 20:19             ` Benjamin LaHaise
2005-12-19 20:32             ` Russell King
2005-12-19 20:57               ` Ingo Molnar
2005-12-19 19:55         ` Ingo Molnar
2005-12-19 20:12           ` Linus Torvalds
2005-12-19 23:37             ` Christoph Hellwig
2005-12-20  8:03             ` Nick Piggin
2005-12-20  8:06               ` Arjan van de Ven
2005-12-20  8:21                 ` Nick Piggin
2005-12-20  8:36                   ` Arjan van de Ven
2005-12-20  8:48                     ` Nick Piggin
2005-12-19 16:22   ` Ingo Molnar

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=20060111063322.GA9261@in.ibm.com \
    --to=balbir@in.ibm.com \
    --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=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=oleg@tv-sign.ru \
    --cc=rostedt@goodmis.org \
    --cc=torvalds@osdl.org \
    --cc=viro@ftp.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