From patchwork Thu Oct 14 23:33:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthik Poduval X-Patchwork-Id: 14156 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 6D71AC323E for ; Fri, 15 Oct 2021 08:42:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D1E0E68F4F; Fri, 15 Oct 2021 10:42:24 +0200 (CEST) Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C370B60501 for ; Fri, 15 Oct 2021 01:34:13 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.85,374,1624320000"; d="scan'208";a="149323119" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-5feb294a.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP; 14 Oct 2021 23:34:12 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-pdx-2a-5feb294a.us-west-2.amazon.com (Postfix) with ESMTPS id 752E680B43 for ; Thu, 14 Oct 2021 23:34:11 +0000 (UTC) Received: from EX13D07UWB001.ant.amazon.com (10.43.161.238) by EX13MTAUWB001.ant.amazon.com (10.43.161.249) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Thu, 14 Oct 2021 23:34:11 +0000 Received: from EX13MTAUEE002.ant.amazon.com (10.43.62.24) by EX13D07UWB001.ant.amazon.com (10.43.161.238) with Microsoft SMTP Server (TLS) id 15.0.1497.24; Thu, 14 Oct 2021 23:34:11 +0000 Received: from dev-dsk-kpoduval-2c-618debf1.us-west-2.amazon.com (172.23.136.85) by mail-relay.amazon.com (10.43.62.224) with Microsoft SMTP Server id 15.0.1497.24 via Frontend Transport; Thu, 14 Oct 2021 23:34:10 +0000 Received: by dev-dsk-kpoduval-2c-618debf1.us-west-2.amazon.com (Postfix, from userid 293312) id 0618B429F3; Thu, 14 Oct 2021 23:34:10 +0000 (UTC) From: Karthik Poduval To: Date: Thu, 14 Oct 2021 23:33:51 +0000 Message-ID: <20211014233351.23092-1-kpoduval@gmail.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Mailman-Approved-At: Fri, 15 Oct 2021 10:42:23 +0200 Subject: [libcamera-devel] [PATCH] libcamera: v4l2_videodevice: close dmabuf fd X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Karthik Poduval 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; } /**