Skip to contentSkip to conversation

virtio_net: RX stall caused by masking callbacks after NAPI ownership release

BUG Superseded Congjie Zhou 5 participants 20 messages Nov 11 – Nov 19

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

Hello,

I observed a persistent virtio-net RX stall on Linux 6.18.58 during a multi-threaded TCP receive workload. After the stall, the RX virtqueue contained unconsumed used entries, but its NAPI instance had no owner, the NAPI timer was inactive and virtqueue notifications were disabled. The receiving TCP threads remained blocked waiting for data.

Completion sequence
------------------------

The completion sequence can produce this interleaving:

old poll successor poll
enable_cb_prepare()
napi_complete_done()
clear SCHED
arm watchdog; result=false
acquire NAPI
poll, enable callbacks, complete
return false to driver
disable_cb() callbacks are now masked again

Root cause
-----------

virtqueue_napi_complete() calls virtqueue_enable_cb_prepare() before napi_complete_done(), then calls virtqueue_disable_cb() if completion returns false.

A false return does not guarantee that the old caller still owns NAPI. The hard IRQ deferral path can clear SCHED, arm the watchdog and return false. Another CPU can acquire NAPI, poll, rearm callbacks and complete before the old caller reaches its final virtqueue_disable_cb(). That stale mask overwrites the notification state established by the new poll.

The watchdog is one-shot. If it has already run, there need not be any remaining owner or timer to recover after the old caller masks the queue. This leaves later used entries without a notification-driven poll.

Current source inspection and local validation
----------------------------------------------

We reproduced the notification-loss condition using a deterministic KUnit fault-injection test on the latest mainline snapshot fetched on November 10, 2026 (7.4-rc2, commit b3dfbde62cf1). The candidate fix passes the same test.

Candidate fix
--------------

I have prepared a candidate change that masks notifications on the relevant completion paths before NAPI ownership can be handed off, rather than masking after a false return. It adds an ordered, non-sleeping, idempotent mask callback to a new completion entry point and uses it in virtio-net. Existing completion callers retain the old API. virtqueue_enable_cb_prepare() remains before ownership release, preserving the ordering required by the earlier RX virtqueue race fix.

#1 lore raw

PATCH 1/2 net: napi: add completion with an ordered IRQ mask callback

napi_complete_done() can clear SCHED, arm the watchdog and return false when deferring hard IRQs. A false return therefore does not guarantee that the caller still owns NAPI. Masking interrupts after this return can overwrite the interrupt state established by a successor poll.

Add napi_complete_done_with_irq_mask() with a mask callback that runs on false-return paths before ownership can be handed off. The callback must be non-sleeping, idempotent and leave NAPI state unchanged, since a state-update retry may invoke it more than once.

Keep napi_complete_done() as a wrapper without a callback, and inline the shared implementation to eliminate the callback path for existing callers.

---
 include/linux/netdevice.h | 18 ++++++++++++++++++
 net/core/dev.c            | 29 +++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)
include/linux/netdevice.h +18 -0
index 435be7880..810527cd8 100644
@@ -601,6 +601,24 @@ static inline void napi_schedule_irqoff(struct napi_struct *n)
  */
 bool napi_complete_done(struct napi_struct *n, int work_done);
 
+/**
+ * napi_complete_done_with_irq_mask - complete with an ordered IRQ mask callback
+ * @n: NAPI context
+ * @work_done: number of packets processed
+ * @mask: non-sleeping, idempotent device interrupt mask callback
+ * @data: callback argument
+ *
+ * Like napi_complete_done(), but call @mask on false-return paths before
+ * releasing NAPI ownership. Drivers which prepare interrupt rearming before
+ * completion must not mask interrupts after ownership has been released.
+ * The callback must not modify NAPI state and may run more than once if the
+ * state update is retried.
+ *
+ * Return: false if device interrupts have been masked by the callback.
+ */
+bool napi_complete_done_with_irq_mask(struct napi_struct *n, int work_done,
+				      void (*mask)(void *data), void *data);
+
 static inline bool napi_complete(struct napi_struct *n)
 {
 	return napi_complete_done(n, 0);
net/core/dev.c +27 -2
index df1a840b0..4ba9e4c1b 100644
@@ -7001,7 +7001,10 @@ void __napi_schedule_irqoff(struct napi_struct *n)
 }
 EXPORT_SYMBOL(__napi_schedule_irqoff);
 
-bool napi_complete_done(struct napi_struct *n, int work_done)
+/* Inline so the legacy API eliminates the optional mask callback path. */
+static __always_inline bool
+__napi_complete_done(struct napi_struct *n, int work_done,
+		     void (*mask)(void *data), void *data)
 {
 	unsigned long flags, val, new, timeout = 0;
 	bool ret = true;
@@ -7013,8 +7016,11 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 	 *    the guarantee we will be called later.
 	 */
 	if (unlikely(n->state & (NAPIF_STATE_NPSVC |
-				 NAPIF_STATE_IN_BUSY_POLL)))
+				 NAPIF_STATE_IN_BUSY_POLL))) {
+		if (mask)
+			mask(data);
 		return false;
+	}
 
 	if (work_done) {
 		if (n->gro.bitmask)
@@ -7047,6 +7053,13 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 	do {
 		WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
 
+		/*
+		 * A false result is not proof of retained ownership. Mask
+		 * before the state transition lets another CPU enter poll.
+		 */
+		if (mask && (!ret || (val & NAPIF_STATE_MISSED)))
+			mask(data);
+
 		new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
 			      NAPIF_STATE_SCHED_THREADED |
 			      NAPIF_STATE_PREFER_BUSY_POLL);
@@ -7069,8 +7082,20 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 			      HRTIMER_MODE_REL_PINNED);
 	return ret;
 }
+
+bool napi_complete_done(struct napi_struct *n, int work_done)
+{
+	return __napi_complete_done(n, work_done, NULL, NULL);
+}
 EXPORT_SYMBOL(napi_complete_done);
 
+bool napi_complete_done_with_irq_mask(struct napi_struct *n, int work_done,
+				      void (*mask)(void *data), void *data)
+{
+	return __napi_complete_done(n, work_done, mask, data);
+}
+EXPORT_SYMBOL(napi_complete_done_with_irq_mask);
+
 static void __skb_defer_free_flush(struct skb_defer_node *sdn, int budget)
 {
 	struct llist_node *free_list;

base-commit: b3dfbde62cf1981c4638939c159dfd087b7a72e9

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH net 1/2] net: napi: add completion with an ordered IRQ mask callback

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

author
#3 lore raw

Re: [PATCH net 1/2] net: napi: add completion with an ordered IRQ mask callback

Hi,

I first encountered this issue on Linux 6.18.58 during a multi-threaded TCP receive workload with virtio-net in KVM guests, and then investigated the NAPI/virtqueue completion ordering.

The observed symptom was a persistent RX stall: the RX virtqueue had unconsumed used entries, but notifications were disabled, NAPI had no owner and its watchdog timer was inactive. The receiving TCP threads remained blocked waiting for data.

A deterministic KUnit fault-injection test also reproduced the notification-loss ordering using the production NAPI completion APIs and watchdog. The test deliberately delays the old caller until a successor poll has rearmed callbacks and completed. The proposed fix passes the same test.

The original bug report describes the symptoms and completion race:
https://lore.kernel.org/netdev/[email protected]/

Thanks,
Congjie Zhou

#4 lore raw

Re: [PATCH net 1/2] net: napi: add completion with an ordered IRQ mask callback

In reply to Congjie Zhou
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

napi_complete_done() can clear SCHED, arm the watchdog and return false when deferring hard IRQs. A false return therefore does not guarantee that the caller still owns NAPI. Masking interrupts after this return can overwrite the interrupt state established by a successor poll.

Add napi_complete_done_with_irq_mask() with a mask callback that runs on false-return paths before ownership can be handed off. The callback must be non-sleeping, idempotent and leave NAPI state unchanged, since a state-update retry may invoke it more than once.

Quoted 5 lines

Keep napi_complete_done() as a wrapper without a callback, and inline the shared implementation to eliminate the callback path for existing callers.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#5 lore raw

PATCH 2/2 virtio_net: mask callbacks before releasing NAPI ownership

In reply to Congjie Zhou

virtqueue_napi_complete() prepares callbacks before calling napi_complete_done(), then disables them if completion returns false. On the hard IRQ deferral path, completion can release NAPI ownership and arm the watchdog before returning false.

A successor poll can rearm callbacks and complete before the old caller executes virtqueue_disable_cb(). The stale mask then disables notifications again. If the one-shot watchdog has already run, no NAPI owner or timer remains to consume later used entries, causing a persistent RX stall.

Use napi_complete_done_with_irq_mask() to disable callbacks before completion can hand NAPI to a successor, and remove the stale mask after a false return. Keep virtqueue_enable_cb_prepare() before completion:
moving it after ownership release would reintroduce the RX virtqueue race fixed by commit cbdadbbf0c79 ("virtio_net: fix race in RX VQ processing").

---
 drivers/net/virtio_net.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +7 -3
index dd2c41ef6..0de6bd95a 100644
@@ -727,19 +727,23 @@ static void virtqueue_napi_schedule(struct napi_struct *napi,
 	}
 }
 
+static void virtqueue_napi_mask(void *data)
+{
+	virtqueue_disable_cb(data);
+}
+
 static bool 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 (napi_complete_done_with_irq_mask(napi, processed,
+					     virtqueue_napi_mask, vq)) {
 		if (unlikely(virtqueue_poll(vq, opaque)))
 			virtqueue_napi_schedule(napi, vq);
 		else
 			return true;
-	} else {
-		virtqueue_disable_cb(vq);
 	}
 
 	return false;
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH net 2/2] virtio_net: mask callbacks before releasing NAPI ownership

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] virtio_net: missing Fixes trailer for virtqueue_napi_complete() race fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] virtio_net: missing Fixes trailer for virtqueue_napi_complete() race fix
--

commit c4014f837fc88f44df9c4728b3bea8a252b5c2fb
Author: Congjie Zhou <[email protected]>

Quoted 13 lines

virtio_net: mask callbacks before releasing NAPI ownership

virtqueue_napi_complete() prepares callbacks before calling napi_complete_done(), then disables them if completion returns false. On the hard IRQ deferral path, completion can release NAPI ownership and arm the watchdog before returning false.

A successor poll can rearm callbacks and complete before the old caller executes virtqueue_disable_cb(). The stale mask then disables notifications again. If the one-shot watchdog has already run, no NAPI owner or timer remains to consume later used entries, causing a persistent RX stall.

Quoted 8 lines

Use napi_complete_done_with_irq_mask() to disable callbacks before completion can hand NAPI to a successor, and remove the stale mask after a false return. Keep virtqueue_enable_cb_prepare() before completion:
moving it after ownership release would reintroduce the RX virtqueue race fixed by commit cbdadbbf0c79 ("virtio_net: fix race in RX VQ processing").

[Severity: Low]
This fixes a persistent RX stall race in virtqueue_napi_complete(). Should it include a Fixes tag for the post-completion virtqueue_disable_cb() call?

Fixes fdaa767aefc1 ("virtio_net: Disable interrupts if napi_complete_done rescheduled napi")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#7 lore raw

Re: [PATCH net 2/2] virtio_net: mask callbacks before releasing NAPI ownership

Quoted 4 lines

Should it include a Fixes tag for the post-completion virtqueue_disable_cb() call?

Fixes fdaa767aefc1 ("virtio_net: Disable interrupts if napi_complete_done rescheduled napi")

Thanks for the review.

fdaa767aefc1 introduced the post-completion virtqueue_disable_cb() call. However, before 6f8b12d661d0, the false-return paths in napi_complete_done() did not clear NAPI_STATE_SCHED.

6f8b12d661d0 added the hard IRQ deferral path that can clear SCHED, arm the watchdog and return false. This allows a successor poll to rearm callbacks before the old caller disables them.

For the watchdog-driven interleaving reported here, I believe the appropriate attribution is:

Fixes 6f8b12d661d0 ("net: napi: add hard irqs deferral feature")

I plan to include this tag in the next revision. The proposed code changes will remain the same.

Thanks,
Congjie Zhou

#8 lore raw

PATCH v2 1/2 net: napi: add completion with an ordered IRQ mask callback

In reply to Congjie Zhou

napi_complete_done() can clear SCHED, arm the watchdog and return false when deferring hard IRQs. A false return therefore does not guarantee that the caller still owns NAPI. Masking interrupts after this return can overwrite the interrupt state established by a successor poll.

Add napi_complete_done_with_irq_mask() with a mask callback that runs on false-return paths before ownership can be handed off. The callback must be non-sleeping, idempotent and leave NAPI state unchanged, since a state-update retry may invoke it more than once.

Keep napi_complete_done() as a wrapper without a callback, and inline the shared implementation to eliminate the callback path for existing callers.

---
Changes in v2:
- Add the Fixes tag to patch 2/2; no code changes in either patch.

v1: https://lore.kernel.org/netdev/[email protected]/
Bug report: https://lore.kernel.org/netdev/[email protected]/
Discovery and validation: https://lore.kernel.org/netdev/[email protected]/

 include/linux/netdevice.h | 18 ++++++++++++++++++
 net/core/dev.c            | 29 +++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)
include/linux/netdevice.h +18 -0
index 435be7880..810527cd8 100644
@@ -601,6 +601,24 @@ static inline void napi_schedule_irqoff(struct napi_struct *n)
  */
 bool napi_complete_done(struct napi_struct *n, int work_done);
 
+/**
+ * napi_complete_done_with_irq_mask - complete with an ordered IRQ mask callback
+ * @n: NAPI context
+ * @work_done: number of packets processed
+ * @mask: non-sleeping, idempotent device interrupt mask callback
+ * @data: callback argument
+ *
+ * Like napi_complete_done(), but call @mask on false-return paths before
+ * releasing NAPI ownership. Drivers which prepare interrupt rearming before
+ * completion must not mask interrupts after ownership has been released.
+ * The callback must not modify NAPI state and may run more than once if the
+ * state update is retried.
+ *
+ * Return: false if device interrupts have been masked by the callback.
+ */
+bool napi_complete_done_with_irq_mask(struct napi_struct *n, int work_done,
+				      void (*mask)(void *data), void *data);
+
 static inline bool napi_complete(struct napi_struct *n)
 {
 	return napi_complete_done(n, 0);
net/core/dev.c +27 -2
index df1a840b0..4ba9e4c1b 100644
@@ -7001,7 +7001,10 @@ void __napi_schedule_irqoff(struct napi_struct *n)
 }
 EXPORT_SYMBOL(__napi_schedule_irqoff);
 
-bool napi_complete_done(struct napi_struct *n, int work_done)
+/* Inline so the legacy API eliminates the optional mask callback path. */
+static __always_inline bool
+__napi_complete_done(struct napi_struct *n, int work_done,
+		     void (*mask)(void *data), void *data)
 {
 	unsigned long flags, val, new, timeout = 0;
 	bool ret = true;
@@ -7013,8 +7016,11 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 	 *    the guarantee we will be called later.
 	 */
 	if (unlikely(n->state & (NAPIF_STATE_NPSVC |
-				 NAPIF_STATE_IN_BUSY_POLL)))
+				 NAPIF_STATE_IN_BUSY_POLL))) {
+		if (mask)
+			mask(data);
 		return false;
+	}
 
 	if (work_done) {
 		if (n->gro.bitmask)
@@ -7047,6 +7053,13 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 	do {
 		WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
 
+		/*
+		 * A false result is not proof of retained ownership. Mask
+		 * before the state transition lets another CPU enter poll.
+		 */
+		if (mask && (!ret || (val & NAPIF_STATE_MISSED)))
+			mask(data);
+
 		new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
 			      NAPIF_STATE_SCHED_THREADED |
 			      NAPIF_STATE_PREFER_BUSY_POLL);
@@ -7069,8 +7082,20 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 			      HRTIMER_MODE_REL_PINNED);
 	return ret;
 }
+
+bool napi_complete_done(struct napi_struct *n, int work_done)
+{
+	return __napi_complete_done(n, work_done, NULL, NULL);
+}
 EXPORT_SYMBOL(napi_complete_done);
 
+bool napi_complete_done_with_irq_mask(struct napi_struct *n, int work_done,
+				      void (*mask)(void *data), void *data)
+{
+	return __napi_complete_done(n, work_done, mask, data);
+}
+EXPORT_SYMBOL(napi_complete_done_with_irq_mask);
+
 static void __skb_defer_free_flush(struct skb_defer_node *sdn, int budget)
 {
 	struct llist_node *free_list;

base-commit: b3dfbde62cf1981c4638939c159dfd087b7a72e9

Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH net v2 1/2] net: napi: add completion with an ordered IRQ mask callback

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

napi_complete_done() can clear SCHED, arm the watchdog and return false when deferring hard IRQs. A false return therefore does not guarantee that the caller still owns NAPI. Masking interrupts after this return can overwrite the interrupt state established by a successor poll.

Add napi_complete_done_with_irq_mask() with a mask callback that runs on false-return paths before ownership can be handed off. The callback must be non-sleeping, idempotent and leave NAPI state unchanged, since a state-update retry may invoke it more than once.

Quoted 5 lines

Keep napi_complete_done() as a wrapper without a callback, and inline the shared implementation to eliminate the callback path for existing callers.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#10 lore raw

Re: [PATCH net v2 1/2] net: napi: add completion with an ordered IRQ mask callback

In reply to Congjie Zhou
Quoted 4 lines · On Sun, 15 Nov 2026 15:12:45 +0800 Congjie Zhou wrote:

napi_complete_done() can clear SCHED, arm the watchdog and return false when deferring hard IRQs. A false return therefore does not guarantee that the caller still owns NAPI. Masking interrupts after this return can overwrite the interrupt state established by a successor poll.

Please stop submitting this LLM slop. You reported the bug, if maintainers have time they will fix it.

#11 lore raw

PATCH v2 2/2 virtio_net: mask callbacks before releasing NAPI ownership

In reply to Congjie Zhou

virtqueue_napi_complete() prepares callbacks before calling napi_complete_done(), then disables them if completion returns false. On the hard IRQ deferral path, completion can release NAPI ownership and arm the watchdog before returning false.

A successor poll can rearm callbacks and complete before the old caller executes virtqueue_disable_cb(). The stale mask then disables notifications again. If the one-shot watchdog has already run, no NAPI owner or timer remains to consume later used entries, causing a persistent RX stall.

Use napi_complete_done_with_irq_mask() to disable callbacks before completion can hand NAPI to a successor, and remove the stale mask after a false return. Keep virtqueue_enable_cb_prepare() before completion:
moving it after ownership release would reintroduce the RX virtqueue race fixed by commit cbdadbbf0c79 ("virtio_net: fix race in RX VQ processing").

Fixes 6f8b12d661d0 ("net: napi: add hard irqs deferral feature")

---
Changes in v2:
- Add the Fixes tag to patch 2/2; no code changes in either patch.

v1: https://lore.kernel.org/netdev/[email protected]/
Bug report: https://lore.kernel.org/netdev/[email protected]/
Discovery and validation: https://lore.kernel.org/netdev/[email protected]/

 drivers/net/virtio_net.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +7 -3
index dd2c41ef6..0de6bd95a 100644
@@ -727,19 +727,23 @@ static void virtqueue_napi_schedule(struct napi_struct *napi,
 	}
 }
 
+static void virtqueue_napi_mask(void *data)
+{
+	virtqueue_disable_cb(data);
+}
+
 static bool 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 (napi_complete_done_with_irq_mask(napi, processed,
+					     virtqueue_napi_mask, vq)) {
 		if (unlikely(virtqueue_poll(vq, opaque)))
 			virtqueue_napi_schedule(napi, vq);
 		else
 			return true;
-	} else {
-		virtqueue_disable_cb(vq);
 	}
 
 	return false;
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH net v2 2/2] virtio_net: mask callbacks before releasing NAPI ownership

Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

virtqueue_napi_complete() prepares callbacks before calling napi_complete_done(), then disables them if completion returns false. On the hard IRQ deferral path, completion can release NAPI ownership and arm the watchdog before returning false.

A successor poll can rearm callbacks and complete before the old caller executes virtqueue_disable_cb(). The stale mask then disables notifications again. If the one-shot watchdog has already run, no NAPI owner or timer remains to consume later used entries, causing a persistent RX stall.

Use napi_complete_done_with_irq_mask() to disable callbacks before completion can hand NAPI to a successor, and remove the stale mask after a false return. Keep virtqueue_enable_cb_prepare() before completion:
moving it after ownership release would reintroduce the RX virtqueue race fixed by commit cbdadbbf0c79 ("virtio_net: fix race in RX VQ processing").

Quoted 2 lines
Fixes 6f8b12d661d0 ("net: napi: add hard irqs deferral feature")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#13 lore raw
In reply to Congjie Zhou
Quoted 53 lines · Congjie Zhou

Hello,

I observed a persistent virtio-net RX stall on Linux 6.18.58 during a multi-threaded TCP receive workload. After the stall, the RX virtqueue contained unconsumed used entries, but its NAPI instance had no owner, the NAPI timer was inactive and virtqueue notifications were disabled. The receiving TCP threads remained blocked waiting for data.

Completion sequence
------------------------

The completion sequence can produce this interleaving:

old poll successor poll
enable_cb_prepare()
napi_complete_done()
clear SCHED
arm watchdog; result=false
acquire NAPI
poll, enable callbacks, complete
return false to driver
disable_cb() callbacks are now masked again

Root cause
-----------

virtqueue_napi_complete() calls virtqueue_enable_cb_prepare() before napi_complete_done(), then calls virtqueue_disable_cb() if completion returns false.

A false return does not guarantee that the old caller still owns NAPI. The hard IRQ deferral path can clear SCHED, arm the watchdog and return false. Another CPU can acquire NAPI, poll, rearm callbacks and complete before the old caller reaches its final virtqueue_disable_cb(). That stale mask overwrites the notification state established by the new poll.

The watchdog is one-shot. If it has already run, there need not be any remaining owner or timer to recover after the old caller masks the queue. This leaves later used entries without a notification-driven poll.

Current source inspection and local validation
----------------------------------------------

We reproduced the notification-loss condition using a deterministic KUnit fault-injection test on the latest mainline snapshot fetched on November 10, 2026 (7.4-rc2, commit b3dfbde62cf1). The candidate fix passes the same test.

Candidate fix
--------------

Quoted 7 lines

I have prepared a candidate change that masks notifications on the relevant completion paths before NAPI ownership can be handed off, rather than masking after a false return. It adds an ordered, non-sleeping, idempotent mask callback to a new completion entry point and uses it in virtio-net. Existing completion callers retain the old API. virtqueue_enable_cb_prepare() remains before ownership release, preserving the ordering required by the earlier RX virtqueue race fix.

so... can we just check for this case? Like this?

drivers/net/virtio_net.c +2 -0
index dd2c41ef6fbe7..41778d727a89a 100644
@@ -740,6 +740,8 @@ static bool virtqueue_napi_complete(struct napi_struct *napi,
 			return true;
 	} else {
 		virtqueue_disable_cb(vq);
+		if (!hrtimer_active(&napi->timer))
+			napi_schedule(napi);
 	}
 
 	return false;
#14 lore raw
In reply to Congjie Zhou
Quoted 7 lines · Congjie Zhou

Hello,

I observed a persistent virtio-net RX stall on Linux 6.18.58 during a multi-threaded TCP receive workload. After the stall, the RX virtqueue contained unconsumed used entries, but its NAPI instance had no owner, the NAPI timer was inactive and virtqueue notifications were disabled. The receiving TCP threads remained blocked waiting for data.

Played with it a bit since. I think the below should do it? Can you confirm?

drivers/net/virtio_net.c +2 -0
index dd2c41ef6fbe7..7bc7306231521 100644
@@ -740,6 +740,8 @@ static bool virtqueue_napi_complete(struct napi_struct *napi,
 			return true;
 	} else {
 		virtqueue_disable_cb(vq);
+		if (!napi_is_deferred(napi) && !napi_is_scheduled(napi))
+			napi_schedule(napi);
 	}
 
 	return false;
include/linux/netdevice.h +16 -0
index c0ae442c4344b..2199750825d4f 100644
@@ -22,6 +22,7 @@
 #define _LINUX_NETDEVICE_H
 
 #include <linux/timer.h>
+#include <linux/hrtimer.h>
 #include <linux/bug.h>
 #include <linux/delay.h>
 #include <linux/atomic.h>
@@ -553,6 +554,21 @@ static inline bool napi_is_scheduled(struct napi_struct *n)
 	return test_bit(NAPI_STATE_SCHED, &n->state);
 }
 
+/**
+ * napi_is_deferred - test if the NAPI timer is armed and will schedule NAPI
+ * @n: NAPI context
+ *
+ * A true answer guarantees NAPI will be polled again without driver
+ * action.  A false answer is "best-effort": the timer can be armed
+ * right after the check.
+ *
+ * Return: True if timer is armed, False otherwise.
+ */
+static inline bool napi_is_deferred(struct napi_struct *n)
+{
+	return hrtimer_active(&n->timer);
+}
+
 bool napi_schedule_prep(struct napi_struct *n);
 
 /**
#15 lore raw

Tried your patch with my earlier TCP test. No more complete NAPI stalls so far, thanks!

But LLM found another possible interleaving, though. I reproduced it in an instrumented KUnit test with injected delays, but haven't hit it in the TCP test.

With defer_hard_irqs=1, it goes like this:

CPU1 / old poll CPU2 / busy-poll
----------------------------- -----------------------------
Complete: clear SCHED,
arm timer, return false.
Take NAPI ownership. Enter busy_poll_stop(), clear IN_BUSY_POLL. The final poll enables callbacks, then pauses before completion.
Watchdog expires, sees
SCHED=1, exits without
scheduling or setting MISSED.
Disable callbacks.
Timer=0, SCHED=1, so skip
napi_schedule() and exit.
napi_complete_done(..., 0):
defer count=0, no MISSED. Clear SCHED, return true. Queue is empty, exit.

Then the device adds a used entry, but callbacks are still disabled, with no timer or poll pending.

So SCHED=1 at the check, but that poll has already enabled callbacks and won't enable them again after the old poll masks them.

#16 lore raw
Quoted 29 lines · Congjie Zhou

Tried your patch with my earlier TCP test. No more complete NAPI stalls so far, thanks!

But LLM found another possible interleaving, though. I reproduced it in an instrumented KUnit test with injected delays, but haven't hit it in the TCP test.

With defer_hard_irqs=1, it goes like this:

CPU1 / old poll CPU2 / busy-poll
----------------------------- -----------------------------
Complete: clear SCHED,
arm timer, return false.
Take NAPI ownership. Enter busy_poll_stop(), clear IN_BUSY_POLL. The final poll enables callbacks, then pauses before completion.
Watchdog expires, sees
SCHED=1, exits without
scheduling or setting MISSED.
Disable callbacks.
Timer=0, SCHED=1, so skip
napi_schedule() and exit.
napi_complete_done(..., 0):
defer count=0, no MISSED. Clear SCHED, return true. Queue is empty, exit.

Quoted 6 lines

Then the device adds a used entry, but callbacks are still disabled, with no timer or poll pending.

So SCHED=1 at the check, but that poll has already enabled callbacks and won't enable them again after the old poll masks them.

Oh good point. Maybe let's just check the poll list. And I guess it's cleaner to add a helper in net core than poke at low level fields like this.

drivers/net/virtio_net.c +2 -0
index dd2c41ef6fbe7..0984f81848174 100644
@@ -740,6 +740,8 @@ static bool virtqueue_napi_complete(struct napi_struct *napi,
 			return true;
 	} else {
 		virtqueue_disable_cb(vq);
+		if (!napi_has_pending_poll(napi))
+			napi_schedule(napi);
 	}
 
 	return false;
include/linux/netdevice.h +16 -0
index c0ae442c4344b..f728499872e8d 100644
@@ -22,6 +22,7 @@
 #define _LINUX_NETDEVICE_H
 
 #include <linux/timer.h>
+#include <linux/hrtimer.h>
 #include <linux/bug.h>
 #include <linux/delay.h>
 #include <linux/atomic.h>
@@ -553,6 +554,21 @@ static inline bool napi_is_scheduled(struct napi_struct *n)
 	return test_bit(NAPI_STATE_SCHED, &n->state);
 }
 
+/**
+ * napi_has_pending_poll - test if NAPI will be polled again
+ * @n: NAPI context
+ *
+ * True if the deferral watchdog is armed or a poll is queued.  A
+ * false answer is "best-effort": a poll can be queued right after
+ * the check.
+ *
+ * Return: True if NAPI will be polled again, False otherwise.
+ */
+static inline bool napi_has_pending_poll(struct napi_struct *n)
+{
+	return hrtimer_active(&n->timer) || !list_empty(&n->poll_list);
+}
+
 bool napi_schedule_prep(struct napi_struct *n);
 
 /**
#17 lore raw
Quoted 1 line

Maybe let's just check the poll list.

Checking the poll list fixes the earlier race where the watchdog has already finished. However, the timer can still be consumed without triggering another poll if it fires after the old caller's check:

old poll / watchdog successor busy-poll
------------------- -------------------
napi_complete_done():
clear SCHED, arm timer,
return false
acquire NAPI enable callbacks, then pause before completion
disable callbacks
napi_has_pending_poll():
timer is still queued,
so skip napi_schedule()
old poll exits
watchdog expires, sees SCHED=1:
no scheduling, no MISSED
watchdog returns
napi_complete_done(..., 0):
clear SCHED, return true
queue is empty, exit

At that point, the next used entry ends up with neither notifications enabled nor a pending timer or scheduled poll to process it.

Stepping back, the fundamental ordering issue here is:

old releases SCHED

    -> successor rearms callbacks
    -> old masks callbacks

Right now, nothing guarantees that the old mask happens before the successor's rearm. Even if the successor still holds SCHED, it might have already done its final rearm. Similarly, having a queued timer doesn't guarantee another poll—the watchdog will simply bail out without scheduling because SCHED is already set. So all these state checks can look completely valid at the time, yet still fail to guarantee recovery.

The root cause is that with the timer introduced by NAPI defer, napi_complete_done() can return false while still giving up SCHED ownership, leaving us with nothing else to synchronize the callback state. Before NAPI defer, returning false meant we still held onto SCHED, so everything worked as expected.

To fix this properly, I think we either need to call virtqueue_disable_cb() before giving up SCHED ownership, or introduce a separate synchronization mechanism (like a lock) to ensure we can always recover NAPI execution. Simple flag checks alone don't seem enough to close this race completely.

#18 lore raw
Quoted 48 lines · Congjie Zhou
Quoted 1 line

Maybe let's just check the poll list.

Checking the poll list fixes the earlier race where the watchdog has already finished. However, the timer can still be consumed without triggering another poll if it fires after the old caller's check:

old poll / watchdog successor busy-poll
------------------- -------------------
napi_complete_done():
clear SCHED, arm timer,
return false
acquire NAPI enable callbacks, then pause before completion
disable callbacks
napi_has_pending_poll():
timer is still queued,
so skip napi_schedule()
old poll exits
watchdog expires, sees SCHED=1:
no scheduling, no MISSED
watchdog returns
napi_complete_done(..., 0):
clear SCHED, return true
queue is empty, exit

At that point, the next used entry ends up with neither notifications enabled nor a pending timer or scheduled poll to process it.

Stepping back, the fundamental ordering issue here is:

old releases SCHED

    -> successor rearms callbacks
    -> old masks callbacks

Right now, nothing guarantees that the old mask happens before the successor's rearm. Even if the successor still holds SCHED, it might have already done its final rearm. Similarly, having a queued timer doesn't guarantee another poll—the watchdog will simply bail out without scheduling because SCHED is already set. So all these state checks can look completely valid at the time, yet still fail to guarantee recovery.

The root cause is that with the timer introduced by NAPI defer, napi_complete_done() can return false while still giving up SCHED ownership, leaving us with nothing else to synchronize the callback state. Before NAPI defer, returning false meant we still held onto SCHED, so everything worked as expected.

Quoted 5 lines

To fix this properly, I think we either need to call virtqueue_disable_cb() before giving up SCHED ownership, or introduce a separate synchronization mechanism (like a lock) to ensure we can always recover NAPI execution. Simple flag checks alone don't seem enough to close this race completely.

Right. So I came up with this then. Pls note i could not repro the problem so I am merely reasoning based on theoretical understanding of it.

--->

virtio_net: mask callbacks before handing off NAPI ownership

Virtio is special in that enabling or disabling callbacks (interrupts) accesses shared virtqueue state and must be serialized with polling. It therefore enables callbacks before completing NAPI, and disables them again if completion returns false.

With napi_complete_done(), that false return can come after ownership has passed to another poll. The first poll can then disable callbacks after its successor has enabled them, leaving RX stalled. Virtio therefore needs to disable callbacks before handing off NAPI ownership.

Add a prepare/finish NAPI completion API so virtio-net can mask interrupts before handing ownership off.

This preserves interrupt suppression without a callback or an additional data-path barrier. Ordinary napi_complete_done() compiles identically in the tested configuration.

Tested on x86-64 KVM with lock debugging, including IRQ deferral, concurrent scheduling, busy polling, netpoll, and threaded NAPI. Split and packed virtqueue TCP RX tests with IRQ deferral and busy polling also passed.

Fixes 6f8b12d661d0 ("net: napi: add hard irqs deferral feature")
Assisted-by LLM

---

drivers/net/virtio_net.c +3 -1
index dd2c41ef6fbe7..de370a39bc8fa 100644
@@ -730,16 +730,18 @@ static void virtqueue_napi_schedule(struct napi_struct *napi,
 static bool virtqueue_napi_complete(struct napi_struct *napi,
 				    struct virtqueue *vq, int processed)
 {
+	struct napi_completion completion;
 	int opaque;
 
 	opaque = virtqueue_enable_cb_prepare(vq);
-	if (napi_complete_done(napi, processed)) {
+	if (napi_complete_done_prepare(napi, processed, &completion)) {
 		if (unlikely(virtqueue_poll(vq, opaque)))
 			virtqueue_napi_schedule(napi, vq);
 		else
 			return true;
 	} else {
 		virtqueue_disable_cb(vq);
+		napi_complete_done_finish(napi, &completion);
 	}
 
 	return false;
include/linux/netdevice.h +9 -0
index c0ae442c4344b..a182882624d6a 100644
@@ -599,6 +599,15 @@ static inline void napi_schedule_irqoff(struct napi_struct *n)
  */
 bool napi_complete_done(struct napi_struct *n, int work_done);
 
+struct napi_completion {
+	unsigned long timeout;
+};
+
+bool napi_complete_done_prepare(struct napi_struct *n, int work_done,
+				struct napi_completion *completion);
+void napi_complete_done_finish(struct napi_struct *n,
+			       struct napi_completion *completion);
+
 static inline bool napi_complete(struct napi_struct *n)
 {
 	return napi_complete_done(n, 0);
net/core/dev.c +66 -3

93 lines not on this page · raw message

#19 lore raw
Quoted 25 lines

Right. So I came up with this then. Pls note i could not repro the problem so I am merely reasoning based on theoretical understanding of it.

--->

virtio_net: mask callbacks before handing off NAPI ownership

Virtio is special in that enabling or disabling callbacks (interrupts) accesses shared virtqueue state and must be serialized with polling. It therefore enables callbacks before completing NAPI, and disables them again if completion returns false.

With napi_complete_done(), that false return can come after ownership has passed to another poll. The first poll can then disable callbacks after its successor has enabled them, leaving RX stalled. Virtio therefore needs to disable callbacks before handing off NAPI ownership.

Add a prepare/finish NAPI completion API so virtio-net can mask interrupts before handing ownership off.

This preserves interrupt suppression without a callback or an additional data-path barrier. Ordinary napi_complete_done() compiles identically in the tested configuration.

Quoted 8 lines

Tested on x86-64 KVM with lock debugging, including IRQ deferral, concurrent scheduling, busy polling, netpoll, and threaded NAPI. Split and packed virtqueue TCP RX tests with IRQ deferral and busy polling also passed.

Fixes 6f8b12d661d0 ("net: napi: add hard irqs deferral feature")
Assisted-by LLM

I've tested this patch locally , and everything looks good now, the issue is gone.