mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Matthew Dharm <mdharm-kernel@one-eyed-alien.net>
To: Linus Torvalds <torvalds@transmeta.com>
Cc: Pavel Machek <pavel@suse.cz>,
	Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Alexander Viro <viro@math.psu.edu>,
	"David S. Miller" <davem@redhat.com>,
	Andrea Arcangeli <andrea@suse.de>, Alan Cox <alan@redhat.com>,
	David Woodhouse <dwmw2@redhat.com>,
	linux-scsi@vger.kernel.org, Andrew Morton <andrewm@uow.edu.au>
Subject: Re: 2.4.7-pre9..
Date: Fri, 27 Jul 2001 12:47:36 -0700	[thread overview]
Message-ID: <20010727124736.B12304@one-eyed-alien.net> (raw)
In-Reply-To: <20010727091858.C10787@one-eyed-alien.net> <Pine.LNX.4.33.0107271029340.21738-100000@penguin.transmeta.com>
In-Reply-To: <Pine.LNX.4.33.0107271029340.21738-100000@penguin.transmeta.com>; from torvalds@transmeta.com on Fri, Jul 27, 2001 at 10:46:44AM -0700

[-- Attachment #1: Type: text/plain, Size: 3425 bytes --]

Hrm... just to be clear, then... this only is a problem with semaphores
that are declared on the local stack?

IIRC, usb-storage only uses semaphores that are allocated via kfree, so I
think we're okay.  Tho, I think the new semantics are probably better, and
will probably switch to them.  Later.

Matt

On Fri, Jul 27, 2001 at 10:46:44AM -0700, Linus Torvalds wrote:
> 
> On Fri, 27 Jul 2001, Matthew Dharm wrote:
> >
> > It looks like I missed an important discussion in the torrent of e-mail
> > that I receive... could someone give me the 30-second executive summary so
> > I can look at what may need to change in usb-storage?
> 
> The basic summary is that we had this (fairly common) way of waiting for
> certain events by having a locked semaphore on the stack of the waiter,
> and then having the waiter do a "down()" which caused it to block until
> the thing it was waiting for did an "up()".
> 
> This works fairly well, _but_ it has a really small (and quite unlikely)
> race on SMP, that is not so much a race of the idea itself, as of the
> implementation of the semaphores. We could have fixed the semaphores, but
> there were a few reasons not to:
> 
>  - the semaphores are optimized (on purpose) for the non-contention case.
>    The "wait for completion" usage has the opposite default case
>  - the semaphores are quite involved and architecture-specific, exactly
>    due to this optimization. Trying to change them is painful as hell.
> 
> So instead, I introduced the notion of "wait for completion":
> 
> 	struct completion event;
> 
> 	init_completion(&event);
> 	.. pass of event pointer to waker ..
> 	wait_for_completion(&event);
> 
> where the thing we're waiting for just does "complete(event)" and we're
> all done.
> 
> This has the advantage of being a bit more obvious just from a syntactic
> angle about what is going on. It also ends up being slightly more
> efficient than semaphores because we can handle the right expected case,
> and it also avoids the implementation issue that made for the race in the
> first place.
> 
> Switching over to the new format is really trivial:
> 
>  struct semaphore	-> struct completion
>  init_MUTEX_LOCKED	-> init_completion
>  DECLARE_MUTEX_LOCKED	-> DECLARE_COMPLETION
>  down()			-> wait_for_completion()
>  up()			-> complete()
> 
> and you can in fact maintain 2.2.x compatibility by just having a 2.2.x
> compatibility file that does the reverse mappings.
> 
> In case anybody cares, the race was that Linux semaphores only protect the
> accesses _inside_ the semaphore, while the accesses by the semaphores
> themselves can "race" in the internal implementation. That helps make an
> efficient implementation, but it means that the race was:
> 
> 	cpu #1				cpu #2
> 
> 	DECLARE_MUTEX_LOCKED(sem);
> 	..
> 	down(&sem);			up(&sem);
> 	return;
> 					wake_up(&sem.wait) /*BOOM*/
> 
> where the waker still touches the semaphore data structure after the
> sleeper has become happy with it no longer being locked - and free'd the
> data structure by virtue of freeing the stack.
> 
> 		Linus

-- 
Matthew Dharm                              Home: mdharm-usb@one-eyed-alien.net 
Maintainer, Linux USB Mass Storage Driver

Hey Chief.  We've figured out how to save the technical department.  We 
need to be committed.
					-- The Techs
User Friendly, 1/22/1998

[-- Attachment #2: Type: application/pgp-signature, Size: 232 bytes --]

  reply	other threads:[~2001-07-27 19:47 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-20  5:17 2.4.7-pre9 Linus Torvalds
2001-07-20  7:22 ` 2.4.7-pre9 Jens Axboe
2001-07-20  8:15   ` 2.4.7-pre9 Linus Torvalds
2001-07-20  8:26     ` 2.4.7-pre9 Jens Axboe
2001-07-20 16:42       ` 2.4.7-pre9 Linus Torvalds
2001-07-20 18:57         ` 2.4.7-pre9 Jens Axboe
2001-07-20 10:23 ` 2.4.7-pre9 David Woodhouse
2001-07-23 12:56 ` 2.4.7-pre9 Pavel Machek
2001-07-27 16:18   ` 2.4.7-pre9 Matthew Dharm
2001-07-27 17:46     ` 2.4.7-pre9 Linus Torvalds
2001-07-27 19:47       ` Matthew Dharm [this message]
2001-07-27 21:00         ` 2.4.7-pre9 Linus Torvalds
2001-07-28  3:53         ` 2.4.7-pre9 David Woodhouse
2001-07-27 19:55           ` 2.4.7-pre9 Matthew Dharm

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=20010727124736.B12304@one-eyed-alien.net \
    --to=mdharm-kernel@one-eyed-alien.net \
    --cc=alan@redhat.com \
    --cc=andrea@suse.de \
    --cc=andrewm@uow.edu.au \
    --cc=davem@redhat.com \
    --cc=dwmw2@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=pavel@suse.cz \
    --cc=torvalds@transmeta.com \
    --cc=viro@math.psu.edu \
    /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®