Commit 59f5296b authored by Tejun Heo's avatar Tejun Heo

cgroup: misc preps for kernfs conversion

* Un-inline seq_css().  After kernfs conversion, the function will
  need to dereference internal data structures.

* Add cgroup_get/put_root() and replace direct super_block->s_active
  manipulatinos with them.  These will be converted to kernfs_root
  refcnting.

* Add cgroup_get/put() and replace dget/put() on cgrp->dentry with
  them.  These will be converted to kernfs refcnting.

* Update current_css_set_cg_links_read() to use cgroup_name() instead
  of reaching into the dentry name.  The end result is the same.

These changes don't make functional differences but will make
transition to kernfs easier.

v2: Rebased on top of 0ab02ca8 ("cgroup: protect modifications to
    cgroup_idr with cgroup_mutex").
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Acked-by: default avatarLi Zefan <lizefan@huawei.com>
parent b1664924
...@@ -516,18 +516,14 @@ static inline ino_t cgroup_ino(struct cgroup *cgrp) ...@@ -516,18 +516,14 @@ static inline ino_t cgroup_ino(struct cgroup *cgrp)
return 0; return 0;
} }
static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
{
struct cgroup_open_file *of = seq->private;
return of->cfe->css;
}
static inline struct cftype *seq_cft(struct seq_file *seq) static inline struct cftype *seq_cft(struct seq_file *seq)
{ {
struct cgroup_open_file *of = seq->private; struct cgroup_open_file *of = seq->private;
return of->cfe->type; return of->cfe->type;
} }
struct cgroup_subsys_state *seq_css(struct seq_file *seq);
int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts); int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
int cgroup_rm_cftypes(struct cftype *cfts); int cgroup_rm_cftypes(struct cftype *cfts);
......
...@@ -169,6 +169,7 @@ static int need_forkexit_callback __read_mostly; ...@@ -169,6 +169,7 @@ static int need_forkexit_callback __read_mostly;
static struct cftype cgroup_base_files[]; static struct cftype cgroup_base_files[];
static void cgroup_put(struct cgroup *cgrp);
static void cgroup_destroy_css_killed(struct cgroup *cgrp); static void cgroup_destroy_css_killed(struct cgroup *cgrp);
static int cgroup_destroy_locked(struct cgroup *cgrp); static int cgroup_destroy_locked(struct cgroup *cgrp);
static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
...@@ -204,6 +205,13 @@ static inline bool cgroup_is_dead(const struct cgroup *cgrp) ...@@ -204,6 +205,13 @@ static inline bool cgroup_is_dead(const struct cgroup *cgrp)
return test_bit(CGRP_DEAD, &cgrp->flags); return test_bit(CGRP_DEAD, &cgrp->flags);
} }
struct cgroup_subsys_state *seq_css(struct seq_file *seq)
{
struct cgroup_open_file *of = seq->private;
return of->cfe->css;
}
EXPORT_SYMBOL_GPL(seq_css);
/** /**
* cgroup_is_descendant - test ancestry * cgroup_is_descendant - test ancestry
* @cgrp: the cgroup to be tested * @cgrp: the cgroup to be tested
...@@ -682,6 +690,16 @@ static struct css_set *find_css_set(struct css_set *old_cset, ...@@ -682,6 +690,16 @@ static struct css_set *find_css_set(struct css_set *old_cset,
return cset; return cset;
} }
static void cgroup_get_root(struct cgroupfs_root *root)
{
atomic_inc(&root->sb->s_active);
}
static void cgroup_put_root(struct cgroupfs_root *root)
{
deactivate_super(root->sb);
}
/* /*
* Return the cgroup for "task" from the given hierarchy. Must be * Return the cgroup for "task" from the given hierarchy. Must be
* called with cgroup_mutex held. * called with cgroup_mutex held.
...@@ -837,18 +855,14 @@ static void cgroup_free_fn(struct work_struct *work) ...@@ -837,18 +855,14 @@ static void cgroup_free_fn(struct work_struct *work)
mutex_unlock(&cgroup_mutex); mutex_unlock(&cgroup_mutex);
/* /*
* We get a ref to the parent's dentry, and put the ref when * We get a ref to the parent, and put the ref when this cgroup is
* this cgroup is being freed, so it's guaranteed that the * being freed, so it's guaranteed that the parent won't be
* parent won't be destroyed before its children. * destroyed before its children.
*/ */
dput(cgrp->parent->dentry); cgroup_put(cgrp->parent);
/* /* put the root reference that we took when we created the cgroup */
* Drop the active superblock reference that we took when we cgroup_put_root(cgrp->root);
* created the cgroup. This will free cgrp->root, if we are
* holding the last reference to @sb.
*/
deactivate_super(cgrp->root->sb);
cgroup_pidlist_destroy_all(cgrp); cgroup_pidlist_destroy_all(cgrp);
...@@ -866,6 +880,11 @@ static void cgroup_free_rcu(struct rcu_head *head) ...@@ -866,6 +880,11 @@ static void cgroup_free_rcu(struct rcu_head *head)
queue_work(cgroup_destroy_wq, &cgrp->destroy_work); queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
} }
static void cgroup_get(struct cgroup *cgrp)
{
dget(cgrp->dentry);
}
static void cgroup_diput(struct dentry *dentry, struct inode *inode) static void cgroup_diput(struct dentry *dentry, struct inode *inode)
{ {
/* is dentry a directory ? if so, kfree() associated cgroup */ /* is dentry a directory ? if so, kfree() associated cgroup */
...@@ -899,6 +918,11 @@ static void cgroup_diput(struct dentry *dentry, struct inode *inode) ...@@ -899,6 +918,11 @@ static void cgroup_diput(struct dentry *dentry, struct inode *inode)
iput(inode); iput(inode);
} }
static void cgroup_put(struct cgroup *cgrp)
{
dput(cgrp->dentry);
}
static void remove_dir(struct dentry *d) static void remove_dir(struct dentry *d)
{ {
struct dentry *parent = dget(d->d_parent); struct dentry *parent = dget(d->d_parent);
...@@ -2724,7 +2748,7 @@ static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) ...@@ -2724,7 +2748,7 @@ static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
struct cgroup_subsys *ss = cfts[0].ss; struct cgroup_subsys *ss = cfts[0].ss;
struct cgroup *root = &ss->root->top_cgroup; struct cgroup *root = &ss->root->top_cgroup;
struct super_block *sb = ss->root->sb; struct super_block *sb = ss->root->sb;
struct dentry *prev = NULL; struct cgroup *prev = NULL;
struct inode *inode; struct inode *inode;
struct cgroup_subsys_state *css; struct cgroup_subsys_state *css;
u64 update_before; u64 update_before;
...@@ -2754,9 +2778,10 @@ static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) ...@@ -2754,9 +2778,10 @@ static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
continue; continue;
inode = cgrp->dentry->d_inode; inode = cgrp->dentry->d_inode;
dget(cgrp->dentry); cgroup_get(cgrp);
dput(prev); if (prev)
prev = cgrp->dentry; cgroup_put(prev);
prev = cgrp;
mutex_unlock(&cgroup_tree_mutex); mutex_unlock(&cgroup_tree_mutex);
mutex_lock(&inode->i_mutex); mutex_lock(&inode->i_mutex);
...@@ -2768,8 +2793,8 @@ static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) ...@@ -2768,8 +2793,8 @@ static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
break; break;
} }
mutex_unlock(&cgroup_tree_mutex); mutex_unlock(&cgroup_tree_mutex);
dput(prev); cgroup_put(prev);
deactivate_super(sb); cgroup_put_root(ss->root);
return ret; return ret;
} }
...@@ -3863,11 +3888,9 @@ static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, ...@@ -3863,11 +3888,9 @@ static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
*/ */
static void cgroup_dput(struct cgroup *cgrp) static void cgroup_dput(struct cgroup *cgrp)
{ {
struct super_block *sb = cgrp->root->sb; cgroup_get_root(cgrp->root);
cgroup_put(cgrp);
atomic_inc(&sb->s_active); cgroup_put_root(cgrp->root);
dput(cgrp->dentry);
deactivate_super(sb);
} }
static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
...@@ -4118,7 +4141,7 @@ static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) ...@@ -4118,7 +4141,7 @@ static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
if (err) if (err)
goto err_free; goto err_free;
dget(cgrp->dentry); cgroup_get(cgrp);
css_get(css->parent); css_get(css->parent);
if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
...@@ -4197,7 +4220,7 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry, ...@@ -4197,7 +4220,7 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
* can be done outside cgroup_mutex, since the sb can't * can be done outside cgroup_mutex, since the sb can't
* disappear while someone has an open control file on the * disappear while someone has an open control file on the
* fs */ * fs */
atomic_inc(&sb->s_active); cgroup_get_root(root);
init_cgroup_housekeeping(cgrp); init_cgroup_housekeeping(cgrp);
...@@ -4231,7 +4254,7 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry, ...@@ -4231,7 +4254,7 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
root->number_of_cgroups++; root->number_of_cgroups++;
/* hold a ref to the parent's dentry */ /* hold a ref to the parent's dentry */
dget(parent->dentry); cgroup_get(parent);
/* /*
* @cgrp is now fully operational. If something fails after this * @cgrp is now fully operational. If something fails after this
...@@ -4261,7 +4284,7 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry, ...@@ -4261,7 +4284,7 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
err_free_id: err_free_id:
idr_remove(&root->cgroup_idr, cgrp->id); idr_remove(&root->cgroup_idr, cgrp->id);
/* Release the reference count that we took on the superblock */ /* Release the reference count that we took on the superblock */
deactivate_super(sb); cgroup_put_root(root);
err_unlock: err_unlock:
mutex_unlock(&cgroup_mutex); mutex_unlock(&cgroup_mutex);
err_unlock_tree: err_unlock_tree:
...@@ -4493,7 +4516,6 @@ static int cgroup_destroy_locked(struct cgroup *cgrp) ...@@ -4493,7 +4516,6 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
static void cgroup_destroy_css_killed(struct cgroup *cgrp) static void cgroup_destroy_css_killed(struct cgroup *cgrp)
{ {
struct cgroup *parent = cgrp->parent; struct cgroup *parent = cgrp->parent;
struct dentry *d = cgrp->dentry;
lockdep_assert_held(&cgroup_tree_mutex); lockdep_assert_held(&cgroup_tree_mutex);
lockdep_assert_held(&cgroup_mutex); lockdep_assert_held(&cgroup_mutex);
...@@ -4501,7 +4523,7 @@ static void cgroup_destroy_css_killed(struct cgroup *cgrp) ...@@ -4501,7 +4523,7 @@ static void cgroup_destroy_css_killed(struct cgroup *cgrp)
/* delete this cgroup from parent->children */ /* delete this cgroup from parent->children */
list_del_rcu(&cgrp->sibling); list_del_rcu(&cgrp->sibling);
dput(d); cgroup_put(cgrp);
set_bit(CGRP_RELEASABLE, &parent->flags); set_bit(CGRP_RELEASABLE, &parent->flags);
check_for_release(parent); check_for_release(parent);
...@@ -5161,12 +5183,11 @@ static int current_css_set_cg_links_read(struct seq_file *seq, void *v) ...@@ -5161,12 +5183,11 @@ static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
cset = rcu_dereference(current->cgroups); cset = rcu_dereference(current->cgroups);
list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
struct cgroup *c = link->cgrp; struct cgroup *c = link->cgrp;
const char *name; const char *name = "?";
if (c != cgroup_dummy_top)
name = cgroup_name(c);
if (c->dentry)
name = c->dentry->d_name.name;
else
name = "?";
seq_printf(seq, "Root %d group %s\n", seq_printf(seq, "Root %d group %s\n",
c->root->hierarchy_id, name); c->root->hierarchy_id, name);
} }
......
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