[{"id":33727,"web_url":"https://patchwork.libcamera.org/comment/33727/","msgid":"<20250326144406.GH8303@pendragon.ideasonboard.com>","date":"2025-03-26T14:44:06","subject":"Re: [RFC PATCH v3 8/9] libcamera: process: Use `close_range()` when\n\tavailable","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Barnabás,\n\nThank you for the patch.\n\nOn Tue, Mar 25, 2025 at 07:08:20PM +0100, Barnabás Pőcze wrote:\n> Use the `close_range()` system call when available as it is\n> simpler and faster than iterating `/proc/self/fd/`.\n> \n> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> ---\n>  meson.build               |  4 ++++\n>  src/libcamera/process.cpp | 20 ++++++++++++++++++++\n>  2 files changed, 24 insertions(+)\n> \n> diff --git a/meson.build b/meson.build\n> index 668ec3969..00291d628 100644\n> --- a/meson.build\n> +++ b/meson.build\n> @@ -103,6 +103,10 @@ if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix : '#define _GNU_SOUR\n>      config_h.set('HAVE_SECURE_GETENV', 1)\n>  endif\n>  \n> +if cc.has_header_symbol('unistd.h', 'close_range', prefix : '#define _GNU_SOURCE')\n> +    config_h.set('HAVE_CLOSE_RANGE', 1)\n> +endif\n> +\n\nPlease move this before HAVE_FILE_SEALS, in alphabetical order.\n\n>  common_arguments = [\n>      '-Wmissing-declarations',\n>      '-Wshadow',\n> diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp\n> index 01503e485..5fa813300 100644\n> --- a/src/libcamera/process.cpp\n> +++ b/src/libcamera/process.cpp\n> @@ -70,6 +70,26 @@ void closeAllFdsExcept(Span<const int> fds)\n>  \n>  \tASSERT(v.empty() || v.front() >= 0);\n>  \n> +#if HAVE_CLOSE_RANGE\n> +\tstatic const bool hasCloseRange = [] {\n> +\t\treturn close_range(~0u, 0, 0) < 0 && errno == EINVAL;\n> +\t}();\n\nWhy do you need to test this ? Is it because we may be running against a\nkernel that doesn't provide the system call ? A comment could be useful:\n\n\t/*\n\t * The close_range() system call was added in Linux v5.11. Perform a\n\t * runtime support check, in case we're running on an older kernel.\n\t */\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\n> +\tif (hasCloseRange) {\n> +\t\tunsigned int prev = 0;\n> +\n> +\t\tfor (unsigned int curr : v) {\n> +\t\t\tASSERT(prev <= curr);\n> +\t\t\tif (prev < curr)\n> +\t\t\t\tclose_range(prev, curr - 1, 0);\n> +\t\t\tprev = curr + 1;\n> +\t\t}\n> +\n> +\t\tclose_range(prev, ~0u, 0);\n> +\t\treturn;\n> +\t}\n> +#endif\n> +\n>  \tDIR *dir = opendir(\"/proc/self/fd\");\n>  \tif (!dir)\n>  \t\treturn;","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 C7E64C3213\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 26 Mar 2025 14:44:31 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E5E176896A;\n\tWed, 26 Mar 2025 15:44:30 +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 CD2E668950\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 26 Mar 2025 15:44:28 +0100 (CET)","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 9D05C475;\n\tWed, 26 Mar 2025 15:42:40 +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=\"vm1RdaXv\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1743000160;\n\tbh=qkuwm/SbxALO/iVXN4u859iXvQooHiUFi9K3U4hHxfM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=vm1RdaXvqSvEqAAQ/nY8W/sVhMWo03vKo7Eg/rsE40vPsfk/DsNpVscsE+9HuU3Of\n\tyEqKNu1Y8ReGdog1UeyDpwP4rjmc4Uo1K03H9F5776qGxRjkcTFt+AEfcXghZDPqtR\n\tbwj+trLD+gFJqSZ4B7Vj/zYvru4NI+p52nM+pd8A=","Date":"Wed, 26 Mar 2025 16:44:06 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v3 8/9] libcamera: process: Use `close_range()` when\n\tavailable","Message-ID":"<20250326144406.GH8303@pendragon.ideasonboard.com>","References":"<20250325180821.1456154-1-barnabas.pocze@ideasonboard.com>\n\t<20250325180821.1456154-9-barnabas.pocze@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250325180821.1456154-9-barnabas.pocze@ideasonboard.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>"}}]