[PATCH] virtio_input: clear the virtqueue pointers on freeze

From: Mingbing Zhai

Date: Wed Dec 02 2026 - 20:24:56 EST


virtinput_freeze() tears down the virtqueues with del_vqs() but
leaves vi->evt and vi->sts pointing at the freed memory. If the
resume then fails (virtio_find_vqs() in virtinput_restore() fails),
the driver stays bound and the next freeze or remove uses the freed
queues in virtqueue_detach_unused_buf() and calls del_vqs() a second
time -- on virtio-mmio that also frees the same IRQs again.

Factor the detach and del_vqs() into virtinput_del_vqs(), clear the
pointers after the queues are gone, and make it a no-op once they
are. This mirrors the hwrng (0f7bf47f5a0c) and vsock (2a24ba1694f1)
fixes for a failed restore.

Fixes: 271c865161c5 ("Add virtio-input driver.")
Fixes: 528d92bfc093 ("virtio_input: Improve freeze handling")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Mingbing Zhai <zhaimingbing@xxxxxxxxxxxxxxxxxxxx>
---
drivers/virtio/virtio_input.c | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/drivers/virtio/virtio_input.c b/drivers/virtio/virtio_input.c
index e3bd0b9616f94..9cb1cd51f77bb 100644
--- a/drivers/virtio/virtio_input.c
+++ b/drivers/virtio/virtio_input.c
@@ -344,10 +344,27 @@ static int virtinput_probe(struct virtio_device *vdev)
return err;
}

-static void virtinput_remove(struct virtio_device *vdev)
+static void virtinput_del_vqs(struct virtio_device *vdev)
{
struct virtio_input *vi = vdev->priv;
void *buf;
+
+ /*
+ * After a failed restore the virtqueues are gone while the driver
+ * stays bound, so a later freeze or remove must not touch them.
+ */
+ if (!vi->sts)
+ return;
+
+ while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
+ kfree(buf);
+ vdev->config->del_vqs(vdev);
+ vi->evt = vi->sts = NULL;
+}
+
+static void virtinput_remove(struct virtio_device *vdev)
+{
+ struct virtio_input *vi = vdev->priv;
unsigned long flags;

spin_lock_irqsave(&vi->lock, flags);
@@ -357,9 +374,8 @@ static void virtinput_remove(struct virtio_device *vdev)
/* Callbacks use vi->idev. */
virtio_reset_device(vdev);
input_unregister_device(vi->idev);
- while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
- kfree(buf);
- vdev->config->del_vqs(vdev);
+
+ virtinput_del_vqs(vdev);
kfree(vi);
}

@@ -368,16 +384,13 @@ static int virtinput_freeze(struct virtio_device *vdev)
{
struct virtio_input *vi = vdev->priv;
unsigned long flags;
- void *buf;

spin_lock_irqsave(&vi->lock, flags);
vi->ready = false;
spin_unlock_irqrestore(&vi->lock, flags);

virtio_reset_device(vdev);
- while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
- kfree(buf);
- vdev->config->del_vqs(vdev);
+ virtinput_del_vqs(vdev);
return 0;
}

--
2.43.0