From patchwork Sat Jun 17 05:36:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13283443 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DB4D2EB64D9 for ; Sat, 17 Jun 2023 05:37:27 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qAOcF-0003M3-L1; Sat, 17 Jun 2023 01:36:51 -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 1qAOcC-0003Li-Ng for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:36: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 1qAOc9-0006VV-Ll for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:36:48 -0400 X-QQ-mid: bizesmtp73t1686980187t9fgj3md Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Sat, 17 Jun 2023 13:36:26 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: G/7Y+EUqWqXTliYupBT/fLrwpK9gg4vtdw4O4gtt5rkguQnU9SBF+Zy5JlwEg mmw8HhGlDX0Bb9vh0WSN+BE8LrmfbCBrxB+H18t2Sepab60zLd+EsWr11rY6JuyBEHwII6J 5I49COqLrW5Xgu2pLEA/4sOzBAufOkh9L1SWsOhr4VAwPLqNGL18/NTXW5WoOBBWtkXVE/h LQYj2mS8dNTMhfKDvc306KWpXE9W1Puj0N26RUXpECgdnXHp7x4jKtc+anZmmZ9E90nakb9 170sDkwDTECV2HN7AXzdT9gTLOMht1dzxkc2QYZsXCD62BAgMMisAlfW0NIv2pFOGrSDtJ5 SzzvZTfBe41GneVlfgQhLDSqnoUn0U553PAy8iv X-QQ-GoodBg: 0 X-BIZMAIL-ID: 12505718854750479731 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , "Edgar E. Iglesias" Subject: [PATCH v3 1/6] tests/tcg/cris: Fix the coding style Date: Sat, 17 Jun 2023 13:36:16 +0800 Message-Id: <20230617053621.50359-2-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230617053621.50359-1-bmeng@tinylab.org> References: <20230617053621.50359-1-bmeng@tinylab.org> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 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_H2=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 Reviewed-by: Philippe Mathieu-Daudé --- (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/check_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 -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); } From patchwork Sat Jun 17 05:36:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13283444 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DDB7EEB64DA for ; Sat, 17 Jun 2023 05:37:27 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qAOcG-0003Me-FB; Sat, 17 Jun 2023 01:36:52 -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 1qAOcD-0003Lr-7E for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:36: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 1qAOcA-0006Vg-4R for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:36:48 -0400 X-QQ-mid: bizesmtp73t1686980190t1xfj5e8 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Sat, 17 Jun 2023 13:36:29 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: YGEVfbPozqkO9POhzd51vSxElWC7EL7UwAVUoogEZckGocwj8Fa9KF7UPkIhc HhaDOPpJYMALsp50mih9Z/rndETnopcwMC8299iEPF2UJ4kb72GWhoYeYlQgS5PnmPVaDqV 34ZljOZOCKuTYMoQ+RovAywB9gVioXagbzesF2+iyanxdWWXRM3jOp0eqf6r8XhrXf9fiVx CCXu9s5woMNiCfiybP8i1dwDFxeExmm1KnZGqzaxH1b3GfHrFfzsjaFlU3GACOq2hhWkpj9 7CWajRuD4aNWxrb/eHQ6+9St/hKTCEAJq84Ws2LrCw/APhjPB7AZDxvdZfbF/Ipvb7cE7Wu USUPDm1AlPtNPgLu/njSxvRm/uiQa4QvJRa4M/Hyb5SUbhx6x4= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 17397187323243324688 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , "Edgar E. Iglesias" Subject: [PATCH v3 2/6] tests/tcg/cris: Correct the off-by-one error Date: Sat, 17 Jun 2023 13:36:17 +0800 Message-Id: <20230617053621.50359-3-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230617053621.50359-1-bmeng@tinylab.org> References: <20230617053621.50359-1-bmeng@tinylab.org> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 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_H2=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 Reviewed-by: Philippe Mathieu-Daudé --- (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/check_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]); - 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(); } From patchwork Sat Jun 17 05:36:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13283446 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E7B4EEB64D9 for ; Sat, 17 Jun 2023 05:37:52 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qAOcV-0003OC-Py; Sat, 17 Jun 2023 01:37:07 -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 1qAOcU-0003Nh-2M for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:37:06 -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 1qAOcS-0006Xe-8U for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:37:05 -0400 X-QQ-mid: bizesmtp73t1686980193tu7g7u6w Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Sat, 17 Jun 2023 13:36:32 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: rZJGTgY0+YOT3oQmERRSL+DXWsKs2Jcv63KNGwgGy65bHQCMbDNI5X4MYzei9 p5eWKVFGrxJBR+BhLzCExe7hqX7hGdVrJ1OoCx6NQxsUoY4m56F1O9bUZmKKMMSzU5tUaTE 7yKL+/MAYSaZ3s7E57uglnL0+H8AcY34D0kaDjlEqo+B0envreyzLRtCanqRbtjZo+wB2R+ NXJX3RYs+j/Wqk9QhhemKshbU0uPO997tAGHSznNC/uvHujBNiBriQaZyDaSNpPk0W65msX nIQKOZaqoa2AszbrNZYuvoJKpomVCfcOTIJ0uPjyGPWWijobI6ZdIfiHh3MmvQ2NMPFutHh SVchiRmR7fXk55Vu6V5bwXJOLXPqVLHQS2zupR9DwTJMBkIx0U= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 8674422273484162088 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , Claudio Imbrenda , Markus Armbruster , "Michael S. Tsirkin" Subject: [PATCH v3 3/6] util/async-teardown: Fall back to close fds one by one Date: Sat, 17 Jun 2023 13:36:18 +0800 Message-Id: <20230617053621.50359-4-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230617053621.50359-1-bmeng@tinylab.org> References: <20230617053621.50359-1-bmeng@tinylab.org> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 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_H2=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 --- (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) 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. */ From patchwork Sat Jun 17 05:36:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13283447 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B6F74EB64DA for ; Sat, 17 Jun 2023 05:38:02 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qAOd5-0003gC-IO; Sat, 17 Jun 2023 01:37:43 -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 1qAOd2-0003cc-8z for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:37:40 -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 1qAOd0-0006bP-Gp for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:37:40 -0400 X-QQ-mid: bizesmtp73t1686980196t9zrj4qw Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Sat, 17 Jun 2023 13:36:35 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: zT6n3Y95oi2EiXqmYRonV2F7ggvWOR3fbksb22QYth0Uiiy96y9ja5+IbqwE0 KoRXvzRVjpLWZSEmEujgLtrRtv5Cgn1KV5XpC8iPcrye4wbEebHi3Oy5YlwmnYx/7hK1vwr 4Iy/ePuSTcCVkM5YSw9EgMHaxU6LQmamfnDmZOWWn+nF0O7eVRxhJ7IARGi7eVUTlF/6+Ub Kc2Dh0vcL4ExbI9vALVqRqQOQmhPhYXRZfHAVuk0kcR5aTAqfMP9LRem+YVV7fWlTFLAuiN 2sFzjWKC9PNzmSzNlaRhiP+BKAFByIqrGw3WOIWVC5RxCadSzCWFLmgCmopBU8Y1OzegfeY UDKJWEUoQzKPzgrHL706po3wQehPIlBGT+LVPJ5IWiZq9k2ngXry3xeMrVMig== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 9052436690627796296 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , Alberto Faria , Kevin Wolf , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Nikita Ivanov , Paolo Bonzini , Thomas Huth , Xuzhou Cheng Subject: [PATCH v3 4/6] util/osdep: Introduce qemu_close_range() Date: Sat, 17 Jun 2023 13:36:19 +0800 Message-Id: <20230617053621.50359-5-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230617053621.50359-1-bmeng@tinylab.org> References: <20230617053621.50359-1-bmeng@tinylab.org> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 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_H2=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 v3: - fix win32 build failure Changes in v2: - new patch: "util/osdep: Introduce qemu_close_range()" include/qemu/osdep.h | 1 + util/osdep.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 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..91275e70f8 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -30,6 +30,7 @@ #include "qemu/mprotect.h" #include "qemu/hw-version.h" #include "monitor/monitor.h" +#include static const char *hw_version = QEMU_HW_VERSION; @@ -411,6 +412,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); + if (fd < first || fd > last) { + /* Exclude the fds outside the target range */ + continue; + } + if (fd != dfd) { + close(fd); + } + } + closedir(dir); +#endif /* _WIN32 */ + + return 0; +} + /* * Delete a file from the filesystem, unless the filename is /dev/fdset/... * From patchwork Sat Jun 17 05:36:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13283449 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 63B22EB64D9 for ; Sat, 17 Jun 2023 05:38:29 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qAOdj-0006Jf-CY; Sat, 17 Jun 2023 01:38:23 -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 1qAOdW-0005pL-Mw for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:38:14 -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 1qAOdT-0006h3-B1 for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:38:09 -0400 X-QQ-mid: bizesmtp73t1686980200tmc8uug1 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Sat, 17 Jun 2023 13:36:39 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: YHTLUubWl25IZCNnJ0AgRvrjw5CKKED9w+RGLCmZdhx6ai2AkIQMFysmZtPm9 xJF7FwaLwGjU6HRiD7D/qTsq4VF2WePudXD/4SGe47dDLT7QOdPuI9JEJJbNoLlpTvw7YxZ oSB37Rvnm2RkUkdSyZyB7417fLZ+jnr1yNPgogkEN+kYJZ5J91HYS85ar6bA9+Dzi2uy8yV 2BHIIomEtgXxxTTv9J2G/qogP/xPkODdjfPAOU5GPTOc+rIPyCGY2xEhjG3Dy0hge2PnSbW 20hQCjRkABtBcXnzTrtUC4xNLt2+8bdrYZctm5ay8/0jwPCdHtQSYC5nzbV9IphttQ0X9w+ VvNMTEZ2VqXIKyiQD9P11jz1rPs9Aghb/VpIiutIW8EZBytJTU= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 16536509347809008363 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , Claudio Imbrenda , Juan Quintela , "Michael S. Tsirkin" , Paolo Bonzini Subject: [PATCH v3 5/6] util/async-teardown: Use qemu_close_range() to close fds Date: Sat, 17 Jun 2023 13:36:20 +0800 Message-Id: <20230617053621.50359-6-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230617053621.50359-1-bmeng@tinylab.org> References: <20230617053621.50359-1-bmeng@tinylab.org> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 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_H2=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 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 | 42 ++---------------------------------------- 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/util/async-teardown.c b/util/async-teardown.c index 7e0177a8da..e102912f3f 100644 --- a/util/async-teardown.c +++ b/util/async-teardown.c @@ -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. */ @@ -84,6 +46,7 @@ static int async_teardown_fn(void *arg) struct sigaction sa = { .sa_handler = hup_handler }; sigset_t hup_signal; char name[16]; + int open_max = sysconf(_SC_OPEN_MAX); /* Set a meaningful name for this process. */ snprintf(name, 16, "cleanup/%d", the_ppid); @@ -92,9 +55,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, open_max - 1); /* Set up a handler for SIGHUP and unblock SIGHUP. */ sigaction(SIGHUP, &sa, NULL); From patchwork Sat Jun 17 05:36:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13283445 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2E93CEB64D9 for ; Sat, 17 Jun 2023 05:37:47 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qAOcL-0003N0-0j; Sat, 17 Jun 2023 01:36: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 1qAOcJ-0003Mo-Je for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:36:55 -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 1qAOcH-0006Ws-Jd for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:36:55 -0400 X-QQ-mid: bizesmtp73t1686980203tqp8xinx Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Sat, 17 Jun 2023 13:36:42 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: Sb649gZU4sJPb/EvvUKTDf299KFDY5T2M2T8X4w9Gbd939gBOH7HMb0xADMtI npLIOoXy/eHjV4ugOnUqW5iSVQ0NaGDxlad9ZAAO24QEuPzT4MRdclYgVZSLxWyOBHDiJ2t 571F62mT07cHqLzprIypLCjMe4L8sB8TyP0HXZykBRuQuIY/N1JNbD/r/5s1K1bAlNby4By zoplx4Gy/n02dp/GOsfH5/OIH7Us7RHo9BquJLjVROcbGULI4HgZNGEJUcdvRNhC8kVFHQx lRO+3U5EsigqbBqLKekcYXMB1K0IBgRVXWXA1lLKd7yfBoBuHsqHr6NBkzGYAH5sagCc+io aXyKDDEKGuAsy1zS1L9bLlFWJjCNXW8EGuXDSAP6IwgFekglkRaAsOspt7LXRPn6c/Exm3f X-QQ-GoodBg: 0 X-BIZMAIL-ID: 2473288236721930917 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , Jason Wang Subject: [PATCH v3 6/6] net: tap: Use qemu_close_range() to close fds Date: Sat, 17 Jun 2023 13:36:21 +0800 Message-Id: <20230617053621.50359-7-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230617053621.50359-1-bmeng@tinylab.org> References: <20230617053621.50359-1-bmeng@tinylab.org> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 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_H2=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 --- (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]; 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]);