mount hierarchy
该过程对应于mount -t cgroup -o cpu,cpuset cpuandset /cgroup
首先它解析mount参数-o ***:parse_cgroupfs_option,创建一个新的cgroupfs_root:cgroup_root_from_opts,通过cgroup_test_super判断新的mount subsys是否与已有的hierarchy一样(它们的subsys一样或者name一样),如果一样则使用原来的cgroupfs_root;否则使用新的cgroupfs_root,接着把相应的subsys初始化到该root的top_cgroup里(rebind_subsystems);
然后分配css_set_count(当前所有的css_set个数)个cg_cgroup_link,再建立top_cgrop到所有css_set(css_set_table[]里的所有node)的关系(link_css_set):通过刚才创建的cg_cgroup_link建立css_set与cgroup(这里就是top_cgroup)的关系,把cg_cgroup_link的cg指向当前的css_set,cgrp指向当前 cgroup(top_cgroup),然后再把该cg_cgroup_link分别移到该cgroup->css_sets list,及css_set->cg_links的末尾(其实就是cg_cgroup_link->cg与cg_cgroup_link->cgrp构成该结构的主键,而cg_cgroup_link->cgrp_link_list是css_set的索引,cg_cgroup_link->cg_link_list是cgroup的索引):
- hlist_for_each_entry(cg, node, hhead, hlist)
- link_css_set(&tmp_cg_links, cg, root_cgrp);
- static void link_css_set(struct list_head *tmp_cg_links,
- struct css_set *cg, struct cgroup *cgrp)
- {
- struct cg_cgroup_link *link;
- BUG_ON(list_empty(tmp_cg_links));
- link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
- cgrp_link_list);
- link->cg = cg;
- link->cgrp = cgrp;
- atomic_inc(&cgrp->count);
- list_move(&link->cgrp_link_list, &cgrp->css_sets);
- /*
- * Always add links to the tail of the list so that the list
- * is sorted by order of hierarchy creation
- */
- list_add_tail(&link->cg_link_list, &cg->cg_links);
- }
最后生成该cgroup相应的文件:cgroup_populate_dir(top_cgroup),首先生成基本文件cgroup_add_files;然后生成每个subsys自己的文件:
- for_each_subsys(cgrp->root, ss) {
- if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
- return err;
- }
- sudo mount -t cgroup -o memory,cpuset mc /cgroup/memory/
- 46144380 8071 (mount) call trace:
- 0xffffffff81054e60 :cpu_cgroup_populate+0x0/0x30 [kernel]
- 0xffffffff810c007a :cgroup_populate_dir+0x7a/0x110 [kernel]
- 0xffffffff810c0cbb : cgroup_get_sb+0x42b/0x520[kernel]
- 0xffffffff8117aa7b :vfs_kern_mount+0x7b/0x1b0 [kernel]
- 0xffffffff8117ac22 :do_kern_mount+0x52/0x130 [kernel]
- 0xffffffff81199302 : do_mount+0x2d2/0x8d0[kernel]
- 0xffffffff81199990 : sys_mount+0x90/0xe0[kernel]
- 0xffffffff8100b0f2 :system_call_fastpath+0x16/0x1b [kernel]