public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jamie Lokier <jamie@shareable.org>
To: RANDAZZO@ddc-web.com
Cc: linux-kernel@vger.kernel.org
Subject: Re: spinlocks dont work
Date: Sat, 14 Feb 2004 00:21:29 +0000	[thread overview]
Message-ID: <20040214002129.GD31199@mail.shareable.org> (raw)
In-Reply-To: <89760D3F308BD41183B000508BAFAC4104B16F6F@DDCNYNTD>

RANDAZZO@ddc-web.com wrote:
> On my uniprocessor system, I have two LKM's
>
> driver1 takes hold of the spinlock....but does not release it...
> driver2 attempts to take hold, and is allowed!!!!

How does driver2 run when driver1 holds the spinlock?  It is not
possible: driver1 is running, and you only have one CPU.

It is an error if you call schedule() while holding a spinlock.  It is
also an error if you call the non-irqsave or non-bh spinlock functions
from a context where an irq or bh could interrupt and take the same lock.

These rules ensure that driver1 and driver2 are prevented from
accessing the hardware at the same time.

> how come spin locks don't work?????

They do, if you use them correctly.

> how can I restrict access (to hardware) to only one driver at a time???

By following the correct rules.
See http://www.kernel.org/pub/linux/kernel/people/rusty/kernel-locking/

> should I use semaphores,  etc...

Are you scheduling while you hold the spinlock?  Then you should use
semaphores instead.  But you cannot use semaphores if you are locking
from any kind of interrupt (this includes timer callbacks and wakeup
functions).

Are you using the spinlock from an interrupt?  This is not normally a
problem because each hardware device normally has just one interrupt source.

Are you using the spinlock from a softirq or whatever they are called
these days, as well as an interrupt?  Then you should be using the
spin_lock_irqsave functions in those cases.

And so on.  You must use the correct type of lock functions.

In a uniprocessor kernel, spinlocks compile to almost no code.
(Someone said they compile to nops; this is not quite accurate).  Even
on a uniprocessor, they disable preemptive scheduling while the lock
is held.  This is completely correct.

-- Jamie

      parent reply	other threads:[~2004-02-14  0:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-02-13 16:27 spinlocks dont work RANDAZZO
2004-02-13 16:48 ` Richard B. Johnson
2004-02-13 16:59 ` Joe Thornber
2004-02-13 23:11 ` Rik van Riel
2004-02-14  0:21 ` Jamie Lokier [this message]

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=20040214002129.GD31199@mail.shareable.org \
    --to=jamie@shareable.org \
    --cc=RANDAZZO@ddc-web.com \
    --cc=linux-kernel@vger.kernel.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