Commit 6678889f authored by Al Viro's avatar Al Viro

cgroup1_get_tree(): separate "get cgroup_root to use" into a separate helper

Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 71d883c3
...@@ -1135,7 +1135,15 @@ struct kernfs_syscall_ops cgroup1_kf_syscall_ops = { ...@@ -1135,7 +1135,15 @@ struct kernfs_syscall_ops cgroup1_kf_syscall_ops = {
.show_path = cgroup_show_path, .show_path = cgroup_show_path,
}; };
int cgroup1_get_tree(struct fs_context *fc) /*
* The guts of cgroup1 mount - find or create cgroup_root to use.
* Called with cgroup_mutex held; returns 0 on success, -E... on
* error and positive - in case when the candidate is busy dying.
* On success it stashes a reference to cgroup_root into given
* cgroup_fs_context; that reference is *NOT* counting towards the
* cgroup_root refcount.
*/
static int cgroup1_root_to_use(struct fs_context *fc)
{ {
struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
struct cgroup_fs_context *ctx = cgroup_fc2context(fc); struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
...@@ -1143,16 +1151,10 @@ int cgroup1_get_tree(struct fs_context *fc) ...@@ -1143,16 +1151,10 @@ int cgroup1_get_tree(struct fs_context *fc)
struct cgroup_subsys *ss; struct cgroup_subsys *ss;
int i, ret; int i, ret;
/* Check if the caller has permission to mount. */
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
return -EPERM;
cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
/* First find the desired set of subsystems */ /* First find the desired set of subsystems */
ret = check_cgroupfs_options(fc); ret = check_cgroupfs_options(fc);
if (ret) if (ret)
goto out_unlock; return ret;
/* /*
* Destruction of cgroup root is asynchronous, so subsystems may * Destruction of cgroup root is asynchronous, so subsystems may
...@@ -1166,12 +1168,8 @@ int cgroup1_get_tree(struct fs_context *fc) ...@@ -1166,12 +1168,8 @@ int cgroup1_get_tree(struct fs_context *fc)
ss->root == &cgrp_dfl_root) ss->root == &cgrp_dfl_root)
continue; continue;
if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) { if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt))
mutex_unlock(&cgroup_mutex); return 1; /* restart */
msleep(10);
ret = restart_syscall();
goto out_free;
}
cgroup_put(&ss->root->cgrp); cgroup_put(&ss->root->cgrp);
} }
...@@ -1200,16 +1198,14 @@ int cgroup1_get_tree(struct fs_context *fc) ...@@ -1200,16 +1198,14 @@ int cgroup1_get_tree(struct fs_context *fc)
(ctx->subsys_mask != root->subsys_mask)) { (ctx->subsys_mask != root->subsys_mask)) {
if (!name_match) if (!name_match)
continue; continue;
ret = -EBUSY; return -EBUSY;
goto out_unlock;
} }
if (root->flags ^ ctx->flags) if (root->flags ^ ctx->flags)
pr_warn("new mount options do not match the existing superblock, will be ignored\n"); pr_warn("new mount options do not match the existing superblock, will be ignored\n");
ctx->root = root; ctx->root = root;
ret = 0; return 0;
goto out_unlock;
} }
/* /*
...@@ -1217,22 +1213,16 @@ int cgroup1_get_tree(struct fs_context *fc) ...@@ -1217,22 +1213,16 @@ int cgroup1_get_tree(struct fs_context *fc)
* specification is allowed for already existing hierarchies but we * specification is allowed for already existing hierarchies but we
* can't create new one without subsys specification. * can't create new one without subsys specification.
*/ */
if (!ctx->subsys_mask && !ctx->none) { if (!ctx->subsys_mask && !ctx->none)
ret = cg_invalf(fc, "cgroup1: No subsys list or none specified"); return cg_invalf(fc, "cgroup1: No subsys list or none specified");
goto out_unlock;
}
/* Hierarchies may only be created in the initial cgroup namespace. */ /* Hierarchies may only be created in the initial cgroup namespace. */
if (ns != &init_cgroup_ns) { if (ns != &init_cgroup_ns)
ret = -EPERM; return -EPERM;
goto out_unlock;
}
root = kzalloc(sizeof(*root), GFP_KERNEL); root = kzalloc(sizeof(*root), GFP_KERNEL);
if (!root) { if (!root)
ret = -ENOMEM; return -ENOMEM;
goto out_unlock;
}
ctx->root = root; ctx->root = root;
init_cgroup_root(ctx); init_cgroup_root(ctx);
...@@ -1240,23 +1230,38 @@ int cgroup1_get_tree(struct fs_context *fc) ...@@ -1240,23 +1230,38 @@ int cgroup1_get_tree(struct fs_context *fc)
ret = cgroup_setup_root(root, ctx->subsys_mask); ret = cgroup_setup_root(root, ctx->subsys_mask);
if (ret) if (ret)
cgroup_free_root(root); cgroup_free_root(root);
return ret;
}
int cgroup1_get_tree(struct fs_context *fc)
{
struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
int ret;
/* Check if the caller has permission to mount. */
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
return -EPERM;
cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
ret = cgroup1_root_to_use(fc);
if (!ret && !percpu_ref_tryget_live(&ctx->root->cgrp.self.refcnt))
ret = 1; /* restart */
out_unlock:
if (!ret && !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
mutex_unlock(&cgroup_mutex);
msleep(10);
return restart_syscall();
}
mutex_unlock(&cgroup_mutex); mutex_unlock(&cgroup_mutex);
out_free:
if (ret)
return ret;
if (!ret)
ret = cgroup_do_mount(fc, CGROUP_SUPER_MAGIC, ns); ret = cgroup_do_mount(fc, CGROUP_SUPER_MAGIC, ns);
if (!ret && percpu_ref_is_dying(&root->cgrp.self.refcnt)) {
if (!ret && percpu_ref_is_dying(&ctx->root->cgrp.self.refcnt)) {
struct super_block *sb = fc->root->d_sb; struct super_block *sb = fc->root->d_sb;
dput(fc->root); dput(fc->root);
deactivate_locked_super(sb); deactivate_locked_super(sb);
ret = 1;
}
if (unlikely(ret > 0)) {
msleep(10); msleep(10);
return restart_syscall(); return restart_syscall();
} }
......
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