From patchwork Thu Apr 15 08:38:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11945 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 4442EBD224 for ; Thu, 15 Apr 2021 08:39:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 092A968816; Thu, 15 Apr 2021 10:39:04 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="SWEq/gKh"; dkim-atps=neutral Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2992A68822 for ; Thu, 15 Apr 2021 10:39:02 +0200 (CEST) Received: by mail-pf1-x435.google.com with SMTP id c17so15595224pfn.6 for ; Thu, 15 Apr 2021 01:39:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0oMmXz+UiOFrr5cvYdkpEx8rCkMr9w7tm7pVdtG8/HY=; b=SWEq/gKhWDOUu5hqas/KXb8Xiu/m674JXbscSlZLvbip+BZuqVqW81Tqa2wwXqYQe4 9McwoJemahnr8p2sW5ddrSQG+i/P397skIzb8qIfQlEVbm7wpnnr9nyu6rNxEiEHXpkf DtLifPxWEPVcGatmAh1nMSRSjYH/+lnJrkaME= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0oMmXz+UiOFrr5cvYdkpEx8rCkMr9w7tm7pVdtG8/HY=; b=RFjp7h/9mwaJuEt/cDWk/09fB9Fw9d+y6K+slDam7METVOP2xof/FB1AnKdNmB5y0C qlYn1xCUZm8aL5+rotSuTBXoNiKdkZCV1JzpJbAfue4uBXLkFgz/0BtLV9hp4LM3xmPd UoULDx47ysiKXsSf8ZnDU29Nwz4X8pOtrYYZD+KXu+Bq5hG78VtXqyWBNSSdWu774EhV qKzbYWGsXLI4Z9cW4IFh8NSOMmTGgoF8Lfo1XVLihSwwRLA+ttekrv2V5u83OQ2qBgoj 5ccbHqmrUeMkOfdiv4PG9vTSePx5Fdt/PJDf2jkcbbuzAfjs0KSxLLU7iGzluqghmi8G E3gQ== X-Gm-Message-State: AOAM532QroW2sLCUIB3dHZ2GjkTx7qlFQ00VCmWmoebNQ7WT9vOWkTA2 YhxwWiEQmWjLkK5Bpls6xsZeYzVn/X1Ytg== X-Google-Smtp-Source: ABdhPJy4F41uN95O4hH67DqTBL/dAYHLd6Ie+dJCfVvwX+UKH040TV8yi7EZSL1pPEG5BkIfnmNjdA== X-Received: by 2002:a62:7592:0:b029:254:ebcd:802b with SMTP id q140-20020a6275920000b0290254ebcd802bmr2128456pfc.41.1618475940417; Thu, 15 Apr 2021 01:39:00 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:39:00 -0700 (PDT) From: Hirokazu Honda 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: raspberrypi: DmaHeaps: Use ScopedFD for a file descriptor X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" DmaHeaps owns a file descriptor for a dma handle. It should be managed by ScopedFD to avoid the leakage. Furthermore, DmaHeaps::alloc() creates a new file descriptor and the returned file descriptor is owned by a caller. This also clarifies it by changing the returned value to ScopedFD. Signed-off-by: Hirokazu Honda --- .../pipeline/raspberrypi/dma_heaps.cpp | 23 ++++++++----------- .../pipeline/raspberrypi/dma_heaps.h | 10 ++++---- .../pipeline/raspberrypi/raspberrypi.cpp | 7 ++++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp index 4d5dd6cb..d5b000b8 100644 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp @@ -35,7 +35,6 @@ LOG_DECLARE_CATEGORY(RPI) namespace RPi { DmaHeap::DmaHeap() - : dmaHeapHandle_(-1) { for (const char *name : heapNames) { int ret = ::open(name, O_RDWR, 0); @@ -46,49 +45,47 @@ DmaHeap::DmaHeap() continue; } - dmaHeapHandle_ = ret; + dmaHeapHandle_ = ScopedFD(ret); break; } - if (dmaHeapHandle_ < 0) + if (!dmaHeapHandle_.isValid()) LOG(RPI, Error) << "Could not open any dmaHeap device"; } DmaHeap::~DmaHeap() { - if (dmaHeapHandle_ > -1) - ::close(dmaHeapHandle_); } -FileDescriptor DmaHeap::alloc(const char *name, std::size_t size) +ScopedFD DmaHeap::alloc(const char *name, std::size_t size) { int ret; if (!name) - return FileDescriptor(); + return ScopedFD(); struct dma_heap_allocation_data alloc = {}; alloc.len = size; alloc.fd_flags = O_CLOEXEC | O_RDWR; - ret = ::ioctl(dmaHeapHandle_, DMA_HEAP_IOCTL_ALLOC, &alloc); + ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc); if (ret < 0) { LOG(RPI, Error) << "dmaHeap allocation failure for " << name; - return FileDescriptor(); + return ScopedFD(); } - ret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name); + ScopedFD allocFd(alloc.fd); + ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name); if (ret < 0) { LOG(RPI, Error) << "dmaHeap naming failure for " << name; - ::close(alloc.fd); - return FileDescriptor(); + return ScopedFD(); } - return FileDescriptor(std::move(alloc.fd)); + return allocFd; } } /* namespace RPi */ diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h index 79f39c51..c49772df 100644 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h @@ -7,7 +7,9 @@ #ifndef __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__ #define __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__ -#include +#include + +#include namespace libcamera { @@ -18,11 +20,11 @@ class DmaHeap public: DmaHeap(); ~DmaHeap(); - bool isValid() const { return dmaHeapHandle_ > -1; } - FileDescriptor alloc(const char *name, std::size_t size); + bool isValid() const { return dmaHeapHandle_.isValid(); } + ScopedFD alloc(const char *name, std::size_t size); private: - int dmaHeapHandle_; + ScopedFD dmaHeapHandle_; }; } /* namespace RPi */ diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index f22e286e..0075fdb1 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -1256,10 +1256,13 @@ int RPiCameraData::configureIPA(const CameraConfiguration *config) /* Allocate the lens shading table via dmaHeap and pass to the IPA. */ if (!lsTable_.isValid()) { - lsTable_ = dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize); - if (!lsTable_.isValid()) + ScopedFD scopedFD = + dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize); + if (!scopedFD.isValid()) return -ENOMEM; + lsTable_ = FileDescriptor(scopedFD.release()); + /* Allow the IPA to mmap the LS table via the file descriptor. */ /* * \todo Investigate if mapping the lens shading table buffer