[PATCH v2 2/4] remoteproc: virtio: Rework memory release
From: Francesco Valla
Date: Sat Nov 14 2026 - 03:07:31 EST
The current memory release setup has two issues, which may happen in
specific corner cases:
- a failure in kzalloc_obj() during subdev.start() would keep alive
the association between the pdev and the memory setups done just
before, leaking them and preventing further start attempts;
- in case of a crash recovery restart, where the pdev is maintained
but subdev.start() is called again, a delayed release of the
previous vdev either makes the start fail with -EBUSY (dedicated
vdev buffer) or, when falling back to the parent's reserved memory,
makes the memory setup disappear underneath the newly created vdev.
Move the memory release calls to subdev.stop(), symmetrically to what
happens in subdev.start(), and add them to the failure path of the
start() itself. The get()/put() actions on the pdev, that are now
useless since no access to it is performed during the vdev release, can
be safely dropped.
Fixes: b327c72753d6 ("remoteproc: virtio: Fix wdg cannot recovery remote processor")
Signed-off-by: Francesco Valla <francesco@xxxxxxxx>
---
drivers/remoteproc/remoteproc_virtio.c | 28 +++++++++++++++-------------
1 file changed, 15 insertions(+), 13 deletions(-)
diff --git a/drivers/remoteproc/remoteproc_virtio.c b/drivers/remoteproc/remoteproc_virtio.c
index b59f4acbb630..6cfdab831185 100644
--- a/drivers/remoteproc/remoteproc_virtio.c
+++ b/drivers/remoteproc/remoteproc_virtio.c
@@ -344,8 +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 platform device's refcount which was taken when vdev was
- * added.
+ * to free the memory allocated for the vdev.
*
* Never call this function directly; it will be called by the driver
* core when needed.
@@ -354,11 +353,6 @@ static void rproc_virtio_dev_release(struct device *dev)
{
struct virtio_device *vdev = dev_to_virtio(dev);
- of_reserved_mem_device_release(dev->parent);
- dma_release_coherent_memory(dev->parent);
-
- put_device(dev->parent);
-
kfree(vdev);
}
@@ -434,27 +428,32 @@ static int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
/* Allocate virtio device */
vdev = kzalloc_obj(*vdev);
- if (!vdev)
- return -ENOMEM;
+ if (!vdev) {
+ ret = -ENOMEM;
+ goto release_mem;
+ }
vdev->id.device = id;
vdev->config = &rproc_virtio_config_ops;
vdev->dev.parent = dev;
vdev->dev.release = rproc_virtio_dev_release;
- /* Reference the parent device, which will be used during teardown. */
- get_device(dev);
-
ret = register_virtio_device(vdev);
if (ret) {
put_device(&vdev->dev);
dev_err(dev, "failed to register vdev: %d\n", ret);
- return ret;
+ goto release_mem;
}
dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
return 0;
+
+release_mem:
+ of_reserved_mem_device_release(dev);
+ dma_release_coherent_memory(dev);
+
+ return ret;
}
/**
@@ -490,6 +489,9 @@ static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
ret = device_for_each_child(dev, NULL, rproc_remove_virtio_dev);
if (ret)
dev_warn(dev, "can't remove vdev child device: %d\n", ret);
+
+ of_reserved_mem_device_release(dev);
+ dma_release_coherent_memory(dev);
}
static int rproc_virtio_probe(struct platform_device *pdev)
--
2.56.0