From patchwork Fri Jun 16 15:27:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13282887 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 1C5C0EB64D7 for ; Fri, 16 Jun 2023 15:29:09 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qABMt-0004My-Ow; Fri, 16 Jun 2023 11:28: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 1qABMr-0004MD-4l for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:28:05 -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 1qABMo-0000eS-By for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:28:04 -0400 X-QQ-mid: bizesmtp84t1686929264tzo7kg5l Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 16 Jun 2023 23:27:43 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: iDzLjIm7mlbJhIxtyny+gy+H2geVvwX+VOUwjvgXXU1PuSf5oCk1Ncw+g9l2c Sy0LE1XOfvNLm8ZwCsA0UZ2aFXPaCitEw9PcUZH3rIgGUwmIuME81mIHFjx1UA8XT1n6zAn dqD5xxVrd03AMmUSyODRpDzMQR37SvI7GOfZ4wqC666cvvIL97IzjoSfAji6zKWwvCWUnH3 wwR33Cq/TDmOwJ5t4BoDsJ4GKgoa0y36hm12RY8IyHILPIRuIJAskZXuven0vsJMxCIuKaD ZIChjNhWan1CFk+vragxOwnZMJqdbndeTUKqbH8G09LFq4KhbMofK+OxKqyaUm8vd8SwQqd UL14Z/0lIjwSMKbF1NPHCuUMes1O4T5CnSB3lv1v2FRCaylIDY= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 640512983429797124 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , "Edgar E. Iglesias" Subject: [PATCH v2 1/6] tests/tcg/cris: Fix the coding style Date: Fri, 16 Jun 2023 23:27:32 +0800 Message-Id: <20230616152737.23545-2-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230616152737.23545-1-bmeng@tinylab.org> References: <20230616152737.23545-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 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 --- (no changes since v1) 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 Fri Jun 16 15:27:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13282885 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 99071EB64D8 for ; Fri, 16 Jun 2023 15:28:55 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qABMt-0004N3-Oh; Fri, 16 Jun 2023 11:28: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 1qABMr-0004MN-JK for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:28:05 -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 1qABMo-0000em-FD for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:28:05 -0400 X-QQ-mid: bizesmtp84t1686929267tgjdfi8k Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 16 Jun 2023 23:27:46 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: +ynUkgUhZJnlA1MWv37RMzQMUYwVOeuiJMqkxFQ2dA8KvJgdzh7bl2OT65aeL txwDQZ5e5ht5SLBSel5AY1tjn5YuYS/UvIT9668SAPRtKYgbXI6zLWTi+PQiaCBIuHmeVGe EsB4+gFxBGpf7exdaZpN252B2FIO2RQCDXSqw9HZlzmzGt+HZv/rp5kwtVX4fcK4zrsc0ic Drvs8DTyoMleAQrQO9RMHwnscXya3rhBiSPmM/FLBWj+L894SOGUurSxxBaEYlXq4K3BECL HTwDP88LyW4F2fu+GMeiYxLeWDxh/h1PTzghX0MpxyfCN0nKw812bg3BMZDdlYioIsH7jEv Mk7WMT+qneVwVJeYrEefIObixT0Qa2E4nlEZVDqaWStGOvaJvs= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 8888829801256046820 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , "Edgar E. Iglesias" Subject: [PATCH v2 2/6] tests/tcg/cris: Correct the off-by-one error Date: Fri, 16 Jun 2023 23:27:33 +0800 Message-Id: <20230616152737.23545-3-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230616152737.23545-1-bmeng@tinylab.org> References: <20230616152737.23545-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 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 --- (no changes since v1) 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 Fri Jun 16 15:27:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13282899 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 64941EB64DA for ; Fri, 16 Jun 2023 15:30:18 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qABNr-0006B4-7b; Fri, 16 Jun 2023 11:29: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 1qABNp-00069x-GA for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:29:05 -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 1qABNn-0000uV-9Q for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:29:05 -0400 X-QQ-mid: bizesmtp84t1686929270t60oiv8o Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 16 Jun 2023 23:27:49 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: 90EFqYDyPxAadufHtL43FjIlVtvePuzH9FZHXO8miEAY9G+nVafLL5Y6i1iBB m/FRQFCEV+iIJbO/bdxZdaQsaKNYxXjd4BDTXr+FEzwIL5ltCq6ntX5SS4eRKeH0gUHKIKD EwmOkpzGpu+/mVLfUpdn+U88UBYy+VlvSLSRp4gE3ejzXIuKA4/glLpg0Cx9+S/vzOv6eRb qHW3SE0FeTAMLf5G+9jB1cXjdSuI+2d8FlWThMJ6dmCJM9oX52LT1ZM6qOxr5W47tIioFQa nE/2CK9ffVdxqg1ul53M5o4vU8/hN+pSJFFwK4p57EM8KPramtlFDCLlouriyl/X2UYs65H iXmCko9IitjSzJr7OGppG+8QpqheorIMA9HGnwZtje31QAK5f4= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 11881041971104729125 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , Claudio Imbrenda , Markus Armbruster , "Michael S. Tsirkin" Subject: [PATCH v2 3/6] util/async-teardown: Fall back to close fds one by one Date: Fri, 16 Jun 2023 23:27:34 +0800 Message-Id: <20230616152737.23545-4-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230616152737.23545-1-bmeng@tinylab.org> References: <20230616152737.23545-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 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 v1) 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 Fri Jun 16 15:27:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13282895 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 0BA68EB64D7 for ; Fri, 16 Jun 2023 15:30:10 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qABO3-0006Zv-69; Fri, 16 Jun 2023 11:29:20 -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 1qABO0-0006VD-Fz for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:29:16 -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 1qABNy-0000wJ-JB for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:29:16 -0400 X-QQ-mid: bizesmtp84t1686929274t65mp03n Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 16 Jun 2023 23:27:53 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: Gq6/1HjPYVXPLR78nFrOrxm64n8pk9C0+/IzNIXgNqYxTRkuX3ntcB4RxIqsJ XOq2cfkCwMtaaYQ6vcS3P+R6rx4IpjylcnjrCvfN0kanPkk0oif7oX9Ia1ri+PyKLd7mSB9 fNyQCKND3krFrO4qKNY12ps398Ftm+bjO1INDyuSFRL5n5WgnCDZSZQi0AJTpbu4HPE0FNN rbXnKfCr1DNQ6TiXFytbCuceogjcz1ymZjng0+7jZqwiexl+Ez7DsCeh7mSV83k3fsstgZA OVZgsifN3Lg4T6qWMJKsC3Own8NRvXRj1qf2AxRFHi0KnK0a7eo6Si5gjGvST22oNvkay6y qKIwqCgI/XjhdtWLyGSIyCuAEDq+SuY83p4O9xEbEJEOm/L83w= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 4659172374281023283 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , David Hildenbrand , Kevin Wolf , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Nikita Ivanov , Paolo Bonzini , Thomas Huth , Xuzhou Cheng Subject: [PATCH v2 4/6] utils/osdep: Introduce qemu_close_range() Date: Fri, 16 Jun 2023 23:27:35 +0800 Message-Id: <20230616152737.23545-5-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230616152737.23545-1-bmeng@tinylab.org> References: <20230616152737.23545-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 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 --- (no changes since v1) include/qemu/osdep.h | 1 + util/osdep.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 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..fd7dd2dbdf 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -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); + 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 Fri Jun 16 15:27:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13282898 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 3C41DEB64D8 for ; Fri, 16 Jun 2023 15:30:16 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qABNn-00060n-7b; Fri, 16 Jun 2023 11:29:03 -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 1qABNl-0005qz-KL for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:29:01 -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 1qABNj-0000tq-KT for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:29:01 -0400 X-QQ-mid: bizesmtp84t1686929278t1n7bm2i Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 16 Jun 2023 23:27:57 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: 7QbCsSX/jDYvEe3MYAXFNrV2/u/ukomn5gMl5ZLM8iOtzL2KCEMNbKgHShU88 XN8CTACAXCG0Xuo8If0gMVLZF3WeIcSVc0J6qpcyMyFVtmFmFdpqZ6c+NHB0JQtX7QHpQsK OW8m4T4chjHSruBjboKDFgKuLjlQUW/FZckcoVCT6qTYjFOs8M2z0R4h7r0Et4sViOO+UWk SkywlqL155jpP4rWkBD66HbtXYVVxH7dzndEg5zg+KpKqN3Fz+n8ilBaYb3NAErWZG9JRON 8Rm3xnJHkm9220HJyeZwFITI/1eUM6sYgpk1x2dyzeE3YJ0//GQTf96BT98lqkJPpx6WMAQ TYXrqv1a/eZhlPEOKcUAaWqQWASPVF3ZNbHmHuIK/GFAZBtf/F4Qz4o10j7pw== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 11677936551556718701 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , Claudio Imbrenda , "Michael S. Tsirkin" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v2 5/6] util/async-teardown: Use qemu_close_range() to close fds Date: Fri, 16 Jun 2023 23:27:36 +0800 Message-Id: <20230616152737.23545-6-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230616152737.23545-1-bmeng@tinylab.org> References: <20230616152737.23545-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 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 --- (no changes since v1) 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..200ec05101 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. */ @@ -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); From patchwork Fri Jun 16 15:27:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 13282892 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 6F89DEB64DA for ; Fri, 16 Jun 2023 15:29:40 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qABNz-0006R4-3F; Fri, 16 Jun 2023 11:29:15 -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 1qABNw-0006J3-Kf for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:29:13 -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 1qABNu-0000vu-Kk for qemu-devel@nongnu.org; Fri, 16 Jun 2023 11:29:12 -0400 X-QQ-mid: bizesmtp84t1686929281t57hu2x1 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 16 Jun 2023 23:28:00 +0800 (CST) X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: +oIWmpEafD9b17Bsn3l+qM6TfpSiQMig/2jVKbBg+YupV8DneZMJaIk3iMQXU 0GkGy2BYh5vXgTmc4PtjryjdaCLPg4Hi426N57fYIQbG+iR61RgPsnZJD/elQdRHvMZNYRx XSqX1Gtw9WnbiC2vBPt0GO3bx15L6jf4pPMIdkkq4TDqBWwU+Ps9hXbkjF5xgJyw+hYX5e4 EXwKumk9dUAadYv4A2uEykFWCRfibvKBL8Q7KAuIAuiTW/DZNq6XnLxuzjSqRRTef3p2hON LyO+8Qg7kNXsXNJR/N+KtIrjBIIcfwtoWC1Wmmtt+v8QdpwThdAiyugTQdcMNLSporkeDH7 NW3k/PN/ZjfqEMZjDCC11bY7ouOu2bxBe9IdoP+dJV1wBFeB6LjOH/7z/lrWg== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 4716907015537848369 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , Jason Wang Subject: [PATCH v2 6/6] net: tap: Use qemu_close_range() to close fds Date: Fri, 16 Jun 2023 23:27:37 +0800 Message-Id: <20230616152737.23545-7-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230616152737.23545-1-bmeng@tinylab.org> References: <20230616152737.23545-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 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 Signed-off-by: Zhangjin Wu Signed-off-by: Bin Meng Reviewed-by: Richard Henderson --- 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]);