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