Skip to contentSkip to conversation

question on spinlocks

DISCUSSION Oliver Neukum 7 participants 13 messages Sep 1, 2002 – Sep 4, 2002 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hi,

is the following sequence legal ?

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

TIA
Oliver

#1 lore raw
Quoted 9 lines · Oliver Neukum

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

#2 lore raw

Hi,

Quoted 11 lines · Ralf Baechle
Quoted 9 lines · Oliver Neukum

is the following sequence legal ?

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

Quoted 2 lines

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

Signature · 4 lines
--./../...-/. -.--/---/..-/.-./..././.-../..-. .---/..-/.../- .-
--/../-./..-/-/./--..-- ../.----./.-../.-.. --./../...-/. -.--/---/..-
.- -/---/--/---/.-./.-./---/.--/.-.-.-
--./.-/-.../.-./.././.-../.-.-.-
#3 lore raw
Quoted 6 lines
Quoted 3 lines

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

#4 lore raw

Hi,

Quoted 7 lines · Oliver Neukum
Quoted 5 lines
Quoted 2 lines

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

Signature · 4 lines
--./../...-/. -.--/---/..-/.-./..././.-../..-. .---/..-/.../- .-
--/../-./..-/-/./--..-- ../.----./.-../.-.. --./../...-/. -.--/---/..-
.- -/---/--/---/.-./.-./---/.--/.-.-.-
--./.-/-.../.-./.././.-../.-.-.-
#5 lore raw
Quoted 5 lines · Thunder from the hill

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

#6 lore raw
In reply to Thunder from the hill

Am Montag, 2. September 2002 00:09 schrieb Thunder from the hill:

Quoted 6 lines

Hi,

Quoted 3 lines · Oliver Neukum
Quoted 3 lines
Quoted 2 lines

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

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

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 ... ;-)

Quoted 3 lines

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

#7 lore raw
Quoted 18 lines · Oliver Neukum

Am Montag, 2. September 2002 00:09 schrieb Thunder from the hill:

Quoted 6 lines

Hi,

Quoted 3 lines · Oliver Neukum
Quoted 3 lines
Quoted 2 lines

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

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

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 ... ;-)

Quoted 8 lines
Quoted 3 lines

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

Quoted 9 lines

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/

Signature · 5 lines
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
#8 lore raw
In reply to Oliver Neukum
Quoted 17 lines · Oliver Neukum

Am Montag, 2. September 2002 00:09 schrieb Thunder from the hill:

Quoted 6 lines

Hi,

Quoted 3 lines · Oliver Neukum
Quoted 3 lines
Quoted 2 lines

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

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

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 ... ;-)

Quoted 8 lines
Quoted 3 lines

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]>

#9 lore raw
In reply to Oliver Neukum
Quoted 8 lines · Oliver Neukum
Quoted 6 lines
Quoted 3 lines

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

#10 lore raw
In reply to Thunder from the hill
Quoted 2 lines · Thunder from the hill

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

#11 lore raw

Hi,

Quoted 7 lines · Robert Love
	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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#13 lore raw
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

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