Skip to contentSkip to conversation

cgroup: Introducing bypass mode

PATCH New Waiman Long 1 participant 4 messages Aug 9, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover cgroup: Introducing bypass mode

v2->v3:
- Remove invalid cgroup subdirectory creation patch.
- Add use cases for the bypass mode and removing statements about control files ownership in cgroup-v2.txt.
- Restrict bypass mode to non-domain (threaded) controllers only.

v1->v2:
- Remove relax no-internal-process constraint patch as this feature is in the thread mode v4 patch.
- Remove subtree root mode patch.
- Remove the skip dying css patch as I can no longer reproduce the problem.
- Rework the bypass mode so that write to "cgroup.controllers" to enable or disable controller interface files is only allowed if the parent grants bypass mode to children by writing the '#'-prefixed controller to "cgroup.subtree_control".
- Add a patch to disable subdirectory creation on an invalid domain.

v1 patch - https://lkml.org/lkml/2017/6/14/551
v2 patch - https://lkml.org/lkml/2017/7/21/606

This patchset introduces new capability to the cgroup v2 core to give more freedom and flexibility to non-domain controllers so that they can shape their own unique views of the virtual cgroup hierarchies that can best suit thier own use cases. It also enables a cgroup parent to selectively enable a non-domain controller in a subset of its child cgroups instead of in either all or none of them.

The bypass mode cannot be used on domain controllers as it will complicate resource distribution model and rules.

One use case is an application that want to use cpuset, for example, to bind some worker threads to individual cpus. At the same time, the application may also want to use cpu controller to limit the amount of cpu consumed by some other threads. Right now, the only way to do that with the current v2 control scheme is to create child cgroups with both cpu and cpuset controllers enabled and put the desired processes or threads into those child cgroups.

The cost of enabling cpuset on a task that need cpu controller is negligible. However, the cost of enabling cpu controller on tasks that only need cpuset can be noticeable. The performance difference may become a concern for users who are thinking of moving from cgroup v1 to v2.

Similarly, instead of cpuset, if we want to use perf_event, freezer or other non-domain controllers in a subset of tasks, we will also need to enable CPU controller along with the associated performance cost.

With bypass mode, we will have the ability to enable just the non-domain controllers the tasks needed in their respective child cgroups. It is just like what we can currently do with cgroup v1.

This patchset is layered on top of the "for-4.14" branch of Tejun's cgroup git tree.

Patch 1 introduces a new bypass mode that allows a non-domain controller to be disabled in a cgroup, but re-enabled again in its children. This is enabled by writing the controller name prefixed with '#' to the "cgroup.subtree_control" file. Then all its children will have this controller in bypass mode.

Patch 2 extends the bypass mode mechanism to allow those child cgroups that are put into the bypass mode for a particular non-domain controller by their parent to be re-enabled again by writing the controller name with the '+' prefix to the "cgroup.controllers" file.

Patch 3 extends the debug controller to expose additional controller masks introduced by this patchset.

Waiman Long (3):
cgroup: subtree_control bypass mode for non-domain controllers
cgroup: Allow reenabling of controller in bypass mode
cgroup: Make debug controller report new controller masks

 Documentation/cgroup-v2.txt |  58 +++++++---
 include/linux/cgroup-defs.h |  19 +++-
 kernel/cgroup/cgroup.c      | 250 +++++++++++++++++++++++++++++++++++---------
 kernel/cgroup/debug.c       |   2 +
 4 files changed, 257 insertions(+), 72 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v3 1/3 cgroup: subtree_control bypass mode for non-domain controllers

The special prefix '#' attached to a non-domain controller name can now be written into the cgroup.subtree_control file to set that controller in bypass mode in all the child cgroups. The controller will show up in the children's cgroup.controllers file, but the corresponding control knobs will be absent. However, that controller can be enabled or bypassed in its children by writing to their respective subtree_control files.

This mode is useful to non-domain controllers where there are costs to each additional layer of hierarchy. This mode will also allow more freedom in how each controller can shape its effective hierarchy independent of each others.

---
 include/linux/cgroup-defs.h |  12 ++--
 kernel/cgroup/cgroup.c      | 143 ++++++++++++++++++++++++++++----------------
 2 files changed, 100 insertions(+), 55 deletions(-)
include/linux/cgroup-defs.h +7 -5
index 59e4ad9..15655e5 100644
@@ -308,16 +308,18 @@ struct cgroup {
 	struct cgroup_file events_file;	/* handle for "cgroup.events" */
 
 	/*
-	 * The bitmask of subsystems enabled on the child cgroups.
-	 * ->subtree_control is the one configured through
-	 * "cgroup.subtree_control" while ->child_ss_mask is the effective
-	 * one which may have more subsystems enabled.  Controller knobs
-	 * are made available iff it's enabled in ->subtree_control.
+	 * The bitmask of subsystems enabled or bypassed on the child cgroups.
+	 * ->subtree_control and ->subtree_bypass are the one configured
+	 * through "cgroup.subtree_control" while ->subtree_ss_mask is the
+	 * effective one which may have more subsystems enabled.  Controller
+	 * knobs are made available iff it's enabled in ->subtree_ss_mask.
 	 */
 	u16 subtree_control;
 	u16 subtree_ss_mask;
+	u16 subtree_bypass;
 	u16 old_subtree_control;
 	u16 old_subtree_ss_mask;
+	u16 old_subtree_bypass;
 
 	/* Private pointers for each registered subsystem */
 	struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
kernel/cgroup/cgroup.c +93 -50

329 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v3 2/3 cgroup: Allow reenabling of controller in bypass mode

In reply to Waiman Long

Non-domain controllers set to bypass mode in the parent's "cgroup.subtree_control" can now be optionally enabled by writing the controller name with the '+' prefix to "cgroup.controllers". Using the '#' prefix will reset it back to the bypass state.

This capability allows a cgroup parent to individually enable non-domain controllers in a subset of its children instead of either all or none of them. This increases the flexibility each controller has in shaping the effective cgroup hierarchy to best suit its need.

---
 Documentation/cgroup-v2.txt |  58 +++++++++++++++++------
 include/linux/cgroup-defs.h |   7 +++
 kernel/cgroup/cgroup.c      | 109 ++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 156 insertions(+), 18 deletions(-)
Documentation/cgroup-v2.txt +43 -15
index dc44785..e76dc4cf 100644
@@ -363,10 +363,16 @@ disabled by writing to the "cgroup.subtree_control" file::
 
   # echo "+cpu +memory -io" > cgroup.subtree_control
 
+The prefixes '+', '-' and '#' are used to enable, disable or put
+a controller in the bypass mode respectively.  In the bypass mode,
+a controller is disabled in a cgroup, but it can be enabled again in
+its child cgroups as it will still be listed in "cgroup.controllers".
+Bypass mode can only be used on non-domain controllers.
+
 Only controllers which are listed in "cgroup.controllers" can be
-enabled.  When multiple operations are specified as above, either they
-all succeed or fail.  If multiple operations on the same controller
-are specified, the last one is effective.
+enabled or bypassed.  When multiple operations are specified as above,
+either they all succeed or fail.  If multiple operations on the same
+controller are specified, the last one is effective.
 
 Enabling a controller in a cgroup indicates that the distribution of
 the target resource across its immediate children will be controlled.
@@ -390,6 +396,20 @@ prefixed controller interface files from C and D.  This means that the
 controller interface files - anything which doesn't start with
 "cgroup." are owned by the parent rather than the cgroup itself.
 
+Once a non-domain controller is put into bypass mode in
+"cgroup.subtree_control", that controller can optionally be enabled
+again in child cgroups by writing the controller name with the '+
+prefix into "cgroup.controllers".  Writing the controller name with
+the '#' prefix into "cgroup.controllers" resets the state back to
+bypass mode.  The state of a non-domain controller cannot be changed
+anymore if it is enabled or bypassed in its "cgroup.subtree_control".
+
+The use of bypass mode thus allows a cgroup parent to have the ability
+to selectively enable a non-domain controller in a subset of its
+child cgroups instead of in either all or none of them. In other words,
+a non-domain controller can be enabled only on the cgroup that actually
+needs it, if desired.
+
 
 Top-down Constraint
 ~~~~~~~~~~~~~~~~~~~
@@ -397,10 +417,11 @@ Top-down Constraint
 Resources are distributed top-down and a cgroup can further distribute
 a resource only if the resource has been distributed to it from the
 parent.  This means that all non-root "cgroup.subtree_control" files
-can only contain controllers which are enabled in the parent's
-"cgroup.subtree_control" file.  A controller can be enabled only if
-the parent has the controller enabled and a controller can't be
-disabled if one or more children have it enabled.
+can only contain controllers which are enabled or bypassed in the parent's
+"cgroup.subtree_control" file.  A controller can be enabled or bypassed
+only if the parent has the controller enabled or bypassed and the
+state of a controller can't be changed if one or more children have
+it enabled or bypassed.
 
 
 No Internal Process Constraint
@@ -823,11 +844,18 @@ All cgroup core files are prefixed with "cgroup."
 	should be granted along with the containing directory.
 
   cgroup.controllers
-	A read-only space separated values file which exists on all
+	A read-write space separated values file which exists on all
 	cgroups.
 
 	It shows space separated list of all controllers available to
-	the cgroup.  The controllers are not ordered.
+	the cgroup.  Controller names with '#' prefix are in bypass
+	mode.  The controllers are not ordered.
+
+	When a controller is set into bypass mode in its parent's
+	"cgroup.subtree_control", its name prefixed with '+' or '#'
+	can be written to enable it or reset it back to bypass mode
+	respectively.  Controllers not in bypass mode are not allowed
+	to be written.
 
   cgroup.subtree_control
 	A read-write space separated values file which exists on all
@@ -837,12 +865,12 @@ All cgroup core files are prefixed with "cgroup."
 	which are enabled to control resource distribution from the
 	cgroup to its children.
 
-	Space separated list of controllers prefixed with '+' or '-'
-	can be written to enable or disable controllers.  A controller
-	name prefixed with '+' enables the controller and '-'
-	disables.  If a controller appears more than once on the list,
-	the last one is effective.  When multiple enable and disable
-	operations are specified, either all succeed or all fail.
+	Space separated list of controllers prefixed with '+', '-' or
+	'#' can be written to enable, disable or bypass controllers
+	respectively.  If a controller appears more than once on
+	the list, the last one is effective.  When multiple enable,
+	disable or bypass operations are specified, either all succeed
+	or all fail.
 
   cgroup.events
 	A read-only flat-keyed file which exists on non-root cgroups.
include/linux/cgroup-defs.h +7 -0
index 15655e5..9f03254 100644
@@ -321,6 +321,13 @@ struct cgroup {
 	u16 old_subtree_ss_mask;
 	u16 old_subtree_bypass;
 
+	/*
+	 * The bitmask of subsystems that are set in its parent's
+	 * ->subtree_bypass and explicitly enabled in this cgroup.
+	 */
+	u16 enable_ss_mask;
+	u16 old_enable_ss_mask;
+
 	/* Private pointers for each registered subsystem */
 	struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
 
kernel/cgroup/cgroup.c +106 -3

151 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v3 3/3 cgroup: Make debug controller report new controller masks

In reply to Waiman Long

The newly added cgroup controller masks (subtree_bypass and enable_ss_mask) are now being reported in the debug.masks controller file.

---
 kernel/cgroup/debug.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/cgroup/debug.c +2 -0
index f661b4c..5f35a76 100644
@@ -262,6 +262,8 @@ static int cgroup_masks_read(struct seq_file *seq, void *v)
 
 	cgroup_masks_read_one(seq, "subtree_control", cgrp->subtree_control);
 	cgroup_masks_read_one(seq, "subtree_ss_mask", cgrp->subtree_ss_mask);
+	cgroup_masks_read_one(seq, "subtree_bypass",  cgrp->subtree_bypass);
+	cgroup_masks_read_one(seq, "enable_ss_mask",  cgrp->enable_ss_mask);
 
 	cgroup_kn_unlock(of->kn);
 	return 0;
Signature · 1 line
1.8.3.1