]>
Commit | Line | Data |
---|---|---|
014cb63b | 1 | .\" Copyright (C) 2015 Serge Hallyn <serge@hallyn.com> |
4242dfbe | 2 | .\" and Copyright (C) 2016, 2017 Michael Kerrisk <mtk.manpages@gmail.com> |
014cb63b MK |
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 | .\" | |
9ba01802 | 26 | .TH CGROUPS 7 2019-03-06 "Linux" "Linux Programmer's Manual" |
21f0d132 MK |
27 | .SH NAME |
28 | cgroups \- Linux control groups | |
29 | .SH DESCRIPTION | |
77eefc59 | 30 | Control groups, usually referred to as cgroups, |
a15e0673 | 31 | are a Linux kernel feature which allow processes to |
8bff7140 MK |
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 | |
21f0d132 | 35 | a pseudo-filesystem called cgroupfs. |
6398ca15 | 36 | Grouping is implemented in the core cgroup kernel code, |
21f0d132 | 37 | while resource tracking and limits are implemented in |
8bff7140 | 38 | a set of per-resource-type subsystems (memory, CPU, and so on). |
21f0d132 | 39 | .\" |
176a4211 MK |
40 | .SS Terminology |
41 | A | |
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. | |
a721e8b2 | 45 | .PP |
176a4211 MK |
46 | A |
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). | |
a721e8b2 | 57 | .PP |
55f52de8 | 58 | The cgroups for a controller are arranged in a |
176a4211 MK |
59 | .IR hierarchy . |
60 | This hierarchy is defined by creating, removing, and | |
61 | renaming subdirectories within the cgroup filesystem. | |
8fc9db1e MK |
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. | |
8bff7140 MK |
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. | |
176a4211 | 69 | .\" |
43df1ab3 MK |
70 | .SS Cgroups version 1 and version 2 |
71 | The initial release of the cgroups implementation was in Linux 2.6.24. | |
55f52de8 | 72 | Over time, various cgroup controllers have been added |
43df1ab3 | 73 | to allow the management of various types of resources. |
55f52de8 MK |
74 | However, the development of these controllers was largely uncoordinated, |
75 | with the result that many inconsistencies arose between controllers | |
43df1ab3 MK |
76 | and management of the cgroup hierarchies became rather complex. |
77 | (A longer description of these problems can be found in | |
78 | the kernel source file | |
0a837899 | 79 | .IR Documentation/cgroup\-v2.txt .) |
a721e8b2 | 80 | .PP |
813d9220 MK |
81 | Because of the problems with the initial cgroups implementation |
82 | (cgroups version 1), | |
43df1ab3 MK |
83 | starting in Linux 3.10, work began on a new, |
84 | orthogonal implementation to remedy these problems. | |
85 | Initially marked experimental, and hidden behind the | |
86 | .I "\-o\ __DEVEL__sane_behavior" | |
87 | mount option, the new version (cgroups version 2) | |
88 | was eventually made official with the release of Linux 4.5. | |
89 | Differences between the two versions are described in the text below. | |
a721e8b2 | 90 | .PP |
43df1ab3 MK |
91 | Although cgroups v2 is intended as a replacement for cgroups v1, |
92 | the older system continues to exist | |
93 | (and for compatibility reasons is unlikely to be removed). | |
94 | Currently, cgroups v2 implements only a subset of the controllers | |
95 | available in cgroups v1. | |
96 | The two systems are implemented so that both v1 controllers and | |
97 | v2 controllers can be mounted on the same system. | |
98 | Thus, for example, it is possible to use those controllers | |
99 | that are supported under version 2, | |
100 | while also using version 1 controllers | |
101 | where version 2 does not yet support those controllers. | |
1a90a85e MK |
102 | The only restriction here is that a controller can't be simultaneously |
103 | employed in both a cgroups v1 hierarchy and in the cgroups v2 hierarchy. | |
43df1ab3 | 104 | .\" |
5714ccee | 105 | .SH CGROUPS VERSION 1 |
8bff7140 MK |
106 | Under cgroups v1, each controller may be mounted against a separate |
107 | cgroup filesystem that provides its own hierarchical organization of the | |
108 | processes on the system. | |
980f1827 | 109 | It is also possible to comount multiple (or even all) cgroups v1 controllers |
8bff7140 MK |
110 | against the same cgroup filesystem, meaning that the comounted controllers |
111 | manage the same hierarchical organization of processes. | |
a721e8b2 | 112 | .PP |
8bff7140 MK |
113 | For each mounted hierarchy, |
114 | the directory tree mirrors the control group hierarchy. | |
115 | Each control group is represented by a directory, with each of its child | |
116 | control cgroups represented as a child directory. | |
117 | For instance, | |
118 | .IR /user/joe/1.session | |
119 | represents control group | |
120 | .IR 1.session , | |
121 | which is a child of cgroup | |
122 | .IR joe , | |
123 | which is a child of | |
124 | .IR /user . | |
125 | Under each cgroup directory is a set of files which can be read or | |
126 | written to, reflecting resource limits and a few general cgroup | |
127 | properties. | |
8bff7140 | 128 | .\" |
6398ca15 | 129 | .SS Tasks (threads) versus processes |
c775bca2 MK |
130 | In cgroups v1, a distinction is drawn between |
131 | .I processes | |
132 | and | |
133 | .IR tasks . | |
134 | In this view, a process can consist of multiple tasks | |
6398ca15 MK |
135 | (more commonly called threads, from a user-space perspective, |
136 | and called such in the remainder of this man page). | |
0ec74e08 | 137 | In cgroups v1, it is possible to independently manipulate |
6398ca15 | 138 | the cgroup memberships of the threads in a process. |
c56ec51b MK |
139 | .PP |
140 | The cgroups v1 ability to split threads across different cgroups | |
141 | caused problems in some cases. | |
142 | For example, it made no sense for the | |
143 | .I memory | |
144 | controller, | |
145 | since all of the threads of a process share a single address space. | |
146 | Because of these problems, | |
c775bca2 | 147 | the ability to independently manipulate the cgroup memberships |
56769384 MK |
148 | of the threads in a process was removed in the initial cgroups v2 |
149 | implementation, and subsequently restored in a more limited form | |
150 | (see the discussion of "thread mode" below). | |
c775bca2 | 151 | .\" |
77e0a626 MK |
152 | .SS Mounting v1 controllers |
153 | The use of cgroups requires a kernel built with the | |
8e6578f8 KF |
154 | .BR CONFIG_CGROUP |
155 | option. | |
77e0a626 MK |
156 | In addition, each of the v1 controllers has an associated |
157 | configuration option that must be set in order to employ that controller. | |
a721e8b2 | 158 | .PP |
77e0a626 MK |
159 | In order to use a v1 controller, |
160 | it must be mounted against a cgroup filesystem. | |
4e07c70f MK |
161 | The usual place for such mounts is under a |
162 | .BR tmpfs (5) | |
163 | filesystem mounted at | |
77e0a626 MK |
164 | .IR /sys/fs/cgroup . |
165 | Thus, one might mount the | |
166 | .I cpu | |
167 | controller as follows: | |
a721e8b2 | 168 | .PP |
77e0a626 | 169 | .in +4n |
b8302363 | 170 | .EX |
77e0a626 | 171 | mount \-t cgroup \-o cpu none /sys/fs/cgroup/cpu |
b8302363 | 172 | .EE |
e646a1ba | 173 | .in |
a721e8b2 | 174 | .PP |
77e0a626 MK |
175 | It is possible to comount multiple controllers against the same hierarchy. |
176 | For example, here the | |
177 | .IR cpu | |
21f0d132 | 178 | and |
77e0a626 MK |
179 | .IR cpuacct |
180 | controllers are comounted against a single hierarchy: | |
a721e8b2 | 181 | .PP |
21f0d132 | 182 | .in +4n |
b8302363 | 183 | .EX |
77e0a626 | 184 | mount \-t cgroup \-o cpu,cpuacct none /sys/fs/cgroup/cpu,cpuacct |
b8302363 | 185 | .EE |
e646a1ba | 186 | .in |
a721e8b2 | 187 | .PP |
55f52de8 | 188 | Comounting controllers has the effect that a process is in the same cgroup for |
77e0a626 | 189 | all of the comounted controllers. |
55f52de8 | 190 | Separately mounting controllers allows a process to |
21f0d132 MK |
191 | be in cgroup |
192 | .I /foo1 | |
55f52de8 | 193 | for one controller while being in |
21f0d132 MK |
194 | .I /foo2/foo3 |
195 | for another. | |
a721e8b2 | 196 | .PP |
77e0a626 | 197 | It is possible to comount all v1 controllers against the same hierarchy: |
a721e8b2 | 198 | .PP |
77e0a626 | 199 | .in +4n |
b8302363 | 200 | .EX |
77e0a626 | 201 | mount \-t cgroup \-o all cgroup /sys/fs/cgroup |
b8302363 | 202 | .EE |
e646a1ba | 203 | .in |
a721e8b2 | 204 | .PP |
77e0a626 MK |
205 | (One can achieve the same result by omitting |
206 | .IR "\-o all" , | |
207 | since it is the default if no controllers are explicitly specified.) | |
a721e8b2 | 208 | .PP |
31ec2a5c MK |
209 | It is not possible to mount the same controller |
210 | against multiple cgroup hierarchies. | |
211 | For example, it is not possible to mount both the | |
212 | .I cpu | |
213 | and | |
214 | .I cpuacct | |
215 | controllers against one hierarchy, and to mount the | |
216 | .I cpu | |
217 | controller alone against another hierarchy. | |
218 | It is possible to create multiple mount points with exactly | |
219 | the same set of comounted controllers. | |
220 | However, in this case all that results is multiple mount points | |
221 | providing a view of the same hierarchy. | |
a721e8b2 | 222 | .PP |
77e0a626 MK |
223 | Note that on many systems, the v1 controllers are automatically mounted under |
224 | .IR /sys/fs/cgroup ; | |
225 | in particular, | |
226 | .BR systemd (1) | |
227 | automatically creates such mount points. | |
21f0d132 | 228 | .\" |
7409b54b MK |
229 | .SS Unmounting v1 controllers |
230 | A mounted cgroup filesystem can be unmounted using the | |
231 | .BR umount (8) | |
232 | command, as in the following example: | |
233 | .PP | |
234 | .in +4n | |
235 | .EX | |
236 | umount /sys/fs/cgroup/pids | |
237 | .EE | |
238 | .in | |
239 | .PP | |
240 | .IR "But note well" : | |
241 | a cgroup filesystem is unmounted only if it is not busy, | |
242 | that is, it has no child cgroups. | |
243 | If this is not the case, then the only effect of the | |
244 | .BR umount (8) | |
245 | is to make the mount invisible. | |
246 | Thus, to ensure that the mount point is really removed, | |
247 | one must first remove all child cgroups, | |
248 | which in turn can be done only after all member processes | |
249 | have been moved from those cgroups to the root cgroup. | |
250 | .\" | |
860573ad MK |
251 | .SS Cgroups version 1 controllers |
252 | Each of the cgroups version 1 controllers is governed | |
253 | by a kernel configuration option (listed below). | |
254 | Additionally, the availability of the cgroups feature is governed by the | |
255 | .BR CONFIG_CGROUPS | |
256 | kernel configuration option. | |
257 | .TP | |
258 | .IR cpu " (since Linux 2.6.24; " \fBCONFIG_CGROUP_SCHED\fP ) | |
259 | Cgroups can be guaranteed a minimum number of "CPU shares" | |
260 | when a system is busy. | |
261 | This does not limit a cgroup's CPU usage if the CPUs are not busy. | |
4ad9a706 MK |
262 | For further information, see |
263 | .IR Documentation/scheduler/sched-design-CFS.txt . | |
a721e8b2 | 264 | .IP |
4ad9a706 MK |
265 | In Linux 3.2, |
266 | this controller was extended to provide CPU "bandwidth" control. | |
267 | If the kernel is configured with | |
81ff7360 | 268 | .BR CONFIG_CFS_BANDWIDTH , |
4ad9a706 MK |
269 | then within each scheduling period |
270 | (defined via a file in the cgroup directory), it is possible to define | |
271 | an upper limit on the CPU time allocated to the processes in a cgroup. | |
272 | This upper limit applies even if there is no other competition for the CPU. | |
860573ad MK |
273 | Further information can be found in the kernel source file |
274 | .IR Documentation/scheduler/sched\-bwc.txt . | |
275 | .TP | |
276 | .IR cpuacct " (since Linux 2.6.24; " \fBCONFIG_CGROUP_CPUACCT\fP ) | |
277 | This provides accounting for CPU usage by groups of processes. | |
a721e8b2 | 278 | .IP |
860573ad MK |
279 | Further information can be found in the kernel source file |
280 | .IR Documentation/cgroup\-v1/cpuacct.txt . | |
281 | .TP | |
282 | .IR cpuset " (since Linux 2.6.24; " \fBCONFIG_CPUSETS\fP ) | |
283 | This cgroup can be used to bind the processes in a cgroup to | |
284 | a specified set of CPUs and NUMA nodes. | |
a721e8b2 | 285 | .IP |
860573ad MK |
286 | Further information can be found in the kernel source file |
287 | .IR Documentation/cgroup\-v1/cpusets.txt . | |
288 | .TP | |
289 | .IR memory " (since Linux 2.6.25; " \fBCONFIG_MEMCG\fP ) | |
290 | The memory controller supports reporting and limiting of process memory, kernel | |
291 | memory, and swap used by cgroups. | |
a721e8b2 | 292 | .IP |
860573ad MK |
293 | Further information can be found in the kernel source file |
294 | .IR Documentation/cgroup\-v1/memory.txt . | |
295 | .TP | |
296 | .IR devices " (since Linux 2.6.26; " \fBCONFIG_CGROUP_DEVICE\fP ) | |
297 | This supports controlling which processes may create (mknod) devices as | |
298 | well as open them for reading or writing. | |
299 | The policies may be specified as whitelists and blacklists. | |
300 | Hierarchy is enforced, so new rules must not | |
301 | violate existing rules for the target or ancestor cgroups. | |
a721e8b2 | 302 | .IP |
860573ad MK |
303 | Further information can be found in the kernel source file |
304 | .IR Documentation/cgroup-v1/devices.txt . | |
305 | .TP | |
306 | .IR freezer " (since Linux 2.6.28; " \fBCONFIG_CGROUP_FREEZER\fP ) | |
307 | The | |
308 | .IR freezer | |
309 | cgroup can suspend and restore (resume) all processes in a cgroup. | |
310 | Freezing a cgroup | |
311 | .I /A | |
312 | also causes its children, for example, processes in | |
313 | .IR /A/B , | |
314 | to be frozen. | |
a721e8b2 | 315 | .IP |
860573ad MK |
316 | Further information can be found in the kernel source file |
317 | .IR Documentation/cgroup-v1/freezer-subsystem.txt . | |
318 | .TP | |
319 | .IR net_cls " (since Linux 2.6.29; " \fBCONFIG_CGROUP_NET_CLASSID\fP ) | |
320 | This places a classid, specified for the cgroup, on network packets | |
321 | created by a cgroup. | |
322 | These classids can then be used in firewall rules, | |
323 | as well as used to shape traffic using | |
324 | .BR tc (8). | |
325 | This applies only to packets | |
326 | leaving the cgroup, not to traffic arriving at the cgroup. | |
a721e8b2 | 327 | .IP |
860573ad MK |
328 | Further information can be found in the kernel source file |
329 | .IR Documentation/cgroup-v1/net_cls.txt . | |
330 | .TP | |
331 | .IR blkio " (since Linux 2.6.33; " \fBCONFIG_BLK_CGROUP\fP ) | |
332 | The | |
333 | .I blkio | |
334 | cgroup controls and limits access to specified block devices by | |
335 | applying IO control in the form of throttling and upper limits against leaf | |
336 | nodes and intermediate nodes in the storage hierarchy. | |
a721e8b2 | 337 | .IP |
860573ad MK |
338 | Two policies are available. |
339 | The first is a proportional-weight time-based division | |
340 | of disk implemented with CFQ. | |
341 | This is in effect for leaf nodes using CFQ. | |
342 | The second is a throttling policy which specifies | |
343 | upper I/O rate limits on a device. | |
a721e8b2 | 344 | .IP |
860573ad MK |
345 | Further information can be found in the kernel source file |
346 | .IR Documentation/cgroup-v1/blkio-controller.txt . | |
347 | .TP | |
348 | .IR perf_event " (since Linux 2.6.39; " \fBCONFIG_CGROUP_PERF\fP ) | |
349 | This controller allows | |
350 | .I perf | |
351 | monitoring of the set of processes grouped in a cgroup. | |
a721e8b2 | 352 | .IP |
860573ad | 353 | Further information can be found in the kernel source file |
c174eb6a | 354 | .IR tools/perf/Documentation/perf-record.txt . |
860573ad MK |
355 | .TP |
356 | .IR net_prio " (since Linux 3.3; " \fBCONFIG_CGROUP_NET_PRIO\fP ) | |
357 | This allows priorities to be specified, per network interface, for cgroups. | |
a721e8b2 | 358 | .IP |
860573ad MK |
359 | Further information can be found in the kernel source file |
360 | .IR Documentation/cgroup-v1/net_prio.txt . | |
361 | .TP | |
362 | .IR hugetlb " (since Linux 3.5; " \fBCONFIG_CGROUP_HUGETLB\fP ) | |
363 | This supports limiting the use of huge pages by cgroups. | |
a721e8b2 | 364 | .IP |
860573ad MK |
365 | Further information can be found in the kernel source file |
366 | .IR Documentation/cgroup-v1/hugetlb.txt . | |
367 | .TP | |
368 | .IR pids " (since Linux 4.3; " \fBCONFIG_CGROUP_PIDS\fP ) | |
369 | This controller permits limiting the number of process that may be created | |
370 | in a cgroup (and its descendants). | |
a721e8b2 | 371 | .IP |
860573ad MK |
372 | Further information can be found in the kernel source file |
373 | .IR Documentation/cgroup-v1/pids.txt . | |
cfec905e NB |
374 | .TP |
375 | .IR rdma " (since Linux 4.11; " \fBCONFIG_CGROUP_RDMA\fP ) | |
d145c025 MK |
376 | The RDMA controller permits limiting the use of |
377 | RDMA/IB-specific resources per cgroup. | |
cfec905e NB |
378 | .IP |
379 | Further information can be found in the kernel source file | |
380 | .IR Documentation/cgroup-v1/rdma.txt . | |
860573ad | 381 | .\" |
6398ca15 | 382 | .SS Creating cgroups and moving processes |
9ed582ac | 383 | A cgroup filesystem initially contains a single root cgroup, '/', |
6398ca15 | 384 | which all processes belong to. |
21f0d132 | 385 | A new cgroup is created by creating a directory in the cgroup filesystem: |
a721e8b2 | 386 | .PP |
4769a778 MK |
387 | .in +4n |
388 | .EX | |
389 | mkdir /sys/fs/cgroup/cpu/cg1 | |
390 | .EE | |
391 | .in | |
a721e8b2 | 392 | .PP |
21f0d132 | 393 | This creates a new empty cgroup. |
a721e8b2 | 394 | .PP |
f524e7f8 | 395 | A process may be moved to this cgroup by writing its PID into the cgroup's |
21f0d132 | 396 | .I cgroup.procs |
21f0d132 | 397 | file: |
a721e8b2 | 398 | .PP |
4769a778 MK |
399 | .in +4n |
400 | .EX | |
401 | echo $$ > /sys/fs/cgroup/cpu/cg1/cgroup.procs | |
402 | .EE | |
403 | .in | |
a721e8b2 | 404 | .PP |
f524e7f8 | 405 | Only one PID at a time should be written to this file. |
a721e8b2 | 406 | .PP |
f524e7f8 MK |
407 | Writing the value 0 to a |
408 | .IR cgroup.procs | |
409 | file causes the writing process to be moved to the corresponding cgroup. | |
a721e8b2 | 410 | .PP |
6398ca15 MK |
411 | When writing a PID into the |
412 | .IR cgroup.procs , | |
87402a2e | 413 | all threads in the process are moved into the new cgroup at once. |
a721e8b2 | 414 | .PP |
f524e7f8 MK |
415 | Within a hierarchy, a process can be a member of exactly one cgroup. |
416 | Writing a process's PID to a | |
417 | .IR cgroup.procs | |
418 | file automatically removes it from the cgroup of | |
419 | which it was previously a member. | |
a721e8b2 | 420 | .PP |
f524e7f8 MK |
421 | The |
422 | .I cgroup.procs | |
423 | file can be read to obtain a list of the processes that are | |
424 | members of a cgroup. | |
425 | The returned list of PIDs is not guaranteed to be in order. | |
426 | Nor is it guaranteed to be free of duplicates. | |
427 | (For example, a PID may be recycled while reading from the list.) | |
a721e8b2 | 428 | .PP |
56769384 | 429 | In cgroups v1, an individual thread can be moved to |
87402a2e MK |
430 | another cgroup by writing its thread ID |
431 | (i.e., the kernel thread ID returned by | |
432 | .BR clone (2) | |
433 | and | |
434 | .BR gettid (2)) | |
435 | to the | |
436 | .IR tasks | |
437 | file in a cgroup directory. | |
438 | This file can be read to discover the set of threads | |
439 | that are members of the cgroup. | |
b43be47e MK |
440 | .\" |
441 | .SS Removing cgroups | |
442 | To remove a cgroup, | |
443 | it must first have no child cgroups and contain no (nonzombie) processes. | |
444 | So long as that is the case, one can simply | |
445 | remove the corresponding directory pathname. | |
446 | Note that files in a cgroup directory cannot and need not be | |
447 | removed. | |
448 | .\" | |
88afe701 | 449 | .SS Cgroups v1 release notification |
23388d41 MK |
450 | Two files can be used to determine whether the kernel provides |
451 | notifications when a cgroup becomes empty. | |
452 | A cgroup is considered to be empty when it contains no child | |
453 | cgroups and no member processes. | |
a721e8b2 | 454 | .PP |
23388d41 | 455 | A special file in the root directory of each cgroup hierarchy, |
88afe701 | 456 | .IR release_agent , |
23388d41 MK |
457 | can be used to register the pathname of a program that may be invoked when |
458 | a cgroup in the hierarchy becomes empty. | |
459 | The pathname of the newly empty cgroup (relative to the cgroup mount point) | |
460 | is provided as the sole command-line argument when the | |
461 | .IR release_agent | |
462 | program is invoked. | |
463 | The | |
464 | .IR release_agent | |
465 | program might remove the cgroup directory, | |
980f1827 | 466 | or perhaps repopulate it with a process. |
a721e8b2 | 467 | .PP |
23388d41 MK |
468 | The default value of the |
469 | .IR release_agent | |
470 | file is empty, meaning that no release agent is invoked. | |
a721e8b2 | 471 | .PP |
59af0514 MK |
472 | The content of the |
473 | .I release_agent | |
474 | file can also be specified via a mount option when the | |
475 | cgroup filesystem is mounted: | |
476 | .PP | |
477 | .in +4n | |
478 | .EX | |
479 | mount -o release_agent=pathname ... | |
480 | .EE | |
481 | .in | |
482 | .PP | |
23388d41 MK |
483 | Whether or not the |
484 | .IR release_agent | |
485 | program is invoked when a particular cgroup becomes empty is determined | |
486 | by the value in the | |
88afe701 | 487 | .IR notify_on_release |
23388d41 MK |
488 | file in the corresponding cgroup directory. |
489 | If this file contains the value 0, then the | |
490 | .IR release_agent | |
491 | program is not invoked. | |
492 | If it contains the value 1, the | |
493 | .IR release_agent | |
494 | program is invoked. | |
495 | The default value for this file in the root cgroup is 0. | |
496 | At the time when a new cgroup is created, | |
497 | the value in this file is inherited from the corresponding file | |
498 | in the parent cgroup. | |
88afe701 | 499 | .\" |
d311c798 MK |
500 | .SS Cgroup v1 named hierarchies |
501 | In cgroups v1, | |
502 | it is possible to mount a cgroup hierarchy that has no attached controllers: | |
503 | .PP | |
504 | .in +4n | |
505 | .EX | |
506 | mount -t cgroup -o none,name=somename none /some/mount/point | |
507 | .EE | |
508 | .in | |
509 | .PP | |
510 | Multiple instances of such hierarchies can be mounted; | |
511 | each hierarchy must have a unique name. | |
512 | The only purpose of such hierarchies is to track processes. | |
513 | (See the discussion of release notification below.) | |
514 | An example of this is the | |
515 | .I name=systemd | |
516 | cgroup hierarchy that is used by | |
517 | .BR systemd (1) | |
518 | to track services and user sessions. | |
29fa4cbc MK |
519 | .PP |
520 | Since Linux 5.0, the | |
521 | .I cgroup_no_v1 | |
522 | kernel boot option (described below) can be used to disable cgroup v1 | |
523 | named hierarchies, by specifying | |
524 | .IR cgroup_no_v1=named . | |
525 | ||
d311c798 | 526 | .\" |
5714ccee | 527 | .SH CGROUPS VERSION 2 |
b43be47e MK |
528 | In cgroups v2, |
529 | all mounted controllers reside in a single unified hierarchy. | |
530 | While (different) controllers may be simultaneously | |
531 | mounted under the v1 and v2 hierarchies, | |
532 | it is not possible to mount the same controller simultaneously | |
533 | under both the v1 and the v2 hierarchies. | |
a721e8b2 | 534 | .PP |
2befa495 MK |
535 | The new behaviors in cgroups v2 are summarized here, |
536 | and in some cases elaborated in the following subsections. | |
537 | .IP 1. 3 | |
a15e0673 | 538 | Cgroups v2 provides a unified hierarchy against |
dddb7ea1 MK |
539 | which all controllers are mounted. |
540 | .IP 2. | |
2befa495 MK |
541 | "Internal" processes are not permitted. |
542 | With the exception of the root cgroup, processes may reside | |
543 | only in leaf nodes (cgroups that do not themselves contain child cgroups). | |
4f017a68 | 544 | The details are somewhat more subtle than this, and are described below. |
dddb7ea1 | 545 | .IP 3. |
2befa495 MK |
546 | Active cgroups must be specified via the files |
547 | .IR cgroup.controllers | |
548 | and | |
549 | .IR cgroup.subtree_control . | |
dddb7ea1 | 550 | .IP 4. |
2befa495 MK |
551 | The |
552 | .I tasks | |
553 | file has been removed. | |
554 | In addition, the | |
555 | .I cgroup.clone_children | |
556 | file that is employed by the | |
557 | .I cpuset | |
558 | controller has been removed. | |
dddb7ea1 | 559 | .IP 5. |
2befa495 MK |
560 | An improved mechanism for notification of empty cgroups is provided by the |
561 | .IR cgroup.events | |
562 | file. | |
563 | .PP | |
564 | For more changes, see the | |
565 | .I Documentation/cgroup-v2.txt | |
566 | file in the kernel source. | |
e91d4f9e MK |
567 | .PP |
568 | Some of the new behaviors listed above saw subsequent modification with | |
569 | the addition in Linux 4.14 of "thread mode" (described below). | |
2befa495 | 570 | .\" |
dddb7ea1 MK |
571 | .SS Cgroups v2 unified hierarchy |
572 | In cgroups v1, the ability to mount different controllers | |
573 | against different hierarchies was intended to allow great flexibility | |
574 | for application design. | |
e91fc446 MK |
575 | In practice, though, |
576 | the flexibility turned out to be less useful than expected, | |
dddb7ea1 MK |
577 | and in many cases added complexity. |
578 | Therefore, in cgroups v2, | |
579 | all available controllers are mounted against a single hierarchy. | |
580 | The available controllers are automatically mounted, | |
581 | meaning that it is not necessary (or possible) to specify the controllers | |
582 | when mounting the cgroup v2 filesystem using a command such as the following: | |
a721e8b2 | 583 | .PP |
4769a778 MK |
584 | .in +4n |
585 | .EX | |
586 | mount -t cgroup2 none /mnt/cgroup2 | |
587 | .EE | |
588 | .in | |
a721e8b2 | 589 | .PP |
dddb7ea1 MK |
590 | A cgroup v2 controller is available only if it is not currently in use |
591 | via a mount against a cgroup v1 hierarchy. | |
592 | Or, to put things another way, it is not possible to employ | |
593 | the same controller against both a v1 hierarchy and the unified v2 hierarchy. | |
57cbb0db MK |
594 | This means that it may be necessary first to unmount a v1 controller |
595 | (as described above) before that controller is available in v2. | |
596 | Since | |
597 | .BR systemd (1) | |
598 | makes heavy use of some v1 controllers by default, | |
599 | it can in some cases be simpler to boot the system with | |
600 | selected v1 controllers disabled. | |
601 | To do this, specify the | |
602 | .IR cgroup_no_v1=list | |
603 | option on the kernel boot command line; | |
604 | .I list | |
605 | is a comma-separated list of the names of the controllers to disable, | |
606 | or the word | |
607 | .I all | |
608 | to disable all v1 controllers. | |
609 | (This situation is correctly handled by | |
610 | .BR systemd (1), | |
611 | which falls back to operating without the specified controllers.) | |
03bb1264 MK |
612 | .PP |
613 | Note that on many modern systems, | |
614 | .BR systemd (1) | |
615 | automatically mounts the | |
616 | .I cgroup2 | |
617 | filesystem at | |
618 | .I /sys/fs/cgroup/unified | |
619 | during the boot process. | |
dddb7ea1 | 620 | .\" |
44c429ed MK |
621 | .SS Cgroups v2 controllers |
622 | The following controllers, documented in the kernel source file | |
623 | .IR Documentation/cgroup-v2.txt , | |
624 | are supported in cgroups version 2: | |
625 | .TP | |
626 | .IR io " (since Linux 4.5)" | |
627 | This is the successor of the version 1 | |
628 | .I blkio | |
629 | controller. | |
630 | .TP | |
631 | .IR memory " (since Linux 4.5)" | |
632 | This is the successor of the version 1 | |
633 | .I memory | |
634 | controller. | |
635 | .TP | |
636 | .IR pids " (since Linux 4.5)" | |
637 | This is the same as the version 1 | |
638 | .I pids | |
639 | controller. | |
640 | .TP | |
641 | .IR perf_event " (since Linux 4.11)" | |
f7286edc | 642 | This is the same as the version 1 |
44c429ed MK |
643 | .I perf_event |
644 | controller. | |
645 | .TP | |
646 | .IR rdma " (since Linux 4.11)" | |
647 | This is the same as the version 1 | |
648 | .I rdma | |
649 | controller. | |
650 | .TP | |
651 | .IR cpu " (since Linux 4.15)" | |
652 | This is the successor to the version 1 | |
653 | .I cpu | |
654 | and | |
655 | .I cpuacct | |
656 | controllers. | |
657 | .\" | |
2befa495 | 658 | .SS Cgroups v2 subtree control |
8d5f42dc MK |
659 | Each cgroup in the v2 hierarchy contains the following two files: |
660 | .TP | |
661 | .IR cgroup.controllers | |
277559a4 | 662 | This read-only file exposes a list of the controllers that are |
8d5f42dc MK |
663 | .I available |
664 | in this cgroup. | |
665 | The contents of this file match the contents of the | |
666 | .I cgroup.subtree_control | |
667 | file in the parent cgroup. | |
668 | .TP | |
669 | .I cgroup.subtree_control | |
670 | This is a list of controllers that are | |
671 | .IR active | |
672 | .RI ( enabled ) | |
673 | in the cgroup. | |
674 | The set of controllers in this file is a subset of the set in the | |
21f0d132 | 675 | .IR cgroup.controllers |
8d5f42dc MK |
676 | of this cgroup. |
677 | The set of active controllers is modified by writing strings to this file | |
678 | containing space-delimited controller names, | |
679 | each preceded by '+' (to enable a controller) | |
680 | or '\-' (to disable a controller), as in the following example: | |
681 | .IP | |
682 | .in +4n | |
683 | .EX | |
684 | echo '+pids -memory' > x/y/cgroup.subtree_control | |
685 | .EE | |
686 | .in | |
687 | .IP | |
c9b101d1 MK |
688 | An attempt to enable a controller |
689 | that is not present in | |
690 | .I cgroup.controllers | |
691 | leads to an | |
692 | .B ENOENT | |
693 | error when writing to the | |
694 | .I cgroup.subtree_control | |
695 | file. | |
696 | .PP | |
8d5f42dc MK |
697 | Because the list of controllers in |
698 | .I cgroup.subtree_control | |
699 | is a subset of those | |
700 | .IR cgroup.controllers , | |
701 | a controller that has been disabled in one cgroup in the hierarchy | |
702 | can never be re-enabled in the subtree below that cgroup. | |
703 | .PP | |
704 | A cgroup's | |
705 | .I cgroup.subtree_control | |
706 | file determines the set of controllers that are exercised in the | |
707 | .I child | |
708 | cgroups. | |
709 | When a controller (e.g., | |
710 | .IR pids ) | |
711 | is present in the | |
712 | .I cgroup.subtree_control | |
713 | file of a parent cgroup, | |
714 | then the corresponding controller-interface files (e.g., | |
715 | .IR pids.max ) | |
716 | are automatically created in the children of that cgroup | |
717 | and can be used to exert resource control in the child cgroups. | |
21f0d132 | 718 | .\" |
2468f14e MK |
719 | .SS Cgroups v2 """no internal processes""" rule |
720 | Cgroups v2 enforces a so-called "no internal processes" rule. | |
721 | Roughly speaking, this rule means that, | |
722 | with the exception of the root cgroup, processes may reside | |
723 | only in leaf nodes (cgroups that do not themselves contain child cgroups). | |
724 | This avoids the need to decide how to partition resources between | |
725 | processes which are members of cgroup A and processes in child cgroups of A. | |
726 | .PP | |
727 | For instance, if cgroup | |
728 | .I /cg1/cg2 | |
729 | exists, then a process may reside in | |
730 | .IR /cg1/cg2 , | |
731 | but not in | |
732 | .IR /cg1 . | |
733 | This is to avoid an ambiguity in cgroups v1 | |
734 | with respect to the delegation of resources between processes in | |
735 | .I /cg1 | |
736 | and its child cgroups. | |
737 | The recommended approach in cgroups v2 is to create a subdirectory called | |
738 | .I leaf | |
739 | for any nonleaf cgroup which should contain processes, but no child cgroups. | |
740 | Thus, processes which previously would have gone into | |
741 | .I /cg1 | |
742 | would now go into | |
743 | .IR /cg1/leaf . | |
744 | This has the advantage of making explicit | |
745 | the relationship between processes in | |
746 | .I /cg1/leaf | |
747 | and | |
748 | .IR /cg1 's | |
749 | other children. | |
750 | .PP | |
751 | The "no internal processes" rule is in fact more subtle than stated above. | |
752 | More precisely, the rule is that a (nonroot) cgroup can't both | |
753 | (1) have member processes, and | |
754 | (2) distribute resources into child cgroups\(emthat is, have a nonempty | |
755 | .I cgroup.subtree_control | |
756 | file. | |
757 | Thus, it | |
758 | .I is | |
759 | possible for a cgroup to have both member processes and child cgroups, | |
760 | but before controllers can be enabled for that cgroup, | |
761 | the member processes must be moved out of the cgroup | |
762 | (e.g., perhaps into the child cgroups). | |
e91d4f9e MK |
763 | .PP |
764 | With the Linux 4.14 addition of "thread mode" (described below), | |
765 | the "no internal processes" rule has been relaxed in some cases. | |
2468f14e | 766 | .\" |
754f4cf5 MK |
767 | .SS Cgroups v2 cgroup.events file |
768 | With cgroups v2, a new mechanism is provided to obtain notification | |
769 | about when a cgroup becomes empty. | |
770 | The cgroups v1 | |
771 | .IR release_agent | |
772 | and | |
773 | .IR notify_on_release | |
774 | files are removed, and replaced by a new, more general-purpose file, | |
775 | .IR cgroup.events . | |
e5bd7e65 | 776 | This read-only file contains key-value pairs |
754f4cf5 MK |
777 | (delimited by newline characters, with the key and value separated by spaces) |
778 | that identify events or state for a cgroup. | |
779 | Currently, only one key appears in this file, | |
780 | .IR populated , | |
781 | which has either the value 0, | |
782 | meaning that the cgroup (and its descendants) | |
783 | contain no (nonzombie) processes, | |
784 | or 1, meaning that the cgroup contains member processes. | |
a721e8b2 | 785 | .PP |
754f4cf5 MK |
786 | The |
787 | .IR cgroup.events | |
788 | file can be monitored, in order to receive notification when a cgroup | |
789 | transitions between the populated and unpopulated states (or vice versa). | |
790 | When monitoring this file using | |
791 | .BR inotify (7), | |
792 | transitions generate | |
793 | .BR IN_MODIFY | |
794 | events, and when monitoring the file using | |
795 | .BR poll (2), | |
7747ed97 | 796 | transitions cause the bits |
754f4cf5 | 797 | .B POLLPRI |
7747ed97 MK |
798 | and |
799 | .B POLLERR | |
800 | to be returned in the | |
801 | .IR revents | |
802 | field. | |
a721e8b2 | 803 | .PP |
ccb1a262 MK |
804 | The cgroups v2 release-notification mechanism provided by the |
805 | .I populated | |
806 | field of the | |
807 | .I cgroup.events | |
808 | file offers at least two advantages over the cgroups v1 | |
754f4cf5 MK |
809 | .IR release_agent |
810 | mechanism. | |
811 | First, it allows for cheaper notification, | |
812 | since a single process can monitor multiple | |
813 | .IR cgroup.events | |
814 | files. | |
815 | By contrast, the cgroups v1 mechanism requires the creation | |
816 | of a process for each notification. | |
a15e0673 | 817 | Second, notification can be delegated to a process that lives inside |
754f4cf5 | 818 | a container associated with the newly empty cgroup. |
c91a9f8a | 819 | .\" |
5e071499 MK |
820 | .SS Cgroups v2 cgroup.stat file |
821 | .\" commit ec39225cca42c05ac36853d11d28f877fde5c42e | |
822 | Each cgroup in the v2 hierarchy contains a read-only | |
823 | .IR cgroup.stat | |
824 | file (first introduced in Linux 4.14) | |
825 | that consists of lines containing key-value pairs. | |
826 | The following keys currently appear in this file: | |
827 | .TP | |
828 | .I nr_descendants | |
829 | This is the total number of visible (i.e., living) descendant cgroups | |
830 | underneath this cgroup. | |
831 | .TP | |
832 | .I nr_dying_descendants | |
833 | This is the total number of dying descendant cgroups | |
834 | underneath this cgroup. | |
835 | A cgroup enters the dying state after being deleted. | |
836 | It remains in that state for an undefined period | |
837 | (which will depend on system load) | |
c7f63e74 MK |
838 | while resources are freed before the cgroup is destroyed. |
839 | Note that the presence of some cgroups in the dying state is normal, | |
840 | and is not indicative of any problem. | |
5e071499 MK |
841 | .IP |
842 | A process can't be made a member of a dying cgroup, | |
843 | and a dying cgroup can't be brought back to life. | |
844 | .\" | |
5845e10b MK |
845 | .SS Limiting the number of descendant cgroups |
846 | Each cgroup in the v2 hierarchy contains the following files, | |
847 | which can be used to view and set limits on the number | |
848 | of descendant cgroups under that cgroup: | |
849 | .TP | |
850 | .IR cgroup.max.depth " (since Linux 4.14)" | |
851 | .\" commit 1a926e0bbab83bae8207d05a533173425e0496d1 | |
852 | This file defines a limit on the depth of nesting of descendant cgroups. | |
853 | A value of 0 in this file means that no descendant cgroups can be created. | |
854 | An attempt to create a descendant whose nesting level exceeds | |
855 | the limit fails | |
856 | .RI ( mkdir (2) | |
857 | fails with the error | |
858 | .BR EAGAIN ). | |
859 | .IP | |
860 | Writing the string | |
861 | .IR """max""" | |
862 | to this file means that no limit is imposed. | |
863 | The default value in this file is | |
864 | .IR """max""" . | |
865 | .TP | |
866 | .IR cgroup.max.descendants " (since Linux 4.14)" | |
867 | .\" commit 1a926e0bbab83bae8207d05a533173425e0496d1 | |
868 | This file defines a limit on the number of live descendant cgroups that | |
869 | this cgroup may have. | |
870 | An attempt to create more descendants than allowed by the limit fails | |
871 | .RI ( mkdir (2) | |
872 | fails with the error | |
873 | .BR EAGAIN ). | |
874 | .IP | |
875 | Writing the string | |
876 | .IR """max""" | |
877 | to this file means that no limit is imposed. | |
878 | The default value in this file is | |
879 | .IR """max""" . | |
880 | .\" | |
4b1c2041 | 881 | .SH CGROUPS DELEGATION: DELEGATING A HIERARCHY TO A LESS PRIVILEGED USER |
4242dfbe MK |
882 | In the context of cgroups, |
883 | delegation means passing management of some subtree | |
51629a30 | 884 | of the cgroup hierarchy to a nonprivileged user. |
87b18a8b MK |
885 | Cgroups v1 provides support for delegation based on file permissions |
886 | in the cgroup hierarchy but with less strict containment rules than v2 | |
887 | (as noted below). | |
888 | Cgroups v2 supports delegation with containment by explicit design. | |
4b1c2041 MK |
889 | The focus of the discussion in this section is on delegation in cgroups v2, |
890 | with some differences for cgroups v1 noted along the way. | |
4242dfbe MK |
891 | .PP |
892 | Some terminology is required in order to describe delegation. | |
893 | A | |
894 | .I delegater | |
895 | is a privileged user (i.e., root) who owns a parent cgroup. | |
896 | A | |
897 | .I delegatee | |
898 | is a nonprivileged user who will be granted the permissions needed | |
899 | to manage some subhierarchy under that parent cgroup, | |
900 | known as the | |
901 | .IR "delegated subtree" . | |
902 | .PP | |
903 | To perform delegation, | |
904 | the delegater makes certain directories and files writable by the delegatee, | |
905 | typically by changing the ownership of the objects to be the user ID | |
906 | of the delegatee. | |
0735069b MK |
907 | Assuming that we want to delegate the hierarchy rooted at (say) |
908 | .I /dlgt_grp | |
4242dfbe MK |
909 | and that there are not yet any child cgroups under that cgroup, |
910 | the ownership of the following is changed to the user ID of the delegatee: | |
911 | .TP | |
0735069b | 912 | .IR /dlgt_grp |
4242dfbe MK |
913 | Changing the ownership of the root of the subtree means that any new |
914 | cgroups created under the subtree (and the files they contain) | |
915 | will also be owned by the delegatee. | |
916 | .TP | |
0735069b | 917 | .IR /dlgt_grp/cgroup.procs |
f7286edc | 918 | Changing the ownership of this file means that the delegatee |
4242dfbe MK |
919 | can move processes into the root of the delegated subtree. |
920 | .TP | |
4b1c2041 | 921 | .IR /dlgt_grp/cgroup.subtree_control " (cgroups v2 only)" |
e5936eb6 MK |
922 | Changing the ownership of this file means that that the delegatee |
923 | can enable controllers (that are present in | |
0735069b | 924 | .IR /dlgt_grp/cgroup.controllers ) |
4242dfbe | 925 | in order to further redistribute resources at lower levels in the subtree. |
e5936eb6 MK |
926 | (As an alternative to changing the ownership of this file, |
927 | the delegater might instead add selected controllers to this file.) | |
639b6c8c | 928 | .TP |
4b1c2041 | 929 | .IR /dlgt_grp/cgroup.threads " (cgroups v2 only)" |
639b6c8c MK |
930 | Changing the ownership of this file is necessary if a threaded subtree |
931 | is being delegated (see the description of "thread mode", below). | |
7b327dd5 | 932 | This permits the delegatee to write thread IDs to the file. |
cd7f4c49 MK |
933 | (The ownership of this file can also be changed when delegating |
934 | a domain subtree, but currently this serves no purpose, | |
935 | since, as described below, it is not possible to move a thread between | |
936 | domain cgroups by writing its thread ID to the | |
2b91ed4e | 937 | .IR cgroup.threads |
cd7f4c49 | 938 | file.) |
4b1c2041 MK |
939 | .IP |
940 | In cgroups v1, the corresponding file that should instead be delegated is the | |
941 | .I tasks | |
942 | file. | |
4242dfbe MK |
943 | .PP |
944 | The delegater should | |
945 | .I not | |
946 | change the ownership of any of the controller interfaces files (e.g., | |
947 | .IR pids.max , | |
948 | .IR memory.high ) | |
949 | in | |
0735069b | 950 | .IR dlgt_grp . |
4242dfbe MK |
951 | Those files are used from the next level above the delegated subtree |
952 | in order to distribute resources into the subtree, | |
953 | and the delegatee should not have permission to change | |
954 | the resources that are distributed into the delegated subtree. | |
955 | .PP | |
668ef765 MK |
956 | See also the discussion of the |
957 | .IR /sys/kernel/cgroup/delegate | |
4b1c2041 | 958 | file in NOTES for information about further delegatable files in cgroups v2. |
668ef765 | 959 | .PP |
4242dfbe MK |
960 | After the aforementioned steps have been performed, |
961 | the delegatee can create child cgroups within the delegated subtree | |
6dc513cd MK |
962 | (the cgroup subdirectories and the files they contain |
963 | will be owned by the delegatee) | |
4242dfbe MK |
964 | and move processes between cgroups in the subtree. |
965 | If some controllers are present in | |
0735069b | 966 | .IR dlgt_grp/cgroup.subtree_control , |
4242dfbe | 967 | or the ownership of that file was passed to the delegatee, |
f7286edc | 968 | the delegatee can also control the further redistribution |
4242dfbe | 969 | of the corresponding resources into the delegated subtree. |
27b086e9 | 970 | .\" |
ed3f4f34 | 971 | .SS Cgroups v2 delegation: nsdelegate and cgroup namespaces |
ed3f4f34 MK |
972 | Starting with Linux 4.13, |
973 | .\" commit 5136f6365ce3eace5a926e10f16ed2a233db5ba9 | |
4b1c2041 | 974 | there is a second way to perform cgroup delegation in the cgroups v2 hierarchy. |
07361828 | 975 | This is done by mounting or remounting the cgroup v2 filesystem with the |
ed3f4f34 | 976 | .I nsdelegate |
07361828 MK |
977 | mount option. |
978 | For example, if the cgroup v2 filesystem has already been mounted, | |
979 | we can remount it with the | |
980 | .I nsdelegate | |
981 | option as follows: | |
ed3f4f34 MK |
982 | .PP |
983 | .in +4n | |
984 | .EX | |
d1a71985 | 985 | mount -t cgroup2 -o remount,nsdelegate \e |
07361828 | 986 | none /sys/fs/cgroup/unified |
ed3f4f34 MK |
987 | .EE |
988 | .in | |
07361828 MK |
989 | .\" |
990 | .\" ALternatively, we could boot the kernel with the options: | |
991 | .\" | |
992 | .\" cgroup_no_v1=all systemd.legacy_systemd_cgroup_controller | |
993 | .\" | |
994 | .\" The effect of the latter option is to prevent systemd from employing | |
995 | .\" its "hybrid" cgroup mode, where it tries to make use of cgroups v2. | |
ed3f4f34 | 996 | .PP |
dc581e07 | 997 | The effect of this mount option is to cause cgroup namespaces |
ed3f4f34 MK |
998 | to automatically become delegation boundaries. |
999 | More specifically, | |
1000 | the following restrictions apply for processes inside the cgroup namespace: | |
1001 | .IP * 3 | |
446d1643 | 1002 | Writes to controller interface files in the root directory of the namespace |
ed3f4f34 MK |
1003 | will fail with the error |
1004 | .BR EPERM . | |
1005 | Processes inside the cgroup namespace can still write to delegatable | |
446d1643 | 1006 | files in the root directory of the cgroup namespace such as |
ed3f4f34 MK |
1007 | .IR cgroup.procs |
1008 | and | |
1009 | .IR cgroup.subtree_control , | |
446d1643 | 1010 | and can create subhierarchy underneath the root directory. |
ed3f4f34 MK |
1011 | .IP * |
1012 | Attempts to migrate processes across the namespace boundary are denied | |
1013 | (with the error | |
1014 | .BR ENOENT ). | |
1015 | Processes inside the cgroup namespace can still | |
1016 | (subject to the containment rules described below) | |
1017 | move processes between cgroups | |
1018 | .I within | |
1019 | the subhierarchy under the namespace root. | |
1020 | .PP | |
1021 | The ability to define cgroup namespaces as delegation boundaries | |
1022 | makes cgroup namespaces more useful. | |
1023 | To understand why, suppose that we already have one cgroup hierarchy | |
1024 | that has been delegated to a nonprivileged user, | |
1025 | .IR cecilia , | |
1026 | using the older delegation technique described above. | |
1027 | Suppose further that | |
1028 | .I cecilia | |
1029 | wanted to further delegate a subhierarchy | |
1030 | under the existing delegated hierarchy. | |
1031 | (For example, the delegated hierarchy might be associated with | |
1032 | an unprivileged container run by | |
1033 | .IR cecilia .) | |
1034 | Even if a cgroup namespace was employed, | |
1035 | because both hierarchies are owned by the unprivileged user | |
1036 | .IR cecilia , | |
1037 | the following illegitimate actions could be performed: | |
1038 | .IP * 3 | |
1039 | A process in the inferior hierarchy could change the | |
619dbe1c | 1040 | resource controller settings in the root directory of that hierarchy. |
ed3f4f34 MK |
1041 | (These resource controller settings are intended to allow control to |
1042 | be exercised from the | |
1043 | .I parent | |
1044 | cgroup; | |
1045 | a process inside the child cgroup should not be allowed to modify them.) | |
1046 | .IP * | |
1047 | A process inside the inferior hierarchy could move processes | |
1048 | into and out of the inferior hierarchy if the cgroups in the | |
1049 | superior hierarchy were somehow visible. | |
1050 | .PP | |
1051 | Employing the | |
1052 | .I nsdelegate | |
1053 | mount option prevents both of these possibilities. | |
1054 | .PP | |
1055 | The | |
1056 | .I nsdelegate | |
1057 | mount option only has an effect when performed in | |
1058 | the initial mount namespace; | |
1059 | in other mount namespaces, the option is silently ignored. | |
07361828 MK |
1060 | .PP |
1061 | .IR Note : | |
1062 | On some systems, | |
1063 | .BR systemd (1) | |
1064 | automatically mounts the cgroup v2 filesystem. | |
1065 | In order to experiment with the | |
1066 | .I nsdelegate | |
44084d19 MK |
1067 | operation, it may be useful to boot the kernel with |
1068 | the following command-line options: | |
1069 | .PP | |
1070 | .in +4n | |
1071 | .EX | |
1072 | cgroup_no_v1=all systemd.legacy_systemd_cgroup_controller | |
1073 | .EE | |
1074 | .in | |
1075 | .PP | |
1076 | These options cause the kernel to boot with the cgroups v1 controllers | |
1077 | disabled (meaning that the controllers are available in the v2 hierarchy), | |
1078 | and tells | |
1079 | .BR systemd (1) | |
1080 | not to mount and use the cgroup v2 hierarchy, | |
1081 | so that the v2 hierarchy can be manually mounted | |
1082 | with the desired options after boot-up. | |
ed3f4f34 | 1083 | .\" |
4b1c2041 | 1084 | .SS Cgroup delegation containment rules |
4242dfbe MK |
1085 | Some delegation |
1086 | .IR "containment rules" | |
1087 | ensure that the delegatee can move processes between cgroups within the | |
1088 | delegated subtree, | |
1089 | but can't move processes from outside the delegated subtree into | |
1090 | the subtree or vice versa. | |
1091 | A nonprivileged process (i.e., the delegatee) can write the PID of | |
1092 | a "target" process into a | |
1093 | .IR cgroup.procs | |
1094 | file only if all of the following are true: | |
1095 | .IP * 3 | |
4242dfbe MK |
1096 | The writer has write permission on the |
1097 | .I cgroup.procs | |
1098 | file in the destination cgroup. | |
1099 | .IP * | |
1100 | The writer has write permission on the | |
1101 | .I cgroup.procs | |
396761ee | 1102 | file in the nearest common ancestor of the source and destination cgroups. |
e366c4d4 MK |
1103 | Note that in some cases, |
1104 | the nearest common ancestor may be the source or destination cgroup itself. | |
4b1c2041 MK |
1105 | This requirement is not enforced for cgroups v1 hierarchies, |
1106 | with the consequence that containment in v1 is less strict than in v2. | |
1107 | (For example, in cgroups v1 the user that owns two distinct | |
1108 | delegated subhierarchies can move a process between the hierarchies.) | |
28f612ea | 1109 | .IP * |
ed3f4f34 MK |
1110 | If the cgroup v2 filesystem was mounted with the |
1111 | .I nsdelegate | |
7b574df5 | 1112 | option, the writer must be able to see the source and destination cgroups |
ed3f4f34 MK |
1113 | from its cgroup namespace. |
1114 | .IP * | |
4b1c2041 | 1115 | In cgroups v1: |
28f612ea MK |
1116 | the effective UID of the writer (i.e., the delegatee) matches the |
1117 | real user ID or the saved set-user-ID of the target process. | |
4b1c2041 MK |
1118 | Before Linux 4.11, |
1119 | .\" commit 576dd464505fc53d501bb94569db76f220104d28 | |
1120 | this requirement also applied in cgroups v2 | |
28f612ea MK |
1121 | (This was a historical requirement inherited from cgroups v1 |
1122 | that was later deemed unnecessary, | |
1123 | since the other rules suffice for containment in cgroups v2.) | |
4242dfbe MK |
1124 | .PP |
1125 | .IR Note : | |
1126 | one consequence of these delegation containment rules is that the | |
0735069b MK |
1127 | unprivileged delegatee can't place the first process into |
1128 | the delegated subtree; | |
1129 | instead, the delegater must place the first process | |
1130 | (a process owned by the delegatee) into the delegated subtree. | |
4242dfbe | 1131 | .\" |
75e83bc2 | 1132 | .SH CGROUPS VERSION 2 THREAD MODE |
c8902e25 MK |
1133 | Among the restrictions imposed by cgroups v2 that were not present |
1134 | in cgroups v1 are the following: | |
1135 | .IP * 3 | |
1136 | .IR "No thread-granularity control" : | |
1137 | all of the threads of a process must be in the same cgroup. | |
1138 | .IP * | |
1139 | .IR "No internal processes" : | |
1140 | a cgroup can't both have member processes and | |
1141 | exercise controllers on child cgroups. | |
1142 | .PP | |
1143 | Both of these restrictions were added because | |
1144 | the lack of these restrictions had caused problems | |
1145 | in cgroups v1. | |
1146 | In particular, the cgroups v1 ability to allow thread-level granularity | |
1147 | for cgroup membership made no sense for some controllers. | |
1148 | (A notable example was the | |
1149 | .I memory | |
1150 | controller: since threads share an address space, | |
1151 | it made no sense to split threads across different | |
1152 | .I memory | |
1153 | cgroups.) | |
1154 | .PP | |
1155 | Notwithstanding the initial design decision in cgroups v2, | |
1156 | there were use cases for certain controllers, notably the | |
1157 | .IR cpu | |
1158 | controller, | |
1159 | for which thread-level granularity of control was meaningful and useful. | |
1160 | To accommodate such use cases, Linux 4.14 added | |
1161 | .I "thread mode" | |
1162 | for cgroups v2. | |
1163 | .PP | |
1164 | Thread mode allows the following: | |
1165 | .IP * 3 | |
1166 | The creation of | |
1167 | .IR "threaded subtrees" | |
1168 | in which the threads of a process may | |
1169 | be spread across cgroups inside the tree. | |
1170 | (A threaded subtree may contain multiple multithreaded processes.) | |
1171 | .IP * | |
1172 | The concept of | |
1173 | .IR "threaded controllers", | |
1174 | which can distribute resources across the cgroups in a threaded subtree. | |
1175 | .IP * | |
1176 | A relaxation of the "no internal processes rule", | |
1177 | so that, within a threaded subtree, | |
1178 | a cgroup can both contain member threads and | |
1179 | exercise resource control over child cgroups. | |
1180 | .PP | |
1181 | With the addition of thread mode, | |
1182 | each nonroot cgroup now contains a new file, | |
1183 | .IR cgroup.type , | |
1184 | that exposes, and in some circumstances can be used to change, | |
1185 | the "type" of a cgroup. | |
1186 | This file contains one of the following type values: | |
1187 | .TP | |
1188 | .I "domain" | |
1189 | This is a normal v2 cgroup that provides process-granularity control. | |
1190 | If a process is a member of this cgroup, | |
1191 | then all threads of the process are (by definition) in the same cgroup. | |
1192 | This is the default cgroup type, | |
1193 | and provides the same behavior that was provided for | |
1194 | cgroups in the initial cgroups v2 implementation. | |
1195 | .TP | |
1196 | .I "threaded" | |
1197 | This cgroup is a member of a threaded subtree. | |
1198 | Threads can be added to this cgroup, | |
1199 | and controllers can be enabled for the cgroup. | |
1200 | .TP | |
1201 | .I "domain threaded" | |
1202 | This is a domain cgroup that serves as the root of a threaded subtree. | |
1203 | This cgroup type is also known as "threaded root". | |
1204 | .TP | |
1205 | .I "domain invalid" | |
1206 | This is a cgroup inside a threaded subtree | |
1207 | that is in an "invalid" state. | |
1208 | Processes can't be added to the cgroup, | |
1209 | and controllers can't be enabled for the cgroup. | |
1210 | The only thing that can be done with this cgroup (other than deleting it) | |
1211 | is to convert it to a | |
1212 | .IR threaded | |
1213 | cgroup by writing the string | |
1214 | .IR """threaded""" | |
1215 | to the | |
1216 | .I cgroup.type | |
1217 | file. | |
61254835 MK |
1218 | .IP |
1219 | The rationale for the existence of this "interim" type | |
1220 | during the creation of a threaded subtree | |
1221 | (rather than the kernel simply immediately converting all cgroups | |
1222 | under the threaded root to the type | |
1223 | .IR threaded ) | |
1224 | is to allow for | |
1225 | possible future extensions to the thread mode model | |
c8902e25 MK |
1226 | .\" |
1227 | .SS Threaded versus domain controllers | |
1228 | With the addition of threads mode, | |
1229 | cgroups v2 now distinguishes two types of resource controllers: | |
1230 | .IP * 3 | |
1231 | .I Threaded | |
2cd9bbfa | 1232 | .\" In the kernel source, look for ".threaded[ \t]*= true" in |
218eadf4 | 1233 | .\" initializations of struct cgroup_subsys |
c8902e25 MK |
1234 | controllers: these controllers support thread-granularity for |
1235 | resource control and can be enabled inside threaded subtrees, | |
1236 | with the result that the corresponding controller-interface files | |
1237 | appear inside the cgroups in the threaded subtree. | |
aa2c3623 | 1238 | As at Linux 4.19, the following controllers are threaded: |
c8902e25 MK |
1239 | .IR cpu , |
1240 | .IR perf_event , | |
1241 | and | |
1242 | .IR pids . | |
1243 | .IP * | |
1244 | .I Domain | |
1245 | controllers: these controllers support only process granularity | |
1246 | for resource control. | |
1247 | From the perspective of a domain controller, | |
1248 | all threads of a process are always in the same cgroup. | |
1249 | Domain controllers can't be enabled inside a threaded subtree. | |
1250 | .\" | |
1251 | .SS Creating a threaded subtree | |
1252 | There are two pathways that lead to the creation of a threaded subtree. | |
1253 | The first pathway proceeds as follows: | |
1254 | .IP 1. 3 | |
1255 | We write the string | |
1256 | .IR """threaded""" | |
1257 | to the | |
1258 | .I cgroup.type | |
1259 | file of a cgroup | |
1260 | .IR y/z | |
1261 | that currently has the type | |
1262 | .IR domain . | |
1263 | This has the following effects: | |
1264 | .RS | |
1265 | .IP * 3 | |
1266 | The type of the cgroup | |
1267 | .IR y/z | |
1268 | becomes | |
1269 | .IR threaded . | |
1270 | .IP * | |
1271 | The type of the parent cgroup, | |
1272 | .IR y , | |
1273 | becomes | |
1274 | .IR "domain threaded" . | |
1275 | The parent cgroup is the root of a threaded subtree | |
1276 | (also known as the "threaded root"). | |
1277 | .IP * | |
1278 | All other cgroups under | |
1279 | .IR y | |
1280 | that were not already of type | |
1281 | .IR threaded | |
1282 | (because they were inside already existing threaded subtrees | |
1283 | under the new threaded root) | |
1284 | are converted to type | |
1285 | .IR "domain invalid" . | |
1286 | Any subsequently created cgroups under | |
1287 | .I y | |
1288 | will also have the type | |
1289 | .IR "domain invalid" . | |
1290 | .RE | |
1291 | .IP 2. | |
1292 | We write the string | |
1293 | .IR """threaded""" | |
1294 | to each of the | |
1295 | .IR "domain invalid" | |
1296 | cgroups under | |
1297 | .IR y , | |
1298 | in order to convert them to the type | |
1299 | .IR threaded . | |
1300 | As a consequence of this step, all threads under the threaded root | |
1301 | now have the type | |
1302 | .IR threaded | |
1303 | and the threaded subtree is now fully usable. | |
1304 | The requirement to write | |
1305 | .IR """threaded""" | |
1306 | to each of these cgroups is somewhat cumbersome, | |
1307 | but allows for possible future extensions to the thread-mode model. | |
1308 | .PP | |
1309 | The second way of creating a threaded subtree is as follows: | |
1310 | .IP 1. 3 | |
1311 | In an existing cgroup, | |
1312 | .IR z , | |
1313 | that currently has the type | |
1314 | .IR domain , | |
1315 | we (1) enable one or more threaded controllers and | |
1316 | (2) make a process a member of | |
1317 | .IR z . | |
1318 | (These two steps can be done in either order.) | |
1319 | This has the following consequences: | |
1320 | .RS | |
1321 | .IP * 3 | |
1322 | The type of | |
1323 | .I z | |
1324 | becomes | |
1325 | .IR "domain threaded" . | |
1326 | .IP * | |
1327 | All of the descendant cgroups of | |
1328 | .I x | |
7a1cddd2 | 1329 | that were not already of type |
c8902e25 MK |
1330 | .IR threaded |
1331 | are converted to type | |
1332 | .IR "domain invalid" . | |
1333 | .RE | |
1334 | .IP 2. | |
1335 | As before, we make the threaded subtree usable by writing the string | |
1336 | .IR """threaded""" | |
1337 | to each of the | |
1338 | .IR "domain invalid" | |
1339 | cgroups under | |
1340 | .IR y , | |
1341 | in order to convert them to the type | |
1342 | .IR threaded . | |
1343 | .PP | |
1344 | One of the consequences of the above pathways to creating a threaded subtree | |
1345 | is that the threaded root cgroup can be a parent only to | |
1346 | .I threaded | |
1347 | (and | |
1348 | .IR "domain invalid" ) | |
1349 | cgroups. | |
1350 | The threaded root cgroup can't be a parent of a | |
1351 | .I domain | |
1352 | cgroups, and a | |
1353 | .I threaded | |
1354 | cgroup | |
1355 | can't have a sibling that is a | |
1356 | .I domain | |
1357 | cgroup. | |
1358 | .\" | |
1359 | .SS Using a threaded subtree | |
1360 | Within a threaded subtree, threaded controllers can be enabled | |
1361 | in each subgroup whose type has been changed to | |
1362 | .IR threaded ; | |
1363 | upon doing so, the corresponding controller interface files | |
1364 | appear in the children of that cgroup. | |
1365 | .PP | |
1366 | A process can be moved into a threaded subtree by writing its PID to the | |
1367 | .I cgroup.procs | |
1368 | file in one of the cgroups inside the tree. | |
1369 | This has the effect of making all of the threads | |
1370 | in the process members of the corresponding cgroup | |
1371 | and makes the process a member of the threaded subtree. | |
1372 | The threads of the process can then be spread across | |
1373 | the threaded subtree by writing their thread IDs (see | |
1374 | .BR gettid (2)) | |
1375 | to the | |
b2c3e720 | 1376 | .I cgroup.threads |
c8902e25 MK |
1377 | files in different cgroups inside the subtree. |
1378 | The threads of a process must all reside in the same threaded subtree. | |
1379 | .PP | |
d84e558e MK |
1380 | As with writing to |
1381 | .IR cgroup.procs , | |
1382 | some containment rules apply when writing to the | |
b2c3e720 | 1383 | .I cgroup.threads |
d84e558e MK |
1384 | file: |
1385 | .IP * 3 | |
1386 | The writer must have write permission on the | |
1387 | cgroup.threads | |
1388 | file in the destination cgroup. | |
1389 | .IP * | |
1390 | The writer must have write permission on the | |
1391 | .I cgroup.procs | |
1392 | file in the common ancestor of the source and destination cgroups. | |
1393 | (In some cases, | |
1394 | the common ancestor may be the source or destination cgroup itself.) | |
1395 | .IP * | |
1396 | The source and destination cgroups must be in the same threaded subtree. | |
1397 | (Outside a threaded subtree, an attempt to move a thread by writing | |
1398 | its thread ID to the | |
1399 | .I cgroup.threads | |
1400 | file in a different | |
1401 | .I domain | |
1402 | cgroup fails with the error | |
1403 | .BR EOPNOTSUPP .) | |
4178f132 MK |
1404 | .PP |
1405 | The | |
1406 | .I cgroup.threads | |
c8902e25 MK |
1407 | file is present in each cgroup (including |
1408 | .I domain | |
1409 | cgroups) and can be read in order to discover the set of threads | |
1410 | that is present in the cgroup. | |
1411 | The set of thread IDs obtained when reading this file | |
1412 | is not guaranteed to be ordered or free of duplicates. | |
1413 | .PP | |
1414 | The | |
1415 | .I cgroup.procs | |
1416 | file in the threaded root shows the PIDs of all processes | |
1417 | that are members of the threaded subtree. | |
1418 | The | |
1419 | .I cgroup.procs | |
1420 | files in the other cgroups in the subtree are not readable. | |
1421 | .PP | |
1422 | Domain controllers can't be enabled in a threaded subtree; | |
1423 | no controller-interface files appear inside the cgroups underneath the | |
1424 | threaded root. | |
1425 | From the point of view of a domain controller, | |
1426 | threaded subtrees are invisible: | |
1427 | a multithreaded process inside a threaded subtree appears to a domain | |
1428 | controller as a process that resides in the threaded root cgroup. | |
1429 | .PP | |
1430 | Within a threaded subtree, the "no internal processes" rule does not apply: | |
1431 | a cgroup can both contain member processes (or thread) | |
1432 | and exercise controllers on child cgroups. | |
1433 | .\" | |
1434 | .SS Rules for writing to cgroup.type and creating threaded subtrees | |
1435 | A number of rules apply when writing to the | |
1436 | .I cgroup.type | |
1437 | file: | |
1438 | .IP * 3 | |
1439 | Only the string | |
1440 | .IR """threaded""" | |
1441 | may be written. | |
1442 | In other words, the only explicit transition that is possible is to convert a | |
1443 | .I domain | |
1444 | cgroup to type | |
1445 | .IR threaded . | |
1446 | .IP * | |
6c9aa5ad | 1447 | The effect of writing |
c8902e25 | 1448 | .IR """threaded""" |
6c9aa5ad MK |
1449 | depends on the current value in |
1450 | .IR cgroup.type , | |
1451 | as follows: | |
c8902e25 MK |
1452 | .RS |
1453 | .IP \(bu 3 | |
6c9aa5ad MK |
1454 | .IR domain |
1455 | or | |
1456 | .IR "domain threaded" : | |
1457 | start the creation of a threaded subtree | |
1458 | (whose root is the parent of this cgroup) via | |
c8902e25 MK |
1459 | the first of the pathways described above; |
1460 | .IP \(bu | |
6c9aa5ad | 1461 | .IR "domain\ invalid" : |
4644794c | 1462 | convert this cgroup (which is inside a threaded subtree) to a usable (i.e., |
c8902e25 MK |
1463 | .IR threaded ) |
1464 | state; | |
1465 | .IP \(bu | |
6c9aa5ad MK |
1466 | .IR threaded : |
1467 | no effect (a "no-op"). | |
c8902e25 MK |
1468 | .RE |
1469 | .IP * | |
1470 | We can't write to a | |
1471 | .I cgroup.type | |
1472 | file if the parent's type is | |
1473 | .IR "domain invalid" . | |
1474 | In other words, the cgroups of a threaded subtree must be converted to the | |
1475 | .I threaded | |
1476 | state in a top-down manner. | |
1477 | .PP | |
00c27092 | 1478 | There are also some constraints that must be satisfied |
c8902e25 MK |
1479 | in order to create a threaded subtree rooted at the cgroup |
1480 | .IR x : | |
1481 | .IP * 3 | |
1482 | There can be no member processes in the descendant cgroups of | |
1483 | .IR x . | |
1484 | (The cgroup | |
1485 | .I x | |
1486 | can itself have member processes.) | |
1487 | .IP * | |
1488 | No domain controllers may be enabled in | |
1489 | .IR x 's | |
1490 | .IR cgroup.subtree_control | |
1491 | file. | |
c8902e25 MK |
1492 | .PP |
1493 | If any of the above constraints is violated, then an attempt to write | |
1494 | .IR """threaded""" | |
1495 | to a | |
1496 | .IR cgroup.type | |
1497 | file fails with the error | |
1498 | .BR ENOTSUP . | |
1499 | .\" | |
1500 | .SS The """domain threaded""" cgroup type | |
1501 | According to the pathways described above, | |
1502 | the type of a cgroup can change to | |
1503 | .IR "domain threaded" | |
1504 | in either of the following cases: | |
1505 | .IP * 3 | |
1506 | The string | |
1507 | .IR """threaded""" | |
1508 | is written to a child cgroup. | |
1509 | .IP * | |
1510 | A threaded controller is enabled inside the cgroup and | |
1511 | a process is made a member of the cgroup. | |
1512 | .PP | |
1513 | A | |
1514 | .IR "domain threaded" | |
1515 | cgroup, | |
1516 | .IR x , | |
1517 | can revert to the type | |
1518 | .IR domain | |
1519 | if the above conditions no longer hold true\(emthat is, if all | |
1520 | .I threaded | |
1521 | child cgroups of | |
1522 | .I x | |
1523 | are removed and either | |
1524 | .I x | |
1525 | no longer has threaded controllers enabled or | |
1526 | no longer has member processes. | |
1527 | .PP | |
1528 | When a | |
1529 | .IR "domain threaded" | |
1530 | cgroup | |
1531 | .IR x | |
1532 | reverts to the type | |
1533 | .IR domain : | |
1534 | .IP * 3 | |
1535 | All | |
1536 | .IR "domain invalid" | |
1537 | descendants of | |
1538 | .I x | |
1539 | that are not in lower-level threaded subtrees revert to the type | |
1540 | .IR domain . | |
1541 | .IP * | |
1542 | The root cgroups in any lower-level threaded subtrees revert to the type | |
1543 | .IR "domain threaded" . | |
1544 | .\" | |
1545 | .SS Exceptions for the root cgroup | |
1546 | The root cgroup of the v2 hierarchy is treated exceptionally: | |
1547 | it can be the parent of both | |
1548 | .I domain | |
1549 | and | |
1550 | .I threaded | |
1551 | cgroups. | |
1552 | If the string | |
1553 | .I """threaded""" | |
1554 | is written to the | |
1555 | .I cgroup.type | |
1556 | file of one of the children of the root cgroup, then | |
1557 | .IP * 3 | |
1558 | The type of that cgroup becomes | |
1559 | .IR threaded . | |
1560 | .IP * | |
1561 | The type of any descendants of that cgroup that | |
1562 | are not part of lower-level threaded subtrees changes to | |
1563 | .IR "domain invalid" . | |
1564 | .PP | |
1565 | Note that in this case, there is no cgroup whose type becomes | |
1566 | .IR "domain threaded" . | |
1567 | (Notionally, the root cgroup can be considered as the threaded root | |
1568 | for the cgroup whose type was changed to | |
1569 | .IR threaded .) | |
1570 | .PP | |
1571 | The aim of this exceptional treatment for the root cgroup is to | |
1572 | allow a threaded cgroup that employs the | |
1573 | .I cpu | |
1574 | controller to be placed as high as possible in the hierarchy, | |
1575 | so as to minimize the (small) cost of traversing the cgroup hierarchy. | |
1576 | .\" | |
edc90967 | 1577 | .SS The cgroups v2 """cpu""" controller and realtime threads |
aa2c3623 | 1578 | As at Linux 4.19, the cgroups v2 |
c8902e25 | 1579 | .I cpu |
0bef253e MK |
1580 | controller does not support control of realtime threads |
1581 | (specifically threads scheduled under any of the policies | |
1582 | .BR SCHED_FIFO , | |
1583 | .BR SCHED_RR , | |
1584 | described | |
1585 | .BR SCHED_DEADLINE ; | |
1586 | see | |
1587 | .BR sched (7)). | |
1588 | Therefore, the | |
1589 | .I cpu | |
1590 | controller can be enabled in the root cgroup only | |
c8902e25 | 1591 | if all realtime threads are in the root cgroup. |
edc90967 | 1592 | (If there are realtime threads in nonroot cgroups, then a |
c8902e25 MK |
1593 | .BR write (2) |
1594 | of the string | |
1595 | .IR """+cpu""" | |
1596 | to the | |
1597 | .I cgroup.subtree_control | |
1598 | file fails with the error | |
c2df7694 | 1599 | .BR EINVAL .) |
17094a28 MK |
1600 | .PP |
1601 | On some systems, | |
c8902e25 | 1602 | .BR systemd (1) |
edc90967 | 1603 | places certain realtime threads in nonroot cgroups in the v2 hierarchy. |
c8902e25 | 1604 | On such systems, |
edc90967 | 1605 | these threads must first be moved to the root cgroup before the |
c8902e25 MK |
1606 | .I cpu |
1607 | controller can be enabled. | |
1608 | .\" | |
1609 | .SH ERRORS | |
1610 | The following errors can occur for | |
1611 | .BR mount (2): | |
1612 | .TP | |
1613 | .B EBUSY | |
1614 | An attempt to mount a cgroup version 1 filesystem specified neither the | |
1615 | .I name= | |
1616 | option (to mount a named hierarchy) nor a controller name (or | |
1617 | .IR all ). | |
1618 | .SH NOTES | |
1619 | A child process created via | |
1620 | .BR fork (2) | |
1621 | inherits its parent's cgroup memberships. | |
1622 | A process's cgroup memberships are preserved across | |
1623 | .BR execve (2). | |
1624 | .\" | |
5c2181ad MK |
1625 | .SS /proc files |
1626 | .TP | |
34eb3340 | 1627 | .IR /proc/cgroups " (since Linux 2.6.24)" |
92bb6d36 | 1628 | This file contains information about the controllers |
1a4f7d59 | 1629 | that are compiled into the kernel. |
34eb3340 MK |
1630 | An example of the contents of this file (reformatted for readability) |
1631 | is the following: | |
a721e8b2 | 1632 | .IP |
34eb3340 | 1633 | .in +4n |
b8302363 | 1634 | .EX |
4580c2f6 MK |
1635 | #subsys_name hierarchy num_cgroups enabled |
1636 | cpuset 4 1 1 | |
1637 | cpu 8 1 1 | |
1638 | cpuacct 8 1 1 | |
1639 | blkio 6 1 1 | |
1640 | memory 3 1 1 | |
1641 | devices 10 84 1 | |
1642 | freezer 7 1 1 | |
1643 | net_cls 9 1 1 | |
1644 | perf_event 5 1 1 | |
1645 | net_prio 9 1 1 | |
1646 | hugetlb 0 1 0 | |
1647 | pids 2 1 1 | |
b8302363 | 1648 | .EE |
e646a1ba | 1649 | .in |
a721e8b2 | 1650 | .IP |
34eb3340 MK |
1651 | The fields in this file are, from left to right: |
1652 | .RS | |
1653 | .IP 1. 3 | |
1654 | The name of the controller. | |
1655 | .IP 2. | |
92bb6d36 | 1656 | The unique ID of the cgroup hierarchy on which this controller is mounted. |
11c0797f | 1657 | If multiple cgroups v1 controllers are bound to the same hierarchy, |
34eb3340 | 1658 | then each will show the same hierarchy ID in this field. |
92bb6d36 MK |
1659 | The value in this field will be 0 if: |
1660 | .RS 5 | |
1661 | .IP a) 3 | |
1662 | the controller is not mounted on a cgroups v1 hierarchy; | |
1663 | .IP b) | |
1664 | the controller is bound to the cgroups v2 single unified hierarchy; or | |
1665 | .IP c) | |
1666 | the controller is disabled (see below). | |
1667 | .RE | |
34eb3340 MK |
1668 | .IP 3. |
1669 | The number of control groups in this hierarchy using this controller. | |
1670 | .IP 4. | |
1671 | This field contains the value 1 if this controller is enabled, | |
1672 | or 0 if it has been disabled (via the | |
1673 | .IR cgroup_disable | |
1674 | kernel command-line boot parameter). | |
1675 | .RE | |
1676 | .TP | |
5c2181ad | 1677 | .IR /proc/[pid]/cgroup " (since Linux 2.6.24)" |
f5faa016 MK |
1678 | This file describes control groups to which the process |
1679 | with the corresponding PID belongs. | |
5f8a7eb2 | 1680 | The displayed information differs for |
2c4fbe35 | 1681 | cgroups version 1 and version 2 hierarchies. |
a721e8b2 | 1682 | .IP |
5f8a7eb2 | 1683 | For each cgroup hierarchy of which the process is a member, |
2e33b59e | 1684 | there is one entry containing three colon-separated fields: |
a721e8b2 | 1685 | .IP |
4769a778 MK |
1686 | .in +4n |
1687 | .EX | |
1688 | hierarchy-ID:controller-list:cgroup-path | |
1689 | .EE | |
1690 | .in | |
a721e8b2 | 1691 | .IP |
5f8a7eb2 | 1692 | For example: |
c1a022dc MK |
1693 | .IP |
1694 | .in +4n | |
1695 | .EX | |
1696 | 5:cpuacct,cpu,cpuset:/daemons | |
1697 | .EE | |
1698 | .in | |
5c2181ad MK |
1699 | .IP |
1700 | The colon-separated fields are, from left to right: | |
5f8a7eb2 | 1701 | .RS |
5c2181ad | 1702 | .IP 1. 3 |
5f8a7eb2 MK |
1703 | For cgroups version 1 hierarchies, |
1704 | this field contains a unique hierarchy ID number | |
1705 | that can be matched to a hierarchy ID in | |
1706 | .IR /proc/cgroups . | |
1707 | For the cgroups version 2 hierarchy, this field contains the value 0. | |
5c2181ad | 1708 | .IP 2. |
5f8a7eb2 | 1709 | For cgroups version 1 hierarchies, |
55f52de8 | 1710 | this field contains a comma-separated list of the controllers |
5f8a7eb2 MK |
1711 | bound to the hierarchy. |
1712 | For the cgroups version 2 hierarchy, this field is empty. | |
5c2181ad | 1713 | .IP 3. |
5f8a7eb2 MK |
1714 | This field contains the pathname of the control group in the hierarchy |
1715 | to which the process belongs. | |
1716 | This pathname is relative to the mount point of the hierarchy. | |
5c2181ad | 1717 | .RE |
668ef765 MK |
1718 | .\" |
1719 | .SS /sys/kernel/cgroup files | |
1720 | .TP | |
1721 | .IR /sys/kernel/cgroup/delegate " (since Linux 4.15)" | |
1722 | .\" commit 01ee6cfb1483fe57c9cbd8e73817dfbf9bacffd3 | |
1723 | This file exports a list of the cgroups v2 files | |
1724 | (one per line) that are delegatable | |
1725 | (i.e., whose ownership should be changed to the user ID of the delegatee). | |
1726 | In the future, the set of delegatable files may change or grow, | |
1727 | and this file provides a way for the kernel to inform | |
1728 | user-space applications of which files must be delegated. | |
1729 | As at Linux 4.15, one sees the following when inspecting this file: | |
1730 | .IP | |
1731 | .EX | |
1732 | .in +4n | |
1733 | $ \fBcat /sys/kernel/cgroup/delegate\fP | |
1734 | cgroup.procs | |
1735 | cgroup.subtree_control | |
c7913617 | 1736 | cgroup.threads |
668ef765 MK |
1737 | .in |
1738 | .EE | |
6413d784 MK |
1739 | .TP |
1740 | .IR /sys/kernel/cgroup/features " (since Linux 4.15)" | |
1741 | .\" commit 5f2e673405b742be64e7c3604ed4ed3ac14f35ce | |
1742 | Over time, the set of cgroups v2 features that are provided by the | |
1743 | kernel may change or grow, | |
1744 | or some features may not be enabled by default. | |
1745 | This file provides a way for user-space applications to discover what | |
fcf115f5 | 1746 | features the running kernel supports and has enabled. |
6413d784 MK |
1747 | Features are listed one per line: |
1748 | .IP | |
1749 | .in +4n | |
1750 | .EX | |
6413d784 MK |
1751 | $ \fBcat /sys/kernel/cgroup/features\fP |
1752 | nsdelegate | |
2e69ff53 | 1753 | .EE |
6413d784 MK |
1754 | .in |
1755 | .IP | |
1756 | The entries that can appear in this file are: | |
1757 | .RS | |
1758 | .TP | |
1759 | .IR nsdelegate " (since Linux 4.15)" | |
1760 | The kernel supports the | |
1761 | .I nsdelegate | |
1762 | mount option. | |
1763 | .RE | |
bbfdf727 | 1764 | .SH SEE ALSO |
ebbc83be | 1765 | .BR prlimit (1), |
f60a5da2 | 1766 | .BR systemd (1), |
edc2a022 MK |
1767 | .BR systemd-cgls (1), |
1768 | .BR systemd-cgtop (1), | |
325b7eb0 | 1769 | .BR clone (2), |
ebbc83be MK |
1770 | .BR ioprio_set (2), |
1771 | .BR perf_event_open (2), | |
1772 | .BR setrlimit (2), | |
cff6de30 | 1773 | .BR cgroup_namespaces (7), |
69c47536 | 1774 | .BR cpuset (7), |
ebbc83be MK |
1775 | .BR namespaces (7), |
1776 | .BR sched (7), | |
1777 | .BR user_namespaces (7) |