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>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Herbert Xu and committed by Linus Torvalds a98ce5c6 48d22684

+18 -2
+18 -2
kernel/irq/manage.c
··· 29 29 void synchronize_irq(unsigned int irq) 30 30 { 31 31 struct irq_desc *desc = irq_desc + irq; 32 + unsigned int status; 32 33 33 34 if (irq >= NR_IRQS) 34 35 return; 35 36 36 - while (desc->status & IRQ_INPROGRESS) 37 - cpu_relax(); 37 + do { 38 + unsigned long flags; 39 + 40 + /* 41 + * Wait until we're out of the critical section. This might 42 + * give the wrong answer due to the lack of memory barriers. 43 + */ 44 + while (desc->status & IRQ_INPROGRESS) 45 + cpu_relax(); 46 + 47 + /* Ok, that indicated we're done: double-check carefully. */ 48 + spin_lock_irqsave(&desc->lock, flags); 49 + status = desc->status; 50 + spin_unlock_irqrestore(&desc->lock, flags); 51 + 52 + /* Oops, that failed? */ 53 + } while (status & IRQ_INPROGRESS); 38 54 } 39 55 EXPORT_SYMBOL(synchronize_irq); 40 56