[IRQ]: Fix synchronize_irq races with IRQ handler

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Herbert Xu
Date: Monday, October 22, 2007 - 8:26 pm

On Mon, Oct 22, 2007 at 07:10:05AM +1000, Benjamin Herrenschmidt wrote:

Thanks for the reminder Ben.  Here it is again:

[IRQ]: Fix synchronize_irq races with IRQ handler

As it is some callers of synchronize_irq rely on memory barriers
to provide synchronisation against the IRQ handlers.  For example,
the tg3 driver does

	tp->irq_sync = 1;
	smp_mb();
	synchronize_irq();

and then in the IRQ handler:

	if (!tp->irq_sync)
		netif_rx_schedule(dev, &tp->napi);

Unfortunately memory barriers only work well when they come in
pairs.  Because we don't actually have memory barriers on the
IRQ path, the memory barrier before the synchronize_irq() doesn't
actually protect us.

In particular, synchronize_irq() may return followed by the
result of netif_rx_schedule being made visible.

This patch (mostly written by Linus) fixes this by using spin
locks instead of memory barries on the synchronize_irq() path.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Acked-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>

Cheers,
-- 
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
index 80eab7a..1f31422 100644
--- a/kernel/irq/manage.c
+++ b/kernel/irq/manage.c
@@ -29,12 +29,28 @@
 void synchronize_irq(unsigned int irq)
 {
 	struct irq_desc *desc = irq_desc + irq;
+	unsigned int status;
 
 	if (irq >= NR_IRQS)
 		return;
 
-	while (desc->status & IRQ_INPROGRESS)
-		cpu_relax();
+	do {
+		unsigned long flags;
+
+		/*
+		 * Wait until we're out of the critical section.  This might
+		 * give the wrong answer due to the lack of memory barriers.
+		 */
+		while (desc->status & IRQ_INPROGRESS)
+			cpu_relax();
+
+		/* Ok, that indicated we're done: double-check carefully. */
+		spin_lock_irqsave(&desc->lock, flags);
+		status = desc->status;
+		spin_unlock_irqrestore(&desc->lock, flags);
+
+		/* Oops, that failed? */
+	} while (status & IRQ_INPROGRESS);
 }
 EXPORT_SYMBOL(synchronize_irq);
 
-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Wed Oct 17, 6:25 pm)
Re: [PATCH] synchronize_irq needs a barrier, Andrew Morton, (Wed Oct 17, 6:45 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Wed Oct 17, 6:55 pm)
Re: [PATCH] synchronize_irq needs a barrier, Linus Torvalds, (Wed Oct 17, 7:12 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Wed Oct 17, 7:40 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Wed Oct 17, 7:57 pm)
Re: [PATCH] synchronize_irq needs a barrier, Herbert Xu, (Thu Oct 18, 7:35 am)
Re: [PATCH] synchronize_irq needs a barrier, Herbert Xu, (Thu Oct 18, 7:56 am)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Thu Oct 18, 2:35 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Thu Oct 18, 3:05 pm)
Re: [PATCH] synchronize_irq needs a barrier, Linus Torvalds, (Thu Oct 18, 3:52 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Thu Oct 18, 4:17 pm)
Re: [PATCH] synchronize_irq needs a barrier, Linus Torvalds, (Thu Oct 18, 4:39 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Thu Oct 18, 4:52 pm)
Re: [PATCH] synchronize_irq needs a barrier, Herbert Xu, (Thu Oct 18, 7:32 pm)
Re: [PATCH] synchronize_irq needs a barrier, Nick Piggin, (Thu Oct 18, 7:52 pm)
Re: [PATCH] synchronize_irq needs a barrier, Linus Torvalds, (Thu Oct 18, 7:55 pm)
Re: [PATCH] synchronize_irq needs a barrier, Linus Torvalds, (Thu Oct 18, 8:26 pm)
Re: [PATCH] synchronize_irq needs a barrier, Herbert Xu, (Thu Oct 18, 8:28 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Thu Oct 18, 9:11 pm)
Re: [PATCH] synchronize_irq needs a barrier, Herbert Xu, (Thu Oct 18, 9:20 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Thu Oct 18, 9:26 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Thu Oct 18, 9:29 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Thu Oct 18, 9:35 pm)
Re: [PATCH] synchronize_irq needs a barrier, Herbert Xu, (Thu Oct 18, 9:48 pm)
Re: [PATCH] synchronize_irq needs a barrier, Nick Piggin, (Thu Oct 18, 9:49 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Thu Oct 18, 9:58 pm)
[NET]: Fix possible dev_deactivate race condition, Herbert Xu, (Thu Oct 18, 10:36 pm)
Re: [NET]: Fix possible dev_deactivate race condition, David Miller, (Thu Oct 18, 10:38 pm)
Re: [PATCH] synchronize_irq needs a barrier, Herbert Xu, (Thu Oct 18, 10:53 pm)
Re: [NET]: Fix possible dev_deactivate race condition, Peter Zijlstra, (Fri Oct 19, 12:35 am)
Re: [PATCH] synchronize_irq needs a barrier, Maxim Levitsky, (Fri Oct 19, 7:02 pm)
Re: [PATCH] synchronize_irq needs a barrier, Linus Torvalds, (Fri Oct 19, 7:25 pm)
Re: [PATCH] synchronize_irq needs a barrier, Maxim Levitsky, (Fri Oct 19, 8:10 pm)
Re: [PATCH] synchronize_irq needs a barrier, Herbert Xu, (Fri Oct 19, 8:37 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Fri Oct 19, 8:56 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Fri Oct 19, 9:04 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Fri Oct 19, 9:06 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Fri Oct 19, 9:09 pm)
Re: [PATCH] synchronize_irq needs a barrier, Maxim Levitsky, (Fri Oct 19, 9:24 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Fri Oct 19, 10:04 pm)
Re: [PATCH] synchronize_irq needs a barrier, Maxim Levitsky, (Fri Oct 19, 10:36 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Fri Oct 19, 10:46 pm)
Re: [PATCH] synchronize_irq needs a barrier, Maxim Levitsky, (Fri Oct 19, 11:06 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Fri Oct 19, 11:13 pm)
Re: [PATCH] synchronize_irq needs a barrier, Benjamin Herrenschmidt, (Sun Oct 21, 2:10 pm)
[IRQ]: Fix synchronize_irq races with IRQ handler, Herbert Xu, (Mon Oct 22, 8:26 pm)