From nobody Sat Sep 26 22:01:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8D3437C908; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003307; cv=none; b=BBQ2rLPEq4oFJnKFQM4H4zEJ9KlofYy2TauFTgd5LjjdnBXzKHXy1WAmqTlGJlmaMSJGlCfRtgvzo2OfNgc1nVdhmu9A6lqNLnDoG3liba7ZyT1nbx/WPBpEODZHCTFKeM030MrCrYJryhtEi9GwqlEOWuI3uw42t5RFtxvhn1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003307; c=relaxed/simple; bh=Un15Zkti7N8QBJ3hax4+aNLOrYGzO78aZ94v4qOMqQQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=grnCNiXOBkH8p529W0O2r6tpniSUmo5Sh7asrkorq0Bqri8QuQ/BogP1rygjqfb/y8GPVPYqJzusOnT/rKfCEoYxZXiZu7gy+W2rMRMCjt0gsSnyI0JXizrws+QplDaCfjuYLmzZRhofBPk3IdlLHQg1Cz8NIuORPcUZK3ubQa8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IWKEPt5b; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IWKEPt5b" Received: by smtp.kernel.org (Postfix) with ESMTPS id 4FA16C2BCF5; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1788003307; bh=Un15Zkti7N8QBJ3hax4+aNLOrYGzO78aZ94v4qOMqQQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=IWKEPt5brm8pdMgdDcLg9SIKbq+ThGBT/2W0UzUXK2pEOMJ3hLKDBA3b+84O3qITW EaCp+MLPgydyS1CP9u7gyxkYOoO8blhus6IVZljPtAbu/Jse4sN73LEYoV6L8nIjRa gnO+IqXjRNiJGLq1aWsURiXFMtsuELM+NbgPAAw4OdzmLv5h3E/oAztSlZmhX57eRE 75AZI8w6qbPFIizKrVjybXTciNOAO1LrKS3vk93+rTST+I8P54wUA5tekxgviwd31h jcnvztJWn4wY7nggICUcVGCTOL3TESXQHr3brqrc5XsecvFXpXN00cdOTnaPvt6+vu kDqlW9FUFrh7A== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32722C61DBE; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) From: Xiubo Li via B4 Relay Date: Sat, 29 Aug 2026 04:35:01 -0700 Subject: [PATCH v6 1/5] ceph: use READ_ONCE/WRITE_ONCE for oldest_tid Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260829-ceph-mdsc-mutex-optimization-v6-1-466936ccbd9d@clyso.com> References: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> In-Reply-To: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> To: Ilya Dryomov , Alex Markuze , Viacheslav Dubeyko Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, Xiubo Li X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788003303; l=3350; i=xiubo.li@clyso.com; s=20260625; h=from:subject:message-id; bh=18WejL2UEV4doOiN98828eEMSZB+FE6A4iIzuLLp1Lw=; b=hhmdkgc8tM+DJQQxEVC2W9IIwaIlN3qGI2L6LhJ73PrckNiweFr8y2avztmz6vPKyCTFwrj+6 AENhPH3wRFQCSNhXsgc670WcImha4SILozFYgA3hC3R9CPdIFn06zr1 X-Developer-Key: i=xiubo.li@clyso.com; a=ed25519; pk=V3NGr0AgAopiUhaLY51ipBkLN5LlcLhjOEfLEq1RoZ8= X-Endpoint-Received: by B4 Relay for xiubo.li@clyso.com/20260625 with auth_id=840 X-Original-From: Xiubo Li Reply-To: xiubo.li@clyso.com From: Xiubo Li The oldest_client_tid sent in the MDS request header is advisory: a stale value is harmless --- at worst the MDS may resend a reply the client already has, or trim its completed-requests table slightly earlier or later than optimal, both of which the protocol handles correctly. The field is monotonic and does not need lock serialization to stay correct. Switch all accesses to READ_ONCE() and WRITE_ONCE() to prevent the compiler from tearing or inventing loads, documenting that these lockless accesses are intentional. This removes the last reason the MDS request-send path had to be serialized under mdsc->mutex. Signed-off-by: Xiubo Li --- fs/ceph/mds_client.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index 85f8ceb10377..b84d04835329 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -1309,8 +1309,8 @@ static void __register_request(struct ceph_mds_client= *mdsc, if (!req->r_mnt_idmap) req->r_mnt_idmap =3D &nop_mnt_idmap; =20 - if (mdsc->oldest_tid =3D=3D 0 && req->r_op !=3D CEPH_MDS_OP_SETFILELOCK) - mdsc->oldest_tid =3D req->r_tid; + if (READ_ONCE(mdsc->oldest_tid) =3D=3D 0 && req->r_op !=3D CEPH_MDS_OP_SE= TFILELOCK) + WRITE_ONCE(mdsc->oldest_tid, req->r_tid); =20 if (dir) { struct ceph_inode_info *ci =3D ceph_inode(dir); @@ -1331,14 +1331,14 @@ static void __unregister_request(struct ceph_mds_cl= ient *mdsc, /* Never leave an unregistered request on an unsafe list! */ list_del_init(&req->r_unsafe_item); =20 - if (req->r_tid =3D=3D mdsc->oldest_tid) { + if (req->r_tid =3D=3D READ_ONCE(mdsc->oldest_tid)) { struct rb_node *p =3D rb_next(&req->r_node); - mdsc->oldest_tid =3D 0; + WRITE_ONCE(mdsc->oldest_tid, 0); while (p) { struct ceph_mds_request *next_req =3D rb_entry(p, struct ceph_mds_request, r_node); if (next_req->r_op !=3D CEPH_MDS_OP_SETFILELOCK) { - mdsc->oldest_tid =3D next_req->r_tid; + WRITE_ONCE(mdsc->oldest_tid, next_req->r_tid); break; } p =3D rb_next(p); @@ -1767,7 +1767,7 @@ create_session_full_msg(struct ceph_mds_client *mdsc,= int op, u64 seq) ceph_encode_32(&p, 0); =20 /* version =3D=3D 7, oldest_client_tid */ - ceph_encode_64(&p, mdsc->oldest_tid); + ceph_encode_64(&p, READ_ONCE(mdsc->oldest_tid)); =20 msg->front.iov_len =3D p - msg->front.iov_base; msg->hdr.front_len =3D cpu_to_le32(msg->front.iov_len); @@ -2834,7 +2834,7 @@ static struct ceph_mds_request *__get_oldest_req(stru= ct ceph_mds_client *mdsc) =20 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc) { - return mdsc->oldest_tid; + return READ_ONCE(mdsc->oldest_tid); } =20 #if IS_ENABLED(CONFIG_FS_ENCRYPTION) @@ -3513,9 +3513,6 @@ static void complete_request(struct ceph_mds_client *= mdsc, complete_all(&req->r_completion); } =20 -/* - * called under mdsc->mutex - */ static int __prepare_send_request(struct ceph_mds_session *session, struct ceph_mds_request *req, bool drop_cap_releases) @@ -3630,9 +3627,6 @@ static int __prepare_send_request(struct ceph_mds_ses= sion *session, return 0; } =20 -/* - * called under mdsc->mutex - */ static int __send_request(struct ceph_mds_session *session, struct ceph_mds_request *req, bool drop_cap_releases) --=20 2.53.0 From nobody Sat Sep 26 22:01:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8EA837F8C3; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003307; cv=none; b=BGt4PG8ZjogQQ8GhwA8m0K2C7+4lP40NHDUynzOfHAx5GLWSkCWEke3uz/iflGdk+lXdcUDZRL0lpPh6OYHSSQIWK1eby1uJTIqg4O9bZ9iPJqmWXS+rxpmUDxsw1YsEN+I4oW/xkIdlM6vZ5WS3kNHkLuJQxr7FhhWSWPKtOtY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003307; c=relaxed/simple; bh=7u0omddTAUlZx9s4cBJ3inLZc+Dsm+KFlQloMEQfpEI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fZ8pucMzFt5Bd7a9MqsvwhY7ZyfTOsUydG0R7fSFs7hz8foKs3H1W4ZLnIG2CDcZFJLfxeRmx0Ro1ycLT3RjUmhqVXBwQkc6aEp64FZ1x6JneUfEgWSD3hr9/M2TrxbEwES6z4d9WJwTg06GwjhDgOPyXF/GW4QAPqHt8xEqh1g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HFSRxRci; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HFSRxRci" Received: by smtp.kernel.org (Postfix) with ESMTPS id 603CBC2BCF7; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1788003307; bh=7u0omddTAUlZx9s4cBJ3inLZc+Dsm+KFlQloMEQfpEI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=HFSRxRci5zUUeF3k1bR/TKYtz88q0zYVcnmWZjIJm4N0PsGHcDgB2gKZADstudCVk Yq817gr2t7i+pqDaMo4+aG/ymusYim+FAPpeMPF7dsdNPUoXB8x9JqnGJOVPopfiS/ 2rdeA21d1RfPm3t0oCaEO1RrMrYToGZY7PmyQ6PPyZM8CO02ZKNVmwftwOfTmSdgY/ /KFmEe211jAS/Hqe/s9Eh7C4HsFYVFDkPJrEP1/CIdBwcms/rCLm1bchpSazpCi50b M9uZ9lbhAde3EXwdPJ/tw65em+Qgw8J1lQT16gdJ61j9MzBJqXh9agnLHpgU9kHVfo BZEfnMLVVD7Ng== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43BD9C61DDD; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) From: Xiubo Li via B4 Relay Date: Sat, 29 Aug 2026 04:35:02 -0700 Subject: [PATCH v6 2/5] ceph: replace the request_tree rbtree with an xarray keyed by r_tid Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260829-ceph-mdsc-mutex-optimization-v6-2-466936ccbd9d@clyso.com> References: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> In-Reply-To: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> To: Ilya Dryomov , Alex Markuze , Viacheslav Dubeyko Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, Xiubo Li X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788003303; l=11800; i=xiubo.li@clyso.com; s=20260625; h=from:subject:message-id; bh=c/9avo6RkJVgyP7p607F3UhMViKNwJJKHp6b9vPjvqw=; b=ulpbrodE5/2slL547+ky/p1hkacvYV2Gtbo0+90qK0Iun+uQ71PYBidRIlmnTHsXzKDS2OOzm BDg6Kl71mtbAELdmST4yG9rSPnwAKtUyoMv96IMnX/J6RXOXAmwORMj X-Developer-Key: i=xiubo.li@clyso.com; a=ed25519; pk=V3NGr0AgAopiUhaLY51ipBkLN5LlcLhjOEfLEq1RoZ8= X-Endpoint-Received: by B4 Relay for xiubo.li@clyso.com/20260625 with auth_id=840 X-Original-From: Xiubo Li Reply-To: xiubo.li@clyso.com From: Xiubo Li Replace the red-black tree that indexes pending MDS requests by transaction ID with an xarray. The xarray provides O(1) keyed lookups versus the rbtree's O(log N), and its internal RCU locking eliminates the need for an external mutex during lookups. Iteration is also simpler, with the xarray's native iterators replacing open-coded rb_first() / rb_next() walks. Because xarray indices are unsigned long, a u64 transaction ID would be truncated on 32-bit platforms. Restrict CEPH_FS to 64BIT since there are no 32-bit users. Validate the xarray insertion and clean up the structure at shutdown. Signed-off-by: Xiubo Li --- fs/ceph/Kconfig | 1 + fs/ceph/debugfs.c | 6 +-- fs/ceph/mds_client.c | 113 +++++++++++++++++++++++------------------------= ---- fs/ceph/mds_client.h | 3 +- 4 files changed, 56 insertions(+), 67 deletions(-) diff --git a/fs/ceph/Kconfig b/fs/ceph/Kconfig index be1c8ada5665..a703071a1089 100644 --- a/fs/ceph/Kconfig +++ b/fs/ceph/Kconfig @@ -2,6 +2,7 @@ config CEPH_FS tristate "Ceph distributed file system" depends on INET + depends on 64BIT select CEPH_LIB select NETFS_SUPPORT select FS_ENCRYPTION_ALGS if FS_ENCRYPTION diff --git a/fs/ceph/debugfs.c b/fs/ceph/debugfs.c index 18eb5da03411..491ead3fe1c6 100644 --- a/fs/ceph/debugfs.c +++ b/fs/ceph/debugfs.c @@ -87,12 +87,12 @@ static int mdsc_show(struct seq_file *s, void *p) struct ceph_fs_client *fsc =3D s->private; struct ceph_mds_client *mdsc =3D fsc->mdsc; struct ceph_mds_request *req; - struct rb_node *rp; + unsigned long idx; char *path; =20 mutex_lock(&mdsc->mutex); - for (rp =3D rb_first(&mdsc->request_tree); rp; rp =3D rb_next(rp)) { - req =3D rb_entry(rp, struct ceph_mds_request, r_node); + idx =3D 0; + xa_for_each(&mdsc->request_tree, idx, req) { =20 if (req->r_request && req->r_session) seq_printf(s, "%lld\tmds%d\t", req->r_tid, diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index b84d04835329..fdaf6f56ecd3 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -1257,7 +1257,6 @@ void ceph_mdsc_release_request(struct kref *kref) kmem_cache_free(ceph_mds_request_cachep, req); } =20 -DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node) =20 /* * lookup session, bump ref if found. @@ -1269,7 +1268,7 @@ lookup_get_request(struct ceph_mds_client *mdsc, u64 = tid) { struct ceph_mds_request *req; =20 - req =3D lookup_request(&mdsc->request_tree, tid); + req =3D xa_load(&mdsc->request_tree, tid); if (req) ceph_mdsc_get_request(req); =20 @@ -1303,7 +1302,14 @@ static void __register_request(struct ceph_mds_clien= t *mdsc, } doutc(cl, "%p tid %lld\n", req, req->r_tid); ceph_mdsc_get_request(req); - insert_request(&mdsc->request_tree, req); + if (xa_is_err(xa_store(&mdsc->request_tree, req->r_tid, req, + GFP_NOFS))) { + pr_err_client(cl, "%p tid %lld: xa_store failed\n", + req, req->r_tid); + ceph_mdsc_put_request(req); + req->r_err =3D -ENOMEM; + return; + } =20 req->r_cred =3D get_current_cred(); if (!req->r_mnt_idmap) @@ -1332,20 +1338,19 @@ static void __unregister_request(struct ceph_mds_cl= ient *mdsc, list_del_init(&req->r_unsafe_item); =20 if (req->r_tid =3D=3D READ_ONCE(mdsc->oldest_tid)) { - struct rb_node *p =3D rb_next(&req->r_node); + unsigned long tidx =3D req->r_tid + 1; + struct ceph_mds_request *next_req; + WRITE_ONCE(mdsc->oldest_tid, 0); - while (p) { - struct ceph_mds_request *next_req =3D - rb_entry(p, struct ceph_mds_request, r_node); + xa_for_each_start(&mdsc->request_tree, tidx, next_req, tidx) { if (next_req->r_op !=3D CEPH_MDS_OP_SETFILELOCK) { WRITE_ONCE(mdsc->oldest_tid, next_req->r_tid); break; } - p =3D rb_next(p); } } =20 - erase_request(&mdsc->request_tree, req); + xa_erase(&mdsc->request_tree, req->r_tid); =20 if (req->r_unsafe_dir) { struct ceph_inode_info *ci =3D ceph_inode(req->r_unsafe_dir); @@ -1905,7 +1910,7 @@ static void cleanup_session_requests(struct ceph_mds_= client *mdsc, { struct ceph_client *cl =3D mdsc->fsc->client; struct ceph_mds_request *req; - struct rb_node *p; + unsigned long idx; =20 doutc(cl, "mds%d\n", session->s_mds); mutex_lock(&mdsc->mutex); @@ -1921,10 +1926,8 @@ static void cleanup_session_requests(struct ceph_mds= _client *mdsc, __unregister_request(mdsc, req); } /* zero r_attempts, so kick_requests() will re-send requests */ - p =3D rb_first(&mdsc->request_tree); - while (p) { - req =3D rb_entry(p, struct ceph_mds_request, r_node); - p =3D rb_next(p); + idx =3D 0; + xa_for_each(&mdsc->request_tree, idx, req) { if (req->r_session && req->r_session->s_mds =3D=3D session->s_mds) req->r_attempts =3D 0; @@ -2806,7 +2809,6 @@ ceph_mdsc_create_request(struct ceph_mds_client *mdsc= , int op, int mode) req->r_fmode =3D -1; req->r_feature_needed =3D -1; kref_init(&req->r_kref); - RB_CLEAR_NODE(&req->r_node); INIT_LIST_HEAD(&req->r_wait); init_completion(&req->r_completion); init_completion(&req->r_safe_completion); @@ -2826,10 +2828,9 @@ ceph_mdsc_create_request(struct ceph_mds_client *mds= c, int op, int mode) */ static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *m= dsc) { - if (RB_EMPTY_ROOT(&mdsc->request_tree)) - return NULL; - return rb_entry(rb_first(&mdsc->request_tree), - struct ceph_mds_request, r_node); + unsigned long idx =3D 0; + + return xa_find(&mdsc->request_tree, &idx, ULONG_MAX, XA_PRESENT); } =20 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc) @@ -3890,12 +3891,11 @@ static void kick_requests(struct ceph_mds_client *m= dsc, int mds) { struct ceph_client *cl =3D mdsc->fsc->client; struct ceph_mds_request *req; - struct rb_node *p =3D rb_first(&mdsc->request_tree); + unsigned long idx; =20 doutc(cl, "kick_requests mds%d\n", mds); - while (p) { - req =3D rb_entry(p, struct ceph_mds_request, r_node); - p =3D rb_next(p); + idx =3D 0; + xa_for_each(&mdsc->request_tree, idx, req) { if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) continue; if (req->r_attempts > 0) @@ -4748,7 +4748,7 @@ static void replay_unsafe_requests(struct ceph_mds_cl= ient *mdsc, struct ceph_mds_session *session) { struct ceph_mds_request *req, *nreq; - struct rb_node *p; + unsigned long idx; =20 doutc(mdsc->fsc->client, "mds%d\n", session->s_mds); =20 @@ -4760,10 +4760,8 @@ static void replay_unsafe_requests(struct ceph_mds_c= lient *mdsc, * also re-send old requests when MDS enters reconnect stage. So that MDS * can process completed request in clientreplay stage. */ - p =3D rb_first(&mdsc->request_tree); - while (p) { - req =3D rb_entry(p, struct ceph_mds_request, r_node); - p =3D rb_next(p); + idx =3D 0; + xa_for_each(&mdsc->request_tree, idx, req) { if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) continue; if (req->r_attempts =3D=3D 0) @@ -5609,7 +5607,7 @@ static void ceph_mdsc_reset_workfn(struct work_struct= *work) */ { struct ceph_mds_request *req; - struct rb_node *rn; + unsigned long idx; u64 last_tid; =20 mutex_lock(&mdsc->mutex); @@ -5617,14 +5615,12 @@ static void ceph_mdsc_reset_workfn(struct work_stru= ct *work) mutex_unlock(&mdsc->mutex); =20 mutex_lock(&mdsc->mutex); - rn =3D rb_first(&mdsc->request_tree); - while (rn) { - req =3D rb_entry(rn, struct ceph_mds_request, r_node); - if (req->r_tid > last_tid) - break; + idx =3D 0; + while ((req =3D xa_find(&mdsc->request_tree, &idx, last_tid, + XA_PRESENT))) { if (req->r_op =3D=3D CEPH_MDS_OP_SETFILELOCK || !(req->r_op & CEPH_MDS_OP_WRITE)) { - rn =3D rb_next(rn); + idx++; continue; } ceph_mdsc_get_request(req); @@ -5637,7 +5633,7 @@ static void ceph_mdsc_reset_workfn(struct work_struct= *work) ceph_mdsc_put_request(req); if (time_after(jiffies, drain_deadline)) break; - rn =3D rb_first(&mdsc->request_tree); + idx =3D 0; /* restart: tree may have changed */ } mutex_unlock(&mdsc->mutex); =20 @@ -6369,7 +6365,7 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc) mdsc->snap_realms =3D RB_ROOT; INIT_LIST_HEAD(&mdsc->snap_empty); spin_lock_init(&mdsc->snap_empty_lock); - mdsc->request_tree =3D RB_ROOT; + xa_init(&mdsc->request_tree); INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work); mdsc->last_renew_caps =3D jiffies; INIT_LIST_HEAD(&mdsc->cap_delay_list); @@ -6695,34 +6691,33 @@ static void flush_mdlog_and_wait_mdsc_unsafe_reques= ts(struct ceph_mds_client *md u64 want_tid) { struct ceph_client *cl =3D mdsc->fsc->client; - struct ceph_mds_request *req =3D NULL, *nextreq; + struct ceph_mds_request *req; struct ceph_mds_session *last_session =3D NULL; - struct rb_node *n; + unsigned long idx; =20 mutex_lock(&mdsc->mutex); doutc(cl, "want %lld\n", want_tid); -restart: - req =3D __get_oldest_req(mdsc); - while (req && req->r_tid <=3D want_tid) { - /* find next request */ - n =3D rb_next(&req->r_node); - if (n) - nextreq =3D rb_entry(n, struct ceph_mds_request, r_node); - else - nextreq =3D NULL; - if (req->r_op !=3D CEPH_MDS_OP_SETFILELOCK && - (req->r_op & CEPH_MDS_OP_WRITE)) { + idx =3D 0; + while ((req =3D xa_find(&mdsc->request_tree, &idx, want_tid, + XA_PRESENT))) { + u64 next_tid =3D req->r_tid + 1; + + if (req->r_op =3D=3D CEPH_MDS_OP_SETFILELOCK || + !(req->r_op & CEPH_MDS_OP_WRITE)) { + idx =3D next_tid; + continue; + } + + { struct ceph_mds_session *s =3D req->r_session; =20 if (!s) { - req =3D nextreq; + idx =3D next_tid; continue; } =20 /* write op */ ceph_mdsc_get_request(req); - if (nextreq) - ceph_mdsc_get_request(nextreq); s =3D ceph_get_mds_session(s); mutex_unlock(&mdsc->mutex); =20 @@ -6740,16 +6735,9 @@ static void flush_mdlog_and_wait_mdsc_unsafe_request= s(struct ceph_mds_client *md =20 mutex_lock(&mdsc->mutex); ceph_mdsc_put_request(req); - if (!nextreq) - break; /* next dne before, so we're done! */ - if (RB_EMPTY_NODE(&nextreq->r_node)) { - /* next request was removed from tree */ - ceph_mdsc_put_request(nextreq); - goto restart; - } - ceph_mdsc_put_request(nextreq); /* won't go away */ + /* restart from the next tid; tree may have changed */ + idx =3D next_tid; } - req =3D nextreq; } mutex_unlock(&mdsc->mutex); ceph_put_mds_session(last_session); @@ -6908,6 +6896,7 @@ static void ceph_mdsc_stop(struct ceph_mds_client *md= sc) if (mdsc->mdsmap) ceph_mdsmap_destroy(mdsc->mdsmap); kfree(mdsc->sessions); + xa_destroy(&mdsc->request_tree); ceph_caps_finalize(mdsc); =20 if (mdsc->s_cap_auths) { diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h index e7a262c9c2ab..baba5dcf0def 100644 --- a/fs/ceph/mds_client.h +++ b/fs/ceph/mds_client.h @@ -331,7 +331,6 @@ typedef int (*ceph_mds_request_wait_callback_t) (struct= ceph_mds_client *mdsc, */ struct ceph_mds_request { u64 r_tid; /* transaction id */ - struct rb_node r_node; struct ceph_mds_client *r_mdsc; =20 struct kref r_kref; @@ -536,7 +535,7 @@ struct ceph_mds_client { u64 last_tid; /* most recent mds request */ u64 oldest_tid; /* oldest incomplete mds request, excluding setfilelock requests */ - struct rb_root request_tree; /* pending mds requests */ + struct xarray request_tree; /* pending mds requests */ struct delayed_work delayed_work; /* delayed work */ unsigned long last_renew_caps; /* last time we renewed our caps */ struct list_head cap_delay_list; /* caps with delayed release */ --=20 2.53.0 From nobody Sat Sep 26 22:01:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8CA137C11C; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003307; cv=none; b=PMkPoC47iUfAEpS1D5knPncLi18Pr4XmzOIjXNbhJD9xgyv4lJSWBgjQ8hhkuOx8mj/0IsLVRNX7bGu1Ictzlef/Eyc3RMrOYCyvnPcA0VYi8sKuHLdlV/gaVlbP9r+ZCMIVhalx3JtMMg4YzpK7EIOmRVwWV5VFg1asUXMUmX8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003307; c=relaxed/simple; bh=ReSU1PA4YpUFweZ1hhi1BF7SlkAYjDQIgzPWmrvFytM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=o9DN4hzozfo9cyxgEbjHtOzdmv8Dav674xpERMcIyw5HEWSH4YA4VgQB3zEgfo4fWVio8MCIRk6xy1XEVcru8AMf62ibocZxsVecWGibjn9+rgc0M3s53NWyx2LOJ12o69eB1Ca6OcVBV6aCe3UObLN5ALjHzw6SQ0t4Sr3cxwc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XAeesi7K; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XAeesi7K" Received: by smtp.kernel.org (Postfix) with ESMTPS id 6BE62C2BCFA; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1788003307; bh=ReSU1PA4YpUFweZ1hhi1BF7SlkAYjDQIgzPWmrvFytM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=XAeesi7Kngn84i+kyu2IUhPjX8uZjPIcSIfCQUtUWi/F/o7m8wfRe2WaI/eoeH8JK oesm47cwpfUO5EKy391DE/UiCLBPBla121jj8WsJt/vbKTTQo56zvwlmiucOvz+7XO gC2Dl61BOSrUH1VyXfgaGIcy9kC5VUJJXI26megaoH+UtlyozaWwPRTPn5i+HW7Jo0 Lwx7+u13cieZ6dX/cOxnnmaEs/ZBwEbyUNiNp6fi1JLKacqSvI8GUhcN+0HE0SdwKI NP7KXepPYGHJOiNtH9I6snCJh2nKlr4oJpLg8D4hCN/g/mpNd22bM2UMlkS7OnQhr4 iyopQPq/2kH1A== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53F50C61DDE; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) From: Xiubo Li via B4 Relay Date: Sat, 29 Aug 2026 04:35:03 -0700 Subject: [PATCH v6 3/5] ceph: add wait_list_lock for wait-list serialization Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260829-ceph-mdsc-mutex-optimization-v6-3-466936ccbd9d@clyso.com> References: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> In-Reply-To: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> To: Ilya Dryomov , Alex Markuze , Viacheslav Dubeyko Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, Xiubo Li X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788003303; l=4398; i=xiubo.li@clyso.com; s=20260625; h=from:subject:message-id; bh=iD8fn2jHJiggYB291Y2t7iJ47QONEWyxmBRxD4/dJ2c=; b=ZsxzaqHenLV0qCgGa6XbN8QG1xTemPT20CW48ZzMgMLWlKWGJjmN+96I3fjcF5bS/9jy/Lzs2 CzaQj+xy0YcCqnLU5idsfocPaEUKmgpBl7EIWBTn4uZv/rT4KR5dpVE X-Developer-Key: i=xiubo.li@clyso.com; a=ed25519; pk=V3NGr0AgAopiUhaLY51ipBkLN5LlcLhjOEfLEq1RoZ8= X-Endpoint-Received: by B4 Relay for xiubo.li@clyso.com/20260625 with auth_id=840 X-Original-From: Xiubo Li Reply-To: xiubo.li@clyso.com From: Xiubo Li The per-MDS session wait list and the global waiting-for-map list are currently serialized by mdsc->mutex, even though the list operations themselves don't need the mutex's broader protection. Introduce a dedicated spinlock to guard these lists so that waking and kicking waiters can run outside the mutex. Reviewed-by: Viacheslav Dubeyko Signed-off-by: Xiubo Li --- fs/ceph/mds_client.c | 18 +++++++++++++++++- fs/ceph/mds_client.h | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index fdaf6f56ecd3..5983ae6e3085 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -3689,7 +3689,9 @@ static void __do_request(struct ceph_mds_client *mdsc, doutc(cl, "no mdsmap, waiting for map\n"); trace_ceph_mdsc_suspend_request(mdsc, session, req, ceph_mdsc_suspend_reason_no_mdsmap); + spin_lock(&mdsc->wait_list_lock); list_add(&req->r_wait, &mdsc->waiting_for_map); + spin_unlock(&mdsc->wait_list_lock); return; } if (!(mdsc->fsc->mount_options->flags & @@ -3712,7 +3714,9 @@ static void __do_request(struct ceph_mds_client *mdsc, doutc(cl, "no mds or not active, waiting for map\n"); trace_ceph_mdsc_suspend_request(mdsc, session, req, ceph_mdsc_suspend_reason_no_active_mds); + spin_lock(&mdsc->wait_list_lock); list_add(&req->r_wait, &mdsc->waiting_for_map); + spin_unlock(&mdsc->wait_list_lock); return; } =20 @@ -3760,9 +3764,12 @@ static void __do_request(struct ceph_mds_client *mds= c, if (ceph_test_mount_opt(mdsc->fsc, CLEANRECOVER)) { trace_ceph_mdsc_suspend_request(mdsc, session, req, ceph_mdsc_suspend_reason_rejected); + spin_lock(&mdsc->wait_list_lock); list_add(&req->r_wait, &mdsc->waiting_for_map); - } else + spin_unlock(&mdsc->wait_list_lock); + } else { err =3D -EACCES; + } goto out_session; } =20 @@ -3777,7 +3784,9 @@ static void __do_request(struct ceph_mds_client *mdsc, } trace_ceph_mdsc_suspend_request(mdsc, session, req, ceph_mdsc_suspend_reason_session); + spin_lock(&mdsc->wait_list_lock); list_add(&req->r_wait, &session->s_waiting); + spin_unlock(&mdsc->wait_list_lock); goto out_session; } =20 @@ -3870,7 +3879,9 @@ static void __wake_requests(struct ceph_mds_client *m= dsc, struct ceph_mds_request *req; LIST_HEAD(tmp_list); =20 + spin_lock(&mdsc->wait_list_lock); list_splice_init(head, &tmp_list); + spin_unlock(&mdsc->wait_list_lock); =20 while (!list_empty(&tmp_list)) { req =3D list_entry(tmp_list.next, @@ -3903,7 +3914,9 @@ static void kick_requests(struct ceph_mds_client *mds= c, int mds) if (req->r_session && req->r_session->s_mds =3D=3D mds) { doutc(cl, " kicking tid %llu\n", req->r_tid); + spin_lock(&mdsc->wait_list_lock); list_del_init(&req->r_wait); + spin_unlock(&mdsc->wait_list_lock); trace_ceph_mdsc_resume_request(mdsc, req); __do_request(mdsc, req); } @@ -6365,6 +6378,7 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc) mdsc->snap_realms =3D RB_ROOT; INIT_LIST_HEAD(&mdsc->snap_empty); spin_lock_init(&mdsc->snap_empty_lock); + spin_lock_init(&mdsc->wait_list_lock); xa_init(&mdsc->request_tree); INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work); mdsc->last_renew_caps =3D jiffies; @@ -6447,7 +6461,9 @@ static void wait_requests(struct ceph_mds_client *mds= c) mutex_lock(&mdsc->mutex); while ((req =3D __get_oldest_req(mdsc))) { doutc(cl, "timed out on tid %llu\n", req->r_tid); + spin_lock(&mdsc->wait_list_lock); list_del_init(&req->r_wait); + spin_unlock(&mdsc->wait_list_lock); __unregister_request(mdsc, req); } } diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h index baba5dcf0def..0ea144ba4c7d 100644 --- a/fs/ceph/mds_client.h +++ b/fs/ceph/mds_client.h @@ -531,6 +531,9 @@ struct ceph_mds_client { struct list_head snap_empty; int num_snap_realms; spinlock_t snap_empty_lock; /* protect snap_empty */ + spinlock_t wait_list_lock; /* protect waiting_for_map + * and s_waiting lists + */ =20 u64 last_tid; /* most recent mds request */ u64 oldest_tid; /* oldest incomplete mds request, --=20 2.53.0 From nobody Sat Sep 26 22:01:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF98D37F8D3; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003307; cv=none; b=GV136g+QI75sUwHXmx9jxq3Cip896P/gCXG1eAHHPNpsDw7PYjfANZEw6XL54k2ugZqKFU/TJcOkD5lXpIGbAIOOrgsGwyOcuZYsyljxiJ1wV9g8ixVNCJmaWrxkfOpBreYHsfIeCNGWeFlWdUPti+0+5fCvz54i2pNgJBVacpo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003307; c=relaxed/simple; bh=0YWhM8Nmzim2CVzBQWb9SCmJsQ0PLEMgxqsU5XJgVIA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Nu+2k24bwISCC1yld1OBt1q6ZSJ88YfkstoD/jdVG2P3Ltd7uSeocrye+A+HCRNK9eL94SKt8AGRzPBEE+dQnRtCNU1Y65xzECOg0LE1vtpEKMw1OPj1TB1pMhXV+9HIVOFyZK5Oj1MlU8fVu9PGT9CU+Gpqa2RDS62stZ7ILiE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EasOIxlc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EasOIxlc" Received: by smtp.kernel.org (Postfix) with ESMTPS id 76890C2BCFC; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1788003307; bh=0YWhM8Nmzim2CVzBQWb9SCmJsQ0PLEMgxqsU5XJgVIA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=EasOIxlcWcikZAKFUFWlouR4hoOKoHNidex6C7ale4j+/W2as62Zd1my2UMTXtDWJ nCBQZcXHryTOe5htoGwUSanl5WffsRcslrLqY5h+tgyBQLEn4NvaMo3yYPUCtzGnpj g8H+MWA9hyiIByChv1dprMKl1JHyNiKdz13X9UEIMkhFYhh+i8vyKUVCrf7QC0Ceam DCuB1tRBI5eutPXv1hKprI6aO2uEzLXBvgqRz7TB5C2in9+CZ2opVMgQ0xEuKWXNyi xlUmCz5ANuII4KIHjJYpir7kyB731eOcI/q1aQrNNnEHjnXuSWFuxmQxivCEit10xi 7t7G5w/7o6ehw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 638D5C61DD6; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) From: Xiubo Li via B4 Relay Date: Sat, 29 Aug 2026 04:35:04 -0700 Subject: [PATCH v6 4/5] ceph: move mdsc->mutex into __do_request() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260829-ceph-mdsc-mutex-optimization-v6-4-466936ccbd9d@clyso.com> References: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> In-Reply-To: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> To: Ilya Dryomov , Alex Markuze , Viacheslav Dubeyko Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, Xiubo Li X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788003304; l=14155; i=xiubo.li@clyso.com; s=20260625; h=from:subject:message-id; bh=WiaRc7WG6LW+9ERqO5UgqTYBp4O03gwE8I2znua1AqI=; b=XB/UgF7fbsA/YHnASqPejIRnIGtkHNRFU4laVCesPrp4Nlnvx89aqrXLE+XKCrCD6VI0nl0+V ha4m9YnVAveAKAmnsOIWyEX+qd/evoX4NLkwB5OGZaaWUBQ/rO3Qb2U X-Developer-Key: i=xiubo.li@clyso.com; a=ed25519; pk=V3NGr0AgAopiUhaLY51ipBkLN5LlcLhjOEfLEq1RoZ8= X-Endpoint-Received: by B4 Relay for xiubo.li@clyso.com/20260625 with auth_id=840 X-Original-From: Xiubo Li Reply-To: xiubo.li@clyso.com From: Xiubo Li Currently every caller must hold mdsc->mutex when invoking the request-send machinery. Move the mutex acquisition inside __do_request() so that callers can fire off a request without first serializing on the global lock. The mutex is released before the network send phase and re-acquired only for cleanup, so dentry traversal and message construction run concurrently across CPUs. This is the primary source of the observed 2x stat throughput improvement: the per-request send path shrinks from hundreds of microseconds to tens of microseconds once it no longer waits on the mutex. Wait-list draining, session-state wake-ups, and request kicking are reworked to either use the new wait-list spinlock or collect candidates under the mutex and process them outside it. Signed-off-by: Xiubo Li --- fs/ceph/mds_client.c | 118 +++++++++++++++++++++++++++++++++++------------= ---- fs/ceph/mds_client.h | 1 + 2 files changed, 83 insertions(+), 36 deletions(-) diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index 5983ae6e3085..0b515df15081 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -2813,6 +2813,7 @@ ceph_mdsc_create_request(struct ceph_mds_client *mdsc= , int op, int mode) init_completion(&req->r_completion); init_completion(&req->r_safe_completion); INIT_LIST_HEAD(&req->r_unsafe_item); + INIT_LIST_HEAD(&req->r_aux_item); =20 ktime_get_coarse_real_ts64(&req->r_stamp); =20 @@ -3532,20 +3533,23 @@ static int __prepare_send_request(struct ceph_mds_s= ession *session, * Avoid infinite retrying after overflow. The client will * increase the retry count and if the MDS is old version, * so we limit to retry at most 256 times. + * + * r_attempts was already incremented under mdsc->mutex by the + * caller (__do_request or replay_unsafe_requests), so the + * actual retry count is r_attempts - 1 and we skip the check + * on the first dispatch (r_attempts =3D=3D 1). */ - if (req->r_attempts) { - old_max_retry =3D sizeof_field(struct ceph_mds_request_head, - num_retry); - old_max_retry =3D 1 << (old_max_retry * BITS_PER_BYTE); - if ((old_version && req->r_attempts >=3D old_max_retry) || - ((uint32_t)req->r_attempts >=3D U32_MAX)) { + if (req->r_attempts > 1) { + old_max_retry =3D sizeof_field(struct ceph_mds_request_head, + num_retry); + old_max_retry =3D 1 << (old_max_retry * BITS_PER_BYTE); + if ((old_version && (req->r_attempts - 1) >=3D old_max_retry) || + ((uint32_t)(req->r_attempts - 1) >=3D U32_MAX)) { pr_warn_ratelimited_client(cl, "request tid %llu seq overflow\n", req->r_tid); return -EMULTIHOP; - } + } } - - req->r_attempts++; if (req->r_inode) { struct ceph_cap *cap =3D ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds); @@ -3657,9 +3661,36 @@ static void __do_request(struct ceph_mds_client *mds= c, int err =3D 0; bool random; =20 + mutex_lock(&mdsc->mutex); + + /* + * r_attempts is bumped under mdsc->mutex just before the mutex + * is dropped to send the request, and it is only ever written + * back to 0 by the forward handler or cleanup_session_requests() + * (both under the mutex) before re-dispatching through a new + * __do_request() call. Consequently, r_attempts > 0 at this + * point always means another __do_request() instance has already + * passed the point of no return for this request, i.e. a racing + * kick_requests() or __wake_requests() picked up the same + * request from the xarray or a wait list and is about to send + * it. Bail out to prevent a double dispatch. + * + * Note: kick_requests() also filters on r_attempts > 0 during + * its collection pass, but that is a one-time snapshot taken + * under the mutex. Between that snapshot and the actual + * __do_request() call the mutex is dropped and re-acquired, so + * the protection is not atomic =E2=80=94 this per-request gate closes + * the remaining window. + */ + if (req->r_attempts > 0) { + mutex_unlock(&mdsc->mutex); + return; + } + if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) { if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) __unregister_request(mdsc, req); + mutex_unlock(&mdsc->mutex); return; } =20 @@ -3692,6 +3723,7 @@ static void __do_request(struct ceph_mds_client *mdsc, spin_lock(&mdsc->wait_list_lock); list_add(&req->r_wait, &mdsc->waiting_for_map); spin_unlock(&mdsc->wait_list_lock); + mutex_unlock(&mdsc->mutex); return; } if (!(mdsc->fsc->mount_options->flags & @@ -3717,6 +3749,7 @@ static void __do_request(struct ceph_mds_client *mdsc, spin_lock(&mdsc->wait_list_lock); list_add(&req->r_wait, &mdsc->waiting_for_map); spin_unlock(&mdsc->wait_list_lock); + mutex_unlock(&mdsc->mutex); return; } =20 @@ -3790,6 +3823,9 @@ static void __do_request(struct ceph_mds_client *mdsc, goto out_session; } =20 + req->r_attempts++; + mutex_unlock(&mdsc->mutex); + /* send request */ req->r_resend_mds =3D -1; /* forget any previous mds hint */ =20 @@ -3820,6 +3856,7 @@ static void __do_request(struct ceph_mds_client *mdsc, err =3D wait_on_bit(&di->flags, CEPH_DENTRY_ASYNC_CREATE_BIT, TASK_KILLABLE); if (err) { + mutex_lock(&mdsc->mutex); mutex_lock(&req->r_fill_mutex); set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags); mutex_unlock(&req->r_fill_mutex); @@ -3857,6 +3894,8 @@ static void __do_request(struct ceph_mds_client *mdsc, =20 err =3D __send_request(session, req, false); =20 + mutex_lock(&mdsc->mutex); + out_session: ceph_put_mds_session(session); finish: @@ -3866,12 +3905,10 @@ static void __do_request(struct ceph_mds_client *md= sc, complete_request(mdsc, req); __unregister_request(mdsc, req); } + mutex_unlock(&mdsc->mutex); return; } =20 -/* - * called under mdsc->mutex - */ static void __wake_requests(struct ceph_mds_client *mdsc, struct list_head *head) { @@ -3901,10 +3938,14 @@ static void __wake_requests(struct ceph_mds_client = *mdsc, static void kick_requests(struct ceph_mds_client *mdsc, int mds) { struct ceph_client *cl =3D mdsc->fsc->client; - struct ceph_mds_request *req; + struct ceph_mds_request *req, *nreq; unsigned long idx; + LIST_HEAD(kick_list); =20 doutc(cl, "kick_requests mds%d\n", mds); + + /* collect matching requests under the mutex */ + mutex_lock(&mdsc->mutex); idx =3D 0; xa_for_each(&mdsc->request_tree, idx, req) { if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) @@ -3913,14 +3954,23 @@ static void kick_requests(struct ceph_mds_client *m= dsc, int mds) continue; /* only new requests */ if (req->r_session && req->r_session->s_mds =3D=3D mds) { - doutc(cl, " kicking tid %llu\n", req->r_tid); + ceph_mdsc_get_request(req); spin_lock(&mdsc->wait_list_lock); list_del_init(&req->r_wait); spin_unlock(&mdsc->wait_list_lock); - trace_ceph_mdsc_resume_request(mdsc, req); - __do_request(mdsc, req); + list_add_tail(&req->r_aux_item, &kick_list); } } + mutex_unlock(&mdsc->mutex); + + /* replay without the mutex */ + list_for_each_entry_safe(req, nreq, &kick_list, r_aux_item) { + doutc(cl, " kicking tid %llu\n", req->r_tid); + trace_ceph_mdsc_resume_request(mdsc, req); + list_del_init(&req->r_aux_item); + __do_request(mdsc, req); + ceph_mdsc_put_request(req); + } } =20 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *d= ir, @@ -3980,10 +4030,11 @@ int ceph_mdsc_submit_request(struct ceph_mds_client= *mdsc, struct inode *dir, doutc(cl, "submit_request on %p for inode %p\n", req, dir); mutex_lock(&mdsc->mutex); __register_request(mdsc, req, dir); + mutex_unlock(&mdsc->mutex); + trace_ceph_mdsc_submit_request(mdsc, req); __do_request(mdsc, req); err =3D req->r_err; - mutex_unlock(&mdsc->mutex); return err; } =20 @@ -4372,13 +4423,14 @@ static void handle_forward(struct ceph_mds_client *= mdsc, req->r_num_fwd =3D fwd_seq; req->r_resend_mds =3D next_mds; put_request_session(req); - __do_request(mdsc, req); } mutex_unlock(&mdsc->mutex); =20 /* kick calling process */ if (aborted) complete_request(mdsc, req); + else if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) + __do_request(mdsc, req); ceph_mdsc_put_request(req); return; =20 @@ -4706,11 +4758,9 @@ static void handle_session(struct ceph_mds_session *= session, =20 mutex_unlock(&session->s_mutex); if (wake) { - mutex_lock(&mdsc->mutex); __wake_requests(mdsc, &session->s_waiting); if (wake =3D=3D 2) kick_requests(mdsc, mds); - mutex_unlock(&mdsc->mutex); } if (op =3D=3D CEPH_SESSION_CLOSE) ceph_put_mds_session(session); @@ -4767,6 +4817,7 @@ static void replay_unsafe_requests(struct ceph_mds_cl= ient *mdsc, =20 mutex_lock(&mdsc->mutex); list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) + req->r_attempts++; __send_request(session, req, true); =20 /* @@ -4786,6 +4837,7 @@ static void replay_unsafe_requests(struct ceph_mds_cl= ient *mdsc, =20 ceph_mdsc_release_dir_caps_async(req); =20 + req->r_attempts++; __send_request(session, req, true); } mutex_unlock(&mdsc->mutex); @@ -5346,9 +5398,7 @@ static int send_mds_reconnect(struct ceph_mds_client = *mdsc, =20 mutex_unlock(&session->s_mutex); =20 - mutex_lock(&mdsc->mutex); __wake_requests(mdsc, &session->s_waiting); - mutex_unlock(&mdsc->mutex); =20 up_read(&mdsc->snap_rwsem); ceph_pagelist_release(recon_state.pagelist); @@ -5773,8 +5823,8 @@ static void ceph_mdsc_reset_workfn(struct work_struct= *work) } sessions[i]->s_state =3D CEPH_MDS_SESSION_CLOSED; __unregister_session(mdsc, sessions[i]); - __wake_requests(mdsc, &sessions[i]->s_waiting); mutex_unlock(&mdsc->mutex); + __wake_requests(mdsc, &sessions[i]->s_waiting); =20 mutex_lock(&sessions[i]->s_mutex); cleanup_session_requests(mdsc, sessions[i]); @@ -5785,9 +5835,7 @@ static void ceph_mdsc_reset_workfn(struct work_struct= *work) =20 ceph_put_mds_session(sessions[i]); =20 - mutex_lock(&mdsc->mutex); kick_requests(mdsc, mds); - mutex_unlock(&mdsc->mutex); =20 torn_down++; pr_info_client(cl, "mds%d session reset complete\n", mds); @@ -5903,8 +5951,8 @@ static void check_new_map(struct ceph_mds_client *mds= c, /* force close session for stopped mds */ ceph_get_mds_session(s); __unregister_session(mdsc, s); - __wake_requests(mdsc, &s->s_waiting); mutex_unlock(&mdsc->mutex); + __wake_requests(mdsc, &s->s_waiting); =20 mutex_lock(&s->s_mutex); cleanup_session_requests(mdsc, s); @@ -5913,8 +5961,8 @@ static void check_new_map(struct ceph_mds_client *mds= c, =20 ceph_put_mds_session(s); =20 - mutex_lock(&mdsc->mutex); kick_requests(mdsc, i); + mutex_lock(&mdsc->mutex); continue; } =20 @@ -5962,9 +6010,9 @@ static void check_new_map(struct ceph_mds_client *mds= c, oldstate !=3D CEPH_MDS_STATE_STARTING) pr_info_client(cl, "mds%d recovery completed\n", s->s_mds); - kick_requests(mdsc, i); ceph_get_mds_session(s); mutex_unlock(&mdsc->mutex); + kick_requests(mdsc, i); mutex_lock(&s->s_mutex); mutex_lock(&mdsc->mutex); ceph_put_mds_session(s); @@ -6877,8 +6925,8 @@ void ceph_mdsc_force_umount(struct ceph_mds_client *m= dsc) =20 if (session->s_state =3D=3D CEPH_MDS_SESSION_REJECTED) __unregister_session(mdsc, session); - __wake_requests(mdsc, &session->s_waiting); mutex_unlock(&mdsc->mutex); + __wake_requests(mdsc, &session->s_waiting); =20 mutex_lock(&session->s_mutex); __close_session(mdsc, session); @@ -6889,11 +6937,11 @@ void ceph_mdsc_force_umount(struct ceph_mds_client = *mdsc) mutex_unlock(&session->s_mutex); ceph_put_mds_session(session); =20 - mutex_lock(&mdsc->mutex); kick_requests(mdsc, mds); + mutex_lock(&mdsc->mutex); } - __wake_requests(mdsc, &mdsc->waiting_for_map); mutex_unlock(&mdsc->mutex); + __wake_requests(mdsc, &mdsc->waiting_for_map); } =20 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc) @@ -7035,8 +7083,8 @@ void ceph_mdsc_handle_fsmap(struct ceph_mds_client *m= dsc, struct ceph_msg *msg) err_out: mutex_lock(&mdsc->mutex); mdsc->mdsmap_err =3D err; - __wake_requests(mdsc, &mdsc->waiting_for_map); mutex_unlock(&mdsc->mutex); + __wake_requests(mdsc, &mdsc->waiting_for_map); } =20 /* @@ -7087,11 +7135,11 @@ void ceph_mdsc_handle_mdsmap(struct ceph_mds_client= *mdsc, struct ceph_msg *msg) mdsc->fsc->max_file_size =3D min((loff_t)mdsc->mdsmap->m_max_file_size, MAX_LFS_FILESIZE); =20 + mutex_unlock(&mdsc->mutex); __wake_requests(mdsc, &mdsc->waiting_for_map); ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP, mdsc->mdsmap->m_epoch); =20 - mutex_unlock(&mdsc->mutex); schedule_delayed(mdsc, 0); return; =20 @@ -7189,8 +7237,8 @@ static void mds_peer_reset(struct ceph_connection *co= n) ceph_get_mds_session(s); s->s_state =3D CEPH_MDS_SESSION_CLOSED; __unregister_session(mdsc, s); - __wake_requests(mdsc, &s->s_waiting); mutex_unlock(&mdsc->mutex); + __wake_requests(mdsc, &s->s_waiting); =20 mutex_lock(&s->s_mutex); cleanup_session_requests(mdsc, s); @@ -7199,9 +7247,7 @@ static void mds_peer_reset(struct ceph_connection *co= n) =20 wake_up_all(&mdsc->session_close_wq); =20 - mutex_lock(&mdsc->mutex); kick_requests(mdsc, s->s_mds); - mutex_unlock(&mdsc->mutex); =20 ceph_put_mds_session(s); break; diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h index 0ea144ba4c7d..11059678284d 100644 --- a/fs/ceph/mds_client.h +++ b/fs/ceph/mds_client.h @@ -428,6 +428,7 @@ struct ceph_mds_request { struct completion r_safe_completion; ceph_mds_request_callback_t r_callback; struct list_head r_unsafe_item; /* per-session unsafe list item */ + struct list_head r_aux_item; /* auxiliary local list item */ =20 long long r_dir_release_cnt; long long r_dir_ordered_cnt; --=20 2.53.0 From nobody Sat Sep 26 22:01:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7BEE396B73; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003308; cv=none; b=FFSHMte3SAp8isP0XLmqbRm0yAH/Cwi65RLr1kpJ9akRJKdRMzHfmkmajqCxAytWiMbttu8BA8MGlHYXnxWeB/4xoAKZlLweD1wIzsdQlB9j/n1obT+JiWbqFwv1fG3VS1B/gn8TxoNNl4sFpvGf31cMQKKGEzGVFqu3tNxPYHo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788003308; c=relaxed/simple; bh=b8eGT81xD59LoNyGSUZ5Ly3rF6alKppgCVacsl6/Fz0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dDOOplgbWZuGt2kWku2H8bbfMvvSJOZPQkMmN9sxQfM7kJ2xjod/orhN2h4r45mhyL5DF6R0am5Cv+8bbg4Ll8Fmvy5ITwe4NBT4QdI3hTx4xbNICsBJWe8yD7RQ0sirGNWyUrVOQ2LibeLT+tlwjx3MyaznteIw0yQ04vZx2VY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AmC9Or+U; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AmC9Or+U" Received: by smtp.kernel.org (Postfix) with ESMTPS id 83B5EC2BCFD; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1788003307; bh=b8eGT81xD59LoNyGSUZ5Ly3rF6alKppgCVacsl6/Fz0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=AmC9Or+UqTx8ZVk5WTaFvBN5c3CRFwnbPFgy17LJenxyIxMXXiKu6nUeaThmQ1rJ/ k/PEHN2SAVRUy5ZBFMVMpoWk2OYofaugUjWbvxdBp3rLKU/61FhdTfxlnSh27COwBO LWXftVKwimjLeEhnyNYTN+EMSM/3RWwLs2IIIUhZc6D5iDM2wxedwgt/oHUXjpfNbu pE82RKr8IIV50QKoOsCRT4uCKjlwVP3yQHFeDs56+NUXskx/M4YD1ulERE7XfAmebC c2fI44t2yx8V68Qv1t3+dm3b6NuaK27kGCScXl6337ynk181m1wNXiGCtlL0NwfITv FQo5X2ztSdXiA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72DE0C61DE1; Sat, 29 Aug 2026 11:35:07 +0000 (UTC) From: Xiubo Li via B4 Relay Date: Sat, 29 Aug 2026 04:35:05 -0700 Subject: [PATCH v6 5/5] ceph: narrow mdsc->mutex scope in replay_unsafe_requests Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260829-ceph-mdsc-mutex-optimization-v6-5-466936ccbd9d@clyso.com> References: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> In-Reply-To: <20260829-ceph-mdsc-mutex-optimization-v6-0-466936ccbd9d@clyso.com> To: Ilya Dryomov , Alex Markuze , Viacheslav Dubeyko Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, Xiubo Li X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788003304; l=20051; i=xiubo.li@clyso.com; s=20260625; h=from:subject:message-id; bh=/EHyoZ7wRER2jIWRnLrnYMw6tSJhevbbtpyXEzZ2iKo=; b=f7omZYl6VFVRNV3sykvWGZ6/1+UGyEj/aL87HgRGWy75DhlTxEULUrfW4obSX0GH0VdnEVz6e Q7sqt0c9KSrAlQ2masxbVSg0ScdUNmXuBKwiuogRes3f8durZk7022m X-Developer-Key: i=xiubo.li@clyso.com; a=ed25519; pk=V3NGr0AgAopiUhaLY51ipBkLN5LlcLhjOEfLEq1RoZ8= X-Endpoint-Received: by B4 Relay for xiubo.li@clyso.com/20260625 with auth_id=840 X-Original-From: Xiubo Li Reply-To: xiubo.li@clyso.com From: Xiubo Li Currently replay_unsafe_requests() holds mdsc->mutex across the entire function, including the __send_request() calls. Since __send_request() is lockless and the async cap-release helper schedules deferred work, neither needs the mutex. Collect the unsafe-list entries and the matching old xarray entries into local lists under mdsc->mutex, taking a reference on each, then replay them outside the mutex. Taking a reference ensures a concurrent reply handler can complete and unregister a request without invalidating the local list or the iterator. Unsafe requests remain on session->s_unsafe; r_aux_item serves only as a walk-list link. This keeps them tracked as unsafe until the MDS replies, so a later reconnect can replay them again and cleanup_session_requests() can still abort them on session teardown. Signed-off-by: Xiubo Li --- fs/ceph/mds_client.c | 311 ++++++++++++++++++++++++++++++++++++++++++++---= ---- fs/ceph/mds_client.h | 30 ++++- 2 files changed, 300 insertions(+), 41 deletions(-) diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index 0b515df15081..aaf7e11b3d60 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -1925,12 +1925,24 @@ static void cleanup_session_requests(struct ceph_md= s_client *mdsc, mapping_set_error(req->r_unsafe_dir->i_mapping, -EIO); __unregister_request(mdsc, req); } - /* zero r_attempts, so kick_requests() will re-send requests */ + /* + * Zero r_attempts so that the following kick_requests() will + * re-send the request. If a dispatch owner is currently in the + * send window (CEPH_MDS_R_DISPATCHING set), a concurrent + * kick_requests() could not pick the request up and the resend + * request would be lost; set CEPH_MDS_R_RESEND instead and let + * the owner re-evaluate the request when it releases dispatch + * ownership. + */ idx =3D 0; xa_for_each(&mdsc->request_tree, idx, req) { if (req->r_session && - req->r_session->s_mds =3D=3D session->s_mds) + req->r_session->s_mds =3D=3D session->s_mds) { req->r_attempts =3D 0; + if (test_bit(CEPH_MDS_R_DISPATCHING, + &req->r_req_flags)) + set_bit(CEPH_MDS_R_RESEND, &req->r_req_flags); + } } mutex_unlock(&mdsc->mutex); } @@ -3661,37 +3673,58 @@ static void __do_request(struct ceph_mds_client *md= sc, int err =3D 0; bool random; =20 +restart: + /* re-entry from the resend loop below: reset per-pass state */ + session =3D NULL; + err =3D 0; mutex_lock(&mdsc->mutex); =20 /* - * r_attempts is bumped under mdsc->mutex just before the mutex - * is dropped to send the request, and it is only ever written - * back to 0 by the forward handler or cleanup_session_requests() - * (both under the mutex) before re-dispatching through a new - * __do_request() call. Consequently, r_attempts > 0 at this - * point always means another __do_request() instance has already - * passed the point of no return for this request, i.e. a racing - * kick_requests() or __wake_requests() picked up the same - * request from the xarray or a wait list and is about to send - * it. Bail out to prevent a double dispatch. + * r_attempts only counts protocol send attempts now. Dispatch + * ownership is claimed separately via CEPH_MDS_R_DISPATCHING + * under mdsc->mutex and held across the unlocked prepare/send + * window: only one context may rebuild and send req->r_request + * at a time. A racing kick_requests() or __wake_requests() + * that sees the claim set simply drops the request here. * - * Note: kick_requests() also filters on r_attempts > 0 during - * its collection pass, but that is a one-time snapshot taken - * under the mutex. Between that snapshot and the actual - * __do_request() call the mutex is dropped and re-acquired, so - * the protection is not atomic =E2=80=94 this per-request gate closes - * the remaining window. + * cleanup_session_requests() and handle_forward() may zero + * r_attempts while the owner is in flight; instead of racing, + * they set CEPH_MDS_R_RESEND and the owner consumes it when + * releasing ownership below. */ - if (req->r_attempts > 0) { + if (test_and_set_bit(CEPH_MDS_R_DISPATCHING, &req->r_req_flags)) { mutex_unlock(&mdsc->mutex); return; } + /* + * Claiming dispatch ownership supersedes any pending resend + * request: the dispatch about to happen below is the + * re-evaluation. RESEND set later, while the send window is + * open, is consumed by the release path at the bottom. + */ + clear_bit(CEPH_MDS_R_RESEND, &req->r_req_flags); =20 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) { if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) __unregister_request(mdsc, req); - mutex_unlock(&mdsc->mutex); - return; + /* + * The request is done (or dead) and must not be sent + * again; the dispatch ownership claimed above is simply + * released, consuming any pending resend request with it. + */ + clear_bit(CEPH_MDS_R_RESEND, &req->r_req_flags); + clear_bit(CEPH_MDS_R_DISPATCHING, &req->r_req_flags); + goto no_dispatch; + } + if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { + /* + * Unsafe requests are replayed only by + * replay_unsafe_requests() during MDS reconnect, never + * through the normal dispatch path. + */ + clear_bit(CEPH_MDS_R_RESEND, &req->r_req_flags); + clear_bit(CEPH_MDS_R_DISPATCHING, &req->r_req_flags); + goto no_dispatch; } =20 if (READ_ONCE(mdsc->fsc->mount_state) =3D=3D CEPH_MOUNT_FENCE_IO) { @@ -3721,10 +3754,10 @@ static void __do_request(struct ceph_mds_client *md= sc, trace_ceph_mdsc_suspend_request(mdsc, session, req, ceph_mdsc_suspend_reason_no_mdsmap); spin_lock(&mdsc->wait_list_lock); + list_del_init(&req->r_wait); list_add(&req->r_wait, &mdsc->waiting_for_map); spin_unlock(&mdsc->wait_list_lock); - mutex_unlock(&mdsc->mutex); - return; + goto out_session; } if (!(mdsc->fsc->mount_options->flags & CEPH_MOUNT_OPT_MOUNTWAIT) && @@ -3747,10 +3780,10 @@ static void __do_request(struct ceph_mds_client *md= sc, trace_ceph_mdsc_suspend_request(mdsc, session, req, ceph_mdsc_suspend_reason_no_active_mds); spin_lock(&mdsc->wait_list_lock); + list_del_init(&req->r_wait); list_add(&req->r_wait, &mdsc->waiting_for_map); spin_unlock(&mdsc->wait_list_lock); - mutex_unlock(&mdsc->mutex); - return; + goto out_session; } =20 /* get, open session */ @@ -3798,6 +3831,7 @@ static void __do_request(struct ceph_mds_client *mdsc, trace_ceph_mdsc_suspend_request(mdsc, session, req, ceph_mdsc_suspend_reason_rejected); spin_lock(&mdsc->wait_list_lock); + list_del_init(&req->r_wait); list_add(&req->r_wait, &mdsc->waiting_for_map); spin_unlock(&mdsc->wait_list_lock); } else { @@ -3818,6 +3852,7 @@ static void __do_request(struct ceph_mds_client *mdsc, trace_ceph_mdsc_suspend_request(mdsc, session, req, ceph_mdsc_suspend_reason_session); spin_lock(&mdsc->wait_list_lock); + list_del_init(&req->r_wait); list_add(&req->r_wait, &session->s_waiting); spin_unlock(&mdsc->wait_list_lock); goto out_session; @@ -3905,6 +3940,20 @@ static void __do_request(struct ceph_mds_client *mds= c, complete_request(mdsc, req); __unregister_request(mdsc, req); } + /* + * Release dispatch ownership. If a session teardown or a + * forward observed this request while the mutex was dropped + * above, it set CEPH_MDS_R_RESEND under the mutex: consume it + * here and re-evaluate, unless the request has already been + * unregistered (error path above, or a racing reply). + */ + clear_bit(CEPH_MDS_R_DISPATCHING, &req->r_req_flags); + if (test_and_clear_bit(CEPH_MDS_R_RESEND, &req->r_req_flags) && + xa_load(&mdsc->request_tree, req->r_tid) =3D=3D req) { + mutex_unlock(&mdsc->mutex); + goto restart; + } +no_dispatch: mutex_unlock(&mdsc->mutex); return; } @@ -3913,22 +3962,68 @@ static void __wake_requests(struct ceph_mds_client = *mdsc, struct list_head *head) { struct ceph_client *cl =3D mdsc->fsc->client; - struct ceph_mds_request *req; - LIST_HEAD(tmp_list); + struct ceph_mds_request *req, *nreq; + LIST_HEAD(wake_list); =20 + /* + * Serialize the splice against the park decision in + * __do_request(): both take mdsc->mutex, so a request cannot + * be parked on @head after we have drained it, and everything + * parked before we take the mutex is moved here. + */ + mutex_lock(&mdsc->mutex); spin_lock(&mdsc->wait_list_lock); - list_splice_init(head, &tmp_list); + list_for_each_entry_safe(req, nreq, head, r_wait) { + /* + * A non-empty r_aux_item means the request is already + * owned by another dispatch collector (kick_requests() + * or replay_unsafe_requests()); let it be dispatched by + * that collector. No stale waiter can result: a + * kick-owned request was already delinked from r_wait + * at claim time, and a replay-owned request is never on + * a wait list (s_unsafe requests carry GOT_UNSAFE, + * which __do_request() refuses to park, and replay's + * old-request scan only takes r_attempts > 0 while + * parked requests always have r_attempts =3D=3D 0). + */ + if (!list_empty(&req->r_aux_item)) + continue; + list_del_init(&req->r_wait); + /* + * Pin the request in the same critical section that + * delinks it from the wait list: once r_wait is + * delinked, no list still owns it, so the reference + * keeps the object alive while it is dispatched. + */ + ceph_mdsc_get_request(req); + list_add_tail(&req->r_aux_item, &wake_list); + } spin_unlock(&mdsc->wait_list_lock); + mutex_unlock(&mdsc->mutex); + + /* + * Dispatch without holding the locks, but pop each node under + * mdsc->mutex: r_aux_item doubles as the collector-ownership + * predicate, so every access to it (collector list_empty() + * checks and these pops) must be serialized by the mutex to + * avoid a real data race on the node. + */ + mutex_lock(&mdsc->mutex); + while (!list_empty(&wake_list)) { + req =3D list_first_entry(&wake_list, struct ceph_mds_request, + r_aux_item); + list_del_init(&req->r_aux_item); + mutex_unlock(&mdsc->mutex); =20 - while (!list_empty(&tmp_list)) { - req =3D list_entry(tmp_list.next, - struct ceph_mds_request, r_wait); - list_del_init(&req->r_wait); doutc(cl, " wake request %p tid %llu\n", req, req->r_tid); trace_ceph_mdsc_resume_request(mdsc, req); __do_request(mdsc, req); + ceph_mdsc_put_request(req); + + mutex_lock(&mdsc->mutex); } + mutex_unlock(&mdsc->mutex); } =20 /* @@ -3938,7 +4033,7 @@ static void __wake_requests(struct ceph_mds_client *m= dsc, static void kick_requests(struct ceph_mds_client *mdsc, int mds) { struct ceph_client *cl =3D mdsc->fsc->client; - struct ceph_mds_request *req, *nreq; + struct ceph_mds_request *req; unsigned long idx; LIST_HEAD(kick_list); =20 @@ -3958,19 +4053,42 @@ static void kick_requests(struct ceph_mds_client *m= dsc, int mds) spin_lock(&mdsc->wait_list_lock); list_del_init(&req->r_wait); spin_unlock(&mdsc->wait_list_lock); + /* + * A non-empty r_aux_item means the request is + * already owned by another dispatch collector + * (__wake_requests() or replay_unsafe_requests()); + * never queue the same node twice. + */ + if (!list_empty(&req->r_aux_item)) { + ceph_mdsc_put_request(req); + continue; + } list_add_tail(&req->r_aux_item, &kick_list); } } mutex_unlock(&mdsc->mutex); =20 /* replay without the mutex */ - list_for_each_entry_safe(req, nreq, &kick_list, r_aux_item) { + /* + * Same as __wake_requests(): r_aux_item is the collector-ownership + * predicate, so pops are done under mdsc->mutex; the dispatch + * itself runs outside the locks. + */ + mutex_lock(&mdsc->mutex); + while (!list_empty(&kick_list)) { + req =3D list_first_entry(&kick_list, struct ceph_mds_request, + r_aux_item); + list_del_init(&req->r_aux_item); + mutex_unlock(&mdsc->mutex); + doutc(cl, " kicking tid %llu\n", req->r_tid); trace_ceph_mdsc_resume_request(mdsc, req); - list_del_init(&req->r_aux_item); __do_request(mdsc, req); ceph_mdsc_put_request(req); + + mutex_lock(&mdsc->mutex); } + mutex_unlock(&mdsc->mutex); } =20 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *d= ir, @@ -4423,6 +4541,15 @@ static void handle_forward(struct ceph_mds_client *m= dsc, req->r_num_fwd =3D fwd_seq; req->r_resend_mds =3D next_mds; put_request_session(req); + /* + * If a dispatch owner is in the send window, the + * __do_request() below cannot claim the request and the + * forward would be lost; set CEPH_MDS_R_RESEND so that + * the owner re-evaluates the request when it releases + * dispatch ownership. + */ + if (test_bit(CEPH_MDS_R_DISPATCHING, &req->r_req_flags)) + set_bit(CEPH_MDS_R_RESEND, &req->r_req_flags); } mutex_unlock(&mdsc->mutex); =20 @@ -4812,13 +4939,44 @@ static void replay_unsafe_requests(struct ceph_mds_= client *mdsc, { struct ceph_mds_request *req, *nreq; unsigned long idx; + LIST_HEAD(unsafe_list); + LIST_HEAD(old_list); =20 doutc(mdsc->fsc->client, "mds%d\n", session->s_mds); =20 + /* + * Collect unsafe and old requests under mdsc->mutex, then + * replay them without it: __send_request() is lockless and + * ceph_mdsc_release_dir_caps_async() schedules work. + */ mutex_lock(&mdsc->mutex); - list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) + list_for_each_entry_safe(req, nreq, &session->s_unsafe, + r_unsafe_item) { + /* + * A non-empty r_aux_item means the request is already + * owned by another dispatch collector; never queue the + * same node twice. Replay sends __send_request() + * directly, so it must also claim dispatch ownership + * (CEPH_MDS_R_DISPATCHING) to exclude a concurrent + * __do_request() from the send window. + */ + if (!list_empty(&req->r_aux_item)) + continue; + if (test_and_set_bit(CEPH_MDS_R_DISPATCHING, &req->r_req_flags)) + continue; + /* the replay send below supersedes any pending resend */ + clear_bit(CEPH_MDS_R_RESEND, &req->r_req_flags); + ceph_mdsc_get_request(req); req->r_attempts++; - __send_request(session, req, true); + /* + * Keep the request on s_unsafe: r_aux_item is only a + * walk list. The request must stay tracked as unsafe + * until the MDS replies, so that a later reconnect can + * replay it again and cleanup_session_requests() can + * still abort it on session teardown. + */ + list_add_tail(&req->r_aux_item, &unsafe_list); + } =20 /* * also re-send old requests when MDS enters reconnect stage. So that MDS @@ -4834,11 +4992,83 @@ static void replay_unsafe_requests(struct ceph_mds_= client *mdsc, continue; if (req->r_session->s_mds !=3D session->s_mds) continue; + if (!list_empty(&req->r_aux_item)) + continue; + if (test_and_set_bit(CEPH_MDS_R_DISPATCHING, &req->r_req_flags)) + continue; + /* the replay send below supersedes any pending resend */ + clear_bit(CEPH_MDS_R_RESEND, &req->r_req_flags); =20 - ceph_mdsc_release_dir_caps_async(req); - + ceph_mdsc_get_request(req); req->r_attempts++; + list_add_tail(&req->r_aux_item, &old_list); + } + + mutex_unlock(&mdsc->mutex); + + /* + * Same as __wake_requests(): r_aux_item is the + * collector-ownership predicate, so every access to it (the + * list_empty() checks in the collectors above and these pops) + * is serialized by mdsc->mutex. The send itself runs outside + * the locks. + */ + + /* replay unsafe requests */ + mutex_lock(&mdsc->mutex); + while (!list_empty(&unsafe_list)) { + bool resend; + + req =3D list_first_entry(&unsafe_list, struct ceph_mds_request, + r_aux_item); + list_del_init(&req->r_aux_item); + mutex_unlock(&mdsc->mutex); + __send_request(session, req, true); + + mutex_lock(&mdsc->mutex); + clear_bit(CEPH_MDS_R_DISPATCHING, &req->r_req_flags); + resend =3D test_and_clear_bit(CEPH_MDS_R_RESEND, &req->r_req_flags); + mutex_unlock(&mdsc->mutex); + /* + * A teardown or forward may have requested a re-evaluation + * while this replay send was in flight. Re-dispatch + * through the normal path, which claims dispatch ownership + * itself: if the request has become unsafe meanwhile, + * __do_request() sees GOT_UNSAFE and bails out, leaving + * the request queued for the next replay; otherwise it is + * sent again normally. + */ + if (resend) + __do_request(mdsc, req); + ceph_mdsc_put_request(req); + + mutex_lock(&mdsc->mutex); + } + mutex_unlock(&mdsc->mutex); + + /* replay old requests */ + mutex_lock(&mdsc->mutex); + while (!list_empty(&old_list)) { + bool resend; + + req =3D list_first_entry(&old_list, struct ceph_mds_request, + r_aux_item); + list_del_init(&req->r_aux_item); + mutex_unlock(&mdsc->mutex); + + ceph_mdsc_release_dir_caps_async(req); + __send_request(session, req, true); + + mutex_lock(&mdsc->mutex); + clear_bit(CEPH_MDS_R_DISPATCHING, &req->r_req_flags); + resend =3D test_and_clear_bit(CEPH_MDS_R_RESEND, &req->r_req_flags); + mutex_unlock(&mdsc->mutex); + if (resend) + __do_request(mdsc, req); + ceph_mdsc_put_request(req); + + mutex_lock(&mdsc->mutex); } mutex_unlock(&mdsc->mutex); } @@ -5398,9 +5628,10 @@ static int send_mds_reconnect(struct ceph_mds_client= *mdsc, =20 mutex_unlock(&session->s_mutex); =20 + up_read(&mdsc->snap_rwsem); + __wake_requests(mdsc, &session->s_waiting); =20 - up_read(&mdsc->snap_rwsem); ceph_pagelist_release(recon_state.pagelist); return 0; =20 diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h index 11059678284d..4de4064ab727 100644 --- a/fs/ceph/mds_client.h +++ b/fs/ceph/mds_client.h @@ -359,6 +359,28 @@ struct ceph_mds_request { #define CEPH_MDS_R_PARENT_LOCKED (7) /* is r_parent->i_rwsem wlocked? */ #define CEPH_MDS_R_ASYNC (8) /* async request */ #define CEPH_MDS_R_FSCRYPT_FILE (9) /* must marshal fscrypt_file field */ +/* + * A dispatch owner has claimed the right to rebuild and send + * req->r_request (__do_request() past the send gate, or + * replay_unsafe_requests()). Set and cleared under mdsc->mutex; + * held across the unlocked send window, cleared by the owner when it + * re-acquires the mutex and releases ownership. + */ +#define CEPH_MDS_R_DISPATCHING (10) +/* + * Another context observed a session/request state change while a + * dispatch owner was in flight and wants a re-evaluation. It is a + * request to re-evaluate, not a guarantee that a resend is still + * needed. + * + * If RESEND is set while DISPATCHING is held, the current dispatcher + * consumes it on release and redispatches. + * If RESEND is set after DISPATCHING has been cleared, the producer + * must itself trigger a redispatch: cleanup_session_requests() is + * followed by kick_requests(), while handle_forward() calls + * __do_request() directly. + */ +#define CEPH_MDS_R_RESEND (11) unsigned long r_req_flags; =20 struct mutex r_fill_mutex; @@ -428,7 +450,13 @@ struct ceph_mds_request { struct completion r_safe_completion; ceph_mds_request_callback_t r_callback; struct list_head r_unsafe_item; /* per-session unsafe list item */ - struct list_head r_aux_item; /* auxiliary local list item */ + /* + * Auxiliary walk list for the dispatch collectors; doubles as + * the collector-ownership token. INIT_LIST_HEAD() is done + * before publication; afterwards, all accesses are serialized + * by mdsc->mutex. + */ + struct list_head r_aux_item; =20 long long r_dir_release_cnt; long long r_dir_ordered_cnt; --=20 2.53.0