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 <[email protected]> Closes: https://lore.kernel.org/netdev/[email protected]/ Fixes: 6f8b12d661d0 ("net: napi: add hard irqs deferral feature") Message-ID: <[email protected]> Assisted-by: LLM Tested-by: Congjie Zhou <[email protected]> --- 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
