aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/cgroup.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/cgroup.c')
-rw-r--r--kernel/cgroup.c694
1 files changed, 642 insertions, 52 deletions
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 4fd90e129772..e2769e13980c 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -4,6 +4,10 @@
4 * Based originally on the cpuset system, extracted by Paul Menage 4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc 5 * Copyright (C) 2006 Google, Inc
6 * 6 *
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
7 * Copyright notices from the original cpuset code: 11 * Copyright notices from the original cpuset code:
8 * -------------------------------------------------- 12 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA. 13 * Copyright (C) 2003 BULL SA.
@@ -23,7 +27,6 @@
23 */ 27 */
24 28
25#include <linux/cgroup.h> 29#include <linux/cgroup.h>
26#include <linux/module.h>
27#include <linux/ctype.h> 30#include <linux/ctype.h>
28#include <linux/errno.h> 31#include <linux/errno.h>
29#include <linux/fs.h> 32#include <linux/fs.h>
@@ -44,6 +47,7 @@
44#include <linux/string.h> 47#include <linux/string.h>
45#include <linux/sort.h> 48#include <linux/sort.h>
46#include <linux/kmod.h> 49#include <linux/kmod.h>
50#include <linux/module.h>
47#include <linux/delayacct.h> 51#include <linux/delayacct.h>
48#include <linux/cgroupstats.h> 52#include <linux/cgroupstats.h>
49#include <linux/hash.h> 53#include <linux/hash.h>
@@ -52,15 +56,21 @@
52#include <linux/pid_namespace.h> 56#include <linux/pid_namespace.h>
53#include <linux/idr.h> 57#include <linux/idr.h>
54#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ 58#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
59#include <linux/eventfd.h>
60#include <linux/poll.h>
55 61
56#include <asm/atomic.h> 62#include <asm/atomic.h>
57 63
58static DEFINE_MUTEX(cgroup_mutex); 64static DEFINE_MUTEX(cgroup_mutex);
59 65
60/* Generate an array of cgroup subsystem pointers */ 66/*
67 * Generate an array of cgroup subsystem pointers. At boot time, this is
68 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
69 * registered after that. The mutable section of this array is protected by
70 * cgroup_mutex.
71 */
61#define SUBSYS(_x) &_x ## _subsys, 72#define SUBSYS(_x) &_x ## _subsys,
62 73static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
63static struct cgroup_subsys *subsys[] = {
64#include <linux/cgroup_subsys.h> 74#include <linux/cgroup_subsys.h>
65}; 75};
66 76
@@ -147,6 +157,35 @@ struct css_id {
147 unsigned short stack[0]; /* Array of Length (depth+1) */ 157 unsigned short stack[0]; /* Array of Length (depth+1) */
148}; 158};
149 159
160/*
161 * cgroup_event represents events which userspace want to recieve.
162 */
163struct cgroup_event {
164 /*
165 * Cgroup which the event belongs to.
166 */
167 struct cgroup *cgrp;
168 /*
169 * Control file which the event associated.
170 */
171 struct cftype *cft;
172 /*
173 * eventfd to signal userspace about the event.
174 */
175 struct eventfd_ctx *eventfd;
176 /*
177 * Each of these stored in a list by the cgroup.
178 */
179 struct list_head list;
180 /*
181 * All fields below needed to unregister event when
182 * userspace closes eventfd.
183 */
184 poll_table pt;
185 wait_queue_head_t *wqh;
186 wait_queue_t wait;
187 struct work_struct remove;
188};
150 189
151/* The list of hierarchy roots */ 190/* The list of hierarchy roots */
152 191
@@ -250,7 +289,8 @@ struct cg_cgroup_link {
250static struct css_set init_css_set; 289static struct css_set init_css_set;
251static struct cg_cgroup_link init_css_set_link; 290static struct cg_cgroup_link init_css_set_link;
252 291
253static int cgroup_subsys_init_idr(struct cgroup_subsys *ss); 292static int cgroup_init_idr(struct cgroup_subsys *ss,
293 struct cgroup_subsys_state *css);
254 294
255/* css_set_lock protects the list of css_set objects, and the 295/* css_set_lock protects the list of css_set objects, and the
256 * chain of tasks off each css_set. Nests outside task->alloc_lock 296 * chain of tasks off each css_set. Nests outside task->alloc_lock
@@ -448,8 +488,11 @@ static struct css_set *find_existing_css_set(
448 struct hlist_node *node; 488 struct hlist_node *node;
449 struct css_set *cg; 489 struct css_set *cg;
450 490
451 /* Built the set of subsystem state objects that we want to 491 /*
452 * see in the new css_set */ 492 * Build the set of subsystem state objects that we want to see in the
493 * new css_set. while subsystems can change globally, the entries here
494 * won't change, so no need for locking.
495 */
453 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 496 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
454 if (root->subsys_bits & (1UL << i)) { 497 if (root->subsys_bits & (1UL << i)) {
455 /* Subsystem is in this hierarchy. So we want 498 /* Subsystem is in this hierarchy. So we want
@@ -696,6 +739,7 @@ void cgroup_lock(void)
696{ 739{
697 mutex_lock(&cgroup_mutex); 740 mutex_lock(&cgroup_mutex);
698} 741}
742EXPORT_SYMBOL_GPL(cgroup_lock);
699 743
700/** 744/**
701 * cgroup_unlock - release lock on cgroup changes 745 * cgroup_unlock - release lock on cgroup changes
@@ -706,6 +750,7 @@ void cgroup_unlock(void)
706{ 750{
707 mutex_unlock(&cgroup_mutex); 751 mutex_unlock(&cgroup_mutex);
708} 752}
753EXPORT_SYMBOL_GPL(cgroup_unlock);
709 754
710/* 755/*
711 * A couple of forward declarations required, due to cyclic reference loop: 756 * A couple of forward declarations required, due to cyclic reference loop:
@@ -757,6 +802,7 @@ static int cgroup_call_pre_destroy(struct cgroup *cgrp)
757 if (ret) 802 if (ret)
758 break; 803 break;
759 } 804 }
805
760 return ret; 806 return ret;
761} 807}
762 808
@@ -884,7 +930,11 @@ void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
884 css_put(css); 930 css_put(css);
885} 931}
886 932
887 933/*
934 * Call with cgroup_mutex held. Drops reference counts on modules, including
935 * any duplicate ones that parse_cgroupfs_options took. If this function
936 * returns an error, no reference counts are touched.
937 */
888static int rebind_subsystems(struct cgroupfs_root *root, 938static int rebind_subsystems(struct cgroupfs_root *root,
889 unsigned long final_bits) 939 unsigned long final_bits)
890{ 940{
@@ -892,6 +942,8 @@ static int rebind_subsystems(struct cgroupfs_root *root,
892 struct cgroup *cgrp = &root->top_cgroup; 942 struct cgroup *cgrp = &root->top_cgroup;
893 int i; 943 int i;
894 944
945 BUG_ON(!mutex_is_locked(&cgroup_mutex));
946
895 removed_bits = root->actual_subsys_bits & ~final_bits; 947 removed_bits = root->actual_subsys_bits & ~final_bits;
896 added_bits = final_bits & ~root->actual_subsys_bits; 948 added_bits = final_bits & ~root->actual_subsys_bits;
897 /* Check that any added subsystems are currently free */ 949 /* Check that any added subsystems are currently free */
@@ -900,6 +952,12 @@ static int rebind_subsystems(struct cgroupfs_root *root,
900 struct cgroup_subsys *ss = subsys[i]; 952 struct cgroup_subsys *ss = subsys[i];
901 if (!(bit & added_bits)) 953 if (!(bit & added_bits))
902 continue; 954 continue;
955 /*
956 * Nobody should tell us to do a subsys that doesn't exist:
957 * parse_cgroupfs_options should catch that case and refcounts
958 * ensure that subsystems won't disappear once selected.
959 */
960 BUG_ON(ss == NULL);
903 if (ss->root != &rootnode) { 961 if (ss->root != &rootnode) {
904 /* Subsystem isn't free */ 962 /* Subsystem isn't free */
905 return -EBUSY; 963 return -EBUSY;
@@ -919,6 +977,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
919 unsigned long bit = 1UL << i; 977 unsigned long bit = 1UL << i;
920 if (bit & added_bits) { 978 if (bit & added_bits) {
921 /* We're binding this subsystem to this hierarchy */ 979 /* We're binding this subsystem to this hierarchy */
980 BUG_ON(ss == NULL);
922 BUG_ON(cgrp->subsys[i]); 981 BUG_ON(cgrp->subsys[i]);
923 BUG_ON(!dummytop->subsys[i]); 982 BUG_ON(!dummytop->subsys[i]);
924 BUG_ON(dummytop->subsys[i]->cgroup != dummytop); 983 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
@@ -930,8 +989,10 @@ static int rebind_subsystems(struct cgroupfs_root *root,
930 if (ss->bind) 989 if (ss->bind)
931 ss->bind(ss, cgrp); 990 ss->bind(ss, cgrp);
932 mutex_unlock(&ss->hierarchy_mutex); 991 mutex_unlock(&ss->hierarchy_mutex);
992 /* refcount was already taken, and we're keeping it */
933 } else if (bit & removed_bits) { 993 } else if (bit & removed_bits) {
934 /* We're removing this subsystem */ 994 /* We're removing this subsystem */
995 BUG_ON(ss == NULL);
935 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); 996 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
936 BUG_ON(cgrp->subsys[i]->cgroup != cgrp); 997 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
937 mutex_lock(&ss->hierarchy_mutex); 998 mutex_lock(&ss->hierarchy_mutex);
@@ -942,9 +1003,20 @@ static int rebind_subsystems(struct cgroupfs_root *root,
942 subsys[i]->root = &rootnode; 1003 subsys[i]->root = &rootnode;
943 list_move(&ss->sibling, &rootnode.subsys_list); 1004 list_move(&ss->sibling, &rootnode.subsys_list);
944 mutex_unlock(&ss->hierarchy_mutex); 1005 mutex_unlock(&ss->hierarchy_mutex);
1006 /* subsystem is now free - drop reference on module */
1007 module_put(ss->module);
945 } else if (bit & final_bits) { 1008 } else if (bit & final_bits) {
946 /* Subsystem state should already exist */ 1009 /* Subsystem state should already exist */
1010 BUG_ON(ss == NULL);
947 BUG_ON(!cgrp->subsys[i]); 1011 BUG_ON(!cgrp->subsys[i]);
1012 /*
1013 * a refcount was taken, but we already had one, so
1014 * drop the extra reference.
1015 */
1016 module_put(ss->module);
1017#ifdef CONFIG_MODULE_UNLOAD
1018 BUG_ON(ss->module && !module_refcount(ss->module));
1019#endif
948 } else { 1020 } else {
949 /* Subsystem state shouldn't exist */ 1021 /* Subsystem state shouldn't exist */
950 BUG_ON(cgrp->subsys[i]); 1022 BUG_ON(cgrp->subsys[i]);
@@ -986,13 +1058,20 @@ struct cgroup_sb_opts {
986 1058
987}; 1059};
988 1060
989/* Convert a hierarchy specifier into a bitmask of subsystems and 1061/*
990 * flags. */ 1062 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
991static int parse_cgroupfs_options(char *data, 1063 * with cgroup_mutex held to protect the subsys[] array. This function takes
992 struct cgroup_sb_opts *opts) 1064 * refcounts on subsystems to be used, unless it returns error, in which case
1065 * no refcounts are taken.
1066 */
1067static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
993{ 1068{
994 char *token, *o = data ?: "all"; 1069 char *token, *o = data ?: "all";
995 unsigned long mask = (unsigned long)-1; 1070 unsigned long mask = (unsigned long)-1;
1071 int i;
1072 bool module_pin_failed = false;
1073
1074 BUG_ON(!mutex_is_locked(&cgroup_mutex));
996 1075
997#ifdef CONFIG_CPUSETS 1076#ifdef CONFIG_CPUSETS
998 mask = ~(1UL << cpuset_subsys_id); 1077 mask = ~(1UL << cpuset_subsys_id);
@@ -1005,10 +1084,11 @@ static int parse_cgroupfs_options(char *data,
1005 return -EINVAL; 1084 return -EINVAL;
1006 if (!strcmp(token, "all")) { 1085 if (!strcmp(token, "all")) {
1007 /* Add all non-disabled subsystems */ 1086 /* Add all non-disabled subsystems */
1008 int i;
1009 opts->subsys_bits = 0; 1087 opts->subsys_bits = 0;
1010 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 1088 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1011 struct cgroup_subsys *ss = subsys[i]; 1089 struct cgroup_subsys *ss = subsys[i];
1090 if (ss == NULL)
1091 continue;
1012 if (!ss->disabled) 1092 if (!ss->disabled)
1013 opts->subsys_bits |= 1ul << i; 1093 opts->subsys_bits |= 1ul << i;
1014 } 1094 }
@@ -1026,7 +1106,6 @@ static int parse_cgroupfs_options(char *data,
1026 if (!opts->release_agent) 1106 if (!opts->release_agent)
1027 return -ENOMEM; 1107 return -ENOMEM;
1028 } else if (!strncmp(token, "name=", 5)) { 1108 } else if (!strncmp(token, "name=", 5)) {
1029 int i;
1030 const char *name = token + 5; 1109 const char *name = token + 5;
1031 /* Can't specify an empty name */ 1110 /* Can't specify an empty name */
1032 if (!strlen(name)) 1111 if (!strlen(name))
@@ -1050,9 +1129,10 @@ static int parse_cgroupfs_options(char *data,
1050 return -ENOMEM; 1129 return -ENOMEM;
1051 } else { 1130 } else {
1052 struct cgroup_subsys *ss; 1131 struct cgroup_subsys *ss;
1053 int i;
1054 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 1132 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1055 ss = subsys[i]; 1133 ss = subsys[i];
1134 if (ss == NULL)
1135 continue;
1056 if (!strcmp(token, ss->name)) { 1136 if (!strcmp(token, ss->name)) {
1057 if (!ss->disabled) 1137 if (!ss->disabled)
1058 set_bit(i, &opts->subsys_bits); 1138 set_bit(i, &opts->subsys_bits);
@@ -1087,9 +1167,54 @@ static int parse_cgroupfs_options(char *data,
1087 if (!opts->subsys_bits && !opts->name) 1167 if (!opts->subsys_bits && !opts->name)
1088 return -EINVAL; 1168 return -EINVAL;
1089 1169
1170 /*
1171 * Grab references on all the modules we'll need, so the subsystems
1172 * don't dance around before rebind_subsystems attaches them. This may
1173 * take duplicate reference counts on a subsystem that's already used,
1174 * but rebind_subsystems handles this case.
1175 */
1176 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1177 unsigned long bit = 1UL << i;
1178
1179 if (!(bit & opts->subsys_bits))
1180 continue;
1181 if (!try_module_get(subsys[i]->module)) {
1182 module_pin_failed = true;
1183 break;
1184 }
1185 }
1186 if (module_pin_failed) {
1187 /*
1188 * oops, one of the modules was going away. this means that we
1189 * raced with a module_delete call, and to the user this is
1190 * essentially a "subsystem doesn't exist" case.
1191 */
1192 for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
1193 /* drop refcounts only on the ones we took */
1194 unsigned long bit = 1UL << i;
1195
1196 if (!(bit & opts->subsys_bits))
1197 continue;
1198 module_put(subsys[i]->module);
1199 }
1200 return -ENOENT;
1201 }
1202
1090 return 0; 1203 return 0;
1091} 1204}
1092 1205
1206static void drop_parsed_module_refcounts(unsigned long subsys_bits)
1207{
1208 int i;
1209 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1210 unsigned long bit = 1UL << i;
1211
1212 if (!(bit & subsys_bits))
1213 continue;
1214 module_put(subsys[i]->module);
1215 }
1216}
1217
1093static int cgroup_remount(struct super_block *sb, int *flags, char *data) 1218static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1094{ 1219{
1095 int ret = 0; 1220 int ret = 0;
@@ -1106,21 +1231,19 @@ static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1106 if (ret) 1231 if (ret)
1107 goto out_unlock; 1232 goto out_unlock;
1108 1233
1109 /* Don't allow flags to change at remount */ 1234 /* Don't allow flags or name to change at remount */
1110 if (opts.flags != root->flags) { 1235 if (opts.flags != root->flags ||
1111 ret = -EINVAL; 1236 (opts.name && strcmp(opts.name, root->name))) {
1112 goto out_unlock;
1113 }
1114
1115 /* Don't allow name to change at remount */
1116 if (opts.name && strcmp(opts.name, root->name)) {
1117 ret = -EINVAL; 1237 ret = -EINVAL;
1238 drop_parsed_module_refcounts(opts.subsys_bits);
1118 goto out_unlock; 1239 goto out_unlock;
1119 } 1240 }
1120 1241
1121 ret = rebind_subsystems(root, opts.subsys_bits); 1242 ret = rebind_subsystems(root, opts.subsys_bits);
1122 if (ret) 1243 if (ret) {
1244 drop_parsed_module_refcounts(opts.subsys_bits);
1123 goto out_unlock; 1245 goto out_unlock;
1246 }
1124 1247
1125 /* (re)populate subsystem files */ 1248 /* (re)populate subsystem files */
1126 cgroup_populate_dir(cgrp); 1249 cgroup_populate_dir(cgrp);
@@ -1151,6 +1274,8 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
1151 INIT_LIST_HEAD(&cgrp->release_list); 1274 INIT_LIST_HEAD(&cgrp->release_list);
1152 INIT_LIST_HEAD(&cgrp->pidlists); 1275 INIT_LIST_HEAD(&cgrp->pidlists);
1153 mutex_init(&cgrp->pidlist_mutex); 1276 mutex_init(&cgrp->pidlist_mutex);
1277 INIT_LIST_HEAD(&cgrp->event_list);
1278 spin_lock_init(&cgrp->event_list_lock);
1154} 1279}
1155 1280
1156static void init_cgroup_root(struct cgroupfs_root *root) 1281static void init_cgroup_root(struct cgroupfs_root *root)
@@ -1306,7 +1431,9 @@ static int cgroup_get_sb(struct file_system_type *fs_type,
1306 struct cgroupfs_root *new_root; 1431 struct cgroupfs_root *new_root;
1307 1432
1308 /* First find the desired set of subsystems */ 1433 /* First find the desired set of subsystems */
1434 mutex_lock(&cgroup_mutex);
1309 ret = parse_cgroupfs_options(data, &opts); 1435 ret = parse_cgroupfs_options(data, &opts);
1436 mutex_unlock(&cgroup_mutex);
1310 if (ret) 1437 if (ret)
1311 goto out_err; 1438 goto out_err;
1312 1439
@@ -1317,7 +1444,7 @@ static int cgroup_get_sb(struct file_system_type *fs_type,
1317 new_root = cgroup_root_from_opts(&opts); 1444 new_root = cgroup_root_from_opts(&opts);
1318 if (IS_ERR(new_root)) { 1445 if (IS_ERR(new_root)) {
1319 ret = PTR_ERR(new_root); 1446 ret = PTR_ERR(new_root);
1320 goto out_err; 1447 goto drop_modules;
1321 } 1448 }
1322 opts.new_root = new_root; 1449 opts.new_root = new_root;
1323 1450
@@ -1326,7 +1453,7 @@ static int cgroup_get_sb(struct file_system_type *fs_type,
1326 if (IS_ERR(sb)) { 1453 if (IS_ERR(sb)) {
1327 ret = PTR_ERR(sb); 1454 ret = PTR_ERR(sb);
1328 cgroup_drop_root(opts.new_root); 1455 cgroup_drop_root(opts.new_root);
1329 goto out_err; 1456 goto drop_modules;
1330 } 1457 }
1331 1458
1332 root = sb->s_fs_info; 1459 root = sb->s_fs_info;
@@ -1382,6 +1509,11 @@ static int cgroup_get_sb(struct file_system_type *fs_type,
1382 free_cg_links(&tmp_cg_links); 1509 free_cg_links(&tmp_cg_links);
1383 goto drop_new_super; 1510 goto drop_new_super;
1384 } 1511 }
1512 /*
1513 * There must be no failure case after here, since rebinding
1514 * takes care of subsystems' refcounts, which are explicitly
1515 * dropped in the failure exit path.
1516 */
1385 1517
1386 /* EBUSY should be the only error here */ 1518 /* EBUSY should be the only error here */
1387 BUG_ON(ret); 1519 BUG_ON(ret);
@@ -1420,6 +1552,8 @@ static int cgroup_get_sb(struct file_system_type *fs_type,
1420 * any) is not needed 1552 * any) is not needed
1421 */ 1553 */
1422 cgroup_drop_root(opts.new_root); 1554 cgroup_drop_root(opts.new_root);
1555 /* no subsys rebinding, so refcounts don't change */
1556 drop_parsed_module_refcounts(opts.subsys_bits);
1423 } 1557 }
1424 1558
1425 simple_set_mnt(mnt, sb); 1559 simple_set_mnt(mnt, sb);
@@ -1429,6 +1563,8 @@ static int cgroup_get_sb(struct file_system_type *fs_type,
1429 1563
1430 drop_new_super: 1564 drop_new_super:
1431 deactivate_locked_super(sb); 1565 deactivate_locked_super(sb);
1566 drop_modules:
1567 drop_parsed_module_refcounts(opts.subsys_bits);
1432 out_err: 1568 out_err:
1433 kfree(opts.release_agent); 1569 kfree(opts.release_agent);
1434 kfree(opts.name); 1570 kfree(opts.name);
@@ -1542,6 +1678,7 @@ int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1542 memmove(buf, start, buf + buflen - start); 1678 memmove(buf, start, buf + buflen - start);
1543 return 0; 1679 return 0;
1544} 1680}
1681EXPORT_SYMBOL_GPL(cgroup_path);
1545 1682
1546/** 1683/**
1547 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp' 1684 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
@@ -1554,7 +1691,7 @@ int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1554int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) 1691int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1555{ 1692{
1556 int retval = 0; 1693 int retval = 0;
1557 struct cgroup_subsys *ss; 1694 struct cgroup_subsys *ss, *failed_ss = NULL;
1558 struct cgroup *oldcgrp; 1695 struct cgroup *oldcgrp;
1559 struct css_set *cg; 1696 struct css_set *cg;
1560 struct css_set *newcg; 1697 struct css_set *newcg;
@@ -1568,8 +1705,16 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1568 for_each_subsys(root, ss) { 1705 for_each_subsys(root, ss) {
1569 if (ss->can_attach) { 1706 if (ss->can_attach) {
1570 retval = ss->can_attach(ss, cgrp, tsk, false); 1707 retval = ss->can_attach(ss, cgrp, tsk, false);
1571 if (retval) 1708 if (retval) {
1572 return retval; 1709 /*
1710 * Remember on which subsystem the can_attach()
1711 * failed, so that we only call cancel_attach()
1712 * against the subsystems whose can_attach()
1713 * succeeded. (See below)
1714 */
1715 failed_ss = ss;
1716 goto out;
1717 }
1573 } 1718 }
1574 } 1719 }
1575 1720
@@ -1583,14 +1728,17 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1583 */ 1728 */
1584 newcg = find_css_set(cg, cgrp); 1729 newcg = find_css_set(cg, cgrp);
1585 put_css_set(cg); 1730 put_css_set(cg);
1586 if (!newcg) 1731 if (!newcg) {
1587 return -ENOMEM; 1732 retval = -ENOMEM;
1733 goto out;
1734 }
1588 1735
1589 task_lock(tsk); 1736 task_lock(tsk);
1590 if (tsk->flags & PF_EXITING) { 1737 if (tsk->flags & PF_EXITING) {
1591 task_unlock(tsk); 1738 task_unlock(tsk);
1592 put_css_set(newcg); 1739 put_css_set(newcg);
1593 return -ESRCH; 1740 retval = -ESRCH;
1741 goto out;
1594 } 1742 }
1595 rcu_assign_pointer(tsk->cgroups, newcg); 1743 rcu_assign_pointer(tsk->cgroups, newcg);
1596 task_unlock(tsk); 1744 task_unlock(tsk);
@@ -1616,7 +1764,22 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1616 * is no longer empty. 1764 * is no longer empty.
1617 */ 1765 */
1618 cgroup_wakeup_rmdir_waiter(cgrp); 1766 cgroup_wakeup_rmdir_waiter(cgrp);
1619 return 0; 1767out:
1768 if (retval) {
1769 for_each_subsys(root, ss) {
1770 if (ss == failed_ss)
1771 /*
1772 * This subsystem was the one that failed the
1773 * can_attach() check earlier, so we don't need
1774 * to call cancel_attach() against it or any
1775 * remaining subsystems.
1776 */
1777 break;
1778 if (ss->cancel_attach)
1779 ss->cancel_attach(ss, cgrp, tsk, false);
1780 }
1781 }
1782 return retval;
1620} 1783}
1621 1784
1622/* 1785/*
@@ -1682,6 +1845,7 @@ bool cgroup_lock_live_group(struct cgroup *cgrp)
1682 } 1845 }
1683 return true; 1846 return true;
1684} 1847}
1848EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
1685 1849
1686static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, 1850static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1687 const char *buffer) 1851 const char *buffer)
@@ -1950,6 +2114,16 @@ static const struct inode_operations cgroup_dir_inode_operations = {
1950 .rename = cgroup_rename, 2114 .rename = cgroup_rename,
1951}; 2115};
1952 2116
2117/*
2118 * Check if a file is a control file
2119 */
2120static inline struct cftype *__file_cft(struct file *file)
2121{
2122 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2123 return ERR_PTR(-EINVAL);
2124 return __d_cft(file->f_dentry);
2125}
2126
1953static int cgroup_create_file(struct dentry *dentry, mode_t mode, 2127static int cgroup_create_file(struct dentry *dentry, mode_t mode,
1954 struct super_block *sb) 2128 struct super_block *sb)
1955{ 2129{
@@ -2069,6 +2243,7 @@ int cgroup_add_file(struct cgroup *cgrp,
2069 error = PTR_ERR(dentry); 2243 error = PTR_ERR(dentry);
2070 return error; 2244 return error;
2071} 2245}
2246EXPORT_SYMBOL_GPL(cgroup_add_file);
2072 2247
2073int cgroup_add_files(struct cgroup *cgrp, 2248int cgroup_add_files(struct cgroup *cgrp,
2074 struct cgroup_subsys *subsys, 2249 struct cgroup_subsys *subsys,
@@ -2083,6 +2258,7 @@ int cgroup_add_files(struct cgroup *cgrp,
2083 } 2258 }
2084 return 0; 2259 return 0;
2085} 2260}
2261EXPORT_SYMBOL_GPL(cgroup_add_files);
2086 2262
2087/** 2263/**
2088 * cgroup_task_count - count the number of tasks in a cgroup. 2264 * cgroup_task_count - count the number of tasks in a cgroup.
@@ -2468,7 +2644,8 @@ static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2468{ 2644{
2469 struct cgroup_pidlist *l; 2645 struct cgroup_pidlist *l;
2470 /* don't need task_nsproxy() if we're looking at ourself */ 2646 /* don't need task_nsproxy() if we're looking at ourself */
2471 struct pid_namespace *ns = get_pid_ns(current->nsproxy->pid_ns); 2647 struct pid_namespace *ns = current->nsproxy->pid_ns;
2648
2472 /* 2649 /*
2473 * We can't drop the pidlist_mutex before taking the l->mutex in case 2650 * We can't drop the pidlist_mutex before taking the l->mutex in case
2474 * the last ref-holder is trying to remove l from the list at the same 2651 * the last ref-holder is trying to remove l from the list at the same
@@ -2478,8 +2655,6 @@ static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2478 mutex_lock(&cgrp->pidlist_mutex); 2655 mutex_lock(&cgrp->pidlist_mutex);
2479 list_for_each_entry(l, &cgrp->pidlists, links) { 2656 list_for_each_entry(l, &cgrp->pidlists, links) {
2480 if (l->key.type == type && l->key.ns == ns) { 2657 if (l->key.type == type && l->key.ns == ns) {
2481 /* found a matching list - drop the extra refcount */
2482 put_pid_ns(ns);
2483 /* make sure l doesn't vanish out from under us */ 2658 /* make sure l doesn't vanish out from under us */
2484 down_write(&l->mutex); 2659 down_write(&l->mutex);
2485 mutex_unlock(&cgrp->pidlist_mutex); 2660 mutex_unlock(&cgrp->pidlist_mutex);
@@ -2490,13 +2665,12 @@ static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2490 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); 2665 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
2491 if (!l) { 2666 if (!l) {
2492 mutex_unlock(&cgrp->pidlist_mutex); 2667 mutex_unlock(&cgrp->pidlist_mutex);
2493 put_pid_ns(ns);
2494 return l; 2668 return l;
2495 } 2669 }
2496 init_rwsem(&l->mutex); 2670 init_rwsem(&l->mutex);
2497 down_write(&l->mutex); 2671 down_write(&l->mutex);
2498 l->key.type = type; 2672 l->key.type = type;
2499 l->key.ns = ns; 2673 l->key.ns = get_pid_ns(ns);
2500 l->use_count = 0; /* don't increment here */ 2674 l->use_count = 0; /* don't increment here */
2501 l->list = NULL; 2675 l->list = NULL;
2502 l->owner = cgrp; 2676 l->owner = cgrp;
@@ -2804,6 +2978,174 @@ static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2804} 2978}
2805 2979
2806/* 2980/*
2981 * Unregister event and free resources.
2982 *
2983 * Gets called from workqueue.
2984 */
2985static void cgroup_event_remove(struct work_struct *work)
2986{
2987 struct cgroup_event *event = container_of(work, struct cgroup_event,
2988 remove);
2989 struct cgroup *cgrp = event->cgrp;
2990
2991 /* TODO: check return code */
2992 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
2993
2994 eventfd_ctx_put(event->eventfd);
2995 kfree(event);
2996 dput(cgrp->dentry);
2997}
2998
2999/*
3000 * Gets called on POLLHUP on eventfd when user closes it.
3001 *
3002 * Called with wqh->lock held and interrupts disabled.
3003 */
3004static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3005 int sync, void *key)
3006{
3007 struct cgroup_event *event = container_of(wait,
3008 struct cgroup_event, wait);
3009 struct cgroup *cgrp = event->cgrp;
3010 unsigned long flags = (unsigned long)key;
3011
3012 if (flags & POLLHUP) {
3013 remove_wait_queue_locked(event->wqh, &event->wait);
3014 spin_lock(&cgrp->event_list_lock);
3015 list_del(&event->list);
3016 spin_unlock(&cgrp->event_list_lock);
3017 /*
3018 * We are in atomic context, but cgroup_event_remove() may
3019 * sleep, so we have to call it in workqueue.
3020 */
3021 schedule_work(&event->remove);
3022 }
3023
3024 return 0;
3025}
3026
3027static void cgroup_event_ptable_queue_proc(struct file *file,
3028 wait_queue_head_t *wqh, poll_table *pt)
3029{
3030 struct cgroup_event *event = container_of(pt,
3031 struct cgroup_event, pt);
3032
3033 event->wqh = wqh;
3034 add_wait_queue(wqh, &event->wait);
3035}
3036
3037/*
3038 * Parse input and register new cgroup event handler.
3039 *
3040 * Input must be in format '<event_fd> <control_fd> <args>'.
3041 * Interpretation of args is defined by control file implementation.
3042 */
3043static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3044 const char *buffer)
3045{
3046 struct cgroup_event *event = NULL;
3047 unsigned int efd, cfd;
3048 struct file *efile = NULL;
3049 struct file *cfile = NULL;
3050 char *endp;
3051 int ret;
3052
3053 efd = simple_strtoul(buffer, &endp, 10);
3054 if (*endp != ' ')
3055 return -EINVAL;
3056 buffer = endp + 1;
3057
3058 cfd = simple_strtoul(buffer, &endp, 10);
3059 if ((*endp != ' ') && (*endp != '\0'))
3060 return -EINVAL;
3061 buffer = endp + 1;
3062
3063 event = kzalloc(sizeof(*event), GFP_KERNEL);
3064 if (!event)
3065 return -ENOMEM;
3066 event->cgrp = cgrp;
3067 INIT_LIST_HEAD(&event->list);
3068 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3069 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3070 INIT_WORK(&event->remove, cgroup_event_remove);
3071
3072 efile = eventfd_fget(efd);
3073 if (IS_ERR(efile)) {
3074 ret = PTR_ERR(efile);
3075 goto fail;
3076 }
3077
3078 event->eventfd = eventfd_ctx_fileget(efile);
3079 if (IS_ERR(event->eventfd)) {
3080 ret = PTR_ERR(event->eventfd);
3081 goto fail;
3082 }
3083
3084 cfile = fget(cfd);
3085 if (!cfile) {
3086 ret = -EBADF;
3087 goto fail;
3088 }
3089
3090 /* the process need read permission on control file */
3091 ret = file_permission(cfile, MAY_READ);
3092 if (ret < 0)
3093 goto fail;
3094
3095 event->cft = __file_cft(cfile);
3096 if (IS_ERR(event->cft)) {
3097 ret = PTR_ERR(event->cft);
3098 goto fail;
3099 }
3100
3101 if (!event->cft->register_event || !event->cft->unregister_event) {
3102 ret = -EINVAL;
3103 goto fail;
3104 }
3105
3106 ret = event->cft->register_event(cgrp, event->cft,
3107 event->eventfd, buffer);
3108 if (ret)
3109 goto fail;
3110
3111 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3112 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3113 ret = 0;
3114 goto fail;
3115 }
3116
3117 /*
3118 * Events should be removed after rmdir of cgroup directory, but before
3119 * destroying subsystem state objects. Let's take reference to cgroup
3120 * directory dentry to do that.
3121 */
3122 dget(cgrp->dentry);
3123
3124 spin_lock(&cgrp->event_list_lock);
3125 list_add(&event->list, &cgrp->event_list);
3126 spin_unlock(&cgrp->event_list_lock);
3127
3128 fput(cfile);
3129 fput(efile);
3130
3131 return 0;
3132
3133fail:
3134 if (cfile)
3135 fput(cfile);
3136
3137 if (event && event->eventfd && !IS_ERR(event->eventfd))
3138 eventfd_ctx_put(event->eventfd);
3139
3140 if (!IS_ERR_OR_NULL(efile))
3141 fput(efile);
3142
3143 kfree(event);
3144
3145 return ret;
3146}
3147
3148/*
2807 * for the common functions, 'private' gives the type of file 3149 * for the common functions, 'private' gives the type of file
2808 */ 3150 */
2809/* for hysterical raisins, we can't put this on the older files */ 3151/* for hysterical raisins, we can't put this on the older files */
@@ -2828,6 +3170,11 @@ static struct cftype files[] = {
2828 .read_u64 = cgroup_read_notify_on_release, 3170 .read_u64 = cgroup_read_notify_on_release,
2829 .write_u64 = cgroup_write_notify_on_release, 3171 .write_u64 = cgroup_write_notify_on_release,
2830 }, 3172 },
3173 {
3174 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3175 .write_string = cgroup_write_event_control,
3176 .mode = S_IWUGO,
3177 },
2831}; 3178};
2832 3179
2833static struct cftype cft_release_agent = { 3180static struct cftype cft_release_agent = {
@@ -2892,8 +3239,14 @@ static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2892 /* We need to take each hierarchy_mutex in a consistent order */ 3239 /* We need to take each hierarchy_mutex in a consistent order */
2893 int i; 3240 int i;
2894 3241
3242 /*
3243 * No worry about a race with rebind_subsystems that might mess up the
3244 * locking order, since both parties are under cgroup_mutex.
3245 */
2895 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 3246 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2896 struct cgroup_subsys *ss = subsys[i]; 3247 struct cgroup_subsys *ss = subsys[i];
3248 if (ss == NULL)
3249 continue;
2897 if (ss->root == root) 3250 if (ss->root == root)
2898 mutex_lock(&ss->hierarchy_mutex); 3251 mutex_lock(&ss->hierarchy_mutex);
2899 } 3252 }
@@ -2905,6 +3258,8 @@ static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2905 3258
2906 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 3259 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2907 struct cgroup_subsys *ss = subsys[i]; 3260 struct cgroup_subsys *ss = subsys[i];
3261 if (ss == NULL)
3262 continue;
2908 if (ss->root == root) 3263 if (ss->root == root)
2909 mutex_unlock(&ss->hierarchy_mutex); 3264 mutex_unlock(&ss->hierarchy_mutex);
2910 } 3265 }
@@ -3028,11 +3383,16 @@ static int cgroup_has_css_refs(struct cgroup *cgrp)
3028 * synchronization other than RCU, and the subsystem linked 3383 * synchronization other than RCU, and the subsystem linked
3029 * list isn't RCU-safe */ 3384 * list isn't RCU-safe */
3030 int i; 3385 int i;
3386 /*
3387 * We won't need to lock the subsys array, because the subsystems
3388 * we're concerned about aren't going anywhere since our cgroup root
3389 * has a reference on them.
3390 */
3031 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 3391 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3032 struct cgroup_subsys *ss = subsys[i]; 3392 struct cgroup_subsys *ss = subsys[i];
3033 struct cgroup_subsys_state *css; 3393 struct cgroup_subsys_state *css;
3034 /* Skip subsystems not in this hierarchy */ 3394 /* Skip subsystems not present or not in this hierarchy */
3035 if (ss->root != cgrp->root) 3395 if (ss == NULL || ss->root != cgrp->root)
3036 continue; 3396 continue;
3037 css = cgrp->subsys[ss->subsys_id]; 3397 css = cgrp->subsys[ss->subsys_id];
3038 /* When called from check_for_release() it's possible 3398 /* When called from check_for_release() it's possible
@@ -3106,6 +3466,7 @@ static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3106 struct dentry *d; 3466 struct dentry *d;
3107 struct cgroup *parent; 3467 struct cgroup *parent;
3108 DEFINE_WAIT(wait); 3468 DEFINE_WAIT(wait);
3469 struct cgroup_event *event, *tmp;
3109 int ret; 3470 int ret;
3110 3471
3111 /* the vfs holds both inode->i_mutex already */ 3472 /* the vfs holds both inode->i_mutex already */
@@ -3189,6 +3550,20 @@ again:
3189 set_bit(CGRP_RELEASABLE, &parent->flags); 3550 set_bit(CGRP_RELEASABLE, &parent->flags);
3190 check_for_release(parent); 3551 check_for_release(parent);
3191 3552
3553 /*
3554 * Unregister events and notify userspace.
3555 * Notify userspace about cgroup removing only after rmdir of cgroup
3556 * directory to avoid race between userspace and kernelspace
3557 */
3558 spin_lock(&cgrp->event_list_lock);
3559 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
3560 list_del(&event->list);
3561 remove_wait_queue(event->wqh, &event->wait);
3562 eventfd_signal(event->eventfd, 1);
3563 schedule_work(&event->remove);
3564 }
3565 spin_unlock(&cgrp->event_list_lock);
3566
3192 mutex_unlock(&cgroup_mutex); 3567 mutex_unlock(&cgroup_mutex);
3193 return 0; 3568 return 0;
3194} 3569}
@@ -3223,9 +3598,198 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
3223 mutex_init(&ss->hierarchy_mutex); 3598 mutex_init(&ss->hierarchy_mutex);
3224 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key); 3599 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
3225 ss->active = 1; 3600 ss->active = 1;
3601
3602 /* this function shouldn't be used with modular subsystems, since they
3603 * need to register a subsys_id, among other things */
3604 BUG_ON(ss->module);
3226} 3605}
3227 3606
3228/** 3607/**
3608 * cgroup_load_subsys: load and register a modular subsystem at runtime
3609 * @ss: the subsystem to load
3610 *
3611 * This function should be called in a modular subsystem's initcall. If the
3612 * subsytem is built as a module, it will be assigned a new subsys_id and set
3613 * up for use. If the subsystem is built-in anyway, work is delegated to the
3614 * simpler cgroup_init_subsys.
3615 */
3616int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
3617{
3618 int i;
3619 struct cgroup_subsys_state *css;
3620
3621 /* check name and function validity */
3622 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
3623 ss->create == NULL || ss->destroy == NULL)
3624 return -EINVAL;
3625
3626 /*
3627 * we don't support callbacks in modular subsystems. this check is
3628 * before the ss->module check for consistency; a subsystem that could
3629 * be a module should still have no callbacks even if the user isn't
3630 * compiling it as one.
3631 */
3632 if (ss->fork || ss->exit)
3633 return -EINVAL;
3634
3635 /*
3636 * an optionally modular subsystem is built-in: we want to do nothing,
3637 * since cgroup_init_subsys will have already taken care of it.
3638 */
3639 if (ss->module == NULL) {
3640 /* a few sanity checks */
3641 BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
3642 BUG_ON(subsys[ss->subsys_id] != ss);
3643 return 0;
3644 }
3645
3646 /*
3647 * need to register a subsys id before anything else - for example,
3648 * init_cgroup_css needs it.
3649 */
3650 mutex_lock(&cgroup_mutex);
3651 /* find the first empty slot in the array */
3652 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
3653 if (subsys[i] == NULL)
3654 break;
3655 }
3656 if (i == CGROUP_SUBSYS_COUNT) {
3657 /* maximum number of subsystems already registered! */
3658 mutex_unlock(&cgroup_mutex);
3659 return -EBUSY;
3660 }
3661 /* assign ourselves the subsys_id */
3662 ss->subsys_id = i;
3663 subsys[i] = ss;
3664
3665 /*
3666 * no ss->create seems to need anything important in the ss struct, so
3667 * this can happen first (i.e. before the rootnode attachment).
3668 */
3669 css = ss->create(ss, dummytop);
3670 if (IS_ERR(css)) {
3671 /* failure case - need to deassign the subsys[] slot. */
3672 subsys[i] = NULL;
3673 mutex_unlock(&cgroup_mutex);
3674 return PTR_ERR(css);
3675 }
3676
3677 list_add(&ss->sibling, &rootnode.subsys_list);
3678 ss->root = &rootnode;
3679
3680 /* our new subsystem will be attached to the dummy hierarchy. */
3681 init_cgroup_css(css, ss, dummytop);
3682 /* init_idr must be after init_cgroup_css because it sets css->id. */
3683 if (ss->use_id) {
3684 int ret = cgroup_init_idr(ss, css);
3685 if (ret) {
3686 dummytop->subsys[ss->subsys_id] = NULL;
3687 ss->destroy(ss, dummytop);
3688 subsys[i] = NULL;
3689 mutex_unlock(&cgroup_mutex);
3690 return ret;
3691 }
3692 }
3693
3694 /*
3695 * Now we need to entangle the css into the existing css_sets. unlike
3696 * in cgroup_init_subsys, there are now multiple css_sets, so each one
3697 * will need a new pointer to it; done by iterating the css_set_table.
3698 * furthermore, modifying the existing css_sets will corrupt the hash
3699 * table state, so each changed css_set will need its hash recomputed.
3700 * this is all done under the css_set_lock.
3701 */
3702 write_lock(&css_set_lock);
3703 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
3704 struct css_set *cg;
3705 struct hlist_node *node, *tmp;
3706 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
3707
3708 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
3709 /* skip entries that we already rehashed */
3710 if (cg->subsys[ss->subsys_id])
3711 continue;
3712 /* remove existing entry */
3713 hlist_del(&cg->hlist);
3714 /* set new value */
3715 cg->subsys[ss->subsys_id] = css;
3716 /* recompute hash and restore entry */
3717 new_bucket = css_set_hash(cg->subsys);
3718 hlist_add_head(&cg->hlist, new_bucket);
3719 }
3720 }
3721 write_unlock(&css_set_lock);
3722
3723 mutex_init(&ss->hierarchy_mutex);
3724 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
3725 ss->active = 1;
3726
3727 /* success! */
3728 mutex_unlock(&cgroup_mutex);
3729 return 0;
3730}
3731EXPORT_SYMBOL_GPL(cgroup_load_subsys);
3732
3733/**
3734 * cgroup_unload_subsys: unload a modular subsystem
3735 * @ss: the subsystem to unload
3736 *
3737 * This function should be called in a modular subsystem's exitcall. When this
3738 * function is invoked, the refcount on the subsystem's module will be 0, so
3739 * the subsystem will not be attached to any hierarchy.
3740 */
3741void cgroup_unload_subsys(struct cgroup_subsys *ss)
3742{
3743 struct cg_cgroup_link *link;
3744 struct hlist_head *hhead;
3745
3746 BUG_ON(ss->module == NULL);
3747
3748 /*
3749 * we shouldn't be called if the subsystem is in use, and the use of
3750 * try_module_get in parse_cgroupfs_options should ensure that it
3751 * doesn't start being used while we're killing it off.
3752 */
3753 BUG_ON(ss->root != &rootnode);
3754
3755 mutex_lock(&cgroup_mutex);
3756 /* deassign the subsys_id */
3757 BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
3758 subsys[ss->subsys_id] = NULL;
3759
3760 /* remove subsystem from rootnode's list of subsystems */
3761 list_del(&ss->sibling);
3762
3763 /*
3764 * disentangle the css from all css_sets attached to the dummytop. as
3765 * in loading, we need to pay our respects to the hashtable gods.
3766 */
3767 write_lock(&css_set_lock);
3768 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
3769 struct css_set *cg = link->cg;
3770
3771 hlist_del(&cg->hlist);
3772 BUG_ON(!cg->subsys[ss->subsys_id]);
3773 cg->subsys[ss->subsys_id] = NULL;
3774 hhead = css_set_hash(cg->subsys);
3775 hlist_add_head(&cg->hlist, hhead);
3776 }
3777 write_unlock(&css_set_lock);
3778
3779 /*
3780 * remove subsystem's css from the dummytop and free it - need to free
3781 * before marking as null because ss->destroy needs the cgrp->subsys
3782 * pointer to find their state. note that this also takes care of
3783 * freeing the css_id.
3784 */
3785 ss->destroy(ss, dummytop);
3786 dummytop->subsys[ss->subsys_id] = NULL;
3787
3788 mutex_unlock(&cgroup_mutex);
3789}
3790EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
3791
3792/**
3229 * cgroup_init_early - cgroup initialization at system boot 3793 * cgroup_init_early - cgroup initialization at system boot
3230 * 3794 *
3231 * Initialize cgroups at system boot, and initialize any 3795 * Initialize cgroups at system boot, and initialize any
@@ -3253,7 +3817,8 @@ int __init cgroup_init_early(void)
3253 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) 3817 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3254 INIT_HLIST_HEAD(&css_set_table[i]); 3818 INIT_HLIST_HEAD(&css_set_table[i]);
3255 3819
3256 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 3820 /* at bootup time, we don't worry about modular subsystems */
3821 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
3257 struct cgroup_subsys *ss = subsys[i]; 3822 struct cgroup_subsys *ss = subsys[i];
3258 3823
3259 BUG_ON(!ss->name); 3824 BUG_ON(!ss->name);
@@ -3288,12 +3853,13 @@ int __init cgroup_init(void)
3288 if (err) 3853 if (err)
3289 return err; 3854 return err;
3290 3855
3291 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 3856 /* at bootup time, we don't worry about modular subsystems */
3857 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
3292 struct cgroup_subsys *ss = subsys[i]; 3858 struct cgroup_subsys *ss = subsys[i];
3293 if (!ss->early_init) 3859 if (!ss->early_init)
3294 cgroup_init_subsys(ss); 3860 cgroup_init_subsys(ss);
3295 if (ss->use_id) 3861 if (ss->use_id)
3296 cgroup_subsys_init_idr(ss); 3862 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
3297 } 3863 }
3298 3864
3299 /* Add init_css_set to the hash table */ 3865 /* Add init_css_set to the hash table */
@@ -3397,9 +3963,16 @@ static int proc_cgroupstats_show(struct seq_file *m, void *v)
3397 int i; 3963 int i;
3398 3964
3399 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); 3965 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
3966 /*
3967 * ideally we don't want subsystems moving around while we do this.
3968 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
3969 * subsys/hierarchy state.
3970 */
3400 mutex_lock(&cgroup_mutex); 3971 mutex_lock(&cgroup_mutex);
3401 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 3972 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3402 struct cgroup_subsys *ss = subsys[i]; 3973 struct cgroup_subsys *ss = subsys[i];
3974 if (ss == NULL)
3975 continue;
3403 seq_printf(m, "%s\t%d\t%d\t%d\n", 3976 seq_printf(m, "%s\t%d\t%d\t%d\n",
3404 ss->name, ss->root->hierarchy_id, 3977 ss->name, ss->root->hierarchy_id,
3405 ss->root->number_of_cgroups, !ss->disabled); 3978 ss->root->number_of_cgroups, !ss->disabled);
@@ -3457,7 +4030,12 @@ void cgroup_fork_callbacks(struct task_struct *child)
3457{ 4030{
3458 if (need_forkexit_callback) { 4031 if (need_forkexit_callback) {
3459 int i; 4032 int i;
3460 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 4033 /*
4034 * forkexit callbacks are only supported for builtin
4035 * subsystems, and the builtin section of the subsys array is
4036 * immutable, so we don't need to lock the subsys array here.
4037 */
4038 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
3461 struct cgroup_subsys *ss = subsys[i]; 4039 struct cgroup_subsys *ss = subsys[i];
3462 if (ss->fork) 4040 if (ss->fork)
3463 ss->fork(ss, child); 4041 ss->fork(ss, child);
@@ -3526,7 +4104,11 @@ void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3526 struct css_set *cg; 4104 struct css_set *cg;
3527 4105
3528 if (run_callbacks && need_forkexit_callback) { 4106 if (run_callbacks && need_forkexit_callback) {
3529 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 4107 /*
4108 * modular subsystems can't use callbacks, so no need to lock
4109 * the subsys array
4110 */
4111 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
3530 struct cgroup_subsys *ss = subsys[i]; 4112 struct cgroup_subsys *ss = subsys[i];
3531 if (ss->exit) 4113 if (ss->exit)
3532 ss->exit(ss, tsk); 4114 ss->exit(ss, tsk);
@@ -3720,12 +4302,13 @@ static void check_for_release(struct cgroup *cgrp)
3720 } 4302 }
3721} 4303}
3722 4304
3723void __css_put(struct cgroup_subsys_state *css) 4305/* Caller must verify that the css is not for root cgroup */
4306void __css_put(struct cgroup_subsys_state *css, int count)
3724{ 4307{
3725 struct cgroup *cgrp = css->cgroup; 4308 struct cgroup *cgrp = css->cgroup;
3726 int val; 4309 int val;
3727 rcu_read_lock(); 4310 rcu_read_lock();
3728 val = atomic_dec_return(&css->refcnt); 4311 val = atomic_sub_return(count, &css->refcnt);
3729 if (val == 1) { 4312 if (val == 1) {
3730 if (notify_on_release(cgrp)) { 4313 if (notify_on_release(cgrp)) {
3731 set_bit(CGRP_RELEASABLE, &cgrp->flags); 4314 set_bit(CGRP_RELEASABLE, &cgrp->flags);
@@ -3736,6 +4319,7 @@ void __css_put(struct cgroup_subsys_state *css)
3736 rcu_read_unlock(); 4319 rcu_read_unlock();
3737 WARN_ON_ONCE(val < 1); 4320 WARN_ON_ONCE(val < 1);
3738} 4321}
4322EXPORT_SYMBOL_GPL(__css_put);
3739 4323
3740/* 4324/*
3741 * Notify userspace when a cgroup is released, by running the 4325 * Notify userspace when a cgroup is released, by running the
@@ -3817,8 +4401,11 @@ static int __init cgroup_disable(char *str)
3817 while ((token = strsep(&str, ",")) != NULL) { 4401 while ((token = strsep(&str, ",")) != NULL) {
3818 if (!*token) 4402 if (!*token)
3819 continue; 4403 continue;
3820 4404 /*
3821 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { 4405 * cgroup_disable, being at boot time, can't know about module
4406 * subsystems, so we don't worry about them.
4407 */
4408 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
3822 struct cgroup_subsys *ss = subsys[i]; 4409 struct cgroup_subsys *ss = subsys[i];
3823 4410
3824 if (!strcmp(token, ss->name)) { 4411 if (!strcmp(token, ss->name)) {
@@ -3848,6 +4435,7 @@ unsigned short css_id(struct cgroup_subsys_state *css)
3848 return cssid->id; 4435 return cssid->id;
3849 return 0; 4436 return 0;
3850} 4437}
4438EXPORT_SYMBOL_GPL(css_id);
3851 4439
3852unsigned short css_depth(struct cgroup_subsys_state *css) 4440unsigned short css_depth(struct cgroup_subsys_state *css)
3853{ 4441{
@@ -3857,6 +4445,7 @@ unsigned short css_depth(struct cgroup_subsys_state *css)
3857 return cssid->depth; 4445 return cssid->depth;
3858 return 0; 4446 return 0;
3859} 4447}
4448EXPORT_SYMBOL_GPL(css_depth);
3860 4449
3861bool css_is_ancestor(struct cgroup_subsys_state *child, 4450bool css_is_ancestor(struct cgroup_subsys_state *child,
3862 const struct cgroup_subsys_state *root) 4451 const struct cgroup_subsys_state *root)
@@ -3893,6 +4482,7 @@ void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3893 spin_unlock(&ss->id_lock); 4482 spin_unlock(&ss->id_lock);
3894 call_rcu(&id->rcu_head, __free_css_id_cb); 4483 call_rcu(&id->rcu_head, __free_css_id_cb);
3895} 4484}
4485EXPORT_SYMBOL_GPL(free_css_id);
3896 4486
3897/* 4487/*
3898 * This is called by init or create(). Then, calls to this function are 4488 * This is called by init or create(). Then, calls to this function are
@@ -3942,15 +4532,14 @@ err_out:
3942 4532
3943} 4533}
3944 4534
3945static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss) 4535static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
4536 struct cgroup_subsys_state *rootcss)
3946{ 4537{
3947 struct css_id *newid; 4538 struct css_id *newid;
3948 struct cgroup_subsys_state *rootcss;
3949 4539
3950 spin_lock_init(&ss->id_lock); 4540 spin_lock_init(&ss->id_lock);
3951 idr_init(&ss->idr); 4541 idr_init(&ss->idr);
3952 4542
3953 rootcss = init_css_set.subsys[ss->subsys_id];
3954 newid = get_new_cssid(ss, 0); 4543 newid = get_new_cssid(ss, 0);
3955 if (IS_ERR(newid)) 4544 if (IS_ERR(newid))
3956 return PTR_ERR(newid); 4545 return PTR_ERR(newid);
@@ -4010,6 +4599,7 @@ struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
4010 4599
4011 return rcu_dereference(cssid->css); 4600 return rcu_dereference(cssid->css);
4012} 4601}
4602EXPORT_SYMBOL_GPL(css_lookup);
4013 4603
4014/** 4604/**
4015 * css_get_next - lookup next cgroup under specified hierarchy. 4605 * css_get_next - lookup next cgroup under specified hierarchy.