[{"id":21325,"web_url":"https://patchwork.libcamera.org/comment/21325/","msgid":"<CAO5uPHOxT30TfGegsty_N-i4hTtYhupHXNXokzeGg2EQCJH8Xg@mail.gmail.com>","date":"2021-11-29T14:01:19","subject":"Re: [libcamera-devel] [PATCH v3 15/17] libcamera: pipeline:\n\traspberrypi: DmaHeaps: Use UniqueFD for a file descriptor","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/people/63/","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"content":"Hi Laurent,\n\nOn Mon, Nov 29, 2021 at 8:58 AM Laurent Pinchart\n<laurent.pinchart@ideasonboard.com> wrote:\n>\n> From: Hirokazu Honda <hiroh@chromium.org>\n>\n> Manages a file descriptor owned by DmaHeaps for a dma handle by\n> UniqueFD. Furthermore, DmaHeaps::alloc() creates a new file\n> descriptor and the returned file descriptor is owned by a caller.\n> This also clarifies it by changing the returned value to UniqueFD.\n>\n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Hirokazu Honda <hiroh@chromium.org> if applicable.\n\n> ---\n> Changes since v2:\n>\n> - Use default destructor\n> - Return {}\n> ---\n>  .../pipeline/raspberrypi/dma_heaps.cpp        | 28 ++++++++-----------\n>  .../pipeline/raspberrypi/dma_heaps.h          | 10 ++++---\n>  .../pipeline/raspberrypi/raspberrypi.cpp      |  6 ++--\n>  3 files changed, 21 insertions(+), 23 deletions(-)\n>\n> diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> index 573ea11de607..69831dabbe75 100644\n> --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> @@ -35,7 +35,6 @@ LOG_DECLARE_CATEGORY(RPI)\n>  namespace RPi {\n>\n>  DmaHeap::DmaHeap()\n> -       : dmaHeapHandle_(-1)\n>  {\n>         for (const char *name : heapNames) {\n>                 int ret = ::open(name, O_RDWR, 0);\n> @@ -46,49 +45,44 @@ DmaHeap::DmaHeap()\n>                         continue;\n>                 }\n>\n> -               dmaHeapHandle_ = ret;\n> +               dmaHeapHandle_ = UniqueFD(ret);\n>                 break;\n>         }\n>\n> -       if (dmaHeapHandle_ < 0)\n> +       if (!dmaHeapHandle_.isValid())\n>                 LOG(RPI, Error) << \"Could not open any dmaHeap device\";\n>  }\n>\n> -DmaHeap::~DmaHeap()\n> -{\n> -       if (dmaHeapHandle_ > -1)\n> -               ::close(dmaHeapHandle_);\n> -}\n> +DmaHeap::~DmaHeap() = default;\n>\n> -FileDescriptor DmaHeap::alloc(const char *name, std::size_t size)\n> +UniqueFD DmaHeap::alloc(const char *name, std::size_t size)\n>  {\n>         int ret;\n>\n>         if (!name)\n> -               return FileDescriptor();\n> +               return {};\n>\n>         struct dma_heap_allocation_data alloc = {};\n>\n>         alloc.len = size;\n>         alloc.fd_flags = O_CLOEXEC | O_RDWR;\n>\n> -       ret = ::ioctl(dmaHeapHandle_, DMA_HEAP_IOCTL_ALLOC, &alloc);\n> -\n> +       ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n>         if (ret < 0) {\n>                 LOG(RPI, Error) << \"dmaHeap allocation failure for \"\n>                                 << name;\n> -               return FileDescriptor();\n> +               return {};\n>         }\n>\n> -       ret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);\n> +       UniqueFD allocFd(alloc.fd);\n> +       ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);\n>         if (ret < 0) {\n>                 LOG(RPI, Error) << \"dmaHeap naming failure for \"\n>                                 << name;\n> -               ::close(alloc.fd);\n> -               return FileDescriptor();\n> +               return {};\n>         }\n>\n> -       return FileDescriptor(std::move(alloc.fd));\n> +       return allocFd;\n>  }\n>\n>  } /* namespace RPi */\n> diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> index 57beaeb2e48a..d38f41eae1a2 100644\n> --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> @@ -7,7 +7,9 @@\n>\n>  #pragma once\n>\n> -#include <libcamera/base/file_descriptor.h>\n> +#include <stddef.h>\n> +\n> +#include <libcamera/base/unique_fd.h>\n>\n>  namespace libcamera {\n>\n> @@ -18,11 +20,11 @@ class DmaHeap\n>  public:\n>         DmaHeap();\n>         ~DmaHeap();\n> -       bool isValid() const { return dmaHeapHandle_ > -1; }\n> -       FileDescriptor alloc(const char *name, std::size_t size);\n> +       bool isValid() const { return dmaHeapHandle_.isValid(); }\n> +       UniqueFD alloc(const char *name, std::size_t size);\n>\n>  private:\n> -       int dmaHeapHandle_;\n> +       UniqueFD dmaHeapHandle_;\n>  };\n>\n>  } /* namespace RPi */\n> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> index 7526edf774a2..ffa51a0c65ca 100644\n> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> @@ -1361,10 +1361,12 @@ int RPiCameraData::configureIPA(const CameraConfiguration *config)\n>\n>         /* Allocate the lens shading table via dmaHeap and pass to the IPA. */\n>         if (!lsTable_.isValid()) {\n> -               lsTable_ = dmaHeap_.alloc(\"ls_grid\", ipa::RPi::MaxLsGridSize);\n> -               if (!lsTable_.isValid())\n> +               UniqueFD fd = dmaHeap_.alloc(\"ls_grid\", ipa::RPi::MaxLsGridSize);\n> +               if (!fd.isValid())\n>                         return -ENOMEM;\n>\n> +               lsTable_ = FileDescriptor(std::move(fd));\n> +\n>                 /* Allow the IPA to mmap the LS table via the file descriptor. */\n>                 /*\n>                  * \\todo Investigate if mapping the lens shading table buffer\n> --\n> Regards,\n>\n> Laurent Pinchart\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 AD9D9BDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 29 Nov 2021 14:01:39 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 61287605A6;\n\tMon, 29 Nov 2021 15:01:39 +0100 (CET)","from mail-ed1-x534.google.com (mail-ed1-x534.google.com\n\t[IPv6:2a00:1450:4864:20::534])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7E56060592\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Nov 2021 15:01:38 +0100 (CET)","by mail-ed1-x534.google.com with SMTP id x6so72185031edr.5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Nov 2021 06:01:38 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"VfkIPu1j\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=mime-version:references:in-reply-to:from:date:message-id:subject:to\n\t:cc; bh=ZPY9nfVzbVBrIzdWXUDakkNPCzIj9Tg0tCrXIlqI3rw=;\n\tb=VfkIPu1jfPYHe/hXE/6Vhz9bHVb9PY50HZIjDKItMIbmpDOOGDrkhFeKndWjCt7wYB\n\ttSTrIHfY0uylDPVRPRGB1EnQBleuA4QT6ikbirSvtPlW6CSJxd+KA0V5A24fGn5845YL\n\tB6xlz274KDm3/1BSnbmAPw36obKMIIGx9+fPo=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:mime-version:references:in-reply-to:from:date\n\t:message-id:subject:to:cc;\n\tbh=ZPY9nfVzbVBrIzdWXUDakkNPCzIj9Tg0tCrXIlqI3rw=;\n\tb=DGGBZ1tATkA5LIpx/fr8Trlj3huMZTXC/1cvMUl+PFgfQ/aMRA8+NDZoJ0gWzFfrFs\n\tqWat2sIeAVlqYtdTV4iY5uZjzcTgszDFDFfQwm9AOL8trdhauZ6c9AWLpx2acNB4qmas\n\t0mq860/znWuMrDMR/1W/0a21m0zqSHZc7Axi666W8cuxGzWSAui6yLN9g/oxDLEoLRpl\n\tKqvari33nzygcRXhCNiiIy7hes2EIRa580WSCWlqv4btbk41O2uNSInTiq8HUHlfHpSU\n\t4EN3JpdO0cxlk5tReuHI9/s6PWe13g+ZjJnif6Ct+Pi3dzM9GYft2QhKx2QpfQ+b+8RU\n\t4k3w==","X-Gm-Message-State":"AOAM5326yRHdvWWVpaqafXLVmZnwiRuyDrOfLJ8SwkeGlTOn8uzAY1oz\n\tcylDi+0P7mIuvnFTjabJ8AXfdVwWItU4MaKmDxXx3w==","X-Google-Smtp-Source":"ABdhPJzYVGQ8+9gx1LDdXRHLxbDe/3fYQPuPWZSOe6dn7Oeb07dBXWT0vTR6knVO+hvNRdcTisuRjvZiCNX5XI2YZWg=","X-Received":"by 2002:a05:6402:254b:: with SMTP id\n\tl11mr75133765edb.225.1638194490889; \n\tMon, 29 Nov 2021 06:01:30 -0800 (PST)","MIME-Version":"1.0","References":"<20211128235752.10836-1-laurent.pinchart@ideasonboard.com>\n\t<20211128235752.10836-16-laurent.pinchart@ideasonboard.com>","In-Reply-To":"<20211128235752.10836-16-laurent.pinchart@ideasonboard.com>","From":"Hirokazu Honda <hiroh@chromium.org>","Date":"Mon, 29 Nov 2021 23:01:19 +0900","Message-ID":"<CAO5uPHOxT30TfGegsty_N-i4hTtYhupHXNXokzeGg2EQCJH8Xg@mail.gmail.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Content-Type":"text/plain; charset=\"UTF-8\"","Subject":"Re: [libcamera-devel] [PATCH v3 15/17] libcamera: pipeline:\n\traspberrypi: DmaHeaps: Use UniqueFD for a file descriptor","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":21347,"web_url":"https://patchwork.libcamera.org/comment/21347/","msgid":"<20211129155947.wqurjqft4hz7lqft@uno.localdomain>","date":"2021-11-29T15:59:47","subject":"Re: [libcamera-devel] [PATCH v3 15/17] libcamera: pipeline:\n\traspberrypi: DmaHeaps: Use UniqueFD for a file descriptor","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Laurent\n\nOn Mon, Nov 29, 2021 at 01:57:50AM +0200, Laurent Pinchart wrote:\n> From: Hirokazu Honda <hiroh@chromium.org>\n>\n> Manages a file descriptor owned by DmaHeaps for a dma handle by\n> UniqueFD. Furthermore, DmaHeaps::alloc() creates a new file\n> descriptor and the returned file descriptor is owned by a caller.\n> This also clarifies it by changing the returned value to UniqueFD.\n>\n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n> Changes since v2:\n>\n> - Use default destructor\n> - Return {}\n> ---\n>  .../pipeline/raspberrypi/dma_heaps.cpp        | 28 ++++++++-----------\n>  .../pipeline/raspberrypi/dma_heaps.h          | 10 ++++---\n>  .../pipeline/raspberrypi/raspberrypi.cpp      |  6 ++--\n>  3 files changed, 21 insertions(+), 23 deletions(-)\n>\n> diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> index 573ea11de607..69831dabbe75 100644\n> --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> @@ -35,7 +35,6 @@ LOG_DECLARE_CATEGORY(RPI)\n>  namespace RPi {\n>\n>  DmaHeap::DmaHeap()\n> -\t: dmaHeapHandle_(-1)\n>  {\n>  \tfor (const char *name : heapNames) {\n>  \t\tint ret = ::open(name, O_RDWR, 0);\n> @@ -46,49 +45,44 @@ DmaHeap::DmaHeap()\n>  \t\t\tcontinue;\n>  \t\t}\n>\n> -\t\tdmaHeapHandle_ = ret;\n> +\t\tdmaHeapHandle_ = UniqueFD(ret);\n\nIn this series you have mostly used\n\n                dmaHeapHandle_ = UniqueFD(::open(name, O_RDWR, 0));\n                if (dmaHeapHandle_.isValid())\n\nbut it's ok the way it is\n\n>  \t\tbreak;\n>  \t}\n>\n> -\tif (dmaHeapHandle_ < 0)\n> +\tif (!dmaHeapHandle_.isValid())\n>  \t\tLOG(RPI, Error) << \"Could not open any dmaHeap device\";\n>  }\n>\n> -DmaHeap::~DmaHeap()\n> -{\n> -\tif (dmaHeapHandle_ > -1)\n> -\t\t::close(dmaHeapHandle_);\n> -}\n> +DmaHeap::~DmaHeap() = default;\n>\n> -FileDescriptor DmaHeap::alloc(const char *name, std::size_t size)\n> +UniqueFD DmaHeap::alloc(const char *name, std::size_t size)\n>  {\n>  \tint ret;\n>\n>  \tif (!name)\n> -\t\treturn FileDescriptor();\n> +\t\treturn {};\n>\n>  \tstruct dma_heap_allocation_data alloc = {};\n>\n>  \talloc.len = size;\n>  \talloc.fd_flags = O_CLOEXEC | O_RDWR;\n>\n> -\tret = ::ioctl(dmaHeapHandle_, DMA_HEAP_IOCTL_ALLOC, &alloc);\n> -\n> +\tret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n>  \tif (ret < 0) {\n>  \t\tLOG(RPI, Error) << \"dmaHeap allocation failure for \"\n>  \t\t\t\t<< name;\n> -\t\treturn FileDescriptor();\n> +\t\treturn {};\n>  \t}\n>\n> -\tret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);\n> +\tUniqueFD allocFd(alloc.fd);\n> +\tret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);\n>  \tif (ret < 0) {\n>  \t\tLOG(RPI, Error) << \"dmaHeap naming failure for \"\n>  \t\t\t\t<< name;\n> -\t\t::close(alloc.fd);\n> -\t\treturn FileDescriptor();\n> +\t\treturn {};\n>  \t}\n>\n> -\treturn FileDescriptor(std::move(alloc.fd));\n> +\treturn allocFd;\n>  }\n>\n>  } /* namespace RPi */\n> diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> index 57beaeb2e48a..d38f41eae1a2 100644\n> --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> @@ -7,7 +7,9 @@\n>\n>  #pragma once\n>\n> -#include <libcamera/base/file_descriptor.h>\n> +#include <stddef.h>\n\nwhat for ?\n\n> +\n> +#include <libcamera/base/unique_fd.h>\n>\n>  namespace libcamera {\n>\n> @@ -18,11 +20,11 @@ class DmaHeap\n>  public:\n>  \tDmaHeap();\n>  \t~DmaHeap();\n> -\tbool isValid() const { return dmaHeapHandle_ > -1; }\n> -\tFileDescriptor alloc(const char *name, std::size_t size);\n> +\tbool isValid() const { return dmaHeapHandle_.isValid(); }\n> +\tUniqueFD alloc(const char *name, std::size_t size);\n>\n>  private:\n> -\tint dmaHeapHandle_;\n> +\tUniqueFD dmaHeapHandle_;\n>  };\n>\n>  } /* namespace RPi */\n> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> index 7526edf774a2..ffa51a0c65ca 100644\n> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> @@ -1361,10 +1361,12 @@ int RPiCameraData::configureIPA(const CameraConfiguration *config)\n>\n>  \t/* Allocate the lens shading table via dmaHeap and pass to the IPA. */\n>  \tif (!lsTable_.isValid()) {\n> -\t\tlsTable_ = dmaHeap_.alloc(\"ls_grid\", ipa::RPi::MaxLsGridSize);\n> -\t\tif (!lsTable_.isValid())\n> +\t\tUniqueFD fd = dmaHeap_.alloc(\"ls_grid\", ipa::RPi::MaxLsGridSize);\n> +\t\tif (!fd.isValid())\n>  \t\t\treturn -ENOMEM;\n>\n> +\t\tlsTable_ = FileDescriptor(std::move(fd));\n> +\n>  \t\t/* Allow the IPA to mmap the LS table via the file descriptor. */\n>  \t\t/*\n>  \t\t * \\todo Investigate if mapping the lens shading table buffer\n> --\n> Regards,\n>\n> Laurent Pinchart\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 EB876BDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 29 Nov 2021 15:58:56 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0A634605A6;\n\tMon, 29 Nov 2021 16:58:56 +0100 (CET)","from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net\n\t[217.70.183.201])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id EBFF960592\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Nov 2021 16:58:54 +0100 (CET)","(Authenticated sender: jacopo@jmondi.org)\n\tby relay8-d.mail.gandi.net (Postfix) with ESMTPSA id 5EA801BF207;\n\tMon, 29 Nov 2021 15:58:54 +0000 (UTC)"],"Date":"Mon, 29 Nov 2021 16:59:47 +0100","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Message-ID":"<20211129155947.wqurjqft4hz7lqft@uno.localdomain>","References":"<20211128235752.10836-1-laurent.pinchart@ideasonboard.com>\n\t<20211128235752.10836-16-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211128235752.10836-16-laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v3 15/17] libcamera: pipeline:\n\traspberrypi: DmaHeaps: Use UniqueFD for a file descriptor","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":21383,"web_url":"https://patchwork.libcamera.org/comment/21383/","msgid":"<YaWCWP6uczwV5YyF@pendragon.ideasonboard.com>","date":"2021-11-30T01:46:00","subject":"Re: [libcamera-devel] [PATCH v3 15/17] libcamera: pipeline:\n\traspberrypi: DmaHeaps: Use UniqueFD for a file descriptor","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nOn Mon, Nov 29, 2021 at 04:59:47PM +0100, Jacopo Mondi wrote:\n> On Mon, Nov 29, 2021 at 01:57:50AM +0200, Laurent Pinchart wrote:\n> > From: Hirokazu Honda <hiroh@chromium.org>\n> >\n> > Manages a file descriptor owned by DmaHeaps for a dma handle by\n> > UniqueFD. Furthermore, DmaHeaps::alloc() creates a new file\n> > descriptor and the returned file descriptor is owned by a caller.\n> > This also clarifies it by changing the returned value to UniqueFD.\n> >\n> > Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> > ---\n> > Changes since v2:\n> >\n> > - Use default destructor\n> > - Return {}\n> > ---\n> >  .../pipeline/raspberrypi/dma_heaps.cpp        | 28 ++++++++-----------\n> >  .../pipeline/raspberrypi/dma_heaps.h          | 10 ++++---\n> >  .../pipeline/raspberrypi/raspberrypi.cpp      |  6 ++--\n> >  3 files changed, 21 insertions(+), 23 deletions(-)\n> >\n> > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > index 573ea11de607..69831dabbe75 100644\n> > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > @@ -35,7 +35,6 @@ LOG_DECLARE_CATEGORY(RPI)\n> >  namespace RPi {\n> >\n> >  DmaHeap::DmaHeap()\n> > -\t: dmaHeapHandle_(-1)\n> >  {\n> >  \tfor (const char *name : heapNames) {\n> >  \t\tint ret = ::open(name, O_RDWR, 0);\n> > @@ -46,49 +45,44 @@ DmaHeap::DmaHeap()\n> >  \t\t\tcontinue;\n> >  \t\t}\n> >\n> > -\t\tdmaHeapHandle_ = ret;\n> > +\t\tdmaHeapHandle_ = UniqueFD(ret);\n> \n> In this series you have mostly used\n> \n>                 dmaHeapHandle_ = UniqueFD(::open(name, O_RDWR, 0));\n>                 if (dmaHeapHandle_.isValid())\n> \n> but it's ok the way it is\n> \n> >  \t\tbreak;\n> >  \t}\n> >\n> > -\tif (dmaHeapHandle_ < 0)\n> > +\tif (!dmaHeapHandle_.isValid())\n> >  \t\tLOG(RPI, Error) << \"Could not open any dmaHeap device\";\n> >  }\n> >\n> > -DmaHeap::~DmaHeap()\n> > -{\n> > -\tif (dmaHeapHandle_ > -1)\n> > -\t\t::close(dmaHeapHandle_);\n> > -}\n> > +DmaHeap::~DmaHeap() = default;\n> >\n> > -FileDescriptor DmaHeap::alloc(const char *name, std::size_t size)\n> > +UniqueFD DmaHeap::alloc(const char *name, std::size_t size)\n> >  {\n> >  \tint ret;\n> >\n> >  \tif (!name)\n> > -\t\treturn FileDescriptor();\n> > +\t\treturn {};\n> >\n> >  \tstruct dma_heap_allocation_data alloc = {};\n> >\n> >  \talloc.len = size;\n> >  \talloc.fd_flags = O_CLOEXEC | O_RDWR;\n> >\n> > -\tret = ::ioctl(dmaHeapHandle_, DMA_HEAP_IOCTL_ALLOC, &alloc);\n> > -\n> > +\tret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n> >  \tif (ret < 0) {\n> >  \t\tLOG(RPI, Error) << \"dmaHeap allocation failure for \"\n> >  \t\t\t\t<< name;\n> > -\t\treturn FileDescriptor();\n> > +\t\treturn {};\n> >  \t}\n> >\n> > -\tret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);\n> > +\tUniqueFD allocFd(alloc.fd);\n> > +\tret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);\n> >  \tif (ret < 0) {\n> >  \t\tLOG(RPI, Error) << \"dmaHeap naming failure for \"\n> >  \t\t\t\t<< name;\n> > -\t\t::close(alloc.fd);\n> > -\t\treturn FileDescriptor();\n> > +\t\treturn {};\n> >  \t}\n> >\n> > -\treturn FileDescriptor(std::move(alloc.fd));\n> > +\treturn allocFd;\n> >  }\n> >\n> >  } /* namespace RPi */\n> > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > index 57beaeb2e48a..d38f41eae1a2 100644\n> > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > @@ -7,7 +7,9 @@\n> >\n> >  #pragma once\n> >\n> > -#include <libcamera/base/file_descriptor.h>\n> > +#include <stddef.h>\n> \n> what for ?\n\nFor std::size_t.\n\n> > +\n> > +#include <libcamera/base/unique_fd.h>\n> >\n> >  namespace libcamera {\n> >\n> > @@ -18,11 +20,11 @@ class DmaHeap\n> >  public:\n> >  \tDmaHeap();\n> >  \t~DmaHeap();\n> > -\tbool isValid() const { return dmaHeapHandle_ > -1; }\n> > -\tFileDescriptor alloc(const char *name, std::size_t size);\n> > +\tbool isValid() const { return dmaHeapHandle_.isValid(); }\n> > +\tUniqueFD alloc(const char *name, std::size_t size);\n> >\n> >  private:\n> > -\tint dmaHeapHandle_;\n> > +\tUniqueFD dmaHeapHandle_;\n> >  };\n> >\n> >  } /* namespace RPi */\n> > diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > index 7526edf774a2..ffa51a0c65ca 100644\n> > --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > @@ -1361,10 +1361,12 @@ int RPiCameraData::configureIPA(const CameraConfiguration *config)\n> >\n> >  \t/* Allocate the lens shading table via dmaHeap and pass to the IPA. */\n> >  \tif (!lsTable_.isValid()) {\n> > -\t\tlsTable_ = dmaHeap_.alloc(\"ls_grid\", ipa::RPi::MaxLsGridSize);\n> > -\t\tif (!lsTable_.isValid())\n> > +\t\tUniqueFD fd = dmaHeap_.alloc(\"ls_grid\", ipa::RPi::MaxLsGridSize);\n> > +\t\tif (!fd.isValid())\n> >  \t\t\treturn -ENOMEM;\n> >\n> > +\t\tlsTable_ = FileDescriptor(std::move(fd));\n> > +\n> >  \t\t/* Allow the IPA to mmap the LS table via the file descriptor. */\n> >  \t\t/*\n> >  \t\t * \\todo Investigate if mapping the lens shading table buffer","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 6E558BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Nov 2021 01:46:27 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AF5A7605A6;\n\tTue, 30 Nov 2021 02:46:26 +0100 (CET)","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 CC614604FC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 02:46:24 +0100 (CET)","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 3F9F32A5;\n\tTue, 30 Nov 2021 02:46:24 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"M4GTgzLT\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638236784;\n\tbh=fvCumT0dOxUwU9hw3PD58+Q8G+/lKbQ3FM91ea23FqI=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=M4GTgzLT/upmVLsW/GbF2UbKTO0eZ2kGtrjJnmCNaMOZ63TNnPEYFo60G8PE4v4Di\n\txNXjLhOEaVXnm0cE1GguCPv5uF45yeX6ZVtk/seb/3VGLNvic53Eqpwj7tnlmmCOdw\n\tx0BgWXABznpBLq6S7iZG6KegFThE5f12sBV7mGdM=","Date":"Tue, 30 Nov 2021 03:46:00 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<YaWCWP6uczwV5YyF@pendragon.ideasonboard.com>","References":"<20211128235752.10836-1-laurent.pinchart@ideasonboard.com>\n\t<20211128235752.10836-16-laurent.pinchart@ideasonboard.com>\n\t<20211129155947.wqurjqft4hz7lqft@uno.localdomain>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211129155947.wqurjqft4hz7lqft@uno.localdomain>","Subject":"Re: [libcamera-devel] [PATCH v3 15/17] libcamera: pipeline:\n\traspberrypi: DmaHeaps: Use UniqueFD for a file descriptor","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>"}}]