qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: "Clément Léger" <cleger@rivosinc.com>
Cc: "Philippe Mathieu-Daudé" <philmd@linaro.org>,
	qemu-devel@nongnu.org, "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:54:55 +0100	[thread overview]
Message-ID: <Zp9hz0NsHTWZD6dt@redhat.com> (raw)
In-Reply-To: <7aadff15-3939-4e0f-a81a-84f78521a8a1@rivosinc.com>

On Tue, Jul 23, 2024 at 09:16:15AM +0200, Clément Léger wrote:
> 
> 
> On 23/07/2024 08:24, Philippe Mathieu-Daudé wrote:
> > 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.
> 
> Hi Philippe,
> 
> Thanks for the info, I was not aware of that ! I'll use 3 '-' from now on.
> 
> > 
> >> 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
> > 
> 
> Should I move this chunk of code in oslib-posix.c and stub that function
> for win32 ? It seems like this code was not built for windows before I
> added it in osdep, which means it is not needed for win32.

Yes, I think that'll be OK. The fork/exec concept isn't present on Windows
so we also don't need to close all FDs.


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



  reply	other threads:[~2024-07-23  7:55 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é
2024-07-23  7:16   ` Clément Léger
2024-07-23  7:54     ` Daniel P. Berrangé [this message]
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=Zp9hz0NsHTWZD6dt@redhat.com \
    --to=berrange@redhat.com \
    --cc=cleger@rivosinc.com \
    --cc=jasowang@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@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).