Skip to contentSkip to conversation

cgroup: Remove task_lock() from cgroup_post_fork()

RFC PATCH Superseded Frederic Weisbecker 3 participants 4 messages Dec 22, 2011 1 message not in the archive cgroup

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 cgroup: Remove task_lock() from cgroup_post_fork()

cgroup_post_fork() is protected between threadgroup_change_begin() and threadgroup_change_end() against concurrent changes of the child's css_set in cgroup_task_migrate(). Also the child can't exit and call cgroup_exit() at this stage, this means it's css_set can't be changed with init_css_set concurrently.

For these reasons, we don't need to hold task_lock() on the child because it's css_set can only remain stable in this place.

Let's remove the lock there.

NOTE: We could do something else: move threadgroup_change_end() before cgroup_post_fork() and keep the task_lock() which, combined with the css_set_lock, would be enough to synchronize against cgroup_task_migrate()'s change on child->cgroup and its cglist. Because outside that, the threadgroup lock doesn't appear to be needed on cgroup_post_fork().

Let's debate!

Cc Li Zefan <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Containers <containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org>
Cc Cgroups <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Paul Menage <[email protected]>
Cc Mandeep Singh Baines <[email protected]>
---
 kernel/cgroup.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
kernel/cgroup.c +8 -3
index 4936d88..d0dbf72 100644
@@ -4622,10 +4622,15 @@ void cgroup_post_fork(struct task_struct *child)
 {
 	if (use_task_css_set_links) {
 		write_lock(&css_set_lock);
-		task_lock(child);
-		if (list_empty(&child->cg_list))
+		if (list_empty(&child->cg_list)) {
+			/*
+			 * It's safe to use child->cgroups without task_lock()
+			 * here because we are protected through
+			 * threadgroup_change_begin() against concurrent
+			 * css_set change in cgroup_task_migrate()
+			 */
 			list_add(&child->cg_list, &child->cgroups->tasks);
-		task_unlock(child);
+		}
 		write_unlock(&css_set_lock);
 	}
 }
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 9 lines · Frederic Weisbecker

cgroup_post_fork() is protected between threadgroup_change_begin() and threadgroup_change_end() against concurrent changes of the child's css_set in cgroup_task_migrate(). Also the child can't exit and call cgroup_exit() at this stage, this means it's css_set can't be changed with init_css_set concurrently.

For these reasons, we don't need to hold task_lock() on the child because it's css_set can only remain stable in this place.

Quoted 9 lines

Let's remove the lock there.

NOTE: We could do something else: move threadgroup_change_end() before cgroup_post_fork() and keep the task_lock() which, combined with the css_set_lock, would be enough to synchronize against cgroup_task_migrate()'s change on child->cgroup and its cglist. Because outside that, the threadgroup lock doesn't appear to be needed on cgroup_post_fork().

To narrow the scope of the threadgroup lock? I think it's preferable to keep cgroup_post_fork() inside the lock, to make things simpler and we have the same lock rule for both cgroup_fork() and cgroup_post_fork().

Quoted 23 lines

Let's debate!

Cc Li Zefan <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Containers <[email protected]>
Cc Cgroups <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Paul Menage <[email protected]>
Cc Mandeep Singh Baines <[email protected]>
---
 kernel/cgroup.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
kernel/cgroup.c +0 -0
index 4936d88..d0dbf72 100644
@@ -4622,10 +4622,15 @@ void cgroup_post_fork(struct task_struct *child)
 {
 	if (use_task_css_set_links) {

kernel/cgroup.c void cgroup_post_fork(struct task_struct *child) · line 4631

Quoted 10 lines
kernel/cgroup.c +7 -2
 		write_lock(&css_set_lock);
-		task_lock(child);
-		if (list_empty(&child->cg_list))
+		if (list_empty(&child->cg_list)) {
+			/*
+			 * It's safe to use child->cgroups without task_lock()
+			 * here because we are protected through
+			 * threadgroup_change_begin() against concurrent
+			 * css_set change in cgroup_task_migrate()
+			 */

Also explain why it won't race with cgroup_exit()? You were not quite confident about that before Oleg's explanation. ;)

Quoted 6 lines
kernel/cgroup.c +1 -1
 			list_add(&child->cg_list, &child->cgroups->tasks);
-		task_unlock(child);
+		}
 		write_unlock(&css_set_lock);
 	}
 }
#2 lore raw
Quoted 20 lines · Li Zefan
Quoted 9 lines · Frederic Weisbecker

cgroup_post_fork() is protected between threadgroup_change_begin() and threadgroup_change_end() against concurrent changes of the child's css_set in cgroup_task_migrate(). Also the child can't exit and call cgroup_exit() at this stage, this means it's css_set can't be changed with init_css_set concurrently.

For these reasons, we don't need to hold task_lock() on the child because it's css_set can only remain stable in this place.

Quoted 9 lines

Let's remove the lock there.

NOTE: We could do something else: move threadgroup_change_end() before cgroup_post_fork() and keep the task_lock() which, combined with the css_set_lock, would be enough to synchronize against cgroup_task_migrate()'s change on child->cgroup and its cglist. Because outside that, the threadgroup lock doesn't appear to be needed on cgroup_post_fork().

Quoted 3 lines

To narrow the scope of the threadgroup lock? I think it's preferable to keep cgroup_post_fork() inside the lock, to make things simpler and we have the same lock rule for both cgroup_fork() and cgroup_post_fork().

Ok!

Quoted 28 lines
Quoted 28 lines

Let's debate!

Cc Li Zefan <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Containers <containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org>
Cc Cgroups <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Paul Menage <[email protected]>
Cc Mandeep Singh Baines <[email protected]>
---
 kernel/cgroup.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
kernel/cgroup.c +2 -2
index 4936d88..d0dbf72 100644
@@ -4622,10 +4622,15 @@ void cgroup_post_fork(struct task_struct *child)
 {
 	if (use_task_css_set_links) {
 		write_lock(&css_set_lock);
-		task_lock(child);
-		if (list_empty(&child->cg_list))
+		if (list_empty(&child->cg_list)) {
+			/*
Quoted 8 lines

kernel/cgroup.c void cgroup_post_fork(struct task_struct *child) · line 4631

Quoted 5 lines
kernel/cgroup.c +5 -0
+			 * It's safe to use child->cgroups without task_lock()
+			 * here because we are protected through
+			 * threadgroup_change_begin() against concurrent
+			 * css_set change in cgroup_task_migrate()
+			 */

Also explain why it won't race with cgroup_exit()? You were not quite confident about that before Oleg's explanation. ;)

hehe indeed :)

Will update, thanks!

Quoted 6 lines
Quoted 6 lines
kernel/cgroup.c +1 -1
 			list_add(&child->cg_list, &child->cgroups->tasks);
-		task_unlock(child);
+		}
 		write_unlock(&css_set_lock);
 	}
 }

A message that is not in the archive (sent off-list or not yet received): 1324516711-26913-1-git-send-email-fweisbec-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw
Quoted 9 lines · Frederic Weisbecker

cgroup_post_fork() is protected between threadgroup_change_begin() and threadgroup_change_end() against concurrent changes of the child's css_set in cgroup_task_migrate(). Also the child can't exit and call cgroup_exit() at this stage, this means it's css_set can't be changed with init_css_set concurrently.

For these reasons, we don't need to hold task_lock() on the child because it's css_set can only remain stable in this place.

Quoted 9 lines

Let's remove the lock there.

NOTE: We could do something else: move threadgroup_change_end() before cgroup_post_fork() and keep the task_lock() which, combined with the css_set_lock, would be enough to synchronize against cgroup_task_migrate()'s change on child->cgroup and its cglist. Because outside that, the threadgroup lock doesn't appear to be needed on cgroup_post_fork().

To narrow the scope of the threadgroup lock? I think it's preferable to keep cgroup_post_fork() inside the lock, to make things simpler and we have the same lock rule for both cgroup_fork() and cgroup_post_fork().

Quoted 23 lines

Let's debate!

Cc Li Zefan <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Containers <containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org>
Cc Cgroups <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Paul Menage <[email protected]>
Cc Mandeep Singh Baines <[email protected]>
---
 kernel/cgroup.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
kernel/cgroup.c +0 -0
index 4936d88..d0dbf72 100644
@@ -4622,10 +4622,15 @@ void cgroup_post_fork(struct task_struct *child)
 {
 	if (use_task_css_set_links) {

kernel/cgroup.c void cgroup_post_fork(struct task_struct *child) · line 4631

Quoted 10 lines
kernel/cgroup.c +7 -2
 		write_lock(&css_set_lock);
-		task_lock(child);
-		if (list_empty(&child->cg_list))
+		if (list_empty(&child->cg_list)) {
+			/*
+			 * It's safe to use child->cgroups without task_lock()
+			 * here because we are protected through
+			 * threadgroup_change_begin() against concurrent
+			 * css_set change in cgroup_task_migrate()
+			 */

Also explain why it won't race with cgroup_exit()? You were not quite confident about that before Oleg's explanation. ;)

Quoted 6 lines
kernel/cgroup.c +1 -1
 			list_add(&child->cg_list, &child->cgroups->tasks);
-		task_unlock(child);
+		}
 		write_unlock(&css_set_lock);
 	}
 }