[PATCH] libcamera: v4l2_videodevice: close dmabuf fd

Poduval, Karthik kpoduval at lab126.com
Fri Oct 15 01:38:30 CEST 2021


Please ignore this mail, it has wrong email id.

-----Original Message-----
From: Karthik Poduval <kpoduval at gmail.com> 
Sent: Thursday, October 14, 2021 4:34 PM
To: libcamera-devel at lists.libcamera.org
Cc: Poduval, Karthik <kpoduval at lab126.com>
Subject: [PATCH] libcamera: v4l2_videodevice: close dmabuf fd

From: Karthik Poduval <kpoduval at lab126.com>

The dmabuf fd that comes from V4L2_EXPBUF is passed to FileDescriptor which makes a dup of the fd and stores it. However the original fd isn't closed and this causes the vb2's buf reference count to remain at
3 (one for kernel, one for V4L2_EXPBUF and one for the dup fd by File Descriptor). When requestbufs is called with zero count, it unable to free the buffer as the refcount is still 2. After FileDecriptor dup the fd the original fd must be closed so that the buf refcount is maintained properly by vb2.

Signed-off-by: Karthik Poduval <kpoduval at lab126.com>
---
 src/libcamera/v4l2_videodevice.cpp | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index ba5f88cd..8c85da22 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -1384,6 +1384,7 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index,  {
 	struct v4l2_exportbuffer expbuf = {};
 	int ret;
+	FileDescriptor fd;
 
 	expbuf.type = bufferType_;
 	expbuf.index = index;
@@ -1397,7 +1398,14 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index,
 		return FileDescriptor();
 	}
 
-	return FileDescriptor(std::move(expbuf.fd));
+	fd = FileDescriptor(std::move(expbuf.fd));
+	/*
+	 * since FileDesciptor makes a dup of the fd, original fd must be
+	 * closed or else driver considers it as an orphaned buffer (due to
+	 * additional buffer refcount) thus causing a frame buffer leak
+	 */
+	::close(expbuf.fd);
+	return fd;
 }
 
 /**
--
2.32.0



More information about the libcamera-devel mailing list