net/tap: Fix QEMU frozen issue when the maximum number of file descriptors is very large
v2 cover net/tap: Fix QEMU frozen issue when the maximum number of file descriptors is very large
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, a new API we add in utils/osdep.c.
Changes in v2:
- Change to use qemu_close_range() to close fds for child process efficiently
- v1 link: https://lore.kernel.org/qemu-devel/[email protected]/
Bin Meng (4):
tests/tcg/cris: Fix the coding style tests/tcg/cris: Correct the off-by-one error util/async-teardown: Fall back to close fds one by one utils/osdep: Introduce qemu_close_range()
Zhangjin Wu (2):
util/async-teardown: Use qemu_close_range() to close fds
net: tap: Use qemu_close_range() to close fds
include/qemu/osdep.h | 1 + net/tap.c | 23 ++++++------ tests/tcg/cris/libc/check_openpf5.c | 57 ++++++++++++++--------------- util/async-teardown.c | 37 +------------------ util/osdep.c | 47 ++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 78 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/6 tests/tcg/cris: Fix the coding style
The code style does not conform with QEMU's. Correct it so that the upcoming commit does not trigger checkpatch warnings.
---
(no changes since v1)
tests/tcg/cris/libc/check_openpf5.c | 57 ++++++++++++++--------------- 1 file changed, 27 insertions(+), 30 deletions(-)
tests/tcg/cris/libc/check_openpf5.c +27 -30
@@ -13,44 +13,41 @@#include <fcntl.h> #include <string.h> -int main (int argc,char *argv[]) +int main (int argc,char *argv[]) { -int i; -int filemax; +int i; +int filemax;#ifdef OPEN_MAX - filemax =OPEN_MAX ; + filemax =OPEN_MAX ;#else - filemax =sysconf (_SC_OPEN_MAX); + filemax =sysconf (_SC_OPEN_MAX);#endif -char *fn =malloc (strlen (argv[0 ]) +2 ); -if (fn ==NULL ) -abort (); -strcpy (fn,"/" ); -strcat (fn, argv[0 ]); +char *fn =malloc (strlen (argv[0 ]) +2 ); +if (fn ==NULL ) { +abort (); + } +strcpy (fn,"/" ); +strcat (fn, argv[0 ]); -for (i =0 ; i < filemax +1 ; i++) - { -if (open (fn,O_RDONLY ) <0 ) - { -/* Shouldn't happen too early. */ -if (i < filemax -3 -1 ) - { -fprintf (stderr,"i: %d\n" , i); -abort (); - } -if (errno !=EMFILE ) - { -perror ("open" ); -abort (); - } - goto ok; - } +for (i =0 ; i < filemax +1 ; i++) { +if (open (fn,O_RDONLY ) <0 ) { +/* Shouldn't happen too early. */ +if (i < filemax -3 -1 ) { +fprintf (stderr,"i: %d\n" , i); +abort (); + } +if (errno !=EMFILE ) { +perror ("open" ); +abort (); + } + goto ok; + } } -abort (); +abort (); ok: -printf ("pass\n" ); -exit (0 ); +printf ("pass\n" ); +exit (0 ); }
Signature · 1 line
2.34.1
Re: [PATCH v2 1/6] tests/tcg/cris: Fix the coding style
Quoted 10 lines · Bin Meng
The code style does not conform with QEMU's. Correct it so that the upcoming commit does not trigger checkpatch warnings.
Signed-off-by Bin Meng<[email protected]>---
(no changes since v1)
tests/tcg/cris/libc/check_openpf5.c | 57 ++++++++++++++--------------- 1 file changed, 27 insertions(+), 30 deletions(-)
r~
PATCH v2 2/6 tests/tcg/cris: Correct the off-by-one error
In reply to Bin Mengsysconf(_SC_OPEN_MAX) returns the maximum number of files that a process can have open at any time, which means the fd should not be larger than or equal to the return value.
---
(no changes since v1)
tests/tcg/cris/libc/check_openpf5.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tests/tcg/cris/libc/check_openpf5.c +2 -2
@@ -31,10 +31,10 @@ int main(int argc, char *argv[])strcpy (fn,"/" );strcat (fn, argv[0 ]); -for (i =0 ; i < filemax +1 ; i++) { +for (i =0 ; i < filemax; i++) {if (open (fn,O_RDONLY ) <0 ) {/* Shouldn't happen too early. */ -if (i < filemax -3 -1 ) { +if (i < filemax -3 ) {fprintf (stderr,"i: %d\n" , i);abort (); }
Signature · 1 line
2.34.1
Re: [PATCH v2 2/6] tests/tcg/cris: Correct the off-by-one error
Quoted 11 lines · Bin Meng
sysconf(_SC_OPEN_MAX)returns the maximum number of files that a process can have open at any time, which means the fd should not be larger than or equal to the return value.Signed-off-by Bin Meng<[email protected]>---
(no changes since v1)
tests/tcg/cris/libc/check_openpf5.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
r~
PATCH v2 3/6 util/async-teardown: Fall back to close fds one by one
In reply to Bin MengWhen opening /proc/self/fd fails, current codes just return directly, but we can fall back to close fds one by one.
---
(no changes since v1)
util/async-teardown.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
util/async-teardown.c +5 -1
@@ -48,7 +48,11 @@ static void close_all_open_fd(void) dir =opendir ("/proc/self/fd" );if (!dir) { -/* If /proc is not mounted, there is nothing that can be done. */ +/* If /proc is not mounted, close fds one by one. */ +int open_max =sysconf (_SC_OPEN_MAX), i; +for (i =0 ; i < open_max; i++) { +close (i); + }return ; }/* Avoid closing the directory. */
Signature · 1 line
2.34.1
Re: [PATCH v2 3/6] util/async-teardown: Fall back to close fds one by one
Quoted 18 lines · Bin Meng
When opening
/proc/self/fdfails, current codes just return directly, but we can fall back to close fds one by one.Signed-off-by Bin Meng <[email protected]>---
(no changes since v1)
util/async-teardown.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)util/async-teardown.c +0
-0@@ -48,7 +48,11 @@ static void close_all_open_fd(void) dir =opendir ("/proc/self/fd" );
util/async-teardown.c static void close_all_open_fd(void) · line 58
Quoted 10 lines
util/async-teardown.c +5
-1if (!dir) { -/* If /proc is not mounted, there is nothing that can be done. */ +/* If /proc is not mounted, close fds one by one. */ +int open_max =sysconf (_SC_OPEN_MAX), i; +for (i =0 ; i < open_max; i++) { +close (i); + }return ; }/* Avoid closing the directory. */
Do we really need to make the 1M close calls?
The process is on its way to exit anyway...
r~
PATCH v2 4/6 utils/osdep: Introduce qemu_close_range()
In reply to Bin MengThis introduces a new QEMU API qemu_close_range() that closes all open file descriptors from first to last (included).
This API will try a more efficient call to close_range(), or walk through of /proc/self/fd whenever these are possible, otherwise it falls back to a plain close loop.
---
(no changes since v1)
include/qemu/osdep.h | 1 + util/osdep.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)
include/qemu/osdep.h +1 -0
@@ -560,6 +560,7 @@ int qemu_open_old(const char *name, int flags, ...);int qemu_open (const char *name,int flags,Error **errp);int qemu_create (const char *name,int flags,mode_t mode,Error **errp);int qemu_close (int fd); +int qemu_close_range (unsigned int first,unsigned int last);int qemu_unlink (const char *name);#ifndef _WIN32int qemu_dup_flags (int fd,int flags);
util/osdep.c +47 -0
53 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 4/6] utils/osdep: Introduce qemu_close_range()
Quoted 70 lines · Bin Meng
This introduces a new QEMU API
qemu_close_range()that closes all open file descriptors from first to last (included).This API will try a more efficient call to
close_range(), or walk through of/proc/self/fdwhenever these are possible, otherwise it falls back to a plain close loop.Co-developed-by Zhangjin Wu <[email protected]>Signed-off-by Bin Meng <[email protected]>---
(no changes since v1)
include/qemu/osdep.h | 1 + util/osdep.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)include/qemu/osdep.h +1
-0@@ -560,6 +560,7 @@ int qemu_open_old(const char *name, int flags, ...);int qemu_open (const char *name,int flags,Error **errp);int qemu_create (const char *name,int flags,mode_t mode,Error **errp);int qemu_close (int fd); +int qemu_close_range (unsigned int first,unsigned int last);int qemu_unlink (const char *name);#ifndef _WIN32int qemu_dup_flags (int fd,int flags);util/osdep.c +33
-0@@ -411,6 +411,53 @@ int qemu_close(int fd)return close (fd); } +int qemu_close_range (unsigned int first,unsigned int last) +{ +DIR *dir =NULL ; + +#ifdef CONFIG_CLOSE_RANGE +int r =close_range (first, last,0 ); +if (!r) { +/* Success, no need to try other ways. */ +return 0 ; + } +#endif + +#ifdef __linux__ + dir =opendir ("/proc/self/fd" ); +#endif +if (!dir) { +/* +* If /proc is not mounted or /proc/self/fd is not supported, +* try close() from first to last. +*/ +for (int i = first; i <= last; i++) { +close (i); + } + +return 0 ; + } + +#ifndef _WIN32 +/* Avoid closing the directory */ +int dfd =dirfd (dir); + +for (struct dirent *de =readdir (dir); de; de =readdir (dir)) { +int fd =atoi (de->d_name);
util/osdep.c int qemu_close(int fd) · line 456
Quoted 10 lines
util/osdep.c +10
-0+if (fd < first || fd > last) { +/* Exclude the fds outside the target range */ +continue ; + } +if (fd != dfd) { +close (fd); + } + } +closedir (dir); +#endif /* _WIN32 */
Poor ordering of ifdefs.
#ifdef __linux__
DIR *dir = opendir("...");
if (dir) {
int dfd = ...
loop
closedir(dir);
return;
}#endif
since the first ifdef is the only way dir can ever be non-null.
util/osdep.c int qemu_close(int fd) · line 434
Quoted 1 line
util/osdep.c +1
-0+for (int i = first; i <= last; i++) {
typeof(i) != typeof(first).
r~
Re: [PATCH v2 4/6] utils/osdep: Introduce qemu_close_range()
In reply to Bin Mengutil/osdep.c int qemu_close(int fd) · line 424
Quoted 11 lines · Bin Meng
util/osdep.c +11
-0+int qemu_close_range (unsigned int first,unsigned int last) +{ +DIR *dir =NULL ; + +#ifdef CONFIG_CLOSE_RANGE +int r =close_range (first, last,0 ); +if (!r) { +/* Success, no need to try other ways. */ +return 0 ; + } +#endif
What about first > last?
close_range will yield EINVAL, and your other fallbacks will burn lots of cpu.
r~
PATCH v2 5/6 util/async-teardown: Use qemu_close_range() to close fds
In reply to Bin MengFrom: Zhangjin Wu <[email protected]>
Based on the old close_all_open_fd() of util/async-teardown.c, a new generic qemu_close_range() has been added in osdep.c.
Now, let's switch over to use the generic qemu_close_range().
---
(no changes since v1)
util/async-teardown.c | 41 +---------------------------------------- 1 file changed, 1 insertion(+), 40 deletions(-)
util/async-teardown.c +1 -40
@@ -29,44 +29,6 @@static pid_t the_ppid; -/* -* Close all open file descriptors. -*/ -static void close_all_open_fd (void ) -{ -struct dirent *de; -int fd, dfd; -DIR *dir; - -#ifdef CONFIG_CLOSE_RANGE -int r =close_range (0 , ~0U ,0 ); -if (!r) { -/* Success, no need to try other ways. */ -return ; - } -#endif - - dir =opendir ("/proc/self/fd" ); -if (!dir) { -/* If /proc is not mounted, close fds one by one. */ -int open_max =sysconf (_SC_OPEN_MAX), i; -for (i =0 ; i < open_max; i++) { -close (i); - } -return ; - } -/* Avoid closing the directory. */ - dfd =dirfd (dir); - -for (de =readdir (dir); de; de =readdir (dir)) { - fd =atoi (de->d_name); -if (fd != dfd) { -close (fd); - } - } -closedir (dir); -} -static void hup_handler (int signal) {/* Check every second if this process has been reparented. */
@@ -92,9 +54,8 @@ static int async_teardown_fn(void *arg)/* * Close all file descriptors that might have been inherited from the * main qemu process when doing clone, needed to make libvirt happy. -* Not using close_range for increased compatibility with older kernels. */ -close_all_open_fd (); +qemu_close_range (0 , ~0U );/* Set up a handler for SIGHUP and unblock SIGHUP. */ sigaction (SIGHUP , &sa,NULL );
Signature · 1 line
2.34.1
Re: [PATCH v2 5/6] util/async-teardown: Use qemu_close_range() to close fds
Quoted 1 line · Bin Meng
+
qemu_close_range(0, ~0U);
This is clearly assuming that close_range exists.
Should qemu_close_range attempt to lower last if >= _SC_OPEN_MAX? Or better to use that here.
r~
PATCH v2 6/6 net: tap: Use qemu_close_range() to close fds
In reply to Bin MengFrom: Zhangjin Wu <[email protected]>
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.
---
Changes in v2:
- Change to use qemu_close_range() to close fds for child process efficiently
- v1 link: https://lore.kernel.org/qemu-devel/[email protected]/
net/tap.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
net/tap.c +11 -12
@@ -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 ]);
Signature · 1 line
2.34.1
Re: [PATCH v2 6/6] net: tap: Use qemu_close_range() to close fds
Quoted 23 lines · Bin Meng
From: Zhangjin Wu<[email protected]>
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 properO_CLOEXECflag 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 usingO_CLOEXECon 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/fdto complete the task efficiently, which is whatqemu_close_range()does.Reported-by Zhangjin Wu<[email protected]>Signed-off-by Zhangjin Wu<[email protected]>Signed-off-by Bin Meng<[email protected]>
Quoted 8 lines
---
Changes in v2:
- Change to useqemu_close_range()to close fds for child process efficiently
- v1 link:https://lore.kernel.org/qemu-devel/[email protected]/net/tap.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
r~