Possible race between cgroup_attach_proc and de_thread, and questionable code in de_thread.
Subsystems (7)
Hi,
I've been exploring the use of RCU in the kernel, particularly looking for things that don't quite look right. I found cgroup_attach_proc which was added a few months ago.
It contains:
rcu_read_lock();
if (!thread_group_leader(leader)) {
/*
* a race with de_thread from another thread's exec() may strip
* us of our leadership, making while_each_thread unsafe to use
* on this task. if this happens, there is no choice but to
* throw this task away and try again (from cgroup_procs_write);
* this is "double-double-toil-and-trouble-check locking".
*/
rcu_read_unlock();
retval = -EAGAIN;
goto out_free_group_list;
}(and having the comment helps a lot!)
The comment acknowledges a race with de_thread but seems to assume that rcu_read_lock() will protect against that race. It won't.
It could possibly protect if the racy code in de_thread() contained a call to synchronize_rcu(), but it doesn't so there is no obvious exclusion between the two.
I note that some other locks are held and maybe some other lock provides the required exclusion - I haven't explored that too deeply - but if that is the case, then the use of rcu_read_lock() here is pointless - it isn't needed just to call thread_group_leader().
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;
which seems to be called with only tasklist_lock held, which doesn't seem to be held in the cgroup code.
If the "thread_group_leader(leader)" call in cgroup_attach_proc() runs before this chunk is run with the same value for 'leader', but the while_each_thread is run after, then the while_read_thread() might loop forever. rcu_read_lock doesn't prevent this from happening.
The code in de_thread() is actually questionable by itself. "list_replace_rcu" cannot really be used on the head of a list - it is only meant to be used on a member of a list.
To move a list from one head to another you should be using list_splice_init_rcu().
The ->tasks list doesn't seem to have a clearly distinguished 'head' but whatever is passed as 'g' to while_each_thread() is effectively a head and removing it from a list can cause a loop using while_each_thread() can not find the head and so never complete.
I' not sure how best to fix this, though possibly changing while_each_thead to:
while ((t = next_task(t)) != g && !thread_group_leader(t))
might be part of it. We would also need to move
tsk->group_leader = tsk;
in the above up to the top, and probably add some memory barrier. However I don't know enough about how the list is used to be sure.
Comments?
Thanks,
NeilBrown
Quoted 5 lines · NeilBrown
Hi,
I've been exploring the use of RCU in the kernel, particularly looking for things that don't quite look right. I foundcgroup_attach_procwhich was added a few months ago.
Awesome, thanks! :)
Quoted 19 lines
It contains:
rcu_read_lock(); if (!thread_group_leader(leader)) { /* * a race with de_thread from another thread's exec() may strip * us of our leadership, making while_each_thread unsafe to use * on this task. if this happens, there is no choice but to * throw this task away and try again (from cgroup_procs_write); * this is "double-double-toil-and-trouble-check locking". */ rcu_read_unlock(); retval = -EAGAIN; goto out_free_group_list; }(and having the comment helps a lot!)
Quoted 9 lines
The comment acknowledges a race with
de_threadbut seems to assume thatrcu_read_lock()will protect against that race. It won't.
It could possibly protect if the racy code inde_thread()contained a call tosynchronize_rcu(), but it doesn't so there is no obvious exclusion between the two.
I note that some other locks are held and maybe some other lock provides the required exclusion - I haven't explored that too deeply - but if that is the case, then the use ofrcu_read_lock()here is pointless - it isn't needed just to callthread_group_leader().
I wrote this code, and I admit to not having a full understanding of RCU myself. The code was once more complicated (before the patches went in, mind you), and had a series of checks like that leading up to a list_for_each_entry over the ->thread_group list (in "step 3", instead of iterating over the flex_array), and had read_lock(&tasklist_lock) around it. (...)
(The other locks held are just cgroup_mutex and threadgroup_fork_lock, which wouldn't provide the exclusion.)
Quoted 11 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;
Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.
Somehow I was under the impression that holding tasklist_lock (for writing) provided exclusion from code that holds rcu_read_lock - probably because there are other points in the kernel which do while_each_thread with only RCU-read held (and not tasklist):
- kernel/hung_task.c, check_hung_uninterruptible_tasks()
- kernel/posix-cpu-timers.c, thread_group_cputime()
- fs/ioprio.c, ioprio_set() and ioprio_get()
(There are also places, like kernel/signal.c, where code does while_each_thread with only sighand->siglock held. this also seems sketchy, since de_thread only takes that lock after the code quoted above. there's a big comment in fs/exec.c where this is also done, but I don't quite understand it.)
You seem to imply that rcu_read_lock() doesn't exclude against write_lock(&tasklist_lock). If that's true, then we can fix the cgroup code simply by replacing rcu_read_lock/rcu_read_unlock with read_lock and read_unlocck on tasklist_lock. (I can hurry a bugfix patch for this together if so.)
Wouldn't this mean that the three places listed above are also wrong?
Quoted 17 lines
The code in
de_thread()is actually questionable by itself. "list_replace_rcu" cannot really be used on the head of a list - it is only meant to be used on a member of a list.
To move a list from one head to another you should be usinglist_splice_init_rcu().
The ->tasks list doesn't seem to have a clearly distinguished 'head' but whatever is passed as 'g' towhile_each_thread()is effectively a head and removing it from a list can cause a loop usingwhile_each_thread()can not find the head and so never complete.I' not sure how best to fix this, though possibly changing
while_each_theadto:while ((t = next_task(t)) != g && !thread_group_leader(t))might be part of it. We would also need to move
Quoted 10 lines
tsk->group_leader = tsk;in the above up to the top, and probably add some memory barrier. However I don't know enough about how the list is used to be sure.
Comments?
Thanks,
NeilBrown
I barely understand de_thread() from the reader's perspective, let alone from the author's perspective, so I can't speak for that one.
Thanks for pointing this out!
-- Ben
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 18 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.
Quoted 6 lines
Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):-
kernel/hung_task.c,check_hung_uninterruptible_tasks()
This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way the task_struct is freed.
Quoted 1 line
-
kernel/posix-cpu-timers.c,thread_group_cputime()
Same story here. The code only needs the task_struct structures to stick around long enough to compute the CPU time consumed, so this should be OK.
Quoted 1 line
-
fs/ioprio.c,ioprio_set()andioprio_get()
Here RCU protects traversal to the task structure as in the two examples above. In the ioprio_set() case, the actual change is carried out under task_lock(). So this code relies on RCU to safely locate the task structure, and on locking to safely carry out the update.
The ioprio_get() is using RCU to protect finding the task structure. This returns an integer (the I/O priority), so no locking is required. Though if compilers continue becoming increasingly aggressive with their optimizations, there might need to be an ACCESS_ONCE() in get_task_ioprio().
Quoted 11 lines
(There are also places, like
kernel/signal.c, where code doeswhile_each_threadwith onlysighand->siglockheld. this also seems sketchy, sincede_threadonly takes that lock after the code quoted above. there's a big comment infs/exec.cwhere this is also done, but I don't quite understand it.)You seem to imply that
rcu_read_lock()doesn't exclude againstwrite_lock(&tasklist_lock). If that's true, then we can fix the cgroup code simply by replacing rcu_read_lock/rcu_read_unlock withread_lockandread_unlocckontasklist_lock. (I can hurry a bugfix patch for this together if so.)
Indeed, rcu_read_lock() does not exclude write_lock(&tasklist_lock). In fact, it doesn't exclude against -any- lock. But don't take my word for it, instead take a look at the heaviest-weight implementation of rcu_read_lock() that currently exists in mainline:
void __rcu_read_lock(void)
{
current->rcu_read_lock_nesting++;
barrier();
}That really is all there is to it, at least in the absence of CONFIG_PROVE_RCU. The first line does nothing more than increment a counter in the current tasks task_struct, while the second line does nothing more than suppress code-motion optimizations that the compiler might otherwise be tempted to undertake.
The purpose of rcu_read_lock() is instead to prevent any subsequent synchronize_rcu() calls (on any CPU or from any task) from returning until this task executes the matching rcu_read_unlock(). If you make careful use of rcu_read_lock(), synchronize_rcu(), and friends, you can get an effect that in some ways resembles mutual exclusion, but that is much faster and more scalable.
In the case of the task structure, rcu_read_lock() guarantees that any task_struct that you can get a pointer to will remain in existence until you execute the matching rcu_read_unlock(). By "remain in existence", I mean that the task_struct won't be freed (and possibly reused).
So rcu_read_lock() is giving you the following guarantee: If you legitimately pick up a pointer to an RCU-protected data structure, then that data structure will not be freed until after you execute the matching rcu_read_unlock().
Quoted 1 line
Wouldn't this mean that the three places listed above are also wrong?
I do not believe so, give or take the ACCESS_ONCE().
The issue in your case is that you are relying not just on the process to exist, but also on the state of group leadership. RCU protects the former, but not the latter.
I suspect that your suggested bugfix patch would do the trick, but I must defer to people who understand tasklist locking better than I do.
Thanx, Paul
Quoted 33 lines
Quoted 16 lines
The code in
de_thread()is actually questionable by itself. "list_replace_rcu" cannot really be used on the head of a list - it is only meant to be used on a member of a list.
To move a list from one head to another you should be usinglist_splice_init_rcu().
The ->tasks list doesn't seem to have a clearly distinguished 'head' but whatever is passed as 'g' towhile_each_thread()is effectively a head and removing it from a list can cause a loop usingwhile_each_thread()can not find the head and so never complete.I' not sure how best to fix this, though possibly changing
while_each_theadto:while ((t = next_task(t)) != g && !thread_group_leader(t))might be part of it. We would also need to move
Quoted 10 lines
tsk->group_leader = tsk;in the above up to the top, and probably add some memory barrier. However I don't know enough about how the list is used to be sure.
Comments?
Thanks,
NeilBrownI barely understand
de_thread()from the reader's perspective, let alone from the author's perspective, so I can't speak for that one.Thanks for pointing this out!
-- Ben
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 18 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.
Quoted 6 lines
Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):-
kernel/hung_task.c,check_hung_uninterruptible_tasks()
This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way the task_struct is freed.
Quoted 1 line
-
kernel/posix-cpu-timers.c,thread_group_cputime()
Same story here. The code only needs the task_struct structures to stick around long enough to compute the CPU time consumed, so this should be OK.
Quoted 1 line
-
fs/ioprio.c,ioprio_set()andioprio_get()
Here RCU protects traversal to the task structure as in the two examples above. In the ioprio_set() case, the actual change is carried out under task_lock(). So this code relies on RCU to safely locate the task structure, and on locking to safely carry out the update.
The ioprio_get() is using RCU to protect finding the task structure. This returns an integer (the I/O priority), so no locking is required. Though if compilers continue becoming increasingly aggressive with their optimizations, there might need to be an ACCESS_ONCE() in get_task_ioprio().
Quoted 11 lines
(There are also places, like
kernel/signal.c, where code doeswhile_each_threadwith onlysighand->siglockheld. this also seems sketchy, sincede_threadonly takes that lock after the code quoted above. there's a big comment infs/exec.cwhere this is also done, but I don't quite understand it.)You seem to imply that
rcu_read_lock()doesn't exclude againstwrite_lock(&tasklist_lock). If that's true, then we can fix the cgroup code simply by replacing rcu_read_lock/rcu_read_unlock withread_lockandread_unlocckontasklist_lock. (I can hurry a bugfix patch for this together if so.)
Indeed, rcu_read_lock() does not exclude write_lock(&tasklist_lock). In fact, it doesn't exclude against -any- lock. But don't take my word for it, instead take a look at the heaviest-weight implementation of rcu_read_lock() that currently exists in mainline:
void __rcu_read_lock(void)
{
current->rcu_read_lock_nesting++;
barrier();
}That really is all there is to it, at least in the absence of CONFIG_PROVE_RCU. The first line does nothing more than increment a counter in the current tasks task_struct, while the second line does nothing more than suppress code-motion optimizations that the compiler might otherwise be tempted to undertake.
The purpose of rcu_read_lock() is instead to prevent any subsequent synchronize_rcu() calls (on any CPU or from any task) from returning until this task executes the matching rcu_read_unlock(). If you make careful use of rcu_read_lock(), synchronize_rcu(), and friends, you can get an effect that in some ways resembles mutual exclusion, but that is much faster and more scalable.
In the case of the task structure, rcu_read_lock() guarantees that any task_struct that you can get a pointer to will remain in existence until you execute the matching rcu_read_unlock(). By "remain in existence", I mean that the task_struct won't be freed (and possibly reused).
So rcu_read_lock() is giving you the following guarantee: If you legitimately pick up a pointer to an RCU-protected data structure, then that data structure will not be freed until after you execute the matching rcu_read_unlock().
Quoted 1 line
Wouldn't this mean that the three places listed above are also wrong?
I do not believe so, give or take the ACCESS_ONCE().
The issue in your case is that you are relying not just on the process to exist, but also on the state of group leadership. RCU protects the former, but not the latter.
I suspect that your suggested bugfix patch would do the trick, but I must defer to people who understand tasklist locking better than I do.
Thanx, Paul
Quoted 33 lines
Quoted 16 lines
The code in
de_thread()is actually questionable by itself. "list_replace_rcu" cannot really be used on the head of a list - it is only meant to be used on a member of a list.
To move a list from one head to another you should be usinglist_splice_init_rcu().
The ->tasks list doesn't seem to have a clearly distinguished 'head' but whatever is passed as 'g' towhile_each_thread()is effectively a head and removing it from a list can cause a loop usingwhile_each_thread()can not find the head and so never complete.I' not sure how best to fix this, though possibly changing
while_each_theadto:while ((t = next_task(t)) != g && !thread_group_leader(t))might be part of it. We would also need to move
Quoted 10 lines
tsk->group_leader = tsk;in the above up to the top, and probably add some memory barrier. However I don't know enough about how the list is used to be sure.
Comments?
Thanks,
NeilBrownI barely understand
de_thread()from the reader's perspective, let alone from the author's perspective, so I can't speak for that one.Thanks for pointing this out!
-- Ben
Quoted 28 lines · On Wed, 27 Jul 2011 16:42:35 -0700 "Paul E. McKenney"…
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 23 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):
Quoted 8 lines
Quoted 1 line
-
kernel/hung_task.c,check_hung_uninterruptible_tasks()This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of
rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way thetask_structis freed.
I disagree. It also requires - by virtue of the use of while_each_thread() - that 'g' remains on the list that 't' is walking along.
Now for a normal list, the head always stays on the list and is accessible even from an rcu-removed entry. But the thread_group list isn't a normal list. It doesn't have a distinct head. It is a loop of all of the 'task_structs' in a thread group. One of them is designated the 'leader' but de_thread() can change the 'leader' - though it doesn't remove the old leader.
__unhash_process in mm/exit.c looks like it could remove the leader from the list and definitely could remove a non-leader.
So if a non-leader calls 'exec' and the leader calls 'exit', then a task_struct that was the leader could become a non-leader and then be removed from the list that kernel/hung_task could be walking along.
So I don't think that while_each_thread() is currently safe. It depends on the thread leader not disappearing and I think it can.
So I'm imagining a patch like this to ensure that while_each_thread() is actually safe. If it is always safe you can remove that extra check in cgroup_attach_proc() which looked wrong.
I just hope someone who understands the process tree is listening.. The change in exit.c is the most uncertain part.
NeilBrown
fs/exec.c +3 -0
@@ -960,6 +960,9 @@ static int de_thread(struct task_struct *tsk)list_replace_init (&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; +smp_mb ();/* ensure that any reader will always be able to see +* a task that claims to be the group leader +*/ leader->group_leader = tsk; tsk->exit_signal =SIGCHLD ;
include/linux/sched.h +3 -1
@@ -2267,8 +2267,10 @@ extern bool current_is_single_threaded(void);#define do_each_thread (g, t) \ for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do +/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))static inline int get_nr_threads (struct task_struct *tsk) {
kernel/exit.c +7 -2
@@ -70,8 +70,13 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); - } -list_del_rcu (&p->thread_group); + }else +/* only remove members from the thread group. +* The thread group leader must stay so that +* while_each_thread() uses can see the end of +* the list and stop. +*/ +list_del_rcu (&p->thread_group); }/*
Quoted 28 lines · On Wed, 27 Jul 2011 16:42:35 -0700 "Paul E. McKenney"…
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 23 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):
Quoted 8 lines
Quoted 1 line
-
kernel/hung_task.c,check_hung_uninterruptible_tasks()This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of
rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way thetask_structis freed.
I disagree. It also requires - by virtue of the use of while_each_thread() - that 'g' remains on the list that 't' is walking along.
Now for a normal list, the head always stays on the list and is accessible even from an rcu-removed entry. But the thread_group list isn't a normal list. It doesn't have a distinct head. It is a loop of all of the 'task_structs' in a thread group. One of them is designated the 'leader' but de_thread() can change the 'leader' - though it doesn't remove the old leader.
__unhash_process in mm/exit.c looks like it could remove the leader from the list and definitely could remove a non-leader.
So if a non-leader calls 'exec' and the leader calls 'exit', then a task_struct that was the leader could become a non-leader and then be removed from the list that kernel/hung_task could be walking along.
So I don't think that while_each_thread() is currently safe. It depends on the thread leader not disappearing and I think it can.
So I'm imagining a patch like this to ensure that while_each_thread() is actually safe. If it is always safe you can remove that extra check in cgroup_attach_proc() which looked wrong.
I just hope someone who understands the process tree is listening.. The change in exit.c is the most uncertain part.
NeilBrown
fs/exec.c +3 -0
@@ -960,6 +960,9 @@ static int de_thread(struct task_struct *tsk)list_replace_init (&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; +smp_mb ();/* ensure that any reader will always be able to see +* a task that claims to be the group leader +*/ leader->group_leader = tsk; tsk->exit_signal =SIGCHLD ;
include/linux/sched.h +3 -1
@@ -2267,8 +2267,10 @@ extern bool current_is_single_threaded(void);#define do_each_thread (g, t) \ for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do +/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))static inline int get_nr_threads (struct task_struct *tsk) {
kernel/exit.c +7 -2
@@ -70,8 +70,13 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); - } -list_del_rcu (&p->thread_group); + }else +/* only remove members from the thread group. +* The thread group leader must stay so that +* while_each_thread() uses can see the end of +* the list and stop. +*/ +list_del_rcu (&p->thread_group); }/*
Quoted 49 lines · NeilBrown
Quoted 28 lines · On Wed, 27 Jul 2011 16:42:35 -0700 "Paul E. McKenney"…
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 23 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):Quoted 8 lines
Quoted 1 line
-
kernel/hung_task.c,check_hung_uninterruptible_tasks()This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of
rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way thetask_structis freed.I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Now for a normal list, the head always stays on the list and is accessible even from an rcu-removed entry. But the
thread_grouplist isn't a normal list. It doesn't have a distinct head. It is a loop of all of the 'task_structs' in a thread group. One of them is designated the 'leader' butde_thread()can change the 'leader' - though it doesn't remove the old leader.
Quoted 6 lines
__unhash_processinmm/exit.clooks like it could remove the leader from the list and definitely could remove a non-leader.So if a non-leader calls 'exec' and the leader calls 'exit', then a
task_structthat was the leader could become a non-leader and then be removed from the list thatkernel/hung_taskcould be walking along.
That agrees with my understanding.
Quoted 3 lines
So I don't think that
while_each_thread()is currently safe. It depends on the thread leader not disappearing and I think it can.
I think that while_each_thread is perfectly safe, it just needs to be protected properly while used. it reads the tasklist, and both competing paths (__unhash_process and de_thread) are done with tasklist_lock write locked, so read-locking ought to suffice. all it needs is to be better documented.
include/linux/sched.h extern bool current_is_single_threaded(void); · line 2273
Quoted 7 lines
[...]
include/linux/sched.h +3
-1+/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))
this is semantically wrong: it will stop as soon as it finds a thread that has newly become the leader, and not run the loop body code in that thread's case. so the thread that just execed would not get run on, and in the case of my code, would "escape" the cgroup migration.
but I argue it is also organisationally wrong. while_each_thread's purpose is just to worry about the structure of the process list, not to account for behavioural details of de_thread. this check belongs outside of the macro, and it should be protected by tasklist_lock in the same critical section in which while_each_thread is used.
-- Ben
Quoted 25 lines
static inline int
get_nr_threads(struct task_struct *tsk){kernel/exit.c +7
-2@@ -70,8 +70,13 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); - } -list_del_rcu (&p->thread_group); + }else +/* only remove members from the thread group. +* The thread group leader must stay so that +* while_each_thread() uses can see the end of +* the list and stop. +*/ +list_del_rcu (&p->thread_group); }/*
Quoted 49 lines · NeilBrown
Quoted 28 lines · On Wed, 27 Jul 2011 16:42:35 -0700 "Paul E. McKenney"…
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 23 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):Quoted 8 lines
Quoted 1 line
-
kernel/hung_task.c,check_hung_uninterruptible_tasks()This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of
rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way thetask_structis freed.I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Now for a normal list, the head always stays on the list and is accessible even from an rcu-removed entry. But the
thread_grouplist isn't a normal list. It doesn't have a distinct head. It is a loop of all of the 'task_structs' in a thread group. One of them is designated the 'leader' butde_thread()can change the 'leader' - though it doesn't remove the old leader.
Quoted 6 lines
__unhash_processinmm/exit.clooks like it could remove the leader from the list and definitely could remove a non-leader.So if a non-leader calls 'exec' and the leader calls 'exit', then a
task_structthat was the leader could become a non-leader and then be removed from the list thatkernel/hung_taskcould be walking along.
That agrees with my understanding.
Quoted 3 lines
So I don't think that
while_each_thread()is currently safe. It depends on the thread leader not disappearing and I think it can.
I think that while_each_thread is perfectly safe, it just needs to be protected properly while used. it reads the tasklist, and both competing paths (__unhash_process and de_thread) are done with tasklist_lock write locked, so read-locking ought to suffice. all it needs is to be better documented.
include/linux/sched.h extern bool current_is_single_threaded(void); · line 2273
Quoted 7 lines
[...]
include/linux/sched.h +3
-1+/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))
this is semantically wrong: it will stop as soon as it finds a thread that has newly become the leader, and not run the loop body code in that thread's case. so the thread that just execed would not get run on, and in the case of my code, would "escape" the cgroup migration.
but I argue it is also organisationally wrong. while_each_thread's purpose is just to worry about the structure of the process list, not to account for behavioural details of de_thread. this check belongs outside of the macro, and it should be protected by tasklist_lock in the same critical section in which while_each_thread is used.
-- Ben
Quoted 25 lines
static inline int
get_nr_threads(struct task_struct *tsk){kernel/exit.c +7
-2@@ -70,8 +70,13 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); - } -list_del_rcu (&p->thread_group); + }else +/* only remove members from the thread group. +* The thread group leader must stay so that +* while_each_thread() uses can see the end of +* the list and stop. +*/ +list_del_rcu (&p->thread_group); }/*
Quoted 60 lines · On Thu, 28 Jul 2011 02:26:16 -0400 Ben Blum…
Quoted 49 lines · NeilBrown
Quoted 28 lines · On Wed, 27 Jul 2011 16:42:35 -0700 "Paul E. McKenney"…
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 23 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):Quoted 8 lines
Quoted 1 line
-
kernel/hung_task.c,check_hung_uninterruptible_tasks()This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of
rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way thetask_structis freed.I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Now for a normal list, the head always stays on the list and is accessible even from an rcu-removed entry. But the
thread_grouplist isn't a normal list. It doesn't have a distinct head. It is a loop of all of the 'task_structs' in a thread group. One of them is designated the 'leader' butde_thread()can change the 'leader' - though it doesn't remove the old leader.Quoted 6 lines
__unhash_processinmm/exit.clooks like it could remove the leader from the list and definitely could remove a non-leader.So if a non-leader calls 'exec' and the leader calls 'exit', then a
task_structthat was the leader could become a non-leader and then be removed from the list thatkernel/hung_taskcould be walking along.That agrees with my understanding.
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
So I don't think that
while_each_thread()is currently safe. It depends on the thread leader not disappearing and I think it can.I think that
while_each_threadis perfectly safe, it just needs to be protected properly while used. it reads the tasklist, and both competing paths (__unhash_processandde_thread) are done withtasklist_lockwrite locked, so read-locking ought to suffice. all it needs is to be better documented.
That might be one answer.
However the fact that rcu primitives are used to walk the list (next_thread() contains list_entry_rcu) seems to suggest that the intention was that RCU could be used for read access - which in large part it can.
So we have two alternatives:
1/ take a reader spinlock to walk the thread_group list
2/ make RCU work safely for walking thread_group
I thought it was best to try the latter first.
Quoted 13 lines
include/linux/sched.h extern bool current_is_single_threaded(void); · line 2273
Quoted 8 lines
[...]
include/linux/sched.h +3
-1+/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))this is semantically wrong: it will stop as soon as it finds a thread that has newly become the leader, and not run the loop body code in that thread's case. so the thread that just execed would not get run on, and in the case of my code, would "escape" the cgroup migration.
Yes, you are right ...
For your case I think you do want the thread group list to be both safe and stable. RCU can only provide 'safe' for a list. So I think I'm convinced that you need a readlock on tasklist_lock. That doesn't mean that all users of while_each_thread do.
Quoted 6 lines
but I argue it is also organisationally wrong. while_each_thread's purpose is just to worry about the structure of the process list, not to account for behavioural details of
de_thread. this check belongs outside of the macro, and it should be protected bytasklist_lockin the same critical section in whichwhile_each_threadis used.
Sound fair.
Maybe we need two versions of while_each_thread(). One that assumes tasklist_lock and guarantees getting every thread, and one that is less precise but only needs rcu_read_lock().
That would require an audit of each call site to see what is really needed.
But yes: if you replace your rcu_read_lock()s with read_lock(&tasklist_lock) then you will be safe, and I don't think you can do any better (i.e. less locking) than that.
Thanks,
NeilBrown
Quoted 60 lines · On Thu, 28 Jul 2011 02:26:16 -0400 Ben Blum <[email protected]> wrote:
Quoted 49 lines · NeilBrown
Quoted 28 lines · On Wed, 27 Jul 2011 16:42:35 -0700 "Paul E. McKenney"…
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 23 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):Quoted 8 lines
Quoted 1 line
-
kernel/hung_task.c,check_hung_uninterruptible_tasks()This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of
rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way thetask_structis freed.I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Now for a normal list, the head always stays on the list and is accessible even from an rcu-removed entry. But the
thread_grouplist isn't a normal list. It doesn't have a distinct head. It is a loop of all of the 'task_structs' in a thread group. One of them is designated the 'leader' butde_thread()can change the 'leader' - though it doesn't remove the old leader.Quoted 6 lines
__unhash_processinmm/exit.clooks like it could remove the leader from the list and definitely could remove a non-leader.So if a non-leader calls 'exec' and the leader calls 'exit', then a
task_structthat was the leader could become a non-leader and then be removed from the list thatkernel/hung_taskcould be walking along.That agrees with my understanding.
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
So I don't think that
while_each_thread()is currently safe. It depends on the thread leader not disappearing and I think it can.I think that
while_each_threadis perfectly safe, it just needs to be protected properly while used. it reads the tasklist, and both competing paths (__unhash_processandde_thread) are done withtasklist_lockwrite locked, so read-locking ought to suffice. all it needs is to be better documented.
That might be one answer.
However the fact that rcu primitives are used to walk the list (next_thread() contains list_entry_rcu) seems to suggest that the intention was that RCU could be used for read access - which in large part it can.
So we have two alternatives:
1/ take a reader spinlock to walk the thread_group list
2/ make RCU work safely for walking thread_group
I thought it was best to try the latter first.
Quoted 13 lines
include/linux/sched.h extern bool current_is_single_threaded(void); · line 2273
Quoted 8 lines
[...]
include/linux/sched.h +3
-1+/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))this is semantically wrong: it will stop as soon as it finds a thread that has newly become the leader, and not run the loop body code in that thread's case. so the thread that just execed would not get run on, and in the case of my code, would "escape" the cgroup migration.
Yes, you are right ...
For your case I think you do want the thread group list to be both safe and stable. RCU can only provide 'safe' for a list. So I think I'm convinced that you need a readlock on tasklist_lock. That doesn't mean that all users of while_each_thread do.
Quoted 6 lines
but I argue it is also organisationally wrong. while_each_thread's purpose is just to worry about the structure of the process list, not to account for behavioural details of
de_thread. this check belongs outside of the macro, and it should be protected bytasklist_lockin the same critical section in whichwhile_each_threadis used.
Sound fair.
Maybe we need two versions of while_each_thread(). One that assumes tasklist_lock and guarantees getting every thread, and one that is less precise but only needs rcu_read_lock().
That would require an audit of each call site to see what is really needed.
But yes: if you replace your rcu_read_lock()s with read_lock(&tasklist_lock) then you will be safe, and I don't think you can do any better (i.e. less locking) than that.
Thanks,
NeilBrown
Quoted 40 lines · NeilBrown
Quoted 28 lines · On Wed, 27 Jul 2011 16:42:35 -0700 "Paul E. McKenney"…
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 23 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):Quoted 8 lines
Quoted 1 line
-
kernel/hung_task.c,check_hung_uninterruptible_tasks()This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of
rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way thetask_structis freed.
Quoted 2 lines
I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.
Doesn't the following code in the loop body deal with this possibilty?
/* Exit if t or g was unhashed during refresh. */ if (t->state == TASK_DEAD || g->state == TASK_DEAD) goto unlock;
Yes, a concurrent dethread could cause some of the tasks to be skipped, but there really is a hung thread, it will still be there to be caught next time, right?
Thanx, Paul
Quoted 76 lines
Now for a normal list, the head always stays on the list and is accessible even from an rcu-removed entry. But the
thread_grouplist isn't a normal list. It doesn't have a distinct head. It is a loop of all of the 'task_structs' in a thread group. One of them is designated the 'leader' butde_thread()can change the 'leader' - though it doesn't remove the old leader.
__unhash_processinmm/exit.clooks like it could remove the leader from the list and definitely could remove a non-leader.So if a non-leader calls 'exec' and the leader calls 'exit', then a
task_structthat was the leader could become a non-leader and then be removed from the list thatkernel/hung_taskcould be walking along.So I don't think that
while_each_thread()is currently safe. It depends on the thread leader not disappearing and I think it can.So I'm imagining a patch like this to ensure that
while_each_thread()is actually safe. If it is always safe you can remove that extra check incgroup_attach_proc()which looked wrong.I just hope someone who understands the process tree is listening.. The change in
exit.cis the most uncertain part.NeilBrown
fs/exec.c +3
-0@@ -960,6 +960,9 @@ static int de_thread(struct task_struct *tsk)list_replace_init (&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; +smp_mb ();/* ensure that any reader will always be able to see +* a task that claims to be the group leader +*/ leader->group_leader = tsk; tsk->exit_signal =SIGCHLD ;include/linux/sched.h +3
-1@@ -2267,8 +2267,10 @@ extern bool current_is_single_threaded(void);#define do_each_thread (g, t) \ for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do +/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))static inline int get_nr_threads (struct task_struct *tsk) {kernel/exit.c +7
-2@@ -70,8 +70,13 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); - } -list_del_rcu (&p->thread_group); + }else +/* only remove members from the thread group. +* The thread group leader must stay so that +* while_each_thread() uses can see the end of +* the list and stop. +*/ +list_del_rcu (&p->thread_group); }/*
Quoted 40 lines · NeilBrown
Quoted 28 lines · On Wed, 27 Jul 2011 16:42:35 -0700 "Paul E. McKenney"…
Quoted 1 line · Ben Blum
On Wed, Jul 27, 2011 at 05:11:01PM +1000, NeilBrown wrote:
[ . . . ]
Quoted 23 lines
Quoted 10 lines
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Somehow I was under the impression that holding
tasklist_lock(for writing) provided exclusion from code that holdsrcu_read_lock- probably because there are other points in the kernel which dowhile_each_threadwith only RCU-read held (and not tasklist):Quoted 8 lines
Quoted 1 line
-
kernel/hung_task.c,check_hung_uninterruptible_tasks()This one looks OK to me. The code is just referencing fields in each of the task structures, and appears to be making proper use of
rcu_dereference(). All this code requires is that the task structures remain in existence through the full lifetime of the RCU read-side critical section, which is guaranteed because of the way thetask_structis freed.
Quoted 2 lines
I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.
Doesn't the following code in the loop body deal with this possibilty?
/* Exit if t or g was unhashed during refresh. */ if (t->state == TASK_DEAD || g->state == TASK_DEAD) goto unlock;
Yes, a concurrent dethread could cause some of the tasks to be skipped, but there really is a hung thread, it will still be there to be caught next time, right?
Thanx, Paul
Quoted 76 lines
Now for a normal list, the head always stays on the list and is accessible even from an rcu-removed entry. But the
thread_grouplist isn't a normal list. It doesn't have a distinct head. It is a loop of all of the 'task_structs' in a thread group. One of them is designated the 'leader' butde_thread()can change the 'leader' - though it doesn't remove the old leader.
__unhash_processinmm/exit.clooks like it could remove the leader from the list and definitely could remove a non-leader.So if a non-leader calls 'exec' and the leader calls 'exit', then a
task_structthat was the leader could become a non-leader and then be removed from the list thatkernel/hung_taskcould be walking along.So I don't think that
while_each_thread()is currently safe. It depends on the thread leader not disappearing and I think it can.So I'm imagining a patch like this to ensure that
while_each_thread()is actually safe. If it is always safe you can remove that extra check incgroup_attach_proc()which looked wrong.I just hope someone who understands the process tree is listening.. The change in
exit.cis the most uncertain part.NeilBrown
fs/exec.c +3
-0@@ -960,6 +960,9 @@ static int de_thread(struct task_struct *tsk)list_replace_init (&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; +smp_mb ();/* ensure that any reader will always be able to see +* a task that claims to be the group leader +*/ leader->group_leader = tsk; tsk->exit_signal =SIGCHLD ;include/linux/sched.h +3
-1@@ -2267,8 +2267,10 @@ extern bool current_is_single_threaded(void);#define do_each_thread (g, t) \ for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do +/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))static inline int get_nr_threads (struct task_struct *tsk) {kernel/exit.c +7
-2@@ -70,8 +70,13 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); - } -list_del_rcu (&p->thread_group); + }else +/* only remove members from the thread group. +* The thread group leader must stay so that +* while_each_thread() uses can see the end of +* the list and stop. +*/ +list_del_rcu (&p->thread_group); }/*
PATCH 1/1 cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to a message not in the archiveFix unstable tasklist locking in cgroup_attach_proc.
From: Ben Blum <[email protected]>
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t. de_thread and exit. Taking tasklist_lock for reading, instead of rcu_read_lock, ensures proper exclusion.
--- kernel/cgroup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/cgroup.c +3 -3
@@ -2024,7 +2024,7 @@ goto out_free_group_list;/* prevent changes to the threadgroup list while we take a snapshot. */ -rcu_read_lock (); +read_lock (&tasklist_lock);if (!thread_group_leader (leader)) {/* * a race with de_thread from another thread's exec() may strip
@@ -2033,7 +2033,7 @@* throw this task away and try again (from cgroup_procs_write); * this is "double-double-toil-and-trouble-check locking". */ -rcu_read_unlock (); +read_unlock (&tasklist_lock); retval = -EAGAIN ; goto out_free_group_list; }
@@ -2054,7 +2054,7 @@ }while_each_thread (leader, tsk);/* remember the number of threads in the array for later. */ group_size = i; -rcu_read_unlock (); +read_unlock (&tasklist_lock);/* * step 1: check that we can legitimately attach to the cgroup.
PATCH 1/1 cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to NeilBrownFix unstable tasklist locking in cgroup_attach_proc.
From: Ben Blum <[email protected]>
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t. de_thread and exit. Taking tasklist_lock for reading, instead of rcu_read_lock, ensures proper exclusion.
--- kernel/cgroup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/cgroup.c +3 -3
@@ -2024,7 +2024,7 @@ goto out_free_group_list;/* prevent changes to the threadgroup list while we take a snapshot. */ -rcu_read_lock (); +read_lock (&tasklist_lock);if (!thread_group_leader (leader)) {/* * a race with de_thread from another thread's exec() may strip
@@ -2033,7 +2033,7 @@* throw this task away and try again (from cgroup_procs_write); * this is "double-double-toil-and-trouble-check locking". */ -rcu_read_unlock (); +read_unlock (&tasklist_lock); retval = -EAGAIN ; goto out_free_group_list; }
@@ -2054,7 +2054,7 @@ }while_each_thread (leader, tsk);/* remember the number of threads in the array for later. */ group_size = i; -rcu_read_unlock (); +read_unlock (&tasklist_lock);/* * step 1: check that we can legitimately attach to the cgroup.
Paul Menage
Acked-byRe: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to a message not in the archiveQuoted 10 lines · Ben Blum
Fix unstable tasklist locking in
cgroup_attach_proc.From: Ben Blum <[email protected]>
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Signed-off-by Ben Blum <[email protected]>
Thanks,
Paul
Quoted 35 lines
---
kernel/cgroup.c | 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)diff
a/kernel/cgroup.cb/kernel/cgroup.ckernel/cgroup.c 2011-07-29 06:17:47.000000000 -0700 renamed from kernel/cgroup.c 2011-07-21 19:17:23.000000000 -0700 +3
-3@@ -2024,7 +2024,7 @@ goto out_free_group_list; /* prevent changes to the threadgroup list while we take a snapshot. */ - rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) { /* * a race with de_thread from another thread's exec() may strip@@ -2033,7 +2033,7 @@ * throw this task away and try again (from cgroup_procs_write); * this is "double-double-toil-and-trouble-check locking". */ - rcu_read_unlock(); + read_unlock(&tasklist_lock); retval = -EAGAIN; goto out_free_group_list; }@@ -2054,7 +2054,7 @@ } while_each_thread(leader, tsk); /* remember the number of threads in the array for later. */ group_size = i; - rcu_read_unlock(); + read_unlock(&tasklist_lock); /* * step 1: check that we can legitimately attach to the cgroup.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to Ben BlumQuoted 10 lines · Ben Blum
Fix unstable tasklist locking in
cgroup_attach_proc.From: Ben Blum <[email protected]>
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Signed-off-by Ben Blum <[email protected]>
Thanks,
Paul
Quoted 35 lines
---
kernel/cgroup.c | 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)diff
a/kernel/cgroup.cb/kernel/cgroup.ckernel/cgroup.c 2011-07-29 06:17:47.000000000 -0700 renamed from kernel/cgroup.c 2011-07-21 19:17:23.000000000 -0700 +3
-3@@ -2024,7 +2024,7 @@ goto out_free_group_list; /* prevent changes to the threadgroup list while we take a snapshot. */ - rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) { /* * a race with de_thread from another thread's exec() may strip@@ -2033,7 +2033,7 @@ * throw this task away and try again (from cgroup_procs_write); * this is "double-double-toil-and-trouble-check locking". */ - rcu_read_unlock(); + read_unlock(&tasklist_lock); retval = -EAGAIN; goto out_free_group_list; }@@ -2054,7 +2054,7 @@ } while_each_thread(leader, tsk); /* remember the number of threads in the array for later. */ group_size = i; - rcu_read_unlock(); + read_unlock(&tasklist_lock); /* * step 1: check that we can legitimately attach to the cgroup.
Sorry for delay, just noticed this thread...
Quoted 11 lines · NeilBrown
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;
Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.
Yes. This was already discussed. See http://marc.info/?t=127688987300002
Damn. I forgot about this completely.
Quoted 5 lines
The code in
de_thread()is actually questionable by itself. "list_replace_rcu" cannot really be used on the head of a list - it is only meant to be used on a member of a list.
To move a list from one head to another you should be usinglist_splice_init_rcu().
Hmm... can't understand this part.
And just in case... list_replace_rcu() looks fine afaics. The real problem is release_task(old_leader) which does list_del_rcu(old_leader->thread_group), this is what breaks while_each_thread().
Quoted 1 line
The ->tasks list doesn't seem to have a clearly distinguished 'head'
Exactly. This is the problem.
But: you seem to confused ->tasks and ->thread_group ;)
Oleg.
Sorry for delay, just noticed this thread...
Quoted 11 lines · NeilBrown
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;
Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.
Yes. This was already discussed. See http://marc.info/?t=127688987300002
Damn. I forgot about this completely.
Quoted 5 lines
The code in
de_thread()is actually questionable by itself. "list_replace_rcu" cannot really be used on the head of a list - it is only meant to be used on a member of a list.
To move a list from one head to another you should be usinglist_splice_init_rcu().
Hmm... can't understand this part.
And just in case... list_replace_rcu() looks fine afaics. The real problem is release_task(old_leader) which does list_del_rcu(old_leader->thread_group), this is what breaks while_each_thread().
Quoted 1 line
The ->tasks list doesn't seem to have a clearly distinguished 'head'
Exactly. This is the problem.
But: you seem to confused ->tasks and ->thread_group ;)
Oleg.
include/linux/sched.h extern bool current_is_single_threaded(void); · line 2273
Quoted 6 lines · NeilBrown
include/linux/sched.h +3
-1+/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))
This assumes that while_each_thread(g, t) always uses g == group_leader.
kernel/exit.c static void __unhash_process(struct task_struct *p, bool group_dead) · line 79
Quoted 13 lines
kernel/exit.c +7
-2@@ -70,8 +70,13 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); - } -list_del_rcu (&p->thread_group); + }else +/* only remove members from the thread group. +* The thread group leader must stay so that +* while_each_thread() uses can see the end of +* the list and stop. +*/ +list_del_rcu (&p->thread_group);
Hmm... this looks obvously wrong. This leaves the dead thread on the list.
Oleg.
include/linux/sched.h extern bool current_is_single_threaded(void); · line 2273
Quoted 6 lines · NeilBrown
include/linux/sched.h +3
-1+/* Thread group leader can change, so stop loop when we see one +* even if it isn't 'g' */ #define while_each_thread (g, t) \ - while ((t = next_thread(t)) != g) + while ((t = next_thread(t)) != g && !thread_group_leader(t))
This assumes that while_each_thread(g, t) always uses g == group_leader.
kernel/exit.c static void __unhash_process(struct task_struct *p, bool group_dead) · line 79
Quoted 13 lines
kernel/exit.c +7
-2@@ -70,8 +70,13 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); - } -list_del_rcu (&p->thread_group); + }else +/* only remove members from the thread group. +* The thread group leader must stay so that +* while_each_thread() uses can see the end of +* the list and stop. +*/ +list_del_rcu (&p->thread_group);
Hmm... this looks obvously wrong. This leaves the dead thread on the list.
Oleg.
Quoted 11 lines · Paul E. McKenney
Quoted 3 lines · NeilBrown
I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Doesn't the following code in the loop body deal with this possibilty?
/* Exit if t or g was unhashed during refresh. */ if (t->state == TASK_DEAD || g->state == TASK_DEAD) goto unlock;
This code is completely wrong even if while_each_thread() was fine.
I sent the patch but it was ignored.
[PATCH] fix the racy check_hung_uninterruptible_tasks()->rcu_lock_break() http://marc.info/?l=linux-kernel&m=127688790019041
Oleg.
Quoted 11 lines · Paul E. McKenney
Quoted 3 lines · NeilBrown
I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Doesn't the following code in the loop body deal with this possibilty?
/* Exit if t or g was unhashed during refresh. */ if (t->state == TASK_DEAD || g->state == TASK_DEAD) goto unlock;
This code is completely wrong even if while_each_thread() was fine.
I sent the patch but it was ignored.
[PATCH] fix the racy check_hung_uninterruptible_tasks()->rcu_lock_break() http://marc.info/?l=linux-kernel&m=127688790019041
Oleg.
Quoted 13 lines · On Sun, 14 Aug 2011 19:51:19 +0200 Oleg Nesterov…
Quoted 11 lines · Paul E. McKenney
Quoted 3 lines · NeilBrown
I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Doesn't the following code in the loop body deal with this possibilty?
/* Exit if t or g was unhashed during refresh. */ if (t->state == TASK_DEAD || g->state == TASK_DEAD) goto unlock;
Quoted 8 lines
This code is completely wrong even if
while_each_thread()was fine.I sent the patch but it was ignored.
[PATCH] fix the racy check_hung_uninterruptible_tasks()->rcu_lock_break() http://marc.info/?l=linux-kernel&m=127688790019041Oleg.
I agree with that patch.
RCU only protects a task_struct until release_task() is called (which removes it from the task list).
So holding rcu_lock doesn't stop put_task_struct from freeing the memory unless we *know* that release_task hasn't been called. This is exactly that pid_alive() tests.
I must say that handling of task_struct seems to violate the law of least surprise a little to often for my taste. Maybe it is just a difficult problem and it needs a complex solution - but it would be really nice if it were a bit simpler :-(
NeilBrown
Quoted 13 lines · On Sun, 14 Aug 2011 19:51:19 +0200 Oleg Nesterov <[email protected]> wrote:
Quoted 11 lines · Paul E. McKenney
Quoted 3 lines · NeilBrown
I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Doesn't the following code in the loop body deal with this possibilty?
/* Exit if t or g was unhashed during refresh. */ if (t->state == TASK_DEAD || g->state == TASK_DEAD) goto unlock;
Quoted 8 lines
This code is completely wrong even if
while_each_thread()was fine.I sent the patch but it was ignored.
[PATCH] fix the racy check_hung_uninterruptible_tasks()->rcu_lock_break() http://marc.info/?l=linux-kernel&m=127688790019041Oleg.
I agree with that patch.
RCU only protects a task_struct until release_task() is called (which removes it from the task list).
So holding rcu_lock doesn't stop put_task_struct from freeing the memory unless we *know* that release_task hasn't been called. This is exactly that pid_alive() tests.
I must say that handling of task_struct seems to violate the law of least surprise a little to often for my taste. Maybe it is just a difficult problem and it needs a complex solution - but it would be really nice if it were a bit simpler :-(
NeilBrown
Quoted 38 lines · On Sun, 14 Aug 2011 19:40:00 +0200 Oleg Nesterov…
Sorry for delay, just noticed this thread...
Quoted 11 lines · NeilBrown
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Yes. This was already discussed. See http://marc.info/?t=127688987300002
Damn. I forgot about this completely.
Quoted 5 lines
The code in
de_thread()is actually questionable by itself. "list_replace_rcu" cannot really be used on the head of a list - it is only meant to be used on a member of a list.
To move a list from one head to another you should be usinglist_splice_init_rcu().Hmm... can't understand this part.
And just in case...
list_replace_rcu()looks fine afaics. The real problem isrelease_task(old_leader)which doeslist_del_rcu(old_leader->thread_group), this is what breakswhile_each_thread().
Quoted 6 lines
Quoted 1 line
The ->tasks list doesn't seem to have a clearly distinguished 'head'
Exactly. This is the problem.
But: you seem to confused ->tasks and ->thread_group ;)
That last point is certainly correct. I caught myself confusing the two several time and wouldn't be at all surprised if I did it without catching myself.
de_thread can change the group_leader of a thread_group, and release_task can remove a non-leader while leaving the rest of the thread_group intact. So any while_each_thread() loop needs some extra care to ensure that it doesn't loop infinitely, because the "head" that it is looking for might not be there any more.
Maybe there are other rules that ensure this can never happen, but they sure aren't obvious to me (i.e. if you know them - please tell ;-)
NeilBrown
Quoted 38 lines · On Sun, 14 Aug 2011 19:40:00 +0200 Oleg Nesterov <[email protected]> wrote:
Sorry for delay, just noticed this thread...
Quoted 11 lines · NeilBrown
The race as I understand it is with this code:
list_replace_rcu(&leader->tasks, &tsk->tasks); list_replace_init(&leader->sibling, &tsk->sibling); tsk->group_leader = tsk; leader->group_leader = tsk;Quoted 7 lines
which seems to be called with only
tasklist_lockheld, which doesn't seem to be held in the cgroup code.If the "
thread_group_leader(leader)" call incgroup_attach_proc()runs before this chunk is run with the same value for 'leader', but thewhile_each_threadis run after, then thewhile_read_thread()might loop forever.rcu_read_lockdoesn't prevent this from happening.Yes. This was already discussed. See http://marc.info/?t=127688987300002
Damn. I forgot about this completely.
Quoted 5 lines
The code in
de_thread()is actually questionable by itself. "list_replace_rcu" cannot really be used on the head of a list - it is only meant to be used on a member of a list.
To move a list from one head to another you should be usinglist_splice_init_rcu().Hmm... can't understand this part.
And just in case...
list_replace_rcu()looks fine afaics. The real problem isrelease_task(old_leader)which doeslist_del_rcu(old_leader->thread_group), this is what breakswhile_each_thread().
Quoted 6 lines
Quoted 1 line
The ->tasks list doesn't seem to have a clearly distinguished 'head'
Exactly. This is the problem.
But: you seem to confused ->tasks and ->thread_group ;)
That last point is certainly correct. I caught myself confusing the two several time and wouldn't be at all surprised if I did it without catching myself.
de_thread can change the group_leader of a thread_group, and release_task can remove a non-leader while leaving the rest of the thread_group intact. So any while_each_thread() loop needs some extra care to ensure that it doesn't loop infinitely, because the "head" that it is looking for might not be there any more.
Maybe there are other rules that ensure this can never happen, but they sure aren't obvious to me (i.e. if you know them - please tell ;-)
NeilBrown
Paul E. McKenney
Acked-byQuoted 13 lines · Oleg Nesterov
Quoted 11 lines · Paul E. McKenney
Quoted 3 lines · NeilBrown
I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Doesn't the following code in the loop body deal with this possibilty?
/* Exit if t or g was unhashed during refresh. */ if (t->state == TASK_DEAD || g->state == TASK_DEAD) goto unlock;
Quoted 6 lines
This code is completely wrong even if
while_each_thread()was fine.I sent the patch but it was ignored.
[PATCH] fix the racy check_hung_uninterruptible_tasks()->rcu_lock_break() http://marc.info/?l=linux-kernel&m=127688790019041
If it helps...
Thanx, Paul
Paul E. McKenney
Acked-byQuoted 13 lines · Oleg Nesterov
Quoted 11 lines · Paul E. McKenney
Quoted 3 lines · NeilBrown
I disagree. It also requires - by virtue of the use of
while_each_thread()- that 'g' remains on the list that 't' is walking along.Doesn't the following code in the loop body deal with this possibilty?
/* Exit if t or g was unhashed during refresh. */ if (t->state == TASK_DEAD || g->state == TASK_DEAD) goto unlock;
Quoted 6 lines
This code is completely wrong even if
while_each_thread()was fine.I sent the patch but it was ignored.
[PATCH] fix the racy check_hung_uninterruptible_tasks()->rcu_lock_break() http://marc.info/?l=linux-kernel&m=127688790019041
If it helps...
Thanx, Paul
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to a message not in the archiveQuoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.
Yes.
So far I still think we should fix while_each_thread() so that it works under rcu_read_lock() "as exepected", I'll try to think more.
But whatever we do with while_each_thread(), this can't help cgroup_attach_proc(), it needs the locking.
Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {
Agreed, this should work.
But can't we avoid the global list? thread_group_leader() or not, we do not really care. We only need to ensure we can safely find all threads.
How about the patch below?
With or without this/your patch this leader can die right after we drop the lock. ss->can_attach(leader) and ss->attach(leader) look suspicious. If a sub-thread execs, this task_struct has nothing to do with the threadgroup.
Also. This is off-topic, but... Why cgroup_attach_proc() and cgroup_attach_task() do ->attach_task() + cgroup_task_migrate() in the different order? cgroup_attach_proc() looks wrong even if currently doesn't matter.
Oleg.
kernel/cgroup.c +5 -13
@@ -2000,6 +2000,7 @@ int cgroup_attach_proc(struct cgroup *cg/* threadgroup list cursor and array */ struct task_struct *tsk;struct flex_array *group; +unsigned long flags;/* * we need to make sure we have css_sets for all the tasks we're * going to move -before- we actually start moving them, so that in
@@ -2027,19 +2028,10 @@ int cgroup_attach_proc(struct cgroup *cg goto out_free_group_list;/* prevent changes to the threadgroup list while we take a snapshot. */ -rcu_read_lock (); -if (!thread_group_leader (leader)) { -/* -* a race with de_thread from another thread's exec() may strip -* us of our leadership, making while_each_thread unsafe to use -* on this task. if this happens, there is no choice but to -* throw this task away and try again (from cgroup_procs_write); -* this is "double-double-toil-and-trouble-check locking". -*/ -rcu_read_unlock (); - retval = -EAGAIN ; + retval = -EAGAIN ; +if (!lock_task_sighand (leader, &flags)) goto out_free_group_list; - } +/* take a reference on each task in the group to go in the array. */ tsk = leader; i =0 ;
@@ -2055,9 +2047,9 @@ int cgroup_attach_proc(struct cgroup *cgBUG_ON (retval !=0 ); i++; }while_each_thread (leader, tsk); +unlock_task_sighand (leader, &flags);/* remember the number of threads in the array for later. */ group_size = i; -rcu_read_unlock ();/* * step 1: check that we can legitimately attach to the cgroup.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to Ben BlumQuoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.
Yes.
So far I still think we should fix while_each_thread() so that it works under rcu_read_lock() "as exepected", I'll try to think more.
But whatever we do with while_each_thread(), this can't help cgroup_attach_proc(), it needs the locking.
Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {
Agreed, this should work.
But can't we avoid the global list? thread_group_leader() or not, we do not really care. We only need to ensure we can safely find all threads.
How about the patch below?
With or without this/your patch this leader can die right after we drop the lock. ss->can_attach(leader) and ss->attach(leader) look suspicious. If a sub-thread execs, this task_struct has nothing to do with the threadgroup.
Also. This is off-topic, but... Why cgroup_attach_proc() and cgroup_attach_task() do ->attach_task() + cgroup_task_migrate() in the different order? cgroup_attach_proc() looks wrong even if currently doesn't matter.
Oleg.
kernel/cgroup.c +5 -13
@@ -2000,6 +2000,7 @@ int cgroup_attach_proc(struct cgroup *cg/* threadgroup list cursor and array */ struct task_struct *tsk;struct flex_array *group; +unsigned long flags;/* * we need to make sure we have css_sets for all the tasks we're * going to move -before- we actually start moving them, so that in
@@ -2027,19 +2028,10 @@ int cgroup_attach_proc(struct cgroup *cg goto out_free_group_list;/* prevent changes to the threadgroup list while we take a snapshot. */ -rcu_read_lock (); -if (!thread_group_leader (leader)) { -/* -* a race with de_thread from another thread's exec() may strip -* us of our leadership, making while_each_thread unsafe to use -* on this task. if this happens, there is no choice but to -* throw this task away and try again (from cgroup_procs_write); -* this is "double-double-toil-and-trouble-check locking". -*/ -rcu_read_unlock (); - retval = -EAGAIN ; + retval = -EAGAIN ; +if (!lock_task_sighand (leader, &flags)) goto out_free_group_list; - } +/* take a reference on each task in the group to go in the array. */ tsk = leader; i =0 ;
@@ -2055,9 +2047,9 @@ int cgroup_attach_proc(struct cgroup *cgBUG_ON (retval !=0 ); i++; }while_each_thread (leader, tsk); +unlock_task_sighand (leader, &flags);/* remember the number of threads in the array for later. */ group_size = i; -rcu_read_unlock ();/* * step 1: check that we can legitimately attach to the cgroup.
Quoted 8 lines · NeilBrown
de_threadcan change thegroup_leaderof athread_group, andrelease_taskcan remove a non-leader while leaving the rest of thethread_groupintact. So anywhile_each_thread()loop needs some extra care to ensure that it doesn't loop infinitely, because the "head" that it is looking for might not be there any more.
Maybe there are other rules that ensure this can never happen, but they sure aren't obvious to me (i.e. if you know them - please tell ;-)
No, I don't know ;)
And note also that if g != leader, then while_each_thread(g, t) can hang simply because g exits. I am still trying to invent something simple to fix while_each_thread-under-rcu.
This looks possible, but I am starting to think that, say, zap_threads() needs locking anyway. With any fix I can imagine, it can miss a thread we should care about.
Oleg.
Quoted 8 lines · NeilBrown
de_threadcan change thegroup_leaderof athread_group, andrelease_taskcan remove a non-leader while leaving the rest of thethread_groupintact. So anywhile_each_thread()loop needs some extra care to ensure that it doesn't loop infinitely, because the "head" that it is looking for might not be there any more.
Maybe there are other rules that ensure this can never happen, but they sure aren't obvious to me (i.e. if you know them - please tell ;-)
No, I don't know ;)
And note also that if g != leader, then while_each_thread(g, t) can hang simply because g exits. I am still trying to invent something simple to fix while_each_thread-under-rcu.
This looks possible, but I am starting to think that, say, zap_threads() needs locking anyway. With any fix I can imagine, it can miss a thread we should care about.
Oleg.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to a message not in the archiveQuoted 34 lines · Oleg Nesterov
Quoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Yes.
So far I still think we should fix
while_each_thread()so that it works underrcu_read_lock()"as exepected", I'll try to think more.But whatever we do with
while_each_thread(), this can't helpcgroup_attach_proc(), it needs the locking.Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.
Quoted 4 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.
Right. As we concluded in our off-list discussion, if there is no strong reason for that, I'm going to fix that in my task counter patchset because there it really matters. If we can't migrate the thread because it has already exited, we really don't want to call ->attach_task() but rather cancel_attach_task().
Thanks.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to Oleg NesterovQuoted 34 lines · Oleg Nesterov
Quoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Yes.
So far I still think we should fix
while_each_thread()so that it works underrcu_read_lock()"as exepected", I'll try to think more.But whatever we do with
while_each_thread(), this can't helpcgroup_attach_proc(), it needs the locking.Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.
Quoted 4 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.
Right. As we concluded in our off-list discussion, if there is no strong reason for that, I'm going to fix that in my task counter patchset because there it really matters. If we can't migrate the thread because it has already exited, we really don't want to call ->attach_task() but rather cancel_attach_task().
Thanks.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
Quoted 40 lines · Frederic Weisbecker
Quoted 34 lines · Oleg Nesterov
Quoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Yes.
So far I still think we should fix
while_each_thread()so that it works underrcu_read_lock()"as exepected", I'll try to think more.But whatever we do with
while_each_thread(), this can't helpcgroup_attach_proc(), it needs the locking.Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.Quoted 4 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.
Quoted 8 lines
Right. As we concluded in our off-list discussion, if there is no strong reason for that, I'm going to fix that in my task counter patchset because there it really matters. If we can't migrate the thread because it has already exited, we really don't want to call ->attach_task() but rather
cancel_attach_task().Thanks.
Yes. Um, this must have been a mistake on my part. The lines of code should be the other way around. It should be done in a separate bugfix patch, though, so it goes through faster...
-- Ben
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to Frederic WeisbeckerQuoted 40 lines · Frederic Weisbecker
Quoted 34 lines · Oleg Nesterov
Quoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Yes.
So far I still think we should fix
while_each_thread()so that it works underrcu_read_lock()"as exepected", I'll try to think more.But whatever we do with
while_each_thread(), this can't helpcgroup_attach_proc(), it needs the locking.Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.Quoted 4 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.
Quoted 8 lines
Right. As we concluded in our off-list discussion, if there is no strong reason for that, I'm going to fix that in my task counter patchset because there it really matters. If we can't migrate the thread because it has already exited, we really don't want to call ->attach_task() but rather
cancel_attach_task().Thanks.
Yes. Um, this must have been a mistake on my part. The lines of code should be the other way around. It should be done in a separate bugfix patch, though, so it goes through faster...
-- Ben
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to a message not in the archiveQuoted 47 lines · Ben Blum
Quoted 46 lines · Frederic Weisbecker
Quoted 34 lines · Oleg Nesterov
Quoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Yes.
So far I still think we should fix
while_each_thread()so that it works underrcu_read_lock()"as exepected", I'll try to think more.But whatever we do with
while_each_thread(), this can't helpcgroup_attach_proc(), it needs the locking.Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.Quoted 4 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.Right. As we concluded in our off-list discussion, if there is no strong reason for that, I'm going to fix that in my task counter patchset because there it really matters. If we can't migrate the thread because it has already exited, we really don't want to call ->attach_task() but rather
cancel_attach_task().
Quoted 9 lines
Quoted 2 lines
Thanks.
Yes. Um, this must have been a mistake on my part. The lines of code should be the other way around. It should be done in a separate bugfix patch, though, so it goes through faster...
-- Ben
also, there is no cancel_attach_task, afaict. cancel_attach is only for memcg, for the whole operation at once - unless you are changing this, in which case feel free to modify on top of the patch I'm about to send out.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to Ben BlumQuoted 47 lines · Ben Blum
Quoted 46 lines · Frederic Weisbecker
Quoted 34 lines · Oleg Nesterov
Quoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Yes.
So far I still think we should fix
while_each_thread()so that it works underrcu_read_lock()"as exepected", I'll try to think more.But whatever we do with
while_each_thread(), this can't helpcgroup_attach_proc(), it needs the locking.Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.Quoted 4 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.Right. As we concluded in our off-list discussion, if there is no strong reason for that, I'm going to fix that in my task counter patchset because there it really matters. If we can't migrate the thread because it has already exited, we really don't want to call ->attach_task() but rather
cancel_attach_task().
Quoted 9 lines
Quoted 2 lines
Thanks.
Yes. Um, this must have been a mistake on my part. The lines of code should be the other way around. It should be done in a separate bugfix patch, though, so it goes through faster...
-- Ben
also, there is no cancel_attach_task, afaict. cancel_attach is only for memcg, for the whole operation at once - unless you are changing this, in which case feel free to modify on top of the patch I'm about to send out.
PATCH 1/1 cgroups: fix ordering of calls in cgroup_attach_proc
In reply to Frederic WeisbeckerFix ordering of cgroup_task_migrate and attach_task in cgroup_attach_proc.
--- kernel/cgroup.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
kernel/cgroup.c +9 -6
@@ -2132,14 +2132,17 @@ oldcgrp =task_cgroup_from_root (tsk, root);if (cgrp == oldcgrp)continue ; -/* attach each task to each subsystem */ -for_each_subsys (root, ss) { -if (ss->attach_task) - ss->attach_task (cgrp, tsk); - }/* if the thread is PF_EXITING, it can just get skipped. */ retval =cgroup_task_migrate (cgrp, oldcgrp, tsk, true); -BUG_ON (retval !=0 && retval != -ESRCH ); +if (retval ==0 ) { +/* attach each task to each subsystem */ +for_each_subsys (root, ss) { +if (ss->attach_task) + ss->attach_task (cgrp, tsk); + } + }else { +BUG_ON (retval != -ESRCH ); + } }/* nothing is sensitive to fork() after this point. */
PATCH 1/1 cgroups: fix ordering of calls in cgroup_attach_proc
In reply to Frederic WeisbeckerFix ordering of cgroup_task_migrate and attach_task in cgroup_attach_proc.
--- kernel/cgroup.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
kernel/cgroup.c +9 -6
@@ -2132,14 +2132,17 @@ oldcgrp =task_cgroup_from_root (tsk, root);if (cgrp == oldcgrp)continue ; -/* attach each task to each subsystem */ -for_each_subsys (root, ss) { -if (ss->attach_task) - ss->attach_task (cgrp, tsk); - }/* if the thread is PF_EXITING, it can just get skipped. */ retval =cgroup_task_migrate (cgrp, oldcgrp, tsk, true); -BUG_ON (retval !=0 && retval != -ESRCH ); +if (retval ==0 ) { +/* attach each task to each subsystem */ +for_each_subsys (root, ss) { +if (ss->attach_task) + ss->attach_task (cgrp, tsk); + } + }else { +BUG_ON (retval != -ESRCH ); + } }/* nothing is sensitive to fork() after this point. */
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to a message not in the archiveQuoted 55 lines · Ben Blum
Quoted 54 lines · Ben Blum
Quoted 40 lines · Frederic Weisbecker
Quoted 34 lines · Oleg Nesterov
Quoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Yes.
So far I still think we should fix
while_each_thread()so that it works underrcu_read_lock()"as exepected", I'll try to think more.But whatever we do with
while_each_thread(), this can't helpcgroup_attach_proc(), it needs the locking.Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.Quoted 4 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.Quoted 8 lines
Right. As we concluded in our off-list discussion, if there is no strong reason for that, I'm going to fix that in my task counter patchset because there it really matters. If we can't migrate the thread because it has already exited, we really don't want to call ->attach_task() but rather
cancel_attach_task().Thanks.
Yes. Um, this must have been a mistake on my part. The lines of code should be the other way around. It should be done in a separate bugfix patch, though, so it goes through faster...
Quoted 7 lines
Quoted 2 lines
-- Ben
also, there is no
cancel_attach_task, afaict.cancel_attachis only for memcg, for the whole operation at once - unless you are changing this, in which case feel free to modify on top of the patch I'm about to send out.
Yeah right, I forgot it's something I'm adding with the task counter subsystem.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to Ben BlumQuoted 55 lines · Ben Blum
Quoted 54 lines · Ben Blum
Quoted 40 lines · Frederic Weisbecker
Quoted 34 lines · Oleg Nesterov
Quoted 5 lines · Ben Blum
According to this thread - https://lkml.org/lkml/2011/7/27/243 - RCU is not sufficient to guarantee the tasklist is stable w.r.t.
de_threadand exit. Takingtasklist_lockfor reading, instead ofrcu_read_lock, ensures proper exclusion.Yes.
So far I still think we should fix
while_each_thread()so that it works underrcu_read_lock()"as exepected", I'll try to think more.But whatever we do with
while_each_thread(), this can't helpcgroup_attach_proc(), it needs the locking.Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.Quoted 4 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.Quoted 8 lines
Right. As we concluded in our off-list discussion, if there is no strong reason for that, I'm going to fix that in my task counter patchset because there it really matters. If we can't migrate the thread because it has already exited, we really don't want to call ->attach_task() but rather
cancel_attach_task().Thanks.
Yes. Um, this must have been a mistake on my part. The lines of code should be the other way around. It should be done in a separate bugfix patch, though, so it goes through faster...
Quoted 7 lines
Quoted 2 lines
-- Ben
also, there is no
cancel_attach_task, afaict.cancel_attachis only for memcg, for the whole operation at once - unless you are changing this, in which case feel free to modify on top of the patch I'm about to send out.
Yeah right, I forgot it's something I'm adding with the task counter subsystem.
Frederic Weisbecker
Acked-byRe: [PATCH][BUGFIX] cgroups: fix ordering of calls in cgroup_attach_proc
In reply to a message not in the archiveQuoted 3 lines · Ben Blum
Fix ordering of
cgroup_task_migrateandattach_taskincgroup_attach_proc.Signed-off-by Ben Blum <[email protected]>
Re: [PATCH][BUGFIX] cgroups: fix ordering of calls in cgroup_attach_proc
In reply to Ben BlumQuoted 3 lines · Ben Blum
Fix ordering of
cgroup_task_migrateandattach_taskincgroup_attach_proc.Signed-off-by Ben Blum <[email protected]>
Paul Menage
Acked-byRe: [PATCH][BUGFIX] cgroups: fix ordering of calls in cgroup_attach_proc
In reply to a message not in the archiveQuoted 3 lines · Ben Blum
Fix ordering of
cgroup_task_migrateandattach_taskincgroup_attach_proc.Signed-off-by Ben Blum <[email protected]>
BTW, I left Google at the end of last week - if it's not bouncing
already, [email protected] isn't going to work for much longer. I'll try to get a patch out to the maintainers files.
Paul
Re: [PATCH][BUGFIX] cgroups: fix ordering of calls in cgroup_attach_proc
In reply to Ben BlumQuoted 3 lines · Ben Blum
Fix ordering of
cgroup_task_migrateandattach_taskincgroup_attach_proc.Signed-off-by Ben Blum <[email protected]>
BTW, I left Google at the end of last week - if it's not bouncing already, [email protected] isn't going to work for much longer. I'll try to get a patch out to the maintainers files.
Paul
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to a message not in the archiveQuoted 10 lines · Oleg Nesterov
Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
I was content with the tasklist_lock because cgroup_attach_proc is already a pretty heavyweight operation, and probably pretty rare that a user would want to do multiple of them at once quickly. I asked Andrew to take the simple tasklist_lock patch just now, since it does fix the bug at least.
Anyway, looking at this, hmm. I am not sure if this protects adequately? In de_thread, the sighand lock is held only around the first half (around zap_other_threads), and not around the following section where leadership is transferred (esp. around the list_replace calls). tasklist_lock is held here, though, so it seems like the right lock to hold.
Quoted 6 lines
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.
hmm. I thought I had this case covered, but it's been so long since I actually wrote the code that if I did I can't remember how. I think exiting is no issue since we hold a reference on the task_struct, but exec may still be a problem. I'm thinking:
- cgroup_attach_proc drops the tasklist_lock
- a sub-thread execs, and in exec_mmap (after de_thread) changes the mm
- ss->attach, for example in memcg, wants to use leader->mm, which is now wrong
this seems to be possible as the code currently is. I wonder if the best fix is just to have exec (maybe around de_thread) bounce off of or hold threadgroup_fork_read_lock somewhere?
Quoted 7 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.
(already submitted a patch for this)
Thanks,
Ben
Quoted 50 lines
Oleg.
kernel/cgroup.c +5
-13@@ -2000,6 +2000,7 @@ int cgroup_attach_proc(struct cgroup *cg/* threadgroup list cursor and array */ struct task_struct *tsk;struct flex_array *group; +unsigned long flags;/* * we need to make sure we have css_sets for all the tasks we're * going to move -before- we actually start moving them, so that in @@ -2027,19 +2028,10 @@ int cgroup_attach_proc(struct cgroup *cg goto out_free_group_list;/* prevent changes to the threadgroup list while we take a snapshot. */ -rcu_read_lock (); -if (!thread_group_leader (leader)) { -/* -* a race with de_thread from another thread's exec() may strip -* us of our leadership, making while_each_thread unsafe to use -* on this task. if this happens, there is no choice but to -* throw this task away and try again (from cgroup_procs_write); -* this is "double-double-toil-and-trouble-check locking". -*/ -rcu_read_unlock (); - retval = -EAGAIN ; + retval = -EAGAIN ; +if (!lock_task_sighand (leader, &flags)) goto out_free_group_list; - } +/* take a reference on each task in the group to go in the array. */ tsk = leader; i =0 ;@@ -2055,9 +2047,9 @@ int cgroup_attach_proc(struct cgroup *cgBUG_ON (retval !=0 ); i++; }while_each_thread (leader, tsk); +unlock_task_sighand (leader, &flags);/* remember the number of threads in the array for later. */ group_size = i; -rcu_read_unlock ();/* * step 1: check that we can legitimately attach to the cgroup.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to Oleg NesterovQuoted 10 lines · Oleg Nesterov
Review
Quoted 3 lines
(quoted diff) +1
-1- rcu_read_lock(); + read_lock(&tasklist_lock); if (!thread_group_leader(leader)) {Agreed, this should work.
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
I was content with the tasklist_lock because cgroup_attach_proc is already a pretty heavyweight operation, and probably pretty rare that a user would want to do multiple of them at once quickly. I asked Andrew to take the simple tasklist_lock patch just now, since it does fix the bug at least.
Anyway, looking at this, hmm. I am not sure if this protects adequately? In de_thread, the sighand lock is held only around the first half (around zap_other_threads), and not around the following section where leadership is transferred (esp. around the list_replace calls). tasklist_lock is held here, though, so it seems like the right lock to hold.
Quoted 6 lines
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.
hmm. I thought I had this case covered, but it's been so long since I actually wrote the code that if I did I can't remember how. I think exiting is no issue since we hold a reference on the task_struct, but exec may still be a problem. I'm thinking:
- cgroup_attach_proc drops the tasklist_lock
- a sub-thread execs, and in exec_mmap (after de_thread) changes the mm
- ss->attach, for example in memcg, wants to use leader->mm, which is now wrong
this seems to be possible as the code currently is. I wonder if the best fix is just to have exec (maybe around de_thread) bounce off of or hold threadgroup_fork_read_lock somewhere?
Quoted 7 lines
Also. This is off-topic, but... Why
cgroup_attach_proc()andcgroup_attach_task()do ->attach_task() +cgroup_task_migrate()in the different order?cgroup_attach_proc()looks wrong even if currently doesn't matter.
(already submitted a patch for this)
Thanks,
Ben
Quoted 50 lines
Oleg.
kernel/cgroup.c +5
-13@@ -2000,6 +2000,7 @@ int cgroup_attach_proc(struct cgroup *cg/* threadgroup list cursor and array */ struct task_struct *tsk;struct flex_array *group; +unsigned long flags;/* * we need to make sure we have css_sets for all the tasks we're * going to move -before- we actually start moving them, so that in @@ -2027,19 +2028,10 @@ int cgroup_attach_proc(struct cgroup *cg goto out_free_group_list;/* prevent changes to the threadgroup list while we take a snapshot. */ -rcu_read_lock (); -if (!thread_group_leader (leader)) { -/* -* a race with de_thread from another thread's exec() may strip -* us of our leadership, making while_each_thread unsafe to use -* on this task. if this happens, there is no choice but to -* throw this task away and try again (from cgroup_procs_write); -* this is "double-double-toil-and-trouble-check locking". -*/ -rcu_read_unlock (); - retval = -EAGAIN ; + retval = -EAGAIN ; +if (!lock_task_sighand (leader, &flags)) goto out_free_group_list; - } +/* take a reference on each task in the group to go in the array. */ tsk = leader; i =0 ;@@ -2055,9 +2047,9 @@ int cgroup_attach_proc(struct cgroup *cgBUG_ON (retval !=0 ); i++; }while_each_thread (leader, tsk); +unlock_task_sighand (leader, &flags);/* remember the number of threads in the array for later. */ group_size = i; -rcu_read_unlock ();/* * step 1: check that we can legitimately attach to the cgroup.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to a message not in the archiveQuoted 11 lines · Ben Blum
Quoted 5 lines · Oleg Nesterov
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
I was content with the
tasklist_lockbecausecgroup_attach_procis already a pretty heavyweight operation, and probably pretty rare that a user would want to do multiple of them at once quickly.
Perhaps. But this is the global lock, no matter what. Why do you prefer to take it instead of per-process ->siglock?
Quoted 3 lines
I asked Andrew
to take the simpletasklist_lockpatch just now, since it does fix the bug at least.
I disagree.
Quoted 3 lines
Anyway, looking at this, hmm. I am not sure if this protects adequately? In
de_thread, the sighand lock is held only around the first half (aroundzap_other_threads),
We only need this lock to find all threads,
Quoted 4 lines
and not around the following section where
leadership is transferred (esp. around thelist_replacecalls).tasklist_lockis held here, though, so it seems like the right lock to hold.
This doesn't matter at all, I think. The code under while_each_thread() doesn't need the stable ->group_leader, and it can be changed right after you drop tasklist. list_replace() calls do not play with ->thread_group list.
How can tasklist make any difference?
Quoted 7 lines
Quoted 4 lines
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.hmm. I thought I had this case covered, but it's been so long since I actually wrote the code that if I did I can't remember how.
This all doesn't look right... Hopefully addressed by Tejun patches.
Oleg.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to Ben BlumQuoted 11 lines · Ben Blum
Quoted 5 lines · Oleg Nesterov
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.How about the patch below?
I was content with the
tasklist_lockbecausecgroup_attach_procis already a pretty heavyweight operation, and probably pretty rare that a user would want to do multiple of them at once quickly.
Perhaps. But this is the global lock, no matter what. Why do you prefer to take it instead of per-process ->siglock?
Quoted 3 lines
I asked Andrew
to take the simpletasklist_lockpatch just now, since it does fix the bug at least.
I disagree.
Quoted 3 lines
Anyway, looking at this, hmm. I am not sure if this protects adequately? In
de_thread, the sighand lock is held only around the first half (aroundzap_other_threads),
We only need this lock to find all threads,
Quoted 4 lines
and not around the following section where
leadership is transferred (esp. around thelist_replacecalls).tasklist_lockis held here, though, so it seems like the right lock to hold.
This doesn't matter at all, I think. The code under while_each_thread() doesn't need the stable ->group_leader, and it can be changed right after you drop tasklist. list_replace() calls do not play with ->thread_group list.
How can tasklist make any difference?
Quoted 7 lines
Quoted 4 lines
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.hmm. I thought I had this case covered, but it's been so long since I actually wrote the code that if I did I can't remember how.
This all doesn't look right... Hopefully addressed by Tejun patches.
Oleg.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to a message not in the archiveQuoted 7 lines · Oleg Nesterov
Quoted 6 lines · Ben Blum
Quoted 4 lines · Oleg Nesterov
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
How about the patch below?
I was content with the
tasklist_lockbecausecgroup_attach_procis already a pretty heavyweight operation, and probably pretty rare that a user would want to do multiple of them at once quickly.Perhaps. But this is the global lock, no matter what. Why do you prefer to take it instead of per-process ->siglock?
Basically just because I wanted to find the simplest race-free implementation - at that point I was just shooting to have a complete set of patches, and didn't judge the global lock to be bad enough to think about replacing. I could yet be convinced that siglock is better.
Quoted 18 lines
Quoted 4 lines
I asked Andrew
to take the simpletasklist_lockpatch just now, since it does fix the bug at least.I disagree.
Quoted 3 lines
Anyway, looking at this, hmm. I am not sure if this protects adequately? In
de_thread, the sighand lock is held only around the first half (aroundzap_other_threads),We only need this lock to find all threads,
Quoted 4 lines
and not around the following section where
leadership is transferred (esp. around thelist_replacecalls).tasklist_lockis held here, though, so it seems like the right lock to hold.
Quoted 6 lines
This doesn't matter at all, I think. The code under
while_each_thread()doesn't need the stable ->group_leader, and it can be changed right after you drop tasklist.list_replace()calls do not play with ->thread_group list.How can tasklist make any difference?
Oops. I misread the list_replace calls as being on ->thread_group, instead of on ->tasks as they actually are. Hm, and I see __exit_signal takes the sighand lock around the call to __unhash_process.
OK, I believe it will work. Oh, and I guess the sighand lock also subsumes the group_leader check that you were objecting to in the other thread.
Sorry for the delay. Any way this change goes through, though, the commenting should be clear on how de_thread interacts with it. Something like:
/* If the leader exits, its links on the thread_group list become
* invalid. One way this can happen is if a sub-thread does exec() when
* de_thread() calls release_task(leader) (and leader->sighand gets set
* to NULL, in which case lock_task_sighand will fail). Since in that
* case the threadgroup is still around, cgroup_procs_write should try
* again (finding the new leader), which EAGAIN indicates here. This is
* "double-double-toil-and-trouble-check locking". */
(I rather like the phrase "double-double-..." to describe the overall approach, and would prefer if it stayed in the comment.)
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.hmm. I thought I had this case covered, but it's been so long since I actually wrote the code that if I did I can't remember how.
This all doesn't look right... Hopefully addressed by Tejun patches.
Will look into those separately.
Thanks,
Ben
Quoted 4 lines
Oleg.
Re: [PATCH][BUGFIX] cgroups: more safe tasklist locking in cgroup_attach_proc
In reply to Oleg NesterovQuoted 7 lines · Oleg Nesterov
Quoted 6 lines · Ben Blum
Quoted 4 lines · Oleg Nesterov
But can't we avoid the global list?
thread_group_leader()or not, we do not really care. We only need to ensure we can safely find all threads.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
How about the patch below?
I was content with the
tasklist_lockbecausecgroup_attach_procis already a pretty heavyweight operation, and probably pretty rare that a user would want to do multiple of them at once quickly.Perhaps. But this is the global lock, no matter what. Why do you prefer to take it instead of per-process ->siglock?
Basically just because I wanted to find the simplest race-free implementation - at that point I was just shooting to have a complete set of patches, and didn't judge the global lock to be bad enough to think about replacing. I could yet be convinced that siglock is better.
Quoted 18 lines
Quoted 4 lines
I asked Andrew
to take the simpletasklist_lockpatch just now, since it does fix the bug at least.I disagree.
Quoted 3 lines
Anyway, looking at this, hmm. I am not sure if this protects adequately? In
de_thread, the sighand lock is held only around the first half (aroundzap_other_threads),We only need this lock to find all threads,
Quoted 4 lines
and not around the following section where
leadership is transferred (esp. around thelist_replacecalls).tasklist_lockis held here, though, so it seems like the right lock to hold.
Quoted 6 lines
This doesn't matter at all, I think. The code under
while_each_thread()doesn't need the stable ->group_leader, and it can be changed right after you drop tasklist.list_replace()calls do not play with ->thread_group list.How can tasklist make any difference?
Oops. I misread the list_replace calls as being on ->thread_group, instead of on ->tasks as they actually are. Hm, and I see __exit_signal takes the sighand lock around the call to __unhash_process.
OK, I believe it will work. Oh, and I guess the sighand lock also subsumes the group_leader check that you were objecting to in the other thread.
Sorry for the delay. Any way this change goes through, though, the commenting should be clear on how de_thread interacts with it. Something like:
/* If the leader exits, its links on the thread_group list become
* invalid. One way this can happen is if a sub-thread does exec() when
* de_thread() calls release_task(leader) (and leader->sighand gets set
* to NULL, in which case lock_task_sighand will fail). Since in that
* case the threadgroup is still around, cgroup_procs_write should try
* again (finding the new leader), which EAGAIN indicates here. This is
* "double-double-toil-and-trouble-check locking". */
(I rather like the phrase "double-double-..." to describe the overall approach, and would prefer if it stayed in the comment.)
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
With or without this/your patch this leader can die right after we drop the lock.
ss->can_attach(leader)andss->attach(leader)look suspicious. If a sub-thread execs, thistask_structhas nothing to do with the threadgroup.hmm. I thought I had this case covered, but it's been so long since I actually wrote the code that if I did I can't remember how.
This all doesn't look right... Hopefully addressed by Tejun patches.
Will look into those separately.
Thanks,
Ben
Quoted 4 lines
Oleg.
PATCH 1/2 cgroups: use sighand lock instead of tasklist_lock in cgroup_attach_proc
In reply to Oleg NesterovUse sighand lock instead of tasklist_lock in cgroup_attach_proc.
From: Ben Blum <[email protected]>
--- kernel/cgroup.c | 27 +++++++++++++-------------- 1 files changed, 13 insertions(+), 14 deletions(-)
kernel/cgroup.c +13 -14
@@ -2003,6 +2003,7 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)/* threadgroup list cursor and array */ struct task_struct *tsk;struct flex_array *group; +unsigned long flags;/* * we need to make sure we have css_sets for all the tasks we're * going to move -before- we actually start moving them, so that in
@@ -2029,20 +2030,18 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)if (retval) goto out_free_group_list; -/* prevent changes to the threadgroup list while we take a snapshot. */ -read_lock (&tasklist_lock); -if (!thread_group_leader (leader)) { -/* -* a race with de_thread from another thread's exec() may strip -* us of our leadership, making while_each_thread unsafe to use -* on this task. if this happens, there is no choice but to -* throw this task away and try again (from cgroup_procs_write); -* this is "double-double-toil-and-trouble-check locking". -*/ -read_unlock (&tasklist_lock); - retval = -EAGAIN ; +/* prevent changes to the threadgroup list while we take a snapshot. +* If the leader exits, its links on the thread_group list become +* invalid. One way this can happen is if a sub-thread does exec() when +* de_thread() calls release_task(leader) (and leader->sighand gets set +* to NULL, in which case lock_task_sighand will fail). Since in that +* case the threadgroup is still around, cgroup_procs_write should try +* again (finding the new leader), which EAGAIN indicates here. This is +* double-double-toil-and-trouble-check locking". */ + retval = -EAGAIN ; +if (!lock_task_sighand (leader, &flags)) goto out_free_group_list; - } +/* take a reference on each task in the group to go in the array. */ tsk = leader; i =0 ;
@@ -2058,9 +2057,9 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)BUG_ON (retval !=0 ); i++; }while_each_thread (leader, tsk); +unlock_task_sighand (leader, &flags);/* remember the number of threads in the array for later. */ group_size = i; -read_unlock (&tasklist_lock);/* * step 1: check that we can legitimately attach to the cgroup.
PATCH 2/2 cgroups: convert ss->attach to use whole threadgroup flex_array (cpuset, memcontrol)
In reply to Oleg NesterovConvert ss->attach to take a flex_array of tasks instead of just the leader.
From: Ben Blum <[email protected]>
This lets subsystems with whole-threadgroup attach calls (i.e., cpuset and memcontrol) to accurately find the group's mm even when a non-leader does exec and leaves the leader with a NULL mm pointer.
Also converts cpuset and memcontrol to take the flex_array and iterate down it until an mm is found, instead of just attempting to use the leader's mm.
--- Documentation/cgroups/cgroups.txt | 7 ++++++- include/linux/cgroup.h | 4 +++- kernel/cgroup.c | 16 +++++++++++++--- kernel/cpuset.c | 16 ++++++++++++++-- mm/memcontrol.c | 17 +++++++++++++++-- 5 files changed, 51 insertions(+), 9 deletions(-)
Documentation/cgroups/cgroups.txt +6 -1
@@ -637,12 +637,17 @@ For any non-per-thread attachment work that needs to happen before attach_task. Needed by cpuset. void attach(struct cgroup_subsys *ss, struct cgroup *cgrp, - struct cgroup *old_cgrp, struct task_struct *task) + struct cgroup *old_cgrp, struct flex_array *group, + int group_size) (cgroup_mutex held by caller) Called after the task has been attached to the cgroup, to allow any post-attachment activity that requires memory allocations or blocking. +The flex_array contains pointers to every task_struct being moved, so +that subsystems can, for example, iterate over a threadgroup's tasks to +find one with an mm that needs to be moved. + void attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp, struct task_struct *tsk); (cgroup_mutex held by caller)
include/linux/cgroup.h +3 -1
@@ -25,6 +25,7 @@ struct cgroupfs_root;struct inode ;struct cgroup ;struct css_id ; +struct flex_array ;extern int cgroup_init_early (void );extern int cgroup_init (void );
@@ -481,7 +482,8 @@ struct cgroup_subsys {void (*attach_task)(struct cgroup *cgrp,struct cgroup *old_cgrp,struct task_struct *tsk);void (*attach)(struct cgroup_subsys *ss,struct cgroup *cgrp, -struct cgroup *old_cgrp,struct task_struct *tsk); +struct cgroup *old_cgrp,struct flex_array *group, +int group_size);int (*fork)(struct cgroup_subsys *ss,struct task_struct *task);void (*exit)(struct cgroup_subsys *ss,struct cgroup *cgrp,struct cgroup *old_cgrp,struct task_struct *task);
kernel/cgroup.c +13 -3
@@ -1824,10 +1824,18 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)struct cgroup *oldcgrp;struct cgroupfs_root *root = cgrp->root; +/* Singleton array, for ss->attach (see cgroup_attach_proc). */ +struct flex_array *group =flex_array_alloc (sizeof (tsk),1 ,GFP_KERNEL ); +if (!group) +return -ENOMEM ; + retval =flex_array_put_ptr (group,0 , tsk,GFP_KERNEL ); +if (retval <0 ) + goto out_free_array; +/* Nothing to do if the task is already in that cgroup */ oldcgrp =task_cgroup_from_root (tsk, root);if (cgrp == oldcgrp) -return 0 ; + goto out_free_array;for_each_subsys (root, ss) {if (ss->can_attach) {
@@ -1862,7 +1870,7 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)if (ss->attach_task) ss->attach_task (cgrp, oldcgrp, tsk);if (ss->attach) - ss->attach (ss, cgrp, oldcgrp, tsk); + ss->attach (ss, cgrp, oldcgrp, group,1 ); }synchronize_rcu ();
@@ -1890,6 +1898,8 @@ out: ss->cancel_attach (ss, cgrp, tsk); } } +out_free_array: +flex_array_free (group);return retval; }
@@ -2164,7 +2174,7 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)*/ for_each_subsys (root, ss) {if (ss->attach) - ss->attach (ss, cgrp, oldcgrp, leader); + ss->attach (ss, cgrp, oldcgrp, group, group_size); }/*
kernel/cpuset.c +14 -2
@@ -59,6 +59,7 @@#include <linux/mutex.h> #include <linux/workqueue.h> #include <linux/cgroup.h> +#include <linux/flex_array.h> /* * Workqueue for cpuset related tasks.
@@ -1440,11 +1441,13 @@ static void cpuset_attach_task(struct cgroup *cont, struct cgroup *old, }static void cpuset_attach (struct cgroup_subsys *ss,struct cgroup *cont, -struct cgroup *oldcont,struct task_struct *tsk) +struct cgroup *oldcont,struct flex_array *group, +int group_size) {struct mm_struct *mm;struct cpuset *cs =cgroup_cs (cont);struct cpuset *oldcs =cgroup_cs (oldcont); +int i;/* * Change mm, possibly for multiple threads in a threadgroup. This is
@@ -1452,7 +1455,16 @@ static void cpuset_attach(struct cgroup_subsys *ss, struct cgroup *cont,*/ cpuset_attach_nodemask_from = oldcs->mems_allowed; cpuset_attach_nodemask_to = cs->mems_allowed; - mm =get_task_mm (tsk); +/* +* Find the first task in the group that still has its mm. (This could +* be not the first one if another did exec() and the leader exited. +*/ +for (i =0 ; i < group_size; i++) { +struct task_struct *tsk =flex_array_get_ptr (group, i); + mm =get_task_mm (tsk); +if (mm) +break ; + }if (mm) {mpol_rebind_mm (mm, &cpuset_attach_nodemask_to);if (is_memory_migrate (cs))
mm/memcontrol.c +15 -2
@@ -49,6 +49,7 @@#include <linux/page_cgroup.h> #include <linux/cpu.h> #include <linux/oom.h> +#include <linux/flex_array.h> #include "internal.h" #include <asm/uaccess.h>
@@ -5455,9 +5456,21 @@ retry:static void mem_cgroup_move_task (struct cgroup_subsys *ss,struct cgroup *cont,struct cgroup *old_cont, -struct task_struct *p) +struct flex_array *group,int group_size) { -struct mm_struct *mm =get_task_mm (p); +struct mm_struct *mm; +int i; + +/* +* Find the first task in the group that still has its mm. (This could +* be not the first one if another did exec() and the leader exited. +*/ +for (i =0 ; i < group_size; i++) { +struct task_struct *tsk =flex_array_get_ptr (group, i); + mm =get_task_mm (tsk); +if (mm) +break ; + }if (mm) {if (mc.to)
Re: [PATCH 1/2] cgroups: use sighand lock instead of tasklist_lock in cgroup_attach_proc
In reply to Ben BlumHi Ben,
Quoted 1 line · Ben Blum
Use sighand lock instead of
tasklist_lockincgroup_attach_proc.
Could you please feed more the changelog to explain the reason of the change? Well, the reason of converting tasklist_lock based locking to something more granular is almost obvious these days, but
it would be nice to know why it is safe to do so. And what exactly that protects.
Thanks.
Re: [PATCH 2/2] cgroups: convert ss->attach to use whole threadgroup flex_array (cpuset, memcontrol)
In reply to Ben BlumQuoted 4 lines · Ben Blum
Convert
ss->attachto take aflex_arrayof tasks instead of just the leader.From: Ben Blum <[email protected]>
Quoted 8 lines
This lets subsystems with whole-threadgroup attach calls (i.e., cpuset and memcontrol) to accurately find the group's mm even when a non-leader does exec and leaves the leader with a NULL mm pointer.
Also converts cpuset and memcontrol to take the
flex_arrayand iterate down it until an mm is found, instead of just attempting to use the leader's mm.Signed-off-by Ben Blum <[email protected]>
I think there are patches from Tejun that handle that did that already?
Re: [PATCH 2/2] cgroups: convert ss->attach to use whole threadgroup flex_array (cpuset, memcontrol)
Quoted 9 lines · Frederic Weisbecker
Quoted 8 lines · Ben Blum
Convert
ss->attachto take aflex_arrayof tasks instead of just the leader.From: Ben Blum <[email protected]>
This lets subsystems with whole-threadgroup attach calls (i.e., cpuset and memcontrol) to accurately find the group's mm even when a non-leader does exec and leaves the leader with a NULL mm pointer.
Quoted 9 lines
Quoted 4 lines
Also converts cpuset and memcontrol to take the
flex_arrayand iterate down it until an mm is found, instead of just attempting to use the leader's mm.Signed-off-by Ben Blum <[email protected]>I think there are patches from Tejun that handle that did that already?
Yeah, I'm not hoping to preempt them or anything; I'm just presenting this alternate approach, since (1) this way addresses the ss->attach() problem directly without needing to add locking anywhere else, while the focus of the discussion around Tejun's patches seems to have moved to more global concerns, and (2) these patches and his patches ought to be compatible with each other.
Ben
Re: [PATCH 2/2] cgroups: convert ss->attach to use whole threadgroup flex_array (cpuset, memcontrol)
Quoted 20 lines · Ben Blum
Quoted 9 lines · Frederic Weisbecker
Quoted 8 lines · Ben Blum
Convert
ss->attachto take aflex_arrayof tasks instead of just the leader.From: Ben Blum <[email protected]>
This lets subsystems with whole-threadgroup attach calls (i.e., cpuset and memcontrol) to accurately find the group's mm even when a non-leader does exec and leaves the leader with a NULL mm pointer.
Quoted 9 lines
Quoted 4 lines
Also converts cpuset and memcontrol to take the
flex_arrayand iterate down it until an mm is found, instead of just attempting to use the leader's mm.Signed-off-by Ben Blum <[email protected]>I think there are patches from Tejun that handle that did that already?
Quoted 6 lines
Yeah, I'm not hoping to preempt them or anything; I'm just presenting this alternate approach, since (1) this way addresses the
ss->attach()problem directly without needing to add locking anywhere else, while the focus of the discussion around Tejun's patches seems to have moved to more global concerns, and (2) these patches and his patches ought to be compatible with each other.
though, I did wonder if, given this approach, I shouldn't just get rid of attach_task and can_attach_task and use the flex_array for all of the calls. would that be nicer?
Quoted 3 lines
Ben
Re: [PATCH 2/2] cgroups: convert ss->attach to use whole threadgroup flex_array (cpuset, memcontrol)
In reply to Ben BlumQuoted 15 lines · Ben Blum
Quoted 14 lines · Frederic Weisbecker
Quoted 12 lines · Ben Blum
Convert
ss->attachto take aflex_arrayof tasks instead of just the leader.From: Ben Blum <[email protected]>
This lets subsystems with whole-threadgroup attach calls (i.e., cpuset and memcontrol) to accurately find the group's mm even when a non-leader does exec and leaves the leader with a NULL mm pointer.
Also converts cpuset and memcontrol to take the
flex_arrayand iterate down it until an mm is found, instead of just attempting to use the leader's mm.Signed-off-by Ben Blum <[email protected]>
Quoted 8 lines
Quoted 4 lines
I think there are patches from Tejun that handle that did that already?
Yeah, I'm not hoping to preempt them or anything; I'm just presenting this alternate approach, since (1) this way addresses the
ss->attach()problem directly without needing to add locking anywhere else
You basically have the same approach, the locking involved in Tejun's patches seem to address a different issue that you would need to address as well anyway.
Quoted 4 lines
, while the
focus of the discussion around Tejun's patches seems to have moved to more global concerns, and (2) these patches and his patches ought to be compatible with each other.
But Tejun's patches seem to address not only your issue but even more. I don't understand the point of this lighter version.
Re: [PATCH 2/2] cgroups: convert ss->attach to use whole threadgroup flex_array (cpuset, memcontrol)
In reply to Ben BlumQuoted 28 lines · Ben Blum
Quoted 20 lines · Ben Blum
Quoted 9 lines · Frederic Weisbecker
Quoted 8 lines · Ben Blum
Convert
ss->attachto take aflex_arrayof tasks instead of just the leader.From: Ben Blum <[email protected]>
This lets subsystems with whole-threadgroup attach calls (i.e., cpuset and memcontrol) to accurately find the group's mm even when a non-leader does exec and leaves the leader with a NULL mm pointer.
Quoted 9 lines
Quoted 4 lines
Also converts cpuset and memcontrol to take the
flex_arrayand iterate down it until an mm is found, instead of just attempting to use the leader's mm.Signed-off-by Ben Blum <[email protected]>I think there are patches from Tejun that handle that did that already?
Quoted 6 lines
Yeah, I'm not hoping to preempt them or anything; I'm just presenting this alternate approach, since (1) this way addresses the
ss->attach()problem directly without needing to add locking anywhere else, while the focus of the discussion around Tejun's patches seems to have moved to more global concerns, and (2) these patches and his patches ought to be compatible with each other.
Quoted 3 lines
though, I did wonder if, given this approach, I shouldn't just get rid of
attach_taskandcan_attach_taskand use theflex_arrayfor all of the calls. would that be nicer?
That's what Tejun's patches do.
Re: [PATCH 2/2] cgroups: convert ss->attach to use whole threadgroup flex_array (cpuset, memcontrol)
Quoted 29 lines · Frederic Weisbecker
Quoted 28 lines · Ben Blum
Quoted 26 lines · Ben Blum
Quoted 9 lines · Frederic Weisbecker
Quoted 8 lines · Ben Blum
Convert
ss->attachto take aflex_arrayof tasks instead of just the leader.From: Ben Blum <[email protected]>
This lets subsystems with whole-threadgroup attach calls (i.e., cpuset and memcontrol) to accurately find the group's mm even when a non-leader does exec and leaves the leader with a NULL mm pointer.
Quoted 9 lines
Quoted 4 lines
Also converts cpuset and memcontrol to take the
flex_arrayand iterate down it until an mm is found, instead of just attempting to use the leader's mm.Signed-off-by Ben Blum <[email protected]>I think there are patches from Tejun that handle that did that already?
Yeah, I'm not hoping to preempt them or anything; I'm just presenting this alternate approach, since (1) this way addresses the
ss->attach()problem directly without needing to add locking anywhere else, while the focus of the discussion around Tejun's patches seems to have moved to more global concerns, and (2) these patches and his patches ought to be compatible with each other.
Quoted 6 lines
Quoted 3 lines
though, I did wonder if, given this approach, I shouldn't just get rid of
attach_taskandcan_attach_taskand use theflex_arrayfor all of the calls. would that be nicer?That's what Tejun's patches do.
Oh, I must have missed that bit. In which case, sorry for the noise, and I support Tejun's change.
-- Ben
Re: [PATCH 2/2] cgroups: convert ss->attach to use whole threadgroup flex_array (cpuset, memcontrol)
In reply to Ben BlumQuoted 4 lines · Ben Blum
Convert
ss->attachto take aflex_arrayof tasks instead of just the leader.From: Ben Blum <[email protected]>
Quoted 8 lines
This lets subsystems with whole-threadgroup attach calls (i.e., cpuset and memcontrol) to accurately find the group's mm even when a non-leader does exec and leaves the leader with a NULL mm pointer.
Also converts cpuset and memcontrol to take the
flex_arrayand iterate down it until an mm is found, instead of just attempting to use the leader's mm.Signed-off-by Ben Blum <[email protected]>
Thanks,
Paul
Quoted 15 lines
---
Documentation/cgroups/cgroups.txt | 7 ++++++-
include/linux/cgroup.h | 4 +++-kernel/cgroup.c | 16 +++++++++++++--- kernel/cpuset.c | 16 ++++++++++++++-- mm/memcontrol.c | 17 +++++++++++++++-- 5 files changed, 51 insertions(+), 9 deletions(-)Documentation/cgroups/cgroups.txt +0
-0@@ -637,12 +637,17 @@ For any non-per-thread attachment work that needs to happen beforeattach_task. Needed by cpuset.
Quoted 4 lines
void attach(struct
cgroup_subsys*ss, struct cgroup *cgrp,
- struct cgroup*old_cgrp, structtask_struct*task)
+ struct cgroup*old_cgrp, structflex_array*group,
+ intgroup_size)
Maybe flex_array should have a size field, so that you can just pass a single pointer here?
Documentation/cgroups/cgroups.txt static void cpuset_attach(struct cgroup_subsys *ss, struct cgroup *cont, · line 1460
Quoted 8 lines
Documentation/cgroups/cgroups.txt +3
-1@@ -1452,7 +1455,16 @@ static void cpuset_attach(struct cgroup_subsys *ss, struct cgroup *cont, */ cpuset_attach_nodemask_from = oldcs->mems_allowed; cpuset_attach_nodemask_to = cs->mems_allowed; - mm = get_task_mm(tsk); + /* + * Find the first task in the group that still has its mm. (This could + * be not the first one if another did exec() and the leader exited.
Maybe rephrase this as "This might not be the first one"?
Paul