Skip to contentSkip to conversation

ceph: acquire write lock only if snap trace has really changed

PATCH In review Max Kellermann 3 participants 4 messages Sep 11 – Nov 5 Ceph client

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ceph: acquire write lock only if snap trace has really changed

MDS replies and cap imports acquire snap_rwsem for writing whenever they contain a snap trace, even when the cached realm information is already current. This causes a lot of lock contention with all processes writing to Ceph, because check_quota_exceeded() needs a read lock on snap_rwsem. This not only delays all writing processes, but also the messenger thread, which adds a lot of latency to all MDS requests.

This patchs adds a wrapper function for ceph_update_snap_trace() which acquires a read lock instead of a write lock, parses the new snap trace and calls ceph_update_snap_trace() with a write-upgraded lock only if the snap trace has really changed. This avoids lock contention almost all of the time.

---
 fs/ceph/caps.c       |  5 +--
 fs/ceph/mds_client.c | 11 ++----
 fs/ceph/snap.c       | 92 ++++++++++++++++++++++++++++++++++++++++++++
 fs/ceph/super.h      |  3 ++
 4 files changed, 100 insertions(+), 11 deletions(-)
fs/ceph/caps.c +1 -4
index bcb04c6cb92c..42c59e4c3716 100644
@@ -4578,15 +4578,12 @@ void ceph_handle_caps(struct ceph_mds_session *session,
 	case CEPH_CAP_OP_IMPORT:
 		realm = NULL;
 		if (snaptrace_len) {
-			down_write(&mdsc->snap_rwsem);
-			if (ceph_update_snap_trace(mdsc, snaptrace,
+			if (ceph_handle_snap_trace(mdsc, snaptrace,
 						   snaptrace + snaptrace_len,
 						   false, &realm)) {
-				up_write(&mdsc->snap_rwsem);
 				close_sessions = true;
 				goto done;
 			}
-			downgrade_write(&mdsc->snap_rwsem);
 		} else {
 			down_read(&mdsc->snap_rwsem);
 		}
fs/ceph/mds_client.c +4 -7
index 085ae0cfb5f7..f94926c520bf 100644
@@ -4198,19 +4198,16 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
 	/* snap trace */
 	realm = NULL;
 	if (rinfo->snapblob_len) {
-		down_write(&mdsc->snap_rwsem);
-		err = ceph_update_snap_trace(mdsc, rinfo->snapblob,
-				rinfo->snapblob + rinfo->snapblob_len,
-				le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
-				&realm);
+		err = ceph_handle_snap_trace(mdsc, rinfo->snapblob,
+					     rinfo->snapblob + rinfo->snapblob_len,
+					     le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
+					     &realm);
 		if (err) {
-			up_write(&mdsc->snap_rwsem);
 			close_sessions = true;
 			if (err == -EIO)
 				ceph_msg_dump(msg);
 			goto out_err;
 		}
-		downgrade_write(&mdsc->snap_rwsem);
 	} else {
 		down_read(&mdsc->snap_rwsem);
 	}
fs/ceph/snap.c +92 -0

98 lines not on this page · raw message

fs/ceph/super.h +3 -0
index 72d4e30304dc..e5782fbfeeb5 100644
@@ -1064,6 +1064,9 @@ extern void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
 extern int ceph_update_snap_trace(struct ceph_mds_client *m,
 				  void *p, void *e, bool deletion,
 				  struct ceph_snap_realm **realm_ret);
+int ceph_handle_snap_trace(struct ceph_mds_client *mdsc,
+			   void *p, void *e, bool deletion,
+			   struct ceph_snap_realm **realm_ret);
 void ceph_change_snap_realm(struct inode *inode, struct ceph_snap_realm *realm);
 extern void ceph_handle_snap(struct ceph_mds_client *mdsc,
 			     struct ceph_mds_session *session,
Signature · 1 line
2.47.3
maintainer
#1 lore raw

Hi Max,

NACK. Please respin.

1. fs/ceph/snap.c:963: Fast path rejects the global realm, so the write lock is never skipped

     `get_snap_trace_if_unmodified()` treats a NULL `realm->parent` as
     "must update".
     Match `adjust_snap_realm_parent()` and compare `realm->parent_ino`
     to `ri->parent`.
Signature · 1 line
Alex Markuze
#2 lore raw
Quoted 5 lines · Alex Markuze

1. fs/ceph/snap.c:963: Fast path rejects the global realm, so the write lock is never skipped

     `get_snap_trace_if_unmodified()` treats a NULL `realm->parent` as
     "must update".
     Match `adjust_snap_realm_parent()` and compare `realm->parent_ino`
     to `ri->parent`.

True, this is a mistake in my patch submission, sorry. My patch is correct (and is extremely effective - reducing write contention by a factor of 678), but depends on other patches I submitted long ago which were never merged. It seems the official Ceph kclient is lagging behind our Linux kernel fork too much now; ensuring compatiblity is difficult. It doesn't make sense for me to submit more Ceph patches until you have caught up. And then a v2 of this patch isn't necessary
- it's correct already.

Signature · 8 lines
Max Kellermann
Principal Architect
Hosting Technology

cm4all | Im Mediapark 6a | 50670 Köln | Germany
General information about the company can be found here:
https://www.cm4all.com/impressum
A member of the IONOS Group
#3 lore raw

这是改好的完整回复,英文原稿在前,中文对照在后:

Quoted 8 lines · Max Kellermann

True, this is a mistake in my patch submission, sorry. My patch is correct (and is extremely effective - reducing write contention by a factor of 678), but depends on other patches I submitted long ago which were never merged. It seems the official Ceph kclient is lagging behind our Linux kernel fork too much now; ensuring compatiblity is difficult. It doesn't make sense for me to submit more Ceph patches until you have caught up. And then a v2 of this patch isn't necessary
- it's correct already.

Hi Max,

This patch is worth having upstream on its own, without any of the other patches. We applied it to a kernel that has none of them, and with only Alex's one-line change on top it removes the snap_rwsem contention as you describe.

Alex's change is all that is missing. The MDS builds a snap trace as the realm followed by its parent's trace (SnapRealm::build_snap_trace() appends parent->get_snap_trace()), so every trace ends with the global realm, whose parent ino is 0. Comparing realm->parent_ino with ri->parent, as adjust_snap_realm_parent() does, lets that realm take the fast path:

- if (!realm || !realm->parent ||
- realm->parent->ino != le64_to_cpu(ri->parent) ||
+ if (!realm ||
+ realm->parent_ino != le64_to_cpu(ri->parent) ||

parent_ino is only written together with ->parent under the write lock, so reading it under the read lock is fine.

Our numbers, from a 3-MDS cluster: one client walks a 2.8M-entry directory while 16 other walkers read large directories.

without the patch with the patch
snap_rwsem sleep in the
MDS connection workers 23.2 s 0 s
directory walk 44-48 s 35-38 s

A full IO500 run (ABBA, 2 runs each) shows no regression in any phase.

So I would like to see a v2 with Alex's change folded in. Would you mind sending one?

Thanks,
Xiubo

Quoted 27 lines · Max Kellermann <[email protected]> 于2026年9月14日周一 08:10写道:
Quoted 5 lines · Alex Markuze

1. fs/ceph/snap.c:963: Fast path rejects the global realm, so the write lock is never skipped

     `get_snap_trace_if_unmodified()` treats a NULL `realm->parent` as
     "must update".
     Match `adjust_snap_realm_parent()` and compare `realm->parent_ino`
     to `ri->parent`.

True, this is a mistake in my patch submission, sorry. My patch is correct (and is extremely effective - reducing write contention by a factor of 678), but depends on other patches I submitted long ago which were never merged. It seems the official Ceph kclient is lagging behind our Linux kernel fork too much now; ensuring compatiblity is difficult. It doesn't make sense for me to submit more Ceph patches until you have caught up. And then a v2 of this patch isn't necessary
- it's correct already.

--
Max Kellermann
Principal Architect
Hosting Technology

cm4all | Im Mediapark 6a | 50670 Köln | Germany General information about the company can be found here:
https://www.cm4all.com/impressum
A member of the IONOS Group