qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: "Clément Léger" <cleger@rivosinc.com>, qemu-devel@nongnu.org
Cc: "Daniel P. Berrangé" <berrange@redhat.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Jason Wang" <jasowang@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>
Subject: Re: [PATCH v4] osdep: add a qemu_close_all_open_fd() helper
Date: Tue, 23 Jul 2024 08:24:22 +0200	[thread overview]
Message-ID: <c2085e4c-d2af-4ea1-9a04-f523c94a7315@linaro.org> (raw)
In-Reply-To: <20240717124534.1200735-1-cleger@rivosinc.com>

Hi Clément,

On 17/7/24 14:45, Clément Léger wrote:
> Since commit 03e471c41d8b ("qemu_init: increase NOFILE soft limit on
> POSIX"), the maximum number of file descriptors that can be opened are
> raised to nofile.rlim_max. On recent debian distro, this yield a maximum
> of 1073741816 file descriptors. Now, when forking to start
> qemu-bridge-helper, this actually calls close() on the full possible file
> descriptor range (more precisely [3 - sysconf(_SC_OPEN_MAX)]) which
> takes a considerable amount of time. In order to reduce that time,
> factorize existing code to close all open files descriptors in a new
> qemu_close_all_open_fd() function. This function uses various methods
> to close all the open file descriptors ranging from the most efficient
> one to the least one. It also accepts an ordered array of file
> descriptors that should not be closed since this is required by the
> callers that calls it after forking.
> 
> Signed-off-by: Clément Léger <cleger@rivosinc.com>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> ----

FYI git tools parse 3 '-', not 4.

> v4:
>   - Add a comment saying that qemu_close_all_fds() can take a NULL skip
>     array and nskip == 0
>   - Added an assert in qemu_close_all_fds() to check for skip/nskip
>     parameters
>   - Fix spurious tabs instead of spaces
>   - Applied checkpatch
>   - v3: https://lore.kernel.org/qemu-devel/20240716144006.6571-1-cleger@rivosinc.com/


> +void qemu_close_all_open_fd(const int *skip, unsigned int nskip)
> +{
> +    int open_max = sysconf(_SC_OPEN_MAX);
> +    unsigned int cur_skip = 0;
> +    int i;
> +
> +    assert(skip != NULL || nskip == 0);
> +
> +    if (qemu_close_all_open_fd_close_range(skip, nskip)) {
> +        return;
> +    }
> +
> +    if (qemu_close_all_open_fd_proc(skip, nskip)) {
> +        return;
> +    }
> +
> +    /* Fallback */
> +    for (i = 0; i < open_max; i++) {
> +        if (cur_skip < nskip && i == skip[cur_skip]) {
> +            cur_skip++;
> +            continue;
> +        }
> +        close(i);
> +    }
> +}

Build failure on windows:

../util/osdep.c: In function 'qemu_close_all_open_fd':
../util/osdep.c:725:20: error: implicit declaration of function 
'sysconf'; did you mean 'swscanf'? [-Wimplicit-function-declaration]
   725 |     int open_max = sysconf(_SC_OPEN_MAX);
       |                    ^~~~~~~
       |                    swscanf
../util/osdep.c:725:20: error: nested extern declaration of 'sysconf' 
[-Werror=nested-externs]
../util/osdep.c:725:28: error: '_SC_OPEN_MAX' undeclared (first use in 
this function); did you mean 'FOPEN_MAX'?
   725 |     int open_max = sysconf(_SC_OPEN_MAX);
       |                            ^~~~~~~~~~~~
       |                            FOPEN_MAX
../util/osdep.c:725:28: note: each undeclared identifier is reported 
only once for each function it appears in



  reply	other threads:[~2024-07-23  6:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-17 12:45 [PATCH v4] osdep: add a qemu_close_all_open_fd() helper Clément Léger
2024-07-23  6:24 ` Philippe Mathieu-Daudé [this message]
2024-07-23  7:16   ` Clément Léger
2024-07-23  7:54     ` Daniel P. Berrangé
2024-07-23  7:42 ` Marc-André Lureau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c2085e4c-d2af-4ea1-9a04-f523c94a7315@linaro.org \
    --to=philmd@linaro.org \
    --cc=berrange@redhat.com \
    --cc=cleger@rivosinc.com \
    --cc=jasowang@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).