Skip to content

Commit

Permalink
cgroup: simplify threadgroup locking
Browse files Browse the repository at this point in the history
Now that threadgroup locking is made global, code paths around it can
be simplified.

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

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

Signed-off-by: Tejun Heo <[email protected]>
  • Loading branch information
htejun committed May 27, 2015
1 parent d59cfc0 commit b5ba75b
Showing 1 changed file with 13 additions and 35 deletions.
48 changes: 13 additions & 35 deletions kernel/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -2402,14 +2402,13 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
if (!cgrp)
return -ENODEV;

retry_find_task:
percpu_down_write(&cgroup_threadgroup_rwsem);
rcu_read_lock();
if (pid) {
tsk = find_task_by_vpid(pid);
if (!tsk) {
rcu_read_unlock();
ret = -ESRCH;
goto out_unlock_cgroup;
goto out_unlock_rcu;
}
/*
* even if we're attaching all tasks in the thread group, we
Expand All @@ -2419,9 +2418,8 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
!uid_eq(cred->euid, tcred->uid) &&
!uid_eq(cred->euid, tcred->suid)) {
rcu_read_unlock();
ret = -EACCES;
goto out_unlock_cgroup;
goto out_unlock_rcu;
}
} else
tsk = current;
Expand All @@ -2436,35 +2434,21 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
*/
if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
ret = -EINVAL;
rcu_read_unlock();
goto out_unlock_cgroup;
goto out_unlock_rcu;
}

get_task_struct(tsk);
rcu_read_unlock();

percpu_down_write(&cgroup_threadgroup_rwsem);
if (threadgroup) {
if (!thread_group_leader(tsk)) {
/*
* a race with de_thread from another thread's exec()
* may strip us of our leadership, if this happens,
* there is no choice but to throw this task away and
* try again; this is
* "double-double-toil-and-trouble-check locking".
*/
percpu_up_write(&cgroup_threadgroup_rwsem);
put_task_struct(tsk);
goto retry_find_task;
}
}

ret = cgroup_attach_task(cgrp, tsk, threadgroup);

percpu_up_write(&cgroup_threadgroup_rwsem);

put_task_struct(tsk);
out_unlock_cgroup:
goto out_unlock_threadgroup;

out_unlock_rcu:
rcu_read_unlock();
out_unlock_threadgroup:
percpu_up_write(&cgroup_threadgroup_rwsem);
cgroup_kn_unlock(of->kn);
return ret ?: nbytes;
}
Expand Down Expand Up @@ -2611,6 +2595,8 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)

lockdep_assert_held(&cgroup_mutex);

percpu_down_write(&cgroup_threadgroup_rwsem);

/* look up all csses currently attached to @cgrp's subtree */
down_read(&css_set_rwsem);
css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
Expand Down Expand Up @@ -2666,17 +2652,8 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
goto out_finish;
last_task = task;

percpu_down_write(&cgroup_threadgroup_rwsem);
/* raced against de_thread() from another thread? */
if (!thread_group_leader(task)) {
percpu_up_write(&cgroup_threadgroup_rwsem);
put_task_struct(task);
continue;
}

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

percpu_up_write(&cgroup_threadgroup_rwsem);
put_task_struct(task);

if (WARN(ret, "cgroup: failed to update controllers for the default hierarchy (%d), further operations may crash or hang\n", ret))
Expand All @@ -2686,6 +2663,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)

out_finish:
cgroup_migrate_finish(&preloaded_csets);
percpu_up_write(&cgroup_threadgroup_rwsem);
return ret;
}

Expand Down

0 comments on commit b5ba75b

Please sign in to comment.