Skip to contentSkip to conversation

cgroups: Allow to bind/unbind subsystems to/from non-trival hierarchy

PATCH Superseded Li Zefan 10 participants 71 messages Oct 22, 2010 – Nov 10, 2010 20 messages not in the archive

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Li Zefan

author
#0 lore raw

cover cgroups: Allow to bind/unbind subsystems to/from non-trival hierarchy

Stephane posted a patchset to add perf_cgroup subsystem, so perf can be used to monitor all threads belonging to a cgroup.

But if you already mounted a cgroup hierarchy but without perf_cgroup and the hierarchy has sub-cgroups, you can't bind perf_cgroup to it, and thus you're not able to use per-cgroup perf feature.

This patchset alleviates the pain, and then a subsytem can be bound/unbound to/from a hierarchy which has sub-cgroups in it.

Some subsystems still can't take advantage of this patchset, memcgroup and cpuset in specific.

For cpuset, if a hierarchy has a sub-cgroup and the cgroup has tasks, we can't decide sub-cgroup's cpuset.mems and cpuset.cpus automatically if we try to bind cpuset to this hierarchy.

For memcgroup, memcgroup uses css_get/put(), and due to some complexity, for now bindable subsystems should not use css_get/put().

Usage:

# mount -t cgroup -o cpuset xxx /mnt
# mkdir /mnt/tmp
# echo $$ > /mnt/tmp/tasks

(add cpuacct to the hierarchy)

# mount -o remount,cpuset,cpuacct xxx /mnt

(remove it from the hierarchy)

# mount -o remount,cpuset xxx /mnt

There's another limitation, cpuacct should not be bound to any mounted hierarchy before the above operation. But that's not a problem, as you can remove it from a hierarchy and bind it to another one.

---
 Documentation/cgroups/cgroups.txt |   26 ++-
 include/linux/cgroup.h            |   25 +++-
 kernel/cgroup.c                   |  305 +++++++++++++++++++++++++++++++------
 kernel/cgroup_freezer.c           |   19 ++-
 kernel/sched.c                    |    1 +
 net/sched/cls_cgroup.c            |    1 +
 security/device_cgroup.c          |    1 +
 7 files changed, 311 insertions(+), 67 deletions(-)
author
#1 lore raw

PATCH 1/7 cgroups: Shrink struct cgroup_subsys

On x86_32, sizeof(struct cgroup_subsys) shrinks from 276 bytes to 264.

---
include/linux/cgroup.h | 10 ++++++----

 1 files changed, 6 insertions(+), 4 deletions(-)
include/linux/cgroup.h +6 -4
index ed4ba11..e23ded6 100644
@@ -481,14 +481,16 @@ struct cgroup_subsys {
 	void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
 
 	int subsys_id;
-	int active;
-	int disabled;
-	int early_init;
+
+	unsigned int active:1;
+	unsigned int disabled:1;
+	unsigned int early_init:1;
 	/*
 	 * True if this subsys uses ID. ID is not available before cgroup_init()
 	 * (not available in early_init time.)
 	 */
-	bool use_id;
+	unsigned int use_id:1;
+
 #define MAX_CGROUP_TYPE_NAMELEN 32
 	const char *name;
 
Signature · 1 line
1.7.0.1
author
#23 lore raw

PATCH 2/7 cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to Li Zefan

Stephane posted a patchset to add perf_cgroup subsystem, so perf can be used to monitor all threads belonging to a cgroup.

But if you already mounted a cgroup hierarchy but without perf_cgroup and the hierarchy has sub-cgroups, you can't bind perf_cgroup to it, and thus you're not able to use per-cgroup perf feature.

This patchset alleviates the pain, and then a subsytem can be bind/unbind to/from a hierarchy which has sub-cgroups in it.

For a cgroup subsystem to become bindable, the can_bind flag of struct cgroup_subsys should be set, and provide ->bind() callback if necessary.

But for some constraints, not all subsystems can take advantage of this patch. For example, we can't decide a cgroup's cpuset.mems and cpuset.cpus automatically, so cpuset is not bindable.

Usage:

# mount -t cgroup -o cpuset xxx /mnt
# mkdir /mnt/tmp
# echo $$ > /mnt/tmp/tasks

(assume cpuacct is bindable, and we add cpuacct to the hierarchy)

# mount -o remount,cpuset,cpuacct xxx /mnt
---
 include/linux/cgroup.h |    5 +
 kernel/cgroup.c        |  225 ++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 187 insertions(+), 43 deletions(-)
include/linux/cgroup.h +5 -0
index e23ded6..49369ff 100644
@@ -490,6 +490,11 @@ struct cgroup_subsys {
 	 * (not available in early_init time.)
 	 */
 	unsigned int use_id:1;
+	/*
+	 * Indicate if this subsystem can be bound/unbound to/from a cgroup
+	 * hierarchy which has child cgroups.
+	 */
+	unsigned int can_bind:1;
 
 #define MAX_CGROUP_TYPE_NAMELEN 32
 	const char *name;
kernel/cgroup.c +182 -43

309 lines not on this page · raw message

Signature · 1 line
1.7.0.1
#40 lore raw

PATCH 1/7 cgroups: Shrink struct cgroup_subsys

In reply to a message not in the archive

On x86_32, sizeof(struct cgroup_subsys) shrinks from 276 bytes to 264.

---
include/linux/cgroup.h | 10 ++++++----

 1 files changed, 6 insertions(+), 4 deletions(-)
include/linux/cgroup.h +6 -4
index ed4ba11..e23ded6 100644
@@ -481,14 +481,16 @@ struct cgroup_subsys {
 	void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
 
 	int subsys_id;
-	int active;
-	int disabled;
-	int early_init;
+
+	unsigned int active:1;
+	unsigned int disabled:1;
+	unsigned int early_init:1;
 	/*
 	 * True if this subsys uses ID. ID is not available before cgroup_init()
 	 * (not available in early_init time.)
 	 */
-	bool use_id;
+	unsigned int use_id:1;
+
 #define MAX_CGROUP_TYPE_NAMELEN 32
 	const char *name;
 
Signature · 1 line
1.7.0.1
author
#47 lore raw

PATCH 3/7 cgroups: Allow to unbind subsystem from a cgroup hierarachy

In reply to Li Zefan

This allows us to unbind a cgroup subsystem from a hierarchy which has sub-cgroups in it.

Due to some complexity, for now subsytems that use css_get/put() can't be unbound from such a hierarchy.

Usage:

# mount -t cgroup -o cpuset,cpuacct xxx /mnt
# mkdir /mnt/tmp
# echo $$ > /mnt/tmp/tasks

(remove it from the hierarchy)

# mount -o remount,cpuset xxx /mnt
---
 kernel/cgroup.c |   76 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 61 insertions(+), 15 deletions(-)
kernel/cgroup.c +61 -15
index 46df5f8..9ce3fdb 100644
@@ -1036,19 +1036,42 @@ static int hierarchy_attach_css(struct cgroup *cgrp, void *data)
 	return ret;
 }
 
-static int hierarchy_update_css_sets(struct cgroup *cgrp, void *data)
+static int hierarchy_remove_css(struct cgroup *cgrp, void *data)
 {
-	unsigned long added_bits = (unsigned long)data;
+	unsigned long removed_bits = (unsigned long)data;
 	int i;
+
+	/*
+	 * There might be some pointers to the cgroup_subsys_state
+	 * that we are going to destroy.
+	 */
+	synchronize_rcu();
+
+	for_each_set_bit(i, &removed_bits, CGROUP_SUBSYS_COUNT) {
+		subsys[i]->destroy(subsys[i], cgrp);
+		cgrp->subsys[i] = NULL;
+	}
+
+	return 0;
+}
+
+static void hierarchy_update_css_sets(struct cgroup *cgrp,
+				      unsigned long bits, bool add)
+{
 	struct cg_cgroup_link *link;
+	int i;
 
 	write_lock(&css_set_lock);
 	list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
 		struct css_set *cg = link->cg;
 		struct hlist_head *hhead;
 
-		for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT)
-			cg->subsys[i] = cgrp->subsys[i];
+		for_each_set_bit(i, &bits, CGROUP_SUBSYS_COUNT) {
+			if (add)
+				cg->subsys[i] = cgrp->subsys[i];
+			else
+				cg->subsys[i] = dummytop->subsys[i];
+		}
 
 		/* rehash */
 		hlist_del(&cg->hlist);
@@ -1056,7 +1079,21 @@ static int hierarchy_update_css_sets(struct cgroup *cgrp, void *data)
 		hlist_add_head(&cg->hlist, hhead);
 	}
 	write_unlock(&css_set_lock);
+}
 
+static int hierarchy_add_to_css_sets(struct cgroup *cgrp, void *data)
+{
+	unsigned long added_bits = (unsigned long)data;
+
+	hierarchy_update_css_sets(cgrp, added_bits, true);
+	return 0;
+}
+
+static int hierarchy_remove_from_css_sets(struct cgroup *cgrp, void *data)
+{
+	unsigned long removed_bits = (unsigned long)data;
+
+	hierarchy_update_css_sets(cgrp, removed_bits, false);
 	return 0;
 }
 
@@ -1076,7 +1113,7 @@ static int hierarchy_populate_dir(struct cgroup *cgrp, void *data)
 static int rebind_subsystems(struct cgroupfs_root *root,
 			      unsigned long final_bits)
 {
-	unsigned long added_bits, removed_bits;
+	unsigned long added_bits, removed_bits, changed_bits;
 	struct cgroup *cgrp = &root->top_cgroup;
 	int i;
 	int err;
@@ -1085,6 +1122,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 
 	removed_bits = root->actual_subsys_bits & ~final_bits;
 	added_bits = final_bits & ~root->actual_subsys_bits;
+	changed_bits = removed_bits | added_bits;
 
 	/* Check that any added subsystems are currently free */
 	for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT) {
@@ -1100,14 +1138,11 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 		}
 	}
 
-	/* removing will be supported later */
-	if (root->number_of_cgroups > 1 && removed_bits)
-		return -EBUSY;
-
 	if (root->number_of_cgroups > 1) {
-		for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT)
+		for_each_set_bit(i, &changed_bits, CGROUP_SUBSYS_COUNT)
 			if (!subsys[i]->can_bind)
 				return -EBUSY;
+	}
 
 	for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT) {
 		BUG_ON(cgrp->subsys[i]);
@@ -1123,7 +1158,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 	if (err)
 		goto failed;
 
-	cgroup_walk_hierarchy(hierarchy_update_css_sets,
+	cgroup_walk_hierarchy(hierarchy_add_to_css_sets,
 			      (void *)added_bits, cgrp);
 
 	/* Process each subsystem */
@@ -1143,11 +1178,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 		} else if (bit & removed_bits) {
 			/* We're removing this subsystem */
 			BUG_ON(ss == NULL);
-			BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
-			BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
 			mutex_lock(&ss->hierarchy_mutex);
-			dummytop->subsys[i]->cgroup = dummytop;
-			cgrp->subsys[i] = NULL;
 			if (ss->bind)
 				ss->bind(ss, dummytop);
 			subsys[i]->root = &rootnode;
@@ -1173,6 +1204,21 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 		}
 	}
 	root->subsys_bits = root->actual_subsys_bits = final_bits;
+
+	for_each_set_bit(i, &removed_bits, CGROUP_SUBSYS_COUNT) {
+		BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
+		BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
+
+		dummytop->subsys[i]->cgroup = dummytop;
+		cgrp->subsys[i] = NULL;
+	}
+
+	cgroup_walk_hierarchy(hierarchy_remove_from_css_sets,
+			      (void *)removed_bits, cgrp);
+
+	cgroup_walk_hierarchy(hierarchy_remove_css,
+			      (void *)removed_bits, cgrp);
+
 	synchronize_rcu();
 
 	return 0;
Signature · 1 line
1.7.0.1
author
#51 lore raw

PATCH 4/7 cgroups: Mark some subsystems bindable

In reply to Li Zefan

For those subsystems (debug, cpuacct, net_cls and devices), setting the can_bind flag is sufficient.

---
 kernel/cgroup.c          |    1 +
 kernel/sched.c           |    1 +
 net/sched/cls_cgroup.c   |    1 +
 security/device_cgroup.c |    1 +
 4 files changed, 4 insertions(+), 0 deletions(-)
kernel/cgroup.c +1 -0
index 9ce3fdb..6364bb5 100644
@@ -5124,5 +5124,6 @@ struct cgroup_subsys debug_subsys = {
 	.destroy = debug_destroy,
 	.populate = debug_populate,
 	.subsys_id = debug_subsys_id,
+	.can_bind = 1,
 };
 #endif /* CONFIG_CGROUP_DEBUG */
kernel/sched.c +1 -0
index 51944e8..cae104f 100644
@@ -9329,6 +9329,7 @@ struct cgroup_subsys cpuacct_subsys = {
 	.destroy = cpuacct_destroy,
 	.populate = cpuacct_populate,
 	.subsys_id = cpuacct_subsys_id,
+	.can_bind = 1,
 };
 #endif	/* CONFIG_CGROUP_CPUACCT */
 
net/sched/cls_cgroup.c +1 -0
index 37dff78..020ddfe 100644
@@ -38,6 +38,7 @@ struct cgroup_subsys net_cls_subsys = {
 #define net_cls_subsys_id net_cls_subsys.subsys_id
 #endif
 	.module		= THIS_MODULE,
+	.can_bind	= 1,
 };
 
 
security/device_cgroup.c +1 -0
index 8d9c48f..b8136fc 100644
@@ -473,6 +473,7 @@ struct cgroup_subsys devices_subsys = {
 	.destroy = devcgroup_destroy,
 	.populate = devcgroup_populate,
 	.subsys_id = devices_subsys_id,
+	.can_bind = 1,
 };
 
 int devcgroup_inode_permission(struct inode *inode, int mask)
Signature · 1 line
1.7.0.1
#41 lore raw

PATCH 3/7 cgroups: Allow to unbind subsystem from a cgroup hierarachy

In reply to a message not in the archive

This allows us to unbind a cgroup subsystem from a hierarchy which has sub-cgroups in it.

Due to some complexity, for now subsytems that use css_get/put() can't be unbound from such a hierarchy.

Usage:

# mount -t cgroup -o cpuset,cpuacct xxx /mnt
# mkdir /mnt/tmp
# echo $$ > /mnt/tmp/tasks

(remove it from the hierarchy)

# mount -o remount,cpuset xxx /mnt
---
 kernel/cgroup.c |   76 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 61 insertions(+), 15 deletions(-)
kernel/cgroup.c +61 -15
index 46df5f8..9ce3fdb 100644
@@ -1036,19 +1036,42 @@ static int hierarchy_attach_css(struct cgroup *cgrp, void *data)
 	return ret;
 }
 
-static int hierarchy_update_css_sets(struct cgroup *cgrp, void *data)
+static int hierarchy_remove_css(struct cgroup *cgrp, void *data)
 {
-	unsigned long added_bits = (unsigned long)data;
+	unsigned long removed_bits = (unsigned long)data;
 	int i;
+
+	/*
+	 * There might be some pointers to the cgroup_subsys_state
+	 * that we are going to destroy.
+	 */
+	synchronize_rcu();
+
+	for_each_set_bit(i, &removed_bits, CGROUP_SUBSYS_COUNT) {
+		subsys[i]->destroy(subsys[i], cgrp);
+		cgrp->subsys[i] = NULL;
+	}
+
+	return 0;
+}
+
+static void hierarchy_update_css_sets(struct cgroup *cgrp,
+				      unsigned long bits, bool add)
+{
 	struct cg_cgroup_link *link;
+	int i;
 
 	write_lock(&css_set_lock);
 	list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
 		struct css_set *cg = link->cg;
 		struct hlist_head *hhead;
 
-		for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT)
-			cg->subsys[i] = cgrp->subsys[i];
+		for_each_set_bit(i, &bits, CGROUP_SUBSYS_COUNT) {
+			if (add)
+				cg->subsys[i] = cgrp->subsys[i];
+			else
+				cg->subsys[i] = dummytop->subsys[i];
+		}
 
 		/* rehash */
 		hlist_del(&cg->hlist);
@@ -1056,7 +1079,21 @@ static int hierarchy_update_css_sets(struct cgroup *cgrp, void *data)
 		hlist_add_head(&cg->hlist, hhead);
 	}
 	write_unlock(&css_set_lock);
+}
 
+static int hierarchy_add_to_css_sets(struct cgroup *cgrp, void *data)
+{
+	unsigned long added_bits = (unsigned long)data;
+
+	hierarchy_update_css_sets(cgrp, added_bits, true);
+	return 0;
+}
+
+static int hierarchy_remove_from_css_sets(struct cgroup *cgrp, void *data)
+{
+	unsigned long removed_bits = (unsigned long)data;
+
+	hierarchy_update_css_sets(cgrp, removed_bits, false);
 	return 0;
 }
 
@@ -1076,7 +1113,7 @@ static int hierarchy_populate_dir(struct cgroup *cgrp, void *data)
 static int rebind_subsystems(struct cgroupfs_root *root,
 			      unsigned long final_bits)
 {
-	unsigned long added_bits, removed_bits;
+	unsigned long added_bits, removed_bits, changed_bits;
 	struct cgroup *cgrp = &root->top_cgroup;
 	int i;
 	int err;
@@ -1085,6 +1122,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 
 	removed_bits = root->actual_subsys_bits & ~final_bits;
 	added_bits = final_bits & ~root->actual_subsys_bits;
+	changed_bits = removed_bits | added_bits;
 
 	/* Check that any added subsystems are currently free */
 	for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT) {
@@ -1100,14 +1138,11 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 		}
 	}
 
-	/* removing will be supported later */
-	if (root->number_of_cgroups > 1 && removed_bits)
-		return -EBUSY;
-
 	if (root->number_of_cgroups > 1) {
-		for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT)
+		for_each_set_bit(i, &changed_bits, CGROUP_SUBSYS_COUNT)
 			if (!subsys[i]->can_bind)
 				return -EBUSY;
+	}
 
 	for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT) {
 		BUG_ON(cgrp->subsys[i]);
@@ -1123,7 +1158,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 	if (err)
 		goto failed;
 
-	cgroup_walk_hierarchy(hierarchy_update_css_sets,
+	cgroup_walk_hierarchy(hierarchy_add_to_css_sets,
 			      (void *)added_bits, cgrp);
 
 	/* Process each subsystem */
@@ -1143,11 +1178,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 		} else if (bit & removed_bits) {
 			/* We're removing this subsystem */
 			BUG_ON(ss == NULL);
-			BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
-			BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
 			mutex_lock(&ss->hierarchy_mutex);
-			dummytop->subsys[i]->cgroup = dummytop;
-			cgrp->subsys[i] = NULL;
 			if (ss->bind)
 				ss->bind(ss, dummytop);
 			subsys[i]->root = &rootnode;
@@ -1173,6 +1204,21 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 		}
 	}
 	root->subsys_bits = root->actual_subsys_bits = final_bits;
+
+	for_each_set_bit(i, &removed_bits, CGROUP_SUBSYS_COUNT) {
+		BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
+		BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
+
+		dummytop->subsys[i]->cgroup = dummytop;
+		cgrp->subsys[i] = NULL;
+	}
+
+	cgroup_walk_hierarchy(hierarchy_remove_from_css_sets,
+			      (void *)removed_bits, cgrp);
+
+	cgroup_walk_hierarchy(hierarchy_remove_css,
+			      (void *)removed_bits, cgrp);
+
 	synchronize_rcu();
 
 	return 0;
Signature · 1 line
1.7.0.1
author
#52 lore raw

PATCH 5/7 cgroups: Make freezer subsystem bindable

In reply to Li Zefan

To make it bindable, we need to thaw all processes when unbinding the freezer subsystem from a cgroup hierarchy.

---
 include/linux/cgroup.h  |    3 ++-
 kernel/cgroup.c         |   22 ++++++++++++++++++++--
 kernel/cgroup_freezer.c |   19 +++++++++++++++++--
 3 files changed, 39 insertions(+), 5 deletions(-)
include/linux/cgroup.h +2 -1
index 49369ff..1e4e1df 100644
@@ -478,7 +478,8 @@ struct cgroup_subsys {
 	int (*populate)(struct cgroup_subsys *ss,
 			struct cgroup *cgrp);
 	void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
-	void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
+	void (*bind)(struct cgroup_subsys *ss, struct cgroup *cgrp,
+		     bool unbind);
 
 	int subsys_id;
 
kernel/cgroup.c +20 -2
index 6364bb5..12c1f7c 100644
@@ -1105,6 +1105,22 @@ static int hierarchy_populate_dir(struct cgroup *cgrp, void *data)
 	return 0;
 }
 
+static int hierarchy_subsys_bind(struct cgroup *cgrp, void *data)
+{
+	struct cgroup_subsys *ss = data;
+
+	ss->bind(ss, cgrp, false);
+	return 0;
+}
+
+static int hierarchy_subsys_unbind(struct cgroup *cgrp, void *data)
+{
+	struct cgroup_subsys *ss = data;;
+
+	ss->bind(ss, cgrp, true);
+	return 0;
+}
+
 /*
  * Call with cgroup_mutex held. Drops reference counts on modules, including
  * any duplicate ones that parse_cgroupfs_options took. If this function
@@ -1172,7 +1188,8 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 			list_move(&ss->sibling, &root->subsys_list);
 			ss->root = root;
 			if (ss->bind)
-				ss->bind(ss, cgrp);
+				cgroup_walk_hierarchy(hierarchy_subsys_bind,
+						      ss, cgrp);
 			mutex_unlock(&ss->hierarchy_mutex);
 			/* refcount was already taken, and we're keeping it */
 		} else if (bit & removed_bits) {
@@ -1180,7 +1197,8 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 			BUG_ON(ss == NULL);
 			mutex_lock(&ss->hierarchy_mutex);
 			if (ss->bind)
-				ss->bind(ss, dummytop);
+				cgroup_walk_hierarchy(hierarchy_subsys_unbind,
+						      ss, cgrp);
 			subsys[i]->root = &rootnode;
 			list_move(&ss->sibling, &rootnode.subsys_list);
 			mutex_unlock(&ss->hierarchy_mutex);
kernel/cgroup_freezer.c +17 -2
index e7bebb7..de13ce4 100644
@@ -383,6 +383,21 @@ static int freezer_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
 	return cgroup_add_files(cgroup, ss, files, ARRAY_SIZE(files));
 }
 
+/*
+ * Thaw all processes before unbinding the freezer subsysem from
+ * cgroup hierarchy.
+ * */
+static void freezer_bind(struct cgroup_subsys *ss, struct cgroup *cgrp,
+			 bool unbind)
+{
+	struct freezer *freezer = cgroup_freezer(cgrp);
+
+	if (!unbind)
+		return;
+
+	unfreeze_cgroup(cgrp, freezer);
+}
+
 struct cgroup_subsys freezer_subsys = {
 	.name		= "freezer",
 	.create		= freezer_create,
@@ -390,7 +405,7 @@ struct cgroup_subsys freezer_subsys = {
 	.populate	= freezer_populate,
 	.subsys_id	= freezer_subsys_id,
 	.can_attach	= freezer_can_attach,
-	.attach		= NULL,
 	.fork		= freezer_fork,
-	.exit		= NULL,
+	.bind		= freezer_bind,
+	.can_bind	= 1,
 };
Signature · 1 line
1.7.0.1
#42 lore raw

PATCH 4/7 cgroups: Mark some subsystems bindable

In reply to a message not in the archive

For those subsystems (debug, cpuacct, net_cls and devices), setting the can_bind flag is sufficient.

---
 kernel/cgroup.c          |    1 +
 kernel/sched.c           |    1 +
 net/sched/cls_cgroup.c   |    1 +
 security/device_cgroup.c |    1 +
 4 files changed, 4 insertions(+), 0 deletions(-)
kernel/cgroup.c +1 -0
index 9ce3fdb..6364bb5 100644
@@ -5124,5 +5124,6 @@ struct cgroup_subsys debug_subsys = {
 	.destroy = debug_destroy,
 	.populate = debug_populate,
 	.subsys_id = debug_subsys_id,
+	.can_bind = 1,
 };
 #endif /* CONFIG_CGROUP_DEBUG */
kernel/sched.c +1 -0
index 51944e8..cae104f 100644
@@ -9329,6 +9329,7 @@ struct cgroup_subsys cpuacct_subsys = {
 	.destroy = cpuacct_destroy,
 	.populate = cpuacct_populate,
 	.subsys_id = cpuacct_subsys_id,
+	.can_bind = 1,
 };
 #endif	/* CONFIG_CGROUP_CPUACCT */
 
net/sched/cls_cgroup.c +1 -0
index 37dff78..020ddfe 100644
@@ -38,6 +38,7 @@ struct cgroup_subsys net_cls_subsys = {
 #define net_cls_subsys_id net_cls_subsys.subsys_id
 #endif
 	.module		= THIS_MODULE,
+	.can_bind	= 1,
 };
 
 
security/device_cgroup.c +1 -0
index 8d9c48f..b8136fc 100644
@@ -473,6 +473,7 @@ struct cgroup_subsys devices_subsys = {
 	.destroy = devcgroup_destroy,
 	.populate = devcgroup_populate,
 	.subsys_id = devices_subsys_id,
+	.can_bind = 1,
 };
 
 int devcgroup_inode_permission(struct inode *inode, int mask)
Signature · 1 line
1.7.0.1
author
#64 lore raw

PATCH 6/7 cgroups: Warn if a bindable subsystem calls css_get()

In reply to Li Zefan

For now bindable subsystems should not use css_get/put(), so warn on this misuse.

---
 include/linux/cgroup.h |    7 +++++--
 kernel/cgroup.c        |    3 +++
 2 files changed, 8 insertions(+), 2 deletions(-)
include/linux/cgroup.h +5 -2
index 1e4e1df..10e4e02 100644
@@ -80,13 +80,15 @@ struct cgroup_subsys_state {
 
 /* bits in struct cgroup_subsys_state flags field */
 enum {
-	CSS_ROOT, /* This CSS is the root of the subsystem */
-	CSS_REMOVED, /* This CSS is dead */
+	CSS_ROOT,	/* This CSS is the root of the subsystem */
+	CSS_REMOVED,	/* This CSS is dead */
+	CSS_NO_GET,	/* Forbid calling css_get/put() */
 };
 
 /* Caller must verify that the css is not for root cgroup */
 static inline void __css_get(struct cgroup_subsys_state *css, int count)
 {
+	BUG_ON(test_bit(CSS_NO_GET, &css->flags));
 	atomic_add(count, &css->refcnt);
 }
 
@@ -119,6 +121,7 @@ static inline bool css_tryget(struct cgroup_subsys_state *css)
 {
 	if (test_bit(CSS_ROOT, &css->flags))
 		return true;
+	BUG_ON(test_bit(CSS_NO_GET, &css->flags));
 	while (!atomic_inc_not_zero(&css->refcnt)) {
 		if (test_bit(CSS_REMOVED, &css->flags))
 			return false;
kernel/cgroup.c +3 -0
index 12c1f7c..88e265d 100644
@@ -936,6 +936,9 @@ static void init_cgroup_css(struct cgroup_subsys_state *css,
 	css->cgroup = cgrp;
 	atomic_set(&css->refcnt, 1);
 	css->flags = 0;
+	/* For now, a bindable subsystem should not call css_get/put(). */
+	if (ss->can_bind)
+		set_bit(CSS_NO_GET, &css->flags);
 	css->id = NULL;
 	if (cgrp == dummytop)
 		set_bit(CSS_ROOT, &css->flags);
Signature · 1 line
1.7.0.1
author
#68 lore raw

PATCH 7/7 cgroups: Update documentation for bindable subsystems

In reply to Li Zefan

Provide a usage example, and update the bind() callback API.

---
 Documentation/cgroups/cgroups.txt |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
Documentation/cgroups/cgroups.txt +17 -9
index 190018b..5b5382a 100644
@@ -363,17 +363,23 @@ Note this will add ns to the hierarchy but won't remove memory or
 cpuset, because the new options are appended to the old ones:
 # mount -o remount,ns /dev/cgroup
 
+For some subsystems you can bind them to a mounted hierarchy or
+remove them from it, even if there're sub-cgroups in it:
+# mount -t cgroup -o freezer hier1 /dev/cgroup
+# echo $$ > /dev/cgroup/my_cgroup
+# mount -o freezer,cpuset hier1 /dev/cgroup
+(failed)
+# mount -o freezer,cpuacct hier1 /dev/cgroup
+# mount -o cpuacct hier1 /dev/cgroup
+
+Note cpuacct should be sit in the default hierarchy before remount.
+
 To Specify a hierarchy's release_agent:
 # mount -t cgroup -o cpuset,release_agent="/sbin/cpuset_release_agent" \
   xxx /dev/cgroup
 
 Note that specifying 'release_agent' more than once will return failure.
 
-Note that changing the set of subsystems is currently only supported
-when the hierarchy consists of a single (root) cgroup. Supporting
-the ability to arbitrarily bind/unbind subsystems from an existing
-cgroup hierarchy is intended to be implemented in the future.
-
 Then under /dev/cgroup you can find a tree that corresponds to the
 tree of the cgroups in the system. For instance, /dev/cgroup
 is the cgroup that holds the whole system.
@@ -623,13 +629,15 @@ initialization which might be required before a task could attach.  For
 example in cpusets, no task may attach before 'cpus' and 'mems' are set
 up.
 
-void bind(struct cgroup_subsys *ss, struct cgroup *root)
+void bind(struct cgroup_subsys *ss, struct cgroup *cgrp, bool unbind)
 (cgroup_mutex and ss->hierarchy_mutex held by caller)
 
 Called when a cgroup subsystem is rebound to a different hierarchy
-and root cgroup. Currently this will only involve movement between
-the default hierarchy (which never has sub-cgroups) and a hierarchy
-that is being created/destroyed (and hence has no sub-cgroups).
+and root cgroup. For some subsystems this will only involve movement
+between the default hierarchy (which never has sub-cgroups) and a
+hierarchy that is being created/destroyed (and hence has no sub-cgroups).
+For some other subsystems this can involve movement between the default
+hierarchy and a mounted hierarchy which may have sub-cgroups in it.
 
 4. Questions
 ============
Signature · 1 line
1.7.0.1
#43 lore raw

PATCH 5/7 cgroups: Make freezer subsystem bindable

In reply to a message not in the archive

To make it bindable, we need to thaw all processes when unbinding the freezer subsystem from a cgroup hierarchy.

---
 include/linux/cgroup.h  |    3 ++-
 kernel/cgroup.c         |   22 ++++++++++++++++++++--
 kernel/cgroup_freezer.c |   19 +++++++++++++++++--
 3 files changed, 39 insertions(+), 5 deletions(-)
include/linux/cgroup.h +2 -1
index 49369ff..1e4e1df 100644
@@ -478,7 +478,8 @@ struct cgroup_subsys {
 	int (*populate)(struct cgroup_subsys *ss,
 			struct cgroup *cgrp);
 	void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
-	void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
+	void (*bind)(struct cgroup_subsys *ss, struct cgroup *cgrp,
+		     bool unbind);
 
 	int subsys_id;
 
kernel/cgroup.c +20 -2
index 6364bb5..12c1f7c 100644
@@ -1105,6 +1105,22 @@ static int hierarchy_populate_dir(struct cgroup *cgrp, void *data)
 	return 0;
 }
 
+static int hierarchy_subsys_bind(struct cgroup *cgrp, void *data)
+{
+	struct cgroup_subsys *ss = data;
+
+	ss->bind(ss, cgrp, false);
+	return 0;
+}
+
+static int hierarchy_subsys_unbind(struct cgroup *cgrp, void *data)
+{
+	struct cgroup_subsys *ss = data;;
+
+	ss->bind(ss, cgrp, true);
+	return 0;
+}
+
 /*
  * Call with cgroup_mutex held. Drops reference counts on modules, including
  * any duplicate ones that parse_cgroupfs_options took. If this function
@@ -1172,7 +1188,8 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 			list_move(&ss->sibling, &root->subsys_list);
 			ss->root = root;
 			if (ss->bind)
-				ss->bind(ss, cgrp);
+				cgroup_walk_hierarchy(hierarchy_subsys_bind,
+						      ss, cgrp);
 			mutex_unlock(&ss->hierarchy_mutex);
 			/* refcount was already taken, and we're keeping it */
 		} else if (bit & removed_bits) {
@@ -1180,7 +1197,8 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 			BUG_ON(ss == NULL);
 			mutex_lock(&ss->hierarchy_mutex);
 			if (ss->bind)
-				ss->bind(ss, dummytop);
+				cgroup_walk_hierarchy(hierarchy_subsys_unbind,
+						      ss, cgrp);
 			subsys[i]->root = &rootnode;
 			list_move(&ss->sibling, &rootnode.subsys_list);
 			mutex_unlock(&ss->hierarchy_mutex);
kernel/cgroup_freezer.c +17 -2
index e7bebb7..de13ce4 100644
@@ -383,6 +383,21 @@ static int freezer_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
 	return cgroup_add_files(cgroup, ss, files, ARRAY_SIZE(files));
 }
 
+/*
+ * Thaw all processes before unbinding the freezer subsysem from
+ * cgroup hierarchy.
+ * */
+static void freezer_bind(struct cgroup_subsys *ss, struct cgroup *cgrp,
+			 bool unbind)
+{
+	struct freezer *freezer = cgroup_freezer(cgrp);
+
+	if (!unbind)
+		return;
+
+	unfreeze_cgroup(cgrp, freezer);
+}
+
 struct cgroup_subsys freezer_subsys = {
 	.name		= "freezer",
 	.create		= freezer_create,
@@ -390,7 +405,7 @@ struct cgroup_subsys freezer_subsys = {
 	.populate	= freezer_populate,
 	.subsys_id	= freezer_subsys_id,
 	.can_attach	= freezer_can_attach,
-	.attach		= NULL,
 	.fork		= freezer_fork,
-	.exit		= NULL,
+	.bind		= freezer_bind,
+	.can_bind	= 1,
 };
Signature · 1 line
1.7.0.1
#44 lore raw

PATCH 6/7 cgroups: Warn if a bindable subsystem calls css_get()

In reply to a message not in the archive

For now bindable subsystems should not use css_get/put(), so warn on this misuse.

---
 include/linux/cgroup.h |    7 +++++--
 kernel/cgroup.c        |    3 +++
 2 files changed, 8 insertions(+), 2 deletions(-)
include/linux/cgroup.h +5 -2
index 1e4e1df..10e4e02 100644
@@ -80,13 +80,15 @@ struct cgroup_subsys_state {
 
 /* bits in struct cgroup_subsys_state flags field */
 enum {
-	CSS_ROOT, /* This CSS is the root of the subsystem */
-	CSS_REMOVED, /* This CSS is dead */
+	CSS_ROOT,	/* This CSS is the root of the subsystem */
+	CSS_REMOVED,	/* This CSS is dead */
+	CSS_NO_GET,	/* Forbid calling css_get/put() */
 };
 
 /* Caller must verify that the css is not for root cgroup */
 static inline void __css_get(struct cgroup_subsys_state *css, int count)
 {
+	BUG_ON(test_bit(CSS_NO_GET, &css->flags));
 	atomic_add(count, &css->refcnt);
 }
 
@@ -119,6 +121,7 @@ static inline bool css_tryget(struct cgroup_subsys_state *css)
 {
 	if (test_bit(CSS_ROOT, &css->flags))
 		return true;
+	BUG_ON(test_bit(CSS_NO_GET, &css->flags));
 	while (!atomic_inc_not_zero(&css->refcnt)) {
 		if (test_bit(CSS_REMOVED, &css->flags))
 			return false;
kernel/cgroup.c +3 -0
index 12c1f7c..88e265d 100644
@@ -936,6 +936,9 @@ static void init_cgroup_css(struct cgroup_subsys_state *css,
 	css->cgroup = cgrp;
 	atomic_set(&css->refcnt, 1);
 	css->flags = 0;
+	/* For now, a bindable subsystem should not call css_get/put(). */
+	if (ss->can_bind)
+		set_bit(CSS_NO_GET, &css->flags);
 	css->id = NULL;
 	if (cgrp == dummytop)
 		set_bit(CSS_ROOT, &css->flags);
Signature · 1 line
1.7.0.1
#45 lore raw

PATCH 7/7 cgroups: Update documentation for bindable subsystems

In reply to a message not in the archive

Provide a usage example, and update the bind() callback API.

---
 Documentation/cgroups/cgroups.txt |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
Documentation/cgroups/cgroups.txt +17 -9
index 190018b..5b5382a 100644
@@ -363,17 +363,23 @@ Note this will add ns to the hierarchy but won't remove memory or
 cpuset, because the new options are appended to the old ones:
 # mount -o remount,ns /dev/cgroup
 
+For some subsystems you can bind them to a mounted hierarchy or
+remove them from it, even if there're sub-cgroups in it:
+# mount -t cgroup -o freezer hier1 /dev/cgroup
+# echo $$ > /dev/cgroup/my_cgroup
+# mount -o freezer,cpuset hier1 /dev/cgroup
+(failed)
+# mount -o freezer,cpuacct hier1 /dev/cgroup
+# mount -o cpuacct hier1 /dev/cgroup
+
+Note cpuacct should be sit in the default hierarchy before remount.
+
 To Specify a hierarchy's release_agent:
 # mount -t cgroup -o cpuset,release_agent="/sbin/cpuset_release_agent" \
   xxx /dev/cgroup
 
 Note that specifying 'release_agent' more than once will return failure.
 
-Note that changing the set of subsystems is currently only supported
-when the hierarchy consists of a single (root) cgroup. Supporting
-the ability to arbitrarily bind/unbind subsystems from an existing
-cgroup hierarchy is intended to be implemented in the future.
-
 Then under /dev/cgroup you can find a tree that corresponds to the
 tree of the cgroups in the system. For instance, /dev/cgroup
 is the cgroup that holds the whole system.
@@ -623,13 +629,15 @@ initialization which might be required before a task could attach.  For
 example in cpusets, no task may attach before 'cpus' and 'mems' are set
 up.
 
-void bind(struct cgroup_subsys *ss, struct cgroup *root)
+void bind(struct cgroup_subsys *ss, struct cgroup *cgrp, bool unbind)
 (cgroup_mutex and ss->hierarchy_mutex held by caller)
 
 Called when a cgroup subsystem is rebound to a different hierarchy
-and root cgroup. Currently this will only involve movement between
-the default hierarchy (which never has sub-cgroups) and a hierarchy
-that is being created/destroyed (and hence has no sub-cgroups).
+and root cgroup. For some subsystems this will only involve movement
+between the default hierarchy (which never has sub-cgroups) and a
+hierarchy that is being created/destroyed (and hence has no sub-cgroups).
+For some other subsystems this can involve movement between the default
+hierarchy and a mounted hierarchy which may have sub-cgroups in it.
 
 4. Questions
 ============
Signature · 1 line
1.7.0.1
#25 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to a message not in the archive
Quoted 2 lines · Li Zefan

For example, we can't decide a cgroup's cpuset.mems and cpuset.cpus automatically, so cpuset is not bindable.

You mean to say that you cannot add cpuset to an existing hierarchy right? Not that you cannot add perf/cpuacct to an existing cpuset hierarchy?

#27 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to Li Zefan
Quoted 2 lines · Li Zefan

For example, we can't decide a cgroup's cpuset.mems and cpuset.cpus automatically, so cpuset is not bindable.

You mean to say that you cannot add cpuset to an existing hierarchy right? Not that you cannot add perf/cpuacct to an existing cpuset hierarchy?

#46 lore raw
In reply to a message not in the archive
Quoted 27 lines · Li Zefan

Stephane posted a patchset to add perf_cgroup subsystem, so perf can be used to monitor all threads belonging to a cgroup.

But if you already mounted a cgroup hierarchy but without perf_cgroup and the hierarchy has sub-cgroups, you can't bind perf_cgroup to it, and thus you're not able to use per-cgroup perf feature.

This patchset alleviates the pain, and then a subsytem can be bound/unbound to/from a hierarchy which has sub-cgroups in it.

Some subsystems still can't take advantage of this patchset, memcgroup and cpuset in specific.

For cpuset, if a hierarchy has a sub-cgroup and the cgroup has tasks, we can't decide sub-cgroup's cpuset.mems and cpuset.cpus automatically if we try to bind cpuset to this hierarchy.

For memcgroup, memcgroup uses css_get/put(), and due to some complexity, for now bindable subsystems should not use css_get/put().

Usage:

# mount -t cgroup -o cpuset xxx /mnt
# mkdir /mnt/tmp
# echo $$ > /mnt/tmp/tasks

(add cpuacct to the hierarchy)

Quoted 8 lines
# mount -o remount,cpuset,cpuacct xxx /mnt

(remove it from the hierarchy)

# mount -o remount,cpuset xxx /mnt

There's another limitation, cpuacct should not be bound to any mounted hierarchy before the above operation. But that's not a problem, as you can remove it from a hierarchy and bind it to another one.

Right, so the only remaining problem I see with this approach is that you cannot profile two different hierarchies at the same time, but I can't really think of a solution to that problem (nor do I care very much).

Seems like a nice approach, Thanks Li!

#85 lore raw
In reply to Li Zefan
Quoted 27 lines · Li Zefan

Stephane posted a patchset to add perf_cgroup subsystem, so perf can be used to monitor all threads belonging to a cgroup.

But if you already mounted a cgroup hierarchy but without perf_cgroup and the hierarchy has sub-cgroups, you can't bind perf_cgroup to it, and thus you're not able to use per-cgroup perf feature.

This patchset alleviates the pain, and then a subsytem can be bound/unbound to/from a hierarchy which has sub-cgroups in it.

Some subsystems still can't take advantage of this patchset, memcgroup and cpuset in specific.

For cpuset, if a hierarchy has a sub-cgroup and the cgroup has tasks, we can't decide sub-cgroup's cpuset.mems and cpuset.cpus automatically if we try to bind cpuset to this hierarchy.

For memcgroup, memcgroup uses css_get/put(), and due to some complexity, for now bindable subsystems should not use css_get/put().

Usage:

# mount -t cgroup -o cpuset xxx /mnt
# mkdir /mnt/tmp
# echo $$ > /mnt/tmp/tasks

(add cpuacct to the hierarchy)

Quoted 8 lines
# mount -o remount,cpuset,cpuacct xxx /mnt

(remove it from the hierarchy)

# mount -o remount,cpuset xxx /mnt

There's another limitation, cpuacct should not be bound to any mounted hierarchy before the above operation. But that's not a problem, as you can remove it from a hierarchy and bind it to another one.

Right, so the only remaining problem I see with this approach is that you cannot profile two different hierarchies at the same time, but I can't really think of a solution to that problem (nor do I care very much).

Seems like a nice approach, Thanks Li!

#53 lore raw

Re: [PATCH 5/7] cgroups: Make freezer subsystem bindable

In reply to Li Zefan
Quoted 4 lines · Li Zefan

To make it bindable, we need to thaw all processes when unbinding the freezer subsystem from a cgroup hierarchy.

Based on experience using cgroups and questions we've fielded in the past on IRC I'd say users will really appreciate this.

We're planning to use the freezer in checkpoint/restart. Since checkpoint requires the tasks to remain frozen for the duration of the syscall we add a kernel-internal freezer subsystem interface which prevents the cgroup from thawing. So we'll need some way to "block" unbinding for that as well.

Perhaps the bind op should be able to return an error when unbind == true? Although that raises the question of what to do if only one of multiple unbinds fails..

Alternately you could split the bind/unbind op function pointers and get rid of the boolean argument. Then just don't fill in the freezer's unbind op and refuse to unbind subsystems that lack the unbind op. That seems a bit cleaner for now at least.

Cheers,
-Matt Helsley

Quoted 29 lines
---
 include/linux/cgroup.h  |    3 ++-
 kernel/cgroup.c         |   22 ++++++++++++++++++++--
 kernel/cgroup_freezer.c |   19 +++++++++++++++++--
 3 files changed, 39 insertions(+), 5 deletions(-)
include/linux/cgroup.h +2 -1
index 49369ff..1e4e1df 100644
@@ -478,7 +478,8 @@ struct cgroup_subsys {
 	int (*populate)(struct cgroup_subsys *ss,
 			struct cgroup *cgrp);
 	void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
-	void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
+	void (*bind)(struct cgroup_subsys *ss, struct cgroup *cgrp,
+		     bool unbind);
 
 	int subsys_id;
 
kernel/cgroup.c +1 -0
index 6364bb5..12c1f7c 100644
@@ -1105,6 +1105,22 @@ static int hierarchy_populate_dir(struct cgroup *cgrp, void *data)
 	return 0;
 }
 
+static int hierarchy_subsys_bind(struct cgroup *cgrp, void *data)

kernel/cgroup.c static int hierarchy_populate_dir(struct cgroup *cgrp, void *data) · line 1118

Quoted 10 lines
kernel/cgroup.c +10 -0
+{
+	struct cgroup_subsys *ss = data;
+
+	ss->bind(ss, cgrp, false);
+	return 0;
+}
+
+static int hierarchy_subsys_unbind(struct cgroup *cgrp, void *data)
+{
+	struct cgroup_subsys *ss = data;;

nit: two semicolons

Quoted 72 lines
kernel/cgroup.c +9 -2
+
+	ss->bind(ss, cgrp, true);
+	return 0;
+}
+
 /*
  * Call with cgroup_mutex held. Drops reference counts on modules, including
  * any duplicate ones that parse_cgroupfs_options took. If this function
@@ -1172,7 +1188,8 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 			list_move(&ss->sibling, &root->subsys_list);
 			ss->root = root;
 			if (ss->bind)
-				ss->bind(ss, cgrp);
+				cgroup_walk_hierarchy(hierarchy_subsys_bind,
+						      ss, cgrp);
 			mutex_unlock(&ss->hierarchy_mutex);
 			/* refcount was already taken, and we're keeping it */
 		} else if (bit & removed_bits) {
@@ -1180,7 +1197,8 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 			BUG_ON(ss == NULL);
 			mutex_lock(&ss->hierarchy_mutex);
 			if (ss->bind)
-				ss->bind(ss, dummytop);
+				cgroup_walk_hierarchy(hierarchy_subsys_unbind,
+						      ss, cgrp);
 			subsys[i]->root = &rootnode;
 			list_move(&ss->sibling, &rootnode.subsys_list);
 			mutex_unlock(&ss->hierarchy_mutex);
kernel/cgroup_freezer.c +17 -2
index e7bebb7..de13ce4 100644
@@ -383,6 +383,21 @@ static int freezer_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
 	return cgroup_add_files(cgroup, ss, files, ARRAY_SIZE(files));
 }
 
+/*
+ * Thaw all processes before unbinding the freezer subsysem from
+ * cgroup hierarchy.
+ * */
+static void freezer_bind(struct cgroup_subsys *ss, struct cgroup *cgrp,
+			 bool unbind)
+{
+	struct freezer *freezer = cgroup_freezer(cgrp);
+
+	if (!unbind)
+		return;
+
+	unfreeze_cgroup(cgrp, freezer);
+}
+
 struct cgroup_subsys freezer_subsys = {
 	.name		= "freezer",
 	.create		= freezer_create,
@@ -390,7 +405,7 @@ struct cgroup_subsys freezer_subsys = {
 	.populate	= freezer_populate,
 	.subsys_id	= freezer_subsys_id,
 	.can_attach	= freezer_can_attach,
-	.attach		= NULL,
 	.fork		= freezer_fork,
-	.exit		= NULL,
+	.bind		= freezer_bind,
+	.can_bind	= 1,
 };
Signature · 7 lines
1.7.0.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
#26 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to a message not in the archive
Quoted 17 lines · Li Zefan

Stephane posted a patchset to add perf_cgroup subsystem, so perf can be used to monitor all threads belonging to a cgroup.

But if you already mounted a cgroup hierarchy but without perf_cgroup and the hierarchy has sub-cgroups, you can't bind perf_cgroup to it, and thus you're not able to use per-cgroup perf feature.

This patchset alleviates the pain, and then a subsytem can be bind/unbind to/from a hierarchy which has sub-cgroups in it.

For a cgroup subsystem to become bindable, the can_bind flag of struct cgroup_subsys should be set, and provide ->bind() callback if necessary.

But for some constraints, not all subsystems can take advantage of this patch. For example, we can't decide a cgroup's cpuset.mems and cpuset.cpus automatically, so cpuset is not bindable.

<snip>

Quoted 4 lines
kernel/cgroup.c +0 -0
index 6c36750..46df5f8 100644

<snip>

Quoted 8 lines

+/*
+ * cgroup_walk_herarchy - iterate through a cgroup hierarchy
+ * @process_cgroup: callback called on each cgroup in the hierarchy
+ * @data: will be passed to @process_cgroup
+ * @top_cgrp: the root cgroup of the hierarchy
+ *
+ * For such a hierarchy:
+ * a1 c1

Quoted 8 lines

+ * / /
+ * Root - a2 - b1 - c2
+ * \
+ * a3
+ *
+ * The iterating order is: a1, a2, b1, c1, c2, a3. So a parent will be
+ * processed before its children.
+ */

You could just say it's a depth-first walk except we process the parent before its children.

Quoted 2 lines
kernel/cgroup.c +2 -0
+static int cgroup_walk_hierarchy(int (*process_cgroup)(struct cgroup *, void *),
+				 void *data, struct cgroup *top_cgrp)

<snip>

Quoted 43 lines
kernel/cgroup.c +13 -7
+static int hierarchy_populate_dir(struct cgroup *cgrp, void *data)
+{
+	mutex_lock_nested(&cgrp->dentry->d_inode->i_mutex, I_MUTEX_CHILD);
+	cgroup_populate_dir(cgrp);
+	mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
+	return 0;
+}
+
 /*
  * Call with cgroup_mutex held. Drops reference counts on modules, including
  * any duplicate ones that parse_cgroupfs_options took. If this function
@@ -945,36 +1079,53 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 	unsigned long added_bits, removed_bits;
 	struct cgroup *cgrp = &root->top_cgroup;
 	int i;
+	int err;
 
 	BUG_ON(!mutex_is_locked(&cgroup_mutex));
 
 	removed_bits = root->actual_subsys_bits & ~final_bits;
 	added_bits = final_bits & ~root->actual_subsys_bits;
+
 	/* Check that any added subsystems are currently free */
-	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
-		unsigned long bit = 1UL << i;
-		struct cgroup_subsys *ss = subsys[i];
-		if (!(bit & added_bits))
-			continue;
+	for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT) {
 		/*
 		 * Nobody should tell us to do a subsys that doesn't exist:
 		 * parse_cgroupfs_options should catch that case and refcounts
 		 * ensure that subsystems won't disappear once selected.
 		 */
-		BUG_ON(ss == NULL);
-		if (ss->root != &rootnode) {
+		BUG_ON(subsys[i] == NULL);
+		if (subsys[i]->root != &rootnode) {
 			/* Subsystem isn't free */
 			return -EBUSY;
 		}
 	}
 

kernel/cgroup.c static int rebind_subsystems(struct cgroupfs_root *root, · line 1107

Quoted 10 lines
kernel/cgroup.c +3 -5
-	/* Currently we don't handle adding/removing subsystems when
-	 * any child cgroups exist. This is theoretically supportable
-	 * but involves complex error handling, so it's being left until
-	 * later */
-	if (root->number_of_cgroups > 1)
+	/* removing will be supported later */
+	if (root->number_of_cgroups > 1 && removed_bits)
 		return -EBUSY;
 
+	if (root->number_of_cgroups > 1) {

Is there something wrong with the indentation here? I can't see the closing brace for the "if (root->number_of_cgroups > 1)" that should precede the for_each_set_bit() loop below.

kernel/cgroup.c static int rebind_subsystems(struct cgroupfs_root *root, · line 1110

Quoted 3 lines
kernel/cgroup.c +3 -0
+		for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT)
+			if (!subsys[i]->can_bind)
+				return -EBUSY;

I think you could avoid the can_bind flag field entirely and do:

			if (!subsys[i]->bind)

Also, if we're going with my "split out unbind" suggestion I think the part here would be:

		for_each_set_bit(i, &removed_bits, CGROUP_SUBSYS_COUNT)
			if (!subsys[i]->unbind)
				return -EBUSY;

<snip>

Cheers,
-Matt Helsley

#30 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to Li Zefan
Quoted 17 lines · Li Zefan

Stephane posted a patchset to add perf_cgroup subsystem, so perf can be used to monitor all threads belonging to a cgroup.

But if you already mounted a cgroup hierarchy but without perf_cgroup and the hierarchy has sub-cgroups, you can't bind perf_cgroup to it, and thus you're not able to use per-cgroup perf feature.

This patchset alleviates the pain, and then a subsytem can be bind/unbind to/from a hierarchy which has sub-cgroups in it.

For a cgroup subsystem to become bindable, the can_bind flag of struct cgroup_subsys should be set, and provide ->bind() callback if necessary.

But for some constraints, not all subsystems can take advantage of this patch. For example, we can't decide a cgroup's cpuset.mems and cpuset.cpus automatically, so cpuset is not bindable.

<snip>

Quoted 4 lines
kernel/cgroup.c +0 -0
index 6c36750..46df5f8 100644

<snip>

Quoted 8 lines

+/*
+ * cgroup_walk_herarchy - iterate through a cgroup hierarchy
+ * @process_cgroup: callback called on each cgroup in the hierarchy
+ * @data: will be passed to @process_cgroup
+ * @top_cgrp: the root cgroup of the hierarchy
+ *
+ * For such a hierarchy:
+ * a1 c1

Quoted 8 lines

+ * / /
+ * Root - a2 - b1 - c2
+ * \
+ * a3
+ *
+ * The iterating order is: a1, a2, b1, c1, c2, a3. So a parent will be
+ * processed before its children.
+ */

You could just say it's a depth-first walk except we process the parent before its children.

Quoted 2 lines
kernel/cgroup.c +2 -0
+static int cgroup_walk_hierarchy(int (*process_cgroup)(struct cgroup *, void *),
+				 void *data, struct cgroup *top_cgrp)

<snip>

Quoted 43 lines
kernel/cgroup.c +13 -7
+static int hierarchy_populate_dir(struct cgroup *cgrp, void *data)
+{
+	mutex_lock_nested(&cgrp->dentry->d_inode->i_mutex, I_MUTEX_CHILD);
+	cgroup_populate_dir(cgrp);
+	mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
+	return 0;
+}
+
 /*
  * Call with cgroup_mutex held. Drops reference counts on modules, including
  * any duplicate ones that parse_cgroupfs_options took. If this function
@@ -945,36 +1079,53 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 	unsigned long added_bits, removed_bits;
 	struct cgroup *cgrp = &root->top_cgroup;
 	int i;
+	int err;
 
 	BUG_ON(!mutex_is_locked(&cgroup_mutex));
 
 	removed_bits = root->actual_subsys_bits & ~final_bits;
 	added_bits = final_bits & ~root->actual_subsys_bits;
+
 	/* Check that any added subsystems are currently free */
-	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
-		unsigned long bit = 1UL << i;
-		struct cgroup_subsys *ss = subsys[i];
-		if (!(bit & added_bits))
-			continue;
+	for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT) {
 		/*
 		 * Nobody should tell us to do a subsys that doesn't exist:
 		 * parse_cgroupfs_options should catch that case and refcounts
 		 * ensure that subsystems won't disappear once selected.
 		 */
-		BUG_ON(ss == NULL);
-		if (ss->root != &rootnode) {
+		BUG_ON(subsys[i] == NULL);
+		if (subsys[i]->root != &rootnode) {
 			/* Subsystem isn't free */
 			return -EBUSY;
 		}
 	}
 

kernel/cgroup.c static int rebind_subsystems(struct cgroupfs_root *root, · line 1107

Quoted 10 lines
kernel/cgroup.c +3 -5
-	/* Currently we don't handle adding/removing subsystems when
-	 * any child cgroups exist. This is theoretically supportable
-	 * but involves complex error handling, so it's being left until
-	 * later */
-	if (root->number_of_cgroups > 1)
+	/* removing will be supported later */
+	if (root->number_of_cgroups > 1 && removed_bits)
 		return -EBUSY;
 
+	if (root->number_of_cgroups > 1) {

Is there something wrong with the indentation here? I can't see the closing brace for the "if (root->number_of_cgroups > 1)" that should precede the for_each_set_bit() loop below.

kernel/cgroup.c static int rebind_subsystems(struct cgroupfs_root *root, · line 1110

Quoted 3 lines
kernel/cgroup.c +3 -0
+		for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT)
+			if (!subsys[i]->can_bind)
+				return -EBUSY;

I think you could avoid the can_bind flag field entirely and do:

			if (!subsys[i]->bind)

Also, if we're going with my "split out unbind" suggestion I think the part here would be:

		for_each_set_bit(i, &removed_bits, CGROUP_SUBSYS_COUNT)
			if (!subsys[i]->unbind)
				return -EBUSY;

<snip>

Cheers,
-Matt Helsley

#55 lore raw

Re: [PATCH 5/7] cgroups: Make freezer subsystem bindable

In reply to a message not in the archive
Quoted 5 lines · Matt Helsley
Quoted 4 lines · Li Zefan

To make it bindable, we need to thaw all processes when unbinding the freezer subsystem from a cgroup hierarchy.

<snip>

Quoted 5 lines

Alternately you could split the bind/unbind op function pointers and get rid of the boolean argument. Then just don't fill in the freezer's unbind op and refuse to unbind subsystems that lack the unbind op. That seems a bit cleaner for now at least.

Or alternately to the alternate :): add a can_unbind:1 field and keep the boolean bind op argument. That might be best -- avoids an extra function pointer and still splits the bind/unbind-ability of a subsystem.

Cheers,
-Matt Helsley

#58 lore raw

Re: [PATCH 5/7] cgroups: Make freezer subsystem bindable

In reply to Matt Helsley
Quoted 5 lines · Matt Helsley
Quoted 4 lines · Li Zefan

To make it bindable, we need to thaw all processes when unbinding the freezer subsystem from a cgroup hierarchy.

<snip>

Quoted 5 lines

Alternately you could split the bind/unbind op function pointers and get rid of the boolean argument. Then just don't fill in the freezer's unbind op and refuse to unbind subsystems that lack the unbind op. That seems a bit cleaner for now at least.

Or alternately to the alternate :): add a can_unbind:1 field and keep the boolean bind op argument. That might be best -- avoids an extra function pointer and still splits the bind/unbind-ability of a subsystem.

Cheers,
-Matt Helsley

#56 lore raw

Re: [PATCH 5/7] cgroups: Make freezer subsystem bindable

In reply to a message not in the archive
Quoted 6 lines · Matt Helsley
Quoted 4 lines · Li Zefan

To make it bindable, we need to thaw all processes when unbinding the freezer subsystem from a cgroup hierarchy.

Quoted 8 lines

Based on experience using cgroups and questions we've fielded in the past on IRC I'd say users will really appreciate this.

We're planning to use the freezer in checkpoint/restart. Since checkpoint requires the tasks to remain frozen for the duration of the syscall we add a kernel-internal freezer subsystem interface which prevents the cgroup from thawing. So we'll need some way to "block" unbinding for that as well.

And in case you're curious here are the portions of the checkpoint/restart tree which implement the above:

http://www.linux-cr.org/git/?p=linux-cr.git;a=commitdiff;h=a584c1c6b729a544c76cbb173ce62ffb6575b1d3
http://www.linux-cr.org/git/?p=linux-cr.git;a=commitdiff;h=34869146ecd662352c0024e0769ec77e643fb1e1

Cheers,
-Matt Helsley

#62 lore raw

Re: [PATCH 5/7] cgroups: Make freezer subsystem bindable

In reply to Matt Helsley
Quoted 6 lines · Matt Helsley
Quoted 4 lines · Li Zefan

To make it bindable, we need to thaw all processes when unbinding the freezer subsystem from a cgroup hierarchy.

Quoted 8 lines

Based on experience using cgroups and questions we've fielded in the past on IRC I'd say users will really appreciate this.

We're planning to use the freezer in checkpoint/restart. Since checkpoint requires the tasks to remain frozen for the duration of the syscall we add a kernel-internal freezer subsystem interface which prevents the cgroup from thawing. So we'll need some way to "block" unbinding for that as well.

And in case you're curious here are the portions of the checkpoint/restart tree which implement the above:

http://www.linux-cr.org/git/?p=linux-cr.git;a=commitdiff;h=a584c1c6b729a544c76cbb173ce62ffb6575b1d3
http://www.linux-cr.org/git/?p=linux-cr.git;a=commitdiff;h=34869146ecd662352c0024e0769ec77e643fb1e1

Cheers,
-Matt Helsley

#70 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to a message not in the archive
Quoted 16 lines · On Fri, 22 Oct 2010 16:12:25 +0800 Li Zefan…

Provide a usage example, and update the bind() callback API.

---
 Documentation/cgroups/cgroups.txt |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
Documentation/cgroups/cgroups.txt +1 -0
index 190018b..5b5382a 100644
@@ -363,17 +363,23 @@ Note this will add ns to the hierarchy but won't remove memory or
 cpuset, because the new options are appended to the old ones:
 # mount -o remount,ns /dev/cgroup
 
+For some subsystems you can bind them to a mounted hierarchy or

Documentation/cgroups/cgroups.txt Note this will add ns to the hierarchy but won't remove memory or · line 376

Quoted 10 lines
Documentation/cgroups/cgroups.txt +10 -0
+remove them from it, even if there're sub-cgroups in it:
+# mount -t cgroup -o freezer hier1 /dev/cgroup
+# echo $$ > /dev/cgroup/my_cgroup
+# mount -o freezer,cpuset hier1 /dev/cgroup
+(failed)
+# mount -o freezer,cpuacct hier1 /dev/cgroup
+# mount -o cpuacct hier1 /dev/cgroup
+
+Note cpuacct should be sit in the default hierarchy before remount.
+

Can this operation be used with noop cgroup ? (IOW, perf cgroup can be attached to noop cgroup ?)

Thanks,
-Kame

#72 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to Li Zefan
Quoted 16 lines · On Fri, 22 Oct 2010 16:12:25 +0800 Li Zefan <[email protected]> wrote:

Provide a usage example, and update the bind() callback API.

---
 Documentation/cgroups/cgroups.txt |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
Documentation/cgroups/cgroups.txt +1 -0
index 190018b..5b5382a 100644
@@ -363,17 +363,23 @@ Note this will add ns to the hierarchy but won't remove memory or
 cpuset, because the new options are appended to the old ones:
 # mount -o remount,ns /dev/cgroup
 
+For some subsystems you can bind them to a mounted hierarchy or

Documentation/cgroups/cgroups.txt Note this will add ns to the hierarchy but won't remove memory or · line 376

Quoted 10 lines
Documentation/cgroups/cgroups.txt +10 -0
+remove them from it, even if there're sub-cgroups in it:
+# mount -t cgroup -o freezer hier1 /dev/cgroup
+# echo $$ > /dev/cgroup/my_cgroup
+# mount -o freezer,cpuset hier1 /dev/cgroup
+(failed)
+# mount -o freezer,cpuacct hier1 /dev/cgroup
+# mount -o cpuacct hier1 /dev/cgroup
+
+Note cpuacct should be sit in the default hierarchy before remount.
+

Can this operation be used with noop cgroup ? (IOW, perf cgroup can be attached to noop cgroup ?)

Thanks,
-Kame

author
#73 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

Quoted 18 lines · KAMEZAWA Hiroyuki
Quoted 15 lines · On Fri, 22 Oct 2010 16:12:25 +0800 Li Zefan <[email protected]> wrote:

Provide a usage example, and update the bind() callback API.

---
 Documentation/cgroups/cgroups.txt |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
Documentation/cgroups/cgroups.txt +0 -0
index 190018b..5b5382a 100644
@@ -363,17 +363,23 @@ Note this will add ns to the hierarchy but won't remove memory or
 cpuset, because the new options are appended to the old ones:
 # mount -o remount,ns /dev/cgroup
 
Quoted 15 lines

Documentation/cgroups/cgroups.txt Note this will add ns to the hierarchy but won't remove memory or · line 376

Quoted 11 lines
Documentation/cgroups/cgroups.txt +11 -0
+For some subsystems you can bind them to a mounted hierarchy or
+remove them from it, even if there're sub-cgroups in it:
+# mount -t cgroup -o freezer hier1 /dev/cgroup
+# echo $$ > /dev/cgroup/my_cgroup
+# mount -o freezer,cpuset hier1 /dev/cgroup
+(failed)
+# mount -o freezer,cpuacct hier1 /dev/cgroup
+# mount -o cpuacct hier1 /dev/cgroup
+
+Note cpuacct should be sit in the default hierarchy before remount.
+

Can this operation be used with noop cgroup ? (IOW, perf cgroup can be attached to noop cgroup ?)

Yes, you can. I've tested it.

#78 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to a message not in the archive
Quoted 18 lines · KAMEZAWA Hiroyuki
Quoted 15 lines · On Fri, 22 Oct 2010 16:12:25 +0800 Li Zefan…

Provide a usage example, and update the bind() callback API.

---
 Documentation/cgroups/cgroups.txt |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
Documentation/cgroups/cgroups.txt +0 -0
index 190018b..5b5382a 100644
@@ -363,17 +363,23 @@ Note this will add ns to the hierarchy but won't remove memory or
 cpuset, because the new options are appended to the old ones:
 # mount -o remount,ns /dev/cgroup
 
Quoted 15 lines

Documentation/cgroups/cgroups.txt Note this will add ns to the hierarchy but won't remove memory or · line 376

Quoted 11 lines
Documentation/cgroups/cgroups.txt +11 -0
+For some subsystems you can bind them to a mounted hierarchy or
+remove them from it, even if there're sub-cgroups in it:
+# mount -t cgroup -o freezer hier1 /dev/cgroup
+# echo $$ > /dev/cgroup/my_cgroup
+# mount -o freezer,cpuset hier1 /dev/cgroup
+(failed)
+# mount -o freezer,cpuacct hier1 /dev/cgroup
+# mount -o cpuacct hier1 /dev/cgroup
+
+Note cpuacct should be sit in the default hierarchy before remount.
+

Can this operation be used with noop cgroup ? (IOW, perf cgroup can be attached to noop cgroup ?)

Yes, you can. I've tested it.

author
#74 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to Li Zefan
Quoted 34 lines · Li Zefan
Quoted 18 lines · KAMEZAWA Hiroyuki
Quoted 15 lines · On Fri, 22 Oct 2010 16:12:25 +0800 Li Zefan <[email protected]> wrote:

Provide a usage example, and update the bind() callback API.

---
 Documentation/cgroups/cgroups.txt |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
Documentation/cgroups/cgroups.txt +0 -0
index 190018b..5b5382a 100644
@@ -363,17 +363,23 @@ Note this will add ns to the hierarchy but won't remove memory or
 cpuset, because the new options are appended to the old ones:
 # mount -o remount,ns /dev/cgroup
 
Quoted 14 lines

Documentation/cgroups/cgroups.txt Note this will add ns to the hierarchy but won't remove memory or · line 376

Quoted 11 lines
Documentation/cgroups/cgroups.txt +11 -0
+For some subsystems you can bind them to a mounted hierarchy or
+remove them from it, even if there're sub-cgroups in it:
+# mount -t cgroup -o freezer hier1 /dev/cgroup
+# echo $$ > /dev/cgroup/my_cgroup
+# mount -o freezer,cpuset hier1 /dev/cgroup
+(failed)
+# mount -o freezer,cpuacct hier1 /dev/cgroup
+# mount -o cpuacct hier1 /dev/cgroup
+
+Note cpuacct should be sit in the default hierarchy before remount.
+

Can this operation be used with noop cgroup ? (IOW, perf cgroup can be attached to noop cgroup ?)

Quoted 1 line

Yes, you can. I've tested it.

But, wait. It stuck when I moved a task from a sub-cgroup to the root. Need to dig more..

#76 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to a message not in the archive
Quoted 34 lines · Li Zefan
Quoted 18 lines · KAMEZAWA Hiroyuki
Quoted 15 lines · On Fri, 22 Oct 2010 16:12:25 +0800 Li Zefan…

Provide a usage example, and update the bind() callback API.

---
 Documentation/cgroups/cgroups.txt |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
Documentation/cgroups/cgroups.txt +0 -0
index 190018b..5b5382a 100644
@@ -363,17 +363,23 @@ Note this will add ns to the hierarchy but won't remove memory or
 cpuset, because the new options are appended to the old ones:
 # mount -o remount,ns /dev/cgroup
 
Quoted 14 lines

Documentation/cgroups/cgroups.txt Note this will add ns to the hierarchy but won't remove memory or · line 376

Quoted 11 lines
Documentation/cgroups/cgroups.txt +11 -0
+For some subsystems you can bind them to a mounted hierarchy or
+remove them from it, even if there're sub-cgroups in it:
+# mount -t cgroup -o freezer hier1 /dev/cgroup
+# echo $$ > /dev/cgroup/my_cgroup
+# mount -o freezer,cpuset hier1 /dev/cgroup
+(failed)
+# mount -o freezer,cpuacct hier1 /dev/cgroup
+# mount -o cpuacct hier1 /dev/cgroup
+
+Note cpuacct should be sit in the default hierarchy before remount.
+

Can this operation be used with noop cgroup ? (IOW, perf cgroup can be attached to noop cgroup ?)

Quoted 1 line

Yes, you can. I've tested it.

But, wait. It stuck when I moved a task from a sub-cgroup to the root. Need to dig more..

author
#28 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 2 lines · Li Zefan

For example, we can't decide a cgroup's cpuset.mems and cpuset.cpus automatically, so cpuset is not bindable.

You mean to say that you cannot add cpuset to an existing hierarchy right? Not that you cannot add perf/cpuacct to an existing cpuset hierarchy?

Exactly. You can see from the example.

#29 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 2 lines · Li Zefan

For example, we can't decide a cgroup's cpuset.mems and cpuset.cpus automatically, so cpuset is not bindable.

You mean to say that you cannot add cpuset to an existing hierarchy right? Not that you cannot add perf/cpuacct to an existing cpuset hierarchy?

Exactly. You can see from the example.

author
#86 lore raw
In reply to Peter Zijlstra
Quoted 33 lines · Peter Zijlstra
Quoted 32 lines · Li Zefan

Stephane posted a patchset to add perf_cgroup subsystem, so perf can be used to monitor all threads belonging to a cgroup.

But if you already mounted a cgroup hierarchy but without perf_cgroup and the hierarchy has sub-cgroups, you can't bind perf_cgroup to it, and thus you're not able to use per-cgroup perf feature.

This patchset alleviates the pain, and then a subsytem can be bound/unbound to/from a hierarchy which has sub-cgroups in it.

Some subsystems still can't take advantage of this patchset, memcgroup and cpuset in specific.

For cpuset, if a hierarchy has a sub-cgroup and the cgroup has tasks, we can't decide sub-cgroup's cpuset.mems and cpuset.cpus automatically if we try to bind cpuset to this hierarchy.

For memcgroup, memcgroup uses css_get/put(), and due to some complexity, for now bindable subsystems should not use css_get/put().

Usage:

# mount -t cgroup -o cpuset xxx /mnt
# mkdir /mnt/tmp
# echo $$ > /mnt/tmp/tasks

(add cpuacct to the hierarchy)

# mount -o remount,cpuset,cpuacct xxx /mnt

(remove it from the hierarchy)

# mount -o remount,cpuset xxx /mnt
Quoted 9 lines
Quoted 3 lines

There's another limitation, cpuacct should not be bound to any mounted hierarchy before the above operation. But that's not a problem, as you can remove it from a hierarchy and bind it to another one.

Right, so the only remaining problem I see with this approach is that you cannot profile two different hierarchies at the same time, but I can't really think of a solution to that problem (nor do I care very much).

Paul had a patch to allow some subsystems to be added to multi-hierarchies, which may help. But it forbids accessing t->cgroups, which makes this feature of limited use.

Anyway I too don't care much about this.

Quoted 2 lines

Seems like a nice approach, Thanks Li!

Thanks!

#90 lore raw
In reply to Peter Zijlstra
Quoted 33 lines · Peter Zijlstra
Quoted 32 lines · Li Zefan

Stephane posted a patchset to add perf_cgroup subsystem, so perf can be used to monitor all threads belonging to a cgroup.

But if you already mounted a cgroup hierarchy but without perf_cgroup and the hierarchy has sub-cgroups, you can't bind perf_cgroup to it, and thus you're not able to use per-cgroup perf feature.

This patchset alleviates the pain, and then a subsytem can be bound/unbound to/from a hierarchy which has sub-cgroups in it.

Some subsystems still can't take advantage of this patchset, memcgroup and cpuset in specific.

For cpuset, if a hierarchy has a sub-cgroup and the cgroup has tasks, we can't decide sub-cgroup's cpuset.mems and cpuset.cpus automatically if we try to bind cpuset to this hierarchy.

For memcgroup, memcgroup uses css_get/put(), and due to some complexity, for now bindable subsystems should not use css_get/put().

Usage:

# mount -t cgroup -o cpuset xxx /mnt
# mkdir /mnt/tmp
# echo $$ > /mnt/tmp/tasks

(add cpuacct to the hierarchy)

# mount -o remount,cpuset,cpuacct xxx /mnt

(remove it from the hierarchy)

# mount -o remount,cpuset xxx /mnt
Quoted 9 lines
Quoted 3 lines

There's another limitation, cpuacct should not be bound to any mounted hierarchy before the above operation. But that's not a problem, as you can remove it from a hierarchy and bind it to another one.

Right, so the only remaining problem I see with this approach is that you cannot profile two different hierarchies at the same time, but I can't really think of a solution to that problem (nor do I care very much).

Paul had a patch to allow some subsystems to be added to multi-hierarchies, which may help. But it forbids accessing t->cgroups, which makes this feature of limited use.

Anyway I too don't care much about this.

Quoted 2 lines

Seems like a nice approach, Thanks Li!

Thanks!

author
#63 lore raw

Re: [PATCH 5/7] cgroups: Make freezer subsystem bindable

In reply to Matt Helsley
Quoted 6 lines · Matt Helsley
Quoted 4 lines · Li Zefan

To make it bindable, we need to thaw all processes when unbinding the freezer subsystem from a cgroup hierarchy.

Quoted 9 lines

Based on experience using cgroups and questions we've fielded in the past on IRC I'd say users will really appreciate this.

We're planning to use the freezer in checkpoint/restart. Since checkpoint requires the tasks to remain frozen for the duration of the syscall we add a kernel-internal freezer subsystem interface which prevents the cgroup from thawing. So we'll need some way to "block" unbinding for that as well.

How about, we allow unbinding only when all the cgroups' state is THAWED ?

Quoted 9 lines

Perhaps the bind op should be able to return an error when unbind == true? Although that raises the question of what to do if only one of multiple unbinds fails..

Alternately you could split the bind/unbind op function pointers and get rid of the boolean argument. Then just don't fill in the freezer's unbind op and refuse to unbind subsystems that lack the unbind op. That seems a bit cleaner for now at least.

We can use bindable:1 along with a callback can_bind().

For some subsystems, we just set bindable to true. For freezer subsystem, we set it to true and provide freezer_can_bind(), and return true only if no cgroup's state is FROZEN.

#57 lore raw

Re: [PATCH 5/7] cgroups: Make freezer subsystem bindable

In reply to a message not in the archive
Quoted 6 lines · Matt Helsley
Quoted 4 lines · Li Zefan

To make it bindable, we need to thaw all processes when unbinding the freezer subsystem from a cgroup hierarchy.

Quoted 9 lines

Based on experience using cgroups and questions we've fielded in the past on IRC I'd say users will really appreciate this.

We're planning to use the freezer in checkpoint/restart. Since checkpoint requires the tasks to remain frozen for the duration of the syscall we add a kernel-internal freezer subsystem interface which prevents the cgroup from thawing. So we'll need some way to "block" unbinding for that as well.

How about, we allow unbinding only when all the cgroups' state is THAWED ?

Quoted 9 lines

Perhaps the bind op should be able to return an error when unbind == true? Although that raises the question of what to do if only one of multiple unbinds fails..

Alternately you could split the bind/unbind op function pointers and get rid of the boolean argument. Then just don't fill in the freezer's unbind op and refuse to unbind subsystems that lack the unbind op. That seems a bit cleaner for now at least.

We can use bindable:1 along with a callback can_bind().

For some subsystems, we just set bindable to true. For freezer subsystem, we set it to true and provide freezer_can_bind(), and return true only if no cgroup's state is FROZEN.

author
#31 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to Matt Helsley
Quoted 17 lines
Quoted 8 lines

+/*
+ * cgroup_walk_herarchy - iterate through a cgroup hierarchy
+ * @process_cgroup: callback called on each cgroup in the hierarchy
+ * @data: will be passed to @process_cgroup
+ * @top_cgrp: the root cgroup of the hierarchy
+ *
+ * For such a hierarchy:
+ * a1 c1

Quoted 8 lines

+ * / /
+ * Root - a2 - b1 - c2
+ * \
+ * a3
+ *
+ * The iterating order is: a1, a2, b1, c1, c2, a3. So a parent will be
+ * processed before its children.
+ */

Quoted 3 lines

You could just say it's a depth-first walk except we process the parent before its children.

Will revise the comment. A diagram is intuitive. :)

Quoted 57 lines
Quoted 2 lines
kernel/cgroup.c +2 -0
+static int cgroup_walk_hierarchy(int (*process_cgroup)(struct cgroup *, void *),
+				 void *data, struct cgroup *top_cgrp)

<snip>

Quoted 52 lines
kernel/cgroup.c +15 -12
+static int hierarchy_populate_dir(struct cgroup *cgrp, void *data)
+{
+	mutex_lock_nested(&cgrp->dentry->d_inode->i_mutex, I_MUTEX_CHILD);
+	cgroup_populate_dir(cgrp);
+	mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
+	return 0;
+}
+
 /*
  * Call with cgroup_mutex held. Drops reference counts on modules, including
  * any duplicate ones that parse_cgroupfs_options took. If this function
@@ -945,36 +1079,53 @@ static int rebind_subsystems(struct cgroupfs_root *root,
 	unsigned long added_bits, removed_bits;
 	struct cgroup *cgrp = &root->top_cgroup;
 	int i;
+	int err;
 
 	BUG_ON(!mutex_is_locked(&cgroup_mutex));
 
 	removed_bits = root->actual_subsys_bits & ~final_bits;
 	added_bits = final_bits & ~root->actual_subsys_bits;
+
 	/* Check that any added subsystems are currently free */
-	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
-		unsigned long bit = 1UL << i;
-		struct cgroup_subsys *ss = subsys[i];
-		if (!(bit & added_bits))
-			continue;
+	for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT) {
 		/*
 		 * Nobody should tell us to do a subsys that doesn't exist:
 		 * parse_cgroupfs_options should catch that case and refcounts
 		 * ensure that subsystems won't disappear once selected.
 		 */
-		BUG_ON(ss == NULL);
-		if (ss->root != &rootnode) {
+		BUG_ON(subsys[i] == NULL);
+		if (subsys[i]->root != &rootnode) {
 			/* Subsystem isn't free */
 			return -EBUSY;
 		}
 	}
 
-	/* Currently we don't handle adding/removing subsystems when
-	 * any child cgroups exist. This is theoretically supportable
-	 * but involves complex error handling, so it's being left until
-	 * later */
-	if (root->number_of_cgroups > 1)
+	/* removing will be supported later */
+	if (root->number_of_cgroups > 1 && removed_bits)
 		return -EBUSY;
 
Quoted 6 lines

kernel/cgroup.c static int rebind_subsystems(struct cgroupfs_root *root, · line 1107

Quoted 1 line
kernel/cgroup.c +1 -0
+	if (root->number_of_cgroups > 1) {

Is there something wrong with the indentation here? I can't see the closing brace for the "if (root->number_of_cgroups > 1)" that should precede the for_each_set_bit() loop below.

Oops, I must have forgot to commit the change when I fixed this.

Quoted 8 lines

kernel/cgroup.c static int rebind_subsystems(struct cgroupfs_root *root, · line 1110

Quoted 3 lines
kernel/cgroup.c +3 -0
+		for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT)
+			if (!subsys[i]->can_bind)
+				return -EBUSY;

I think you could avoid the can_bind flag field entirely and do:

			if (!subsys[i]->bind)

Nope. For some subsystems we just set the flag and need not to provide a bind() callback.

Quoted 7 lines

Also, if we're going with my "split out unbind" suggestion I think the part here would be:

		for_each_set_bit(i, &removed_bits, CGROUP_SUBSYS_COUNT)
			if (!subsys[i]->unbind)
				return -EBUSY;
#88 lore raw
In reply to a message not in the archive
Quoted 3 lines · Li Zefan

Paul had a patch to allow some subsystems to be added to multi-hierarchies, which may help.

I more or less dropped it since I couldn't see a big use for it - all the examples I had were more easily implemented as basic cgroup control files (and hence available in all hierarchies automatically)

Quoted 2 lines

But it forbids accessing t->cgroups, which makes this feature of limited use.

Not exactly - it meant that you couldn't do a fast dereference via t->cgroups to get to your cgroup_subsys_state, since there was no longer a unique css for each task for that subsystem - there would be one per mounted instance of the subsystem. Instead it became necessary to follow a chain of pointers to find the appropriate css for the hierarchy in question.

In general this also means that it's harder for the multi-bound subsystem have any real-world meaning - it's mostly useful for stateless subsystems, or ones where the cgroup_subsys_state contents have no direct connection with machine-wide data.

Paul

#89 lore raw
In reply to Li Zefan
Quoted 3 lines · Li Zefan

Paul had a patch to allow some subsystems to be added to multi-hierarchies, which may help.

I more or less dropped it since I couldn't see a big use for it - all the examples I had were more easily implemented as basic cgroup control files (and hence available in all hierarchies automatically)

Quoted 2 lines

But it forbids accessing t->cgroups, which makes this feature of limited use.

Not exactly - it meant that you couldn't do a fast dereference via t->cgroups to get to your cgroup_subsys_state, since there was no longer a unique css for each task for that subsystem - there would be one per mounted instance of the subsystem. Instead it became necessary to follow a chain of pointers to find the appropriate css for the hierarchy in question.

In general this also means that it's harder for the multi-bound subsystem have any real-world meaning - it's mostly useful for stateless subsystems, or ones where the cgroup_subsys_state contents have no direct connection with machine-wide data.

Paul

Acked-by
#3 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to a message not in the archive
Quoted 4 lines · Li Zefan

On x86_32, sizeof(struct cgroup_subsys) shrinks from 276 bytes to 264.

Maybe use "bool" here?

Paul

Quoted 33 lines

---
include/linux/cgroup.h |   10 ++++++----

 1 files changed, 6 insertions(+), 4 deletions(-)
include/linux/cgroup.h +6 -5
index ed4ba11..e23ded6 100644
@@ -481,14 +481,16 @@ struct cgroup_subsys {
        void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
 
        int subsys_id;
-       int active;
-       int disabled;
-       int early_init;
+
+       unsigned int active:1;
+       unsigned int disabled:1;
+       unsigned int early_init:1;
        /*
         * True if this subsys uses ID. ID is not available before cgroup_init()
         * (not available in early_init time.)
         */
-       bool use_id;
+       unsigned int use_id:1;
+
  #define MAX_CGROUP_TYPE_NAMELEN 32
        const char *name;
 
--

1.7.0.1

Acked-by on v1 1/7
#4 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to Li Zefan
Quoted 4 lines · Li Zefan

On x86_32, sizeof(struct cgroup_subsys) shrinks from 276 bytes to 264.

Maybe use "bool" here?

Paul

Quoted 33 lines

---
include/linux/cgroup.h |   10 ++++++----

 1 files changed, 6 insertions(+), 4 deletions(-)
include/linux/cgroup.h +6 -5
index ed4ba11..e23ded6 100644
@@ -481,14 +481,16 @@ struct cgroup_subsys {
        void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
 
        int subsys_id;
-       int active;
-       int disabled;
-       int early_init;
+
+       unsigned int active:1;
+       unsigned int disabled:1;
+       unsigned int early_init:1;
        /*
         * True if this subsys uses ID. ID is not available before cgroup_init()
         * (not available in early_init time.)
         */
-       bool use_id;
+       unsigned int use_id:1;
+
  #define MAX_CGROUP_TYPE_NAMELEN 32
        const char *name;
 
--

1.7.0.1

#35 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to Li Zefan
Quoted 5 lines · Li Zefan

+       /*
+        * Indicate if this subsystem can be bound/unbound to/from a cgroup
+        * hierarchy which has child cgroups.
+        */
+       unsigned int can_bind:1;

Maybe call this "bindable"?

Basic idea looks great, it could do with a bunch more comments, and maybe locking rules.

Is there any chance of a lock inversion between dir->i_mutex and cgroup_lock in hierarchy_popuiate_dir() ?

Paul

#33 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to a message not in the archive
Quoted 7 lines · Matt Helsley
Quoted 4 lines

+ *
+ * The iterating order is: a1, a2, b1, c1, c2, a3. So a parent will be
+ * processed before its children.
+ */

You could just say it's a depth-first walk except we process the parent before its children.

The standard term for that is "pre-order traversal". You shouldn't need a diagram.

Paul

#34 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to Matt Helsley
Quoted 7 lines · Matt Helsley
Quoted 4 lines

+ *
+ * The iterating order is: a1, a2, b1, c1, c2, a3. So a parent will be
+ * processed before its children.
+ */

You could just say it's a depth-first walk except we process the parent before its children.

The standard term for that is "pre-order traversal". You shouldn't need a diagram.

Paul

#49 lore raw

Re: [PATCH 3/7] cgroups: Allow to unbind subsystem from a cgroup hierarachy

In reply to a message not in the archive
Quoted 8 lines · Li Zefan

-               for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT)
-                       cg->subsys[i] = cgrp->subsys[i];
+               for_each_set_bit(i, &bits, CGROUP_SUBSYS_COUNT) {
+                       if (add)
+                               cg->subsys[i] = cgrp->subsys[i];
+                       else
+                               cg->subsys[i] = dummytop->subsys[i];
+               }

Should these technically be rcu_assign_pointer() ?

Paul

#50 lore raw

Re: [PATCH 3/7] cgroups: Allow to unbind subsystem from a cgroup hierarachy

In reply to Li Zefan
Quoted 8 lines · Li Zefan

-               for_each_set_bit(i, &added_bits, CGROUP_SUBSYS_COUNT)
-                       cg->subsys[i] = cgrp->subsys[i];
+               for_each_set_bit(i, &bits, CGROUP_SUBSYS_COUNT) {
+                       if (add)
+                               cg->subsys[i] = cgrp->subsys[i];
+                       else
+                               cg->subsys[i] = dummytop->subsys[i];
+               }

Should these technically be rcu_assign_pointer() ?

Paul

#66 lore raw

Re: [PATCH 6/7] cgroups: Warn if a bindable subsystem calls css_get()

In reply to a message not in the archive

Maybe re-title this patch to BUG() if a bindable subsystem calls css_get() ?

Paul

Quoted 57 lines · Li Zefan

For now bindable subsystems should not use css_get/put(), so warn on this misuse.

---
 include/linux/cgroup.h |    7 +++++--
 kernel/cgroup.c        |    3 +++

 2 files changed, 8 insertions(+), 2 deletions(-)
include/linux/cgroup.h +5 -2
index 1e4e1df..10e4e02 100644
@@ -80,13 +80,15 @@ struct cgroup_subsys_state {
 
  /* bits in struct cgroup_subsys_state flags field */
  enum {
-       CSS_ROOT, /* This CSS is the root of the subsystem */
-       CSS_REMOVED, /* This CSS is dead */
+       CSS_ROOT,       /* This CSS is the root of the subsystem */
+       CSS_REMOVED,    /* This CSS is dead */
+       CSS_NO_GET,     /* Forbid calling css_get/put() */
  };
 
  /* Caller must verify that the css is not for root cgroup */
  static inline void __css_get(struct cgroup_subsys_state *css, int count)
  {
+       BUG_ON(test_bit(CSS_NO_GET, &css->flags));
        atomic_add(count, &css->refcnt);
  }
 
@@ -119,6 +121,7 @@ static inline bool css_tryget(struct cgroup_subsys_state *css)
  {
        if (test_bit(CSS_ROOT, &css->flags))
                return true;
+       BUG_ON(test_bit(CSS_NO_GET, &css->flags));
        while (!atomic_inc_not_zero(&css->refcnt)) {
                if (test_bit(CSS_REMOVED, &css->flags))
                        return false;
kernel/cgroup.c +3 -1
index 12c1f7c..88e265d 100644
@@ -936,6 +936,9 @@ static void init_cgroup_css(struct cgroup_subsys_state *css,
        css->cgroup = cgrp;
        atomic_set(&css->refcnt, 1);
        css->flags = 0;
+       /* For now, a bindable subsystem should not call css_get/put(). */
+       if (ss->can_bind)
+               set_bit(CSS_NO_GET, &css->flags);
        css->id = NULL;
        if (cgrp == dummytop)
                set_bit(CSS_ROOT, &css->flags);
--

1.7.0.1

#67 lore raw

Re: [PATCH 6/7] cgroups: Warn if a bindable subsystem calls css_get()

In reply to Li Zefan

Maybe re-title this patch to BUG() if a bindable subsystem calls css_get() ?

Paul

Quoted 57 lines · Li Zefan

For now bindable subsystems should not use css_get/put(), so warn on this misuse.

---
 include/linux/cgroup.h |    7 +++++--
 kernel/cgroup.c        |    3 +++

 2 files changed, 8 insertions(+), 2 deletions(-)
include/linux/cgroup.h +5 -2
index 1e4e1df..10e4e02 100644
@@ -80,13 +80,15 @@ struct cgroup_subsys_state {
 
  /* bits in struct cgroup_subsys_state flags field */
  enum {
-       CSS_ROOT, /* This CSS is the root of the subsystem */
-       CSS_REMOVED, /* This CSS is dead */
+       CSS_ROOT,       /* This CSS is the root of the subsystem */
+       CSS_REMOVED,    /* This CSS is dead */
+       CSS_NO_GET,     /* Forbid calling css_get/put() */
  };
 
  /* Caller must verify that the css is not for root cgroup */
  static inline void __css_get(struct cgroup_subsys_state *css, int count)
  {
+       BUG_ON(test_bit(CSS_NO_GET, &css->flags));
        atomic_add(count, &css->refcnt);
  }
 
@@ -119,6 +121,7 @@ static inline bool css_tryget(struct cgroup_subsys_state *css)
  {
        if (test_bit(CSS_ROOT, &css->flags))
                return true;
+       BUG_ON(test_bit(CSS_NO_GET, &css->flags));
        while (!atomic_inc_not_zero(&css->refcnt)) {
                if (test_bit(CSS_REMOVED, &css->flags))
                        return false;
kernel/cgroup.c +3 -1
index 12c1f7c..88e265d 100644
@@ -936,6 +936,9 @@ static void init_cgroup_css(struct cgroup_subsys_state *css,
        css->cgroup = cgrp;
        atomic_set(&css->refcnt, 1);
        css->flags = 0;
+       /* For now, a bindable subsystem should not call css_get/put(). */
+       if (ss->can_bind)
+               set_bit(CSS_NO_GET, &css->flags);
        css->id = NULL;
        if (cgrp == dummytop)
                set_bit(CSS_ROOT, &css->flags);
--

1.7.0.1

#60 lore raw

Re: [PATCH 5/7] cgroups: Make freezer subsystem bindable

In reply to a message not in the archive
Quoted 9 lines · Matt Helsley
Quoted 4 lines

Alternately you could split the bind/unbind op function pointers and get rid of the boolean argument. Then just don't fill in the freezer's unbind op and refuse to unbind subsystems that lack the unbind op. That seems a bit cleaner for now at least.

Or alternately to the alternate :):  add a can_unbind:1 field and keep the boolean bind op argument. That might be best -- avoids an extra function pointer and still splits the bind/unbind-ability of a subsystem.

I think I'd vote for the separate bind() / unbind() callbacks.

Paul

#61 lore raw

Re: [PATCH 5/7] cgroups: Make freezer subsystem bindable

In reply to Matt Helsley
Quoted 9 lines · Matt Helsley
Quoted 4 lines

Alternately you could split the bind/unbind op function pointers and get rid of the boolean argument. Then just don't fill in the freezer's unbind op and refuse to unbind subsystems that lack the unbind op. That seems a bit cleaner for now at least.

Or alternately to the alternate :):  add a can_unbind:1 field and keep the boolean bind op argument. That might be best -- avoids an extra function pointer and still splits the bind/unbind-ability of a subsystem.

I think I'd vote for the separate bind() / unbind() callbacks.

Paul

#71 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to a message not in the archive

Documentation/cgroups/cgroups.txt initialization which might be required before a task could attach. For · line 640

Quoted 10 lines

 Called when a cgroup subsystem is rebound to a different hierarchy

Documentation/cgroups/cgroups.txt +5 -3
-and root cgroup. Currently this will only involve movement between
-the default hierarchy (which never has sub-cgroups) and a hierarchy
-that is being created/destroyed (and hence has no sub-cgroups).
+and root cgroup. For some subsystems this will only involve movement
+between the default hierarchy (which never has sub-cgroups) and a
+hierarchy that is being created/destroyed (and hence has no sub-cgroups).
+For some other subsystems this can involve movement between the default
+hierarchy and a mounted hierarchy which may have sub-cgroups in it.

This is a bit vague. How about:

For non-bindable subsystems, this will only involve movement between the default hierarchy (which never has sub-cgroups) and a hierarchy that is being created/destroyed (and hence has no sub-cgroups).

For binadable subsystems, this may also involve movement between the default hierarchy and a mounted hierarchy that's populated with sub-cgroups.

Also, the docs should mention that a cgroup setting the can_bind flag has to be able to support side-effect free movement of a task into any just-created cgroup, and into the root cgroup at any time. i.e. it's not suitable for any subsystem where can_attach() might return false for the root cgroup or a newly-created cgroup, or attach() might have side-effects for those same cases.

Actually, perhaps we should forbid the combination of having both an attach() callback and can_bind=true ?

Also, post_clone() doesn't get called when creating the css hierarchy during binding.

Paul

#79 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to Li Zefan

Documentation/cgroups/cgroups.txt initialization which might be required before a task could attach. For · line 640

Quoted 10 lines

 Called when a cgroup subsystem is rebound to a different hierarchy

Documentation/cgroups/cgroups.txt +5 -3
-and root cgroup. Currently this will only involve movement between
-the default hierarchy (which never has sub-cgroups) and a hierarchy
-that is being created/destroyed (and hence has no sub-cgroups).
+and root cgroup. For some subsystems this will only involve movement
+between the default hierarchy (which never has sub-cgroups) and a
+hierarchy that is being created/destroyed (and hence has no sub-cgroups).
+For some other subsystems this can involve movement between the default
+hierarchy and a mounted hierarchy which may have sub-cgroups in it.

This is a bit vague. How about:

For non-bindable subsystems, this will only involve movement between the default hierarchy (which never has sub-cgroups) and a hierarchy that is being created/destroyed (and hence has no sub-cgroups).

For binadable subsystems, this may also involve movement between the default hierarchy and a mounted hierarchy that's populated with sub-cgroups.

Also, the docs should mention that a cgroup setting the can_bind flag has to be able to support side-effect free movement of a task into any just-created cgroup, and into the root cgroup at any time. i.e. it's not suitable for any subsystem where can_attach() might return false for the root cgroup or a newly-created cgroup, or attach() might have side-effects for those same cases.

Actually, perhaps we should forbid the combination of having both an attach() callback and can_bind=true ?

Also, post_clone() doesn't get called when creating the css hierarchy during binding.

Paul

#81 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to a message not in the archive
Quoted 4 lines · Paul Menage

Also, post_clone() doesn't get called when creating the css hierarchy during binding.

Oops, ignore that - I noticed that it does.

Paul

#83 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to Paul Menage
Quoted 4 lines · Paul Menage

Also, post_clone() doesn't get called when creating the css hierarchy during binding.

Oops, ignore that - I noticed that it does.

Paul

author
#5 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to Paul Menage

(Sorry for the delayed reply)

Quoted 10 lines · Paul Menage
Quoted 4 lines · Li Zefan

On x86_32, sizeof(struct cgroup_subsys) shrinks from 276 bytes to 264.

Maybe use "bool" here?

Do you mean:

bool active;
bool disabled;
...

?

With alignment 5-8 bool values == 8 bytes in 64-bit machine, compared to 4 bytes with the approach this patch takes.

Quoted 35 lines

Paul

Quoted 33 lines

---
include/linux/cgroup.h | 10 ++++++----

 1 files changed, 6 insertions(+), 4 deletions(-)
include/linux/cgroup.h +6 -5
index ed4ba11..e23ded6 100644
@@ -481,14 +481,16 @@ struct cgroup_subsys {
       void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
 
       int subsys_id;
-       int active;
-       int disabled;
-       int early_init;
+
+       unsigned int active:1;
+       unsigned int disabled:1;
+       unsigned int early_init:1;
       /*
        * True if this subsys uses ID. ID is not available before cgroup_init()
        * (not available in early_init time.)
        */
-       bool use_id;
+       unsigned int use_id:1;
+
 #define MAX_CGROUP_TYPE_NAMELEN 32
       const char *name;
 
--

1.7.0.1

#22 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to a message not in the archive

(Sorry for the delayed reply)

Quoted 10 lines · Paul Menage
Quoted 4 lines · Li Zefan

On x86_32, sizeof(struct cgroup_subsys) shrinks from 276 bytes to 264.

Maybe use "bool" here?

Do you mean:

bool active;
bool disabled;
...

?

With alignment 5-8 bool values == 8 bytes in 64-bit machine, compared to 4 bytes with the approach this patch takes.

Quoted 35 lines

Paul

Quoted 33 lines

---
include/linux/cgroup.h | 10 ++++++----

 1 files changed, 6 insertions(+), 4 deletions(-)
include/linux/cgroup.h +6 -5
index ed4ba11..e23ded6 100644
@@ -481,14 +481,16 @@ struct cgroup_subsys {
       void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
 
       int subsys_id;
-       int active;
-       int disabled;
-       int early_init;
+
+       unsigned int active:1;
+       unsigned int disabled:1;
+       unsigned int early_init:1;
       /*
        * True if this subsys uses ID. ID is not available before cgroup_init()
        * (not available in early_init time.)
        */
-       bool use_id;
+       unsigned int use_id:1;
+
 #define MAX_CGROUP_TYPE_NAMELEN 32
       const char *name;
 
--

1.7.0.1

author
#36 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to Paul Menage
Quoted 7 lines · Paul Menage

include/linux/cgroup.h struct cgroup_subsys { · line 497

Quoted 5 lines · Li Zefan
include/linux/cgroup.h +5 -0
+       /*
+        * Indicate if this subsystem can be bound/unbound to/from a cgroup
+        * hierarchy which has child cgroups.
+        */
+       unsigned int can_bind:1;
Quoted 8 lines

Maybe call this "bindable"?

Basic idea looks great, it could do with a bunch more comments, and maybe locking rules.

Is there any chance of a lock inversion between dir->i_mutex and cgroup_lock in hierarchy_popuiate_dir() ?

The lock order is:

mutex_lock(&dir->i_mutex)
mutex_lock(&cgroup_mutex)

    mutex_lock(&dir->i_mutex, I_MUTEX_CHILD)

it should be safe.

#38 lore raw

Re: [PATCH 2/7] cgroups: Allow to bind a subsystem to a cgroup hierarchy

In reply to a message not in the archive
Quoted 7 lines · Paul Menage

include/linux/cgroup.h struct cgroup_subsys { · line 497

Quoted 5 lines · Li Zefan
include/linux/cgroup.h +5 -0
+       /*
+        * Indicate if this subsystem can be bound/unbound to/from a cgroup
+        * hierarchy which has child cgroups.
+        */
+       unsigned int can_bind:1;
Quoted 8 lines

Maybe call this "bindable"?

Basic idea looks great, it could do with a bunch more comments, and maybe locking rules.

Is there any chance of a lock inversion between dir->i_mutex and cgroup_lock in hierarchy_popuiate_dir() ?

The lock order is:

mutex_lock(&dir->i_mutex)
mutex_lock(&cgroup_mutex)

    mutex_lock(&dir->i_mutex, I_MUTEX_CHILD)

it should be safe.

author
#84 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to Paul Menage
Quoted 27 lines · Paul Menage

Documentation/cgroups/cgroups.txt initialization which might be required before a task could attach. For · line 640

Quoted 9 lines

Called when a cgroup subsystem is rebound to a different hierarchy

Documentation/cgroups/cgroups.txt +5 -3
-and root cgroup. Currently this will only involve movement between
-the default hierarchy (which never has sub-cgroups) and a hierarchy
-that is being created/destroyed (and hence has no sub-cgroups).
+and root cgroup. For some subsystems this will only involve movement
+between the default hierarchy (which never has sub-cgroups) and a
+hierarchy that is being created/destroyed (and hence has no sub-cgroups).
+For some other subsystems this can involve movement between the default
+hierarchy and a mounted hierarchy which may have sub-cgroups in it.

This is a bit vague. How about:

For non-bindable subsystems, this will only involve movement between the default hierarchy (which never has sub-cgroups) and a hierarchy that is being created/destroyed (and hence has no sub-cgroups).

For binadable subsystems, this may also involve movement between the default hierarchy and a mounted hierarchy that's populated with sub-cgroups.

Also, the docs should mention that a cgroup setting the can_bind flag has to be able to support side-effect free movement of a task into any just-created cgroup, and into the root cgroup at any time. i.e. it's not suitable for any subsystem where can_attach() might return false for the root cgroup or a newly-created cgroup, or attach() might have side-effects for those same cases.

Quoted 6 lines

Actually, perhaps we should forbid the combination of having both an attach() callback and can_bind=true ?

Also, post_clone() doesn't get called when creating the css hierarchy during binding.

This is much better. :)

Documentation often causes my headache due to my limited English skill.

#82 lore raw

Re: [PATCH 7/7] cgroups: Update documentation for bindable subsystems

In reply to a message not in the archive
Quoted 27 lines · Paul Menage

Documentation/cgroups/cgroups.txt initialization which might be required before a task could attach. For · line 640

Quoted 9 lines

Called when a cgroup subsystem is rebound to a different hierarchy

Documentation/cgroups/cgroups.txt +5 -3
-and root cgroup. Currently this will only involve movement between
-the default hierarchy (which never has sub-cgroups) and a hierarchy
-that is being created/destroyed (and hence has no sub-cgroups).
+and root cgroup. For some subsystems this will only involve movement
+between the default hierarchy (which never has sub-cgroups) and a
+hierarchy that is being created/destroyed (and hence has no sub-cgroups).
+For some other subsystems this can involve movement between the default
+hierarchy and a mounted hierarchy which may have sub-cgroups in it.

This is a bit vague. How about:

For non-bindable subsystems, this will only involve movement between the default hierarchy (which never has sub-cgroups) and a hierarchy that is being created/destroyed (and hence has no sub-cgroups).

For binadable subsystems, this may also involve movement between the default hierarchy and a mounted hierarchy that's populated with sub-cgroups.

Also, the docs should mention that a cgroup setting the can_bind flag has to be able to support side-effect free movement of a task into any just-created cgroup, and into the root cgroup at any time. i.e. it's not suitable for any subsystem where can_attach() might return false for the root cgroup or a newly-created cgroup, or attach() might have side-effects for those same cases.

Quoted 6 lines

Actually, perhaps we should forbid the combination of having both an attach() callback and can_bind=true ?

Also, post_clone() doesn't get called when creating the css hierarchy during binding.

This is much better. :)

Documentation often causes my headache due to my limited English skill.

#7 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to a message not in the archive
Quoted 9 lines · Li Zefan

bool active;
bool disabled;
...

?

With alignment 5-8 bool values == 8 bytes in 64-bit machine, compared to 4 bytes with the approach this patch takes.

I meant specifying it as:

bool active:1;
bool disabled:1;

i.e. keeping the bit-sized flags but also keeping the bool semantics. Having said that, I'm not really sure why saving 12 bytes per subsystem is worth a patch.

Paul

#8 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to Li Zefan
Quoted 9 lines · Li Zefan

bool active;
bool disabled;
...

?

With alignment 5-8 bool values == 8 bytes in 64-bit machine, compared to 4 bytes with the approach this patch takes.

I meant specifying it as:

bool active:1;
bool disabled:1;

i.e. keeping the bit-sized flags but also keeping the bool semantics. Having said that, I'm not really sure why saving 12 bytes per subsystem is worth a patch.

Paul

author
#9 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

Quoted 7 lines · Paul Menage
Quoted 6 lines · Li Zefan

bool active;
bool disabled;
...

?

Quoted 8 lines
Quoted 2 lines

With alignment 5-8 bool values == 8 bytes in 64-bit machine, compared to 4 bytes with the approach this patch takes.

I meant specifying it as:

bool active:1;
bool disabled:1;

It won't compile, but unsigned char active:1 will do. ;)

Quoted 3 lines

i.e. keeping the bit-sized flags but also keeping the bool semantics. Having said that, I'm not really sure why saving 12 bytes per subsystem is worth a patch.

Every thing that reduces code size (without sacrifice readability and maintain maintainability) should be worth.

This is one of the reasons we accept patches that replacing kmalloc+memset with kzalloc, which just saves 8 bytes in my box.

#20 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to a message not in the archive
Quoted 7 lines · Paul Menage
Quoted 6 lines · Li Zefan

bool active;
bool disabled;
...

?

Quoted 8 lines
Quoted 2 lines

With alignment 5-8 bool values == 8 bytes in 64-bit machine, compared to 4 bytes with the approach this patch takes.

I meant specifying it as:

bool active:1;
bool disabled:1;

It won't compile, but unsigned char active:1 will do. ;)

Quoted 3 lines

i.e. keeping the bit-sized flags but also keeping the bool semantics. Having said that, I'm not really sure why saving 12 bytes per subsystem is worth a patch.

Every thing that reduces code size (without sacrifice readability and maintain maintainability) should be worth.

This is one of the reasons we accept patches that replacing kmalloc+memset with kzalloc, which just saves 8 bytes in my box.

#11 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to a message not in the archive
Quoted 6 lines · Li Zefan
Quoted 4 lines

bool active:1;
bool disabled:1;

It won't compile, but unsigned char active:1 will do. ;)

Are you sure? I don't have a buildable kernel tree at the moment, but the following fragment compiled fine for me (with gcc 4.4.3):

struct foo {
_Bool b1:1;
_Bool b2:1;
};

and was sized at one byte. And "bool" is just a typedef of _Bool in the kernel headers.

Quoted 3 lines

Every thing that reduces code size (without sacrifice readability and maintain maintainability) should be worth.

Agreed, within reason. But this patch doesn't reduce code size - it makes the code fractionally more complicated and reduces the *binary* size by a few bytes.

Quoted 4 lines

This is one of the reasons we accept patches that replacing kmalloc+memset with kzalloc, which just saves 8 bytes in my box.

Replacing two function calls with one function call is a code simplification and hence (generally) a good thing - the minuscule reduction in binary size reduction that comes with it is just noise.

Paul

#12 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to Li Zefan
Quoted 6 lines · Li Zefan
Quoted 4 lines

bool active:1;
bool disabled:1;

It won't compile, but unsigned char active:1 will do. ;)

Are you sure? I don't have a buildable kernel tree at the moment, but the following fragment compiled fine for me (with gcc 4.4.3):

struct foo {
_Bool b1:1;
_Bool b2:1;
};

and was sized at one byte. And "bool" is just a typedef of _Bool in the kernel headers.

Quoted 3 lines

Every thing that reduces code size (without sacrifice readability and maintain maintainability) should be worth.

Agreed, within reason. But this patch doesn't reduce code size - it makes the code fractionally more complicated and reduces the *binary* size by a few bytes.

Quoted 4 lines

This is one of the reasons we accept patches that replacing kmalloc+memset with kzalloc, which just saves 8 bytes in my box.

Replacing two function calls with one function call is a code simplification and hence (generally) a good thing - the minuscule reduction in binary size reduction that comes with it is just noise.

Paul

author
#13 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

Quoted 9 lines · Paul Menage
Quoted 4 lines · Li Zefan
Quoted 3 lines

bool active:1;
bool disabled:1;

It won't compile, but unsigned char active:1 will do. ;)

Are you sure? I don't have a buildable kernel tree at the moment, but the following fragment compiled fine for me (with gcc 4.4.3):

Quoted 8 lines

struct foo {
_Bool b1:1;
_Bool b2:1;
};

and was sized at one byte. And "bool" is just a typedef of _Bool in the kernel headers.

Oops, I just used bool outside kernel tree..

Quoted 4 lines
Quoted 2 lines

Every thing that reduces code size (without sacrifice readability and maintain maintainability) should be worth.

Agreed, within reason. But this patch doesn't reduce code size - it

I meant binary size.

Quoted 3 lines

makes the code fractionally more complicated and reduces the *binary* size by a few bytes.

It's a commonly used skill in kernel code, so I can't say it makes code more complicated.

That said, I'll happily drop this patch. It just came to me when I started to add new bool values to the structure. Or if you prefer bool xxx:1 or just bool xxx, I can do that.

Quoted 8 lines
Quoted 3 lines

This is one of the reasons we accept patches that replacing kmalloc+memset with kzalloc, which just saves 8 bytes in my box.

Replacing two function calls with one function call is a code simplification and hence (generally) a good thing - the minuscule reduction in binary size reduction that comes with it is just noise.

#18 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to a message not in the archive
Quoted 9 lines · Paul Menage
Quoted 4 lines · Li Zefan
Quoted 3 lines

bool active:1;
bool disabled:1;

It won't compile, but unsigned char active:1 will do. ;)

Are you sure? I don't have a buildable kernel tree at the moment, but the following fragment compiled fine for me (with gcc 4.4.3):

Quoted 8 lines

struct foo {
_Bool b1:1;
_Bool b2:1;
};

and was sized at one byte. And "bool" is just a typedef of _Bool in the kernel headers.

Oops, I just used bool outside kernel tree..

Quoted 4 lines
Quoted 2 lines

Every thing that reduces code size (without sacrifice readability and maintain maintainability) should be worth.

Agreed, within reason. But this patch doesn't reduce code size - it

I meant binary size.

Quoted 3 lines

makes the code fractionally more complicated and reduces the *binary* size by a few bytes.

It's a commonly used skill in kernel code, so I can't say it makes code more complicated.

That said, I'll happily drop this patch. It just came to me when I started to add new bool values to the structure. Or if you prefer bool xxx:1 or just bool xxx, I can do that.

Quoted 8 lines
Quoted 3 lines

This is one of the reasons we accept patches that replacing kmalloc+memset with kzalloc, which just saves 8 bytes in my box.

Replacing two function calls with one function call is a code simplification and hence (generally) a good thing - the minuscule reduction in binary size reduction that comes with it is just noise.

#15 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to a message not in the archive
Quoted 3 lines · Li Zefan

That said, I'll happily drop this patch. It just came to me when I started to add new bool values to the structure. Or if you prefer bool xxx:1 or just bool xxx, I can do that.

bool xxx:1 is fine with me - I think it's worth keeping the semantics of these being boolean flags as obvious as possible.

I just wouldn't invest much effort in similar patches in the future (given that there are only likely to be, what, <20 instances of cgroup_subsys in the kernel?). Shrinking a structure that had potentially very many instances, such as css_set or cg_cgroup_link, would be different of course.

Paul

#16 lore raw

Re: [PATCH 1/7] cgroups: Shrink struct cgroup_subsys

In reply to Li Zefan
Quoted 3 lines · Li Zefan

That said, I'll happily drop this patch. It just came to me when I started to add new bool values to the structure. Or if you prefer bool xxx:1 or just bool xxx, I can do that.

bool xxx:1 is fine with me - I think it's worth keeping the semantics of these being boolean flags as obvious as possible.

I just wouldn't invest much effort in similar patches in the future (given that there are only likely to be, what, <20 instances of cgroup_subsys in the kernel?). Shrinking a structure that had potentially very many instances, such as css_set or cg_cgroup_link, would be different of course.

Paul