Skip to content

Commit 1ed1328

Browse files
committed
sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem
Note: This commit was originally committed as d59cfc0 but got reverted by 0c98625 due to the performance regression from the percpu_rwsem write down/up operations added to cgroup task migration path. percpu_rwsem changes which alleviate the performance issue are pending for v4.4-rc1 merge window. Re-apply. The cgroup side of threadgroup locking uses signal_struct->group_rwsem to synchronize against threadgroup changes. This per-process rwsem adds small overhead to thread creation, exit and exec paths, forces cgroup code paths to do lock-verify-unlock-retry dance in a couple places and makes it impossible to atomically perform operations across multiple processes. This patch replaces signal_struct->group_rwsem with a global percpu_rwsem cgroup_threadgroup_rwsem which is cheaper on the reader side and contained in cgroups proper. This patch converts one-to-one. This does make writer side heavier and lower the granularity; however, cgroup process migration is a fairly cold path, we do want to optimize thread operations over it and cgroup migration operations don't take enough time for the lower granularity to matter. Signed-off-by: Tejun Heo <[email protected]> Link: http://lkml.kernel.org/g/[email protected] Cc: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]>
1 parent 0c98625 commit 1ed1328

File tree

5 files changed

+45
-83
lines changed

5 files changed

+45
-83
lines changed

include/linux/cgroup-defs.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,31 @@ struct cgroup_subsys {
473473
unsigned int depends_on;
474474
};
475475

476-
void cgroup_threadgroup_change_begin(struct task_struct *tsk);
477-
void cgroup_threadgroup_change_end(struct task_struct *tsk);
476+
extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
477+
478+
/**
479+
* cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
480+
* @tsk: target task
481+
*
482+
* Called from threadgroup_change_begin() and allows cgroup operations to
483+
* synchronize against threadgroup changes using a percpu_rw_semaphore.
484+
*/
485+
static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
486+
{
487+
percpu_down_read(&cgroup_threadgroup_rwsem);
488+
}
489+
490+
/**
491+
* cgroup_threadgroup_change_end - threadgroup exclusion for cgroups
492+
* @tsk: target task
493+
*
494+
* Called from threadgroup_change_end(). Counterpart of
495+
* cgroup_threadcgroup_change_begin().
496+
*/
497+
static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
498+
{
499+
percpu_up_read(&cgroup_threadgroup_rwsem);
500+
}
478501

479502
#else /* CONFIG_CGROUPS */
480503

include/linux/init_task.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
extern struct files_struct init_files;
2626
extern struct fs_struct init_fs;
2727

28-
#ifdef CONFIG_CGROUPS
29-
#define INIT_GROUP_RWSEM(sig) \
30-
.group_rwsem = __RWSEM_INITIALIZER(sig.group_rwsem),
31-
#else
32-
#define INIT_GROUP_RWSEM(sig)
33-
#endif
34-
3528
#ifdef CONFIG_CPUSETS
3629
#define INIT_CPUSET_SEQ(tsk) \
3730
.mems_allowed_seq = SEQCNT_ZERO(tsk.mems_allowed_seq),
@@ -64,7 +57,6 @@ extern struct fs_struct init_fs;
6457
INIT_PREV_CPUTIME(sig) \
6558
.cred_guard_mutex = \
6659
__MUTEX_INITIALIZER(sig.cred_guard_mutex), \
67-
INIT_GROUP_RWSEM(sig) \
6860
}
6961

7062
extern struct nsproxy init_nsproxy;

include/linux/sched.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -762,18 +762,6 @@ struct signal_struct {
762762
unsigned audit_tty_log_passwd;
763763
struct tty_audit_buf *tty_audit_buf;
764764
#endif
765-
#ifdef CONFIG_CGROUPS
766-
/*
767-
* group_rwsem prevents new tasks from entering the threadgroup and
768-
* member tasks from exiting,a more specifically, setting of
769-
* PF_EXITING. fork and exit paths are protected with this rwsem
770-
* using threadgroup_change_begin/end(). Users which require
771-
* threadgroup to remain stable should use threadgroup_[un]lock()
772-
* which also takes care of exec path. Currently, cgroup is the
773-
* only user.
774-
*/
775-
struct rw_semaphore group_rwsem;
776-
#endif
777765

778766
oom_flags_t oom_flags;
779767
short oom_score_adj; /* OOM kill score adjustment */

kernel/cgroup.c

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <linux/slab.h>
4747
#include <linux/spinlock.h>
4848
#include <linux/rwsem.h>
49+
#include <linux/percpu-rwsem.h>
4950
#include <linux/string.h>
5051
#include <linux/sort.h>
5152
#include <linux/kmod.h>
@@ -103,6 +104,8 @@ static DEFINE_SPINLOCK(cgroup_idr_lock);
103104
*/
104105
static DEFINE_SPINLOCK(release_agent_path_lock);
105106

107+
struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
108+
106109
#define cgroup_assert_mutex_or_rcu_locked() \
107110
RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
108111
!lockdep_is_held(&cgroup_mutex), \
@@ -871,48 +874,6 @@ static struct css_set *find_css_set(struct css_set *old_cset,
871874
return cset;
872875
}
873876

874-
void cgroup_threadgroup_change_begin(struct task_struct *tsk)
875-
{
876-
down_read(&tsk->signal->group_rwsem);
877-
}
878-
879-
void cgroup_threadgroup_change_end(struct task_struct *tsk)
880-
{
881-
up_read(&tsk->signal->group_rwsem);
882-
}
883-
884-
/**
885-
* threadgroup_lock - lock threadgroup
886-
* @tsk: member task of the threadgroup to lock
887-
*
888-
* Lock the threadgroup @tsk belongs to. No new task is allowed to enter
889-
* and member tasks aren't allowed to exit (as indicated by PF_EXITING) or
890-
* change ->group_leader/pid. This is useful for cases where the threadgroup
891-
* needs to stay stable across blockable operations.
892-
*
893-
* fork and exit explicitly call threadgroup_change_{begin|end}() for
894-
* synchronization. While held, no new task will be added to threadgroup
895-
* and no existing live task will have its PF_EXITING set.
896-
*
897-
* de_thread() does threadgroup_change_{begin|end}() when a non-leader
898-
* sub-thread becomes a new leader.
899-
*/
900-
static void threadgroup_lock(struct task_struct *tsk)
901-
{
902-
down_write(&tsk->signal->group_rwsem);
903-
}
904-
905-
/**
906-
* threadgroup_unlock - unlock threadgroup
907-
* @tsk: member task of the threadgroup to unlock
908-
*
909-
* Reverse threadgroup_lock().
910-
*/
911-
static inline void threadgroup_unlock(struct task_struct *tsk)
912-
{
913-
up_write(&tsk->signal->group_rwsem);
914-
}
915-
916877
static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
917878
{
918879
struct cgroup *root_cgrp = kf_root->kn->priv;
@@ -2113,9 +2074,9 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,
21132074
lockdep_assert_held(&css_set_rwsem);
21142075

21152076
/*
2116-
* We are synchronized through threadgroup_lock() against PF_EXITING
2117-
* setting such that we can't race against cgroup_exit() changing the
2118-
* css_set to init_css_set and dropping the old one.
2077+
* We are synchronized through cgroup_threadgroup_rwsem against
2078+
* PF_EXITING setting such that we can't race against cgroup_exit()
2079+
* changing the css_set to init_css_set and dropping the old one.
21192080
*/
21202081
WARN_ON_ONCE(tsk->flags & PF_EXITING);
21212082
old_cset = task_css_set(tsk);
@@ -2172,10 +2133,11 @@ static void cgroup_migrate_finish(struct list_head *preloaded_csets)
21722133
* @src_cset and add it to @preloaded_csets, which should later be cleaned
21732134
* up by cgroup_migrate_finish().
21742135
*
2175-
* This function may be called without holding threadgroup_lock even if the
2176-
* target is a process. Threads may be created and destroyed but as long
2177-
* as cgroup_mutex is not dropped, no new css_set can be put into play and
2178-
* the preloaded css_sets are guaranteed to cover all migrations.
2136+
* This function may be called without holding cgroup_threadgroup_rwsem
2137+
* even if the target is a process. Threads may be created and destroyed
2138+
* but as long as cgroup_mutex is not dropped, no new css_set can be put
2139+
* into play and the preloaded css_sets are guaranteed to cover all
2140+
* migrations.
21792141
*/
21802142
static void cgroup_migrate_add_src(struct css_set *src_cset,
21812143
struct cgroup *dst_cgrp,
@@ -2278,7 +2240,7 @@ static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
22782240
* @threadgroup: whether @leader points to the whole process or a single task
22792241
*
22802242
* Migrate a process or task denoted by @leader to @cgrp. If migrating a
2281-
* process, the caller must be holding threadgroup_lock of @leader. The
2243+
* process, the caller must be holding cgroup_threadgroup_rwsem. The
22822244
* caller is also responsible for invoking cgroup_migrate_add_src() and
22832245
* cgroup_migrate_prepare_dst() on the targets before invoking this
22842246
* function and following up with cgroup_migrate_finish().
@@ -2406,7 +2368,7 @@ static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
24062368
* @leader: the task or the leader of the threadgroup to be attached
24072369
* @threadgroup: attach the whole threadgroup?
24082370
*
2409-
* Call holding cgroup_mutex and threadgroup_lock of @leader.
2371+
* Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
24102372
*/
24112373
static int cgroup_attach_task(struct cgroup *dst_cgrp,
24122374
struct task_struct *leader, bool threadgroup)
@@ -2528,7 +2490,7 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
25282490
get_task_struct(tsk);
25292491
rcu_read_unlock();
25302492

2531-
threadgroup_lock(tsk);
2493+
percpu_down_write(&cgroup_threadgroup_rwsem);
25322494
if (threadgroup) {
25332495
if (!thread_group_leader(tsk)) {
25342496
/*
@@ -2538,7 +2500,7 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
25382500
* try again; this is
25392501
* "double-double-toil-and-trouble-check locking".
25402502
*/
2541-
threadgroup_unlock(tsk);
2503+
percpu_up_write(&cgroup_threadgroup_rwsem);
25422504
put_task_struct(tsk);
25432505
goto retry_find_task;
25442506
}
@@ -2548,7 +2510,7 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
25482510
if (!ret)
25492511
ret = cgroup_attach_task(cgrp, tsk, threadgroup);
25502512

2551-
threadgroup_unlock(tsk);
2513+
percpu_up_write(&cgroup_threadgroup_rwsem);
25522514

25532515
put_task_struct(tsk);
25542516
out_unlock_cgroup:
@@ -2751,17 +2713,17 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
27512713
goto out_finish;
27522714
last_task = task;
27532715

2754-
threadgroup_lock(task);
2716+
percpu_down_write(&cgroup_threadgroup_rwsem);
27552717
/* raced against de_thread() from another thread? */
27562718
if (!thread_group_leader(task)) {
2757-
threadgroup_unlock(task);
2719+
percpu_up_write(&cgroup_threadgroup_rwsem);
27582720
put_task_struct(task);
27592721
continue;
27602722
}
27612723

27622724
ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
27632725

2764-
threadgroup_unlock(task);
2726+
percpu_up_write(&cgroup_threadgroup_rwsem);
27652727
put_task_struct(task);
27662728

27672729
if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
@@ -5083,6 +5045,7 @@ int __init cgroup_init(void)
50835045
unsigned long key;
50845046
int ssid, err;
50855047

5048+
BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
50865049
BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
50875050
BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
50885051

kernel/fork.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,10 +1149,6 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
11491149
tty_audit_fork(sig);
11501150
sched_autogroup_fork(sig);
11511151

1152-
#ifdef CONFIG_CGROUPS
1153-
init_rwsem(&sig->group_rwsem);
1154-
#endif
1155-
11561152
sig->oom_score_adj = current->signal->oom_score_adj;
11571153
sig->oom_score_adj_min = current->signal->oom_score_adj_min;
11581154

0 commit comments

Comments
 (0)