From nobody Sun May 19 08:30:43 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687966147656160.5204393187296; Wed, 28 Jun 2023 08:29:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qEX5I-0003IT-At; Wed, 28 Jun 2023 11:27:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX59-0003H5-LC for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:27:48 -0400 Received: from bg4.exmail.qq.com ([43.155.67.158]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX57-0004yu-7R for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:27:47 -0400 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 28 Jun 2023 23:27:31 +0800 (CST) X-QQ-mid: bizesmtp72t1687966052tr3r64no X-QQ-SSF: 00200000000000F0G000000A0000000 X-QQ-FEAT: xQoAiglG4R6wB1LBl/nmFf3sqqIgXKRxvklvQMMV1KOS4BOK5Wl8dzVSvq+0d AQH5Uc/MHuzTWeEVmGxShTLHyY6zzWu+9Jke9Xe5sHg+KUejB2AW1y4qUnpoaBl5Y0uBmij Vbzzd4YBL7yrvR274AKltErBPJKhwW1kAJxkO89rp2PGuE7T77raAz1fdC0WXz+6c1IYtKG Q4aA8I+tB9SBMqjZf9yAb2oG93ZcK0z8Dl4AtDsWANl0kogEcxaES4/L/zWjA1d3oibXkCz Io0ElCJpebM4cxi4uIM6AlObS6HhlQOOOooDiJjBuFuTpUf9V0NQzHx/BbU5+LJkGdsKfK+ o5ikJ9bG3R+gmh4vgPbs/tkQbZ7me5fKJfnOsb8V09Zlj9lBrfQAze5DCNlMA== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 6150981274832304845 From: Bin Meng To: qemu-devel@nongnu.org Cc: Richard Henderson , Zhangjin Wu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , "Edgar E. Iglesias" Subject: [PATCH v4 1/6] tests/tcg/cris: Fix the coding style Date: Wed, 28 Jun 2023 23:27:21 +0800 Message-Id: <20230628152726.110295-2-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230628152726.110295-1-bmeng@tinylab.org> References: <20230628152726.110295-1-bmeng@tinylab.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=43.155.67.158; envelope-from=bmeng@tinylab.org; helo=bg4.exmail.qq.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL=0.141, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1687966148437100002 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 Acked-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- (no changes since v2) Changes in v2: - new patch: "tests/tcg/cris: Fix the coding style" tests/tcg/cris/libc/check_openpf5.c | 57 ++++++++++++++--------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/tests/tcg/cris/libc/check_openpf5.c b/tests/tcg/cris/libc/chec= k_openpf5.c index 1f86ea283d..0037fbca4c 100644 --- a/tests/tcg/cris/libc/check_openpf5.c +++ b/tests/tcg/cris/libc/check_openpf5.c @@ -13,44 +13,41 @@ #include #include =20 -int main (int argc, char *argv[]) +int main(int argc, char *argv[]) { - int i; - int filemax; + int i; + int filemax; =20 #ifdef OPEN_MAX - filemax =3D OPEN_MAX; + filemax =3D OPEN_MAX; #else - filemax =3D sysconf (_SC_OPEN_MAX); + filemax =3D sysconf(_SC_OPEN_MAX); #endif =20 - char *fn =3D malloc (strlen (argv[0]) + 2); - if (fn =3D=3D NULL) - abort (); - strcpy (fn, "/"); - strcat (fn, argv[0]); + char *fn =3D malloc(strlen(argv[0]) + 2); + if (fn =3D=3D NULL) { + abort(); + } + strcpy(fn, "/"); + strcat(fn, argv[0]); =20 - for (i =3D 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 !=3D EMFILE) - { - perror ("open"); - abort (); - } - goto ok; - } + for (i =3D 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 !=3D EMFILE) { + perror("open"); + abort(); + } + goto ok; + } } - abort (); + abort(); =20 ok: - printf ("pass\n"); - exit (0); + printf("pass\n"); + exit(0); } --=20 2.34.1 From nobody Sun May 19 08:30:43 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687966096390465.6772307460669; Wed, 28 Jun 2023 08:28:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qEX5J-0003JK-2G; Wed, 28 Jun 2023 11:27:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX59-0003H9-UN for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:27:48 -0400 Received: from bg4.exmail.qq.com ([43.155.67.158]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX57-0004z4-MU for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:27:47 -0400 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 28 Jun 2023 23:27:35 +0800 (CST) X-QQ-mid: bizesmtp72t1687966056teq41a0n X-QQ-SSF: 00200000000000F0G000000A0000000 X-QQ-FEAT: +ynUkgUhZJlHv263HMmQjaNSQYOpeqA2KWbXdM1LY1fnpjS+VRDXhX3BgyEra iXoo0fHNjirvCCsIclcS8NbNRihttc+41zZGAm8vFCep34VMtt2EqcWr8Yv0jU0MyiZX2vv IK1/xFTfcYFXwxQudpUfDXySXBcjMpIZAdz221ShcRxCz9HIstMqyIf3Ne7jeMhpmc3LH6Y 2QYVJUxa9rycgPCw8znGi9F+ndfakSLNDPn+IAU7UNTN0+/VrseOT7VSiubYzJ6kvEaRGi6 XlmvKM98MFCVvLI9XanAmWA5XSkKmp90529UCzgMfKquPHO69o+Gxjp6HgihVrm7QpjysPJ yNC7xClFBWAEBlnI3E7abpWQehGGVa+C1UUk5SI4hDCNr3jQgE= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 8344174067842687383 From: Bin Meng To: qemu-devel@nongnu.org Cc: Richard Henderson , Zhangjin Wu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , "Edgar E. Iglesias" Subject: [PATCH v4 2/6] tests/tcg/cris: Correct the off-by-one error Date: Wed, 28 Jun 2023 23:27:22 +0800 Message-Id: <20230628152726.110295-3-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230628152726.110295-1-bmeng@tinylab.org> References: <20230628152726.110295-1-bmeng@tinylab.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=43.155.67.158; envelope-from=bmeng@tinylab.org; helo=bg4.exmail.qq.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL=0.141, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1687966098144100001 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 Acked-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- (no changes since v2) Changes in v2: - new patch: "tests/tcg/cris: Correct the off-by-one error" tests/tcg/cris/libc/check_openpf5.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tcg/cris/libc/check_openpf5.c b/tests/tcg/cris/libc/chec= k_openpf5.c index 0037fbca4c..7f585c6d37 100644 --- a/tests/tcg/cris/libc/check_openpf5.c +++ b/tests/tcg/cris/libc/check_openpf5.c @@ -31,10 +31,10 @@ int main(int argc, char *argv[]) strcpy(fn, "/"); strcat(fn, argv[0]); =20 - for (i =3D 0; i < filemax + 1; i++) { + for (i =3D 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(); } --=20 2.34.1 From nobody Sun May 19 08:30:43 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687966168120152.57687257003442; Wed, 28 Jun 2023 08:29:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qEX6d-0005rk-2Y; Wed, 28 Jun 2023 11:29:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX6b-0005jj-SH for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:29:17 -0400 Received: from bg4.exmail.qq.com ([43.155.67.158]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX6Z-0005Kx-J2 for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:29:17 -0400 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 28 Jun 2023 23:27:38 +0800 (CST) X-QQ-mid: bizesmtp72t1687966059twph72m8 X-QQ-SSF: 01200000000000F0G000000A0000000 X-QQ-FEAT: eSZ1CZgv+JCZ8G2fmkeRtne/bwO71AkfsFqcRzVwtOLT9g+ZQA+dZbEkgRLYs MQD/YyUBXGaQ/3Dbop11VZd62CDIz3rIIy37+8d/SturWFUK1S3pjyWNC4GUSnNU8v4JQJH OgQI/GMFAg/esvHyaxkrEHLjiigUy7DgMW9jmy6PJYygZUCmi7ShvldH04kar2uij+jtnm9 ZUi22QJgY25TBV1NfWakwYAkoY0UUrouzDks7RQU8BipUNpHB7YFghdLzTelPsQXklQ5kae Lw0r+vviS03W/IdGx5pJDpdFSUHvCwBWcrE3XbK/Bj3kKy17N4oxy+/aVZelmSFuwldX1Ce vrUafgNAijTjRiQdaXZII821tpr66SJoQ+aEO20lo2nK3DJSTriJNfJ1KJnNw== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 2859246573490842033 From: Bin Meng To: qemu-devel@nongnu.org Cc: Richard Henderson , Zhangjin Wu , Claudio Imbrenda , "Michael S. Tsirkin" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth Subject: [PATCH v4 3/6] util/async-teardown: Fall back to close fds one by one Date: Wed, 28 Jun 2023 23:27:23 +0800 Message-Id: <20230628152726.110295-4-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230628152726.110295-1-bmeng@tinylab.org> References: <20230628152726.110295-1-bmeng@tinylab.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=43.155.67.158; envelope-from=bmeng@tinylab.org; helo=bg4.exmail.qq.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL=0.141, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1687966169087100002 Content-Type: text/plain; charset="utf-8" When opening /proc/self/fd fails, current codes just return directly, but we can fall back to close fds one by one. Signed-off-by: Bin Meng --- - feel free to drop this patch if it does not make too much sense (no changes since v2) Changes in v2: - new patch: "util/async-teardown: Fall back to close fds one by one" util/async-teardown.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/util/async-teardown.c b/util/async-teardown.c index 3ab19c8740..7e0177a8da 100644 --- a/util/async-teardown.c +++ b/util/async-teardown.c @@ -48,7 +48,11 @@ static void close_all_open_fd(void) =20 dir =3D 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 =3D sysconf(_SC_OPEN_MAX), i; + for (i =3D 0; i < open_max; i++) { + close(i); + } return; } /* Avoid closing the directory. */ --=20 2.34.1 From nobody Sun May 19 08:30:43 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687966122287546.8883777512075; Wed, 28 Jun 2023 08:28:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qEX5m-0003PY-Tz; Wed, 28 Jun 2023 11:28:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX5j-0003Ow-Gb for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:28:23 -0400 Received: from bg4.exmail.qq.com ([43.154.221.58]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX5h-00054W-Jg for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:28:23 -0400 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 28 Jun 2023 23:27:41 +0800 (CST) X-QQ-mid: bizesmtp72t1687966063txy5lbs1 X-QQ-SSF: 01200000000000F0G000000A0000000 X-QQ-FEAT: uGhnJwy6xZL0JhD88Z/CAM3kvUKefY+l+7qT39yqnAoGg2KyjtsuqtdiiyEXg NbaU3YDPwBOnvebkI2I40saDZ1P5ev98mAnRTD3eg2/1CVNTjnxsP2oArsM35WkpsQWs/iB moa4+rLFzbKM5Ak00xAYVmjLYjCdC7/JJtbI/qQFzFoBUYt5jrArFoqOt/2TkAgYvi7EUVJ dscsaEoKn+uiXIeuP93q3YPnxcEXC9i657Z7DyJD8yuWE+01DjfDDSPIV1szndqPBL4OFLk SXV17au1j4Hus4ormNqgbGmofAERGuWoOZR3L9v9zYouIRnVboNNsk6gGVGDnzUiLpeIVw1 qZKRTXiA3b7jZTRTXmZG4Z0gwNG93l0yxtkkq92iXiiF+gfK+/7SdZqc/jGxQ== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 1278064396760589535 From: Bin Meng To: qemu-devel@nongnu.org Cc: Richard Henderson , Zhangjin Wu , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Kevin Wolf , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , Thomas Huth , Xuzhou Cheng Subject: [PATCH v4 4/6] util/osdep: Introduce qemu_close_range() Date: Wed, 28 Jun 2023 23:27:24 +0800 Message-Id: <20230628152726.110295-5-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230628152726.110295-1-bmeng@tinylab.org> References: <20230628152726.110295-1-bmeng@tinylab.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=43.154.221.58; envelope-from=bmeng@tinylab.org; helo=bg4.exmail.qq.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL=0.141, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1687966123137100001 Content-Type: text/plain; charset="utf-8" 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/fd whenever these are possible, otherwise it falls back to a plain close loop. Co-developed-by: Zhangjin Wu Signed-off-by: Bin Meng --- Changes in v4: - add 'first > last' check logic - reorder the ifdefs logic - change i to unsigned int type - use qemu_strtoi() instead of atoi() - limit last upper value to sysconf(_SC_OPEN_MAX) - 1 Changes in v3: - fix win32 build failure Changes in v2: - new patch: "util/osdep: Introduce qemu_close_range()" include/qemu/osdep.h | 1 + util/osdep.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index cc61b00ba9..e22434ce10 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -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 _WIN32 int qemu_dup_flags(int fd, int flags); diff --git a/util/osdep.c b/util/osdep.c index e996c4744a..1d8c719b3f 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -411,6 +411,66 @@ int qemu_close(int fd) return close(fd); } =20 +int qemu_close_range(unsigned int first, unsigned int last) +{ + if (first > last) { + errno =3D EINVAL; + return -1; + } + +#ifndef _WIN32 + if (last >=3D sysconf(_SC_OPEN_MAX)) { + last =3D sysconf(_SC_OPEN_MAX) - 1; + } +#endif + +#ifdef CONFIG_CLOSE_RANGE + int r =3D close_range(first, last, 0); + if (!r) { + /* Success, no need to try other ways */ + return 0; + } +#endif + +#ifdef __linux__ + DIR *dir =3D opendir("/proc/self/fd"); + if (dir) { + /* Avoid closing the directory */ + int dfd =3D dirfd(dir); + + for (struct dirent *de =3D readdir(dir); de; de =3D readdir(dir)) { + int fd, ret; + + ret =3D qemu_strtoi(de->d_name, NULL, 10, &fd); + if (ret) { + /* skip "." and ".." */ + continue; + } + if (fd < first || fd > last) { + /* Exclude the fds outside the target range */ + continue; + } + if (fd !=3D dfd) { + close(fd); + } + } + closedir(dir); + + return 0; + } +#endif + + /* + * If /proc is not mounted or /proc/self/fd is not supported, + * try close() from first to last. + */ + for (unsigned int i =3D first; i <=3D last; i++) { + close(i); + } + + return 0; +} + /* * Delete a file from the filesystem, unless the filename is /dev/fdset/... * --=20 2.34.1 From nobody Sun May 19 08:30:43 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687966201495843.1361669926906; Wed, 28 Jun 2023 08:30:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qEX77-0007ef-TK; Wed, 28 Jun 2023 11:29:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX76-0007Wy-7B for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:29:48 -0400 Received: from bg4.exmail.qq.com ([43.154.221.58]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX74-0005SO-8Q for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:29:47 -0400 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 28 Jun 2023 23:27:45 +0800 (CST) X-QQ-mid: bizesmtp72t1687966066twpgsy9m X-QQ-SSF: 01200000000000F0G000000A0000000 X-QQ-FEAT: 5q30pvLz2icAGFkF82a5M/N8UMRBKLiaGF0AAETKyxVxLH0r8oR/LEEqUoh3d 7wBLICrGCN1GC2PEGPl7rfq62a7Hhz9aJaN5S47mBJIzswVE1XgYQmxkOIZXAwS2fQQ19TE ODzxTBzNJ8d8MTCkzK1G4V1yY/kqmrOymiKEpvVOQhywngiLzMpbN0z2Y7O3wabqDCPCAKf s2VJNmidFWeF4iMTppwxtCOubGCYQCyqDh1vv9tiLFYUF1cTeZTjl7teQIpAt6r1Q40ILOA NhgOZgHElF/so8qQVv8syAXLFwB8FWOIIY0PI/ziO3b/QjhilxHpLUyL0dUovNpWsC6yx65 xQIn3cTEgg7ntARjyzA6arUsbf3+lhaO5SOjDxmNZRBUOPxuRMZhgF29MFL5A== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 11925419786260877323 From: Bin Meng To: qemu-devel@nongnu.org Cc: Richard Henderson , Zhangjin Wu , Claudio Imbrenda , "Michael S. Tsirkin" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth Subject: [PATCH v4 5/6] util/async-teardown: Use qemu_close_range() to close fds Date: Wed, 28 Jun 2023 23:27:25 +0800 Message-Id: <20230628152726.110295-6-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230628152726.110295-1-bmeng@tinylab.org> References: <20230628152726.110295-1-bmeng@tinylab.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=43.154.221.58; envelope-from=bmeng@tinylab.org; helo=bg4.exmail.qq.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL=0.141, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1687966202771100003 Content-Type: text/plain; charset="utf-8" From: Zhangjin Wu 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(). Signed-off-by: Zhangjin Wu Signed-off-by: Bin Meng --- Changes in v4: - call sysconf directly instead of using a variable Changes in v3: - limit the last_fd of qemu_close_range() to sysconf(_SC_OPEN_MAX) Changes in v2: - new patch: "util/async-teardown: Use qemu_close_range() to close fds" util/async-teardown.c | 41 +---------------------------------------- 1 file changed, 1 insertion(+), 40 deletions(-) diff --git a/util/async-teardown.c b/util/async-teardown.c index 7e0177a8da..a038a255ff 100644 --- a/util/async-teardown.c +++ b/util/async-teardown.c @@ -29,44 +29,6 @@ =20 static pid_t the_ppid; =20 -/* - * 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 =3D close_range(0, ~0U, 0); - if (!r) { - /* Success, no need to try other ways. */ - return; - } -#endif - - dir =3D opendir("/proc/self/fd"); - if (!dir) { - /* If /proc is not mounted, close fds one by one. */ - int open_max =3D sysconf(_SC_OPEN_MAX), i; - for (i =3D 0; i < open_max; i++) { - close(i); - } - return; - } - /* Avoid closing the directory. */ - dfd =3D dirfd(dir); - - for (de =3D readdir(dir); de; de =3D readdir(dir)) { - fd =3D atoi(de->d_name); - if (fd !=3D 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 kernel= s. */ - close_all_open_fd(); + qemu_close_range(0, sysconf(_SC_OPEN_MAX) - 1); =20 /* Set up a handler for SIGHUP and unblock SIGHUP. */ sigaction(SIGHUP, &sa, NULL); --=20 2.34.1 From nobody Sun May 19 08:30:43 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687966153280142.52699974725522; Wed, 28 Jun 2023 08:29:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qEX6A-0004QP-EP; Wed, 28 Jun 2023 11:28:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX69-0004PT-4m for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:28:49 -0400 Received: from bg4.exmail.qq.com ([43.154.221.58]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEX67-0005A7-6d for qemu-devel@nongnu.org; Wed, 28 Jun 2023 11:28:48 -0400 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 28 Jun 2023 23:27:48 +0800 (CST) X-QQ-mid: bizesmtp72t1687966069tmqlnmkx X-QQ-SSF: 01200000000000F0G000000A0000000 X-QQ-FEAT: hvKw+PyJ8U7RjaySGGyFw+oMJe+/6zgEDwZMqVXUjA1W39vtXjC1xN2WiE9J5 2flQK+Q45p9P2HSK22klOny9VuMZ51HSGfh1qTBjIDSfqZgVVWzv5WFJL4H/YsS+2D1tuBC vo3CW+f+ST36LV5bXE+mQciSUWVgmnpPpl+t4Z43RI+1njXU0HkzEU48i8Ph5fkKGdfkS8P dNp75uieNUwLwfyavhrs+8hmaHpUFxEpSbrDInjysJ5/xQCTqasB+q9th3CkZI0GmXaEr1m AqxvGXR67qCjU/+S3Z5e0Op+ZRjkO/z4AGRFO8JiKKKald7SYLzug6qYyj09lZT/UfRuY3Z JnX4i0C8dsnoofqAHjbqEdm0+NQYVF3ESWPIgFbl3vzx6lhUp3svwD6XIJAa+uf30ue8CEi X-QQ-GoodBg: 0 X-BIZMAIL-ID: 13845974076954433580 From: Bin Meng To: qemu-devel@nongnu.org Cc: Richard Henderson , Zhangjin Wu , Jason Wang Subject: [PATCH v4 6/6] net: tap: Use qemu_close_range() to close fds Date: Wed, 28 Jun 2023 23:27:26 +0800 Message-Id: <20230628152726.110295-7-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230628152726.110295-1-bmeng@tinylab.org> References: <20230628152726.110295-1-bmeng@tinylab.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=43.154.221.58; envelope-from=bmeng@tinylab.org; helo=bg4.exmail.qq.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL=0.141, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1687966155080100001 Content-Type: text/plain; charset="utf-8" From: Zhangjin Wu 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 Co-developed-by: Bin Meng Signed-off-by: Zhangjin Wu Signed-off-by: Bin Meng Reviewed-by: Richard Henderson --- Changes in v4: - put fd on its own line Changes in v2: - Change to use qemu_close_range() to close fds for child process efficient= ly - v1 link: https://lore.kernel.org/qemu-devel/20230406112041.798585-1-bmeng= @tinylab.org/ net/tap.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/net/tap.c b/net/tap.c index 1bf085d422..9f080215f0 100644 --- a/net/tap.c +++ b/net/tap.c @@ -446,13 +446,13 @@ static void launch_script(const char *setup_script, c= onst char *ifname, return; } if (pid =3D=3D 0) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; + unsigned int last_fd =3D 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); =20 - for (i =3D 3; i < open_max; i++) { - if (i !=3D fd) { - close(i); - } - } parg =3D args; *parg++ =3D (char *)setup_script; *parg++ =3D (char *)ifname; @@ -536,16 +536,16 @@ static int net_bridge_run_helper(const char *helper, = const char *bridge, return -1; } if (pid =3D=3D 0) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; + unsigned int last_fd =3D sysconf(_SC_OPEN_MAX) - 1; + unsigned int fd =3D sv[1]; char *fd_buf =3D NULL; char *br_buf =3D NULL; char *helper_cmd =3D NULL; =20 - for (i =3D 3; i < open_max; i++) { - if (i !=3D 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); =20 fd_buf =3D g_strdup_printf("%s%d", "--fd=3D", sv[1]); =20 --=20 2.34.1