Skip to content

Instantly share code, notes, and snippets.

@kevgs
Created October 4, 2017 08:56
Show Gist options
  • Save kevgs/d06c3bc4659a2b4da4085b6bf45f90ff to your computer and use it in GitHub Desktop.
Save kevgs/d06c3bc4659a2b4da4085b6bf45f90ff to your computer and use it in GitHub Desktop.
void cgroup_setup_root(struct cgroup_root *root, u16 ss_mask, int ref_flags)
{
LIST_HEAD(tmp_links);
struct cgroup *root_cgrp = &root->cgrp;
struct kernfs_syscall_ops *kf_sops;
struct css_set *cset;
int i;
lockdep_assert_held(&cgroup_mutex);
cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
root_cgrp->id = ret;
root_cgrp->ancestor_ids[0] = ret;
percpu_ref_init(&root_cgrp->self.refcnt, css_release, ref_flags,
GFP_KERNEL);
/*
* We're accessing css_set_count without locking css_set_lock here,
* but that's OK - it can only be increased by someone holding
* cgroup_lock, and that's us. Later rebinding may disable
* controllers on the default hierarchy and thus create new csets,
* which can't be more than the existing ones. Allocate 2x.
*/
allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
cgroup_init_root_id(root);
kf_sops = root == &cgrp_dfl_root ?
&cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
root->kf_root = kernfs_create_root(kf_sops,
KERNFS_ROOT_CREATE_DEACTIVATED |
KERNFS_ROOT_SUPPORT_EXPORTOP,
root_cgrp);
root_cgrp->kn = root->kf_root->kn;
css_populate_dir(&root_cgrp->self);
rebind_subsystems(root, ss_mask);
trace_cgroup_setup_root(root);
/*
* There must be no failure case after here, since rebinding takes
* care of subsystems' refcounts, which are explicitly dropped in
* the failure exit path.
*/
list_add(&root->root_list, &cgroup_roots);
cgroup_root_count++;
/*
* Link the root cgroup in this hierarchy into all the css_set
* objects.
*/
spin_lock_irq(&css_set_lock);
hash_for_each(css_set_table, i, cset, hlist) {
link_css_set(&tmp_links, cset, root_cgrp);
if (css_set_populated(cset))
cgroup_update_populated(root_cgrp, true);
}
spin_unlock_irq(&css_set_lock);
BUG_ON(!list_empty(&root_cgrp->self.children));
BUG_ON(atomic_read(&root->nr_cgrps) != 1);
kernfs_activate(root_cgrp->kn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment