From patchwork Thu Oct 14 23:38:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthik Poduval X-Patchwork-Id: 14153 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 54EF6C324C for ; Thu, 14 Oct 2021 23:38:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1D7BF68F52; Fri, 15 Oct 2021 01:38:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1634254721; bh=fViZEgX7k+Ia9bZq1nKJIpRCpwAriVsJjeVAglMH+Xw=; h=To:Date:List-Id:List-Post:From:List-Subscribe:List-Unsubscribe: List-Archive:Reply-To:List-Help:Subject:From; b=Q3fN7WxSokHRdk8AfgfXXqNiUMs4ykGB/Nl/45rZbsTKMWB9xXOY7PyF/a804NXr3 VwZAKclFukcl8cfb9ko8uBaqA75ZNn/N3dFImv23TDNaNEi/oEodyjTFEvsMjShyLc 89hVfiG4WYlNBuPAxBYqU0cV6EJJ0fYqkTGYcYKeQKVMsNlmBS2KfsbSQaZZhJMJ2Q QfFY70ypGQFEtYE2SXdmuW2zaeaNK3pljPZpXW7fE7UKKjywkdvR8qGUlnMtwQJX3J pxf8YPo7phoqZ5ktuxfLGCXJ3QZlYrdtKP3LryCWluWdvF2uiEdYJqrVxfgp7pbLeh xJjQYVK74g8eQ== To: Date: Thu, 14 Oct 2021 23:38:33 +0000 MIME-Version: 1.0 Message-ID: List-Id: List-Post: X-Patchwork-Original-From: Karthik Poduval via libcamera-devel From: Karthik Poduval Precedence: list X-Mailman-Version: 2.1.29 X-BeenThere: libcamera-devel@lists.libcamera.org List-Subscribe: , List-Unsubscribe: , List-Archive: Reply-To: Karthik Poduval List-Help: Subject: [libcamera-devel] [PATCH] libcamera: v4l2_videodevice: close dmabuf fd Content-Disposition: inline Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 --- 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; } /**