Skip to content

Commit 1a90dd5

Browse files
committed
cgroup: deactivate CSS's and mark cgroup dead before invoking ->pre_destroy()
Because ->pre_destroy() could fail and can't be called under cgroup_mutex, cgroup destruction did something very ugly. 1. Grab cgroup_mutex and verify it can be destroyed; fail otherwise. 2. Release cgroup_mutex and call ->pre_destroy(). 3. Re-grab cgroup_mutex and verify it can still be destroyed; fail otherwise. 4. Continue destroying. In addition to being ugly, it has been always broken in various ways. For example, memcg ->pre_destroy() expects the cgroup to be inactive after it's done but tasks can be attached and detached between Digilent#2 and #3 and the conditions that memcg verified in ->pre_destroy() might no longer hold by the time control reaches #3. Now that ->pre_destroy() is no longer allowed to fail. We can switch to the following. 1. Grab cgroup_mutex and verify it can be destroyed; fail otherwise. 2. Deactivate CSS's and mark the cgroup removed thus preventing any further operations which can invalidate the verification from Digilent#1. 3. Release cgroup_mutex and call ->pre_destroy(). 4. Re-grab cgroup_mutex and continue destroying. After this change, controllers can safely assume that ->pre_destroy() will only be called only once for a given cgroup and, once ->pre_destroy() is called, the cgroup will stay dormant till it's destroyed. This removes the only reason ->pre_destroy() can fail - new task being attached or child cgroup being created inbetween. Error out path is removed and ->pre_destroy() invocation is open coded in cgroup_rmdir(). v2: cgroup_call_pre_destroy() removal moved to this patch per Michal. Commit message updated per Glauber. Signed-off-by: Tejun Heo <tj@kernel.org> Reviewed-by: Michal Hocko <mhocko@suse.cz> Reviewed-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Acked-by: Li Zefan <lizefan@huawei.com> Cc: Glauber Costa <glommer@parallels.com>
1 parent 976c06b commit 1a90dd5

1 file changed

Lines changed: 19 additions & 46 deletions

File tree

kernel/cgroup.c

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -851,27 +851,6 @@ static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
851851
return inode;
852852
}
853853

854-
/*
855-
* Call subsys's pre_destroy handler.
856-
* This is called before css refcnt check.
857-
*/
858-
static int cgroup_call_pre_destroy(struct cgroup *cgrp)
859-
{
860-
struct cgroup_subsys *ss;
861-
int ret = 0;
862-
863-
for_each_subsys(cgrp->root, ss) {
864-
if (!ss->pre_destroy)
865-
continue;
866-
867-
ret = ss->pre_destroy(cgrp);
868-
if (WARN_ON_ONCE(ret))
869-
break;
870-
}
871-
872-
return ret;
873-
}
874-
875854
static void cgroup_diput(struct dentry *dentry, struct inode *inode)
876855
{
877856
/* is dentry a directory ? if so, kfree() associated cgroup */
@@ -4078,19 +4057,6 @@ static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
40784057
DEFINE_WAIT(wait);
40794058
struct cgroup_event *event, *tmp;
40804059
struct cgroup_subsys *ss;
4081-
int ret;
4082-
4083-
/* the vfs holds both inode->i_mutex already */
4084-
mutex_lock(&cgroup_mutex);
4085-
if (atomic_read(&cgrp->count) != 0) {
4086-
mutex_unlock(&cgroup_mutex);
4087-
return -EBUSY;
4088-
}
4089-
if (!list_empty(&cgrp->children)) {
4090-
mutex_unlock(&cgroup_mutex);
4091-
return -EBUSY;
4092-
}
4093-
mutex_unlock(&cgroup_mutex);
40944060

40954061
/*
40964062
* In general, subsystem has no css->refcnt after pre_destroy(). But
@@ -4103,16 +4069,7 @@ static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
41034069
*/
41044070
set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
41054071

4106-
/*
4107-
* Call pre_destroy handlers of subsys. Notify subsystems
4108-
* that rmdir() request comes.
4109-
*/
4110-
ret = cgroup_call_pre_destroy(cgrp);
4111-
if (ret) {
4112-
clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4113-
return ret;
4114-
}
4115-
4072+
/* the vfs holds both inode->i_mutex already */
41164073
mutex_lock(&cgroup_mutex);
41174074
parent = cgrp->parent;
41184075
if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
@@ -4122,13 +4079,30 @@ static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
41224079
}
41234080
prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
41244081

4125-
/* block new css_tryget() by deactivating refcnt */
4082+
/*
4083+
* Block new css_tryget() by deactivating refcnt and mark @cgrp
4084+
* removed. This makes future css_tryget() and child creation
4085+
* attempts fail thus maintaining the removal conditions verified
4086+
* above.
4087+
*/
41264088
for_each_subsys(cgrp->root, ss) {
41274089
struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
41284090

41294091
WARN_ON(atomic_read(&css->refcnt) < 0);
41304092
atomic_add(CSS_DEACT_BIAS, &css->refcnt);
41314093
}
4094+
set_bit(CGRP_REMOVED, &cgrp->flags);
4095+
4096+
/*
4097+
* Tell subsystems to initate destruction. pre_destroy() should be
4098+
* called with cgroup_mutex unlocked. See 3fa59dfbc3 ("cgroup: fix
4099+
* potential deadlock in pre_destroy") for details.
4100+
*/
4101+
mutex_unlock(&cgroup_mutex);
4102+
for_each_subsys(cgrp->root, ss)
4103+
if (ss->pre_destroy)
4104+
WARN_ON_ONCE(ss->pre_destroy(cgrp));
4105+
mutex_lock(&cgroup_mutex);
41324106

41334107
/*
41344108
* Put all the base refs. Each css holds an extra reference to the
@@ -4144,7 +4118,6 @@ static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
41444118
clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
41454119

41464120
raw_spin_lock(&release_list_lock);
4147-
set_bit(CGRP_REMOVED, &cgrp->flags);
41484121
if (!list_empty(&cgrp->release_list))
41494122
list_del_init(&cgrp->release_list);
41504123
raw_spin_unlock(&release_list_lock);

0 commit comments

Comments
 (0)