Skip to contentSkip to conversation

b44: netpoll locking fix

PATCH In review Francois Romieu 3 participants 5 messages May 29, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 b44: netpoll locking fix

The irq handling thread (b44_interrupt) uses the same lock as the NAPI thread. This change should prevent a deadlock if something interrupts the b44 NAPI thread and tries to printk through netconsole.

---
 drivers/net/b44.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
drivers/net/b44.c +4 -4
index 879a2ff..64d75e4 100644
@@ -853,16 +853,18 @@ static int b44_rx(struct b44 *bp, int budget)
 static int b44_poll(struct net_device *netdev, int *budget)
 {
 	struct b44 *bp = netdev_priv(netdev);
+	unsigned long flags;
 	int done;
 
-	spin_lock_irq(&bp->lock);
+	spin_lock_irqsave(&bp->lock, flags);
 
 	if (bp->istat & (ISTAT_TX | ISTAT_TO)) {
 		/* spin_lock(&bp->tx_lock); */
 		b44_tx(bp);
 		/* spin_unlock(&bp->tx_lock); */
 	}
-	spin_unlock_irq(&bp->lock);
+
+	spin_unlock_irqrestore(&bp->lock, flags);
 
 	done = 1;
 	if (bp->istat & ISTAT_RX) {
@@ -882,8 +884,6 @@ static int b44_poll(struct net_device *netdev, int *budget)
 	}
 
 	if (bp->istat & ISTAT_ERRORS) {
-		unsigned long flags;
-
 		spin_lock_irqsave(&bp->lock, flags);
 		b44_halt(bp);
 		b44_init_rings(bp);
Signature · 1 line
1.5.2
#1 lore raw
Quoted 5 lines · On Tue, 29 May 2007 23:14:58 +0200 Francois Romieu <[email protected]> wrote:

The irq handling thread (b44_interrupt) uses the same lock as the NAPI thread. This change should prevent a deadlock if something interrupts the b44 NAPI thread and tries to printk through netconsole.

Better to just get rid of using the lock as a transmit lock and use netif_tx_lock instead.

drivers/net/b44.c +16 -24
@@ -607,6 +607,7 @@ static void b44_tx(struct b44 *bp)
 {
 	u32 cur, cons;
 
+	netif_tx_lock(bp->dev);
 	cur  = br32(bp, B44_DMATX_STAT) & DMATX_STAT_CDMASK;
 	cur /= sizeof(struct dma_desc);
 
@@ -622,7 +623,7 @@ static void b44_tx(struct b44 *bp)
 				 skb->len,
 				 PCI_DMA_TODEVICE);
 		rp->skb = NULL;
-		dev_kfree_skb_irq(skb);
+		dev_kfree_skb_any(skb);
 	}
 
 	bp->tx_cons = cons;
@@ -631,6 +632,7 @@ static void b44_tx(struct b44 *bp)
 		netif_wake_queue(bp->dev);
 
 	bw32(bp, B44_GPTIMER, 0);
+	netif_tx_unlock(bp->dev);
 }
 
 /* Works like this.  This chip writes a 'struct rx_header" 30 bytes
@@ -855,14 +857,8 @@ static int b44_poll(struct net_device *n
 	struct b44 *bp = netdev_priv(netdev);
 	int done;
 
-	spin_lock_irq(&bp->lock);
-
-	if (bp->istat & (ISTAT_TX | ISTAT_TO)) {
-		/* spin_lock(&bp->tx_lock); */
+	if (bp->istat & (ISTAT_TX | ISTAT_TO))
 		b44_tx(bp);
-		/* spin_unlock(&bp->tx_lock); */
-	}
-	spin_unlock_irq(&bp->lock);
 
 	done = 1;
 	if (bp->istat & ISTAT_RX) {
@@ -970,21 +966,19 @@ static int b44_start_xmit(struct sk_buff
 {
 	struct b44 *bp = netdev_priv(dev);
 	struct sk_buff *bounce_skb;
-	int rc = NETDEV_TX_OK;
 	dma_addr_t mapping;
 	u32 len, entry, ctrl;
 
-	len = skb->len;
-	spin_lock_irq(&bp->lock);
-
-	/* This is a hard error, log it. */
 	if (unlikely(TX_BUFFS_AVAIL(bp) < 1)) {
-		netif_stop_queue(dev);
-		printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n",
-		       dev->name);
-		goto err_out;
+		if (!netif_queue_stopped(dev)) {
+			netif_stop_queue(dev);
+			printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n",
+			       dev->name);
+		}
+		return NETDEV_TX_BUSY;
 	}
 
+	len = skb->len;
 	mapping = pci_map_single(bp->pdev, skb->data, len, PCI_DMA_TODEVICE);
 	if (dma_mapping_error(mapping) || mapping + len > DMA_30BIT_MASK) {
 		/* Chip can't handle DMA to/from >1GB, use bounce buffer */
@@ -1044,16 +1038,14 @@ static int b44_start_xmit(struct sk_buff
 	if (TX_BUFFS_AVAIL(bp) < 1)
 		netif_stop_queue(dev);
 
-	dev->trans_start = jiffies;
-
-out_unlock:
-	spin_unlock_irq(&bp->lock);
+	mmiowb();
 
-	return rc;
+	dev->trans_start = jiffies;
 
+	return NETDEV_TX_OK;
 err_out:
-	rc = NETDEV_TX_BUSY;
-	goto out_unlock;
+	dev_kfree_skb(skb);
+	return NETDEV_TX_OK;
 }
 
 static int b44_change_mtu(struct net_device *dev, int new_mtu)
#2 lore raw

Stephen Hemminger <[email protected]> :
⅜...]

drivers/net/b44.c static void b44_tx(struct b44 *bp) · line 613

Quoted 12 lines

Better to just get rid of using the lock as a transmit lock and use netif_tx_lock instead.

drivers/net/b44.c +1 -0
@@ -607,6 +607,7 @@ static void b44_tx(struct b44 *bp)
 {
 	u32 cur, cons;
 
+	netif_tx_lock(bp->dev);
 	cur  = br32(bp, B44_DMATX_STAT) & DMATX_STAT_CDMASK;
 	cur /= sizeof(struct dma_desc);
 

(damn, you are quick)

I am not completely convinced.

1. netpoll_send_skb (calls netif_tx_trylock(dev))
-> netpoll_poll(np)

      -> poll_napi(np)
         -> np->dev->poll(np->dev, &budget) ( == b44_poll)
            -> b44_tx
               -> netif_tx_lock(bp->dev) *deadlock*

2. Hunk #5 clashes with John Linville's wireless-dev#master

3. Moderately appropriate for 2.6.22-rc (imho, fwiw, etc.)

Signature · 1 line
Ueimor
#3 lore raw
Quoted 17 lines · On Wed, 30 May 2007 00:20:41 +0200 Francois Romieu <[email protected]> wrote:

Stephen Hemminger <[email protected]> :
⅜...]

drivers/net/b44.c static void b44_tx(struct b44 *bp) · line 613

Quoted 12 lines

Better to just get rid of using the lock as a transmit lock and use netif_tx_lock instead.

drivers/net/b44.c +1 -0
@@ -607,6 +607,7 @@ static void b44_tx(struct b44 *bp)
 {
 	u32 cur, cons;
 
+	netif_tx_lock(bp->dev);
 	cur  = br32(bp, B44_DMATX_STAT) & DMATX_STAT_CDMASK;
 	cur /= sizeof(struct dma_desc);
 

(damn, you are quick)

Quoted 8 lines

I am not completely convinced.

1. netpoll_send_skb (calls netif_tx_trylock(dev))
-> netpoll_poll(np)

      -> poll_napi(np)
         -> np->dev->poll(np->dev, &budget) ( == b44_poll)
            -> b44_tx
               -> netif_tx_lock(bp->dev) *deadlock*

Netpoll needs to be fixed. (or scrapped), as is it will break drivers trying to use tx_lock in poll routine. I know sky2 would get borked.

Something like this:

net/core/netpoll.c +10 -9
@@ -250,22 +250,23 @@ static void netpoll_send_skb(struct netp
 		unsigned long flags;
 
 		local_irq_save(flags);
-		if (netif_tx_trylock(dev)) {
-			/* try until next clock tick */
-			for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
-					tries > 0; --tries) {
+		/* try until next clock tick */
+		for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
+		     tries > 0; --tries) {
+			if (netif_tx_trylock(dev)) {
 				if (!netif_queue_stopped(dev))
 					status = dev->hard_start_xmit(skb, dev);
+				netif_tx_unlock(dev);
 
 				if (status == NETDEV_TX_OK)
 					break;
 
-				/* tickle device maybe there is some cleanup */
-				netpoll_poll(np);
-
-				udelay(USEC_PER_POLL);
 			}
-			netif_tx_unlock(dev);
+
+			/* tickle device maybe there is some cleanup */
+			netpoll_poll(np);
+
+			udelay(USEC_PER_POLL);
 		}
 		local_irq_restore(flags);
 	}