From nobody Wed Apr 2 13:01:35 2025 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=quarantine dis=none) header.from=crudebyte.com ARC-Seal: i=1; a=rsa-sha256; t=1741340069; cv=none; d=zohomail.com; s=zohoarc; b=K2f0fnHFmFiLHf3OLqtQrmOgs726MPF6IZZD4nULFHQW4nYIt0tWzyxBOtrfFeI94rvoido3aFUNHJWXpt5hsSw65+E+ebKLIrgSd3n8iqhO8ZkCMHCnJ7BlJfu5yr43l87ZxF0SAFkt/PlmUSZp6trb4bdX5pzQHgRMForOfxI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741340069; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=HE/Kzh5V43tzdD8T3Art6OIycWgew71hLgw70s1MLto=; b=Mi8Kdg1cG1+B9ypOn0dH1alFKj/wUBXMlT2+07buvIzpcGeER0jOyTUePtEG5tvn1HAdQc4qOtbw938/kN1kzRm6tudwT4ABfV+I++8KWp7Vu6nfGF8t4FfAqKxOctPcWNCIJ2CJ+G9dRbbjf1TJHO6a7xO5HTai0T1P0weZGOA= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1741340069246685.866742939681; Fri, 7 Mar 2025 01:34:29 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tqU5O-0000W1-5o; Fri, 07 Mar 2025 04:33:42 -0500 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 <5c622067efd66dd4ee5eca740dcf263f41db20b2@kylie.crudebyte.com>) id 1tqU5K-0000VD-C2; Fri, 07 Mar 2025 04:33:39 -0500 Received: from kylie.crudebyte.com ([5.189.157.229]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <5c622067efd66dd4ee5eca740dcf263f41db20b2@kylie.crudebyte.com>) id 1tqU5I-0002u5-Nv; Fri, 07 Mar 2025 04:33:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=kylie; h=Cc:To:Subject:Date:From:References:In-Reply-To: Message-Id:Content-Type:Content-Transfer-Encoding:MIME-Version:Content-ID: Content-Description; bh=HE/Kzh5V43tzdD8T3Art6OIycWgew71hLgw70s1MLto=; b=xF65K /aPM4fh7UdW3Fsfg+ryknOJDnym0Z7PmBflcQm1CEYgfeGPLdPoBVaHFdp21kH2Yg9nvK8LPTkyZ7 dclsXbYp/J89XGkiGLIvXCkOHmweDz5REkzqJPdHdBPenAa+mkz8SXacpOpMn2x1DetIQLt/JP+dH OAZHzcOFMEnfCmXPT7DWclN6peVVk0a1wC/+3yHDGGw1d52n9ng3lNriN99irN87Tc0gkKSKgbLmo OzOcmq7XW6yo8wKZ7nPkbDtR8G00fgMaXmfLmhCYoctBBh7qLNxtxfkiuze3oLMQTRK0ILIsrYSeK i9DR3tMIPxsTxg351sZlzCA7iEfb4xQtGFBDqW77hsW0Din7FbMyvNn/Xef/p9JfUG21nT6rd8qtC LPVQHU6SuMBkMwAEM0ME9DL5pXFWkZeJOWhC2OzEgWNE2XnDiNLOMrBUYhprXMIVT8EcBAjgrbzWP xt1kkU9/hbfagpFHqv588F5qSI2y0/d3hFzEX1PNaeHhQXtJcQJyNs+k3hb5pRGPdqoFJl+my2jHp 6mGpE8vcRXPFFszXMibNLo8RLtCPbPd5wRvlYb1ja8wflTFmCPlN7VVu/uUH39iPtNvDiB4a+8JIJ DW17bThB73oGghtwN9gtS9xeV4HTus6sbQYvf8nlkroHiZ9vOU2eWKra2SSEAU=; Message-Id: <5c622067efd66dd4ee5eca740dcf263f41db20b2.1741339452.git.qemu_oss@crudebyte.com> In-Reply-To: References: From: Christian Schoenebeck Date: Fri, 7 Mar 2025 10:22:56 +0100 Subject: [PATCH v2 1/2] 9pfs: fix concurrent v9fs_reclaim_fd() calls To: qemu-devel@nongnu.org Cc: Greg Kurz , qemu-stable@nongnu.org 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=5.189.157.229; envelope-from=5c622067efd66dd4ee5eca740dcf263f41db20b2@kylie.crudebyte.com; helo=kylie.crudebyte.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_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-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 @crudebyte.com) X-ZM-MESSAGEID: 1741340071202019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Even though this function is serialized to be always called from main thread, v9fs_reclaim_fd() is dispatching the coroutine to a worker thread in between via its v9fs_co_*() calls, hence leading to the situation where v9fs_reclaim_fd() is effectively executed multiple times simultaniously, which renders its LRU algorithm useless and causes high latency. Fix this by adding a simple boolean variable to ensure this function is only called once at a time. No synchronization needed for this boolean variable as this function is only entered and returned on main thread. Fixes: 7a46274529c ('hw/9pfs: Add file descriptor reclaim support') Signed-off-by: Christian Schoenebeck Reviewed-by: Greg Kurz --- hw/9pfs/9p.c | 10 ++++++++++ hw/9pfs/9p.h | 1 + 2 files changed, 11 insertions(+) diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index 7cad2bce62..4f9c2dde9c 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -435,6 +435,12 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) GHashTableIter iter; gpointer fid; =20 + /* prevent multiple coroutines running this function simultaniously */ + if (s->reclaiming) { + return; + } + s->reclaiming =3D true; + g_hash_table_iter_init(&iter, s->fids); =20 QSLIST_HEAD(, V9fsFidState) reclaim_list =3D @@ -510,6 +516,8 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) */ put_fid(pdu, f); } + + s->reclaiming =3D false; } =20 /* @@ -4324,6 +4332,8 @@ int v9fs_device_realize_common(V9fsState *s, const V9= fsTransport *t, s->ctx.fst =3D &fse->fst; fsdev_throttle_init(s->ctx.fst); =20 + s->reclaiming =3D false; + rc =3D 0; out: if (rc) { diff --git a/hw/9pfs/9p.h b/hw/9pfs/9p.h index 5e041e1f60..259ad32ed1 100644 --- a/hw/9pfs/9p.h +++ b/hw/9pfs/9p.h @@ -362,6 +362,7 @@ struct V9fsState { uint64_t qp_ndevices; /* Amount of entries in qpd_table. */ uint16_t qp_affix_next; uint64_t qp_fullpath_next; + bool reclaiming; }; =20 /* 9p2000.L open flags */ --=20 2.39.5 From nobody Wed Apr 2 13:01:35 2025 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=quarantine dis=none) header.from=crudebyte.com ARC-Seal: i=1; a=rsa-sha256; t=1741340090; cv=none; d=zohomail.com; s=zohoarc; b=aqtOqNnKTByA9K3PTZMfYCZaUDIpbRXQCwKFGIQIBnajI00ZZc4kniHcvp7oELIwKMNfYmUZz16HWlhlEFBTy1JdIlNf7/x4RCUsBtOTFg4qxIBsKT2Px7FuhNcSX5YDV0azAGlActlEy0zNS4gcvF8VNjCu0wpFuL/jb5b3xaw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741340090; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=XhRQCO3CqaCfCeOHnE5ie3iPCFDdVrdm5SSTjs8R93g=; b=IbWSHy0+7nkZOTVCLsCC2eADody3dEfUZpfFKDNCc69HXDEBgGNfjuoxV0MscOxWWiXZV9Zfuw4I6nzQl0KHT+gB33RKYBU65KTyhyxAkL9ON8whTvsIzLWofNW7Cm7OrEcLXhVIaylOqd82QeLOWFQFP/PmwYMYim4irl/ktuc= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1741340090533813.6503875347627; Fri, 7 Mar 2025 01:34:50 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tqU5Q-0000XG-AW; Fri, 07 Mar 2025 04:33:44 -0500 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 <5747469d3f039c53147e850b456943a1d4b5485c@kylie.crudebyte.com>) id 1tqU5P-0000WW-5a; Fri, 07 Mar 2025 04:33:43 -0500 Received: from kylie.crudebyte.com ([5.189.157.229]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <5747469d3f039c53147e850b456943a1d4b5485c@kylie.crudebyte.com>) id 1tqU5N-0002ug-Fr; Fri, 07 Mar 2025 04:33:42 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=kylie; h=Cc:To:Subject:Date:From:References:In-Reply-To: Message-Id:Content-Type:Content-Transfer-Encoding:MIME-Version:Content-ID: Content-Description; bh=XhRQCO3CqaCfCeOHnE5ie3iPCFDdVrdm5SSTjs8R93g=; b=oxD9f 74hommUzY7Xtok/qZvX05iqYXIZpT/U55QawxkyTzsDBx/4FHzn5qj5/E8bC6UR4+jn6+IhFypPFQ PapG1l4Uni+Hdr0FLviA/X3XVgF79Jz5CRfSpbjsguwzMGkUmZ0YBhJ8hek4Zw018mGXttuNRK78R ZMsIukt6nGMNHhwYX+ZU1hu+mtwKSaDp9tDblTan5K8GnM5VRorsK0E3yQQnqarFz1Aq/3T7jdNT+ zRytVuhf99UwM0tdvVJNwkluSM8n1vZLXfmCTN5ZdmrvWWYb8HLSRTDlbml4tnrR35kheH0tTM+/l kubDlC0s8UVEjkO+cGFLVp2V4jxyfkz4gPV6IgrN1NiyX1VyweLEthFn/A78EW9GCwcMFs11lRJGs iY+mzfc4ZXHZ9yfbrkhB1elEtpEOE5uJ7/iWi2J7XMyE22kiYe5TTAbxXCkmguRMu4bdtYjVbiiQE +dFoy28BSD8j3GFxR/ihCDerr6q6CTReDfzr/4nuDexBYNXLf6J6k4TrTktNoANi9W+gS6aSZifex Iq52a5dexJ65qiNdtceJGteNETF7qJ7XdQdQ5+aDEOfD46PwHH0OnjlvwF5egeTHkeqVk8mgQ1Lhb wjr41pkMw6gGTEhXKNEap2mkT8h497iEB94NaxbHTkvwfV0GXcF+HzdvKh2PUw=; Message-Id: <5747469d3f039c53147e850b456943a1d4b5485c.1741339452.git.qemu_oss@crudebyte.com> In-Reply-To: References: From: Christian Schoenebeck Date: Fri, 7 Mar 2025 10:23:02 +0100 Subject: [PATCH v2 2/2] 9pfs: fix FD leak and reduce latency of v9fs_reclaim_fd() To: qemu-devel@nongnu.org Cc: Greg Kurz , qemu-stable@nongnu.org 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=5.189.157.229; envelope-from=5747469d3f039c53147e850b456943a1d4b5485c@kylie.crudebyte.com; helo=kylie.crudebyte.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_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-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 @crudebyte.com) X-ZM-MESSAGEID: 1741340090905019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch fixes two different bugs in v9fs_reclaim_fd(): 1. Reduce latency: This function calls v9fs_co_close() and v9fs_co_closedir() in a loop. Each one of the calls adds two thread hops (between main thread and a fs driver background thread). Each thread hop adds latency, which sums up in function's loop to a significant duration. Reduce overall latency by open coding what v9fs_co_close() and v9fs_co_closedir() do, executing those and the loop itself altogether in only one background thread block, hence reducing the total amount of thread hops to only two. 2. Fix file descriptor leak: The existing code called v9fs_co_close() and v9fs_co_closedir() to close file descriptors. Both functions check right at the beginning if the 9p request was cancelled: if (v9fs_request_cancelled(pdu)) { return -EINTR; } So if client sent a 'Tflush' message, v9fs_co_close() / v9fs_co_closedir() returned without having closed the file descriptor and v9fs_reclaim_fd() subsequently freed the FID without its file descriptor being closed, hence leaking those file descriptors. This 2nd bug is fixed by this patch as well by open coding v9fs_co_close() and v9fs_co_closedir() inside of v9fs_reclaim_fd() and not performing the v9fs_request_cancelled(pdu) check there. Fixes: 7a46274529c ('hw/9pfs: Add file descriptor reclaim support') Fixes: bccacf6c792 ('hw/9pfs: Implement TFLUSH operation') Signed-off-by: Christian Schoenebeck Reviewed-by: Greg Kurz --- hw/9pfs/9p.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index 4f9c2dde9c..80b190ff5b 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -434,6 +434,8 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) V9fsFidState *f; GHashTableIter iter; gpointer fid; + int err; + int nclosed =3D 0; =20 /* prevent multiple coroutines running this function simultaniously */ if (s->reclaiming) { @@ -446,10 +448,10 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) QSLIST_HEAD(, V9fsFidState) reclaim_list =3D QSLIST_HEAD_INITIALIZER(reclaim_list); =20 + /* Pick FIDs to be closed, collect them on reclaim_list. */ while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &f)) { /* - * Unlink fids cannot be reclaimed. Check - * for them and skip them. Also skip fids + * Unlinked fids cannot be reclaimed, skip those, and also skip fi= ds * currently being operated on. */ if (f->ref || f->flags & FID_NON_RECLAIMABLE) { @@ -499,17 +501,26 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) } } /* - * Now close the fid in reclaim list. Free them if they - * are already clunked. + * Close the picked FIDs altogether on a background I/O driver thread.= Do + * this all at once to keep latency (i.e. amount of thread hops betwee= n main + * thread <-> fs driver background thread) as low as possible. */ + v9fs_co_run_in_worker({ + QSLIST_FOREACH(f, &reclaim_list, reclaim_next) { + err =3D (f->fid_type =3D=3D P9_FID_DIR) ? + s->ops->closedir(&s->ctx, &f->fs_reclaim) : + s->ops->close(&s->ctx, &f->fs_reclaim); + if (!err) { + /* total_open_fd must only be mutated on main thread */ + nclosed++; + } + } + }); + total_open_fd -=3D nclosed; + /* Free the closed FIDs. */ while (!QSLIST_EMPTY(&reclaim_list)) { f =3D QSLIST_FIRST(&reclaim_list); QSLIST_REMOVE(&reclaim_list, f, V9fsFidState, reclaim_next); - if (f->fid_type =3D=3D P9_FID_FILE) { - v9fs_co_close(pdu, &f->fs_reclaim); - } else if (f->fid_type =3D=3D P9_FID_DIR) { - v9fs_co_closedir(pdu, &f->fs_reclaim); - } /* * Now drop the fid reference, free it * if clunked. --=20 2.39.5