]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/cgroup.c
cgroup: Implement default propagation of MemoryLow with DefaultMemoryLow
[thirdparty/systemd.git] / src / core / cgroup.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
8e274523 2
c6c18be3 3#include <fcntl.h>
e41969e3 4#include <fnmatch.h>
8c6db833 5
afcfaa69
LP
6#include "sd-messages.h"
7
b5efdb8a 8#include "alloc-util.h"
18c528e9 9#include "blockdev-util.h"
d8b4d14d 10#include "bpf-devices.h"
906c06f6 11#include "bpf-firewall.h"
45c2e068 12#include "btrfs-util.h"
6592b975 13#include "bus-error.h"
03a7b521 14#include "cgroup-util.h"
3ffd4af2
LP
15#include "cgroup.h"
16#include "fd-util.h"
0d39fa9c 17#include "fileio.h"
77601719 18#include "fs-util.h"
d8b4d14d 19#include "nulstr-util.h"
6bedfcbb 20#include "parse-util.h"
9eb977db 21#include "path-util.h"
03a7b521 22#include "process-util.h"
c36a69f4 23#include "procfs-util.h"
9444b1f2 24#include "special.h"
74c48bf5 25#include "stat-util.h"
906c06f6 26#include "stdio-util.h"
8b43440b 27#include "string-table.h"
07630cea 28#include "string-util.h"
cc6271f1 29#include "virt.h"
8e274523 30
10f28641 31#define CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC)
9a054909 32
39b9fefb
LP
33/* Returns the log level to use when cgroup attribute writes fail. When an attribute is missing or we have access
34 * problems we downgrade to LOG_DEBUG. This is supposed to be nice to container managers and kernels which want to mask
35 * out specific attributes from us. */
36#define LOG_LEVEL_CGROUP_WRITE(r) (IN_SET(abs(r), ENOENT, EROFS, EACCES, EPERM) ? LOG_DEBUG : LOG_WARNING)
37
611c4f8a 38bool manager_owns_host_root_cgroup(Manager *m) {
cc6271f1
LP
39 assert(m);
40
41 /* Returns true if we are managing the root cgroup. Note that it isn't sufficient to just check whether the
42 * group root path equals "/" since that will also be the case if CLONE_NEWCGROUP is in the mix. Since there's
43 * appears to be no nice way to detect whether we are in a CLONE_NEWCGROUP namespace we instead just check if
44 * we run in any kind of container virtualization. */
45
28cfdc5a
LP
46 if (MANAGER_IS_USER(m))
47 return false;
48
cc6271f1
LP
49 if (detect_container() > 0)
50 return false;
51
57ea45e1 52 return empty_or_root(m->cgroup_root);
cc6271f1
LP
53}
54
611c4f8a 55bool unit_has_host_root_cgroup(Unit *u) {
f3725e64
LP
56 assert(u);
57
cc6271f1
LP
58 /* Returns whether this unit manages the root cgroup. This will return true if this unit is the root slice and
59 * the manager manages the root cgroup. */
f3725e64 60
611c4f8a 61 if (!manager_owns_host_root_cgroup(u->manager))
f3725e64
LP
62 return false;
63
cc6271f1 64 return unit_has_name(u, SPECIAL_ROOT_SLICE);
f3725e64
LP
65}
66
293d32df
LP
67static int set_attribute_and_warn(Unit *u, const char *controller, const char *attribute, const char *value) {
68 int r;
69
70 r = cg_set_attribute(controller, u->cgroup_path, attribute, value);
71 if (r < 0)
72 log_unit_full(u, LOG_LEVEL_CGROUP_WRITE(r), r, "Failed to set '%s' attribute on '%s' to '%.*s': %m",
73 strna(attribute), isempty(u->cgroup_path) ? "/" : u->cgroup_path, (int) strcspn(value, NEWLINE), value);
74
75 return r;
76}
77
2b40998d 78static void cgroup_compat_warn(void) {
128fadc9
TH
79 static bool cgroup_compat_warned = false;
80
81 if (cgroup_compat_warned)
82 return;
83
cc6271f1
LP
84 log_warning("cgroup compatibility translation between legacy and unified hierarchy settings activated. "
85 "See cgroup-compat debug messages for details.");
86
128fadc9
TH
87 cgroup_compat_warned = true;
88}
89
90#define log_cgroup_compat(unit, fmt, ...) do { \
91 cgroup_compat_warn(); \
92 log_unit_debug(unit, "cgroup-compat: " fmt, ##__VA_ARGS__); \
2b40998d 93 } while (false)
128fadc9 94
4ad49000
LP
95void cgroup_context_init(CGroupContext *c) {
96 assert(c);
97
de8a711a 98 /* Initialize everything to the kernel defaults. */
4ad49000 99
de8a711a
LP
100 *c = (CGroupContext) {
101 .cpu_weight = CGROUP_WEIGHT_INVALID,
102 .startup_cpu_weight = CGROUP_WEIGHT_INVALID,
103 .cpu_quota_per_sec_usec = USEC_INFINITY,
10f28641 104 .cpu_quota_period_usec = USEC_INFINITY,
66ebf6c0 105
de8a711a
LP
106 .cpu_shares = CGROUP_CPU_SHARES_INVALID,
107 .startup_cpu_shares = CGROUP_CPU_SHARES_INVALID,
d53d9474 108
de8a711a
LP
109 .memory_high = CGROUP_LIMIT_MAX,
110 .memory_max = CGROUP_LIMIT_MAX,
111 .memory_swap_max = CGROUP_LIMIT_MAX,
da4d897e 112
de8a711a 113 .memory_limit = CGROUP_LIMIT_MAX,
b2f8b02e 114
de8a711a
LP
115 .io_weight = CGROUP_WEIGHT_INVALID,
116 .startup_io_weight = CGROUP_WEIGHT_INVALID,
13c31542 117
de8a711a
LP
118 .blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID,
119 .startup_blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID,
d53d9474 120
de8a711a
LP
121 .tasks_max = CGROUP_LIMIT_MAX,
122 };
4ad49000 123}
8e274523 124
4ad49000
LP
125void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) {
126 assert(c);
127 assert(a);
128
71fda00f 129 LIST_REMOVE(device_allow, c->device_allow, a);
4ad49000
LP
130 free(a->path);
131 free(a);
132}
133
13c31542
TH
134void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w) {
135 assert(c);
136 assert(w);
137
138 LIST_REMOVE(device_weights, c->io_device_weights, w);
139 free(w->path);
140 free(w);
141}
142
6ae4283c
TH
143void cgroup_context_free_io_device_latency(CGroupContext *c, CGroupIODeviceLatency *l) {
144 assert(c);
145 assert(l);
146
147 LIST_REMOVE(device_latencies, c->io_device_latencies, l);
148 free(l->path);
149 free(l);
150}
151
13c31542
TH
152void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l) {
153 assert(c);
154 assert(l);
155
156 LIST_REMOVE(device_limits, c->io_device_limits, l);
157 free(l->path);
158 free(l);
159}
160
4ad49000
LP
161void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w) {
162 assert(c);
163 assert(w);
164
71fda00f 165 LIST_REMOVE(device_weights, c->blockio_device_weights, w);
4ad49000
LP
166 free(w->path);
167 free(w);
168}
169
170void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b) {
171 assert(c);
8e274523 172 assert(b);
8e274523 173
71fda00f 174 LIST_REMOVE(device_bandwidths, c->blockio_device_bandwidths, b);
4ad49000
LP
175 free(b->path);
176 free(b);
177}
178
179void cgroup_context_done(CGroupContext *c) {
180 assert(c);
181
13c31542
TH
182 while (c->io_device_weights)
183 cgroup_context_free_io_device_weight(c, c->io_device_weights);
184
6ae4283c
TH
185 while (c->io_device_latencies)
186 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
187
13c31542
TH
188 while (c->io_device_limits)
189 cgroup_context_free_io_device_limit(c, c->io_device_limits);
190
4ad49000
LP
191 while (c->blockio_device_weights)
192 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
193
194 while (c->blockio_device_bandwidths)
195 cgroup_context_free_blockio_device_bandwidth(c, c->blockio_device_bandwidths);
196
197 while (c->device_allow)
198 cgroup_context_free_device_allow(c, c->device_allow);
6a48d82f
DM
199
200 c->ip_address_allow = ip_address_access_free_all(c->ip_address_allow);
201 c->ip_address_deny = ip_address_access_free_all(c->ip_address_deny);
4ad49000
LP
202}
203
204void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
13c31542
TH
205 CGroupIODeviceLimit *il;
206 CGroupIODeviceWeight *iw;
6ae4283c 207 CGroupIODeviceLatency *l;
4ad49000
LP
208 CGroupBlockIODeviceBandwidth *b;
209 CGroupBlockIODeviceWeight *w;
210 CGroupDeviceAllow *a;
c21c9906 211 IPAddressAccessItem *iaai;
9a054909 212 char u[FORMAT_TIMESPAN_MAX];
10f28641 213 char v[FORMAT_TIMESPAN_MAX];
4ad49000
LP
214
215 assert(c);
216 assert(f);
217
218 prefix = strempty(prefix);
219
220 fprintf(f,
221 "%sCPUAccounting=%s\n"
13c31542 222 "%sIOAccounting=%s\n"
4ad49000
LP
223 "%sBlockIOAccounting=%s\n"
224 "%sMemoryAccounting=%s\n"
d53d9474 225 "%sTasksAccounting=%s\n"
c21c9906 226 "%sIPAccounting=%s\n"
66ebf6c0
TH
227 "%sCPUWeight=%" PRIu64 "\n"
228 "%sStartupCPUWeight=%" PRIu64 "\n"
d53d9474
LP
229 "%sCPUShares=%" PRIu64 "\n"
230 "%sStartupCPUShares=%" PRIu64 "\n"
b2f8b02e 231 "%sCPUQuotaPerSecSec=%s\n"
10f28641 232 "%sCPUQuotaPeriodSec=%s\n"
13c31542
TH
233 "%sIOWeight=%" PRIu64 "\n"
234 "%sStartupIOWeight=%" PRIu64 "\n"
d53d9474
LP
235 "%sBlockIOWeight=%" PRIu64 "\n"
236 "%sStartupBlockIOWeight=%" PRIu64 "\n"
c52db42b 237 "%sDefaultMemoryLow=%" PRIu64 "\n"
48422635 238 "%sMemoryMin=%" PRIu64 "\n"
da4d897e
TH
239 "%sMemoryLow=%" PRIu64 "\n"
240 "%sMemoryHigh=%" PRIu64 "\n"
241 "%sMemoryMax=%" PRIu64 "\n"
96e131ea 242 "%sMemorySwapMax=%" PRIu64 "\n"
4ad49000 243 "%sMemoryLimit=%" PRIu64 "\n"
03a7b521 244 "%sTasksMax=%" PRIu64 "\n"
a931ad47
LP
245 "%sDevicePolicy=%s\n"
246 "%sDelegate=%s\n",
4ad49000 247 prefix, yes_no(c->cpu_accounting),
13c31542 248 prefix, yes_no(c->io_accounting),
4ad49000
LP
249 prefix, yes_no(c->blockio_accounting),
250 prefix, yes_no(c->memory_accounting),
d53d9474 251 prefix, yes_no(c->tasks_accounting),
c21c9906 252 prefix, yes_no(c->ip_accounting),
66ebf6c0
TH
253 prefix, c->cpu_weight,
254 prefix, c->startup_cpu_weight,
4ad49000 255 prefix, c->cpu_shares,
95ae05c0 256 prefix, c->startup_cpu_shares,
b1d6dcf5 257 prefix, format_timespan(u, sizeof(u), c->cpu_quota_per_sec_usec, 1),
10f28641 258 prefix, format_timespan(v, sizeof(v), c->cpu_quota_period_usec, 1),
13c31542
TH
259 prefix, c->io_weight,
260 prefix, c->startup_io_weight,
4ad49000 261 prefix, c->blockio_weight,
95ae05c0 262 prefix, c->startup_blockio_weight,
c52db42b 263 prefix, c->default_memory_low,
48422635 264 prefix, c->memory_min,
da4d897e
TH
265 prefix, c->memory_low,
266 prefix, c->memory_high,
267 prefix, c->memory_max,
96e131ea 268 prefix, c->memory_swap_max,
4ad49000 269 prefix, c->memory_limit,
03a7b521 270 prefix, c->tasks_max,
a931ad47
LP
271 prefix, cgroup_device_policy_to_string(c->device_policy),
272 prefix, yes_no(c->delegate));
4ad49000 273
02638280
LP
274 if (c->delegate) {
275 _cleanup_free_ char *t = NULL;
276
277 (void) cg_mask_to_string(c->delegate_controllers, &t);
278
47a78d41 279 fprintf(f, "%sDelegateControllers=%s\n",
02638280
LP
280 prefix,
281 strempty(t));
282 }
283
4ad49000
LP
284 LIST_FOREACH(device_allow, a, c->device_allow)
285 fprintf(f,
286 "%sDeviceAllow=%s %s%s%s\n",
287 prefix,
288 a->path,
289 a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : "");
290
13c31542
TH
291 LIST_FOREACH(device_weights, iw, c->io_device_weights)
292 fprintf(f,
6ae4283c 293 "%sIODeviceWeight=%s %" PRIu64 "\n",
13c31542
TH
294 prefix,
295 iw->path,
296 iw->weight);
297
6ae4283c
TH
298 LIST_FOREACH(device_latencies, l, c->io_device_latencies)
299 fprintf(f,
300 "%sIODeviceLatencyTargetSec=%s %s\n",
301 prefix,
302 l->path,
303 format_timespan(u, sizeof(u), l->target_usec, 1));
304
13c31542
TH
305 LIST_FOREACH(device_limits, il, c->io_device_limits) {
306 char buf[FORMAT_BYTES_MAX];
9be57249
TH
307 CGroupIOLimitType type;
308
309 for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
310 if (il->limits[type] != cgroup_io_limit_defaults[type])
311 fprintf(f,
312 "%s%s=%s %s\n",
313 prefix,
314 cgroup_io_limit_type_to_string(type),
315 il->path,
316 format_bytes(buf, sizeof(buf), il->limits[type]));
13c31542
TH
317 }
318
4ad49000
LP
319 LIST_FOREACH(device_weights, w, c->blockio_device_weights)
320 fprintf(f,
d53d9474 321 "%sBlockIODeviceWeight=%s %" PRIu64,
4ad49000
LP
322 prefix,
323 w->path,
324 w->weight);
325
326 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
327 char buf[FORMAT_BYTES_MAX];
328
979d0311
TH
329 if (b->rbps != CGROUP_LIMIT_MAX)
330 fprintf(f,
331 "%sBlockIOReadBandwidth=%s %s\n",
332 prefix,
333 b->path,
334 format_bytes(buf, sizeof(buf), b->rbps));
335 if (b->wbps != CGROUP_LIMIT_MAX)
336 fprintf(f,
337 "%sBlockIOWriteBandwidth=%s %s\n",
338 prefix,
339 b->path,
340 format_bytes(buf, sizeof(buf), b->wbps));
4ad49000 341 }
c21c9906
LP
342
343 LIST_FOREACH(items, iaai, c->ip_address_allow) {
344 _cleanup_free_ char *k = NULL;
345
346 (void) in_addr_to_string(iaai->family, &iaai->address, &k);
347 fprintf(f, "%sIPAddressAllow=%s/%u\n", prefix, strnull(k), iaai->prefixlen);
348 }
349
350 LIST_FOREACH(items, iaai, c->ip_address_deny) {
351 _cleanup_free_ char *k = NULL;
352
353 (void) in_addr_to_string(iaai->family, &iaai->address, &k);
354 fprintf(f, "%sIPAddressDeny=%s/%u\n", prefix, strnull(k), iaai->prefixlen);
355 }
4ad49000
LP
356}
357
fd870bac
YW
358int cgroup_add_device_allow(CGroupContext *c, const char *dev, const char *mode) {
359 _cleanup_free_ CGroupDeviceAllow *a = NULL;
360 _cleanup_free_ char *d = NULL;
361
362 assert(c);
363 assert(dev);
364 assert(isempty(mode) || in_charset(mode, "rwm"));
365
366 a = new(CGroupDeviceAllow, 1);
367 if (!a)
368 return -ENOMEM;
369
370 d = strdup(dev);
371 if (!d)
372 return -ENOMEM;
373
374 *a = (CGroupDeviceAllow) {
375 .path = TAKE_PTR(d),
490c5a37
LP
376 .r = isempty(mode) || strchr(mode, 'r'),
377 .w = isempty(mode) || strchr(mode, 'w'),
378 .m = isempty(mode) || strchr(mode, 'm'),
fd870bac
YW
379 };
380
381 LIST_PREPEND(device_allow, c->device_allow, a);
382 TAKE_PTR(a);
383
384 return 0;
385}
386
c52db42b
CD
387uint64_t unit_get_ancestor_memory_low(Unit *u) {
388 CGroupContext *c;
389
390 /* 1. Is MemoryLow set in this unit? If so, use that.
391 * 2. Is DefaultMemoryLow set in any ancestor? If so, use that.
392 * 3. Otherwise, return CGROUP_LIMIT_MIN. */
393
394 assert(u);
395
396 c = unit_get_cgroup_context(u);
397
398 if (c->memory_low_set)
399 return c->memory_low;
400
401 while (UNIT_ISSET(u->slice)) {
402 u = UNIT_DEREF(u->slice);
403 c = unit_get_cgroup_context(u);
404
405 if (c->default_memory_low_set)
406 return c->default_memory_low;
407 }
408
409 /* We've reached the root, but nobody had DefaultMemoryLow set, so set it to the kernel default. */
410 return CGROUP_LIMIT_MIN;
411}
412
0d2d6fbf
CD
413static void cgroup_xattr_apply(Unit *u) {
414 char ids[SD_ID128_STRING_MAX];
415 int r;
416
417 assert(u);
418
419 if (!MANAGER_IS_SYSTEM(u->manager))
420 return;
421
422 if (sd_id128_is_null(u->invocation_id))
423 return;
424
425 r = cg_set_xattr(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path,
426 "trusted.invocation_id",
427 sd_id128_to_string(u->invocation_id, ids), 32,
428 0);
429 if (r < 0)
430 log_unit_debug_errno(u, r, "Failed to set invocation ID on control group %s, ignoring: %m", u->cgroup_path);
431}
432
45c2e068 433static int lookup_block_device(const char *p, dev_t *ret) {
f5855697
YS
434 dev_t rdev, dev = 0;
435 mode_t mode;
45c2e068 436 int r;
4ad49000
LP
437
438 assert(p);
45c2e068 439 assert(ret);
4ad49000 440
f5855697 441 r = device_path_parse_major_minor(p, &mode, &rdev);
d5aecba6 442 if (r == -ENODEV) { /* not a parsable device node, need to go to disk */
f5855697 443 struct stat st;
d5aecba6
LP
444 if (stat(p, &st) < 0)
445 return log_warning_errno(errno, "Couldn't stat device '%s': %m", p);
f5855697
YS
446 rdev = (dev_t)st.st_rdev;
447 dev = (dev_t)st.st_dev;
448 mode = st.st_mode;
d5aecba6
LP
449 } else if (r < 0)
450 return log_warning_errno(r, "Failed to parse major/minor from path '%s': %m", p);
451
f5855697 452 if (S_ISCHR(mode)) {
d5aecba6
LP
453 log_warning("Device node '%s' is a character device, but block device needed.", p);
454 return -ENOTBLK;
f5855697
YS
455 } else if (S_ISBLK(mode))
456 *ret = rdev;
457 else if (major(dev) != 0)
458 *ret = dev; /* If this is not a device node then use the block device this file is stored on */
45c2e068
LP
459 else {
460 /* If this is btrfs, getting the backing block device is a bit harder */
461 r = btrfs_get_block_device(p, ret);
462 if (r < 0 && r != -ENOTTY)
463 return log_warning_errno(r, "Failed to determine block device backing btrfs file system '%s': %m", p);
464 if (r == -ENOTTY) {
465 log_warning("'%s' is not a block device node, and file system block device cannot be determined or is not local.", p);
466 return -ENODEV;
467 }
4ad49000 468 }
8e274523 469
45c2e068
LP
470 /* If this is a LUKS device, try to get the originating block device */
471 (void) block_get_originating(*ret, ret);
472
473 /* If this is a partition, try to get the originating block device */
474 (void) block_get_whole_disk(*ret, ret);
8e274523 475 return 0;
8e274523
LP
476}
477
084c7007 478static int whitelist_device(BPFProgram *prog, const char *path, const char *node, const char *acc) {
f5855697
YS
479 dev_t rdev;
480 mode_t mode;
8c6db833 481 int r;
8e274523 482
4ad49000
LP
483 assert(path);
484 assert(acc);
8e274523 485
74c48bf5
LP
486 /* Some special handling for /dev/block/%u:%u, /dev/char/%u:%u, /run/systemd/inaccessible/chr and
487 * /run/systemd/inaccessible/blk paths. Instead of stat()ing these we parse out the major/minor directly. This
488 * means clients can use these path without the device node actually around */
f5855697 489 r = device_path_parse_major_minor(node, &mode, &rdev);
74c48bf5
LP
490 if (r < 0) {
491 if (r != -ENODEV)
492 return log_warning_errno(r, "Couldn't parse major/minor from device path '%s': %m", node);
b200489b 493
f5855697 494 struct stat st;
74c48bf5
LP
495 if (stat(node, &st) < 0)
496 return log_warning_errno(errno, "Couldn't stat device %s: %m", node);
e7330dfe 497
74c48bf5
LP
498 if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
499 log_warning("%s is not a device.", node);
500 return -ENODEV;
501 }
f5855697
YS
502 rdev = (dev_t) st.st_rdev;
503 mode = st.st_mode;
4ad49000
LP
504 }
505
084c7007
RG
506 if (cg_all_unified() > 0) {
507 if (!prog)
508 return 0;
4ad49000 509
f5855697
YS
510 return cgroup_bpf_whitelist_device(prog, S_ISCHR(mode) ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK,
511 major(rdev), minor(rdev), acc);
b9839ac9 512
084c7007
RG
513 } else {
514 char buf[2+DECIMAL_STR_MAX(dev_t)*2+2+4];
515
516 sprintf(buf,
517 "%c %u:%u %s",
f5855697
YS
518 S_ISCHR(mode) ? 'c' : 'b',
519 major(rdev), minor(rdev),
084c7007
RG
520 acc);
521
8c838407
LP
522 /* Changing the devices list of a populated cgroup might result in EINVAL, hence ignore EINVAL here. */
523
084c7007
RG
524 r = cg_set_attribute("devices", path, "devices.allow", buf);
525 if (r < 0)
2c74e12b 526 return log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING,
b9839ac9 527 r, "Failed to set devices.allow on %s: %m", path);
4ad49000 528
b9839ac9
LP
529 return 0;
530 }
8e274523
LP
531}
532
084c7007 533static int whitelist_major(BPFProgram *prog, const char *path, const char *name, char type, const char *acc) {
90060676 534 _cleanup_fclose_ FILE *f = NULL;
8e8b5d2e 535 char buf[2+DECIMAL_STR_MAX(unsigned)+3+4];
90060676 536 bool good = false;
8e8b5d2e 537 unsigned maj;
90060676
LP
538 int r;
539
540 assert(path);
541 assert(acc);
4c701096 542 assert(IN_SET(type, 'b', 'c'));
90060676 543
8e8b5d2e
LP
544 if (streq(name, "*")) {
545 /* If the name is a wildcard, then apply this list to all devices of this type */
546
547 if (cg_all_unified() > 0) {
548 if (!prog)
549 return 0;
550
551 (void) cgroup_bpf_whitelist_class(prog, type == 'c' ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK, acc);
552 } else {
553 xsprintf(buf, "%c *:* %s", type, acc);
554
555 r = cg_set_attribute("devices", path, "devices.allow", buf);
556 if (r < 0)
557 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
558 "Failed to set devices.allow on %s: %m", path);
559 return 0;
560 }
561 }
562
563 if (safe_atou(name, &maj) >= 0 && DEVICE_MAJOR_VALID(maj)) {
564 /* The name is numeric and suitable as major. In that case, let's take is major, and create the entry
565 * directly */
566
567 if (cg_all_unified() > 0) {
568 if (!prog)
569 return 0;
570
571 (void) cgroup_bpf_whitelist_major(prog,
572 type == 'c' ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK,
573 maj, acc);
574 } else {
575 xsprintf(buf, "%c %u:* %s", type, maj, acc);
576
577 r = cg_set_attribute("devices", path, "devices.allow", buf);
578 if (r < 0)
579 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
580 "Failed to set devices.allow on %s: %m", path);
581 }
582
583 return 0;
584 }
585
90060676 586 f = fopen("/proc/devices", "re");
4a62c710
MS
587 if (!f)
588 return log_warning_errno(errno, "Cannot open /proc/devices to resolve %s (%c): %m", name, type);
90060676 589
c66e60a8
LP
590 for (;;) {
591 _cleanup_free_ char *line = NULL;
8e8b5d2e 592 char *w, *p;
90060676 593
c66e60a8
LP
594 r = read_line(f, LONG_LINE_MAX, &line);
595 if (r < 0)
596 return log_warning_errno(r, "Failed to read /proc/devices: %m");
597 if (r == 0)
598 break;
90060676
LP
599
600 if (type == 'c' && streq(line, "Character devices:")) {
601 good = true;
602 continue;
603 }
604
605 if (type == 'b' && streq(line, "Block devices:")) {
606 good = true;
607 continue;
608 }
609
610 if (isempty(line)) {
611 good = false;
612 continue;
613 }
614
615 if (!good)
616 continue;
617
618 p = strstrip(line);
619
620 w = strpbrk(p, WHITESPACE);
621 if (!w)
622 continue;
623 *w = 0;
624
625 r = safe_atou(p, &maj);
626 if (r < 0)
627 continue;
628 if (maj <= 0)
629 continue;
630
631 w++;
632 w += strspn(w, WHITESPACE);
e41969e3
LP
633
634 if (fnmatch(name, w, 0) != 0)
90060676
LP
635 continue;
636
084c7007
RG
637 if (cg_all_unified() > 0) {
638 if (!prog)
639 continue;
90060676 640
913c898c
LP
641 (void) cgroup_bpf_whitelist_major(prog,
642 type == 'c' ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK,
643 maj, acc);
084c7007 644 } else {
084c7007
RG
645 sprintf(buf,
646 "%c %u:* %s",
647 type,
648 maj,
649 acc);
650
8c838407
LP
651 /* Changing the devices list of a populated cgroup might result in EINVAL, hence ignore EINVAL
652 * here. */
653
084c7007
RG
654 r = cg_set_attribute("devices", path, "devices.allow", buf);
655 if (r < 0)
2c74e12b 656 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING,
084c7007
RG
657 r, "Failed to set devices.allow on %s: %m", path);
658 }
90060676
LP
659 }
660
661 return 0;
90060676
LP
662}
663
66ebf6c0
TH
664static bool cgroup_context_has_cpu_weight(CGroupContext *c) {
665 return c->cpu_weight != CGROUP_WEIGHT_INVALID ||
666 c->startup_cpu_weight != CGROUP_WEIGHT_INVALID;
667}
668
669static bool cgroup_context_has_cpu_shares(CGroupContext *c) {
670 return c->cpu_shares != CGROUP_CPU_SHARES_INVALID ||
671 c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID;
672}
673
674static uint64_t cgroup_context_cpu_weight(CGroupContext *c, ManagerState state) {
675 if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) &&
676 c->startup_cpu_weight != CGROUP_WEIGHT_INVALID)
677 return c->startup_cpu_weight;
678 else if (c->cpu_weight != CGROUP_WEIGHT_INVALID)
679 return c->cpu_weight;
680 else
681 return CGROUP_WEIGHT_DEFAULT;
682}
683
684static uint64_t cgroup_context_cpu_shares(CGroupContext *c, ManagerState state) {
685 if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) &&
686 c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID)
687 return c->startup_cpu_shares;
688 else if (c->cpu_shares != CGROUP_CPU_SHARES_INVALID)
689 return c->cpu_shares;
690 else
691 return CGROUP_CPU_SHARES_DEFAULT;
692}
693
10f28641
FB
694usec_t cgroup_cpu_adjust_period(usec_t period, usec_t quota, usec_t resolution, usec_t max_period) {
695 /* kernel uses a minimum resolution of 1ms, so both period and (quota * period)
696 * need to be higher than that boundary. quota is specified in USecPerSec.
697 * Additionally, period must be at most max_period. */
698 assert(quota > 0);
699
700 return MIN(MAX3(period, resolution, resolution * USEC_PER_SEC / quota), max_period);
701}
702
703static usec_t cgroup_cpu_adjust_period_and_log(Unit *u, usec_t period, usec_t quota) {
704 usec_t new_period;
705
706 if (quota == USEC_INFINITY)
707 /* Always use default period for infinity quota. */
708 return CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC;
709
710 if (period == USEC_INFINITY)
711 /* Default period was requested. */
712 period = CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC;
713
714 /* Clamp to interval [1ms, 1s] */
715 new_period = cgroup_cpu_adjust_period(period, quota, USEC_PER_MSEC, USEC_PER_SEC);
716
717 if (new_period != period) {
718 char v[FORMAT_TIMESPAN_MAX];
527ede0c 719 log_unit_full(u, u->warned_clamping_cpu_quota_period ? LOG_DEBUG : LOG_WARNING, 0,
10f28641
FB
720 "Clamping CPU interval for cpu.max: period is now %s",
721 format_timespan(v, sizeof(v), new_period, 1));
527ede0c 722 u->warned_clamping_cpu_quota_period = true;
10f28641
FB
723 }
724
725 return new_period;
726}
727
52fecf20
LP
728static void cgroup_apply_unified_cpu_weight(Unit *u, uint64_t weight) {
729 char buf[DECIMAL_STR_MAX(uint64_t) + 2];
66ebf6c0
TH
730
731 xsprintf(buf, "%" PRIu64 "\n", weight);
293d32df 732 (void) set_attribute_and_warn(u, "cpu", "cpu.weight", buf);
52fecf20
LP
733}
734
10f28641 735static void cgroup_apply_unified_cpu_quota(Unit *u, usec_t quota, usec_t period) {
52fecf20 736 char buf[(DECIMAL_STR_MAX(usec_t) + 1) * 2 + 1];
66ebf6c0 737
10f28641 738 period = cgroup_cpu_adjust_period_and_log(u, period, quota);
66ebf6c0
TH
739 if (quota != USEC_INFINITY)
740 xsprintf(buf, USEC_FMT " " USEC_FMT "\n",
10f28641 741 MAX(quota * period / USEC_PER_SEC, USEC_PER_MSEC), period);
66ebf6c0 742 else
10f28641 743 xsprintf(buf, "max " USEC_FMT "\n", period);
293d32df 744 (void) set_attribute_and_warn(u, "cpu", "cpu.max", buf);
66ebf6c0
TH
745}
746
52fecf20
LP
747static void cgroup_apply_legacy_cpu_shares(Unit *u, uint64_t shares) {
748 char buf[DECIMAL_STR_MAX(uint64_t) + 2];
66ebf6c0
TH
749
750 xsprintf(buf, "%" PRIu64 "\n", shares);
293d32df 751 (void) set_attribute_and_warn(u, "cpu", "cpu.shares", buf);
52fecf20
LP
752}
753
10f28641 754static void cgroup_apply_legacy_cpu_quota(Unit *u, usec_t quota, usec_t period) {
52fecf20 755 char buf[DECIMAL_STR_MAX(usec_t) + 2];
66ebf6c0 756
10f28641
FB
757 period = cgroup_cpu_adjust_period_and_log(u, period, quota);
758
759 xsprintf(buf, USEC_FMT "\n", period);
293d32df 760 (void) set_attribute_and_warn(u, "cpu", "cpu.cfs_period_us", buf);
66ebf6c0
TH
761
762 if (quota != USEC_INFINITY) {
10f28641 763 xsprintf(buf, USEC_FMT "\n", MAX(quota * period / USEC_PER_SEC, USEC_PER_MSEC));
293d32df 764 (void) set_attribute_and_warn(u, "cpu", "cpu.cfs_quota_us", buf);
66ebf6c0 765 } else
589a5f7a 766 (void) set_attribute_and_warn(u, "cpu", "cpu.cfs_quota_us", "-1\n");
66ebf6c0
TH
767}
768
769static uint64_t cgroup_cpu_shares_to_weight(uint64_t shares) {
770 return CLAMP(shares * CGROUP_WEIGHT_DEFAULT / CGROUP_CPU_SHARES_DEFAULT,
771 CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
772}
773
774static uint64_t cgroup_cpu_weight_to_shares(uint64_t weight) {
775 return CLAMP(weight * CGROUP_CPU_SHARES_DEFAULT / CGROUP_WEIGHT_DEFAULT,
776 CGROUP_CPU_SHARES_MIN, CGROUP_CPU_SHARES_MAX);
777}
778
508c45da 779static bool cgroup_context_has_io_config(CGroupContext *c) {
538b4852
TH
780 return c->io_accounting ||
781 c->io_weight != CGROUP_WEIGHT_INVALID ||
782 c->startup_io_weight != CGROUP_WEIGHT_INVALID ||
783 c->io_device_weights ||
6ae4283c 784 c->io_device_latencies ||
538b4852
TH
785 c->io_device_limits;
786}
787
508c45da 788static bool cgroup_context_has_blockio_config(CGroupContext *c) {
538b4852
TH
789 return c->blockio_accounting ||
790 c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID ||
791 c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID ||
792 c->blockio_device_weights ||
793 c->blockio_device_bandwidths;
794}
795
508c45da 796static uint64_t cgroup_context_io_weight(CGroupContext *c, ManagerState state) {
64faf04c
TH
797 if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) &&
798 c->startup_io_weight != CGROUP_WEIGHT_INVALID)
799 return c->startup_io_weight;
800 else if (c->io_weight != CGROUP_WEIGHT_INVALID)
801 return c->io_weight;
802 else
803 return CGROUP_WEIGHT_DEFAULT;
804}
805
508c45da 806static uint64_t cgroup_context_blkio_weight(CGroupContext *c, ManagerState state) {
64faf04c
TH
807 if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) &&
808 c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID)
809 return c->startup_blockio_weight;
810 else if (c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID)
811 return c->blockio_weight;
812 else
813 return CGROUP_BLKIO_WEIGHT_DEFAULT;
814}
815
508c45da 816static uint64_t cgroup_weight_blkio_to_io(uint64_t blkio_weight) {
538b4852
TH
817 return CLAMP(blkio_weight * CGROUP_WEIGHT_DEFAULT / CGROUP_BLKIO_WEIGHT_DEFAULT,
818 CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
819}
820
508c45da 821static uint64_t cgroup_weight_io_to_blkio(uint64_t io_weight) {
538b4852
TH
822 return CLAMP(io_weight * CGROUP_BLKIO_WEIGHT_DEFAULT / CGROUP_WEIGHT_DEFAULT,
823 CGROUP_BLKIO_WEIGHT_MIN, CGROUP_BLKIO_WEIGHT_MAX);
824}
825
f29ff115 826static void cgroup_apply_io_device_weight(Unit *u, const char *dev_path, uint64_t io_weight) {
64faf04c
TH
827 char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1];
828 dev_t dev;
829 int r;
830
831 r = lookup_block_device(dev_path, &dev);
832 if (r < 0)
833 return;
834
835 xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), io_weight);
293d32df 836 (void) set_attribute_and_warn(u, "io", "io.weight", buf);
64faf04c
TH
837}
838
f29ff115 839static void cgroup_apply_blkio_device_weight(Unit *u, const char *dev_path, uint64_t blkio_weight) {
64faf04c
TH
840 char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1];
841 dev_t dev;
842 int r;
843
844 r = lookup_block_device(dev_path, &dev);
845 if (r < 0)
846 return;
847
848 xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), blkio_weight);
293d32df 849 (void) set_attribute_and_warn(u, "blkio", "blkio.weight_device", buf);
64faf04c
TH
850}
851
6ae4283c
TH
852static void cgroup_apply_io_device_latency(Unit *u, const char *dev_path, usec_t target) {
853 char buf[DECIMAL_STR_MAX(dev_t)*2+2+7+DECIMAL_STR_MAX(uint64_t)+1];
854 dev_t dev;
855 int r;
856
857 r = lookup_block_device(dev_path, &dev);
858 if (r < 0)
859 return;
860
861 if (target != USEC_INFINITY)
862 xsprintf(buf, "%u:%u target=%" PRIu64 "\n", major(dev), minor(dev), target);
863 else
864 xsprintf(buf, "%u:%u target=max\n", major(dev), minor(dev));
865
293d32df 866 (void) set_attribute_and_warn(u, "io", "io.latency", buf);
6ae4283c
TH
867}
868
17ae2780 869static void cgroup_apply_io_device_limit(Unit *u, const char *dev_path, uint64_t *limits) {
64faf04c
TH
870 char limit_bufs[_CGROUP_IO_LIMIT_TYPE_MAX][DECIMAL_STR_MAX(uint64_t)];
871 char buf[DECIMAL_STR_MAX(dev_t)*2+2+(6+DECIMAL_STR_MAX(uint64_t)+1)*4];
872 CGroupIOLimitType type;
873 dev_t dev;
64faf04c
TH
874 int r;
875
876 r = lookup_block_device(dev_path, &dev);
877 if (r < 0)
17ae2780 878 return;
64faf04c 879
17ae2780
LP
880 for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
881 if (limits[type] != cgroup_io_limit_defaults[type])
64faf04c 882 xsprintf(limit_bufs[type], "%" PRIu64, limits[type]);
17ae2780 883 else
64faf04c 884 xsprintf(limit_bufs[type], "%s", limits[type] == CGROUP_LIMIT_MAX ? "max" : "0");
64faf04c
TH
885
886 xsprintf(buf, "%u:%u rbps=%s wbps=%s riops=%s wiops=%s\n", major(dev), minor(dev),
887 limit_bufs[CGROUP_IO_RBPS_MAX], limit_bufs[CGROUP_IO_WBPS_MAX],
888 limit_bufs[CGROUP_IO_RIOPS_MAX], limit_bufs[CGROUP_IO_WIOPS_MAX]);
293d32df 889 (void) set_attribute_and_warn(u, "io", "io.max", buf);
64faf04c
TH
890}
891
17ae2780 892static void cgroup_apply_blkio_device_limit(Unit *u, const char *dev_path, uint64_t rbps, uint64_t wbps) {
64faf04c
TH
893 char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1];
894 dev_t dev;
64faf04c
TH
895 int r;
896
897 r = lookup_block_device(dev_path, &dev);
898 if (r < 0)
17ae2780 899 return;
64faf04c 900
64faf04c 901 sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), rbps);
293d32df 902 (void) set_attribute_and_warn(u, "blkio", "blkio.throttle.read_bps_device", buf);
64faf04c 903
64faf04c 904 sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), wbps);
293d32df 905 (void) set_attribute_and_warn(u, "blkio", "blkio.throttle.write_bps_device", buf);
64faf04c
TH
906}
907
c52db42b
CD
908static bool unit_has_unified_memory_config(Unit *u) {
909 CGroupContext *c;
910
911 assert(u);
912
913 c = unit_get_cgroup_context(u);
914 assert(c);
915
916 return c->memory_min > 0 || unit_get_ancestor_memory_low(u) > 0 ||
917 c->memory_high != CGROUP_LIMIT_MAX || c->memory_max != CGROUP_LIMIT_MAX ||
918 c->memory_swap_max != CGROUP_LIMIT_MAX;
da4d897e
TH
919}
920
f29ff115 921static void cgroup_apply_unified_memory_limit(Unit *u, const char *file, uint64_t v) {
589a5f7a 922 char buf[DECIMAL_STR_MAX(uint64_t) + 1] = "max\n";
da4d897e
TH
923
924 if (v != CGROUP_LIMIT_MAX)
925 xsprintf(buf, "%" PRIu64 "\n", v);
926
293d32df 927 (void) set_attribute_and_warn(u, "memory", file, buf);
da4d897e
TH
928}
929
0f2d84d2 930static void cgroup_apply_firewall(Unit *u) {
0f2d84d2
LP
931 assert(u);
932
acf7f253 933 /* Best-effort: let's apply IP firewalling and/or accounting if that's enabled */
906c06f6 934
acf7f253 935 if (bpf_firewall_compile(u) < 0)
906c06f6
DM
936 return;
937
938 (void) bpf_firewall_install(u);
906c06f6
DM
939}
940
941static void cgroup_context_apply(
942 Unit *u,
943 CGroupMask apply_mask,
906c06f6
DM
944 ManagerState state) {
945
f29ff115
TH
946 const char *path;
947 CGroupContext *c;
52fecf20 948 bool is_host_root, is_local_root;
4ad49000
LP
949 int r;
950
f29ff115
TH
951 assert(u);
952
906c06f6 953 /* Nothing to do? Exit early! */
17f14955 954 if (apply_mask == 0)
4ad49000 955 return;
8e274523 956
52fecf20
LP
957 /* Some cgroup attributes are not supported on the host root cgroup, hence silently ignore them here. And other
958 * attributes should only be managed for cgroups further down the tree. */
959 is_local_root = unit_has_name(u, SPECIAL_ROOT_SLICE);
960 is_host_root = unit_has_host_root_cgroup(u);
f3725e64
LP
961
962 assert_se(c = unit_get_cgroup_context(u));
963 assert_se(path = u->cgroup_path);
964
52fecf20 965 if (is_local_root) /* Make sure we don't try to display messages with an empty path. */
6da13913 966 path = "/";
01efdf13 967
be2c0327
LP
968 /* We generally ignore errors caused by read-only mounted cgroup trees (assuming we are running in a container
969 * then), and missing cgroups, i.e. EROFS and ENOENT. */
714e2e1d 970
be2c0327
LP
971 /* In fully unified mode these attributes don't exist on the host cgroup root. On legacy the weights exist, but
972 * setting the weight makes very little sense on the host root cgroup, as there are no other cgroups at this
973 * level. The quota exists there too, but any attempt to write to it is refused with EINVAL. Inside of
4e1dfa45 974 * containers we want to leave control of these to the container manager (and if cgroup v2 delegation is used
be2c0327
LP
975 * we couldn't even write to them if we wanted to). */
976 if ((apply_mask & CGROUP_MASK_CPU) && !is_local_root) {
8e274523 977
b4cccbc1 978 if (cg_all_unified() > 0) {
be2c0327 979 uint64_t weight;
b2f8b02e 980
be2c0327
LP
981 if (cgroup_context_has_cpu_weight(c))
982 weight = cgroup_context_cpu_weight(c, state);
983 else if (cgroup_context_has_cpu_shares(c)) {
984 uint64_t shares;
66ebf6c0 985
be2c0327
LP
986 shares = cgroup_context_cpu_shares(c, state);
987 weight = cgroup_cpu_shares_to_weight(shares);
66ebf6c0 988
be2c0327
LP
989 log_cgroup_compat(u, "Applying [Startup]CPUShares=%" PRIu64 " as [Startup]CPUWeight=%" PRIu64 " on %s",
990 shares, weight, path);
991 } else
992 weight = CGROUP_WEIGHT_DEFAULT;
66ebf6c0 993
be2c0327 994 cgroup_apply_unified_cpu_weight(u, weight);
10f28641 995 cgroup_apply_unified_cpu_quota(u, c->cpu_quota_per_sec_usec, c->cpu_quota_period_usec);
66ebf6c0 996
52fecf20 997 } else {
be2c0327 998 uint64_t shares;
52fecf20 999
be2c0327
LP
1000 if (cgroup_context_has_cpu_weight(c)) {
1001 uint64_t weight;
52fecf20 1002
be2c0327
LP
1003 weight = cgroup_context_cpu_weight(c, state);
1004 shares = cgroup_cpu_weight_to_shares(weight);
52fecf20 1005
be2c0327
LP
1006 log_cgroup_compat(u, "Applying [Startup]CPUWeight=%" PRIu64 " as [Startup]CPUShares=%" PRIu64 " on %s",
1007 weight, shares, path);
1008 } else if (cgroup_context_has_cpu_shares(c))
1009 shares = cgroup_context_cpu_shares(c, state);
1010 else
1011 shares = CGROUP_CPU_SHARES_DEFAULT;
66ebf6c0 1012
be2c0327 1013 cgroup_apply_legacy_cpu_shares(u, shares);
10f28641 1014 cgroup_apply_legacy_cpu_quota(u, c->cpu_quota_per_sec_usec, c->cpu_quota_period_usec);
66ebf6c0 1015 }
4ad49000
LP
1016 }
1017
4e1dfa45 1018 /* The 'io' controller attributes are not exported on the host's root cgroup (being a pure cgroup v2
52fecf20
LP
1019 * controller), and in case of containers we want to leave control of these attributes to the container manager
1020 * (and we couldn't access that stuff anyway, even if we tried if proper delegation is used). */
1021 if ((apply_mask & CGROUP_MASK_IO) && !is_local_root) {
1022 char buf[8+DECIMAL_STR_MAX(uint64_t)+1];
1023 bool has_io, has_blockio;
1024 uint64_t weight;
13c31542 1025
52fecf20
LP
1026 has_io = cgroup_context_has_io_config(c);
1027 has_blockio = cgroup_context_has_blockio_config(c);
13c31542 1028
52fecf20
LP
1029 if (has_io)
1030 weight = cgroup_context_io_weight(c, state);
1031 else if (has_blockio) {
1032 uint64_t blkio_weight;
128fadc9 1033
52fecf20
LP
1034 blkio_weight = cgroup_context_blkio_weight(c, state);
1035 weight = cgroup_weight_blkio_to_io(blkio_weight);
128fadc9 1036
67e2ea15 1037 log_cgroup_compat(u, "Applying [Startup]BlockIOWeight=%" PRIu64 " as [Startup]IOWeight=%" PRIu64,
52fecf20
LP
1038 blkio_weight, weight);
1039 } else
1040 weight = CGROUP_WEIGHT_DEFAULT;
13c31542 1041
52fecf20
LP
1042 xsprintf(buf, "default %" PRIu64 "\n", weight);
1043 (void) set_attribute_and_warn(u, "io", "io.weight", buf);
538b4852 1044
52fecf20
LP
1045 if (has_io) {
1046 CGroupIODeviceLatency *latency;
1047 CGroupIODeviceLimit *limit;
1048 CGroupIODeviceWeight *w;
128fadc9 1049
52fecf20
LP
1050 LIST_FOREACH(device_weights, w, c->io_device_weights)
1051 cgroup_apply_io_device_weight(u, w->path, w->weight);
128fadc9 1052
52fecf20
LP
1053 LIST_FOREACH(device_limits, limit, c->io_device_limits)
1054 cgroup_apply_io_device_limit(u, limit->path, limit->limits);
6ae4283c 1055
52fecf20
LP
1056 LIST_FOREACH(device_latencies, latency, c->io_device_latencies)
1057 cgroup_apply_io_device_latency(u, latency->path, latency->target_usec);
6ae4283c 1058
52fecf20
LP
1059 } else if (has_blockio) {
1060 CGroupBlockIODeviceWeight *w;
1061 CGroupBlockIODeviceBandwidth *b;
13c31542 1062
52fecf20
LP
1063 LIST_FOREACH(device_weights, w, c->blockio_device_weights) {
1064 weight = cgroup_weight_blkio_to_io(w->weight);
17ae2780 1065
67e2ea15 1066 log_cgroup_compat(u, "Applying BlockIODeviceWeight=%" PRIu64 " as IODeviceWeight=%" PRIu64 " for %s",
52fecf20 1067 w->weight, weight, w->path);
538b4852 1068
52fecf20
LP
1069 cgroup_apply_io_device_weight(u, w->path, weight);
1070 }
538b4852 1071
17ae2780 1072 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
538b4852
TH
1073 uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX];
1074 CGroupIOLimitType type;
1075
1076 for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
1077 limits[type] = cgroup_io_limit_defaults[type];
1078
1079 limits[CGROUP_IO_RBPS_MAX] = b->rbps;
1080 limits[CGROUP_IO_WBPS_MAX] = b->wbps;
1081
67e2ea15 1082 log_cgroup_compat(u, "Applying BlockIO{Read|Write}Bandwidth=%" PRIu64 " %" PRIu64 " as IO{Read|Write}BandwidthMax= for %s",
128fadc9
TH
1083 b->rbps, b->wbps, b->path);
1084
17ae2780 1085 cgroup_apply_io_device_limit(u, b->path, limits);
538b4852 1086 }
13c31542
TH
1087 }
1088 }
1089
906c06f6 1090 if (apply_mask & CGROUP_MASK_BLKIO) {
52fecf20 1091 bool has_io, has_blockio;
4ad49000 1092
52fecf20
LP
1093 has_io = cgroup_context_has_io_config(c);
1094 has_blockio = cgroup_context_has_blockio_config(c);
1095
1096 /* Applying a 'weight' never makes sense for the host root cgroup, and for containers this should be
1097 * left to our container manager, too. */
1098 if (!is_local_root) {
64faf04c
TH
1099 char buf[DECIMAL_STR_MAX(uint64_t)+1];
1100 uint64_t weight;
64faf04c 1101
7d862ab8 1102 if (has_io) {
52fecf20 1103 uint64_t io_weight;
128fadc9 1104
52fecf20 1105 io_weight = cgroup_context_io_weight(c, state);
538b4852 1106 weight = cgroup_weight_io_to_blkio(cgroup_context_io_weight(c, state));
128fadc9 1107
67e2ea15 1108 log_cgroup_compat(u, "Applying [Startup]IOWeight=%" PRIu64 " as [Startup]BlockIOWeight=%" PRIu64,
128fadc9 1109 io_weight, weight);
7d862ab8
TH
1110 } else if (has_blockio)
1111 weight = cgroup_context_blkio_weight(c, state);
1112 else
538b4852 1113 weight = CGROUP_BLKIO_WEIGHT_DEFAULT;
64faf04c
TH
1114
1115 xsprintf(buf, "%" PRIu64 "\n", weight);
293d32df 1116 (void) set_attribute_and_warn(u, "blkio", "blkio.weight", buf);
4ad49000 1117
7d862ab8 1118 if (has_io) {
538b4852
TH
1119 CGroupIODeviceWeight *w;
1120
128fadc9
TH
1121 LIST_FOREACH(device_weights, w, c->io_device_weights) {
1122 weight = cgroup_weight_io_to_blkio(w->weight);
1123
67e2ea15 1124 log_cgroup_compat(u, "Applying IODeviceWeight=%" PRIu64 " as BlockIODeviceWeight=%" PRIu64 " for %s",
128fadc9
TH
1125 w->weight, weight, w->path);
1126
1127 cgroup_apply_blkio_device_weight(u, w->path, weight);
1128 }
7d862ab8
TH
1129 } else if (has_blockio) {
1130 CGroupBlockIODeviceWeight *w;
1131
7d862ab8
TH
1132 LIST_FOREACH(device_weights, w, c->blockio_device_weights)
1133 cgroup_apply_blkio_device_weight(u, w->path, w->weight);
538b4852 1134 }
4ad49000
LP
1135 }
1136
52fecf20
LP
1137 /* The bandwith limits are something that make sense to be applied to the host's root but not container
1138 * roots, as there we want the container manager to handle it */
1139 if (is_host_root || !is_local_root) {
1140 if (has_io) {
1141 CGroupIODeviceLimit *l;
538b4852 1142
52fecf20 1143 LIST_FOREACH(device_limits, l, c->io_device_limits) {
67e2ea15 1144 log_cgroup_compat(u, "Applying IO{Read|Write}Bandwidth=%" PRIu64 " %" PRIu64 " as BlockIO{Read|Write}BandwidthMax= for %s",
52fecf20 1145 l->limits[CGROUP_IO_RBPS_MAX], l->limits[CGROUP_IO_WBPS_MAX], l->path);
128fadc9 1146
52fecf20
LP
1147 cgroup_apply_blkio_device_limit(u, l->path, l->limits[CGROUP_IO_RBPS_MAX], l->limits[CGROUP_IO_WBPS_MAX]);
1148 }
1149 } else if (has_blockio) {
1150 CGroupBlockIODeviceBandwidth *b;
7d862ab8 1151
52fecf20
LP
1152 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths)
1153 cgroup_apply_blkio_device_limit(u, b->path, b->rbps, b->wbps);
1154 }
d686d8a9 1155 }
8e274523
LP
1156 }
1157
be2c0327
LP
1158 /* In unified mode 'memory' attributes do not exist on the root cgroup. In legacy mode 'memory.limit_in_bytes'
1159 * exists on the root cgroup, but any writes to it are refused with EINVAL. And if we run in a container we
4e1dfa45 1160 * want to leave control to the container manager (and if proper cgroup v2 delegation is used we couldn't even
be2c0327
LP
1161 * write to this if we wanted to.) */
1162 if ((apply_mask & CGROUP_MASK_MEMORY) && !is_local_root) {
efdb0237 1163
52fecf20 1164 if (cg_all_unified() > 0) {
be2c0327
LP
1165 uint64_t max, swap_max = CGROUP_LIMIT_MAX;
1166
c52db42b 1167 if (unit_has_unified_memory_config(u)) {
be2c0327
LP
1168 max = c->memory_max;
1169 swap_max = c->memory_swap_max;
1170 } else {
1171 max = c->memory_limit;
efdb0237 1172
be2c0327
LP
1173 if (max != CGROUP_LIMIT_MAX)
1174 log_cgroup_compat(u, "Applying MemoryLimit=%" PRIu64 " as MemoryMax=", max);
128fadc9 1175 }
da4d897e 1176
be2c0327 1177 cgroup_apply_unified_memory_limit(u, "memory.min", c->memory_min);
c52db42b 1178 cgroup_apply_unified_memory_limit(u, "memory.low", unit_get_ancestor_memory_low(u));
be2c0327
LP
1179 cgroup_apply_unified_memory_limit(u, "memory.high", c->memory_high);
1180 cgroup_apply_unified_memory_limit(u, "memory.max", max);
1181 cgroup_apply_unified_memory_limit(u, "memory.swap.max", swap_max);
128fadc9 1182
afcfaa69
LP
1183 (void) set_attribute_and_warn(u, "memory", "memory.oom.group", one_zero(c->memory_oom_group));
1184
be2c0327
LP
1185 } else {
1186 char buf[DECIMAL_STR_MAX(uint64_t) + 1];
1187 uint64_t val;
52fecf20 1188
c52db42b 1189 if (unit_has_unified_memory_config(u)) {
be2c0327
LP
1190 val = c->memory_max;
1191 log_cgroup_compat(u, "Applying MemoryMax=%" PRIi64 " as MemoryLimit=", val);
1192 } else
1193 val = c->memory_limit;
78a4ee59 1194
be2c0327
LP
1195 if (val == CGROUP_LIMIT_MAX)
1196 strncpy(buf, "-1\n", sizeof(buf));
1197 else
1198 xsprintf(buf, "%" PRIu64 "\n", val);
1199
1200 (void) set_attribute_and_warn(u, "memory", "memory.limit_in_bytes", buf);
da4d897e 1201 }
4ad49000 1202 }
8e274523 1203
4e1dfa45 1204 /* On cgroup v2 we can apply BPF everywhere. On cgroup v1 we apply it everywhere except for the root of
52fecf20
LP
1205 * containers, where we leave this to the manager */
1206 if ((apply_mask & (CGROUP_MASK_DEVICES | CGROUP_MASK_BPF_DEVICES)) &&
1207 (is_host_root || cg_all_unified() > 0 || !is_local_root)) {
084c7007 1208 _cleanup_(bpf_program_unrefp) BPFProgram *prog = NULL;
4ad49000 1209 CGroupDeviceAllow *a;
8e274523 1210
084c7007
RG
1211 if (cg_all_unified() > 0) {
1212 r = cgroup_init_device_bpf(&prog, c->device_policy, c->device_allow);
1213 if (r < 0)
1214 log_unit_warning_errno(u, r, "Failed to initialize device control bpf program: %m");
1215 } else {
8c838407 1216 /* Changing the devices list of a populated cgroup might result in EINVAL, hence ignore EINVAL
084c7007 1217 * here. */
714e2e1d 1218
084c7007
RG
1219 if (c->device_allow || c->device_policy != CGROUP_AUTO)
1220 r = cg_set_attribute("devices", path, "devices.deny", "a");
1221 else
1222 r = cg_set_attribute("devices", path, "devices.allow", "a");
1223 if (r < 0)
2c74e12b
LP
1224 log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r,
1225 "Failed to reset devices.allow/devices.deny: %m");
084c7007 1226 }
fb385181 1227
4ad49000
LP
1228 if (c->device_policy == CGROUP_CLOSED ||
1229 (c->device_policy == CGROUP_AUTO && c->device_allow)) {
1230 static const char auto_devices[] =
7d711efb
LP
1231 "/dev/null\0" "rwm\0"
1232 "/dev/zero\0" "rwm\0"
1233 "/dev/full\0" "rwm\0"
1234 "/dev/random\0" "rwm\0"
1235 "/dev/urandom\0" "rwm\0"
1236 "/dev/tty\0" "rwm\0"
5a7f87a9 1237 "/dev/ptmx\0" "rwm\0"
0d9e7991 1238 /* Allow /run/systemd/inaccessible/{chr,blk} devices for mapping InaccessiblePaths */
74c48bf5
LP
1239 "/run/systemd/inaccessible/chr\0" "rwm\0"
1240 "/run/systemd/inaccessible/blk\0" "rwm\0";
4ad49000
LP
1241
1242 const char *x, *y;
1243
1244 NULSTR_FOREACH_PAIR(x, y, auto_devices)
913c898c 1245 (void) whitelist_device(prog, path, x, y);
7d711efb 1246
5a7f87a9 1247 /* PTS (/dev/pts) devices may not be duplicated, but accessed */
913c898c 1248 (void) whitelist_major(prog, path, "pts", 'c', "rw");
4ad49000
LP
1249 }
1250
1251 LIST_FOREACH(device_allow, a, c->device_allow) {
fb4650aa 1252 char acc[4], *val;
4ad49000
LP
1253 unsigned k = 0;
1254
1255 if (a->r)
1256 acc[k++] = 'r';
1257 if (a->w)
1258 acc[k++] = 'w';
1259 if (a->m)
1260 acc[k++] = 'm';
fb385181 1261
4ad49000
LP
1262 if (k == 0)
1263 continue;
fb385181 1264
4ad49000 1265 acc[k++] = 0;
90060676 1266
27458ed6 1267 if (path_startswith(a->path, "/dev/"))
913c898c 1268 (void) whitelist_device(prog, path, a->path, acc);
fb4650aa 1269 else if ((val = startswith(a->path, "block-")))
913c898c 1270 (void) whitelist_major(prog, path, val, 'b', acc);
fb4650aa 1271 else if ((val = startswith(a->path, "char-")))
913c898c 1272 (void) whitelist_major(prog, path, val, 'c', acc);
90060676 1273 else
8e8b5d2e 1274 log_unit_debug(u, "Ignoring device '%s' while writing cgroup attribute.", a->path);
4ad49000 1275 }
084c7007
RG
1276
1277 r = cgroup_apply_device_bpf(u, prog, c->device_policy, c->device_allow);
1278 if (r < 0) {
1279 static bool warned = false;
1280
1281 log_full_errno(warned ? LOG_DEBUG : LOG_WARNING, r,
1282 "Unit %s configures device ACL, but the local system doesn't seem to support the BPF-based device controller.\n"
1283 "Proceeding WITHOUT applying ACL (all devices will be accessible)!\n"
1284 "(This warning is only shown for the first loaded unit using device ACL.)", u->id);
1285
1286 warned = true;
1287 }
4ad49000 1288 }
03a7b521 1289
00b5974f
LP
1290 if (apply_mask & CGROUP_MASK_PIDS) {
1291
52fecf20 1292 if (is_host_root) {
00b5974f
LP
1293 /* So, the "pids" controller does not expose anything on the root cgroup, in order not to
1294 * replicate knobs exposed elsewhere needlessly. We abstract this away here however, and when
1295 * the knobs of the root cgroup are modified propagate this to the relevant sysctls. There's a
1296 * non-obvious asymmetry however: unlike the cgroup properties we don't really want to take
1297 * exclusive ownership of the sysctls, but we still want to honour things if the user sets
1298 * limits. Hence we employ sort of a one-way strategy: when the user sets a bounded limit
1299 * through us it counts. When the user afterwards unsets it again (i.e. sets it to unbounded)
1300 * it also counts. But if the user never set a limit through us (i.e. we are the default of
1301 * "unbounded") we leave things unmodified. For this we manage a global boolean that we turn on
1302 * the first time we set a limit. Note that this boolean is flushed out on manager reload,
1303 * which is desirable so that there's an offical way to release control of the sysctl from
1304 * systemd: set the limit to unbounded and reload. */
1305
1306 if (c->tasks_max != CGROUP_LIMIT_MAX) {
1307 u->manager->sysctl_pid_max_changed = true;
1308 r = procfs_tasks_set_limit(c->tasks_max);
1309 } else if (u->manager->sysctl_pid_max_changed)
1310 r = procfs_tasks_set_limit(TASKS_MAX);
1311 else
1312 r = 0;
00b5974f 1313 if (r < 0)
39b9fefb 1314 log_unit_full(u, LOG_LEVEL_CGROUP_WRITE(r), r,
00b5974f 1315 "Failed to write to tasks limit sysctls: %m");
52fecf20 1316 }
03a7b521 1317
52fecf20
LP
1318 /* The attribute itself is not available on the host root cgroup, and in the container case we want to
1319 * leave it for the container manager. */
1320 if (!is_local_root) {
00b5974f
LP
1321 if (c->tasks_max != CGROUP_LIMIT_MAX) {
1322 char buf[DECIMAL_STR_MAX(uint64_t) + 2];
03a7b521 1323
00b5974f 1324 sprintf(buf, "%" PRIu64 "\n", c->tasks_max);
293d32df 1325 (void) set_attribute_and_warn(u, "pids", "pids.max", buf);
00b5974f 1326 } else
589a5f7a 1327 (void) set_attribute_and_warn(u, "pids", "pids.max", "max\n");
00b5974f 1328 }
03a7b521 1329 }
906c06f6 1330
17f14955 1331 if (apply_mask & CGROUP_MASK_BPF_FIREWALL)
0f2d84d2 1332 cgroup_apply_firewall(u);
fb385181
LP
1333}
1334
16492445
LP
1335static bool unit_get_needs_bpf_firewall(Unit *u) {
1336 CGroupContext *c;
1337 Unit *p;
1338 assert(u);
1339
1340 c = unit_get_cgroup_context(u);
1341 if (!c)
1342 return false;
1343
1344 if (c->ip_accounting ||
1345 c->ip_address_allow ||
1346 c->ip_address_deny)
1347 return true;
1348
1349 /* If any parent slice has an IP access list defined, it applies too */
1350 for (p = UNIT_DEREF(u->slice); p; p = UNIT_DEREF(p->slice)) {
1351 c = unit_get_cgroup_context(p);
1352 if (!c)
1353 return false;
1354
1355 if (c->ip_address_allow ||
1356 c->ip_address_deny)
1357 return true;
1358 }
1359
1360 return false;
1361}
1362
c52db42b 1363static CGroupMask unit_get_cgroup_mask(Unit *u) {
efdb0237 1364 CGroupMask mask = 0;
c52db42b
CD
1365 CGroupContext *c;
1366
1367 assert(u);
1368
1369 c = unit_get_cgroup_context(u);
8e274523 1370
fae9bc29 1371 /* Figure out which controllers we need, based on the cgroup context object */
8e274523 1372
fae9bc29 1373 if (c->cpu_accounting)
f98c2585 1374 mask |= get_cpu_accounting_mask();
fae9bc29
LP
1375
1376 if (cgroup_context_has_cpu_weight(c) ||
66ebf6c0 1377 cgroup_context_has_cpu_shares(c) ||
3a43da28 1378 c->cpu_quota_per_sec_usec != USEC_INFINITY)
fae9bc29 1379 mask |= CGROUP_MASK_CPU;
ecedd90f 1380
538b4852
TH
1381 if (cgroup_context_has_io_config(c) || cgroup_context_has_blockio_config(c))
1382 mask |= CGROUP_MASK_IO | CGROUP_MASK_BLKIO;
ecedd90f 1383
4ad49000 1384 if (c->memory_accounting ||
da4d897e 1385 c->memory_limit != CGROUP_LIMIT_MAX ||
c52db42b 1386 unit_has_unified_memory_config(u))
efdb0237 1387 mask |= CGROUP_MASK_MEMORY;
8e274523 1388
a931ad47
LP
1389 if (c->device_allow ||
1390 c->device_policy != CGROUP_AUTO)
084c7007 1391 mask |= CGROUP_MASK_DEVICES | CGROUP_MASK_BPF_DEVICES;
4ad49000 1392
03a7b521 1393 if (c->tasks_accounting ||
8793fa25 1394 c->tasks_max != CGROUP_LIMIT_MAX)
03a7b521
LP
1395 mask |= CGROUP_MASK_PIDS;
1396
fae9bc29 1397 return CGROUP_MASK_EXTEND_JOINED(mask);
8e274523
LP
1398}
1399
53aea74a 1400static CGroupMask unit_get_bpf_mask(Unit *u) {
17f14955
RG
1401 CGroupMask mask = 0;
1402
fae9bc29
LP
1403 /* Figure out which controllers we need, based on the cgroup context, possibly taking into account children
1404 * too. */
1405
17f14955
RG
1406 if (unit_get_needs_bpf_firewall(u))
1407 mask |= CGROUP_MASK_BPF_FIREWALL;
1408
1409 return mask;
1410}
1411
efdb0237 1412CGroupMask unit_get_own_mask(Unit *u) {
4ad49000 1413 CGroupContext *c;
8e274523 1414
442ce775
LP
1415 /* Returns the mask of controllers the unit needs for itself. If a unit is not properly loaded, return an empty
1416 * mask, as we shouldn't reflect it in the cgroup hierarchy then. */
1417
1418 if (u->load_state != UNIT_LOADED)
1419 return 0;
efdb0237 1420
4ad49000
LP
1421 c = unit_get_cgroup_context(u);
1422 if (!c)
1423 return 0;
8e274523 1424
c52db42b 1425 return (unit_get_cgroup_mask(u) | unit_get_bpf_mask(u) | unit_get_delegate_mask(u)) & ~unit_get_ancestor_disable_mask(u);
02638280
LP
1426}
1427
1428CGroupMask unit_get_delegate_mask(Unit *u) {
1429 CGroupContext *c;
1430
1431 /* If delegation is turned on, then turn on selected controllers, unless we are on the legacy hierarchy and the
1432 * process we fork into is known to drop privileges, and hence shouldn't get access to the controllers.
19af675e 1433 *
02638280 1434 * Note that on the unified hierarchy it is safe to delegate controllers to unprivileged services. */
a931ad47 1435
1d9cc876 1436 if (!unit_cgroup_delegate(u))
02638280
LP
1437 return 0;
1438
1439 if (cg_all_unified() <= 0) {
a931ad47
LP
1440 ExecContext *e;
1441
1442 e = unit_get_exec_context(u);
02638280
LP
1443 if (e && !exec_context_maintains_privileges(e))
1444 return 0;
a931ad47
LP
1445 }
1446
1d9cc876 1447 assert_se(c = unit_get_cgroup_context(u));
fae9bc29 1448 return CGROUP_MASK_EXTEND_JOINED(c->delegate_controllers);
8e274523
LP
1449}
1450
efdb0237 1451CGroupMask unit_get_members_mask(Unit *u) {
4ad49000 1452 assert(u);
bc432dc7 1453
02638280 1454 /* Returns the mask of controllers all of the unit's children require, merged */
efdb0237 1455
bc432dc7 1456 if (u->cgroup_members_mask_valid)
26a17ca2 1457 return u->cgroup_members_mask; /* Use cached value if possible */
bc432dc7 1458
64e844e5 1459 u->cgroup_members_mask = 0;
bc432dc7
LP
1460
1461 if (u->type == UNIT_SLICE) {
eef85c4a 1462 void *v;
bc432dc7
LP
1463 Unit *member;
1464 Iterator i;
1465
eef85c4a 1466 HASHMAP_FOREACH_KEY(v, member, u->dependencies[UNIT_BEFORE], i) {
cb5e3bc3
CD
1467 if (UNIT_DEREF(member->slice) == u)
1468 u->cgroup_members_mask |= unit_get_subtree_mask(member); /* note that this calls ourselves again, for the children */
bc432dc7
LP
1469 }
1470 }
1471
1472 u->cgroup_members_mask_valid = true;
6414b7c9 1473 return u->cgroup_members_mask;
246aa6dd
LP
1474}
1475
efdb0237 1476CGroupMask unit_get_siblings_mask(Unit *u) {
4ad49000 1477 assert(u);
246aa6dd 1478
efdb0237
LP
1479 /* Returns the mask of controllers all of the unit's siblings
1480 * require, i.e. the members mask of the unit's parent slice
1481 * if there is one. */
1482
bc432dc7 1483 if (UNIT_ISSET(u->slice))
637f421e 1484 return unit_get_members_mask(UNIT_DEREF(u->slice));
4ad49000 1485
64e844e5 1486 return unit_get_subtree_mask(u); /* we are the top-level slice */
246aa6dd
LP
1487}
1488
4f6f62e4
CD
1489CGroupMask unit_get_disable_mask(Unit *u) {
1490 CGroupContext *c;
1491
1492 c = unit_get_cgroup_context(u);
1493 if (!c)
1494 return 0;
1495
1496 return c->disable_controllers;
1497}
1498
1499CGroupMask unit_get_ancestor_disable_mask(Unit *u) {
1500 CGroupMask mask;
1501
1502 assert(u);
1503 mask = unit_get_disable_mask(u);
1504
1505 /* Returns the mask of controllers which are marked as forcibly
1506 * disabled in any ancestor unit or the unit in question. */
1507
1508 if (UNIT_ISSET(u->slice))
1509 mask |= unit_get_ancestor_disable_mask(UNIT_DEREF(u->slice));
1510
1511 return mask;
1512}
1513
efdb0237
LP
1514CGroupMask unit_get_subtree_mask(Unit *u) {
1515
1516 /* Returns the mask of this subtree, meaning of the group
1517 * itself and its children. */
1518
1519 return unit_get_own_mask(u) | unit_get_members_mask(u);
1520}
1521
1522CGroupMask unit_get_target_mask(Unit *u) {
1523 CGroupMask mask;
1524
1525 /* This returns the cgroup mask of all controllers to enable
1526 * for a specific cgroup, i.e. everything it needs itself,
1527 * plus all that its children need, plus all that its siblings
1528 * need. This is primarily useful on the legacy cgroup
1529 * hierarchy, where we need to duplicate each cgroup in each
1530 * hierarchy that shall be enabled for it. */
6414b7c9 1531
efdb0237
LP
1532 mask = unit_get_own_mask(u) | unit_get_members_mask(u) | unit_get_siblings_mask(u);
1533 mask &= u->manager->cgroup_supported;
c72703e2 1534 mask &= ~unit_get_ancestor_disable_mask(u);
efdb0237
LP
1535
1536 return mask;
1537}
1538
1539CGroupMask unit_get_enable_mask(Unit *u) {
1540 CGroupMask mask;
1541
1542 /* This returns the cgroup mask of all controllers to enable
1543 * for the children of a specific cgroup. This is primarily
1544 * useful for the unified cgroup hierarchy, where each cgroup
1545 * controls which controllers are enabled for its children. */
1546
1547 mask = unit_get_members_mask(u);
6414b7c9 1548 mask &= u->manager->cgroup_supported;
c72703e2 1549 mask &= ~unit_get_ancestor_disable_mask(u);
6414b7c9
DS
1550
1551 return mask;
1552}
1553
5af88058 1554void unit_invalidate_cgroup_members_masks(Unit *u) {
bc432dc7
LP
1555 assert(u);
1556
5af88058
LP
1557 /* Recurse invalidate the member masks cache all the way up the tree */
1558 u->cgroup_members_mask_valid = false;
bc432dc7 1559
5af88058
LP
1560 if (UNIT_ISSET(u->slice))
1561 unit_invalidate_cgroup_members_masks(UNIT_DEREF(u->slice));
6414b7c9
DS
1562}
1563
6592b975 1564const char *unit_get_realized_cgroup_path(Unit *u, CGroupMask mask) {
03b90d4b 1565
6592b975 1566 /* Returns the realized cgroup path of the specified unit where all specified controllers are available. */
03b90d4b
LP
1567
1568 while (u) {
6592b975 1569
03b90d4b
LP
1570 if (u->cgroup_path &&
1571 u->cgroup_realized &&
d94a24ca 1572 FLAGS_SET(u->cgroup_realized_mask, mask))
03b90d4b
LP
1573 return u->cgroup_path;
1574
1575 u = UNIT_DEREF(u->slice);
1576 }
1577
1578 return NULL;
1579}
1580
6592b975
LP
1581static const char *migrate_callback(CGroupMask mask, void *userdata) {
1582 return unit_get_realized_cgroup_path(userdata, mask);
1583}
1584
303ee601 1585char *unit_default_cgroup_path(const Unit *u) {
efdb0237
LP
1586 _cleanup_free_ char *escaped = NULL, *slice = NULL;
1587 int r;
1588
1589 assert(u);
1590
1591 if (unit_has_name(u, SPECIAL_ROOT_SLICE))
1592 return strdup(u->manager->cgroup_root);
1593
1594 if (UNIT_ISSET(u->slice) && !unit_has_name(UNIT_DEREF(u->slice), SPECIAL_ROOT_SLICE)) {
1595 r = cg_slice_to_path(UNIT_DEREF(u->slice)->id, &slice);
1596 if (r < 0)
1597 return NULL;
1598 }
1599
1600 escaped = cg_escape(u->id);
1601 if (!escaped)
1602 return NULL;
1603
1604 if (slice)
605405c6
ZJS
1605 return strjoin(u->manager->cgroup_root, "/", slice, "/",
1606 escaped);
efdb0237 1607 else
605405c6 1608 return strjoin(u->manager->cgroup_root, "/", escaped);
efdb0237
LP
1609}
1610
1611int unit_set_cgroup_path(Unit *u, const char *path) {
1612 _cleanup_free_ char *p = NULL;
1613 int r;
1614
1615 assert(u);
1616
5210387e
LP
1617 if (streq_ptr(u->cgroup_path, path))
1618 return 0;
1619
efdb0237
LP
1620 if (path) {
1621 p = strdup(path);
1622 if (!p)
1623 return -ENOMEM;
5210387e 1624 }
efdb0237
LP
1625
1626 if (p) {
1627 r = hashmap_put(u->manager->cgroup_unit, p, u);
1628 if (r < 0)
1629 return r;
1630 }
1631
1632 unit_release_cgroup(u);
ae2a15bc 1633 u->cgroup_path = TAKE_PTR(p);
efdb0237
LP
1634
1635 return 1;
1636}
1637
1638int unit_watch_cgroup(Unit *u) {
ab2c3861 1639 _cleanup_free_ char *events = NULL;
efdb0237
LP
1640 int r;
1641
1642 assert(u);
1643
0bb814c2
LP
1644 /* Watches the "cgroups.events" attribute of this unit's cgroup for "empty" events, but only if
1645 * cgroupv2 is available. */
1646
efdb0237
LP
1647 if (!u->cgroup_path)
1648 return 0;
1649
0bb814c2 1650 if (u->cgroup_control_inotify_wd >= 0)
efdb0237
LP
1651 return 0;
1652
1653 /* Only applies to the unified hierarchy */
c22800e4 1654 r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
b4cccbc1
LP
1655 if (r < 0)
1656 return log_error_errno(r, "Failed to determine whether the name=systemd hierarchy is unified: %m");
1657 if (r == 0)
efdb0237
LP
1658 return 0;
1659
0bb814c2 1660 /* No point in watch the top-level slice, it's never going to run empty. */
efdb0237
LP
1661 if (unit_has_name(u, SPECIAL_ROOT_SLICE))
1662 return 0;
1663
0bb814c2 1664 r = hashmap_ensure_allocated(&u->manager->cgroup_control_inotify_wd_unit, &trivial_hash_ops);
efdb0237
LP
1665 if (r < 0)
1666 return log_oom();
1667
ab2c3861 1668 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, "cgroup.events", &events);
efdb0237
LP
1669 if (r < 0)
1670 return log_oom();
1671
0bb814c2
LP
1672 u->cgroup_control_inotify_wd = inotify_add_watch(u->manager->cgroup_inotify_fd, events, IN_MODIFY);
1673 if (u->cgroup_control_inotify_wd < 0) {
efdb0237 1674
0bb814c2
LP
1675 if (errno == ENOENT) /* If the directory is already gone we don't need to track it, so this
1676 * is not an error */
efdb0237
LP
1677 return 0;
1678
0bb814c2 1679 return log_unit_error_errno(u, errno, "Failed to add control inotify watch descriptor for control group %s: %m", u->cgroup_path);
efdb0237
LP
1680 }
1681
0bb814c2 1682 r = hashmap_put(u->manager->cgroup_control_inotify_wd_unit, INT_TO_PTR(u->cgroup_control_inotify_wd), u);
efdb0237 1683 if (r < 0)
0bb814c2 1684 return log_unit_error_errno(u, r, "Failed to add control inotify watch descriptor to hash map: %m");
efdb0237
LP
1685
1686 return 0;
1687}
1688
afcfaa69
LP
1689int unit_watch_cgroup_memory(Unit *u) {
1690 _cleanup_free_ char *events = NULL;
1691 CGroupContext *c;
1692 int r;
1693
1694 assert(u);
1695
1696 /* Watches the "memory.events" attribute of this unit's cgroup for "oom_kill" events, but only if
1697 * cgroupv2 is available. */
1698
1699 if (!u->cgroup_path)
1700 return 0;
1701
1702 c = unit_get_cgroup_context(u);
1703 if (!c)
1704 return 0;
1705
1706 /* The "memory.events" attribute is only available if the memory controller is on. Let's hence tie
1707 * this to memory accounting, in a way watching for OOM kills is a form of memory accounting after
1708 * all. */
1709 if (!c->memory_accounting)
1710 return 0;
1711
1712 /* Don't watch inner nodes, as the kernel doesn't report oom_kill events recursively currently, and
1713 * we also don't want to generate a log message for each parent cgroup of a process. */
1714 if (u->type == UNIT_SLICE)
1715 return 0;
1716
1717 if (u->cgroup_memory_inotify_wd >= 0)
1718 return 0;
1719
1720 /* Only applies to the unified hierarchy */
1721 r = cg_all_unified();
1722 if (r < 0)
1723 return log_error_errno(r, "Failed to determine whether the memory controller is unified: %m");
1724 if (r == 0)
1725 return 0;
1726
1727 r = hashmap_ensure_allocated(&u->manager->cgroup_memory_inotify_wd_unit, &trivial_hash_ops);
1728 if (r < 0)
1729 return log_oom();
1730
1731 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, "memory.events", &events);
1732 if (r < 0)
1733 return log_oom();
1734
1735 u->cgroup_memory_inotify_wd = inotify_add_watch(u->manager->cgroup_inotify_fd, events, IN_MODIFY);
1736 if (u->cgroup_memory_inotify_wd < 0) {
1737
1738 if (errno == ENOENT) /* If the directory is already gone we don't need to track it, so this
1739 * is not an error */
1740 return 0;
1741
1742 return log_unit_error_errno(u, errno, "Failed to add memory inotify watch descriptor for control group %s: %m", u->cgroup_path);
1743 }
1744
1745 r = hashmap_put(u->manager->cgroup_memory_inotify_wd_unit, INT_TO_PTR(u->cgroup_memory_inotify_wd), u);
1746 if (r < 0)
1747 return log_unit_error_errno(u, r, "Failed to add memory inotify watch descriptor to hash map: %m");
1748
1749 return 0;
1750}
1751
a4634b21
LP
1752int unit_pick_cgroup_path(Unit *u) {
1753 _cleanup_free_ char *path = NULL;
1754 int r;
1755
1756 assert(u);
1757
1758 if (u->cgroup_path)
1759 return 0;
1760
1761 if (!UNIT_HAS_CGROUP_CONTEXT(u))
1762 return -EINVAL;
1763
1764 path = unit_default_cgroup_path(u);
1765 if (!path)
1766 return log_oom();
1767
1768 r = unit_set_cgroup_path(u, path);
1769 if (r == -EEXIST)
1770 return log_unit_error_errno(u, r, "Control group %s exists already.", path);
1771 if (r < 0)
1772 return log_unit_error_errno(u, r, "Failed to set unit's control group path to %s: %m", path);
1773
1774 return 0;
1775}
1776
efdb0237
LP
1777static int unit_create_cgroup(
1778 Unit *u,
1779 CGroupMask target_mask,
0d2d6fbf
CD
1780 CGroupMask enable_mask,
1781 ManagerState state) {
efdb0237 1782
65be7e06 1783 bool created;
27adcc97 1784 int r;
64747e2d 1785
4ad49000 1786 assert(u);
64747e2d 1787
27c4ed79 1788 if (!UNIT_HAS_CGROUP_CONTEXT(u))
0cd385d3
LP
1789 return 0;
1790
a4634b21
LP
1791 /* Figure out our cgroup path */
1792 r = unit_pick_cgroup_path(u);
1793 if (r < 0)
1794 return r;
b58b8e11 1795
03b90d4b 1796 /* First, create our own group */
efdb0237 1797 r = cg_create_everywhere(u->manager->cgroup_supported, target_mask, u->cgroup_path);
23bbb0de 1798 if (r < 0)
efdb0237 1799 return log_unit_error_errno(u, r, "Failed to create cgroup %s: %m", u->cgroup_path);
490c5a37 1800 created = r;
efdb0237
LP
1801
1802 /* Start watching it */
1803 (void) unit_watch_cgroup(u);
afcfaa69 1804 (void) unit_watch_cgroup_memory(u);
efdb0237 1805
65be7e06 1806 /* Preserve enabled controllers in delegated units, adjust others. */
1fd3a10c 1807 if (created || !u->cgroup_realized || !unit_cgroup_delegate(u)) {
27adcc97 1808 CGroupMask result_mask = 0;
65be7e06
ZJS
1809
1810 /* Enable all controllers we need */
27adcc97 1811 r = cg_enable_everywhere(u->manager->cgroup_supported, enable_mask, u->cgroup_path, &result_mask);
65be7e06 1812 if (r < 0)
27adcc97
LP
1813 log_unit_warning_errno(u, r, "Failed to enable/disable controllers on cgroup %s, ignoring: %m", u->cgroup_path);
1814
1815 /* If we just turned off a controller, this might release the controller for our parent too, let's
1816 * enqueue the parent for re-realization in that case again. */
1817 if (UNIT_ISSET(u->slice)) {
1818 CGroupMask turned_off;
1819
1820 turned_off = (u->cgroup_realized ? u->cgroup_enabled_mask & ~result_mask : 0);
1821 if (turned_off != 0) {
1822 Unit *parent;
1823
1824 /* Force the parent to propagate the enable mask to the kernel again, by invalidating
1825 * the controller we just turned off. */
1826
1827 for (parent = UNIT_DEREF(u->slice); parent; parent = UNIT_DEREF(parent->slice))
1828 unit_invalidate_cgroup(parent, turned_off);
1829 }
1830 }
1831
1832 /* Remember what's actually enabled now */
1833 u->cgroup_enabled_mask = result_mask;
65be7e06 1834 }
03b90d4b
LP
1835
1836 /* Keep track that this is now realized */
4ad49000 1837 u->cgroup_realized = true;
efdb0237 1838 u->cgroup_realized_mask = target_mask;
4ad49000 1839
1d9cc876 1840 if (u->type != UNIT_SLICE && !unit_cgroup_delegate(u)) {
0cd385d3
LP
1841
1842 /* Then, possibly move things over, but not if
1843 * subgroups may contain processes, which is the case
1844 * for slice and delegation units. */
1845 r = cg_migrate_everywhere(u->manager->cgroup_supported, u->cgroup_path, u->cgroup_path, migrate_callback, u);
1846 if (r < 0)
efdb0237 1847 log_unit_warning_errno(u, r, "Failed to migrate cgroup from to %s, ignoring: %m", u->cgroup_path);
0cd385d3 1848 }
03b90d4b 1849
0d2d6fbf
CD
1850 /* Set attributes */
1851 cgroup_context_apply(u, target_mask, state);
1852 cgroup_xattr_apply(u);
1853
64747e2d
LP
1854 return 0;
1855}
1856
6592b975
LP
1857static int unit_attach_pid_to_cgroup_via_bus(Unit *u, pid_t pid, const char *suffix_path) {
1858 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1859 char *pp;
7b3fd631 1860 int r;
6592b975 1861
7b3fd631
LP
1862 assert(u);
1863
6592b975
LP
1864 if (MANAGER_IS_SYSTEM(u->manager))
1865 return -EINVAL;
1866
1867 if (!u->manager->system_bus)
1868 return -EIO;
1869
1870 if (!u->cgroup_path)
1871 return -EINVAL;
1872
1873 /* Determine this unit's cgroup path relative to our cgroup root */
1874 pp = path_startswith(u->cgroup_path, u->manager->cgroup_root);
1875 if (!pp)
1876 return -EINVAL;
1877
1878 pp = strjoina("/", pp, suffix_path);
858d36c1 1879 path_simplify(pp, false);
6592b975
LP
1880
1881 r = sd_bus_call_method(u->manager->system_bus,
1882 "org.freedesktop.systemd1",
1883 "/org/freedesktop/systemd1",
1884 "org.freedesktop.systemd1.Manager",
1885 "AttachProcessesToUnit",
1886 &error, NULL,
1887 "ssau",
1888 NULL /* empty unit name means client's unit, i.e. us */, pp, 1, (uint32_t) pid);
7b3fd631 1889 if (r < 0)
6592b975
LP
1890 return log_unit_debug_errno(u, r, "Failed to attach unit process " PID_FMT " via the bus: %s", pid, bus_error_message(&error, r));
1891
1892 return 0;
1893}
1894
1895int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path) {
1896 CGroupMask delegated_mask;
1897 const char *p;
1898 Iterator i;
1899 void *pidp;
1900 int r, q;
1901
1902 assert(u);
1903
1904 if (!UNIT_HAS_CGROUP_CONTEXT(u))
1905 return -EINVAL;
1906
1907 if (set_isempty(pids))
1908 return 0;
7b3fd631 1909
6592b975 1910 r = unit_realize_cgroup(u);
7b3fd631
LP
1911 if (r < 0)
1912 return r;
1913
6592b975
LP
1914 if (isempty(suffix_path))
1915 p = u->cgroup_path;
1916 else
1917 p = strjoina(u->cgroup_path, "/", suffix_path);
1918
1919 delegated_mask = unit_get_delegate_mask(u);
1920
1921 r = 0;
1922 SET_FOREACH(pidp, pids, i) {
1923 pid_t pid = PTR_TO_PID(pidp);
1924 CGroupController c;
1925
1926 /* First, attach the PID to the main cgroup hierarchy */
1927 q = cg_attach(SYSTEMD_CGROUP_CONTROLLER, p, pid);
1928 if (q < 0) {
1929 log_unit_debug_errno(u, q, "Couldn't move process " PID_FMT " to requested cgroup '%s': %m", pid, p);
1930
1931 if (MANAGER_IS_USER(u->manager) && IN_SET(q, -EPERM, -EACCES)) {
1932 int z;
1933
1934 /* If we are in a user instance, and we can't move the process ourselves due to
1935 * permission problems, let's ask the system instance about it instead. Since it's more
1936 * privileged it might be able to move the process across the leaves of a subtree who's
1937 * top node is not owned by us. */
1938
1939 z = unit_attach_pid_to_cgroup_via_bus(u, pid, suffix_path);
1940 if (z < 0)
1941 log_unit_debug_errno(u, z, "Couldn't move process " PID_FMT " to requested cgroup '%s' via the system bus either: %m", pid, p);
1942 else
1943 continue; /* When the bus thing worked via the bus we are fully done for this PID. */
1944 }
1945
1946 if (r >= 0)
1947 r = q; /* Remember first error */
1948
1949 continue;
1950 }
1951
1952 q = cg_all_unified();
1953 if (q < 0)
1954 return q;
1955 if (q > 0)
1956 continue;
1957
1958 /* In the legacy hierarchy, attach the process to the request cgroup if possible, and if not to the
1959 * innermost realized one */
1960
1961 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1962 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
1963 const char *realized;
1964
1965 if (!(u->manager->cgroup_supported & bit))
1966 continue;
1967
1968 /* If this controller is delegated and realized, honour the caller's request for the cgroup suffix. */
1969 if (delegated_mask & u->cgroup_realized_mask & bit) {
1970 q = cg_attach(cgroup_controller_to_string(c), p, pid);
1971 if (q >= 0)
1972 continue; /* Success! */
1973
1974 log_unit_debug_errno(u, q, "Failed to attach PID " PID_FMT " to requested cgroup %s in controller %s, falling back to unit's cgroup: %m",
1975 pid, p, cgroup_controller_to_string(c));
1976 }
1977
1978 /* So this controller is either not delegate or realized, or something else weird happened. In
1979 * that case let's attach the PID at least to the closest cgroup up the tree that is
1980 * realized. */
1981 realized = unit_get_realized_cgroup_path(u, bit);
1982 if (!realized)
1983 continue; /* Not even realized in the root slice? Then let's not bother */
1984
1985 q = cg_attach(cgroup_controller_to_string(c), realized, pid);
1986 if (q < 0)
1987 log_unit_debug_errno(u, q, "Failed to attach PID " PID_FMT " to realized cgroup %s in controller %s, ignoring: %m",
1988 pid, realized, cgroup_controller_to_string(c));
1989 }
1990 }
1991
1992 return r;
7b3fd631
LP
1993}
1994
906c06f6
DM
1995static bool unit_has_mask_realized(
1996 Unit *u,
1997 CGroupMask target_mask,
17f14955 1998 CGroupMask enable_mask) {
906c06f6 1999
bc432dc7
LP
2000 assert(u);
2001
d5095dcd
LP
2002 /* Returns true if this unit is fully realized. We check four things:
2003 *
2004 * 1. Whether the cgroup was created at all
4e1dfa45
CD
2005 * 2. Whether the cgroup was created in all the hierarchies we need it to be created in (in case of cgroup v1)
2006 * 3. Whether the cgroup has all the right controllers enabled (in case of cgroup v2)
d5095dcd
LP
2007 * 4. Whether the invalidation mask is currently zero
2008 *
2009 * If you wonder why we mask the target realization and enable mask with CGROUP_MASK_V1/CGROUP_MASK_V2: note
4e1dfa45
CD
2010 * that there are three sets of bitmasks: CGROUP_MASK_V1 (for real cgroup v1 controllers), CGROUP_MASK_V2 (for
2011 * real cgroup v2 controllers) and CGROUP_MASK_BPF (for BPF-based pseudo-controllers). Now, cgroup_realized_mask
2012 * is only matters for cgroup v1 controllers, and cgroup_enabled_mask only used for cgroup v2, and if they
d5095dcd
LP
2013 * differ in the others, we don't really care. (After all, the cgroup_enabled_mask tracks with controllers are
2014 * enabled through cgroup.subtree_control, and since the BPF pseudo-controllers don't show up there, they
2015 * simply don't matter. */
2016
906c06f6 2017 return u->cgroup_realized &&
d5095dcd
LP
2018 ((u->cgroup_realized_mask ^ target_mask) & CGROUP_MASK_V1) == 0 &&
2019 ((u->cgroup_enabled_mask ^ enable_mask) & CGROUP_MASK_V2) == 0 &&
17f14955 2020 u->cgroup_invalidated_mask == 0;
6414b7c9
DS
2021}
2022
4f6f62e4
CD
2023static bool unit_has_mask_disables_realized(
2024 Unit *u,
2025 CGroupMask target_mask,
2026 CGroupMask enable_mask) {
2027
2028 assert(u);
2029
2030 /* Returns true if all controllers which should be disabled are indeed disabled.
2031 *
2032 * Unlike unit_has_mask_realized, we don't care what was enabled, only that anything we want to remove is
2033 * already removed. */
2034
2035 return !u->cgroup_realized ||
2036 (FLAGS_SET(u->cgroup_realized_mask, target_mask & CGROUP_MASK_V1) &&
2037 FLAGS_SET(u->cgroup_enabled_mask, enable_mask & CGROUP_MASK_V2));
2038}
2039
a57669d2
CD
2040static bool unit_has_mask_enables_realized(
2041 Unit *u,
2042 CGroupMask target_mask,
2043 CGroupMask enable_mask) {
2044
2045 assert(u);
2046
2047 /* Returns true if all controllers which should be enabled are indeed enabled.
2048 *
2049 * Unlike unit_has_mask_realized, we don't care about the controllers that are not present, only that anything
2050 * we want to add is already added. */
2051
2052 return u->cgroup_realized &&
c72703e2
CD
2053 ((u->cgroup_realized_mask | target_mask) & CGROUP_MASK_V1) == (u->cgroup_realized_mask & CGROUP_MASK_V1) &&
2054 ((u->cgroup_enabled_mask | enable_mask) & CGROUP_MASK_V2) == (u->cgroup_enabled_mask & CGROUP_MASK_V2);
a57669d2
CD
2055}
2056
27adcc97 2057void unit_add_to_cgroup_realize_queue(Unit *u) {
2aa57a65
LP
2058 assert(u);
2059
2060 if (u->in_cgroup_realize_queue)
2061 return;
2062
2063 LIST_PREPEND(cgroup_realize_queue, u->manager->cgroup_realize_queue, u);
2064 u->in_cgroup_realize_queue = true;
2065}
2066
2067static void unit_remove_from_cgroup_realize_queue(Unit *u) {
2068 assert(u);
2069
2070 if (!u->in_cgroup_realize_queue)
2071 return;
2072
2073 LIST_REMOVE(cgroup_realize_queue, u->manager->cgroup_realize_queue, u);
2074 u->in_cgroup_realize_queue = false;
2075}
2076
a57669d2
CD
2077/* Controllers can only be enabled breadth-first, from the root of the
2078 * hierarchy downwards to the unit in question. */
2079static int unit_realize_cgroup_now_enable(Unit *u, ManagerState state) {
2080 CGroupMask target_mask, enable_mask, new_target_mask, new_enable_mask;
2081 int r;
2082
2083 assert(u);
2084
2085 /* First go deal with this unit's parent, or we won't be able to enable
2086 * any new controllers at this layer. */
2087 if (UNIT_ISSET(u->slice)) {
2088 r = unit_realize_cgroup_now_enable(UNIT_DEREF(u->slice), state);
2089 if (r < 0)
2090 return r;
2091 }
2092
2093 target_mask = unit_get_target_mask(u);
2094 enable_mask = unit_get_enable_mask(u);
2095
2096 /* We can only enable in this direction, don't try to disable anything.
2097 */
2098 if (unit_has_mask_enables_realized(u, target_mask, enable_mask))
2099 return 0;
2100
2101 new_target_mask = u->cgroup_realized_mask | target_mask;
2102 new_enable_mask = u->cgroup_enabled_mask | enable_mask;
2103
c72703e2 2104 return unit_create_cgroup(u, new_target_mask, new_enable_mask, state);
a57669d2
CD
2105}
2106
4f6f62e4
CD
2107/* Controllers can only be disabled depth-first, from the leaves of the
2108 * hierarchy upwards to the unit in question. */
2109static int unit_realize_cgroup_now_disable(Unit *u, ManagerState state) {
2110 Iterator i;
2111 Unit *m;
2112 void *v;
2113
2114 assert(u);
2115
2116 if (u->type != UNIT_SLICE)
2117 return 0;
2118
2119 HASHMAP_FOREACH_KEY(v, m, u->dependencies[UNIT_BEFORE], i) {
2120 CGroupMask target_mask, enable_mask, new_target_mask, new_enable_mask;
2121 int r;
2122
2123 if (UNIT_DEREF(m->slice) != u)
2124 continue;
2125
2126 /* The cgroup for this unit might not actually be fully
2127 * realised yet, in which case it isn't holding any controllers
2128 * open anyway. */
2129 if (!m->cgroup_path)
2130 continue;
2131
2132 /* We must disable those below us first in order to release the
2133 * controller. */
2134 if (m->type == UNIT_SLICE)
2135 (void) unit_realize_cgroup_now_disable(m, state);
2136
2137 target_mask = unit_get_target_mask(m);
2138 enable_mask = unit_get_enable_mask(m);
2139
2140 /* We can only disable in this direction, don't try to enable
2141 * anything. */
2142 if (unit_has_mask_disables_realized(m, target_mask, enable_mask))
2143 continue;
2144
2145 new_target_mask = m->cgroup_realized_mask & target_mask;
2146 new_enable_mask = m->cgroup_enabled_mask & enable_mask;
2147
2148 r = unit_create_cgroup(m, new_target_mask, new_enable_mask, state);
2149 if (r < 0)
2150 return r;
2151 }
2152
2153 return 0;
2154}
a57669d2 2155
6414b7c9
DS
2156/* Check if necessary controllers and attributes for a unit are in place.
2157 *
a57669d2
CD
2158 * - If so, do nothing.
2159 * - If not, create paths, move processes over, and set attributes.
2160 *
2161 * Controllers can only be *enabled* in a breadth-first way, and *disabled* in
2162 * a depth-first way. As such the process looks like this:
2163 *
2164 * Suppose we have a cgroup hierarchy which looks like this:
2165 *
2166 * root
2167 * / \
2168 * / \
2169 * / \
2170 * a b
2171 * / \ / \
2172 * / \ / \
2173 * c d e f
2174 * / \ / \ / \ / \
2175 * h i j k l m n o
2176 *
2177 * 1. We want to realise cgroup "d" now.
c72703e2 2178 * 2. cgroup "a" has DisableControllers=cpu in the associated unit.
a57669d2
CD
2179 * 3. cgroup "k" just started requesting the memory controller.
2180 *
2181 * To make this work we must do the following in order:
2182 *
2183 * 1. Disable CPU controller in k, j
2184 * 2. Disable CPU controller in d
2185 * 3. Enable memory controller in root
2186 * 4. Enable memory controller in a
2187 * 5. Enable memory controller in d
2188 * 6. Enable memory controller in k
2189 *
2190 * Notice that we need to touch j in one direction, but not the other. We also
2191 * don't go beyond d when disabling -- it's up to "a" to get realized if it
2192 * wants to disable further. The basic rules are therefore:
2193 *
2194 * - If you're disabling something, you need to realise all of the cgroups from
2195 * your recursive descendants to the root. This starts from the leaves.
2196 * - If you're enabling something, you need to realise from the root cgroup
2197 * downwards, but you don't need to iterate your recursive descendants.
6414b7c9
DS
2198 *
2199 * Returns 0 on success and < 0 on failure. */
db785129 2200static int unit_realize_cgroup_now(Unit *u, ManagerState state) {
efdb0237 2201 CGroupMask target_mask, enable_mask;
6414b7c9 2202 int r;
64747e2d 2203
4ad49000 2204 assert(u);
64747e2d 2205
2aa57a65 2206 unit_remove_from_cgroup_realize_queue(u);
64747e2d 2207
efdb0237 2208 target_mask = unit_get_target_mask(u);
ccf78df1
TH
2209 enable_mask = unit_get_enable_mask(u);
2210
17f14955 2211 if (unit_has_mask_realized(u, target_mask, enable_mask))
0a1eb06d 2212 return 0;
64747e2d 2213
4f6f62e4
CD
2214 /* Disable controllers below us, if there are any */
2215 r = unit_realize_cgroup_now_disable(u, state);
2216 if (r < 0)
2217 return r;
2218
2219 /* Enable controllers above us, if there are any */
6414b7c9 2220 if (UNIT_ISSET(u->slice)) {
a57669d2 2221 r = unit_realize_cgroup_now_enable(UNIT_DEREF(u->slice), state);
6414b7c9
DS
2222 if (r < 0)
2223 return r;
2224 }
4ad49000 2225
0d2d6fbf
CD
2226 /* Now actually deal with the cgroup we were trying to realise and set attributes */
2227 r = unit_create_cgroup(u, target_mask, enable_mask, state);
6414b7c9
DS
2228 if (r < 0)
2229 return r;
2230
c2baf11c
LP
2231 /* Now, reset the invalidation mask */
2232 u->cgroup_invalidated_mask = 0;
6414b7c9 2233 return 0;
64747e2d
LP
2234}
2235
91a6073e 2236unsigned manager_dispatch_cgroup_realize_queue(Manager *m) {
db785129 2237 ManagerState state;
4ad49000 2238 unsigned n = 0;
db785129 2239 Unit *i;
6414b7c9 2240 int r;
ecedd90f 2241
91a6073e
LP
2242 assert(m);
2243
db785129
LP
2244 state = manager_state(m);
2245
91a6073e
LP
2246 while ((i = m->cgroup_realize_queue)) {
2247 assert(i->in_cgroup_realize_queue);
ecedd90f 2248
2aa57a65
LP
2249 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(i))) {
2250 /* Maybe things changed, and the unit is not actually active anymore? */
2251 unit_remove_from_cgroup_realize_queue(i);
2252 continue;
2253 }
2254
db785129 2255 r = unit_realize_cgroup_now(i, state);
6414b7c9 2256 if (r < 0)
efdb0237 2257 log_warning_errno(r, "Failed to realize cgroups for queued unit %s, ignoring: %m", i->id);
0a1eb06d 2258
4ad49000
LP
2259 n++;
2260 }
ecedd90f 2261
4ad49000 2262 return n;
8e274523
LP
2263}
2264
91a6073e 2265static void unit_add_siblings_to_cgroup_realize_queue(Unit *u) {
4ad49000 2266 Unit *slice;
ca949c9d 2267
4ad49000
LP
2268 /* This adds the siblings of the specified unit and the
2269 * siblings of all parent units to the cgroup queue. (But
2270 * neither the specified unit itself nor the parents.) */
2271
2272 while ((slice = UNIT_DEREF(u->slice))) {
2273 Iterator i;
2274 Unit *m;
eef85c4a 2275 void *v;
8f53a7b8 2276
eef85c4a 2277 HASHMAP_FOREACH_KEY(v, m, u->dependencies[UNIT_BEFORE], i) {
6414b7c9
DS
2278 /* Skip units that have a dependency on the slice
2279 * but aren't actually in it. */
4ad49000 2280 if (UNIT_DEREF(m->slice) != slice)
50159e6a 2281 continue;
8e274523 2282
6414b7c9
DS
2283 /* No point in doing cgroup application for units
2284 * without active processes. */
2285 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(m)))
2286 continue;
2287
2288 /* If the unit doesn't need any new controllers
2289 * and has current ones realized, it doesn't need
2290 * any changes. */
906c06f6
DM
2291 if (unit_has_mask_realized(m,
2292 unit_get_target_mask(m),
17f14955 2293 unit_get_enable_mask(m)))
6414b7c9
DS
2294 continue;
2295
91a6073e 2296 unit_add_to_cgroup_realize_queue(m);
50159e6a
LP
2297 }
2298
4ad49000 2299 u = slice;
8e274523 2300 }
4ad49000
LP
2301}
2302
0a1eb06d 2303int unit_realize_cgroup(Unit *u) {
4ad49000
LP
2304 assert(u);
2305
35b7ff80 2306 if (!UNIT_HAS_CGROUP_CONTEXT(u))
0a1eb06d 2307 return 0;
8e274523 2308
4ad49000
LP
2309 /* So, here's the deal: when realizing the cgroups for this
2310 * unit, we need to first create all parents, but there's more
2311 * actually: for the weight-based controllers we also need to
2312 * make sure that all our siblings (i.e. units that are in the
73e231ab 2313 * same slice as we are) have cgroups, too. Otherwise, things
4ad49000
LP
2314 * would become very uneven as each of their processes would
2315 * get as much resources as all our group together. This call
2316 * will synchronously create the parent cgroups, but will
2317 * defer work on the siblings to the next event loop
2318 * iteration. */
ca949c9d 2319
4ad49000 2320 /* Add all sibling slices to the cgroup queue. */
91a6073e 2321 unit_add_siblings_to_cgroup_realize_queue(u);
4ad49000 2322
6414b7c9 2323 /* And realize this one now (and apply the values) */
db785129 2324 return unit_realize_cgroup_now(u, manager_state(u->manager));
8e274523
LP
2325}
2326
efdb0237
LP
2327void unit_release_cgroup(Unit *u) {
2328 assert(u);
2329
8a0d5388
LP
2330 /* Forgets all cgroup details for this cgroup — but does *not* destroy the cgroup. This is hence OK to call
2331 * when we close down everything for reexecution, where we really want to leave the cgroup in place. */
efdb0237
LP
2332
2333 if (u->cgroup_path) {
2334 (void) hashmap_remove(u->manager->cgroup_unit, u->cgroup_path);
2335 u->cgroup_path = mfree(u->cgroup_path);
2336 }
2337
0bb814c2
LP
2338 if (u->cgroup_control_inotify_wd >= 0) {
2339 if (inotify_rm_watch(u->manager->cgroup_inotify_fd, u->cgroup_control_inotify_wd) < 0)
2340 log_unit_debug_errno(u, errno, "Failed to remove cgroup control inotify watch %i for %s, ignoring: %m", u->cgroup_control_inotify_wd, u->id);
efdb0237 2341
0bb814c2
LP
2342 (void) hashmap_remove(u->manager->cgroup_control_inotify_wd_unit, INT_TO_PTR(u->cgroup_control_inotify_wd));
2343 u->cgroup_control_inotify_wd = -1;
efdb0237 2344 }
afcfaa69
LP
2345
2346 if (u->cgroup_memory_inotify_wd >= 0) {
2347 if (inotify_rm_watch(u->manager->cgroup_inotify_fd, u->cgroup_memory_inotify_wd) < 0)
2348 log_unit_debug_errno(u, errno, "Failed to remove cgroup memory inotify watch %i for %s, ignoring: %m", u->cgroup_memory_inotify_wd, u->id);
2349
2350 (void) hashmap_remove(u->manager->cgroup_memory_inotify_wd_unit, INT_TO_PTR(u->cgroup_memory_inotify_wd));
2351 u->cgroup_memory_inotify_wd = -1;
2352 }
efdb0237
LP
2353}
2354
2355void unit_prune_cgroup(Unit *u) {
8e274523 2356 int r;
efdb0237 2357 bool is_root_slice;
8e274523 2358
4ad49000 2359 assert(u);
8e274523 2360
efdb0237
LP
2361 /* Removes the cgroup, if empty and possible, and stops watching it. */
2362
4ad49000
LP
2363 if (!u->cgroup_path)
2364 return;
8e274523 2365
fe700f46
LP
2366 (void) unit_get_cpu_usage(u, NULL); /* Cache the last CPU usage value before we destroy the cgroup */
2367
efdb0237
LP
2368 is_root_slice = unit_has_name(u, SPECIAL_ROOT_SLICE);
2369
2370 r = cg_trim_everywhere(u->manager->cgroup_supported, u->cgroup_path, !is_root_slice);
dab5bf85 2371 if (r < 0) {
f29ff115 2372 log_unit_debug_errno(u, r, "Failed to destroy cgroup %s, ignoring: %m", u->cgroup_path);
dab5bf85
RL
2373 return;
2374 }
8e274523 2375
efdb0237
LP
2376 if (is_root_slice)
2377 return;
2378
2379 unit_release_cgroup(u);
0a1eb06d 2380
4ad49000 2381 u->cgroup_realized = false;
bc432dc7 2382 u->cgroup_realized_mask = 0;
ccf78df1 2383 u->cgroup_enabled_mask = 0;
084c7007
RG
2384
2385 u->bpf_device_control_installed = bpf_program_unref(u->bpf_device_control_installed);
8e274523
LP
2386}
2387
efdb0237 2388int unit_search_main_pid(Unit *u, pid_t *ret) {
4ad49000 2389 _cleanup_fclose_ FILE *f = NULL;
4d051546 2390 pid_t pid = 0, npid;
efdb0237 2391 int r;
4ad49000
LP
2392
2393 assert(u);
efdb0237 2394 assert(ret);
4ad49000
LP
2395
2396 if (!u->cgroup_path)
efdb0237 2397 return -ENXIO;
4ad49000 2398
efdb0237
LP
2399 r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &f);
2400 if (r < 0)
2401 return r;
4ad49000 2402
4ad49000 2403 while (cg_read_pid(f, &npid) > 0) {
4ad49000
LP
2404
2405 if (npid == pid)
2406 continue;
8e274523 2407
4d051546 2408 if (pid_is_my_child(npid) == 0)
4ad49000 2409 continue;
8e274523 2410
efdb0237 2411 if (pid != 0)
4ad49000
LP
2412 /* Dang, there's more than one daemonized PID
2413 in this group, so we don't know what process
2414 is the main process. */
efdb0237
LP
2415
2416 return -ENODATA;
8e274523 2417
4ad49000 2418 pid = npid;
8e274523
LP
2419 }
2420
efdb0237
LP
2421 *ret = pid;
2422 return 0;
2423}
2424
2425static int unit_watch_pids_in_path(Unit *u, const char *path) {
b3c5bad3 2426 _cleanup_closedir_ DIR *d = NULL;
efdb0237
LP
2427 _cleanup_fclose_ FILE *f = NULL;
2428 int ret = 0, r;
2429
2430 assert(u);
2431 assert(path);
2432
2433 r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, path, &f);
2434 if (r < 0)
2435 ret = r;
2436 else {
2437 pid_t pid;
2438
2439 while ((r = cg_read_pid(f, &pid)) > 0) {
f75f613d 2440 r = unit_watch_pid(u, pid, false);
efdb0237
LP
2441 if (r < 0 && ret >= 0)
2442 ret = r;
2443 }
2444
2445 if (r < 0 && ret >= 0)
2446 ret = r;
2447 }
2448
2449 r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, path, &d);
2450 if (r < 0) {
2451 if (ret >= 0)
2452 ret = r;
2453 } else {
2454 char *fn;
2455
2456 while ((r = cg_read_subgroup(d, &fn)) > 0) {
2457 _cleanup_free_ char *p = NULL;
2458
605405c6 2459 p = strjoin(path, "/", fn);
efdb0237
LP
2460 free(fn);
2461
2462 if (!p)
2463 return -ENOMEM;
2464
2465 r = unit_watch_pids_in_path(u, p);
2466 if (r < 0 && ret >= 0)
2467 ret = r;
2468 }
2469
2470 if (r < 0 && ret >= 0)
2471 ret = r;
2472 }
2473
2474 return ret;
2475}
2476
11aef522
LP
2477int unit_synthesize_cgroup_empty_event(Unit *u) {
2478 int r;
2479
2480 assert(u);
2481
2482 /* Enqueue a synthetic cgroup empty event if this unit doesn't watch any PIDs anymore. This is compatibility
2483 * support for non-unified systems where notifications aren't reliable, and hence need to take whatever we can
2484 * get as notification source as soon as we stopped having any useful PIDs to watch for. */
2485
2486 if (!u->cgroup_path)
2487 return -ENOENT;
2488
2489 r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
2490 if (r < 0)
2491 return r;
2492 if (r > 0) /* On unified we have reliable notifications, and don't need this */
2493 return 0;
2494
2495 if (!set_isempty(u->pids))
2496 return 0;
2497
2498 unit_add_to_cgroup_empty_queue(u);
2499 return 0;
2500}
2501
efdb0237 2502int unit_watch_all_pids(Unit *u) {
b4cccbc1
LP
2503 int r;
2504
efdb0237
LP
2505 assert(u);
2506
2507 /* Adds all PIDs from our cgroup to the set of PIDs we
2508 * watch. This is a fallback logic for cases where we do not
2509 * get reliable cgroup empty notifications: we try to use
2510 * SIGCHLD as replacement. */
2511
2512 if (!u->cgroup_path)
2513 return -ENOENT;
2514
c22800e4 2515 r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
b4cccbc1
LP
2516 if (r < 0)
2517 return r;
2518 if (r > 0) /* On unified we can use proper notifications */
efdb0237
LP
2519 return 0;
2520
2521 return unit_watch_pids_in_path(u, u->cgroup_path);
2522}
2523
09e24654
LP
2524static int on_cgroup_empty_event(sd_event_source *s, void *userdata) {
2525 Manager *m = userdata;
2526 Unit *u;
efdb0237
LP
2527 int r;
2528
09e24654
LP
2529 assert(s);
2530 assert(m);
efdb0237 2531
09e24654
LP
2532 u = m->cgroup_empty_queue;
2533 if (!u)
efdb0237
LP
2534 return 0;
2535
09e24654
LP
2536 assert(u->in_cgroup_empty_queue);
2537 u->in_cgroup_empty_queue = false;
2538 LIST_REMOVE(cgroup_empty_queue, m->cgroup_empty_queue, u);
2539
2540 if (m->cgroup_empty_queue) {
2541 /* More stuff queued, let's make sure we remain enabled */
2542 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
2543 if (r < 0)
19a691a9 2544 log_debug_errno(r, "Failed to reenable cgroup empty event source, ignoring: %m");
09e24654 2545 }
efdb0237
LP
2546
2547 unit_add_to_gc_queue(u);
2548
2549 if (UNIT_VTABLE(u)->notify_cgroup_empty)
2550 UNIT_VTABLE(u)->notify_cgroup_empty(u);
2551
2552 return 0;
2553}
2554
09e24654
LP
2555void unit_add_to_cgroup_empty_queue(Unit *u) {
2556 int r;
2557
2558 assert(u);
2559
2560 /* Note that there are four different ways how cgroup empty events reach us:
2561 *
2562 * 1. On the unified hierarchy we get an inotify event on the cgroup
2563 *
2564 * 2. On the legacy hierarchy, when running in system mode, we get a datagram on the cgroup agent socket
2565 *
2566 * 3. On the legacy hierarchy, when running in user mode, we get a D-Bus signal on the system bus
2567 *
2568 * 4. On the legacy hierarchy, in service units we start watching all processes of the cgroup for SIGCHLD as
2569 * soon as we get one SIGCHLD, to deal with unreliable cgroup notifications.
2570 *
2571 * Regardless which way we got the notification, we'll verify it here, and then add it to a separate
2572 * queue. This queue will be dispatched at a lower priority than the SIGCHLD handler, so that we always use
2573 * SIGCHLD if we can get it first, and only use the cgroup empty notifications if there's no SIGCHLD pending
2574 * (which might happen if the cgroup doesn't contain processes that are our own child, which is typically the
2575 * case for scope units). */
2576
2577 if (u->in_cgroup_empty_queue)
2578 return;
2579
2580 /* Let's verify that the cgroup is really empty */
2581 if (!u->cgroup_path)
2582 return;
2583 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
2584 if (r < 0) {
2585 log_unit_debug_errno(u, r, "Failed to determine whether cgroup %s is empty: %m", u->cgroup_path);
2586 return;
2587 }
2588 if (r == 0)
2589 return;
2590
2591 LIST_PREPEND(cgroup_empty_queue, u->manager->cgroup_empty_queue, u);
2592 u->in_cgroup_empty_queue = true;
2593
2594 /* Trigger the defer event */
2595 r = sd_event_source_set_enabled(u->manager->cgroup_empty_event_source, SD_EVENT_ONESHOT);
2596 if (r < 0)
2597 log_debug_errno(r, "Failed to enable cgroup empty event source: %m");
2598}
2599
afcfaa69
LP
2600static int unit_check_oom(Unit *u) {
2601 _cleanup_free_ char *oom_kill = NULL;
2602 bool increased;
2603 uint64_t c;
2604 int r;
2605
2606 if (!u->cgroup_path)
2607 return 0;
2608
2609 r = cg_get_keyed_attribute("memory", u->cgroup_path, "memory.events", STRV_MAKE("oom_kill"), &oom_kill);
2610 if (r < 0)
2611 return log_unit_debug_errno(u, r, "Failed to read oom_kill field of memory.events cgroup attribute: %m");
2612
2613 r = safe_atou64(oom_kill, &c);
2614 if (r < 0)
2615 return log_unit_debug_errno(u, r, "Failed to parse oom_kill field: %m");
2616
2617 increased = c > u->oom_kill_last;
2618 u->oom_kill_last = c;
2619
2620 if (!increased)
2621 return 0;
2622
2623 log_struct(LOG_NOTICE,
2624 "MESSAGE_ID=" SD_MESSAGE_UNIT_OUT_OF_MEMORY_STR,
2625 LOG_UNIT_ID(u),
2626 LOG_UNIT_INVOCATION_ID(u),
2627 LOG_UNIT_MESSAGE(u, "A process of this unit has been killed by the OOM killer."));
2628
2629 if (UNIT_VTABLE(u)->notify_cgroup_oom)
2630 UNIT_VTABLE(u)->notify_cgroup_oom(u);
2631
2632 return 1;
2633}
2634
2635static int on_cgroup_oom_event(sd_event_source *s, void *userdata) {
2636 Manager *m = userdata;
2637 Unit *u;
2638 int r;
2639
2640 assert(s);
2641 assert(m);
2642
2643 u = m->cgroup_oom_queue;
2644 if (!u)
2645 return 0;
2646
2647 assert(u->in_cgroup_oom_queue);
2648 u->in_cgroup_oom_queue = false;
2649 LIST_REMOVE(cgroup_oom_queue, m->cgroup_oom_queue, u);
2650
2651 if (m->cgroup_oom_queue) {
2652 /* More stuff queued, let's make sure we remain enabled */
2653 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
2654 if (r < 0)
2655 log_debug_errno(r, "Failed to reenable cgroup oom event source, ignoring: %m");
2656 }
2657
2658 (void) unit_check_oom(u);
2659 return 0;
2660}
2661
2662static void unit_add_to_cgroup_oom_queue(Unit *u) {
2663 int r;
2664
2665 assert(u);
2666
2667 if (u->in_cgroup_oom_queue)
2668 return;
2669 if (!u->cgroup_path)
2670 return;
2671
2672 LIST_PREPEND(cgroup_oom_queue, u->manager->cgroup_oom_queue, u);
2673 u->in_cgroup_oom_queue = true;
2674
2675 /* Trigger the defer event */
2676 if (!u->manager->cgroup_oom_event_source) {
2677 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
2678
2679 r = sd_event_add_defer(u->manager->event, &s, on_cgroup_oom_event, u->manager);
2680 if (r < 0) {
2681 log_error_errno(r, "Failed to create cgroup oom event source: %m");
2682 return;
2683 }
2684
2685 r = sd_event_source_set_priority(s, SD_EVENT_PRIORITY_NORMAL-8);
2686 if (r < 0) {
2687 log_error_errno(r, "Failed to set priority of cgroup oom event source: %m");
2688 return;
2689 }
2690
2691 (void) sd_event_source_set_description(s, "cgroup-oom");
2692 u->manager->cgroup_oom_event_source = TAKE_PTR(s);
2693 }
2694
2695 r = sd_event_source_set_enabled(u->manager->cgroup_oom_event_source, SD_EVENT_ONESHOT);
2696 if (r < 0)
2697 log_error_errno(r, "Failed to enable cgroup oom event source: %m");
2698}
2699
efdb0237
LP
2700static int on_cgroup_inotify_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2701 Manager *m = userdata;
2702
2703 assert(s);
2704 assert(fd >= 0);
2705 assert(m);
2706
2707 for (;;) {
2708 union inotify_event_buffer buffer;
2709 struct inotify_event *e;
2710 ssize_t l;
2711
2712 l = read(fd, &buffer, sizeof(buffer));
2713 if (l < 0) {
47249640 2714 if (IN_SET(errno, EINTR, EAGAIN))
efdb0237
LP
2715 return 0;
2716
2717 return log_error_errno(errno, "Failed to read control group inotify events: %m");
2718 }
2719
2720 FOREACH_INOTIFY_EVENT(e, buffer, l) {
2721 Unit *u;
2722
2723 if (e->wd < 0)
2724 /* Queue overflow has no watch descriptor */
2725 continue;
2726
2727 if (e->mask & IN_IGNORED)
2728 /* The watch was just removed */
2729 continue;
2730
afcfaa69
LP
2731 /* Note that inotify might deliver events for a watch even after it was removed,
2732 * because it was queued before the removal. Let's ignore this here safely. */
2733
0bb814c2 2734 u = hashmap_get(m->cgroup_control_inotify_wd_unit, INT_TO_PTR(e->wd));
afcfaa69
LP
2735 if (u)
2736 unit_add_to_cgroup_empty_queue(u);
efdb0237 2737
afcfaa69
LP
2738 u = hashmap_get(m->cgroup_memory_inotify_wd_unit, INT_TO_PTR(e->wd));
2739 if (u)
2740 unit_add_to_cgroup_oom_queue(u);
efdb0237
LP
2741 }
2742 }
8e274523
LP
2743}
2744
17f14955
RG
2745static int cg_bpf_mask_supported(CGroupMask *ret) {
2746 CGroupMask mask = 0;
2747 int r;
2748
2749 /* BPF-based firewall */
2750 r = bpf_firewall_supported();
2751 if (r > 0)
2752 mask |= CGROUP_MASK_BPF_FIREWALL;
2753
084c7007
RG
2754 /* BPF-based device access control */
2755 r = bpf_devices_supported();
2756 if (r > 0)
2757 mask |= CGROUP_MASK_BPF_DEVICES;
2758
17f14955
RG
2759 *ret = mask;
2760 return 0;
2761}
2762
8e274523 2763int manager_setup_cgroup(Manager *m) {
9444b1f2 2764 _cleanup_free_ char *path = NULL;
10bd3e2e 2765 const char *scope_path;
efdb0237 2766 CGroupController c;
b4cccbc1 2767 int r, all_unified;
17f14955 2768 CGroupMask mask;
efdb0237 2769 char *e;
8e274523
LP
2770
2771 assert(m);
2772
35d2e7ec 2773 /* 1. Determine hierarchy */
efdb0237 2774 m->cgroup_root = mfree(m->cgroup_root);
9444b1f2 2775 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &m->cgroup_root);
23bbb0de
MS
2776 if (r < 0)
2777 return log_error_errno(r, "Cannot determine cgroup we are running in: %m");
8e274523 2778
efdb0237
LP
2779 /* Chop off the init scope, if we are already located in it */
2780 e = endswith(m->cgroup_root, "/" SPECIAL_INIT_SCOPE);
0d8c31ff 2781
efdb0237
LP
2782 /* LEGACY: Also chop off the system slice if we are in
2783 * it. This is to support live upgrades from older systemd
2784 * versions where PID 1 was moved there. Also see
2785 * cg_get_root_path(). */
463d0d15 2786 if (!e && MANAGER_IS_SYSTEM(m)) {
9444b1f2 2787 e = endswith(m->cgroup_root, "/" SPECIAL_SYSTEM_SLICE);
15c60e99 2788 if (!e)
efdb0237 2789 e = endswith(m->cgroup_root, "/system"); /* even more legacy */
0baf24dd 2790 }
efdb0237
LP
2791 if (e)
2792 *e = 0;
7ccfb64a 2793
7546145e
LP
2794 /* And make sure to store away the root value without trailing slash, even for the root dir, so that we can
2795 * easily prepend it everywhere. */
2796 delete_trailing_chars(m->cgroup_root, "/");
8e274523 2797
35d2e7ec 2798 /* 2. Show data */
9444b1f2 2799 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, NULL, &path);
23bbb0de
MS
2800 if (r < 0)
2801 return log_error_errno(r, "Cannot find cgroup mount point: %m");
8e274523 2802
415fc41c
TH
2803 r = cg_unified_flush();
2804 if (r < 0)
2805 return log_error_errno(r, "Couldn't determine if we are running in the unified hierarchy: %m");
5da38d07 2806
b4cccbc1 2807 all_unified = cg_all_unified();
d4c819ed
ZJS
2808 if (all_unified < 0)
2809 return log_error_errno(all_unified, "Couldn't determine whether we are in all unified mode: %m");
2810 if (all_unified > 0)
efdb0237 2811 log_debug("Unified cgroup hierarchy is located at %s.", path);
b4cccbc1 2812 else {
c22800e4 2813 r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
b4cccbc1
LP
2814 if (r < 0)
2815 return log_error_errno(r, "Failed to determine whether systemd's own controller is in unified mode: %m");
2816 if (r > 0)
2817 log_debug("Unified cgroup hierarchy is located at %s. Controllers are on legacy hierarchies.", path);
2818 else
2819 log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER_LEGACY ". File system hierarchy is at %s.", path);
2820 }
efdb0237 2821
09e24654
LP
2822 /* 3. Allocate cgroup empty defer event source */
2823 m->cgroup_empty_event_source = sd_event_source_unref(m->cgroup_empty_event_source);
2824 r = sd_event_add_defer(m->event, &m->cgroup_empty_event_source, on_cgroup_empty_event, m);
2825 if (r < 0)
2826 return log_error_errno(r, "Failed to create cgroup empty event source: %m");
2827
cbe83389
LP
2828 /* Schedule cgroup empty checks early, but after having processed service notification messages or
2829 * SIGCHLD signals, so that a cgroup running empty is always just the last safety net of
2830 * notification, and we collected the metadata the notification and SIGCHLD stuff offers first. */
09e24654
LP
2831 r = sd_event_source_set_priority(m->cgroup_empty_event_source, SD_EVENT_PRIORITY_NORMAL-5);
2832 if (r < 0)
2833 return log_error_errno(r, "Failed to set priority of cgroup empty event source: %m");
2834
2835 r = sd_event_source_set_enabled(m->cgroup_empty_event_source, SD_EVENT_OFF);
2836 if (r < 0)
2837 return log_error_errno(r, "Failed to disable cgroup empty event source: %m");
2838
2839 (void) sd_event_source_set_description(m->cgroup_empty_event_source, "cgroup-empty");
2840
2841 /* 4. Install notifier inotify object, or agent */
10bd3e2e 2842 if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0) {
c6c18be3 2843
09e24654 2844 /* In the unified hierarchy we can get cgroup empty notifications via inotify. */
efdb0237 2845
10bd3e2e
LP
2846 m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source);
2847 safe_close(m->cgroup_inotify_fd);
efdb0237 2848
10bd3e2e
LP
2849 m->cgroup_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
2850 if (m->cgroup_inotify_fd < 0)
2851 return log_error_errno(errno, "Failed to create control group inotify object: %m");
efdb0237 2852
10bd3e2e
LP
2853 r = sd_event_add_io(m->event, &m->cgroup_inotify_event_source, m->cgroup_inotify_fd, EPOLLIN, on_cgroup_inotify_event, m);
2854 if (r < 0)
2855 return log_error_errno(r, "Failed to watch control group inotify object: %m");
efdb0237 2856
cbe83389
LP
2857 /* Process cgroup empty notifications early. Note that when this event is dispatched it'll
2858 * just add the unit to a cgroup empty queue, hence let's run earlier than that. Also see
2859 * handling of cgroup agent notifications, for the classic cgroup hierarchy support. */
2860 r = sd_event_source_set_priority(m->cgroup_inotify_event_source, SD_EVENT_PRIORITY_NORMAL-9);
10bd3e2e
LP
2861 if (r < 0)
2862 return log_error_errno(r, "Failed to set priority of inotify event source: %m");
efdb0237 2863
10bd3e2e 2864 (void) sd_event_source_set_description(m->cgroup_inotify_event_source, "cgroup-inotify");
efdb0237 2865
611c4f8a 2866 } else if (MANAGER_IS_SYSTEM(m) && manager_owns_host_root_cgroup(m) && !MANAGER_IS_TEST_RUN(m)) {
efdb0237 2867
10bd3e2e
LP
2868 /* On the legacy hierarchy we only get notifications via cgroup agents. (Which isn't really reliable,
2869 * since it does not generate events when control groups with children run empty. */
8e274523 2870
10bd3e2e 2871 r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, SYSTEMD_CGROUP_AGENT_PATH);
23bbb0de 2872 if (r < 0)
10bd3e2e
LP
2873 log_warning_errno(r, "Failed to install release agent, ignoring: %m");
2874 else if (r > 0)
2875 log_debug("Installed release agent.");
2876 else if (r == 0)
2877 log_debug("Release agent already installed.");
2878 }
efdb0237 2879
09e24654 2880 /* 5. Make sure we are in the special "init.scope" unit in the root slice. */
10bd3e2e
LP
2881 scope_path = strjoina(m->cgroup_root, "/" SPECIAL_INIT_SCOPE);
2882 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, scope_path, 0);
aa77e234
MS
2883 if (r >= 0) {
2884 /* Also, move all other userspace processes remaining in the root cgroup into that scope. */
2885 r = cg_migrate(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, SYSTEMD_CGROUP_CONTROLLER, scope_path, 0);
2886 if (r < 0)
2887 log_warning_errno(r, "Couldn't move remaining userspace processes, ignoring: %m");
c6c18be3 2888
aa77e234
MS
2889 /* 6. And pin it, so that it cannot be unmounted */
2890 safe_close(m->pin_cgroupfs_fd);
2891 m->pin_cgroupfs_fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK);
2892 if (m->pin_cgroupfs_fd < 0)
2893 return log_error_errno(errno, "Failed to open pin file: %m");
0d8c31ff 2894
638cece4 2895 } else if (!MANAGER_IS_TEST_RUN(m))
aa77e234 2896 return log_error_errno(r, "Failed to create %s control group: %m", scope_path);
10bd3e2e 2897
09e24654 2898 /* 7. Always enable hierarchical support if it exists... */
638cece4 2899 if (!all_unified && !MANAGER_IS_TEST_RUN(m))
10bd3e2e 2900 (void) cg_set_attribute("memory", "/", "memory.use_hierarchy", "1");
c6c18be3 2901
17f14955 2902 /* 8. Figure out which controllers are supported */
efdb0237
LP
2903 r = cg_mask_supported(&m->cgroup_supported);
2904 if (r < 0)
2905 return log_error_errno(r, "Failed to determine supported controllers: %m");
17f14955
RG
2906
2907 /* 9. Figure out which bpf-based pseudo-controllers are supported */
2908 r = cg_bpf_mask_supported(&mask);
2909 if (r < 0)
2910 return log_error_errno(r, "Failed to determine supported bpf-based pseudo-controllers: %m");
2911 m->cgroup_supported |= mask;
2912
2913 /* 10. Log which controllers are supported */
efdb0237 2914 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++)
eee0a1e4 2915 log_debug("Controller '%s' supported: %s", cgroup_controller_to_string(c), yes_no(m->cgroup_supported & CGROUP_CONTROLLER_TO_MASK(c)));
9156e799 2916
a32360f1 2917 return 0;
8e274523
LP
2918}
2919
c6c18be3 2920void manager_shutdown_cgroup(Manager *m, bool delete) {
8e274523
LP
2921 assert(m);
2922
9444b1f2
LP
2923 /* We can't really delete the group, since we are in it. But
2924 * let's trim it. */
f6c63f6f 2925 if (delete && m->cgroup_root && m->test_run_flags != MANAGER_TEST_RUN_MINIMAL)
efdb0237
LP
2926 (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, false);
2927
09e24654
LP
2928 m->cgroup_empty_event_source = sd_event_source_unref(m->cgroup_empty_event_source);
2929
0bb814c2 2930 m->cgroup_control_inotify_wd_unit = hashmap_free(m->cgroup_control_inotify_wd_unit);
afcfaa69 2931 m->cgroup_memory_inotify_wd_unit = hashmap_free(m->cgroup_memory_inotify_wd_unit);
efdb0237
LP
2932
2933 m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source);
2934 m->cgroup_inotify_fd = safe_close(m->cgroup_inotify_fd);
8e274523 2935
03e334a1 2936 m->pin_cgroupfs_fd = safe_close(m->pin_cgroupfs_fd);
c6c18be3 2937
efdb0237 2938 m->cgroup_root = mfree(m->cgroup_root);
8e274523
LP
2939}
2940
4ad49000 2941Unit* manager_get_unit_by_cgroup(Manager *m, const char *cgroup) {
acb14d31 2942 char *p;
4ad49000 2943 Unit *u;
acb14d31
LP
2944
2945 assert(m);
2946 assert(cgroup);
acb14d31 2947
4ad49000
LP
2948 u = hashmap_get(m->cgroup_unit, cgroup);
2949 if (u)
2950 return u;
acb14d31 2951
8e70580b 2952 p = strdupa(cgroup);
acb14d31
LP
2953 for (;;) {
2954 char *e;
2955
2956 e = strrchr(p, '/');
efdb0237
LP
2957 if (!e || e == p)
2958 return hashmap_get(m->cgroup_unit, SPECIAL_ROOT_SLICE);
acb14d31
LP
2959
2960 *e = 0;
2961
4ad49000
LP
2962 u = hashmap_get(m->cgroup_unit, p);
2963 if (u)
2964 return u;
acb14d31
LP
2965 }
2966}
2967
b3ac818b 2968Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid) {
4ad49000 2969 _cleanup_free_ char *cgroup = NULL;
8e274523 2970
8c47c732
LP
2971 assert(m);
2972
62a76913 2973 if (!pid_is_valid(pid))
b3ac818b
LP
2974 return NULL;
2975
62a76913 2976 if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup) < 0)
b3ac818b
LP
2977 return NULL;
2978
2979 return manager_get_unit_by_cgroup(m, cgroup);
2980}
2981
2982Unit *manager_get_unit_by_pid(Manager *m, pid_t pid) {
62a76913 2983 Unit *u, **array;
b3ac818b
LP
2984
2985 assert(m);
2986
62a76913
LP
2987 /* Note that a process might be owned by multiple units, we return only one here, which is good enough for most
2988 * cases, though not strictly correct. We prefer the one reported by cgroup membership, as that's the most
2989 * relevant one as children of the process will be assigned to that one, too, before all else. */
2990
2991 if (!pid_is_valid(pid))
8c47c732
LP
2992 return NULL;
2993
2ca9d979 2994 if (pid == getpid_cached())
efdb0237
LP
2995 return hashmap_get(m->units, SPECIAL_INIT_SCOPE);
2996
62a76913 2997 u = manager_get_unit_by_pid_cgroup(m, pid);
5fe8876b
LP
2998 if (u)
2999 return u;
3000
62a76913 3001 u = hashmap_get(m->watch_pids, PID_TO_PTR(pid));
5fe8876b
LP
3002 if (u)
3003 return u;
3004
62a76913
LP
3005 array = hashmap_get(m->watch_pids, PID_TO_PTR(-pid));
3006 if (array)
3007 return array[0];
3008
3009 return NULL;
6dde1f33 3010}
4fbf50b3 3011
4ad49000
LP
3012int manager_notify_cgroup_empty(Manager *m, const char *cgroup) {
3013 Unit *u;
4fbf50b3 3014
4ad49000
LP
3015 assert(m);
3016 assert(cgroup);
4fbf50b3 3017
09e24654
LP
3018 /* Called on the legacy hierarchy whenever we get an explicit cgroup notification from the cgroup agent process
3019 * or from the --system instance */
3020
d8fdc620
LP
3021 log_debug("Got cgroup empty notification for: %s", cgroup);
3022
4ad49000 3023 u = manager_get_unit_by_cgroup(m, cgroup);
5ad096b3
LP
3024 if (!u)
3025 return 0;
b56c28c3 3026
09e24654
LP
3027 unit_add_to_cgroup_empty_queue(u);
3028 return 1;
5ad096b3
LP
3029}
3030
3031int unit_get_memory_current(Unit *u, uint64_t *ret) {
3032 _cleanup_free_ char *v = NULL;
3033 int r;
3034
3035 assert(u);
3036 assert(ret);
3037
2e4025c0 3038 if (!UNIT_CGROUP_BOOL(u, memory_accounting))
cf3b4be1
LP
3039 return -ENODATA;
3040
5ad096b3
LP
3041 if (!u->cgroup_path)
3042 return -ENODATA;
3043
1f73aa00 3044 /* The root cgroup doesn't expose this information, let's get it from /proc instead */
611c4f8a 3045 if (unit_has_host_root_cgroup(u))
c482724a 3046 return procfs_memory_get_used(ret);
1f73aa00 3047
efdb0237 3048 if ((u->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0)
5ad096b3
LP
3049 return -ENODATA;
3050
b4cccbc1
LP
3051 r = cg_all_unified();
3052 if (r < 0)
3053 return r;
3054 if (r > 0)
efdb0237 3055 r = cg_get_attribute("memory", u->cgroup_path, "memory.current", &v);
b4cccbc1
LP
3056 else
3057 r = cg_get_attribute("memory", u->cgroup_path, "memory.usage_in_bytes", &v);
5ad096b3
LP
3058 if (r == -ENOENT)
3059 return -ENODATA;
3060 if (r < 0)
3061 return r;
3062
3063 return safe_atou64(v, ret);
3064}
3065
03a7b521
LP
3066int unit_get_tasks_current(Unit *u, uint64_t *ret) {
3067 _cleanup_free_ char *v = NULL;
3068 int r;
3069
3070 assert(u);
3071 assert(ret);
3072
2e4025c0 3073 if (!UNIT_CGROUP_BOOL(u, tasks_accounting))
cf3b4be1
LP
3074 return -ENODATA;
3075
03a7b521
LP
3076 if (!u->cgroup_path)
3077 return -ENODATA;
3078
c36a69f4 3079 /* The root cgroup doesn't expose this information, let's get it from /proc instead */
611c4f8a 3080 if (unit_has_host_root_cgroup(u))
c36a69f4
LP
3081 return procfs_tasks_get_current(ret);
3082
1f73aa00
LP
3083 if ((u->cgroup_realized_mask & CGROUP_MASK_PIDS) == 0)
3084 return -ENODATA;
3085
03a7b521
LP
3086 r = cg_get_attribute("pids", u->cgroup_path, "pids.current", &v);
3087 if (r == -ENOENT)
3088 return -ENODATA;
3089 if (r < 0)
3090 return r;
3091
3092 return safe_atou64(v, ret);
3093}
3094
5ad096b3
LP
3095static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) {
3096 _cleanup_free_ char *v = NULL;
3097 uint64_t ns;
3098 int r;
3099
3100 assert(u);
3101 assert(ret);
3102
3103 if (!u->cgroup_path)
3104 return -ENODATA;
3105
1f73aa00 3106 /* The root cgroup doesn't expose this information, let's get it from /proc instead */
611c4f8a 3107 if (unit_has_host_root_cgroup(u))
1f73aa00
LP
3108 return procfs_cpu_get_usage(ret);
3109
f98c2585
CD
3110 /* Requisite controllers for CPU accounting are not enabled */
3111 if ((get_cpu_accounting_mask() & ~u->cgroup_realized_mask) != 0)
3112 return -ENODATA;
3113
92a99304
LP
3114 r = cg_all_unified();
3115 if (r < 0)
3116 return r;
b4cccbc1 3117 if (r > 0) {
66ebf6c0
TH
3118 _cleanup_free_ char *val = NULL;
3119 uint64_t us;
5ad096b3 3120
b734a4ff 3121 r = cg_get_keyed_attribute("cpu", u->cgroup_path, "cpu.stat", STRV_MAKE("usage_usec"), &val);
b734a4ff
LP
3122 if (IN_SET(r, -ENOENT, -ENXIO))
3123 return -ENODATA;
d742f4b5
LP
3124 if (r < 0)
3125 return r;
66ebf6c0
TH
3126
3127 r = safe_atou64(val, &us);
3128 if (r < 0)
3129 return r;
3130
3131 ns = us * NSEC_PER_USEC;
3132 } else {
66ebf6c0
TH
3133 r = cg_get_attribute("cpuacct", u->cgroup_path, "cpuacct.usage", &v);
3134 if (r == -ENOENT)
3135 return -ENODATA;
3136 if (r < 0)
3137 return r;
3138
3139 r = safe_atou64(v, &ns);
3140 if (r < 0)
3141 return r;
3142 }
5ad096b3
LP
3143
3144 *ret = ns;
3145 return 0;
3146}
3147
3148int unit_get_cpu_usage(Unit *u, nsec_t *ret) {
3149 nsec_t ns;
3150 int r;
3151
fe700f46
LP
3152 assert(u);
3153
3154 /* Retrieve the current CPU usage counter. This will subtract the CPU counter taken when the unit was
3155 * started. If the cgroup has been removed already, returns the last cached value. To cache the value, simply
3156 * call this function with a NULL return value. */
3157
2e4025c0 3158 if (!UNIT_CGROUP_BOOL(u, cpu_accounting))
cf3b4be1
LP
3159 return -ENODATA;
3160
5ad096b3 3161 r = unit_get_cpu_usage_raw(u, &ns);
fe700f46
LP
3162 if (r == -ENODATA && u->cpu_usage_last != NSEC_INFINITY) {
3163 /* If we can't get the CPU usage anymore (because the cgroup was already removed, for example), use our
3164 * cached value. */
3165
3166 if (ret)
3167 *ret = u->cpu_usage_last;
3168 return 0;
3169 }
5ad096b3
LP
3170 if (r < 0)
3171 return r;
3172
66ebf6c0
TH
3173 if (ns > u->cpu_usage_base)
3174 ns -= u->cpu_usage_base;
5ad096b3
LP
3175 else
3176 ns = 0;
3177
fe700f46
LP
3178 u->cpu_usage_last = ns;
3179 if (ret)
3180 *ret = ns;
3181
5ad096b3
LP
3182 return 0;
3183}
3184
906c06f6
DM
3185int unit_get_ip_accounting(
3186 Unit *u,
3187 CGroupIPAccountingMetric metric,
3188 uint64_t *ret) {
3189
6b659ed8 3190 uint64_t value;
906c06f6
DM
3191 int fd, r;
3192
3193 assert(u);
3194 assert(metric >= 0);
3195 assert(metric < _CGROUP_IP_ACCOUNTING_METRIC_MAX);
3196 assert(ret);
3197
2e4025c0 3198 if (!UNIT_CGROUP_BOOL(u, ip_accounting))
cf3b4be1
LP
3199 return -ENODATA;
3200
906c06f6
DM
3201 fd = IN_SET(metric, CGROUP_IP_INGRESS_BYTES, CGROUP_IP_INGRESS_PACKETS) ?
3202 u->ip_accounting_ingress_map_fd :
3203 u->ip_accounting_egress_map_fd;
906c06f6
DM
3204 if (fd < 0)
3205 return -ENODATA;
3206
3207 if (IN_SET(metric, CGROUP_IP_INGRESS_BYTES, CGROUP_IP_EGRESS_BYTES))
6b659ed8 3208 r = bpf_firewall_read_accounting(fd, &value, NULL);
906c06f6 3209 else
6b659ed8
LP
3210 r = bpf_firewall_read_accounting(fd, NULL, &value);
3211 if (r < 0)
3212 return r;
3213
3214 /* Add in additional metrics from a previous runtime. Note that when reexecing/reloading the daemon we compile
3215 * all BPF programs and maps anew, but serialize the old counters. When deserializing we store them in the
3216 * ip_accounting_extra[] field, and add them in here transparently. */
3217
3218 *ret = value + u->ip_accounting_extra[metric];
906c06f6
DM
3219
3220 return r;
3221}
3222
3223int unit_reset_cpu_accounting(Unit *u) {
5ad096b3
LP
3224 nsec_t ns;
3225 int r;
3226
3227 assert(u);
3228
fe700f46
LP
3229 u->cpu_usage_last = NSEC_INFINITY;
3230
5ad096b3
LP
3231 r = unit_get_cpu_usage_raw(u, &ns);
3232 if (r < 0) {
66ebf6c0 3233 u->cpu_usage_base = 0;
5ad096b3 3234 return r;
b56c28c3 3235 }
2633eb83 3236
66ebf6c0 3237 u->cpu_usage_base = ns;
4ad49000 3238 return 0;
4fbf50b3
LP
3239}
3240
906c06f6
DM
3241int unit_reset_ip_accounting(Unit *u) {
3242 int r = 0, q = 0;
3243
3244 assert(u);
3245
3246 if (u->ip_accounting_ingress_map_fd >= 0)
3247 r = bpf_firewall_reset_accounting(u->ip_accounting_ingress_map_fd);
3248
3249 if (u->ip_accounting_egress_map_fd >= 0)
3250 q = bpf_firewall_reset_accounting(u->ip_accounting_egress_map_fd);
3251
6b659ed8
LP
3252 zero(u->ip_accounting_extra);
3253
906c06f6
DM
3254 return r < 0 ? r : q;
3255}
3256
e7ab4d1a
LP
3257void unit_invalidate_cgroup(Unit *u, CGroupMask m) {
3258 assert(u);
3259
3260 if (!UNIT_HAS_CGROUP_CONTEXT(u))
3261 return;
3262
3263 if (m == 0)
3264 return;
3265
538b4852
TH
3266 /* always invalidate compat pairs together */
3267 if (m & (CGROUP_MASK_IO | CGROUP_MASK_BLKIO))
3268 m |= CGROUP_MASK_IO | CGROUP_MASK_BLKIO;
3269
7cce4fb7
LP
3270 if (m & (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT))
3271 m |= CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT;
3272
e00068e7 3273 if (FLAGS_SET(u->cgroup_invalidated_mask, m)) /* NOP? */
e7ab4d1a
LP
3274 return;
3275
e00068e7 3276 u->cgroup_invalidated_mask |= m;
91a6073e 3277 unit_add_to_cgroup_realize_queue(u);
e7ab4d1a
LP
3278}
3279
906c06f6
DM
3280void unit_invalidate_cgroup_bpf(Unit *u) {
3281 assert(u);
3282
3283 if (!UNIT_HAS_CGROUP_CONTEXT(u))
3284 return;
3285
17f14955 3286 if (u->cgroup_invalidated_mask & CGROUP_MASK_BPF_FIREWALL) /* NOP? */
906c06f6
DM
3287 return;
3288
17f14955 3289 u->cgroup_invalidated_mask |= CGROUP_MASK_BPF_FIREWALL;
91a6073e 3290 unit_add_to_cgroup_realize_queue(u);
906c06f6
DM
3291
3292 /* If we are a slice unit, we also need to put compile a new BPF program for all our children, as the IP access
3293 * list of our children includes our own. */
3294 if (u->type == UNIT_SLICE) {
3295 Unit *member;
3296 Iterator i;
eef85c4a 3297 void *v;
906c06f6 3298
eef85c4a 3299 HASHMAP_FOREACH_KEY(v, member, u->dependencies[UNIT_BEFORE], i) {
cb5e3bc3
CD
3300 if (UNIT_DEREF(member->slice) == u)
3301 unit_invalidate_cgroup_bpf(member);
906c06f6
DM
3302 }
3303 }
3304}
3305
1d9cc876
LP
3306bool unit_cgroup_delegate(Unit *u) {
3307 CGroupContext *c;
3308
3309 assert(u);
3310
3311 if (!UNIT_VTABLE(u)->can_delegate)
3312 return false;
3313
3314 c = unit_get_cgroup_context(u);
3315 if (!c)
3316 return false;
3317
3318 return c->delegate;
3319}
3320
e7ab4d1a
LP
3321void manager_invalidate_startup_units(Manager *m) {
3322 Iterator i;
3323 Unit *u;
3324
3325 assert(m);
3326
3327 SET_FOREACH(u, m->startup_units, i)
13c31542 3328 unit_invalidate_cgroup(u, CGROUP_MASK_CPU|CGROUP_MASK_IO|CGROUP_MASK_BLKIO);
e7ab4d1a
LP
3329}
3330
4ad49000
LP
3331static const char* const cgroup_device_policy_table[_CGROUP_DEVICE_POLICY_MAX] = {
3332 [CGROUP_AUTO] = "auto",
3333 [CGROUP_CLOSED] = "closed",
3334 [CGROUP_STRICT] = "strict",
3335};
4fbf50b3 3336
4ad49000 3337DEFINE_STRING_TABLE_LOOKUP(cgroup_device_policy, CGroupDevicePolicy);