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 <[email protected]>
Link: https://lore.kernel.org/all/[email protected]/
Cc: [email protected]
Signed-off-by: Yige Jiang <[email protected]>
---
AI assistance was used for analysis and patch preparation.

Based on next-20261104 (8b7179210b22).
The same OF node mismatch was independently noted in the linked review.
Compile-tested on 2026-11-05: arm64, GCC 14.2.0, W=1,
drivers/virtio/virtio.o; baseline and patched objects built without warnings.
The code diff is unchanged from that tested version; this refresh adds
the public report attribution and updates the mail headers.
On 2026-11-19 the patch also applies to the identical virtio.c in mainline
4405c18c95d2. No new build, hardware, boot, or KASAN test was performed.

 drivers/virtio/virtio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 
a8588d7ad109f1d65135dc7394b1dd4ae04978f1..c8be1a3f479d6abf1249f5fde66b6c93cb260d0d
 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);

base-commit: 8b7179210b22f8e48a82c5ceacb9d56a9c4cfec4

Reply via email to