qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Claudio Imbrenda <imbrenda@linux.ibm.com>
To: Bin Meng <bmeng@tinylab.org>
Cc: qemu-devel@nongnu.org, Zhangjin Wu <falcon@tinylab.org>,
	Jason Wang <jasowang@redhat.com>
Subject: Re: [PATCH v3 6/6] net: tap: Use qemu_close_range() to close fds
Date: Mon, 19 Jun 2023 11:23:41 +0200	[thread overview]
Message-ID: <20230619112341.1b9e98de@p-imbrenda> (raw)
In-Reply-To: <20230617053621.50359-7-bmeng@tinylab.org>

On Sat, 17 Jun 2023 13:36:21 +0800
Bin Meng <bmeng@tinylab.org> wrote:

> From: Zhangjin Wu <falcon@tinylab.org>
> 
> Current codes using a brute-force traversal of all file descriptors
> do not scale on a system where the maximum number of file descriptors
> is set to a very large value (e.g.: in a Docker container of Manjaro
> distribution it is set to 1073741816). QEMU just looks frozen during
> start-up.
> 
> The close-on-exec flag (O_CLOEXEC) was introduced since Linux kernel
> 2.6.23, FreeBSD 8.3, OpenBSD 5.0, Solaris 11. While it's true QEMU
> doesn't need to manually close the fds for child process as the proper
> O_CLOEXEC flag should have been set properly on files with its own
> codes, QEMU uses a huge number of 3rd party libraries and we don't
> trust them to reliably be using O_CLOEXEC on everything they open.
> 
> Modern Linux and BSDs have the close_range() call we can use to do the
> job, and on Linux we have one more way to walk through /proc/self/fd
> to complete the task efficiently, which is what qemu_close_range() does.
> 
> Reported-by: Zhangjin Wu <falcon@tinylab.org>
> Co-developed-by: Bin Meng <bmeng@tinylab.org>
> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> Signed-off-by: Bin Meng <bmeng@tinylab.org>
> 
> ---
> 
> (no changes since v2)
> 
> Changes in v2:
> - Change to use qemu_close_range() to close fds for child process efficiently
> - v1 link: https://lore.kernel.org/qemu-devel/20230406112041.798585-1-bmeng@tinylab.org/
> 
>  net/tap.c | 23 +++++++++++------------
>  1 file changed, 11 insertions(+), 12 deletions(-)
> 
> diff --git a/net/tap.c b/net/tap.c
> index 1bf085d422..d482fabdff 100644
> --- a/net/tap.c
> +++ b/net/tap.c
> @@ -446,13 +446,13 @@ static void launch_script(const char *setup_script, const char *ifname,
>          return;
>      }
>      if (pid == 0) {
> -        int open_max = sysconf(_SC_OPEN_MAX), i;
> +        unsigned int last_fd = sysconf(_SC_OPEN_MAX) - 1;
> +
> +        /* skip stdin, stdout and stderr */
> +        qemu_close_range(3, fd - 1);
> +        /* skip the currently used fd */
> +        qemu_close_range(fd + 1, last_fd);
>  
> -        for (i = 3; i < open_max; i++) {
> -            if (i != fd) {
> -                close(i);
> -            }
> -        }
>          parg = args;
>          *parg++ = (char *)setup_script;
>          *parg++ = (char *)ifname;
> @@ -536,16 +536,15 @@ static int net_bridge_run_helper(const char *helper, const char *bridge,
>          return -1;
>      }
>      if (pid == 0) {
> -        int open_max = sysconf(_SC_OPEN_MAX), i;
> +        unsigned int last_fd = sysconf(_SC_OPEN_MAX) - 1, fd = sv[1];

please put fd on its own line

>          char *fd_buf = NULL;
>          char *br_buf = NULL;
>          char *helper_cmd = NULL;
>  
> -        for (i = 3; i < open_max; i++) {
> -            if (i != sv[1]) {
> -                close(i);
> -            }
> -        }
> +        /* skip stdin, stdout and stderr */
> +        qemu_close_range(3, fd - 1);
> +        /* skip the currently used fd */
> +        qemu_close_range(fd + 1, last_fd);
>  
>          fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
>  



  reply	other threads:[~2023-06-19  9:25 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-17  5:36 [PATCH v3 0/6] net/tap: Fix QEMU frozen issue when the maximum number of file descriptors is very large Bin Meng
2023-06-17  5:36 ` [PATCH v3 1/6] tests/tcg/cris: Fix the coding style Bin Meng
2023-06-17 22:52   ` Philippe Mathieu-Daudé
2023-06-17  5:36 ` [PATCH v3 2/6] tests/tcg/cris: Correct the off-by-one error Bin Meng
2023-06-17 22:52   ` Philippe Mathieu-Daudé
2023-06-17  5:36 ` [PATCH v3 3/6] util/async-teardown: Fall back to close fds one by one Bin Meng
2023-06-19  9:18   ` Claudio Imbrenda
2023-06-25 15:17     ` Bin Meng
2023-06-17  5:36 ` [PATCH v3 4/6] util/osdep: Introduce qemu_close_range() Bin Meng
2023-06-19  9:18   ` Claudio Imbrenda
2023-06-28 15:12     ` Bin Meng
2023-06-19  9:22   ` Markus Armbruster
2023-06-28 15:40     ` Bin Meng
2023-06-17  5:36 ` [PATCH v3 5/6] util/async-teardown: Use qemu_close_range() to close fds Bin Meng
2023-06-19  9:19   ` Claudio Imbrenda
2023-06-17  5:36 ` [PATCH v3 6/6] net: tap: " Bin Meng
2023-06-19  9:23   ` Claudio Imbrenda [this message]
2023-06-19  7:13 ` [PATCH v3 0/6] net/tap: Fix QEMU frozen issue when the maximum number of file descriptors is very large Richard Henderson
2023-06-28 17:13 ` Michael Tokarev

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=20230619112341.1b9e98de@p-imbrenda \
    --to=imbrenda@linux.ibm.com \
    --cc=bmeng@tinylab.org \
    --cc=falcon@tinylab.org \
    --cc=jasowang@redhat.com \
    --cc=qemu-devel@nongnu.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).