public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 2.6.16] Shared interrupts sometimes lost
@ 2006-04-08  4:10 Neil Brown
  2006-04-08 16:31 ` Lee Revell
  2006-04-11 17:07 ` Pavel Machek
  0 siblings, 2 replies; 9+ messages in thread
From: Neil Brown @ 2006-04-08  4:10 UTC (permalink / raw)
  To: linux-kernel


Hi,
 I've been having enormous fun just lately playing with my new Minitar
 wireless cards - based on the RaLink 2560 chip set.    I have a
 couple of PCI cards that seem to work well, and a PCMCIA that mostly
 works, but will lockup the computer under load, which isn't nice :-(

 This is using the "legacy" rt2500 driver derived from the vendor
 provided driver, rather than the newer rt2x00 which I cannot get to
 work at all.  These are all part of rt2400.sourceforge.net.

 Anyway, it turns out that my lockup problem is actually a problem in
 the Linux kernel rather than with the driver (though possibly aspects
 of the driver make the situation worse).  I have a patch, below,
 which fixes my problem for me.

 The problem is that after a while, the interrupts generated by the
 card stop being handled by Linux, so the ISR routine in the driver
 doesn't get called, and progress of all sorts stop.  The fact that
 this leads to a full system lockup is the fault of the driver, not
 Linux.  But Linux is definitely implicated in the loss of interrupts.
 I can work around the problem using "irqpoll", but that is a bit
 heavy handed, and shouldn't be needed.

 This is my first real introduction to the IRQ handling code in Linux,
 so please forgive any little errors.  I'm fairly sure the big picture
 is right, partly because the patch helps so much.

 To explain what I think is happening, let me start with a very simple
 case.  A number of PCI devices (this one included) have a number of
 events which can trigger an interrupt.  The events which are current
 are presented as bits in a register, and are ORed together (and
 possibly masked by another register) to make the IRQ line.
 When 1's are written to any bits in this register, it acknowledges
 the event and clears the bit.
 A typical code fragment is 
   events = read_register(INTERRUPTS);
   write_register(INTERRUPTS, events);
   ... handle each 1 bits in events ....

 This would normally clear all pending events and cause the interrupt
 line to go low (or at least to not be asserted).

 However there is room for a race here.  If an event occurs between
 the read and the write, then this will NOT de-assert the IRQ line.
 It will remain asserted throughout.

 Now if the IRQ is handled as an edge-triggered line (which I believe
 they are in Linux), then losing this race will mean that we don't see
 any more interrupts on this line.

 This is a race that would be fairly hard to hit, and is easily fixed
 by the driver.  After handling all of 'events', it should read the
 INTERRUPTS register again and see if there is anything more.  I tried
 this and it didn't fix my problem (though it might have made it
 happen less often, I'm not certain).

 In my particular case (and probably quite commonly with PCMCIA
 cards), the IRQ line is shared:

 10:      66178          XT-PIC  yenta, yenta, ohci_hcd:usb2, ohci_hcd:usb3, ehci_hcd:usb4, eth0

 So now the race is somewhat larger.
 If yenta-1 handles its interrupt events and then another event
 happens for yenta-1 before eth0 gets around to clearing its
 interrupts, the shared IRQ line will remain asserted after all IRQ
 handlers have been called, and so another edge will never be seen.

 This race - with a shared IRQ line - cannot be handled within any one
 driver.  There needs to be some sort of end-to-end check.

 In particular, the list of 'action' handlers needs to be called
 repeatedly until a full pass has been made through all, and none of
 them have reported that there was anything to do.  Only then can we
 be sure that the IRQ line has been de-asserted.

 The following patch does that, and "works for me".  It included a
 kernel-parameter which allows me to disable the fix so I can test
 that the fix really makes a difference.  It does.

 I am presenting this patch for comment at the moment rather than
 inclusion.  If it is generally acceptable, I'll remove the __setup
 stuff and put a proper change-log at the top etc.

 It might be appropriate to put a loop limit in so it doesn't more
 than 1000 times or something, just in case someone claims to always
 handle the interrupt.  Is that needed?

 Looking forward to your comments,
 NeilBrown


------------------------------
Signed-off-by: Neil Brown <neilb@suse.de>

### Diffstat output
 ./kernel/irq/handle.c |   28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff ./kernel/irq/handle.c~current~ ./kernel/irq/handle.c
--- ./kernel/irq/handle.c~current~	2006-04-08 13:26:46.000000000 +1000
+++ ./kernel/irq/handle.c	2006-04-08 13:27:05.000000000 +1000
@@ -73,23 +73,47 @@ irqreturn_t no_action(int cpl, void *dev
 	return IRQ_NONE;
 }
 
+static int safeirq = 1;
+int __init unsafeirq_setup(char *str)
+{
+	safeirq = 0;
+	printk(KERN_INFO "IRQ safety-line removed - good luck\n");
+	return 1;
+}
+__setup("unsafeirq", unsafeirq_setup);
 /*
  * Have got an event to handle:
  */
 fastcall int handle_IRQ_event(unsigned int irq, struct pt_regs *regs,
-				struct irqaction *action)
+				struct irqaction *actionlist)
 {
 	int ret, retval = 0, status = 0;
+	struct irqaction *action = actionlist;
+	int repeat=0;
 
 	if (!(action->flags & SA_INTERRUPT))
 		local_irq_enable();
 
 	do {
 		ret = action->handler(irq, action->dev_id, regs);
-		if (ret == IRQ_HANDLED)
+		if (ret == IRQ_HANDLED) {
 			status |= action->flags;
+			repeat = 1;
+		}
 		retval |= ret;
 		action = action->next;
+		if (!action &&
+		    repeat &&
+		    safeirq &&
+		    (actionlist->flags & SA_SHIRQ)) {
+			/* at least one handler on the list did something,
+			 * and the interrupt is sharable, so give
+			 * every handler another chance, incase a new event
+			 * came in and is holding the irq line asserted.
+			 */
+			action = actionlist;
+			repeat = 0;
+		}
 	} while (action);
 
 	if (status & SA_SAMPLE_RANDOM)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2.6.16] Shared interrupts sometimes lost
       [not found] <5Zd5E-3vi-7@gated-at.bofh.it>
@ 2006-04-08 14:10 ` Robert Hancock
       [not found] ` <5ZoDL-3rE-7@gated-at.bofh.it>
  1 sibling, 0 replies; 9+ messages in thread
From: Robert Hancock @ 2006-04-08 14:10 UTC (permalink / raw)
  To: linux-kernel; +Cc: Neil Brown

Neil Brown wrote:
>  However there is room for a race here.  If an event occurs between
>  the read and the write, then this will NOT de-assert the IRQ line.
>  It will remain asserted throughout.
> 
>  Now if the IRQ is handled as an edge-triggered line (which I believe
>  they are in Linux), then losing this race will mean that we don't see
>  any more interrupts on this line.

PCI interrupts should always be level triggered, not edge triggered 
(except maybe in a few special cases - non-native-mode PCI IDE maybe? 
and in those cases I don't think the interrupt is considered sharable). 
With a level triggered interrupt the ISR will simply be triggered again 
and the event handled in this case so there is no race. I think this 
patch is going to double interrupt overhead and only covers up some 
other problem.

I think that in cases where the interrupt is edge triggered and is 
shared (for example on ISA cards that support it) the kernel already has 
such logic as you describe.

-- 
Robert Hancock      Saskatoon, SK, Canada
To email, remove "nospam" from hancockr@nospamshaw.ca
Home Page: http://www.roberthancock.com/


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2.6.16] Shared interrupts sometimes lost
  2006-04-08  4:10 [PATCH 2.6.16] Shared interrupts sometimes lost Neil Brown
@ 2006-04-08 16:31 ` Lee Revell
  2006-04-09  6:02   ` Neil Brown
  2006-04-11 17:07 ` Pavel Machek
  1 sibling, 1 reply; 9+ messages in thread
From: Lee Revell @ 2006-04-08 16:31 UTC (permalink / raw)
  To: Neil Brown; +Cc: linux-kernel

On Sat, 2006-04-08 at 14:10 +1000, Neil Brown wrote:
> 
>  To explain what I think is happening, let me start with a very simple
>  case.  A number of PCI devices (this one included) have a number of
>  events which can trigger an interrupt.  The events which are current
>  are presented as bits in a register, and are ORed together (and
>  possibly masked by another register) to make the IRQ line.
>  When 1's are written to any bits in this register, it acknowledges
>  the event and clears the bit.
>  A typical code fragment is 
>    events = read_register(INTERRUPTS);
>    write_register(INTERRUPTS, events);
>    ... handle each 1 bits in events ....
> 

Isn't a more typical IRQ handler:

while (events = read_register(INTERRUPTS) != 0) {
	...handle each bit in events and ACK it...
}

Lee


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2.6.16] Shared interrupts sometimes lost
  2006-04-08 16:31 ` Lee Revell
@ 2006-04-09  6:02   ` Neil Brown
  0 siblings, 0 replies; 9+ messages in thread
From: Neil Brown @ 2006-04-09  6:02 UTC (permalink / raw)
  To: Lee Revell; +Cc: linux-kernel

On Saturday April 8, rlrevell@joe-job.com wrote:
> On Sat, 2006-04-08 at 14:10 +1000, Neil Brown wrote:
> > 
> >  To explain what I think is happening, let me start with a very simple
> >  case.  A number of PCI devices (this one included) have a number of
> >  events which can trigger an interrupt.  The events which are current
> >  are presented as bits in a register, and are ORed together (and
> >  possibly masked by another register) to make the IRQ line.
> >  When 1's are written to any bits in this register, it acknowledges
> >  the event and clears the bit.
> >  A typical code fragment is 
> >    events = read_register(INTERRUPTS);
> >    write_register(INTERRUPTS, events);
> >    ... handle each 1 bits in events ....
> > 
> 
> Isn't a more typical IRQ handler:
> 
> while (events = read_register(INTERRUPTS) != 0) {
> 	...handle each bit in events and ACK it...
> }

Maybe... I admit that I generalised from 2 examples: rt2500 and yenta.
However I don't think it makes a big difference to the problem with
shared interrupts (assuming my analysis is right).

The loop isn't really necessary if you are sure that unserviced
interrupts will be re-asserted (as level-triggered interrupts would
be) (though it may still help performance) and the loop isn't
sufficient if you need to be sure that all events get services (as
there may be more in the shared-chain).

Thanks,
NeilBrown

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2.6.16] Shared interrupts sometimes lost
       [not found] ` <5ZoDL-3rE-7@gated-at.bofh.it>
@ 2006-04-09 18:12   ` Robert Hancock
  2006-04-09 18:24     ` Lee Revell
  0 siblings, 1 reply; 9+ messages in thread
From: Robert Hancock @ 2006-04-09 18:12 UTC (permalink / raw)
  To: linux-kernel; +Cc: Lee Revell

Lee Revell wrote:
> On Sat, 2006-04-08 at 14:10 +1000, Neil Brown wrote:
>>  To explain what I think is happening, let me start with a very simple
>>  case.  A number of PCI devices (this one included) have a number of
>>  events which can trigger an interrupt.  The events which are current
>>  are presented as bits in a register, and are ORed together (and
>>  possibly masked by another register) to make the IRQ line.
>>  When 1's are written to any bits in this register, it acknowledges
>>  the event and clears the bit.
>>  A typical code fragment is 
>>    events = read_register(INTERRUPTS);
>>    write_register(INTERRUPTS, events);
>>    ... handle each 1 bits in events ....
>>
> 
> Isn't a more typical IRQ handler:
> 
> while (events = read_register(INTERRUPTS) != 0) {
> 	...handle each bit in events and ACK it...
> }

That would be less efficient, it would read the register twice or more 
if any events have been set, and reading device registers can be 
expensive. In the unlikely event the event was set while inside the ISR 
the interrupt should be asserted again so there is no need to do this.

-- 
Robert Hancock      Saskatoon, SK, Canada
To email, remove "nospam" from hancockr@nospamshaw.ca
Home Page: http://www.roberthancock.com/


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2.6.16] Shared interrupts sometimes lost
  2006-04-09 18:12   ` Robert Hancock
@ 2006-04-09 18:24     ` Lee Revell
  0 siblings, 0 replies; 9+ messages in thread
From: Lee Revell @ 2006-04-09 18:24 UTC (permalink / raw)
  To: Robert Hancock; +Cc: linux-kernel

On Sun, 2006-04-09 at 12:12 -0600, Robert Hancock wrote:
> Lee Revell wrote: 
> > Isn't a more typical IRQ handler:
> > 
> > while (events = read_register(INTERRUPTS) != 0) {
> >       ...handle each bit in events and ACK it...
> > }
> 
> That would be less efficient, it would read the register twice or more
> if any events have been set, and reading device registers can be 
> expensive. In the unlikely event the event was set while inside the
> ISR the interrupt should be asserted again so there is no need to do
> this. 

OK.  FWIW I am looking at the emu10k1 driver (though I've seen this in
others).  The OSS driver has this comment:

    /*
     ** NOTE :
     ** We do a 'while loop' here cos on certain machines, with both
     ** playback and recording going on at the same time, IRQs will
     ** stop coming in after a while. Checking IPND indeed shows that
     ** there are interrupts pending but the PIC says no IRQs pending.
     ** I suspect that some boards need edge-triggered IRQs but are not
     ** getting that condition if we don't completely clear the IPND
     ** (make sure no more interrupts are pending).
     ** - Eric
     */

The ALSA driver preserves the while loop but omits the comment :-/

Lee




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2.6.16] Shared interrupts sometimes lost
  2006-04-08  4:10 [PATCH 2.6.16] Shared interrupts sometimes lost Neil Brown
  2006-04-08 16:31 ` Lee Revell
@ 2006-04-11 17:07 ` Pavel Machek
  2006-04-12  0:01   ` Neil Brown
  1 sibling, 1 reply; 9+ messages in thread
From: Pavel Machek @ 2006-04-11 17:07 UTC (permalink / raw)
  To: Neil Brown; +Cc: linux-kernel

Hi!

>  This is my first real introduction to the IRQ handling code in Linux,
>  so please forgive any little errors.  I'm fairly sure the big picture
>  is right, partly because the patch helps so much.
> 
>  To explain what I think is happening, let me start with a very simple
>  case.  A number of PCI devices (this one included) have a number of
>  events which can trigger an interrupt.  The events which are current
>  are presented as bits in a register, and are ORed together (and
>  possibly masked by another register) to make the IRQ line.
>  When 1's are written to any bits in this register, it acknowledges
>  the event and clears the bit.
>  A typical code fragment is 
>    events = read_register(INTERRUPTS);
>    write_register(INTERRUPTS, events);
>    ... handle each 1 bits in events ....
> 
>  This would normally clear all pending events and cause the interrupt
>  line to go low (or at least to not be asserted).
> 
>  However there is room for a race here.  If an event occurs between
>  the read and the write, then this will NOT de-assert the IRQ line.
>  It will remain asserted throughout.
> 
>  Now if the IRQ is handled as an edge-triggered line (which I believe
>  they are in Linux), then losing this race will mean that we don't see
>  any more interrupts on this line.

I believe that

a) any shared interrupts should be level-triggered. It is not okay to
share edge-triggered interrupt

b) your patch does not fix that issue. It only makes race window
smaller.

>  	if (!(action->flags & SA_INTERRUPT))
>  		local_irq_enable();
>  
>  	do {
>  		ret = action->handler(irq, action->dev_id, regs);
> -		if (ret == IRQ_HANDLED)
> +		if (ret == IRQ_HANDLED) {
>  			status |= action->flags;
> +			repeat = 1;
> +		}
>  		retval |= ret;
>  		action = action->next;
> +		if (!action &&
> +		    repeat &&
> +		    safeirq &&
> +		    (actionlist->flags & SA_SHIRQ)) {
> +			/* at least one handler on the list did something,
> +			 * and the interrupt is sharable, so give
> +			 * every handler another chance, incase a new event
> +			 * came in and is holding the irq line asserted.
> +			 */
> +			action = actionlist;
> +			repeat = 0;
> +		}
>  	} while (action);

I think it is still racy. What if another interrupt comes here?

>  	if (status & SA_SAMPLE_RANDOM)

								Pavel
-- 
Thanks for all the (sleeping) penguins.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2.6.16] Shared interrupts sometimes lost
  2006-04-11 17:07 ` Pavel Machek
@ 2006-04-12  0:01   ` Neil Brown
  2006-04-13  5:41     ` Pavel Machek
  0 siblings, 1 reply; 9+ messages in thread
From: Neil Brown @ 2006-04-12  0:01 UTC (permalink / raw)
  To: Pavel Machek; +Cc: linux-kernel

On Tuesday April 11, pavel@suse.cz wrote:
> > 
> >  Now if the IRQ is handled as an edge-triggered line (which I believe
> >  they are in Linux), then losing this race will mean that we don't see
> >  any more interrupts on this line.
> 
> I believe that
> 
> a) any shared interrupts should be level-triggered. It is not okay to
> share edge-triggered interrupt

I see that now, thanks.

> 
> b) your patch does not fix that issue. It only makes race window
> smaller.
> 
> >  	if (!(action->flags & SA_INTERRUPT))
> >  		local_irq_enable();
> >  
> >  	do {
> >  		ret = action->handler(irq, action->dev_id, regs);
> > -		if (ret == IRQ_HANDLED)
> > +		if (ret == IRQ_HANDLED) {
> >  			status |= action->flags;
> > +			repeat = 1;
> > +		}
> >  		retval |= ret;
> >  		action = action->next;
> > +		if (!action &&
> > +		    repeat &&
> > +		    safeirq &&
> > +		    (actionlist->flags & SA_SHIRQ)) {
> > +			/* at least one handler on the list did something,
> > +			 * and the interrupt is sharable, so give
> > +			 * every handler another chance, incase a new event
> > +			 * came in and is holding the irq line asserted.
> > +			 */
> > +			action = actionlist;
> > +			repeat = 0;
> > +		}
> >  	} while (action);
> 
> I think it is still racy. What if another interrupt comes here?

Well... at this point we are certain that the irq line was low at some
point since the loop was first entered, otherwise we would not have
been able to pass through all actions without getting a single
IRQ_HANDLED.
So if an interrupt happens now, it will generate an edge.
However I concede that I don't know how the PIC works exactly.  The
interrupt is still disabled in the PIC at this point.  If this means
that the edge will be ignored, then you are right.  If it just means
that the edge will be "delayed" and we will see the interrupt when
they are re-enabled at the PIC, then we should be safe.


> 
> >  	if (status & SA_SAMPLE_RANDOM)
> 
> 								Pavel
> -- 
> Thanks for all the (sleeping) penguins.

Do you have a gallery somewhere ???

Thanks for your input.

NeilBrown

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2.6.16] Shared interrupts sometimes lost
  2006-04-12  0:01   ` Neil Brown
@ 2006-04-13  5:41     ` Pavel Machek
  0 siblings, 0 replies; 9+ messages in thread
From: Pavel Machek @ 2006-04-13  5:41 UTC (permalink / raw)
  To: Neil Brown; +Cc: linux-kernel

Hi!

> > >  	if (status & SA_SAMPLE_RANDOM)
> > 
> > 								Pavel
> > -- 
> > Thanks for all the (sleeping) penguins.
> 
> Do you have a gallery somewhere ???

http://atrey.karlin.mff.cuni.cz/~pavel/swsusp/

								Pavel
-- 
Thanks, Sharp!

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2006-04-13  5:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-08  4:10 [PATCH 2.6.16] Shared interrupts sometimes lost Neil Brown
2006-04-08 16:31 ` Lee Revell
2006-04-09  6:02   ` Neil Brown
2006-04-11 17:07 ` Pavel Machek
2006-04-12  0:01   ` Neil Brown
2006-04-13  5:41     ` Pavel Machek
     [not found] <5Zd5E-3vi-7@gated-at.bofh.it>
2006-04-08 14:10 ` Robert Hancock
     [not found] ` <5ZoDL-3rE-7@gated-at.bofh.it>
2006-04-09 18:12   ` Robert Hancock
2006-04-09 18:24     ` Lee Revell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox