From nobody Sun Nov 24 11:43:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1722817953; cv=none; d=zohomail.com; s=zohoarc; b=nMckRIQgSppxJ7PENU64nLD8vz9w30tx94zHd7gxPTKWSXvFm25nh+y2Fha1a1B/+IKrySA9o5wUYGBGQ1oF5kXmPUOVfnebI+NjAsRdFGOjtfiiPkRqFtfYLkA11gmoJR64P6/M1vcxvh9K/m0On3UxTW4kk+PydooVnoGS81s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722817953; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=8WJQORCpqGoeFxGSwBWz+2gItcHfSAbHK8FGjbSdfpM=; b=Kb4a1KMDVYGS7zqXI7ANQJv3xybQeDFBRcSTQzcPCeH1sc4FdsFDWoO08OZQ41xTA7MbdzMMj/I1sQuRYDSP8gttWphQblQFOF8O2dvmu41dFgiuPAMxZYpk4poY98coT6bk7JQqyHcCMYGSDq1CWxn5vQ1fvvFa2rRkpuD3GHA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722817953421912.9258826424434; Sun, 4 Aug 2024 17:32:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1saldY-0007Z6-8Y; Sun, 04 Aug 2024 20:31:44 -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 1saldW-0007Rl-Fj for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:42 -0400 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldU-0002IY-TS for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:42 -0400 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-70d2b921c48so7633708b3a.1 for ; Sun, 04 Aug 2024 17:31:40 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817899; x=1723422699; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8WJQORCpqGoeFxGSwBWz+2gItcHfSAbHK8FGjbSdfpM=; b=g53sSO+RsOfxs0qHyP53M+L/fOnR+O3Ag8kiudkpuAHWWoLv+Ig+e3mwbYt7k+o8TO TRsoRbzOZghwfMztIjKkVM5vm6B/HEcU8sgJ8coRZlAn4/E7eCwu5/LpmjWK0b7C7uZc KSSrgDR7VjKf1dEroFhP5iLQNxP6pEbS35f+LNCuTY2rQAPrpvYDM2q9+Kt2FoODsj2P htcYfBiH4wURX6R+UQ5P6cgvVmnNZm9b7KJKjo58QKnoXV9GsQ4PgO7zEVQeHflDPWmd 1Dgsq+Ku9ziq+e0SFiIB6b7cLHp7k1vw31ZMY+tbqZ0ucw1q4KtLodSBZlU0ms/nX+49 0E0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817899; x=1723422699; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8WJQORCpqGoeFxGSwBWz+2gItcHfSAbHK8FGjbSdfpM=; b=D9UDyadZCHeBYZs6YXW8Dpe7G+qYGVbn7wd/OPVv1TjqPVxEvJQVTbNgvf7fau084C KkdC7bwqM9OPUw504SbqUeENUuQJAgWiXdgvlMulNi3/mqEezMqk70p0E9o6/LYOG83A gQZVXskX2tzqYyiTJ/ixttcAtYhw+396j+xU4mAsVzyFiiV864QSjBlrtoc9BBlqMlTN ulJLbEll5EtBABTQ53K8yxgjyVC5QYrv5CCX1u2R3QdAbOynsqML148cz+fY/JdmCvCm +BkYtd1iaDv3wkG1eskmH4QjK7kkDKtG/6W9yVYvH2amZLUnOsnpW+NjizK5oZ/ui2cw KGOA== X-Gm-Message-State: AOJu0YzOJxAVXKhb2Kepo+47SgQlBKBRJgH/63Avq/fvk4Jjv3eRPoIa p0X9zKGOO6BRGvKM5jij+VLlcWxfsn8Lkure1pt0RI4021yRsGgRxcbEEHT5HliZikHy1/CKq6W Znn0hBg== X-Google-Smtp-Source: AGHT+IHRVeJYIAOugczrEUp3rwDRJdt4EzDd+zTnObXo5TGnvEWwuh9O7O3BIZqsRwjwEp/04Nt1qw== X-Received: by 2002:a05:6a20:7f84:b0:1c4:8da5:5825 with SMTP id adf61e73a8af0-1c699429e34mr10679109637.0.1722817899175; Sun, 04 Aug 2024 17:31:39 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Ilya Leoshkevich , qemu-stable@nongnu.org Subject: [PULL 1/6] linux-user/elfload: Fix pr_pid values in core files Date: Mon, 5 Aug 2024 10:31:23 +1000 Message-ID: <20240805003130.1421051-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240805003130.1421051-1-richard.henderson@linaro.org> References: <20240805003130.1421051-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::42f; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1722817955549116600 Content-Type: text/plain; charset="utf-8" From: Ilya Leoshkevich Analyzing qemu-produced core dumps of multi-threaded apps runs into: (gdb) info threads [...] 21 Thread 0x3ff83cc0740 (LWP 9295) warning: Couldn't find general-p= urpose registers in core file. in ?? () The reason is that all pr_pid values are the same, because the same TaskState is used for all CPUs when generating NT_PRSTATUS notes. Fix by using TaskStates associated with individual CPUs. Cc: qemu-stable@nongnu.org Fixes: 243c47066253 ("linux-user/elfload: Write corefile elf header in one = block") Signed-off-by: Ilya Leoshkevich Reviewed-by: Richard Henderson Message-ID: <20240801202340.21845-1-iii@linux.ibm.com> Signed-off-by: Richard Henderson --- linux-user/elfload.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 0d4dc1f6d1..b27dd01734 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4102,8 +4102,7 @@ static void fill_elf_note_phdr(struct elf_phdr *phdr,= size_t sz, off_t offset) bswap_phdr(phdr, 1); } =20 -static void fill_prstatus_note(void *data, const TaskState *ts, - CPUState *cpu, int signr) +static void fill_prstatus_note(void *data, CPUState *cpu, int signr) { /* * Because note memory is only aligned to 4, and target_elf_prstatus @@ -4113,7 +4112,7 @@ static void fill_prstatus_note(void *data, const Task= State *ts, struct target_elf_prstatus prstatus =3D { .pr_info.si_signo =3D signr, .pr_cursig =3D signr, - .pr_pid =3D ts->ts_tid, + .pr_pid =3D get_task_state(cpu)->ts_tid, .pr_ppid =3D getppid(), .pr_pgrp =3D getpgrp(), .pr_sid =3D getsid(0), @@ -4428,8 +4427,7 @@ static int elf_core_dump(int signr, const CPUArchStat= e *env) CPU_FOREACH(cpu_iter) { dptr =3D fill_note(&hptr, NT_PRSTATUS, "CORE", sizeof(struct target_elf_prstatus)); - fill_prstatus_note(dptr, ts, cpu_iter, - cpu_iter =3D=3D cpu ? signr : 0); + fill_prstatus_note(dptr, cpu_iter, cpu_iter =3D=3D cpu ? signr= : 0); } =20 if (dump_write(fd, header, data_offset) < 0) { --=20 2.43.0 From nobody Sun Nov 24 11:43:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1722818007; cv=none; d=zohomail.com; s=zohoarc; b=B0ZhccvwU/MAIwsQB71xpp6MUFtIdYL0lmE/kp00oqPAwDiOlqkadHpgbD83sVrZgec8vAVvWAbEFWSw7hxiZqBJX/3wGzLYSF0iNCSorZ7QiAsZkuVtuSrHtd4u0+uNruOzMf54jN+hzfKzM4fCqh8s96k0GOgWZvkdzUSm9jI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722818007; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=hbxagQGZ9enwm2uWyobspLUjv+kLe0ys5wANtBKsnzE=; b=a3FMzx4uMIuEeLJxxt906aCXeHfX803tyvwgPQIS2kt9S+YhQi69jCnx0Rzc8EszZlzfO7A4n8ftJBs55fQNK3TF0RDl3Wu3IAUcn64whD+wm1PBsgM5iStzcnLsTRtqquIjPgA4+J6sPbmouSLU0TYQ7Hor42CAcbsIuKyu+0U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722818007861182.26481540230895; Sun, 4 Aug 2024 17:33:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1saldd-0007ts-Co; Sun, 04 Aug 2024 20:31: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 1saldb-0007m6-FN for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:47 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldZ-0002JM-Qj for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:47 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-70d150e8153so3301865b3a.0 for ; Sun, 04 Aug 2024 17:31:45 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817904; x=1723422704; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hbxagQGZ9enwm2uWyobspLUjv+kLe0ys5wANtBKsnzE=; b=CZ1DcJwZuqy/nrM8pY5oc5XgukNTvh4OffcFLyd3oxKMZDsUYaGuZFQ3yicXXaaZyk bDFVhKOQ6tgnchLnIqOSP1xSpAot9W1RljpQlUWAcJLpJWivoRO/rHkjl1e/o6eDovCH PlmZecFGjRZZbyzCfGFR964hRQcShryN8dTxYsKVkG/cHUtBXhFXR0ZcqWQE0tEveW20 1FM+hU3DDmUXMEI9YAX88h36u4mXkZmBSoRXOysYkFPqbOfpyBfv6e/Hz1BmeXg06via UMjyDLAiKkYlrb+2GVKqJNVNhF2PxLyvy5VjksJYt1MmAWRWQbS0/KpYEENFnM8joqjI 6Euw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817904; x=1723422704; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hbxagQGZ9enwm2uWyobspLUjv+kLe0ys5wANtBKsnzE=; b=ICOfgLHxGY705U8ryk2DvRz03OXMMZnO2jNu2cCfK8sAJvFGnbVRpGMpC+zmKLKGPu wA9SDkFN1xMvGm60hxMpOdijchlZUrESZ1lZkK0nTVkmgCTz/D5ZKWaiwgfLdLKcyAJm rS11y0wksvqM/VdHue7ea6EN+8UwVtHaI5kO9g2tvotyqwiAUi3AHfmcJOIjKkLy7xKC FHXCwkeVlY4eKyAs7GPKtEimMs3la7MzXOGFzPrI0tkWpKIhwZFphd5otpqhAGzaWzpl L4EeRVWLn0uORDe/1/UEls+ShrdwS4Qag4Tg90IgrA7XC4l1bdcJbZ0cSCoYuLaO4RE5 Homg== X-Gm-Message-State: AOJu0Yx7ZNyNX7D84xnrCEBpjjo7/PG4d3Uhdv/R5RptUi6/E/Kid9+F MuHPbrYi662E4ltNA9W38EaUBEQnQo+MMfx/xjIzyurKU1VCML+9WHCHu6kGPF4iGvBwN5xqhKG /yM+JuA== X-Google-Smtp-Source: AGHT+IFkkUC+mFjLaRBAUN6z7tpvd3EEx0ZAA7xLlBHfBHi/onGdIPKnCeGszYUm0YHph88ERT6c3g== X-Received: by 2002:a05:6a00:2406:b0:70d:14d1:1bad with SMTP id d2e1a72fcca58-7106d9d9330mr18041602b3a.3.1722817904230; Sun, 04 Aug 2024 17:31:44 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 2/6] qemu/osdep: Move close_all_open_fds() to oslib-posix Date: Mon, 5 Aug 2024 10:31:25 +1000 Message-ID: <20240805003130.1421051-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240805003130.1421051-1-richard.henderson@linaro.org> References: <20240805003130.1421051-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::430; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x430.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1722818008664116600 From: Cl=C3=A9ment L=C3=A9ger Move close_all_open_fds() in oslib-posix, rename it qemu_close_all_open_fds() and export it. Signed-off-by: Cl=C3=A9ment L=C3=A9ger Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Richard Henderson Message-ID: <20240802145423.3232974-2-cleger@rivosinc.com> Signed-off-by: Richard Henderson --- include/qemu/osdep.h | 7 +++++++ system/async-teardown.c | 37 +------------------------------------ util/oslib-posix.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 720ed21a7e..de77c5c254 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -757,6 +757,13 @@ static inline void qemu_reset_optind(void) =20 int qemu_fdatasync(int fd); =20 +/** + * qemu_close_all_open_fd: + * + * Close all open file descriptors + */ +void qemu_close_all_open_fd(void); + /** * Sync changes made to the memory mapped file back to the backing * storage. For POSIX compliant systems this will fallback diff --git a/system/async-teardown.c b/system/async-teardown.c index 396963c091..edf49e1007 100644 --- a/system/async-teardown.c +++ b/system/async-teardown.c @@ -26,40 +26,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, there is nothing that can be done. */ - 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. */ @@ -85,9 +51,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_all_open_fd(); =20 /* Set up a handler for SIGHUP and unblock SIGHUP. */ sigaction(SIGHUP, &sa, NULL); diff --git a/util/oslib-posix.c b/util/oslib-posix.c index b090fe0eed..1e867efa47 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -807,3 +807,37 @@ int qemu_msync(void *addr, size_t length, int fd) =20 return msync(addr, length, MS_SYNC); } + +/* + * Close all open file descriptors. + */ +void qemu_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, there is nothing that can be done. */ + 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); +} --=20 2.43.0 From nobody Sun Nov 24 11:43:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1722817998; cv=none; d=zohomail.com; s=zohoarc; b=n9IEN5m5Cwnz4EQqu4xkvFRGXeb/He/seunI0bggl9Ep/l9iwAF2/BPf9nNwmd+gbepr602CEqF2ZNqqGjoDcT7P0PBx78VodqnfRwVdpe4buc/SnMHK+omLpS4orAOp59GNAoNEHp57SeITzN42jZYRrsyfwmevvoUoGboQjKI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722817998; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=V6ZlS+VMsFpi/JD9xfA3NQaSnzna6YC/nJ4q2H/FlJk=; b=KNXsflKIsXsY1tB1ALmqBqJw1iL01ad82TnoOhtHuETHpK2LD99unt+m80WDkbXopLGLmvFRdgrZnmq3u6tLddWPslSILWX8TwJepdE4TMntE/FIzniE0B3b9HqLp/IDVCB44wVnOc3fUH0YwkJ111DWT8/Sj6vcBWJOXv+tkG8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722817998762716.6126976867496; Sun, 4 Aug 2024 17:33:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1salde-00080A-W8; Sun, 04 Aug 2024 20:31: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 1saldd-0007vp-SI for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:49 -0400 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldc-0002JW-97 for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:49 -0400 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-70d1c655141so7301749b3a.1 for ; Sun, 04 Aug 2024 17:31:47 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817907; x=1723422707; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=V6ZlS+VMsFpi/JD9xfA3NQaSnzna6YC/nJ4q2H/FlJk=; b=FL00/yi0BB+1eNCp4Jl2CuQYIMPbINmyP0hYKnOoLPQ4hkKU9bZv7ko3kxEbpSm2iS YUI/WQub3LhHKooFSF1c7M6S1q1Fb+R1Ub5y4o4JY2he+Pe6YcN5iwTpk9H2M7vjGFv2 F1DRtu9hKhEs4cHvfBLQzX2YlZoWLg/KNv7UnmVod3Q/xrfToWyWccpMrSFdbnmtXIKf vNs8ZQzqOXyDQtmwHqZHdUtGUL160mMoXsK8+9PvuVgpeIDm4TpFxTDcWRRq9Hfav126 icbLcS5KEmQiCQGvMzbFMBGfJqY3+GjL/g7VOYKItWFx6fw85LLstrVw6FxbGmzp1jCy 9Bcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817907; x=1723422707; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V6ZlS+VMsFpi/JD9xfA3NQaSnzna6YC/nJ4q2H/FlJk=; b=f5UDe403Kj7E6uXdpL1iX5PhOjrt18ks7cHXfYkNSq2XbbDTgnxMS0uKTGsE0wq4vq mBNjMj1hnm/nB3cORemNn4wetD23+rgZ6I8dNrPLK3G0TXvRHLJDLHy3k+XovZPwA2+x Z+/nbxvaqJ4gm2qvrVFsHE0IxpuFoEJc43ZJtixqGtuUgsP2nIT/od28gRGCKf8/WTg9 YdyhlxXS0IW+Gm91yRUBUhak0gI2Y6xohI0miSt3rSrkiJ6hzQK6PgStQTC6B0wux36/ rEqW38imEp91nMBPSP5N1pFQOSXzqB4Di8pjLeRKjyVv9iIvUkgQT0qXJUdu61Dxh4gX ctTA== X-Gm-Message-State: AOJu0YyEjF815SCWC4XbdPNOD1MhRkju5O4FT+nddgU1fD8HzbjDFA3i dLndhVfzoQZWpVPGGU1L9VI4aTjsNCThOg+GCMVTpvunAMtCTbhJxwoO2nFuwCnZZseb62HYKhF GFUuZew== X-Google-Smtp-Source: AGHT+IH6fhoe6GxYqp8faMs8tNpspGeK+SxjbQLgT/5zILlVRNg279qzJemFoQGNWGAWZneOqFdaSg== X-Received: by 2002:a05:6a00:194a:b0:70d:3a5d:5d4a with SMTP id d2e1a72fcca58-7106cfafe51mr9332762b3a.12.1722817906752; Sun, 04 Aug 2024 17:31:46 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= Subject: [PULL 3/6] qemu/osdep: Split qemu_close_all_open_fd() and add fallback Date: Mon, 5 Aug 2024 10:31:26 +1000 Message-ID: <20240805003130.1421051-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240805003130.1421051-1-richard.henderson@linaro.org> References: <20240805003130.1421051-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::42c; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1722818000554116600 From: Cl=C3=A9ment L=C3=A9ger In order to make it cleaner, split qemu_close_all_open_fd() logic into multiple subfunctions (close with close_range(), with /proc/self/fd and fallback). Signed-off-by: Cl=C3=A9ment L=C3=A9ger Reviewed-by: Richard Henderson Message-ID: <20240802145423.3232974-3-cleger@rivosinc.com> Signed-off-by: Richard Henderson --- util/oslib-posix.c | 50 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 1e867efa47..9b79fc7cff 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -808,27 +808,16 @@ int qemu_msync(void *addr, size_t length, int fd) return msync(addr, length, MS_SYNC); } =20 -/* - * Close all open file descriptors. - */ -void qemu_close_all_open_fd(void) +static bool qemu_close_all_open_fd_proc(void) { struct dirent *de; int fd, dfd; DIR *dir; =20 -#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, there is nothing that can be done. */ - return; + return false; } /* Avoid closing the directory. */ dfd =3D dirfd(dir); @@ -840,4 +829,39 @@ void qemu_close_all_open_fd(void) } } closedir(dir); + + return true; +} + +static bool qemu_close_all_open_fd_close_range(void) +{ +#ifdef CONFIG_CLOSE_RANGE + int r =3D close_range(0, ~0U, 0); + if (!r) { + /* Success, no need to try other ways. */ + return true; + } +#endif + return false; +} + +static void qemu_close_all_open_fd_fallback(void) +{ + int open_max =3D sysconf(_SC_OPEN_MAX), i; + + /* Fallback */ + for (i =3D 0; i < open_max; i++) { + close(i); + } +} + +/* + * Close all open file descriptors. + */ +void qemu_close_all_open_fd(void) +{ + if (!qemu_close_all_open_fd_close_range() && + !qemu_close_all_open_fd_proc()) { + qemu_close_all_open_fd_fallback(); + } } --=20 2.43.0 From nobody Sun Nov 24 11:43:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1722817976; cv=none; d=zohomail.com; s=zohoarc; b=NIRGPZv2QAcpDRduR0XZ1Fwqt/dMRji09dA4bMC8F1DiRBID+OlHQc+8z/4SCE1bMHJ6nV8WLDTDZw2+OtETtvoBBdj4DN8N+lXAtL0dtv4BOgU3UcZ2AzIak0wOewpNPvj93XAr2iyuSg96Z7PZ3TaakhVg0c0b6DutWt5WRqA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722817976; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=W8wloA238Rk9C1XCBXsRbgtFn/P+Z4RpbJw5FHD4LSg=; b=El8z9sgd/syR+5lzJ/lPoPk+349yszCiMIYSvPQS/5+yzSENKoPvETirhgLUSg9fUpuIEGiYQ0tA5gkV7/yBYqw6Jm/kYAw1xLiYR84WhPg2nxUJN6x+q5Tr7Ij6aFlFQ3cQm1xNyDKeoOYfti/gP69XfilfYjnKBhTw0gK6BsI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722817976782900.6635564706579; Sun, 4 Aug 2024 17:32:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1saldi-0008Ef-IT; Sun, 04 Aug 2024 20:31:54 -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 1saldg-00086B-FQ for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:52 -0400 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1salde-0002Jp-SH for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:52 -0400 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-70d2b921cd1so8973281b3a.1 for ; Sun, 04 Aug 2024 17:31:50 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817909; x=1723422709; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=W8wloA238Rk9C1XCBXsRbgtFn/P+Z4RpbJw5FHD4LSg=; b=no9g+cF7i2SPDvqzbjzzpwImCOBFaFlePq1zHQ9CMzNCoa9p7SpRW4SPLOtFeCrPqP 40hI1xHC4cageNpmONS7xeIh4woZ39eVALO1BYfMOGdNGvaKOxM0bKjiEGJ5mZh8FSzT xiZYQuyTXvKwX9aeWGe1ND42zwPeBRJPnnkLg2hMMFcntpODWdOSPhpdG+OSKuSIXW34 xiK4HvUE46oUrP+qCnP1z7x6bpZ1mDqLROSJQ2jOJztbg3JPO5qp4Y8r4aBFdamJjqqp 0YC9fUPkK3DpX5EKf5TGC4QEYeSlfs9ahssUzmd4WwIhQFzjc2F6omEQCFv4Zl4sIEQ+ NfVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817909; x=1723422709; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=W8wloA238Rk9C1XCBXsRbgtFn/P+Z4RpbJw5FHD4LSg=; b=Fdin+vjr8L/DyEoZ/pjscXJ7OiFKgR85zFbOgB8djfmor8H6TDDIwnCjHTG2oCEnfB YTOx7FH8SnKfiAkAIrUZ2g1xm/SDo7L38y5oJptSQSN+UPhxhUh9gHb4pVntJYsLVbr6 e9SaT3sZTVdFQ+lQThDccdtSBtSTzxM+Rb3l6Rabxc7Dfw10ctpO6P5z+YAdc7fvK3F9 aipLinyLqO2dRtzQeojjJhG2CRqKTsPD+vmDaKpACnidUrvF7OxiZ+W6NOom9gXULTDe g7oaIW6p12xmvW29CTQN7WLLOw6cevURV2aSJpejedAHPz9Ly/DI3FWenQTHB8wvnJKH 3f0g== X-Gm-Message-State: AOJu0Yz/WPsMIkHlAHf0rlU4BIn+NpfK8qzCG7YLAgzWuaB2E8G9vda5 af61hgFcxbBy4kH4/TGT/V35+94oATTKPYeIL6dAGaw+Mkbu2edVrOMmsxK6qH/I3BVJBlZY/yX fAhY0nQ== X-Google-Smtp-Source: AGHT+IHeBj42U4EyspU8ERcDuYOk1rg9SnhvJpOreqRiSepaXuyIpLjXYNVnWE6Tj+dOQnwSkArVeg== X-Received: by 2002:a05:6a20:729c:b0:1be:c5d9:5a19 with SMTP id adf61e73a8af0-1c6995c9d4fmr16817280637.24.1722817909352; Sun, 04 Aug 2024 17:31:49 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 4/6] net/tap: Factorize fd closing after forking Date: Mon, 5 Aug 2024 10:31:27 +1000 Message-ID: <20240805003130.1421051-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240805003130.1421051-1-richard.henderson@linaro.org> References: <20240805003130.1421051-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::42f; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1722817978471116600 From: Cl=C3=A9ment L=C3=A9ger The same code is used twice to actually close all open file descriptors after forking. Factorize it in a single place. Signed-off-by: Cl=C3=A9ment L=C3=A9ger Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Richard Henderson Message-ID: <20240802145423.3232974-4-cleger@rivosinc.com> Signed-off-by: Richard Henderson --- net/tap.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/net/tap.c b/net/tap.c index 51f7aec39d..7b2d5d5703 100644 --- a/net/tap.c +++ b/net/tap.c @@ -385,6 +385,17 @@ static TAPState *net_tap_fd_init(NetClientState *peer, return s; } =20 +static void close_all_fds_after_fork(int excluded_fd) +{ + int open_max =3D sysconf(_SC_OPEN_MAX), i; + + for (i =3D 3; i < open_max; i++) { + if (i !=3D excluded_fd) { + close(i); + } + } +} + static void launch_script(const char *setup_script, const char *ifname, int fd, Error **errp) { @@ -400,13 +411,7 @@ static void launch_script(const char *setup_script, co= nst char *ifname, return; } if (pid =3D=3D 0) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; - - for (i =3D 3; i < open_max; i++) { - if (i !=3D fd) { - close(i); - } - } + close_all_fds_after_fork(fd); parg =3D args; *parg++ =3D (char *)setup_script; *parg++ =3D (char *)ifname; @@ -490,17 +495,11 @@ 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; 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); - } - } - + close_all_fds_after_fork(sv[1]); fd_buf =3D g_strdup_printf("%s%d", "--fd=3D", sv[1]); =20 if (strrchr(helper, ' ') || strrchr(helper, '\t')) { --=20 2.43.0 From nobody Sun Nov 24 11:43:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1722817983; cv=none; d=zohomail.com; s=zohoarc; b=RY/RiNlZETtsT36U5LxChgcyhgcBFIkEKJLIFBWpLZY8wvC9/UGUEaQVfKsiV1M2hGfdP4ywWIZFo+t9lp+TcVo8akhMiINFNLxXCCIA4DG/jCVYKcRYv78Bnqv7FIScoU+Ov0Pv+G5q1uuuhX902z8ZscdeA+JAx6ejFwxaUec= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722817983; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=9loKzQW0fz51/k/WMOHKeqW5+iqLnIhl+VnJpSdO5ZI=; b=PyEovzeWfI2XiKZddjohkQ6j2a7Be3iMOdhqlA2VY6Pi/RgXSjRI+fvWW06wVcwdALDIVYD4OeeJd7a4bt92RTVvrpAheTMRgy+5qsfpQD3hSkVhF3rItEyr5ALXs3szvjJ17dpVDnRhTEN8sak9krr+fOpQVv2nb+t8oJ0nZEA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722817983902310.396078149627; Sun, 4 Aug 2024 17:33:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1saldk-0008Kp-44; Sun, 04 Aug 2024 20:31: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 1saldi-0008GK-Vs for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:55 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldh-0002K0-74 for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:54 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-70eb73a9f14so7496153b3a.2 for ; Sun, 04 Aug 2024 17:31:52 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817912; x=1723422712; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9loKzQW0fz51/k/WMOHKeqW5+iqLnIhl+VnJpSdO5ZI=; b=z8JohOe5JP8EdqkOEMIzg7roJLJ9w0DnhGtYH8RzmrDAWxQvQ4k1XqMLV4P200wwa+ S6DyzEpIMYkiJHO4q9+EAUbdyo7bHnQZx2dlYZHArmmGI20cvOy5TJOdeAz7A0j71XAy JRsxlT6YiXG4iyw6wC8dUKoBbecSUdR2LplJ43k6Ov96TGhi2dBSvA7liIt0DECxi6HC aVLGAJG2HS9HCz8q59h/76IO6DPGFqiqtTNAyX+Fp+iQhZCetuDygh7x1NaKpn3TDmt/ dBFLDyd2cwpYnkf59vP4f0/5M+lYHL614CaagUD0z3SJukXW6X99nnjvYs+cJbk06NnQ WCFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817912; x=1723422712; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9loKzQW0fz51/k/WMOHKeqW5+iqLnIhl+VnJpSdO5ZI=; b=nXUNWaoQNbu1N8j3dOBOoZ+uOBXvSQemn1M3bgNXWD6ovOqlSVyw1mDHxfL3tOcag9 c9/0SpcgtgQQvCBab7TUMRLxU3PIq27gjOyfeSgRcQN1sppipaFZjjKNvyaajdBlXC83 0XURflMuq/6b4iIHz7inxub4m2qrQMpkWmoA75jOMTKNGmpcbLDf9aC00fpiulCcDOyh PYu1Yi2VJPpLjSf0kr+uQf3fv4R2x6uggyB7TZhPge7GPE5Vw5grlhlD3LDycM/HKCc3 8RhxLrozBwnhTbNAdaEFS99rroH4aqxkt/RO1nNTtaWLVY17sc9Q5rzfBjqiiuBbY+Pu Ad3w== X-Gm-Message-State: AOJu0Yyehg4RrOKq5QcReITwmAuj/ymPgY2LvAKu5PW0RDQ9qh+BX8x/ l2coECoFi3G9xWp0nNgrbpQHnhvnlN+rUbRVACy+wSSCEiG6tED7cJBnQP80ls9HT7V7G6jask6 gnIRLSA== X-Google-Smtp-Source: AGHT+IEvdMNgPGsehcKqxCVF7owcv9eNvXWwTqHmDDWT5K9tnLJhGkLLkBmAhXEYjFBIwLm4jyAdXw== X-Received: by 2002:a05:6a00:22d6:b0:710:591e:b52f with SMTP id d2e1a72fcca58-7106cf933f9mr13521120b3a.5.1722817911689; Sun, 04 Aug 2024 17:31:51 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= Subject: [PULL 5/6] qemu/osdep: Add excluded fd parameter to qemu_close_all_open_fd() Date: Mon, 5 Aug 2024 10:31:28 +1000 Message-ID: <20240805003130.1421051-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240805003130.1421051-1-richard.henderson@linaro.org> References: <20240805003130.1421051-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::431; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x431.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1722817984500116600 From: Cl=C3=A9ment L=C3=A9ger In order for this function to be usable by tap.c code, add a list of file descriptors that should not be closed. Signed-off-by: Cl=C3=A9ment L=C3=A9ger Message-ID: <20240802145423.3232974-5-cleger@rivosinc.com> [rth: Use max_fd in qemu_close_all_open_fd_close_range] Signed-off-by: Richard Henderson --- include/qemu/osdep.h | 8 +++- system/async-teardown.c | 2 +- util/oslib-posix.c | 98 ++++++++++++++++++++++++++++++++++------- 3 files changed, 89 insertions(+), 19 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index de77c5c254..4cc4c32b14 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -760,9 +760,13 @@ int qemu_fdatasync(int fd); /** * qemu_close_all_open_fd: * - * Close all open file descriptors + * Close all open file descriptors except the ones supplied in the @skip a= rray + * + * @skip: ordered array of distinct file descriptors that should not be cl= osed + * if any, or NULL. + * @nskip: number of entries in the @skip array or 0 if @skip is NULL. */ -void qemu_close_all_open_fd(void); +void qemu_close_all_open_fd(const int *skip, unsigned int nskip); =20 /** * Sync changes made to the memory mapped file back to the backing diff --git a/system/async-teardown.c b/system/async-teardown.c index edf49e1007..9148ee8d04 100644 --- a/system/async-teardown.c +++ b/system/async-teardown.c @@ -52,7 +52,7 @@ 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. */ - qemu_close_all_open_fd(); + qemu_close_all_open_fd(NULL, 0); =20 /* Set up a handler for SIGHUP and unblock SIGHUP. */ sigaction(SIGHUP, &sa, NULL); diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 9b79fc7cff..11b35e48fb 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -808,11 +808,12 @@ int qemu_msync(void *addr, size_t length, int fd) return msync(addr, length, MS_SYNC); } =20 -static bool qemu_close_all_open_fd_proc(void) +static bool qemu_close_all_open_fd_proc(const int *skip, unsigned int nski= p) { struct dirent *de; int fd, dfd; DIR *dir; + unsigned int skip_start =3D 0, skip_end =3D nskip; =20 dir =3D opendir("/proc/self/fd"); if (!dir) { @@ -823,8 +824,33 @@ static bool qemu_close_all_open_fd_proc(void) dfd =3D dirfd(dir); =20 for (de =3D readdir(dir); de; de =3D readdir(dir)) { + bool close_fd =3D true; + + if (de->d_name[0] =3D=3D '.') { + continue; + } fd =3D atoi(de->d_name); - if (fd !=3D dfd) { + if (fd =3D=3D dfd) { + continue; + } + + for (unsigned int i =3D skip_start; i < skip_end; i++) { + if (fd < skip[i]) { + /* We are below the next skipped fd, break */ + break; + } else if (fd =3D=3D skip[i]) { + close_fd =3D false; + /* Restrict the range as we found fds matching start/end */ + if (i =3D=3D skip_start) { + skip_start++; + } else if (i =3D=3D skip_end) { + skip_end--; + } + break; + } + } + + if (close_fd) { close(fd); } } @@ -833,24 +859,60 @@ static bool qemu_close_all_open_fd_proc(void) return true; } =20 -static bool qemu_close_all_open_fd_close_range(void) +static bool qemu_close_all_open_fd_close_range(const int *skip, + unsigned int nskip, + int open_max) { #ifdef CONFIG_CLOSE_RANGE - int r =3D close_range(0, ~0U, 0); - if (!r) { - /* Success, no need to try other ways. */ - return true; - } -#endif + int max_fd =3D open_max - 1; + int first =3D 0, last; + unsigned int cur_skip =3D 0; + int ret; + + do { + /* Find the start boundary of the range to close */ + while (cur_skip < nskip && first =3D=3D skip[cur_skip]) { + cur_skip++; + first++; + } + + /* Find the upper boundary of the range to close */ + last =3D max_fd; + if (cur_skip < nskip) { + last =3D skip[cur_skip] - 1; + last =3D MIN(last, max_fd); + } + + /* With the adjustments to the range, we might be done. */ + if (first > last) { + break; + } + + ret =3D close_range(first, last, 0); + if (ret < 0) { + return false; + } + + first =3D last + 1; + } while (last < max_fd); + + return true; +#else return false; +#endif } =20 -static void qemu_close_all_open_fd_fallback(void) +static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int = nskip, + int open_max) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; + unsigned int cur_skip =3D 0; =20 /* Fallback */ - for (i =3D 0; i < open_max; i++) { + for (int i =3D 0; i < open_max; i++) { + if (cur_skip < nskip && i =3D=3D skip[cur_skip]) { + cur_skip++; + continue; + } close(i); } } @@ -858,10 +920,14 @@ static void qemu_close_all_open_fd_fallback(void) /* * Close all open file descriptors. */ -void qemu_close_all_open_fd(void) +void qemu_close_all_open_fd(const int *skip, unsigned int nskip) { - if (!qemu_close_all_open_fd_close_range() && - !qemu_close_all_open_fd_proc()) { - qemu_close_all_open_fd_fallback(); + int open_max =3D sysconf(_SC_OPEN_MAX); + + assert(skip !=3D NULL || nskip =3D=3D 0); + + if (!qemu_close_all_open_fd_close_range(skip, nskip, open_max) && + !qemu_close_all_open_fd_proc(skip, nskip)) { + qemu_close_all_open_fd_fallback(skip, nskip, open_max); } } --=20 2.43.0 From nobody Sun Nov 24 11:43:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1722817961; cv=none; d=zohomail.com; s=zohoarc; b=nyEci+ReVsz2Gz1HxEM7HTMmrGWq264IyaiA7YYZi43uHyAbCC9CdZDDiObwX4I9+jHqDgW7aVx/BESGT+6A9gnK3S8HRJ/u0VgjI2Asj94WjuEf7J+Lqoq16Ry9/B9E1xZk7ebiw5C53XtrWfjLhNUcicn8PYInPWM7Q+PtJew= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722817961; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zCe4GkwH/iAnpmcpXgqjGYu6VSZ8QZlYv/hHC8b9IYI=; b=isF13AJEWx0YNqdzDKqRPrTJN6lnbfEApE7pIpKR/REj9ePx33sR4ypioB828uefAIEEzhArQooT2gb9GWhIaEzvVG5+OuCGG381bjEf7p9cGQiy6xMITs76eF5xZ2c+juUL3g8OtnZfEFbHS4Yoi/hf9rX2ak3/S3mj85SyDiI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722817961178925.7816971715763; Sun, 4 Aug 2024 17:32:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1saldn-00006d-MW; Sun, 04 Aug 2024 20:31:59 -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 1saldl-0008PV-8c for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:57 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldj-0002KC-LK for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:57 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-70eaf5874ddso7494603b3a.3 for ; Sun, 04 Aug 2024 17:31:55 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817914; x=1723422714; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zCe4GkwH/iAnpmcpXgqjGYu6VSZ8QZlYv/hHC8b9IYI=; b=rdkeGhj16oa8Y5B8j6NgH7jUWTJkb1fHjsRMQFoVhbrYuMCJcGGe+4+/FTsk/ipgWb 5PZ8kCs/7w6tBt7phOj8g9buewNhOE9+/J89nSEFteJBqcpl7uwI55ANK+yTcjvODb/8 WjvOvg91F5a9Y/nvMYd21I3IvVeO6R3NSN7SZ2X7deG/D71GdDUsgnqn6+uR3dCBhwMi mLsQWtHpb7dsbiKTLFNoIEu3dCSD8U9wAZ4Wh5s57lHPW+DovOZBE8QmVWHGOpTjOOkk 32NtHvbFkKv8QPSh7wIIICFYtMmQs+7rJb9poU3pgXk1Q8pGmHnQ1mvoEYAiH8AOx5Mz 5kMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817914; x=1723422714; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zCe4GkwH/iAnpmcpXgqjGYu6VSZ8QZlYv/hHC8b9IYI=; b=r+gSc7Qdm3BkhDQ/A0awXJPRGXtuXlLe8EyYzeUam22kOfp2gYsPisUzl1ZtFoL6AC maYPXoyCmxKDSCez2pR5yytyLG3O09kEqy/I3XJGtOAmORpT9fb1Gjajm6jLkzBVbuNG +bJWrGnMAi/SSbcmn54wOwDa+0hqpqcf+Cur8Snr+6Z992HmR72knKYoYboMlvnQMyDk jJKIXSMcxAk1bxgYYPRDHWEuZyko/tPd/cfXR5mQidLYArr2KqnikRL4Ae1eLBYifCC7 jQr+34c+HJyfTIwVLaQPta/bs9Lqj0isr0XZOd/YbXsHrlevxnzP1MVQWUWsTIGX1EXH IseQ== X-Gm-Message-State: AOJu0YziB3XwGQibUombyQ6LMUIuI1F3F02gaZmZakdce6JqTYCCtc3m nnAIQxce50jWwOMzUgAd1m0arN3C9rhU7sekHtwc7/nBmmsqGJGC18E1A0YDGICFIBdGW0FmO0U UYHaLAg== X-Google-Smtp-Source: AGHT+IGI8rWmmxTFk6Jo3jU6wPokUjmP1UWm5VMT4a4ZHL/LMt90XTZXeuWrVzcA3TcD+o8TqJV/Dw== X-Received: by 2002:a05:6a20:8416:b0:1c4:b927:1ffb with SMTP id adf61e73a8af0-1c69953db33mr9534159637.17.1722817914049; Sun, 04 Aug 2024 17:31:54 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= Subject: [PULL 6/6] net/tap: Use qemu_close_all_open_fd() Date: Mon, 5 Aug 2024 10:31:29 +1000 Message-ID: <20240805003130.1421051-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240805003130.1421051-1-richard.henderson@linaro.org> References: <20240805003130.1421051-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::42d; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1722817963352116600 From: Cl=C3=A9ment L=C3=A9ger Instead of using a slow implementation to close all open fd after forking, use qemu_close_all_open_fd(). Signed-off-by: Cl=C3=A9ment L=C3=A9ger Reviewed-by: Richard Henderson Message-ID: <20240802145423.3232974-6-cleger@rivosinc.com> Signed-off-by: Richard Henderson --- net/tap.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/net/tap.c b/net/tap.c index 7b2d5d5703..3f90022c0b 100644 --- a/net/tap.c +++ b/net/tap.c @@ -387,13 +387,20 @@ static TAPState *net_tap_fd_init(NetClientState *peer, =20 static void close_all_fds_after_fork(int excluded_fd) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; + const int skip_fd[] =3D {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, + excluded_fd}; + unsigned int nskip =3D ARRAY_SIZE(skip_fd); =20 - for (i =3D 3; i < open_max; i++) { - if (i !=3D excluded_fd) { - close(i); - } + /* + * skip_fd must be an ordered array of distinct fds, exclude + * excluded_fd if already included in the [STDIN_FILENO - STDERR_FILEN= O] + * range + */ + if (excluded_fd <=3D STDERR_FILENO) { + nskip--; } + + qemu_close_all_open_fd(skip_fd, nskip); } =20 static void launch_script(const char *setup_script, const char *ifname, --=20 2.43.0 From nobody Sun Nov 24 11:43:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1722817952; cv=none; d=zohomail.com; s=zohoarc; b=YN0A43F8KyxzcljmAKjoEITFAE4HJ0wFuhuMDnGU2X4sAAQ23nAoHZ2vMxp1Er2EFt/Ma724CwRrwhsuMq4bEkzNgq7gtuKLVO+W5PmZFACqso4+BUHWuPM1fKq4KRIwE2rB3tNwH0SEIQKAeIr+xpbOLMd9kLEmxWbrJMi4HBg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722817952; h=Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=2FAvv3yZjIy3oYnoHY2EdlCQMFjiKoZMDRTSrJ4FWdU=; b=CRFGEp3WFuCa5L7KIQtkLAA/ZERHB+l093kknTFkglK3zEAmRu9oqwwrVSjwueMQms6G721JRSq7eo+bVUuQzumGph+4b+7I42iMoft8EpB73TizHjjM9om+0h2bArGlhokERXq6Oqv+vK9SylqxFFLEIL/X6ksfkWTuRsHZnp0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722817952643946.9565212647431; Sun, 4 Aug 2024 17:32:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1saldZ-0007fY-Rw; Sun, 04 Aug 2024 20:31:45 -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 1saldY-0007bh-Sm for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:44 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldX-0002Iw-1q for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:44 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-70eae5896bcso8560951b3a.2 for ; Sun, 04 Aug 2024 17:31:42 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817902; x=1723422702; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=2FAvv3yZjIy3oYnoHY2EdlCQMFjiKoZMDRTSrJ4FWdU=; b=DGw77imu05A8s3Wm9ChBRvPP2bHqXr9CRSXCth994kMQ6p4uuyhnOALg4HgcBTgBe8 wYf8ej5gjLQ8pfA7js+SDHnkDxaeNVjiNOIFOKmolqbYzIr0N87JVnB/t7PH7hJUFUvh 7sc+xsQc0w3ssfyVuGqR/ckDcnDZzytnmk/dR+2DAzVDii+stZyLsXFM7xQvaWBc7ZVU QHgZ6iCvH7ikhUNrn2ilOyhBKa4O5p7MszRFm0HWuQHPcXhmW76Tc3OIne1A0KyHqp06 QUbszlhOB+nBeydCW5KBFkY1sfuZoKtrmuIHauVLA3yB6WlV9g89n+ZQEIeNrglL2RNR U0lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817902; x=1723422702; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2FAvv3yZjIy3oYnoHY2EdlCQMFjiKoZMDRTSrJ4FWdU=; b=oioJ6C3VZo6iWLkNEDNMJ625EOnaxpoyAmBElnm4RT7vzoNyMwj9CXHlY98WNxLY+/ HlAtLKaAZ5IJp6I72kVMUJcM6XGyvkSDnfFPTxGhIr0yltsDRaRowuFDysmElTcxu4pT XHN9CuAOB8eeZOIm2ul3YHnl6C74swy/54evTjYYOjfIg2MhmxEBzGeA81W9/dJqPlbt bE77cOLIjjcQ8wmPWO9ci2FAiCzDNopsZykf6C9s9Qv0XhIqEsn8Qv1x/ckaAfQvH6gw UbwPI9iyl4ZQVcQ0p6M8suX0P2X4E/8PUnJr+Ta6A299lvIhXX2SBqvOgzbR5kzDa6zE UoBQ== X-Gm-Message-State: AOJu0Yz672P3R8TBkR58JIVS+DQaU8cSlMvn6qOhrnP7nLqz/eoER3sZ EhOJlcIz39j54m2xR+RrIEzd8RX7E0nAfI4v94EUSBjcppjnGgQ7E0L50aAtJwfr7RViXCH+9Vk PKkm+yw== X-Google-Smtp-Source: AGHT+IFaTP8oCUyXymoIl3zTm62gsDOW4sU49if+syXviCtCfc9ZQ/LZi+pDCHnf+Uw4SQP9eQ7Sgw== X-Received: by 2002:a05:6a00:1407:b0:70d:3a45:5b44 with SMTP id d2e1a72fcca58-7106cfccce4mr13703097b3a.14.1722817901440; Sun, 04 Aug 2024 17:31:41 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH for-9.1] target/i386: Fix VSIB decode Date: Mon, 5 Aug 2024 10:31:24 +1000 Message-ID: <20240805003130.1421051-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240805003130.1421051-1-richard.henderson@linaro.org> References: <20240805003130.1421051-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::431; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x431.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1722817955534116600 Content-Type: text/plain; charset="utf-8" With normal SIB, index =3D=3D 4 indicates no index. With VSIB, there is no exception for VR4/VR12. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2474 Signed-off-by: Richard Henderson --- target/i386/tcg/translate.c | 20 ++++++++++---------- target/i386/tcg/decode-new.c.inc | 3 ++- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c index fb0d01b356..98f5fe61ed 100644 --- a/target/i386/tcg/translate.c +++ b/target/i386/tcg/translate.c @@ -1534,7 +1534,7 @@ typedef struct AddressParts { } AddressParts; =20 static AddressParts gen_lea_modrm_0(CPUX86State *env, DisasContext *s, - int modrm) + int modrm, bool is_vsib) { int def_seg, base, index, scale, mod, rm; target_long disp; @@ -1563,7 +1563,7 @@ static AddressParts gen_lea_modrm_0(CPUX86State *env,= DisasContext *s, int code =3D x86_ldub_code(env, s); scale =3D (code >> 6) & 3; index =3D ((code >> 3) & 7) | REX_X(s); - if (index =3D=3D 4) { + if (index =3D=3D 4 && !is_vsib) { index =3D -1; /* no index */ } base =3D (code & 7) | REX_B(s); @@ -1693,21 +1693,21 @@ static TCGv gen_lea_modrm_1(DisasContext *s, Addres= sParts a, bool is_vsib) =20 static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm) { - AddressParts a =3D gen_lea_modrm_0(env, s, modrm); + AddressParts a =3D gen_lea_modrm_0(env, s, modrm, false); TCGv ea =3D gen_lea_modrm_1(s, a, false); gen_lea_v_seg(s, ea, a.def_seg, s->override); } =20 static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm) { - (void)gen_lea_modrm_0(env, s, modrm); + (void)gen_lea_modrm_0(env, s, modrm, false); } =20 /* Used for BNDCL, BNDCU, BNDCN. */ static void gen_bndck(CPUX86State *env, DisasContext *s, int modrm, TCGCond cond, TCGv_i64 bndv) { - AddressParts a =3D gen_lea_modrm_0(env, s, modrm); + AddressParts a =3D gen_lea_modrm_0(env, s, modrm, false); TCGv ea =3D gen_lea_modrm_1(s, a, false); =20 tcg_gen_extu_tl_i64(s->tmp1_i64, ea); @@ -2428,7 +2428,7 @@ static bool disas_insn_x87(DisasContext *s, CPUState = *cpu, int b) op =3D ((b & 7) << 3) | ((modrm >> 3) & 7); if (mod !=3D 3) { /* memory op */ - AddressParts a =3D gen_lea_modrm_0(env, s, modrm); + AddressParts a =3D gen_lea_modrm_0(env, s, modrm, false); TCGv ea =3D gen_lea_modrm_1(s, a, false); TCGv last_addr =3D tcg_temp_new(); bool update_fdp =3D true; @@ -3089,7 +3089,7 @@ static void disas_insn_old(DisasContext *s, CPUState = *cpu, int b) rm =3D (modrm & 7) | REX_B(s); gen_op_mov_v_reg(s, MO_32, s->T1, reg); if (mod !=3D 3) { - AddressParts a =3D gen_lea_modrm_0(env, s, modrm); + AddressParts a =3D gen_lea_modrm_0(env, s, modrm, false); /* specific case: we need to add a displacement */ gen_exts(ot, s->T1); tcg_gen_sari_tl(s->tmp0, s->T1, 3 + ot); @@ -3646,7 +3646,7 @@ static void disas_insn_old(DisasContext *s, CPUState = *cpu, int b) } } else if (mod !=3D 3) { /* bndldx */ - AddressParts a =3D gen_lea_modrm_0(env, s, modrm); + AddressParts a =3D gen_lea_modrm_0(env, s, modrm, false); if (reg >=3D 4 || (prefixes & PREFIX_LOCK) || s->aflag =3D=3D MO_16 @@ -3690,7 +3690,7 @@ static void disas_insn_old(DisasContext *s, CPUState = *cpu, int b) || s->aflag =3D=3D MO_16) { goto illegal_op; } - AddressParts a =3D gen_lea_modrm_0(env, s, modrm); + AddressParts a =3D gen_lea_modrm_0(env, s, modrm, false); if (a.base >=3D 0) { tcg_gen_extu_tl_i64(cpu_bndl[reg], cpu_regs[a.base]); if (!CODE64(s)) { @@ -3751,7 +3751,7 @@ static void disas_insn_old(DisasContext *s, CPUState = *cpu, int b) } } else if (mod !=3D 3) { /* bndstx */ - AddressParts a =3D gen_lea_modrm_0(env, s, modrm); + AddressParts a =3D gen_lea_modrm_0(env, s, modrm, false); if (reg >=3D 4 || (prefixes & PREFIX_LOCK) || s->aflag =3D=3D MO_16 diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.= c.inc index d2da1d396d..b22210f45d 100644 --- a/target/i386/tcg/decode-new.c.inc +++ b/target/i386/tcg/decode-new.c.inc @@ -1811,7 +1811,8 @@ static int decode_modrm(DisasContext *s, CPUX86State = *env, X86DecodedInsn *decod } else { op->has_ea =3D true; op->n =3D -1; - decode->mem =3D gen_lea_modrm_0(env, s, get_modrm(s, env)); + decode->mem =3D gen_lea_modrm_0(env, s, modrm, + decode->e.vex_class =3D=3D 12); } return modrm; } --=20 2.43.0