From nobody Sun Nov 24 16:58:36 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 ARC-Seal: i=1; a=rsa-sha256; t=1722415804; cv=none; d=zohomail.com; s=zohoarc; b=IRh83szxch0cjinmx6oWH7FyzgSepAAxxEw9JV7wGEX1D7rk087u+nXa5eZdTwVEjpUA0OMWxHMLZkk1HZoYZ3Ix58pry9JGFKCXvEGT+GlsatEQ3l8UzfdGtguMdeOPhAfqEw8vebWZb8ODSi9C+lDRj/HUvHEJJNk/OiylOPQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722415804; 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=UZswZ/5MIFNXWQkpnF7EXAEm1eZvqChAOiAdHEAGgjA=; b=GqeX0pWhg8OkvWrw/c/a4C2QFTWwkK+eurNwm3ynGGe8GvrTOPXD1V0dHEkbJf3Zx8W148hLWk1hIOBqzho/BLDCu76DJUY0L4YIF7A59M8mif6TwNgy1VSxNVu4h5mjDbsv7J41gMrAV+2CHCKalHFWfLxzUjoEy3Tl1W9V9bI= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 172241580487624.17685274786413; Wed, 31 Jul 2024 01:50:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZ50n-0002Vw-Mp; Wed, 31 Jul 2024 04:48: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 1sZ50l-0002Ng-O5 for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:43 -0400 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZ50j-0004c1-Oo for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:43 -0400 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-4281e715904so2823505e9.0 for ; Wed, 31 Jul 2024 01:48:41 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b367c0344sm16576425f8f.2.2024.07.31.01.48.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 01:48:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722415720; x=1723020520; 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=UZswZ/5MIFNXWQkpnF7EXAEm1eZvqChAOiAdHEAGgjA=; b=QVQT6SAmGMakOxVmj9JW3Kg69zF/kqRbgjgSDdIB9pZR8qSUbUjcvBdgmSWPAlqsxu NKSeOo+/ok9CRJFjLc8BlaCd2DjMNuwy5qbwF5YQXy400MitOnVMt4u0ASUc50ZO++rq RN0M2F6yre4gjRZCi9ZRWjkCftjNhJRxDoYMEzJ+oxYchICqr8EHWsBYJOQXl/wFYEUB J5dMOWatoD2WKzDWDX3g6IZH0zJYtSYXonjx+EtehaXHjxwMIizrrpVYhHUTAJp1z2qf wVqhKxIz8ghj3OaV+XcEx74SG2Xh3vBfBbGNOIk4XjO2q4jG5IDQ1a9+34xQ9uzGY423 vDqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722415720; x=1723020520; 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=UZswZ/5MIFNXWQkpnF7EXAEm1eZvqChAOiAdHEAGgjA=; b=WyU+v+CNIZacmBuzicxkaPoc6FVg5dhDrDejsk1grJkV/LqGuY5lUVN31xsnqMjcnc ZMv0MjsKgqZdguw2H8NzhtbSP3tYIQ4b0u5Of8klTOMdoDaTvjQ+5wHH147S1U91dTHJ o4kKVL3eMlf6FzXqKHR+w2N5R3oS7Ga8SwY6ZS9xfZDYq5v/zOWgeEY96K+7aYZ7kDg2 UTzc4chKGsoHPG0ElVcCkVj4Bb1PdBssvpZOi7hfCIj1M1Q+GvxoTCvjgrwgjw1GYAZy ZXQRIZ6RuA8F0iFXNbL1cKUx2rmlL3WfHTD+Dyv1sQmXZdmelnLbxdzcOMSqSSfvXZZD CqwA== X-Gm-Message-State: AOJu0Yx0y34zrbmoNfI57DiyBRuu9Mmbz1JMeuUjU4TONkXmgGUiIk5X eJjYSBoWnTqXKUlQ/BQZrgADfi1AbFgR37pwOCN6YyiKqt2LVkNp12rmo2g7cpd/6Qv3C1jzGaq AytA= X-Google-Smtp-Source: AGHT+IFrNa+Qcp+j0nYQbca6HofxQChaxNE4u33lZFNIqMyMdPu75bwou5Y1FquqlKshGujff9d/fw== X-Received: by 2002:a05:6000:188e:b0:362:4aac:8697 with SMTP id ffacd0b85a97d-36b3497af4emr8144064f8f.0.1722415719751; Wed, 31 Jul 2024 01:48:39 -0700 (PDT) From: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Peter Maydell , Jason Wang , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v7 4/5] qemu/osdep: Add excluded fd parameter to qemu_close_all_open_fd() Date: Wed, 31 Jul 2024 10:48:28 +0200 Message-ID: <20240731084832.1829291-5-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240731084832.1829291-1-cleger@rivosinc.com> References: <20240731084832.1829291-1-cleger@rivosinc.com> 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=2a00:1450:4864:20::333; envelope-from=cleger@rivosinc.com; helo=mail-wm1-x333.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1722415805824116600 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 --- include/qemu/osdep.h | 8 ++- system/async-teardown.c | 2 +- util/oslib-posix.c | 107 ++++++++++++++++++++++++++++++++++------ 3 files changed, 98 insertions(+), 19 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 5cd8517380..0bf6f0a356 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..7583192192 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,34 @@ 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) { + close_fd =3D false; + 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 +860,68 @@ 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 first =3D 0, last =3D open_max; + 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 */ + if (cur_skip < nskip) { + last =3D skip[cur_skip] - 1; + } + /* + * Adjust the maximum fd to close if it's above what the system + * supports + */ + if (last > open_max) { + last =3D open_max; + /* + * We can directly skip the remaining skip fds since the curre= nt + * one is already above the maximum supported one. + */ + cur_skip =3D nskip; + } + /* If last was adjusted, we might be > first, bail out */ + if (first > last) { + break; + } + + ret =3D close_range(first, last, 0); + if (ret < 0) { + return false; + } + first =3D last + 1; + last =3D open_max; + } while (cur_skip < nskip); + + 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 +929,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) - 1; + + 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.45.2