From: Srivatsa Vaddagiri Currently cpuset_exit() changes the exiting task's ->cpuset pointer w/o taking task_lock(). This can lead to ugly races between attach_task and cpuset_exit. Details of the races are described at http://lkml.org/lkml/2007/3/24/132. Patch below closes those races. It is against 2.6.21-rc4 and has undergone a simple compile/boot test on a x86_64 box. Signed-off-by: Srivatsa Vaddagiri Cc: Paul Jackson Cc: Balbir Singh Cc: Paul Menage Signed-off-by: Andrew Morton --- kernel/cpuset.c | 22 ++++++++++++---------- 1 files changed, 12 insertions(+), 10 deletions(-) diff -puN kernel/cpuset.c~fix-race-between-attach_task-and-cpuset_exit kernel/cpuset.c --- a/kernel/cpuset.c~fix-race-between-attach_task-and-cpuset_exit +++ a/kernel/cpuset.c @@ -1120,6 +1120,7 @@ static int attach_task(struct cpuset *cs pid_t pid; struct task_struct *tsk; struct cpuset *oldcs; + struct cpuset *oldcs_to_be_released = NULL; cpumask_t cpus; nodemask_t from, to; struct mm_struct *mm; @@ -1175,6 +1176,8 @@ static int attach_task(struct cpuset *cs } atomic_inc(&cs->count); rcu_assign_pointer(tsk->cpuset, cs); + if (atomic_dec_and_test(&oldcs->count)) + oldcs_to_be_released = oldcs; task_unlock(tsk); guarantee_online_cpus(cs, &cpus); @@ -1195,8 +1198,8 @@ static int attach_task(struct cpuset *cs put_task_struct(tsk); synchronize_rcu(); - if (atomic_dec_and_test(&oldcs->count)) - check_for_release(oldcs, ppathbuf); + if (oldcs_to_be_released) + check_for_release(oldcs_to_be_released, ppathbuf); return 0; } @@ -2120,10 +2123,6 @@ void cpuset_fork(struct task_struct *chi * it is holding that mutex while calling check_for_release(), * which calls kmalloc(), so can't be called holding callback_mutex(). * - * We don't need to task_lock() this reference to tsk->cpuset, - * because tsk is already marked PF_EXITING, so attach_task() won't - * mess with it, or task is a failed fork, never visible to attach_task. - * * the_top_cpuset_hack: * * Set the exiting tasks cpuset to the root cpuset (top_cpuset). @@ -2161,20 +2160,23 @@ void cpuset_fork(struct task_struct *chi void cpuset_exit(struct task_struct *tsk) { struct cpuset *cs; + struct cpuset *oldcs_to_be_released = NULL; + task_lock(tsk); cs = tsk->cpuset; tsk->cpuset = &top_cpuset; /* the_top_cpuset_hack - see above */ + if (atomic_dec_and_test(&cs->count)) + oldcs_to_be_released = cs; + task_unlock(tsk); if (notify_on_release(cs)) { char *pathbuf = NULL; mutex_lock(&manage_mutex); - if (atomic_dec_and_test(&cs->count)) - check_for_release(cs, &pathbuf); + if (oldcs_to_be_released) + check_for_release(oldcs_to_be_released, &pathbuf); mutex_unlock(&manage_mutex); cpuset_release_agent(pathbuf); - } else { - atomic_dec(&cs->count); } } _