Skip to contentSkip to conversation

virtio-net: disable delayed refill when setting up xdp

PATCH In review Bui Quang Minh 2 participants 5 messages Apr 2, 2025 – Apr 4, 2025

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

PATCH 1/1 virtio-net: disable delayed refill when setting up xdp

When setting up XDP for a running interface, we call napi_disable() on the receive queue's napi. In delayed refill_work, it also calls napi_disable() on the receive queue's napi. This can leads to deadlock when napi_disable() is called on an already disabled napi. This commit fixes this by disabling future and cancelling all inflight delayed refill works before calling napi_disabled() in virtnet_xdp_set.

Fixes 4941d472bf95 ("virtio-net: do not reset during XDP set")
---
 drivers/net/virtio_net.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/net/virtio_net.c +12 -0
index 7e4617216a4b..33406d59efe2 100644
@@ -5956,6 +5956,15 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
 	if (!prog && !old_prog)
 		return 0;
 
+	/*
+	 * Make sure refill_work does not run concurrently to
+	 * avoid napi_disable race which leads to deadlock.
+	 */
+	if (netif_running(dev)) {
+		disable_delayed_refill(vi);
+		cancel_delayed_work_sync(&vi->refill);
+	}
+
 	if (prog)
 		bpf_prog_add(prog, vi->max_queue_pairs - 1);
 
@@ -6004,6 +6013,8 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
 			virtnet_napi_tx_enable(&vi->sq[i]);
 		}
 	}
+	if (netif_running(dev))
+		enable_delayed_refill(vi);
 
 	return 0;
 
@@ -6019,6 +6030,7 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
 			virtnet_napi_enable(&vi->rq[i]);
 			virtnet_napi_tx_enable(&vi->sq[i]);
 		}
+		enable_delayed_refill(vi);
 	}
 	if (prog)
 		bpf_prog_sub(prog, vi->max_queue_pairs - 1);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 18 lines · Bui Quang Minh

When setting up XDP for a running interface, we call napi_disable() on the receive queue's napi. In delayed refill_work, it also calls napi_disable() on the receive queue's napi. This can leads to deadlock when napi_disable() is called on an already disabled napi. This commit fixes this by disabling future and cancelling all inflight delayed refill works before calling napi_disabled() in virtnet_xdp_set.

Fixes 4941d472bf95 ("virtio-net: do not reset during XDP set")
---
 drivers/net/virtio_net.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/net/virtio_net.c +0 -0
index 7e4617216a4b..33406d59efe2 100644

drivers/net/virtio_net.c static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, · line 5965

Quoted 10 lines
drivers/net/virtio_net.c +7 -0
@@ -5956,6 +5956,15 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
 	if (!prog && !old_prog)
 		return 0;
 
+	/*
+	 * Make sure refill_work does not run concurrently to
+	 * avoid napi_disable race which leads to deadlock.
+	 */
+	if (netif_running(dev)) {
+		disable_delayed_refill(vi);
+		cancel_delayed_work_sync(&vi->refill);

AFAICS at this point refill_work() could still be running, why don't you need to call flush_delayed_work()?

@Jason: somewhat related, why virtnet_close() does not use flush_delayed_work(), too?

Thanks,

Paolo

#2 lore raw
Quoted 22 lines · Paolo Abeni
Quoted 21 lines · Bui Quang Minh

When setting up XDP for a running interface, we call napi_disable() on the receive queue's napi. In delayed refill_work, it also calls napi_disable() on the receive queue's napi. This can leads to deadlock when napi_disable() is called on an already disabled napi. This commit fixes this by disabling future and cancelling all inflight delayed refill works before calling napi_disabled() in virtnet_xdp_set.

Fixes 4941d472bf95 ("virtio-net: do not reset during XDP set")
---
  drivers/net/virtio_net.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
drivers/net/virtio_net.c +0 -0
index 7e4617216a4b..33406d59efe2 100644
@@ -5956,6 +5956,15 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  	if (!prog && !old_prog)
  		return 0;
 
Quoted 9 lines

drivers/net/virtio_net.c static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, · line 5965

Quoted 7 lines
drivers/net/virtio_net.c +7 -0
+	/*
+	 * Make sure refill_work does not run concurrently to
+	 * avoid napi_disable race which leads to deadlock.
+	 */
+	if (netif_running(dev)) {
+		disable_delayed_refill(vi);
+		cancel_delayed_work_sync(&vi->refill);

AFAICS at this point refill_work() could still be running, why don't you need to call flush_delayed_work()?

AFAIK, the cancel_delayed_work_sync (this is a synchronous version) provides somewhat stronger guarantee than the flush_delayed_work. Internally, the cancel_delayed_work_sync will also call to __flush_work. The cancel_delayed_work_sync temporarily disables the work before calling __flush_work, so that even if refill_work tries to re-queue itself, that re-queue will fail. As the refill_work can actually re-queue itself, I think we must use cancel_delayed_work_sync here.

Thanks,
Quang Minh.

#3 lore raw
In reply to Bui Quang Minh
Quoted 34 lines · Bui Quang Minh

When setting up XDP for a running interface, we call napi_disable() on the receive queue's napi. In delayed refill_work, it also calls napi_disable() on the receive queue's napi. This can leads to deadlock when napi_disable() is called on an already disabled napi. This commit fixes this by disabling future and cancelling all inflight delayed refill works before calling napi_disabled() in virtnet_xdp_set.

Fixes 4941d472bf95 ("virtio-net: do not reset during XDP set")
---
  drivers/net/virtio_net.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
drivers/net/virtio_net.c +9 -0
index 7e4617216a4b..33406d59efe2 100644
@@ -5956,6 +5956,15 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  	if (!prog && !old_prog)
  		return 0;
 
+	/*
+	 * Make sure refill_work does not run concurrently to
+	 * avoid napi_disable race which leads to deadlock.
+	 */
+	if (netif_running(dev)) {
+		disable_delayed_refill(vi);
+		cancel_delayed_work_sync(&vi->refill);
+	}
+
  	if (prog)
  		bpf_prog_add(prog, vi->max_queue_pairs - 1);
 

drivers/net/virtio_net.c static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, · line 6017

Quoted 5 lines
drivers/net/virtio_net.c +2 -0
@@ -6004,6 +6013,8 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  			virtnet_napi_tx_enable(&vi->sq[i]);
  		}
  	}
+	if (netif_running(dev))
+		enable_delayed_refill(vi);

While doing some testing, it look likes that we must call try_fill_recv to resume the rx path. I'll do more testing and send a new v2 patch.

Quoted 11 lines
drivers/net/virtio_net.c +1 -0
  	return 0;
 
@@ -6019,6 +6030,7 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  			virtnet_napi_enable(&vi->rq[i]);
  			virtnet_napi_tx_enable(&vi->sq[i]);
  		}
+		enable_delayed_refill(vi);
  	}
  	if (prog)
  		bpf_prog_sub(prog, vi->max_queue_pairs - 1);
#4 lore raw
Quoted 32 lines · Bui Quang Minh
Quoted 31 lines · Bui Quang Minh

When setting up XDP for a running interface, we call napi_disable() on the receive queue's napi. In delayed refill_work, it also calls napi_disable() on the receive queue's napi. This can leads to deadlock when napi_disable() is called on an already disabled napi. This commit fixes this by disabling future and cancelling all inflight delayed refill works before calling napi_disabled() in virtnet_xdp_set.

Fixes 4941d472bf95 ("virtio-net: do not reset during XDP set")

---
  drivers/net/virtio_net.c | 12 ++++++++++++

  1 file changed, 12 insertions(+)
drivers/net/virtio_net.c +0 -0
index 7e4617216a4b..33406d59efe2 100644
@@ -5956,6 +5956,15 @@ static int virtnet_xdp_set(struct net_device 
 *dev, struct bpf_prog *prog,
       if (!prog && !old_prog)
           return 0;

  +    /*
+     * Make sure refill_work does not run concurrently to +     * avoid napi_disable race which leads to deadlock. +     */
+    if (netif_running(dev)) {
+        disable_delayed_refill(vi);
+        cancel_delayed_work_sync(&vi->refill);
+    }
+
      if (prog)

Quoted 8 lines
Quoted 7 lines

          bpf_prog_add(prog, vi->max_queue_pairs - 1);
  @@ -6004,6 +6013,8 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, virtnet_napi_tx_enable(&vi->sq[i]);
          }
      }
+    if (netif_running(dev))
+        enable_delayed_refill(vi);

While doing some testing, it look likes that we must call try_fill_recv to resume the rx path. I'll do more testing and send a new v2 patch.

I've sent a new patch here: https://lore.kernel.org/virtualization/[email protected]/T/#u. As the commit title has changed a little bit, I don't use v2 tag.

Thank you,
Quang Minh.