Commit e8c82d20 authored by Li Zefan's avatar Li Zefan Committed by Tejun Heo

cgroup: convert cgroup_cft_commit() to use cgroup_for_each_descendant_pre()

We used root->allcg_list to iterate cgroup hierarchy because at that time
cgroup_for_each_descendant_pre() hasn't been invented.

tj: In cgroup_cfts_commit(), s/@serial_nr/@update_upto/, move the
    assignment right above releasing cgroup_mutex and explain what's
    going on there.
Signed-off-by: default avatarLi Zefan <lizefan@huawei.com>
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
parent 794611a1
...@@ -206,9 +206,6 @@ struct cgroup { ...@@ -206,9 +206,6 @@ struct cgroup {
*/ */
struct list_head cset_links; struct list_head cset_links;
struct list_head allcg_node; /* cgroupfs_root->allcg_list */
struct list_head cft_q_node; /* used during cftype add/rm */
/* /*
* Linked list running through all cgroups that can * Linked list running through all cgroups that can
* potentially be reaped by the release agent. Protected by * potentially be reaped by the release agent. Protected by
...@@ -313,9 +310,6 @@ struct cgroupfs_root { ...@@ -313,9 +310,6 @@ struct cgroupfs_root {
/* A list running through the active hierarchies */ /* A list running through the active hierarchies */
struct list_head root_list; struct list_head root_list;
/* All cgroups on this root, cgroup_mutex protected */
struct list_head allcg_list;
/* Hierarchy-specific flags */ /* Hierarchy-specific flags */
unsigned long flags; unsigned long flags;
......
...@@ -1399,7 +1399,6 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp) ...@@ -1399,7 +1399,6 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
INIT_LIST_HEAD(&cgrp->children); INIT_LIST_HEAD(&cgrp->children);
INIT_LIST_HEAD(&cgrp->files); INIT_LIST_HEAD(&cgrp->files);
INIT_LIST_HEAD(&cgrp->cset_links); INIT_LIST_HEAD(&cgrp->cset_links);
INIT_LIST_HEAD(&cgrp->allcg_node);
INIT_LIST_HEAD(&cgrp->release_list); INIT_LIST_HEAD(&cgrp->release_list);
INIT_LIST_HEAD(&cgrp->pidlists); INIT_LIST_HEAD(&cgrp->pidlists);
mutex_init(&cgrp->pidlist_mutex); mutex_init(&cgrp->pidlist_mutex);
...@@ -1414,12 +1413,10 @@ static void init_cgroup_root(struct cgroupfs_root *root) ...@@ -1414,12 +1413,10 @@ static void init_cgroup_root(struct cgroupfs_root *root)
INIT_LIST_HEAD(&root->subsys_list); INIT_LIST_HEAD(&root->subsys_list);
INIT_LIST_HEAD(&root->root_list); INIT_LIST_HEAD(&root->root_list);
INIT_LIST_HEAD(&root->allcg_list);
root->number_of_cgroups = 1; root->number_of_cgroups = 1;
cgrp->root = root; cgrp->root = root;
cgrp->name = &root_cgroup_name; cgrp->name = &root_cgroup_name;
init_cgroup_housekeeping(cgrp); init_cgroup_housekeeping(cgrp);
list_add_tail(&cgrp->allcg_node, &root->allcg_list);
} }
static int cgroup_init_root_id(struct cgroupfs_root *root) static int cgroup_init_root_id(struct cgroupfs_root *root)
...@@ -2785,65 +2782,78 @@ static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, ...@@ -2785,65 +2782,78 @@ static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
return ret; return ret;
} }
static DEFINE_MUTEX(cgroup_cft_mutex);
static void cgroup_cfts_prepare(void) static void cgroup_cfts_prepare(void)
__acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex) __acquires(&cgroup_mutex)
{ {
/* /*
* Thanks to the entanglement with vfs inode locking, we can't walk * Thanks to the entanglement with vfs inode locking, we can't walk
* the existing cgroups under cgroup_mutex and create files. * the existing cgroups under cgroup_mutex and create files.
* Instead, we increment reference on all cgroups and build list of * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
* them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure * read lock before calling cgroup_addrm_files().
* exclusive access to the field.
*/ */
mutex_lock(&cgroup_cft_mutex);
mutex_lock(&cgroup_mutex); mutex_lock(&cgroup_mutex);
} }
static void cgroup_cfts_commit(struct cgroup_subsys *ss, static void cgroup_cfts_commit(struct cgroup_subsys *ss,
struct cftype *cfts, bool is_add) struct cftype *cfts, bool is_add)
__releases(&cgroup_mutex) __releases(&cgroup_cft_mutex) __releases(&cgroup_mutex)
{ {
LIST_HEAD(pending); LIST_HEAD(pending);
struct cgroup *cgrp, *n; struct cgroup *cgrp, *root = &ss->root->top_cgroup;
struct super_block *sb = ss->root->sb; struct super_block *sb = ss->root->sb;
struct dentry *prev = NULL;
struct inode *inode;
u64 update_upto;
/* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
if (cfts && ss->root != &rootnode && if (!cfts || ss->root == &rootnode ||
atomic_inc_not_zero(sb->s_active)) { !atomic_inc_not_zero(&sb->s_active)) {
list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) { mutex_unlock(&cgroup_mutex);
dget(cgrp->dentry); return;
list_add_tail(&cgrp->cft_q_node, &pending);
}
} else {
sb = NULL;
} }
mutex_unlock(&cgroup_mutex);
/* /*
* All new cgroups will see @cfts update on @ss->cftsets. Add/rm * All cgroups which are created after we drop cgroup_mutex will
* files for all cgroups which were created before. * have the updated set of files, so we only need to update the
* cgroups created before the current @cgroup_serial_nr_cursor.
*/ */
list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) { update_upto = atomic64_read(&cgroup_serial_nr_cursor);
struct inode *inode = cgrp->dentry->d_inode;
mutex_unlock(&cgroup_mutex);
/* @root always needs to be updated */
inode = root->dentry->d_inode;
mutex_lock(&inode->i_mutex);
mutex_lock(&cgroup_mutex);
cgroup_addrm_files(root, ss, cfts, is_add);
mutex_unlock(&cgroup_mutex);
mutex_unlock(&inode->i_mutex);
/* add/rm files for all cgroups created before */
rcu_read_lock();
cgroup_for_each_descendant_pre(cgrp, root) {
if (cgroup_is_dead(cgrp))
continue;
inode = cgrp->dentry->d_inode;
dget(cgrp->dentry);
rcu_read_unlock();
dput(prev);
prev = cgrp->dentry;
mutex_lock(&inode->i_mutex); mutex_lock(&inode->i_mutex);
mutex_lock(&cgroup_mutex); mutex_lock(&cgroup_mutex);
if (!cgroup_is_dead(cgrp)) if (cgrp->serial_nr <= update_upto && !cgroup_is_dead(cgrp))
cgroup_addrm_files(cgrp, ss, cfts, is_add); cgroup_addrm_files(cgrp, ss, cfts, is_add);
mutex_unlock(&cgroup_mutex); mutex_unlock(&cgroup_mutex);
mutex_unlock(&inode->i_mutex); mutex_unlock(&inode->i_mutex);
list_del_init(&cgrp->cft_q_node); rcu_read_lock();
dput(cgrp->dentry);
} }
rcu_read_unlock();
if (sb) dput(prev);
deactivate_super(sb); deactivate_super(sb);
mutex_unlock(&cgroup_cft_mutex);
} }
/** /**
...@@ -4320,7 +4330,6 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry, ...@@ -4320,7 +4330,6 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
cgrp->serial_nr = atomic64_inc_return(&cgroup_serial_nr_cursor); cgrp->serial_nr = atomic64_inc_return(&cgroup_serial_nr_cursor);
/* allocation complete, commit to creation */ /* allocation complete, commit to creation */
list_add_tail(&cgrp->allcg_node, &root->allcg_list);
list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
root->number_of_cgroups++; root->number_of_cgroups++;
...@@ -4559,7 +4568,6 @@ static void cgroup_offline_fn(struct work_struct *work) ...@@ -4559,7 +4568,6 @@ static void cgroup_offline_fn(struct work_struct *work)
/* delete this cgroup from parent->children */ /* delete this cgroup from parent->children */
list_del_rcu(&cgrp->sibling); list_del_rcu(&cgrp->sibling);
list_del_init(&cgrp->allcg_node);
dput(d); dput(d);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment