Skip to contentSkip to conversation

cgroup/rstat: convert rstat lock from spinlock to rwlock

RFC PATCH In review Thomas Falcon 3 participants 4 messages May 19 – May 22

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 cgroup/rstat: convert rstat lock from spinlock to rwlock

Implement rstat_ss_lock and rstat_base_lock as read-write locks instead of spinlocks. In addition, update tracing to reflect new locking implementation.

The benchmark below, meant to simulate a workload performing many concurrent cgroup cpu.stat reads, completes in 134 seconds on an Intel Xeon Platinum 8568Y with 144 cpus compared to 241 seconds when using spinlocks.

---
Initially discussed here: https://lore.kernel.org/cgroups/[email protected]/
Benchmark: https://gist.github.com/bwlodarcz/c955b36b5667f0167dffcff23953d1da
musl-gcc -o benchmark -static -g3 -DNUM_THREADS=10 -DNUM_ITER=10000 -O2 -Wall benchmark.c -pthread
---
include/linux/cgroup-defs.h | 2 +-

 include/trace/events/cgroup.h | 22 ++++++++--------
 kernel/cgroup/rstat.c         | 47 ++++++++++++++++++++++-------------
 3 files changed, 43 insertions(+), 28 deletions(-)
include/linux/cgroup-defs.h +1 -1
index 50a784da7a81..8609d6f6b29f 100644
@@ -861,7 +861,7 @@ struct cgroup_subsys {
 	 */
 	unsigned int depends_on;
 
-	spinlock_t rstat_ss_lock;
+	rwlock_t rstat_ss_lock;
 	struct llist_head __percpu *lhead; /* lockless update list head */
 };
 
include/trace/events/cgroup.h +12 -10
index b736da06340a..1bd80a08c116 100644
@@ -206,15 +206,16 @@ DEFINE_EVENT(cgroup_event, cgroup_notify_frozen,
 
 DECLARE_EVENT_CLASS(cgroup_rstat,
 
-	TP_PROTO(struct cgroup *cgrp, int cpu, bool contended),
+	TP_PROTO(struct cgroup *cgrp, int cpu, const char *type, bool contended),
 
-	TP_ARGS(cgrp, cpu, contended),
+	TP_ARGS(cgrp, cpu, type, contended),
 
 	TP_STRUCT__entry(
 		__field(	int,		root			)
 		__field(	int,		level			)
 		__field(	u64,		id			)
 		__field(	int,		cpu			)
+		__string(	type,		type			)
 		__field(	bool,		contended		)
 	),
 
@@ -223,12 +224,13 @@ DECLARE_EVENT_CLASS(cgroup_rstat,
 		__entry->id = cgroup_id(cgrp);
 		__entry->level = cgrp->level;
 		__entry->cpu = cpu;
+		__assign_str(type);
 		__entry->contended = contended;
 	),
 
-	TP_printk("root=%d id=%llu level=%d cpu=%d lock contended:%d",
+	TP_printk("root=%d id=%llu level=%d cpu=%d lock-type=%s lock contended:%d",
 		  __entry->root, __entry->id, __entry->level,
-		  __entry->cpu, __entry->contended)
+		  __entry->cpu, __get_str(type), __entry->contended)
 );
 
 /*
@@ -238,23 +240,23 @@ DECLARE_EVENT_CLASS(cgroup_rstat,
  */
 DEFINE_EVENT(cgroup_rstat, cgroup_rstat_lock_contended,
 
-	TP_PROTO(struct cgroup *cgrp, int cpu, bool contended),
+	TP_PROTO(struct cgroup *cgrp, int cpu, const char *type, bool contended),
 
-	TP_ARGS(cgrp, cpu, contended)
+	TP_ARGS(cgrp, cpu, type, contended)
 );
 
 DEFINE_EVENT(cgroup_rstat, cgroup_rstat_locked,
 
-	TP_PROTO(struct cgroup *cgrp, int cpu, bool contended),
+	TP_PROTO(struct cgroup *cgrp, int cpu, const char *type, bool contended),
 
-	TP_ARGS(cgrp, cpu, contended)
+	TP_ARGS(cgrp, cpu, type, contended)
 );
 
 DEFINE_EVENT(cgroup_rstat, cgroup_rstat_unlock,
 
-	TP_PROTO(struct cgroup *cgrp, int cpu, bool contended),
+	TP_PROTO(struct cgroup *cgrp, int cpu, const char *type, bool contended),
 
-	TP_ARGS(cgrp, cpu, contended)
+	TP_ARGS(cgrp, cpu, type, contended)
 );
 
 #endif /* _TRACE_CGROUP_H */
kernel/cgroup/rstat.c +30 -17

108 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 8 lines · Thomas Falcon

Implement rstat_ss_lock and rstat_base_lock as read-write locks instead of spinlocks. In addition, update tracing to reflect new locking implementation.

The benchmark below, meant to simulate a workload performing many concurrent cgroup cpu.stat reads, completes in 134 seconds on an Intel Xeon Platinum 8568Y with 144 cpus compared to 241 seconds when using spinlocks.

Can you try using seqlock for readers? That'd be decouple readers a lot better than rwlocks.

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 11 lines · Tejun Heo
Quoted 9 lines · Thomas Falcon

Implement rstat_ss_lock and rstat_base_lock as read-write locks instead of spinlocks. In addition, update tracing to reflect new locking implementation.

The benchmark below, meant to simulate a workload performing many concurrent cgroup cpu.stat reads, completes in 134 seconds on an Intel
Xeon Platinum 8568Y with 144 cpus compared to 241 seconds when using spinlocks.

Quoted 6 lines

Can you try using seqlock for readers? That'd be decouple readers a lot
better than rwlocks.

Thanks.

Hi, thanks for the quick response. I will try that.

Thanks,
Tom

maintainer
#3 lore raw
In reply to Thomas Falcon

Hi Thomas.

Quoted 21 lines · Thomas Falcon
kernel/cgroup/rstat.c +2 -2
@@ -414,7 +427,7 @@ __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css)
 		struct cgroup_subsys_state *pos;
 
 		/* Reacquire for each CPU to avoid disabling IRQs too long */
-		__css_rstat_lock(css, cpu);
+		__css_rstat_lock(css, cpu, true);
 		pos = css_rstat_updated_list(css, cpu);
 		for (; pos; pos = pos->rstat_flush_next) {
 			if (is_self) {
@@ -424,7 +437,7 @@ __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css)
 			} else
 				pos->ss->css_rstat_flush(pos, cpu);
 		}
-		__css_rstat_unlock(css, cpu);
+		__css_rstat_unlock(css, cpu, true);
 		if (!cond_resched())
 			cpu_relax();
 	}

kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 737

Quoted 10 lines
kernel/cgroup/rstat.c +2 -2
@@ -717,11 +730,11 @@ void cgroup_base_stat_cputime_show(struct seq_file *seq)
 
 	if (cgroup_parent(cgrp)) {
 		css_rstat_flush(&cgrp->self);
-		__css_rstat_lock(&cgrp->self, -1);
+		__css_rstat_lock(&cgrp->self, -1, false);
 		bstat = cgrp->bstat;
 		cputime_adjust(&cgrp->bstat.cputime, &cgrp->prev_cputime,
 			       &bstat.cputime.utime, &bstat.cputime.stime);
-		__css_rstat_unlock(&cgrp->self, -1);
+		__css_rstat_unlock(&cgrp->self, -1, false);

I was wondering where these distinctions of readers vs writers stem from and here I see that it's mainly the per-subsys vs rstat_base_lock. Given that cputime_adjust() is here only modifying the local bstat value, the read-like lock makes sense.

However, there's still the cgroup's flush right above which would take the per-subsys locks in write-mode anyway.
Can you add some more explanation why this works?

More generally, I'm wondering where are the opportunities for replacing per-subsys lock with an RW lock (or seqcount).

Thanks for looking into cpu.stat scalability,
Michal