[{"id":20224,"web_url":"https://patchwork.libcamera.org/comment/20224/","msgid":"<YWjdMCNiDFlWgLU/@pendragon.ideasonboard.com>","date":"2021-10-15T01:45:20","subject":"Re: [libcamera-devel] [PATCH] libcamera: v4l2_videodevice: close\n\tdmabuf fd","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Karthik,\n\nThank you for the patch.\n\nOn Thu, Oct 14, 2021 at 11:38:33PM +0000, Karthik Poduval via libcamera-devel wrote:\n> \n> The dmabuf fd that comes from V4L2_EXPBUF is passed to FileDescriptor\n> which makes a dup of the fd and stores it. However the original fd\n> isn't closed and this causes the vb2's buf reference count to remain at\n> 3 (one for kernel, one for V4L2_EXPBUF and one for the dup fd by File\n> Descriptor). When requestbufs is called with zero count, it unable to\n> free the buffer as the refcount is still 2. After FileDecriptor dup the\n> fd the original fd must be closed so that the buf refcount is maintained\n> properly by vb2.\n> \n> Signed-off-by: Karthik Poduval <kpoduval@lab126.com>\n> ---\n>  src/libcamera/v4l2_videodevice.cpp | 10 +++++++++-\n>  1 file changed, 9 insertions(+), 1 deletion(-)\n> \n> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\n> index ba5f88cd..8c85da22 100644\n> --- a/src/libcamera/v4l2_videodevice.cpp\n> +++ b/src/libcamera/v4l2_videodevice.cpp\n> @@ -1384,6 +1384,7 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index,\n>  {\n>  \tstruct v4l2_exportbuffer expbuf = {};\n>  \tint ret;\n> +\tFileDescriptor fd;\n>  \n>  \texpbuf.type = bufferType_;\n>  \texpbuf.index = index;\n> @@ -1397,7 +1398,14 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index,\n>  \t\treturn FileDescriptor();\n>  \t}\n>  \n> -\treturn FileDescriptor(std::move(expbuf.fd));\n> +\tfd = FileDescriptor(std::move(expbuf.fd));\n\nWith the std::move(), the following FileDescriptor constructor is\ncalled:\n\nFileDescriptor::FileDescriptor(int &&fd)\n{\n\tif (fd < 0)\n\t\treturn;\n\n\tfd_ = std::make_shared<Descriptor>(fd, false);\n\t/*\n\t * The Descriptor constructor can't have failed here, as it took over\n\t * the fd without duplicating it. Just set the original fd to -1 to\n\t * implement move semantics.\n\t */\n\tfd = -1;\n}\n\nThe inner Descriptor constructor thus doesn't call dup(). After this\nline, expbuf.fd will be equal to -1, so the close() call below will be a\nno-op. Unless I'm missing something :-)\n\n> +\t/*\n> +\t * since FileDesciptor makes a dup of the fd, original fd must be\n> +\t * closed or else driver considers it as an orphaned buffer (due to\n> +\t * additional buffer refcount) thus causing a frame buffer leak\n> +\t */\n> +\t::close(expbuf.fd);\n> +\treturn fd;\n>  }\n>  \n>  /**","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 24F85C324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 15 Oct 2021 01:45:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 75FF268F50;\n\tFri, 15 Oct 2021 03:45:37 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 08E0C68F4D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 15 Oct 2021 03:45:36 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7549C2E3;\n\tFri, 15 Oct 2021 03:45:35 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"L3/axZrL\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634262335;\n\tbh=vNQF6lc96qma+tC5oWWlSjEJTFrAZmsf4/oaQxttFxI=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=L3/axZrLLyc/K9ZB7vcWhUx67HaXZwLMzZRQGeIZNFWWleoMLf+sjhoN4MvxNOnoB\n\t10u8JKLVQbn2LOtOMK2wZyLBglmW/8nHqdjg0ou2H/AKtxrXTWgm43/GAGydT0FZaU\n\tGVAvVVUeb+475KPLLD99sA3hwwdAP7SlA2p8h/lQ=","Date":"Fri, 15 Oct 2021 04:45:20 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Karthik Poduval <kpoduval@lab126.com>","Message-ID":"<YWjdMCNiDFlWgLU/@pendragon.ideasonboard.com>","References":"<mailman.593.1634254720.837.libcamera-devel@lists.libcamera.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<mailman.593.1634254720.837.libcamera-devel@lists.libcamera.org>","Subject":"Re: [libcamera-devel] [PATCH] libcamera: v4l2_videodevice: close\n\tdmabuf fd","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":20228,"web_url":"https://patchwork.libcamera.org/comment/20228/","msgid":"<mailman.600.1634269772.837.libcamera-devel@lists.libcamera.org>","date":"2021-10-15T03:49:29","subject":"Re: [libcamera-devel] [PATCH] libcamera: v4l2_videodevice: close\n\tdmabuf fd","submitter":{"id":97,"url":"https://patchwork.libcamera.org/api/people/97/","name":"Nicolas Dufresne via libcamera-devel","email":"libcamera-devel@lists.libcamera.org"},"content":"Thanks Laurent,\n\nYou are right. I saw this issue on an earlier version of libcamera (it caused vb2 memory leaks and also hit max fd limit) and I ported it over without diving deep. Glad to know its not an issue on latest version.\n\n--\nRegards,\nKarthik Poduval","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id F3387C323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 15 Oct 2021 03:49:33 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7E4C868F4E;\n\tFri, 15 Oct 2021 05:49:33 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1634269773;\n\tbh=oIQDdwz+BqYX0agp5Sa1AwR+GgBzvShiD/IPAGFDg40=;\n\th=To:Date:References:In-Reply-To:List-Id:List-Post:From:Cc:\n\tList-Subscribe:List-Unsubscribe:List-Archive:Reply-To:List-Help:\n\tSubject:From;\n\tb=sJ8HKqF9R75VVaJDAJsgfUoGd0ubBC8ozmwofXmBbNaRBFxityXDfInLunmQ4RnPc\n\tYceq673GFJJNIAl3QRLsliXr8GfguxUDvWGOcqNdjtCoFYm3wPcpzfo22bn50lafI7\n\t+sFRrcT4vsA3jneM4VOrRuiqS+NINF8dhmPBKEarHSqulfqtYcATZXY2x60VyISWwz\n\tqUh1N9lBbhzJPdGDZXNIArKsgDpSsOFwjVoa2ttOGXtYVgYSySxyiY0oQTRccYjn+j\n\tkWL9A6YSgHGOrDLttmuke3hY7gyHndNwK1EzcXzk67CyTjTgwin+O7AXBpQFjCTbW4\n\tz5TdnKxDUdlBw==","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Date":"Fri, 15 Oct 2021 03:49:29 +0000","References":"<mailman.593.1634254720.837.libcamera-devel@lists.libcamera.org>,\n\t<YWjdMCNiDFlWgLU/@pendragon.ideasonboard.com>","In-Reply-To":"<YWjdMCNiDFlWgLU/@pendragon.ideasonboard.com>","MIME-Version":"1.0","Message-ID":"<mailman.600.1634269772.837.libcamera-devel@lists.libcamera.org>","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","From":"\"Poduval,\n\tKarthik via libcamera-devel\" <libcamera-devel@lists.libcamera.org>","Precedence":"list","Cc":"\"libcamera-devel@lists.libcamera.org\"\n\t<libcamera-devel@lists.libcamera.org>","X-Mailman-Version":"2.1.29","X-BeenThere":"libcamera-devel@lists.libcamera.org","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","Reply-To":"\"Poduval, Karthik\" <kpoduval@lab126.com>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","Subject":"Re: [libcamera-devel] [PATCH] libcamera: v4l2_videodevice: close\n\tdmabuf fd","Content-Type":"message/rfc822","Content-Disposition":"inline","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]