Message ID | 20230616152737.23545-7-bmeng@tinylab.org (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | net/tap: Fix QEMU frozen issue when the maximum number of file descriptors is very large | expand |
On 6/16/23 17:27, Bin Meng 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> > Signed-off-by: Zhangjin Wu<falcon@tinylab.org> > Signed-off-by: Bin Meng<bmeng@tinylab.org> > > --- > > 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(-) Reviewed-by: Richard Henderson <richard.henderson@linaro.org> r~
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]; 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]);