[Concept,08/33] virtio: emul_blk: Free disk data on remove

Message ID 20260416023021.626949-9-sjg@u-boot.org
State New
Headers
Series Fix memory leaks and test pollution in sandbox tests |

Commit Message

Simon Glass April 16, 2026, 2:29 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

The probe function allocates 1MB for the emulated disk contents but
there is no corresponding free on remove, so the memory leaks every
time the device is torn down. This shows up as a 1MB leak per test in
several bootstd tests.

Add a remove method to free the buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/virtio/emul_blk.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
  

Patch

diff --git a/drivers/virtio/emul_blk.c b/drivers/virtio/emul_blk.c
index 1d42b34b8ab..e9eab4a6470 100644
--- a/drivers/virtio/emul_blk.c
+++ b/drivers/virtio/emul_blk.c
@@ -131,6 +131,15 @@  static int virtio_blk_emul_probe(struct udevice *dev)
 	return 0;
 }
 
+static int virtio_blk_emul_remove(struct udevice *dev)
+{
+	struct virtio_blk_emul_priv *priv = dev_get_priv(dev);
+
+	free(priv->disk_data);
+
+	return 0;
+}
+
 static struct virtio_emul_ops blk_emul_ops = {
 	.process_request = blk_emul_process_request,
 	.get_config = blk_emul_get_config,
@@ -148,6 +157,7 @@  U_BOOT_DRIVER(virtio_blk_emul) = {
 	.id	= UCLASS_VIRTIO_EMUL,
 	.of_match = virtio_blk_emul_ids,
 	.probe	= virtio_blk_emul_probe,
+	.remove	= virtio_blk_emul_remove,
 	.ops	= &blk_emul_ops,
 	.priv_auto	= sizeof(struct virtio_blk_emul_priv),
 };