Skip to contentSkip to conversation

virtio-net: locking in tx napi

DISCUSSION Michael S. Tsirkin 3 participants 5 messages Apr 12, 2021 – Apr 13, 2021

Subsystems (3)
lore
Share & tools
lore

Michael S. Tsirkin

author maintainer
#0 lore raw

I was working on the spurios interrupt problem and I noticed something weird.

static int virtnet_poll_tx(struct napi_struct *napi, int budget)

{       
        struct send_queue *sq = container_of(napi, struct send_queue, napi);
        struct virtnet_info *vi = sq->vq->vdev->priv;
        unsigned int index = vq2txq(sq->vq);
        struct netdev_queue *txq;

        if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
                /* We don't need to enable cb for XDP */
                napi_complete_done(napi, 0);
                return 0;
        }

        txq = netdev_get_tx_queue(vi->dev, index);
        __netif_tx_lock(txq, raw_smp_processor_id());
        free_old_xmit_skbs(sq, true);
        __netif_tx_unlock(txq);
        
        virtqueue_napi_complete(napi, sq->vq, 0);
        
        if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
                netif_tx_wake_queue(txq);
        
        return 0;
}       

So virtqueue_napi_complete is called when txq is not locked, thinkably start_xmit can happen right?

Now virtqueue_napi_complete

static void virtqueue_napi_complete(struct napi_struct *napi,
struct virtqueue *vq, int processed)

{
        int opaque;

        opaque = virtqueue_enable_cb_prepare(vq);
        if (napi_complete_done(napi, processed)) {
                if (unlikely(virtqueue_poll(vq, opaque)))
                        virtqueue_napi_schedule(napi, vq);
        } else {
                virtqueue_disable_cb(vq);
        }
}

So it is calling virtqueue_enable_cb_prepare but tx queue could be running and can process things in parallel ... What gives? I suspect this corrupts the ring, and explains why we are seeing weird hangs with vhost packed ring ...

Jason?

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#1 lore raw
Quoted 49 lines · Michael S. Tsirkin

I was working on the spurios interrupt problem and I noticed something weird.

static int virtnet_poll_tx(struct napi_struct *napi, int budget)

{       
        struct send_queue *sq = container_of(napi, struct send_queue, napi);
        struct virtnet_info *vi = sq->vq->vdev->priv;
        unsigned int index = vq2txq(sq->vq);
        struct netdev_queue *txq;

        if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
                /* We don't need to enable cb for XDP */
                napi_complete_done(napi, 0);
                return 0;
        }

        txq = netdev_get_tx_queue(vi->dev, index);
        __netif_tx_lock(txq, raw_smp_processor_id());
        free_old_xmit_skbs(sq, true);
        __netif_tx_unlock(txq);
        
        virtqueue_napi_complete(napi, sq->vq, 0);
        
        if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
                netif_tx_wake_queue(txq);
        
        return 0;
}       

So virtqueue_napi_complete is called when txq is not locked, thinkably start_xmit can happen right?

Now virtqueue_napi_complete

static void virtqueue_napi_complete(struct napi_struct *napi,
struct virtqueue *vq, int processed)

{
        int opaque;

        opaque = virtqueue_enable_cb_prepare(vq);
        if (napi_complete_done(napi, processed)) {
                if (unlikely(virtqueue_poll(vq, opaque)))
                        virtqueue_napi_schedule(napi, vq);
        } else {
                virtqueue_disable_cb(vq);
        }
}
Quoted 10 lines

So it is calling virtqueue_enable_cb_prepare but tx queue could be running and can process things in parallel ... What gives? I suspect this corrupts the ring, and explains why we are seeing weird hangs with vhost packed ring ...

Jason?

Signature · 1 line
MST

and wouldn't the following untested patch make sense then?

drivers/net/virtio_net.c +20 -1
index 82e520d2cb12..c23341b18eb5 100644
@@ -1504,6 +1505,8 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 	struct virtnet_info *vi = sq->vq->vdev->priv;
 	unsigned int index = vq2txq(sq->vq);
 	struct netdev_queue *txq;
+	int opaque;
+	bool done;
 
 	if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
 		/* We don't need to enable cb for XDP */
@@ -1514,9 +1517,26 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 	txq = netdev_get_tx_queue(vi->dev, index);
 	__netif_tx_lock(txq, raw_smp_processor_id());
 	free_old_xmit_skbs(sq, true);
+
+	opaque = virtqueue_enable_cb_prepare(sq->vq);
+
+	done = napi_complete_done(napi, 0);
+
+	if (!done)
+		virtqueue_disable_cb(sq->vq);
+
 	__netif_tx_unlock(txq);
 
-	virtqueue_napi_complete(napi, sq->vq, 0);
+	if (done) {
+		if (unlikely(virtqueue_poll(vq, opaque))) {
+			if (napi_schedule_prep(napi)) {
+				__netif_tx_lock(txq, raw_smp_processor_id());
+				virtqueue_disable_cb(sq->vq);
+				__netif_tx_unlock(txq);
+				__napi_schedule(napi);
+			}
+		}
+	}
 
 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
 		netif_tx_wake_queue(txq);

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Michael S. Tsirkin

I was working on the spurios interrupt problem and I noticed something weird.

static int virtnet_poll_tx(struct napi_struct *napi, int budget)

{
         struct send_queue *sq = container_of(napi, struct send_queue, napi);
         struct virtnet_info *vi = sq->vq->vdev->priv;
         unsigned int index = vq2txq(sq->vq);
         struct netdev_queue *txq;

         if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
                 /* We don't need to enable cb for XDP */
                 napi_complete_done(napi, 0);
                 return 0;
         }

         txq = netdev_get_tx_queue(vi->dev, index);
         __netif_tx_lock(txq, raw_smp_processor_id());
         free_old_xmit_skbs(sq, true);
         __netif_tx_unlock(txq);
         
         virtqueue_napi_complete(napi, sq->vq, 0);
Quoted 8 lines
Quoted 8 lines
         if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
                 netif_tx_wake_queue(txq);
         
         return 0;
}

So virtqueue_napi_complete is called when txq is not locked, thinkably start_xmit can happen right?

Yes, I think so.

Quoted 18 lines
Quoted 18 lines

Now virtqueue_napi_complete

static void virtqueue_napi_complete(struct napi_struct *napi,
struct virtqueue *vq, int processed)

{
         int opaque;

         opaque = virtqueue_enable_cb_prepare(vq);
         if (napi_complete_done(napi, processed)) {
                 if (unlikely(virtqueue_poll(vq, opaque)))
                         virtqueue_napi_schedule(napi, vq);
         } else {
                 virtqueue_disable_cb(vq);
         }
}
Quoted 6 lines
Quoted 6 lines

So it is calling virtqueue_enable_cb_prepare but tx queue could be running and can process things in parallel ... What gives? I suspect this corrupts the ring, and explains why we are seeing weird hangs with vhost packed ring ...

Jason?

It might cause the interrupt to be disabled unexpectedly I think.

Quoted 38 lines
Quoted 4 lines
Signature · 2 lines
-- 
MST

and wouldn't the following untested patch make sense then?

drivers/net/virtio_net.c +13 -1
index 82e520d2cb12..c23341b18eb5 100644
@@ -1504,6 +1505,8 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
  	struct virtnet_info *vi = sq->vq->vdev->priv;
  	unsigned int index = vq2txq(sq->vq);
  	struct netdev_queue *txq;
+	int opaque;
+	bool done;
 
  	if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
  		/* We don't need to enable cb for XDP */
@@ -1514,9 +1517,26 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
  	txq = netdev_get_tx_queue(vi->dev, index);
  	__netif_tx_lock(txq, raw_smp_processor_id());
  	free_old_xmit_skbs(sq, true);
+
+	opaque = virtqueue_enable_cb_prepare(sq->vq);
+
+	done = napi_complete_done(napi, 0);
+
+	if (!done)
+		virtqueue_disable_cb(sq->vq);
+
  	__netif_tx_unlock(txq);
 
-	virtqueue_napi_complete(napi, sq->vq, 0);
+	if (done) {
+		if (unlikely(virtqueue_poll(vq, opaque))) {
+			if (napi_schedule_prep(napi)) {

drivers/net/virtio_net.c static int virtnet_poll_tx(struct napi_struct *napi, int budget) · line 1542

Quoted 10 lines
drivers/net/virtio_net.c +7 -0
+				__netif_tx_lock(txq, raw_smp_processor_id());
+				virtqueue_disable_cb(sq->vq);
+				__netif_tx_unlock(txq);
+				__napi_schedule(napi);
+			}
+		}
+	}
 
  	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  		netif_tx_wake_queue(txq);

I wonder why not simply protect the whole poll_tx with tx lock in this case?

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author maintainer
#3 lore raw
Quoted 114 lines · Jason Wang
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Michael S. Tsirkin

I was working on the spurios interrupt problem and I noticed something weird.

static int virtnet_poll_tx(struct napi_struct *napi, int budget)

{
         struct send_queue *sq = container_of(napi, struct send_queue, napi);
         struct virtnet_info *vi = sq->vq->vdev->priv;
         unsigned int index = vq2txq(sq->vq);
         struct netdev_queue *txq;

         if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
                 /* We don't need to enable cb for XDP */
                 napi_complete_done(napi, 0);
                 return 0;
         }

         txq = netdev_get_tx_queue(vi->dev, index);
         __netif_tx_lock(txq, raw_smp_processor_id());
         free_old_xmit_skbs(sq, true);
         __netif_tx_unlock(txq);
         virtqueue_napi_complete(napi, sq->vq, 0);
         if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
                 netif_tx_wake_queue(txq);
         return 0;
}
Quoted 2 lines
Quoted 2 lines

So virtqueue_napi_complete is called when txq is not locked, thinkably start_xmit can happen right?

Yes, I think so.

Quoted 18 lines
Quoted 18 lines

Now virtqueue_napi_complete

static void virtqueue_napi_complete(struct napi_struct *napi,
struct virtqueue *vq, int processed)

{
         int opaque;

         opaque = virtqueue_enable_cb_prepare(vq);
         if (napi_complete_done(napi, processed)) {
                 if (unlikely(virtqueue_poll(vq, opaque)))
                         virtqueue_napi_schedule(napi, vq);
         } else {
                 virtqueue_disable_cb(vq);
         }
}
Quoted 6 lines
Quoted 6 lines

So it is calling virtqueue_enable_cb_prepare but tx queue could be running and can process things in parallel ... What gives? I suspect this corrupts the ring, and explains why we are seeing weird hangs with vhost packed ring ...

Jason?

It might cause the interrupt to be disabled unexpectedly I think.

Quoted 35 lines
Quoted 4 lines
Signature · 2 lines
-- 
MST

and wouldn't the following untested patch make sense then?

drivers/net/virtio_net.c +12 -1
index 82e520d2cb12..c23341b18eb5 100644
@@ -1504,6 +1505,8 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
  	struct virtnet_info *vi = sq->vq->vdev->priv;
  	unsigned int index = vq2txq(sq->vq);
  	struct netdev_queue *txq;
+	int opaque;
+	bool done;
  	if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
  		/* We don't need to enable cb for XDP */
@@ -1514,9 +1517,26 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
  	txq = netdev_get_tx_queue(vi->dev, index);
  	__netif_tx_lock(txq, raw_smp_processor_id());
  	free_old_xmit_skbs(sq, true);
+
+	opaque = virtqueue_enable_cb_prepare(sq->vq);
+
+	done = napi_complete_done(napi, 0);
+
+	if (!done)
+		virtqueue_disable_cb(sq->vq);
+
  	__netif_tx_unlock(txq);
-	virtqueue_napi_complete(napi, sq->vq, 0);
+	if (done) {
+		if (unlikely(virtqueue_poll(vq, opaque))) {

drivers/net/virtio_net.c static int virtnet_poll_tx(struct napi_struct *napi, int budget) · line 1540

Quoted 10 lines
drivers/net/virtio_net.c +8 -0
+			if (napi_schedule_prep(napi)) {
+				__netif_tx_lock(txq, raw_smp_processor_id());
+				virtqueue_disable_cb(sq->vq);
+				__netif_tx_unlock(txq);
+				__napi_schedule(napi);
+			}
+		}
+	}
  	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  		netif_tx_wake_queue(txq);

I wonder why not simply protect the whole poll_tx with tx lock in this case?

Quoted 2 lines

Thanks

Well it takes __netif_tx_lock internally does it not? Sounds like a deadlock to me ..,

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#4 lore raw
Quoted 31 lines · Michael S. Tsirkin
Quoted 29 lines · Jason Wang
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Michael S. Tsirkin

I was working on the spurios interrupt problem and I noticed something weird.

static int virtnet_poll_tx(struct napi_struct *napi, int budget)

{
         struct send_queue *sq = container_of(napi, struct send_queue, napi);
         struct virtnet_info *vi = sq->vq->vdev->priv;
         unsigned int index = vq2txq(sq->vq);
         struct netdev_queue *txq;

         if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
                 /* We don't need to enable cb for XDP */
                 napi_complete_done(napi, 0);
                 return 0;
         }

         txq = netdev_get_tx_queue(vi->dev, index);
         __netif_tx_lock(txq, raw_smp_processor_id());
         free_old_xmit_skbs(sq, true);
         __netif_tx_unlock(txq);
         virtqueue_napi_complete(napi, sq->vq, 0);
         if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
                 netif_tx_wake_queue(txq);
         return 0;
}
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

So virtqueue_napi_complete is called when txq is not locked, thinkably start_xmit can happen right?

Yes, I think so.

Agreed.

I see I'm quite a few emails behind already. Will respond in more detail to the latest patch series.
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization