mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/4] ceph: fix the buffered read size metric and make the subvolume metrics per CPU
@ 2026-10-19 11:33 Xiubo Li via B4 Relay
  2026-10-19 11:33 ` [PATCH v2 1/4] ceph: report the requested length in the netfs read metrics Xiubo Li via B4 Relay
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Xiubo Li via B4 Relay @ 2026-10-19 11:33 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Xiubo Li, Viacheslav Dubeyko
  Cc: ceph-devel, linux-kernel, Dan van der Ster

Two problems in the client I/O metrics, reported by Dan van der Ster
while looking at IO500 runs on 144-core Grace clients.

Patch 1: metrics/size has reported 0 bytes for every page cache read
since buffered reads moved to CEPH_OSD_DATA_TYPE_ITER, because
finish_netfs_read() still reads osd_data->length, which only the page
vector type sets.

Patches 2-4: every OSD read and write completion updates the subvolume
metrics through state shared by the whole mount.  Even with subvolume
metrics disabled it bumped an atomic64 counter that nothing reads;
with them enabled it took a mount-wide spinlock and searched an rbtree.
Patch 2 drops the unused counters.  Patch 3 flushes the OSD completion
workqueue before the mds client and its metrics are freed.  Patch 4
accumulates the counters in a per-CPU slot and only folds them into the
tree when the slot changes subvolume or when the metrics are sent or
dumped.

Tested on a 12-vCPU x86_64 VM against a main MDS with SUBVOLUME_METRICS,
with KASAN, lockdep and kmemleak enabled:

- 12 fio jobs doing 4k direct I/O in one subvolume: the op and byte
  totals the MDS received over all metrics sends match what fio issued.
  Alternating between two subvolumes on every I/O, the MDS sees the
  same totals as with the unpatched client.
- The same with failslab failing half of the subvolume entry
  allocations: in one subvolume v1 lost about 9% of the ops and this
  version loses none; alternating between two subvolumes it drops 64
  of 240k I/Os, those whose direct insert failed as well.
- lock_stat over 20s of 12-job random reads in one subvolume:
  tracker->lock goes from 90k acquisitions and 78 contentions to 316
  and none; the per-CPU slot locks see no contention.
- umount -f with the OSDs paused and I/O in flight, and MDS failover
  and kill -9 under I/O: no KASAN, lockdep or kmemleak reports.  The
  race patch 3 closes was not hit there: all 3840 aborted requests
  completed before ceph_mdsc_destroy().

Throughput on that VM is unchanged within noise: at 12 vCPUs the
tracker lock is not a bottleneck, so the gain is only expected on
large clients like the ones in the report.

---
Changes in v2:
- Fold a per-CPU slot into the tree under the tracker lock and clear it
  only once its entry is in the tree, so its counters are kept when the
  entry allocation fails (Alex Markuze)
- New patch 3: flush osdc->completion_wq in ceph_mdsc_destroy() before
  the metrics are freed (Alex Markuze)
- Link to v1: https://patch.msgid.link/20261014-ceph-metrics-percpu-v1-0-38c5a24fec6e@clyso.com

To: Ilya Dryomov <idryomov@gmail.com>
To: Alex Markuze <amarkuze@redhat.com>
To: Xiubo Li <xiubo.li@clyso.com>
To: Viacheslav Dubeyko <slava@dubeyko.com>
Cc: ceph-devel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org

---
Xiubo Li (4):
      ceph: report the requested length in the netfs read metrics
      ceph: drop the unused subvolume metrics debug counters
      ceph: flush the OSD completions before destroying the mds client
      ceph: accumulate subvolume metrics per CPU

 fs/ceph/addr.c                  |   2 +-
 fs/ceph/mds_client.c            |   8 +-
 fs/ceph/subvolume_metrics.c     | 275 ++++++++++++++++++++++++++++------------
 fs/ceph/subvolume_metrics.h     |  26 +---
 include/linux/ceph/osd_client.h |   1 +
 net/ceph/osd_client.c           |  11 ++
 6 files changed, 217 insertions(+), 106 deletions(-)
---
base-commit: ed0502cb11f84fff29f374238e526ae94d9bdfde
change-id: 20261014-ceph-metrics-percpu-1cb5047f7150

Best regards,
--  
Xiubo Li <xiubo.li@clyso.com>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/4] ceph: report the requested length in the netfs read metrics
  2026-10-19 11:33 [PATCH v2 0/4] ceph: fix the buffered read size metric and make the subvolume metrics per CPU Xiubo Li via B4 Relay
@ 2026-10-19 11:33 ` Xiubo Li via B4 Relay
  2026-10-19 11:33 ` [PATCH v2 2/4] ceph: drop the unused subvolume metrics debug counters Xiubo Li via B4 Relay
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Xiubo Li via B4 Relay @ 2026-10-19 11:33 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Xiubo Li, Viacheslav Dubeyko
  Cc: ceph-devel, linux-kernel, Dan van der Ster

From: Xiubo Li <xiubo.li@clyso.com>

finish_netfs_read() passes osd_data->length to
ceph_update_read_metrics(), but a buffered read of an unencrypted
file attaches its data as CEPH_OSD_DATA_TYPE_ITER, and
ceph_osd_iter_init() only sets the type and the iov_iter.  length
shares a union with the iov_iter, so the read size metrics record
some unrelated iov_iter field, which is normally 0, for every page
cache read, and metrics/size is useless for buffered reads.

Report op->extent.length, the length the OSD read asked for, which is
set for every data type and equals osd_data->length for the page
vector that encrypted files still use.

Fixes: 4de77f25fd85 ("ceph: use osd_req_op_extent_osd_iter for netfs reads")
Reported-by: Dan van der Ster <dan.vanderster@clyso.com>
Signed-off-by: Xiubo Li <xiubo.li@clyso.com>
---
 fs/ceph/addr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
index dd2bd8ed2424..8500d72702d6 100644
--- a/fs/ceph/addr.c
+++ b/fs/ceph/addr.c
@@ -236,7 +236,7 @@ static void finish_netfs_read(struct ceph_osd_request *req)
 	ceph_blog_enter_gfp(fsc, &__ji, GFP_ATOMIC);
 
 	ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
-				 req->r_end_latency, osd_data->length, err);
+				 req->r_end_latency, op->extent.length, err);
 
 	boutc(cl, "result %d subreq->len=%zu i_size=%lld\n", req->r_result,
 	      subreq->len, i_size_read(req->r_inode));

-- 
2.53.0



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 2/4] ceph: drop the unused subvolume metrics debug counters
  2026-10-19 11:33 [PATCH v2 0/4] ceph: fix the buffered read size metric and make the subvolume metrics per CPU Xiubo Li via B4 Relay
  2026-10-19 11:33 ` [PATCH v2 1/4] ceph: report the requested length in the netfs read metrics Xiubo Li via B4 Relay
@ 2026-10-19 11:33 ` Xiubo Li via B4 Relay
  2026-10-19 11:33 ` [PATCH v2 3/4] ceph: flush the OSD completions before destroying the mds client Xiubo Li via B4 Relay
  2026-10-19 11:33 ` [PATCH v2 4/4] ceph: accumulate subvolume metrics per CPU Xiubo Li via B4 Relay
  3 siblings, 0 replies; 5+ messages in thread
From: Xiubo Li via B4 Relay @ 2026-10-19 11:33 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Xiubo Li, Viacheslav Dubeyko
  Cc: ceph-devel, linux-kernel, Dan van der Ster

From: Xiubo Li <xiubo.li@clyso.com>

ceph_subvolume_metrics_record_io() bumps tracker->record_calls on
every OSD read and write completion, plus record_disabled or
record_no_subvol when it bails out, and ceph_subvolume_metrics_record()
adds to the total_read/write_ops/bytes counters on top of its own
per-subvolume ones.  All of them are atomic64_t shared by every CPU
of the mount, so even with subvolume metrics disabled each I/O
completion dirties a cache line that every other completion on the
client also writes.

Nothing ever reads these counters, nor the snapshot_* ones: debugfs
prints the per-subvolume entries and the totals kept under
subvol_metrics_last_mutex.  Remove them.

Fixes: b1137e0b3d4b ("ceph: add subvolume metrics collection and reporting")
Reported-by: Dan van der Ster <dan.vanderster@clyso.com>
Signed-off-by: Xiubo Li <xiubo.li@clyso.com>
---
 fs/ceph/subvolume_metrics.c | 34 ++++------------------------------
 fs/ceph/subvolume_metrics.h | 21 ---------------------
 2 files changed, 4 insertions(+), 51 deletions(-)

diff --git a/fs/ceph/subvolume_metrics.c b/fs/ceph/subvolume_metrics.c
index 03fda1f9257b..895878a49ea7 100644
--- a/fs/ceph/subvolume_metrics.c
+++ b/fs/ceph/subvolume_metrics.c
@@ -39,16 +39,6 @@ void ceph_subvolume_metrics_init(struct ceph_subvolume_metrics_tracker *tracker)
 	tracker->tree = RB_ROOT_CACHED;
 	tracker->nr_entries = 0;
 	tracker->enabled = false;
-	atomic64_set(&tracker->snapshot_attempts, 0);
-	atomic64_set(&tracker->snapshot_empty, 0);
-	atomic64_set(&tracker->snapshot_failures, 0);
-	atomic64_set(&tracker->record_calls, 0);
-	atomic64_set(&tracker->record_disabled, 0);
-	atomic64_set(&tracker->record_no_subvol, 0);
-	atomic64_set(&tracker->total_read_ops, 0);
-	atomic64_set(&tracker->total_read_bytes, 0);
-	atomic64_set(&tracker->total_write_ops, 0);
-	atomic64_set(&tracker->total_write_bytes, 0);
 }
 
 static struct ceph_subvol_metric_rb_entry *
@@ -196,14 +186,10 @@ void ceph_subvolume_metrics_record(struct ceph_subvolume_metrics_tracker *tracke
 			entry->write_ops++;
 			entry->write_bytes += size;
 			entry->write_latency_us += latency_us;
-			atomic64_inc(&tracker->total_write_ops);
-			atomic64_add(size, &tracker->total_write_bytes);
 		} else {
 			entry->read_ops++;
 			entry->read_bytes += size;
 			entry->read_latency_us += latency_us;
-			atomic64_inc(&tracker->total_read_ops);
-			atomic64_add(size, &tracker->total_read_bytes);
 		}
 		spin_unlock(&tracker->lock);
 		if (new_entry)
@@ -227,8 +213,6 @@ int ceph_subvolume_metrics_snapshot(struct ceph_subvolume_metrics_tracker *track
 	if (!READ_ONCE(tracker->enabled))
 		return 0;
 
-	atomic64_inc(&tracker->snapshot_attempts);
-
 	spin_lock(&tracker->lock);
 	for (node = rb_first_cached(&tracker->tree); node; node = rb_next(node)) {
 		struct ceph_subvol_metric_rb_entry *entry =
@@ -240,16 +224,12 @@ int ceph_subvolume_metrics_snapshot(struct ceph_subvolume_metrics_tracker *track
 	}
 	spin_unlock(&tracker->lock);
 
-	if (!count) {
-		atomic64_inc(&tracker->snapshot_empty);
+	if (!count)
 		return 0;
-	}
 
 	snap = kcalloc(count, sizeof(*snap), GFP_NOFS);
-	if (!snap) {
-		atomic64_inc(&tracker->snapshot_failures);
+	if (!snap)
 		return -ENOMEM;
-	}
 
 	spin_lock(&tracker->lock);
 	node = rb_first_cached(&tracker->tree);
@@ -380,18 +360,12 @@ void ceph_subvolume_metrics_record_io(struct ceph_mds_client *mdsc,
 		return;
 
 	tracker = &mdsc->subvol_metrics;
-	atomic64_inc(&tracker->record_calls);
-
-	if (!ceph_subvolume_metrics_enabled(tracker)) {
-		atomic64_inc(&tracker->record_disabled);
+	if (!ceph_subvolume_metrics_enabled(tracker))
 		return;
-	}
 
 	subvol_id = READ_ONCE(ci->i_subvolume_id);
-	if (subvol_id == CEPH_SUBVOLUME_ID_NONE) {
-		atomic64_inc(&tracker->record_no_subvol);
+	if (subvol_id == CEPH_SUBVOLUME_ID_NONE)
 		return;
-	}
 
 	delta_us = ktime_to_us(ktime_sub(end, start));
 	if (delta_us <= 0)
diff --git a/fs/ceph/subvolume_metrics.h b/fs/ceph/subvolume_metrics.h
index 6f53ff726c75..8b901ae05e00 100644
--- a/fs/ceph/subvolume_metrics.h
+++ b/fs/ceph/subvolume_metrics.h
@@ -6,7 +6,6 @@
 #include <linux/rbtree.h>
 #include <linux/spinlock.h>
 #include <linux/ktime.h>
-#include <linux/atomic.h>
 
 struct seq_file;
 struct ceph_mds_client;
@@ -38,32 +37,12 @@ struct ceph_subvol_metric_snapshot {
  * @tree: Red-black tree of per-subvolume entries, keyed by subvolume_id
  * @nr_entries: Number of entries currently in @tree
  * @enabled: Whether collection is enabled (requires MDS feature support)
- * @snapshot_attempts: Debug counter: total ceph_subvolume_metrics_snapshot() calls
- * @snapshot_empty: Debug counter: snapshots that found no data to report
- * @snapshot_failures: Debug counter: snapshots that failed to allocate memory
- * @record_calls: Debug counter: total ceph_subvolume_metrics_record() calls
- * @record_disabled: Debug counter: record calls skipped because disabled
- * @record_no_subvol: Debug counter: record calls skipped (no subvolume_id)
- * @total_read_ops: Cumulative read ops across all snapshots (never reset)
- * @total_read_bytes: Cumulative bytes read across all snapshots (never reset)
- * @total_write_ops: Cumulative write ops across all snapshots (never reset)
- * @total_write_bytes: Cumulative bytes written across all snapshots (never reset)
  */
 struct ceph_subvolume_metrics_tracker {
 	spinlock_t lock;
 	struct rb_root_cached tree;
 	u32 nr_entries;
 	bool enabled;
-	atomic64_t snapshot_attempts;
-	atomic64_t snapshot_empty;
-	atomic64_t snapshot_failures;
-	atomic64_t record_calls;
-	atomic64_t record_disabled;
-	atomic64_t record_no_subvol;
-	atomic64_t total_read_ops;
-	atomic64_t total_read_bytes;
-	atomic64_t total_write_ops;
-	atomic64_t total_write_bytes;
 };
 
 void ceph_subvolume_metrics_init(struct ceph_subvolume_metrics_tracker *tracker);

-- 
2.53.0



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 3/4] ceph: flush the OSD completions before destroying the mds client
  2026-10-19 11:33 [PATCH v2 0/4] ceph: fix the buffered read size metric and make the subvolume metrics per CPU Xiubo Li via B4 Relay
  2026-10-19 11:33 ` [PATCH v2 1/4] ceph: report the requested length in the netfs read metrics Xiubo Li via B4 Relay
  2026-10-19 11:33 ` [PATCH v2 2/4] ceph: drop the unused subvolume metrics debug counters Xiubo Li via B4 Relay
@ 2026-10-19 11:33 ` Xiubo Li via B4 Relay
  2026-10-19 11:33 ` [PATCH v2 4/4] ceph: accumulate subvolume metrics per CPU Xiubo Li via B4 Relay
  3 siblings, 0 replies; 5+ messages in thread
From: Xiubo Li via B4 Relay @ 2026-10-19 11:33 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Xiubo Li, Viacheslav Dubeyko
  Cc: ceph-devel, linux-kernel

From: Xiubo Li <xiubo.li@clyso.com>

The callbacks of OSD requests that complete with an error, or that are
aborted, are run from osdc->completion_wq instead of the messenger.
Like the ones run from the messenger, the read and write ones update the
client metrics in mdsc->metric and mdsc->subvol_metrics.

ceph_mdsc_destroy() only flushes the messenger before it frees those and
mdsc itself, while completion_wq keeps running until ceph_osdc_stop()
destroys it from ceph_destroy_client(), long after.  A callback still
queued there then updates freed memory.

Add ceph_osdc_flush_completions() to libceph and call it right after
ceph_msgr_flush() in ceph_mdsc_destroy().

Signed-off-by: Xiubo Li <xiubo.li@clyso.com>
---
 fs/ceph/mds_client.c            |  2 ++
 include/linux/ceph/osd_client.h |  1 +
 net/ceph/osd_client.c           | 11 +++++++++++
 3 files changed, 14 insertions(+)

diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
index d4ef8a355a48..b62291f0e351 100644
--- a/fs/ceph/mds_client.c
+++ b/fs/ceph/mds_client.c
@@ -7046,6 +7046,8 @@ void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
 
 	/* flush out any connection work with references to us */
 	ceph_msgr_flush();
+	/* and the OSD request callbacks that update our metrics */
+	ceph_osdc_flush_completions(&fsc->client->osdc);
 
 	/*
 	 * Mark reset as failed and wake any blocked waiters before
diff --git a/include/linux/ceph/osd_client.h b/include/linux/ceph/osd_client.h
index 52eb76e9d62a..6028754efe6a 100644
--- a/include/linux/ceph/osd_client.h
+++ b/include/linux/ceph/osd_client.h
@@ -585,6 +585,7 @@ extern int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
 extern void ceph_osdc_sync(struct ceph_osd_client *osdc);
 
 extern void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc);
+extern void ceph_osdc_flush_completions(struct ceph_osd_client *osdc);
 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc);
 
 int ceph_osdc_call(struct ceph_osd_client *osdc,
diff --git a/net/ceph/osd_client.c b/net/ceph/osd_client.c
index 5f2c561ff4e2..ec5e8870ec6f 100644
--- a/net/ceph/osd_client.c
+++ b/net/ceph/osd_client.c
@@ -5164,6 +5164,17 @@ void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
 }
 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
 
+/*
+ * Wait for the request callbacks queued on completion_wq to finish - for
+ * use before freeing what those callbacks touch
+ */
+void ceph_osdc_flush_completions(struct ceph_osd_client *osdc)
+{
+	dout("%s osdc %p\n", __func__, osdc);
+	flush_workqueue(osdc->completion_wq);
+}
+EXPORT_SYMBOL(ceph_osdc_flush_completions);
+
 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
 {
 	down_read(&osdc->lock);

-- 
2.53.0



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 4/4] ceph: accumulate subvolume metrics per CPU
  2026-10-19 11:33 [PATCH v2 0/4] ceph: fix the buffered read size metric and make the subvolume metrics per CPU Xiubo Li via B4 Relay
                   ` (2 preceding siblings ...)
  2026-10-19 11:33 ` [PATCH v2 3/4] ceph: flush the OSD completions before destroying the mds client Xiubo Li via B4 Relay
@ 2026-10-19 11:33 ` Xiubo Li via B4 Relay
  3 siblings, 0 replies; 5+ messages in thread
From: Xiubo Li via B4 Relay @ 2026-10-19 11:33 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Xiubo Li, Viacheslav Dubeyko
  Cc: ceph-devel, linux-kernel, Dan van der Ster

From: Xiubo Li <xiubo.li@clyso.com>

With subvolume metrics enabled, every OSD read and write completion
takes the mount-wide tracker->lock and walks the rbtree of subvolumes
to add one op to its entry.  On a client with many cores doing a
large bandwidth, the completions of all its CPUs serialize on that
spinlock and bounce its cache line.

Give each CPU a slot holding the counters of one subvolume.  A
completion only takes its own slot's lock and adds to it; it goes to
the tree only when the slot held another subvolume, whose counters it
then folds into the tree entry the old way.  The metrics sender and
the debugfs dump fold every slot into the tree before they read it,
so what they report is unchanged.  A client usually works in one
subvolume at a time, so the tree is touched once per CPU and snapshot
instead of once per I/O.

The slot is looked up with raw_cpu_ptr() and protected by its own
lock, so a task that migrates in between just updates another CPU's
slot.  A fold takes the slot's lock inside tracker->lock and clears the
slot only once the subvolume's entry is in the tree, so if that entry
cannot be allocated the counters stay in the slot for the next fold.
A completion for another subvolume then adds its single I/O to the tree
directly, which drops it only when that allocation fails as well, as
before.  ceph_subvolume_metrics_init() now allocates the per-CPU slots
and can fail.

Reported-by: Dan van der Ster <dan.vanderster@clyso.com>
Signed-off-by: Xiubo Li <xiubo.li@clyso.com>
---
 fs/ceph/mds_client.c        |   6 +-
 fs/ceph/subvolume_metrics.c | 243 ++++++++++++++++++++++++++++++++++----------
 fs/ceph/subvolume_metrics.h |   5 +-
 3 files changed, 199 insertions(+), 55 deletions(-)

diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
index b62291f0e351..fa6b57bb9120 100644
--- a/fs/ceph/mds_client.c
+++ b/fs/ceph/mds_client.c
@@ -6501,7 +6501,9 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
 	err = ceph_metric_init(&mdsc->metric);
 	if (err)
 		goto err_mdsmap;
-	ceph_subvolume_metrics_init(&mdsc->subvol_metrics);
+	err = ceph_subvolume_metrics_init(&mdsc->subvol_metrics);
+	if (err)
+		goto err_metric;
 	mutex_init(&mdsc->subvol_metrics_last_mutex);
 	mdsc->subvol_metrics_last = NULL;
 	mdsc->subvol_metrics_last_nr = 0;
@@ -6533,6 +6535,8 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
 	fsc->mdsc = mdsc;
 	return 0;
 
+err_metric:
+	ceph_metric_destroy(&mdsc->metric);
 err_mdsmap:
 	kfree(mdsc->mdsmap);
 err_mdsc:
diff --git a/fs/ceph/subvolume_metrics.c b/fs/ceph/subvolume_metrics.c
index 895878a49ea7..3c9479b5c14a 100644
--- a/fs/ceph/subvolume_metrics.c
+++ b/fs/ceph/subvolume_metrics.c
@@ -31,14 +31,36 @@ struct ceph_subvol_metric_rb_entry {
 	u64 write_latency_us;
 };
 
+/**
+ * struct ceph_subvol_metric_pcpu - Per-CPU accumulator in front of the tree
+ * @lock: Protects @m; nests inside tracker->lock
+ * @m: Counters of one subvolume (@m.subvolume_id) not yet folded into the tree
+ *
+ * I/O completions add to the slot of the CPU they run on and only take
+ * tracker->lock when the slot holds another subvolume, or when a snapshot
+ * or a dump folds every slot into the tree.
+ */
+struct ceph_subvol_metric_pcpu {
+	spinlock_t lock;
+	struct ceph_subvol_metric_snapshot m;
+};
+
 static struct kmem_cache *ceph_subvol_metric_entry_cachep;
 
-void ceph_subvolume_metrics_init(struct ceph_subvolume_metrics_tracker *tracker)
+int ceph_subvolume_metrics_init(struct ceph_subvolume_metrics_tracker *tracker)
 {
+	int cpu;
+
 	spin_lock_init(&tracker->lock);
 	tracker->tree = RB_ROOT_CACHED;
 	tracker->nr_entries = 0;
 	tracker->enabled = false;
+	tracker->pcpu = alloc_percpu(struct ceph_subvol_metric_pcpu);
+	if (!tracker->pcpu)
+		return -ENOMEM;
+	for_each_possible_cpu(cpu)
+		spin_lock_init(&per_cpu_ptr(tracker->pcpu, cpu)->lock);
+	return 0;
 }
 
 static struct ceph_subvol_metric_rb_entry *
@@ -109,12 +131,28 @@ static void ceph_subvolume_metrics_clear_locked(
 	tracker->tree = RB_ROOT_CACHED;
 }
 
+static void ceph_subvolume_metrics_clear_pcpu(
+		struct ceph_subvolume_metrics_tracker *tracker)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu) {
+		struct ceph_subvol_metric_pcpu *pc = per_cpu_ptr(tracker->pcpu, cpu);
+
+		spin_lock(&pc->lock);
+		memset(&pc->m, 0, sizeof(pc->m));
+		spin_unlock(&pc->lock);
+	}
+}
+
 void ceph_subvolume_metrics_destroy(struct ceph_subvolume_metrics_tracker *tracker)
 {
 	spin_lock(&tracker->lock);
 	ceph_subvolume_metrics_clear_locked(tracker);
 	tracker->enabled = false;
 	spin_unlock(&tracker->lock);
+	free_percpu(tracker->pcpu);
+	tracker->pcpu = NULL;
 }
 
 void ceph_subvolume_metrics_enable(struct ceph_subvolume_metrics_tracker *tracker,
@@ -128,74 +166,168 @@ void ceph_subvolume_metrics_enable(struct ceph_subvolume_metrics_tracker *tracke
 		ceph_subvolume_metrics_clear_locked(tracker);
 	}
 	spin_unlock(&tracker->lock);
+	if (!enable)
+		ceph_subvolume_metrics_clear_pcpu(tracker);
 }
 
-void ceph_subvolume_metrics_record(struct ceph_subvolume_metrics_tracker *tracker,
-				   u64 subvol_id, bool is_write,
-				   size_t size, u64 latency_us)
+/*
+ * Add counters to their subvolume's entry in the tree: those held by the
+ * per-CPU slot @pc if it is given, else those in @d.  The slot is read and
+ * cleared under tracker->lock only once its entry is in the tree, so when
+ * the entry cannot be allocated its counters stay in the slot for the next
+ * fold instead of being lost.
+ */
+static void ceph_subvolume_metrics_fold(struct ceph_subvolume_metrics_tracker *tracker,
+					struct ceph_subvol_metric_pcpu *pc,
+					const struct ceph_subvol_metric_snapshot *d)
 {
 	struct ceph_subvol_metric_rb_entry *entry, *new_entry = NULL;
-	bool retry = false;
+	struct ceph_subvol_metric_snapshot m;
 
-	/* CEPH_SUBVOLUME_ID_NONE (0) means unknown/unset subvolume */
-	if (!READ_ONCE(tracker->enabled) ||
-	    subvol_id == CEPH_SUBVOLUME_ID_NONE || !size || !latency_us)
-		return;
-
-	/*
-	 * Retry loop for lock-free allocation pattern:
-	 * 1. First iteration: lookup under lock, if miss -> drop lock, alloc, retry
-	 * 2. Second iteration: lookup again (may have been inserted), insert if still missing
-	 * 3. On race (another thread inserted same key): free our alloc, retry
-	 * All successful paths exit via return, so retry flag doesn't need reset.
-	 */
-	do {
+	for (;;) {
 		spin_lock(&tracker->lock);
+		/* disabling clears the tree and the slots */
 		if (!tracker->enabled) {
 			spin_unlock(&tracker->lock);
-			if (new_entry)
-				kmem_cache_free(ceph_subvol_metric_entry_cachep, new_entry);
-			return;
+			break;
 		}
 
-		entry = __lookup_entry(tracker, subvol_id);
-		if (!entry) {
-			if (!new_entry) {
-				spin_unlock(&tracker->lock);
-				new_entry = kmem_cache_zalloc(ceph_subvol_metric_entry_cachep,
-						      GFP_NOFS);
-				if (!new_entry)
-					return;
-				new_entry->subvolume_id = subvol_id;
-				retry = true;
-				continue;
-			}
-			entry = __insert_entry(tracker, new_entry);
-			if (entry != new_entry) {
-				/* raced with another insert */
-				spin_unlock(&tracker->lock);
-				kmem_cache_free(ceph_subvol_metric_entry_cachep, new_entry);
+		if (pc) {
+			spin_lock(&pc->lock);
+			m = pc->m;
+		} else {
+			m = *d;
+		}
+
+		entry = NULL;
+		if (m.read_ops || m.write_ops) {
+			entry = __lookup_entry(tracker, m.subvolume_id);
+			if (!entry && new_entry) {
+				new_entry->subvolume_id = m.subvolume_id;
+				entry = __insert_entry(tracker, new_entry);
 				new_entry = NULL;
-				retry = true;
-				continue;
 			}
-			new_entry = NULL;
+			if (entry) {
+				entry->read_ops += m.read_ops;
+				entry->read_bytes += m.read_bytes;
+				entry->read_latency_us += m.read_latency_us;
+				entry->write_ops += m.write_ops;
+				entry->write_bytes += m.write_bytes;
+				entry->write_latency_us += m.write_latency_us;
+				if (pc)
+					pc->m = (struct ceph_subvol_metric_snapshot) {
+						.subvolume_id = m.subvolume_id,
+					};
+			}
 		}
 
-		if (is_write) {
-			entry->write_ops++;
-			entry->write_bytes += size;
-			entry->write_latency_us += latency_us;
-		} else {
-			entry->read_ops++;
-			entry->read_bytes += size;
-			entry->read_latency_us += latency_us;
-		}
+		if (pc)
+			spin_unlock(&pc->lock);
 		spin_unlock(&tracker->lock);
-		if (new_entry)
-			kmem_cache_free(ceph_subvol_metric_entry_cachep, new_entry);
+
+		if (entry || (!m.read_ops && !m.write_ops))
+			break;
+
+		/* the key is set on insert, the slot may change meanwhile */
+		new_entry = kmem_cache_zalloc(ceph_subvol_metric_entry_cachep,
+					      GFP_NOFS);
+		if (!new_entry)
+			return;
+	}
+
+	if (new_entry)
+		kmem_cache_free(ceph_subvol_metric_entry_cachep, new_entry);
+}
+
+/* Move what every per-CPU slot holds into the tree. */
+static void ceph_subvolume_metrics_fold_pcpu(
+		struct ceph_subvolume_metrics_tracker *tracker)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu) {
+		struct ceph_subvol_metric_pcpu *pc = per_cpu_ptr(tracker->pcpu, cpu);
+
+		/* skip idle slots without taking tracker->lock */
+		if (!data_race(pc->m.read_ops) && !data_race(pc->m.write_ops))
+			continue;
+
+		/* on failure the slot keeps its counters for the next snapshot */
+		ceph_subvolume_metrics_fold(tracker, pc, NULL);
+	}
+}
+
+/*
+ * Make the slot hold @subvol_id, unless it still has counters of another
+ * subvolume.  Called with pc->lock held.
+ */
+static bool ceph_subvol_metric_pcpu_claim(struct ceph_subvol_metric_pcpu *pc,
+					  u64 subvol_id)
+{
+	if (likely(pc->m.subvolume_id == subvol_id))
+		return true;
+	if (pc->m.read_ops || pc->m.write_ops)
+		return false;
+	pc->m.subvolume_id = subvol_id;
+	return true;
+}
+
+void ceph_subvolume_metrics_record(struct ceph_subvolume_metrics_tracker *tracker,
+				   u64 subvol_id, bool is_write,
+				   size_t size, u64 latency_us)
+{
+	struct ceph_subvol_metric_pcpu *pc;
+
+	/* CEPH_SUBVOLUME_ID_NONE (0) means unknown/unset subvolume */
+	if (!READ_ONCE(tracker->enabled) ||
+	    subvol_id == CEPH_SUBVOLUME_ID_NONE || !size || !latency_us)
 		return;
-	} while (retry);
+
+	/*
+	 * The slot of the CPU we start on; if we migrate, the lock still
+	 * keeps the update consistent and it just lands in another slot.
+	 */
+	pc = raw_cpu_ptr(tracker->pcpu);
+	spin_lock(&pc->lock);
+	if (unlikely(!ceph_subvol_metric_pcpu_claim(pc, subvol_id))) {
+		/* the slot holds another subvolume: hand its counters to the tree */
+		spin_unlock(&pc->lock);
+		ceph_subvolume_metrics_fold(tracker, pc, NULL);
+		spin_lock(&pc->lock);
+		if (!ceph_subvol_metric_pcpu_claim(pc, subvol_id)) {
+			struct ceph_subvol_metric_snapshot d = {
+				.subvolume_id = subvol_id,
+			};
+
+			/*
+			 * The fold could not allocate an entry, so the slot
+			 * keeps the old subvolume; add this I/O to the tree
+			 * directly, which drops it only if that fails too.
+			 */
+			spin_unlock(&pc->lock);
+			if (is_write) {
+				d.write_ops = 1;
+				d.write_bytes = size;
+				d.write_latency_us = latency_us;
+			} else {
+				d.read_ops = 1;
+				d.read_bytes = size;
+				d.read_latency_us = latency_us;
+			}
+			ceph_subvolume_metrics_fold(tracker, NULL, &d);
+			return;
+		}
+	}
+	if (is_write) {
+		pc->m.write_ops++;
+		pc->m.write_bytes += size;
+		pc->m.write_latency_us += latency_us;
+	} else {
+		pc->m.read_ops++;
+		pc->m.read_bytes += size;
+		pc->m.read_latency_us += latency_us;
+	}
+	spin_unlock(&pc->lock);
 }
 
 int ceph_subvolume_metrics_snapshot(struct ceph_subvolume_metrics_tracker *tracker,
@@ -213,6 +345,8 @@ int ceph_subvolume_metrics_snapshot(struct ceph_subvolume_metrics_tracker *track
 	if (!READ_ONCE(tracker->enabled))
 		return 0;
 
+	ceph_subvolume_metrics_fold_pcpu(tracker);
+
 	spin_lock(&tracker->lock);
 	for (node = rb_first_cached(&tracker->tree); node; node = rb_next(node)) {
 		struct ceph_subvol_metric_rb_entry *entry =
@@ -306,6 +440,9 @@ void ceph_subvolume_metrics_dump(struct ceph_subvolume_metrics_tracker *tracker,
 	struct rb_node *node;
 	bool found = false;
 
+	if (READ_ONCE(tracker->enabled))
+		ceph_subvolume_metrics_fold_pcpu(tracker);
+
 	spin_lock(&tracker->lock);
 	if (!tracker->enabled) {
 		spin_unlock(&tracker->lock);
diff --git a/fs/ceph/subvolume_metrics.h b/fs/ceph/subvolume_metrics.h
index 8b901ae05e00..b29d48e9a4f1 100644
--- a/fs/ceph/subvolume_metrics.h
+++ b/fs/ceph/subvolume_metrics.h
@@ -10,6 +10,7 @@
 struct seq_file;
 struct ceph_mds_client;
 struct ceph_inode_info;
+struct ceph_subvol_metric_pcpu;
 
 /**
  * struct ceph_subvol_metric_snapshot - Point-in-time snapshot of subvolume metrics
@@ -37,15 +38,17 @@ struct ceph_subvol_metric_snapshot {
  * @tree: Red-black tree of per-subvolume entries, keyed by subvolume_id
  * @nr_entries: Number of entries currently in @tree
  * @enabled: Whether collection is enabled (requires MDS feature support)
+ * @pcpu: Per-CPU accumulators that I/O completions update without @lock
  */
 struct ceph_subvolume_metrics_tracker {
 	spinlock_t lock;
 	struct rb_root_cached tree;
 	u32 nr_entries;
 	bool enabled;
+	struct ceph_subvol_metric_pcpu __percpu *pcpu;
 };
 
-void ceph_subvolume_metrics_init(struct ceph_subvolume_metrics_tracker *tracker);
+int ceph_subvolume_metrics_init(struct ceph_subvolume_metrics_tracker *tracker);
 void ceph_subvolume_metrics_destroy(struct ceph_subvolume_metrics_tracker *tracker);
 void ceph_subvolume_metrics_enable(struct ceph_subvolume_metrics_tracker *tracker,
 				   bool enable);

-- 
2.53.0



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-10-19 11:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-19 11:33 [PATCH v2 0/4] ceph: fix the buffered read size metric and make the subvolume metrics per CPU Xiubo Li via B4 Relay
2026-10-19 11:33 ` [PATCH v2 1/4] ceph: report the requested length in the netfs read metrics Xiubo Li via B4 Relay
2026-10-19 11:33 ` [PATCH v2 2/4] ceph: drop the unused subvolume metrics debug counters Xiubo Li via B4 Relay
2026-10-19 11:33 ` [PATCH v2 3/4] ceph: flush the OSD completions before destroying the mds client Xiubo Li via B4 Relay
2026-10-19 11:33 ` [PATCH v2 4/4] ceph: accumulate subvolume metrics per CPU Xiubo Li via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®