Skip to contentSkip to conversation

cgroup: adjust criteria for rstat subsystem cpu lock access

PATCH Applied JP Kobryn 6 participants 10 messages May 28, 2025 – Jun 3, 2025 cgroup

lore
Share & tools
lore

JP Kobryn

author
#0 lore raw

PATCH 1/1 cgroup: adjust criteria for rstat subsystem cpu lock access

Previously it was found that on uniprocessor machines the size of raw_spinlock_t could be zero so a pre-processor conditional was used to avoid the allocation of ss->rstat_ss_cpu_lock. The conditional did not take into account cases where lock debugging features were enabled. Cover these cases along with the original non-smp case by explicitly using the size of size of the lock type as criteria for allocation/access where applicable.

Fixes 748922dcfabd "cgroup: use subsystem-specific rstat locks to avoid contention"
---
 kernel/cgroup/rstat.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
kernel/cgroup/rstat.c +16 -9
index ce4752ab9e09..cbeaa499a96a 100644
@@ -47,8 +47,20 @@ static spinlock_t *ss_rstat_lock(struct cgroup_subsys *ss)
 
 static raw_spinlock_t *ss_rstat_cpu_lock(struct cgroup_subsys *ss, int cpu)
 {
-	if (ss)
+	if (ss) {
+		/*
+		 * Depending on config, the subsystem per-cpu lock type may be an
+		 * empty struct. In enviromnents where this is the case, allocation
+		 * of this field is not performed in ss_rstat_init(). Avoid a
+		 * cpu-based offset relative to NULL by returning early. When the
+		 * lock type is zero in size, the corresponding lock functions are
+		 * no-ops so passing them NULL is acceptable.
+		 */
+		if (sizeof(*ss->rstat_ss_cpu_lock) == 0)
+			return NULL;
+
 		return per_cpu_ptr(ss->rstat_ss_cpu_lock, cpu);
+	}
 
 	return per_cpu_ptr(&rstat_base_cpu_lock, cpu);
 }
@@ -510,20 +522,15 @@ int __init ss_rstat_init(struct cgroup_subsys *ss)
 {
 	int cpu;
 
-#ifdef CONFIG_SMP
 	/*
-	 * On uniprocessor machines, arch_spinlock_t is defined as an empty
-	 * struct. Avoid allocating a size of zero by having this block
-	 * excluded in this case. It's acceptable to leave the subsystem locks
-	 * unitialized since the associated lock functions are no-ops in the
-	 * non-smp case.
+	 * Depending on config, the subsystem per-cpu lock type may be an empty
+	 * struct. Avoid allocating a size of zero in this case.
 	 */
-	if (ss) {
+	if (ss && sizeof(*ss->rstat_ss_cpu_lock)) {
 		ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
 		if (!ss->rstat_ss_cpu_lock)
 			return -ENOMEM;
 	}
-#endif
 
 	spin_lock_init(ss_rstat_lock(ss));
 	for_each_possible_cpu(cpu)
Signature · 1 line
2.47.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · JP Kobryn

Previously it was found that on uniprocessor machines the size of raw_spinlock_t could be zero so a pre-processor conditional was used to avoid the allocation of ss->rstat_ss_cpu_lock. The conditional did not take into account cases where lock debugging features were enabled. Cover these cases along with the original non-smp case by explicitly using the size of size of the lock type as criteria for allocation/access where applicable.

Fixes 748922dcfabd "cgroup: use subsystem-specific rstat locks to avoid contention"
#2 lore raw
In reply to JP Kobryn
Quoted 9 lines · JP Kobryn

Previously it was found that on uniprocessor machines the size of raw_spinlock_t could be zero so a pre-processor conditional was used to avoid the allocation of ss->rstat_ss_cpu_lock. The conditional did not take into account cases where lock debugging features were enabled. Cover these cases along with the original non-smp case by explicitly using the size of size of the lock type as criteria for allocation/access where applicable.

Fixes 748922dcfabd "cgroup: use subsystem-specific rstat locks to avoid contention"

Should the commit to be fixed 731bdd97466a ("cgroup: avoid per-cpu allocation of size zero rstat cpu locks")?

Other than that, the patch looks good to me.

Cheers,
Longman

Quoted 56 lines
---
  kernel/cgroup/rstat.c | 25 ++++++++++++++++---------
  1 file changed, 16 insertions(+), 9 deletions(-)
kernel/cgroup/rstat.c +16 -9
index ce4752ab9e09..cbeaa499a96a 100644
@@ -47,8 +47,20 @@ static spinlock_t *ss_rstat_lock(struct cgroup_subsys *ss)
 
  static raw_spinlock_t *ss_rstat_cpu_lock(struct cgroup_subsys *ss, int cpu)
  {
-	if (ss)
+	if (ss) {
+		/*
+		 * Depending on config, the subsystem per-cpu lock type may be an
+		 * empty struct. In enviromnents where this is the case, allocation
+		 * of this field is not performed in ss_rstat_init(). Avoid a
+		 * cpu-based offset relative to NULL by returning early. When the
+		 * lock type is zero in size, the corresponding lock functions are
+		 * no-ops so passing them NULL is acceptable.
+		 */
+		if (sizeof(*ss->rstat_ss_cpu_lock) == 0)
+			return NULL;
+
  		return per_cpu_ptr(ss->rstat_ss_cpu_lock, cpu);
+	}
 
  	return per_cpu_ptr(&rstat_base_cpu_lock, cpu);
  }
@@ -510,20 +522,15 @@ int __init ss_rstat_init(struct cgroup_subsys *ss)
  {
  	int cpu;
 
-#ifdef CONFIG_SMP
  	/*
-	 * On uniprocessor machines, arch_spinlock_t is defined as an empty
-	 * struct. Avoid allocating a size of zero by having this block
-	 * excluded in this case. It's acceptable to leave the subsystem locks
-	 * unitialized since the associated lock functions are no-ops in the
-	 * non-smp case.
+	 * Depending on config, the subsystem per-cpu lock type may be an empty
+	 * struct. Avoid allocating a size of zero in this case.
  	 */
-	if (ss) {
+	if (ss && sizeof(*ss->rstat_ss_cpu_lock)) {
  		ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
  		if (!ss->rstat_ss_cpu_lock)
  			return -ENOMEM;
  	}
-#endif
 
  	spin_lock_init(ss_rstat_lock(ss));
  	for_each_possible_cpu(cpu)
author
#3 lore raw
Quoted 8 lines · Waiman Long
Quoted 7 lines · JP Kobryn

Previously it was found that on uniprocessor machines the size of raw_spinlock_t could be zero so a pre-processor conditional was used to avoid the allocation of ss->rstat_ss_cpu_lock. The conditional did not take into account cases where lock debugging features were enabled. Cover these cases along with the original non-smp case by explicitly using the size of size of the lock type as criteria for allocation/access where applicable.

Quoted 4 lines
Quoted 2 lines
Fixes 748922dcfabd "cgroup: use subsystem-specific rstat locks to avoid contention"

Should the commit to be fixed 731bdd97466a ("cgroup: avoid per-cpu allocation of size zero rstat cpu locks")?

I chose 748922dcfabd because that is where the underlying issue originated from, and 731bdd97466a was only a partial fix.

Quoted 2 lines

Other than that, the patch looks good to me.

Thanks for taking a look.

Reviewed-by
#4 lore raw
Quoted 9 lines · JP Kobryn
Quoted 8 lines · Waiman Long
Quoted 7 lines · JP Kobryn

Previously it was found that on uniprocessor machines the size of raw_spinlock_t could be zero so a pre-processor conditional was used to avoid the allocation of ss->rstat_ss_cpu_lock. The conditional did not take into account cases where lock debugging features were enabled. Cover these cases along with the original non-smp case by explicitly using the size of size of the lock type as criteria for allocation/access where applicable.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Fixes 748922dcfabd "cgroup: use subsystem-specific rstat locks to avoid contention"

Should the commit to be fixed 731bdd97466a ("cgroup: avoid per-cpu allocation of size zero rstat cpu locks")?

I chose 748922dcfabd because that is where the underlying issue originated from, and 731bdd97466a was only a partial fix.

OK

For the patch,

Quoted 5 lines
Quoted 2 lines

Other than that, the patch looks good to me.

Thanks for taking a look.

maintainer
#5 lore raw
In reply to JP Kobryn
Quoted 5 lines · JP Kobryn
kernel/cgroup/rstat.c +1 -1
-	if (ss) {
+	if (ss && sizeof(*ss->rstat_ss_cpu_lock)) {
 		ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
 		if (!ss->rstat_ss_cpu_lock)
 			return -ENOMEM;

<snip>

kernel/cgroup/rstat.c static spinlock_t *ss_rstat_lock(struct cgroup_subsys *ss) · line 57

Quoted 3 lines
kernel/cgroup/rstat.c +3 -0
+		 *                                                       When the
+		 * lock type is zero in size, the corresponding lock functions are
+		 * no-ops so passing them NULL is acceptable.

In the ideal world this consumer code (cgroup) shouldn't care about this at all, no?

I.e. this
ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
should work transparently on !CONFIG_SMP (return NULL or some special value) and the locking functions would be specialized for this value properly !CONFIG_SMP (no-ops as you write).

Or is my proposition not so ideal?

Thanks,
Michal

author
#6 lore raw
Quoted 7 lines · Michal Koutný
Quoted 5 lines · JP Kobryn
kernel/cgroup/rstat.c +1 -1
-	if (ss) {
+	if (ss && sizeof(*ss->rstat_ss_cpu_lock)) {
  		ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
  		if (!ss->rstat_ss_cpu_lock)
  			return -ENOMEM;

<snip>

Quoted 5 lines

kernel/cgroup/rstat.c static spinlock_t *ss_rstat_lock(struct cgroup_subsys *ss) · line 57

Quoted 3 lines
kernel/cgroup/rstat.c +3 -0
+		 *                                                       When the
+		 * lock type is zero in size, the corresponding lock functions are
+		 * no-ops so passing them NULL is acceptable.

In the ideal world this consumer code (cgroup) shouldn't care about this at all, no?

I feel the same way. It seems this pattern can be found in a few other places [0][1]:

Quoted 10 lines
         if (!tlocks)
             return -ENOMEM;

I.e. this
ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
should work transparently on !CONFIG_SMP (return NULL or some special value) and the locking functions would be specialized for this value properly !CONFIG_SMP (no-ops as you write).

Or is my proposition not so ideal?

I think an issue would be how to distinguish between receiving NULL as a result of allocating size zero vs being out of memory. If returning some special value like an error pointer, any related non-NULL checks in the code base would have to be adjusted.

I don't think the locking functions would have to change though. All I see is a barrier in the macros like this [2]:

# define arch_spin_lock(lock)       do { barrier(); (void)(lock); } while (0)

[0] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/bucket_locks.c?h=v6.15#n33
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/page_alloc.c?h=v6.15#n5830
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/spinlock_up.h?h=v6.15#n64

maintainer
#7 lore raw
In reply to Michal Koutný

Hello,

Quoted 4 lines · Michal Koutný

ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
should work transparently on !CONFIG_SMP (return NULL or some special value) and the locking functions would be specialized for this value properly !CONFIG_SMP (no-ops as you write).

alloc_percpu() triggers warn when called with zero size, so we at least need something guarding that. I applied as-is for now but further cleanups are welcome.

Thanks.

Signature · 1 line
tejun
maintainer Applied
#8 lore raw
In reply to JP Kobryn
Quoted 11 lines · JP Kobryn

Previously it was found that on uniprocessor machines the size of raw_spinlock_t could be zero so a pre-processor conditional was used to avoid the allocation of ss->rstat_ss_cpu_lock. The conditional did not take into account cases where lock debugging features were enabled. Cover these cases along with the original non-smp case by explicitly using the size of size of the lock type as criteria for allocation/access where applicable.

Fixes 748922dcfabd "cgroup: use subsystem-specific rstat locks to avoid contention"

Applied to cgroup/for-6.16-fixes.

Thanks.

Signature · 1 line
tejun
Tested-by on v1 1/1
#9 lore raw
In reply to JP Kobryn
Quoted 11 lines · JP Kobryn

Previously it was found that on uniprocessor machines the size of raw_spinlock_t could be zero so a pre-processor conditional was used to avoid the allocation of ss->rstat_ss_cpu_lock. The conditional did not take into account cases where lock debugging features were enabled. Cover these cases along with the original non-smp case by explicitly using the size of size of the lock type as criteria for allocation/access where applicable.

Fixes 748922dcfabd "cgroup: use subsystem-specific rstat locks to avoid contention"
Quoted 57 lines
---
 kernel/cgroup/rstat.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
kernel/cgroup/rstat.c +16 -9
index ce4752ab9e09..cbeaa499a96a 100644
@@ -47,8 +47,20 @@ static spinlock_t *ss_rstat_lock(struct cgroup_subsys *ss)
 
 static raw_spinlock_t *ss_rstat_cpu_lock(struct cgroup_subsys *ss, int cpu)
 {
-	if (ss)
+	if (ss) {
+		/*
+		 * Depending on config, the subsystem per-cpu lock type may be an
+		 * empty struct. In enviromnents where this is the case, allocation
+		 * of this field is not performed in ss_rstat_init(). Avoid a
+		 * cpu-based offset relative to NULL by returning early. When the
+		 * lock type is zero in size, the corresponding lock functions are
+		 * no-ops so passing them NULL is acceptable.
+		 */
+		if (sizeof(*ss->rstat_ss_cpu_lock) == 0)
+			return NULL;
+
 		return per_cpu_ptr(ss->rstat_ss_cpu_lock, cpu);
+	}
 
 	return per_cpu_ptr(&rstat_base_cpu_lock, cpu);
 }
@@ -510,20 +522,15 @@ int __init ss_rstat_init(struct cgroup_subsys *ss)
 {
 	int cpu;
 
-#ifdef CONFIG_SMP
 	/*
-	 * On uniprocessor machines, arch_spinlock_t is defined as an empty
-	 * struct. Avoid allocating a size of zero by having this block
-	 * excluded in this case. It's acceptable to leave the subsystem locks
-	 * unitialized since the associated lock functions are no-ops in the
-	 * non-smp case.
+	 * Depending on config, the subsystem per-cpu lock type may be an empty
+	 * struct. Avoid allocating a size of zero in this case.
 	 */
-	if (ss) {
+	if (ss && sizeof(*ss->rstat_ss_cpu_lock)) {
 		ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
 		if (!ss->rstat_ss_cpu_lock)
 			return -ENOMEM;
 	}
-#endif
 
 	spin_lock_init(ss_rstat_lock(ss));
 	for_each_possible_cpu(cpu)
Signature · 1 line
2.47.1