[{"id":19507,"web_url":"https://patchwork.libcamera.org/comment/19507/","msgid":"<9b797084-cf2b-8121-75f3-ae283a8d1bcf@ideasonboard.com>","date":"2021-09-07T11:49:37","subject":"Re: [libcamera-devel] [PATCH v3 25/30] cam: drm: Avoid importing\n\tthe same dmabuf multiple times","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"On 06/09/2021 23:56, Laurent Pinchart wrote:\n> When creating a DRM frame buffer, the dmabufs for the planes are\n> imported as GEM objects. For multi-planar formats, all planes may use\n> the same dmabuf, which results in multiple imports. This doesn't cause\n> any issue at import time, as DRM detects this situation and returns the\n> same GEM object. However, when destroying the frame buffer, the same GEM\n> object ends up being closed multiple times, which generates an error.\n> \n> Fix this by avoiding multiple imports of the same dmabuf for the same\n> frame buffer. While the issue may theoretically occur with identical\n> dmabufs for different frame buffers, this is quite unlikely and is thus\n> not addressed.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n>  src/cam/drm.cpp | 29 +++++++++++++++++------------\n>  src/cam/drm.h   |  2 +-\n>  2 files changed, 18 insertions(+), 13 deletions(-)\n> \n> diff --git a/src/cam/drm.cpp b/src/cam/drm.cpp\n> index d5a75d039fd8..f25300913a7f 100644\n> --- a/src/cam/drm.cpp\n> +++ b/src/cam/drm.cpp\n> @@ -283,9 +283,9 @@ FrameBuffer::FrameBuffer(Device *dev)\n>  \n>  FrameBuffer::~FrameBuffer()\n>  {\n> -\tfor (FrameBuffer::Plane &plane : planes_) {\n> +\tfor (const auto &plane : planes_) {\n>  \t\tstruct drm_gem_close gem_close = {\n> -\t\t\t.handle = plane.handle,\n> +\t\t\t.handle = plane.second.handle,\n>  \t\t\t.pad = 0,\n>  \t\t};\n>  \t\tint ret;\n> @@ -605,22 +605,27 @@ std::unique_ptr<FrameBuffer> Device::createFrameBuffer(\n>  \tint ret;\n>  \n>  \tconst std::vector<libcamera::FrameBuffer::Plane> &planes = buffer.planes();\n> -\tfb->planes_.reserve(planes.size());\n>  \n>  \tunsigned int i = 0;\n>  \tfor (const libcamera::FrameBuffer::Plane &plane : planes) {\n> +\t\tint fd = plane.fd.fd();\n>  \t\tuint32_t handle;\n>  \n> -\t\tret = drmPrimeFDToHandle(fd_, plane.fd.fd(), &handle);\n> -\t\tif (ret < 0) {\n> -\t\t\tret = -errno;\n> -\t\t\tstd::cerr\n> -\t\t\t\t<< \"Unable to import framebuffer dmabuf: \"\n> -\t\t\t\t<< strerror(-ret) << std::endl;\n> -\t\t\treturn nullptr;\n> -\t\t}\n> +\t\tauto iter = fb->planes_.find(fd);\n> +\t\tif (iter == fb->planes_.end()) {\n> +\t\t\tret = drmPrimeFDToHandle(fd_, plane.fd.fd(), &handle);\n> +\t\t\tif (ret < 0) {\n> +\t\t\t\tret = -errno;\n> +\t\t\t\tstd::cerr\n> +\t\t\t\t\t<< \"Unable to import framebuffer dmabuf: \"\n> +\t\t\t\t\t<< strerror(-ret) << std::endl;\n> +\t\t\t\treturn nullptr;\n> +\t\t\t}\n>  \n> -\t\tfb->planes_.push_back({ handle });\n> +\t\t\tfb->planes_[fd] = { handle };\n> +\t\t} else {\n> +\t\t\thandle = iter->second.handle;\n> +\t\t}\n>  \n>  \t\thandles[i] = handle;\n>  \t\toffsets[i] = plane.offset;\n> diff --git a/src/cam/drm.h b/src/cam/drm.h\n> index 00f7e798b771..0b88f9a33912 100644\n> --- a/src/cam/drm.h\n> +++ b/src/cam/drm.h\n> @@ -242,7 +242,7 @@ private:\n>  \n>  \tFrameBuffer(Device *dev);\n>  \n> -\tstd::vector<Plane> planes_;\n> +\tstd::map<int, Plane> planes_;\n>  };\n>  \n>  class AtomicRequest\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 413F6BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  7 Sep 2021 11:49:43 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A26226916C;\n\tTue,  7 Sep 2021 13:49:42 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A7F6D60251\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  7 Sep 2021 13:49:40 +0200 (CEST)","from [192.168.0.20]\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 29910499;\n\tTue,  7 Sep 2021 13:49:40 +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=\"HXQleYgX\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1631015380;\n\tbh=HAPRzTf414jucK7+TYa+WD1J/m7lgMt4/xmFtkK06Go=;\n\th=To:References:From:Subject:Date:In-Reply-To:From;\n\tb=HXQleYgXjkhgE+PA/vpeXwsAkYM762wep/6fByZ/MqjkWKqHzYSg8UDKXqtrwFkKH\n\tJiLtQzrCRiYGgdrZrc3vx7Cv6Fci6FmUf/TcKE3T34hsCfZm8tCaZyUxdhSe3A1RqI\n\txWB7+ghjDslH2NJsSSeZBz+sjX7OJsx00gm7F+xw=","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20210906225420.13275-1-laurent.pinchart@ideasonboard.com>\n\t<20210906225636.14683-25-laurent.pinchart@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<9b797084-cf2b-8121-75f3-ae283a8d1bcf@ideasonboard.com>","Date":"Tue, 7 Sep 2021 12:49:37 +0100","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101\n\tThunderbird/78.11.0","MIME-Version":"1.0","In-Reply-To":"<20210906225636.14683-25-laurent.pinchart@ideasonboard.com>","Content-Type":"text/plain; charset=utf-8","Content-Language":"en-GB","Content-Transfer-Encoding":"8bit","Subject":"Re: [libcamera-devel] [PATCH v3 25/30] cam: drm: Avoid importing\n\tthe same dmabuf multiple times","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]