2002-09-01 17:23:12

by Oliver Neukum

[permalink] [raw]
Subject: question on spinlocks

Hi,

is the following sequence legal ?

spin_lock_irqsave(...);
...
spin_unlock(...);
schedule();
spin_lock(...);
...
spin_unlock_irqrestore(...);

TIA
Oliver


2002-09-01 19:00:51

by Ralf Baechle

[permalink] [raw]
Subject: Re: question on spinlocks

On Sun, Sep 01, 2002 at 07:27:53PM +0200, Oliver Neukum wrote:

> is the following sequence legal ?
>
> spin_lock_irqsave(...);
> ...
> spin_unlock(...);
> schedule();
> spin_lock(...);
> ...
> spin_unlock_irqrestore(...);

No; spin_lock_irqsave/spin_unlock_irqrestore and spin_lock/spin_unlock
have to be used in matching pairs.

Ralf

2002-09-01 21:49:48

by Thunder from the hill

[permalink] [raw]
Subject: Re: question on spinlocks

Hi,

On Sun, 1 Sep 2002, Ralf Baechle wrote:
> On Sun, Sep 01, 2002 at 07:27:53PM +0200, Oliver Neukum wrote:
> > is the following sequence legal ?
> >
> > spin_lock_irqsave(...);
> > ...
> > spin_unlock(...);
> > schedule();
> > spin_lock(...);
> > ...
> > spin_unlock_irqrestore(...);
>
> No; spin_lock_irqsave/spin_unlock_irqrestore and spin_lock/spin_unlock
> have to be used in matching pairs.

If it was his least problem! He'll run straight into a "schedule w/IRQs
disabled" bug.

Thunder
--
--./../...-/. -.--/---/..-/.-./..././.-../..-. .---/..-/.../- .-
--/../-./..-/-/./--..-- ../.----./.-../.-.. --./../...-/. -.--/---/..-
.- -/---/--/---/.-./.-./---/.--/.-.-.-
--./.-/-.../.-./.././.-../.-.-.-

2002-09-01 21:58:00

by Oliver Neukum

[permalink] [raw]
Subject: Re: question on spinlocks


> > No; spin_lock_irqsave/spin_unlock_irqrestore and spin_lock/spin_unlock
> > have to be used in matching pairs.
>
> If it was his least problem! He'll run straight into a "schedule w/IRQs
> disabled" bug.

OK, how do I drop an irqsave spinlock if I don't have flags?

Regards
Oliver

2002-09-01 22:03:54

by Robert Love

[permalink] [raw]
Subject: Re: question on spinlocks

On Sun, 2002-09-01 at 17:53, Thunder from the hill wrote:

> If it was his least problem! He'll run straight into a "schedule w/IRQs
> disabled" bug.

No, the "schedule with irqs disabled" message is on involuntary
reschedule (e.g. kernel preemption) when interrupts are disabled.

It "safe" (maybe not sane) to call schedule() with interrupts disabled -
some system calls and scheduler code do it since interrupts will be
unconditionally enabled when rescheduled or upon returning to
user-space. E.g., see sys_sched_yield().

The actual problem with the above is that when schedule() returns,
interrupts will be on and that is probably not the intention of the
author. What Oliver probably wants to do is:

spin_lock_irq(&lck);
...
spin_unlock(&lck);
schedule();
spin_lock_irq(&lck);
...
spin_unlock_irq(&lck);

The first unlock not having the irq-enable is an optimization as
described above. Also note I did not use irqsave... if there is a
chance interrupts were previously disabled and you have who-knows-what
sort of call-chain behind you, it is probably not safe to go calling
schedule() and reenabling interrupts anyhow.

Robert Love

2002-09-01 22:06:48

by Robert Love

[permalink] [raw]
Subject: Re: question on spinlocks

On Sun, 2002-09-01 at 18:02, Oliver Neukum wrote:
>
> > > No; spin_lock_irqsave/spin_unlock_irqrestore and spin_lock/spin_unlock
> > > have to be used in matching pairs.
> >
> > If it was his least problem! He'll run straight into a "schedule w/IRQs
> > disabled" bug.
>
> OK, how do I drop an irqsave spinlock if I don't have flags?

See my previous message.

Do not do what you are trying to do. Dropping a lock and calling
schedule is fine. Ditto with the interrupt part.

But note:

- interrupts will be reenabled when you reschedule and still
enabled when your task is finally running again.

- Since interrupts are going to magically restore, if you are
worried about the state of interrupts previous to your
function... you have a problem.

OK?

Robert Love

2002-09-01 22:05:01

by Thunder from the hill

[permalink] [raw]
Subject: Re: question on spinlocks

Hi,

On Mon, 2 Sep 2002, Oliver Neukum wrote:
> > > No; spin_lock_irqsave/spin_unlock_irqrestore and spin_lock/spin_unlock
> > > have to be used in matching pairs.
> >
> > If it was his least problem! He'll run straight into a "schedule w/IRQs
> > disabled" bug.
>
> OK, how do I drop an irqsave spinlock if I don't have flags?

IMHO you might even ask "How do I start a car when I don't have the keys?"

You might find a way, but it's not desired. Are you sure you want to
reschedule in an interrupt handler? If it's none, are you sure you want to
disable interrupts?

Thunder
--
--./../...-/. -.--/---/..-/.-./..././.-../..-. .---/..-/.../- .-
--/../-./..-/-/./--..-- ../.----./.-../.-.. --./../...-/. -.--/---/..-
.- -/---/--/---/.-./.-./---/.--/.-.-.-
--./.-/-.../.-./.././.-../.-.-.-

2002-09-01 22:07:26

by Robert Love

[permalink] [raw]
Subject: Re: question on spinlocks

On Sun, 2002-09-01 at 18:09, Thunder from the hill wrote:

> IMHO you might even ask "How do I start a car when I don't have the keys?"
>
> You might find a way, but it's not desired. Are you sure you want to
> reschedule in an interrupt handler? If it's none, are you sure you want to
> disable interrupts?

I do not think he is in an interrupt handler.

If he were, the system would die when he called schedule().

Robert Love

2002-09-01 22:11:36

by Thunder from the hill

[permalink] [raw]
Subject: Re: question on spinlocks

Hi,

On 1 Sep 2002, Robert Love wrote:
> spin_lock_irq(&lck);
> ...
> spin_unlock(&lck);
> schedule();
> spin_lock_irq(&lck);
> ...
> spin_unlock_irq(&lck);

That makes me understand his intention a lot more. I must have got beaten
up by the "irqsave".

Thunder

2002-09-01 22:28:34

by Oliver Neukum

[permalink] [raw]
Subject: Re: question on spinlocks

Am Montag, 2. September 2002 00:09 schrieb Thunder from the hill:
> Hi,
>
> On Mon, 2 Sep 2002, Oliver Neukum wrote:
> > > > No; spin_lock_irqsave/spin_unlock_irqrestore and
> > > > spin_lock/spin_unlock have to be used in matching pairs.
> > >
> > > If it was his least problem! He'll run straight into a "schedule
> > > w/IRQs disabled" bug.
> >
> > OK, how do I drop an irqsave spinlock if I don't have flags?
>
> IMHO you might even ask "How do I start a car when I don't have the
> keys?"

Break off the lock, touch some cables ... ;-)

> You might find a way, but it's not desired. Are you sure you want to
> reschedule in an interrupt handler? If it's none, are you sure you want
> to disable interrupts?

I am not in an interrupt handler. It's not my fault that the scsi layer
calls queuecommand with a spinlock held. But I need to sleep,
I have to get rid of that spinlock's effects. If possible I even want
interrupts to be enabled.

Regards
Oliver

2002-09-03 20:04:58

by Pete Zaitcev

[permalink] [raw]
Subject: Re: question on spinlocks

>> > No; spin_lock_irqsave/spin_unlock_irqrestore and spin_lock/spin_unlock
>> > have to be used in matching pairs.
>[...]
> OK, how do I drop an irqsave spinlock if I don't have flags?

It was a good thing you didn't have flags, because everything
that passes flags as arguments blows up on sparc immediately.

Most likely answer is "restructure your locking".

-- Pete

2002-09-03 22:09:13

by George Anzinger

[permalink] [raw]
Subject: Re: question on spinlocks

Oliver Neukum wrote:
>
> Am Montag, 2. September 2002 00:09 schrieb Thunder from the hill:
> > Hi,
> >
> > On Mon, 2 Sep 2002, Oliver Neukum wrote:
> > > > > No; spin_lock_irqsave/spin_unlock_irqrestore and
> > > > > spin_lock/spin_unlock have to be used in matching pairs.
> > > >
> > > > If it was his least problem! He'll run straight into a "schedule
> > > > w/IRQs disabled" bug.
> > >
> > > OK, how do I drop an irqsave spinlock if I don't have flags?
> >
> > IMHO you might even ask "How do I start a car when I don't have the
> > keys?"
>
> Break off the lock, touch some cables ... ;-)
>
> > You might find a way, but it's not desired. Are you sure you want to
> > reschedule in an interrupt handler? If it's none, are you sure you want
> > to disable interrupts?
>
> I am not in an interrupt handler. It's not my fault that the scsi layer
> calls queuecommand with a spinlock held. But I need to sleep,
> I have to get rid of that spinlock's effects. If possible I even want
> interrupts to be enabled.

I don't know scsi, but if the coder decided that the lock
and irq were needed, I suspect that messing with them will
get you in big trouble. I think you need to rethink this
thing at the scsi layer...

-g
>
> Regards
> Oliver
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/

--
George Anzinger [email protected]
High-res-timers:
http://sourceforge.net/projects/high-res-timers/
Preemption patch:
http://www.kernel.org/pub/linux/kernel/people/rml

2002-09-04 08:02:42

by Jan Hudec

[permalink] [raw]
Subject: Re: question on spinlocks

On Mon, Sep 02, 2002 at 12:33:23AM +0200, Oliver Neukum wrote:
> Am Montag, 2. September 2002 00:09 schrieb Thunder from the hill:
> > Hi,
> >
> > On Mon, 2 Sep 2002, Oliver Neukum wrote:
> > > > > No; spin_lock_irqsave/spin_unlock_irqrestore and
> > > > > spin_lock/spin_unlock have to be used in matching pairs.
> > > >
> > > > If it was his least problem! He'll run straight into a "schedule
> > > > w/IRQs disabled" bug.
> > >
> > > OK, how do I drop an irqsave spinlock if I don't have flags?
> >
> > IMHO you might even ask "How do I start a car when I don't have the
> > keys?"
>
> Break off the lock, touch some cables ... ;-)
>
> > You might find a way, but it's not desired. Are you sure you want to
> > reschedule in an interrupt handler? If it's none, are you sure you want
> > to disable interrupts?
>
> I am not in an interrupt handler. It's not my fault that the scsi layer
> calls queuecommand with a spinlock held. But I need to sleep,
> I have to get rid of that spinlock's effects. If possible I even want
> interrupts to be enabled.

If it's calling it with spinlock held, it probably relies on that it
won't be sleeping. So you either have to avoid sleeping (start now,
finish in tasklet) or patch the calling code.

-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <[email protected]>