cgroup: replace global percpu_rwsem with signal_struct->group_rwsem when writing cgroup.procs/threads
Subsystems (4)
PATCH 1/1 cgroup: replace global percpu_rwsem with signal_struct->group_rwsem when writing cgroup.procs/threads
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on signal_struct->group_rwsem when writing pid to cgroup.procs/threads instead of holding a global write lock.
Cgroup process migration has historically relied on signal_struct->group_rwsem to protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem"), this was changed to a global cgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.
The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------
cgroup_lock() | down_read(&g_rwsem) | cgroup_lock() down_write(&p_rwsem) | down_read(&p_rwsem) | down_write(&g_rwsem) critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) | up_write(&g_rwsem) cgroup_unlock() | up_read(&g_rwsem) | cgroup_unlock()
g_rwsem denotes cgroup_threadgroup_rwsem, p_rwsem denotes signal_struct->group_rwsem.
This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
--- include/linux/cgroup-defs.h | 2 ++ include/linux/sched/signal.h | 4 +++ init/init_task.c | 3 ++ kernel/cgroup/cgroup-internal.h | 6 ++-- kernel/cgroup/cgroup-v1.c | 8 ++--- kernel/cgroup/cgroup.c | 56 ++++++++++++++++++++------------- kernel/fork.c | 4 +++ 7 files changed, 55 insertions(+), 28 deletions(-)
include/linux/cgroup-defs.h +2 -0
@@ -838,6 +838,7 @@ struct cgroup_of_peak {static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +down_read (&tsk->signal->group_rwsem); }/**
@@ -848,6 +849,7 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +up_read (&tsk->signal->group_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem); }
include/linux/sched/signal.h +4 -0
@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore group_rwsem; +#endif +/* * Thread is the potential origin of an oom condition; kill first on * oom
init/init_task.c +3 -0
@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim =INIT_RLIMITS , +#ifdef CONFIG_CGROUPS + .group_rwsem = __RWSEM_INITIALIZER(init_signals.group_rwsem), +#endif .cred_guard_mutex =__MUTEX_INITIALIZER (init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERS
kernel/cgroup/cgroup-internal.h +4 -2
@@ -249,8 +249,10 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup, +bool global); +void cgroup_attach_unlock (struct task_struct *tsk,bool lock_threadgroup, +bool global);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup,bool *locked) __acquires(&cgroup_threadgroup_rwsem);
kernel/cgroup/cgroup-v1.c +4 -4
@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (NULL , true, true);for_each_root (root) {struct cgroup *from_cgrp;
@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (true); +cgroup_attach_unlock (NULL , true, true);cgroup_unlock ();return retval;
@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (NULL , true, true);/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);
@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (true); +cgroup_attach_unlock (NULL , true, true);cgroup_unlock ();return ret; }
kernel/cgroup/cgroup.c +34 -22
113 lines not on this page · raw message
kernel/fork.c +4 -0
@@ -1688,6 +1688,10 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)tty_audit_fork (sig);sched_autogroup_fork (sig); +#ifdef CONFIG_CGROUPS +init_rwsem (&sig->group_rwsem); +#endif + sig->oom_score_adj = current->signal->oom_score_adj; sig->oom_score_adj_min = current->signal->oom_score_adj_min;
Signature · 1 line
2.32.0.3.g01195cf9f
Quoted 32 lines · Yi Tao
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on
signal_struct->group_rwsemwhen writing pid to cgroup.procs/threads instead of holding a global write lock.Cgroup process migration has historically relied on
signal_struct->group_rwsemto protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem"), this was changed to a globalcgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------cgroup_lock() | down_read(&g_rwsem) | cgroup_lock() down_write(&p_rwsem) | down_read(&p_rwsem) | down_write(&g_rwsem) critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) | up_write(&g_rwsem) cgroup_unlock() | up_read(&g_rwsem) | cgroup_unlock()
Quoted 7 lines
g_rwsemdenotescgroup_threadgroup_rwsem,p_rwsemdenotessignal_struct->group_rwsem.This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
Do you have any performance numbers to showcase how much performance benefit does this change provide?
Quoted 129 lines
Signed-off-by Yi Tao <[email protected]>--- include/linux/cgroup-defs.h | 2 ++ include/linux/sched/signal.h | 4 +++ init/init_task.c | 3 ++ kernel/cgroup/cgroup-internal.h | 6 ++-- kernel/cgroup/cgroup-v1.c | 8 ++--- kernel/cgroup/cgroup.c | 56 ++++++++++++++++++++------------- kernel/fork.c | 4 +++ 7 files changed, 55 insertions(+), 28 deletions(-)include/linux/cgroup-defs.h +2
-0@@ -838,6 +838,7 @@ struct cgroup_of_peak {static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +down_read (&tsk->signal->group_rwsem); }/** @@ -848,6 +849,7 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +up_read (&tsk->signal->group_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem); }include/linux/sched/signal.h +4
-0@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore group_rwsem; +#endif +/* * Thread is the potential origin of an oom condition; kill first on * oom init/init_task.c +3
-0@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim =INIT_RLIMITS , +#ifdef CONFIG_CGROUPS + .group_rwsem = __RWSEM_INITIALIZER(init_signals.group_rwsem), +#endif .cred_guard_mutex =__MUTEX_INITIALIZER (init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERSkernel/cgroup/cgroup-internal.h +4
-2@@ -249,8 +249,10 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup, +bool global); +void cgroup_attach_unlock (struct task_struct *tsk,bool lock_threadgroup, +bool global);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup,bool *locked) __acquires(&cgroup_threadgroup_rwsem);kernel/cgroup/cgroup-v1.c +4
-4@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (NULL , true, true);for_each_root (root) {struct cgroup *from_cgrp;@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (true); +cgroup_attach_unlock (NULL , true, true);cgroup_unlock ();return retval;@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (NULL , true, true);/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (true); +cgroup_attach_unlock (NULL , true, true);cgroup_unlock ();return ret; }kernel/cgroup/cgroup.c +2
-1@@ -2480,21 +2480,31 @@ EXPORT_SYMBOL_GPL(cgroup_path_ns);* write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that * CPU hotplug is disabled on entry. */ -void cgroup_attach_lock (bool lock_threadgroup) +void cgroup_attach_lock (struct task_struct *tsk, +bool lock_threadgroup,bool global) {
kernel/cgroup/cgroup.c EXPORT_SYMBOL_GPL(cgroup_path_ns); · line 2493
Quoted 10 lines
kernel/cgroup/cgroup.c +6
-2cpus_read_lock (); -if (lock_threadgroup) -percpu_down_write (&cgroup_threadgroup_rwsem); +if (lock_threadgroup) { +if (global) +percpu_down_write (&cgroup_threadgroup_rwsem); +else +down_write (&tsk->signal->group_rwsem); + } }
tsk is only used when global is false. So you can eliminate the redundant global argument and use the presence of tsk to indicate which lock to take.
Cheers,
Longman
Quoted 9 lines · Waiman Long
Quoted 7 lines · Yi Tao
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
We are facing the same issue.
Quoted 25 lines
Quoted 25 lines
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on
signal_struct->group_rwsemwhen writing pid to cgroup.procs/threads instead of holding a global write lock.Cgroup process migration has historically relied on
signal_struct->group_rwsemto protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem"), this was changed to a globalcgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------
cgroup_lock() | down_read(&g_rwsem) |cgroup_lock()down_write(&p_rwsem) | down_read(&p_rwsem) |down_write(&g_rwsem)critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) |up_write(&g_rwsem)cgroup_unlock() | up_read(&g_rwsem) |cgroup_unlock()
Quoted 8 lines
Quoted 7 lines
g_rwsemdenotescgroup_threadgroup_rwsem,p_rwsemdenotessignal_struct->group_rwsem.This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
Do you have any performance numbers to showcase how much performance benefit does this change provide?
Yes, I would like this performance data as well.
Quoted 136 lines
Quoted 119 lines
Signed-off-by Yi Tao <[email protected]>---
include/linux/cgroup-defs.h | 2 ++ include/linux/sched/signal.h | 4 +++
init/init_task.c | 3 ++kernel/cgroup/cgroup-internal.h| 6 ++-- kernel/cgroup/cgroup-v1.c | 8 ++--- kernel/cgroup/cgroup.c | 56 ++++++++++++++++++++------------- kernel/fork.c | 4 +++7 files changed, 55 insertions(+), 28 deletions(-)include/linux/cgroup-defs.h +2
-0@@ -838,6 +838,7 @@ struct cgroup_of_peak { static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) { percpu_down_read(&cgroup_threadgroup_rwsem); + down_read(&tsk->signal->group_rwsem); } /**@@ -848,6 +849,7 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) { + up_read(&tsk->signal->group_rwsem); percpu_up_read(&cgroup_threadgroup_rwsem); }diff --git
a/include/linux/sched/signal.hb/include/linux/sched/signal.hindex1ef1edbaaf79..86fbc99a9174100644include/linux/sched/signal.h +0
-0@@ -226,6 +226,10 @@ struct signal_struct { struct tty_audit_buf *tty_audit_buf; #endif+#ifdef
CONFIG_CGROUPS
+ structrw_semaphoregroup_rwsem;include/linux/sched/signal.h +2
-0+#endif +/*
* Thread is the potential origin of an oom condition; kill first on
* oominit/init_task.c +3
-0@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, +#ifdef CONFIG_CGROUPS + .group_rwsem = __RWSEM_INITIALIZER(init_signals.group_rwsem), +#endif .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERSkernel/cgroup/cgroup-internal.h +4
-2@@ -249,8 +249,10 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup, int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup, + bool global); +void cgroup_attach_unlock (struct task_struct *tsk,bool lock_threadgroup, + bool global); struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, bool *locked) __acquires(&cgroup_threadgroup_rwsem);kernel/cgroup/cgroup-v1.c +1
-1@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) int retval = 0; cgroup_lock(); - cgroup_attach_lock(true); + cgroup_attach_lock(NULL, true, true); for_each_root(root) { struct cgroup *from_cgrp; @@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) if (retval)break;
}
-cgroup_attach_unlock(true);
+cgroup_attach_unlock(NULL, true, true);
cgroup_unlock();
return retval;kernel/cgroup/cgroup-v1.c +1
-0@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) cgroup_lock(); - cgroup_attach_lock(true); + cgroup_attach_lock(NULL, true, true); /* all tasks in @from are being moved, all csets are source */ spin_lock_irq(&css_set_lock);@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) } while (task && !ret);
out_err:
cgroup_migrate_finish(&mgctx);
-cgroup_attach_unlock(true);
+cgroup_attach_unlock(NULL, true, true);
cgroup_unlock();
return ret;
}kernel/cgroup/cgroup.c +2
-1@@ -2480,21 +2480,31 @@ EXPORT_SYMBOL_GPL(cgroup_path_ns);* write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that * CPU hotplug is disabled on entry. */ -void cgroup_attach_lock (bool lock_threadgroup) +void cgroup_attach_lock (struct task_struct *tsk, + bool lock_threadgroup, bool global) {kernel/cgroup/cgroup.c EXPORT_SYMBOL_GPL(cgroup_path_ns); · line 2493
Quoted 10 lines
kernel/cgroup/cgroup.c +6
-2cpus_read_lock(); - if (lock_threadgroup) - percpu_down_write(&cgroup_threadgroup_rwsem); + if (lock_threadgroup) { + if (global) + percpu_down_write(&cgroup_threadgroup_rwsem); + else + down_write(&tsk->signal->group_rwsem); + } }tsk is only used when global is false. So you can eliminate the redundant global argument and use the presence of tsk to indicate which lock to take.
Cheers,
Longman
Signature · 2 lines
Best regards, Ridong
Quoted 35 lines · Waiman Long
Quoted 33 lines · Yi Tao
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on
signal_struct->group_rwsemwhen writing pid to cgroup.procs/threads instead of holding a global write lock.Cgroup process migration has historically relied on
signal_struct->group_rwsemto protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem"), this was changed to a globalcgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------cgroup_lock() | down_read(&g_rwsem) |
cgroup_lock()down_write(&p_rwsem) | down_read(&p_rwsem) |down_write(&g_rwsem)critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) |up_write(&g_rwsem)cgroup_unlock() | up_read(&g_rwsem) |cgroup_unlock()
Quoted 8 lines
Quoted 7 lines
g_rwsemdenotescgroup_threadgroup_rwsem,p_rwsemdenotessignal_struct->group_rwsem.This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
Do you have any performance numbers to showcase how much performance benefit does this change provide?
Yes, here is the simulation test setup and results.
Machine Configuration:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 192
On-line CPU(s) list: 0-191
Thread(s) per core: 2
Core(s) per socket: 48
Socket(s): 2 NUMA node(s): 2
Vendor ID: GenuineIntel
BIOS Vendor ID: Intel(R) Corporation
CPU family: 6
Model: 143
Model name: Intel(R) Xeon(R) Platinum 8475B
BIOS Model name: Intel(R) Xeon(R) Platinum 8475B
NUMA node0 CPU(s): 0-47,96-143
NUMA node1 CPU(s): 48-95,144-191
Test Program 1: cgroup_attach, four threads each write to cgroup.procs every 10ms, simulating frequent cgroup process migrations.
Test Program 2: stress-ng or UnixBench, used to generate CPU and fork-intensive workloads.
Terminal 1: taskset -ac 0-47 ./cgroup_attach Terminal 2: taskset -ac 48-95 stress-ng --cpu 48 --forkheavy 4
Use bpftrace to monitor the latency of cgroup1_procs_write. Without the patch: The tail latency is 30ms
With the patch:The tail latency is 16μs.
Replace stress-ng with UnixBench to evaluate fork performance:
Terminal 2: taskset -ac 48-95 ./Run spawn
Without the patch: Multi-CPU score =15753.9
With the patch: Multi-CPU score = 17111.2
We can observe that the latency of cgroup process migration has been significantly reduced, and the performance of process forking has improved.
In production environments running big data computing workloads, without this patch, we observed a large number of processes entering the uninterruptible sleep (D) state due to contention on cgroup_threadgroup_rwsem. With the patch applied, the system load average has decreased.
cgroup_attach.c
```
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <string.h>
#define NUM_THREADS 4
#define SLEEP_US 10000 // 10ms
#define CGROUP_ROOT "/sys/fs/cgroup/cpu"
pid_t gettid() {
return syscall(SYS_gettid);
}
void* thread_func(void* arg) {
char dirname[256];
char procs_file[256];
int fd;
pid_t child_pid;
FILE* file;
pid_t tid = gettid();
snprintf(dirname, sizeof(dirname), "%s/test-%d", CGROUP_ROOT, tid);
snprintf(procs_file, sizeof(procs_file), "%s/cgroup.procs", dirname);
if (mkdir(dirname, 0755) != 0) {
perror(dirname);
pthread_exit(NULL);
}
printf("thread %d: cgroup dir %s\n", tid, dirname);
while (1) {
child_pid = fork();
if (child_pid < 0) {
perror("fork");
continue;
}
if (child_pid == 0) {
usleep(SLEEP_US);
exit(0);
}
fd = open(procs_file, O_WRONLY);
if (fd < 0) {
perror("open cgroup.procs");
} else {
char pid_str[16];
int len = snprintf(pid_str, sizeof(pid_str), "%d\n", child_pid);
if (write(fd, pid_str, len) < 0) {
perror("write cgroup.procs");
}
close(fd);
}
close(fd);
waitpid(child_pid, NULL, NULL);
usleep(SLEEP_US);
}
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int i;
for (i = 0; i < NUM_THREADS; i++) {
if (pthread_create(&threads[i], NULL, thread_func, NULL) != 0) {
perror("pthread_create");
break;
}
}
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
```Quoted 140 lines
Quoted 125 lines
Signed-off-by Yi Tao <[email protected]>---
include/linux/cgroup-defs.h | 2 ++ include/linux/sched/signal.h | 4 +++
init/init_task.c | 3 ++kernel/cgroup/cgroup-internal.h| 6 ++-- kernel/cgroup/cgroup-v1.c | 8 ++--- kernel/cgroup/cgroup.c | 56 ++++++++++++++++++++------------- kernel/fork.c | 4 +++7 files changed, 55 insertions(+), 28 deletions(-)include/linux/cgroup-defs.h +2
-0@@ -838,6 +838,7 @@ struct cgroup_of_peak { static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) { percpu_down_read(&cgroup_threadgroup_rwsem); + down_read(&tsk->signal->group_rwsem); } /**@@ -848,6 +849,7 @@ static inline voidcgroup_threadgroup_change_begin(struct task_struct *tsk) */ static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) { + up_read(&tsk->signal->group_rwsem); percpu_up_read(&cgroup_threadgroup_rwsem); }diff --git
a/include/linux/sched/signal.hb/include/linux/sched/signal.hindex1ef1edbaaf79..86fbc99a9174100644include/linux/sched/signal.h +0
-0@@ -226,6 +226,10 @@ struct signal_struct { struct tty_audit_buf *tty_audit_buf; #endif+#ifdef
CONFIG_CGROUPS
+ structrw_semaphoregroup_rwsem;include/linux/sched/signal.h +2
-0+#endif +/*
* Thread is the potential origin of an oom condition; kill first on
* oominit/init_task.c +3
-0@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, +#ifdef CONFIG_CGROUPS + .group_rwsem = __RWSEM_INITIALIZER(init_signals.group_rwsem), +#endif .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERSkernel/cgroup/cgroup-internal.h +4
-2@@ -249,8 +249,10 @@ int cgroup_migrate(struct task_struct *leader,bool threadgroup, int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup, + bool global); +void cgroup_attach_unlock (struct task_struct *tsk,bool lock_threadgroup, + bool global); struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, bool *locked)
__acquires(&cgroup_threadgroup_rwsem);kernel/cgroup/cgroup-v1.c +1
-1@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) int retval = 0; cgroup_lock(); - cgroup_attach_lock(true); + cgroup_attach_lock(NULL, true, true); for_each_root(root) { struct cgroup *from_cgrp; @@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)
break;
}
-cgroup_attach_unlock(true);
+cgroup_attach_unlock(NULL, true, true);
cgroup_unlock();
return retval;kernel/cgroup/cgroup-v1.c +1
-0@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) cgroup_lock(); - cgroup_attach_lock(true); + cgroup_attach_lock(NULL, true, true); /* all tasks in @from are being moved, all csets are source */ spin_lock_irq(&css_set_lock);@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) } while (task && !ret);
out_err:
cgroup_migrate_finish(&mgctx);
-cgroup_attach_unlock(true);
+cgroup_attach_unlock(NULL, true, true);
cgroup_unlock();
return ret;
}kernel/cgroup/cgroup.c +2
-1@@ -2480,21 +2480,31 @@ EXPORT_SYMBOL_GPL(cgroup_path_ns);* write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that * CPU hotplug is disabled on entry. */ -void cgroup_attach_lock (bool lock_threadgroup) +void cgroup_attach_lock (struct task_struct *tsk, + bool lock_threadgroup, bool global) {kernel/cgroup/cgroup.c EXPORT_SYMBOL_GPL(cgroup_path_ns); · line 2493
Quoted 10 lines
kernel/cgroup/cgroup.c +6
-2cpus_read_lock(); - if (lock_threadgroup) - percpu_down_write(&cgroup_threadgroup_rwsem); + if (lock_threadgroup) { + if (global) + percpu_down_write(&cgroup_threadgroup_rwsem); + else + down_write(&tsk->signal->group_rwsem); + } }tsk is only used when global is false. So you can eliminate the redundant global argument and use the presence of tsk to indicate which lock to take.
Cheers,
Longman
Quoted 181 lines · Waiman Long
Quoted 33 lines · Yi Tao
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on
signal_struct->group_rwsemwhen writing pid to cgroup.procs/threads instead of holding a global write lock.Cgroup process migration has historically relied on
signal_struct->group_rwsemto protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem"), this was changed to a globalcgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------cgroup_lock() | down_read(&g_rwsem) |
cgroup_lock()down_write(&p_rwsem) | down_read(&p_rwsem) |down_write(&g_rwsem)critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) |up_write(&g_rwsem)cgroup_unlock() | up_read(&g_rwsem) |cgroup_unlock()Quoted 7 lines
g_rwsemdenotescgroup_threadgroup_rwsem,p_rwsemdenotessignal_struct->group_rwsem.This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
Do you have any performance numbers to showcase how much performance benefit does this change provide?
Quoted 125 lines
Signed-off-by Yi Tao <[email protected]>---
include/linux/cgroup-defs.h | 2 ++ include/linux/sched/signal.h | 4 +++
init/init_task.c | 3 ++kernel/cgroup/cgroup-internal.h| 6 ++-- kernel/cgroup/cgroup-v1.c | 8 ++--- kernel/cgroup/cgroup.c | 56 ++++++++++++++++++++------------- kernel/fork.c | 4 +++7 files changed, 55 insertions(+), 28 deletions(-)include/linux/cgroup-defs.h +2
-0@@ -838,6 +838,7 @@ struct cgroup_of_peak { static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) { percpu_down_read(&cgroup_threadgroup_rwsem); + down_read(&tsk->signal->group_rwsem); } /**@@ -848,6 +849,7 @@ static inline voidcgroup_threadgroup_change_begin(struct task_struct *tsk) */ static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) { + up_read(&tsk->signal->group_rwsem); percpu_up_read(&cgroup_threadgroup_rwsem); }diff --git
a/include/linux/sched/signal.hb/include/linux/sched/signal.hindex1ef1edbaaf79..86fbc99a9174100644include/linux/sched/signal.h +0
-0@@ -226,6 +226,10 @@ struct signal_struct { struct tty_audit_buf *tty_audit_buf; #endif+#ifdef
CONFIG_CGROUPS
+ structrw_semaphoregroup_rwsem;include/linux/sched/signal.h +2
-0+#endif +/*
* Thread is the potential origin of an oom condition; kill first on
* oominit/init_task.c +3
-0@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim = INIT_RLIMITS, +#ifdef CONFIG_CGROUPS + .group_rwsem = __RWSEM_INITIALIZER(init_signals.group_rwsem), +#endif .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERSkernel/cgroup/cgroup-internal.h +4
-2@@ -249,8 +249,10 @@ int cgroup_migrate(struct task_struct *leader,bool threadgroup, int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup, + bool global); +void cgroup_attach_unlock (struct task_struct *tsk,bool lock_threadgroup, + bool global); struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, bool *locked)
__acquires(&cgroup_threadgroup_rwsem);kernel/cgroup/cgroup-v1.c +1
-1@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) int retval = 0; cgroup_lock(); - cgroup_attach_lock(true); + cgroup_attach_lock(NULL, true, true); for_each_root(root) { struct cgroup *from_cgrp; @@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)
break;
}
-cgroup_attach_unlock(true);
+cgroup_attach_unlock(NULL, true, true);
cgroup_unlock();
return retval;kernel/cgroup/cgroup-v1.c +1
-0@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) cgroup_lock(); - cgroup_attach_lock(true); + cgroup_attach_lock(NULL, true, true); /* all tasks in @from are being moved, all csets are source */ spin_lock_irq(&css_set_lock);@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) } while (task && !ret);
out_err:
cgroup_migrate_finish(&mgctx);
-cgroup_attach_unlock(true);
+cgroup_attach_unlock(NULL, true, true);
cgroup_unlock();
return ret;
}kernel/cgroup/cgroup.c +2
-1@@ -2480,21 +2480,31 @@ EXPORT_SYMBOL_GPL(cgroup_path_ns);* write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that * CPU hotplug is disabled on entry. */ -void cgroup_attach_lock (bool lock_threadgroup) +void cgroup_attach_lock (struct task_struct *tsk, + bool lock_threadgroup, bool global) {kernel/cgroup/cgroup.c EXPORT_SYMBOL_GPL(cgroup_path_ns); · line 2493
Quoted 10 lines
kernel/cgroup/cgroup.c +6
-2cpus_read_lock(); - if (lock_threadgroup) - percpu_down_write(&cgroup_threadgroup_rwsem); + if (lock_threadgroup) { + if (global) + percpu_down_write(&cgroup_threadgroup_rwsem); + else + down_write(&tsk->signal->group_rwsem); + } }tsk is only used when global is false. So you can eliminate the redundant global argument and use the presence of tsk to indicate which lock to take.
Quoted 2 lines
Cheers,
Longman
Thank you for your suggestion. As you pointed out, we can determine which lock to acquire
based on whether the task is NULL. I'll incorporate this change in the next version of the patch.
Thanks.
Tejun Heo
maintainerHello,
Quoted 6 lines · Yi Tao
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
If you use CLONE_INTO_CGROUP, cgroup migration doesn't just become cold. It disappears completely and CLONE_INTO_CGROUP doesn't need any global locks from cgroup side. Are there reasons why you can't use CLONE_INTO_CGROUP?
Thanks.
Signature · 1 line
tejun
Michal Koutný
maintainerQuoted 3 lines · Tejun Heo
If you use
CLONE_INTO_CGROUP, cgroup migration doesn't just become cold. It disappears completely andCLONE_INTO_CGROUPdoesn't need any global locks from cgroup side.
CLONE_INTO_CGROUP uses cgroup_mutex and threadgroup rwsem like regular migration, no? Its effect is atomicity wrt clone.
Or, Tejum, what do you mean that it disappears? (I think we cannot give up cgroup_mutex as it ensures synchronization of possible parent's migration.)
Michal
Tejun Heo
maintainerHello, Michal.
Quoted 10 lines · Michal Koutný
Quoted 3 lines · Tejun Heo
If you use
CLONE_INTO_CGROUP, cgroup migration doesn't just become cold. It disappears completely andCLONE_INTO_CGROUPdoesn't need any global locks from cgroup side.
CLONE_INTO_CGROUPusescgroup_mutexand threadgroup rwsem like regular migration, no? Its effect is atomicity wrt clone.
Or, Tejum, what do you mean that it disappears? (I think we cannot give upcgroup_mutexas it ensures synchronization of possible parent's migration.)
Sorry, I was confused. We no longer need to write lock threadgroup rwsem when CLONE_INTO_CGROUP'ing into an empty cgroup. We do still need cgroup_mutex.
671c11f0619e ("cgroup: Elide write-locking threadgroup_rwsem when updating csses on an empty subtree")
Thanks.
Signature · 1 line
tejun
Quoted 14 lines · Tejun Heo
Hello, Michal.
Quoted 10 lines · Michal Koutný
Quoted 3 lines · Tejun Heo
If you use
CLONE_INTO_CGROUP, cgroup migration doesn't just become cold. It disappears completely andCLONE_INTO_CGROUPdoesn't need any global locks from cgroup side.
CLONE_INTO_CGROUPusescgroup_mutexand threadgroup rwsem like regular migration, no? Its effect is atomicity wrt clone.
Or, Tejum, what do you mean that it disappears? (I think we cannot give upcgroup_mutexas it ensures synchronization of possible parent's migration.)
Quoted 8 lines
Sorry, I was confused. We no longer need to write lock threadgroup rwsem when CLONE_INTO_CGROUP'ing into an empty cgroup. We do still need
cgroup_mutex.
671c11f0619e("cgroup: Elide write-lockingthreadgroup_rwsemwhen updating csses on an empty subtree")Thanks.
I'm still a bit confused. Commit 671c11f0619e ("cgroup: Elide write-locking threadgroup_rwsem when updating csses on an empty subtree") only applies to CSS updates. However, cloning with CLONE_INTO_CGROUP still requires acquiring the threadgroup_rwsem.
cgroup_can_fork
cgroup_css_set_fork
if (kargs->flags & CLONE_INTO_CGROUP) cgroup_lock(); cgroup_threadgroup_change_begin(current);
Signature · 2 lines
Best regards, Ridong
Quoted 26 lines · Chen Ridong
Quoted 12 lines · Tejun Heo
Hello, Michal.
Quoted 9 lines · Michal Koutný
Quoted 3 lines · Tejun Heo
If you use
CLONE_INTO_CGROUP, cgroup migration doesn't just become cold. It disappears completely andCLONE_INTO_CGROUPdoesn't need any global locks from cgroup side.
CLONE_INTO_CGROUPusescgroup_mutexand threadgroup rwsem like regular migration, no? Its effect is atomicity wrt clone.
Or, Tejum, what do you mean that it disappears? (I think we cannot give upcgroup_mutexas it ensures synchronization of possible parent's migration.)Quoted 8 lines
Sorry, I was confused. We no longer need to write lock threadgroup rwsem when CLONE_INTO_CGROUP'ing into an empty cgroup. We do still need
cgroup_mutex.
671c11f0619e("cgroup: Elide write-lockingthreadgroup_rwsemwhen updating csses on an empty subtree")Thanks.
I'm still a bit confused. Commit
671c11f0619e("cgroup: Elide write-lockingthreadgroup_rwsemwhen updating csses on an empty subtree") only applies to CSS updates. However, cloning withCLONE_INTO_CGROUPstill requires acquiring thethreadgroup_rwsem.
Quoted 6 lines
cgroup_can_fork
cgroup_css_set_forkif (kargs->flags & CLONE_INTO_CGROUP) cgroup_lock(); cgroup_threadgroup_change_begin(current);
When using CLONE_INTO_CGROUP, there is no need to write cgroup.procs. So although a read lock is required here, there is no contention with the write lock.
Thanks.
Tejun Heo
maintainerHello,
On Thu, Sep 04, 2025 at 09:40:12AM +0800, Chen Ridong wrote:
...
Quoted 13 lines
Quoted 8 lines
Sorry, I was confused. We no longer need to write lock threadgroup rwsem when CLONE_INTO_CGROUP'ing into an empty cgroup. We do still need
cgroup_mutex.
671c11f0619e("cgroup: Elide write-lockingthreadgroup_rwsemwhen updating csses on an empty subtree")Thanks.
I'm still a bit confused. Commit
671c11f0619e("cgroup: Elide write-lockingthreadgroup_rwsemwhen updating csses on an empty subtree") only applies to CSS updates. However, cloning withCLONE_INTO_CGROUPstill requires acquiring thethreadgroup_rwsem.
Quoted 5 lines
cgroup_can_fork
cgroup_css_set_forkif (kargs->flags & CLONE_INTO_CGROUP) cgroup_lock(); cgroup_threadgroup_change_begin(current);
Ah, yeah, I'm misremembering things, sorry. What got elided in that commit is down_write of threadgroup_rwsem when enabling controllers on empty cgroups, which was the only operation which still needed to down_write the rwsem. Here's an excerpt from the commit message:
After this optimization, the usage pattern of creating a cgroup, enabling the necessary controllers, and then seeding it with CLONE_INTO_CGROUP and then removing the cgroup after it becomes empty doesn't need to write-lock threadgroup_rwsem at all.
It's true that cgroup_threadgroup_change_begin() down_reads the threadgroup_rwsem but that is a percpu_rwsem whose read operations are percpu inc/dec. This doesn't add any noticeable overhead or has any scalability concerns.
So, if you follow the "recommended" workflow, the only remaining possible scalability bottleneck is cgroup_mutex.
Thanks.
Signature · 1 line
tejun
Quoted 9 lines · Tejun Heo
Hello,
Quoted 6 lines · Yi Tao
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
Quoted 6 lines
If you use
CLONE_INTO_CGROUP, cgroup migration doesn't just become cold. It disappears completely andCLONE_INTO_CGROUPdoesn't need any global locks from cgroup side. Are there reasons why you can't useCLONE_INTO_CGROUP?Thanks.
As Ridong pointed out, in the current code, using CLONE_INTO_CGROUP still requires holding the threadgroup_rwsem, so contention with fork operations persists.
CLONE_INTO_CGROUP helps alleviate the contention between cgroup creation and deletion, but its usage comes with significant limitations:
1. CLONE_INTO_CGROUP is only available in cgroup v2. Although cgroup v2 adoption is gradually increasing, many applications have not yet been adapted to cgroup v2, and phasing out cgroup v1 will be a long and gradual process.
2. CLONE_INTO_CGROUP requires specifying the cgroup file descriptor at the time of process fork, effectively restricting cgroup migration to the fork stage. This differs significantly from the typical cgroup attach workflow. For example, in Kubernetes, systemd is the recommended cgroup driver; kubelet communicates with systemd via D-Bus, and systemd performs the actual cgroup attachment. In this case, the process being attached typically does not have systemd as its parent. Using CLONE_INTO_CGROUP in such a scenario is impractical and would require coordinated changes to both systemd and kubelet.
Thanks.
Quoted 15 lines · escape
Quoted 13 lines · Tejun Heo
Hello,
Quoted 6 lines · Yi Tao
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
If you use
CLONE_INTO_CGROUP, cgroup migration doesn't just become cold. It disappears completely andCLONE_INTO_CGROUPdoesn't need any global locks from cgroup side. Are there reasons why you can't useCLONE_INTO_CGROUP?
Quoted 5 lines
Quoted 2 lines
Thanks.
As Ridong pointed out, in the current code, using
CLONE_INTO_CGROUPstill requires holding thethreadgroup_rwsem, so contention with fork operations persists.
Sorry, my understanding here was wrong; using CLONE_INTO_CGROUP can indeed avoid the race condition with fork, but the restrictions do exist.
Thanks.
Quoted 22 lines
CLONE_INTO_CGROUPhelps alleviate the contention between cgroup creation and deletion, but its usage comes with significant limitations:1.
CLONE_INTO_CGROUPis only available in cgroup v2. Although cgroup v2 adoption is gradually increasing, many applications have not yet been adapted to cgroup v2, and phasing out cgroup v1 will be a long and gradual process.2.
CLONE_INTO_CGROUPrequires specifying the cgroup file descriptor at the time of process fork, effectively restricting cgroup migration to the fork stage. This differs significantly from the typical cgroup attach workflow. For example, in Kubernetes, systemd is the recommended cgroup driver; kubelet communicates with systemd via D-Bus, and systemd performs the actual cgroup attachment. In this case, the process being attached typically does not have systemd as its parent. UsingCLONE_INTO_CGROUPin such a scenario is impractical and would require coordinated changes to both systemd and kubelet.Thanks.
Tejun Heo
maintainerHello,
Quoted 2 lines · escape
Quoted 1 line · Tejun Heo
Hello,
...
Quoted 3 lines
As Ridong pointed out, in the current code, using
CLONE_INTO_CGROUPstill requires holding thethreadgroup_rwsem, so contention with fork operations persists.
Sorry about my fumbling explanations repeatedly but this isn't true. On cgroup2, if you create a cgroup, enable controllers and then seed it with CLONE_INTO_CGROUP, threadgroup_rwsem is out of the picture. The only remaining contention point is cgroup_mutex.
Quoted 8 lines
CLONE_INTO_CGROUPhelps alleviate the contention between cgroup creation and deletion, but its usage comes with significant limitations:1.
CLONE_INTO_CGROUPis only available in cgroup v2. Although cgroup v2 adoption is gradually increasing, many applications have not yet been adapted to cgroup v2, and phasing out cgroup v1 will be a long and gradual process.
Quoted 9 lines
2.
CLONE_INTO_CGROUPrequires specifying the cgroup file descriptor at the time of process fork, effectively restricting cgroup migration to the fork stage. This differs significantly from the typical cgroup attach workflow. For example, in Kubernetes, systemd is the recommended cgroup driver; kubelet communicates with systemd via D-Bus, and systemd performs the actual cgroup attachment. In this case, the process being attached typically does not have systemd as its parent. UsingCLONE_INTO_CGROUPin such a scenario is impractical and would require coordinated changes to both systemd and kubelet.
A percpu rwsem (threadgroup_rwsem) was used instead of per-threadgroup locking to avoid adding overhead to hot paths - fork and exit - because cgroup operations were expected to be a lot colder. Now, threadgroup rwsem is *really* expensive for the writers, so the trade-off could be a bit too extreme for some use cases.
However, now that the most common usage pattern doesn't involve threadgroup_rwsem, I don't feel too enthusiastic about adding hot path overhead to work around usage patterns that we want to move away from. Note that dynamic migrations have other more fundamental problems for stateful resources and we generally want to move away from it. Sure, a single rwsem operation in fork/exit isn't a lot of overhead but it isn't nothing either and this will impact everybody.
Maybe we can make it a mount option so that use cases that still depend on it can toggle it on? In fact, there's already favordynmods mount option which seems like a good fit. Maybe put the extra locking behind that flag?
Thanks.
Signature · 1 line
tejun
Quoted 45 lines · Tejun Heo
Hello,
Quoted 2 lines · escape
Quoted 1 line · Tejun Heo
Hello,
...
Quoted 3 lines
As Ridong pointed out, in the current code, using
CLONE_INTO_CGROUPstill requires holding thethreadgroup_rwsem, so contention with fork operations persists.Sorry about my fumbling explanations repeatedly but this isn't true. On cgroup2, if you create a cgroup, enable controllers and then seed it with
CLONE_INTO_CGROUP,threadgroup_rwsemis out of the picture. The only remaining contention point iscgroup_mutex.Quoted 8 lines
CLONE_INTO_CGROUPhelps alleviate the contention between cgroup creation and deletion, but its usage comes with significant limitations:1.
CLONE_INTO_CGROUPis only available in cgroup v2. Although cgroup v2 adoption is gradually increasing, many applications have not yet been adapted to cgroup v2, and phasing out cgroup v1 will be a long and gradual process.Quoted 9 lines
2.
CLONE_INTO_CGROUPrequires specifying the cgroup file descriptor at the time of process fork, effectively restricting cgroup migration to the fork stage. This differs significantly from the typical cgroup attach workflow. For example, in Kubernetes, systemd is the recommended cgroup driver; kubelet communicates with systemd via D-Bus, and systemd performs the actual cgroup attachment. In this case, the process being attached typically does not have systemd as its parent. UsingCLONE_INTO_CGROUPin such a scenario is impractical and would require coordinated changes to both systemd and kubelet.A percpu rwsem (
threadgroup_rwsem) was used instead of per-threadgroup locking to avoid adding overhead to hot paths - fork and exit - because cgroup operations were expected to be a lot colder. Now, threadgroup rwsem is *really* expensive for the writers, so the trade-off could be a bit too extreme for some use cases.However, now that the most common usage pattern doesn't involve
threadgroup_rwsem, I don't feel too enthusiastic about adding hot path overhead to work around usage patterns that we want to move away from. Note that dynamic migrations have other more fundamental problems for stateful resources and we generally want to move away from it. Sure, a single rwsem operation in fork/exit isn't a lot of overhead but it isn't nothing either and this will impact everybody.
Quoted 6 lines
Maybe we can make it a mount option so that use cases that still depend on it can toggle it on? In fact, there's already favordynmods mount option which seems like a good fit. Maybe put the extra locking behind that flag?
Thanks.
Thank you for your reply.
I agree that mounting with cgroupv2, creating a cgroup, enabling controllers, and then seeding it with CLONE_INTO_CGROUP is an excellent solution. However, we've encountered significant obstacles when applying this approach on some older systems. We are simultaneously working on enabling CLONE_INTO_CGROUP support in runc, systemd, and other components, but this will take some time. This patch aims to alleviate the issue to some extent during this transitional period.
Regarding the impact of the extra rwsem operations on hot paths, I have conducted performance testing. In cases where there is no contention on down_write, the UnixBench spawn test scores remain unaffected.
The suggestion to use the favordynmods flag to control whether the extra rwsem is used is excellent. I will incorporate this condition in the next version of the patch.
Thanks.
v2 cover cgroup: replace global percpu_rwsem with signal_struct->group_rwsem when writing cgroup.procs/threads
In reply to Yi TaoChanges in v2:
- Use favordynmods as the enabling switch.
- Determine whether to use the per-thread-group rwsem based on whether the task is NULL.
- Fix system hang caused by acquiring cgroup_threadgroup_rwsem inside rcu_read_lock.
Yi Tao (1):
cgroup: replace global percpu_rwsem with signal_struct->group_rwsem
when writing cgroup.procs/threads
include/linux/cgroup-defs.h | 6 +++ include/linux/sched/signal.h | 4 ++ init/init_task.c | 3 ++ kernel/cgroup/cgroup-internal.h | 4 +- kernel/cgroup/cgroup-v1.c | 8 ++-- kernel/cgroup/cgroup.c | 72 +++++++++++++++++++-------------- kernel/fork.c | 4 ++ 7 files changed, 64 insertions(+), 37 deletions(-)
Signature · 1 line
2.32.0.3.g01195cf9f
PATCH v2 1/1 cgroup: replace global percpu_rwsem with signal_struct->group_rwsem when writing cgroup.procs/threads
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on signal_struct->group_rwsem when writing pid to cgroup.procs/threads instead of holding a global write lock.
Cgroup process migration has historically relied on signal_struct->group_rwsem to protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem"), this was changed to a global cgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.
The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------
cgroup_lock() | down_read(&g_rwsem) | cgroup_lock() down_write(&p_rwsem) | down_read(&p_rwsem) | down_write(&g_rwsem) critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) | up_write(&g_rwsem) cgroup_unlock() | up_read(&g_rwsem) | cgroup_unlock()
g_rwsem denotes cgroup_threadgroup_rwsem, p_rwsem denotes signal_struct->group_rwsem.
This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
To avoid affecting other users, the per-thread-group rwsem is only used when the favordynmods flag is enabled.
--- include/linux/cgroup-defs.h | 6 +++ include/linux/sched/signal.h | 4 ++ init/init_task.c | 3 ++ kernel/cgroup/cgroup-internal.h | 4 +- kernel/cgroup/cgroup-v1.c | 8 ++-- kernel/cgroup/cgroup.c | 72 +++++++++++++++++++-------------- kernel/fork.c | 4 ++ 7 files changed, 64 insertions(+), 37 deletions(-)
include/linux/cgroup-defs.h +6 -0
@@ -828,6 +828,8 @@ struct cgroup_of_peak {struct list_head list; }; +extern bool have_favordynmods; +/** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups * @tsk: target task
@@ -838,6 +840,8 @@ struct cgroup_of_peak {static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +if (have_favordynmods) +down_read (&tsk->signal->group_rwsem); }/**
@@ -848,6 +852,8 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +if (have_favordynmods) +up_read (&tsk->signal->group_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem); }
include/linux/sched/signal.h +4 -0
@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore group_rwsem; +#endif +/* * Thread is the potential origin of an oom condition; kill first on * oom
init/init_task.c +3 -0
@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim =INIT_RLIMITS , +#ifdef CONFIG_CGROUPS + .group_rwsem = __RWSEM_INITIALIZER(init_signals.group_rwsem), +#endif .cred_guard_mutex =__MUTEX_INITIALIZER (init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERS
kernel/cgroup/cgroup-internal.h +2 -2
@@ -249,8 +249,8 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup); +void cgroup_attach_unlock (struct task_struct *tsk,bool lock_threadgroup);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup,bool *locked) __acquires(&cgroup_threadgroup_rwsem);
kernel/cgroup/cgroup-v1.c +4 -4
@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (NULL , true);for_each_root (root) {struct cgroup *from_cgrp;
@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (true); +cgroup_attach_unlock (NULL , true);cgroup_unlock ();return retval;
@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (NULL , true);/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);
@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (true); +cgroup_attach_unlock (NULL , true);cgroup_unlock ();return ret; }
kernel/cgroup/cgroup.c +41 -31
136 lines not on this page · raw message
kernel/fork.c +4 -0
@@ -1688,6 +1688,10 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)tty_audit_fork (sig);sched_autogroup_fork (sig); +#ifdef CONFIG_CGROUPS +init_rwsem (&sig->group_rwsem); +#endif + sig->oom_score_adj = current->signal->oom_score_adj; sig->oom_score_adj_min = current->signal->oom_score_adj_min;
Signature · 1 line
2.32.0.3.g01195cf9f
Tejun Heo
maintainerHello,
On Thu, Sep 04, 2025 at 07:39:32PM +0800, Yi Tao wrote:
...
Quoted 2 lines
To avoid affecting other users, the per-thread-group rwsem is only used when the
favordynmodsflag is enabled.
Can you please:
- Note that this isn't necessary for cgroup2's recommended workflow and is thus gated behind favordynmods.
- Include performance numbers briefly.
Quoted 15 lines
include/linux/cgroup-defs.h +4
-0+extern bool have_favordynmods; +/** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups * @tsk: target task @@ -838,6 +840,8 @@ struct cgroup_of_peak {static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +if (have_favordynmods) +down_read (&tsk->signal->group_rwsem); }/**
include/linux/cgroup-defs.h static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) · line 857
Quoted 6 lines
include/linux/cgroup-defs.h +2
-0@@ -848,6 +852,8 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +if (have_favordynmods) +up_read (&tsk->signal->group_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem);
Hmm... I wonder whether turning on/off the flag is racy. ie. what prevents have_favordynmods flipping while a task is between change_begin and end?
include/linux/sched/signal.h struct signal_struct { · line 231
Quoted 11 lines
include/linux/sched/signal.h +3
-0@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore group_rwsem; +#endif
Maybe name it more specific - e.g. cgroup_threadgroup_rwsem?
include/linux/sched/signal.h EXPORT_SYMBOL_GPL(cgroup_path_ns); · line 2483
Quoted 12 lines
include/linux/sched/signal.h +2
-1/** * cgroup_attach_lock - Lock for ->attach() -* @lock_threadgroup: whether to down_write cgroup_threadgroup_rwsem +* @tsk: thread group to lock +* @lock_threadgroup: whether to down_write rwsem *
* cgroup migration sometimes needs to stabilize threadgroups against forks and
* exits by write-lockingcgroup_threadgroup_rwsem. However, some ->attach()include/linux/sched/signal.h +0
-0@@ -2480,21 +2481,30 @@ EXPORT_SYMBOL_GPL(cgroup_path_ns);* write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that * CPU hotplug is disabled on entry. */
Please expand the function comment to explain what's going on and why and maybe point to it from a comment on top of favor_dynmods.
include/linux/sched/signal.h EXPORT_SYMBOL_GPL(cgroup_path_ns); · line 2484
Quoted 2 lines
include/linux/sched/signal.h +1
-1-void cgroup_attach_lock (bool lock_threadgroup) +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup)
As @tsk is an optional argument, it'd probably make more sense to put it at the end.
Quoted 18 lines
include/linux/sched/signal.h +5
-6@@ -3010,15 +3008,27 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,*/ if (tsk->no_cgroup_migration || (tsk->flags &PF_NO_SETAFFINITY )) { tsk =ERR_PTR (-EINVAL ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } -get_task_struct (tsk); - goto out_unlock_rcu; -out_unlock_threadgroup: -cgroup_attach_unlock (*threadgroup_locked); - *threadgroup_locked = false; +rcu_read_unlock (); + +/* +* If we migrate a single thread, we don't care about threadgroup
include/linux/sched/signal.h struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, · line 3028
Quoted 10 lines
include/linux/sched/signal.h +10
-0+* stability. If the thread is `current`, it won't exit(2) under our +* hands or change PID through exec(2). We exclude +* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write +* callers by cgroup_mutex. +* Therefore, we can skip the global lock. +*/ +lockdep_assert_held (&cgroup_mutex); + *threadgroup_locked = pid || threadgroup; + +cgroup_attach_lock (tsk, *threadgroup_locked);
I'm not sure this relocation is safe. What prevents e.g. @tsk changing its group leader or signal struct before lock is grabbed?
Thanks.
Signature · 1 line
tejun
Thanks for your review.
Quoted 26 lines · Tejun Heo
Hello,
On Thu, Sep 04, 2025 at 07:39:32PM +0800, Yi Tao wrote:
...Quoted 2 lines
To avoid affecting other users, the per-thread-group rwsem is only used when the
favordynmodsflag is enabled.Can you please:
- Note that this isn't necessary for cgroup2's recommended workflow and is thus gated behind favordynmods.
- Include performance numbers briefly.
Quoted 13 lines
include/linux/cgroup-defs.h +4
-0+extern bool have_favordynmods; +/** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups * @tsk: target task @@ -838,6 +840,8 @@ struct cgroup_of_peak {static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +if (have_favordynmods) +down_read (&tsk->signal->group_rwsem); }
Quoted 10 lines
include/linux/cgroup-defs.h static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) · line 857
Quoted 8 lines
include/linux/cgroup-defs.h +2
-0/** @@ -848,6 +852,8 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +if (have_favordynmods) +up_read (&tsk->signal->group_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem);Hmm... I wonder whether turning on/off the flag is racy. ie. what prevents
have_favordynmodsflipping while a task is betweenchange_beginand end?
have_favordynmods is read-only after initialization and will not change during runtime.
Quoted 55 lines
include/linux/sched/signal.h struct signal_struct { · line 231
Quoted 11 lines
include/linux/sched/signal.h +3
-0@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore group_rwsem; +#endif Maybe name it more specific - e.g.
cgroup_threadgroup_rwsem?include/linux/sched/signal.h EXPORT_SYMBOL_GPL(cgroup_path_ns); · line 2483
Quoted 12 lines
include/linux/sched/signal.h +2
-1/** * cgroup_attach_lock - Lock for ->attach() -* @lock_threadgroup: whether to down_write cgroup_threadgroup_rwsem +* @tsk: thread group to lock +* @lock_threadgroup: whether to down_write rwsem *
* cgroup migration sometimes needs to stabilize threadgroups against forks and
* exits by write-lockingcgroup_threadgroup_rwsem. However, some ->attach()include/linux/sched/signal.h +0
-0@@ -2480,21 +2481,30 @@ EXPORT_SYMBOL_GPL(cgroup_path_ns);* write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that * CPU hotplug is disabled on entry. */ Please expand the function comment to explain what's going on and why and maybe point to it from a comment on top of
favor_dynmods.include/linux/sched/signal.h EXPORT_SYMBOL_GPL(cgroup_path_ns); · line 2484
Quoted 2 lines
include/linux/sched/signal.h +1
-1-void cgroup_attach_lock (bool lock_threadgroup) +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup)As @tsk is an optional argument, it'd probably make more sense to put it at the end.
Quoted 22 lines
include/linux/sched/signal.h +9
-6@@ -3010,15 +3008,27 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,*/ if (tsk->no_cgroup_migration || (tsk->flags &PF_NO_SETAFFINITY )) { tsk =ERR_PTR (-EINVAL ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } -get_task_struct (tsk); - goto out_unlock_rcu; -out_unlock_threadgroup: -cgroup_attach_unlock (*threadgroup_locked); - *threadgroup_locked = false; +rcu_read_unlock (); + +/* +* If we migrate a single thread, we don't care about threadgroup +* stability. If the thread is `current`, it won't exit(2) under our +* hands or change PID through exec(2). We exclude +* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write +* callers by cgroup_mutex.
Quoted 8 lines
include/linux/sched/signal.h struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, · line 3028
Quoted 6 lines
include/linux/sched/signal.h +6
-0+* Therefore, we can skip the global lock. +*/ +lockdep_assert_held (&cgroup_mutex); + *threadgroup_locked = pid || threadgroup; + +cgroup_attach_lock (tsk, *threadgroup_locked);I'm not sure this relocation is safe. What prevents e.g. @tsk changing its group leader or signal struct before lock is grabbed?
When a non-leader thread in a thread group executes the exec system call, the thread group leader is updated, but the signal_struct remains unchanged, so this part is safe.
Quoted 2 lines
Thanks.
I will accept the remaining comments and apply them in the next version of the patch.
Thanks.
Tejun Heo
maintainerHello,
Quoted 8 lines · escape
Quoted 5 lines
include/linux/cgroup-defs.h static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) · line 857
Quoted 3 lines
include/linux/cgroup-defs.h +2
-0+if (have_favordynmods) +up_read (&tsk->signal->group_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem);Hmm... I wonder whether turning on/off the flag is racy. ie. what prevents
have_favordynmodsflipping while a task is betweenchange_beginand end?
have_favordynmodsis read-only after initialization and will not change during runtime.
I don't think that's necessarily true. favordynmods can also be specified as a mount option and mount can race against forks, execs and exits. Also, IIRC, cgroup2 doesn't allow remounts but there's nothing preventing someone from unmounting and mounting it again with different options.
Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
kernel/cgroup/cgroup.c +9
-6@@ -3010,15 +3008,27 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,*/ if (tsk->no_cgroup_migration || (tsk->flags &PF_NO_SETAFFINITY )) { tsk =ERR_PTR (-EINVAL ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } -get_task_struct (tsk); - goto out_unlock_rcu; -out_unlock_threadgroup: -cgroup_attach_unlock (*threadgroup_locked); - *threadgroup_locked = false; +rcu_read_unlock (); + +/* +* If we migrate a single thread, we don't care about threadgroup +* stability. If the thread is `current`, it won't exit(2) under our +* hands or change PID through exec(2). We exclude +* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write +* callers by cgroup_mutex. Quoted 8 lines
kernel/cgroup/cgroup.c struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, · line 3028
Quoted 6 lines
kernel/cgroup/cgroup.c +6
-0+* Therefore, we can skip the global lock. +*/ +lockdep_assert_held (&cgroup_mutex); + *threadgroup_locked = pid || threadgroup; + +cgroup_attach_lock (tsk, *threadgroup_locked);I'm not sure this relocation is safe. What prevents e.g. @tsk changing its group leader or signal struct before lock is grabbed?
Quoted 3 lines
When a non-leader thread in a thread group executes the exec system call, the thread group leader is updated, but the
signal_structremains unchanged, so this part is safe.
But the leader can change, right? So, we can end up in a situation where threadgroup is set but the task is not the leader which I think can lead to really subtle incorrect behaviors like write succeeding but nothing happening when racing against exec.
Thanks.
Signature · 1 line
tejun
Hello,
Quoted 8 lines · Tejun Heo
Hello,
Quoted 5 lines · escape
Quoted 5 lines
include/linux/cgroup-defs.h static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) · line 857
Quoted 3 lines
include/linux/cgroup-defs.h +2
-0+if (have_favordynmods) +up_read (&tsk->signal->group_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem);Hmm... I wonder whether turning on/off the flag is racy. ie. what prevents
have_favordynmodsflipping while a task is betweenchange_beginand end?
Quoted 6 lines
Quoted 2 lines
have_favordynmodsis read-only after initialization and will not change during runtime.I don't think that's necessarily true. favordynmods can also be specified as a mount option and mount can race against forks, execs and exits. Also, IIRC, cgroup2 doesn't allow remounts but there's nothing preventing someone from unmounting and mounting it again with different options.
There are two ways to set favordynmods. The first is through kernel config or the kernel command line parameter cgroup_favordynmods, which sets the value of the variable have_favordynmods and automatically sets the CGRP_ROOT_FAVOR_DYNMODS flag on cgroup_root->flags at mount time. The second is by passing a mount option, which only sets the CGRP_ROOT_FAVOR_DYNMODS flag on cgroup_root->flags during mount.
In this patch, the decision whether to use the per-threadgroup rwsem is based on have_favordynmods, not on cgroup_root->flags. An umount & mount affects cgroup_root->flags, but does not change have_favordynmods.
Indeed, there is a minor flaw here: if favordynmods is enabled via a mount option rather than the kernel command line, the per-threadgroup rwsem will not take effect.
To avoid the complexity of runtime changes to favordynmods, perhaps a better approach would be to introduce a separate control variable, configured via a kernel command line parameter such as cgroup_migration_lock=[global|thread_group] to explicitly govern this behavior. What do you think about this approach?
Quoted 32 lines
Quoted 29 lines
Quoted 21 lines
Quoted 21 lines
kernel/cgroup/cgroup.c +9
-6@@ -3010,15 +3008,27 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,*/ if (tsk->no_cgroup_migration || (tsk->flags &PF_NO_SETAFFINITY )) { tsk =ERR_PTR (-EINVAL ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } -get_task_struct (tsk); - goto out_unlock_rcu; -out_unlock_threadgroup: -cgroup_attach_unlock (*threadgroup_locked); - *threadgroup_locked = false; +rcu_read_unlock (); + +/* +* If we migrate a single thread, we don't care about threadgroup +* stability. If the thread is `current`, it won't exit(2) under our +* hands or change PID through exec(2). We exclude +* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write +* callers by cgroup_mutex. Quoted 8 lines
kernel/cgroup/cgroup.c struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, · line 3028
Quoted 6 lines
kernel/cgroup/cgroup.c +6
-0+* Therefore, we can skip the global lock. +*/ +lockdep_assert_held (&cgroup_mutex); + *threadgroup_locked = pid || threadgroup; + +cgroup_attach_lock (tsk, *threadgroup_locked);I'm not sure this relocation is safe. What prevents e.g. @tsk changing its group leader or signal struct before lock is grabbed?
Quoted 3 lines
When a non-leader thread in a thread group executes the exec system call, the thread group leader is updated, but the
signal_structremains unchanged, so this part is safe.
Quoted 7 lines
But the leader can change, right? So, we can end up in a situation where threadgroup is set but the task is not the leader which I think can lead to really subtle incorrect behaviors like write succeeding but nothing happening when racing against exec.
Thanks.
You are right. If the thread group leader changes, the task passed to cgroup_attach_task may not be the leader, which could lead to errors when iterating through all threads in while_each_thread.
Similar to commit b78949ebfb56 ("cgroup: simplify double-check locking in cgroup_attach_proc"), it should check whether the leader has changed after acquiring the lock, to determine if a retry is needed. I will fix this in the next version patch.
Thanks.
Tejun Heo
maintainerHello,
On Fri, Sep 05, 2025 at 11:44:53AM +0800, escape wrote:
...
Quoted 5 lines
To avoid the complexity of runtime changes to favordynmods, perhaps a better approach would be to introduce a separate control variable, configured via a kernel command line parameter such as cgroup_migration_lock=[global|thread_group] to explicitly govern this behavior. What do you think about this approach?
Can't you just down_write threadgroup_rwsem when flipping the flag? Wouldn't that provide sufficient synchronization?
Thanks.
Signature · 1 line
tejun
Quoted 9 lines · Tejun Heo
Hello,
On Fri, Sep 05, 2025 at 11:44:53AM +0800, escape wrote:
...Quoted 5 lines
To avoid the complexity of runtime changes to favordynmods, perhaps a better approach would be to introduce a separate control variable, configured via a kernel command line parameter such as cgroup_migration_lock=[global|thread_group] to explicitly govern this behavior. What do you think about this approach?
Quoted 5 lines
Can't you just
down_writethreadgroup_rwsemwhen flipping the flag? Wouldn't that provide sufficient synchronization?Thanks.
You're right, it's clear and simple. I will make the changes in this way.
Thanks.
Quoted 174 lines · Yi Tao
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on
signal_struct->group_rwsemwhen writing pid to cgroup.procs/threads instead of holding a global write lock.Cgroup process migration has historically relied on
signal_struct->group_rwsemto protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem"), this was changed to a globalcgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------cgroup_lock() | down_read(&g_rwsem) | cgroup_lock() down_write(&p_rwsem) | down_read(&p_rwsem) | down_write(&g_rwsem) critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) | up_write(&g_rwsem) cgroup_unlock() | up_read(&g_rwsem) | cgroup_unlock()
g_rwsemdenotescgroup_threadgroup_rwsem,p_rwsemdenotessignal_struct->group_rwsem.This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
To avoid affecting other users, the per-thread-group rwsem is only used when the
favordynmodsflag is enabled.Signed-off-by Yi Tao <[email protected]>--- include/linux/cgroup-defs.h | 6 +++ include/linux/sched/signal.h | 4 ++ init/init_task.c | 3 ++ kernel/cgroup/cgroup-internal.h | 4 +- kernel/cgroup/cgroup-v1.c | 8 ++-- kernel/cgroup/cgroup.c | 72 +++++++++++++++++++-------------- kernel/fork.c | 4 ++ 7 files changed, 64 insertions(+), 37 deletions(-)include/linux/cgroup-defs.h +6
-0@@ -828,6 +828,8 @@ struct cgroup_of_peak {struct list_head list; }; +extern bool have_favordynmods; +/** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups * @tsk: target task @@ -838,6 +840,8 @@ struct cgroup_of_peak {static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +if (have_favordynmods) +down_read (&tsk->signal->group_rwsem); }/** @@ -848,6 +852,8 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +if (have_favordynmods) +up_read (&tsk->signal->group_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem); }include/linux/sched/signal.h +4
-0@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore group_rwsem; +#endif +/* * Thread is the potential origin of an oom condition; kill first on * oom init/init_task.c +3
-0@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim =INIT_RLIMITS , +#ifdef CONFIG_CGROUPS + .group_rwsem = __RWSEM_INITIALIZER(init_signals.group_rwsem), +#endif .cred_guard_mutex =__MUTEX_INITIALIZER (init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERSkernel/cgroup/cgroup-internal.h +2
-2@@ -249,8 +249,8 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup); +void cgroup_attach_unlock (struct task_struct *tsk,bool lock_threadgroup);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup,bool *locked) __acquires(&cgroup_threadgroup_rwsem);kernel/cgroup/cgroup-v1.c +4
-4@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (NULL , true);for_each_root (root) {struct cgroup *from_cgrp;@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (true); +cgroup_attach_unlock (NULL , true);cgroup_unlock ();return retval;@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (NULL , true);/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (true); +cgroup_attach_unlock (NULL , true);cgroup_unlock ();return ret; }kernel/cgroup/cgroup.c +0
-0
kernel/cgroup/cgroup.c static u16 have_exit_callback __read_mostly; · line 218
Quoted 6 lines
kernel/cgroup/cgroup.c +1
-1@@ -214,7 +214,7 @@ static u16 have_exit_callback __read_mostly;static u16 have_release_callback__read_mostly ;static u16 have_canfork_callback__read_mostly ; -static bool have_favordynmods__ro_after_init =IS_ENABLED (CONFIG_CGROUP_FAVOR_DYNMODS ); +bool have_favordynmods__ro_after_init =IS_ENABLED (CONFIG_CGROUP_FAVOR_DYNMODS );
If have_favordynmods is gating the acquisition of tsk->signal->group_rwsem, would it be better to replace it with a static key?
Quoted 152 lines
kernel/cgroup/cgroup.c +40
-30/* cgroup namespace for init task */ struct cgroup_namespace init_cgroup_ns = {@@ -2459,7 +2459,8 @@ EXPORT_SYMBOL_GPL(cgroup_path_ns);/** * cgroup_attach_lock - Lock for ->attach() -* @lock_threadgroup: whether to down_write cgroup_threadgroup_rwsem +* @tsk: thread group to lock +* @lock_threadgroup: whether to down_write rwsem * * cgroup migration sometimes needs to stabilize threadgroups against forks and * exits by write-locking cgroup_threadgroup_rwsem. However, some ->attach() @@ -2480,21 +2481,30 @@ EXPORT_SYMBOL_GPL(cgroup_path_ns);* write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that * CPU hotplug is disabled on entry. */ -void cgroup_attach_lock (bool lock_threadgroup) +void cgroup_attach_lock (struct task_struct *tsk,bool lock_threadgroup) {cpus_read_lock (); -if (lock_threadgroup) -percpu_down_write (&cgroup_threadgroup_rwsem); +if (lock_threadgroup) { +if (tsk && favor_dynmods) +down_write (&tsk->signal->group_rwsem); +else +percpu_down_write (&cgroup_threadgroup_rwsem); + } }/** * cgroup_attach_unlock - Undo cgroup_attach_lock() -* @lock_threadgroup: whether to up_write cgroup_threadgroup_rwsem +* @tsk: thread group to lock +* @lock_threadgroup: whether to up_write rwsem */ -void cgroup_attach_unlock (bool lock_threadgroup) +void cgroup_attach_unlock (struct task_struct *tsk,bool lock_threadgroup) { -if (lock_threadgroup) -percpu_up_write (&cgroup_threadgroup_rwsem); +if (lock_threadgroup) { +if (tsk && favor_dynmods) +up_write (&tsk->signal->group_rwsem); +else +percpu_up_write (&cgroup_threadgroup_rwsem); + }cpus_read_unlock (); }@@ -2976,24 +2986,12 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,if (kstrtoint (strstrip (buf),0 , &pid) || pid <0 )return ERR_PTR (-EINVAL ); -/* -* If we migrate a single thread, we don't care about threadgroup -* stability. If the thread is `current`, it won't exit(2) under our -* hands or change PID through exec(2). We exclude -* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write -* callers by cgroup_mutex. -* Therefore, we can skip the global lock. -*/ -lockdep_assert_held (&cgroup_mutex); - *threadgroup_locked = pid || threadgroup; -cgroup_attach_lock (*threadgroup_locked); -rcu_read_lock ();if (pid) { tsk =find_task_by_vpid (pid);if (!tsk) { tsk =ERR_PTR (-ESRCH ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } }else { tsk = current;@@ -3010,15 +3008,27 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,*/ if (tsk->no_cgroup_migration || (tsk->flags &PF_NO_SETAFFINITY )) { tsk =ERR_PTR (-EINVAL ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } -get_task_struct (tsk); - goto out_unlock_rcu; -out_unlock_threadgroup: -cgroup_attach_unlock (*threadgroup_locked); - *threadgroup_locked = false; +rcu_read_unlock (); + +/* +* If we migrate a single thread, we don't care about threadgroup +* stability. If the thread is `current`, it won't exit(2) under our +* hands or change PID through exec(2). We exclude +* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write +* callers by cgroup_mutex. +* Therefore, we can skip the global lock. +*/ +lockdep_assert_held (&cgroup_mutex); + *threadgroup_locked = pid || threadgroup; + +cgroup_attach_lock (tsk, *threadgroup_locked); + +return tsk; + out_unlock_rcu:rcu_read_unlock ();return tsk;@@ -3032,7 +3042,7 @@ void cgroup_procs_write_finish(struct task_struct *task, bool threadgroup_locked/* release reference from cgroup_procs_write_start() */ put_task_struct (task); -cgroup_attach_unlock (threadgroup_locked); +cgroup_attach_unlock (task, threadgroup_locked);for_each_subsys (ss, ssid)if (ss->post_attach)@@ -3119,7 +3129,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)* write-locking can be skipped safely. */ has_tasks = !list_empty (&mgctx.preloaded_src_csets); -cgroup_attach_lock (has_tasks); +cgroup_attach_lock (NULL , has_tasks);/* NULL dst indicates self on default hierarchy */ ret =cgroup_migrate_prepare_dst (&mgctx);@@ -3140,7 +3150,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp) ret =cgroup_migrate_execute (&mgctx); out_finish:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (has_tasks); +cgroup_attach_unlock (NULL , has_tasks);return ret; }kernel/fork.c +4
-0@@ -1688,6 +1688,10 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)tty_audit_fork (sig);sched_autogroup_fork (sig); +#ifdef CONFIG_CGROUPS +init_rwsem (&sig->group_rwsem); +#endif + sig->oom_score_adj = current->signal->oom_score_adj; sig->oom_score_adj_min = current->signal->oom_score_adj_min;
Signature · 2 lines
Best regards, Ridong
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Yi,
kernel test robot noticed the following build errors:
[auto build test ERROR on tj-cgroup/for-next]
[also build test ERROR on kees/for-next/execve akpm-mm/mm-everything tip/sched/core linus/master v6.17-rc4] [cannot apply to next-20250905]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Yi-Tao/cgroup-replace-global-percpu_rwsem-with-signal_struct-group_rwsem-when-writing-cgroup-procs-threads/20250904-194505 base: https://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git for-next patch link: https://lore.kernel.org/r/068d58f1f497bc4971c6ac0bae58bf53b98451fd.1756985260.git.escape%40linux.alibaba.com patch subject: [PATCH v2 1/1] cgroup: replace global percpu_rwsem with signal_struct->group_rwsem when writing cgroup.procs/threads config: i386-buildonly-randconfig-004-20250905 (https://download.01.org/0day-ci/archive/20250905/[email protected]/config) compiler: gcc-13 (Debian 13.3.0-16) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250905/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
kernel/cgroup/cgroup.c: In function 'cgroup_attach_lock':
>> kernel/cgroup/cgroup.c:2511:28: error: 'favor_dynmods' undeclared (first use in this function); did you mean 'Opt_favordynmods'?
2511 | if (tsk && favor_dynmods)
| ^~~~~~~~~~~~~
| Opt_favordynmods
kernel/cgroup/cgroup.c:2511:28: note: each undeclared identifier is reported only once for each function it appears in
kernel/cgroup/cgroup.c: In function 'cgroup_attach_unlock':
kernel/cgroup/cgroup.c:2526:28: error: 'favor_dynmods' undeclared (first use in this function); did you mean 'Opt_favordynmods'?
2526 | if (tsk && favor_dynmods)
| ^~~~~~~~~~~~~
| Opt_favordynmodsvim +2511 kernel/cgroup/cgroup.c
2482
2483 /**
2484 * cgroup_attach_lock - Lock for ->attach()
2485 * @tsk: thread group to lock
2486 * @lock_threadgroup: whether to down_write rwsem
2487 *
2488 * cgroup migration sometimes needs to stabilize threadgroups against forks and
2489 * exits by write-locking cgroup_threadgroup_rwsem. However, some ->attach()
2490 * implementations (e.g. cpuset), also need to disable CPU hotplug.
2491 * Unfortunately, letting ->attach() operations acquire cpus_read_lock() can
2492 * lead to deadlocks.
2493 *
2494 * Bringing up a CPU may involve creating and destroying tasks which requires
2495 * read-locking threadgroup_rwsem, so threadgroup_rwsem nests inside
2496 * cpus_read_lock(). If we call an ->attach() which acquires the cpus lock while
2497 * write-locking threadgroup_rwsem, the locking order is reversed and we end up
2498 * waiting for an on-going CPU hotplug operation which in turn is waiting for
2499 * the threadgroup_rwsem to be released to create new tasks. For more details:
2500 *
2501 * http://lkml.kernel.org/r/20220711174629.uehfmqegcwn2lqzu@wubuntu
2502 *
2503 * Resolve the situation by always acquiring cpus_read_lock() before optionally
2504 * write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that
2505 * CPU hotplug is disabled on entry.
2506 */
2507 void cgroup_attach_lock(struct task_struct *tsk, bool lock_threadgroup)
2508 {
2509 cpus_read_lock();
2510 if (lock_threadgroup) {
> 2511 if (tsk && favor_dynmods)
2512 down_write(&tsk->signal->group_rwsem);
2513 else
2514 percpu_down_write(&cgroup_threadgroup_rwsem);
2515 }
2516 }
2517
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
v3 cover cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
In reply to Yi TaoChanges in v3:
- Expend commit log and comments.
- Put argument @tsk at end in cgroup_attach_lock/unlock.
- down_write global cgroup_thread_rwsem when flipping favordynmods to synchronize with task between cgroup_threadgroup_change_begin and end.
- Rename group_rwsem to cgroup_threadgroup_rwsem.
- Fix bug causing abnormal cgroup migration due to threadgroup leader changes。
Changes in v2:
- Use favordynmods as the enabling switch.
- Determine whether to use the per-thread-group rwsem based on whether the task is NULL.
- Fix system hang caused by acquiring cgroup_threadgroup_rwsem inside rcu_read_lock.
Yi Tao (1):
cgroup: replace global percpu_rwsem with per threadgroup resem when
writing to cgroup.procs
include/linux/cgroup-defs.h | 12 +++- include/linux/sched/signal.h | 4 ++ init/init_task.c | 3 + kernel/cgroup/cgroup-internal.h | 4 +- kernel/cgroup/cgroup-v1.c | 8 +-- kernel/cgroup/cgroup.c | 105 ++++++++++++++++++++++---------- kernel/fork.c | 4 ++ 7 files changed, 101 insertions(+), 39 deletions(-)
Signature · 1 line
2.32.0.3.g01195cf9f
PATCH v3 1/1 cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on signal_struct->group_rwsem when writing pid to cgroup.procs/threads instead of holding a global write lock.
Cgroup process migration has historically relied on signal_struct->group_rwsem to protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem"), this was changed to a global cgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.
The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------
cgroup_lock() | down_read(&g_rwsem) | cgroup_lock() down_write(&p_rwsem) | down_read(&p_rwsem) | down_write(&g_rwsem) critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) | up_write(&g_rwsem) cgroup_unlock() | up_read(&g_rwsem) | cgroup_unlock()
g_rwsem denotes cgroup_threadgroup_rwsem, p_rwsem denotes signal_struct->group_rwsem.
This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
With this patch, under heavy fork and exec interference, the long-tail latency of cgroup migration has been reduced from milliseconds to microseconds. Under heavy cgroup migration interference, the multi-CPU score of the spawn test case in UnixBench increased by 9%.
The static usage pattern of creating a cgroup, enabling controllers, and then seeding it with CLONE_INTO_CGROUP doesn't require write locking cgroup_threadgroup_rwsem and thus doesn't benefit from this patch.
To avoid affecting other users, the per threadgroup rwsem is only used when the favordynmods flag is enabled.
--- include/linux/cgroup-defs.h | 12 +++- include/linux/sched/signal.h | 4 ++ init/init_task.c | 3 + kernel/cgroup/cgroup-internal.h | 4 +- kernel/cgroup/cgroup-v1.c | 8 +-- kernel/cgroup/cgroup.c | 105 ++++++++++++++++++++++---------- kernel/fork.c | 4 ++ 7 files changed, 101 insertions(+), 39 deletions(-)
include/linux/cgroup-defs.h +10 -2
@@ -88,7 +88,8 @@ enum {/* * Reduce latencies on dynamic cgroup modifications such as task * migrations and controller on/offs by disabling percpu operation on -* cgroup_threadgroup_rwsem. This makes hot path operations such as +* cgroup_threadgroup_rwsem and taking per threadgroup rwsem when +* writing to cgroup.procs. This makes hot path operations such as * forks and exits into the slow path and more expensive. * * The static usage pattern of creating a cgroup, enabling controllers,
@@ -828,16 +829,21 @@ struct cgroup_of_peak {struct list_head list; }; +extern int take_per_threadgroup_rwsem; +/** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups * @tsk: target task * * Allows cgroup operations to synchronize against threadgroup changes -* using a percpu_rw_semaphore. +* using a global percpu_rw_semaphore and a per threadgroup rw_semaphore when +* favordynmods is on. See the comment above CGRP_ROOT_FAVOR_DYNMODS definition. */ static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +if (take_per_threadgroup_rwsem) +down_read (&tsk->signal->cgroup_threadgroup_rwsem); }/**
@@ -848,6 +854,8 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +if (take_per_threadgroup_rwsem) +up_read (&tsk->signal->cgroup_threadgroup_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem); }
include/linux/sched/signal.h +4 -0
@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore cgroup_threadgroup_rwsem; +#endif +/* * Thread is the potential origin of an oom condition; kill first on * oom
init/init_task.c +3 -0
@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim =INIT_RLIMITS , +#ifdef CONFIG_CGROUPS + .cgroup_threadgroup_rwsem = __RWSEM_INITIALIZER(init_signals.cgroup_threadgroup_rwsem), +#endif .cred_guard_mutex =__MUTEX_INITIALIZER (init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERS
kernel/cgroup/cgroup-internal.h +2 -2
@@ -249,8 +249,8 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (bool lock_threadgroup,struct task_struct *tsk); +void cgroup_attach_unlock (bool lock_threadgroup,struct task_struct *tsk);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup,bool *locked) __acquires(&cgroup_threadgroup_rwsem);
kernel/cgroup/cgroup-v1.c +4 -4
@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (true,NULL );for_each_root (root) {struct cgroup *from_cgrp;
@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (true); +cgroup_attach_unlock (true,NULL );cgroup_unlock ();return retval;
@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (true,NULL );/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);
@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (true); +cgroup_attach_unlock (true,NULL );cgroup_unlock ();return ret; }
kernel/cgroup/cgroup.c +74 -31
181 lines not on this page · raw message
kernel/fork.c +4 -0
@@ -1688,6 +1688,10 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)tty_audit_fork (sig);sched_autogroup_fork (sig); +#ifdef CONFIG_CGROUPS +init_rwsem (&sig->cgroup_threadgroup_rwsem); +#endif + sig->oom_score_adj = current->signal->oom_score_adj; sig->oom_score_adj_min = current->signal->oom_score_adj_min;
Signature · 1 line
2.32.0.3.g01195cf9f
Tejun Heo
maintainerRe: [PATCH v3 1/1] cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
Hello,
On Mon, Sep 08, 2025 at 06:20:27PM +0800, Yi Tao wrote:
...
Quoted 4 lines
The static usage pattern of creating a cgroup, enabling controllers, and then seeding it with
CLONE_INTO_CGROUPdoesn't require write lockingcgroup_threadgroup_rwsemand thus doesn't benefit from this patch.
Please bring this to the top, note that this is the default mode of operation and the mechanism being introduced is thus an optional one.
include/linux/cgroup-defs.h enum { · line 93
Quoted 8 lines
include/linux/cgroup-defs.h +2
-1@@ -88,7 +88,8 @@ enum {/* * Reduce latencies on dynamic cgroup modifications such as task * migrations and controller on/offs by disabling percpu operation on -* cgroup_threadgroup_rwsem. This makes hot path operations such as +* cgroup_threadgroup_rwsem and taking per threadgroup rwsem when +* writing to cgroup.procs. This makes hot path operations such as * forks and exits into the slow path and more expensive.
This comment is pointed to from all other places. Please expand on why per-threadgroup rwsem is beneficial for what use cases.
include/linux/cgroup-defs.h struct cgroup_of_peak { · line 832
Quoted 6 lines
include/linux/cgroup-defs.h +1
-0* The static usage pattern of creating a cgroup, enabling controllers, @@ -828,16 +829,21 @@ struct cgroup_of_peak {struct list_head list; }; +extern int take_per_threadgroup_rwsem;
I think it needs cgroup in its name. Maybe something like cgroup_enable_per_threadgroup_rwsem?
kernel/cgroup/cgroup.c struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) · line 1301
Quoted 9 lines
kernel/cgroup/cgroup.c +1
-0@@ -1298,18 +1298,29 @@ struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)return root_cgrp->root; } +int take_per_threadgroup_rwsem;
Please put it where other global variables are and also note what it does and that writes protected by cgroup_mutex and write-lock of cgroup_threadgroup_rwsem and thus reads are protected by either.
kernel/cgroup/cgroup.c struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) · line 1311
Quoted 10 lines
kernel/cgroup/cgroup.c +5
-1void cgroup_favor_dynmods (struct cgroup_root *root,bool favor) {bool favoring = root->flags &CGRP_ROOT_FAVOR_DYNMODS ; -/* see the comment above CGRP_ROOT_FAVOR_DYNMODS definition */ +/* +* see the comment above CGRP_ROOT_FAVOR_DYNMODS definition. +* favordynmods can flip while task is between +* cgroup_threadgroup_change_begin and cgroup_threadgroup_change_end, +* so down_write global cgroup_threadgroup_rwsem to synchronize them.
Maybe: take_per_threadgroup_rwsem must not be flipped while threads are between cgroup_threadgroup_change_begin and cgroup_threadgroup_change_end. down_write global group_threadgroup_rwsem to exclude them.
But does this actually work? It works for turning it on. I don't think it'd work for turning it off, right? Maybe make it enable only and trigger a warning message when people try to turn it off?
kernel/cgroup/cgroup.c struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) · line 1315
Quoted 4 lines
kernel/cgroup/cgroup.c +3
-0+*/ +percpu_down_write (&cgroup_threadgroup_rwsem);if (favor && !favoring) { + take_per_threadgroup_rwsem++;
Given that favoring is gating the switch, this can be a bool, right?
kernel/cgroup/cgroup.c struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) · line 1319
Quoted 4 lines
kernel/cgroup/cgroup.c +1
-0rcu_sync_enter (&cgroup_threadgroup_rwsem.rss); root->flags |=CGRP_ROOT_FAVOR_DYNMODS ; }else if (!favor && favoring) { + take_per_threadgroup_rwsem--;
And here, you can trigger a warning that per_threadgroup opreation can't be disabled once enabled instead of actually turning it off.
Another alternative would be using a task flag to track whether %current is holding per_threadgroup_rwsem and then using that to decide whether to unlock. Maybe that's cleaner but I don't think it really matters here.
..
kernel/cgroup/cgroup.c EXPORT_SYMBOL_GPL(cgroup_path_ns); · line 2497
Quoted 3 lines
kernel/cgroup/cgroup.c +3
-0+* When favordynmods is enabled, take per threadgroup rwsem to reduce latencies +* on dynamic cgroup modifications. see the comment above +* CGRP_ROOT_FAVOR_DYNMODS definition.
This is more about scalability, right? Maybe just say overhead?
Quoted 58 lines
kernel/cgroup/cgroup.c +20
-19@@ -2976,24 +3003,13 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,if (kstrtoint (strstrip (buf),0 , &pid) || pid <0 )return ERR_PTR (-EINVAL ); -/* -* If we migrate a single thread, we don't care about threadgroup -* stability. If the thread is `current`, it won't exit(2) under our -* hands or change PID through exec(2). We exclude -* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write -* callers by cgroup_mutex. -* Therefore, we can skip the global lock. -*/ -lockdep_assert_held (&cgroup_mutex); - *threadgroup_locked = pid || threadgroup; -cgroup_attach_lock (*threadgroup_locked); - +retry_find_task:rcu_read_lock ();if (pid) { tsk =find_task_by_vpid (pid);if (!tsk) { tsk =ERR_PTR (-ESRCH ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } }else { tsk = current;@@ -3010,15 +3026,42 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,*/ if (tsk->no_cgroup_migration || (tsk->flags &PF_NO_SETAFFINITY )) { tsk =ERR_PTR (-EINVAL ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } -get_task_struct (tsk); - goto out_unlock_rcu; -out_unlock_threadgroup: -cgroup_attach_unlock (*threadgroup_locked); - *threadgroup_locked = false; +rcu_read_unlock (); + +/* +* If we migrate a single thread, we don't care about threadgroup +* stability. If the thread is `current`, it won't exit(2) under our +* hands or change PID through exec(2). We exclude +* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write +* callers by cgroup_mutex. +* Therefore, we can skip the global lock. +*/ +lockdep_assert_held (&cgroup_mutex); + *threadgroup_locked = pid || threadgroup; + +cgroup_attach_lock (*threadgroup_locked, tsk); + +if (threadgroup) { +if (!thread_group_leader (tsk)) {
kernel/cgroup/cgroup.c struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, · line 3059
Quoted 10 lines
kernel/cgroup/cgroup.c +10
-0+/* +* a race with de_thread from another thread's exec() +* may strip us of our leadership, if this happens, +* there is no choice but to throw this task away and +* try again; this is +* "double-double-toil-and-trouble-check locking". +*/ +cgroup_attach_unlock (*threadgroup_locked, tsk); +put_task_struct (tsk); + goto retry_find_task;
This is subtle. Can you please separate this out to a spearate patch?
Thanks.
Signature · 1 line
tejun
Re: [PATCH v3 1/1] cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
In reply to Yi TaoQuoted 199 lines · Yi Tao
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on
signal_struct->group_rwsemwhen writing pid to cgroup.procs/threads instead of holding a global write lock.Cgroup process migration has historically relied on
signal_struct->group_rwsemto protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem"), this was changed to a globalcgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------cgroup_lock() | down_read(&g_rwsem) | cgroup_lock() down_write(&p_rwsem) | down_read(&p_rwsem) | down_write(&g_rwsem) critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) | up_write(&g_rwsem) cgroup_unlock() | up_read(&g_rwsem) | cgroup_unlock()
g_rwsemdenotescgroup_threadgroup_rwsem,p_rwsemdenotessignal_struct->group_rwsem.This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
With this patch, under heavy fork and exec interference, the long-tail latency of cgroup migration has been reduced from milliseconds to microseconds. Under heavy cgroup migration interference, the multi-CPU score of the spawn test case in UnixBench increased by 9%.
The static usage pattern of creating a cgroup, enabling controllers, and then seeding it with
CLONE_INTO_CGROUPdoesn't require write lockingcgroup_threadgroup_rwsemand thus doesn't benefit from this patch.To avoid affecting other users, the per threadgroup rwsem is only used when the
favordynmodsflag is enabled.Signed-off-by Yi Tao <[email protected]>--- include/linux/cgroup-defs.h | 12 +++- include/linux/sched/signal.h | 4 ++ init/init_task.c | 3 + kernel/cgroup/cgroup-internal.h | 4 +- kernel/cgroup/cgroup-v1.c | 8 +-- kernel/cgroup/cgroup.c | 105 ++++++++++++++++++++++---------- kernel/fork.c | 4 ++ 7 files changed, 101 insertions(+), 39 deletions(-)include/linux/cgroup-defs.h +10
-2@@ -88,7 +88,8 @@ enum {/* * Reduce latencies on dynamic cgroup modifications such as task * migrations and controller on/offs by disabling percpu operation on -* cgroup_threadgroup_rwsem. This makes hot path operations such as +* cgroup_threadgroup_rwsem and taking per threadgroup rwsem when +* writing to cgroup.procs. This makes hot path operations such as * forks and exits into the slow path and more expensive. * * The static usage pattern of creating a cgroup, enabling controllers, @@ -828,16 +829,21 @@ struct cgroup_of_peak {struct list_head list; }; +extern int take_per_threadgroup_rwsem; +/** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups * @tsk: target task * * Allows cgroup operations to synchronize against threadgroup changes -* using a percpu_rw_semaphore. +* using a global percpu_rw_semaphore and a per threadgroup rw_semaphore when +* favordynmods is on. See the comment above CGRP_ROOT_FAVOR_DYNMODS definition. */ static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +if (take_per_threadgroup_rwsem) +down_read (&tsk->signal->cgroup_threadgroup_rwsem); }/** @@ -848,6 +854,8 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +if (take_per_threadgroup_rwsem) +up_read (&tsk->signal->cgroup_threadgroup_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem); }include/linux/sched/signal.h +4
-0@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore cgroup_threadgroup_rwsem; +#endif +/* * Thread is the potential origin of an oom condition; kill first on * oom init/init_task.c +3
-0@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim =INIT_RLIMITS , +#ifdef CONFIG_CGROUPS + .cgroup_threadgroup_rwsem = __RWSEM_INITIALIZER(init_signals.cgroup_threadgroup_rwsem), +#endif .cred_guard_mutex =__MUTEX_INITIALIZER (init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERSkernel/cgroup/cgroup-internal.h +2
-2@@ -249,8 +249,8 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (bool lock_threadgroup,struct task_struct *tsk); +void cgroup_attach_unlock (bool lock_threadgroup,struct task_struct *tsk);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup,bool *locked) __acquires(&cgroup_threadgroup_rwsem);kernel/cgroup/cgroup-v1.c +4
-4@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (true,NULL );for_each_root (root) {struct cgroup *from_cgrp;@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (true); +cgroup_attach_unlock (true,NULL );cgroup_unlock ();return retval;@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (true,NULL );/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (true); +cgroup_attach_unlock (true,NULL );cgroup_unlock ();return ret; }kernel/cgroup/cgroup.c +0
-0
kernel/cgroup/cgroup.c struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) · line 1301
Quoted 4 lines
kernel/cgroup/cgroup.c +1
-0@@ -1298,18 +1298,29 @@ struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)return root_cgrp->root; } +int take_per_threadgroup_rwsem;
I would suggest adding the "__read_mostly" attribute to avoid the chance of false cacheline sharing.
Quoted 15 lines
kernel/cgroup/cgroup.c +9
-1+void cgroup_favor_dynmods (struct cgroup_root *root,bool favor) {bool favoring = root->flags &CGRP_ROOT_FAVOR_DYNMODS ; -/* see the comment above CGRP_ROOT_FAVOR_DYNMODS definition */ +/* +* see the comment above CGRP_ROOT_FAVOR_DYNMODS definition. +* favordynmods can flip while task is between +* cgroup_threadgroup_change_begin and cgroup_threadgroup_change_end, +* so down_write global cgroup_threadgroup_rwsem to synchronize them. +*/ +percpu_down_write (&cgroup_threadgroup_rwsem);if (favor && !favoring) { + take_per_threadgroup_rwsem++;
kernel/cgroup/cgroup.c struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) · line 1325
Quoted 10 lines
kernel/cgroup/cgroup.c +2
-0rcu_sync_enter (&cgroup_threadgroup_rwsem.rss); root->flags |=CGRP_ROOT_FAVOR_DYNMODS ; }else if (!favor && favoring) { + take_per_threadgroup_rwsem--;rcu_sync_exit (&cgroup_threadgroup_rwsem.rss); root->flags &= ~CGRP_ROOT_FAVOR_DYNMODS ; } +percpu_up_write (&cgroup_threadgroup_rwsem); }
Changing take_per_threadgroup_rwsem inside the cgroup_threadgroup_rwsem critical section will ensure that the flag won't change in between cgroup_threadgroup_change_begin() and cgroup_threadgroup_change_end(). However, it may still change in between cgroup_attach_lock() and cgroup_attach_unlock().
Since cgroup_attach_[un]lock() has already taken a threadgroup_locked argument, we can extend it to a flag word that holds 2 flag bits - one for threadgroup_locked and another one for whether the threadgroup rwsem has been taken or not. So take_per_threadgroup_rwsem will only be checked in cgroup_attach_lock(). cgroup_attach_lock() can either return a value to indicate the state or the argument can be changed into a flag pointer with the new flag bit added internally.
It should be a separate patch if you decide to do the extension.
Cheers,
Longman
v4 cover cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
In reply to Yi TaoChanges in v4:
- Adjust commit log and comments.
- Rename take_per_threadgroup_rwsem to cgroup_enable_per_threadgroup_rwsem and add attr __read_mostly.
- Trigger a warning that per_threadgroup opreation can't be disabled once enabled instead of actually turning it off.
- Split the code for retrying when the threadgroup leader changes into a separate patch.
- Refactor the cgroup_attach_lock code to make it clearer.
Changes in v3:
- Expend commit log and comments.
- Put argument @tsk at end in cgroup_attach_lock/unlock.
- down_write global cgroup_thread_rwsem when flipping favordynmods to synchronize with task between cgroup_threadgroup_change_begin and end.
- Rename group_rwsem to cgroup_threadgroup_rwsem.
- Fix bug causing abnormal cgroup migration due to threadgroup leader changes。
Changes in v2:
- Use favordynmods as the enabling switch.
- Determine whether to use the per-thread-group rwsem based on whether the task is NULL.
- Fix system hang caused by acquiring cgroup_threadgroup_rwsem inside rcu_read_lock.
Yi Tao (3):
cgroup: replace global percpu_rwsem with per threadgroup resem when
writing to cgroup.procs
cgroup: retry find task if threadgroup leader changed
cgroup: refactor the cgroup_attach_lock code to make it clearer
include/linux/cgroup-defs.h | 25 +++++- include/linux/sched/signal.h | 4 + init/init_task.c | 3 + kernel/cgroup/cgroup-internal.h | 8 +- kernel/cgroup/cgroup-v1.c | 14 +-- kernel/cgroup/cgroup.c | 149 ++++++++++++++++++++++++-------- kernel/fork.c | 4 + 7 files changed, 161 insertions(+), 46 deletions(-)
Signature · 1 line
2.32.0.3.g01195cf9f
PATCH v4 1/3 cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
The static usage pattern of creating a cgroup, enabling controllers, and then seeding it with CLONE_INTO_CGROUP doesn't require write locking cgroup_threadgroup_rwsem and thus doesn't benefit from this patch.
To avoid affecting other users, the per threadgroup rwsem is only used when the favordynmods is enabled.
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on signal_struct->group_rwsem when writing pid to cgroup.procs/threads instead of holding a global write lock.
Cgroup process migration has historically relied on signal_struct->group_rwsem to protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem"), this was changed to a global cgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.
The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------
cgroup_lock() | down_read(&g_rwsem) | cgroup_lock() down_write(&p_rwsem) | down_read(&p_rwsem) | down_write(&g_rwsem) critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) | up_write(&g_rwsem) cgroup_unlock() | up_read(&g_rwsem) | cgroup_unlock()
g_rwsem denotes cgroup_threadgroup_rwsem, p_rwsem denotes signal_struct->group_rwsem.
This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
With this patch, under heavy fork and exec interference, the long-tail latency of cgroup migration has been reduced from milliseconds to microseconds. Under heavy cgroup migration interference, the multi-CPU score of the spawn test case in UnixBench increased by 9%.
--- include/linux/cgroup-defs.h | 14 ++++- include/linux/sched/signal.h | 4 ++ init/init_task.c | 3 ++ kernel/cgroup/cgroup-internal.h | 4 +- kernel/cgroup/cgroup-v1.c | 8 +-- kernel/cgroup/cgroup.c | 96 ++++++++++++++++++++++----------- kernel/fork.c | 4 ++ 7 files changed, 95 insertions(+), 38 deletions(-)
include/linux/cgroup-defs.h +13 -1
@@ -91,6 +91,12 @@ enum {* cgroup_threadgroup_rwsem. This makes hot path operations such as * forks and exits into the slow path and more expensive. * +* Alleviate the contention between fork, exec, exit operations and +* writing to cgroup.procs by taking a per threadgroup rwsem instead of +* the global cgroup_threadgroup_rwsem. Fork and other operations +* from threads in different thread groups no longer contend with +* writing to cgroup.procs. +* * The static usage pattern of creating a cgroup, enabling controllers, * and then seeding it with CLONE_INTO_CGROUP doesn't require write * locking cgroup_threadgroup_rwsem and thus doesn't benefit from
@@ -822,6 +828,7 @@ struct cgroup_subsys { };extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +extern bool cgroup_enable_per_threadgroup_rwsem;struct cgroup_of_peak {unsigned long value;
@@ -833,11 +840,14 @@ struct cgroup_of_peak {* @tsk: target task * * Allows cgroup operations to synchronize against threadgroup changes -* using a percpu_rw_semaphore. +* using a global percpu_rw_semaphore and a per threadgroup rw_semaphore when +* favordynmods is on. See the comment above CGRP_ROOT_FAVOR_DYNMODS definition. */ static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +if (cgroup_enable_per_threadgroup_rwsem) +down_read (&tsk->signal->cgroup_threadgroup_rwsem); }/**
@@ -848,6 +858,8 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +if (cgroup_enable_per_threadgroup_rwsem) +up_read (&tsk->signal->cgroup_threadgroup_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem); }
include/linux/sched/signal.h +4 -0
@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore cgroup_threadgroup_rwsem; +#endif +/* * Thread is the potential origin of an oom condition; kill first on * oom
init/init_task.c +3 -0
@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim =INIT_RLIMITS , +#ifdef CONFIG_CGROUPS + .cgroup_threadgroup_rwsem = __RWSEM_INITIALIZER(init_signals.cgroup_threadgroup_rwsem), +#endif .cred_guard_mutex =__MUTEX_INITIALIZER (init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERS
kernel/cgroup/cgroup-internal.h +2 -2
@@ -249,8 +249,8 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (bool lock_threadgroup,struct task_struct *tsk); +void cgroup_attach_unlock (bool lock_threadgroup,struct task_struct *tsk);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup,bool *locked) __acquires(&cgroup_threadgroup_rwsem);
kernel/cgroup/cgroup-v1.c +4 -4
@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (true,NULL );for_each_root (root) {struct cgroup *from_cgrp;
@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (true); +cgroup_attach_unlock (true,NULL );cgroup_unlock ();return retval;
@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (true,NULL );/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);
@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (true); +cgroup_attach_unlock (true,NULL );cgroup_unlock ();return ret; }
kernel/cgroup/cgroup.c +65 -31
174 lines not on this page · raw message
kernel/fork.c +4 -0
@@ -1688,6 +1688,10 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)tty_audit_fork (sig);sched_autogroup_fork (sig); +#ifdef CONFIG_CGROUPS +init_rwsem (&sig->cgroup_threadgroup_rwsem); +#endif + sig->oom_score_adj = current->signal->oom_score_adj; sig->oom_score_adj_min = current->signal->oom_score_adj_min;
Signature · 1 line
2.32.0.3.g01195cf9f
PATCH v4 2/3 cgroup: retry find task if threadgroup leader changed
In reply to Yi TaoBetween obtaining the threadgroup leader via PID and acquiring the cgroup attach lock, the threadgroup leader may change, which could lead to incorrect cgroup migration. Therefore, after acquiring the cgroup attach lock, we check whether the threadgroup leader has changed, and if so, retry the operation.
--- kernel/cgroup/cgroup.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
kernel/cgroup/cgroup.c +16 -0
@@ -3010,6 +3010,7 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,if (kstrtoint (strstrip (buf),0 , &pid) || pid <0 )return ERR_PTR (-EINVAL ); +retry_find_task:rcu_read_lock ();if (pid) { tsk =find_task_by_vpid (pid);
@@ -3051,6 +3052,21 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,cgroup_attach_lock (*threadgroup_locked, tsk); +if (threadgroup) { +if (!thread_group_leader (tsk)) { +/* +* a race with de_thread from another thread's exec() +* may strip us of our leadership, if this happens, +* there is no choice but to throw this task away and +* try again; this is +* "double-double-toil-and-trouble-check locking". +*/ +cgroup_attach_unlock (*threadgroup_locked, tsk); +put_task_struct (tsk); + goto retry_find_task; + } + } +return tsk; out_unlock_rcu:
Signature · 1 line
2.32.0.3.g01195cf9f
Tejun Heo
maintainerRe: [PATCH v4 2/3] cgroup: retry find task if threadgroup leader changed
Quoted 7 lines · Yi Tao
Between obtaining the threadgroup leader via PID and acquiring the cgroup attach lock, the threadgroup leader may change, which could lead to incorrect cgroup migration. Therefore, after acquiring the cgroup attach lock, we check whether the threadgroup leader has changed, and if so, retry the operation.
Signed-off-by Yi Tao <[email protected]>
This and thread group lock relocation should come before the patch which requires it, not after.
Thanks.
Signature · 1 line
tejun
PATCH v4 3/3 cgroup: refactor the cgroup_attach_lock code to make it clearer
In reply to Yi TaoDynamic cgroup migration involving threadgroup locks can be in one of three states: no lock held, holding the global lock, or holding a per threadgroup lock. Explicitly declaring the different lock modes to make the code easier to understand.
--- include/linux/cgroup-defs.h | 11 ++++ kernel/cgroup/cgroup-internal.h | 8 +-- kernel/cgroup/cgroup-v1.c | 14 ++--- kernel/cgroup/cgroup.c | 91 ++++++++++++++++++++++----------- 4 files changed, 83 insertions(+), 41 deletions(-)
include/linux/cgroup-defs.h +11 -0
@@ -146,6 +146,17 @@ enum { __CFTYPE_ADDED = (1 << 18), }; +enum { +/* Default */ +CGRP_ATTACH_LOCK_GLOBAL , + +/* When pid=0 && threadgroup=false, see comments in cgroup_procs_write_start */ +CGRP_ATTACH_LOCK_NONE , + +/* When favordynmods is on, see comments above CGRP_ROOT_FAVOR_DYNMODS */ +CGRP_ATTACH_LOCK_PER_THREADGROUP , +}; +/* * cgroup_file is the handle for a file instance created in a cgroup which * is used, for example, to generate file changed notifications. This can
kernel/cgroup/cgroup-internal.h +4 -4
@@ -249,12 +249,12 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup,struct task_struct *tsk); -void cgroup_attach_unlock (bool lock_threadgroup,struct task_struct *tsk); +void cgroup_attach_lock (int lock_mode,struct task_struct *tsk); +void cgroup_attach_unlock (int lock_mode,struct task_struct *tsk);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup, -bool *locked) +int *lock_mode) __acquires(&cgroup_threadgroup_rwsem); -void cgroup_procs_write_finish(struct task_struct *task, bool locked) +void cgroup_procs_write_finish(struct task_struct *task, int locke_mode) __releases(&cgroup_threadgroup_rwsem);void cgroup_lock_and_drain_offline (struct cgroup *cgrp);
kernel/cgroup/cgroup-v1.c +7 -7
@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (true,NULL ); +cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );for_each_root (root) {struct cgroup *from_cgrp;
@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (true,NULL ); +cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );cgroup_unlock ();return retval;
@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (true,NULL ); +cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);
@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (true,NULL ); +cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );cgroup_unlock ();return ret; }
@@ -502,13 +502,13 @@ static ssize_t __cgroup1_procs_write(struct kernfs_open_file *of,struct task_struct *task;const struct cred *cred, *tcred;ssize_t ret; -bool locked; +int lock_mode; cgrp =cgroup_kn_lock_live (of->kn, false);if (!cgrp)return -ENODEV ; - task =cgroup_procs_write_start (buf, threadgroup, &locked); + task =cgroup_procs_write_start (buf, threadgroup, &lock_mode); ret =PTR_ERR_OR_ZERO (task);if (ret) goto out_unlock;
@@ -531,7 +531,7 @@ static ssize_t __cgroup1_procs_write(struct kernfs_open_file *of, ret =cgroup_attach_task (cgrp, task, threadgroup); out_finish: -cgroup_procs_write_finish (task, locked); +cgroup_procs_write_finish (task, lock_mode); out_unlock:cgroup_kn_unlock (of->kn);
kernel/cgroup/cgroup.c +61 -30
182 lines not on this page · raw message
Signature · 1 line
2.32.0.3.g01195cf9f
Tejun Heo
maintainerRe: [PATCH v4 3/3] cgroup: refactor the cgroup_attach_lock code to make it clearer
Quoted 1 line · Yi Tao
+enum {
Please give it a type name. It makes difference when it gets exported through BTF for debugging and tracing.
include/linux/cgroup-defs.h enum { · line 158
Quoted 9 lines
include/linux/cgroup-defs.h +9
-0+/* Default */ +CGRP_ATTACH_LOCK_GLOBAL , + +/* When pid=0 && threadgroup=false, see comments in cgroup_procs_write_start */ +CGRP_ATTACH_LOCK_NONE , + +/* When favordynmods is on, see comments above CGRP_ROOT_FAVOR_DYNMODS */ + CGRP_ATTACH_LOCK_PER_THREADGROUP, +};
And, I'd put this before the actual patch too. Please make it a prep patch.
Thanks.
Signature · 1 line
tejun
v5 cover cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
In reply to Yi TaoChanges in v5:
- Adjust the order of patches.
- Add the type name for enum used by cgroup_attach_lock.
Changes in v4:
- Adjust commit log and comments.
- Rename take_per_threadgroup_rwsem to cgroup_enable_per_threadgroup_rwsem and add attr __read_mostly.
- Trigger a warning that per_threadgroup opreation can't be disabled once enabled instead of actually turning it off.
- Split the code for retrying when the threadgroup leader changes into a separate patch.
- Refactor the cgroup_attach_lock code to make it clearer.
Changes in v3:
- Expend commit log and comments.
- Put argument @tsk at end in cgroup_attach_lock/unlock.
- down_write global cgroup_thread_rwsem when flipping favordynmods to synchronize with task between cgroup_threadgroup_change_begin and end.
- Rename group_rwsem to cgroup_threadgroup_rwsem.
- Fix bug causing abnormal cgroup migration due to threadgroup leader changes。
Changes in v2:
- Use favordynmods as the enabling switch.
- Determine whether to use the per-thread-group rwsem based on whether the task is NULL.
- Fix system hang caused by acquiring cgroup_threadgroup_rwsem inside rcu_read_lock.
Yi Tao (3):
cgroup: refactor the cgroup_attach_lock code to make it clearer
cgroup: relocate cgroup_attach_lock within cgroup_procs_write_start
cgroup: replace global percpu_rwsem with per threadgroup resem when
writing to cgroup.procs
include/linux/cgroup-defs.h | 25 +++++- include/linux/sched/signal.h | 4 + init/init_task.c | 3 + kernel/cgroup/cgroup-internal.h | 11 ++- kernel/cgroup/cgroup-v1.c | 14 +-- kernel/cgroup/cgroup.c | 153 ++++++++++++++++++++++++-------- kernel/fork.c | 4 + 7 files changed, 167 insertions(+), 47 deletions(-)
Signature · 1 line
2.32.0.3.g01195cf9f
PATCH v5 1/3 cgroup: refactor the cgroup_attach_lock code to make it clearer
Dynamic cgroup migration involving threadgroup locks can be in one of two states: no lock held, or holding the global lock. Explicitly declaring the different lock modes to make the code easier to understand and facilitates future extensions of the lock modes.
--- include/linux/cgroup-defs.h | 8 ++++ kernel/cgroup/cgroup-internal.h | 9 +++-- kernel/cgroup/cgroup-v1.c | 14 +++---- kernel/cgroup/cgroup.c | 67 ++++++++++++++++++++++++--------- 4 files changed, 69 insertions(+), 29 deletions(-)
include/linux/cgroup-defs.h +8 -0
@@ -140,6 +140,14 @@ enum { __CFTYPE_ADDED = (1 << 18), }; +enum cgroup_attach_lock_mode { +/* Default */ +CGRP_ATTACH_LOCK_GLOBAL , + +/* When pid=0 && threadgroup=false, see comments in cgroup_procs_write_start */ +CGRP_ATTACH_LOCK_NONE , +}; +/* * cgroup_file is the handle for a file instance created in a cgroup which * is used, for example, to generate file changed notifications. This can
kernel/cgroup/cgroup-internal.h +5 -4
@@ -249,12 +249,13 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (bool lock_threadgroup); -void cgroup_attach_unlock (bool lock_threadgroup); +void cgroup_attach_lock (enum cgroup_attach_lock_mode lock_mode); +void cgroup_attach_unlock (enum cgroup_attach_lock_mode lock_mode);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup, -bool *locked) +enum cgroup_attach_lock_mode *lock_mode) __acquires(&cgroup_threadgroup_rwsem); -void cgroup_procs_write_finish(struct task_struct *task, bool locked) +void cgroup_procs_write_finish(struct task_struct *task, + enum cgroup_attach_lock_mode lock_mode) __releases(&cgroup_threadgroup_rwsem);void cgroup_lock_and_drain_offline (struct cgroup *cgrp);
kernel/cgroup/cgroup-v1.c +7 -7
@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL );for_each_root (root) {struct cgroup *from_cgrp;
@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (true); +cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL );cgroup_unlock ();return retval;
@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (true); +cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL );/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);
@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (true); +cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL );cgroup_unlock ();return ret; }
@@ -502,13 +502,13 @@ static ssize_t __cgroup1_procs_write(struct kernfs_open_file *of,struct task_struct *task;const struct cred *cred, *tcred;ssize_t ret; -bool locked; +enum cgroup_attach_lock_mode lock_mode; cgrp =cgroup_kn_lock_live (of->kn, false);if (!cgrp)return -ENODEV ; - task =cgroup_procs_write_start (buf, threadgroup, &locked); + task =cgroup_procs_write_start (buf, threadgroup, &lock_mode); ret =PTR_ERR_OR_ZERO (task);if (ret) goto out_unlock;
@@ -531,7 +531,7 @@ static ssize_t __cgroup1_procs_write(struct kernfs_open_file *of, ret =cgroup_attach_task (cgrp, task, threadgroup); out_finish: -cgroup_procs_write_finish (task, locked); +cgroup_procs_write_finish (task, lock_mode); out_unlock:cgroup_kn_unlock (of->kn);
kernel/cgroup/cgroup.c +49 -18
153 lines not on this page · raw message
Signature · 1 line
2.32.0.3.g01195cf9f
Re: [PATCH v5 1/3] cgroup: refactor the cgroup_attach_lock code to make it clearer
Quoted 6 lines · Yi Tao
Dynamic cgroup migration involving threadgroup locks can be in one of two states: no lock held, or holding the global lock. Explicitly declaring the different lock modes to make the code easier to understand and facilitates future extensions of the lock modes.
Signed-off-by Yi Tao <[email protected]>
Applied to cgroup/for-6.18. There was a conflict that I resolved. Please check the tree to make sure that it looks alright.
Thanks.
Signature · 1 line
tejun
PATCH v5 2/3 cgroup: relocate cgroup_attach_lock within cgroup_procs_write_start
In reply to Yi TaoLater patches will introduce a new parameter task to cgroup_attach_lock, thus adjusting the position of cgroup_attach_lock within cgroup_procs_write_start.
Between obtaining the threadgroup leader via PID and acquiring the cgroup attach lock, the threadgroup leader may change, which could lead to incorrect cgroup migration. Therefore, after acquiring the cgroup attach lock, we check whether the threadgroup leader has changed, and if so, retry the operation.
--- kernel/cgroup/cgroup.c | 61 ++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 23 deletions(-)
kernel/cgroup/cgroup.c +38 -23
@@ -2994,29 +2994,13 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,if (kstrtoint (strstrip (buf),0 , &pid) || pid <0 )return ERR_PTR (-EINVAL ); -/* -* If we migrate a single thread, we don't care about threadgroup -* stability. If the thread is `current`, it won't exit(2) under our -* hands or change PID through exec(2). We exclude -* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write -* callers by cgroup_mutex. -* Therefore, we can skip the global lock. -*/ -lockdep_assert_held (&cgroup_mutex); - -if (pid || threadgroup) - *lock_mode =CGRP_ATTACH_LOCK_GLOBAL ; -else - *lock_mode =CGRP_ATTACH_LOCK_NONE ; - -cgroup_attach_lock (*lock_mode); - +retry_find_task:rcu_read_lock ();if (pid) { tsk =find_task_by_vpid (pid);if (!tsk) { tsk =ERR_PTR (-ESRCH ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } }else { tsk = current;
@@ -3033,15 +3017,46 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,*/ if (tsk->no_cgroup_migration || (tsk->flags &PF_NO_SETAFFINITY )) { tsk =ERR_PTR (-EINVAL ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; }get_task_struct (tsk); - goto out_unlock_rcu; +rcu_read_unlock (); + +/* +* If we migrate a single thread, we don't care about threadgroup +* stability. If the thread is `current`, it won't exit(2) under our +* hands or change PID through exec(2). We exclude +* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write +* callers by cgroup_mutex. +* Therefore, we can skip the global lock. +*/ +lockdep_assert_held (&cgroup_mutex); + +if (pid || threadgroup) + *lock_mode =CGRP_ATTACH_LOCK_GLOBAL ; +else + *lock_mode =CGRP_ATTACH_LOCK_NONE ; + +cgroup_attach_lock (*lock_mode); + +if (threadgroup) { +if (!thread_group_leader (tsk)) { +/* +* a race with de_thread from another thread's exec() +* may strip us of our leadership, if this happens, +* there is no choice but to throw this task away and +* try again; this is +* "double-double-toil-and-trouble-check locking". +*/ +cgroup_attach_unlock (*lock_mode); +put_task_struct (tsk); + goto retry_find_task; + } + } + +return tsk; -out_unlock_threadgroup: -cgroup_attach_unlock (*lock_mode); - *lock_mode =CGRP_ATTACH_LOCK_NONE ; out_unlock_rcu:rcu_read_unlock ();return tsk;
Signature · 1 line
2.32.0.3.g01195cf9f
Re: [PATCH v5 2/3] cgroup: relocate cgroup_attach_lock within cgroup_procs_write_start
Quoted 11 lines · Yi Tao
Later patches will introduce a new parameter
tasktocgroup_attach_lock, thus adjusting the position ofcgroup_attach_lockwithincgroup_procs_write_start.Between obtaining the threadgroup leader via PID and acquiring the cgroup attach lock, the threadgroup leader may change, which could lead to incorrect cgroup migration. Therefore, after acquiring the cgroup attach lock, we check whether the threadgroup leader has changed, and if so, retry the operation.
Signed-off-by Yi Tao <[email protected]>
Applied to cgroup/for-6.18 with minor comment adjustments.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v5 2/3] cgroup: relocate cgroup_attach_lock within cgroup_procs_write_start
In reply to Yi TaoQuoted 72 lines · Yi Tao
Later patches will introduce a new parameter
tasktocgroup_attach_lock, thus adjusting the position ofcgroup_attach_lockwithincgroup_procs_write_start.Between obtaining the threadgroup leader via PID and acquiring the cgroup attach lock, the threadgroup leader may change, which could lead to incorrect cgroup migration. Therefore, after acquiring the cgroup attach lock, we check whether the threadgroup leader has changed, and if so, retry the operation.
Signed-off-by Yi Tao <[email protected]>--- kernel/cgroup/cgroup.c | 61 ++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 23 deletions(-)kernel/cgroup/cgroup.c +14
-20@@ -2994,29 +2994,13 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,if (kstrtoint (strstrip (buf),0 , &pid) || pid <0 )return ERR_PTR (-EINVAL ); -/* -* If we migrate a single thread, we don't care about threadgroup -* stability. If the thread is `current`, it won't exit(2) under our -* hands or change PID through exec(2). We exclude -* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write -* callers by cgroup_mutex. -* Therefore, we can skip the global lock. -*/ -lockdep_assert_held (&cgroup_mutex); - -if (pid || threadgroup) - *lock_mode =CGRP_ATTACH_LOCK_GLOBAL ; -else - *lock_mode =CGRP_ATTACH_LOCK_NONE ; - -cgroup_attach_lock (*lock_mode); - +retry_find_task:rcu_read_lock ();if (pid) { tsk =find_task_by_vpid (pid);if (!tsk) { tsk =ERR_PTR (-ESRCH ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; } }else { tsk = current;@@ -3033,15 +3017,46 @@ struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,*/ if (tsk->no_cgroup_migration || (tsk->flags &PF_NO_SETAFFINITY )) { tsk =ERR_PTR (-EINVAL ); - goto out_unlock_threadgroup; + goto out_unlock_rcu; }get_task_struct (tsk); - goto out_unlock_rcu; +rcu_read_unlock (); + +/* +* If we migrate a single thread, we don't care about threadgroup +* stability. If the thread is `current`, it won't exit(2) under our +* hands or change PID through exec(2). We exclude +* cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write +* callers by cgroup_mutex. +* Therefore, we can skip the global lock. +*/ +lockdep_assert_held (&cgroup_mutex);
kernel/cgroup/cgroup.c struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, · line 3044
Quoted 10 lines
kernel/cgroup/cgroup.c +10
-0+ +if (pid || threadgroup) + *lock_mode =CGRP_ATTACH_LOCK_GLOBAL ; +else + *lock_mode =CGRP_ATTACH_LOCK_NONE ; + +cgroup_attach_lock (*lock_mode); + +if (threadgroup) { +if (!thread_group_leader (tsk)) {
Nit: You can combine the 2 conditions together to avoid excessive indent.
if (threadgroup && !thread_group_leader(tsk)) {
kernel/cgroup/cgroup.c struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, · line 3046
Quoted 2 lines
kernel/cgroup/cgroup.c +2
-0+/* +* a race with de_thread from another thread's exec()
Should be "de_thread()" to signal that it is a function.
kernel/cgroup/cgroup.c struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, · line 3050
Quoted 4 lines
kernel/cgroup/cgroup.c +4
-0+* may strip us of our leadership, if this happens, +* there is no choice but to throw this task away and +* try again; this is +* "double-double-toil-and-trouble-check locking".
This "double-double-toil-and-trouble-check" is a new term in the kernel source tree. I will suggest to use something simpler to avoid confusion.
Cheers, Longman
PATCH v5 3/3 cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
In reply to Yi TaoThe static usage pattern of creating a cgroup, enabling controllers, and then seeding it with CLONE_INTO_CGROUP doesn't require write locking cgroup_threadgroup_rwsem and thus doesn't benefit from this patch.
To avoid affecting other users, the per threadgroup rwsem is only used when the favordynmods is enabled.
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on signal_struct->group_rwsem when writing pid to cgroup.procs/threads instead of holding a global write lock.
Cgroup process migration has historically relied on signal_struct->group_rwsem to protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem"), this was changed to a global cgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.
The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------
cgroup_lock() | down_read(&g_rwsem) | cgroup_lock() down_write(&p_rwsem) | down_read(&p_rwsem) | down_write(&g_rwsem) critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) | up_write(&g_rwsem) cgroup_unlock() | up_read(&g_rwsem) | cgroup_unlock()
g_rwsem denotes cgroup_threadgroup_rwsem, p_rwsem denotes signal_struct->group_rwsem.
This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
With this patch, under heavy fork and exec interference, the long-tail latency of cgroup migration has been reduced from milliseconds to microseconds. Under heavy cgroup migration interference, the multi-CPU score of the spawn test case in UnixBench increased by 9%.
--- include/linux/cgroup-defs.h | 17 ++++++++- include/linux/sched/signal.h | 4 ++ init/init_task.c | 3 ++ kernel/cgroup/cgroup-internal.h | 6 ++- kernel/cgroup/cgroup-v1.c | 8 ++-- kernel/cgroup/cgroup.c | 67 +++++++++++++++++++++++++-------- kernel/fork.c | 4 ++ 7 files changed, 87 insertions(+), 22 deletions(-)
include/linux/cgroup-defs.h +16 -1
@@ -91,6 +91,12 @@ enum {* cgroup_threadgroup_rwsem. This makes hot path operations such as * forks and exits into the slow path and more expensive. * +* Alleviate the contention between fork, exec, exit operations and +* writing to cgroup.procs by taking a per threadgroup rwsem instead of +* the global cgroup_threadgroup_rwsem. Fork and other operations +* from threads in different thread groups no longer contend with +* writing to cgroup.procs. +* * The static usage pattern of creating a cgroup, enabling controllers, * and then seeding it with CLONE_INTO_CGROUP doesn't require write * locking cgroup_threadgroup_rwsem and thus doesn't benefit from
@@ -146,6 +152,9 @@ enum cgroup_attach_lock_mode {/* When pid=0 && threadgroup=false, see comments in cgroup_procs_write_start */ CGRP_ATTACH_LOCK_NONE , + +/* When favordynmods is on, see comments above CGRP_ROOT_FAVOR_DYNMODS */ + CGRP_ATTACH_LOCK_PER_THREADGROUP, };/*
@@ -830,6 +839,7 @@ struct cgroup_subsys { };extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +extern bool cgroup_enable_per_threadgroup_rwsem;struct cgroup_of_peak {unsigned long value;
@@ -841,11 +851,14 @@ struct cgroup_of_peak {* @tsk: target task * * Allows cgroup operations to synchronize against threadgroup changes -* using a percpu_rw_semaphore. +* using a global percpu_rw_semaphore and a per threadgroup rw_semaphore when +* favordynmods is on. See the comment above CGRP_ROOT_FAVOR_DYNMODS definition. */ static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +if (cgroup_enable_per_threadgroup_rwsem) +down_read (&tsk->signal->cgroup_threadgroup_rwsem); }/**
@@ -856,6 +869,8 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +if (cgroup_enable_per_threadgroup_rwsem) +up_read (&tsk->signal->cgroup_threadgroup_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem); }
include/linux/sched/signal.h +4 -0
@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore cgroup_threadgroup_rwsem; +#endif +/* * Thread is the potential origin of an oom condition; kill first on * oom
init/init_task.c +3 -0
@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim =INIT_RLIMITS , +#ifdef CONFIG_CGROUPS + .cgroup_threadgroup_rwsem = __RWSEM_INITIALIZER(init_signals.cgroup_threadgroup_rwsem), +#endif .cred_guard_mutex =__MUTEX_INITIALIZER (init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERS
kernel/cgroup/cgroup-internal.h +4 -2
@@ -249,8 +249,10 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (enum cgroup_attach_lock_mode lock_mode); -void cgroup_attach_unlock (enum cgroup_attach_lock_mode lock_mode); +void cgroup_attach_lock (enum cgroup_attach_lock_mode lock_mode, +struct task_struct *tsk); +void cgroup_attach_unlock (enum cgroup_attach_lock_mode lock_mode, +struct task_struct *tsk);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup,enum cgroup_attach_lock_mode *lock_mode) __acquires(&cgroup_threadgroup_rwsem);
kernel/cgroup/cgroup-v1.c +4 -4
@@ -68,7 +68,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ); +cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );for_each_root (root) {struct cgroup *from_cgrp;
@@ -80,7 +80,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ); +cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );cgroup_unlock ();return retval;
@@ -117,7 +117,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ); +cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);
@@ -153,7 +153,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ); +cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );cgroup_unlock ();return ret; }
kernel/cgroup/cgroup.c +52 -15
159 lines not on this page · raw message
kernel/fork.c +4 -0
@@ -1688,6 +1688,10 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)tty_audit_fork (sig);sched_autogroup_fork (sig); +#ifdef CONFIG_CGROUPS +init_rwsem (&sig->cgroup_threadgroup_rwsem); +#endif + sig->oom_score_adj = current->signal->oom_score_adj; sig->oom_score_adj_min = current->signal->oom_score_adj_min;
Signature · 1 line
2.32.0.3.g01195cf9f
Re: [PATCH v5 3/3] cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
Applied to cgroup/for-6.18 with a comment update and s/WARN_ONCE()/pr_warn_once()/ as the backtrace isn't likely helpful here. The applied version follows:
------ 8< ------
From 0568f89d4fb82d98001baeb870e92f43cd1f7317 Mon Sep 17 00:00:00 2001
From: Yi Tao <[email protected]>
Date: Wed, 10 Sep 2025 14:59:35 +0800
Subject: [PATCH] cgroup: replace global percpu_rwsem with per threadgroup
resem when writing to cgroup.procs
The static usage pattern of creating a cgroup, enabling controllers, and then seeding it with CLONE_INTO_CGROUP doesn't require write locking cgroup_threadgroup_rwsem and thus doesn't benefit from this patch.
To avoid affecting other users, the per threadgroup rwsem is only used when the favordynmods is enabled.
As computer hardware advances, modern systems are typically equipped with many CPU cores and large amounts of memory, enabling the deployment of numerous applications. On such systems, container creation and deletion become frequent operations, making cgroup process migration no longer a cold path. This leads to noticeable contention with common process operations such as fork, exec, and exit.
To alleviate the contention between cgroup process migration and operations like process fork, this patch modifies lock to take the write lock on signal_struct->group_rwsem when writing pid to cgroup.procs/threads instead of holding a global write lock.
Cgroup process migration has historically relied on signal_struct->group_rwsem to protect thread group integrity. In commit <1ed1328792ff> ("sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem"), this was changed to a global cgroup_threadgroup_rwsem. The advantage of using a global lock was simplified handling of process group migrations. This patch retains the use of the global lock for protecting process group migration, while reducing contention by using per thread group lock during cgroup.procs/threads writes.
The locking behavior is as follows:
write cgroup.procs/threads | process fork,exec,exit | process group migration
------------------------------------------------------------------------------
cgroup_lock() | down_read(&g_rwsem) | cgroup_lock() down_write(&p_rwsem) | down_read(&p_rwsem) | down_write(&g_rwsem) critical section | critical section | critical section up_write(&p_rwsem) | up_read(&p_rwsem) | up_write(&g_rwsem) cgroup_unlock() | up_read(&g_rwsem) | cgroup_unlock()
g_rwsem denotes cgroup_threadgroup_rwsem, p_rwsem denotes signal_struct->group_rwsem.
This patch eliminates contention between cgroup migration and fork operations for threads that belong to different thread groups, thereby reducing the long-tail latency of cgroup migrations and lowering system load.
With this patch, under heavy fork and exec interference, the long-tail latency of cgroup migration has been reduced from milliseconds to microseconds. Under heavy cgroup migration interference, the multi-CPU score of the spawn test case in UnixBench increased by 9%.
tj: Update comment in cgroup_favor_dynmods() and switch WARN_ONCE() to
pr_warn_once().
--- include/linux/cgroup-defs.h | 17 +++++++- include/linux/sched/signal.h | 4 ++ init/init_task.c | 3 ++ kernel/cgroup/cgroup-internal.h | 6 ++- kernel/cgroup/cgroup-v1.c | 8 ++-- kernel/cgroup/cgroup.c | 73 ++++++++++++++++++++++++++------- kernel/fork.c | 4 ++ 7 files changed, 93 insertions(+), 22 deletions(-)
include/linux/cgroup-defs.h +16 -1
@@ -91,6 +91,12 @@ enum {* cgroup_threadgroup_rwsem. This makes hot path operations such as * forks and exits into the slow path and more expensive. * +* Alleviate the contention between fork, exec, exit operations and +* writing to cgroup.procs by taking a per threadgroup rwsem instead of +* the global cgroup_threadgroup_rwsem. Fork and other operations +* from threads in different thread groups no longer contend with +* writing to cgroup.procs. +* * The static usage pattern of creating a cgroup, enabling controllers, * and then seeding it with CLONE_INTO_CGROUP doesn't require write * locking cgroup_threadgroup_rwsem and thus doesn't benefit from
@@ -146,6 +152,9 @@ enum cgroup_attach_lock_mode {/* When pid=0 && threadgroup=false, see comments in cgroup_procs_write_start */ CGRP_ATTACH_LOCK_NONE , + +/* When favordynmods is on, see comments above CGRP_ROOT_FAVOR_DYNMODS */ + CGRP_ATTACH_LOCK_PER_THREADGROUP, };/*
@@ -846,6 +855,7 @@ struct cgroup_subsys { };extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +extern bool cgroup_enable_per_threadgroup_rwsem;struct cgroup_of_peak {unsigned long value;
@@ -857,11 +867,14 @@ struct cgroup_of_peak {* @tsk: target task * * Allows cgroup operations to synchronize against threadgroup changes -* using a percpu_rw_semaphore. +* using a global percpu_rw_semaphore and a per threadgroup rw_semaphore when +* favordynmods is on. See the comment above CGRP_ROOT_FAVOR_DYNMODS definition. */ static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) {percpu_down_read (&cgroup_threadgroup_rwsem); +if (cgroup_enable_per_threadgroup_rwsem) +down_read (&tsk->signal->cgroup_threadgroup_rwsem); }/**
@@ -872,6 +885,8 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { +if (cgroup_enable_per_threadgroup_rwsem) +up_read (&tsk->signal->cgroup_threadgroup_rwsem);percpu_up_read (&cgroup_threadgroup_rwsem); }
include/linux/sched/signal.h +4 -0
@@ -226,6 +226,10 @@ struct signal_struct {struct tty_audit_buf *tty_audit_buf;#endif +#ifdef CONFIG_CGROUPS +struct rw_semaphore cgroup_threadgroup_rwsem; +#endif +/* * Thread is the potential origin of an oom condition; kill first on * oom
init/init_task.c +3 -0
@@ -27,6 +27,9 @@ static struct signal_struct init_signals = { }, .multiprocess = HLIST_HEAD_INIT, .rlim =INIT_RLIMITS , +#ifdef CONFIG_CGROUPS + .cgroup_threadgroup_rwsem = __RWSEM_INITIALIZER(init_signals.cgroup_threadgroup_rwsem), +#endif .cred_guard_mutex =__MUTEX_INITIALIZER (init_signals.cred_guard_mutex), .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERS
kernel/cgroup/cgroup-internal.h +4 -2
@@ -249,8 +249,10 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,int cgroup_attach_task (struct cgroup *dst_cgrp,struct task_struct *leader,bool threadgroup); -void cgroup_attach_lock (enum cgroup_attach_lock_mode lock_mode); -void cgroup_attach_unlock (enum cgroup_attach_lock_mode lock_mode); +void cgroup_attach_lock (enum cgroup_attach_lock_mode lock_mode, +struct task_struct *tsk); +void cgroup_attach_unlock (enum cgroup_attach_lock_mode lock_mode, +struct task_struct *tsk);struct task_struct *cgroup_procs_write_start (char *buf,bool threadgroup,enum cgroup_attach_lock_mode *lock_mode) __acquires(&cgroup_threadgroup_rwsem);
kernel/cgroup/cgroup-v1.c +4 -4
@@ -69,7 +69,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)int retval =0 ;cgroup_lock (); -cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ); +cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );for_each_root (root) {struct cgroup *from_cgrp;
@@ -81,7 +81,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)if (retval)break ; } -cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ); +cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );cgroup_unlock ();return retval;
@@ -118,7 +118,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)cgroup_lock (); -cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ); +cgroup_attach_lock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );/* all tasks in @from are being moved, all csets are source */ spin_lock_irq (&css_set_lock);
@@ -154,7 +154,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) }while (task && !ret); out_err:cgroup_migrate_finish (&mgctx); -cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ); +cgroup_attach_unlock (CGRP_ATTACH_LOCK_GLOBAL ,NULL );cgroup_unlock ();return ret; }
kernel/cgroup/cgroup.c +58 -15
164 lines not on this page · raw message
kernel/fork.c +4 -0
@@ -1688,6 +1688,10 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)tty_audit_fork (sig);sched_autogroup_fork (sig); +#ifdef CONFIG_CGROUPS +init_rwsem (&sig->cgroup_threadgroup_rwsem); +#endif + sig->oom_score_adj = current->signal->oom_score_adj; sig->oom_score_adj_min = current->signal->oom_score_adj_min;
Signature · 1 line
2.51.0
Re: [PATCH v5 3/3] cgroup: replace global percpu_rwsem with per threadgroup resem when writing to cgroup.procs
Thank you for the review and guidance.