blob: c783bbb9d0fbb9099fa34a02f9594c35edbff8f3 [file] [log] [blame]
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001================
Tejun Heo6c292092015-11-16 11:13:34 -05002Control Group v2
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03003================
Tejun Heo6c292092015-11-16 11:13:34 -05004
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03005:Date: October, 2015
6:Author: Tejun Heo <tj@kernel.org>
Tejun Heo6c292092015-11-16 11:13:34 -05007
8This is the authoritative documentation on the design, interface and
9conventions of cgroup v2. It describes all userland-visible aspects
10of cgroup including core and specific controller behaviors. All
11future changes must be reflected in this document. Documentation for
W. Trevor King9a2ddda2016-01-27 13:01:52 -080012v1 is available under Documentation/cgroup-v1/.
Tejun Heo6c292092015-11-16 11:13:34 -050013
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -030014.. CONTENTS
Tejun Heo6c292092015-11-16 11:13:34 -050015
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -030016 1. Introduction
17 1-1. Terminology
18 1-2. What is cgroup?
19 2. Basic Operations
20 2-1. Mounting
Tejun Heo8cfd8142017-07-21 11:14:51 -040021 2-2. Organizing Processes and Threads
22 2-2-1. Processes
23 2-2-2. Threads
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -030024 2-3. [Un]populated Notification
25 2-4. Controlling Controllers
26 2-4-1. Enabling and Disabling
27 2-4-2. Top-down Constraint
28 2-4-3. No Internal Process Constraint
29 2-5. Delegation
30 2-5-1. Model of Delegation
31 2-5-2. Delegation Containment
32 2-6. Guidelines
33 2-6-1. Organize Once and Control
34 2-6-2. Avoid Name Collisions
35 3. Resource Distribution Models
36 3-1. Weights
37 3-2. Limits
38 3-3. Protections
39 3-4. Allocations
40 4. Interface Files
41 4-1. Format
42 4-2. Conventions
43 4-3. Core Interface Files
44 5. Controllers
45 5-1. CPU
46 5-1-1. CPU Interface Files
47 5-2. Memory
48 5-2-1. Memory Interface Files
49 5-2-2. Usage Guidelines
50 5-2-3. Memory Ownership
51 5-3. IO
52 5-3-1. IO Interface Files
53 5-3-2. Writeback
54 5-4. PID
55 5-4-1. PID Interface Files
Roman Gushchin4ad5a322017-12-13 19:49:03 +000056 5-5. Device
57 5-6. RDMA
58 5-6-1. RDMA Interface Files
59 5-7. Misc
60 5-7-1. perf_event
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -030061 6. Namespace
62 6-1. Basics
63 6-2. The Root and Views
64 6-3. Migration and setns(2)
65 6-4. Interaction with Other Namespaces
66 P. Information on Kernel Programming
67 P-1. Filesystem Support for Writeback
68 D. Deprecated v1 Core Features
69 R. Issues with v1 and Rationales for v2
70 R-1. Multiple Hierarchies
71 R-2. Thread Granularity
72 R-3. Competition Between Inner Nodes and Threads
73 R-4. Other Interface Issues
74 R-5. Controller Issues and Remedies
75 R-5-1. Memory
Tejun Heo6c292092015-11-16 11:13:34 -050076
77
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -030078Introduction
79============
Tejun Heo6c292092015-11-16 11:13:34 -050080
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -030081Terminology
82-----------
Tejun Heo6c292092015-11-16 11:13:34 -050083
84"cgroup" stands for "control group" and is never capitalized. The
85singular form is used to designate the whole feature and also as a
86qualifier as in "cgroup controllers". When explicitly referring to
87multiple individual control groups, the plural form "cgroups" is used.
88
89
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -030090What is cgroup?
91---------------
Tejun Heo6c292092015-11-16 11:13:34 -050092
93cgroup is a mechanism to organize processes hierarchically and
94distribute system resources along the hierarchy in a controlled and
95configurable manner.
96
97cgroup is largely composed of two parts - the core and controllers.
98cgroup core is primarily responsible for hierarchically organizing
99processes. A cgroup controller is usually responsible for
100distributing a specific type of system resource along the hierarchy
101although there are utility controllers which serve purposes other than
102resource distribution.
103
104cgroups form a tree structure and every process in the system belongs
105to one and only one cgroup. All threads of a process belong to the
106same cgroup. On creation, all processes are put in the cgroup that
107the parent process belongs to at the time. A process can be migrated
108to another cgroup. Migration of a process doesn't affect already
109existing descendant processes.
110
111Following certain structural constraints, controllers may be enabled or
112disabled selectively on a cgroup. All controller behaviors are
113hierarchical - if a controller is enabled on a cgroup, it affects all
114processes which belong to the cgroups consisting the inclusive
115sub-hierarchy of the cgroup. When a controller is enabled on a nested
116cgroup, it always restricts the resource distribution further. The
117restrictions set closer to the root in the hierarchy can not be
118overridden from further away.
119
120
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300121Basic Operations
122================
Tejun Heo6c292092015-11-16 11:13:34 -0500123
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300124Mounting
125--------
Tejun Heo6c292092015-11-16 11:13:34 -0500126
127Unlike v1, cgroup v2 has only single hierarchy. The cgroup v2
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300128hierarchy can be mounted with the following mount command::
Tejun Heo6c292092015-11-16 11:13:34 -0500129
130 # mount -t cgroup2 none $MOUNT_POINT
131
132cgroup2 filesystem has the magic number 0x63677270 ("cgrp"). All
133controllers which support v2 and are not bound to a v1 hierarchy are
134automatically bound to the v2 hierarchy and show up at the root.
135Controllers which are not in active use in the v2 hierarchy can be
136bound to other hierarchies. This allows mixing v2 hierarchy with the
137legacy v1 multiple hierarchies in a fully backward compatible way.
138
139A controller can be moved across hierarchies only after the controller
140is no longer referenced in its current hierarchy. Because per-cgroup
141controller states are destroyed asynchronously and controllers may
142have lingering references, a controller may not show up immediately on
143the v2 hierarchy after the final umount of the previous hierarchy.
144Similarly, a controller should be fully disabled to be moved out of
145the unified hierarchy and it may take some time for the disabled
146controller to become available for other hierarchies; furthermore, due
147to inter-controller dependencies, other controllers may need to be
148disabled too.
149
150While useful for development and manual configurations, moving
151controllers dynamically between the v2 and other hierarchies is
152strongly discouraged for production use. It is recommended to decide
153the hierarchies and controller associations before starting using the
154controllers after system boot.
155
Johannes Weiner1619b6d2016-02-16 13:21:14 -0500156During transition to v2, system management software might still
157automount the v1 cgroup filesystem and so hijack all controllers
158during boot, before manual intervention is possible. To make testing
159and experimenting easier, the kernel parameter cgroup_no_v1= allows
160disabling controllers in v1 and make them always available in v2.
161
Tejun Heo5136f632017-06-27 14:30:28 -0400162cgroup v2 currently supports the following mount options.
163
164 nsdelegate
165
166 Consider cgroup namespaces as delegation boundaries. This
167 option is system wide and can only be set on mount or modified
168 through remount from the init namespace. The mount option is
169 ignored on non-init namespace mounts. Please refer to the
170 Delegation section for details.
171
Tejun Heo6c292092015-11-16 11:13:34 -0500172
Tejun Heo8cfd8142017-07-21 11:14:51 -0400173Organizing Processes and Threads
174--------------------------------
175
176Processes
177~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500178
179Initially, only the root cgroup exists to which all processes belong.
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300180A child cgroup can be created by creating a sub-directory::
Tejun Heo6c292092015-11-16 11:13:34 -0500181
182 # mkdir $CGROUP_NAME
183
184A given cgroup may have multiple child cgroups forming a tree
185structure. Each cgroup has a read-writable interface file
186"cgroup.procs". When read, it lists the PIDs of all processes which
187belong to the cgroup one-per-line. The PIDs are not ordered and the
188same PID may show up more than once if the process got moved to
189another cgroup and then back or the PID got recycled while reading.
190
191A process can be migrated into a cgroup by writing its PID to the
192target cgroup's "cgroup.procs" file. Only one process can be migrated
193on a single write(2) call. If a process is composed of multiple
194threads, writing the PID of any thread migrates all threads of the
195process.
196
197When a process forks a child process, the new process is born into the
198cgroup that the forking process belongs to at the time of the
199operation. After exit, a process stays associated with the cgroup
200that it belonged to at the time of exit until it's reaped; however, a
201zombie process does not appear in "cgroup.procs" and thus can't be
202moved to another cgroup.
203
204A cgroup which doesn't have any children or live processes can be
205destroyed by removing the directory. Note that a cgroup which doesn't
206have any children and is associated only with zombie processes is
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300207considered empty and can be removed::
Tejun Heo6c292092015-11-16 11:13:34 -0500208
209 # rmdir $CGROUP_NAME
210
211"/proc/$PID/cgroup" lists a process's cgroup membership. If legacy
212cgroup is in use in the system, this file may contain multiple lines,
213one for each hierarchy. The entry for cgroup v2 is always in the
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300214format "0::$PATH"::
Tejun Heo6c292092015-11-16 11:13:34 -0500215
216 # cat /proc/842/cgroup
217 ...
218 0::/test-cgroup/test-cgroup-nested
219
220If the process becomes a zombie and the cgroup it was associated with
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300221is removed subsequently, " (deleted)" is appended to the path::
Tejun Heo6c292092015-11-16 11:13:34 -0500222
223 # cat /proc/842/cgroup
224 ...
225 0::/test-cgroup/test-cgroup-nested (deleted)
226
227
Tejun Heo8cfd8142017-07-21 11:14:51 -0400228Threads
229~~~~~~~
230
231cgroup v2 supports thread granularity for a subset of controllers to
232support use cases requiring hierarchical resource distribution across
233the threads of a group of processes. By default, all threads of a
234process belong to the same cgroup, which also serves as the resource
235domain to host resource consumptions which are not specific to a
236process or thread. The thread mode allows threads to be spread across
237a subtree while still maintaining the common resource domain for them.
238
239Controllers which support thread mode are called threaded controllers.
240The ones which don't are called domain controllers.
241
242Marking a cgroup threaded makes it join the resource domain of its
243parent as a threaded cgroup. The parent may be another threaded
244cgroup whose resource domain is further up in the hierarchy. The root
245of a threaded subtree, that is, the nearest ancestor which is not
246threaded, is called threaded domain or thread root interchangeably and
247serves as the resource domain for the entire subtree.
248
249Inside a threaded subtree, threads of a process can be put in
250different cgroups and are not subject to the no internal process
251constraint - threaded controllers can be enabled on non-leaf cgroups
252whether they have threads in them or not.
253
254As the threaded domain cgroup hosts all the domain resource
255consumptions of the subtree, it is considered to have internal
256resource consumptions whether there are processes in it or not and
257can't have populated child cgroups which aren't threaded. Because the
258root cgroup is not subject to no internal process constraint, it can
259serve both as a threaded domain and a parent to domain cgroups.
260
261The current operation mode or type of the cgroup is shown in the
262"cgroup.type" file which indicates whether the cgroup is a normal
263domain, a domain which is serving as the domain of a threaded subtree,
264or a threaded cgroup.
265
266On creation, a cgroup is always a domain cgroup and can be made
267threaded by writing "threaded" to the "cgroup.type" file. The
268operation is single direction::
269
270 # echo threaded > cgroup.type
271
272Once threaded, the cgroup can't be made a domain again. To enable the
273thread mode, the following conditions must be met.
274
275- As the cgroup will join the parent's resource domain. The parent
276 must either be a valid (threaded) domain or a threaded cgroup.
277
Tejun Heo918a8c22017-07-23 08:18:26 -0400278- When the parent is an unthreaded domain, it must not have any domain
279 controllers enabled or populated domain children. The root is
280 exempt from this requirement.
Tejun Heo8cfd8142017-07-21 11:14:51 -0400281
282Topology-wise, a cgroup can be in an invalid state. Please consider
Vladimir Rutsky2877cbe2018-01-02 17:27:41 +0100283the following topology::
Tejun Heo8cfd8142017-07-21 11:14:51 -0400284
285 A (threaded domain) - B (threaded) - C (domain, just created)
286
287C is created as a domain but isn't connected to a parent which can
288host child domains. C can't be used until it is turned into a
289threaded cgroup. "cgroup.type" file will report "domain (invalid)" in
290these cases. Operations which fail due to invalid topology use
291EOPNOTSUPP as the errno.
292
293A domain cgroup is turned into a threaded domain when one of its child
294cgroup becomes threaded or threaded controllers are enabled in the
295"cgroup.subtree_control" file while there are processes in the cgroup.
296A threaded domain reverts to a normal domain when the conditions
297clear.
298
299When read, "cgroup.threads" contains the list of the thread IDs of all
300threads in the cgroup. Except that the operations are per-thread
301instead of per-process, "cgroup.threads" has the same format and
302behaves the same way as "cgroup.procs". While "cgroup.threads" can be
303written to in any cgroup, as it can only move threads inside the same
304threaded domain, its operations are confined inside each threaded
305subtree.
306
307The threaded domain cgroup serves as the resource domain for the whole
308subtree, and, while the threads can be scattered across the subtree,
309all the processes are considered to be in the threaded domain cgroup.
310"cgroup.procs" in a threaded domain cgroup contains the PIDs of all
311processes in the subtree and is not readable in the subtree proper.
312However, "cgroup.procs" can be written to from anywhere in the subtree
313to migrate all threads of the matching process to the cgroup.
314
315Only threaded controllers can be enabled in a threaded subtree. When
316a threaded controller is enabled inside a threaded subtree, it only
317accounts for and controls resource consumptions associated with the
318threads in the cgroup and its descendants. All consumptions which
319aren't tied to a specific thread belong to the threaded domain cgroup.
320
321Because a threaded subtree is exempt from no internal process
322constraint, a threaded controller must be able to handle competition
323between threads in a non-leaf cgroup and its child cgroups. Each
324threaded controller defines how such competitions are handled.
325
326
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300327[Un]populated Notification
328--------------------------
Tejun Heo6c292092015-11-16 11:13:34 -0500329
330Each non-root cgroup has a "cgroup.events" file which contains
331"populated" field indicating whether the cgroup's sub-hierarchy has
332live processes in it. Its value is 0 if there is no live process in
333the cgroup and its descendants; otherwise, 1. poll and [id]notify
334events are triggered when the value changes. This can be used, for
335example, to start a clean-up operation after all processes of a given
336sub-hierarchy have exited. The populated state updates and
337notifications are recursive. Consider the following sub-hierarchy
338where the numbers in the parentheses represent the numbers of processes
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300339in each cgroup::
Tejun Heo6c292092015-11-16 11:13:34 -0500340
341 A(4) - B(0) - C(1)
342 \ D(0)
343
344A, B and C's "populated" fields would be 1 while D's 0. After the one
345process in C exits, B and C's "populated" fields would flip to "0" and
346file modified events will be generated on the "cgroup.events" files of
347both cgroups.
348
349
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300350Controlling Controllers
351-----------------------
Tejun Heo6c292092015-11-16 11:13:34 -0500352
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300353Enabling and Disabling
354~~~~~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500355
356Each cgroup has a "cgroup.controllers" file which lists all
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300357controllers available for the cgroup to enable::
Tejun Heo6c292092015-11-16 11:13:34 -0500358
359 # cat cgroup.controllers
360 cpu io memory
361
362No controller is enabled by default. Controllers can be enabled and
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300363disabled by writing to the "cgroup.subtree_control" file::
Tejun Heo6c292092015-11-16 11:13:34 -0500364
365 # echo "+cpu +memory -io" > cgroup.subtree_control
366
367Only controllers which are listed in "cgroup.controllers" can be
368enabled. When multiple operations are specified as above, either they
369all succeed or fail. If multiple operations on the same controller
370are specified, the last one is effective.
371
372Enabling a controller in a cgroup indicates that the distribution of
373the target resource across its immediate children will be controlled.
374Consider the following sub-hierarchy. The enabled controllers are
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300375listed in parentheses::
Tejun Heo6c292092015-11-16 11:13:34 -0500376
377 A(cpu,memory) - B(memory) - C()
378 \ D()
379
380As A has "cpu" and "memory" enabled, A will control the distribution
381of CPU cycles and memory to its children, in this case, B. As B has
382"memory" enabled but not "CPU", C and D will compete freely on CPU
383cycles but their division of memory available to B will be controlled.
384
385As a controller regulates the distribution of the target resource to
386the cgroup's children, enabling it creates the controller's interface
387files in the child cgroups. In the above example, enabling "cpu" on B
388would create the "cpu." prefixed controller interface files in C and
389D. Likewise, disabling "memory" from B would remove the "memory."
390prefixed controller interface files from C and D. This means that the
391controller interface files - anything which doesn't start with
392"cgroup." are owned by the parent rather than the cgroup itself.
393
394
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300395Top-down Constraint
396~~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500397
398Resources are distributed top-down and a cgroup can further distribute
399a resource only if the resource has been distributed to it from the
400parent. This means that all non-root "cgroup.subtree_control" files
401can only contain controllers which are enabled in the parent's
402"cgroup.subtree_control" file. A controller can be enabled only if
403the parent has the controller enabled and a controller can't be
404disabled if one or more children have it enabled.
405
406
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300407No Internal Process Constraint
408~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500409
Tejun Heo8cfd8142017-07-21 11:14:51 -0400410Non-root cgroups can distribute domain resources to their children
411only when they don't have any processes of their own. In other words,
412only domain cgroups which don't contain any processes can have domain
413controllers enabled in their "cgroup.subtree_control" files.
Tejun Heo6c292092015-11-16 11:13:34 -0500414
Tejun Heo8cfd8142017-07-21 11:14:51 -0400415This guarantees that, when a domain controller is looking at the part
416of the hierarchy which has it enabled, processes are always only on
417the leaves. This rules out situations where child cgroups compete
418against internal processes of the parent.
Tejun Heo6c292092015-11-16 11:13:34 -0500419
420The root cgroup is exempt from this restriction. Root contains
421processes and anonymous resource consumption which can't be associated
422with any other cgroups and requires special treatment from most
423controllers. How resource consumption in the root cgroup is governed
424is up to each controller.
425
426Note that the restriction doesn't get in the way if there is no
427enabled controller in the cgroup's "cgroup.subtree_control". This is
428important as otherwise it wouldn't be possible to create children of a
429populated cgroup. To control resource distribution of a cgroup, the
430cgroup must create children and transfer all its processes to the
431children before enabling controllers in its "cgroup.subtree_control"
432file.
433
434
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300435Delegation
436----------
Tejun Heo6c292092015-11-16 11:13:34 -0500437
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300438Model of Delegation
439~~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500440
Tejun Heo5136f632017-06-27 14:30:28 -0400441A cgroup can be delegated in two ways. First, to a less privileged
Tejun Heo8cfd8142017-07-21 11:14:51 -0400442user by granting write access of the directory and its "cgroup.procs",
443"cgroup.threads" and "cgroup.subtree_control" files to the user.
444Second, if the "nsdelegate" mount option is set, automatically to a
445cgroup namespace on namespace creation.
Tejun Heo6c292092015-11-16 11:13:34 -0500446
Tejun Heo5136f632017-06-27 14:30:28 -0400447Because the resource control interface files in a given directory
448control the distribution of the parent's resources, the delegatee
449shouldn't be allowed to write to them. For the first method, this is
450achieved by not granting access to these files. For the second, the
451kernel rejects writes to all files other than "cgroup.procs" and
452"cgroup.subtree_control" on a namespace root from inside the
453namespace.
454
455The end results are equivalent for both delegation types. Once
456delegated, the user can build sub-hierarchy under the directory,
457organize processes inside it as it sees fit and further distribute the
458resources it received from the parent. The limits and other settings
459of all resource controllers are hierarchical and regardless of what
460happens in the delegated sub-hierarchy, nothing can escape the
461resource restrictions imposed by the parent.
Tejun Heo6c292092015-11-16 11:13:34 -0500462
463Currently, cgroup doesn't impose any restrictions on the number of
464cgroups in or nesting depth of a delegated sub-hierarchy; however,
465this may be limited explicitly in the future.
466
467
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300468Delegation Containment
469~~~~~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500470
471A delegated sub-hierarchy is contained in the sense that processes
Tejun Heo5136f632017-06-27 14:30:28 -0400472can't be moved into or out of the sub-hierarchy by the delegatee.
473
474For delegations to a less privileged user, this is achieved by
475requiring the following conditions for a process with a non-root euid
476to migrate a target process into a cgroup by writing its PID to the
477"cgroup.procs" file.
Tejun Heo6c292092015-11-16 11:13:34 -0500478
Tejun Heo6c292092015-11-16 11:13:34 -0500479- The writer must have write access to the "cgroup.procs" file.
480
481- The writer must have write access to the "cgroup.procs" file of the
482 common ancestor of the source and destination cgroups.
483
Tejun Heo576dd462017-01-20 11:29:54 -0500484The above two constraints ensure that while a delegatee may migrate
Tejun Heo6c292092015-11-16 11:13:34 -0500485processes around freely in the delegated sub-hierarchy it can't pull
486in from or push out to outside the sub-hierarchy.
487
488For an example, let's assume cgroups C0 and C1 have been delegated to
489user U0 who created C00, C01 under C0 and C10 under C1 as follows and
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300490all processes under C0 and C1 belong to U0::
Tejun Heo6c292092015-11-16 11:13:34 -0500491
492 ~~~~~~~~~~~~~ - C0 - C00
493 ~ cgroup ~ \ C01
494 ~ hierarchy ~
495 ~~~~~~~~~~~~~ - C1 - C10
496
497Let's also say U0 wants to write the PID of a process which is
498currently in C10 into "C00/cgroup.procs". U0 has write access to the
Tejun Heo576dd462017-01-20 11:29:54 -0500499file; however, the common ancestor of the source cgroup C10 and the
500destination cgroup C00 is above the points of delegation and U0 would
501not have write access to its "cgroup.procs" files and thus the write
502will be denied with -EACCES.
Tejun Heo6c292092015-11-16 11:13:34 -0500503
Tejun Heo5136f632017-06-27 14:30:28 -0400504For delegations to namespaces, containment is achieved by requiring
505that both the source and destination cgroups are reachable from the
506namespace of the process which is attempting the migration. If either
507is not reachable, the migration is rejected with -ENOENT.
508
Tejun Heo6c292092015-11-16 11:13:34 -0500509
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300510Guidelines
511----------
Tejun Heo6c292092015-11-16 11:13:34 -0500512
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300513Organize Once and Control
514~~~~~~~~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500515
516Migrating a process across cgroups is a relatively expensive operation
517and stateful resources such as memory are not moved together with the
518process. This is an explicit design decision as there often exist
519inherent trade-offs between migration and various hot paths in terms
520of synchronization cost.
521
522As such, migrating processes across cgroups frequently as a means to
523apply different resource restrictions is discouraged. A workload
524should be assigned to a cgroup according to the system's logical and
525resource structure once on start-up. Dynamic adjustments to resource
526distribution can be made by changing controller configuration through
527the interface files.
528
529
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300530Avoid Name Collisions
531~~~~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500532
533Interface files for a cgroup and its children cgroups occupy the same
534directory and it is possible to create children cgroups which collide
535with interface files.
536
537All cgroup core interface files are prefixed with "cgroup." and each
538controller's interface files are prefixed with the controller name and
539a dot. A controller's name is composed of lower case alphabets and
540'_'s but never begins with an '_' so it can be used as the prefix
541character for collision avoidance. Also, interface file names won't
542start or end with terms which are often used in categorizing workloads
543such as job, service, slice, unit or workload.
544
545cgroup doesn't do anything to prevent name collisions and it's the
546user's responsibility to avoid them.
547
548
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300549Resource Distribution Models
550============================
Tejun Heo6c292092015-11-16 11:13:34 -0500551
552cgroup controllers implement several resource distribution schemes
553depending on the resource type and expected use cases. This section
554describes major schemes in use along with their expected behaviors.
555
556
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300557Weights
558-------
Tejun Heo6c292092015-11-16 11:13:34 -0500559
560A parent's resource is distributed by adding up the weights of all
561active children and giving each the fraction matching the ratio of its
562weight against the sum. As only children which can make use of the
563resource at the moment participate in the distribution, this is
564work-conserving. Due to the dynamic nature, this model is usually
565used for stateless resources.
566
567All weights are in the range [1, 10000] with the default at 100. This
568allows symmetric multiplicative biases in both directions at fine
569enough granularity while staying in the intuitive range.
570
571As long as the weight is in range, all configuration combinations are
572valid and there is no reason to reject configuration changes or
573process migrations.
574
575"cpu.weight" proportionally distributes CPU cycles to active children
576and is an example of this type.
577
578
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300579Limits
580------
Tejun Heo6c292092015-11-16 11:13:34 -0500581
582A child can only consume upto the configured amount of the resource.
583Limits can be over-committed - the sum of the limits of children can
584exceed the amount of resource available to the parent.
585
586Limits are in the range [0, max] and defaults to "max", which is noop.
587
588As limits can be over-committed, all configuration combinations are
589valid and there is no reason to reject configuration changes or
590process migrations.
591
592"io.max" limits the maximum BPS and/or IOPS that a cgroup can consume
593on an IO device and is an example of this type.
594
595
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300596Protections
597-----------
Tejun Heo6c292092015-11-16 11:13:34 -0500598
599A cgroup is protected to be allocated upto the configured amount of
600the resource if the usages of all its ancestors are under their
601protected levels. Protections can be hard guarantees or best effort
602soft boundaries. Protections can also be over-committed in which case
603only upto the amount available to the parent is protected among
604children.
605
606Protections are in the range [0, max] and defaults to 0, which is
607noop.
608
609As protections can be over-committed, all configuration combinations
610are valid and there is no reason to reject configuration changes or
611process migrations.
612
613"memory.low" implements best-effort memory protection and is an
614example of this type.
615
616
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300617Allocations
618-----------
Tejun Heo6c292092015-11-16 11:13:34 -0500619
620A cgroup is exclusively allocated a certain amount of a finite
621resource. Allocations can't be over-committed - the sum of the
622allocations of children can not exceed the amount of resource
623available to the parent.
624
625Allocations are in the range [0, max] and defaults to 0, which is no
626resource.
627
628As allocations can't be over-committed, some configuration
629combinations are invalid and should be rejected. Also, if the
630resource is mandatory for execution of processes, process migrations
631may be rejected.
632
633"cpu.rt.max" hard-allocates realtime slices and is an example of this
634type.
635
636
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300637Interface Files
638===============
Tejun Heo6c292092015-11-16 11:13:34 -0500639
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300640Format
641------
Tejun Heo6c292092015-11-16 11:13:34 -0500642
643All interface files should be in one of the following formats whenever
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300644possible::
Tejun Heo6c292092015-11-16 11:13:34 -0500645
646 New-line separated values
647 (when only one value can be written at once)
648
649 VAL0\n
650 VAL1\n
651 ...
652
653 Space separated values
654 (when read-only or multiple values can be written at once)
655
656 VAL0 VAL1 ...\n
657
658 Flat keyed
659
660 KEY0 VAL0\n
661 KEY1 VAL1\n
662 ...
663
664 Nested keyed
665
666 KEY0 SUB_KEY0=VAL00 SUB_KEY1=VAL01...
667 KEY1 SUB_KEY0=VAL10 SUB_KEY1=VAL11...
668 ...
669
670For a writable file, the format for writing should generally match
671reading; however, controllers may allow omitting later fields or
672implement restricted shortcuts for most common use cases.
673
674For both flat and nested keyed files, only the values for a single key
675can be written at a time. For nested keyed files, the sub key pairs
676may be specified in any order and not all pairs have to be specified.
677
678
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300679Conventions
680-----------
Tejun Heo6c292092015-11-16 11:13:34 -0500681
682- Settings for a single feature should be contained in a single file.
683
684- The root cgroup should be exempt from resource control and thus
685 shouldn't have resource control interface files. Also,
686 informational files on the root cgroup which end up showing global
687 information available elsewhere shouldn't exist.
688
689- If a controller implements weight based resource distribution, its
690 interface file should be named "weight" and have the range [1,
691 10000] with 100 as the default. The values are chosen to allow
692 enough and symmetric bias in both directions while keeping it
693 intuitive (the default is 100%).
694
695- If a controller implements an absolute resource guarantee and/or
696 limit, the interface files should be named "min" and "max"
697 respectively. If a controller implements best effort resource
698 guarantee and/or limit, the interface files should be named "low"
699 and "high" respectively.
700
701 In the above four control files, the special token "max" should be
702 used to represent upward infinity for both reading and writing.
703
704- If a setting has a configurable default value and keyed specific
705 overrides, the default entry should be keyed with "default" and
706 appear as the first entry in the file.
707
708 The default value can be updated by writing either "default $VAL" or
709 "$VAL".
710
711 When writing to update a specific override, "default" can be used as
712 the value to indicate removal of the override. Override entries
713 with "default" as the value must not appear when read.
714
715 For example, a setting which is keyed by major:minor device numbers
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300716 with integer values may look like the following::
Tejun Heo6c292092015-11-16 11:13:34 -0500717
718 # cat cgroup-example-interface-file
719 default 150
720 8:0 300
721
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300722 The default value can be updated by::
Tejun Heo6c292092015-11-16 11:13:34 -0500723
724 # echo 125 > cgroup-example-interface-file
725
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300726 or::
Tejun Heo6c292092015-11-16 11:13:34 -0500727
728 # echo "default 125" > cgroup-example-interface-file
729
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300730 An override can be set by::
Tejun Heo6c292092015-11-16 11:13:34 -0500731
732 # echo "8:16 170" > cgroup-example-interface-file
733
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300734 and cleared by::
Tejun Heo6c292092015-11-16 11:13:34 -0500735
736 # echo "8:0 default" > cgroup-example-interface-file
737 # cat cgroup-example-interface-file
738 default 125
739 8:16 170
740
741- For events which are not very high frequency, an interface file
742 "events" should be created which lists event key value pairs.
743 Whenever a notifiable event happens, file modified event should be
744 generated on the file.
745
746
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300747Core Interface Files
748--------------------
Tejun Heo6c292092015-11-16 11:13:34 -0500749
750All cgroup core files are prefixed with "cgroup."
751
Tejun Heo8cfd8142017-07-21 11:14:51 -0400752 cgroup.type
753
754 A read-write single value file which exists on non-root
755 cgroups.
756
757 When read, it indicates the current type of the cgroup, which
758 can be one of the following values.
759
760 - "domain" : A normal valid domain cgroup.
761
762 - "domain threaded" : A threaded domain cgroup which is
763 serving as the root of a threaded subtree.
764
765 - "domain invalid" : A cgroup which is in an invalid state.
766 It can't be populated or have controllers enabled. It may
767 be allowed to become a threaded cgroup.
768
769 - "threaded" : A threaded cgroup which is a member of a
770 threaded subtree.
771
772 A cgroup can be turned into a threaded cgroup by writing
773 "threaded" to this file.
774
Tejun Heo6c292092015-11-16 11:13:34 -0500775 cgroup.procs
Tejun Heo6c292092015-11-16 11:13:34 -0500776 A read-write new-line separated values file which exists on
777 all cgroups.
778
779 When read, it lists the PIDs of all processes which belong to
780 the cgroup one-per-line. The PIDs are not ordered and the
781 same PID may show up more than once if the process got moved
782 to another cgroup and then back or the PID got recycled while
783 reading.
784
785 A PID can be written to migrate the process associated with
786 the PID to the cgroup. The writer should match all of the
787 following conditions.
788
Tejun Heo6c292092015-11-16 11:13:34 -0500789 - It must have write access to the "cgroup.procs" file.
790
791 - It must have write access to the "cgroup.procs" file of the
792 common ancestor of the source and destination cgroups.
793
794 When delegating a sub-hierarchy, write access to this file
795 should be granted along with the containing directory.
796
Tejun Heo8cfd8142017-07-21 11:14:51 -0400797 In a threaded cgroup, reading this file fails with EOPNOTSUPP
798 as all the processes belong to the thread root. Writing is
799 supported and moves every thread of the process to the cgroup.
800
801 cgroup.threads
802 A read-write new-line separated values file which exists on
803 all cgroups.
804
805 When read, it lists the TIDs of all threads which belong to
806 the cgroup one-per-line. The TIDs are not ordered and the
807 same TID may show up more than once if the thread got moved to
808 another cgroup and then back or the TID got recycled while
809 reading.
810
811 A TID can be written to migrate the thread associated with the
812 TID to the cgroup. The writer should match all of the
813 following conditions.
814
815 - It must have write access to the "cgroup.threads" file.
816
817 - The cgroup that the thread is currently in must be in the
818 same resource domain as the destination cgroup.
819
820 - It must have write access to the "cgroup.procs" file of the
821 common ancestor of the source and destination cgroups.
822
823 When delegating a sub-hierarchy, write access to this file
824 should be granted along with the containing directory.
825
Tejun Heo6c292092015-11-16 11:13:34 -0500826 cgroup.controllers
Tejun Heo6c292092015-11-16 11:13:34 -0500827 A read-only space separated values file which exists on all
828 cgroups.
829
830 It shows space separated list of all controllers available to
831 the cgroup. The controllers are not ordered.
832
833 cgroup.subtree_control
Tejun Heo6c292092015-11-16 11:13:34 -0500834 A read-write space separated values file which exists on all
835 cgroups. Starts out empty.
836
837 When read, it shows space separated list of the controllers
838 which are enabled to control resource distribution from the
839 cgroup to its children.
840
841 Space separated list of controllers prefixed with '+' or '-'
842 can be written to enable or disable controllers. A controller
843 name prefixed with '+' enables the controller and '-'
844 disables. If a controller appears more than once on the list,
845 the last one is effective. When multiple enable and disable
846 operations are specified, either all succeed or all fail.
847
848 cgroup.events
Tejun Heo6c292092015-11-16 11:13:34 -0500849 A read-only flat-keyed file which exists on non-root cgroups.
850 The following entries are defined. Unless specified
851 otherwise, a value change in this file generates a file
852 modified event.
853
854 populated
Tejun Heo6c292092015-11-16 11:13:34 -0500855 1 if the cgroup or its descendants contains any live
856 processes; otherwise, 0.
857
Roman Gushchin1a926e02017-07-28 18:28:44 +0100858 cgroup.max.descendants
859 A read-write single value files. The default is "max".
860
861 Maximum allowed number of descent cgroups.
862 If the actual number of descendants is equal or larger,
863 an attempt to create a new cgroup in the hierarchy will fail.
864
865 cgroup.max.depth
866 A read-write single value files. The default is "max".
867
868 Maximum allowed descent depth below the current cgroup.
869 If the actual descent depth is equal or larger,
870 an attempt to create a new child cgroup will fail.
871
Roman Gushchinec392252017-08-02 17:55:31 +0100872 cgroup.stat
873 A read-only flat-keyed file with the following entries:
874
875 nr_descendants
876 Total number of visible descendant cgroups.
877
878 nr_dying_descendants
879 Total number of dying descendant cgroups. A cgroup becomes
880 dying after being deleted by a user. The cgroup will remain
881 in dying state for some time undefined time (which can depend
882 on system load) before being completely destroyed.
883
884 A process can't enter a dying cgroup under any circumstances,
885 a dying cgroup can't revive.
886
887 A dying cgroup can consume system resources not exceeding
888 limits, which were active at the moment of cgroup deletion.
889
Tejun Heo6c292092015-11-16 11:13:34 -0500890
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300891Controllers
892===========
Tejun Heo6c292092015-11-16 11:13:34 -0500893
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300894CPU
895---
Tejun Heo6c292092015-11-16 11:13:34 -0500896
Tejun Heo6c292092015-11-16 11:13:34 -0500897The "cpu" controllers regulates distribution of CPU cycles. This
898controller implements weight and absolute bandwidth limit models for
899normal scheduling policy and absolute bandwidth allocation model for
900realtime scheduling policy.
901
Tejun Heoc2f31b72017-12-05 09:10:17 -0800902WARNING: cgroup2 doesn't yet support control of realtime processes and
903the cpu controller can only be enabled when all RT processes are in
904the root cgroup. Be aware that system management software may already
905have placed RT processes into nonroot cgroups during the system boot
906process, and these processes may need to be moved to the root cgroup
907before the cpu controller can be enabled.
908
Tejun Heo6c292092015-11-16 11:13:34 -0500909
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300910CPU Interface Files
911~~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500912
913All time durations are in microseconds.
914
915 cpu.stat
Tejun Heo6c292092015-11-16 11:13:34 -0500916 A read-only flat-keyed file which exists on non-root cgroups.
Tejun Heod41bf8c2017-10-23 16:18:27 -0700917 This file exists whether the controller is enabled or not.
Tejun Heo6c292092015-11-16 11:13:34 -0500918
Tejun Heod41bf8c2017-10-23 16:18:27 -0700919 It always reports the following three stats:
Tejun Heo6c292092015-11-16 11:13:34 -0500920
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300921 - usage_usec
922 - user_usec
923 - system_usec
Tejun Heod41bf8c2017-10-23 16:18:27 -0700924
925 and the following three when the controller is enabled:
926
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300927 - nr_periods
928 - nr_throttled
929 - throttled_usec
Tejun Heo6c292092015-11-16 11:13:34 -0500930
931 cpu.weight
Tejun Heo6c292092015-11-16 11:13:34 -0500932 A read-write single value file which exists on non-root
933 cgroups. The default is "100".
934
935 The weight in the range [1, 10000].
936
Tejun Heo0d593632017-09-25 09:00:19 -0700937 cpu.weight.nice
938 A read-write single value file which exists on non-root
939 cgroups. The default is "0".
940
941 The nice value is in the range [-20, 19].
942
943 This interface file is an alternative interface for
944 "cpu.weight" and allows reading and setting weight using the
945 same values used by nice(2). Because the range is smaller and
946 granularity is coarser for the nice values, the read value is
947 the closest approximation of the current weight.
948
Tejun Heo6c292092015-11-16 11:13:34 -0500949 cpu.max
Tejun Heo6c292092015-11-16 11:13:34 -0500950 A read-write two value file which exists on non-root cgroups.
951 The default is "max 100000".
952
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300953 The maximum bandwidth limit. It's in the following format::
Tejun Heo6c292092015-11-16 11:13:34 -0500954
955 $MAX $PERIOD
956
957 which indicates that the group may consume upto $MAX in each
958 $PERIOD duration. "max" for $MAX indicates no limit. If only
959 one number is written, $MAX is updated.
960
Tejun Heo6c292092015-11-16 11:13:34 -0500961
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300962Memory
963------
Tejun Heo6c292092015-11-16 11:13:34 -0500964
965The "memory" controller regulates distribution of memory. Memory is
966stateful and implements both limit and protection models. Due to the
967intertwining between memory usage and reclaim pressure and the
968stateful nature of memory, the distribution model is relatively
969complex.
970
971While not completely water-tight, all major memory usages by a given
972cgroup are tracked so that the total memory consumption can be
973accounted and controlled to a reasonable extent. Currently, the
974following types of memory usages are tracked.
975
976- Userland memory - page cache and anonymous memory.
977
978- Kernel data structures such as dentries and inodes.
979
980- TCP socket buffers.
981
982The above list may expand in the future for better coverage.
983
984
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -0300985Memory Interface Files
986~~~~~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -0500987
988All memory amounts are in bytes. If a value which is not aligned to
989PAGE_SIZE is written, the value may be rounded up to the closest
990PAGE_SIZE multiple when read back.
991
992 memory.current
Tejun Heo6c292092015-11-16 11:13:34 -0500993 A read-only single value file which exists on non-root
994 cgroups.
995
996 The total amount of memory currently being used by the cgroup
997 and its descendants.
998
999 memory.low
Tejun Heo6c292092015-11-16 11:13:34 -05001000 A read-write single value file which exists on non-root
1001 cgroups. The default is "0".
1002
1003 Best-effort memory protection. If the memory usages of a
1004 cgroup and all its ancestors are below their low boundaries,
1005 the cgroup's memory won't be reclaimed unless memory can be
1006 reclaimed from unprotected cgroups.
1007
1008 Putting more memory than generally available under this
1009 protection is discouraged.
1010
1011 memory.high
Tejun Heo6c292092015-11-16 11:13:34 -05001012 A read-write single value file which exists on non-root
1013 cgroups. The default is "max".
1014
1015 Memory usage throttle limit. This is the main mechanism to
1016 control memory usage of a cgroup. If a cgroup's usage goes
1017 over the high boundary, the processes of the cgroup are
1018 throttled and put under heavy reclaim pressure.
1019
1020 Going over the high limit never invokes the OOM killer and
1021 under extreme conditions the limit may be breached.
1022
1023 memory.max
Tejun Heo6c292092015-11-16 11:13:34 -05001024 A read-write single value file which exists on non-root
1025 cgroups. The default is "max".
1026
1027 Memory usage hard limit. This is the final protection
1028 mechanism. If a cgroup's memory usage reaches this limit and
1029 can't be reduced, the OOM killer is invoked in the cgroup.
1030 Under certain circumstances, the usage may go over the limit
1031 temporarily.
1032
1033 This is the ultimate protection mechanism. As long as the
1034 high limit is used and monitored properly, this limit's
1035 utility is limited to providing the final safety net.
1036
1037 memory.events
Tejun Heo6c292092015-11-16 11:13:34 -05001038 A read-only flat-keyed file which exists on non-root cgroups.
1039 The following entries are defined. Unless specified
1040 otherwise, a value change in this file generates a file
1041 modified event.
1042
1043 low
Tejun Heo6c292092015-11-16 11:13:34 -05001044 The number of times the cgroup is reclaimed due to
1045 high memory pressure even though its usage is under
1046 the low boundary. This usually indicates that the low
1047 boundary is over-committed.
1048
1049 high
Tejun Heo6c292092015-11-16 11:13:34 -05001050 The number of times processes of the cgroup are
1051 throttled and routed to perform direct memory reclaim
1052 because the high memory boundary was exceeded. For a
1053 cgroup whose memory usage is capped by the high limit
1054 rather than global memory pressure, this event's
1055 occurrences are expected.
1056
1057 max
Tejun Heo6c292092015-11-16 11:13:34 -05001058 The number of times the cgroup's memory usage was
1059 about to go over the max boundary. If direct reclaim
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -07001060 fails to bring it down, the cgroup goes to OOM state.
Tejun Heo6c292092015-11-16 11:13:34 -05001061
1062 oom
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -07001063 The number of time the cgroup's memory usage was
1064 reached the limit and allocation was about to fail.
1065
1066 Depending on context result could be invocation of OOM
Vladimir Rutsky2877cbe2018-01-02 17:27:41 +01001067 killer and retrying allocation or failing allocation.
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -07001068
1069 Failed allocation in its turn could be returned into
Vladimir Rutsky2877cbe2018-01-02 17:27:41 +01001070 userspace as -ENOMEM or silently ignored in cases like
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001071 disk readahead. For now OOM in memory cgroup kills
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -07001072 tasks iff shortage has happened inside page fault.
1073
1074 oom_kill
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -07001075 The number of processes belonging to this cgroup
1076 killed by any kind of OOM killer.
Tejun Heo6c292092015-11-16 11:13:34 -05001077
Johannes Weiner587d9f72016-01-20 15:03:19 -08001078 memory.stat
Johannes Weiner587d9f72016-01-20 15:03:19 -08001079 A read-only flat-keyed file which exists on non-root cgroups.
1080
1081 This breaks down the cgroup's memory footprint into different
1082 types of memory, type-specific details, and other information
1083 on the state and past events of the memory management system.
1084
1085 All memory amounts are in bytes.
1086
1087 The entries are ordered to be human readable, and new entries
1088 can show up in the middle. Don't rely on items remaining in a
1089 fixed position; use the keys to look up specific values!
1090
1091 anon
Johannes Weiner587d9f72016-01-20 15:03:19 -08001092 Amount of memory used in anonymous mappings such as
1093 brk(), sbrk(), and mmap(MAP_ANONYMOUS)
1094
1095 file
Johannes Weiner587d9f72016-01-20 15:03:19 -08001096 Amount of memory used to cache filesystem data,
1097 including tmpfs and shared memory.
1098
Vladimir Davydov12580e42016-03-17 14:17:38 -07001099 kernel_stack
Vladimir Davydov12580e42016-03-17 14:17:38 -07001100 Amount of memory allocated to kernel stacks.
1101
Vladimir Davydov27ee57c2016-03-17 14:17:35 -07001102 slab
Vladimir Davydov27ee57c2016-03-17 14:17:35 -07001103 Amount of memory used for storing in-kernel data
1104 structures.
1105
Johannes Weiner4758e192016-02-02 16:57:41 -08001106 sock
Johannes Weiner4758e192016-02-02 16:57:41 -08001107 Amount of memory used in network transmission buffers
1108
Johannes Weiner9a4caf12017-05-03 14:52:45 -07001109 shmem
Johannes Weiner9a4caf12017-05-03 14:52:45 -07001110 Amount of cached filesystem data that is swap-backed,
1111 such as tmpfs, shm segments, shared anonymous mmap()s
1112
Johannes Weiner587d9f72016-01-20 15:03:19 -08001113 file_mapped
Johannes Weiner587d9f72016-01-20 15:03:19 -08001114 Amount of cached filesystem data mapped with mmap()
1115
1116 file_dirty
Johannes Weiner587d9f72016-01-20 15:03:19 -08001117 Amount of cached filesystem data that was modified but
1118 not yet written back to disk
1119
1120 file_writeback
Johannes Weiner587d9f72016-01-20 15:03:19 -08001121 Amount of cached filesystem data that was modified and
1122 is currently being written back to disk
1123
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001124 inactive_anon, active_anon, inactive_file, active_file, unevictable
Johannes Weiner587d9f72016-01-20 15:03:19 -08001125 Amount of memory, swap-backed and filesystem-backed,
1126 on the internal memory management lists used by the
1127 page reclaim algorithm
1128
Vladimir Davydov27ee57c2016-03-17 14:17:35 -07001129 slab_reclaimable
Vladimir Davydov27ee57c2016-03-17 14:17:35 -07001130 Part of "slab" that might be reclaimed, such as
1131 dentries and inodes.
1132
1133 slab_unreclaimable
Vladimir Davydov27ee57c2016-03-17 14:17:35 -07001134 Part of "slab" that cannot be reclaimed on memory
1135 pressure.
1136
Johannes Weiner587d9f72016-01-20 15:03:19 -08001137 pgfault
Johannes Weiner587d9f72016-01-20 15:03:19 -08001138 Total number of page faults incurred
1139
1140 pgmajfault
Johannes Weiner587d9f72016-01-20 15:03:19 -08001141 Number of major page faults incurred
1142
Roman Gushchinb3409592017-05-12 15:47:09 -07001143 workingset_refault
1144
1145 Number of refaults of previously evicted pages
1146
1147 workingset_activate
1148
1149 Number of refaulted pages that were immediately activated
1150
1151 workingset_nodereclaim
1152
1153 Number of times a shadow node has been reclaimed
1154
Roman Gushchin22621852017-07-06 15:40:25 -07001155 pgrefill
1156
1157 Amount of scanned pages (in an active LRU list)
1158
1159 pgscan
1160
1161 Amount of scanned pages (in an inactive LRU list)
1162
1163 pgsteal
1164
1165 Amount of reclaimed pages
1166
1167 pgactivate
1168
1169 Amount of pages moved to the active LRU list
1170
1171 pgdeactivate
1172
1173 Amount of pages moved to the inactive LRU lis
1174
1175 pglazyfree
1176
1177 Amount of pages postponed to be freed under memory pressure
1178
1179 pglazyfreed
1180
1181 Amount of reclaimed lazyfree pages
1182
Vladimir Davydov3e24b192016-01-20 15:03:13 -08001183 memory.swap.current
Vladimir Davydov3e24b192016-01-20 15:03:13 -08001184 A read-only single value file which exists on non-root
1185 cgroups.
1186
1187 The total amount of swap currently being used by the cgroup
1188 and its descendants.
1189
1190 memory.swap.max
Vladimir Davydov3e24b192016-01-20 15:03:13 -08001191 A read-write single value file which exists on non-root
1192 cgroups. The default is "max".
1193
1194 Swap usage hard limit. If a cgroup's swap usage reaches this
Vladimir Rutsky2877cbe2018-01-02 17:27:41 +01001195 limit, anonymous memory of the cgroup will not be swapped out.
Vladimir Davydov3e24b192016-01-20 15:03:13 -08001196
Tejun Heo6c292092015-11-16 11:13:34 -05001197
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001198Usage Guidelines
1199~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -05001200
1201"memory.high" is the main mechanism to control memory usage.
1202Over-committing on high limit (sum of high limits > available memory)
1203and letting global memory pressure to distribute memory according to
1204usage is a viable strategy.
1205
1206Because breach of the high limit doesn't trigger the OOM killer but
1207throttles the offending cgroup, a management agent has ample
1208opportunities to monitor and take appropriate actions such as granting
1209more memory or terminating the workload.
1210
1211Determining whether a cgroup has enough memory is not trivial as
1212memory usage doesn't indicate whether the workload can benefit from
1213more memory. For example, a workload which writes data received from
1214network to a file can use all available memory but can also operate as
1215performant with a small amount of memory. A measure of memory
1216pressure - how much the workload is being impacted due to lack of
1217memory - is necessary to determine whether a workload needs more
1218memory; unfortunately, memory pressure monitoring mechanism isn't
1219implemented yet.
1220
1221
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001222Memory Ownership
1223~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -05001224
1225A memory area is charged to the cgroup which instantiated it and stays
1226charged to the cgroup until the area is released. Migrating a process
1227to a different cgroup doesn't move the memory usages that it
1228instantiated while in the previous cgroup to the new cgroup.
1229
1230A memory area may be used by processes belonging to different cgroups.
1231To which cgroup the area will be charged is in-deterministic; however,
1232over time, the memory area is likely to end up in a cgroup which has
1233enough memory allowance to avoid high reclaim pressure.
1234
1235If a cgroup sweeps a considerable amount of memory which is expected
1236to be accessed repeatedly by other cgroups, it may make sense to use
1237POSIX_FADV_DONTNEED to relinquish the ownership of memory areas
1238belonging to the affected files to ensure correct memory ownership.
1239
1240
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001241IO
1242--
Tejun Heo6c292092015-11-16 11:13:34 -05001243
1244The "io" controller regulates the distribution of IO resources. This
1245controller implements both weight based and absolute bandwidth or IOPS
1246limit distribution; however, weight based distribution is available
1247only if cfq-iosched is in use and neither scheme is available for
1248blk-mq devices.
1249
1250
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001251IO Interface Files
1252~~~~~~~~~~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -05001253
1254 io.stat
Tejun Heo6c292092015-11-16 11:13:34 -05001255 A read-only nested-keyed file which exists on non-root
1256 cgroups.
1257
1258 Lines are keyed by $MAJ:$MIN device numbers and not ordered.
1259 The following nested keys are defined.
1260
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001261 ====== ===================
Tejun Heo6c292092015-11-16 11:13:34 -05001262 rbytes Bytes read
1263 wbytes Bytes written
1264 rios Number of read IOs
1265 wios Number of write IOs
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001266 ====== ===================
Tejun Heo6c292092015-11-16 11:13:34 -05001267
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001268 An example read output follows:
Tejun Heo6c292092015-11-16 11:13:34 -05001269
1270 8:16 rbytes=1459200 wbytes=314773504 rios=192 wios=353
1271 8:0 rbytes=90430464 wbytes=299008000 rios=8950 wios=1252
1272
1273 io.weight
Tejun Heo6c292092015-11-16 11:13:34 -05001274 A read-write flat-keyed file which exists on non-root cgroups.
1275 The default is "default 100".
1276
1277 The first line is the default weight applied to devices
1278 without specific override. The rest are overrides keyed by
1279 $MAJ:$MIN device numbers and not ordered. The weights are in
1280 the range [1, 10000] and specifies the relative amount IO time
1281 the cgroup can use in relation to its siblings.
1282
1283 The default weight can be updated by writing either "default
1284 $WEIGHT" or simply "$WEIGHT". Overrides can be set by writing
1285 "$MAJ:$MIN $WEIGHT" and unset by writing "$MAJ:$MIN default".
1286
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001287 An example read output follows::
Tejun Heo6c292092015-11-16 11:13:34 -05001288
1289 default 100
1290 8:16 200
1291 8:0 50
1292
1293 io.max
Tejun Heo6c292092015-11-16 11:13:34 -05001294 A read-write nested-keyed file which exists on non-root
1295 cgroups.
1296
1297 BPS and IOPS based IO limit. Lines are keyed by $MAJ:$MIN
1298 device numbers and not ordered. The following nested keys are
1299 defined.
1300
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001301 ===== ==================================
Tejun Heo6c292092015-11-16 11:13:34 -05001302 rbps Max read bytes per second
1303 wbps Max write bytes per second
1304 riops Max read IO operations per second
1305 wiops Max write IO operations per second
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001306 ===== ==================================
Tejun Heo6c292092015-11-16 11:13:34 -05001307
1308 When writing, any number of nested key-value pairs can be
1309 specified in any order. "max" can be specified as the value
1310 to remove a specific limit. If the same key is specified
1311 multiple times, the outcome is undefined.
1312
1313 BPS and IOPS are measured in each IO direction and IOs are
1314 delayed if limit is reached. Temporary bursts are allowed.
1315
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001316 Setting read limit at 2M BPS and write at 120 IOPS for 8:16::
Tejun Heo6c292092015-11-16 11:13:34 -05001317
1318 echo "8:16 rbps=2097152 wiops=120" > io.max
1319
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001320 Reading returns the following::
Tejun Heo6c292092015-11-16 11:13:34 -05001321
1322 8:16 rbps=2097152 wbps=max riops=max wiops=120
1323
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001324 Write IOPS limit can be removed by writing the following::
Tejun Heo6c292092015-11-16 11:13:34 -05001325
1326 echo "8:16 wiops=max" > io.max
1327
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001328 Reading now returns the following::
Tejun Heo6c292092015-11-16 11:13:34 -05001329
1330 8:16 rbps=2097152 wbps=max riops=max wiops=max
1331
1332
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001333Writeback
1334~~~~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -05001335
1336Page cache is dirtied through buffered writes and shared mmaps and
1337written asynchronously to the backing filesystem by the writeback
1338mechanism. Writeback sits between the memory and IO domains and
1339regulates the proportion of dirty memory by balancing dirtying and
1340write IOs.
1341
1342The io controller, in conjunction with the memory controller,
1343implements control of page cache writeback IOs. The memory controller
1344defines the memory domain that dirty memory ratio is calculated and
1345maintained for and the io controller defines the io domain which
1346writes out dirty pages for the memory domain. Both system-wide and
1347per-cgroup dirty memory states are examined and the more restrictive
1348of the two is enforced.
1349
1350cgroup writeback requires explicit support from the underlying
1351filesystem. Currently, cgroup writeback is implemented on ext2, ext4
1352and btrfs. On other filesystems, all writeback IOs are attributed to
1353the root cgroup.
1354
1355There are inherent differences in memory and writeback management
1356which affects how cgroup ownership is tracked. Memory is tracked per
1357page while writeback per inode. For the purpose of writeback, an
1358inode is assigned to a cgroup and all IO requests to write dirty pages
1359from the inode are attributed to that cgroup.
1360
1361As cgroup ownership for memory is tracked per page, there can be pages
1362which are associated with different cgroups than the one the inode is
1363associated with. These are called foreign pages. The writeback
1364constantly keeps track of foreign pages and, if a particular foreign
1365cgroup becomes the majority over a certain period of time, switches
1366the ownership of the inode to that cgroup.
1367
1368While this model is enough for most use cases where a given inode is
1369mostly dirtied by a single cgroup even when the main writing cgroup
1370changes over time, use cases where multiple cgroups write to a single
1371inode simultaneously are not supported well. In such circumstances, a
1372significant portion of IOs are likely to be attributed incorrectly.
1373As memory controller assigns page ownership on the first use and
1374doesn't update it until the page is released, even if writeback
1375strictly follows page ownership, multiple cgroups dirtying overlapping
1376areas wouldn't work as expected. It's recommended to avoid such usage
1377patterns.
1378
1379The sysctl knobs which affect writeback behavior are applied to cgroup
1380writeback as follows.
1381
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001382 vm.dirty_background_ratio, vm.dirty_ratio
Tejun Heo6c292092015-11-16 11:13:34 -05001383 These ratios apply the same to cgroup writeback with the
1384 amount of available memory capped by limits imposed by the
1385 memory controller and system-wide clean memory.
1386
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001387 vm.dirty_background_bytes, vm.dirty_bytes
Tejun Heo6c292092015-11-16 11:13:34 -05001388 For cgroup writeback, this is calculated into ratio against
1389 total available memory and applied the same way as
1390 vm.dirty[_background]_ratio.
1391
1392
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001393PID
1394---
Hans Ragas20c56e52017-01-10 17:42:34 +00001395
1396The process number controller is used to allow a cgroup to stop any
1397new tasks from being fork()'d or clone()'d after a specified limit is
1398reached.
1399
1400The number of tasks in a cgroup can be exhausted in ways which other
1401controllers cannot prevent, thus warranting its own controller. For
1402example, a fork bomb is likely to exhaust the number of tasks before
1403hitting memory restrictions.
1404
1405Note that PIDs used in this controller refer to TIDs, process IDs as
1406used by the kernel.
1407
1408
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001409PID Interface Files
1410~~~~~~~~~~~~~~~~~~~
Hans Ragas20c56e52017-01-10 17:42:34 +00001411
1412 pids.max
Tobias Klauser312eb712017-02-17 18:44:11 +01001413 A read-write single value file which exists on non-root
1414 cgroups. The default is "max".
Hans Ragas20c56e52017-01-10 17:42:34 +00001415
Tobias Klauser312eb712017-02-17 18:44:11 +01001416 Hard limit of number of processes.
Hans Ragas20c56e52017-01-10 17:42:34 +00001417
1418 pids.current
Tobias Klauser312eb712017-02-17 18:44:11 +01001419 A read-only single value file which exists on all cgroups.
Hans Ragas20c56e52017-01-10 17:42:34 +00001420
Tobias Klauser312eb712017-02-17 18:44:11 +01001421 The number of processes currently in the cgroup and its
1422 descendants.
Hans Ragas20c56e52017-01-10 17:42:34 +00001423
1424Organisational operations are not blocked by cgroup policies, so it is
1425possible to have pids.current > pids.max. This can be done by either
1426setting the limit to be smaller than pids.current, or attaching enough
1427processes to the cgroup such that pids.current is larger than
1428pids.max. However, it is not possible to violate a cgroup PID policy
1429through fork() or clone(). These will return -EAGAIN if the creation
1430of a new process would cause a cgroup policy to be violated.
1431
1432
Roman Gushchin4ad5a322017-12-13 19:49:03 +00001433Device controller
1434-----------------
1435
1436Device controller manages access to device files. It includes both
1437creation of new device files (using mknod), and access to the
1438existing device files.
1439
1440Cgroup v2 device controller has no interface files and is implemented
1441on top of cgroup BPF. To control access to device files, a user may
1442create bpf programs of the BPF_CGROUP_DEVICE type and attach them
1443to cgroups. On an attempt to access a device file, corresponding
1444BPF programs will be executed, and depending on the return value
1445the attempt will succeed or fail with -EPERM.
1446
1447A BPF_CGROUP_DEVICE program takes a pointer to the bpf_cgroup_dev_ctx
1448structure, which describes the device access attempt: access type
1449(mknod/read/write) and device (type, major and minor numbers).
1450If the program returns 0, the attempt fails with -EPERM, otherwise
1451it succeeds.
1452
1453An example of BPF_CGROUP_DEVICE program may be found in the kernel
1454source tree in the tools/testing/selftests/bpf/dev_cgroup.c file.
1455
1456
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001457RDMA
1458----
Tejun Heo968ebff2017-01-29 14:35:20 -05001459
Parav Pandit9c1e67f2017-01-10 00:02:15 +00001460The "rdma" controller regulates the distribution and accounting of
1461of RDMA resources.
1462
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001463RDMA Interface Files
1464~~~~~~~~~~~~~~~~~~~~
Parav Pandit9c1e67f2017-01-10 00:02:15 +00001465
1466 rdma.max
1467 A readwrite nested-keyed file that exists for all the cgroups
1468 except root that describes current configured resource limit
1469 for a RDMA/IB device.
1470
1471 Lines are keyed by device name and are not ordered.
1472 Each line contains space separated resource name and its configured
1473 limit that can be distributed.
1474
1475 The following nested keys are defined.
1476
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001477 ========== =============================
Parav Pandit9c1e67f2017-01-10 00:02:15 +00001478 hca_handle Maximum number of HCA Handles
1479 hca_object Maximum number of HCA Objects
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001480 ========== =============================
Parav Pandit9c1e67f2017-01-10 00:02:15 +00001481
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001482 An example for mlx4 and ocrdma device follows::
Parav Pandit9c1e67f2017-01-10 00:02:15 +00001483
1484 mlx4_0 hca_handle=2 hca_object=2000
1485 ocrdma1 hca_handle=3 hca_object=max
1486
1487 rdma.current
1488 A read-only file that describes current resource usage.
1489 It exists for all the cgroup except root.
1490
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001491 An example for mlx4 and ocrdma device follows::
Parav Pandit9c1e67f2017-01-10 00:02:15 +00001492
1493 mlx4_0 hca_handle=1 hca_object=20
1494 ocrdma1 hca_handle=1 hca_object=23
1495
1496
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001497Misc
1498----
Tejun Heo63f1ca52017-02-02 13:50:35 -05001499
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001500perf_event
1501~~~~~~~~~~
Tejun Heo968ebff2017-01-29 14:35:20 -05001502
1503perf_event controller, if not mounted on a legacy hierarchy, is
1504automatically enabled on the v2 hierarchy so that perf events can
1505always be filtered by cgroup v2 path. The controller can still be
1506moved to a legacy hierarchy after v2 hierarchy is populated.
1507
1508
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001509Namespace
1510=========
Serge Hallynd4021f62016-01-29 02:54:10 -06001511
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001512Basics
1513------
Serge Hallynd4021f62016-01-29 02:54:10 -06001514
1515cgroup namespace provides a mechanism to virtualize the view of the
1516"/proc/$PID/cgroup" file and cgroup mounts. The CLONE_NEWCGROUP clone
1517flag can be used with clone(2) and unshare(2) to create a new cgroup
1518namespace. The process running inside the cgroup namespace will have
1519its "/proc/$PID/cgroup" output restricted to cgroupns root. The
1520cgroupns root is the cgroup of the process at the time of creation of
1521the cgroup namespace.
1522
1523Without cgroup namespace, the "/proc/$PID/cgroup" file shows the
1524complete path of the cgroup of a process. In a container setup where
1525a set of cgroups and namespaces are intended to isolate processes the
1526"/proc/$PID/cgroup" file may leak potential system level information
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001527to the isolated processes. For Example::
Serge Hallynd4021f62016-01-29 02:54:10 -06001528
1529 # cat /proc/self/cgroup
1530 0::/batchjobs/container_id1
1531
1532The path '/batchjobs/container_id1' can be considered as system-data
1533and undesirable to expose to the isolated processes. cgroup namespace
1534can be used to restrict visibility of this path. For example, before
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001535creating a cgroup namespace, one would see::
Serge Hallynd4021f62016-01-29 02:54:10 -06001536
1537 # ls -l /proc/self/ns/cgroup
1538 lrwxrwxrwx 1 root root 0 2014-07-15 10:37 /proc/self/ns/cgroup -> cgroup:[4026531835]
1539 # cat /proc/self/cgroup
1540 0::/batchjobs/container_id1
1541
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001542After unsharing a new namespace, the view changes::
Serge Hallynd4021f62016-01-29 02:54:10 -06001543
1544 # ls -l /proc/self/ns/cgroup
1545 lrwxrwxrwx 1 root root 0 2014-07-15 10:35 /proc/self/ns/cgroup -> cgroup:[4026532183]
1546 # cat /proc/self/cgroup
1547 0::/
1548
1549When some thread from a multi-threaded process unshares its cgroup
1550namespace, the new cgroupns gets applied to the entire process (all
1551the threads). This is natural for the v2 hierarchy; however, for the
1552legacy hierarchies, this may be unexpected.
1553
1554A cgroup namespace is alive as long as there are processes inside or
1555mounts pinning it. When the last usage goes away, the cgroup
1556namespace is destroyed. The cgroupns root and the actual cgroups
1557remain.
1558
1559
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001560The Root and Views
1561------------------
Serge Hallynd4021f62016-01-29 02:54:10 -06001562
1563The 'cgroupns root' for a cgroup namespace is the cgroup in which the
1564process calling unshare(2) is running. For example, if a process in
1565/batchjobs/container_id1 cgroup calls unshare, cgroup
1566/batchjobs/container_id1 becomes the cgroupns root. For the
1567init_cgroup_ns, this is the real root ('/') cgroup.
1568
1569The cgroupns root cgroup does not change even if the namespace creator
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001570process later moves to a different cgroup::
Serge Hallynd4021f62016-01-29 02:54:10 -06001571
1572 # ~/unshare -c # unshare cgroupns in some cgroup
1573 # cat /proc/self/cgroup
1574 0::/
1575 # mkdir sub_cgrp_1
1576 # echo 0 > sub_cgrp_1/cgroup.procs
1577 # cat /proc/self/cgroup
1578 0::/sub_cgrp_1
1579
1580Each process gets its namespace-specific view of "/proc/$PID/cgroup"
1581
1582Processes running inside the cgroup namespace will be able to see
1583cgroup paths (in /proc/self/cgroup) only inside their root cgroup.
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001584From within an unshared cgroupns::
Serge Hallynd4021f62016-01-29 02:54:10 -06001585
1586 # sleep 100000 &
1587 [1] 7353
1588 # echo 7353 > sub_cgrp_1/cgroup.procs
1589 # cat /proc/7353/cgroup
1590 0::/sub_cgrp_1
1591
1592From the initial cgroup namespace, the real cgroup path will be
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001593visible::
Serge Hallynd4021f62016-01-29 02:54:10 -06001594
1595 $ cat /proc/7353/cgroup
1596 0::/batchjobs/container_id1/sub_cgrp_1
1597
1598From a sibling cgroup namespace (that is, a namespace rooted at a
1599different cgroup), the cgroup path relative to its own cgroup
1600namespace root will be shown. For instance, if PID 7353's cgroup
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001601namespace root is at '/batchjobs/container_id2', then it will see::
Serge Hallynd4021f62016-01-29 02:54:10 -06001602
1603 # cat /proc/7353/cgroup
1604 0::/../container_id2/sub_cgrp_1
1605
1606Note that the relative path always starts with '/' to indicate that
1607its relative to the cgroup namespace root of the caller.
1608
1609
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001610Migration and setns(2)
1611----------------------
Serge Hallynd4021f62016-01-29 02:54:10 -06001612
1613Processes inside a cgroup namespace can move into and out of the
1614namespace root if they have proper access to external cgroups. For
1615example, from inside a namespace with cgroupns root at
1616/batchjobs/container_id1, and assuming that the global hierarchy is
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001617still accessible inside cgroupns::
Serge Hallynd4021f62016-01-29 02:54:10 -06001618
1619 # cat /proc/7353/cgroup
1620 0::/sub_cgrp_1
1621 # echo 7353 > batchjobs/container_id2/cgroup.procs
1622 # cat /proc/7353/cgroup
1623 0::/../container_id2
1624
1625Note that this kind of setup is not encouraged. A task inside cgroup
1626namespace should only be exposed to its own cgroupns hierarchy.
1627
1628setns(2) to another cgroup namespace is allowed when:
1629
1630(a) the process has CAP_SYS_ADMIN against its current user namespace
1631(b) the process has CAP_SYS_ADMIN against the target cgroup
1632 namespace's userns
1633
1634No implicit cgroup changes happen with attaching to another cgroup
1635namespace. It is expected that the someone moves the attaching
1636process under the target cgroup namespace root.
1637
1638
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001639Interaction with Other Namespaces
1640---------------------------------
Serge Hallynd4021f62016-01-29 02:54:10 -06001641
1642Namespace specific cgroup hierarchy can be mounted by a process
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001643running inside a non-init cgroup namespace::
Serge Hallynd4021f62016-01-29 02:54:10 -06001644
1645 # mount -t cgroup2 none $MOUNT_POINT
1646
1647This will mount the unified cgroup hierarchy with cgroupns root as the
1648filesystem root. The process needs CAP_SYS_ADMIN against its user and
1649mount namespaces.
1650
1651The virtualization of /proc/self/cgroup file combined with restricting
1652the view of cgroup hierarchy by namespace-private cgroupfs mount
1653provides a properly isolated cgroup view inside the container.
1654
1655
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001656Information on Kernel Programming
1657=================================
Tejun Heo6c292092015-11-16 11:13:34 -05001658
1659This section contains kernel programming information in the areas
1660where interacting with cgroup is necessary. cgroup core and
1661controllers are not covered.
1662
1663
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001664Filesystem Support for Writeback
1665--------------------------------
Tejun Heo6c292092015-11-16 11:13:34 -05001666
1667A filesystem can support cgroup writeback by updating
1668address_space_operations->writepage[s]() to annotate bio's using the
1669following two functions.
1670
1671 wbc_init_bio(@wbc, @bio)
Tejun Heo6c292092015-11-16 11:13:34 -05001672 Should be called for each bio carrying writeback data and
1673 associates the bio with the inode's owner cgroup. Can be
1674 called anytime between bio allocation and submission.
1675
1676 wbc_account_io(@wbc, @page, @bytes)
Tejun Heo6c292092015-11-16 11:13:34 -05001677 Should be called for each data segment being written out.
1678 While this function doesn't care exactly when it's called
1679 during the writeback session, it's the easiest and most
1680 natural to call it as data segments are added to a bio.
1681
1682With writeback bio's annotated, cgroup support can be enabled per
1683super_block by setting SB_I_CGROUPWB in ->s_iflags. This allows for
1684selective disabling of cgroup writeback support which is helpful when
1685certain filesystem features, e.g. journaled data mode, are
1686incompatible.
1687
1688wbc_init_bio() binds the specified bio to its cgroup. Depending on
1689the configuration, the bio may be executed at a lower priority and if
1690the writeback session is holding shared resources, e.g. a journal
1691entry, may lead to priority inversion. There is no one easy solution
1692for the problem. Filesystems can try to work around specific problem
1693cases by skipping wbc_init_bio() or using bio_associate_blkcg()
1694directly.
1695
1696
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001697Deprecated v1 Core Features
1698===========================
Tejun Heo6c292092015-11-16 11:13:34 -05001699
1700- Multiple hierarchies including named ones are not supported.
1701
Tejun Heo5136f632017-06-27 14:30:28 -04001702- All v1 mount options are not supported.
Tejun Heo6c292092015-11-16 11:13:34 -05001703
1704- The "tasks" file is removed and "cgroup.procs" is not sorted.
1705
1706- "cgroup.clone_children" is removed.
1707
1708- /proc/cgroups is meaningless for v2. Use "cgroup.controllers" file
1709 at the root instead.
1710
1711
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001712Issues with v1 and Rationales for v2
1713====================================
Tejun Heo6c292092015-11-16 11:13:34 -05001714
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001715Multiple Hierarchies
1716--------------------
Tejun Heo6c292092015-11-16 11:13:34 -05001717
1718cgroup v1 allowed an arbitrary number of hierarchies and each
1719hierarchy could host any number of controllers. While this seemed to
1720provide a high level of flexibility, it wasn't useful in practice.
1721
1722For example, as there is only one instance of each controller, utility
1723type controllers such as freezer which can be useful in all
1724hierarchies could only be used in one. The issue is exacerbated by
1725the fact that controllers couldn't be moved to another hierarchy once
1726hierarchies were populated. Another issue was that all controllers
1727bound to a hierarchy were forced to have exactly the same view of the
1728hierarchy. It wasn't possible to vary the granularity depending on
1729the specific controller.
1730
1731In practice, these issues heavily limited which controllers could be
1732put on the same hierarchy and most configurations resorted to putting
1733each controller on its own hierarchy. Only closely related ones, such
1734as the cpu and cpuacct controllers, made sense to be put on the same
1735hierarchy. This often meant that userland ended up managing multiple
1736similar hierarchies repeating the same steps on each hierarchy
1737whenever a hierarchy management operation was necessary.
1738
1739Furthermore, support for multiple hierarchies came at a steep cost.
1740It greatly complicated cgroup core implementation but more importantly
1741the support for multiple hierarchies restricted how cgroup could be
1742used in general and what controllers was able to do.
1743
1744There was no limit on how many hierarchies there might be, which meant
1745that a thread's cgroup membership couldn't be described in finite
1746length. The key might contain any number of entries and was unlimited
1747in length, which made it highly awkward to manipulate and led to
1748addition of controllers which existed only to identify membership,
1749which in turn exacerbated the original problem of proliferating number
1750of hierarchies.
1751
1752Also, as a controller couldn't have any expectation regarding the
1753topologies of hierarchies other controllers might be on, each
1754controller had to assume that all other controllers were attached to
1755completely orthogonal hierarchies. This made it impossible, or at
1756least very cumbersome, for controllers to cooperate with each other.
1757
1758In most use cases, putting controllers on hierarchies which are
1759completely orthogonal to each other isn't necessary. What usually is
1760called for is the ability to have differing levels of granularity
1761depending on the specific controller. In other words, hierarchy may
1762be collapsed from leaf towards root when viewed from specific
1763controllers. For example, a given configuration might not care about
1764how memory is distributed beyond a certain level while still wanting
1765to control how CPU cycles are distributed.
1766
1767
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001768Thread Granularity
1769------------------
Tejun Heo6c292092015-11-16 11:13:34 -05001770
1771cgroup v1 allowed threads of a process to belong to different cgroups.
1772This didn't make sense for some controllers and those controllers
1773ended up implementing different ways to ignore such situations but
1774much more importantly it blurred the line between API exposed to
1775individual applications and system management interface.
1776
1777Generally, in-process knowledge is available only to the process
1778itself; thus, unlike service-level organization of processes,
1779categorizing threads of a process requires active participation from
1780the application which owns the target process.
1781
1782cgroup v1 had an ambiguously defined delegation model which got abused
1783in combination with thread granularity. cgroups were delegated to
1784individual applications so that they can create and manage their own
1785sub-hierarchies and control resource distributions along them. This
1786effectively raised cgroup to the status of a syscall-like API exposed
1787to lay programs.
1788
1789First of all, cgroup has a fundamentally inadequate interface to be
1790exposed this way. For a process to access its own knobs, it has to
1791extract the path on the target hierarchy from /proc/self/cgroup,
1792construct the path by appending the name of the knob to the path, open
1793and then read and/or write to it. This is not only extremely clunky
1794and unusual but also inherently racy. There is no conventional way to
1795define transaction across the required steps and nothing can guarantee
1796that the process would actually be operating on its own sub-hierarchy.
1797
1798cgroup controllers implemented a number of knobs which would never be
1799accepted as public APIs because they were just adding control knobs to
1800system-management pseudo filesystem. cgroup ended up with interface
1801knobs which were not properly abstracted or refined and directly
1802revealed kernel internal details. These knobs got exposed to
1803individual applications through the ill-defined delegation mechanism
1804effectively abusing cgroup as a shortcut to implementing public APIs
1805without going through the required scrutiny.
1806
1807This was painful for both userland and kernel. Userland ended up with
1808misbehaving and poorly abstracted interfaces and kernel exposing and
1809locked into constructs inadvertently.
1810
1811
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001812Competition Between Inner Nodes and Threads
1813-------------------------------------------
Tejun Heo6c292092015-11-16 11:13:34 -05001814
1815cgroup v1 allowed threads to be in any cgroups which created an
1816interesting problem where threads belonging to a parent cgroup and its
1817children cgroups competed for resources. This was nasty as two
1818different types of entities competed and there was no obvious way to
1819settle it. Different controllers did different things.
1820
1821The cpu controller considered threads and cgroups as equivalents and
1822mapped nice levels to cgroup weights. This worked for some cases but
1823fell flat when children wanted to be allocated specific ratios of CPU
1824cycles and the number of internal threads fluctuated - the ratios
1825constantly changed as the number of competing entities fluctuated.
1826There also were other issues. The mapping from nice level to weight
1827wasn't obvious or universal, and there were various other knobs which
1828simply weren't available for threads.
1829
1830The io controller implicitly created a hidden leaf node for each
1831cgroup to host the threads. The hidden leaf had its own copies of all
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001832the knobs with ``leaf_`` prefixed. While this allowed equivalent
Tejun Heo6c292092015-11-16 11:13:34 -05001833control over internal threads, it was with serious drawbacks. It
1834always added an extra layer of nesting which wouldn't be necessary
1835otherwise, made the interface messy and significantly complicated the
1836implementation.
1837
1838The memory controller didn't have a way to control what happened
1839between internal tasks and child cgroups and the behavior was not
1840clearly defined. There were attempts to add ad-hoc behaviors and
1841knobs to tailor the behavior to specific workloads which would have
1842led to problems extremely difficult to resolve in the long term.
1843
1844Multiple controllers struggled with internal tasks and came up with
1845different ways to deal with it; unfortunately, all the approaches were
1846severely flawed and, furthermore, the widely different behaviors
1847made cgroup as a whole highly inconsistent.
1848
1849This clearly is a problem which needs to be addressed from cgroup core
1850in a uniform way.
1851
1852
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001853Other Interface Issues
1854----------------------
Tejun Heo6c292092015-11-16 11:13:34 -05001855
1856cgroup v1 grew without oversight and developed a large number of
1857idiosyncrasies and inconsistencies. One issue on the cgroup core side
1858was how an empty cgroup was notified - a userland helper binary was
1859forked and executed for each event. The event delivery wasn't
1860recursive or delegatable. The limitations of the mechanism also led
1861to in-kernel event delivery filtering mechanism further complicating
1862the interface.
1863
1864Controller interfaces were problematic too. An extreme example is
1865controllers completely ignoring hierarchical organization and treating
1866all cgroups as if they were all located directly under the root
1867cgroup. Some controllers exposed a large amount of inconsistent
1868implementation details to userland.
1869
1870There also was no consistency across controllers. When a new cgroup
1871was created, some controllers defaulted to not imposing extra
1872restrictions while others disallowed any resource usage until
1873explicitly configured. Configuration knobs for the same type of
1874control used widely differing naming schemes and formats. Statistics
1875and information knobs were named arbitrarily and used different
1876formats and units even in the same controller.
1877
1878cgroup v2 establishes common conventions where appropriate and updates
1879controllers so that they expose minimal and consistent interfaces.
1880
1881
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001882Controller Issues and Remedies
1883------------------------------
Tejun Heo6c292092015-11-16 11:13:34 -05001884
Mauro Carvalho Chehab633b11b2017-05-14 08:48:40 -03001885Memory
1886~~~~~~
Tejun Heo6c292092015-11-16 11:13:34 -05001887
1888The original lower boundary, the soft limit, is defined as a limit
1889that is per default unset. As a result, the set of cgroups that
1890global reclaim prefers is opt-in, rather than opt-out. The costs for
1891optimizing these mostly negative lookups are so high that the
1892implementation, despite its enormous size, does not even provide the
1893basic desirable behavior. First off, the soft limit has no
1894hierarchical meaning. All configured groups are organized in a global
1895rbtree and treated like equal peers, regardless where they are located
1896in the hierarchy. This makes subtree delegation impossible. Second,
1897the soft limit reclaim pass is so aggressive that it not just
1898introduces high allocation latencies into the system, but also impacts
1899system performance due to overreclaim, to the point where the feature
1900becomes self-defeating.
1901
1902The memory.low boundary on the other hand is a top-down allocated
1903reserve. A cgroup enjoys reclaim protection when it and all its
1904ancestors are below their low boundaries, which makes delegation of
1905subtrees possible. Secondly, new cgroups have no reserve per default
1906and in the common case most cgroups are eligible for the preferred
1907reclaim pass. This allows the new low boundary to be efficiently
1908implemented with just a minor addition to the generic reclaim code,
1909without the need for out-of-band data structures and reclaim passes.
1910Because the generic reclaim code considers all cgroups except for the
1911ones running low in the preferred first reclaim pass, overreclaim of
1912individual groups is eliminated as well, resulting in much better
1913overall workload performance.
1914
1915The original high boundary, the hard limit, is defined as a strict
1916limit that can not budge, even if the OOM killer has to be called.
1917But this generally goes against the goal of making the most out of the
1918available memory. The memory consumption of workloads varies during
1919runtime, and that requires users to overcommit. But doing that with a
1920strict upper limit requires either a fairly accurate prediction of the
1921working set size or adding slack to the limit. Since working set size
1922estimation is hard and error prone, and getting it wrong results in
1923OOM kills, most users tend to err on the side of a looser limit and
1924end up wasting precious resources.
1925
1926The memory.high boundary on the other hand can be set much more
1927conservatively. When hit, it throttles allocations by forcing them
1928into direct reclaim to work off the excess, but it never invokes the
1929OOM killer. As a result, a high boundary that is chosen too
1930aggressively will not terminate the processes, but instead it will
1931lead to gradual performance degradation. The user can monitor this
1932and make corrections until the minimal memory footprint that still
1933gives acceptable performance is found.
1934
1935In extreme cases, with many concurrent allocations and a complete
1936breakdown of reclaim progress within the group, the high boundary can
1937be exceeded. But even then it's mostly better to satisfy the
1938allocation from the slack available in other groups or the rest of the
1939system than killing the group. Otherwise, memory.max is there to
1940limit this type of spillover and ultimately contain buggy or even
1941malicious applications.
Vladimir Davydov3e24b192016-01-20 15:03:13 -08001942
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07001943Setting the original memory.limit_in_bytes below the current usage was
1944subject to a race condition, where concurrent charges could cause the
1945limit setting to fail. memory.max on the other hand will first set the
1946limit to prevent new charges, and then reclaim and OOM kill until the
1947new limit is met - or the task writing to memory.max is killed.
1948
Vladimir Davydov3e24b192016-01-20 15:03:13 -08001949The combined memory+swap accounting and limiting is replaced by real
1950control over swap space.
1951
1952The main argument for a combined memory+swap facility in the original
1953cgroup design was that global or parental pressure would always be
1954able to swap all anonymous memory of a child group, regardless of the
1955child's own (possibly untrusted) configuration. However, untrusted
1956groups can sabotage swapping by other means - such as referencing its
1957anonymous memory in a tight loop - and an admin can not assume full
1958swappability when overcommitting untrusted jobs.
1959
1960For trusted jobs, on the other hand, a combined counter is not an
1961intuitive userspace interface, and it flies in the face of the idea
1962that cgroup controllers should account and limit specific physical
1963resources. Swap space is a resource like all others in the system,
1964and that's why unified hierarchy allows distributing it separately.