{"id":11945,"url":"https://patchwork.libcamera.org/api/patches/11945/?format=json","web_url":"https://patchwork.libcamera.org/patch/11945/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210415083843.3399502-9-hiroh@chromium.org>","date":"2021-04-15T08:38:42","name":"[libcamera-devel,RFC,09/10] libcamera: pipeline: raspberrypi: DmaHeaps: Use ScopedFD for a file descriptor","commit_ref":null,"pull_url":null,"state":"changes-requested","archived":false,"hash":"dbe6a32defae04f107b334ae5e71c615727fb62c","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/people/63/?format=json","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/11945/mbox/","series":[{"id":1933,"url":"https://patchwork.libcamera.org/api/series/1933/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1933","date":"2021-04-15T08:38:35","name":null,"version":1,"mbox":"https://patchwork.libcamera.org/series/1933/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/11945/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11945/checks/","tags":{},"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 4442EBD224\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 15 Apr 2021 08:39:04 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 092A968816;\n\tThu, 15 Apr 2021 10:39:04 +0200 (CEST)","from mail-pf1-x435.google.com (mail-pf1-x435.google.com\n\t[IPv6:2607:f8b0:4864:20::435])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2992A68822\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 10:39:02 +0200 (CEST)","by mail-pf1-x435.google.com with SMTP id c17so15595224pfn.6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 01:39:02 -0700 (PDT)","from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:2:84f5:7981:dfbe:8f02])\n\tby smtp.gmail.com with ESMTPSA id\n\t205sm1520258pfc.201.2021.04.15.01.38.59\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 15 Apr 2021 01:39:00 -0700 (PDT)"],"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=\"SWEq/gKh\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=0oMmXz+UiOFrr5cvYdkpEx8rCkMr9w7tm7pVdtG8/HY=;\n\tb=SWEq/gKhWDOUu5hqas/KXb8Xiu/m674JXbscSlZLvbip+BZuqVqW81Tqa2wwXqYQe4\n\t9McwoJemahnr8p2sW5ddrSQG+i/P397skIzb8qIfQlEVbm7wpnnr9nyu6rNxEiEHXpkf\n\tDtLifPxWEPVcGatmAh1nMSRSjYH/+lnJrkaME=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=0oMmXz+UiOFrr5cvYdkpEx8rCkMr9w7tm7pVdtG8/HY=;\n\tb=RFjp7h/9mwaJuEt/cDWk/09fB9Fw9d+y6K+slDam7METVOP2xof/FB1AnKdNmB5y0C\n\tqlYn1xCUZm8aL5+rotSuTBXoNiKdkZCV1JzpJbAfue4uBXLkFgz/0BtLV9hp4LM3xmPd\n\tUoULDx47ysiKXsSf8ZnDU29Nwz4X8pOtrYYZD+KXu+Bq5hG78VtXqyWBNSSdWu774EhV\n\tqKzbYWGsXLI4Z9cW4IFh8NSOMmTGgoF8Lfo1XVLihSwwRLA+ttekrv2V5u83OQ2qBgoj\n\t5ccbHqmrUeMkOfdiv4PG9vTSePx5Fdt/PJDf2jkcbbuzAfjs0KSxLLU7iGzluqghmi8G\n\tE3gQ==","X-Gm-Message-State":"AOAM532QroW2sLCUIB3dHZ2GjkTx7qlFQ00VCmWmoebNQ7WT9vOWkTA2\n\tYhxwWiEQmWjLkK5Bpls6xsZeYzVn/X1Ytg==","X-Google-Smtp-Source":"ABdhPJy4F41uN95O4hH67DqTBL/dAYHLd6Ie+dJCfVvwX+UKH040TV8yi7EZSL1pPEG5BkIfnmNjdA==","X-Received":"by 2002:a62:7592:0:b029:254:ebcd:802b with SMTP id\n\tq140-20020a6275920000b0290254ebcd802bmr2128456pfc.41.1618475940417; \n\tThu, 15 Apr 2021 01:39:00 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 15 Apr 2021 17:38:42 +0900","Message-Id":"<20210415083843.3399502-9-hiroh@chromium.org>","X-Mailer":"git-send-email 2.31.1.368.gbe11c130af-goog","In-Reply-To":"<20210415083843.3399502-1-hiroh@chromium.org>","References":"<20210415083843.3399502-1-hiroh@chromium.org>","MIME-Version":"1.0","Subject":"[libcamera-devel] [RFC PATCH 09/10] libcamera: pipeline:\n\traspberrypi: DmaHeaps: Use ScopedFD 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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"DmaHeaps owns a file descriptor for a dma handle. It should be\nmanaged by ScopedFD to avoid the leakage. Furthermore,\nDmaHeaps::alloc() creates a new file descriptor and the returned\nfile descriptor is owned by a caller. This also clarifies it by\nchanging the returned value to ScopedFD.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n .../pipeline/raspberrypi/dma_heaps.cpp        | 23 ++++++++-----------\n .../pipeline/raspberrypi/dma_heaps.h          | 10 ++++----\n .../pipeline/raspberrypi/raspberrypi.cpp      |  7 ++++--\n 3 files changed, 21 insertions(+), 19 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\nindex 4d5dd6cb..d5b000b8 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,47 @@ DmaHeap::DmaHeap()\n \t\t\tcontinue;\n \t\t}\n \n-\t\tdmaHeapHandle_ = ret;\n+\t\tdmaHeapHandle_ = ScopedFD(ret);\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 \n-FileDescriptor DmaHeap::alloc(const char *name, std::size_t size)\n+ScopedFD DmaHeap::alloc(const char *name, std::size_t size)\n {\n \tint ret;\n \n \tif (!name)\n-\t\treturn FileDescriptor();\n+\t\treturn ScopedFD();\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+\tret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n \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 ScopedFD();\n \t}\n \n-\tret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);\n+\tScopedFD 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 ScopedFD();\n \t}\n \n-\treturn FileDescriptor(std::move(alloc.fd));\n+\treturn allocFd;\n }\n \n } /* namespace RPi */\ndiff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\nindex 79f39c51..c49772df 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 #ifndef __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__\n #define __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__\n \n-#include <libcamera/file_descriptor.h>\n+#include <cstddef>\n+\n+#include <libcamera/scoped_file_descriptor.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+\tScopedFD alloc(const char *name, std::size_t size);\n \n private:\n-\tint dmaHeapHandle_;\n+\tScopedFD dmaHeapHandle_;\n };\n \n } /* namespace RPi */\ndiff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex f22e286e..0075fdb1 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -1256,10 +1256,13 @@ 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\tScopedFD scopedFD =\n+\t\t\tdmaHeap_.alloc(\"ls_grid\", ipa::RPi::MaxLsGridSize);\n+\t\tif (!scopedFD.isValid())\n \t\t\treturn -ENOMEM;\n \n+\t\tlsTable_ = FileDescriptor(scopedFD.release());\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","prefixes":["libcamera-devel","RFC","09/10"]}