[{"id":34057,"web_url":"https://patchwork.libcamera.org/comment/34057/","msgid":"<174567436550.1742020.16577808845026266938@ping.linuxembedded.co.uk>","date":"2025-04-26T13:32:45","subject":"Re: [RFC PATCH v5 8/9] libcamera: process: Use `close_range()` when\n\tavailable","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Barnabás Pőcze (2025-04-24 12:41:02)\n> Use the `close_range()` system call when available as it is simpler\n> and faster than iterating `/proc/self/fd/`.\n> \n> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  meson.build               |  4 ++++\n>  src/libcamera/process.cpp | 25 +++++++++++++++++++++++++\n>  2 files changed, 29 insertions(+)\n> \n> diff --git a/meson.build b/meson.build\n> index 9ba5e2ca9..ae13727eb 100644\n> --- a/meson.build\n> +++ b/meson.build\n> @@ -74,6 +74,10 @@ cc = meson.get_compiler('c')\n>  cxx = meson.get_compiler('cpp')\n>  config_h = configuration_data()\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>  if cc.has_header_symbol('fcntl.h', 'F_ADD_SEALS', prefix : '#define _GNU_SOURCE')\n>      config_h.set('HAVE_FILE_SEALS', 1)\n>  endif\n> diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp\n> index 369fdb12d..42b56374d 100644\n> --- a/src/libcamera/process.cpp\n> +++ b/src/libcamera/process.cpp\n> @@ -70,6 +70,31 @@ void closeAllFdsExcept(Span<const int> fds)\n>  \n>         ASSERT(v.empty() || v.front() >= 0);\n>  \n> +#if HAVE_CLOSE_RANGE\n> +       /*\n> +        * At the moment libcamera does not require at least Linux 5.9,\n> +        * which introduced the `close_range()` system call, so a runtime\n> +        * check is also needed to make sure that it is supported.\n> +        */\n> +       static const bool hasCloseRange = [] {\n> +               return close_range(~0u, 0, 0) < 0 && errno == EINVAL;\n> +       }();\n> +\n> +       if (hasCloseRange) {\n> +               unsigned int prev = 0;\n> +\n> +               for (unsigned int curr : v) {\n> +                       ASSERT(prev <= curr + 1);\n> +                       if (prev < curr)\n> +                               close_range(prev, curr - 1, 0);\n> +                       prev = curr + 1;\n\nAha, I see it now. I was lost thinking this patch was a change in\nbehaviour - but it looks sane to me.\n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> +               }\n> +\n> +               close_range(prev, ~0u, 0);\n> +               return;\n> +       }\n> +#endif\n> +\n>         DIR *dir = opendir(\"/proc/self/fd\");\n>         if (!dir)\n>                 return;\n> -- \n> 2.49.0\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 F0E04BE08B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 26 Apr 2025 13:32:51 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2A52168ACA;\n\tSat, 26 Apr 2025 15:32:51 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E3EFE617E3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 26 Apr 2025 15:32:48 +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 4A302735;\n\tSat, 26 Apr 2025 15:32:45 +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=\"nGwYGg8s\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1745674365;\n\tbh=cplyAlZj9BVGQNfR7TpcsBy2/x1KaF3EmCssDgmYFCs=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=nGwYGg8sGSA+aBkKyLHd8V7lWSpbRrYiDVo6qkR8H2V2hU8m7gTwC6EXgf8Ivqbjb\n\tBLUKIBExOdJCMe38FKfyTvAr4QB4ddw2KdaQtyyrLGiszFADcg4NAuQMUm37KMlnw2\n\tC2HGPX/OiRk+nI+S6bv5M314Zz85yhxeJqkAXknA=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250424114103.451395-9-barnabas.pocze@ideasonboard.com>","References":"<20250424114103.451395-1-barnabas.pocze@ideasonboard.com>\n\t<20250424114103.451395-9-barnabas.pocze@ideasonboard.com>","Subject":"Re: [RFC PATCH v5 8/9] libcamera: process: Use `close_range()` when\n\tavailable","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Sat, 26 Apr 2025 14:32:45 +0100","Message-ID":"<174567436550.1742020.16577808845026266938@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>"}}]