Skip to contentSkip to conversation

media: videobuf2: Fix device reference leak in vb2_dc_alloc error path

PATCH In review Haotian Zhang 2 participants 2 messages Oct 28, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: videobuf2: Fix device reference leak in vb2_dc_alloc error path

In vb2_dc_alloc(), get_device() is called to increment the device reference count. However, if subsequent DMA allocation fails (vb2_dc_alloc_coherent or vb2_dc_alloc_non_coherent returns error), the function returns without calling put_device(), causing a device reference leak.

Add put_device() call in the error path before kfree() to properly release the device reference acquired earlier.

Fixes de27891f675e ("media: videobuf2: handle non-contiguous DMA allocations")
---
 drivers/media/common/videobuf2/videobuf2-dma-contig.c | 1 +
 1 file changed, 1 insertion(+)
drivers/media/common/videobuf2/videobuf2-dma-contig.c +1 -0
index a13ec569c82f..7123c5fae92c 100644
@@ -258,6 +258,7 @@ static void *vb2_dc_alloc(struct vb2_buffer *vb,
 
 	if (ret) {
 		dev_err(dev, "dma alloc of size %lu failed\n", size);
+		put_device(buf->dev);
 		kfree(buf);
 		return ERR_PTR(-ENOMEM);
 	}
Signature · 1 line
2.50.1.windows.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Haotian Zhang

In vb2_dc_alloc(), get_device() is called to increment the device reference count. However, if subsequent DMA allocation fails (vb2_dc_alloc_coherent or vb2_dc_alloc_non_coherent returns error), the function returns without calling put_device(), causing a device reference leak.

Add put_device() call in the error path before kfree() to properly release the device reference acquired earlier.

Fixes de27891f675e ("media: videobuf2: handle non-contiguous DMA allocations")
Quoted 9 lines
---
  drivers/media/common/videobuf2/videobuf2-dma-contig.c | 1 +
  1 file changed, 1 insertion(+)
drivers/media/common/videobuf2/videobuf2-dma-contig.c +0 -0
index a13ec569c82f..7123c5fae92c 100644

drivers/media/common/videobuf2/videobuf2-dma-contig.c static void *vb2_dc_alloc(struct vb2_buffer *vb, · line 264

Quoted 7 lines
drivers/media/common/videobuf2/videobuf2-dma-contig.c +1 -0
@@ -258,6 +258,7 @@ static void *vb2_dc_alloc(struct vb2_buffer *vb,
 
  	if (ret) {
  		dev_err(dev, "dma alloc of size %lu failed\n", size);
+		put_device(buf->dev);
  		kfree(buf);
  		return ERR_PTR(-ENOMEM);
  	}

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland