[{"id":29683,"web_url":"https://patchwork.libcamera.org/comment/29683/","msgid":"<171710856853.372008.9780861300286677569@ping.linuxembedded.co.uk>","date":"2024-05-30T22:36:08","subject":"Re: [PATCH v2 4/5] libcamera: DmaBufAllocator: Support allocating\n\tfrom /dev/udmabuf","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Hans de Goede (2024-05-30 18:15:59)\n> The dma-buf allocator currently allocates from CMA and system heaps.\n> \n> Extend the dma-buf allocator to support allocating dma-buffers by creating\n> memfd-s and turning those into dma-buffers using /dev/udmabuf.\n> \n> The buffers allocated through memfd/udmabuf are not suitable for zero-copy\n> buffer sharing with other devices.\n> \n> Co-developed-by: Harvey Yang <chenghaoyang@chromium.org>\n> Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> Signed-off-by: Hans de Goede <hdegoede@redhat.com>\n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n> Changes in v2:\n> - Reword the commit message\n> - Add a new DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf type for udmabuf\n> - Drop unnecessary size != size check\n> - Reword log messages to be more like the DMA heap alloc path\n> - Move UniqueFD(ret) up so as to not leak the fd on errors\n> ---\n>  .../libcamera/internal/dma_buf_allocator.h    |   4 +\n>  src/libcamera/dma_buf_allocator.cpp           | 111 +++++++++++++++---\n>  2 files changed, 100 insertions(+), 15 deletions(-)\n> \n> diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h\n> index a881042e..36ec1696 100644\n> --- a/include/libcamera/internal/dma_buf_allocator.h\n> +++ b/include/libcamera/internal/dma_buf_allocator.h\n> @@ -20,6 +20,7 @@ public:\n>         enum class DmaBufAllocatorFlag {\n>                 CmaHeap = 1 << 0,\n>                 SystemHeap = 1 << 1,\n> +               UDmaBuf = 1 << 2,\n>         };\n>  \n>         using DmaBufAllocatorFlags = Flags<DmaBufAllocatorFlag>;\n> @@ -30,7 +31,10 @@ public:\n>         UniqueFD alloc(const char *name, std::size_t size);\n>  \n>  private:\n> +       UniqueFD allocFromHeap(const char *name, std::size_t size);\n> +       UniqueFD allocFromUDmaBuf(const char *name, std::size_t size);\n>         UniqueFD providerHandle_;\n> +       DmaBufAllocatorFlag type_;\n>  };\n>  \n>  LIBCAMERA_FLAGS_ENABLE_OPERATORS(DmaBufAllocator::DmaBufAllocatorFlag)\n> diff --git a/src/libcamera/dma_buf_allocator.cpp b/src/libcamera/dma_buf_allocator.cpp\n> index bc0d78ef..369a419a 100644\n> --- a/src/libcamera/dma_buf_allocator.cpp\n> +++ b/src/libcamera/dma_buf_allocator.cpp\n> @@ -11,10 +11,14 @@\n>  #include <array>\n>  #include <fcntl.h>\n>  #include <sys/ioctl.h>\n> +#include <sys/mman.h>\n> +#include <sys/stat.h>\n> +#include <sys/types.h>\n>  #include <unistd.h>\n>  \n>  #include <linux/dma-buf.h>\n>  #include <linux/dma-heap.h>\n> +#include <linux/udmabuf.h>\n>  \n>  #include <libcamera/base/log.h>\n>  \n> @@ -32,7 +36,7 @@ struct DmaBufAllocatorInfo {\n>  };\n>  #endif\n>  \n> -static constexpr std::array<DmaBufAllocatorInfo, 3> providerInfos = { {\n> +static constexpr std::array<DmaBufAllocatorInfo, 4> providerInfos = { {\n>         /*\n>          * /dev/dma_heap/linux,cma is the CMA dma-heap. When the cma heap size is\n>          * specified on the kernel command line, this gets renamed to \"reserved\".\n> @@ -40,6 +44,7 @@ static constexpr std::array<DmaBufAllocatorInfo, 3> providerInfos = { {\n>         { DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap, \"/dev/dma_heap/linux,cma\" },\n>         { DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap, \"/dev/dma_heap/reserved\" },\n>         { DmaBufAllocator::DmaBufAllocatorFlag::SystemHeap, \"/dev/dma_heap/system\" },\n> +       { DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf, \"/dev/udmabuf\" },\n>  } };\n>  \n>  LOG_DEFINE_CATEGORY(DmaBufAllocator)\n> @@ -63,6 +68,8 @@ LOG_DEFINE_CATEGORY(DmaBufAllocator)\n>   * \\brief Allocate from a CMA dma-heap, providing physically-contiguous memory\n>   * \\var DmaBufAllocator::SystemHeap\n>   * \\brief Allocate from the system dma-heap, using the page allocator\n> + * \\var DmaBufAllocator::UDmaBuf\n> + * \\brief Allocate using a memfd + /dev/udmabuf\n>   */\n>  \n>  /**\n> @@ -99,6 +106,7 @@ DmaBufAllocator::DmaBufAllocator(DmaBufAllocatorFlags type)\n>  \n>                 LOG(DmaBufAllocator, Debug) << \"Using \" << info.deviceNodeName;\n>                 providerHandle_ = UniqueFD(ret);\n> +               type_ = info.type;\n>                 break;\n>         }\n>  \n> @@ -117,25 +125,76 @@ DmaBufAllocator::~DmaBufAllocator() = default;\n>   * \\return True if the DmaBufAllocator is valid, false otherwise\n>   */\n>  \n> -/**\n> - * \\brief Allocate a dma-buf from the DmaBufAllocator\n> - * \\param [in] name The name to set for the allocated buffer\n> - * \\param [in] size The size of the buffer to allocate\n> - *\n> - * Allocates a dma-buf with read/write access.\n> - *\n> - * If the allocation fails, return an invalid UniqueFD.\n> - *\n> - * \\return The UniqueFD of the allocated buffer\n> - */\n> -UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size)\n> +UniqueFD DmaBufAllocator::allocFromUDmaBuf(const char *name, std::size_t size)\n>  {\n> -       int ret;\n> +       /* Size must be a multiple of the page-size round it up. */\n> +       std::size_t pageMask = sysconf(_SC_PAGESIZE) - 1;\n> +       size = (size + pageMask) & ~pageMask;\n>  \n> -       if (!name)\n> +       int ret = memfd_create(name, MFD_ALLOW_SEALING);\n> +       if (ret < 0) {\n> +               ret = errno;\n> +               LOG(DmaBufAllocator, Error)\n> +                       << \"Failed to allocate memfd storage for \" << name\n> +                       << \": \" << strerror(ret);\n>                 return {};\n> +       }\n>  \n> +       UniqueFD memfd(ret);\n> +\n> +       ret = ftruncate(memfd.get(), size);\n> +       if (ret < 0) {\n> +               ret = errno;\n> +               LOG(DmaBufAllocator, Error)\n> +                       << \"Failed to set memfd size for \" << name\n> +                       << \": \" << strerror(ret);\n> +               return {};\n> +       }\n> +\n> +       /* udmabuf dma-buffers *must* have the F_SEAL_SHRINK seal. */\n> +       ret = fcntl(memfd.get(), F_ADD_SEALS, F_SEAL_SHRINK);\n> +       if (ret < 0) {\n> +               ret = errno;\n> +               LOG(DmaBufAllocator, Error)\n> +                       << \"Failed to seal the memfd for \" << name\n> +                       << \": \" << strerror(ret);\n> +               return {};\n> +       }\n> +\n> +       struct udmabuf_create create;\n> +\n> +       create.memfd = memfd.get();\n> +       create.flags = UDMABUF_FLAGS_CLOEXEC;\n> +       create.offset = 0;\n> +       create.size = size;\n> +\n> +       ret = ::ioctl(providerHandle_.get(), UDMABUF_CREATE, &create);\n> +       if (ret < 0) {\n> +               ret = errno;\n> +               LOG(DmaBufAllocator, Error)\n> +                       << \"Failed to allocate \" << size << \" bytes for \"\n> +                       << name << \": \" << strerror(ret);\n> +               return {};\n> +       }\n> +\n> +       /* The underlying memfd is kept as as a reference in the kernel */\n> +       UniqueFD uDma(ret);\n> +\n> +       /* Fail if not suitable, the allocation will be free'd by UniqueFD */\n> +       if (create.size < size) {\n> +               LOG(DmaBufAllocator, Error)\n> +                       << \"UDMABUF_CREATE for \" << name << \" allocated \"\n> +                       << create.size << \" bytes instead of \" << size << \" bytes\";\n> +               return {};\n> +       }\n> +\n> +       return uDma;\n> +}\n> +\n> +UniqueFD DmaBufAllocator::allocFromHeap(const char *name, std::size_t size)\n> +{\n>         struct dma_heap_allocation_data alloc = {};\n> +       int ret;\n>  \n>         alloc.len = size;\n>         alloc.fd_flags = O_CLOEXEC | O_RDWR;\n> @@ -156,4 +215,26 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size)\n>         return allocFd;\n>  }\n>  \n> +/**\n> + * \\brief Allocate a dma-buf from the DmaBufAllocator\n> + * \\param [in] name The name to set for the allocated buffer\n> + * \\param [in] size The size of the buffer to allocate\n> + *\n> + * Allocates a dma-buf with read/write access.\n> + *\n> + * If the allocation fails, return an invalid UniqueFD.\n> + *\n> + * \\return The UniqueFD of the allocated buffer\n> + */\n> +UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size)\n> +{\n> +       if (!name)\n> +               return {};\n> +\n> +       if (type_ == DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf)\n> +               return allocFromUDmaBuf(name, size);\n> +       else\n> +               return allocFromHeap(name, size);\n> +}\n> +\n>  } /* namespace libcamera */\n> -- \n> 2.45.1\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 5E9ECBD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 30 May 2024 22:36:14 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 38790634B6;\n\tFri, 31 May 2024 00:36:13 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 943DA6347E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 31 May 2024 00:36:11 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id BF18DA06;\n\tFri, 31 May 2024 00:36:06 +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=\"JLoakio+\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1717108566;\n\tbh=/BNO7XNXY3zDxL3uaPcvxU2QhkF+Tyh44rdFrq8aeAw=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=JLoakio+/gjGvLHzEb3KFBi7UuOCtA/huAcwWqrCLv9KyHSp5jDSydRpj8SVwh9aS\n\tRAhvoxAZkhCwMMYfTZYrKQAGmCN7JGKL7eWk74k1c+48WqzEk6Wk8d6oARqBSjJ7pI\n\tVEj3PvsX/Mi70pllhHSWo/yOuY90IrUPgu5oKgvk=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20240530171600.259495-5-hdegoede@redhat.com>","References":"<20240530171600.259495-1-hdegoede@redhat.com>\n\t<20240530171600.259495-5-hdegoede@redhat.com>","Subject":"Re: [PATCH v2 4/5] libcamera: DmaBufAllocator: Support allocating\n\tfrom /dev/udmabuf","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Andrey Konovalov <andrey.konovalov.ynk@gmail.com>,\n\tPavel Machek <pavel@ucw.cz>,\n\tBryan O'Donoghue <bryan.odonoghue@linaro.org>, \n\tMilan Zamazal <mzamazal@redhat.com>, Maxime Ripard <mripard@redhat.com>, \n\tHans de Goede <hdegoede@redhat.com>,\n\tHarvey Yang <chenghaoyang@chromium.org>","To":"Hans de Goede <hdegoede@redhat.com>, libcamera-devel@lists.libcamera.org","Date":"Thu, 30 May 2024 23:36:08 +0100","Message-ID":"<171710856853.372008.9780861300286677569@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","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>"}},{"id":29731,"web_url":"https://patchwork.libcamera.org/comment/29731/","msgid":"<20240601233721.GF6683@pendragon.ideasonboard.com>","date":"2024-06-01T23:37:21","subject":"Re: [PATCH v2 4/5] libcamera: DmaBufAllocator: Support allocating\n\tfrom /dev/udmabuf","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hans,\n\nThank you for the patch.\n\nOn Thu, May 30, 2024 at 07:15:59PM +0200, Hans de Goede wrote:\n> The dma-buf allocator currently allocates from CMA and system heaps.\n> \n> Extend the dma-buf allocator to support allocating dma-buffers by creating\n> memfd-s and turning those into dma-buffers using /dev/udmabuf.\n> \n> The buffers allocated through memfd/udmabuf are not suitable for zero-copy\n> buffer sharing with other devices.\n> \n> Co-developed-by: Harvey Yang <chenghaoyang@chromium.org>\n> Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> Signed-off-by: Hans de Goede <hdegoede@redhat.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n> Changes in v2:\n> - Reword the commit message\n> - Add a new DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf type for udmabuf\n> - Drop unnecessary size != size check\n> - Reword log messages to be more like the DMA heap alloc path\n> - Move UniqueFD(ret) up so as to not leak the fd on errors\n> ---\n>  .../libcamera/internal/dma_buf_allocator.h    |   4 +\n>  src/libcamera/dma_buf_allocator.cpp           | 111 +++++++++++++++---\n>  2 files changed, 100 insertions(+), 15 deletions(-)\n> \n> diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h\n> index a881042e..36ec1696 100644\n> --- a/include/libcamera/internal/dma_buf_allocator.h\n> +++ b/include/libcamera/internal/dma_buf_allocator.h\n> @@ -20,6 +20,7 @@ public:\n>  \tenum class DmaBufAllocatorFlag {\n>  \t\tCmaHeap = 1 << 0,\n>  \t\tSystemHeap = 1 << 1,\n> +\t\tUDmaBuf = 1 << 2,\n>  \t};\n>  \n>  \tusing DmaBufAllocatorFlags = Flags<DmaBufAllocatorFlag>;\n> @@ -30,7 +31,10 @@ public:\n>  \tUniqueFD alloc(const char *name, std::size_t size);\n>  \n>  private:\n> +\tUniqueFD allocFromHeap(const char *name, std::size_t size);\n> +\tUniqueFD allocFromUDmaBuf(const char *name, std::size_t size);\n>  \tUniqueFD providerHandle_;\n> +\tDmaBufAllocatorFlag type_;\n>  };\n>  \n>  LIBCAMERA_FLAGS_ENABLE_OPERATORS(DmaBufAllocator::DmaBufAllocatorFlag)\n> diff --git a/src/libcamera/dma_buf_allocator.cpp b/src/libcamera/dma_buf_allocator.cpp\n> index bc0d78ef..369a419a 100644\n> --- a/src/libcamera/dma_buf_allocator.cpp\n> +++ b/src/libcamera/dma_buf_allocator.cpp\n> @@ -11,10 +11,14 @@\n>  #include <array>\n>  #include <fcntl.h>\n>  #include <sys/ioctl.h>\n> +#include <sys/mman.h>\n> +#include <sys/stat.h>\n> +#include <sys/types.h>\n>  #include <unistd.h>\n>  \n>  #include <linux/dma-buf.h>\n>  #include <linux/dma-heap.h>\n> +#include <linux/udmabuf.h>\n>  \n>  #include <libcamera/base/log.h>\n>  \n> @@ -32,7 +36,7 @@ struct DmaBufAllocatorInfo {\n>  };\n>  #endif\n>  \n> -static constexpr std::array<DmaBufAllocatorInfo, 3> providerInfos = { {\n> +static constexpr std::array<DmaBufAllocatorInfo, 4> providerInfos = { {\n>  \t/*\n>  \t * /dev/dma_heap/linux,cma is the CMA dma-heap. When the cma heap size is\n>  \t * specified on the kernel command line, this gets renamed to \"reserved\".\n> @@ -40,6 +44,7 @@ static constexpr std::array<DmaBufAllocatorInfo, 3> providerInfos = { {\n>  \t{ DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap, \"/dev/dma_heap/linux,cma\" },\n>  \t{ DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap, \"/dev/dma_heap/reserved\" },\n>  \t{ DmaBufAllocator::DmaBufAllocatorFlag::SystemHeap, \"/dev/dma_heap/system\" },\n> +\t{ DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf, \"/dev/udmabuf\" },\n>  } };\n>  \n>  LOG_DEFINE_CATEGORY(DmaBufAllocator)\n> @@ -63,6 +68,8 @@ LOG_DEFINE_CATEGORY(DmaBufAllocator)\n>   * \\brief Allocate from a CMA dma-heap, providing physically-contiguous memory\n>   * \\var DmaBufAllocator::SystemHeap\n>   * \\brief Allocate from the system dma-heap, using the page allocator\n> + * \\var DmaBufAllocator::UDmaBuf\n> + * \\brief Allocate using a memfd + /dev/udmabuf\n>   */\n>  \n>  /**\n> @@ -99,6 +106,7 @@ DmaBufAllocator::DmaBufAllocator(DmaBufAllocatorFlags type)\n>  \n>  \t\tLOG(DmaBufAllocator, Debug) << \"Using \" << info.deviceNodeName;\n>  \t\tproviderHandle_ = UniqueFD(ret);\n> +\t\ttype_ = info.type;\n>  \t\tbreak;\n>  \t}\n>  \n> @@ -117,25 +125,76 @@ DmaBufAllocator::~DmaBufAllocator() = default;\n>   * \\return True if the DmaBufAllocator is valid, false otherwise\n>   */\n>  \n> -/**\n> - * \\brief Allocate a dma-buf from the DmaBufAllocator\n> - * \\param [in] name The name to set for the allocated buffer\n> - * \\param [in] size The size of the buffer to allocate\n> - *\n> - * Allocates a dma-buf with read/write access.\n> - *\n> - * If the allocation fails, return an invalid UniqueFD.\n> - *\n> - * \\return The UniqueFD of the allocated buffer\n> - */\n> -UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size)\n> +UniqueFD DmaBufAllocator::allocFromUDmaBuf(const char *name, std::size_t size)\n>  {\n> -\tint ret;\n> +\t/* Size must be a multiple of the page-size round it up. */\n\n\t/* Size must be a multiple of the page size. Round it up. */\n\n> +\tstd::size_t pageMask = sysconf(_SC_PAGESIZE) - 1;\n> +\tsize = (size + pageMask) & ~pageMask;\n>  \n> -\tif (!name)\n> +\tint ret = memfd_create(name, MFD_ALLOW_SEALING);\n> +\tif (ret < 0) {\n> +\t\tret = errno;\n> +\t\tLOG(DmaBufAllocator, Error)\n> +\t\t\t<< \"Failed to allocate memfd storage for \" << name\n> +\t\t\t<< \": \" << strerror(ret);\n>  \t\treturn {};\n> +\t}\n>  \n> +\tUniqueFD memfd(ret);\n> +\n> +\tret = ftruncate(memfd.get(), size);\n> +\tif (ret < 0) {\n> +\t\tret = errno;\n> +\t\tLOG(DmaBufAllocator, Error)\n> +\t\t\t<< \"Failed to set memfd size for \" << name\n> +\t\t\t<< \": \" << strerror(ret);\n> +\t\treturn {};\n> +\t}\n> +\n> +\t/* udmabuf dma-buffers *must* have the F_SEAL_SHRINK seal. */\n> +\tret = fcntl(memfd.get(), F_ADD_SEALS, F_SEAL_SHRINK);\n> +\tif (ret < 0) {\n> +\t\tret = errno;\n> +\t\tLOG(DmaBufAllocator, Error)\n> +\t\t\t<< \"Failed to seal the memfd for \" << name\n> +\t\t\t<< \": \" << strerror(ret);\n> +\t\treturn {};\n> +\t}\n> +\n> +\tstruct udmabuf_create create;\n> +\n> +\tcreate.memfd = memfd.get();\n> +\tcreate.flags = UDMABUF_FLAGS_CLOEXEC;\n> +\tcreate.offset = 0;\n> +\tcreate.size = size;\n> +\n> +\tret = ::ioctl(providerHandle_.get(), UDMABUF_CREATE, &create);\n> +\tif (ret < 0) {\n> +\t\tret = errno;\n> +\t\tLOG(DmaBufAllocator, Error)\n> +\t\t\t<< \"Failed to allocate \" << size << \" bytes for \"\n> +\t\t\t<< name << \": \" << strerror(ret);\n\nThis doesn't allocate memory, did you mean\n\n\t\tLOG(DmaBufAllocator, Error)\n\t\t\t<< \"Failed to create dma buf for \" << name << \": \"\n\t\t\t<< strerror(ret);\n\n?\n\n> +\t\treturn {};\n> +\t}\n> +\n> +\t/* The underlying memfd is kept as as a reference in the kernel */\n\ns/kernel/kernel./\n\n> +\tUniqueFD uDma(ret);\n> +\n> +\t/* Fail if not suitable, the allocation will be free'd by UniqueFD */\n\ns/UniqueFD/UniqueFD./\n\n> +\tif (create.size < size) {\n\nThis can't happen, at least with the current implementation in the\nkernel, as UDMABUF_CREATE doesn't update the struct udmabuf_create\nargument but instead returns an error if something goes wrong. I think\nyou can drop this check, and just do\n\n\treturn UniqueFD(ret);\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\t\tLOG(DmaBufAllocator, Error)\n> +\t\t\t<< \"UDMABUF_CREATE for \" << name << \" allocated \"\n> +\t\t\t<< create.size << \" bytes instead of \" << size << \" bytes\";\n> +\t\treturn {};\n> +\t}\n> +\n> +\treturn uDma;\n> +}\n> +\n> +UniqueFD DmaBufAllocator::allocFromHeap(const char *name, std::size_t size)\n> +{\n>  \tstruct dma_heap_allocation_data alloc = {};\n> +\tint ret;\n>  \n>  \talloc.len = size;\n>  \talloc.fd_flags = O_CLOEXEC | O_RDWR;\n> @@ -156,4 +215,26 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size)\n>  \treturn allocFd;\n>  }\n>  \n> +/**\n> + * \\brief Allocate a dma-buf from the DmaBufAllocator\n> + * \\param [in] name The name to set for the allocated buffer\n> + * \\param [in] size The size of the buffer to allocate\n> + *\n> + * Allocates a dma-buf with read/write access.\n> + *\n> + * If the allocation fails, return an invalid UniqueFD.\n> + *\n> + * \\return The UniqueFD of the allocated buffer\n> + */\n> +UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size)\n> +{\n> +\tif (!name)\n> +\t\treturn {};\n> +\n> +\tif (type_ == DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf)\n> +\t\treturn allocFromUDmaBuf(name, size);\n> +\telse\n> +\t\treturn allocFromHeap(name, size);\n> +}\n> +\n>  } /* namespace libcamera */","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 4E852BD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat,  1 Jun 2024 23:37:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E62E5634C9;\n\tSun,  2 Jun 2024 01:37:36 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7F6C761A46\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  2 Jun 2024 01:37:35 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 33D681BA;\n\tSun,  2 Jun 2024 01:37:29 +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=\"ijklVL+L\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1717285049;\n\tbh=BSXttZ4N0i8HvSzCoCUHBskt0TY3+1QIqX0Vy6hQ8dw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=ijklVL+LFazQLQTtOFQzCkeMfmP+n9i/3VRaus8DyNSF3P1SMihikRS8JrluerNFX\n\t4vYJ/ZpZEEHeiwWqlAkiyL7lg1Lm70pDvdj2PqcPTZVv8Il5ZWsOXLYkbcIdhvOeN8\n\t5zLbLte9q1VcE/F104CaQ8ASLVdqvAAnttp2Kk5s=","Date":"Sun, 2 Jun 2024 02:37:21 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hans de Goede <hdegoede@redhat.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tAndrey Konovalov <andrey.konovalov.ynk@gmail.com>,\n\tPavel Machek <pavel@ucw.cz>,\n\tBryan O'Donoghue <bryan.odonoghue@linaro.org>, \n\tMilan Zamazal <mzamazal@redhat.com>, Maxime Ripard <mripard@redhat.com>, \n\tHarvey Yang <chenghaoyang@chromium.org>","Subject":"Re: [PATCH v2 4/5] libcamera: DmaBufAllocator: Support allocating\n\tfrom /dev/udmabuf","Message-ID":"<20240601233721.GF6683@pendragon.ideasonboard.com>","References":"<20240530171600.259495-1-hdegoede@redhat.com>\n\t<20240530171600.259495-5-hdegoede@redhat.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20240530171600.259495-5-hdegoede@redhat.com>","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>"}}]