* [PATCH net] virtio_net: mask callbacks before handing off NAPI ownership
@ 2026-11-19 17:13 Michael S. Tsirkin
2026-11-23 5:14 ` netdev-bot+sashiko
2026-11-23 18:41 ` Jakub Kicinski
0 siblings, 2 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 17:13 UTC (permalink / raw)
To: linux-kernel
Cc: Congjie Zhou, Jason Wang, Eugenio Pérez, Xuan Zhuo,
Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, netdev, virtualization
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.
Reported-by: Congjie Zhou <zcjie0802@qq.com>
Closes: https://lore.kernel.org/netdev/tencent_D0650F8CA2AAE4C258BFE5BCE805CDB4CA0A@qq.com/
Fixes: 6f8b12d661d0 ("net: napi: add hard irqs deferral feature")
Message-ID: <20261119064843-mutt-send-email-mst@kernel.org>
Assisted-by: LLM
Tested-by: Congjie Zhou <zcjie0802@qq.com>
---
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.
drivers/net/virtio_net.c | 4 ++-
include/linux/netdevice.h | 9 +++++
net/core/dev.c | 69 +++++++++++++++++++++++++++++++++++++--
3 files changed, 78 insertions(+), 4 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index dd2c41ef6fbe7..de370a39bc8fa 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -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;
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index c0ae442c4344b..a182882624d6a 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -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);
diff --git a/net/core/dev.c b/net/core/dev.c
index 3ac700d37e596..fa9baa70e9d72 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -6999,9 +6999,17 @@ void __napi_schedule_irqoff(struct napi_struct *n)
}
EXPORT_SYMBOL(__napi_schedule_irqoff);
-bool napi_complete_done(struct napi_struct *n, int work_done)
+static __always_inline bool
+__napi_complete_done_finish(struct napi_struct *n, unsigned long timeout,
+ bool ret, bool allow_pending,
+ struct napi_completion *completion);
+
+static __always_inline bool
+__napi_complete_done_prepare(struct napi_struct *n, int work_done,
+ bool allow_pending,
+ struct napi_completion *completion)
{
- unsigned long flags, val, new, timeout = 0;
+ unsigned long flags, timeout = 0;
bool ret = true;
/*
@@ -7041,10 +7049,25 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
}
WRITE_ONCE(n->list_owner, -1);
- val = READ_ONCE(n->state);
+ return __napi_complete_done_finish(n, timeout, ret, allow_pending,
+ completion);
+}
+
+static __always_inline bool
+__napi_complete_done_finish(struct napi_struct *n, unsigned long timeout,
+ bool ret, bool allow_pending,
+ struct napi_completion *completion)
+{
+ unsigned long val = READ_ONCE(n->state), new;
+
do {
WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
+ if (allow_pending && (!ret || (val & NAPIF_STATE_MISSED))) {
+ completion->timeout = timeout;
+ return false;
+ }
+
new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
NAPIF_STATE_SCHED_THREADED |
NAPIF_STATE_PREFER_BUSY_POLL);
@@ -7067,8 +7090,48 @@ 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_prepare(n, work_done, false, NULL);
+}
EXPORT_SYMBOL(napi_complete_done);
+/**
+ * napi_complete_done_prepare - prepare NAPI completion
+ * @n: NAPI context
+ * @work_done: number of packets processed
+ * @completion: opaque completion token
+ *
+ * Return: true if NAPI is complete and interrupts may remain enabled;
+ * false if ownership is retained. On false, mask interrupts and call
+ * napi_complete_done_finish().
+ */
+bool napi_complete_done_prepare(struct napi_struct *n, int work_done,
+ struct napi_completion *completion)
+{
+ return __napi_complete_done_prepare(n, work_done, true, completion);
+}
+EXPORT_SYMBOL_GPL(napi_complete_done_prepare);
+
+/**
+ * napi_complete_done_finish - finish NAPI completion after masking interrupts
+ * @n: NAPI context
+ * @completion: token passed to napi_complete_done_prepare()
+ *
+ * The caller must provide any device-specific interrupt ordering.
+ */
+void napi_complete_done_finish(struct napi_struct *n,
+ struct napi_completion *completion)
+{
+ if (unlikely(n->state & (NAPIF_STATE_NPSVC |
+ NAPIF_STATE_IN_BUSY_POLL)))
+ return;
+
+ __napi_complete_done_finish(n, completion->timeout, false, false, NULL);
+}
+EXPORT_SYMBOL_GPL(napi_complete_done_finish);
+
static void __skb_defer_free_flush(struct skb_defer_node *sdn, int budget)
{
struct llist_node *free_list;
--
MST
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net] virtio_net: mask callbacks before handing off NAPI ownership
2026-11-19 17:13 [PATCH net] virtio_net: mask callbacks before handing off NAPI ownership Michael S. Tsirkin
@ 2026-11-23 5:14 ` netdev-bot+sashiko
2026-11-23 18:41 ` Jakub Kicinski
1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 5:14 UTC (permalink / raw)
To: mst
Cc: linux-kernel, zcjie0802, jasowangio, eperezma, xuanzhuo,
andrew+netdev, davem, edumazet, kuba, pabeni, horms, netdev,
virtualization
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] napi_complete_done_finish() (net/core/dev.c:7142-7150) repeats
the `n->state & (NAPIF_STATE_NPSVC | NAPIF_STATE_IN_BUSY_POLL)` test…
- [Low] The commit has no Signed-off-by trailer.
- [Low] The patch adds a second way to complete NAPI
(napi_complete_done_prepare()/napi_complete_done_finish(), exported…
--
virtio_net: mask callbacks before handing off NAPI ownership
This commit splits napi_complete_done() into napi_complete_done_prepare()
and napi_complete_done_finish(). With the split, a false return keeps NAPI
ownership until the caller calls finish. virtio_net's
virtqueue_napi_complete() uses this to disable virtqueue callbacks before
ownership passes to another poll, which avoids an RX stall when IRQ
deferral is in use.
Closes: https://lore.kernel.org/netdev/tencent_D0650F8CA2AAE4C258BFE5BCE805CDB4CA0A@qq.com/
> Reported-by: Congjie Zhou <zcjie0802@qq.com>
> Closes: https://lore.kernel.org/netdev/tencent_D0650F8CA2AAE4C258BFE5BCE805CDB4CA0A@qq.com/
> Fixes: 6f8b12d661d0 ("net: napi: add hard irqs deferral feature")
> Message-ID: <20261119064843-mutt-send-email-mst@kernel.org>
> Assisted-by: LLM
> Tested-by: Congjie Zhou <zcjie0802@qq.com>
[Severity: Low]
This isn't a code issue, but the trailer block has no Signed-off-by.
Documentation/process/coding-assistants.rst says AI agents must not add
Signed-off-by. It also says the human submitter is responsible for
"Adding their own Signed-off-by tag to certify the DCO".
This patch carries Assisted-by: LLM. Could the author's Signed-off-by be
added?
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 6140fd9d11cd0..4be810fb5175e 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
[ ... ]
> @@ -7085,8 +7108,48 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
[ ... ]
> +/**
> + * napi_complete_done_prepare - prepare NAPI completion
> + * @n: NAPI context
> + * @work_done: number of packets processed
> + * @completion: opaque completion token
> + *
> + * Return: true if NAPI is complete and interrupts may remain enabled;
> + * false if ownership is retained. On false, mask interrupts and call
> + * napi_complete_done_finish().
> + */
[Severity: Low]
Should Documentation/networking/napi.rst be updated to cover this API?
It currently says:
napi_complete_done() releases the ownership of the instance.
and:
IRQ should only be unmasked after a successful call to napi_complete_done():
The new API uses a different contract. Interrupts are enabled before
completion. A false return from napi_complete_done_prepare() keeps
ownership. The caller must then call napi_complete_done_finish(), or
NAPIF_STATE_SCHED stays set.
Right now that requirement is only described in this kernel-doc.
> +bool napi_complete_done_prepare(struct napi_struct *n, int work_done,
> + struct napi_completion *completion)
> +{
> + return __napi_complete_done_prepare(n, work_done, true, completion);
> +}
> +EXPORT_SYMBOL_GPL(napi_complete_done_prepare);
> +
> +/**
> + * napi_complete_done_finish - finish NAPI completion after masking interrupts
> + * @n: NAPI context
> + * @completion: token passed to napi_complete_done_prepare()
> + *
> + * The caller must provide any device-specific interrupt ordering.
> + */
> +void napi_complete_done_finish(struct napi_struct *n,
> + struct napi_completion *completion)
> +{
> + if (unlikely(n->state & (NAPIF_STATE_NPSVC |
> + NAPIF_STATE_IN_BUSY_POLL)))
> + return;
> +
> + __napi_complete_done_finish(n, completion->timeout, false, false, NULL);
> +}
[Severity: Medium]
Can this second check of n->state disagree with the decision made in
__napi_complete_done_prepare()?
struct napi_completion only records the timeout. So finish() works out
which path prepare() took by testing NPSVC and IN_BUSY_POLL again. The
early return in __napi_complete_done_prepare() never writes
completion->timeout:
if (unlikely(n->state & (NAPIF_STATE_NPSVC |
NAPIF_STATE_IN_BUSY_POLL)))
return false;
IN_BUSY_POLL is only set by whoever acquires SCHED, so it can't change
while this poll owns the instance.
NPSVC is different. poll_one_napi() sets and clears it while holding only
napi->poll_owner:
net/core/netpoll.c:poll_one_napi() {
...
if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state))
return;
...
clear_bit(NAPI_STATE_NPSVC, &napi->state);
}
A regular poller only takes poll_owner if dev->npinfo was already set
when the poll started:
include/linux/netpoll.h:netpoll_poll_lock() {
...
if (dev && rcu_access_pointer(dev->npinfo)) {
int owner = smp_processor_id();
while (cmpxchg(&napi->poll_owner, -1, owner) != -1)
cpu_relax();
...
}
__netpoll_setup() publishes npinfo with a bare rcu_assign_pointer(). It
does not wait for polls that are already running.
Say netconsole, or a bond/bridge netpoll setup, attaches to a virtio_net
device while a poll is between prepare() and finish(). netpoll_poll_dev()
can then take poll_owner and toggle NPSVC inside that window.
In the first case, prepare() saw NPSVC clear and returned false because of
MISSED or defer_hard_irqs. By then it has already done list_del_init() and
set list_owner to -1, but SCHED is still set. If NPSVC gets set before
finish() runs, finish() returns early:
virtqueue_napi_complete()
napi_complete_done_prepare() <- returns false, SCHED kept
virtqueue_disable_cb(vq)
netpoll sets NPSVC
napi_complete_done_finish() <- returns early
Doesn't this leave SCHED set, callbacks masked, and the NAPI on no poll
list? The queue would stall, and a later napi_disable() in
virtnet_close() would spin forever.
The opposite case also looks wrong. If prepare() saw NPSVC set and took
the early return, the on-stack token in virtqueue_napi_complete() is
never written:
struct napi_completion completion;
Now suppose NPSVC is cleared before finish() runs. Then
__napi_complete_done_finish() runs with an uninitialized
completion->timeout. It clears SCHED and can call hrtimer_start() with
that stack value:
if (timeout)
hrtimer_start(&n->timer, ns_to_ktime(timeout),
HRTIMER_MODE_REL_PINNED);
The old single-pass napi_complete_done() could not hit either case,
because its cmpxchg ran regardless of a late NPSVC change.
The window for concurrent polls during netpoll attach predates this
patch. What is new is that the decision is now split across two separate
reads of n->state.
Could prepare() record its decision in struct napi_completion, or always
initialize the token, and finish() use that instead of re-reading
n->state?
> +EXPORT_SYMBOL_GPL(napi_complete_done_finish);
> +
> static void __skb_defer_free_flush(struct skb_defer_node *sdn, int budget)
> {
> struct llist_node *free_list;
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/ad74886a8429a862c00d1e42f1dc746f2924b75f.1795108341.git.mst%40redhat.com
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net] virtio_net: mask callbacks before handing off NAPI ownership
2026-11-19 17:13 [PATCH net] virtio_net: mask callbacks before handing off NAPI ownership Michael S. Tsirkin
2026-11-23 5:14 ` netdev-bot+sashiko
@ 2026-11-23 18:41 ` Jakub Kicinski
1 sibling, 0 replies; 3+ messages in thread
From: Jakub Kicinski @ 2026-11-23 18:41 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: linux-kernel, Congjie Zhou, Jason Wang, Eugenio Pérez,
Xuan Zhuo, Andrew Lunn, David S. Miller, Eric Dumazet,
Paolo Abeni, Simon Horman, netdev, virtualization
On Thu, 19 Nov 2026 12:13:44 -0500 Michael S. Tsirkin wrote:
> 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.
>
> Reported-by: Congjie Zhou <zcjie0802@qq.com>
> Closes: https://lore.kernel.org/netdev/tencent_D0650F8CA2AAE4C258BFE5BCE805CDB4CA0A@qq.com/
> Fixes: 6f8b12d661d0 ("net: napi: add hard irqs deferral feature")
> Message-ID: <20261119064843-mutt-send-email-mst@kernel.org>
> Assisted-by: LLM
> Tested-by: Congjie Zhou <zcjie0802@qq.com>
Not sure what model you used for this but it looks like shameful
slop. It'd be highly preferable to fix the driver. Highly preferable.
If messing with the core is unavoidable and doesn't cost any perf -
please look at the output of your LLM before you submit a patch :/
--
pw-bot: cr
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-23 18:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 17:13 [PATCH net] virtio_net: mask callbacks before handing off NAPI ownership Michael S. Tsirkin
2026-11-23 5:14 ` netdev-bot+sashiko
2026-11-23 18:41 ` Jakub Kicinski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox