Commit | Line | Data |
---|---|---|
6c292092 TH |
1 | |
2 | Control Group v2 | |
3 | ||
4 | October, 2015 Tejun Heo <tj@kernel.org> | |
5 | ||
6 | This is the authoritative documentation on the design, interface and | |
7 | conventions of cgroup v2. It describes all userland-visible aspects | |
8 | of cgroup including core and specific controller behaviors. All | |
9 | future changes must be reflected in this document. Documentation for | |
9a2ddda5 | 10 | v1 is available under Documentation/cgroup-v1/. |
6c292092 TH |
11 | |
12 | CONTENTS | |
13 | ||
14 | 1. Introduction | |
15 | 1-1. Terminology | |
16 | 1-2. What is cgroup? | |
17 | 2. Basic Operations | |
18 | 2-1. Mounting | |
19 | 2-2. Organizing Processes | |
20 | 2-3. [Un]populated Notification | |
21 | 2-4. Controlling Controllers | |
22 | 2-4-1. Enabling and Disabling | |
23 | 2-4-2. Top-down Constraint | |
24 | 2-4-3. No Internal Process Constraint | |
25 | 2-5. Delegation | |
26 | 2-5-1. Model of Delegation | |
27 | 2-5-2. Delegation Containment | |
28 | 2-6. Guidelines | |
29 | 2-6-1. Organize Once and Control | |
30 | 2-6-2. Avoid Name Collisions | |
31 | 3. Resource Distribution Models | |
32 | 3-1. Weights | |
33 | 3-2. Limits | |
34 | 3-3. Protections | |
35 | 3-4. Allocations | |
36 | 4. Interface Files | |
37 | 4-1. Format | |
38 | 4-2. Conventions | |
39 | 4-3. Core Interface Files | |
40 | 5. Controllers | |
41 | 5-1. CPU | |
42 | 5-1-1. CPU Interface Files | |
43 | 5-2. Memory | |
44 | 5-2-1. Memory Interface Files | |
45 | 5-2-2. Usage Guidelines | |
46 | 5-2-3. Memory Ownership | |
47 | 5-3. IO | |
48 | 5-3-1. IO Interface Files | |
49 | 5-3-2. Writeback | |
20c56e59 HR |
50 | 5-4. PID |
51 | 5-4-1. PID Interface Files | |
d4021f6c SH |
52 | 6. Namespace |
53 | 6-1. Basics | |
54 | 6-2. The Root and Views | |
55 | 6-3. Migration and setns(2) | |
56 | 6-4. Interaction with Other Namespaces | |
6c292092 TH |
57 | P. Information on Kernel Programming |
58 | P-1. Filesystem Support for Writeback | |
59 | D. Deprecated v1 Core Features | |
60 | R. Issues with v1 and Rationales for v2 | |
61 | R-1. Multiple Hierarchies | |
62 | R-2. Thread Granularity | |
63 | R-3. Competition Between Inner Nodes and Threads | |
64 | R-4. Other Interface Issues | |
65 | R-5. Controller Issues and Remedies | |
66 | R-5-1. Memory | |
67 | ||
68 | ||
69 | 1. Introduction | |
70 | ||
71 | 1-1. Terminology | |
72 | ||
73 | "cgroup" stands for "control group" and is never capitalized. The | |
74 | singular form is used to designate the whole feature and also as a | |
75 | qualifier as in "cgroup controllers". When explicitly referring to | |
76 | multiple individual control groups, the plural form "cgroups" is used. | |
77 | ||
78 | ||
79 | 1-2. What is cgroup? | |
80 | ||
81 | cgroup is a mechanism to organize processes hierarchically and | |
82 | distribute system resources along the hierarchy in a controlled and | |
83 | configurable manner. | |
84 | ||
85 | cgroup is largely composed of two parts - the core and controllers. | |
86 | cgroup core is primarily responsible for hierarchically organizing | |
87 | processes. A cgroup controller is usually responsible for | |
88 | distributing a specific type of system resource along the hierarchy | |
89 | although there are utility controllers which serve purposes other than | |
90 | resource distribution. | |
91 | ||
92 | cgroups form a tree structure and every process in the system belongs | |
93 | to one and only one cgroup. All threads of a process belong to the | |
94 | same cgroup. On creation, all processes are put in the cgroup that | |
95 | the parent process belongs to at the time. A process can be migrated | |
96 | to another cgroup. Migration of a process doesn't affect already | |
97 | existing descendant processes. | |
98 | ||
99 | Following certain structural constraints, controllers may be enabled or | |
100 | disabled selectively on a cgroup. All controller behaviors are | |
101 | hierarchical - if a controller is enabled on a cgroup, it affects all | |
102 | processes which belong to the cgroups consisting the inclusive | |
103 | sub-hierarchy of the cgroup. When a controller is enabled on a nested | |
104 | cgroup, it always restricts the resource distribution further. The | |
105 | restrictions set closer to the root in the hierarchy can not be | |
106 | overridden from further away. | |
107 | ||
108 | ||
109 | 2. Basic Operations | |
110 | ||
111 | 2-1. Mounting | |
112 | ||
113 | Unlike v1, cgroup v2 has only single hierarchy. The cgroup v2 | |
114 | hierarchy can be mounted with the following mount command. | |
115 | ||
116 | # mount -t cgroup2 none $MOUNT_POINT | |
117 | ||
118 | cgroup2 filesystem has the magic number 0x63677270 ("cgrp"). All | |
119 | controllers which support v2 and are not bound to a v1 hierarchy are | |
120 | automatically bound to the v2 hierarchy and show up at the root. | |
121 | Controllers which are not in active use in the v2 hierarchy can be | |
122 | bound to other hierarchies. This allows mixing v2 hierarchy with the | |
123 | legacy v1 multiple hierarchies in a fully backward compatible way. | |
124 | ||
125 | A controller can be moved across hierarchies only after the controller | |
126 | is no longer referenced in its current hierarchy. Because per-cgroup | |
127 | controller states are destroyed asynchronously and controllers may | |
128 | have lingering references, a controller may not show up immediately on | |
129 | the v2 hierarchy after the final umount of the previous hierarchy. | |
130 | Similarly, a controller should be fully disabled to be moved out of | |
131 | the unified hierarchy and it may take some time for the disabled | |
132 | controller to become available for other hierarchies; furthermore, due | |
133 | to inter-controller dependencies, other controllers may need to be | |
134 | disabled too. | |
135 | ||
136 | While useful for development and manual configurations, moving | |
137 | controllers dynamically between the v2 and other hierarchies is | |
138 | strongly discouraged for production use. It is recommended to decide | |
139 | the hierarchies and controller associations before starting using the | |
140 | controllers after system boot. | |
141 | ||
1619b6d4 JW |
142 | During transition to v2, system management software might still |
143 | automount the v1 cgroup filesystem and so hijack all controllers | |
144 | during boot, before manual intervention is possible. To make testing | |
145 | and experimenting easier, the kernel parameter cgroup_no_v1= allows | |
146 | disabling controllers in v1 and make them always available in v2. | |
147 | ||
6c292092 TH |
148 | |
149 | 2-2. Organizing Processes | |
150 | ||
151 | Initially, only the root cgroup exists to which all processes belong. | |
152 | A child cgroup can be created by creating a sub-directory. | |
153 | ||
154 | # mkdir $CGROUP_NAME | |
155 | ||
156 | A given cgroup may have multiple child cgroups forming a tree | |
157 | structure. Each cgroup has a read-writable interface file | |
158 | "cgroup.procs". When read, it lists the PIDs of all processes which | |
159 | belong to the cgroup one-per-line. The PIDs are not ordered and the | |
160 | same PID may show up more than once if the process got moved to | |
161 | another cgroup and then back or the PID got recycled while reading. | |
162 | ||
163 | A process can be migrated into a cgroup by writing its PID to the | |
164 | target cgroup's "cgroup.procs" file. Only one process can be migrated | |
165 | on a single write(2) call. If a process is composed of multiple | |
166 | threads, writing the PID of any thread migrates all threads of the | |
167 | process. | |
168 | ||
169 | When a process forks a child process, the new process is born into the | |
170 | cgroup that the forking process belongs to at the time of the | |
171 | operation. After exit, a process stays associated with the cgroup | |
172 | that it belonged to at the time of exit until it's reaped; however, a | |
173 | zombie process does not appear in "cgroup.procs" and thus can't be | |
174 | moved to another cgroup. | |
175 | ||
176 | A cgroup which doesn't have any children or live processes can be | |
177 | destroyed by removing the directory. Note that a cgroup which doesn't | |
178 | have any children and is associated only with zombie processes is | |
179 | considered empty and can be removed. | |
180 | ||
181 | # rmdir $CGROUP_NAME | |
182 | ||
183 | "/proc/$PID/cgroup" lists a process's cgroup membership. If legacy | |
184 | cgroup is in use in the system, this file may contain multiple lines, | |
185 | one for each hierarchy. The entry for cgroup v2 is always in the | |
186 | format "0::$PATH". | |
187 | ||
188 | # cat /proc/842/cgroup | |
189 | ... | |
190 | 0::/test-cgroup/test-cgroup-nested | |
191 | ||
192 | If the process becomes a zombie and the cgroup it was associated with | |
193 | is removed subsequently, " (deleted)" is appended to the path. | |
194 | ||
195 | # cat /proc/842/cgroup | |
196 | ... | |
197 | 0::/test-cgroup/test-cgroup-nested (deleted) | |
198 | ||
199 | ||
200 | 2-3. [Un]populated Notification | |
201 | ||
202 | Each non-root cgroup has a "cgroup.events" file which contains | |
203 | "populated" field indicating whether the cgroup's sub-hierarchy has | |
204 | live processes in it. Its value is 0 if there is no live process in | |
205 | the cgroup and its descendants; otherwise, 1. poll and [id]notify | |
206 | events are triggered when the value changes. This can be used, for | |
207 | example, to start a clean-up operation after all processes of a given | |
208 | sub-hierarchy have exited. The populated state updates and | |
209 | notifications are recursive. Consider the following sub-hierarchy | |
210 | where the numbers in the parentheses represent the numbers of processes | |
211 | in each cgroup. | |
212 | ||
213 | A(4) - B(0) - C(1) | |
214 | \ D(0) | |
215 | ||
216 | A, B and C's "populated" fields would be 1 while D's 0. After the one | |
217 | process in C exits, B and C's "populated" fields would flip to "0" and | |
218 | file modified events will be generated on the "cgroup.events" files of | |
219 | both cgroups. | |
220 | ||
221 | ||
222 | 2-4. Controlling Controllers | |
223 | ||
224 | 2-4-1. Enabling and Disabling | |
225 | ||
226 | Each cgroup has a "cgroup.controllers" file which lists all | |
227 | controllers available for the cgroup to enable. | |
228 | ||
229 | # cat cgroup.controllers | |
230 | cpu io memory | |
231 | ||
232 | No controller is enabled by default. Controllers can be enabled and | |
233 | disabled by writing to the "cgroup.subtree_control" file. | |
234 | ||
235 | # echo "+cpu +memory -io" > cgroup.subtree_control | |
236 | ||
237 | Only controllers which are listed in "cgroup.controllers" can be | |
238 | enabled. When multiple operations are specified as above, either they | |
239 | all succeed or fail. If multiple operations on the same controller | |
240 | are specified, the last one is effective. | |
241 | ||
242 | Enabling a controller in a cgroup indicates that the distribution of | |
243 | the target resource across its immediate children will be controlled. | |
244 | Consider the following sub-hierarchy. The enabled controllers are | |
245 | listed in parentheses. | |
246 | ||
247 | A(cpu,memory) - B(memory) - C() | |
248 | \ D() | |
249 | ||
250 | As A has "cpu" and "memory" enabled, A will control the distribution | |
251 | of CPU cycles and memory to its children, in this case, B. As B has | |
252 | "memory" enabled but not "CPU", C and D will compete freely on CPU | |
253 | cycles but their division of memory available to B will be controlled. | |
254 | ||
255 | As a controller regulates the distribution of the target resource to | |
256 | the cgroup's children, enabling it creates the controller's interface | |
257 | files in the child cgroups. In the above example, enabling "cpu" on B | |
258 | would create the "cpu." prefixed controller interface files in C and | |
259 | D. Likewise, disabling "memory" from B would remove the "memory." | |
260 | prefixed controller interface files from C and D. This means that the | |
261 | controller interface files - anything which doesn't start with | |
262 | "cgroup." are owned by the parent rather than the cgroup itself. | |
263 | ||
264 | ||
265 | 2-4-2. Top-down Constraint | |
266 | ||
267 | Resources are distributed top-down and a cgroup can further distribute | |
268 | a resource only if the resource has been distributed to it from the | |
269 | parent. This means that all non-root "cgroup.subtree_control" files | |
270 | can only contain controllers which are enabled in the parent's | |
271 | "cgroup.subtree_control" file. A controller can be enabled only if | |
272 | the parent has the controller enabled and a controller can't be | |
273 | disabled if one or more children have it enabled. | |
274 | ||
275 | ||
276 | 2-4-3. No Internal Process Constraint | |
277 | ||
278 | Non-root cgroups can only distribute resources to their children when | |
279 | they don't have any processes of their own. In other words, only | |
280 | cgroups which don't contain any processes can have controllers enabled | |
281 | in their "cgroup.subtree_control" files. | |
282 | ||
283 | This guarantees that, when a controller is looking at the part of the | |
284 | hierarchy which has it enabled, processes are always only on the | |
285 | leaves. This rules out situations where child cgroups compete against | |
286 | internal processes of the parent. | |
287 | ||
288 | The root cgroup is exempt from this restriction. Root contains | |
289 | processes and anonymous resource consumption which can't be associated | |
290 | with any other cgroups and requires special treatment from most | |
291 | controllers. How resource consumption in the root cgroup is governed | |
292 | is up to each controller. | |
293 | ||
294 | Note that the restriction doesn't get in the way if there is no | |
295 | enabled controller in the cgroup's "cgroup.subtree_control". This is | |
296 | important as otherwise it wouldn't be possible to create children of a | |
297 | populated cgroup. To control resource distribution of a cgroup, the | |
298 | cgroup must create children and transfer all its processes to the | |
299 | children before enabling controllers in its "cgroup.subtree_control" | |
300 | file. | |
301 | ||
302 | ||
303 | 2-5. Delegation | |
304 | ||
305 | 2-5-1. Model of Delegation | |
306 | ||
307 | A cgroup can be delegated to a less privileged user by granting write | |
308 | access of the directory and its "cgroup.procs" file to the user. Note | |
309 | that resource control interface files in a given directory control the | |
310 | distribution of the parent's resources and thus must not be delegated | |
311 | along with the directory. | |
312 | ||
313 | Once delegated, the user can build sub-hierarchy under the directory, | |
314 | organize processes as it sees fit and further distribute the resources | |
315 | it received from the parent. The limits and other settings of all | |
316 | resource controllers are hierarchical and regardless of what happens | |
317 | in the delegated sub-hierarchy, nothing can escape the resource | |
318 | restrictions imposed by the parent. | |
319 | ||
320 | Currently, cgroup doesn't impose any restrictions on the number of | |
321 | cgroups in or nesting depth of a delegated sub-hierarchy; however, | |
322 | this may be limited explicitly in the future. | |
323 | ||
324 | ||
325 | 2-5-2. Delegation Containment | |
326 | ||
327 | A delegated sub-hierarchy is contained in the sense that processes | |
328 | can't be moved into or out of the sub-hierarchy by the delegatee. For | |
329 | a process with a non-root euid to migrate a target process into a | |
330 | cgroup by writing its PID to the "cgroup.procs" file, the following | |
331 | conditions must be met. | |
332 | ||
333 | - The writer's euid must match either uid or suid of the target process. | |
334 | ||
335 | - The writer must have write access to the "cgroup.procs" file. | |
336 | ||
337 | - The writer must have write access to the "cgroup.procs" file of the | |
338 | common ancestor of the source and destination cgroups. | |
339 | ||
340 | The above three constraints ensure that while a delegatee may migrate | |
341 | processes around freely in the delegated sub-hierarchy it can't pull | |
342 | in from or push out to outside the sub-hierarchy. | |
343 | ||
344 | For an example, let's assume cgroups C0 and C1 have been delegated to | |
345 | user U0 who created C00, C01 under C0 and C10 under C1 as follows and | |
346 | all processes under C0 and C1 belong to U0. | |
347 | ||
348 | ~~~~~~~~~~~~~ - C0 - C00 | |
349 | ~ cgroup ~ \ C01 | |
350 | ~ hierarchy ~ | |
351 | ~~~~~~~~~~~~~ - C1 - C10 | |
352 | ||
353 | Let's also say U0 wants to write the PID of a process which is | |
354 | currently in C10 into "C00/cgroup.procs". U0 has write access to the | |
355 | file and uid match on the process; however, the common ancestor of the | |
356 | source cgroup C10 and the destination cgroup C00 is above the points | |
357 | of delegation and U0 would not have write access to its "cgroup.procs" | |
358 | files and thus the write will be denied with -EACCES. | |
359 | ||
360 | ||
361 | 2-6. Guidelines | |
362 | ||
363 | 2-6-1. Organize Once and Control | |
364 | ||
365 | Migrating a process across cgroups is a relatively expensive operation | |
366 | and stateful resources such as memory are not moved together with the | |
367 | process. This is an explicit design decision as there often exist | |
368 | inherent trade-offs between migration and various hot paths in terms | |
369 | of synchronization cost. | |
370 | ||
371 | As such, migrating processes across cgroups frequently as a means to | |
372 | apply different resource restrictions is discouraged. A workload | |
373 | should be assigned to a cgroup according to the system's logical and | |
374 | resource structure once on start-up. Dynamic adjustments to resource | |
375 | distribution can be made by changing controller configuration through | |
376 | the interface files. | |
377 | ||
378 | ||
379 | 2-6-2. Avoid Name Collisions | |
380 | ||
381 | Interface files for a cgroup and its children cgroups occupy the same | |
382 | directory and it is possible to create children cgroups which collide | |
383 | with interface files. | |
384 | ||
385 | All cgroup core interface files are prefixed with "cgroup." and each | |
386 | controller's interface files are prefixed with the controller name and | |
387 | a dot. A controller's name is composed of lower case alphabets and | |
388 | '_'s but never begins with an '_' so it can be used as the prefix | |
389 | character for collision avoidance. Also, interface file names won't | |
390 | start or end with terms which are often used in categorizing workloads | |
391 | such as job, service, slice, unit or workload. | |
392 | ||
393 | cgroup doesn't do anything to prevent name collisions and it's the | |
394 | user's responsibility to avoid them. | |
395 | ||
396 | ||
397 | 3. Resource Distribution Models | |
398 | ||
399 | cgroup controllers implement several resource distribution schemes | |
400 | depending on the resource type and expected use cases. This section | |
401 | describes major schemes in use along with their expected behaviors. | |
402 | ||
403 | ||
404 | 3-1. Weights | |
405 | ||
406 | A parent's resource is distributed by adding up the weights of all | |
407 | active children and giving each the fraction matching the ratio of its | |
408 | weight against the sum. As only children which can make use of the | |
409 | resource at the moment participate in the distribution, this is | |
410 | work-conserving. Due to the dynamic nature, this model is usually | |
411 | used for stateless resources. | |
412 | ||
413 | All weights are in the range [1, 10000] with the default at 100. This | |
414 | allows symmetric multiplicative biases in both directions at fine | |
415 | enough granularity while staying in the intuitive range. | |
416 | ||
417 | As long as the weight is in range, all configuration combinations are | |
418 | valid and there is no reason to reject configuration changes or | |
419 | process migrations. | |
420 | ||
421 | "cpu.weight" proportionally distributes CPU cycles to active children | |
422 | and is an example of this type. | |
423 | ||
424 | ||
425 | 3-2. Limits | |
426 | ||
427 | A child can only consume upto the configured amount of the resource. | |
428 | Limits can be over-committed - the sum of the limits of children can | |
429 | exceed the amount of resource available to the parent. | |
430 | ||
431 | Limits are in the range [0, max] and defaults to "max", which is noop. | |
432 | ||
433 | As limits can be over-committed, all configuration combinations are | |
434 | valid and there is no reason to reject configuration changes or | |
435 | process migrations. | |
436 | ||
437 | "io.max" limits the maximum BPS and/or IOPS that a cgroup can consume | |
438 | on an IO device and is an example of this type. | |
439 | ||
440 | ||
441 | 3-3. Protections | |
442 | ||
443 | A cgroup is protected to be allocated upto the configured amount of | |
444 | the resource if the usages of all its ancestors are under their | |
445 | protected levels. Protections can be hard guarantees or best effort | |
446 | soft boundaries. Protections can also be over-committed in which case | |
447 | only upto the amount available to the parent is protected among | |
448 | children. | |
449 | ||
450 | Protections are in the range [0, max] and defaults to 0, which is | |
451 | noop. | |
452 | ||
453 | As protections can be over-committed, all configuration combinations | |
454 | are valid and there is no reason to reject configuration changes or | |
455 | process migrations. | |
456 | ||
457 | "memory.low" implements best-effort memory protection and is an | |
458 | example of this type. | |
459 | ||
460 | ||
461 | 3-4. Allocations | |
462 | ||
463 | A cgroup is exclusively allocated a certain amount of a finite | |
464 | resource. Allocations can't be over-committed - the sum of the | |
465 | allocations of children can not exceed the amount of resource | |
466 | available to the parent. | |
467 | ||
468 | Allocations are in the range [0, max] and defaults to 0, which is no | |
469 | resource. | |
470 | ||
471 | As allocations can't be over-committed, some configuration | |
472 | combinations are invalid and should be rejected. Also, if the | |
473 | resource is mandatory for execution of processes, process migrations | |
474 | may be rejected. | |
475 | ||
476 | "cpu.rt.max" hard-allocates realtime slices and is an example of this | |
477 | type. | |
478 | ||
479 | ||
480 | 4. Interface Files | |
481 | ||
482 | 4-1. Format | |
483 | ||
484 | All interface files should be in one of the following formats whenever | |
485 | possible. | |
486 | ||
487 | New-line separated values | |
488 | (when only one value can be written at once) | |
489 | ||
490 | VAL0\n | |
491 | VAL1\n | |
492 | ... | |
493 | ||
494 | Space separated values | |
495 | (when read-only or multiple values can be written at once) | |
496 | ||
497 | VAL0 VAL1 ...\n | |
498 | ||
499 | Flat keyed | |
500 | ||
501 | KEY0 VAL0\n | |
502 | KEY1 VAL1\n | |
503 | ... | |
504 | ||
505 | Nested keyed | |
506 | ||
507 | KEY0 SUB_KEY0=VAL00 SUB_KEY1=VAL01... | |
508 | KEY1 SUB_KEY0=VAL10 SUB_KEY1=VAL11... | |
509 | ... | |
510 | ||
511 | For a writable file, the format for writing should generally match | |
512 | reading; however, controllers may allow omitting later fields or | |
513 | implement restricted shortcuts for most common use cases. | |
514 | ||
515 | For both flat and nested keyed files, only the values for a single key | |
516 | can be written at a time. For nested keyed files, the sub key pairs | |
517 | may be specified in any order and not all pairs have to be specified. | |
518 | ||
519 | ||
520 | 4-2. Conventions | |
521 | ||
522 | - Settings for a single feature should be contained in a single file. | |
523 | ||
524 | - The root cgroup should be exempt from resource control and thus | |
525 | shouldn't have resource control interface files. Also, | |
526 | informational files on the root cgroup which end up showing global | |
527 | information available elsewhere shouldn't exist. | |
528 | ||
529 | - If a controller implements weight based resource distribution, its | |
530 | interface file should be named "weight" and have the range [1, | |
531 | 10000] with 100 as the default. The values are chosen to allow | |
532 | enough and symmetric bias in both directions while keeping it | |
533 | intuitive (the default is 100%). | |
534 | ||
535 | - If a controller implements an absolute resource guarantee and/or | |
536 | limit, the interface files should be named "min" and "max" | |
537 | respectively. If a controller implements best effort resource | |
538 | guarantee and/or limit, the interface files should be named "low" | |
539 | and "high" respectively. | |
540 | ||
541 | In the above four control files, the special token "max" should be | |
542 | used to represent upward infinity for both reading and writing. | |
543 | ||
544 | - If a setting has a configurable default value and keyed specific | |
545 | overrides, the default entry should be keyed with "default" and | |
546 | appear as the first entry in the file. | |
547 | ||
548 | The default value can be updated by writing either "default $VAL" or | |
549 | "$VAL". | |
550 | ||
551 | When writing to update a specific override, "default" can be used as | |
552 | the value to indicate removal of the override. Override entries | |
553 | with "default" as the value must not appear when read. | |
554 | ||
555 | For example, a setting which is keyed by major:minor device numbers | |
556 | with integer values may look like the following. | |
557 | ||
558 | # cat cgroup-example-interface-file | |
559 | default 150 | |
560 | 8:0 300 | |
561 | ||
562 | The default value can be updated by | |
563 | ||
564 | # echo 125 > cgroup-example-interface-file | |
565 | ||
566 | or | |
567 | ||
568 | # echo "default 125" > cgroup-example-interface-file | |
569 | ||
570 | An override can be set by | |
571 | ||
572 | # echo "8:16 170" > cgroup-example-interface-file | |
573 | ||
574 | and cleared by | |
575 | ||
576 | # echo "8:0 default" > cgroup-example-interface-file | |
577 | # cat cgroup-example-interface-file | |
578 | default 125 | |
579 | 8:16 170 | |
580 | ||
581 | - For events which are not very high frequency, an interface file | |
582 | "events" should be created which lists event key value pairs. | |
583 | Whenever a notifiable event happens, file modified event should be | |
584 | generated on the file. | |
585 | ||
586 | ||
587 | 4-3. Core Interface Files | |
588 | ||
589 | All cgroup core files are prefixed with "cgroup." | |
590 | ||
591 | cgroup.procs | |
592 | ||
593 | A read-write new-line separated values file which exists on | |
594 | all cgroups. | |
595 | ||
596 | When read, it lists the PIDs of all processes which belong to | |
597 | the cgroup one-per-line. The PIDs are not ordered and the | |
598 | same PID may show up more than once if the process got moved | |
599 | to another cgroup and then back or the PID got recycled while | |
600 | reading. | |
601 | ||
602 | A PID can be written to migrate the process associated with | |
603 | the PID to the cgroup. The writer should match all of the | |
604 | following conditions. | |
605 | ||
606 | - Its euid is either root or must match either uid or suid of | |
607 | the target process. | |
608 | ||
609 | - It must have write access to the "cgroup.procs" file. | |
610 | ||
611 | - It must have write access to the "cgroup.procs" file of the | |
612 | common ancestor of the source and destination cgroups. | |
613 | ||
614 | When delegating a sub-hierarchy, write access to this file | |
615 | should be granted along with the containing directory. | |
616 | ||
617 | cgroup.controllers | |
618 | ||
619 | A read-only space separated values file which exists on all | |
620 | cgroups. | |
621 | ||
622 | It shows space separated list of all controllers available to | |
623 | the cgroup. The controllers are not ordered. | |
624 | ||
625 | cgroup.subtree_control | |
626 | ||
627 | A read-write space separated values file which exists on all | |
628 | cgroups. Starts out empty. | |
629 | ||
630 | When read, it shows space separated list of the controllers | |
631 | which are enabled to control resource distribution from the | |
632 | cgroup to its children. | |
633 | ||
634 | Space separated list of controllers prefixed with '+' or '-' | |
635 | can be written to enable or disable controllers. A controller | |
636 | name prefixed with '+' enables the controller and '-' | |
637 | disables. If a controller appears more than once on the list, | |
638 | the last one is effective. When multiple enable and disable | |
639 | operations are specified, either all succeed or all fail. | |
640 | ||
641 | cgroup.events | |
642 | ||
643 | A read-only flat-keyed file which exists on non-root cgroups. | |
644 | The following entries are defined. Unless specified | |
645 | otherwise, a value change in this file generates a file | |
646 | modified event. | |
647 | ||
648 | populated | |
649 | ||
650 | 1 if the cgroup or its descendants contains any live | |
651 | processes; otherwise, 0. | |
652 | ||
653 | ||
654 | 5. Controllers | |
655 | ||
656 | 5-1. CPU | |
657 | ||
658 | [NOTE: The interface for the cpu controller hasn't been merged yet] | |
659 | ||
660 | The "cpu" controllers regulates distribution of CPU cycles. This | |
661 | controller implements weight and absolute bandwidth limit models for | |
662 | normal scheduling policy and absolute bandwidth allocation model for | |
663 | realtime scheduling policy. | |
664 | ||
665 | ||
666 | 5-1-1. CPU Interface Files | |
667 | ||
668 | All time durations are in microseconds. | |
669 | ||
670 | cpu.stat | |
671 | ||
672 | A read-only flat-keyed file which exists on non-root cgroups. | |
673 | ||
674 | It reports the following six stats. | |
675 | ||
676 | usage_usec | |
677 | user_usec | |
678 | system_usec | |
679 | nr_periods | |
680 | nr_throttled | |
681 | throttled_usec | |
682 | ||
683 | cpu.weight | |
684 | ||
685 | A read-write single value file which exists on non-root | |
686 | cgroups. The default is "100". | |
687 | ||
688 | The weight in the range [1, 10000]. | |
689 | ||
690 | cpu.max | |
691 | ||
692 | A read-write two value file which exists on non-root cgroups. | |
693 | The default is "max 100000". | |
694 | ||
695 | The maximum bandwidth limit. It's in the following format. | |
696 | ||
697 | $MAX $PERIOD | |
698 | ||
699 | which indicates that the group may consume upto $MAX in each | |
700 | $PERIOD duration. "max" for $MAX indicates no limit. If only | |
701 | one number is written, $MAX is updated. | |
702 | ||
703 | cpu.rt.max | |
704 | ||
705 | [NOTE: The semantics of this file is still under discussion and the | |
706 | interface hasn't been merged yet] | |
707 | ||
708 | A read-write two value file which exists on all cgroups. | |
709 | The default is "0 100000". | |
710 | ||
711 | The maximum realtime runtime allocation. Over-committing | |
712 | configurations are disallowed and process migrations are | |
713 | rejected if not enough bandwidth is available. It's in the | |
714 | following format. | |
715 | ||
716 | $MAX $PERIOD | |
717 | ||
718 | which indicates that the group may consume upto $MAX in each | |
719 | $PERIOD duration. If only one number is written, $MAX is | |
720 | updated. | |
721 | ||
722 | ||
723 | 5-2. Memory | |
724 | ||
725 | The "memory" controller regulates distribution of memory. Memory is | |
726 | stateful and implements both limit and protection models. Due to the | |
727 | intertwining between memory usage and reclaim pressure and the | |
728 | stateful nature of memory, the distribution model is relatively | |
729 | complex. | |
730 | ||
731 | While not completely water-tight, all major memory usages by a given | |
732 | cgroup are tracked so that the total memory consumption can be | |
733 | accounted and controlled to a reasonable extent. Currently, the | |
734 | following types of memory usages are tracked. | |
735 | ||
736 | - Userland memory - page cache and anonymous memory. | |
737 | ||
738 | - Kernel data structures such as dentries and inodes. | |
739 | ||
740 | - TCP socket buffers. | |
741 | ||
742 | The above list may expand in the future for better coverage. | |
743 | ||
744 | ||
745 | 5-2-1. Memory Interface Files | |
746 | ||
747 | All memory amounts are in bytes. If a value which is not aligned to | |
748 | PAGE_SIZE is written, the value may be rounded up to the closest | |
749 | PAGE_SIZE multiple when read back. | |
750 | ||
751 | memory.current | |
752 | ||
753 | A read-only single value file which exists on non-root | |
754 | cgroups. | |
755 | ||
756 | The total amount of memory currently being used by the cgroup | |
757 | and its descendants. | |
758 | ||
759 | memory.low | |
760 | ||
761 | A read-write single value file which exists on non-root | |
762 | cgroups. The default is "0". | |
763 | ||
764 | Best-effort memory protection. If the memory usages of a | |
765 | cgroup and all its ancestors are below their low boundaries, | |
766 | the cgroup's memory won't be reclaimed unless memory can be | |
767 | reclaimed from unprotected cgroups. | |
768 | ||
769 | Putting more memory than generally available under this | |
770 | protection is discouraged. | |
771 | ||
772 | memory.high | |
773 | ||
774 | A read-write single value file which exists on non-root | |
775 | cgroups. The default is "max". | |
776 | ||
777 | Memory usage throttle limit. This is the main mechanism to | |
778 | control memory usage of a cgroup. If a cgroup's usage goes | |
779 | over the high boundary, the processes of the cgroup are | |
780 | throttled and put under heavy reclaim pressure. | |
781 | ||
782 | Going over the high limit never invokes the OOM killer and | |
783 | under extreme conditions the limit may be breached. | |
784 | ||
785 | memory.max | |
786 | ||
787 | A read-write single value file which exists on non-root | |
788 | cgroups. The default is "max". | |
789 | ||
790 | Memory usage hard limit. This is the final protection | |
791 | mechanism. If a cgroup's memory usage reaches this limit and | |
792 | can't be reduced, the OOM killer is invoked in the cgroup. | |
793 | Under certain circumstances, the usage may go over the limit | |
794 | temporarily. | |
795 | ||
796 | This is the ultimate protection mechanism. As long as the | |
797 | high limit is used and monitored properly, this limit's | |
798 | utility is limited to providing the final safety net. | |
799 | ||
800 | memory.events | |
801 | ||
802 | A read-only flat-keyed file which exists on non-root cgroups. | |
803 | The following entries are defined. Unless specified | |
804 | otherwise, a value change in this file generates a file | |
805 | modified event. | |
806 | ||
807 | low | |
808 | ||
809 | The number of times the cgroup is reclaimed due to | |
810 | high memory pressure even though its usage is under | |
811 | the low boundary. This usually indicates that the low | |
812 | boundary is over-committed. | |
813 | ||
814 | high | |
815 | ||
816 | The number of times processes of the cgroup are | |
817 | throttled and routed to perform direct memory reclaim | |
818 | because the high memory boundary was exceeded. For a | |
819 | cgroup whose memory usage is capped by the high limit | |
820 | rather than global memory pressure, this event's | |
821 | occurrences are expected. | |
822 | ||
823 | max | |
824 | ||
825 | The number of times the cgroup's memory usage was | |
826 | about to go over the max boundary. If direct reclaim | |
827 | fails to bring it down, the OOM killer is invoked. | |
828 | ||
829 | oom | |
830 | ||
831 | The number of times the OOM killer has been invoked in | |
832 | the cgroup. This may not exactly match the number of | |
833 | processes killed but should generally be close. | |
834 | ||
587d9f72 JW |
835 | memory.stat |
836 | ||
837 | A read-only flat-keyed file which exists on non-root cgroups. | |
838 | ||
839 | This breaks down the cgroup's memory footprint into different | |
840 | types of memory, type-specific details, and other information | |
841 | on the state and past events of the memory management system. | |
842 | ||
843 | All memory amounts are in bytes. | |
844 | ||
845 | The entries are ordered to be human readable, and new entries | |
846 | can show up in the middle. Don't rely on items remaining in a | |
847 | fixed position; use the keys to look up specific values! | |
848 | ||
849 | anon | |
850 | ||
851 | Amount of memory used in anonymous mappings such as | |
852 | brk(), sbrk(), and mmap(MAP_ANONYMOUS) | |
853 | ||
854 | file | |
855 | ||
856 | Amount of memory used to cache filesystem data, | |
857 | including tmpfs and shared memory. | |
858 | ||
12580e4b VD |
859 | kernel_stack |
860 | ||
861 | Amount of memory allocated to kernel stacks. | |
862 | ||
27ee57c9 VD |
863 | slab |
864 | ||
865 | Amount of memory used for storing in-kernel data | |
866 | structures. | |
867 | ||
4758e198 JW |
868 | sock |
869 | ||
870 | Amount of memory used in network transmission buffers | |
871 | ||
587d9f72 JW |
872 | file_mapped |
873 | ||
874 | Amount of cached filesystem data mapped with mmap() | |
875 | ||
876 | file_dirty | |
877 | ||
878 | Amount of cached filesystem data that was modified but | |
879 | not yet written back to disk | |
880 | ||
881 | file_writeback | |
882 | ||
883 | Amount of cached filesystem data that was modified and | |
884 | is currently being written back to disk | |
885 | ||
886 | inactive_anon | |
887 | active_anon | |
888 | inactive_file | |
889 | active_file | |
890 | unevictable | |
891 | ||
892 | Amount of memory, swap-backed and filesystem-backed, | |
893 | on the internal memory management lists used by the | |
894 | page reclaim algorithm | |
895 | ||
27ee57c9 VD |
896 | slab_reclaimable |
897 | ||
898 | Part of "slab" that might be reclaimed, such as | |
899 | dentries and inodes. | |
900 | ||
901 | slab_unreclaimable | |
902 | ||
903 | Part of "slab" that cannot be reclaimed on memory | |
904 | pressure. | |
905 | ||
587d9f72 JW |
906 | pgfault |
907 | ||
908 | Total number of page faults incurred | |
909 | ||
910 | pgmajfault | |
911 | ||
912 | Number of major page faults incurred | |
913 | ||
3e24b19d VD |
914 | memory.swap.current |
915 | ||
916 | A read-only single value file which exists on non-root | |
917 | cgroups. | |
918 | ||
919 | The total amount of swap currently being used by the cgroup | |
920 | and its descendants. | |
921 | ||
922 | memory.swap.max | |
923 | ||
924 | A read-write single value file which exists on non-root | |
925 | cgroups. The default is "max". | |
926 | ||
927 | Swap usage hard limit. If a cgroup's swap usage reaches this | |
928 | limit, anonymous meomry of the cgroup will not be swapped out. | |
929 | ||
6c292092 | 930 | |
6c83e6cb | 931 | 5-2-2. Usage Guidelines |
6c292092 TH |
932 | |
933 | "memory.high" is the main mechanism to control memory usage. | |
934 | Over-committing on high limit (sum of high limits > available memory) | |
935 | and letting global memory pressure to distribute memory according to | |
936 | usage is a viable strategy. | |
937 | ||
938 | Because breach of the high limit doesn't trigger the OOM killer but | |
939 | throttles the offending cgroup, a management agent has ample | |
940 | opportunities to monitor and take appropriate actions such as granting | |
941 | more memory or terminating the workload. | |
942 | ||
943 | Determining whether a cgroup has enough memory is not trivial as | |
944 | memory usage doesn't indicate whether the workload can benefit from | |
945 | more memory. For example, a workload which writes data received from | |
946 | network to a file can use all available memory but can also operate as | |
947 | performant with a small amount of memory. A measure of memory | |
948 | pressure - how much the workload is being impacted due to lack of | |
949 | memory - is necessary to determine whether a workload needs more | |
950 | memory; unfortunately, memory pressure monitoring mechanism isn't | |
951 | implemented yet. | |
952 | ||
953 | ||
954 | 5-2-3. Memory Ownership | |
955 | ||
956 | A memory area is charged to the cgroup which instantiated it and stays | |
957 | charged to the cgroup until the area is released. Migrating a process | |
958 | to a different cgroup doesn't move the memory usages that it | |
959 | instantiated while in the previous cgroup to the new cgroup. | |
960 | ||
961 | A memory area may be used by processes belonging to different cgroups. | |
962 | To which cgroup the area will be charged is in-deterministic; however, | |
963 | over time, the memory area is likely to end up in a cgroup which has | |
964 | enough memory allowance to avoid high reclaim pressure. | |
965 | ||
966 | If a cgroup sweeps a considerable amount of memory which is expected | |
967 | to be accessed repeatedly by other cgroups, it may make sense to use | |
968 | POSIX_FADV_DONTNEED to relinquish the ownership of memory areas | |
969 | belonging to the affected files to ensure correct memory ownership. | |
970 | ||
971 | ||
972 | 5-3. IO | |
973 | ||
974 | The "io" controller regulates the distribution of IO resources. This | |
975 | controller implements both weight based and absolute bandwidth or IOPS | |
976 | limit distribution; however, weight based distribution is available | |
977 | only if cfq-iosched is in use and neither scheme is available for | |
978 | blk-mq devices. | |
979 | ||
980 | ||
981 | 5-3-1. IO Interface Files | |
982 | ||
983 | io.stat | |
984 | ||
985 | A read-only nested-keyed file which exists on non-root | |
986 | cgroups. | |
987 | ||
988 | Lines are keyed by $MAJ:$MIN device numbers and not ordered. | |
989 | The following nested keys are defined. | |
990 | ||
991 | rbytes Bytes read | |
992 | wbytes Bytes written | |
993 | rios Number of read IOs | |
994 | wios Number of write IOs | |
995 | ||
996 | An example read output follows. | |
997 | ||
998 | 8:16 rbytes=1459200 wbytes=314773504 rios=192 wios=353 | |
999 | 8:0 rbytes=90430464 wbytes=299008000 rios=8950 wios=1252 | |
1000 | ||
1001 | io.weight | |
1002 | ||
1003 | A read-write flat-keyed file which exists on non-root cgroups. | |
1004 | The default is "default 100". | |
1005 | ||
1006 | The first line is the default weight applied to devices | |
1007 | without specific override. The rest are overrides keyed by | |
1008 | $MAJ:$MIN device numbers and not ordered. The weights are in | |
1009 | the range [1, 10000] and specifies the relative amount IO time | |
1010 | the cgroup can use in relation to its siblings. | |
1011 | ||
1012 | The default weight can be updated by writing either "default | |
1013 | $WEIGHT" or simply "$WEIGHT". Overrides can be set by writing | |
1014 | "$MAJ:$MIN $WEIGHT" and unset by writing "$MAJ:$MIN default". | |
1015 | ||
1016 | An example read output follows. | |
1017 | ||
1018 | default 100 | |
1019 | 8:16 200 | |
1020 | 8:0 50 | |
1021 | ||
1022 | io.max | |
1023 | ||
1024 | A read-write nested-keyed file which exists on non-root | |
1025 | cgroups. | |
1026 | ||
1027 | BPS and IOPS based IO limit. Lines are keyed by $MAJ:$MIN | |
1028 | device numbers and not ordered. The following nested keys are | |
1029 | defined. | |
1030 | ||
1031 | rbps Max read bytes per second | |
1032 | wbps Max write bytes per second | |
1033 | riops Max read IO operations per second | |
1034 | wiops Max write IO operations per second | |
1035 | ||
1036 | When writing, any number of nested key-value pairs can be | |
1037 | specified in any order. "max" can be specified as the value | |
1038 | to remove a specific limit. If the same key is specified | |
1039 | multiple times, the outcome is undefined. | |
1040 | ||
1041 | BPS and IOPS are measured in each IO direction and IOs are | |
1042 | delayed if limit is reached. Temporary bursts are allowed. | |
1043 | ||
1044 | Setting read limit at 2M BPS and write at 120 IOPS for 8:16. | |
1045 | ||
1046 | echo "8:16 rbps=2097152 wiops=120" > io.max | |
1047 | ||
1048 | Reading returns the following. | |
1049 | ||
1050 | 8:16 rbps=2097152 wbps=max riops=max wiops=120 | |
1051 | ||
1052 | Write IOPS limit can be removed by writing the following. | |
1053 | ||
1054 | echo "8:16 wiops=max" > io.max | |
1055 | ||
1056 | Reading now returns the following. | |
1057 | ||
1058 | 8:16 rbps=2097152 wbps=max riops=max wiops=max | |
1059 | ||
1060 | ||
1061 | 5-3-2. Writeback | |
1062 | ||
1063 | Page cache is dirtied through buffered writes and shared mmaps and | |
1064 | written asynchronously to the backing filesystem by the writeback | |
1065 | mechanism. Writeback sits between the memory and IO domains and | |
1066 | regulates the proportion of dirty memory by balancing dirtying and | |
1067 | write IOs. | |
1068 | ||
1069 | The io controller, in conjunction with the memory controller, | |
1070 | implements control of page cache writeback IOs. The memory controller | |
1071 | defines the memory domain that dirty memory ratio is calculated and | |
1072 | maintained for and the io controller defines the io domain which | |
1073 | writes out dirty pages for the memory domain. Both system-wide and | |
1074 | per-cgroup dirty memory states are examined and the more restrictive | |
1075 | of the two is enforced. | |
1076 | ||
1077 | cgroup writeback requires explicit support from the underlying | |
1078 | filesystem. Currently, cgroup writeback is implemented on ext2, ext4 | |
1079 | and btrfs. On other filesystems, all writeback IOs are attributed to | |
1080 | the root cgroup. | |
1081 | ||
1082 | There are inherent differences in memory and writeback management | |
1083 | which affects how cgroup ownership is tracked. Memory is tracked per | |
1084 | page while writeback per inode. For the purpose of writeback, an | |
1085 | inode is assigned to a cgroup and all IO requests to write dirty pages | |
1086 | from the inode are attributed to that cgroup. | |
1087 | ||
1088 | As cgroup ownership for memory is tracked per page, there can be pages | |
1089 | which are associated with different cgroups than the one the inode is | |
1090 | associated with. These are called foreign pages. The writeback | |
1091 | constantly keeps track of foreign pages and, if a particular foreign | |
1092 | cgroup becomes the majority over a certain period of time, switches | |
1093 | the ownership of the inode to that cgroup. | |
1094 | ||
1095 | While this model is enough for most use cases where a given inode is | |
1096 | mostly dirtied by a single cgroup even when the main writing cgroup | |
1097 | changes over time, use cases where multiple cgroups write to a single | |
1098 | inode simultaneously are not supported well. In such circumstances, a | |
1099 | significant portion of IOs are likely to be attributed incorrectly. | |
1100 | As memory controller assigns page ownership on the first use and | |
1101 | doesn't update it until the page is released, even if writeback | |
1102 | strictly follows page ownership, multiple cgroups dirtying overlapping | |
1103 | areas wouldn't work as expected. It's recommended to avoid such usage | |
1104 | patterns. | |
1105 | ||
1106 | The sysctl knobs which affect writeback behavior are applied to cgroup | |
1107 | writeback as follows. | |
1108 | ||
1109 | vm.dirty_background_ratio | |
1110 | vm.dirty_ratio | |
1111 | ||
1112 | These ratios apply the same to cgroup writeback with the | |
1113 | amount of available memory capped by limits imposed by the | |
1114 | memory controller and system-wide clean memory. | |
1115 | ||
1116 | vm.dirty_background_bytes | |
1117 | vm.dirty_bytes | |
1118 | ||
1119 | For cgroup writeback, this is calculated into ratio against | |
1120 | total available memory and applied the same way as | |
1121 | vm.dirty[_background]_ratio. | |
1122 | ||
1123 | ||
20c56e59 HR |
1124 | 5-4. PID |
1125 | ||
1126 | The process number controller is used to allow a cgroup to stop any | |
1127 | new tasks from being fork()'d or clone()'d after a specified limit is | |
1128 | reached. | |
1129 | ||
1130 | The number of tasks in a cgroup can be exhausted in ways which other | |
1131 | controllers cannot prevent, thus warranting its own controller. For | |
1132 | example, a fork bomb is likely to exhaust the number of tasks before | |
1133 | hitting memory restrictions. | |
1134 | ||
1135 | Note that PIDs used in this controller refer to TIDs, process IDs as | |
1136 | used by the kernel. | |
1137 | ||
1138 | ||
1139 | 5-4-1. PID Interface Files | |
1140 | ||
1141 | pids.max | |
1142 | ||
1143 | A read-write single value file which exists on non-root cgroups. The | |
1144 | default is "max". | |
1145 | ||
1146 | Hard limit of number of processes. | |
1147 | ||
1148 | pids.current | |
1149 | ||
1150 | A read-only single value file which exists on all cgroups. | |
1151 | ||
1152 | The number of processes currently in the cgroup and its descendants. | |
1153 | ||
1154 | Organisational operations are not blocked by cgroup policies, so it is | |
1155 | possible to have pids.current > pids.max. This can be done by either | |
1156 | setting the limit to be smaller than pids.current, or attaching enough | |
1157 | processes to the cgroup such that pids.current is larger than | |
1158 | pids.max. However, it is not possible to violate a cgroup PID policy | |
1159 | through fork() or clone(). These will return -EAGAIN if the creation | |
1160 | of a new process would cause a cgroup policy to be violated. | |
1161 | ||
1162 | ||
d4021f6c SH |
1163 | 6. Namespace |
1164 | ||
1165 | 6-1. Basics | |
1166 | ||
1167 | cgroup namespace provides a mechanism to virtualize the view of the | |
1168 | "/proc/$PID/cgroup" file and cgroup mounts. The CLONE_NEWCGROUP clone | |
1169 | flag can be used with clone(2) and unshare(2) to create a new cgroup | |
1170 | namespace. The process running inside the cgroup namespace will have | |
1171 | its "/proc/$PID/cgroup" output restricted to cgroupns root. The | |
1172 | cgroupns root is the cgroup of the process at the time of creation of | |
1173 | the cgroup namespace. | |
1174 | ||
1175 | Without cgroup namespace, the "/proc/$PID/cgroup" file shows the | |
1176 | complete path of the cgroup of a process. In a container setup where | |
1177 | a set of cgroups and namespaces are intended to isolate processes the | |
1178 | "/proc/$PID/cgroup" file may leak potential system level information | |
1179 | to the isolated processes. For Example: | |
1180 | ||
1181 | # cat /proc/self/cgroup | |
1182 | 0::/batchjobs/container_id1 | |
1183 | ||
1184 | The path '/batchjobs/container_id1' can be considered as system-data | |
1185 | and undesirable to expose to the isolated processes. cgroup namespace | |
1186 | can be used to restrict visibility of this path. For example, before | |
1187 | creating a cgroup namespace, one would see: | |
1188 | ||
1189 | # ls -l /proc/self/ns/cgroup | |
1190 | lrwxrwxrwx 1 root root 0 2014-07-15 10:37 /proc/self/ns/cgroup -> cgroup:[4026531835] | |
1191 | # cat /proc/self/cgroup | |
1192 | 0::/batchjobs/container_id1 | |
1193 | ||
1194 | After unsharing a new namespace, the view changes. | |
1195 | ||
1196 | # ls -l /proc/self/ns/cgroup | |
1197 | lrwxrwxrwx 1 root root 0 2014-07-15 10:35 /proc/self/ns/cgroup -> cgroup:[4026532183] | |
1198 | # cat /proc/self/cgroup | |
1199 | 0::/ | |
1200 | ||
1201 | When some thread from a multi-threaded process unshares its cgroup | |
1202 | namespace, the new cgroupns gets applied to the entire process (all | |
1203 | the threads). This is natural for the v2 hierarchy; however, for the | |
1204 | legacy hierarchies, this may be unexpected. | |
1205 | ||
1206 | A cgroup namespace is alive as long as there are processes inside or | |
1207 | mounts pinning it. When the last usage goes away, the cgroup | |
1208 | namespace is destroyed. The cgroupns root and the actual cgroups | |
1209 | remain. | |
1210 | ||
1211 | ||
1212 | 6-2. The Root and Views | |
1213 | ||
1214 | The 'cgroupns root' for a cgroup namespace is the cgroup in which the | |
1215 | process calling unshare(2) is running. For example, if a process in | |
1216 | /batchjobs/container_id1 cgroup calls unshare, cgroup | |
1217 | /batchjobs/container_id1 becomes the cgroupns root. For the | |
1218 | init_cgroup_ns, this is the real root ('/') cgroup. | |
1219 | ||
1220 | The cgroupns root cgroup does not change even if the namespace creator | |
1221 | process later moves to a different cgroup. | |
1222 | ||
1223 | # ~/unshare -c # unshare cgroupns in some cgroup | |
1224 | # cat /proc/self/cgroup | |
1225 | 0::/ | |
1226 | # mkdir sub_cgrp_1 | |
1227 | # echo 0 > sub_cgrp_1/cgroup.procs | |
1228 | # cat /proc/self/cgroup | |
1229 | 0::/sub_cgrp_1 | |
1230 | ||
1231 | Each process gets its namespace-specific view of "/proc/$PID/cgroup" | |
1232 | ||
1233 | Processes running inside the cgroup namespace will be able to see | |
1234 | cgroup paths (in /proc/self/cgroup) only inside their root cgroup. | |
1235 | From within an unshared cgroupns: | |
1236 | ||
1237 | # sleep 100000 & | |
1238 | [1] 7353 | |
1239 | # echo 7353 > sub_cgrp_1/cgroup.procs | |
1240 | # cat /proc/7353/cgroup | |
1241 | 0::/sub_cgrp_1 | |
1242 | ||
1243 | From the initial cgroup namespace, the real cgroup path will be | |
1244 | visible: | |
1245 | ||
1246 | $ cat /proc/7353/cgroup | |
1247 | 0::/batchjobs/container_id1/sub_cgrp_1 | |
1248 | ||
1249 | From a sibling cgroup namespace (that is, a namespace rooted at a | |
1250 | different cgroup), the cgroup path relative to its own cgroup | |
1251 | namespace root will be shown. For instance, if PID 7353's cgroup | |
1252 | namespace root is at '/batchjobs/container_id2', then it will see | |
1253 | ||
1254 | # cat /proc/7353/cgroup | |
1255 | 0::/../container_id2/sub_cgrp_1 | |
1256 | ||
1257 | Note that the relative path always starts with '/' to indicate that | |
1258 | its relative to the cgroup namespace root of the caller. | |
1259 | ||
1260 | ||
1261 | 6-3. Migration and setns(2) | |
1262 | ||
1263 | Processes inside a cgroup namespace can move into and out of the | |
1264 | namespace root if they have proper access to external cgroups. For | |
1265 | example, from inside a namespace with cgroupns root at | |
1266 | /batchjobs/container_id1, and assuming that the global hierarchy is | |
1267 | still accessible inside cgroupns: | |
1268 | ||
1269 | # cat /proc/7353/cgroup | |
1270 | 0::/sub_cgrp_1 | |
1271 | # echo 7353 > batchjobs/container_id2/cgroup.procs | |
1272 | # cat /proc/7353/cgroup | |
1273 | 0::/../container_id2 | |
1274 | ||
1275 | Note that this kind of setup is not encouraged. A task inside cgroup | |
1276 | namespace should only be exposed to its own cgroupns hierarchy. | |
1277 | ||
1278 | setns(2) to another cgroup namespace is allowed when: | |
1279 | ||
1280 | (a) the process has CAP_SYS_ADMIN against its current user namespace | |
1281 | (b) the process has CAP_SYS_ADMIN against the target cgroup | |
1282 | namespace's userns | |
1283 | ||
1284 | No implicit cgroup changes happen with attaching to another cgroup | |
1285 | namespace. It is expected that the someone moves the attaching | |
1286 | process under the target cgroup namespace root. | |
1287 | ||
1288 | ||
1289 | 6-4. Interaction with Other Namespaces | |
1290 | ||
1291 | Namespace specific cgroup hierarchy can be mounted by a process | |
1292 | running inside a non-init cgroup namespace. | |
1293 | ||
1294 | # mount -t cgroup2 none $MOUNT_POINT | |
1295 | ||
1296 | This will mount the unified cgroup hierarchy with cgroupns root as the | |
1297 | filesystem root. The process needs CAP_SYS_ADMIN against its user and | |
1298 | mount namespaces. | |
1299 | ||
1300 | The virtualization of /proc/self/cgroup file combined with restricting | |
1301 | the view of cgroup hierarchy by namespace-private cgroupfs mount | |
1302 | provides a properly isolated cgroup view inside the container. | |
1303 | ||
1304 | ||
6c292092 TH |
1305 | P. Information on Kernel Programming |
1306 | ||
1307 | This section contains kernel programming information in the areas | |
1308 | where interacting with cgroup is necessary. cgroup core and | |
1309 | controllers are not covered. | |
1310 | ||
1311 | ||
1312 | P-1. Filesystem Support for Writeback | |
1313 | ||
1314 | A filesystem can support cgroup writeback by updating | |
1315 | address_space_operations->writepage[s]() to annotate bio's using the | |
1316 | following two functions. | |
1317 | ||
1318 | wbc_init_bio(@wbc, @bio) | |
1319 | ||
1320 | Should be called for each bio carrying writeback data and | |
1321 | associates the bio with the inode's owner cgroup. Can be | |
1322 | called anytime between bio allocation and submission. | |
1323 | ||
1324 | wbc_account_io(@wbc, @page, @bytes) | |
1325 | ||
1326 | Should be called for each data segment being written out. | |
1327 | While this function doesn't care exactly when it's called | |
1328 | during the writeback session, it's the easiest and most | |
1329 | natural to call it as data segments are added to a bio. | |
1330 | ||
1331 | With writeback bio's annotated, cgroup support can be enabled per | |
1332 | super_block by setting SB_I_CGROUPWB in ->s_iflags. This allows for | |
1333 | selective disabling of cgroup writeback support which is helpful when | |
1334 | certain filesystem features, e.g. journaled data mode, are | |
1335 | incompatible. | |
1336 | ||
1337 | wbc_init_bio() binds the specified bio to its cgroup. Depending on | |
1338 | the configuration, the bio may be executed at a lower priority and if | |
1339 | the writeback session is holding shared resources, e.g. a journal | |
1340 | entry, may lead to priority inversion. There is no one easy solution | |
1341 | for the problem. Filesystems can try to work around specific problem | |
1342 | cases by skipping wbc_init_bio() or using bio_associate_blkcg() | |
1343 | directly. | |
1344 | ||
1345 | ||
1346 | D. Deprecated v1 Core Features | |
1347 | ||
1348 | - Multiple hierarchies including named ones are not supported. | |
1349 | ||
1350 | - All mount options and remounting are not supported. | |
1351 | ||
1352 | - The "tasks" file is removed and "cgroup.procs" is not sorted. | |
1353 | ||
1354 | - "cgroup.clone_children" is removed. | |
1355 | ||
1356 | - /proc/cgroups is meaningless for v2. Use "cgroup.controllers" file | |
1357 | at the root instead. | |
1358 | ||
1359 | ||
1360 | R. Issues with v1 and Rationales for v2 | |
1361 | ||
1362 | R-1. Multiple Hierarchies | |
1363 | ||
1364 | cgroup v1 allowed an arbitrary number of hierarchies and each | |
1365 | hierarchy could host any number of controllers. While this seemed to | |
1366 | provide a high level of flexibility, it wasn't useful in practice. | |
1367 | ||
1368 | For example, as there is only one instance of each controller, utility | |
1369 | type controllers such as freezer which can be useful in all | |
1370 | hierarchies could only be used in one. The issue is exacerbated by | |
1371 | the fact that controllers couldn't be moved to another hierarchy once | |
1372 | hierarchies were populated. Another issue was that all controllers | |
1373 | bound to a hierarchy were forced to have exactly the same view of the | |
1374 | hierarchy. It wasn't possible to vary the granularity depending on | |
1375 | the specific controller. | |
1376 | ||
1377 | In practice, these issues heavily limited which controllers could be | |
1378 | put on the same hierarchy and most configurations resorted to putting | |
1379 | each controller on its own hierarchy. Only closely related ones, such | |
1380 | as the cpu and cpuacct controllers, made sense to be put on the same | |
1381 | hierarchy. This often meant that userland ended up managing multiple | |
1382 | similar hierarchies repeating the same steps on each hierarchy | |
1383 | whenever a hierarchy management operation was necessary. | |
1384 | ||
1385 | Furthermore, support for multiple hierarchies came at a steep cost. | |
1386 | It greatly complicated cgroup core implementation but more importantly | |
1387 | the support for multiple hierarchies restricted how cgroup could be | |
1388 | used in general and what controllers was able to do. | |
1389 | ||
1390 | There was no limit on how many hierarchies there might be, which meant | |
1391 | that a thread's cgroup membership couldn't be described in finite | |
1392 | length. The key might contain any number of entries and was unlimited | |
1393 | in length, which made it highly awkward to manipulate and led to | |
1394 | addition of controllers which existed only to identify membership, | |
1395 | which in turn exacerbated the original problem of proliferating number | |
1396 | of hierarchies. | |
1397 | ||
1398 | Also, as a controller couldn't have any expectation regarding the | |
1399 | topologies of hierarchies other controllers might be on, each | |
1400 | controller had to assume that all other controllers were attached to | |
1401 | completely orthogonal hierarchies. This made it impossible, or at | |
1402 | least very cumbersome, for controllers to cooperate with each other. | |
1403 | ||
1404 | In most use cases, putting controllers on hierarchies which are | |
1405 | completely orthogonal to each other isn't necessary. What usually is | |
1406 | called for is the ability to have differing levels of granularity | |
1407 | depending on the specific controller. In other words, hierarchy may | |
1408 | be collapsed from leaf towards root when viewed from specific | |
1409 | controllers. For example, a given configuration might not care about | |
1410 | how memory is distributed beyond a certain level while still wanting | |
1411 | to control how CPU cycles are distributed. | |
1412 | ||
1413 | ||
1414 | R-2. Thread Granularity | |
1415 | ||
1416 | cgroup v1 allowed threads of a process to belong to different cgroups. | |
1417 | This didn't make sense for some controllers and those controllers | |
1418 | ended up implementing different ways to ignore such situations but | |
1419 | much more importantly it blurred the line between API exposed to | |
1420 | individual applications and system management interface. | |
1421 | ||
1422 | Generally, in-process knowledge is available only to the process | |
1423 | itself; thus, unlike service-level organization of processes, | |
1424 | categorizing threads of a process requires active participation from | |
1425 | the application which owns the target process. | |
1426 | ||
1427 | cgroup v1 had an ambiguously defined delegation model which got abused | |
1428 | in combination with thread granularity. cgroups were delegated to | |
1429 | individual applications so that they can create and manage their own | |
1430 | sub-hierarchies and control resource distributions along them. This | |
1431 | effectively raised cgroup to the status of a syscall-like API exposed | |
1432 | to lay programs. | |
1433 | ||
1434 | First of all, cgroup has a fundamentally inadequate interface to be | |
1435 | exposed this way. For a process to access its own knobs, it has to | |
1436 | extract the path on the target hierarchy from /proc/self/cgroup, | |
1437 | construct the path by appending the name of the knob to the path, open | |
1438 | and then read and/or write to it. This is not only extremely clunky | |
1439 | and unusual but also inherently racy. There is no conventional way to | |
1440 | define transaction across the required steps and nothing can guarantee | |
1441 | that the process would actually be operating on its own sub-hierarchy. | |
1442 | ||
1443 | cgroup controllers implemented a number of knobs which would never be | |
1444 | accepted as public APIs because they were just adding control knobs to | |
1445 | system-management pseudo filesystem. cgroup ended up with interface | |
1446 | knobs which were not properly abstracted or refined and directly | |
1447 | revealed kernel internal details. These knobs got exposed to | |
1448 | individual applications through the ill-defined delegation mechanism | |
1449 | effectively abusing cgroup as a shortcut to implementing public APIs | |
1450 | without going through the required scrutiny. | |
1451 | ||
1452 | This was painful for both userland and kernel. Userland ended up with | |
1453 | misbehaving and poorly abstracted interfaces and kernel exposing and | |
1454 | locked into constructs inadvertently. | |
1455 | ||
1456 | ||
1457 | R-3. Competition Between Inner Nodes and Threads | |
1458 | ||
1459 | cgroup v1 allowed threads to be in any cgroups which created an | |
1460 | interesting problem where threads belonging to a parent cgroup and its | |
1461 | children cgroups competed for resources. This was nasty as two | |
1462 | different types of entities competed and there was no obvious way to | |
1463 | settle it. Different controllers did different things. | |
1464 | ||
1465 | The cpu controller considered threads and cgroups as equivalents and | |
1466 | mapped nice levels to cgroup weights. This worked for some cases but | |
1467 | fell flat when children wanted to be allocated specific ratios of CPU | |
1468 | cycles and the number of internal threads fluctuated - the ratios | |
1469 | constantly changed as the number of competing entities fluctuated. | |
1470 | There also were other issues. The mapping from nice level to weight | |
1471 | wasn't obvious or universal, and there were various other knobs which | |
1472 | simply weren't available for threads. | |
1473 | ||
1474 | The io controller implicitly created a hidden leaf node for each | |
1475 | cgroup to host the threads. The hidden leaf had its own copies of all | |
1476 | the knobs with "leaf_" prefixed. While this allowed equivalent | |
1477 | control over internal threads, it was with serious drawbacks. It | |
1478 | always added an extra layer of nesting which wouldn't be necessary | |
1479 | otherwise, made the interface messy and significantly complicated the | |
1480 | implementation. | |
1481 | ||
1482 | The memory controller didn't have a way to control what happened | |
1483 | between internal tasks and child cgroups and the behavior was not | |
1484 | clearly defined. There were attempts to add ad-hoc behaviors and | |
1485 | knobs to tailor the behavior to specific workloads which would have | |
1486 | led to problems extremely difficult to resolve in the long term. | |
1487 | ||
1488 | Multiple controllers struggled with internal tasks and came up with | |
1489 | different ways to deal with it; unfortunately, all the approaches were | |
1490 | severely flawed and, furthermore, the widely different behaviors | |
1491 | made cgroup as a whole highly inconsistent. | |
1492 | ||
1493 | This clearly is a problem which needs to be addressed from cgroup core | |
1494 | in a uniform way. | |
1495 | ||
1496 | ||
1497 | R-4. Other Interface Issues | |
1498 | ||
1499 | cgroup v1 grew without oversight and developed a large number of | |
1500 | idiosyncrasies and inconsistencies. One issue on the cgroup core side | |
1501 | was how an empty cgroup was notified - a userland helper binary was | |
1502 | forked and executed for each event. The event delivery wasn't | |
1503 | recursive or delegatable. The limitations of the mechanism also led | |
1504 | to in-kernel event delivery filtering mechanism further complicating | |
1505 | the interface. | |
1506 | ||
1507 | Controller interfaces were problematic too. An extreme example is | |
1508 | controllers completely ignoring hierarchical organization and treating | |
1509 | all cgroups as if they were all located directly under the root | |
1510 | cgroup. Some controllers exposed a large amount of inconsistent | |
1511 | implementation details to userland. | |
1512 | ||
1513 | There also was no consistency across controllers. When a new cgroup | |
1514 | was created, some controllers defaulted to not imposing extra | |
1515 | restrictions while others disallowed any resource usage until | |
1516 | explicitly configured. Configuration knobs for the same type of | |
1517 | control used widely differing naming schemes and formats. Statistics | |
1518 | and information knobs were named arbitrarily and used different | |
1519 | formats and units even in the same controller. | |
1520 | ||
1521 | cgroup v2 establishes common conventions where appropriate and updates | |
1522 | controllers so that they expose minimal and consistent interfaces. | |
1523 | ||
1524 | ||
1525 | R-5. Controller Issues and Remedies | |
1526 | ||
1527 | R-5-1. Memory | |
1528 | ||
1529 | The original lower boundary, the soft limit, is defined as a limit | |
1530 | that is per default unset. As a result, the set of cgroups that | |
1531 | global reclaim prefers is opt-in, rather than opt-out. The costs for | |
1532 | optimizing these mostly negative lookups are so high that the | |
1533 | implementation, despite its enormous size, does not even provide the | |
1534 | basic desirable behavior. First off, the soft limit has no | |
1535 | hierarchical meaning. All configured groups are organized in a global | |
1536 | rbtree and treated like equal peers, regardless where they are located | |
1537 | in the hierarchy. This makes subtree delegation impossible. Second, | |
1538 | the soft limit reclaim pass is so aggressive that it not just | |
1539 | introduces high allocation latencies into the system, but also impacts | |
1540 | system performance due to overreclaim, to the point where the feature | |
1541 | becomes self-defeating. | |
1542 | ||
1543 | The memory.low boundary on the other hand is a top-down allocated | |
1544 | reserve. A cgroup enjoys reclaim protection when it and all its | |
1545 | ancestors are below their low boundaries, which makes delegation of | |
1546 | subtrees possible. Secondly, new cgroups have no reserve per default | |
1547 | and in the common case most cgroups are eligible for the preferred | |
1548 | reclaim pass. This allows the new low boundary to be efficiently | |
1549 | implemented with just a minor addition to the generic reclaim code, | |
1550 | without the need for out-of-band data structures and reclaim passes. | |
1551 | Because the generic reclaim code considers all cgroups except for the | |
1552 | ones running low in the preferred first reclaim pass, overreclaim of | |
1553 | individual groups is eliminated as well, resulting in much better | |
1554 | overall workload performance. | |
1555 | ||
1556 | The original high boundary, the hard limit, is defined as a strict | |
1557 | limit that can not budge, even if the OOM killer has to be called. | |
1558 | But this generally goes against the goal of making the most out of the | |
1559 | available memory. The memory consumption of workloads varies during | |
1560 | runtime, and that requires users to overcommit. But doing that with a | |
1561 | strict upper limit requires either a fairly accurate prediction of the | |
1562 | working set size or adding slack to the limit. Since working set size | |
1563 | estimation is hard and error prone, and getting it wrong results in | |
1564 | OOM kills, most users tend to err on the side of a looser limit and | |
1565 | end up wasting precious resources. | |
1566 | ||
1567 | The memory.high boundary on the other hand can be set much more | |
1568 | conservatively. When hit, it throttles allocations by forcing them | |
1569 | into direct reclaim to work off the excess, but it never invokes the | |
1570 | OOM killer. As a result, a high boundary that is chosen too | |
1571 | aggressively will not terminate the processes, but instead it will | |
1572 | lead to gradual performance degradation. The user can monitor this | |
1573 | and make corrections until the minimal memory footprint that still | |
1574 | gives acceptable performance is found. | |
1575 | ||
1576 | In extreme cases, with many concurrent allocations and a complete | |
1577 | breakdown of reclaim progress within the group, the high boundary can | |
1578 | be exceeded. But even then it's mostly better to satisfy the | |
1579 | allocation from the slack available in other groups or the rest of the | |
1580 | system than killing the group. Otherwise, memory.max is there to | |
1581 | limit this type of spillover and ultimately contain buggy or even | |
1582 | malicious applications. | |
3e24b19d | 1583 | |
b6e6edcf JW |
1584 | Setting the original memory.limit_in_bytes below the current usage was |
1585 | subject to a race condition, where concurrent charges could cause the | |
1586 | limit setting to fail. memory.max on the other hand will first set the | |
1587 | limit to prevent new charges, and then reclaim and OOM kill until the | |
1588 | new limit is met - or the task writing to memory.max is killed. | |
1589 | ||
3e24b19d VD |
1590 | The combined memory+swap accounting and limiting is replaced by real |
1591 | control over swap space. | |
1592 | ||
1593 | The main argument for a combined memory+swap facility in the original | |
1594 | cgroup design was that global or parental pressure would always be | |
1595 | able to swap all anonymous memory of a child group, regardless of the | |
1596 | child's own (possibly untrusted) configuration. However, untrusted | |
1597 | groups can sabotage swapping by other means - such as referencing its | |
1598 | anonymous memory in a tight loop - and an admin can not assume full | |
1599 | swappability when overcommitting untrusted jobs. | |
1600 | ||
1601 | For trusted jobs, on the other hand, a combined counter is not an | |
1602 | intuitive userspace interface, and it flies in the face of the idea | |
1603 | that cgroup controllers should account and limit specific physical | |
1604 | resources. Swap space is a resource like all others in the system, | |
1605 | and that's why unified hierarchy allows distributing it separately. |