Skip to contentSkip to conversation

virtio: Release the OF node when unregistering the device

PATCH New Yige Jiang 2 participants 2 messages Nov 19

Subsystems (2)
lore
Share & tools
lore

Yige Jiang

author
#0 lore raw

PATCH 1/1 virtio: Release the OF node when unregistering the device

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")

---
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(-)
drivers/virtio/virtio.c +2 -2
index a8588d7ad109f1d65135dc7394b1dd4ae04978f1..c8be1a3f479d6abf1249f5fde66b6c93cb260d0d 100644
@@ -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

#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 12 lines

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.

Quoted 5 lines
Fixes 694a1116b405 ("virtio: Bind virtio device to device-tree node")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1