wait.2: Add ESRCH for when pid == INT_MIN
[man-pages.git] / man7 / cgroups.7
bloba5aa4a37d1c5f4bf3206499060ba7cb3ef26ed18
1 .\" Copyright (C) 2015 Serge Hallyn <serge@hallyn.com>
2 .\" and Copyright (C) 2016, 2017 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH CGROUPS 7 2021-03-22 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 cgroups \- Linux control groups
29 .SH DESCRIPTION
30 Control groups, usually referred to as cgroups,
31 are a Linux kernel feature which allow processes to
32 be organized into hierarchical groups whose usage of
33 various types of resources can then be limited and monitored.
34 The kernel's cgroup interface is provided through
35 a pseudo-filesystem called cgroupfs.
36 Grouping is implemented in the core cgroup kernel code,
37 while resource tracking and limits are implemented in
38 a set of per-resource-type subsystems (memory, CPU, and so on).
39 .\"
40 .SS Terminology
42 .I cgroup
43 is a collection of processes that are bound to a set of
44 limits or parameters defined via the cgroup filesystem.
45 .PP
47 .I subsystem
48 is a kernel component that modifies the behavior of
49 the processes in a cgroup.
50 Various subsystems have been implemented, making it possible to do things
51 such as limiting the amount of CPU time and memory available to a cgroup,
52 accounting for the CPU time used by a cgroup,
53 and freezing and resuming execution of the processes in a cgroup.
54 Subsystems are sometimes also known as
55 .IR "resource controllers"
56 (or simply, controllers).
57 .PP
58 The cgroups for a controller are arranged in a
59 .IR hierarchy .
60 This hierarchy is defined by creating, removing, and
61 renaming subdirectories within the cgroup filesystem.
62 At each level of the hierarchy, attributes (e.g., limits) can be defined.
63 The limits, control, and accounting provided by cgroups generally have
64 effect throughout the subhierarchy underneath the cgroup where the
65 attributes are defined.
66 Thus, for example, the limits placed on
67 a cgroup at a higher level in the hierarchy cannot be exceeded
68 by descendant cgroups.
69 .\"
70 .SS Cgroups version 1 and version 2
71 The initial release of the cgroups implementation was in Linux 2.6.24.
72 Over time, various cgroup controllers have been added
73 to allow the management of various types of resources.
74 However, the development of these controllers was largely uncoordinated,
75 with the result that many inconsistencies arose between controllers
76 and management of the cgroup hierarchies became rather complex.
77 A longer description of these problems can be found in the kernel
78 source file
79 .IR Documentation/admin\-guide/cgroup\-v2.rst
80 (or
81 .IR Documentation/cgroup\-v2.txt
82 in Linux 4.17 and earlier).
83 .PP
84 Because of the problems with the initial cgroups implementation
85 (cgroups version 1),
86 starting in Linux 3.10, work began on a new,
87 orthogonal implementation to remedy these problems.
88 Initially marked experimental, and hidden behind the
89 .I "\-o\ __DEVEL__sane_behavior"
90 mount option, the new version (cgroups version 2)
91 was eventually made official with the release of Linux 4.5.
92 Differences between the two versions are described in the text below.
93 The file
94 .IR cgroup.sane_behavior ,
95 present in cgroups v1, is a relic of this mount option. The file
96 always reports "0" and is only retained for backward compatibility.
97 .PP
98 Although cgroups v2 is intended as a replacement for cgroups v1,
99 the older system continues to exist
100 (and for compatibility reasons is unlikely to be removed).
101 Currently, cgroups v2 implements only a subset of the controllers
102 available in cgroups v1.
103 The two systems are implemented so that both v1 controllers and
104 v2 controllers can be mounted on the same system.
105 Thus, for example, it is possible to use those controllers
106 that are supported under version 2,
107 while also using version 1 controllers
108 where version 2 does not yet support those controllers.
109 The only restriction here is that a controller can't be simultaneously
110 employed in both a cgroups v1 hierarchy and in the cgroups v2 hierarchy.
112 .SH CGROUPS VERSION 1
113 Under cgroups v1, each controller may be mounted against a separate
114 cgroup filesystem that provides its own hierarchical organization of the
115 processes on the system.
116 It is also possible to comount multiple (or even all) cgroups v1 controllers
117 against the same cgroup filesystem, meaning that the comounted controllers
118 manage the same hierarchical organization of processes.
120 For each mounted hierarchy,
121 the directory tree mirrors the control group hierarchy.
122 Each control group is represented by a directory, with each of its child
123 control cgroups represented as a child directory.
124 For instance,
125 .IR /user/joe/1.session
126 represents control group
127 .IR 1.session ,
128 which is a child of cgroup
129 .IR joe ,
130 which is a child of
131 .IR /user .
132 Under each cgroup directory is a set of files which can be read or
133 written to, reflecting resource limits and a few general cgroup
134 properties.
136 .SS Tasks (threads) versus processes
137 In cgroups v1, a distinction is drawn between
138 .I processes
140 .IR tasks .
141 In this view, a process can consist of multiple tasks
142 (more commonly called threads, from a user-space perspective,
143 and called such in the remainder of this man page).
144 In cgroups v1, it is possible to independently manipulate
145 the cgroup memberships of the threads in a process.
147 The cgroups v1 ability to split threads across different cgroups
148 caused problems in some cases.
149 For example, it made no sense for the
150 .I memory
151 controller,
152 since all of the threads of a process share a single address space.
153 Because of these problems,
154 the ability to independently manipulate the cgroup memberships
155 of the threads in a process was removed in the initial cgroups v2
156 implementation, and subsequently restored in a more limited form
157 (see the discussion of "thread mode" below).
159 .SS Mounting v1 controllers
160 The use of cgroups requires a kernel built with the
161 .BR CONFIG_CGROUP
162 option.
163 In addition, each of the v1 controllers has an associated
164 configuration option that must be set in order to employ that controller.
166 In order to use a v1 controller,
167 it must be mounted against a cgroup filesystem.
168 The usual place for such mounts is under a
169 .BR tmpfs (5)
170 filesystem mounted at
171 .IR /sys/fs/cgroup .
172 Thus, one might mount the
173 .I cpu
174 controller as follows:
176 .in +4n
178 mount \-t cgroup \-o cpu none /sys/fs/cgroup/cpu
182 It is possible to comount multiple controllers against the same hierarchy.
183 For example, here the
184 .IR cpu
186 .IR cpuacct
187 controllers are comounted against a single hierarchy:
189 .in +4n
191 mount \-t cgroup \-o cpu,cpuacct none /sys/fs/cgroup/cpu,cpuacct
195 Comounting controllers has the effect that a process is in the same cgroup for
196 all of the comounted controllers.
197 Separately mounting controllers allows a process to
198 be in cgroup
199 .I /foo1
200 for one controller while being in
201 .I /foo2/foo3
202 for another.
204 It is possible to comount all v1 controllers against the same hierarchy:
206 .in +4n
208 mount \-t cgroup \-o all cgroup /sys/fs/cgroup
212 (One can achieve the same result by omitting
213 .IR "\-o all" ,
214 since it is the default if no controllers are explicitly specified.)
216 It is not possible to mount the same controller
217 against multiple cgroup hierarchies.
218 For example, it is not possible to mount both the
219 .I cpu
221 .I cpuacct
222 controllers against one hierarchy, and to mount the
223 .I cpu
224 controller alone against another hierarchy.
225 It is possible to create multiple mount points with exactly
226 the same set of comounted controllers.
227 However, in this case all that results is multiple mount points
228 providing a view of the same hierarchy.
230 Note that on many systems, the v1 controllers are automatically mounted under
231 .IR /sys/fs/cgroup ;
232 in particular,
233 .BR systemd (1)
234 automatically creates such mount points.
236 .SS Unmounting v1 controllers
237 A mounted cgroup filesystem can be unmounted using the
238 .BR umount (8)
239 command, as in the following example:
241 .in +4n
243 umount /sys/fs/cgroup/pids
247 .IR "But note well" :
248 a cgroup filesystem is unmounted only if it is not busy,
249 that is, it has no child cgroups.
250 If this is not the case, then the only effect of the
251 .BR umount (8)
252 is to make the mount invisible.
253 Thus, to ensure that the mount point is really removed,
254 one must first remove all child cgroups,
255 which in turn can be done only after all member processes
256 have been moved from those cgroups to the root cgroup.
258 .SS Cgroups version 1 controllers
259 Each of the cgroups version 1 controllers is governed
260 by a kernel configuration option (listed below).
261 Additionally, the availability of the cgroups feature is governed by the
262 .BR CONFIG_CGROUPS
263 kernel configuration option.
265 .IR cpu " (since Linux 2.6.24; " \fBCONFIG_CGROUP_SCHED\fP )
266 Cgroups can be guaranteed a minimum number of "CPU shares"
267 when a system is busy.
268 This does not limit a cgroup's CPU usage if the CPUs are not busy.
269 For further information, see
270 .IR Documentation/scheduler/sched\-design\-CFS.rst
272 .IR Documentation/scheduler/sched\-design\-CFS.txt
273 in Linux 5.2 and earlier).
275 In Linux 3.2,
276 this controller was extended to provide CPU "bandwidth" control.
277 If the kernel is configured with
278 .BR CONFIG_CFS_BANDWIDTH ,
279 then within each scheduling period
280 (defined via a file in the cgroup directory), it is possible to define
281 an upper limit on the CPU time allocated to the processes in a cgroup.
282 This upper limit applies even if there is no other competition for the CPU.
283 Further information can be found in the kernel source file
284 .IR Documentation/scheduler/sched\-bwc.rst
286 .IR Documentation/scheduler/sched\-bwc.txt
287 in Linux 5.2 and earlier).
289 .IR cpuacct " (since Linux 2.6.24; " \fBCONFIG_CGROUP_CPUACCT\fP )
290 This provides accounting for CPU usage by groups of processes.
292 Further information can be found in the kernel source file
293 .IR Documentation/admin\-guide/cgroup\-v1/cpuacct.rst
295 .IR Documentation/cgroup\-v1/cpuacct.txt
296 in Linux 5.2 and earlier).
298 .IR cpuset " (since Linux 2.6.24; " \fBCONFIG_CPUSETS\fP )
299 This cgroup can be used to bind the processes in a cgroup to
300 a specified set of CPUs and NUMA nodes.
302 Further information can be found in the kernel source file
303 .IR Documentation/admin\-guide/cgroup\-v1/cpusets.rst
305 .IR Documentation/cgroup\-v1/cpusets.txt
306 in Linux 5.2 and earlier).
309 .IR memory " (since Linux 2.6.25; " \fBCONFIG_MEMCG\fP )
310 The memory controller supports reporting and limiting of process memory, kernel
311 memory, and swap used by cgroups.
313 Further information can be found in the kernel source file
314 .IR Documentation/admin\-guide/cgroup\-v1/memory.rst
316 .IR Documentation/cgroup\-v1/memory.txt
317 in Linux 5.2 and earlier).
319 .IR devices " (since Linux 2.6.26; " \fBCONFIG_CGROUP_DEVICE\fP )
320 This supports controlling which processes may create (mknod) devices as
321 well as open them for reading or writing.
322 The policies may be specified as allow-lists and deny-lists.
323 Hierarchy is enforced, so new rules must not
324 violate existing rules for the target or ancestor cgroups.
326 Further information can be found in the kernel source file
327 .IR Documentation/admin\-guide/cgroup\-v1/devices.rst
329 .IR Documentation/cgroup\-v1/devices.txt
330 in Linux 5.2 and earlier).
332 .IR freezer " (since Linux 2.6.28; " \fBCONFIG_CGROUP_FREEZER\fP )
334 .IR freezer
335 cgroup can suspend and restore (resume) all processes in a cgroup.
336 Freezing a cgroup
337 .I /A
338 also causes its children, for example, processes in
339 .IR /A/B ,
340 to be frozen.
342 Further information can be found in the kernel source file
343 .IR Documentation/admin\-guide/cgroup\-v1/freezer\-subsystem.rst
345 .IR Documentation/cgroup\-v1/freezer\-subsystem.txt
346 in Linux 5.2 and earlier).
348 .IR net_cls " (since Linux 2.6.29; " \fBCONFIG_CGROUP_NET_CLASSID\fP )
349 This places a classid, specified for the cgroup, on network packets
350 created by a cgroup.
351 These classids can then be used in firewall rules,
352 as well as used to shape traffic using
353 .BR tc (8).
354 This applies only to packets
355 leaving the cgroup, not to traffic arriving at the cgroup.
357 Further information can be found in the kernel source file
358 .IR Documentation/admin\-guide/cgroup\-v1/net_cls.rst
360 .IR Documentation/cgroup\-v1/net_cls.txt
361 in Linux 5.2 and earlier).
363 .IR blkio " (since Linux 2.6.33; " \fBCONFIG_BLK_CGROUP\fP )
365 .I blkio
366 cgroup controls and limits access to specified block devices by
367 applying IO control in the form of throttling and upper limits against leaf
368 nodes and intermediate nodes in the storage hierarchy.
370 Two policies are available.
371 The first is a proportional-weight time-based division
372 of disk implemented with CFQ.
373 This is in effect for leaf nodes using CFQ.
374 The second is a throttling policy which specifies
375 upper I/O rate limits on a device.
377 Further information can be found in the kernel source file
378 .IR Documentation/admin\-guide/cgroup\-v1/blkio\-controller.rst
380 .IR Documentation/cgroup\-v1/blkio\-controller.txt
381 in Linux 5.2 and earlier).
383 .IR perf_event " (since Linux 2.6.39; " \fBCONFIG_CGROUP_PERF\fP )
384 This controller allows
385 .I perf
386 monitoring of the set of processes grouped in a cgroup.
388 Further information can be found in the kernel source files
390 .IR net_prio " (since Linux 3.3; " \fBCONFIG_CGROUP_NET_PRIO\fP )
391 This allows priorities to be specified, per network interface, for cgroups.
393 Further information can be found in the kernel source file
394 .IR Documentation/admin\-guide/cgroup\-v1/net_prio.rst
396 .IR Documentation/cgroup\-v1/net_prio.txt
397 in Linux 5.2 and earlier).
399 .IR hugetlb " (since Linux 3.5; " \fBCONFIG_CGROUP_HUGETLB\fP )
400 This supports limiting the use of huge pages by cgroups.
402 Further information can be found in the kernel source file
403 .IR Documentation/admin\-guide/cgroup\-v1/hugetlb.rst
405 .IR Documentation/cgroup\-v1/hugetlb.txt
406 in Linux 5.2 and earlier).
408 .IR pids " (since Linux 4.3; " \fBCONFIG_CGROUP_PIDS\fP )
409 This controller permits limiting the number of process that may be created
410 in a cgroup (and its descendants).
412 Further information can be found in the kernel source file
413 .IR Documentation/admin\-guide/cgroup\-v1/pids.rst
415 .IR Documentation/cgroup\-v1/pids.txt
416 in Linux 5.2 and earlier).
418 .IR rdma " (since Linux 4.11; " \fBCONFIG_CGROUP_RDMA\fP )
419 The RDMA controller permits limiting the use of
420 RDMA/IB-specific resources per cgroup.
422 Further information can be found in the kernel source file
423 .IR Documentation/admin\-guide/cgroup\-v1/rdma.rst
425 .IR Documentation/cgroup\-v1/rdma.txt
426 in Linux 5.2 and earlier).
428 .SS Creating cgroups and moving processes
429 A cgroup filesystem initially contains a single root cgroup, '/',
430 which all processes belong to.
431 A new cgroup is created by creating a directory in the cgroup filesystem:
433 .in +4n
435 mkdir /sys/fs/cgroup/cpu/cg1
439 This creates a new empty cgroup.
441 A process may be moved to this cgroup by writing its PID into the cgroup's
442 .I cgroup.procs
443 file:
445 .in +4n
447 echo $$ > /sys/fs/cgroup/cpu/cg1/cgroup.procs
451 Only one PID at a time should be written to this file.
453 Writing the value 0 to a
454 .IR cgroup.procs
455 file causes the writing process to be moved to the corresponding cgroup.
457 When writing a PID into the
458 .IR cgroup.procs ,
459 all threads in the process are moved into the new cgroup at once.
461 Within a hierarchy, a process can be a member of exactly one cgroup.
462 Writing a process's PID to a
463 .IR cgroup.procs
464 file automatically removes it from the cgroup of
465 which it was previously a member.
468 .I cgroup.procs
469 file can be read to obtain a list of the processes that are
470 members of a cgroup.
471 The returned list of PIDs is not guaranteed to be in order.
472 Nor is it guaranteed to be free of duplicates.
473 (For example, a PID may be recycled while reading from the list.)
475 In cgroups v1, an individual thread can be moved to
476 another cgroup by writing its thread ID
477 (i.e., the kernel thread ID returned by
478 .BR clone (2)
480 .BR gettid (2))
481 to the
482 .IR tasks
483 file in a cgroup directory.
484 This file can be read to discover the set of threads
485 that are members of the cgroup.
487 .SS Removing cgroups
488 To remove a cgroup,
489 it must first have no child cgroups and contain no (nonzombie) processes.
490 So long as that is the case, one can simply
491 remove the corresponding directory pathname.
492 Note that files in a cgroup directory cannot and need not be
493 removed.
495 .SS Cgroups v1 release notification
496 Two files can be used to determine whether the kernel provides
497 notifications when a cgroup becomes empty.
498 A cgroup is considered to be empty when it contains no child
499 cgroups and no member processes.
501 A special file in the root directory of each cgroup hierarchy,
502 .IR release_agent ,
503 can be used to register the pathname of a program that may be invoked when
504 a cgroup in the hierarchy becomes empty.
505 The pathname of the newly empty cgroup (relative to the cgroup mount point)
506 is provided as the sole command-line argument when the
507 .IR release_agent
508 program is invoked.
510 .IR release_agent
511 program might remove the cgroup directory,
512 or perhaps repopulate it with a process.
514 The default value of the
515 .IR release_agent
516 file is empty, meaning that no release agent is invoked.
518 The content of the
519 .I release_agent
520 file can also be specified via a mount option when the
521 cgroup filesystem is mounted:
523 .in +4n
525 mount \-o release_agent=pathname ...
529 Whether or not the
530 .IR release_agent
531 program is invoked when a particular cgroup becomes empty is determined
532 by the value in the
533 .IR notify_on_release
534 file in the corresponding cgroup directory.
535 If this file contains the value 0, then the
536 .IR release_agent
537 program is not invoked.
538 If it contains the value 1, the
539 .IR release_agent
540 program is invoked.
541 The default value for this file in the root cgroup is 0.
542 At the time when a new cgroup is created,
543 the value in this file is inherited from the corresponding file
544 in the parent cgroup.
546 .SS Cgroup v1 named hierarchies
547 In cgroups v1,
548 it is possible to mount a cgroup hierarchy that has no attached controllers:
550 .in +4n
552 mount \-t cgroup \-o none,name=somename none /some/mount/point
556 Multiple instances of such hierarchies can be mounted;
557 each hierarchy must have a unique name.
558 The only purpose of such hierarchies is to track processes.
559 (See the discussion of release notification below.)
560 An example of this is the
561 .I name=systemd
562 cgroup hierarchy that is used by
563 .BR systemd (1)
564 to track services and user sessions.
566 Since Linux 5.0, the
567 .I cgroup_no_v1
568 kernel boot option (described below) can be used to disable cgroup v1
569 named hierarchies, by specifying
570 .IR cgroup_no_v1=named .
572 .SH CGROUPS VERSION 2
573 In cgroups v2,
574 all mounted controllers reside in a single unified hierarchy.
575 While (different) controllers may be simultaneously
576 mounted under the v1 and v2 hierarchies,
577 it is not possible to mount the same controller simultaneously
578 under both the v1 and the v2 hierarchies.
580 The new behaviors in cgroups v2 are summarized here,
581 and in some cases elaborated in the following subsections.
582 .IP 1. 3
583 Cgroups v2 provides a unified hierarchy against
584 which all controllers are mounted.
585 .IP 2.
586 "Internal" processes are not permitted.
587 With the exception of the root cgroup, processes may reside
588 only in leaf nodes (cgroups that do not themselves contain child cgroups).
589 The details are somewhat more subtle than this, and are described below.
590 .IP 3.
591 Active cgroups must be specified via the files
592 .IR cgroup.controllers
594 .IR cgroup.subtree_control .
595 .IP 4.
597 .I tasks
598 file has been removed.
599 In addition, the
600 .I cgroup.clone_children
601 file that is employed by the
602 .I cpuset
603 controller has been removed.
604 .IP 5.
605 An improved mechanism for notification of empty cgroups is provided by the
606 .IR cgroup.events
607 file.
609 For more changes, see the
610 .IR Documentation/admin\-guide/cgroup\-v2.rst
611 file in the kernel source
613 .IR Documentation/cgroup\-v2.txt
614 in Linux 4.17 and earlier).
617 Some of the new behaviors listed above saw subsequent modification with
618 the addition in Linux 4.14 of "thread mode" (described below).
620 .SS Cgroups v2 unified hierarchy
621 In cgroups v1, the ability to mount different controllers
622 against different hierarchies was intended to allow great flexibility
623 for application design.
624 In practice, though,
625 the flexibility turned out to be less useful than expected,
626 and in many cases added complexity.
627 Therefore, in cgroups v2,
628 all available controllers are mounted against a single hierarchy.
629 The available controllers are automatically mounted,
630 meaning that it is not necessary (or possible) to specify the controllers
631 when mounting the cgroup v2 filesystem using a command such as the following:
633 .in +4n
635 mount \-t cgroup2 none /mnt/cgroup2
639 A cgroup v2 controller is available only if it is not currently in use
640 via a mount against a cgroup v1 hierarchy.
641 Or, to put things another way, it is not possible to employ
642 the same controller against both a v1 hierarchy and the unified v2 hierarchy.
643 This means that it may be necessary first to unmount a v1 controller
644 (as described above) before that controller is available in v2.
645 Since
646 .BR systemd (1)
647 makes heavy use of some v1 controllers by default,
648 it can in some cases be simpler to boot the system with
649 selected v1 controllers disabled.
650 To do this, specify the
651 .IR cgroup_no_v1=list
652 option on the kernel boot command line;
653 .I list
654 is a comma-separated list of the names of the controllers to disable,
655 or the word
656 .I all
657 to disable all v1 controllers.
658 (This situation is correctly handled by
659 .BR systemd (1),
660 which falls back to operating without the specified controllers.)
662 Note that on many modern systems,
663 .BR systemd (1)
664 automatically mounts the
665 .I cgroup2
666 filesystem at
667 .I /sys/fs/cgroup/unified
668 during the boot process.
670 .SS Cgroups v2 mount options
671 The following options
672 .RI ( "mount \-o" )
673 can be specified when mounting the group v2 filesystem:
675 .IR nsdelegate " (since Linux 4.15)"
676 Treat cgroup namespaces as delegation boundaries.
677 For details, see below.
679 .IR memory_localevents " (since Linux 5.2)"
680 .\" commit 9852ae3fe5293264f01c49f2571ef7688f7823ce
682 .I memory.events
683 should show statistics only for the cgroup itself,
684 and not for any descendant cgroups.
685 This was the behavior before Linux 5.2.
686 Starting in Linux 5.2,
687 the default behavior is to include statistics for descendant cgroups in
688 .IR memory.events ,
689 and this mount option can be used to revert to the legacy behavior.
690 This option is system wide and can be set on mount or
691 modified through remount only from the initial mount namespace;
692 it is silently ignored in noninitial namespaces.
694 .SS Cgroups v2 controllers
695 The following controllers, documented in the kernel source file
696 .IR Documentation/admin\-guide/cgroup\-v2.rst
698 .IR Documentation/cgroup\-v2.txt
699 in Linux 4.17 and earlier),
700 are supported in cgroups version 2:
702 .IR cpu " (since Linux 4.15)"
703 This is the successor to the version 1
704 .I cpu
706 .I cpuacct
707 controllers.
709 .IR cpuset " (since Linux 5.0)"
710 This is the successor of the version 1
711 .I cpuset
712 controller.
714 .IR freezer " (since Linux 5.2)"
715 .\" commit 76f969e8948d82e78e1bc4beb6b9465908e74873
716 This is the successor of the version 1
717 .I freezer
718 controller.
720 .IR hugetlb " (since Linux 5.6)"
721 This is the successor of the version 1
722 .I hugetlb
723 controller.
725 .IR io " (since Linux 4.5)"
726 This is the successor of the version 1
727 .I blkio
728 controller.
730 .IR memory " (since Linux 4.5)"
731 This is the successor of the version 1
732 .I memory
733 controller.
735 .IR perf_event " (since Linux 4.11)"
736 This is the same as the version 1
737 .I perf_event
738 controller.
740 .IR pids " (since Linux 4.5)"
741 This is the same as the version 1
742 .I pids
743 controller.
745 .IR rdma " (since Linux 4.11)"
746 This is the same as the version 1
747 .I rdma
748 controller.
750 There is no direct equivalent of the
751 .I net_cls
753 .I net_prio
754 controllers from cgroups version 1.
755 Instead, support has been added to
756 .BR iptables (8)
757 to allow eBPF filters that hook on cgroup v2 pathnames to make decisions
758 about network traffic on a per-cgroup basis.
760 The v2
761 .I devices
762 controller provides no interface files;
763 instead, device control is gated by attaching an eBPF
764 .RB ( BPF_CGROUP_DEVICE )
765 program to a v2 cgroup.
767 .SS Cgroups v2 subtree control
768 Each cgroup in the v2 hierarchy contains the following two files:
770 .IR cgroup.controllers
771 This read-only file exposes a list of the controllers that are
772 .I available
773 in this cgroup.
774 The contents of this file match the contents of the
775 .I cgroup.subtree_control
776 file in the parent cgroup.
778 .I cgroup.subtree_control
779 This is a list of controllers that are
780 .IR active
781 .RI ( enabled )
782 in the cgroup.
783 The set of controllers in this file is a subset of the set in the
784 .IR cgroup.controllers
785 of this cgroup.
786 The set of active controllers is modified by writing strings to this file
787 containing space-delimited controller names,
788 each preceded by '+' (to enable a controller)
789 or '\-' (to disable a controller), as in the following example:
791 .in +4n
793 echo \(aq+pids \-memory\(aq > x/y/cgroup.subtree_control
797 An attempt to enable a controller
798 that is not present in
799 .I cgroup.controllers
800 leads to an
801 .B ENOENT
802 error when writing to the
803 .I cgroup.subtree_control
804 file.
806 Because the list of controllers in
807 .I cgroup.subtree_control
808 is a subset of those
809 .IR cgroup.controllers ,
810 a controller that has been disabled in one cgroup in the hierarchy
811 can never be re-enabled in the subtree below that cgroup.
813 A cgroup's
814 .I cgroup.subtree_control
815 file determines the set of controllers that are exercised in the
816 .I child
817 cgroups.
818 When a controller (e.g.,
819 .IR pids )
820 is present in the
821 .I cgroup.subtree_control
822 file of a parent cgroup,
823 then the corresponding controller-interface files (e.g.,
824 .IR pids.max )
825 are automatically created in the children of that cgroup
826 and can be used to exert resource control in the child cgroups.
828 .SS Cgroups v2 """no internal processes""" rule
829 Cgroups v2 enforces a so-called "no internal processes" rule.
830 Roughly speaking, this rule means that,
831 with the exception of the root cgroup, processes may reside
832 only in leaf nodes (cgroups that do not themselves contain child cgroups).
833 This avoids the need to decide how to partition resources between
834 processes which are members of cgroup A and processes in child cgroups of A.
836 For instance, if cgroup
837 .I /cg1/cg2
838 exists, then a process may reside in
839 .IR /cg1/cg2 ,
840 but not in
841 .IR /cg1 .
842 This is to avoid an ambiguity in cgroups v1
843 with respect to the delegation of resources between processes in
844 .I /cg1
845 and its child cgroups.
846 The recommended approach in cgroups v2 is to create a subdirectory called
847 .I leaf
848 for any nonleaf cgroup which should contain processes, but no child cgroups.
849 Thus, processes which previously would have gone into
850 .I /cg1
851 would now go into
852 .IR /cg1/leaf .
853 This has the advantage of making explicit
854 the relationship between processes in
855 .I /cg1/leaf
857 .IR /cg1 's
858 other children.
860 The "no internal processes" rule is in fact more subtle than stated above.
861 More precisely, the rule is that a (nonroot) cgroup can't both
862 (1) have member processes, and
863 (2) distribute resources into child cgroups\(emthat is, have a nonempty
864 .I cgroup.subtree_control
865 file.
866 Thus, it
867 .I is
868 possible for a cgroup to have both member processes and child cgroups,
869 but before controllers can be enabled for that cgroup,
870 the member processes must be moved out of the cgroup
871 (e.g., perhaps into the child cgroups).
873 With the Linux 4.14 addition of "thread mode" (described below),
874 the "no internal processes" rule has been relaxed in some cases.
876 .SS Cgroups v2 cgroup.events file
877 Each nonroot cgroup in the v2 hierarchy contains a read-only file,
878 .IR cgroup.events ,
879 whose contents are key-value pairs
880 (delimited by newline characters, with the key and value separated by spaces)
881 providing state information about the cgroup:
883 .in +4n
885 $ \fBcat mygrp/cgroup.events\fP
886 populated 1
887 frozen 0
891 The following keys may appear in this file:
893 .IR populated
894 The value of this key is either 1,
895 if this cgroup or any of its descendants has member processes,
896 or otherwise 0.
898 .IR frozen " (since Linux 5.2)"
899 .\" commit 76f969e8948d82e78e1bc4beb6b9465908e7487
900 The value of this key is 1 if this cgroup is currently frozen,
901 or 0 if it is not.
904 .IR cgroup.events
905 file can be monitored, in order to receive notification when the value of
906 one of its keys changes.
907 Such monitoring can be done using
908 .BR inotify (7),
909 which notifies changes as
910 .BR IN_MODIFY
911 events, or
912 .BR poll (2),
913 which notifies changes by returning the
914 .B POLLPRI
916 .B POLLERR
917 bits in the
918 .IR revents
919 field.
921 .SS Cgroup v2 release notification
922 Cgroups v2 provides a new mechanism for obtaining notification
923 when a cgroup becomes empty.
924 The cgroups v1
925 .IR release_agent
927 .IR notify_on_release
928 files are removed, and replaced by the
929 .I populated
930 key in the
931 .IR cgroup.events
932 file.
933 This key either has the value 0,
934 meaning that the cgroup (and its descendants)
935 contain no (nonzombie) member processes,
936 or 1, meaning that the cgroup (or one of its descendants)
937 contains member processes.
939 The cgroups v2 release-notification mechanism
940 offers the following advantages over the cgroups v1
941 .IR release_agent
942 mechanism:
943 .IP * 3
944 It allows for cheaper notification,
945 since a single process can monitor multiple
946 .IR cgroup.events
947 files (using the techniques described earlier).
948 By contrast, the cgroups v1 mechanism requires the expense of creating
949 a process for each notification.
950 .IP *
951 Notification for different cgroup subhierarchies can be delegated
952 to different processes.
953 By contrast, the cgroups v1 mechanism allows only one release agent
954 for an entire hierarchy.
956 .SS Cgroups v2 cgroup.stat file
957 .\" commit ec39225cca42c05ac36853d11d28f877fde5c42e
958 Each cgroup in the v2 hierarchy contains a read-only
959 .IR cgroup.stat
960 file (first introduced in Linux 4.14)
961 that consists of lines containing key-value pairs.
962 The following keys currently appear in this file:
964 .I nr_descendants
965 This is the total number of visible (i.e., living) descendant cgroups
966 underneath this cgroup.
968 .I nr_dying_descendants
969 This is the total number of dying descendant cgroups
970 underneath this cgroup.
971 A cgroup enters the dying state after being deleted.
972 It remains in that state for an undefined period
973 (which will depend on system load)
974 while resources are freed before the cgroup is destroyed.
975 Note that the presence of some cgroups in the dying state is normal,
976 and is not indicative of any problem.
978 A process can't be made a member of a dying cgroup,
979 and a dying cgroup can't be brought back to life.
981 .SS Limiting the number of descendant cgroups
982 Each cgroup in the v2 hierarchy contains the following files,
983 which can be used to view and set limits on the number
984 of descendant cgroups under that cgroup:
986 .IR cgroup.max.depth " (since Linux 4.14)"
987 .\" commit 1a926e0bbab83bae8207d05a533173425e0496d1
988 This file defines a limit on the depth of nesting of descendant cgroups.
989 A value of 0 in this file means that no descendant cgroups can be created.
990 An attempt to create a descendant whose nesting level exceeds
991 the limit fails
992 .RI ( mkdir (2)
993 fails with the error
994 .BR EAGAIN ).
996 Writing the string
997 .IR """max"""
998 to this file means that no limit is imposed.
999 The default value in this file is
1000 .IR """max""" .
1002 .IR cgroup.max.descendants " (since Linux 4.14)"
1003 .\" commit 1a926e0bbab83bae8207d05a533173425e0496d1
1004 This file defines a limit on the number of live descendant cgroups that
1005 this cgroup may have.
1006 An attempt to create more descendants than allowed by the limit fails
1007 .RI ( mkdir (2)
1008 fails with the error
1009 .BR EAGAIN ).
1011 Writing the string
1012 .IR """max"""
1013 to this file means that no limit is imposed.
1014 The default value in this file is
1015 .IR """max""" .
1017 .SH CGROUPS DELEGATION: DELEGATING A HIERARCHY TO A LESS PRIVILEGED USER
1018 In the context of cgroups,
1019 delegation means passing management of some subtree
1020 of the cgroup hierarchy to a nonprivileged user.
1021 Cgroups v1 provides support for delegation based on file permissions
1022 in the cgroup hierarchy but with less strict containment rules than v2
1023 (as noted below).
1024 Cgroups v2 supports delegation with containment by explicit design.
1025 The focus of the discussion in this section is on delegation in cgroups v2,
1026 with some differences for cgroups v1 noted along the way.
1028 Some terminology is required in order to describe delegation.
1030 .I delegater
1031 is a privileged user (i.e., root) who owns a parent cgroup.
1033 .I delegatee
1034 is a nonprivileged user who will be granted the permissions needed
1035 to manage some subhierarchy under that parent cgroup,
1036 known as the
1037 .IR "delegated subtree" .
1039 To perform delegation,
1040 the delegater makes certain directories and files writable by the delegatee,
1041 typically by changing the ownership of the objects to be the user ID
1042 of the delegatee.
1043 Assuming that we want to delegate the hierarchy rooted at (say)
1044 .I /dlgt_grp
1045 and that there are not yet any child cgroups under that cgroup,
1046 the ownership of the following is changed to the user ID of the delegatee:
1048 .IR /dlgt_grp
1049 Changing the ownership of the root of the subtree means that any new
1050 cgroups created under the subtree (and the files they contain)
1051 will also be owned by the delegatee.
1053 .IR /dlgt_grp/cgroup.procs
1054 Changing the ownership of this file means that the delegatee
1055 can move processes into the root of the delegated subtree.
1057 .IR /dlgt_grp/cgroup.subtree_control " (cgroups v2 only)"
1058 Changing the ownership of this file means that the delegatee
1059 can enable controllers (that are present in
1060 .IR /dlgt_grp/cgroup.controllers )
1061 in order to further redistribute resources at lower levels in the subtree.
1062 (As an alternative to changing the ownership of this file,
1063 the delegater might instead add selected controllers to this file.)
1065 .IR /dlgt_grp/cgroup.threads " (cgroups v2 only)"
1066 Changing the ownership of this file is necessary if a threaded subtree
1067 is being delegated (see the description of "thread mode", below).
1068 This permits the delegatee to write thread IDs to the file.
1069 (The ownership of this file can also be changed when delegating
1070 a domain subtree, but currently this serves no purpose,
1071 since, as described below, it is not possible to move a thread between
1072 domain cgroups by writing its thread ID to the
1073 .IR cgroup.threads
1074 file.)
1076 In cgroups v1, the corresponding file that should instead be delegated is the
1077 .I tasks
1078 file.
1080 The delegater should
1081 .I not
1082 change the ownership of any of the controller interfaces files (e.g.,
1083 .IR pids.max ,
1084 .IR memory.high )
1086 .IR dlgt_grp .
1087 Those files are used from the next level above the delegated subtree
1088 in order to distribute resources into the subtree,
1089 and the delegatee should not have permission to change
1090 the resources that are distributed into the delegated subtree.
1092 See also the discussion of the
1093 .IR /sys/kernel/cgroup/delegate
1094 file in NOTES for information about further delegatable files in cgroups v2.
1096 After the aforementioned steps have been performed,
1097 the delegatee can create child cgroups within the delegated subtree
1098 (the cgroup subdirectories and the files they contain
1099 will be owned by the delegatee)
1100 and move processes between cgroups in the subtree.
1101 If some controllers are present in
1102 .IR dlgt_grp/cgroup.subtree_control ,
1103 or the ownership of that file was passed to the delegatee,
1104 the delegatee can also control the further redistribution
1105 of the corresponding resources into the delegated subtree.
1107 .SS Cgroups v2 delegation: nsdelegate and cgroup namespaces
1108 Starting with Linux 4.13,
1109 .\" commit 5136f6365ce3eace5a926e10f16ed2a233db5ba9
1110 there is a second way to perform cgroup delegation in the cgroups v2 hierarchy.
1111 This is done by mounting or remounting the cgroup v2 filesystem with the
1112 .I nsdelegate
1113 mount option.
1114 For example, if the cgroup v2 filesystem has already been mounted,
1115 we can remount it with the
1116 .I nsdelegate
1117 option as follows:
1119 .in +4n
1121 mount \-t cgroup2 \-o remount,nsdelegate \e
1122                  none /sys/fs/cgroup/unified
1126 .\" Alternatively, we could boot the kernel with the options:
1128 .\"    cgroup_no_v1=all systemd.legacy_systemd_cgroup_controller
1130 .\" The effect of the latter option is to prevent systemd from employing
1131 .\" its "hybrid" cgroup mode, where it tries to make use of cgroups v2.
1133 The effect of this mount option is to cause cgroup namespaces
1134 to automatically become delegation boundaries.
1135 More specifically,
1136 the following restrictions apply for processes inside the cgroup namespace:
1137 .IP * 3
1138 Writes to controller interface files in the root directory of the namespace
1139 will fail with the error
1140 .BR EPERM .
1141 Processes inside the cgroup namespace can still write to delegatable
1142 files in the root directory of the cgroup namespace such as
1143 .IR cgroup.procs
1145 .IR cgroup.subtree_control ,
1146 and can create subhierarchy underneath the root directory.
1147 .IP *
1148 Attempts to migrate processes across the namespace boundary are denied
1149 (with the error
1150 .BR ENOENT ).
1151 Processes inside the cgroup namespace can still
1152 (subject to the containment rules described below)
1153 move processes between cgroups
1154 .I within
1155 the subhierarchy under the namespace root.
1157 The ability to define cgroup namespaces as delegation boundaries
1158 makes cgroup namespaces more useful.
1159 To understand why, suppose that we already have one cgroup hierarchy
1160 that has been delegated to a nonprivileged user,
1161 .IR cecilia ,
1162 using the older delegation technique described above.
1163 Suppose further that
1164 .I cecilia
1165 wanted to further delegate a subhierarchy
1166 under the existing delegated hierarchy.
1167 (For example, the delegated hierarchy might be associated with
1168 an unprivileged container run by
1169 .IR cecilia .)
1170 Even if a cgroup namespace was employed,
1171 because both hierarchies are owned by the unprivileged user
1172 .IR cecilia ,
1173 the following illegitimate actions could be performed:
1174 .IP * 3
1175 A process in the inferior hierarchy could change the
1176 resource controller settings in the root directory of that hierarchy.
1177 (These resource controller settings are intended to allow control to
1178 be exercised from the
1179 .I parent
1180 cgroup;
1181 a process inside the child cgroup should not be allowed to modify them.)
1182 .IP *
1183 A process inside the inferior hierarchy could move processes
1184 into and out of the inferior hierarchy if the cgroups in the
1185 superior hierarchy were somehow visible.
1187 Employing the
1188 .I nsdelegate
1189 mount option prevents both of these possibilities.
1192 .I nsdelegate
1193 mount option only has an effect when performed in
1194 the initial mount namespace;
1195 in other mount namespaces, the option is silently ignored.
1197 .IR Note :
1198 On some systems,
1199 .BR systemd (1)
1200 automatically mounts the cgroup v2 filesystem.
1201 In order to experiment with the
1202 .I nsdelegate
1203 operation, it may be useful to boot the kernel with
1204 the following command-line options:
1206 .in +4n
1208 cgroup_no_v1=all systemd.legacy_systemd_cgroup_controller
1212 These options cause the kernel to boot with the cgroups v1 controllers
1213 disabled (meaning that the controllers are available in the v2 hierarchy),
1214 and tells
1215 .BR systemd (1)
1216 not to mount and use the cgroup v2 hierarchy,
1217 so that the v2 hierarchy can be manually mounted
1218 with the desired options after boot-up.
1220 .SS Cgroup delegation containment rules
1221 Some delegation
1222 .IR "containment rules"
1223 ensure that the delegatee can move processes between cgroups within the
1224 delegated subtree,
1225 but can't move processes from outside the delegated subtree into
1226 the subtree or vice versa.
1227 A nonprivileged process (i.e., the delegatee) can write the PID of
1228 a "target" process into a
1229 .IR cgroup.procs
1230 file only if all of the following are true:
1231 .IP * 3
1232 The writer has write permission on the
1233 .I cgroup.procs
1234 file in the destination cgroup.
1235 .IP *
1236 The writer has write permission on the
1237 .I cgroup.procs
1238 file in the nearest common ancestor of the source and destination cgroups.
1239 Note that in some cases,
1240 the nearest common ancestor may be the source or destination cgroup itself.
1241 This requirement is not enforced for cgroups v1 hierarchies,
1242 with the consequence that containment in v1 is less strict than in v2.
1243 (For example, in cgroups v1 the user that owns two distinct
1244 delegated subhierarchies can move a process between the hierarchies.)
1245 .IP *
1246 If the cgroup v2 filesystem was mounted with the
1247 .I nsdelegate
1248 option, the writer must be able to see the source and destination cgroups
1249 from its cgroup namespace.
1250 .IP *
1251 In cgroups v1:
1252 the effective UID of the writer (i.e., the delegatee) matches the
1253 real user ID or the saved set-user-ID of the target process.
1254 Before Linux 4.11,
1255 .\" commit 576dd464505fc53d501bb94569db76f220104d28
1256 this requirement also applied in cgroups v2
1257 (This was a historical requirement inherited from cgroups v1
1258 that was later deemed unnecessary,
1259 since the other rules suffice for containment in cgroups v2.)
1261 .IR Note :
1262 one consequence of these delegation containment rules is that the
1263 unprivileged delegatee can't place the first process into
1264 the delegated subtree;
1265 instead, the delegater must place the first process
1266 (a process owned by the delegatee) into the delegated subtree.
1268 .SH CGROUPS VERSION 2 THREAD MODE
1269 Among the restrictions imposed by cgroups v2 that were not present
1270 in cgroups v1 are the following:
1271 .IP * 3
1272 .IR "No thread-granularity control" :
1273 all of the threads of a process must be in the same cgroup.
1274 .IP *
1275 .IR "No internal processes" :
1276 a cgroup can't both have member processes and
1277 exercise controllers on child cgroups.
1279 Both of these restrictions were added because
1280 the lack of these restrictions had caused problems
1281 in cgroups v1.
1282 In particular, the cgroups v1 ability to allow thread-level granularity
1283 for cgroup membership made no sense for some controllers.
1284 (A notable example was the
1285 .I memory
1286 controller: since threads share an address space,
1287 it made no sense to split threads across different
1288 .I memory
1289 cgroups.)
1291 Notwithstanding the initial design decision in cgroups v2,
1292 there were use cases for certain controllers, notably the
1293 .IR cpu
1294 controller,
1295 for which thread-level granularity of control was meaningful and useful.
1296 To accommodate such use cases, Linux 4.14 added
1297 .I "thread mode"
1298 for cgroups v2.
1300 Thread mode allows the following:
1301 .IP * 3
1302 The creation of
1303 .IR "threaded subtrees"
1304 in which the threads of a process may
1305 be spread across cgroups inside the tree.
1306 (A threaded subtree may contain multiple multithreaded processes.)
1307 .IP *
1308 The concept of
1309 .IR "threaded controllers",
1310 which can distribute resources across the cgroups in a threaded subtree.
1311 .IP *
1312 A relaxation of the "no internal processes rule",
1313 so that, within a threaded subtree,
1314 a cgroup can both contain member threads and
1315 exercise resource control over child cgroups.
1317 With the addition of thread mode,
1318 each nonroot cgroup now contains a new file,
1319 .IR cgroup.type ,
1320 that exposes, and in some circumstances can be used to change,
1321 the "type" of a cgroup.
1322 This file contains one of the following type values:
1324 .I "domain"
1325 This is a normal v2 cgroup that provides process-granularity control.
1326 If a process is a member of this cgroup,
1327 then all threads of the process are (by definition) in the same cgroup.
1328 This is the default cgroup type,
1329 and provides the same behavior that was provided for
1330 cgroups in the initial cgroups v2 implementation.
1332 .I "threaded"
1333 This cgroup is a member of a threaded subtree.
1334 Threads can be added to this cgroup,
1335 and controllers can be enabled for the cgroup.
1337 .I "domain threaded"
1338 This is a domain cgroup that serves as the root of a threaded subtree.
1339 This cgroup type is also known as "threaded root".
1341 .I "domain invalid"
1342 This is a cgroup inside a threaded subtree
1343 that is in an "invalid" state.
1344 Processes can't be added to the cgroup,
1345 and controllers can't be enabled for the cgroup.
1346 The only thing that can be done with this cgroup (other than deleting it)
1347 is to convert it to a
1348 .IR threaded
1349 cgroup by writing the string
1350 .IR """threaded"""
1351 to the
1352 .I cgroup.type
1353 file.
1355 The rationale for the existence of this "interim" type
1356 during the creation of a threaded subtree
1357 (rather than the kernel simply immediately converting all cgroups
1358 under the threaded root to the type
1359 .IR threaded )
1360 is to allow for
1361 possible future extensions to the thread mode model
1363 .SS Threaded versus domain controllers
1364 With the addition of threads mode,
1365 cgroups v2 now distinguishes two types of resource controllers:
1366 .IP * 3
1367 .I Threaded
1368 .\" In the kernel source, look for ".threaded[ \t]*= true" in
1369 .\" initializations of struct cgroup_subsys
1370 controllers: these controllers support thread-granularity for
1371 resource control and can be enabled inside threaded subtrees,
1372 with the result that the corresponding controller-interface files
1373 appear inside the cgroups in the threaded subtree.
1374 As at Linux 4.19, the following controllers are threaded:
1375 .IR cpu ,
1376 .IR perf_event ,
1378 .IR pids .
1379 .IP *
1380 .I Domain
1381 controllers: these controllers support only process granularity
1382 for resource control.
1383 From the perspective of a domain controller,
1384 all threads of a process are always in the same cgroup.
1385 Domain controllers can't be enabled inside a threaded subtree.
1387 .SS Creating a threaded subtree
1388 There are two pathways that lead to the creation of a threaded subtree.
1389 The first pathway proceeds as follows:
1390 .IP 1. 3
1391 We write the string
1392 .IR """threaded"""
1393 to the
1394 .I cgroup.type
1395 file of a cgroup
1396 .IR y/z
1397 that currently has the type
1398 .IR domain .
1399 This has the following effects:
1401 .IP * 3
1402 The type of the cgroup
1403 .IR y/z
1404 becomes
1405 .IR threaded .
1406 .IP *
1407 The type of the parent cgroup,
1408 .IR y ,
1409 becomes
1410 .IR "domain threaded" .
1411 The parent cgroup is the root of a threaded subtree
1412 (also known as the "threaded root").
1413 .IP *
1414 All other cgroups under
1415 .IR y
1416 that were not already of type
1417 .IR threaded
1418 (because they were inside already existing threaded subtrees
1419 under the new threaded root)
1420 are converted to type
1421 .IR "domain invalid" .
1422 Any subsequently created cgroups under
1423 .I y
1424 will also have the type
1425 .IR "domain invalid" .
1427 .IP 2.
1428 We write the string
1429 .IR """threaded"""
1430 to each of the
1431 .IR "domain invalid"
1432 cgroups under
1433 .IR y ,
1434 in order to convert them to the type
1435 .IR threaded .
1436 As a consequence of this step, all threads under the threaded root
1437 now have the type
1438 .IR threaded
1439 and the threaded subtree is now fully usable.
1440 The requirement to write
1441 .IR """threaded"""
1442 to each of these cgroups is somewhat cumbersome,
1443 but allows for possible future extensions to the thread-mode model.
1445 The second way of creating a threaded subtree is as follows:
1446 .IP 1. 3
1447 In an existing cgroup,
1448 .IR z ,
1449 that currently has the type
1450 .IR domain ,
1451 we (1) enable one or more threaded controllers and
1452 (2) make a process a member of
1453 .IR z .
1454 (These two steps can be done in either order.)
1455 This has the following consequences:
1457 .IP * 3
1458 The type of
1459 .I z
1460 becomes
1461 .IR "domain threaded" .
1462 .IP *
1463 All of the descendant cgroups of
1464 .I x
1465 that were not already of type
1466 .IR threaded
1467 are converted to type
1468 .IR "domain invalid" .
1470 .IP 2.
1471 As before, we make the threaded subtree usable by writing the string
1472 .IR """threaded"""
1473 to each of the
1474 .IR "domain invalid"
1475 cgroups under
1476 .IR y ,
1477 in order to convert them to the type
1478 .IR threaded .
1480 One of the consequences of the above pathways to creating a threaded subtree
1481 is that the threaded root cgroup can be a parent only to
1482 .I threaded
1483 (and
1484 .IR "domain invalid" )
1485 cgroups.
1486 The threaded root cgroup can't be a parent of a
1487 .I domain
1488 cgroups, and a
1489 .I threaded
1490 cgroup
1491 can't have a sibling that is a
1492 .I domain
1493 cgroup.
1495 .SS Using a threaded subtree
1496 Within a threaded subtree, threaded controllers can be enabled
1497 in each subgroup whose type has been changed to
1498 .IR threaded ;
1499 upon doing so, the corresponding controller interface files
1500 appear in the children of that cgroup.
1502 A process can be moved into a threaded subtree by writing its PID to the
1503 .I cgroup.procs
1504 file in one of the cgroups inside the tree.
1505 This has the effect of making all of the threads
1506 in the process members of the corresponding cgroup
1507 and makes the process a member of the threaded subtree.
1508 The threads of the process can then be spread across
1509 the threaded subtree by writing their thread IDs (see
1510 .BR gettid (2))
1511 to the
1512 .I cgroup.threads
1513 files in different cgroups inside the subtree.
1514 The threads of a process must all reside in the same threaded subtree.
1516 As with writing to
1517 .IR cgroup.procs ,
1518 some containment rules apply when writing to the
1519 .I cgroup.threads
1520 file:
1521 .IP * 3
1522 The writer must have write permission on the
1523 cgroup.threads
1524 file in the destination cgroup.
1525 .IP *
1526 The writer must have write permission on the
1527 .I cgroup.procs
1528 file in the common ancestor of the source and destination cgroups.
1529 (In some cases,
1530 the common ancestor may be the source or destination cgroup itself.)
1531 .IP *
1532 The source and destination cgroups must be in the same threaded subtree.
1533 (Outside a threaded subtree, an attempt to move a thread by writing
1534 its thread ID to the
1535 .I cgroup.threads
1536 file in a different
1537 .I domain
1538 cgroup fails with the error
1539 .BR EOPNOTSUPP .)
1542 .I cgroup.threads
1543 file is present in each cgroup (including
1544 .I domain
1545 cgroups) and can be read in order to discover the set of threads
1546 that is present in the cgroup.
1547 The set of thread IDs obtained when reading this file
1548 is not guaranteed to be ordered or free of duplicates.
1551 .I cgroup.procs
1552 file in the threaded root shows the PIDs of all processes
1553 that are members of the threaded subtree.
1555 .I cgroup.procs
1556 files in the other cgroups in the subtree are not readable.
1558 Domain controllers can't be enabled in a threaded subtree;
1559 no controller-interface files appear inside the cgroups underneath the
1560 threaded root.
1561 From the point of view of a domain controller,
1562 threaded subtrees are invisible:
1563 a multithreaded process inside a threaded subtree appears to a domain
1564 controller as a process that resides in the threaded root cgroup.
1566 Within a threaded subtree, the "no internal processes" rule does not apply:
1567 a cgroup can both contain member processes (or thread)
1568 and exercise controllers on child cgroups.
1570 .SS Rules for writing to cgroup.type and creating threaded subtrees
1571 A number of rules apply when writing to the
1572 .I cgroup.type
1573 file:
1574 .IP * 3
1575 Only the string
1576 .IR """threaded"""
1577 may be written.
1578 In other words, the only explicit transition that is possible is to convert a
1579 .I domain
1580 cgroup to type
1581 .IR threaded .
1582 .IP *
1583 The effect of writing
1584 .IR """threaded"""
1585 depends on the current value in
1586 .IR cgroup.type ,
1587 as follows:
1589 .IP \(bu 3
1590 .IR domain
1592 .IR "domain threaded" :
1593 start the creation of a threaded subtree
1594 (whose root is the parent of this cgroup) via
1595 the first of the pathways described above;
1596 .IP \(bu
1597 .IR "domain\ invalid" :
1598 convert this cgroup (which is inside a threaded subtree) to a usable (i.e.,
1599 .IR threaded )
1600 state;
1601 .IP \(bu
1602 .IR threaded :
1603 no effect (a "no-op").
1605 .IP *
1606 We can't write to a
1607 .I cgroup.type
1608 file if the parent's type is
1609 .IR "domain invalid" .
1610 In other words, the cgroups of a threaded subtree must be converted to the
1611 .I threaded
1612 state in a top-down manner.
1614 There are also some constraints that must be satisfied
1615 in order to create a threaded subtree rooted at the cgroup
1616 .IR x :
1617 .IP * 3
1618 There can be no member processes in the descendant cgroups of
1619 .IR x .
1620 (The cgroup
1621 .I x
1622 can itself have member processes.)
1623 .IP *
1624 No domain controllers may be enabled in
1625 .IR x 's
1626 .IR cgroup.subtree_control
1627 file.
1629 If any of the above constraints is violated, then an attempt to write
1630 .IR """threaded"""
1631 to a
1632 .IR cgroup.type
1633 file fails with the error
1634 .BR ENOTSUP .
1636 .SS The """domain threaded""" cgroup type
1637 According to the pathways described above,
1638 the type of a cgroup can change to
1639 .IR "domain threaded"
1640 in either of the following cases:
1641 .IP * 3
1642 The string
1643 .IR """threaded"""
1644 is written to a child cgroup.
1645 .IP *
1646 A threaded controller is enabled inside the cgroup and
1647 a process is made a member of the cgroup.
1650 .IR "domain threaded"
1651 cgroup,
1652 .IR x ,
1653 can revert to the type
1654 .IR domain
1655 if the above conditions no longer hold true\(emthat is, if all
1656 .I threaded
1657 child cgroups of
1658 .I x
1659 are removed and either
1660 .I x
1661 no longer has threaded controllers enabled or
1662 no longer has member processes.
1664 When a
1665 .IR "domain threaded"
1666 cgroup
1667 .IR x
1668 reverts to the type
1669 .IR domain :
1670 .IP * 3
1672 .IR "domain invalid"
1673 descendants of
1674 .I x
1675 that are not in lower-level threaded subtrees revert to the type
1676 .IR domain .
1677 .IP *
1678 The root cgroups in any lower-level threaded subtrees revert to the type
1679 .IR "domain threaded" .
1681 .SS Exceptions for the root cgroup
1682 The root cgroup of the v2 hierarchy is treated exceptionally:
1683 it can be the parent of both
1684 .I domain
1686 .I threaded
1687 cgroups.
1688 If the string
1689 .I """threaded"""
1690 is written to the
1691 .I cgroup.type
1692 file of one of the children of the root cgroup, then
1693 .IP * 3
1694 The type of that cgroup becomes
1695 .IR threaded .
1696 .IP *
1697 The type of any descendants of that cgroup that
1698 are not part of lower-level threaded subtrees changes to
1699 .IR "domain invalid" .
1701 Note that in this case, there is no cgroup whose type becomes
1702 .IR "domain threaded" .
1703 (Notionally, the root cgroup can be considered as the threaded root
1704 for the cgroup whose type was changed to
1705 .IR threaded .)
1707 The aim of this exceptional treatment for the root cgroup is to
1708 allow a threaded cgroup that employs the
1709 .I cpu
1710 controller to be placed as high as possible in the hierarchy,
1711 so as to minimize the (small) cost of traversing the cgroup hierarchy.
1713 .SS The cgroups v2 """cpu""" controller and realtime threads
1714 As at Linux 4.19, the cgroups v2
1715 .I cpu
1716 controller does not support control of realtime threads
1717 (specifically threads scheduled under any of the policies
1718 .BR SCHED_FIFO ,
1719 .BR SCHED_RR ,
1720 described
1721 .BR SCHED_DEADLINE ;
1723 .BR sched (7)).
1724 Therefore, the
1725 .I cpu
1726 controller can be enabled in the root cgroup only
1727 if all realtime threads are in the root cgroup.
1728 (If there are realtime threads in nonroot cgroups, then a
1729 .BR write (2)
1730 of the string
1731 .IR """+cpu"""
1732 to the
1733 .I cgroup.subtree_control
1734 file fails with the error
1735 .BR EINVAL .)
1737 On some systems,
1738 .BR systemd (1)
1739 places certain realtime threads in nonroot cgroups in the v2 hierarchy.
1740 On such systems,
1741 these threads must first be moved to the root cgroup before the
1742 .I cpu
1743 controller can be enabled.
1745 .SH ERRORS
1746 The following errors can occur for
1747 .BR mount (2):
1749 .B EBUSY
1750 An attempt to mount a cgroup version 1 filesystem specified neither the
1751 .I name=
1752 option (to mount a named hierarchy) nor a controller name (or
1753 .IR all ).
1754 .SH NOTES
1755 A child process created via
1756 .BR fork (2)
1757 inherits its parent's cgroup memberships.
1758 A process's cgroup memberships are preserved across
1759 .BR execve (2).
1762 .BR clone3 (2)
1763 .B CLONE_INTO_CGROUP
1764 flag can be used to create a child process that begins its life in
1765 a different version 2 cgroup from the parent process.
1767 .SS /proc files
1769 .IR /proc/cgroups " (since Linux 2.6.24)"
1770 This file contains information about the controllers
1771 that are compiled into the kernel.
1772 An example of the contents of this file (reformatted for readability)
1773 is the following:
1775 .in +4n
1777 #subsys_name    hierarchy      num_cgroups    enabled
1778 cpuset          4              1              1
1779 cpu             8              1              1
1780 cpuacct         8              1              1
1781 blkio           6              1              1
1782 memory          3              1              1
1783 devices         10             84             1
1784 freezer         7              1              1
1785 net_cls         9              1              1
1786 perf_event      5              1              1
1787 net_prio        9              1              1
1788 hugetlb         0              1              0
1789 pids            2              1              1
1793 The fields in this file are, from left to right:
1795 .IP 1. 3
1796 The name of the controller.
1797 .IP 2.
1798 The unique ID of the cgroup hierarchy on which this controller is mounted.
1799 If multiple cgroups v1 controllers are bound to the same hierarchy,
1800 then each will show the same hierarchy ID in this field.
1801 The value in this field will be 0 if:
1802 .RS 5
1803 .IP a) 3
1804 the controller is not mounted on a cgroups v1 hierarchy;
1805 .IP b)
1806 the controller is bound to the cgroups v2 single unified hierarchy; or
1807 .IP c)
1808 the controller is disabled (see below).
1810 .IP 3.
1811 The number of control groups in this hierarchy using this controller.
1812 .IP 4.
1813 This field contains the value 1 if this controller is enabled,
1814 or 0 if it has been disabled (via the
1815 .IR cgroup_disable
1816 kernel command-line boot parameter).
1819 .IR /proc/[pid]/cgroup " (since Linux 2.6.24)"
1820 This file describes control groups to which the process
1821 with the corresponding PID belongs.
1822 The displayed information differs for
1823 cgroups version 1 and version 2 hierarchies.
1825 For each cgroup hierarchy of which the process is a member,
1826 there is one entry containing three colon-separated fields:
1828 .in +4n
1830 hierarchy\-ID:controller\-list:cgroup\-path
1834 For example:
1836 .in +4n
1838 5:cpuacct,cpu,cpuset:/daemons
1842 The colon-separated fields are, from left to right:
1844 .IP 1. 3
1845 For cgroups version 1 hierarchies,
1846 this field contains a unique hierarchy ID number
1847 that can be matched to a hierarchy ID in
1848 .IR /proc/cgroups .
1849 For the cgroups version 2 hierarchy, this field contains the value 0.
1850 .IP 2.
1851 For cgroups version 1 hierarchies,
1852 this field contains a comma-separated list of the controllers
1853 bound to the hierarchy.
1854 For the cgroups version 2 hierarchy, this field is empty.
1855 .IP 3.
1856 This field contains the pathname of the control group in the hierarchy
1857 to which the process belongs.
1858 This pathname is relative to the mount point of the hierarchy.
1861 .SS /sys/kernel/cgroup files
1863 .IR /sys/kernel/cgroup/delegate " (since Linux 4.15)"
1864 .\" commit 01ee6cfb1483fe57c9cbd8e73817dfbf9bacffd3
1865 This file exports a list of the cgroups v2 files
1866 (one per line) that are delegatable
1867 (i.e., whose ownership should be changed to the user ID of the delegatee).
1868 In the future, the set of delegatable files may change or grow,
1869 and this file provides a way for the kernel to inform
1870 user-space applications of which files must be delegated.
1871 As at Linux 4.15, one sees the following when inspecting this file:
1873 .in +4n
1875 $ \fBcat /sys/kernel/cgroup/delegate\fP
1876 cgroup.procs
1877 cgroup.subtree_control
1878 cgroup.threads
1882 .IR /sys/kernel/cgroup/features " (since Linux 4.15)"
1883 .\" commit 5f2e673405b742be64e7c3604ed4ed3ac14f35ce
1884 Over time, the set of cgroups v2 features that are provided by the
1885 kernel may change or grow,
1886 or some features may not be enabled by default.
1887 This file provides a way for user-space applications to discover what
1888 features the running kernel supports and has enabled.
1889 Features are listed one per line:
1891 .in +4n
1893 $ \fBcat /sys/kernel/cgroup/features\fP
1894 nsdelegate
1895 memory_localevents
1899 The entries that can appear in this file are:
1902 .IR memory_localevents " (since Linux 5.2)"
1903 The kernel supports the
1904 .I memory_localevents
1905 mount option.
1907 .IR nsdelegate " (since Linux 4.15)"
1908 The kernel supports the
1909 .I nsdelegate
1910 mount option.
1912 .SH SEE ALSO
1913 .BR prlimit (1),
1914 .BR systemd (1),
1915 .BR systemd\-cgls (1),
1916 .BR systemd\-cgtop (1),
1917 .BR clone (2),
1918 .BR ioprio_set (2),
1919 .BR perf_event_open (2),
1920 .BR setrlimit (2),
1921 .BR cgroup_namespaces (7),
1922 .BR cpuset (7),
1923 .BR namespaces (7),
1924 .BR sched (7),
1925 .BR user_namespaces (7)
1927 The kernel source file
1928 .IR Documentation/admin\-guide/cgroup\-v2.rst .