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