cgroup: refactor mount path and clearly distinguish v1 and v2 paths
While sharing some mechanisms, the mount paths of v1 and v2 are substantially different. Their implementations were mixed in cgroup_mount(). This patch splits them out so that they're easier to follow and organize. This patch causes one functional change - the WARN_ON(new_sb) gets lost. This is because the actual mounting gets moved to cgroup_do_mount() and thus @new_sb is no longer accessible by default to cgroup1_mount(). While we can add it as an explicit out parameter to cgroup_do_mount(), this part of code hasn't changed and the warning hasn't triggered for quite a while. Dropping it should be fine. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Acked-by: Zefan Li <lizefan@huawei.com>
This commit is contained in:
parent
0a268dbd79
commit
633feee310
@ -1989,48 +1989,55 @@ out:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct dentry *cgroup_mount(struct file_system_type *fs_type,
|
static struct dentry *cgroup_do_mount(struct file_system_type *fs_type,
|
||||||
int flags, const char *unused_dev_name,
|
int flags, struct cgroup_root *root,
|
||||||
void *data)
|
unsigned long magic,
|
||||||
|
struct cgroup_namespace *ns)
|
||||||
{
|
{
|
||||||
bool is_v2 = fs_type == &cgroup2_fs_type;
|
|
||||||
struct super_block *pinned_sb = NULL;
|
|
||||||
struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
|
|
||||||
struct cgroup_subsys *ss;
|
|
||||||
struct cgroup_root *root;
|
|
||||||
struct cgroup_sb_opts opts;
|
|
||||||
struct dentry *dentry;
|
struct dentry *dentry;
|
||||||
int ret;
|
|
||||||
int i;
|
|
||||||
bool new_sb;
|
bool new_sb;
|
||||||
|
|
||||||
get_cgroup_ns(ns);
|
dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb);
|
||||||
|
|
||||||
/* Check if the caller has permission to mount. */
|
|
||||||
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) {
|
|
||||||
put_cgroup_ns(ns);
|
|
||||||
return ERR_PTR(-EPERM);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The first time anyone tries to mount a cgroup, enable the list
|
* In non-init cgroup namespace, instead of root cgroup's dentry,
|
||||||
* linking each css_set to its tasks and fix up all existing tasks.
|
* we return the dentry corresponding to the cgroupns->root_cgrp.
|
||||||
*/
|
*/
|
||||||
if (!use_task_css_set_links)
|
if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
|
||||||
cgroup_enable_task_cg_lists();
|
struct dentry *nsdentry;
|
||||||
|
struct cgroup *cgrp;
|
||||||
|
|
||||||
if (is_v2) {
|
mutex_lock(&cgroup_mutex);
|
||||||
if (data) {
|
spin_lock_irq(&css_set_lock);
|
||||||
pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
|
|
||||||
put_cgroup_ns(ns);
|
cgrp = cset_cgroup_from_root(ns->root_cset, root);
|
||||||
return ERR_PTR(-EINVAL);
|
|
||||||
}
|
spin_unlock_irq(&css_set_lock);
|
||||||
cgrp_dfl_visible = true;
|
mutex_unlock(&cgroup_mutex);
|
||||||
root = &cgrp_dfl_root;
|
|
||||||
cgroup_get(&root->cgrp);
|
nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
|
||||||
goto out_mount;
|
dput(dentry);
|
||||||
|
dentry = nsdentry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IS_ERR(dentry) || !new_sb)
|
||||||
|
cgroup_put(&root->cgrp);
|
||||||
|
|
||||||
|
return dentry;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct dentry *cgroup1_mount(struct file_system_type *fs_type,
|
||||||
|
int flags, void *data,
|
||||||
|
unsigned long magic,
|
||||||
|
struct cgroup_namespace *ns)
|
||||||
|
{
|
||||||
|
struct super_block *pinned_sb = NULL;
|
||||||
|
struct cgroup_sb_opts opts;
|
||||||
|
struct cgroup_root *root;
|
||||||
|
struct cgroup_subsys *ss;
|
||||||
|
struct dentry *dentry;
|
||||||
|
int i, ret;
|
||||||
|
|
||||||
cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
|
cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
|
||||||
|
|
||||||
/* First find the desired set of subsystems */
|
/* First find the desired set of subsystems */
|
||||||
@ -2152,47 +2159,58 @@ out_free:
|
|||||||
kfree(opts.release_agent);
|
kfree(opts.release_agent);
|
||||||
kfree(opts.name);
|
kfree(opts.name);
|
||||||
|
|
||||||
if (ret) {
|
if (ret)
|
||||||
put_cgroup_ns(ns);
|
|
||||||
return ERR_PTR(ret);
|
return ERR_PTR(ret);
|
||||||
}
|
|
||||||
out_mount:
|
|
||||||
dentry = kernfs_mount(fs_type, flags, root->kf_root,
|
|
||||||
is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC,
|
|
||||||
&new_sb);
|
|
||||||
|
|
||||||
/*
|
dentry = cgroup_do_mount(&cgroup_fs_type, flags, root,
|
||||||
* In non-init cgroup namespace, instead of root cgroup's
|
CGROUP_SUPER_MAGIC, ns);
|
||||||
* dentry, we return the dentry corresponding to the
|
|
||||||
* cgroupns->root_cgrp.
|
|
||||||
*/
|
|
||||||
if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
|
|
||||||
struct dentry *nsdentry;
|
|
||||||
struct cgroup *cgrp;
|
|
||||||
|
|
||||||
mutex_lock(&cgroup_mutex);
|
|
||||||
spin_lock_irq(&css_set_lock);
|
|
||||||
|
|
||||||
cgrp = cset_cgroup_from_root(ns->root_cset, root);
|
|
||||||
|
|
||||||
spin_unlock_irq(&css_set_lock);
|
|
||||||
mutex_unlock(&cgroup_mutex);
|
|
||||||
|
|
||||||
nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
|
|
||||||
dput(dentry);
|
|
||||||
dentry = nsdentry;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IS_ERR(dentry) || !new_sb)
|
|
||||||
cgroup_put(&root->cgrp);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If @pinned_sb, we're reusing an existing root and holding an
|
* If @pinned_sb, we're reusing an existing root and holding an
|
||||||
* extra ref on its sb. Mount is complete. Put the extra ref.
|
* extra ref on its sb. Mount is complete. Put the extra ref.
|
||||||
*/
|
*/
|
||||||
if (pinned_sb) {
|
if (pinned_sb)
|
||||||
WARN_ON(new_sb);
|
|
||||||
deactivate_super(pinned_sb);
|
deactivate_super(pinned_sb);
|
||||||
|
|
||||||
|
return dentry;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct dentry *cgroup_mount(struct file_system_type *fs_type,
|
||||||
|
int flags, const char *unused_dev_name,
|
||||||
|
void *data)
|
||||||
|
{
|
||||||
|
struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
|
||||||
|
struct dentry *dentry;
|
||||||
|
|
||||||
|
get_cgroup_ns(ns);
|
||||||
|
|
||||||
|
/* Check if the caller has permission to mount. */
|
||||||
|
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) {
|
||||||
|
put_cgroup_ns(ns);
|
||||||
|
return ERR_PTR(-EPERM);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The first time anyone tries to mount a cgroup, enable the list
|
||||||
|
* linking each css_set to its tasks and fix up all existing tasks.
|
||||||
|
*/
|
||||||
|
if (!use_task_css_set_links)
|
||||||
|
cgroup_enable_task_cg_lists();
|
||||||
|
|
||||||
|
if (fs_type == &cgroup2_fs_type) {
|
||||||
|
if (data) {
|
||||||
|
pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
|
||||||
|
put_cgroup_ns(ns);
|
||||||
|
return ERR_PTR(-EINVAL);
|
||||||
|
}
|
||||||
|
cgrp_dfl_visible = true;
|
||||||
|
cgroup_get(&cgrp_dfl_root.cgrp);
|
||||||
|
|
||||||
|
dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root,
|
||||||
|
CGROUP2_SUPER_MAGIC, ns);
|
||||||
|
} else {
|
||||||
|
dentry = cgroup1_mount(&cgroup_fs_type, flags, data,
|
||||||
|
CGROUP_SUPER_MAGIC, ns);
|
||||||
}
|
}
|
||||||
|
|
||||||
put_cgroup_ns(ns);
|
put_cgroup_ns(ns);
|
||||||
|
Loading…
Reference in New Issue
Block a user