can: virtio: fix NAPI stall and virtqueue use before DRIVER_OK
Subsystems (4)
v2 cover can: virtio: fix NAPI stall and virtqueue use before DRIVER_OK
This series fixes two issues in the virtio CAN driver.
Patch 1 fixes an RX/TX stall when an interrupt arrives after the START command but before NAPI is enabled in virtio_can_open().
Patch 2 calls virtio_device_ready() before the first virtqueue use in probe and restore. Without it, resume hangs if the interface was up.
Patch 1 goes first: without patch 2, restore hangs waiting for the START reply before it reaches that window, so patch 2 alone would expose the stall.
The interrupt handlers could also use virtqueue_napi_schedule() like virtio-net does. I left that for a follow-up cleanup.
Tested with QEMU 8.2 and vhost-device-can:
- With the interface up, suspend/resume (pm_test=devices) hangs without patch 2 and completes with the series.
- With a temporary delay before napi_enable() and continuous traffic, RX interrupts arrive while NAPI is disabled and napi_schedule_prep() fails. With patch 1, NAPI is scheduled right after napi_enable() and the RX poll runs without waiting for another interrupt. vhost-device-can keeps sending interrupts, which hides the stall without patch 1.
- RX traffic and repeated down/up work. No lockdep warnings.
---
Changes in v2:
- Add patch 1 for the NAPI stall reported by sashiko-bot. With patch 2
the restore path can reach it (Michael's question about callbacks
after virtio_device_ready()).
- Patch 2: describe the callbacks, add the hung task trace and
Cc: stable.
- Link to v1: https://patch.msgid.link/[email protected]
---
Seonghyeon Lim (2):
can: virtio: fix RX/TX stall on interrupt before NAPI is enabled
can: virtio: fix virtqueue use before DRIVER_OK
drivers/net/can/virtio_can.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
---
base-commit: ddfe51b0a0f5dc9e415cc2c8ad8db17532dd426b
change-id: 20261111-virtio-can-ready-85cefff06a84
Best regards,
Signature · 1 line
Seonghyeon Lim <[email protected]>
PATCH v2 1/2 can: virtio: fix RX/TX stall on interrupt before NAPI is enabled
virtio_can_open() sends the START command before it enables NAPI. Once the device has started, an RX or TX interrupt can arrive in between. The interrupt handler then disables the virtqueue callback and calls napi_schedule(), which does nothing because NAPI is still disabled. napi_enable() does not schedule a poll either, and only the poll function re-enables the callback, so the queue can stall until the device happens to send another interrupt.
Schedule NAPI once after napi_enable(), as virtio-net does in virtnet_napi_do_enable(). This processes buffers that are already used and re-enables the callback.
--- drivers/net/can/virtio_can.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/net/can/virtio_can.c +13 -0
@@ -116,6 +116,19 @@ static void virtio_can_napi_enable(struct virtio_can_priv *priv)if (!priv->napi_active) {napi_enable (&priv->napi);napi_enable (&priv->napi_tx); + +/* An interrupt that arrived while NAPI was disabled has +* disabled the virtqueue callback but could not schedule +* NAPI, so no further interrupt may come. Schedule NAPI once +* to process pending buffers and re-enable the callback. +*/ +local_bh_disable (); +virtqueue_napi_schedule (&priv->napi, + priv->vqs[VIRTIO_CAN_QUEUE_RX ]); +virtqueue_napi_schedule (&priv->napi_tx, + priv->vqs[VIRTIO_CAN_QUEUE_TX ]); +local_bh_enable (); + priv->napi_active = true; } }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 1/2] can: virtio: fix RX/TX stall on interrupt before NAPI is enabled
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
From: Seonghyeon Lim <[email protected]>
virtio_can_open()sends the START command before it enables NAPI. Once the device has started, an RX or TX interrupt can arrive in between. The interrupt handler then disables the virtqueue callback and callsnapi_schedule(), which does nothing because NAPI is still disabled.napi_enable()does not schedule a poll either, and only the poll function re-enables the callback, so the queue can stall until the device happens to send another interrupt.
Quoted 7 lines
Schedule NAPI once after
napi_enable(), as virtio-net does invirtnet_napi_do_enable(). This processes buffers that are already used and re-enables the callback.Fixes 083082a4e6d8 ("can: virtio: Add virtio CAN driver")Signed-off-by Seonghyeon Lim <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/2 can: virtio: fix virtqueue use before DRIVER_OK
In reply to Seonghyeon Limvirtio_can_probe() and virtio_can_restore() fill the RX virtqueue and kick it before the device is set DRIVER_OK. The virtio specification does not allow the driver to notify the device before DRIVER_OK.
In restore, if the interface was up, virtio_can_open() then sends a START control message and waits for the reply. DRIVER_OK is only set by the virtio core after restore returns, so the device may never handle the request and the wait never completes, which hangs system resume:
task:kworker/u4:5 state:D
Workqueue: async async_run_entry_fn Call Trace: __wait_for_common virtio_can_send_ctrl_msg virtio_can_open virtio_can_restore virtio_device_restore_priv dpm_run_callback device_resume
In probe, an ndo_open() right after register_candev() can likewise send the START message before DRIVER_OK.
Call virtio_device_ready() before the first virtqueue use in both probe and restore, as virtio-net does. Config change callbacks are still deferred by the virtio core until probe or restore returns, and virtqueue callbacks that arrive before NAPI is enabled are handled in virtio_can_napi_enable().
--- drivers/net/can/virtio_can.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/net/can/virtio_can.c +6 -0
@@ -925,6 +925,9 @@ static int virtio_can_probe(struct virtio_device *vdev) err = -ENOMEM ; goto on_failure; } + +virtio_device_ready (vdev); +virtio_can_populate_rx_vq (vdev); err =register_virtio_can_dev (dev);
@@ -979,6 +982,9 @@ static int __maybe_unused virtio_can_restore(struct virtio_device *vdev)virtio_can_del_vq (vdev);return -ENOMEM ; } + +virtio_device_ready (vdev); +virtio_can_populate_rx_vq (vdev);if (netif_running (ndev)) {
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 2/2] can: virtio: fix virtqueue use before DRIVER_OK
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines
From: Seonghyeon Lim <[email protected]>
virtio_can_probe()andvirtio_can_restore()fill the RX virtqueue and kick it before the device is setDRIVER_OK. The virtio specification does not allow the driver to notify the device beforeDRIVER_OK.In restore, if the interface was up,
virtio_can_open()then sends a START control message and waits for the reply.DRIVER_OKis only set by the virtio core after restore returns, so the device may never handle the request and the wait never completes, which hangs system resume:task:kworker/u4:5 state:D
Quoted 8 lines
Workqueue: async async_run_entry_fn Call Trace:[ ... ]
virtio_can_napi_enable().Fixes 083082a4e6d8 ("can: virtio: Add virtio CAN driver")Signed-off-by Seonghyeon Lim <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2