[PATCH 1/4] remoteproc: virtio: Decouple vdev from the parent pdev
From: Francesco Valla
Date: Sun Nov 08 2026 - 17:34:09 EST
The rproc-virtio platform device can have a lifetime different from the
virtio_device it registers. During cleanup (e.g.: when a remoteproc is
stopped) the platform device is de-registered by the remoteproc core,
but the related virtio_device might be released later; if this happens,
rproc_virtio_dev_release() will try to de-reference a NULL pointer as
part of vdev_to_rvdev(). This can easily be reproduced with
CONFIG_DEBUG_KOBJECT_RELEASE.
Remove the access to the platform device structure from the cleanup path
and interact instead with its backing device, whose refcount was
explicitly increased during rproc_add_virtio_dev() and will be
decremented as part of rproc_virtio_dev_release(). The same is done for
the pdev de-reference inside vdev_to_rvdev(), even if no more used
during cleanup, to maintain the same access pattern.
While at it, fix the comments on the specific matter.
Fixes: 1d7b61c06dc3 ("remoteproc: virtio: Create platform device for the remoteproc_virtio")
Signed-off-by: Francesco Valla <francesco@xxxxxxxx>
---
drivers/remoteproc/remoteproc_virtio.c | 22 ++++++++--------------
1 file changed, 8 insertions(+), 14 deletions(-)
diff --git a/drivers/remoteproc/remoteproc_virtio.c b/drivers/remoteproc/remoteproc_virtio.c
index 12f55a81cfa0..7979b2570998 100644
--- a/drivers/remoteproc/remoteproc_virtio.c
+++ b/drivers/remoteproc/remoteproc_virtio.c
@@ -49,11 +49,7 @@ static int copy_dma_range_map(struct device *to, struct device *from)
static struct rproc_vdev *vdev_to_rvdev(struct virtio_device *vdev)
{
- struct platform_device *pdev;
-
- pdev = container_of(vdev->dev.parent, struct platform_device, dev);
-
- return platform_get_drvdata(pdev);
+ return dev_get_drvdata(vdev->dev.parent);
}
static struct rproc *vdev_to_rproc(struct virtio_device *vdev)
@@ -348,7 +344,7 @@ static const struct virtio_config_ops rproc_virtio_config_ops = {
/*
* This function is called whenever vdev is released, and is responsible
- * to decrement the remote processor's refcount which was taken when vdev was
+ * to decrement the platform device's refcount which was taken when vdev was
* added.
*
* Never call this function directly; it will be called by the driver
@@ -357,14 +353,13 @@ static const struct virtio_config_ops rproc_virtio_config_ops = {
static void rproc_virtio_dev_release(struct device *dev)
{
struct virtio_device *vdev = dev_to_virtio(dev);
- struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
- kfree(vdev);
+ of_reserved_mem_device_release(dev->parent);
+ dma_release_coherent_memory(dev->parent);
- of_reserved_mem_device_release(&rvdev->pdev->dev);
- dma_release_coherent_memory(&rvdev->pdev->dev);
+ put_device(dev->parent);
- put_device(&rvdev->pdev->dev);
+ kfree(vdev);
}
/**
@@ -372,8 +367,7 @@ static void rproc_virtio_dev_release(struct device *dev)
* @rvdev: the remote vdev
* @id: the device type identification (used to match it with a driver).
*
- * This function registers a virtio device. This vdev's partent is
- * the rproc device.
+ * This function registers a virtio device with the platform device as parent.
*
* Return: 0 on success or an appropriate error value otherwise
*/
@@ -450,7 +444,7 @@ static int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
vdev->dev.parent = dev;
vdev->dev.release = rproc_virtio_dev_release;
- /* Reference the vdev and vring allocations */
+ /* Reference the parent device, which will be used during teardown. */
get_device(dev);
ret = register_virtio_device(vdev);
--
2.56.0