From nobody Wed Mar 5 02:06:08 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=1741102039; cv=none; d=zohomail.com; s=zohoarc; b=m9KwnkLmTmJhurTQGUp7J/GPzXQR53n8Zgs5ASRHUSpK7mxDdKMmYwaTgvNNfnKnTdbJIciWugb+TkP1GxaM6+h0vBpqSVs0CmkFqyrI/08ySlVTKw3M8Ezdh0g5pBWE7MxX5lzthQspUxy5KcM7Doujj8AkEG+aUzX/jip3MoE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741102039; 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=620FshvAVPKcENtDD1iG6QmqaFxggLab8Oiyh7VhCtk=; b=GbmBRKeFcznLGZXVZW+VoKZ9PGlEFT5WaLdFrFKfykltGisTZJy90e8EDsplxBgNUEO8VCQdpLIpDTVb6GmKf5OlGJuaMgMICuSWhg6dfyIYRszzV4GUh16YtfZKoAtP7/i3tguyLn/Bc1WcQkyEXnyE8U19cGXFPYQtue+lYvI= 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 1741102039498944.2181171145163; Tue, 4 Mar 2025 07:27:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tpU9W-0001qS-Gr; Tue, 04 Mar 2025 10:25:52 -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 <3429da65ff753b47654b7ae26607417c571a7cb1@kylie.crudebyte.com>) id 1tpU9M-0001q2-16; Tue, 04 Mar 2025 10:25:40 -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 <3429da65ff753b47654b7ae26607417c571a7cb1@kylie.crudebyte.com>) id 1tpU9K-0006tZ-9y; Tue, 04 Mar 2025 10:25:39 -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=620FshvAVPKcENtDD1iG6QmqaFxggLab8Oiyh7VhCtk=; b=ndqw+ KSKSWH6XyN5tjTLjSHwYHHu59UhRVxhq6kNlQA/jB1qqJ2EkgMg+0xhsxNVb30R6t7jmjQraS/PUR KpmrNursAZhim+coYsnlQ3xuuSCK4/HHNH5n12nvQF+HWbA8Hc0AX4yZxMJtYpVuu8ppZqnRNsnCs K0kYFQNQlV9xa0JjWSIgyzz5nT4bTDB7Zn/yihnXkC3qxpHT76JAt7Mp1DDsAd1U2fCCrzWWiP6Qi SoeIyldaUru3QmgAdRxFZdjLHbXoNgqEf5ccSizdoE4n48eH62RipSuKjCBJEwot4DOlsNKJimLn6 WxJgebcmzNUAoOMVgEGWr0BZRSkvc3qqMldSvJCp4u73QkT9uySs4UrVZtGH2NqaAeYLiQIm7/0Ik umbCzdG5++p4pSGMSZ3Wk8TgBWq/xN2aGIDdjMc6+Pq7QKUk/4YmW35c3Ics81YhABhuh1FxA8hEC FTRhMrSWrYx9y2KoaVeud8hfsuSQy7c7vxfgBmM5eGNl78TmE1/lWDvQ99b4ho0fjv8HejBzkWaXr oOnw87LBVnixtOBFC7a8bkLfh4EcTRhBE4sJzJ0YHBLvqJvZKUgyO39Wnuqa6MEGnMsTs7/N+chM5 sJjq1Fe30c2DlpxgA50l8WE/Y4Na1t0XUKQ5rVcugB2rLvtK8LD0t9h0SWxqpI=; Message-Id: <3429da65ff753b47654b7ae26607417c571a7cb1.1741101468.git.qemu_oss@crudebyte.com> In-Reply-To: References: From: Christian Schoenebeck Date: Tue, 4 Mar 2025 16:15:57 +0100 Subject: [PATCH 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=3429da65ff753b47654b7ae26607417c571a7cb1@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: 1741102041950019000 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 --- 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 Mar 5 02:06:08 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=1741102006; cv=none; d=zohomail.com; s=zohoarc; b=H4DrbtbJ46nvQBTq20J+U18OnyXbVUt3f42gkY+iVMsDPyLj9iq4qRYfjGLASfITu5nNn/w8eaILsS5ZxQw+NLwECQfjWgvBOBwQtJOZFeW4gSbPLkaIwEXO0Obeyi8jUW01PjzdY3/PKP78fyEx7WBI96tjroZp9/30i0U3iqQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741102006; 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=qs3hdQFdbF+iWTUVIJhD79rAzTaqIH0Vi558ltmbvFs=; b=UDD67RAs7MwZvaZlv3PjeoWEd/v2nN9pwe706ZjO93adbh4VVJA55ElZRbOuZmGNHXcADlcAQeet720gGMBiua7VsqVxOFM2ZvrM3CHos5WXMAfW+f4LH0S24zHVwyYJJ3tYF/Koze41quYrjLqnsuWuQK6adsfRa4+xmebrUeI= 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 1741102006061568.0992331645832; Tue, 4 Mar 2025 07:26:46 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tpU9k-0001uZ-9t; Tue, 04 Mar 2025 10:26:04 -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 <35b3e1bc74c4f2825acb98252398d2f8805fc99f@kylie.crudebyte.com>) id 1tpU9S-0001rh-FP for qemu-devel@nongnu.org; Tue, 04 Mar 2025 10:25:50 -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 <35b3e1bc74c4f2825acb98252398d2f8805fc99f@kylie.crudebyte.com>) id 1tpU9O-0006ub-MJ for qemu-devel@nongnu.org; Tue, 04 Mar 2025 10:25:43 -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=qs3hdQFdbF+iWTUVIJhD79rAzTaqIH0Vi558ltmbvFs=; b=cMbs7 r64DZioGb9vW1HMoKlftk3HoT4xKpegbLxewRjt6vGzlZqwK9czdjwM94oDfiHdowzBDf7PA5e/wC bWXIkf6XEns/OFV4NzzhNLzxXLptiQKEWSwD6rJ1nYjDlcODJDVBdNokqvWXUZ23IN3AkzzM8DD+r cShWV0xGgUgfK7AeIBDsp8mV20O3FMyQiocjXg04KNdUm2nEw9lYHdJTK7ks4F92BDUwNyAhArHDe UA3LQ8aXrHfquHkvENLGKZ/SFYN/Hh1IGsNUZ6Tb06hzVReC3lRF1FxRFpR9wqoS/vDS6EEEuZgRK +iarH+z0MNAJYC2eXwqkT2kZIwMQzXcp4vrWQ5xfvbA83XxXnZbg+qrSv0eWp72iOVCgRLeN/8vYe HTGmAG2d46YfblMYcW1e91P0DBoi3w1ZQp2JacWjlgubpH+qmnJOFB3DXp2mqJQ5Hsf91b+4y06ix SnGfqhmYPEaDGU9qxT6RVm8tFh2n1lDl3MnqH1TsGylNhnJLDb5zTqYg9ILS9kpYXivEWiNIEO2Iu /S2YvuMEsBeKifAart/qWl1BCBXOXAD0eShLcbU9vS8XJ1A8IkKcfyC4JsPaHK0uAQVmWdfixnKak uDqsT8zV4wW5S3zAIkOYcutc28wzgpuGjTGSYVT3hi40m8HYAPcGpfoyMtl9Aw=; Message-Id: <35b3e1bc74c4f2825acb98252398d2f8805fc99f.1741101468.git.qemu_oss@crudebyte.com> In-Reply-To: References: From: Christian Schoenebeck Date: Tue, 4 Mar 2025 16:16:05 +0100 Subject: [PATCH 2/2] 9pfs: reduce latency of v9fs_reclaim_fd() To: qemu-devel@nongnu.org Cc: Greg Kurz 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=35b3e1bc74c4f2825acb98252398d2f8805fc99f@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: 1741102009805019100 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Christian Schoenebeck --- hw/9pfs/9p.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index 4f9c2dde9c..4b52b1a22a 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -434,6 +434,7 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) V9fsFidState *f; GHashTableIter iter; gpointer fid; + int err; =20 /* prevent multiple coroutines running this function simultaniously */ if (s->reclaiming) { @@ -446,10 +447,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 +500,24 @@ 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--; + } + } + }); + /* 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