Skip to contentSkip to conversation

8139too: harden against TX ring overflow

PATCH In review Sergei Shtylyov 4 participants 8 messages Apr 5, 2007 – May 24, 2007

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

PATCH 1/1 8139too: harden against TX ring overflow

This driver's 4-packet deep TX queue is too sensible to the "careless" callers ignoring its state (like netpoll in trapped mode), so add "queue full" check at the start of the hard_start_xmit() method (only under #ifndef RTL8139_NDEBUG, otherwise the queue will get stuck once dirty pointer gets out of sync); switch to using appropriate mnemonics for the return values while at it. Also, the out-of-sync dirty pointer check is misplaced in rtl8139_tx_interrupt() which causes TX descriptors to be inspected more than once in case the pointer really gets out-of-sync (and incrementing the dirty pointer always by 4 is just not enough, e.g. KGDBoE managed to stuff 20+ extra buffers into the queue) -- place it before the loop and limit the loop to only look through 4 descriptors at most, so that already overwritten descriptors are just not counted.

---

 drivers/net/8139too.c |   31 ++++++++++++++++++++-----------
 1 files changed, 20 insertions(+), 11 deletions(-)
drivers/net/8139too.c +20 -11
Index: linux-2.6/drivers/net/8139too.c
===================================================================
@@ -90,7 +90,7 @@
 */
 
 #define DRV_NAME	"8139too"
-#define DRV_VERSION	"0.9.28"
+#define DRV_VERSION	"0.9.31"
 
 
 #include <linux/module.h>
@@ -1708,6 +1708,13 @@ static int rtl8139_start_xmit (struct sk
 	unsigned int len = skb->len;
 	unsigned long flags;
 
+#ifndef RTL8139_NDEBUG
+	if (unlikely((tp->cur_tx - tp->dirty_tx) >= NUM_TX_DESC)) {
+		printk(KERN_ERR "%s: TX queue full!\n", dev->name);
+		return NETDEV_TX_BUSY;
+	}
+#endif
+
 	/* Calculate the next Tx descriptor entry. */
 	entry = tp->cur_tx % NUM_TX_DESC;
 
@@ -1720,7 +1727,7 @@ static int rtl8139_start_xmit (struct sk
 	} else {
 		dev_kfree_skb(skb);
 		tp->stats.tx_dropped++;
-		return 0;
+		return NETDEV_TX_OK;
 	}
 
 	spin_lock_irqsave(&tp->lock, flags);
@@ -1740,7 +1747,7 @@ static int rtl8139_start_xmit (struct sk
 		printk (KERN_DEBUG "%s: Queued Tx packet size %u to slot %d.\n",
 			dev->name, len, entry);
 
-	return 0;
+	return NETDEV_TX_OK;
 }
 
 
@@ -1755,6 +1762,16 @@ static void rtl8139_tx_interrupt (struct
 
 	dirty_tx = tp->dirty_tx;
 	tx_left = tp->cur_tx - dirty_tx;
+
+#ifndef RTL8139_NDEBUG
+	if (unlikely(tx_left > NUM_TX_DESC)) {
+		printk(KERN_ERR "%s: Out-of-sync dirty pointer, %ld vs. %ld.\n",
+		       dev->name, dirty_tx, tp->cur_tx);
+		tx_left  = NUM_TX_DESC;
+		dirty_tx = tp->cur_tx - NUM_TX_DESC;
+	}
+#endif /* RTL8139_NDEBUG */
+
 	while (tx_left > 0) {
 		int entry = dirty_tx % NUM_TX_DESC;
 		int txstatus;
@@ -1797,14 +1814,6 @@ static void rtl8139_tx_interrupt (struct
 		tx_left--;
 	}
 
-#ifndef RTL8139_NDEBUG
-	if (tp->cur_tx - dirty_tx > NUM_TX_DESC) {
-		printk (KERN_ERR "%s: Out-of-sync dirty pointer, %ld vs. %ld.\n",
-		        dev->name, dirty_tx, tp->cur_tx);
-		dirty_tx += NUM_TX_DESC;
-	}
-#endif /* RTL8139_NDEBUG */
-
 	/* only wake the queue if we did work, and the queue is stopped */
 	if (tp->dirty_tx != dirty_tx) {
 		tp->dirty_tx = dirty_tx;
#1 lore raw
Quoted 5 lines · Sergei Shtylyov

This driver's 4-packet deep TX queue is too sensible to the "careless" callers ignoring its state (like netpoll in trapped mode), so add "queue full" check at the start of the hard_start_xmit() method (only under #ifndef RTL8139_NDEBUG, otherwise the queue will get stuck once dirty pointer gets out of sync); switch to using appropriate mnemonics for the return values while at it.

Could you please describe this netpoll scenario in more detail? More importantly, why wouldn't we fix netpoll instead?

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw
Quoted 10 lines · Herbert Xu
Quoted 6 lines · Sergei Shtylyov

This driver's 4-packet deep TX queue is too sensible to the "careless" callers ignoring its state (like netpoll in trapped mode), so add "queue full" check at the start of the hard_start_xmit() method (only under #ifndef RTL8139_NDEBUG, otherwise the queue will get stuck once dirty pointer gets out of sync); switch to using appropriate mnemonics for the return values while at it.

Could you please describe this netpoll scenario in more detail? More importantly, why wouldn't we fix netpoll instead?

We're trying to figure out a way of fixing netpoll. Don't know what the solution is yet.

Here is what happens: in KGDB we set netpoll trapped flag. This prevents stopping and starting of a netdev queue. Interfaces that have a small ring (8139) run into a problem because of this. When the ring goes full, it can't stop the queue. This doesn't make sense since in absence of ring descriptors, the device can't transmit any more packets. Sergie had posted one more patch last week that lets us start and stop queues in trapped state.

This patch fixes the 8139 side behavior in this context. -Amit

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

#3 lore raw

Hello.

Quoted 7 lines · Amit S. Kale
Quoted 6 lines
Quoted 6 lines

This driver's 4-packet deep TX queue is too sensible to the "careless" callers ignoring its state (like netpoll in trapped mode), so add "queue full" check at the start of the hard_start_xmit() method (only under #ifndef RTL8139_NDEBUG, otherwise the queue will get stuck once dirty pointer gets out of sync); switch to using appropriate mnemonics for the return values while at it.

Quoted 8 lines
Quoted 2 lines

Could you please describe this netpoll scenario in more detail? More importantly, why wouldn't we fix netpoll instead?

We're trying to figure out a way of fixing netpoll. Don't know what the solution is yet.

Here is what happens: in KGDB we set netpoll trapped flag. This prevents stopping and starting of a netdev queue. Interfaces that have a small ring (8139) run into a problem because of this. When the ring goes full, it can't stop the queue. This doesn't make sense since in absence of ring descriptors, the device can't transmit any more packets. Sergie had posted one more patch last week that lets us start and stop queues in trapped state.

This patch fixes the 8139 side behavior in this context.

Not really -- the patch doesn't seem necessary in this context. It's just
an attempt to make the driver more robust against out-of-sync dirty pointer and posting packets to already stopped queue -- that check constituting the second part is still necessary in certain out-of-sync pointer situation where the queue may erroneously be woken up, i.e. with the difference between the dirty_tx and cur_tx is multiple of 4 by the end of loop (well, I guess that has happened only with netpoll in trapped mode so far). Remember that all these checks are done only when the driver debugging is enabled (it *is* enabled by default though).

Quoted 1 line

-Amit

MBR, Sergei

#4 lore raw
In reply to Sergei Shtylyov
Quoted 12 lines · Hello, I wrote:

This driver's 4-packet deep TX queue is too sensible to the "careless" callers ignoring its state (like netpoll in trapped mode), so add "queue full" check at the start of the hard_start_xmit() method (only under #ifndef RTL8139_NDEBUG, otherwise the queue will get stuck once dirty pointer gets out of sync); switch to using appropriate mnemonics for the return values while at it. Also, the out-of-sync dirty pointer check is misplaced in rtl8139_tx_interrupt() which causes TX descriptors to be inspected more than once in case the pointer really gets out-of-sync (and incrementing the dirty pointer always by 4 is just not enough, e.g. KGDBoE managed to stuff 20+ extra buffers into the queue) -- place it before the loop and limit the loop to only look through 4 descriptors at most, so that already overwritten descriptors are just not counted.

Quoted 1 line

Jeff, do you have any opinion on this patch?

Quoted 68 lines

Index: linux-2.6/drivers/net/8139too.c
===================================================================

drivers/net/8139too.c +20 -4
@@ -90,7 +90,7 @@
 */
 
 #define DRV_NAME	"8139too"
-#define DRV_VERSION	"0.9.28"
+#define DRV_VERSION	"0.9.31"
 
 
 #include <linux/module.h>
@@ -1708,6 +1708,13 @@ static int rtl8139_start_xmit (struct sk
 	unsigned int len = skb->len;
 	unsigned long flags;
 
+#ifndef RTL8139_NDEBUG
+	if (unlikely((tp->cur_tx - tp->dirty_tx) >= NUM_TX_DESC)) {
+		printk(KERN_ERR "%s: TX queue full!\n", dev->name);
+		return NETDEV_TX_BUSY;
+	}
+#endif
+
 	/* Calculate the next Tx descriptor entry. */
 	entry = tp->cur_tx % NUM_TX_DESC;
 
@@ -1720,7 +1727,7 @@ static int rtl8139_start_xmit (struct sk
 	} else {
 		dev_kfree_skb(skb);
 		tp->stats.tx_dropped++;
-		return 0;
+		return NETDEV_TX_OK;
 	}
 
 	spin_lock_irqsave(&tp->lock, flags);
@@ -1740,7 +1747,7 @@ static int rtl8139_start_xmit (struct sk
 		printk (KERN_DEBUG "%s: Queued Tx packet size %u to slot %d.\n",
 			dev->name, len, entry);
 
-	return 0;
+	return NETDEV_TX_OK;
 }
 
 
@@ -1755,6 +1762,16 @@ static void rtl8139_tx_interrupt (struct
 
 	dirty_tx = tp->dirty_tx;
 	tx_left = tp->cur_tx - dirty_tx;
+
+#ifndef RTL8139_NDEBUG
+	if (unlikely(tx_left > NUM_TX_DESC)) {
+		printk(KERN_ERR "%s: Out-of-sync dirty pointer, %ld vs. %ld.\n",
+		       dev->name, dirty_tx, tp->cur_tx);
+		tx_left  = NUM_TX_DESC;
+		dirty_tx = tp->cur_tx - NUM_TX_DESC;
+	}
+#endif /* RTL8139_NDEBUG */
+
 	while (tx_left > 0) {
 		int entry = dirty_tx % NUM_TX_DESC;
 		int txstatus;
@@ -1797,14 +1814,6 @@ static void rtl8139_tx_interrupt (struct
 		tx_left--;
 	}
 
-#ifndef RTL8139_NDEBUG

drivers/net/8139too.c static void rtl8139_tx_interrupt (struct · line 1819

Quoted 10 lines
drivers/net/8139too.c +0 -7
-	if (tp->cur_tx - dirty_tx > NUM_TX_DESC) {
-		printk (KERN_ERR "%s: Out-of-sync dirty pointer, %ld vs. %ld.\n",
-		        dev->name, dirty_tx, tp->cur_tx);
-		dirty_tx += NUM_TX_DESC;
-	}
-#endif /* RTL8139_NDEBUG */
-
 	/* only wake the queue if we did work, and the queue is stopped */
 	if (tp->dirty_tx != dirty_tx) {
 		tp->dirty_tx = dirty_tx;

WBR, Sergei

#5 lore raw
Quoted 13 lines · Sergei Shtylyov
Quoted 11 lines · Hello, I wrote:

This driver's 4-packet deep TX queue is too sensible to the "careless" callers ignoring its state (like netpoll in trapped mode), so add "queue full" check at the start of the hard_start_xmit() method (only under #ifndef RTL8139_NDEBUG, otherwise the queue will get stuck once dirty pointer gets out of sync); switch to using appropriate mnemonics for the return values while at it. Also, the out-of-sync dirty pointer check is misplaced in rtl8139_tx_interrupt() which causes TX descriptors to be inspected more than once in case the pointer really gets out-of-sync (and incrementing the dirty pointer always by 4 is just not enough, e.g. KGDBoE managed to stuff 20+ extra buffers into the queue) -- place it before the loop and limit the loop to only look through 4 descriptors at most, so that already overwritten descriptors are just not counted.

Quoted 3 lines
Quoted 1 line

Jeff, do you have any opinion on this patch?

The out-of-sync dirty pointer check is leftover boilerplate, and not really indicative of anything except for some code to be removed.

As for the other stuff, I would say "fix the caller". We don't need to hack every driver for cases where netpoll is being dumb.

Jeff

#6 lore raw
Quoted 12 lines · Jeff Garzik
Quoted 11 lines
Quoted 11 lines

This driver's 4-packet deep TX queue is too sensible to the "careless" callers ignoring its state (like netpoll in trapped mode), so add "queue full" check at the start of the hard_start_xmit() method (only under #ifndef RTL8139_NDEBUG, otherwise the queue will get stuck once dirty pointer gets out of sync); switch to using appropriate mnemonics for the return values while at it. Also, the out-of-sync dirty pointer check is misplaced in rtl8139_tx_interrupt() which causes TX descriptors to be inspected more than once in case the pointer really gets out-of-sync (and incrementing the dirty pointer always by 4 is just not enough, e.g. KGDBoE managed to stuff 20+ extra buffers into the queue) -- place it before the loop and limit the loop to only look through 4 descriptors at most, so that already overwritten descriptors are just not counted.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Jeff, do you have any opinion on this patch?

The out-of-sync dirty pointer check is leftover boilerplate, and not really indicative of anything except for some code to be removed.

As for the other stuff, I would say "fix the caller". We don't need to hack every driver for cases where netpoll is being dumb.

Caller's been fixed now. So, I read that as reject. :-)

Quoted 1 line

Jeff

WBR, Sergei

#7 lore raw
Quoted 16 lines · Sergei Shtylyov
Quoted 13 lines · Jeff Garzik
Quoted 13 lines
Quoted 13 lines

This driver's 4-packet deep TX queue is too sensible to the "careless" callers ignoring its state (like netpoll in trapped mode), so add "queue full" check at the start of the hard_start_xmit() method (only under #ifndef RTL8139_NDEBUG, otherwise the queue will get stuck once dirty pointer gets out of sync); switch to using appropriate mnemonics for the return values while at it. Also, the out-of-sync dirty pointer check is misplaced in rtl8139_tx_interrupt() which causes TX descriptors to be inspected more than once in case the pointer really gets out-of-sync (and incrementing the dirty pointer always by 4 is just not enough, e.g. KGDBoE managed to stuff 20+ extra buffers into the queue) -- place it before the loop and limit the loop to only look through 4 descriptors at most, so that already overwritten descriptors are just not counted.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Jeff, do you have any opinion on this patch?

The out-of-sync dirty pointer check is leftover boilerplate, and not really indicative of anything except for some code to be removed.

As for the other stuff, I would say "fix the caller". We don't need to hack every driver for cases where netpoll is being dumb.

Caller's been fixed now. So, I read that as reject. :-)

Mostly. You're still welcome to remove the silly boilerplate checks...

And ->hard_start_xmit() return value update(s) were sane.

Jeff