Skip to content

Commit

Permalink
cgroup: store a reference to cgroup_ns into cgroup_fs_context
Browse files Browse the repository at this point in the history
... and trim cgroup_do_mount() arguments (renaming it to cgroup_do_get_tree())

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
  • Loading branch information
Al Viro committed Feb 28, 2019
1 parent 6678889 commit cca8f32
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions kernel/cgroup/cgroup-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern void __init enable_debug_cgroup(void);
*/
struct cgroup_fs_context {
struct cgroup_root *root;
struct cgroup_namespace *ns;
unsigned int flags; /* CGRP_ROOT_* flags */

/* cgroup1 bits */
Expand Down Expand Up @@ -212,8 +213,7 @@ void cgroup_free_root(struct cgroup_root *root);
void init_cgroup_root(struct cgroup_fs_context *ctx);
int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask);
int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask);
int cgroup_do_mount(struct fs_context *fc, unsigned long magic,
struct cgroup_namespace *ns);
int cgroup_do_get_tree(struct fs_context *fc);

int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp);
void cgroup_migrate_finish(struct cgroup_mgctx *mgctx);
Expand Down
8 changes: 3 additions & 5 deletions kernel/cgroup/cgroup-v1.c
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,6 @@ struct kernfs_syscall_ops cgroup1_kf_syscall_ops = {
*/
static int cgroup1_root_to_use(struct fs_context *fc)
{
struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
struct cgroup_root *root;
struct cgroup_subsys *ss;
Expand Down Expand Up @@ -1217,7 +1216,7 @@ static int cgroup1_root_to_use(struct fs_context *fc)
return cg_invalf(fc, "cgroup1: No subsys list or none specified");

/* Hierarchies may only be created in the initial cgroup namespace. */
if (ns != &init_cgroup_ns)
if (ctx->ns != &init_cgroup_ns)
return -EPERM;

root = kzalloc(sizeof(*root), GFP_KERNEL);
Expand All @@ -1235,12 +1234,11 @@ static int cgroup1_root_to_use(struct fs_context *fc)

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))
if (!ns_capable(ctx->ns->user_ns, CAP_SYS_ADMIN))
return -EPERM;

cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
Expand All @@ -1252,7 +1250,7 @@ int cgroup1_get_tree(struct fs_context *fc)
mutex_unlock(&cgroup_mutex);

if (!ret)
ret = cgroup_do_mount(fc, CGROUP_SUPER_MAGIC, ns);
ret = cgroup_do_get_tree(fc);

if (!ret && percpu_ref_is_dying(&ctx->root->cgrp.self.refcnt)) {
struct super_block *sb = fc->root->d_sb;
Expand Down
17 changes: 12 additions & 5 deletions kernel/cgroup/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -2036,13 +2036,17 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
return ret;
}

int cgroup_do_mount(struct fs_context *fc, unsigned long magic,
struct cgroup_namespace *ns)
int cgroup_do_get_tree(struct fs_context *fc)
{
struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
bool new_sb = false;
unsigned long magic;
int ret = 0;

if (fc->fs_type == &cgroup2_fs_type)
magic = CGROUP2_SUPER_MAGIC;
else
magic = CGROUP_SUPER_MAGIC;
fc->root = kernfs_mount(fc->fs_type, fc->sb_flags, ctx->root->kf_root,
magic, &new_sb);
if (IS_ERR(fc->root))
Expand All @@ -2052,15 +2056,15 @@ int cgroup_do_mount(struct fs_context *fc, unsigned long magic,
* In non-init cgroup namespace, instead of root cgroup's dentry,
* we return the dentry corresponding to the cgroupns->root_cgrp.
*/
if (!ret && ns != &init_cgroup_ns) {
if (!ret && ctx->ns != &init_cgroup_ns) {
struct dentry *nsdentry;
struct super_block *sb = fc->root->d_sb;
struct cgroup *cgrp;

mutex_lock(&cgroup_mutex);
spin_lock_irq(&css_set_lock);

cgrp = cset_cgroup_from_root(ns->root_cset, ctx->root);
cgrp = cset_cgroup_from_root(ctx->ns->root_cset, ctx->root);

spin_unlock_irq(&css_set_lock);
mutex_unlock(&cgroup_mutex);
Expand Down Expand Up @@ -2089,6 +2093,7 @@ static void cgroup_fs_context_free(struct fs_context *fc)

kfree(ctx->name);
kfree(ctx->release_agent);
put_cgroup_ns(ctx->ns);
kfree(ctx);
}

Expand All @@ -2106,7 +2111,7 @@ static int cgroup_get_tree(struct fs_context *fc)
cgroup_get_live(&cgrp_dfl_root.cgrp);
ctx->root = &cgrp_dfl_root;

ret = cgroup_do_mount(fc, CGROUP2_SUPER_MAGIC, ns);
ret = cgroup_do_get_tree(fc);
if (!ret)
apply_cgroup_root_flags(ctx->flags);
return ret;
Expand Down Expand Up @@ -2144,6 +2149,8 @@ static int cgroup_init_fs_context(struct fs_context *fc)
if (!use_task_css_set_links)
cgroup_enable_task_cg_lists();

ctx->ns = current->nsproxy->cgroup_ns;
get_cgroup_ns(ctx->ns);
fc->fs_private = ctx;
if (fc->fs_type == &cgroup2_fs_type)
fc->ops = &cgroup_fs_context_ops;
Expand Down

0 comments on commit cca8f32

Please sign in to comment.