Skip to contentSkip to conversation

virtio: fix virtio_net xmit of freed skb bug

PATCH In review Rusty Russell 1 participant 1 message Jul 15, 2008

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

PATCH 2/5 virtio: fix virtio_net xmit of freed skb bug

[PATCH] virtio_net: Delay dropping tx skbs
Cc: Mark McLoughlin <[email protected]>,
[email protected], [email protected]
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Message-Id: <[email protected]>

From: Mark McLoughlin <[email protected]>

Quoted 6 lines · Rusty Russell

If we fail to transmit a packet, we assume the queue is full and put the skb into last_xmit_skb. However, if more space frees up before we xmit it, we loop, and the result can be transmitting the same skb twice.

Fix is simple: set skb to NULL if we've used it in some way, and check before sending.

...

Quoted 7 lines

diff -r 564237b31993 drivers/net/virtio_net.c

drivers/net/virtio_net.c +0 -0
@@ -287,21 +287,25 @@ again:
 	free_old_xmit_skbs(vi);
 
 	/* If we has a buffer left over from last time, send it now. */

drivers/net/virtio_net.c again: · line 298

Quoted 10 lines
drivers/net/virtio_net.c +2 -1
-	if (vi->last_xmit_skb) {
+	if (unlikely(vi->last_xmit_skb)) {
 		if (xmit_skb(vi, vi->last_xmit_skb) != 0) {
 			/* Drop this skb: we only queue one. */
 			vi->dev->stats.tx_dropped++;
 			kfree_skb(skb);
+			skb = NULL;
 			goto stop_queue;
 		}
 		vi->last_xmit_skb = NULL;

With this, may drop an skb and then later in the function discover that we could have sent it after all. Poor wee skb :)

How about the incremental patch below?

Cheers,
Mark.

Currently we drop the skb in start_xmit() if we have a queued buffer and fail to transmit it.

However, if we delay dropping it until we've stopped the queue and enabled the tx notification callback, then there is a chance space might become available for it.

---
 drivers/net/virtio_net.c |   20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/net/virtio_net.c +10 -10
index d50f4fe..30af05c 100644
@@ -287,16 +287,11 @@ again:
 	free_old_xmit_skbs(vi);
 
 	/* If we has a buffer left over from last time, send it now. */
-	if (unlikely(vi->last_xmit_skb)) {
-		if (xmit_skb(vi, vi->last_xmit_skb) != 0) {
-			/* Drop this skb: we only queue one. */
-			vi->dev->stats.tx_dropped++;
-			kfree_skb(skb);
-			skb = NULL;
-			goto stop_queue;
-		}
-		vi->last_xmit_skb = NULL;
-	}
+	if (unlikely(vi->last_xmit_skb) &&
+	    xmit_skb(vi, vi->last_xmit_skb) != 0)
+		goto stop_queue;
+
+	vi->last_xmit_skb = NULL;
 
 	/* Put new one in send queue and do transmit */
 	if (likely(skb)) {
@@ -322,6 +317,11 @@ stop_queue:
 		netif_start_queue(dev);
 		goto again;
 	}
+	if (skb) {
+		/* Drop this skb: we only queue one. */
+		vi->dev->stats.tx_dropped++;
+		kfree_skb(skb);
+	}
 	goto done;
 }