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