From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796199906; b=0W+klhjxjc5bAvE2xhghAhMRex6auNApgVqIzw/cGjLcSkO7L8PV/AbGbQXGokuDjWgE IuWDLUc8sCElMmbpvn3/Z2D2VrBKa9jJvkIaGh8gRzkeUxn4z/UB9788X6u0TG7sk6XNS 8uitluSJyl83XKbHGNOK8ineOnKQg+i5QNHN3OJx6enRfFHnZg19YvvPcW14e7kbdZhXc z1GimYYbpscisSElWY0SywJ0Bsq2LKXBjzKqXXeqlsyiqg++XL+ezLQcYVE5mL9Wt4igs +dF43EezC3AmtFpyeSqXHbz/Rt1iOP37j/86dubqJCgE/DG++Kdo/ZSiR9IzzH6sEuQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796199906; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=ibngalSfsUGLXMURWFfsQ2Va4gwCdqZ+G1aMqd2Kl5I=; b=vN3CkIc+NnRQi2fFM/Uvjm9CY34Cg2ZJ5v0LggCiV3fXxdwR3qyoaMQ/aHAdHy5EnilX YFyox91AgutY03KZjWr8KDm4zhULVYhGYKuoyQALJGdOiLOyNeX4TDp8ZwG9Mco5QYpur QRGH6DyrxnNNArcTB8wA2ztfhEgqCXy/vMFTdJPBZ1bJE02rRyALr0SLOjepWX8qXArbs BNyRbTxKQYJJyKxt+TVsiJL3ZZbwJ8GRKOJHj2/TxqB1O/5SAf97QgqoUnPwdA/IPPBZV DAv9KwcJRUT1nmuiVmv4J5FGa+Vsi+W0vSk6iFBTtBt10sYDDkNSLDdIkCR8YyW+PPQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BhoG6wV5; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6029606A322; Wed, 2 Dec 2026 08:25:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BhoG6wV5 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796199904; b=zRLApsyggyvZicy4eTRsKvTROP4wmwz4PUL3HXQD495cyw+H4ZDYV3JdS4usJRtm9urh hJ0YPxThcn1shOIwhvrgNMEIDw4ERj90LkqqDkeySJzkzWfES73AJTlNuPURn2FIPtaix i8ux0gsMY/u+E7C7r2BZa1hrAZO7+7AbLXZ5tW7OmETOH8yTvXmMXkICWqGCT2bdXdRRR hVTPZhme7OY6IScrGLN0OEI/xYfp+bShuDyrekyhGiE9RN31JdkCkZsvIdPpBNGP1HF4X N9phPD4kwkg74WqlI25NOYdUmh5pTtITxJiApWeSD8PPnp5WGcUt5lTwAJsrc2p7SWw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796199904; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=ibngalSfsUGLXMURWFfsQ2Va4gwCdqZ+G1aMqd2Kl5I=; b=Q/b8P3R7HIadp5MGuUxqVt5et8U7cZFky1BD4Kp9D1Jpjt5r90jOh0apImoIXzYKc99z Qti9P6Qw/v1T+UfAuKfcu3Ez0Cb5oZBEx/mPdwtpKaxzEsT5+sFz8Y6IoA3OOS9g8YFid DVQ/HSspdoAgjvwRp5dANI1LSZT9navPmD6I3GoHoju2IPsESGjJlIoLKfgYtPR2q132E kLGRK1dLagoQuYZDxZIycoTWR3Z+4k9sa2fQ6OrtUgtB4kqlIENgbfGp9QR6DXqAocg5z O8qczDA5R9ng05w3DQCP6zb9xV0oPqa5q7OqcLIvR1sDNzPcm8Q8E6IegDnPHqb2woQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 7A6B31F00893; Wed, 2 Dec 2026 08:24:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796199904; bh=ibngalSfsUGLXMURWFfsQ2Va4gwCdqZ+G1aMqd2Kl5I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=BhoG6wV5kHidx3Zl/eJeHh4kwaebBJZhvkqMvXErPcsn4hjvs6cSSBZfuJ76T0EcW tDHbOdQ398jhvZ8FtGRASBZttwPDsED+htbRegxm7C5JsBfUUrBSnIrEOoAvbOScRg Yk9dpF920xSHE25X7kznRUg9dCaKdBCpT761INISJjwc8P4teZgTWh2uzRSFEUX5Ev E8lSwl1G3ALLCig/KCJHkkBDKqEGWqIOG56/FjsRxTFoY8nauK+HzDRIFqv/LTjcxH ApZ1jOWKMqxKqXUDgDtLQ5MpZO+aAjrpi+3cgSeMd7abSPFQkt3RF08MrHAaMiipYV tjJ3l1/y5SdFg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 705A44F305; Wed, 2 Dec 2026 08:24:49 +0000 (UTC) From: Xiubo Li via B4 Relay Date: Wed, 02 Dec 2026 09:24:43 +0100 Subject: [PATCH v9 2/5] ceph: replace the request_tree rbtree with an xarray keyed by r_tid X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261202-ceph-mdsc-mutex-optimization-v9-2-f7719b7c4f57@clyso.com> References: <20261202-ceph-mdsc-mutex-optimization-v9-0-f7719b7c4f57@clyso.com> In-Reply-To: <20261202-ceph-mdsc-mutex-optimization-v9-0-f7719b7c4f57@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=1796199886; l=12043; i=xiubo.li@clyso.com; s=20260625; h=from:subject:message-id; bh=SSk42FlzKyO+5CjgLdm12xeSSkJOi2Ovx8NsQls2KQg=; b=ZlERXUpXj0nl1gYlRIVIF43EQgqUwk7BFN6JSxn3uWD+N2lLXvtIOFU3OQW1lyhI1/PwwH9jq E1ow2la4MUrCw5vmzaP+r4aGyLloGLhJETvCPeDuBShNL+LT6jVElLA 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 Content-Transfer-Encoding: 7bit From: Xiubo Li Replace the red-black tree that indexes pending MDS requests by transaction ID with an xarray. A keyed lookup walks a few radix levels instead of an O(log N) rbtree path, and the xarray's native iterators replace open-coded rb_first() / rb_next() walks. Lookups that take a reference on the request still run under mdsc->mutex, which keeps the request alive until the reference is taken. 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 | 112 +++++++++++++++++++++++---------------------------- fs/ceph/mds_client.h | 3 +- 4 files changed, 55 insertions(+), 67 deletions(-) diff --git a/fs/ceph/Kconfig b/fs/ceph/Kconfig index 12d55d26806b..2e5a3e800098 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 NETFS_PGPRIV2 diff --git a/fs/ceph/debugfs.c b/fs/ceph/debugfs.c index 7851cfe51d06..c2bd4c760f56 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 = s->private; struct ceph_mds_client *mdsc = fsc->mdsc; struct ceph_mds_request *req; - struct rb_node *rp; + unsigned long idx; char *path; mutex_lock(&mdsc->mutex); - for (rp = rb_first(&mdsc->request_tree); rp; rp = rb_next(rp)) { - req = rb_entry(rp, struct ceph_mds_request, r_node); + idx = 0; + xa_for_each(&mdsc->request_tree, idx, req) { 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 a6820b7567fe..0465b4ca06c7 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -1268,7 +1268,6 @@ void ceph_mdsc_release_request(struct kref *kref) kmem_cache_free(ceph_mds_request_cachep, req); } -DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node) /* * lookup session, bump ref if found. @@ -1280,7 +1279,7 @@ lookup_get_request(struct ceph_mds_client *mdsc, u64 tid) { struct ceph_mds_request *req; - req = lookup_request(&mdsc->request_tree, tid); + req = xa_load(&mdsc->request_tree, tid); if (req) ceph_mdsc_get_request(req); @@ -1314,7 +1313,14 @@ static void __register_request(struct ceph_mds_client *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 = -ENOMEM; + return; + } req->r_cred = get_current_cred(); if (!req->r_mnt_idmap) @@ -1343,7 +1349,8 @@ static void __unregister_request(struct ceph_mds_client *mdsc, list_del_init(&req->r_unsafe_item); if (req->r_tid == READ_ONCE(mdsc->oldest_tid)) { - struct rb_node *p = rb_next(&req->r_node); + unsigned long tidx = req->r_tid + 1; + struct ceph_mds_request *next_req; u64 next_tid = 0; /* @@ -1352,19 +1359,16 @@ static void __unregister_request(struct ceph_mds_client *mdsc, * every completed request", so a lockless reader must never * see a transient 0 while another tracked request remains. */ - while (p) { - struct ceph_mds_request *next_req = - 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 != CEPH_MDS_OP_SETFILELOCK) { next_tid = next_req->r_tid; break; } - p = rb_next(p); } WRITE_ONCE(mdsc->oldest_tid, next_tid); } - erase_request(&mdsc->request_tree, req); + xa_erase(&mdsc->request_tree, req->r_tid); if (req->r_unsafe_dir) { struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir); @@ -1926,7 +1930,7 @@ static void cleanup_session_requests(struct ceph_mds_client *mdsc, { struct ceph_client *cl = mdsc->fsc->client; struct ceph_mds_request *req; - struct rb_node *p; + unsigned long idx; doutc(cl, "mds%d\n", session->s_mds); mutex_lock(&mdsc->mutex); @@ -1942,10 +1946,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 = rb_first(&mdsc->request_tree); - while (p) { - req = rb_entry(p, struct ceph_mds_request, r_node); - p = rb_next(p); + idx = 0; + xa_for_each(&mdsc->request_tree, idx, req) { if (req->r_session && req->r_session->s_mds == session->s_mds) req->r_attempts = 0; @@ -2862,7 +2864,6 @@ ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode) req->r_fmode = -1; req->r_feature_needed = -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); @@ -2882,10 +2883,9 @@ ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode) */ static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc) { - 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 = 0; + + return xa_find(&mdsc->request_tree, &idx, ULONG_MAX, XA_PRESENT); } static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc) @@ -3946,12 +3946,11 @@ static void kick_requests(struct ceph_mds_client *mdsc, int mds) { struct ceph_client *cl = mdsc->fsc->client; struct ceph_mds_request *req; - struct rb_node *p = rb_first(&mdsc->request_tree); + unsigned long idx; doutc(cl, "kick_requests mds%d\n", mds); - while (p) { - req = rb_entry(p, struct ceph_mds_request, r_node); - p = rb_next(p); + idx = 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) @@ -4852,7 +4851,7 @@ static void replay_unsafe_requests(struct ceph_mds_client *mdsc, struct ceph_mds_session *session) { struct ceph_mds_request *req, *nreq; - struct rb_node *p; + unsigned long idx; doutc(mdsc->fsc->client, "mds%d\n", session->s_mds); @@ -4864,10 +4863,8 @@ static void replay_unsafe_requests(struct ceph_mds_client *mdsc, * also re-send old requests when MDS enters reconnect stage. So that MDS * can process completed request in clientreplay stage. */ - p = rb_first(&mdsc->request_tree); - while (p) { - req = rb_entry(p, struct ceph_mds_request, r_node); - p = rb_next(p); + idx = 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) @@ -5713,7 +5710,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; mutex_lock(&mdsc->mutex); @@ -5721,14 +5718,12 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work) mutex_unlock(&mdsc->mutex); mutex_lock(&mdsc->mutex); - rn = rb_first(&mdsc->request_tree); - while (rn) { - req = rb_entry(rn, struct ceph_mds_request, r_node); - if (req->r_tid > last_tid) - break; + idx = 0; + while ((req = xa_find(&mdsc->request_tree, &idx, last_tid, + XA_PRESENT))) { if (req->r_op == CEPH_MDS_OP_SETFILELOCK || !(req->r_op & CEPH_MDS_OP_WRITE)) { - rn = rb_next(rn); + idx++; continue; } ceph_mdsc_get_request(req); @@ -5741,7 +5736,7 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work) ceph_mdsc_put_request(req); if (time_after(jiffies, drain_deadline)) break; - rn = rb_first(&mdsc->request_tree); + idx = 0; /* restart: tree may have changed */ } mutex_unlock(&mdsc->mutex); @@ -6478,7 +6473,7 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc) mdsc->snap_realms = RB_ROOT; INIT_LIST_HEAD(&mdsc->snap_empty); spin_lock_init(&mdsc->snap_empty_lock); - mdsc->request_tree = RB_ROOT; + xa_init(&mdsc->request_tree); INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work); mdsc->last_renew_caps = jiffies; INIT_LIST_HEAD(&mdsc->cap_delay_list); @@ -6809,34 +6804,33 @@ static void flush_mdlog_and_wait_mdsc_unsafe_requests(struct ceph_mds_client *md u64 want_tid) { struct ceph_client *cl = mdsc->fsc->client; - struct ceph_mds_request *req = NULL, *nextreq; + struct ceph_mds_request *req; struct ceph_mds_session *last_session = NULL; - struct rb_node *n; + unsigned long idx; mutex_lock(&mdsc->mutex); doutc(cl, "want %lld\n", want_tid); -restart: - req = __get_oldest_req(mdsc); - while (req && req->r_tid <= want_tid) { - /* find next request */ - n = rb_next(&req->r_node); - if (n) - nextreq = rb_entry(n, struct ceph_mds_request, r_node); - else - nextreq = NULL; - if (req->r_op != CEPH_MDS_OP_SETFILELOCK && - (req->r_op & CEPH_MDS_OP_WRITE)) { + idx = 0; + while ((req = xa_find(&mdsc->request_tree, &idx, want_tid, + XA_PRESENT))) { + u64 next_tid = req->r_tid + 1; + + if (req->r_op == CEPH_MDS_OP_SETFILELOCK || + !(req->r_op & CEPH_MDS_OP_WRITE)) { + idx = next_tid; + continue; + } + + { struct ceph_mds_session *s = req->r_session; if (!s) { - req = nextreq; + idx = next_tid; continue; } /* write op */ ceph_mdsc_get_request(req); - if (nextreq) - ceph_mdsc_get_request(nextreq); s = ceph_get_mds_session(s); mutex_unlock(&mdsc->mutex); @@ -6854,16 +6848,9 @@ static void flush_mdlog_and_wait_mdsc_unsafe_requests(struct ceph_mds_client *md 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 = next_tid; } - req = nextreq; } mutex_unlock(&mdsc->mutex); ceph_put_mds_session(last_session); @@ -7023,6 +7010,7 @@ static void ceph_mdsc_stop(struct ceph_mds_client *mdsc) if (mdsc->mdsmap) ceph_mdsmap_destroy(mdsc->mdsmap); kfree(mdsc->sessions); + xa_destroy(&mdsc->request_tree); ceph_caps_finalize(mdsc); if (mdsc->s_cap_auths) { diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h index ff9ba09f3b59..9528b839a04a 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; struct kref r_kref; @@ -537,7 +536,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 */ -- 2.53.0