cgroup: implement cgroup_subsys->depends_on
[pandora-kernel.git] / Documentation / cgroups / unified-hierarchy.txt
1
2 Cgroup unified hierarchy
3
4 April, 2014             Tejun Heo <tj@kernel.org>
5
6 This document describes the changes made by unified hierarchy and
7 their rationales.  It will eventually be merged into the main cgroup
8 documentation.
9
10 CONTENTS
11
12 1. Background
13 2. Basic Operation
14   2-1. Mounting
15   2-2. cgroup.subtree_control
16   2-3. cgroup.controllers
17 3. Structural Constraints
18   3-1. Top-down
19   3-2. No internal tasks
20 4. Other Changes
21   4-1. [Un]populated Notification
22   4-2. Other Core Changes
23   4-3. Per-Controller Changes
24     4-3-1. blkio
25     4-3-2. cpuset
26     4-3-3. memory
27 5. Planned Changes
28   5-1. CAP for resource control
29
30
31 1. Background
32
33 cgroup allows an arbitrary number of hierarchies and each hierarchy
34 can host any number of controllers.  While this seems to provide a
35 high level of flexibility, it isn't quite useful in practice.
36
37 For example, as there is only one instance of each controller, utility
38 type controllers such as freezer which can be useful in all
39 hierarchies can only be used in one.  The issue is exacerbated by the
40 fact that controllers can't be moved around once hierarchies are
41 populated.  Another issue is that all controllers bound to a hierarchy
42 are forced to have exactly the same view of the hierarchy.  It isn't
43 possible to vary the granularity depending on the specific controller.
44
45 In practice, these issues heavily limit which controllers can be put
46 on the same hierarchy and most configurations resort to putting each
47 controller on its own hierarchy.  Only closely related ones, such as
48 the cpu and cpuacct controllers, make sense to put on the same
49 hierarchy.  This often means that userland ends up managing multiple
50 similar hierarchies repeating the same steps on each hierarchy
51 whenever a hierarchy management operation is necessary.
52
53 Unfortunately, support for multiple hierarchies comes at a steep cost.
54 Internal implementation in cgroup core proper is dazzlingly
55 complicated but more importantly the support for multiple hierarchies
56 restricts how cgroup is used in general and what controllers can do.
57
58 There's no limit on how many hierarchies there may be, which means
59 that a task's cgroup membership can't be described in finite length.
60 The key may contain any varying number of entries and is unlimited in
61 length, which makes it highly awkward to handle and leads to addition
62 of controllers which exist only to identify membership, which in turn
63 exacerbates the original problem.
64
65 Also, as a controller can't have any expectation regarding what shape
66 of hierarchies other controllers would be on, each controller has to
67 assume that all other controllers are operating on completely
68 orthogonal hierarchies.  This makes it impossible, or at least very
69 cumbersome, for controllers to cooperate with each other.
70
71 In most use cases, putting controllers on hierarchies which are
72 completely orthogonal to each other isn't necessary.  What usually is
73 called for is the ability to have differing levels of granularity
74 depending on the specific controller.  In other words, hierarchy may
75 be collapsed from leaf towards root when viewed from specific
76 controllers.  For example, a given configuration might not care about
77 how memory is distributed beyond a certain level while still wanting
78 to control how CPU cycles are distributed.
79
80 Unified hierarchy is the next version of cgroup interface.  It aims to
81 address the aforementioned issues by having more structure while
82 retaining enough flexibility for most use cases.  Various other
83 general and controller-specific interface issues are also addressed in
84 the process.
85
86
87 2. Basic Operation
88
89 2-1. Mounting
90
91 Currently, unified hierarchy can be mounted with the following mount
92 command.  Note that this is still under development and scheduled to
93 change soon.
94
95  mount -t cgroup -o __DEVEL__sane_behavior cgroup $MOUNT_POINT
96
97 All controllers which are not bound to other hierarchies are
98 automatically bound to unified hierarchy and show up at the root of
99 it.  Controllers which are enabled only in the root of unified
100 hierarchy can be bound to other hierarchies.  This allows mixing
101 unified hierarchy with the traditional multiple hierarchies in a fully
102 backward compatible way.
103
104 A controller can be moved across hierarchies only after the controller
105 is no longer referenced in its current hierarchy.  Because per-cgroup
106 controller states are destroyed asynchronously and controllers may
107 have lingering references, a controller may not show up immediately on
108 the unified hierarchy after the final umount of the previous
109 hierarchy.  Similarly, a controller should be fully disabled to be
110 moved out of the unified hierarchy and it may take some time for the
111 disabled controller to become available for other hierarchies;
112 furthermore, due to dependencies among controllers, other controllers
113 may need to be disabled too.
114
115 While useful for development and manual configurations, dynamically
116 moving controllers between the unified and other hierarchies is
117 strongly discouraged for production use.  It is recommended to decide
118 the hierarchies and controller associations before starting using the
119 controllers.
120
121
122 2-2. cgroup.subtree_control
123
124 All cgroups on unified hierarchy have a "cgroup.subtree_control" file
125 which governs which controllers are enabled on the children of the
126 cgroup.  Let's assume a hierarchy like the following.
127
128   root - A - B - C
129                \ D
130
131 root's "cgroup.subtree_control" file determines which controllers are
132 enabled on A.  A's on B.  B's on C and D.  This coincides with the
133 fact that controllers on the immediate sub-level are used to
134 distribute the resources of the parent.  In fact, it's natural to
135 assume that resource control knobs of a child belong to its parent.
136 Enabling a controller in a "cgroup.subtree_control" file declares that
137 distribution of the respective resources of the cgroup will be
138 controlled.  Note that this means that controller enable states are
139 shared among siblings.
140
141 When read, the file contains a space-separated list of currently
142 enabled controllers.  A write to the file should contain a
143 space-separated list of controllers with '+' or '-' prefixed (without
144 the quotes).  Controllers prefixed with '+' are enabled and '-'
145 disabled.  If a controller is listed multiple times, the last entry
146 wins.  The specific operations are executed atomically - either all
147 succeed or fail.
148
149
150 2-3. cgroup.controllers
151
152 Read-only "cgroup.controllers" file contains a space-separated list of
153 controllers which can be enabled in the cgroup's
154 "cgroup.subtree_control" file.
155
156 In the root cgroup, this lists controllers which are not bound to
157 other hierarchies and the content changes as controllers are bound to
158 and unbound from other hierarchies.
159
160 In non-root cgroups, the content of this file equals that of the
161 parent's "cgroup.subtree_control" file as only controllers enabled
162 from the parent can be used in its children.
163
164
165 3. Structural Constraints
166
167 3-1. Top-down
168
169 As it doesn't make sense to nest control of an uncontrolled resource,
170 all non-root "cgroup.subtree_control" files can only contain
171 controllers which are enabled in the parent's "cgroup.subtree_control"
172 file.  A controller can be enabled only if the parent has the
173 controller enabled and a controller can't be disabled if one or more
174 children have it enabled.
175
176
177 3-2. No internal tasks
178
179 One long-standing issue that cgroup faces is the competition between
180 tasks belonging to the parent cgroup and its children cgroups.  This
181 is inherently nasty as two different types of entities compete and
182 there is no agreed-upon obvious way to handle it.  Different
183 controllers are doing different things.
184
185 The cpu controller considers tasks and cgroups as equivalents and maps
186 nice levels to cgroup weights.  This works for some cases but falls
187 flat when children should be allocated specific ratios of CPU cycles
188 and the number of internal tasks fluctuates - the ratios constantly
189 change as the number of competing entities fluctuates.  There also are
190 other issues.  The mapping from nice level to weight isn't obvious or
191 universal, and there are various other knobs which simply aren't
192 available for tasks.
193
194 The blkio controller implicitly creates a hidden leaf node for each
195 cgroup to host the tasks.  The hidden leaf has its own copies of all
196 the knobs with "leaf_" prefixed.  While this allows equivalent control
197 over internal tasks, it's with serious drawbacks.  It always adds an
198 extra layer of nesting which may not be necessary, makes the interface
199 messy and significantly complicates the implementation.
200
201 The memory controller currently doesn't have a way to control what
202 happens between internal tasks and child cgroups and the behavior is
203 not clearly defined.  There have been attempts to add ad-hoc behaviors
204 and knobs to tailor the behavior to specific workloads.  Continuing
205 this direction will lead to problems which will be extremely difficult
206 to resolve in the long term.
207
208 Multiple controllers struggle with internal tasks and came up with
209 different ways to deal with it; unfortunately, all the approaches in
210 use now are severely flawed and, furthermore, the widely different
211 behaviors make cgroup as whole highly inconsistent.
212
213 It is clear that this is something which needs to be addressed from
214 cgroup core proper in a uniform way so that controllers don't need to
215 worry about it and cgroup as a whole shows a consistent and logical
216 behavior.  To achieve that, unified hierarchy enforces the following
217 structural constraint:
218
219  Except for the root, only cgroups which don't contain any task may
220  have controllers enabled in their "cgroup.subtree_control" files.
221
222 Combined with other properties, this guarantees that, when a
223 controller is looking at the part of the hierarchy which has it
224 enabled, tasks are always only on the leaves.  This rules out
225 situations where child cgroups compete against internal tasks of the
226 parent.
227
228 There are two things to note.  Firstly, the root cgroup is exempt from
229 the restriction.  Root contains tasks and anonymous resource
230 consumption which can't be associated with any other cgroup and
231 requires special treatment from most controllers.  How resource
232 consumption in the root cgroup is governed is up to each controller.
233
234 Secondly, the restriction doesn't take effect if there is no enabled
235 controller in the cgroup's "cgroup.subtree_control" file.  This is
236 important as otherwise it wouldn't be possible to create children of a
237 populated cgroup.  To control resource distribution of a cgroup, the
238 cgroup must create children and transfer all its tasks to the children
239 before enabling controllers in its "cgroup.subtree_control" file.
240
241
242 4. Other Changes
243
244 4-1. [Un]populated Notification
245
246 cgroup users often need a way to determine when a cgroup's
247 subhierarchy becomes empty so that it can be cleaned up.  cgroup
248 currently provides release_agent for it; unfortunately, this mechanism
249 is riddled with issues.
250
251 - It delivers events by forking and execing a userland binary
252   specified as the release_agent.  This is a long deprecated method of
253   notification delivery.  It's extremely heavy, slow and cumbersome to
254   integrate with larger infrastructure.
255
256 - There is single monitoring point at the root.  There's no way to
257   delegate management of a subtree.
258
259 - The event isn't recursive.  It triggers when a cgroup doesn't have
260   any tasks or child cgroups.  Events for internal nodes trigger only
261   after all children are removed.  This again makes it impossible to
262   delegate management of a subtree.
263
264 - Events are filtered from the kernel side.  A "notify_on_release"
265   file is used to subscribe to or suppress release events.  This is
266   unnecessarily complicated and probably done this way because event
267   delivery itself was expensive.
268
269 Unified hierarchy implements an interface file "cgroup.populated"
270 which can be used to monitor whether the cgroup's subhierarchy has
271 tasks in it or not.  Its value is 0 if there is no task in the cgroup
272 and its descendants; otherwise, 1.  poll and [id]notify events are
273 triggered when the value changes.
274
275 This is significantly lighter and simpler and trivially allows
276 delegating management of subhierarchy - subhierarchy monitoring can
277 block further propagation simply by putting itself or another process
278 in the subhierarchy and monitor events that it's interested in from
279 there without interfering with monitoring higher in the tree.
280
281 In unified hierarchy, the release_agent mechanism is no longer
282 supported and the interface files "release_agent" and
283 "notify_on_release" do not exist.
284
285
286 4-2. Other Core Changes
287
288 - None of the mount options is allowed.
289
290 - remount is disallowed.
291
292 - rename(2) is disallowed.
293
294 - The "tasks" file is removed.  Everything should at process
295   granularity.  Use the "cgroup.procs" file instead.
296
297 - The "cgroup.procs" file is not sorted.  pids will be unique unless
298   they got recycled in-between reads.
299
300 - The "cgroup.clone_children" file is removed.
301
302
303 4-3. Per-Controller Changes
304
305 4-3-1. blkio
306
307 - blk-throttle becomes properly hierarchical.
308
309
310 4-3-2. cpuset
311
312 - Tasks are kept in empty cpusets after hotplug and take on the masks
313   of the nearest non-empty ancestor, instead of being moved to it.
314
315 - A task can be moved into an empty cpuset, and again it takes on the
316   masks of the nearest non-empty ancestor.
317
318
319 4-3-3. memory
320
321 - use_hierarchy is on by default and the cgroup file for the flag is
322   not created.
323
324
325 5. Planned Changes
326
327 5-1. CAP for resource control
328
329 Unified hierarchy will require one of the capabilities(7), which is
330 yet to be decided, for all resource control related knobs.  Process
331 organization operations - creation of sub-cgroups and migration of
332 processes in sub-hierarchies may be delegated by changing the
333 ownership and/or permissions on the cgroup directory and
334 "cgroup.procs" interface file; however, all operations which affect
335 resource control - writes to a "cgroup.subtree_control" file or any
336 controller-specific knobs - will require an explicit CAP privilege.
337
338 This, in part, is to prevent the cgroup interface from being
339 inadvertently promoted to programmable API used by non-privileged
340 binaries.  cgroup exposes various aspects of the system in ways which
341 aren't properly abstracted for direct consumption by regular programs.
342 This is an administration interface much closer to sysctl knobs than
343 system calls.  Even the basic access model, being filesystem path
344 based, isn't suitable for direct consumption.  There's no way to
345 access "my cgroup" in a race-free way or make multiple operations
346 atomic against migration to another cgroup.
347
348 Another aspect is that, for better or for worse, the cgroup interface
349 goes through far less scrutiny than regular interfaces for
350 unprivileged userland.  The upside is that cgroup is able to expose
351 useful features which may not be suitable for general consumption in a
352 reasonable time frame.  It provides a relatively short path between
353 internal details and userland-visible interface.  Of course, this
354 shortcut comes with high risk.  We go through what we go through for
355 general kernel APIs for good reasons.  It may end up leaking internal
356 details in a way which can exert significant pain by locking the
357 kernel into a contract that can't be maintained in a reasonable
358 manner.
359
360 Also, due to the specific nature, cgroup and its controllers don't
361 tend to attract attention from a wide scope of developers.  cgroup's
362 short history is already fraught with severely mis-designed
363 interfaces, unnecessary commitments to and exposing of internal
364 details, broken and dangerous implementations of various features.
365
366 Keeping cgroup as an administration interface is both advantageous for
367 its role and imperative given its nature.  Some of the cgroup features
368 may make sense for unprivileged access.  If deemed justified, those
369 must be further abstracted and implemented as a different interface,
370 be it a system call or process-private filesystem, and survive through
371 the scrutiny that any interface for general consumption is required to
372 go through.
373
374 Requiring CAP is not a complete solution but should serve as a
375 significant deterrent against spraying cgroup usages in non-privileged
376 programs.