Re: [PATCH] virtio: Release the OF node when unregistering the device
From: Francesco Valla
Date: Sun Nov 29 2026 - 17:47:39 EST
> virtio_device_of_init() acquires the OF node reference once during device
> registration. virtio_dev_remove() currently puts it on each driver unbind,
> although rebinding the driver does not acquire a new reference. Repeated
> unbinds can therefore consume references not owned by the virtio device.
> Conversely, unregistering a device that never bound to a driver leaves
> the reference held.
>
> Move the put to unregister_virtio_device() so it is paired with device
> registration. Save the node pointer before device_unregister(), which
> can free the virtio device, and put it after unregister has completed the
> driver removal.
>
> Fixes: 694a1116b405 ("virtio: Bind virtio device to device-tree node")
> Reported-by: sashiko-bot <sashiko-bot@xxxxxxxxxx>
> Link: https://lore.kernel.org/all/sashiko-outbox-203883@xxxxxxxxxx/
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Yige Jiang <yigejiang86@xxxxxxxxx>
>
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index a8588d7ad109..c8be1a3f479d 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -381,8 +381,6 @@ static void virtio_dev_remove(struct device *_d)
>
> /* Acknowledge the device's existence again. */
> virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
> -
> - of_node_put(dev->dev.of_node);
> }
>
> /*
> @@ -604,10 +602,12 @@ EXPORT_SYMBOL_GPL(is_virtio_device);
>
> void unregister_virtio_device(struct virtio_device *dev)
> {
> + struct device_node *np = dev->dev.of_node;
> int index = dev->index; /* save for after device release */
>
> virtio_debug_device_exit(dev);
> device_unregister(&dev->dev);
> + of_node_put(np);
> ida_free(&virtio_index_ida, index);
> }
> EXPORT_SYMBOL_GPL(unregister_virtio_device);
Bug reproduced and fix tested on QEMU (aarch64) with virtio-mmio.
Reviewed-by: Francesco Valla <francesco@xxxxxxxx>
Tested-by: Francesco Valla <francesco@xxxxxxxx>
Regards,
Francesco
--
Francesco Valla <francesco@xxxxxxxx>