From: Menglong Dong <[email protected]>

For now, XDP_RING_NEED_WAKEUP is not supported properly by the virtio-net
in the tx path for example: we set xsk_set_tx_need_wakeup() in
virtnet_xsk_xmit(), but we didn't call xsk_clear_tx_need_wakeup()
anywhere, which means the user will call send() for every packet.

We call xsk_set_tx_need_wakeup() after virtnet_xsk_xmit_batch() if sq->vq
is empty, as we can't be wakeup by the skb_xmit_done() in this case.
Otherwise, we will clear the wakeup flag.

Race condition is considered for tx path.

Fixes: 89f86675cb03 ("virtio_net: xsk: tx: support xmit xsk buffer")
Signed-off-by: Menglong Dong <[email protected]>
---
 drivers/net/virtio_net.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 4b5b3fa62008..86b5c1ca568c 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -1459,8 +1459,9 @@ static bool virtnet_xsk_xmit(struct send_queue *sq, 
struct xsk_buff_pool *pool,
        struct virtnet_info *vi = sq->vq->vdev->priv;
        struct virtnet_sq_free_stats stats = {};
        struct net_device *dev = vi->dev;
+       int sent, vring_size;
+       bool need_wakeup;
        u64 kicks = 0;
-       int sent;
 
        /* Avoid to wakeup napi meanless, so call __free_old_xmit instead of
         * free_old_xmit().
@@ -1470,8 +1471,29 @@ static bool virtnet_xsk_xmit(struct send_queue *sq, 
struct xsk_buff_pool *pool,
        if (stats.xsk)
                xsk_tx_completed(sq->xsk_pool, stats.xsk);
 
+       vring_size = virtqueue_get_vring_size(sq->vq);
+       need_wakeup = xsk_uses_need_wakeup(pool);
+       /* If the sq->vq is empty, and the tx ring is empty, and the user
+        * submit an entry to the tx ring after virtnet_xsk_xmit_batch() and
+        * before xsk_set_tx_need_wakeup(), we will lose the chance to wake
+        * up the tx napi, so we have to set the need_wakeup flag here.
+        */
+       if (need_wakeup && vring_size == sq->vq->num_free)
+               xsk_set_tx_need_wakeup(pool);
+
        sent = virtnet_xsk_xmit_batch(sq, pool, budget, &kicks);
 
+       if (need_wakeup) {
+               if (vring_size == sq->vq->num_free)
+                       /* we can't wake up by ourself, and it should be done
+                        * by the user.
+                        */
+                       xsk_set_tx_need_wakeup(pool);
+               else
+                       /* we can wake up from skb_xmit_done() */
+                       xsk_clear_tx_need_wakeup(pool);
+       }
+
        if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
                check_sq_full_and_disable(vi, vi->dev, sq);
 
@@ -1489,9 +1511,6 @@ static bool virtnet_xsk_xmit(struct send_queue *sq, 
struct xsk_buff_pool *pool,
        u64_stats_add(&sq->stats.xdp_tx,  sent);
        u64_stats_update_end(&sq->stats.syncp);
 
-       if (xsk_uses_need_wakeup(pool))
-               xsk_set_tx_need_wakeup(pool);
-
        return sent;
 }
 
-- 
2.54.0


Reply via email to