Skip to contentSkip to conversation

Need for a new spinlock API?

DISCUSSION Rajat Jain 5 participants 14 messages Mar 21, 2007 – Apr 4, 2007

lore
Share & tools
lore

Rajat Jain

author
#0 lore raw

Hi,

We often have a case where a driver wants to access its data structure in process context as well as in interrupt context (in its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as disable all the local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs the lock) while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock) on the local processor.

Isn't this sub-optimal? Shouldn't there be a finer grained locking?

I was wondering if it is possible to have a spin lock API that grabs the lock and disables ONLY the specified irq on the local processor?

Am I missing something here?

Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs

#1 lore raw
Quoted 11 lines · Rajat Jain

Hi,

We often have a case where a driver wants to access its data structure in process context as well as in interrupt context (in its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as disable all the local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs the lock) while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock) on the local processor.

Quoted 1 line

Isn't this sub-optimal? Shouldn't there be a finer grained locking?

actually it's optimal.
It's fastest to delay the interrupts a little and be done with what you want to do under the lock quickly, and THEN take the interrupt. This means the lock hold time is short, which significantly reduces contention on this lock...

also it's really hard to impossible to get the more complex locking scenarios and dependencies right in this context; it's just so much simpler (and even this simple drivers get it wrong often .. :)

Signature · 8 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs
#2 lore raw
Quoted 12 lines · Arjan van de Ven
Quoted 11 lines · Rajat Jain

Hi,

We often have a case where a driver wants to access its data structure in process context as well as in interrupt context (in its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as disable all the local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs the lock) while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock) on the local processor.

Quoted 3 lines
Quoted 1 line

Isn't this sub-optimal? Shouldn't there be a finer grained locking?

actually it's optimal.

how is it optimal,when all you require is to disable just one particular IRQ?

Quoted 4 lines

It's fastest to delay the interrupts a little and be done with what you want to do under the lock quickly, and THEN take the interrupt. This means the lock hold time is short, which significantly reduces contention on this lock...

Aren't we increasing the latency because of this scheme?

Quoted 17 lines

also it's really hard to impossible to get the more complex locking scenarios and dependencies right in this context; it's just so much simpler (and even this simple drivers get it wrong often .. :)

--
if you want to mail me at work (you don't), use arjan (at) linux.intel.com Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org

--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ

Signature · 6 lines
Anubhav Rakshit
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs
#3 lore raw
Quoted 13 lines · anubhav rakshit
Quoted 12 lines · Arjan van de Ven
Quoted 11 lines · Rajat Jain

Hi,

We often have a case where a driver wants to access its data structure in process context as well as in interrupt context (in its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as disable all the local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs the lock) while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock) on the local processor.

Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Isn't this sub-optimal? Shouldn't there be a finer grained locking?

actually it's optimal.

how is it optimal,when all you require is to disable just one particular IRQ?

because if you don't disable all you increase hold times, which increases contention. Contention is BAD.

Quoted 6 lines
Quoted 5 lines

It's fastest to delay the interrupts a little and be done with what you want to do under the lock quickly, and THEN take the interrupt. This means the lock hold time is short, which significantly reduces contention on this lock...

Aren't we increasing the latency because of this scheme?

very very tiny amounts; since typically lock hold times are really short

Signature · 8 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs
#4 lore raw
Quoted 16 lines · Arjan van de Ven
Quoted 15 lines · anubhav rakshit
Quoted 14 lines · Arjan van de Ven
Quoted 12 lines · Rajat Jain

Hi,

We often have a case where a driver wants to access its data structure in process context as well as in interrupt context (in its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as disable all the local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs the lock) while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock) on the local processor.

Isn't this sub-optimal? Shouldn't there be a finer grained locking?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

actually it's optimal.

how is it optimal,when all you require is to disable just one particular IRQ?

because if you don't disable all you increase hold times, which increases contention. Contention is BAD.

Newbie advice:
Well, looking at LDD3 -> interrupt handling, there are a few functions void disable_irq(int irq);
void disable_irq_nosync(int irq);
void enable_irq(int irq);

But you cannot use them if you share an irq and your hardware must not generate another interrupt while you are handling this one. This is usually the case (i think) since many devices have some kind of "handled" register.

Signature · 12 lines
Regards,
        Tzahi.
--
Tzahi Fadida
Blog: http://tzahi.blogsite.org | Home Site: http://tzahi.webhop.info
WARNING TO SPAMMERS:  see at 
http://members.lycos.co.uk/my2nis/spamwarning.html
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs
author
#5 lore raw
In reply to Arjan van de Ven

Hi Arjan,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

We often have a case where a driver wants to access its data structure in process context as well as in interrupt context (in its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as disable all the local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs the lock) while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock) on the local processor.

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Isn't this sub-optimal? Shouldn't there be a finer grained locking?

actually it's optimal.

how is it optimal,when all you require is to disable just one particular IRQ?

because if you don't disable all you increase hold times, which increases contention. Contention is BAD.

Do you mean the lock hold time here? How is lock hold time affected by whether we disable just one or all the irqs?

Secondly, is it possible AT ALL to disable a particular irq at the local CPU?

Thanks,

Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs

#6 lore raw
Quoted 29 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Rajat Jain
Sent: Thursday, March 22, 2007 6:43 AM
To: Arjan van de Ven
Cc: anubhav rakshit; kernel mail; newbie
Subject: Re: Need for a new spinlock API?

Hi Arjan,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

We often have a case where a driver wants to access its data structure in process context as well as in interrupt

context (in

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

its ISR). In such scenarios, we generally use
spin_lock_irqsave() to grab the lock as well as

disable all the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs

the lock)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock)

on the local processor.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Isn't this sub-optimal? Shouldn't there be a finer

grained locking?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

actually it's optimal.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

how is it optimal,when all you require is to disable

just one particular IRQ?

Quoted 3 lines

because if you don't disable all you increase hold times, which increases contention. Contention is BAD.

Do you mean the lock hold time here? How is lock hold time affected by whether we disable just one or all the irqs?

The lock hold time may increase if you are in one ISR holding a lock and some other interrupt occurs. Processor will now call ISR linked to that interrupt (nested interrupts) assuming that priority of newly arrived interrupt is more.

Quoted 3 lines

Secondly, is it possible AT ALL to disable a particular irq at the local CPU?

It depends on the interrupt controller implementation in h/w. In most cases it should be possible. Most of them are quite programmable where you can mask out, disable, enable any interrupt line, set the priority of the interrupt line.

Ajay.

Quoted 10 lines

Thanks,

Rajat

--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs

author
#7 lore raw
Quoted 35 lines · Ajay Singh (ajaysi)
Quoted 35 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Rajat Jain
Sent: Thursday, March 22, 2007 6:43 AM
To: Arjan van de Ven
Cc: anubhav rakshit; kernel mail; newbie
Subject: Re: Need for a new spinlock API?

Hi Arjan,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

We often have a case where a driver wants to access its data structure in process context as well as in interrupt

context (in

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

its ISR). In such scenarios, we generally use
spin_lock_irqsave() to grab the lock as well as

disable all the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs

the lock)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock)

on the local processor.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Isn't this sub-optimal? Shouldn't there be a finer

grained locking?

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

actually it's optimal.

how is it optimal,when all you require is to disable

just one particular IRQ?

Quoted 3 lines

because if you don't disable all you increase hold times, which increases contention. Contention is BAD.

Quoted 7 lines
Quoted 2 lines

Do you mean the lock hold time here? How is lock hold time affected by whether we disable just one or all the irqs?

The lock hold time may increase if you are in one ISR holding a lock and some other interrupt occurs. Processor will now call ISR linked to that interrupt (nested interrupts) assuming that priority of newly arrived interrupt is more.

Yes, but isn't that desired? Because the newly arrived interrupt is of higher priority than the currently executing ISR and it doesn't need any lock at all, shouldn't the new ISR experience a lesser interrupt latency (executed before the current ISR finishes)?

Quoted 9 lines
Quoted 4 lines

Secondly, is it possible AT ALL to disable a particular irq at the local CPU?

It depends on the interrupt controller implementation in h/w. In most cases it should be possible. Most of them are quite programmable where you can mask out, disable, enable any interrupt line, set the priority of the interrupt line.

So we can disable a single interrupt ONLY for the local CPU (leaving it enabled for other CPUs), right?

Thanks,

Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs

#8 lore raw
Quoted 33 lines
Quoted 4 lines
Quoted 4 lines

Hi Arjan,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

We often have a case where a driver wants to

access its data

Quoted 15 lines
Quoted 15 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

structure in process context as well as in interrupt

context (in

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as

disable all the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs

the lock)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock)

on the local processor.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Isn't this sub-optimal? Shouldn't there be a finer

grained locking?

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

actually it's optimal.

how is it optimal,when all you require is to disable

just one particular IRQ?

Quoted 3 lines

because if you don't disable all you increase hold times, which increases contention. Contention is BAD.

Do you mean the lock hold time here? How is lock hold

time affected

Quoted 3 lines
Quoted 1 line

by whether we disable just one or all the irqs?

The lock hold time may increase if you are in one ISR

holding a lock

Quoted 8 lines
Quoted 1 line

and some other interrupt occurs. Processor will now call

ISR linked to

Quoted 2 lines

that interrupt (nested interrupts) assuming that priority of newly arrived interrupt is more.

Yes, but isn't that desired? Because the newly arrived interrupt is of higher priority than the currently executing ISR and it doesn't need any lock at all,

The lock contention is between the process or another instance of same ISR on other processor(say uP1) which are waiting for that same spinlock to be released. They will have to wait till old ISR instance finishes on processor (say uP0).

shouldn't the new

Quoted 3 lines

ISR experience a lesser interrupt latency (executed before the current ISR finishes)?

On some real time Oses it is implemented like this, but not on linux I think (CMIIAW). Also in that case you need to calculate how much stack space you need in worst case of interrupt nesting.

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

Secondly, is it possible AT ALL to disable a particular

irq at the

Quoted 3 lines
Quoted 1 line

local CPU?

It depends on the interrupt controller implementation in

h/w. In most

Quoted 9 lines
Quoted 1 line

cases it should be possible. Most of them are quite

programmable where

Quoted 1 line

you can mask out, disable, enable any interrupt line, set

the priority

Quoted 1 line

of the interrupt line.

So we can disable a single interrupt ONLY for the local CPU (leaving it enabled for other CPUs), right?

Not sure about this.

Quoted 4 lines

Thanks,

Rajat

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs

author
#9 lore raw
Quoted 4 lines

The lock contention is between the process or another instance of same ISR on other processor(say uP1) which are waiting for that same spinlock to be released. They will have to wait till old ISR instance finishes on processor (say uP0).

No, by design there cannot be two instances of your ISR running on two seperate processes (since the interrupt is disabled on all processors untill the ISR returns).
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs

#11 lore raw
In reply to Rajat Jain
Quoted 13 lines · Rajat Jain

Hi Arjan,

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

We often have a case where a driver wants to access its data structure in process context as well as in interrupt context (in its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as disable all the local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs the lock) while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock) on the local processor.

Isn't this sub-optimal? Shouldn't there be a finer grained locking?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

actually it's optimal.

how is it optimal,when all you require is to disable just one particular IRQ?

because if you don't disable all you increase hold times, which increases contention. Contention is BAD.

Do you mean the lock hold time here? How is lock hold time affected by whether we disable just one or all the irqs?

because if you don't disable all irqs, you get interrupted by others, and the irq time is added to your hold time.

Quoted 2 lines

Secondly, is it possible AT ALL to disable a particular irq at the local CPU?

not cheaply. You can fidge with apic stuff if you really want to. It's also horrible in case of shared interrupts :)

Signature · 8 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs
#10 lore raw
In reply to Rajat Jain
Quoted 8 lines · Rajat Jain
Quoted 4 lines

The lock contention is between the process or another instance of same ISR on other processor(say uP1) which are waiting for that same spinlock to be released. They will have to wait till old ISR instance finishes on processor (say uP0).

No, by design there cannot be two instances of your ISR running on two seperate processes (since the interrupt is disabled on all processors untill the ISR returns).

but the USERCONTEXT on the other cpu ALSO uses this lock, right? (Otherwise this entire discussion was moot already, that was your initial premise)

Signature · 8 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs
author
#12 lore raw
In reply to Arjan van de Ven
Quoted 9 lines
Quoted 9 lines

We often have a case where a driver wants to access its data structure in process context as well as in interrupt context (in its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as disable all the local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs the lock) while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock) on the local processor.

Quoted 7 lines
Quoted 1 line

Isn't this sub-optimal? Shouldn't there be a finer grained locking?

actually it's optimal.
It's fastest to delay the interrupts a little and be done with what you want to do under the lock quickly, and THEN take the interrupt. This means the lock hold time is short, which significantly reduces contention on this lock...

So on the same lines, if a data structure is accessed in both process context and in a (single) driver ISR, should a driver use spin_lock_irqsave() to get the lock in ISR? Or will a simple spin_lock() suffice?

Thanks,

Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs

#13 lore raw
Quoted 17 lines · Rajat Jain
Quoted 9 lines
Quoted 9 lines

We often have a case where a driver wants to access its data structure in process context as well as in interrupt context (in its ISR). In such scenarios, we generally use spin_lock_irqsave() to grab the lock as well as disable all the local interrupts. AFAIK, disabling of local interrupts is required so as to avoid running your ISR (which needs the lock) while process context is holding the lock. However, this also disables any other ISRs (which DO NOT need the lock) on the local processor.

Quoted 7 lines
Quoted 1 line

Isn't this sub-optimal? Shouldn't there be a finer grained locking?

actually it's optimal.
It's fastest to delay the interrupts a little and be done with what you want to do under the lock quickly, and THEN take the interrupt. This means the lock hold time is short, which significantly reduces contention on this lock...

Quoted 4 lines

So on the same lines, if a data structure is accessed in both process context and in a (single) driver ISR, should a driver use spin_lock_irqsave() to get the lock in ISR? Or will a simple spin_lock() suffice?

a simple spin_lock() should do,as in Linux the ISR's are not recursive,and you just need protection in a single ISR.

Anubhav Rakshit
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" 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.linux-learn.org/faqs