[libcamera-devel,v4,17/22] libcamera: pipeline: raspberrypi: DmaHeaps: Use UniqueFD for a file descriptor
diff mbox series

Message ID 20211130033820.18235-18-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • libcamera: Introduce UniqueFD
Related show

Commit Message

Laurent Pinchart Nov. 30, 2021, 3:38 a.m. UTC
From: Hirokazu Honda <hiroh@chromium.org>

Manages a file descriptor owned by DmaHeaps for a dma handle by
UniqueFD. 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 UniqueFD.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
---
Changes since v2:

- Use default destructor
- Return {}
---
 .../pipeline/raspberrypi/dma_heaps.cpp        | 28 ++++++++-----------
 .../pipeline/raspberrypi/dma_heaps.h          | 10 ++++---
 .../pipeline/raspberrypi/raspberrypi.cpp      |  6 ++--
 3 files changed, 21 insertions(+), 23 deletions(-)

Comments

Jacopo Mondi Nov. 30, 2021, 8:06 a.m. UTC | #1
Hi LAurent

On Tue, Nov 30, 2021 at 05:38:15AM +0200, Laurent Pinchart wrote:
> From: Hirokazu Honda <hiroh@chromium.org>
>
> Manages a file descriptor owned by DmaHeaps for a dma handle by
> UniqueFD. 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 UniqueFD.
>
> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
> ---
> Changes since v2:
>
> - Use default destructor
> - Return {}
> ---
>  .../pipeline/raspberrypi/dma_heaps.cpp        | 28 ++++++++-----------
>  .../pipeline/raspberrypi/dma_heaps.h          | 10 ++++---
>  .../pipeline/raspberrypi/raspberrypi.cpp      |  6 ++--
>  3 files changed, 21 insertions(+), 23 deletions(-)
>
> diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp
> index 573ea11de607..69831dabbe75 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,44 @@ DmaHeap::DmaHeap()
>  			continue;
>  		}
>
> -		dmaHeapHandle_ = ret;
> +		dmaHeapHandle_ = UniqueFD(ret);
>  		break;
>  	}
>
> -	if (dmaHeapHandle_ < 0)
> +	if (!dmaHeapHandle_.isValid())
>  		LOG(RPI, Error) << "Could not open any dmaHeap device";
>  }
>
> -DmaHeap::~DmaHeap()
> -{
> -	if (dmaHeapHandle_ > -1)
> -		::close(dmaHeapHandle_);
> -}
> +DmaHeap::~DmaHeap() = default;
>
> -FileDescriptor DmaHeap::alloc(const char *name, std::size_t size)
> +UniqueFD DmaHeap::alloc(const char *name, std::size_t size)
>  {
>  	int ret;
>
>  	if (!name)
> -		return FileDescriptor();
> +		return {};
>
>  	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 {};
>  	}
>
> -	ret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);
> +	UniqueFD 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 {};
>  	}
>
> -	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 57beaeb2e48a..d38f41eae1a2 100644
> --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h
> +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h
> @@ -7,7 +7,9 @@
>
>  #pragma once
>
> -#include <libcamera/base/file_descriptor.h>
> +#include <stddef.h>
> +
> +#include <libcamera/base/unique_fd.h>
>
>  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(); }
> +	UniqueFD alloc(const char *name, std::size_t size);
>
>  private:
> -	int dmaHeapHandle_;
> +	UniqueFD dmaHeapHandle_;
>  };
>
>  } /* namespace RPi */
> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> index d82cb30f0e77..d4f248a799e5 100644
> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> @@ -1382,10 +1382,12 @@ 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())
> +		UniqueFD fd = dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize);
> +		if (!fd.isValid())
>  			return -ENOMEM;
>
> +		lsTable_ = FileDescriptor(std::move(fd));

As lsTable_ is initialized once, why going through a UniqueFD and then
a FileDescriptor ?

Otherwise
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks
  j

> +
>  		/* Allow the IPA to mmap the LS table via the file descriptor. */
>  		/*
>  		 * \todo Investigate if mapping the lens shading table buffer
> --
> Regards,
>
> Laurent Pinchart
>
Laurent Pinchart Nov. 30, 2021, 10:16 a.m. UTC | #2
Hi Jacopo,

On Tue, Nov 30, 2021 at 09:06:55AM +0100, Jacopo Mondi wrote:
> On Tue, Nov 30, 2021 at 05:38:15AM +0200, Laurent Pinchart wrote:
> > From: Hirokazu Honda <hiroh@chromium.org>
> >
> > Manages a file descriptor owned by DmaHeaps for a dma handle by
> > UniqueFD. 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 UniqueFD.
> >
> > Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
> > ---
> > Changes since v2:
> >
> > - Use default destructor
> > - Return {}
> > ---
> >  .../pipeline/raspberrypi/dma_heaps.cpp        | 28 ++++++++-----------
> >  .../pipeline/raspberrypi/dma_heaps.h          | 10 ++++---
> >  .../pipeline/raspberrypi/raspberrypi.cpp      |  6 ++--
> >  3 files changed, 21 insertions(+), 23 deletions(-)
> >
> > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp
> > index 573ea11de607..69831dabbe75 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,44 @@ DmaHeap::DmaHeap()
> >  			continue;
> >  		}
> >
> > -		dmaHeapHandle_ = ret;
> > +		dmaHeapHandle_ = UniqueFD(ret);
> >  		break;
> >  	}
> >
> > -	if (dmaHeapHandle_ < 0)
> > +	if (!dmaHeapHandle_.isValid())
> >  		LOG(RPI, Error) << "Could not open any dmaHeap device";
> >  }
> >
> > -DmaHeap::~DmaHeap()
> > -{
> > -	if (dmaHeapHandle_ > -1)
> > -		::close(dmaHeapHandle_);
> > -}
> > +DmaHeap::~DmaHeap() = default;
> >
> > -FileDescriptor DmaHeap::alloc(const char *name, std::size_t size)
> > +UniqueFD DmaHeap::alloc(const char *name, std::size_t size)
> >  {
> >  	int ret;
> >
> >  	if (!name)
> > -		return FileDescriptor();
> > +		return {};
> >
> >  	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 {};
> >  	}
> >
> > -	ret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);
> > +	UniqueFD 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 {};
> >  	}
> >
> > -	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 57beaeb2e48a..d38f41eae1a2 100644
> > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h
> > +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h
> > @@ -7,7 +7,9 @@
> >
> >  #pragma once
> >
> > -#include <libcamera/base/file_descriptor.h>
> > +#include <stddef.h>
> > +
> > +#include <libcamera/base/unique_fd.h>
> >
> >  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(); }
> > +	UniqueFD alloc(const char *name, std::size_t size);
> >
> >  private:
> > -	int dmaHeapHandle_;
> > +	UniqueFD dmaHeapHandle_;
> >  };
> >
> >  } /* namespace RPi */
> > diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> > index d82cb30f0e77..d4f248a799e5 100644
> > --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> > +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> > @@ -1382,10 +1382,12 @@ 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())
> > +		UniqueFD fd = dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize);
> > +		if (!fd.isValid())
> >  			return -ENOMEM;
> >
> > +		lsTable_ = FileDescriptor(std::move(fd));
> 
> As lsTable_ is initialized once, why going through a UniqueFD and then
> a FileDescriptor ?

Good point. I'll change this to

	lsTable_ = FileDescriptor(dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize));
	if (!lsTable_.isValid())
		return -ENOMEM;

> Otherwise
> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
> 
> > +
> >  		/* Allow the IPA to mmap the LS table via the file descriptor. */
> >  		/*
> >  		 * \todo Investigate if mapping the lens shading table buffer

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp
index 573ea11de607..69831dabbe75 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,44 @@  DmaHeap::DmaHeap()
 			continue;
 		}
 
-		dmaHeapHandle_ = ret;
+		dmaHeapHandle_ = UniqueFD(ret);
 		break;
 	}
 
-	if (dmaHeapHandle_ < 0)
+	if (!dmaHeapHandle_.isValid())
 		LOG(RPI, Error) << "Could not open any dmaHeap device";
 }
 
-DmaHeap::~DmaHeap()
-{
-	if (dmaHeapHandle_ > -1)
-		::close(dmaHeapHandle_);
-}
+DmaHeap::~DmaHeap() = default;
 
-FileDescriptor DmaHeap::alloc(const char *name, std::size_t size)
+UniqueFD DmaHeap::alloc(const char *name, std::size_t size)
 {
 	int ret;
 
 	if (!name)
-		return FileDescriptor();
+		return {};
 
 	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 {};
 	}
 
-	ret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);
+	UniqueFD 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 {};
 	}
 
-	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 57beaeb2e48a..d38f41eae1a2 100644
--- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h
+++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h
@@ -7,7 +7,9 @@ 
 
 #pragma once
 
-#include <libcamera/base/file_descriptor.h>
+#include <stddef.h>
+
+#include <libcamera/base/unique_fd.h>
 
 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(); }
+	UniqueFD alloc(const char *name, std::size_t size);
 
 private:
-	int dmaHeapHandle_;
+	UniqueFD dmaHeapHandle_;
 };
 
 } /* namespace RPi */
diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
index d82cb30f0e77..d4f248a799e5 100644
--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
@@ -1382,10 +1382,12 @@  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())
+		UniqueFD fd = dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize);
+		if (!fd.isValid())
 			return -ENOMEM;
 
+		lsTable_ = FileDescriptor(std::move(fd));
+
 		/* Allow the IPA to mmap the LS table via the file descriptor. */
 		/*
 		 * \todo Investigate if mapping the lens shading table buffer