aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2013-07-12 16:38:17 -0400
committerTejun Heo <tj@kernel.org>2013-07-16 07:28:24 -0400
commit1d5be6b287c8efc879fbe578e2b7bc8f7a38f313 (patch)
tree68117b91fc21c26754c23721ede5b0b90cf78a5d
parentf172e67cf9d842bc646d0f66792e38435a334b1e (diff)
cgroup: move module ref handling into rebind_subsystems()
Module ref handling in cgroup is rather weird. parse_cgroupfs_options() grabs all the modules for the specified subsystems. A module ref is kept if the specified subsystem is newly bound to the hierarchy. If not, or the operation fails, the refs are dropped. This scatters module ref handling across multiple functions making it difficult to track. It also make the function nasty to use for dynamic subsystem binding which is necessary for the planned unified hierarchy. There's nothing which requires the subsystem modules to be pinned between parse_cgroupfs_options() and rebind_subsystems() in both mount and remount paths. parse_cgroupfs_options() can just parse and rebind_subsystems() can handle pinning the subsystems that it wants to bind, which is a natural part of its task - binding - anyway. Move module ref handling into rebind_subsystems() which makes the code a lot simpler - modules are gotten iff it's gonna be bound and put iff unbound or binding fails. v2: Li pointed out that if a controller module is unloaded between parsing and binding, rebind_subsystems() won't notice the missing controller as it only iterates through existing controllers. Fix it by updating rebind_subsystems() to compare @added_mask to @pinned and fail with -ENOENT if they don't match. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Li Zefan <lizefan@huawei.com>
-rw-r--r--kernel/cgroup.c93
1 files changed, 28 insertions, 65 deletions
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index c108d3d1ea30..2a8cf1a7d2f4 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -1003,6 +1003,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
1003{ 1003{
1004 struct cgroup *cgrp = &root->top_cgroup; 1004 struct cgroup *cgrp = &root->top_cgroup;
1005 struct cgroup_subsys *ss; 1005 struct cgroup_subsys *ss;
1006 unsigned long pinned = 0;
1006 int i, ret; 1007 int i, ret;
1007 1008
1008 BUG_ON(!mutex_is_locked(&cgroup_mutex)); 1009 BUG_ON(!mutex_is_locked(&cgroup_mutex));
@@ -1010,20 +1011,32 @@ static int rebind_subsystems(struct cgroupfs_root *root,
1010 1011
1011 /* Check that any added subsystems are currently free */ 1012 /* Check that any added subsystems are currently free */
1012 for_each_subsys(ss, i) { 1013 for_each_subsys(ss, i) {
1013 unsigned long bit = 1UL << i; 1014 if (!(added_mask & (1 << i)))
1014
1015 if (!(bit & added_mask))
1016 continue; 1015 continue;
1017 1016
1017 /* is the subsystem mounted elsewhere? */
1018 if (ss->root != &cgroup_dummy_root) { 1018 if (ss->root != &cgroup_dummy_root) {
1019 /* Subsystem isn't free */ 1019 ret = -EBUSY;
1020 return -EBUSY; 1020 goto out_put;
1021 }
1022
1023 /* pin the module */
1024 if (!try_module_get(ss->module)) {
1025 ret = -ENOENT;
1026 goto out_put;
1021 } 1027 }
1028 pinned |= 1 << i;
1029 }
1030
1031 /* subsys could be missing if unloaded between parsing and here */
1032 if (added_mask != pinned) {
1033 ret = -ENOENT;
1034 goto out_put;
1022 } 1035 }
1023 1036
1024 ret = cgroup_populate_dir(cgrp, added_mask); 1037 ret = cgroup_populate_dir(cgrp, added_mask);
1025 if (ret) 1038 if (ret)
1026 return ret; 1039 goto out_put;
1027 1040
1028 /* 1041 /*
1029 * Nothing can fail from this point on. Remove files for the 1042 * Nothing can fail from this point on. Remove files for the
@@ -1067,11 +1080,6 @@ static int rebind_subsystems(struct cgroupfs_root *root,
1067 } else if (bit & root->subsys_mask) { 1080 } else if (bit & root->subsys_mask) {
1068 /* Subsystem state should already exist */ 1081 /* Subsystem state should already exist */
1069 BUG_ON(!cgrp->subsys[i]); 1082 BUG_ON(!cgrp->subsys[i]);
1070 /*
1071 * a refcount was taken, but we already had one, so
1072 * drop the extra reference.
1073 */
1074 module_put(ss->module);
1075#ifdef CONFIG_MODULE_UNLOAD 1083#ifdef CONFIG_MODULE_UNLOAD
1076 BUG_ON(ss->module && !module_refcount(ss->module)); 1084 BUG_ON(ss->module && !module_refcount(ss->module));
1077#endif 1085#endif
@@ -1088,6 +1096,12 @@ static int rebind_subsystems(struct cgroupfs_root *root,
1088 root->flags |= CGRP_ROOT_SUBSYS_BOUND; 1096 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1089 1097
1090 return 0; 1098 return 0;
1099
1100out_put:
1101 for_each_subsys(ss, i)
1102 if (pinned & (1 << i))
1103 module_put(ss->module);
1104 return ret;
1091} 1105}
1092 1106
1093static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) 1107static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
@@ -1138,7 +1152,6 @@ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
1138 char *token, *o = data; 1152 char *token, *o = data;
1139 bool all_ss = false, one_ss = false; 1153 bool all_ss = false, one_ss = false;
1140 unsigned long mask = (unsigned long)-1; 1154 unsigned long mask = (unsigned long)-1;
1141 bool module_pin_failed = false;
1142 struct cgroup_subsys *ss; 1155 struct cgroup_subsys *ss;
1143 int i; 1156 int i;
1144 1157
@@ -1281,52 +1294,9 @@ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
1281 if (!opts->subsys_mask && !opts->name) 1294 if (!opts->subsys_mask && !opts->name)
1282 return -EINVAL; 1295 return -EINVAL;
1283 1296
1284 /*
1285 * Grab references on all the modules we'll need, so the subsystems
1286 * don't dance around before rebind_subsystems attaches them. This may
1287 * take duplicate reference counts on a subsystem that's already used,
1288 * but rebind_subsystems handles this case.
1289 */
1290 for_each_subsys(ss, i) {
1291 if (!(opts->subsys_mask & (1UL << i)))
1292 continue;
1293 if (!try_module_get(cgroup_subsys[i]->module)) {
1294 module_pin_failed = true;
1295 break;
1296 }
1297 }
1298 if (module_pin_failed) {
1299 /*
1300 * oops, one of the modules was going away. this means that we
1301 * raced with a module_delete call, and to the user this is
1302 * essentially a "subsystem doesn't exist" case.
1303 */
1304 for (i--; i >= 0; i--) {
1305 /* drop refcounts only on the ones we took */
1306 unsigned long bit = 1UL << i;
1307
1308 if (!(bit & opts->subsys_mask))
1309 continue;
1310 module_put(cgroup_subsys[i]->module);
1311 }
1312 return -ENOENT;
1313 }
1314
1315 return 0; 1297 return 0;
1316} 1298}
1317 1299
1318static void drop_parsed_module_refcounts(unsigned long subsys_mask)
1319{
1320 struct cgroup_subsys *ss;
1321 int i;
1322
1323 mutex_lock(&cgroup_mutex);
1324 for_each_subsys(ss, i)
1325 if (subsys_mask & (1UL << i))
1326 module_put(cgroup_subsys[i]->module);
1327 mutex_unlock(&cgroup_mutex);
1328}
1329
1330static int cgroup_remount(struct super_block *sb, int *flags, char *data) 1300static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1331{ 1301{
1332 int ret = 0; 1302 int ret = 0;
@@ -1384,8 +1354,6 @@ static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1384 mutex_unlock(&cgroup_root_mutex); 1354 mutex_unlock(&cgroup_root_mutex);
1385 mutex_unlock(&cgroup_mutex); 1355 mutex_unlock(&cgroup_mutex);
1386 mutex_unlock(&cgrp->dentry->d_inode->i_mutex); 1356 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
1387 if (ret)
1388 drop_parsed_module_refcounts(opts.subsys_mask);
1389 return ret; 1357 return ret;
1390} 1358}
1391 1359
@@ -1591,7 +1559,7 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1591 new_root = cgroup_root_from_opts(&opts); 1559 new_root = cgroup_root_from_opts(&opts);
1592 if (IS_ERR(new_root)) { 1560 if (IS_ERR(new_root)) {
1593 ret = PTR_ERR(new_root); 1561 ret = PTR_ERR(new_root);
1594 goto drop_modules; 1562 goto out_err;
1595 } 1563 }
1596 opts.new_root = new_root; 1564 opts.new_root = new_root;
1597 1565
@@ -1600,7 +1568,7 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1600 if (IS_ERR(sb)) { 1568 if (IS_ERR(sb)) {
1601 ret = PTR_ERR(sb); 1569 ret = PTR_ERR(sb);
1602 cgroup_free_root(opts.new_root); 1570 cgroup_free_root(opts.new_root);
1603 goto drop_modules; 1571 goto out_err;
1604 } 1572 }
1605 1573
1606 root = sb->s_fs_info; 1574 root = sb->s_fs_info;
@@ -1708,9 +1676,6 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1708 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); 1676 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1709 } 1677 }
1710 } 1678 }
1711
1712 /* no subsys rebinding, so refcounts don't change */
1713 drop_parsed_module_refcounts(opts.subsys_mask);
1714 } 1679 }
1715 1680
1716 kfree(opts.release_agent); 1681 kfree(opts.release_agent);
@@ -1728,8 +1693,6 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1728 mutex_unlock(&inode->i_mutex); 1693 mutex_unlock(&inode->i_mutex);
1729 drop_new_super: 1694 drop_new_super:
1730 deactivate_locked_super(sb); 1695 deactivate_locked_super(sb);
1731 drop_modules:
1732 drop_parsed_module_refcounts(opts.subsys_mask);
1733 out_err: 1696 out_err:
1734 kfree(opts.release_agent); 1697 kfree(opts.release_agent);
1735 kfree(opts.name); 1698 kfree(opts.name);
@@ -4837,7 +4800,7 @@ void cgroup_unload_subsys(struct cgroup_subsys *ss)
4837 4800
4838 /* 4801 /*
4839 * we shouldn't be called if the subsystem is in use, and the use of 4802 * we shouldn't be called if the subsystem is in use, and the use of
4840 * try_module_get in parse_cgroupfs_options should ensure that it 4803 * try_module_get() in rebind_subsystems() should ensure that it
4841 * doesn't start being used while we're killing it off. 4804 * doesn't start being used while we're killing it off.
4842 */ 4805 */
4843 BUG_ON(ss->root != &cgroup_dummy_root); 4806 BUG_ON(ss->root != &cgroup_dummy_root);