Skip to contentSkip to conversation

virtio-net: don't re-enable refill work too early when NAPI is disabled

PATCH Superseded Jakub Kicinski 4 participants 6 messages Apr 29, 2025 – Apr 30, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 virtio-net: don't re-enable refill work too early when NAPI is disabled

Commit 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx") fixed a deadlock between reconfig paths and refill work trying to disable the same NAPI instance. The refill work can't run in parallel with reconfig because trying to double-disable a NAPI instance causes a stall under the instance lock, which the reconfig path needs to re-enable the NAPI and therefore unblock the stalled thread.

There are two cases where we re-enable refill too early. One is in the virtnet_set_queues() handler. We call it when installing XDP:

   virtnet_rx_pause_all(vi);
   ...
   virtnet_napi_tx_disable(..);
   ...
   virtnet_set_queues(..);
   ...
   virtnet_rx_resume_all(..);

We want the work to be disabled until we call virtnet_rx_resume_all(), but virtnet_set_queues() kicks it before NAPIs were re-enabled.

The other case is a more trivial case of mis-ordering in __virtnet_rx_resume() found by code inspection.

Fixes 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx")
Fixes 413f0271f396 ("net: protect NAPI enablement with netdev_lock()")

---
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 drivers/net/virtio_net.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +6 -3
index 848fab51dfa1..4c904e176495 100644
@@ -3383,12 +3383,15 @@ static void __virtnet_rx_resume(struct virtnet_info *vi,
 				bool refill)
 {
 	bool running = netif_running(vi->dev);
+	bool schedule_refill = false;
 
 	if (refill && !try_fill_recv(vi, rq, GFP_KERNEL))
-		schedule_delayed_work(&vi->refill, 0);
-
+		schedule_refill = true;
 	if (running)
 		virtnet_napi_enable(rq);
+
+	if (schedule_refill)
+		schedule_delayed_work(&vi->refill, 0);
 }
 
 static void virtnet_rx_resume_all(struct virtnet_info *vi)
@@ -3728,7 +3731,7 @@ static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
 succ:
 	vi->curr_queue_pairs = queue_pairs;
 	/* virtnet_open() will refill when device is going to up. */
-	if (dev->flags & IFF_UP)
+	if (dev->flags & IFF_UP && vi->refill_enabled)
 		schedule_delayed_work(&vi->refill, 0);
 
 	return 0;
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 63 lines · Jakub Kicinski

Commit 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx") fixed a deadlock between reconfig paths and refill work trying to disable the same NAPI instance. The refill work can't run in parallel with reconfig because trying to double-disable a NAPI instance causes a stall under the instance lock, which the reconfig path needs to re-enable the NAPI and therefore unblock the stalled thread.

There are two cases where we re-enable refill too early. One is in the virtnet_set_queues() handler. We call it when installing XDP:

    virtnet_rx_pause_all(vi);
    ...
    virtnet_napi_tx_disable(..);
    ...
    virtnet_set_queues(..);
    ...
    virtnet_rx_resume_all(..);

We want the work to be disabled until we call virtnet_rx_resume_all(), but virtnet_set_queues() kicks it before NAPIs were re-enabled.

The other case is a more trivial case of mis-ordering in __virtnet_rx_resume() found by code inspection.

Fixes 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx")
Fixes 413f0271f396 ("net: protect NAPI enablement with netdev_lock()")

---
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
  drivers/net/virtio_net.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +5 -2
index 848fab51dfa1..4c904e176495 100644
@@ -3383,12 +3383,15 @@ static void __virtnet_rx_resume(struct virtnet_info *vi,
  				bool refill)
  {
  	bool running = netif_running(vi->dev);
+	bool schedule_refill = false;
 
  	if (refill && !try_fill_recv(vi, rq, GFP_KERNEL))
-		schedule_delayed_work(&vi->refill, 0);
-
+		schedule_refill = true;
  	if (running)
  		virtnet_napi_enable(rq);
+
+	if (schedule_refill)
+		schedule_delayed_work(&vi->refill, 0);
  }
 
  static void virtnet_rx_resume_all(struct virtnet_info *vi)

drivers/net/virtio_net.c static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) · line 3737

Quoted 8 lines
drivers/net/virtio_net.c +1 -1
@@ -3728,7 +3731,7 @@ static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  succ:
  	vi->curr_queue_pairs = queue_pairs;
  	/* virtnet_open() will refill when device is going to up. */
-	if (dev->flags & IFF_UP)
+	if (dev->flags & IFF_UP && vi->refill_enabled)
  		schedule_delayed_work(&vi->refill, 0);
 
  	return 0;

Thanks,
Quang Minh.

#2 lore raw
In reply to Jakub Kicinski
Quoted 64 lines · Jakub Kicinski

Commit 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx") fixed a deadlock between reconfig paths and refill work trying to disable the same NAPI instance. The refill work can't run in parallel with reconfig because trying to double-disable a NAPI instance causes a stall under the instance lock, which the reconfig path needs to re-enable the NAPI and therefore unblock the stalled thread.

There are two cases where we re-enable refill too early. One is in the virtnet_set_queues() handler. We call it when installing XDP:

   virtnet_rx_pause_all(vi);
   ...
   virtnet_napi_tx_disable(..);
   ...
   virtnet_set_queues(..);
   ...
   virtnet_rx_resume_all(..);

We want the work to be disabled until we call virtnet_rx_resume_all(), but virtnet_set_queues() kicks it before NAPIs were re-enabled.

The other case is a more trivial case of mis-ordering in __virtnet_rx_resume() found by code inspection.

Fixes 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx")
Fixes 413f0271f396 ("net: protect NAPI enablement with netdev_lock()")

---
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 drivers/net/virtio_net.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +5 -2
index 848fab51dfa1..4c904e176495 100644
@@ -3383,12 +3383,15 @@ static void __virtnet_rx_resume(struct virtnet_info *vi,
                                bool refill)
 {
        bool running = netif_running(vi->dev);
+       bool schedule_refill = false;
 
        if (refill && !try_fill_recv(vi, rq, GFP_KERNEL))
-               schedule_delayed_work(&vi->refill, 0);
-
+               schedule_refill = true;
        if (running)
                virtnet_napi_enable(rq);
+
+       if (schedule_refill)
+               schedule_delayed_work(&vi->refill, 0);
 }
 
 static void virtnet_rx_resume_all(struct virtnet_info *vi)

drivers/net/virtio_net.c static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) · line 3735

Quoted 6 lines
drivers/net/virtio_net.c +1 -1
@@ -3728,7 +3731,7 @@ static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
 succ:
        vi->curr_queue_pairs = queue_pairs;
        /* virtnet_open() will refill when device is going to up. */
-       if (dev->flags & IFF_UP)
+       if (dev->flags & IFF_UP && vi->refill_enabled)
                schedule_delayed_work(&vi->refill, 0);

This has the assumption that the toggle of the refill_enabled is under RTNL. Though it's true now but it looks to me it's better to protect it against refill_lock.

Thanks

Quoted 5 lines
drivers/net/virtio_net.c +0 -1
        return 0;
--

2.49.0

maintainer Acked-by on v1 1/1
#5 lore raw
In reply to Jakub Kicinski
Quoted 21 lines · Jakub Kicinski

Commit 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx") fixed a deadlock between reconfig paths and refill work trying to disable the same NAPI instance. The refill work can't run in parallel with reconfig because trying to double-disable a NAPI instance causes a stall under the instance lock, which the reconfig path needs to re-enable the NAPI and therefore unblock the stalled thread.

There are two cases where we re-enable refill too early. One is in the virtnet_set_queues() handler. We call it when installing XDP:

   virtnet_rx_pause_all(vi);
   ...
   virtnet_napi_tx_disable(..);
   ...
   virtnet_set_queues(..);
   ...
   virtnet_rx_resume_all(..);

We want the work to be disabled until we call virtnet_rx_resume_all(), but virtnet_set_queues() kicks it before NAPIs were re-enabled.

Quoted 6 lines

The other case is a more trivial case of mis-ordering in __virtnet_rx_resume() found by code inspection.

Fixes 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx")
Fixes 413f0271f396 ("net: protect NAPI enablement with netdev_lock()")
Quoted 46 lines

---
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 drivers/net/virtio_net.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +6 -3
index 848fab51dfa1..4c904e176495 100644
@@ -3383,12 +3383,15 @@ static void __virtnet_rx_resume(struct virtnet_info *vi,
 				bool refill)
 {
 	bool running = netif_running(vi->dev);
+	bool schedule_refill = false;
 
 	if (refill && !try_fill_recv(vi, rq, GFP_KERNEL))
-		schedule_delayed_work(&vi->refill, 0);
-
+		schedule_refill = true;
 	if (running)
 		virtnet_napi_enable(rq);
+
+	if (schedule_refill)
+		schedule_delayed_work(&vi->refill, 0);
 }
 
 static void virtnet_rx_resume_all(struct virtnet_info *vi)
@@ -3728,7 +3731,7 @@ static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
 succ:
 	vi->curr_queue_pairs = queue_pairs;
 	/* virtnet_open() will refill when device is going to up. */
-	if (dev->flags & IFF_UP)
+	if (dev->flags & IFF_UP && vi->refill_enabled)
 		schedule_delayed_work(&vi->refill, 0);
 
 	return 0;
Signature · 1 line
2.49.0
#3 lore raw
In reply to Jason Wang
Quoted 76 lines · Jason Wang
Quoted 64 lines · Jakub Kicinski

Commit 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx") fixed a deadlock between reconfig paths and refill work trying to disable the same NAPI instance. The refill work can't run in parallel with reconfig because trying to double-disable a NAPI instance causes a stall under the instance lock, which the reconfig path needs to re-enable the NAPI and therefore unblock the stalled thread.

There are two cases where we re-enable refill too early. One is in the virtnet_set_queues() handler. We call it when installing XDP:

   virtnet_rx_pause_all(vi);
   ...
   virtnet_napi_tx_disable(..);
   ...
   virtnet_set_queues(..);
   ...
   virtnet_rx_resume_all(..);

We want the work to be disabled until we call virtnet_rx_resume_all(), but virtnet_set_queues() kicks it before NAPIs were re-enabled.

The other case is a more trivial case of mis-ordering in __virtnet_rx_resume() found by code inspection.

Fixes 4bc12818b363 ("virtio-net: disable delayed refill when pausing rx")
Fixes 413f0271f396 ("net: protect NAPI enablement with netdev_lock()")

---
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 drivers/net/virtio_net.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +5 -2
index 848fab51dfa1..4c904e176495 100644
@@ -3383,12 +3383,15 @@ static void __virtnet_rx_resume(struct virtnet_info *vi,
                                bool refill)
 {
        bool running = netif_running(vi->dev);
+       bool schedule_refill = false;
 
        if (refill && !try_fill_recv(vi, rq, GFP_KERNEL))
-               schedule_delayed_work(&vi->refill, 0);
-
+               schedule_refill = true;
        if (running)
                virtnet_napi_enable(rq);
+
+       if (schedule_refill)
+               schedule_delayed_work(&vi->refill, 0);
 }
 
 static void virtnet_rx_resume_all(struct virtnet_info *vi)

drivers/net/virtio_net.c static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) · line 3735

Quoted 6 lines
drivers/net/virtio_net.c +1 -1
@@ -3728,7 +3731,7 @@ static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
 succ:
        vi->curr_queue_pairs = queue_pairs;
        /* virtnet_open() will refill when device is going to up. */
-       if (dev->flags & IFF_UP)
+       if (dev->flags & IFF_UP && vi->refill_enabled)
                schedule_delayed_work(&vi->refill, 0);

This has the assumption that the toggle of the refill_enabled is under RTNL. Though it's true now but it looks to me it's better to protect it against refill_lock.

Quoted 1 line

Thanks

Good point.

Quoted 5 lines
Quoted 5 lines
drivers/net/virtio_net.c +0 -1
        return 0;
--

2.49.0

author maintainer
#4 lore raw
Quoted 10 lines · On Wed, 30 Apr 2025 01:29:06 -0400 Michael S. Tsirkin wrote:
Quoted 10 lines

drivers/net/virtio_net.c static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) · line 3735

Quoted 7 lines
drivers/net/virtio_net.c +1 -1
@@ -3728,7 +3731,7 @@ static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
 succ:
        vi->curr_queue_pairs = queue_pairs;
        /* virtnet_open() will refill when device is going to up. */
-       if (dev->flags & IFF_UP)
+       if (dev->flags & IFF_UP && vi->refill_enabled)
                schedule_delayed_work(&vi->refill, 0);  

This has the assumption that the toggle of the refill_enabled is under RTNL.

Yes, this line of code must be under rtnl_lock to be correct, since it is also checking flags & IFF_UP. I was thinking of moving it to virtnet_restore(), AFAIU that's the only place that needs it. But that's more of a -next change.

Quoted 4 lines
Quoted 2 lines

Though it's true now but it looks to me it's better to protect it against refill_lock.

Good point.

Sure. I'll wrap the check and the call to schedule_.. with the lock.