31 #include <linux/ctype.h>
32 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/list.h>
43 #include <linux/sched.h>
46 #include <linux/slab.h>
47 #include <linux/magic.h>
49 #include <linux/string.h>
52 #include <linux/module.h>
55 #include <linux/hash.h>
61 #include <linux/poll.h>
68 #define CSS_DEACT_BIAS INT_MIN
95 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
96 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
97 static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
101 #define MAX_CGROUP_ROOT_NAMELEN 64
168 #define CSS_ID_MAX (65535)
229 static int root_count;
232 static int next_hierarchy_id;
236 #define dummytop (&rootnode.top_cgroup)
245 #ifdef CONFIG_PROVE_LOCKING
248 return lockdep_is_held(&cgroup_mutex);
253 return mutex_is_locked(&cgroup_mutex);
259 static int css_unbias_refcnt(
int refcnt)
265 static int css_refcnt(
struct cgroup_subsys_state *css)
269 return css_unbias_refcnt(v);
275 return test_bit(CGRP_REMOVED, &cgrp->flags);
284 static int cgroup_is_releasable(
const struct cgroup *cgrp)
287 (1 << CGRP_RELEASABLE) |
288 (1 << CGRP_NOTIFY_ON_RELEASE);
289 return (cgrp->flags & bits) ==
bits;
292 static int notify_on_release(
const struct cgroup *cgrp)
294 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
297 static int clone_children(
const struct cgroup *cgrp)
299 return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
306 #define for_each_subsys(_root, _ss) \
307 list_for_each_entry(_ss, &_root->subsys_list, sibling)
310 #define for_each_active_root(_root) \
311 list_for_each_entry(_root, &roots, root_list)
313 static inline struct cgroup *__d_cgrp(
struct dentry *
dentry)
323 static inline struct cftype *__d_cft(
struct dentry *
dentry)
325 return __d_cfe(dentry)->type;
333 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
334 static void check_for_release(
struct cgroup *cgrp);
359 static struct css_set init_css_set;
362 static int cgroup_init_idr(
struct cgroup_subsys *
ss,
363 struct cgroup_subsys_state *css);
369 static int css_set_count;
376 #define CSS_SET_HASH_BITS 7
377 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
380 static struct hlist_head *css_set_hash(
struct cgroup_subsys_state *css[])
384 unsigned long tmp = 0
UL;
386 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
387 tmp += (
unsigned long)css[
i];
388 tmp = (tmp >> 16) ^ tmp;
392 return &css_set_table[
index];
401 static void __put_css_set(
struct css_set *cg,
int taskexit)
410 if (atomic_add_unless(&cg->refcount, -1, 1))
419 hlist_del(&cg->hlist);
424 struct cgroup *cgrp = link->
cgrp;
428 notify_on_release(cgrp)) {
430 set_bit(CGRP_RELEASABLE, &cgrp->flags);
431 check_for_release(cgrp);
444 static inline void get_css_set(
struct css_set *cg)
449 static inline void put_css_set(
struct css_set *cg)
451 __put_css_set(cg, 0);
454 static inline void put_css_set_taskexit(
struct css_set *cg)
456 __put_css_set(cg, 1);
469 static bool compare_css_sets(
struct css_set *cg,
470 struct css_set *old_cg,
471 struct cgroup *new_cgrp,
472 struct cgroup_subsys_state *
template[])
476 if (
memcmp(
template, cg->subsys,
sizeof(cg->subsys))) {
491 l2 = &old_cg->cg_links;
494 struct cgroup *cg1, *cg2;
499 if (l1 == &cg->cg_links) {
500 BUG_ON(l2 != &old_cg->cg_links);
503 BUG_ON(l2 == &old_cg->cg_links);
511 BUG_ON(cg1->root != cg2->root);
520 if (cg1->root == new_cgrp->root) {
544 static struct css_set *find_existing_css_set(
545 struct css_set *oldcg,
547 struct cgroup_subsys_state *
template[])
560 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
565 template[
i] = cgrp->subsys[
i];
569 template[
i] = oldcg->subsys[
i];
573 hhead = css_set_hash(
template);
575 if (!compare_css_sets(cg, oldcg, cgrp,
template))
586 static void free_cg_links(
struct list_head *tmp)
607 for (i = 0; i <
count; i++) {
624 static void link_css_set(
struct list_head *tmp_cg_links,
625 struct css_set *cg,
struct cgroup *cgrp)
629 BUG_ON(list_empty(tmp_cg_links));
650 static struct css_set *find_css_set(
651 struct css_set *oldcg,
struct cgroup *cgrp)
654 struct cgroup_subsys_state *
template[CGROUP_SUBSYS_COUNT];
664 res = find_existing_css_set(oldcg, cgrp,
template);
677 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
683 INIT_LIST_HEAD(&res->cg_links);
684 INIT_LIST_HEAD(&res->tasks);
685 INIT_HLIST_NODE(&res->hlist);
689 memcpy(res->subsys,
template,
sizeof(res->subsys));
694 struct cgroup *
c = link->
cgrp;
695 if (c->root == cgrp->root)
697 link_css_set(&tmp_cg_links, res, c);
700 BUG_ON(!list_empty(&tmp_cg_links));
705 hhead = css_set_hash(res->subsys);
706 hlist_add_head(&res->hlist, hhead);
721 struct cgroup *res =
NULL;
723 BUG_ON(!mutex_is_locked(&cgroup_mutex));
731 if (css == &init_css_set) {
736 struct cgroup *c = link->
cgrp;
737 if (c->root == root) {
827 static struct dentry *cgroup_lookup(
struct inode *,
struct dentry *,
unsigned int);
829 static int cgroup_populate_dir(
struct cgroup *cgrp,
bool base_files,
830 unsigned long subsys_mask);
839 static int alloc_css_id(
struct cgroup_subsys *
ss,
840 struct cgroup *parent,
struct cgroup *
child);
852 inode->
i_mapping->backing_dev_info = &cgroup_backing_dev_info;
861 static int cgroup_call_pre_destroy(
struct cgroup *cgrp)
863 struct cgroup_subsys *
ss;
867 if (!ss->pre_destroy)
870 ret = ss->pre_destroy(cgrp);
881 static void cgroup_diput(
struct dentry *
dentry,
struct inode *inode)
885 struct cgroup *cgrp = dentry->
d_fsdata;
886 struct cgroup_subsys *
ss;
903 cgrp->root->number_of_cgroups--;
916 BUG_ON(!list_empty(&cgrp->pidlists));
918 simple_xattrs_free(&cgrp->xattrs);
922 struct cfent *cfe = __d_cfe(dentry);
923 struct cgroup *cgrp = dentry->
d_parent->d_fsdata;
924 struct cftype *cft = cfe->
type;
927 cgrp != &cgrp->root->top_cgroup,
928 "cfe still linked for %s\n", cfe->
type->name);
930 simple_xattrs_free(&cft->xattrs);
935 static int cgroup_delete(
const struct dentry *
d)
940 static void remove_dir(
struct dentry *
d)
949 static int cgroup_rm_file(
struct cgroup *cgrp,
const struct cftype *cft)
957 struct dentry *
d = cfe->
dentry;
959 if (cft && cfe->
type != cft)
965 list_del_init(&cfe->
node);
979 static void cgroup_clear_directory(
struct dentry *dir,
bool base_files,
980 unsigned long subsys_mask)
982 struct cgroup *cgrp = __d_cgrp(dir);
983 struct cgroup_subsys *
ss;
986 struct cftype_set *
set;
987 if (!
test_bit(ss->subsys_id, &subsys_mask))
990 cgroup_rm_file(cgrp,
set->cfts);
993 while (!list_empty(&cgrp->files))
994 cgroup_rm_file(cgrp,
NULL);
1001 static void cgroup_d_remove_dir(
struct dentry *dentry)
1006 cgroup_clear_directory(dentry,
true, root->
subsys_mask);
1009 spin_lock(&parent->
d_lock);
1012 spin_unlock(&dentry->
d_lock);
1013 spin_unlock(&parent->
d_lock);
1027 static void cgroup_wakeup_rmdir_waiter(
struct cgroup *cgrp)
1040 cgroup_wakeup_rmdir_waiter(css->cgroup);
1050 unsigned long final_subsys_mask)
1052 unsigned long added_mask, removed_mask;
1056 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1057 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
1062 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1063 unsigned long bit = 1
UL <<
i;
1064 struct cgroup_subsys *ss = subsys[
i];
1065 if (!(bit & added_mask))
1073 if (ss->root != &rootnode) {
1087 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1088 struct cgroup_subsys *ss = subsys[
i];
1089 unsigned long bit = 1
UL <<
i;
1090 if (bit & added_mask) {
1097 cgrp->subsys[
i]->cgroup = cgrp;
1103 }
else if (bit & removed_mask) {
1107 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
1111 cgrp->subsys[
i] =
NULL;
1112 subsys[
i]->root = &rootnode;
1113 list_move(&ss->sibling, &rootnode.subsys_list);
1115 module_put(ss->module);
1116 }
else if (bit & final_subsys_mask) {
1119 BUG_ON(!cgrp->subsys[i]);
1124 module_put(ss->module);
1125 #ifdef CONFIG_MODULE_UNLOAD
1126 BUG_ON(ss->module && !module_refcount(ss->module));
1139 static int cgroup_show_options(
struct seq_file *seq,
struct dentry *dentry)
1142 struct cgroup_subsys *
ss;
1151 if (
strlen(root->release_agent_path))
1152 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
1153 if (clone_children(&root->top_cgroup))
1183 bool all_ss =
false, one_ss =
false;
1184 unsigned long mask = (
unsigned long)-1;
1186 bool module_pin_failed =
false;
1188 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1190 #ifdef CONFIG_CPUSETS
1191 mask = ~(1
UL << cpuset_subsys_id);
1194 memset(opts, 0,
sizeof(*opts));
1199 if (!
strcmp(token,
"none")) {
1204 if (!
strcmp(token,
"all")) {
1211 if (!
strcmp(token,
"noprefix")) {
1215 if (!
strcmp(token,
"clone_children")) {
1219 if (!
strcmp(token,
"xattr")) {
1223 if (!
strncmp(token,
"release_agent=", 14)) {
1233 if (!
strncmp(token,
"name=", 5)) {
1234 const char *
name = token + 5;
1239 for (i = 0; i <
strlen(name); i++) {
1243 if ((c ==
'.') || (c ==
'-') || (c ==
'_'))
1259 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1260 struct cgroup_subsys *ss = subsys[
i];
1263 if (
strcmp(token, ss->name))
1276 if (i == CGROUP_SUBSYS_COUNT)
1285 if (all_ss || (!one_ss && !opts->
none && !opts->
name)) {
1286 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1287 struct cgroup_subsys *ss = subsys[
i];
1325 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1326 unsigned long bit = 1
UL <<
i;
1330 if (!try_module_get(subsys[i]->
module)) {
1331 module_pin_failed =
true;
1335 if (module_pin_failed) {
1341 for (i--; i >= 0; i--) {
1343 unsigned long bit = 1
UL <<
i;
1347 module_put(subsys[i]->
module);
1355 static void drop_parsed_module_refcounts(
unsigned long subsys_mask)
1358 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1359 unsigned long bit = 1
UL <<
i;
1361 if (!(bit & subsys_mask))
1363 module_put(subsys[i]->
module);
1373 unsigned long added_mask, removed_mask;
1380 ret = parse_cgroupfs_options(data, &opts);
1386 pr_warning(
"cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1407 cgroup_clear_directory(cgrp->dentry,
false, removed_mask);
1409 cgroup_populate_dir(cgrp,
false, added_mask);
1425 .show_options = cgroup_show_options,
1426 .remount_fs = cgroup_remount,
1429 static void init_cgroup_housekeeping(
struct cgroup *cgrp)
1431 INIT_LIST_HEAD(&cgrp->sibling);
1432 INIT_LIST_HEAD(&cgrp->children);
1433 INIT_LIST_HEAD(&cgrp->files);
1434 INIT_LIST_HEAD(&cgrp->css_sets);
1435 INIT_LIST_HEAD(&cgrp->release_list);
1436 INIT_LIST_HEAD(&cgrp->pidlists);
1438 INIT_LIST_HEAD(&cgrp->event_list);
1440 simple_xattrs_init(&cgrp->xattrs);
1454 init_cgroup_housekeeping(cgrp);
1464 spin_lock(&hierarchy_id_lock);
1473 }
else if (ret != -
EAGAIN) {
1477 spin_unlock(&hierarchy_id_lock);
1482 static int cgroup_test_super(
struct super_block *sb,
void *data)
1513 if (!init_root_id(root)) {
1517 init_cgroup_root(root);
1536 spin_lock(&hierarchy_id_lock);
1538 spin_unlock(&hierarchy_id_lock);
1542 static int cgroup_set_super(
struct super_block *sb,
void *data)
1563 sb->
s_op = &cgroup_ops;
1568 static int cgroup_get_rootdir(
struct super_block *sb)
1572 .d_delete = cgroup_delete,
1575 struct inode *inode =
1582 inode->
i_op = &cgroup_dir_inode_operations;
1589 sb->
s_d_op = &cgroup_dops;
1594 int flags,
const char *unused_dev_name,
1602 struct inode *
inode;
1606 ret = parse_cgroupfs_options(data, &opts);
1615 new_root = cgroup_root_from_opts(&opts);
1616 if (IS_ERR(new_root)) {
1617 ret = PTR_ERR(new_root);
1623 sb =
sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
1635 struct cgroup *root_cgrp = &root->
top_cgroup;
1642 ret = cgroup_get_rootdir(sb);
1644 goto drop_new_super;
1645 inode = sb->
s_root->d_inode;
1665 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1669 ret = rebind_subsystems(root, root->subsys_mask);
1671 free_cg_links(&tmp_cg_links);
1686 sb->
s_root->d_fsdata = root_cgrp;
1698 link_css_set(&tmp_cg_links, cg, root_cgrp);
1702 free_cg_links(&tmp_cg_links);
1704 BUG_ON(!list_empty(&root_cgrp->sibling));
1705 BUG_ON(!list_empty(&root_cgrp->children));
1706 BUG_ON(root->number_of_cgroups != 1);
1709 cgroup_populate_dir(root_cgrp,
true, root->subsys_mask);
1739 return ERR_PTR(ret);
1742 static void cgroup_kill_sb(
struct super_block *sb) {
1752 BUG_ON(!list_empty(&cgrp->children));
1753 BUG_ON(!list_empty(&cgrp->sibling));
1759 ret = rebind_subsystems(root, 0);
1785 simple_xattrs_free(&cgrp->xattrs);
1788 cgroup_drop_root(root);
1793 .mount = cgroup_mount,
1794 .kill_sb = cgroup_kill_sb,
1797 static struct kobject *cgroup_kobj;
1830 if ((start -= len) < buf)
1833 cgrp = cgrp->parent;
1845 memmove(buf, start, buf + buflen - start);
1880 return tset->
single.task;
1937 static void cgroup_task_migrate(
struct cgroup *cgrp,
struct cgroup *oldcgrp,
1940 struct css_set *oldcg;
1948 oldcg = tsk->cgroups;
1956 if (!list_empty(&tsk->cg_list))
1957 list_move(&tsk->cg_list, &newcg->
tasks);
1965 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1980 struct cgroup_subsys *
ss, *failed_ss =
NULL;
1981 struct cgroup *oldcgrp;
1984 struct css_set *newcg;
1991 oldcgrp = task_cgroup_from_root(tsk, root);
1992 if (cgrp == oldcgrp)
1996 tset.
single.cgrp = oldcgrp;
1999 if (ss->can_attach) {
2000 retval = ss->can_attach(cgrp, &tset);
2014 newcg = find_css_set(tsk->cgroups, cgrp);
2020 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
2024 ss->attach(cgrp, &tset);
2033 cgroup_wakeup_rmdir_waiter(cgrp);
2037 if (ss == failed_ss)
2045 if (ss->cancel_attach)
2046 ss->cancel_attach(cgrp, &tset);
2064 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2084 static int cgroup_attach_proc(
struct cgroup *cgrp,
struct task_struct *leader)
2087 struct cgroup_subsys *
ss, *failed_ss =
NULL;
2103 group_size = get_nr_threads(leader);
2111 goto out_free_group_list;
2131 ent.
cgrp = task_cgroup_from_root(tsk, root);
2133 if (ent.
cgrp == cgrp)
2152 goto out_free_group_list;
2158 if (ss->can_attach) {
2159 retval = ss->can_attach(cgrp, &tset);
2162 goto out_cancel_attach;
2171 for (i = 0; i < group_size; i++) {
2173 tc->
cg = find_css_set(tc->
task->cgroups, cgrp);
2176 goto out_put_css_set_refs;
2185 for (i = 0; i < group_size; i++) {
2187 cgroup_task_migrate(cgrp, tc->
cgrp, tc->
task, tc->
cg);
2196 ss->attach(cgrp, &tset);
2203 cgroup_wakeup_rmdir_waiter(cgrp);
2205 out_put_css_set_refs:
2207 for (i = 0; i < group_size; i++) {
2211 put_css_set(tc->
cg);
2217 if (ss == failed_ss)
2219 if (ss->cancel_attach)
2220 ss->cancel_attach(cgrp, &tset);
2223 out_free_group_list:
2233 static int attach_task_by_pid(
struct cgroup *cgrp,
u64 pid,
bool threadgroup)
2249 goto out_unlock_cgroup;
2257 !uid_eq(cred->
euid, tcred->uid) &&
2258 !uid_eq(cred->
euid, tcred->suid)) {
2261 goto out_unlock_cgroup;
2277 goto out_unlock_cgroup;
2283 threadgroup_lock(tsk);
2285 if (!thread_group_leader(tsk)) {
2293 threadgroup_unlock(tsk);
2294 put_task_struct(tsk);
2295 goto retry_find_task;
2297 ret = cgroup_attach_proc(cgrp, tsk);
2300 threadgroup_unlock(tsk);
2302 put_task_struct(tsk);
2308 static int cgroup_tasks_write(
struct cgroup *cgrp,
struct cftype *cft,
u64 pid)
2310 return attach_task_by_pid(cgrp, pid,
false);
2313 static int cgroup_procs_write(
struct cgroup *cgrp,
struct cftype *cft,
u64 tgid)
2315 return attach_task_by_pid(cgrp, tgid,
true);
2336 static int cgroup_release_agent_write(
struct cgroup *cgrp,
struct cftype *cft,
2345 strcpy(cgrp->root->release_agent_path, buffer);
2351 static int cgroup_release_agent_show(
struct cgroup *cgrp,
struct cftype *cft,
2356 seq_puts(seq, cgrp->root->release_agent_path);
2363 #define CGROUP_LOCAL_BUFFER_SIZE 64
2365 static ssize_t cgroup_write_X64(
struct cgroup *cgrp,
struct cftype *cft,
2367 const char __user *userbuf,
2368 size_t nbytes, loff_t *unused_ppos)
2376 if (nbytes >=
sizeof(buffer))
2382 if (cft->write_u64) {
2386 retval = cft->write_u64(cgrp, cft, val);
2391 retval = cft->write_s64(cgrp, cft, val);
2398 static ssize_t cgroup_write_string(
struct cgroup *cgrp,
struct cftype *cft,
2400 const char __user *userbuf,
2401 size_t nbytes, loff_t *unused_ppos)
2406 char *buffer = local_buffer;
2409 max_bytes =
sizeof(local_buffer) - 1;
2410 if (nbytes >= max_bytes)
2413 if (nbytes >=
sizeof(local_buffer)) {
2424 retval = cft->write_string(cgrp, cft, strstrip(buffer));
2428 if (buffer != local_buffer)
2433 static ssize_t cgroup_file_write(
struct file *file,
const char __user *
buf,
2434 size_t nbytes, loff_t *ppos)
2436 struct cftype *cft = __d_cft(file->f_dentry);
2437 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2442 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
2443 if (cft->write_u64 || cft->write_s64)
2444 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
2445 if (cft->write_string)
2446 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
2448 int ret = cft->trigger(cgrp, (
unsigned int)cft->private);
2449 return ret ? ret :
nbytes;
2454 static ssize_t cgroup_read_u64(
struct cgroup *cgrp,
struct cftype *cft,
2456 char __user *buf,
size_t nbytes,
2460 u64 val = cft->read_u64(cgrp, cft);
2461 int len =
sprintf(tmp,
"%llu\n", (
unsigned long long) val);
2466 static ssize_t cgroup_read_s64(
struct cgroup *cgrp,
struct cftype *cft,
2468 char __user *buf,
size_t nbytes,
2472 s64 val = cft->read_s64(cgrp, cft);
2473 int len =
sprintf(tmp,
"%lld\n", (
long long) val);
2478 static ssize_t cgroup_file_read(
struct file *file,
char __user *buf,
2479 size_t nbytes, loff_t *ppos)
2481 struct cftype *cft = __d_cft(file->f_dentry);
2482 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2488 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
2490 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
2492 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
2506 static int cgroup_map_add(
struct cgroup_map_cb *
cb,
const char *
key,
u64 value)
2509 return seq_printf(sf,
"%s %llu\n", key, (
unsigned long long)value);
2512 static int cgroup_seqfile_show(
struct seq_file *
m,
void *
arg)
2515 struct cftype *cft = state->
cft;
2516 if (cft->read_map) {
2517 struct cgroup_map_cb
cb = {
2518 .fill = cgroup_map_add,
2521 return cft->read_map(state->
cgroup, cft, &cb);
2523 return cft->read_seq_string(state->
cgroup, cft, m);
2526 static int cgroup_seqfile_release(
struct inode *inode,
struct file *file)
2535 .write = cgroup_file_write,
2537 .release = cgroup_seqfile_release,
2540 static int cgroup_file_open(
struct inode *inode,
struct file *file)
2548 cft = __d_cft(file->f_dentry);
2550 if (cft->read_map || cft->read_seq_string) {
2556 state->
cgroup = __d_cgrp(file->f_dentry->d_parent);
2557 file->
f_op = &cgroup_seqfile_operations;
2558 err =
single_open(file, cgroup_seqfile_show, state);
2561 }
else if (cft->open)
2562 err = cft->open(inode, file);
2569 static int cgroup_file_release(
struct inode *inode,
struct file *file)
2571 struct cftype *cft = __d_cft(file->f_dentry);
2573 return cft->release(inode, file);
2580 static int cgroup_rename(
struct inode *old_dir,
struct dentry *old_dentry,
2581 struct inode *new_dir,
struct dentry *new_dentry)
2587 if (old_dir != new_dir)
2589 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2592 static struct simple_xattrs *__d_xattrs(
struct dentry *dentry)
2595 return &__d_cgrp(dentry)->xattrs;
2597 return &__d_cft(dentry)->xattrs;
2600 static inline int xattr_enabled(
struct dentry *dentry)
2606 static bool is_valid_xattr(
const char *
name)
2614 static int cgroup_setxattr(
struct dentry *dentry,
const char *name,
2615 const void *val,
size_t size,
int flags)
2617 if (!xattr_enabled(dentry))
2619 if (!is_valid_xattr(name))
2624 static int cgroup_removexattr(
struct dentry *dentry,
const char *name)
2626 if (!xattr_enabled(dentry))
2628 if (!is_valid_xattr(name))
2633 static ssize_t cgroup_getxattr(
struct dentry *dentry,
const char *name,
2634 void *buf,
size_t size)
2636 if (!xattr_enabled(dentry))
2638 if (!is_valid_xattr(name))
2643 static ssize_t cgroup_listxattr(
struct dentry *dentry,
char *buf,
size_t size)
2645 if (!xattr_enabled(dentry))
2651 .read = cgroup_file_read,
2652 .write = cgroup_file_write,
2654 .open = cgroup_file_open,
2655 .release = cgroup_file_release,
2659 .setxattr = cgroup_setxattr,
2660 .getxattr = cgroup_getxattr,
2661 .listxattr = cgroup_listxattr,
2662 .removexattr = cgroup_removexattr,
2667 .mkdir = cgroup_mkdir,
2668 .rmdir = cgroup_rmdir,
2669 .rename = cgroup_rename,
2670 .setxattr = cgroup_setxattr,
2671 .getxattr = cgroup_getxattr,
2672 .listxattr = cgroup_listxattr,
2673 .removexattr = cgroup_removexattr,
2676 static struct dentry *cgroup_lookup(
struct inode *dir,
struct dentry *dentry,
unsigned int flags)
2680 d_add(dentry,
NULL);
2687 static inline struct cftype *__file_cft(
struct file *file)
2689 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2691 return __d_cft(file->f_dentry);
2694 static int cgroup_create_file(
struct dentry *dentry,
umode_t mode,
2697 struct inode *
inode;
2704 inode = cgroup_new_inode(mode, sb);
2709 inode->
i_op = &cgroup_dir_inode_operations;
2720 inode->
i_fop = &cgroup_file_operations;
2721 inode->
i_op = &cgroup_file_inode_operations;
2735 static int cgroup_create_dir(
struct cgroup *cgrp,
struct dentry *dentry,
2741 parent = cgrp->parent->dentry;
2742 error = cgroup_create_file(dentry,
S_IFDIR | mode, cgrp->root->sb);
2763 static umode_t cgroup_file_mode(
const struct cftype *cft)
2770 if (cft->read || cft->read_u64 || cft->read_s64 ||
2771 cft->read_map || cft->read_seq_string)
2774 if (cft->write || cft->write_u64 || cft->write_s64 ||
2775 cft->write_string || cft->trigger)
2781 static int cgroup_add_file(
struct cgroup *cgrp,
struct cgroup_subsys *subsys,
2784 struct dentry *dir = cgrp->dentry;
2785 struct cgroup *parent = __d_cgrp(dir);
2786 struct dentry *dentry;
2790 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
2792 simple_xattrs_init(&cft->xattrs);
2795 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2797 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2801 strcpy(name, subsys->name);
2813 if (IS_ERR(dentry)) {
2814 error = PTR_ERR(dentry);
2818 mode = cgroup_file_mode(cft);
2819 error = cgroup_create_file(dentry, mode |
S_IFREG, cgrp->root->sb);
2821 cfe->
type = (
void *)cft;
2833 static int cgroup_addrm_files(
struct cgroup *cgrp,
struct cgroup_subsys *subsys,
2834 struct cftype cfts[],
bool is_add)
2839 for (cft = cfts; cft->name[0] !=
'\0'; cft++) {
2841 err = cgroup_add_file(cgrp, subsys, cft);
2843 err = cgroup_rm_file(cgrp, cft);
2845 pr_warning(
"cgroup_addrm_files: failed to %s %s, err=%d\n",
2846 is_add ?
"add" :
"remove", cft->name, err);
2855 static void cgroup_cfts_prepare(
void)
2869 static void cgroup_cfts_commit(
struct cgroup_subsys *ss,
2870 struct cftype *cfts,
bool is_add)
2874 struct cgroup *cgrp, *
n;
2877 if (cfts && ss->root != &rootnode) {
2891 struct inode *inode = cgrp->dentry->d_inode;
2896 cgroup_addrm_files(cgrp, ss, cfts, is_add);
2900 list_del_init(&cgrp->cft_q_node);
2923 struct cftype_set *
set;
2929 cgroup_cfts_prepare();
2932 cgroup_cfts_commit(ss, cfts,
true);
2953 struct cftype_set *
set;
2955 cgroup_cfts_prepare();
2958 if (
set->cfts == cfts) {
2959 list_del_init(&
set->node);
2960 cgroup_cfts_commit(ss, cfts,
false);
2965 cgroup_cfts_commit(ss,
NULL,
false);
2992 static void cgroup_advance_iter(
struct cgroup *cgrp,
2993 struct cgroup_iter *it)
3002 if (l == &cgrp->css_sets) {
3008 }
while (list_empty(&cg->tasks));
3010 it->task = cg->tasks.next;
3019 static void cgroup_enable_task_cg_lists(
void)
3023 use_task_css_set_links = 1;
3040 list_add(&p->cg_list, &p->cgroups->
tasks);
3055 if (!use_task_css_set_links)
3056 cgroup_enable_task_cg_lists();
3059 it->cg_link = &cgrp->css_sets;
3060 cgroup_advance_iter(cgrp, it);
3064 struct cgroup_iter *it)
3077 if (l == &link->
cg->tasks) {
3080 cgroup_advance_iter(cgrp, it);
3093 static inline int started_after_time(
struct task_struct *
t1,
3097 int start_diff = timespec_compare(&t1->
start_time, time);
3098 if (start_diff > 0) {
3100 }
else if (start_diff < 0) {
3120 static inline int started_after(
void *
p1,
void *p2)
3124 return started_after_time(t1, &t2->
start_time, t2);
3157 struct cgroup_iter it;
3163 struct timespec latest_time = { 0, 0 };
3168 heap->
gt = &started_after;
3198 if (scan->test_task && !scan->test_task(p, scan))
3204 if (!started_after_time(p, &latest_time, latest_task))
3207 if (dropped ==
NULL) {
3213 }
else if (dropped != p) {
3219 put_task_struct(dropped);
3229 for (i = 0; i < heap->
size; i++) {
3236 scan->process_task(q, scan);
3248 if (heap == &tmp_heap)
3300 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3301 static void *pidlist_allocate(
int count)
3308 static void pidlist_free(
void *p)
3310 if (is_vmalloc_addr(p))
3315 static void *pidlist_resize(
void *p,
int newcount)
3319 if (is_vmalloc_addr(p)) {
3338 #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3349 if (length == 0 || length == 1)
3352 for (src = 1; src <
length; src++) {
3354 while (list[src] == list[src-1]) {
3370 newlist = pidlist_resize(list, dest);
3377 static int cmppid(
const void *
a,
const void *
b)
3388 static struct cgroup_pidlist *cgroup_pidlist_find(
struct cgroup *cgrp,
3403 if (l->
key.type == type && l->
key.ns == ns) {
3419 l->
key.ns = get_pid_ns(ns);
3423 list_add(&l->
links, &cgrp->pidlists);
3431 static int pidlist_array_load(
struct cgroup *cgrp,
enum cgroup_filetype type,
3437 struct cgroup_iter it;
3448 array = pidlist_allocate(length);
3458 pid = task_tgid_vnr(tsk);
3460 pid = task_pid_vnr(tsk);
3469 length = pidlist_uniq(&array, length);
3470 l = cgroup_pidlist_find(cgrp, type);
3472 pidlist_free(array);
3476 pidlist_free(l->
list);
3497 struct cgroup *cgrp;
3498 struct cgroup_iter it;
3505 if (dentry->
d_sb->s_op != &cgroup_ops ||
3514 switch (tsk->
state) {
3528 if (delayacct_is_task_waiting_on_io(tsk))
3546 static void *cgroup_pidlist_start(
struct seq_file *
s, loff_t *
pos)
3555 int index = 0, pid = *
pos;
3562 while (index < end) {
3563 int mid = (index +
end) / 2;
3564 if (l->
list[mid] == pid) {
3567 }
else if (l->
list[mid] <= pid)
3582 static void cgroup_pidlist_stop(
struct seq_file *s,
void *
v)
3588 static void *cgroup_pidlist_next(
struct seq_file *s,
void *
v, loff_t *pos)
3606 static int cgroup_pidlist_show(
struct seq_file *s,
void *v)
3615 static const struct seq_operations cgroup_pidlist_seq_operations = {
3616 .start = cgroup_pidlist_start,
3617 .stop = cgroup_pidlist_stop,
3618 .next = cgroup_pidlist_next,
3619 .show = cgroup_pidlist_show,
3637 pidlist_free(l->
list);
3647 static int cgroup_pidlist_release(
struct inode *inode,
struct file *file)
3657 cgroup_release_pid_array(l);
3664 .write = cgroup_file_write,
3665 .release = cgroup_pidlist_release,
3674 static int cgroup_pidlist_open(
struct file *file,
enum cgroup_filetype type)
3676 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
3685 retval = pidlist_array_load(cgrp, type, &l);
3689 file->
f_op = &cgroup_pidlist_operations;
3691 retval =
seq_open(file, &cgroup_pidlist_seq_operations);
3693 cgroup_release_pid_array(l);
3699 static int cgroup_tasks_open(
struct inode *
unused,
struct file *file)
3703 static int cgroup_procs_open(
struct inode *
unused,
struct file *file)
3708 static u64 cgroup_read_notify_on_release(
struct cgroup *cgrp,
3711 return notify_on_release(cgrp);
3714 static int cgroup_write_notify_on_release(
struct cgroup *cgrp,
3718 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3720 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3722 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3735 struct cgroup *cgrp = event->
cgrp;
3737 event->
cft->unregister_event(cgrp, event->
cft, event->
eventfd);
3754 struct cgroup *cgrp = event->
cgrp;
3755 unsigned long flags = (
unsigned long)key;
3758 __remove_wait_queue(event->
wqh, &event->
wait);
3759 spin_lock(&cgrp->event_list_lock);
3761 spin_unlock(&cgrp->event_list_lock);
3772 static void cgroup_event_ptable_queue_proc(
struct file *file,
3788 static int cgroup_write_event_control(
struct cgroup *cgrp,
struct cftype *cft,
3792 unsigned int efd, cfd;
3793 struct file *efile =
NULL;
3794 struct file *cfile =
NULL;
3804 if ((*endp !=
' ') && (*endp !=
'\0'))
3812 INIT_LIST_HEAD(&event->
list);
3813 init_poll_funcptr(&event->
pt, cgroup_event_ptable_queue_proc);
3814 init_waitqueue_func_entry(&event->
wait, cgroup_event_wake);
3818 if (IS_ERR(efile)) {
3819 ret = PTR_ERR(efile);
3825 ret = PTR_ERR(event->
eventfd);
3841 event->
cft = __file_cft(cfile);
3842 if (IS_ERR(event->
cft)) {
3843 ret = PTR_ERR(event->
cft);
3847 if (!event->
cft->register_event || !event->
cft->unregister_event) {
3852 ret = event->
cft->register_event(cgrp, event->
cft,
3858 event->
cft->unregister_event(cgrp, event->
cft, event->
eventfd);
3870 spin_lock(&cgrp->event_list_lock);
3871 list_add(&event->
list, &cgrp->event_list);
3872 spin_unlock(&cgrp->event_list_lock);
3886 if (!IS_ERR_OR_NULL(efile))
3894 static u64 cgroup_clone_children_read(
struct cgroup *cgrp,
3897 return clone_children(cgrp);
3900 static int cgroup_clone_children_write(
struct cgroup *cgrp,
3905 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3907 clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3915 #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
3916 static struct cftype
files[] = {
3919 .open = cgroup_tasks_open,
3920 .write_u64 = cgroup_tasks_write,
3921 .release = cgroup_pidlist_release,
3926 .open = cgroup_procs_open,
3927 .write_u64 = cgroup_procs_write,
3928 .release = cgroup_pidlist_release,
3932 .name =
"notify_on_release",
3933 .read_u64 = cgroup_read_notify_on_release,
3934 .write_u64 = cgroup_write_notify_on_release,
3938 .write_string = cgroup_write_event_control,
3942 .name =
"cgroup.clone_children",
3943 .read_u64 = cgroup_clone_children_read,
3944 .write_u64 = cgroup_clone_children_write,
3947 .name =
"release_agent",
3948 .flags = CFTYPE_ONLY_ON_ROOT,
3949 .read_seq_string = cgroup_release_agent_show,
3950 .write_string = cgroup_release_agent_write,
3962 static int cgroup_populate_dir(
struct cgroup *cgrp,
bool base_files,
3963 unsigned long subsys_mask)
3966 struct cgroup_subsys *
ss;
3969 err = cgroup_addrm_files(cgrp,
NULL, files,
true);
3976 struct cftype_set *
set;
3977 if (!
test_bit(ss->subsys_id, &subsys_mask))
3981 cgroup_addrm_files(cgrp, ss,
set->cfts,
true);
3986 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4001 struct cgroup_subsys_state *css =
4002 container_of(work,
struct cgroup_subsys_state, dput_work);
4003 struct dentry *dentry = css->cgroup->dentry;
4011 static void init_cgroup_css(
struct cgroup_subsys_state *css,
4012 struct cgroup_subsys *ss,
4013 struct cgroup *cgrp)
4020 set_bit(CSS_ROOT, &css->flags);
4021 BUG_ON(cgrp->subsys[ss->subsys_id]);
4022 cgrp->subsys[ss->subsys_id] = css;
4030 INIT_WORK(&css->dput_work, css_dput_fn);
4031 if (ss->__DEPRECATED_clear_css_refs)
4032 set_bit(CSS_CLEAR_CSS_REFS, &css->flags);
4043 static long cgroup_create(
struct cgroup *parent,
struct dentry *dentry,
4046 struct cgroup *cgrp;
4049 struct cgroup_subsys *
ss;
4065 init_cgroup_housekeeping(cgrp);
4067 cgrp->parent = parent;
4068 cgrp->root = parent->root;
4069 cgrp->top_cgroup = parent->top_cgroup;
4071 if (notify_on_release(parent))
4072 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4074 if (clone_children(parent))
4075 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
4078 struct cgroup_subsys_state *css;
4080 css = ss->create(cgrp);
4085 init_cgroup_css(css, ss, cgrp);
4087 err = alloc_css_id(ss, parent, cgrp);
4092 if (clone_children(parent) && ss->post_clone)
4093 ss->post_clone(cgrp);
4095 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4097 pr_warning(
"cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4099 if (!
strcmp(ss->name,
"memory"))
4100 pr_warning(
"cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4101 ss->warned_broken_hierarchy =
true;
4105 list_add(&cgrp->sibling, &cgrp->parent->children);
4108 err = cgroup_create_dir(cgrp, dentry, mode);
4114 if (!ss->__DEPRECATED_clear_css_refs)
4118 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
4122 err = cgroup_populate_dir(cgrp,
true, root->subsys_mask);
4133 root->number_of_cgroups--;
4138 if (cgrp->subsys[ss->subsys_id])
4151 static int cgroup_mkdir(
struct inode *dir,
struct dentry *dentry,
umode_t mode)
4153 struct cgroup *c_parent = dentry->
d_parent->d_fsdata;
4156 return cgroup_create(c_parent, dentry, mode |
S_IFDIR);
4168 static int cgroup_has_css_refs(
struct cgroup *cgrp)
4177 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4178 struct cgroup_subsys *ss = subsys[
i];
4179 struct cgroup_subsys_state *css;
4182 if (ss ==
NULL || ss->root != cgrp->root)
4185 css = cgrp->subsys[ss->subsys_id];
4194 if (css && css_refcnt(css) > 1)
4222 static int cgroup_clear_css_refs(
struct cgroup *cgrp)
4224 struct cgroup_subsys *
ss;
4225 unsigned long flags;
4226 bool failed =
false;
4236 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4241 if (ss->__DEPRECATED_clear_css_refs)
4242 failed |= css_refcnt(css) != 1;
4251 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4254 set_bit(CSS_REMOVED, &css->flags);
4265 static int cgroup_rmdir(
struct inode *unused_dir,
struct dentry *dentry)
4267 struct cgroup *cgrp = dentry->
d_fsdata;
4269 struct cgroup *parent;
4281 if (!list_empty(&cgrp->children)) {
4296 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4302 ret = cgroup_call_pre_destroy(cgrp);
4304 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4309 parent = cgrp->parent;
4310 if (
atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
4311 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4316 if (!cgroup_clear_css_refs(cgrp)) {
4322 if (
test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
4325 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4332 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4335 set_bit(CGRP_REMOVED, &cgrp->flags);
4336 if (!list_empty(&cgrp->release_list))
4337 list_del_init(&cgrp->release_list);
4341 list_del_init(&cgrp->sibling);
4343 list_del_init(&cgrp->allcg_node);
4345 d = dget(cgrp->dentry);
4347 cgroup_d_remove_dir(d);
4350 set_bit(CGRP_RELEASABLE, &parent->flags);
4351 check_for_release(parent);
4358 spin_lock(&cgrp->event_list_lock);
4365 spin_unlock(&cgrp->event_list_lock);
4373 INIT_LIST_HEAD(&ss->cftsets);
4379 if (ss->base_cftypes) {
4380 ss->base_cftset.cfts = ss->base_cftypes;
4385 static void __init cgroup_init_subsys(
struct cgroup_subsys *ss)
4387 struct cgroup_subsys_state *css;
4392 cgroup_init_cftsets(ss);
4395 list_add(&ss->sibling, &rootnode.subsys_list);
4396 ss->root = &rootnode;
4400 init_cgroup_css(css, ss,
dummytop);
4406 init_css_set.subsys[ss->subsys_id] =
dummytop->subsys[ss->subsys_id];
4408 need_forkexit_callback |= ss->fork || ss->exit;
4434 struct cgroup_subsys_state *css;
4437 if (ss->name ==
NULL ||
strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4438 ss->create ==
NULL || ss->destroy ==
NULL)
4447 if (ss->fork || ss->exit)
4454 if (ss->module ==
NULL) {
4456 BUG_ON(subsys[ss->subsys_id] != ss);
4461 cgroup_init_cftsets(ss);
4464 subsys[ss->subsys_id] =
ss;
4473 subsys[ss->subsys_id] =
NULL;
4475 return PTR_ERR(css);
4478 list_add(&ss->sibling, &rootnode.subsys_list);
4479 ss->root = &rootnode;
4482 init_cgroup_css(css, ss,
dummytop);
4485 int ret = cgroup_init_idr(ss, css);
4489 subsys[ss->subsys_id] =
NULL;
4507 struct hlist_head *bucket = &css_set_table[
i], *new_bucket;
4511 if (cg->subsys[ss->subsys_id])
4514 hlist_del(&cg->hlist);
4516 cg->subsys[ss->subsys_id] = css;
4518 new_bucket = css_set_hash(cg->subsys);
4519 hlist_add_head(&cg->hlist, new_bucket);
4552 BUG_ON(ss->root != &rootnode);
4556 subsys[ss->subsys_id] =
NULL;
4559 list_del_init(&ss->sibling);
4567 struct css_set *cg = link->
cg;
4569 hlist_del(&cg->hlist);
4570 BUG_ON(!cg->subsys[ss->subsys_id]);
4571 cg->subsys[ss->subsys_id] =
NULL;
4572 hhead = css_set_hash(cg->subsys);
4573 hlist_add_head(&cg->hlist, hhead);
4600 INIT_LIST_HEAD(&init_css_set.cg_links);
4601 INIT_LIST_HEAD(&init_css_set.tasks);
4602 INIT_HLIST_NODE(&init_css_set.hlist);
4604 init_cgroup_root(&rootnode);
4608 init_css_set_link.cg = &init_css_set;
4610 list_add(&init_css_set_link.cgrp_link_list,
4611 &rootnode.top_cgroup.css_sets);
4612 list_add(&init_css_set_link.cg_link_list,
4613 &init_css_set.cg_links);
4618 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4619 struct cgroup_subsys *ss = subsys[
i];
4622 if (!ss || ss->module)
4629 if (ss->subsys_id != i) {
4631 ss->name, ss->subsys_id);
4636 cgroup_init_subsys(ss);
4653 err =
bdi_init(&cgroup_backing_dev_info);
4657 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4658 struct cgroup_subsys *ss = subsys[
i];
4661 if (!ss || ss->module)
4663 if (!ss->early_init)
4664 cgroup_init_subsys(ss);
4666 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
4670 hhead = css_set_hash(init_css_set.subsys);
4671 hlist_add_head(&init_css_set.hlist, hhead);
4672 BUG_ON(!init_root_id(&rootnode));
4686 proc_create(
"cgroups", 0,
NULL, &proc_cgroupstats_operations);
4708 static int proc_cgroup_show(
struct seq_file *m,
void *v)
4732 struct cgroup_subsys *
ss;
4733 struct cgroup *cgrp;
4738 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
4743 cgrp = task_cgroup_from_root(tsk, root);
4753 put_task_struct(tsk);
4760 static
int cgroup_open(
struct inode *inode,
struct file *file)
4762 struct pid *pid = PROC_I(inode)->pid;
4767 .open = cgroup_open,
4774 static int proc_cgroupstats_show(
struct seq_file *m,
void *v)
4778 seq_puts(m,
"#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
4785 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4786 struct cgroup_subsys *ss = subsys[
i];
4790 ss->name, ss->root->hierarchy_id,
4791 ss->root->number_of_cgroups, !ss->disabled);
4797 static int cgroupstats_open(
struct inode *inode,
struct file *file)
4803 .
open = cgroupstats_open,
4828 child->cgroups =
current->cgroups;
4829 get_css_set(child->cgroups);
4831 INIT_LIST_HEAD(&child->cg_list);
4844 if (need_forkexit_callback) {
4846 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4847 struct cgroup_subsys *ss = subsys[
i];
4853 if (!ss || ss->module)
4884 if (use_task_css_set_links) {
4887 if (list_empty(&child->cg_list))
4888 list_add(&child->cg_list, &child->cgroups->
tasks);
4938 if (!list_empty(&tsk->cg_list)) {
4940 if (!list_empty(&tsk->cg_list))
4941 list_del_init(&tsk->cg_list);
4948 tsk->cgroups = &init_css_set;
4950 if (run_callbacks && need_forkexit_callback) {
4951 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4952 struct cgroup_subsys *ss = subsys[
i];
4955 if (!ss || ss->module)
4959 struct cgroup *old_cgrp =
4961 struct cgroup *cgrp = task_cgroup(tsk, i);
4962 ss->exit(cgrp, old_cgrp, tsk);
4969 put_css_set_taskexit(cg);
4993 target = task_cgroup_from_root(task, cgrp->root);
4994 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4995 cgrp = cgrp->parent;
5000 static void check_for_release(
struct cgroup *cgrp)
5004 if (cgroup_is_releasable(cgrp) && !
atomic_read(&cgrp->count)
5005 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
5009 int need_schedule_work = 0;
5012 list_empty(&cgrp->release_list)) {
5013 list_add(&cgrp->release_list, &release_list);
5014 need_schedule_work = 1;
5017 if (need_schedule_work)
5026 int v = css_refcnt(css);
5031 }
while (!
test_bit(CSS_REMOVED, &css->flags));
5040 struct cgroup *cgrp = css->cgroup;
5048 if (notify_on_release(cgrp)) {
5049 set_bit(CGRP_RELEASABLE, &cgrp->flags);
5050 check_for_release(cgrp);
5052 cgroup_wakeup_rmdir_waiter(cgrp);
5055 if (!
test_bit(CSS_CLEAR_CSS_REFS, &css->flags))
5086 static void cgroup_release_agent(
struct work_struct *work)
5088 BUG_ON(work != &release_agent_work);
5091 while (!list_empty(&release_list)) {
5092 char *argv[3], *envp[3];
5094 char *pathbuf =
NULL, *agentbuf =
NULL;
5095 struct cgroup *cgrp =
list_entry(release_list.next,
5098 list_del_init(&cgrp->release_list);
5110 argv[i++] = agentbuf;
5111 argv[i++] = pathbuf;
5116 envp[i++] =
"HOME=/";
5117 envp[i++] =
"PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5135 static int __init cgroup_disable(
char *
str)
5140 while ((token =
strsep(&str,
",")) !=
NULL) {
5143 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
5144 struct cgroup_subsys *ss = subsys[
i];
5151 if (!ss || ss->module)
5154 if (!
strcmp(token, ss->name)) {
5157 " subsystem\n", ss->name);
5164 __setup(
"cgroup_disable=", cgroup_disable);
5173 unsigned short css_id(
struct cgroup_subsys_state *css)
5197 return cssid->
depth;
5216 const struct cgroup_subsys_state *root)
5234 void free_css_id(
struct cgroup_subsys *ss,
struct cgroup_subsys_state *css)
5245 spin_lock(&ss->id_lock);
5247 spin_unlock(&ss->id_lock);
5257 static struct css_id *get_new_cssid(
struct cgroup_subsys *ss,
int depth)
5264 size =
sizeof(*newid) +
sizeof(
unsigned short) * (depth + 1);
5273 spin_lock(&ss->id_lock);
5276 spin_unlock(&ss->id_lock);
5291 spin_lock(&ss->id_lock);
5293 spin_unlock(&ss->id_lock);
5296 return ERR_PTR(error);
5301 struct cgroup_subsys_state *rootcss)
5308 newid = get_new_cssid(ss, 0);
5310 return PTR_ERR(newid);
5313 newid->
css = rootcss;
5314 rootcss->id = newid;
5318 static int alloc_css_id(
struct cgroup_subsys *ss,
struct cgroup *parent,
5319 struct cgroup *
child)
5321 int subsys_id,
i, depth = 0;
5322 struct cgroup_subsys_state *parent_css, *child_css;
5323 struct css_id *child_id, *parent_id;
5325 subsys_id = ss->subsys_id;
5326 parent_css = parent->subsys[subsys_id];
5327 child_css = child->subsys[subsys_id];
5328 parent_id = parent_css->
id;
5329 depth = parent_id->
depth + 1;
5331 child_id = get_new_cssid(ss, depth);
5332 if (IS_ERR(child_id))
5333 return PTR_ERR(child_id);
5335 for (i = 0; i <
depth; i++)
5337 child_id->
stack[depth] = child_id->
id;
5355 struct cgroup_subsys_state *
css_lookup(
struct cgroup_subsys *ss,
int id)
5379 struct cgroup_subsys_state *
5381 struct cgroup_subsys_state *root,
int *foundid)
5383 struct cgroup_subsys_state *ret =
NULL;
5386 int rootid =
css_id(root);
5405 if (tmp->
depth >= depth && tmp->
stack[depth] == rootid) {
5423 struct cgroup *cgrp;
5424 struct inode *
inode;
5425 struct cgroup_subsys_state *css;
5427 inode = f->f_dentry->d_inode;
5429 if (inode->
i_op != &cgroup_dir_inode_operations)
5430 return ERR_PTR(-
EBADF);
5436 cgrp = __d_cgrp(f->f_dentry);
5437 css = cgrp->subsys[
id];
5438 return css ? css : ERR_PTR(-
ENOENT);
5441 #ifdef CONFIG_CGROUP_DEBUG
5442 static struct cgroup_subsys_state *debug_create(
struct cgroup *
cont)
5444 struct cgroup_subsys_state *css = kzalloc(
sizeof(*css),
GFP_KERNEL);
5452 static void debug_destroy(
struct cgroup *cont)
5454 kfree(cont->subsys[debug_subsys_id]);
5457 static u64 cgroup_refcount_read(
struct cgroup *cont,
struct cftype *cft)
5462 static u64 debug_taskcount_read(
struct cgroup *cont,
struct cftype *cft)
5467 static u64 current_css_set_read(
struct cgroup *cont,
struct cftype *cft)
5472 static u64 current_css_set_refcount_read(
struct cgroup *cont,
5483 static int current_css_set_cg_links_read(
struct cgroup *cont,
5494 struct cgroup *c = link->
cgrp;
5498 name = c->dentry->d_name.name;
5502 c->root->hierarchy_id, name);
5509 #define MAX_TASKS_SHOWN_PER_CSS 25
5510 static int cgroup_css_links_read(
struct cgroup *cont,
5518 struct css_set *cg = link->
cg;
5523 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5528 task_pid_vnr(task));
5536 static u64 releasable_read(
struct cgroup *cgrp,
struct cftype *cft)
5538 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5541 static struct cftype debug_files[] = {
5543 .name =
"cgroup_refcount",
5544 .read_u64 = cgroup_refcount_read,
5547 .name =
"taskcount",
5548 .read_u64 = debug_taskcount_read,
5552 .name =
"current_css_set",
5553 .read_u64 = current_css_set_read,
5557 .name =
"current_css_set_refcount",
5558 .read_u64 = current_css_set_refcount_read,
5562 .name =
"current_css_set_cg_links",
5563 .read_seq_string = current_css_set_cg_links_read,
5567 .name =
"cgroup_css_links",
5568 .read_seq_string = cgroup_css_links_read,
5572 .name =
"releasable",
5573 .read_u64 = releasable_read,
5579 struct cgroup_subsys debug_subsys = {
5581 .create = debug_create,
5582 .destroy = debug_destroy,
5583 .subsys_id = debug_subsys_id,
5584 .base_cftypes = debug_files,