Skip to contentSkip to conversation

cgroup, sched: restructure threadgroup locking and replace it with a percpu_rwsem

PATCH In review Tejun Heo 4 participants 14 messages May 13, 2015 – May 27, 2015

Subsystems (4)
lore
Share & tools
lore

Tejun Heo

author maintainer
#0 lore raw

PATCH 1/1 cgroup, sched: restructure threadgroup locking and replace it with a percpu_rwsem

threadgroup locking was added because cgroup needs threadgroups to stay stable across attach operations. It was implemented as a per-signal_struct generic locking mechanism so that other users which require threadgroups stable across blocking operations can use it too; however, it hasn't grown any other use cases and still conditionalized on CONFIG_CGROUPS.

It turns out that a single percpu_rwsem would serve cgroup's use case better as it's lighter on the thread operations and allows atomic operations across multiple processes. We can change the generic threadgroup lock mechanism to do that but it's pointless to put this in core task code. What's necessary from the task code is a place which subsystems needing synchronization against threadgroup operations can hook into so that they can implement the necessary exclusion.

This patchset moves the specific locking details into cgroup proper leaving threadgroup_changes_begin/end() as the section markers and then converts the cgroup specific locking to use a percp_rwsem.

This patchset contains the following three patches.

0001-sched-cgroup-reorganize-threadgroup-locking.patch 0002-sched-cgroup-replace-signal_struct-group_rwsem-with-.patch 0003-cgroup-simplify-threadgroup-locking.patch

0001 moves threadgroup locking details into cgroup proper.

0002-0003 convert per-signal_struct group_rwsem with a global percpu_rwsem.

This patchset is on top of

cgroup/for-4.2 d0f702e648dc ("cgroup: fix some comment typos")
+ [1] [PATCH] cgroup: separate out include/linux/cgroup-defs.h
+ [2] [PATCH] cgroup: reorganize include/linux/cgroup.h

git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git review-threadgroup-percpu-rwsem

diffstat follows. Thanks.

 include/linux/cgroup-defs.h |   33 ++++++++++++++++++++
 include/linux/init_task.h   |    8 ----
 include/linux/sched.h       |   65 ++++++++++------------------------------
 init/Kconfig                |    1
 kernel/cgroup.c             |   71 ++++++++++++++++----------------------------
 kernel/fork.c               |    4 --
 6 files changed, 78 insertions(+), 104 deletions(-)

--
tejun

[1] http://lkml.kernel.org/g/[email protected]
[2] http://lkml.kernel.org/g/[email protected]

author maintainer
#1 lore raw

PATCH 1/3 sched, cgroup: reorganize threadgroup locking

threadgroup_change_begin/end() are used to mark the beginning and end of threadgroup modifying operations to allow code paths which require a threadgroup to stay stable across blocking operations to synchronize against those sections using threadgroup_lock/unlock().

It's currently implemented as a general mechanism in sched.h using per-signal_struct rwsem; however, this never grew non-cgroup use cases and becomes noop if !CONFIG_CGROUPS. It turns out that cgroups is gonna be better served with a different sycnrhonization scheme and is a bit silly to keep cgroups specific details as a general mechanism.

What's general here is identifying the places where threadgroups are modified. This patch restructures threadgroup locking so that threadgroup_change_begin/end() become a place where subsystems which need to sycnhronize against threadgroup changes can hook into.

cgroup_threadgroup_change_begin/end() which operate on the per-signal_struct rwsem are created and threadgroup_lock/unlock() are moved to cgroup.c and made static.

This is pure reorganization which doesn't cause any functional changes.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 include/linux/cgroup-defs.h | 10 +++++++++
 include/linux/sched.h       | 53 +++++++++++++++------------------------------
 kernel/cgroup.c             | 42 +++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 36 deletions(-)
include/linux/cgroup-defs.h +10 -0
index 55f3120..1b8c938 100644
@@ -14,6 +14,7 @@
 #include <linux/mutex.h>
 #include <linux/rcupdate.h>
 #include <linux/percpu-refcount.h>
+#include <linux/percpu-rwsem.h>
 #include <linux/workqueue.h>
 
 #ifdef CONFIG_CGROUPS
@@ -460,5 +461,14 @@ struct cgroup_subsys {
 	unsigned int depends_on;
 };
 
+void cgroup_threadgroup_change_begin(struct task_struct *tsk);
+void cgroup_threadgroup_change_end(struct task_struct *tsk);
+
+#else	/* CONFIG_CGROUPS */
+
+static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) {}
+static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
+
 #endif	/* CONFIG_CGROUPS */
+
 #endif	/* _LINUX_CGROUP_DEFS_H */
include/linux/sched.h +17 -36

76 lines not on this page · raw message

kernel/cgroup.c +42 -0

48 lines not on this page · raw message

Signature · 1 line
2.1.0
#2 lore raw

Re: [PATCH 1/3] sched, cgroup: reorganize threadgroup locking

Hello,

On (05/13/15 16:35), Tejun Heo wrote:
[..]

include/linux/sched.h static inline void unlock_task_sighand(struct task_struct *tsk, · line 2665

Quoted 5 lines
include/linux/sched.h +2 -2
-static inline void threadgroup_lock(struct task_struct *tsk)
+static inline void threadgroup_change_begin(struct task_struct *tsk)
 {
-	down_write(&tsk->signal->group_rwsem);
+	might_sleep();

I think cgroup_threadgroup_change_begin()->down_read() already does might_sleep() check.

	-ss
Quoted 88 lines
include/linux/sched.h +6 -11
+	cgroup_threadgroup_change_begin(tsk);
 }
 
 /**
- * threadgroup_unlock - unlock threadgroup
- * @tsk: member task of the threadgroup to unlock
+ * threadgroup_change_end - mark the end of changes to a threadgroup
+ * @tsk: task causing the changes
  *
- * Reverse threadgroup_lock().
+ * See threadgroup_change_begin().
  */
-static inline void threadgroup_unlock(struct task_struct *tsk)
+static inline void threadgroup_change_end(struct task_struct *tsk)
 {
-	up_write(&tsk->signal->group_rwsem);
+	cgroup_threadgroup_change_end(tsk);
 }
-#else
-static inline void threadgroup_change_begin(struct task_struct *tsk) {}
-static inline void threadgroup_change_end(struct task_struct *tsk) {}
-static inline void threadgroup_lock(struct task_struct *tsk) {}
-static inline void threadgroup_unlock(struct task_struct *tsk) {}
-#endif
 
 #ifndef __HAVE_THREAD_FUNCTIONS
kernel/cgroup.c +42 -0
index cfa27f9..9309452 100644
@@ -848,6 +848,48 @@ static struct css_set *find_css_set(struct css_set *old_cset,
 	return cset;
 }
 
+void cgroup_threadgroup_change_begin(struct task_struct *tsk)
+{
+	down_read(&tsk->signal->group_rwsem);
+}
+
+void cgroup_threadgroup_change_end(struct task_struct *tsk)
+{
+	up_read(&tsk->signal->group_rwsem);
+}
+
+/**
+ * threadgroup_lock - lock threadgroup
+ * @tsk: member task of the threadgroup to lock
+ *
+ * Lock the threadgroup @tsk belongs to.  No new task is allowed to enter
+ * and member tasks aren't allowed to exit (as indicated by PF_EXITING) or
+ * change ->group_leader/pid.  This is useful for cases where the threadgroup
+ * needs to stay stable across blockable operations.
+ *
+ * fork and exit explicitly call threadgroup_change_{begin|end}() for
+ * synchronization.  While held, no new task will be added to threadgroup
+ * and no existing live task will have its PF_EXITING set.
+ *
+ * de_thread() does threadgroup_change_{begin|end}() when a non-leader
+ * sub-thread becomes a new leader.
+ */
+static void threadgroup_lock(struct task_struct *tsk)
+{
+	down_write(&tsk->signal->group_rwsem);
+}
+
+/**
+ * threadgroup_unlock - unlock threadgroup
+ * @tsk: member task of the threadgroup to unlock
+ *
+ * Reverse threadgroup_lock().
+ */
+static inline void threadgroup_unlock(struct task_struct *tsk)
+{
+	up_write(&tsk->signal->group_rwsem);
+}
+
 static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
 {
 	struct cgroup *root_cgrp = kf_root->kn->priv;
Signature · 6 lines
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe cgroups" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#3 lore raw

Re: [PATCH 1/3] sched, cgroup: reorganize threadgroup locking

Hello, Sergey.

Quoted 7 lines · Sergey Senozhatsky

include/linux/sched.h static inline void unlock_task_sighand(struct task_struct *tsk, · line 2665

Quoted 4 lines
include/linux/sched.h +2 -1
+static inline void threadgroup_change_begin(struct task_struct *tsk)
 {
-	down_write(&tsk->signal->group_rwsem);
+	might_sleep();

I think cgroup_threadgroup_change_begin()->down_read() already does might_sleep() check.

Sure but it's a layering thing. threadgroup_change_begin() should be called from a blockable context whether the hook users actually make use of it or not. e.g. We want might_sleep() even when !CONFIG_CGROUP.

Thanks.

Signature · 1 line
tejun
author maintainer
#4 lore raw

PATCH 2/3 sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem

In reply to Tejun Heo

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.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 include/linux/cgroup-defs.h | 27 ++++++++++++++--
 include/linux/init_task.h   |  8 -----
 include/linux/sched.h       | 12 -------
 init/Kconfig                |  1 +
 kernel/cgroup.c             | 77 ++++++++++++---------------------------------
 kernel/fork.c               |  4 ---
 6 files changed, 46 insertions(+), 83 deletions(-)
include/linux/cgroup-defs.h +25 -2
index 1b8c938..7d83d7f 100644
@@ -461,8 +461,31 @@ struct cgroup_subsys {
 	unsigned int depends_on;
 };
 
-void cgroup_threadgroup_change_begin(struct task_struct *tsk);
-void cgroup_threadgroup_change_end(struct task_struct *tsk);
+extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
+
+/**
+ * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
+ * @tsk: target task
+ *
+ * Called from threadgroup_change_begin() and allows cgroup operations to
+ * synchronize against threadgroup changes using a percpu_rw_semaphore.
+ */
+static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
+{
+	percpu_down_read(&cgroup_threadgroup_rwsem);
+}
+
+/**
+ * cgroup_threadgroup_change_end - threadgroup exclusion for cgroups
+ * @tsk: target task
+ *
+ * Called from threadgroup_change_end().  Counterpart of
+ * cgroup_threadcgroup_change_begin().
+ */
+static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
+{
+	percpu_up_read(&cgroup_threadgroup_rwsem);
+}
 
 #else	/* CONFIG_CGROUPS */
 
include/linux/init_task.h +0 -8
index 696d223..0cc0bbf 100644
@@ -25,13 +25,6 @@
 extern struct files_struct init_files;
 extern struct fs_struct init_fs;
 
-#ifdef CONFIG_CGROUPS
-#define INIT_GROUP_RWSEM(sig)						\
-	.group_rwsem = __RWSEM_INITIALIZER(sig.group_rwsem),
-#else
-#define INIT_GROUP_RWSEM(sig)
-#endif
-
 #ifdef CONFIG_CPUSETS
 #define INIT_CPUSET_SEQ(tsk)							\
 	.mems_allowed_seq = SEQCNT_ZERO(tsk.mems_allowed_seq),
@@ -56,7 +49,6 @@ extern struct fs_struct init_fs;
 	},								\
 	.cred_guard_mutex =						\
 		 __MUTEX_INITIALIZER(sig.cred_guard_mutex),		\
-	INIT_GROUP_RWSEM(sig)						\
 }
 
 extern struct nsproxy init_nsproxy;
include/linux/sched.h +0 -12
index 5ee2900..add524a 100644
@@ -743,18 +743,6 @@ struct signal_struct {
 	unsigned audit_tty_log_passwd;
 	struct tty_audit_buf *tty_audit_buf;
 #endif
-#ifdef CONFIG_CGROUPS
-	/*
-	 * group_rwsem prevents new tasks from entering the threadgroup and
-	 * member tasks from exiting,a more specifically, setting of
-	 * PF_EXITING.  fork and exit paths are protected with this rwsem
-	 * using threadgroup_change_begin/end().  Users which require
-	 * threadgroup to remain stable should use threadgroup_[un]lock()
-	 * which also takes care of exec path.  Currently, cgroup is the
-	 * only user.
-	 */
-	struct rw_semaphore group_rwsem;
-#endif
 
 	oom_flags_t oom_flags;
 	short oom_score_adj;		/* OOM kill score adjustment */
init/Kconfig +1 -0
index dc24dec..b9b824b 100644
@@ -938,6 +938,7 @@ config NUMA_BALANCING_DEFAULT_ENABLED
 menuconfig CGROUPS
 	bool "Control Group support"
 	select KERNFS
+	select PERCPU_RWSEM
 	help
 	  This option adds support for grouping sets of processes together, for
 	  use with process control subsystems such as Cpusets, CFS, memory
kernel/cgroup.c +20 -57

157 lines not on this page · raw message

kernel/fork.c +0 -4
index 03c1eaa..9531275 100644
@@ -1144,10 +1144,6 @@ 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.1.0
maintainer
#5 lore raw

Re: [PATCH 2/3] sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem

On Wed, May 13, 2015 at 04:35:17PM -0400, Tejun Heo wrote:

.gitconfig:

[diff "default"]

        xfuncname = "^[[:alpha:]$_].*[^:]$"

Will avoid keying on labels like that and show us this is __cgroup_procs_write().

Quoted 30 lines
kernel/cgroup.c +4 -4
@@ -2480,7 +2442,7 @@ retry_find_task:
 	get_task_struct(tsk);
 	rcu_read_unlock();
 
-	threadgroup_lock(tsk);
+	percpu_down_write(&cgroup_threadgroup_rwsem);
 	if (threadgroup) {
 		if (!thread_group_leader(tsk)) {
 			/*
@@ -2490,7 +2452,7 @@ retry_find_task:
 			 * try again; this is
 			 * "double-double-toil-and-trouble-check locking".
 			 */
-			threadgroup_unlock(tsk);
+			percpu_up_write(&cgroup_threadgroup_rwsem);
 			put_task_struct(tsk);
 			goto retry_find_task;
 		}
@@ -2703,17 +2665,17 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
 				goto out_finish;
 			last_task = task;
 
-			threadgroup_lock(task);
+			percpu_down_write(&cgroup_threadgroup_rwsem);
 			/* raced against de_thread() from another thread? */
 			if (!thread_group_leader(task)) {
-				threadgroup_unlock(task);
+				percpu_up_write(&cgroup_threadgroup_rwsem);
 				put_task_struct(task);

kernel/cgroup.c static int cgroup_update_dfl_csses(struct cgroup *cgrp) · line 2681

Quoted 10 lines
kernel/cgroup.c +1 -1
 				continue;
 			}
 
 			ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
 
-			threadgroup_unlock(task);
+			percpu_up_write(&cgroup_threadgroup_rwsem);
 			put_task_struct(task);
 
 			if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))

So my only worry with this patch-set is that these operations will be hugely expensive.

Now it looks like the cgroup_update_dfl_csses() thing is very rare, its when you change which controllers are active in a given subtree under the uber-l337-super-comount design.

The other one, __cgorup_procs_write() is every /procs, /tasks write to a cgroup, and that does worry me, this could be a somewhat common thing.

The Changelog states task migration is a cold path, but is tens of miliseconds per task really no problem?

author maintainer
#6 lore raw

Re: [PATCH 2/3] sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem

Hello, Peter.

Quoted 7 lines · Peter Zijlstra

.gitconfig:

[diff "default"]

        xfuncname = "^[[:alpha:]$_].*[^:]$"

Will avoid keying on labels like that and show us this is __cgroup_procs_write().

Ah, nice trick.

Quoted 7 lines

So my only worry with this patch-set is that these operations will be hugely expensive.

Now it looks like the cgroup_update_dfl_csses() thing is very rare, its when you change which controllers are active in a given subtree under the uber-l337-super-comount design.

Quoted 5 lines

The other one, __cgorup_procs_write() is every /procs, /tasks write to a cgroup, and that does worry me, this could be a somewhat common thing.

The Changelog states task migration is a cold path, but is tens of miliseconds per task really no problem?

The latency is bound by synchronize_sched_expedited(). Given the way cgroups are used in majority of setups (process migration happening only during service / session setups), I think this should be okay.

I agree that something which is closer to lglock in characteristics would fit the workload better tho. If this actually becomes a problem, we can come up with a different percpu locking scheme which puts a bit more overhead on the reader side to reduce the latency / overhead on the writer side which shouldn't be that difficult but let's see whether we need to get there at all.

Thanks.

Signature · 1 line
tejun
#7 lore raw

Re: [PATCH 2/3] sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem

Quoted 23 lines · Tejun Heo

Hello, Peter.

Quoted 7 lines · Peter Zijlstra

.gitconfig:

[diff "default"]

        xfuncname = "^[[:alpha:]$_].*[^:]$"

Will avoid keying on labels like that and show us this is __cgroup_procs_write().

Ah, nice trick.

Quoted 10 lines

So my only worry with this patch-set is that these operations will be hugely expensive.

Now it looks like the cgroup_update_dfl_csses() thing is very rare, its when you change which controllers are active in a given subtree under the uber-l337-super-comount design.

The other one, __cgorup_procs_write() is every /procs, /tasks write to a cgroup, and that does worry me, this could be a somewhat common thing.

Quoted 7 lines
Quoted 2 lines

The Changelog states task migration is a cold path, but is tens of miliseconds per task really no problem?

The latency is bound by synchronize_sched_expedited(). Given the way cgroups are used in majority of setups (process migration happening only during service / session setups), I think this should be okay.

Actually process migration can happen quite frequently, for example in Android phones, and that's why Google had an out-of-tree patch to remove the synchronize_rcu() in that path, which turned out to be buggy.

Quoted 9 lines

I agree that something which is closer to lglock in characteristics would fit the workload better tho. If this actually becomes a problem, we can come up with a different percpu locking scheme which puts a bit more overhead on the reader side to reduce the latency / overhead on the writer side which shouldn't be that difficult but let's see whether we need to get there at all.

Thanks.

author maintainer
#8 lore raw

Re: [PATCH 2/3] sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem

Hello, Li.

Quoted 7 lines · Zefan Li
Quoted 3 lines

The latency is bound by synchronize_sched_expedited(). Given the way cgroups are used in majority of setups (process migration happening only during service / session setups), I think this should be okay.

Actually process migration can happen quite frequently, for example in Android phones, and that's why Google had an out-of-tree patch to remove the synchronize_rcu() in that path, which turned out to be buggy.

It's still not a very frequent operation tho. We're talking about users switching fore/background jobs here and the expedited synchronization w/ preemption enabled doesn't take much time. In addition, as it currently stands, android is doing memory charge immigration on each fore/background switches. I'm pretty doubtful this would make any difference.

Thanks.

Signature · 1 line
tejun
#9 lore raw

Re: [PATCH 2/3] sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem

Quoted 11 lines · Tejun Heo

Hello, Li.

Quoted 7 lines · Zefan Li
Quoted 3 lines

The latency is bound by synchronize_sched_expedited(). Given the way cgroups are used in majority of setups (process migration happening only during service / session setups), I think this should be okay.

Actually process migration can happen quite frequently, for example in Android phones, and that's why Google had an out-of-tree patch to remove the synchronize_rcu() in that path, which turned out to be buggy.

Quoted 7 lines

It's still not a very frequent operation tho. We're talking about users switching fore/background jobs here and the expedited synchronization w/ preemption enabled doesn't take much time. In addition, as it currently stands, android is doing memory charge immigration on each fore/background switches. I'm pretty doubtful this would make any difference.

I did some testing with my laptop.

Moving a task between 2 cgroups for 10W times with one or two threads:

                  1T        2T
orig              3.36s     3.65s
orig+tj           3.55s     6.31s
orig+sync_rcu     16.69s    28.47s    (only 1000 times)

The overhead looks acceptable.

author maintainer
#10 lore raw

PATCH 3/3 cgroup: simplify threadgroup locking

In reply to Tejun Heo

Now that threadgroup locking is made global, code paths around it can be simplified.

* lock-verify-unlock-retry dancing removed from __cgroup_procs_write().

* Race protection against de_thread() removed from cgroup_update_dfl_csses().

---
 kernel/cgroup.c | 48 +++++++++++++-----------------------------------
 1 file changed, 13 insertions(+), 35 deletions(-)
kernel/cgroup.c +13 -35
index 5435b9d..afe9a7e 100644
@@ -2401,14 +2401,13 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
 	if (!cgrp)
 		return -ENODEV;
 
-retry_find_task:
+	percpu_down_write(&cgroup_threadgroup_rwsem);
 	rcu_read_lock();
 	if (pid) {
 		tsk = find_task_by_vpid(pid);
 		if (!tsk) {
-			rcu_read_unlock();
 			ret = -ESRCH;
-			goto out_unlock_cgroup;
+			goto out_unlock_rcu;
 		}
 		/*
 		 * even if we're attaching all tasks in the thread group, we
@@ -2418,9 +2417,8 @@ retry_find_task:
 		if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
 		    !uid_eq(cred->euid, tcred->uid) &&
 		    !uid_eq(cred->euid, tcred->suid)) {
-			rcu_read_unlock();
 			ret = -EACCES;
-			goto out_unlock_cgroup;
+			goto out_unlock_rcu;
 		}
 	} else
 		tsk = current;
@@ -2435,35 +2433,21 @@ retry_find_task:
 	 */
 	if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
 		ret = -EINVAL;
-		rcu_read_unlock();
-		goto out_unlock_cgroup;
+		goto out_unlock_rcu;
 	}
 
 	get_task_struct(tsk);
 	rcu_read_unlock();
 
-	percpu_down_write(&cgroup_threadgroup_rwsem);
-	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".
-			 */
-			percpu_up_write(&cgroup_threadgroup_rwsem);
-			put_task_struct(tsk);
-			goto retry_find_task;
-		}
-	}
-
 	ret = cgroup_attach_task(cgrp, tsk, threadgroup);
 
-	percpu_up_write(&cgroup_threadgroup_rwsem);
-
 	put_task_struct(tsk);
-out_unlock_cgroup:
+	goto out_unlock_threadgroup;
+
+out_unlock_rcu:
+	rcu_read_unlock();
+out_unlock_threadgroup:
+	percpu_up_write(&cgroup_threadgroup_rwsem);
 	cgroup_kn_unlock(of->kn);
 	return ret ?: nbytes;
 }
@@ -2610,6 +2594,8 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
 
 	lockdep_assert_held(&cgroup_mutex);
 
+	percpu_down_write(&cgroup_threadgroup_rwsem);
+
 	/* look up all csses currently attached to @cgrp's subtree */
 	down_read(&css_set_rwsem);
 	css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
@@ -2665,17 +2651,8 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
 				goto out_finish;
 			last_task = task;
 
-			percpu_down_write(&cgroup_threadgroup_rwsem);
-			/* raced against de_thread() from another thread? */
-			if (!thread_group_leader(task)) {
-				percpu_up_write(&cgroup_threadgroup_rwsem);
-				put_task_struct(task);
-				continue;
-			}
-
 			ret = cgroup_migrate(src_cset->dfl_cgrp, task, true);
 
-			percpu_up_write(&cgroup_threadgroup_rwsem);
 			put_task_struct(task);
 
 			if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
@@ -2685,6 +2662,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
 
 out_finish:
 	cgroup_migrate_finish(&preloaded_csets);
+	percpu_up_write(&cgroup_threadgroup_rwsem);
 	return ret;
 }
 
Signature · 1 line
2.1.0
author maintainer
#11 lore raw
In reply to Tejun Heo
Quoted 6 lines · Tejun Heo

threadgroup locking was added because cgroup needs threadgroups to stay stable across attach operations. It was implemented as a per-signal_struct generic locking mechanism so that other users which require threadgroups stable across blocking operations can use it too; however, it hasn't grown any other use cases and still conditionalized on CONFIG_CGROUPS.

Ingo, Peter, what do you guys think? If you guys are okay with the changes, how do you want to route the patches? Given that it's mostly cgroup-specific and there are more cgroup changes depending on these, it'd be the easiest to route these through the cgroup tree but putting it elsewhere and pulling into cgroup is fine too.

Thanks.

Signature · 1 line
tejun
maintainer
#12 lore raw
Quoted 9 lines · Tejun Heo
Quoted 6 lines · Tejun Heo

threadgroup locking was added because cgroup needs threadgroups to stay stable across attach operations. It was implemented as a per-signal_struct generic locking mechanism so that other users which require threadgroups stable across blocking operations can use it too; however, it hasn't grown any other use cases and still conditionalized on CONFIG_CGROUPS.

Ingo, Peter, what do you guys think?

I had a brief look and didn't spot anhything really weird. I'll try and give is a little more time tomorrow.

On routing I think you can take it through the cgroup tree if Ingo is ok with that.

author maintainer
#13 lore raw
In reply to Tejun Heo
Quoted 6 lines · Tejun Heo

threadgroup locking was added because cgroup needs threadgroups to stay stable across attach operations. It was implemented as a per-signal_struct generic locking mechanism so that other users which require threadgroups stable across blocking operations can use it too; however, it hasn't grown any other use cases and still conditionalized on CONFIG_CGROUPS.

Applying to cgroups/for-4.2.

Thanks.

Signature · 1 line
tejun