Debian Codegraph @

src/core/cgroup.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "cgroup-util.h"
5 #include "core-forward.h"
6 #include "cpu-set-util.h"
7 #include "firewall-util.h"
8 #include "list.h"
9 #include "psi-util.h"
10
11 typedef struct CGroupTasksMax {
12 /* If scale == 0, just use value; otherwise, value / scale.
13 * See tasks_max_resolve(). */
14 uint64_t value;
15 uint64_t scale;
16 } CGroupTasksMax;
17
18 #define CGROUP_TASKS_MAX_UNSET ((const CGroupTasksMax) { .value = UINT64_MAX, .scale = 0 })
19
20 static inline bool cgroup_tasks_max_isset(const CGroupTasksMax *tasks_max) {
21 return tasks_max->value != UINT64_MAX || tasks_max->scale != 0;
22 }
23
24 uint64_t cgroup_tasks_max_resolve(const CGroupTasksMax *tasks_max);
25
26 typedef enum CGroupDevicePolicy {
27 /* When devices listed, will allow those, plus built-in ones, if none are listed will allow
28 * everything. */
29 CGROUP_DEVICE_POLICY_AUTO,
30
31 /* Everything forbidden, except built-in ones and listed ones. */
32 CGROUP_DEVICE_POLICY_CLOSED,
33
34 /* Everything forbidden, except for the listed devices */
35 CGROUP_DEVICE_POLICY_STRICT,
36
37 _CGROUP_DEVICE_POLICY_MAX,
38 _CGROUP_DEVICE_POLICY_INVALID = -EINVAL,
39 } CGroupDevicePolicy;
40
41 typedef enum FreezerAction {
42 FREEZER_FREEZE,
43 FREEZER_PARENT_FREEZE,
44 FREEZER_THAW,
45 FREEZER_PARENT_THAW,
46 _FREEZER_ACTION_MAX,
47 _FREEZER_ACTION_INVALID = -EINVAL,
48 } FreezerAction;
49
50 typedef enum CPUSetPartition {
51 CPUSET_PARTITION_MEMBER,
52 CPUSET_PARTITION_ROOT,
53 CPUSET_PARTITION_ISOLATED,
54 _CPUSET_PARTITION_MAX,
55 _CPUSET_PARTITION_INVALID = -EINVAL,
56 } CPUSetPartition;
57
58 typedef enum CGroupDevicePermissions {
59 /* We reuse the same bit meanings the kernel's BPF_DEVCG_ACC_xyz definitions use */
60 CGROUP_DEVICE_MKNOD = 1 << 0,
61 CGROUP_DEVICE_READ = 1 << 1,
62 CGROUP_DEVICE_WRITE = 1 << 2,
63 _CGROUP_DEVICE_PERMISSIONS_MAX = 1 << 3,
64 _CGROUP_DEVICE_PERMISSIONS_ALL = _CGROUP_DEVICE_PERMISSIONS_MAX - 1,
65 _CGROUP_DEVICE_PERMISSIONS_INVALID = -EINVAL,
66 } CGroupDevicePermissions;
67
68 typedef struct CGroupDeviceAllow {
69 LIST_FIELDS(CGroupDeviceAllow, device_allow);
70 char *path;
71 CGroupDevicePermissions permissions;
72 } CGroupDeviceAllow;
73
74 typedef struct CGroupIODeviceWeight {
75 LIST_FIELDS(CGroupIODeviceWeight, device_weights);
76 char *path;
77 uint64_t weight;
78 } CGroupIODeviceWeight;
79
80 typedef struct CGroupIODeviceLimit {
81 LIST_FIELDS(CGroupIODeviceLimit, device_limits);
82 char *path;
83 uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX];
84 } CGroupIODeviceLimit;
85
86 typedef struct CGroupIODeviceLatency {
87 LIST_FIELDS(CGroupIODeviceLatency, device_latencies);
88 char *path;
89 usec_t target_usec;
90 } CGroupIODeviceLatency;
91
92 typedef struct CGroupBPFForeignProgram {
93 LIST_FIELDS(CGroupBPFForeignProgram, programs);
94 uint32_t attach_type;
95 char *bpffs_path;
96 } CGroupBPFForeignProgram;
97
98 typedef struct CGroupSocketBindItem {
99 LIST_FIELDS(CGroupSocketBindItem, socket_bind_items);
100 int address_family;
101 int ip_protocol;
102 uint16_t nr_ports;
103 uint16_t port_min;
104 } CGroupSocketBindItem;
105
106 typedef enum CGroupPressureWatch {
107 CGROUP_PRESSURE_WATCH_NO, /* → tells the service payload explicitly not to watch for pressure */
108 CGROUP_PRESSURE_WATCH_YES,
109 CGROUP_PRESSURE_WATCH_AUTO, /* → on if relevant accounting is on anyway for the unit, otherwise off */
110 CGROUP_PRESSURE_WATCH_SKIP, /* → doesn't set up pressure watch, but also doesn't explicitly tell payload to avoid it */
111 _CGROUP_PRESSURE_WATCH_MAX,
112 _CGROUP_PRESSURE_WATCH_INVALID = -EINVAL,
113 } CGroupPressureWatch;
114
115 typedef struct CGroupPressure {
116 CGroupPressureWatch watch;
117 usec_t threshold_usec;
118 } CGroupPressure;
119
120 /* The user-supplied cgroup-related configuration options. This remains mostly immutable while the service
121 * manager is running (except for an occasional SetProperties() configuration change), outside of reload
122 * cycles. */
123 typedef struct CGroupContext {
124 bool io_accounting;
125 bool memory_accounting;
126 bool tasks_accounting;
127 bool ip_accounting;
128
129 /* Configures the memory.oom.group attribute (on unified) */
130 bool memory_oom_group;
131
132 bool delegate;
133 CGroupMask delegate_controllers;
134 CGroupMask disable_controllers;
135 char *delegate_subgroup;
136
137 /* For unified hierarchy */
138 uint64_t cpu_weight;
139 uint64_t startup_cpu_weight;
140 usec_t cpu_quota_per_sec_usec;
141 usec_t cpu_quota_period_usec;
142
143 CPUSet cpuset_cpus;
144 CPUSet startup_cpuset_cpus;
145 CPUSet cpuset_mems;
146 CPUSet startup_cpuset_mems;
147 CPUSetPartition cpuset_partition;
148
149 uint64_t io_weight;
150 uint64_t startup_io_weight;
151 LIST_HEAD(CGroupIODeviceWeight, io_device_weights);
152 LIST_HEAD(CGroupIODeviceLimit, io_device_limits);
153 LIST_HEAD(CGroupIODeviceLatency, io_device_latencies);
154
155 uint64_t memory_min;
156 uint64_t memory_low;
157 uint64_t startup_memory_low;
158 uint64_t memory_high;
159 uint64_t startup_memory_high;
160 uint64_t memory_max;
161 uint64_t startup_memory_max;
162 uint64_t memory_swap_max;
163 uint64_t startup_memory_swap_max;
164 uint64_t memory_zswap_max;
165 uint64_t startup_memory_zswap_max;
166
167 bool startup_memory_low_set:1;
168 bool startup_memory_high_set:1;
169 bool startup_memory_max_set:1;
170 bool startup_memory_swap_max_set:1;
171 bool startup_memory_zswap_max_set:1;
172
173 bool memory_zswap_writeback;
174
175 Set *ip_address_allow;
176 Set *ip_address_deny;
177 /* These two flags indicate that redundant entries have been removed from
178 * ip_address_allow/ip_address_deny, i.e. in_addr_prefixes_reduce() has already been called. */
179 bool ip_address_allow_reduced;
180 bool ip_address_deny_reduced;
181
182 char **ip_filters_ingress;
183 char **ip_filters_egress;
184 LIST_HEAD(CGroupBPFForeignProgram, bpf_foreign_programs);
185
186 Set *restrict_network_interfaces;
187 bool restrict_network_interfaces_is_allow_list;
188
189 CGroupDevicePolicy device_policy;
190 LIST_HEAD(CGroupDeviceAllow, device_allow);
191
192 LIST_HEAD(CGroupSocketBindItem, socket_bind_allow);
193 LIST_HEAD(CGroupSocketBindItem, socket_bind_deny);
194
195 char *bind_network_interface;
196
197 /* Common */
198 CGroupTasksMax tasks_max;
199
200 /* Settings for systemd-oomd */
201 ManagedOOMMode moom_swap;
202 ManagedOOMMode moom_mem_pressure;
203 uint32_t moom_mem_pressure_limit; /* Normalized to 2^32-1 == 100% */
204 usec_t moom_mem_pressure_duration_usec;
205 ManagedOOMPreference moom_preference;
206 char **moom_rules;
207
208 /* Pressure logic */
209 CGroupPressure pressure[_PRESSURE_RESOURCE_MAX];
210
211 NFTSetContext nft_set_context;
212
213 /* Forward coredumps for processes that crash within this cgroup.
214 * Requires 'delegate' to also be true. */
215 bool coredump_receive;
216 } CGroupContext;
217
218 /* Used when querying IP accounting data */
219 typedef enum CGroupIPAccountingMetric {
220 CGROUP_IP_INGRESS_BYTES,
221 CGROUP_IP_INGRESS_PACKETS,
222 CGROUP_IP_EGRESS_BYTES,
223 CGROUP_IP_EGRESS_PACKETS,
224 _CGROUP_IP_ACCOUNTING_METRIC_MAX,
225 _CGROUP_IP_ACCOUNTING_METRIC_INVALID = -EINVAL,
226 } CGroupIPAccountingMetric;
227
228 /* Used when querying IO accounting data */
229 typedef enum CGroupIOAccountingMetric {
230 CGROUP_IO_READ_BYTES,
231 CGROUP_IO_WRITE_BYTES,
232 CGROUP_IO_READ_OPERATIONS,
233 CGROUP_IO_WRITE_OPERATIONS,
234 _CGROUP_IO_ACCOUNTING_METRIC_MAX,
235 _CGROUP_IO_ACCOUNTING_METRIC_INVALID = -EINVAL,
236 } CGroupIOAccountingMetric;
237
238 typedef enum CGroupMemoryAccountingMetric {
239 CGROUP_MEMORY_PEAK,
240 CGROUP_MEMORY_SWAP_PEAK,
241 /* We cache the above attributes, so that they can be fetched even after the cgroup is gone, e.g.
242 * when systemd-run exits. */
243 _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST = CGROUP_MEMORY_SWAP_PEAK,
244
245 /* These attributes are transient, so no need for caching. */
246 CGROUP_MEMORY_CURRENT,
247 CGROUP_MEMORY_SWAP_CURRENT,
248 CGROUP_MEMORY_ZSWAP_CURRENT,
249
250 _CGROUP_MEMORY_ACCOUNTING_METRIC_MAX,
251 _CGROUP_MEMORY_ACCOUNTING_METRIC_INVALID = -EINVAL,
252 } CGroupMemoryAccountingMetric;
253
254 /* Used for limits whose value sets have infimum */
255 typedef enum CGroupLimitType {
256 CGROUP_LIMIT_MEMORY_MAX,
257 CGROUP_LIMIT_MEMORY_HIGH,
258 CGROUP_LIMIT_TASKS_MAX,
259 _CGROUP_LIMIT_TYPE_MAX,
260 _CGROUP_LIMIT_INVALID = -EINVAL,
261 } CGroupLimitType;
262
263 /* The dynamic, regular updated information about a unit that as a realized cgroup. This is only allocated when a unit is first realized */
264 typedef struct CGroupRuntime {
265 /* Where the cpu.stat or cpuacct.usage was at the time the unit was started */
266 nsec_t cpu_usage_base;
267 nsec_t cpu_usage_last; /* the most recently read value */
268
269 /* Most recently read value of memory accounting metrics */
270 uint64_t memory_accounting_last[_CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST + 1];
271
272 /* The current counter of OOM kills initiated by systemd-oomd */
273 uint64_t managed_oom_kill_last;
274
275 /* The current counter of the oom_kill field in the memory.events cgroup attribute */
276 uint64_t oom_kill_last;
277
278 /* Where the io.stat data was at the time the unit was started */
279 uint64_t io_accounting_base[_CGROUP_IO_ACCOUNTING_METRIC_MAX];
280 uint64_t io_accounting_last[_CGROUP_IO_ACCOUNTING_METRIC_MAX]; /* the most recently read value */
281
282 /* Counterparts in the cgroup filesystem */
283 char *cgroup_path;
284 uint64_t cgroup_id;
285 CGroupMask cgroup_realized_mask; /* In which hierarchies does this unit's cgroup exist? (only relevant on cgroup v1) */
286 CGroupMask cgroup_enabled_mask; /* Which controllers are enabled (or more correctly: enabled for the children) for this unit's cgroup? (only relevant on cgroup v2) */
287 CGroupMask cgroup_invalidated_mask; /* A mask specifying controllers which shall be considered invalidated, and require re-realization */
288 CGroupMask cgroup_members_mask; /* A cache for the controllers required by all children of this cgroup (only relevant for slice units) */
289
290 /* Inotify watch descriptors for watching cgroup.events and memory.events on cgroupv2 */
291 int cgroup_control_inotify_wd;
292 int cgroup_memory_inotify_wd;
293
294 /* Device Controller BPF program */
295 BPFProgram *bpf_device_control_installed;
296
297 /* IP BPF Firewalling/accounting */
298 int ip_accounting_ingress_map_fd;
299 int ip_accounting_egress_map_fd;
300 uint64_t ip_accounting_extra[_CGROUP_IP_ACCOUNTING_METRIC_MAX];
301
302 int ipv4_allow_map_fd;
303 int ipv6_allow_map_fd;
304 int ipv4_deny_map_fd;
305 int ipv6_deny_map_fd;
306 BPFProgram *ip_bpf_ingress, *ip_bpf_ingress_installed;
307 BPFProgram *ip_bpf_egress, *ip_bpf_egress_installed;
308
309 Set *ip_bpf_custom_ingress;
310 Set *ip_bpf_custom_ingress_installed;
311 Set *ip_bpf_custom_egress;
312 Set *ip_bpf_custom_egress_installed;
313
314 /* BPF programs managed (e.g. loaded to kernel) by an entity external to systemd,
315 * attached to unit cgroup by provided program fd and attach type. */
316 Hashmap *bpf_foreign_by_key;
317
318 FDSet *initial_socket_bind_link_fds;
319 #if BPF_FRAMEWORK
320 /* BPF links to BPF programs attached to cgroup/bind{4|6} hooks and
321 * responsible for allowing or denying a unit to bind(2) to a socket
322 * address. */
323 struct bpf_link *ipv4_socket_bind_link;
324 struct bpf_link *ipv6_socket_bind_link;
325 #endif
326
327 FDSet *initial_restrict_ifaces_link_fds;
328 #if BPF_FRAMEWORK
329 struct bpf_link *restrict_ifaces_ingress_bpf_link;
330 struct bpf_link *restrict_ifaces_egress_bpf_link;
331 #endif
332
333 #if BPF_FRAMEWORK
334 /* BPF link to BPF programs attached to cgroup/sock_create hooks and
335 * responsible for binding created sockets to a given VRF interface. */
336 struct bpf_link *bpf_bind_network_interface_link;
337 #endif
338 int initial_bind_network_interface_link_fd;
339
340 bool cgroup_members_mask_valid:1;
341
342 /* Reset cgroup accounting next time we fork something off */
343 bool reset_accounting:1;
344
345 /* Whether we warned about clamping the CPU quota period */
346 bool warned_clamping_cpu_quota_period:1;
347
348 int deserialized_cgroup_realized; /* tristate, for backwards compat */
349 } CGroupRuntime;
350
351 uint64_t cgroup_context_cpu_weight(CGroupContext *c, ManagerState state);
352
353 usec_t cgroup_cpu_adjust_period(usec_t period, usec_t quota, usec_t resolution, usec_t max_period);
354
355 void cgroup_context_init(CGroupContext *c);
356 void cgroup_context_done(CGroupContext *c);
357
358 void cgroup_context_dump(Unit *u, FILE* f, const char *prefix);
359 void cgroup_context_dump_socket_bind_item(const CGroupSocketBindItem *item, FILE *f);
360 void cgroup_context_dump_socket_bind_items(const CGroupSocketBindItem *items, FILE *f);
361
362 void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
363 void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w);
364 void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l);
365 void cgroup_context_free_io_device_latency(CGroupContext *c, CGroupIODeviceLatency *l);
366 void cgroup_context_remove_bpf_foreign_program(CGroupContext *c, CGroupBPFForeignProgram *p);
367 void cgroup_context_remove_socket_bind(CGroupSocketBindItem **head);
368
369 static inline bool cgroup_context_want_pressure(const CGroupContext *c, PressureResource t) {
370 assert(c);
371 assert(t >= 0 && t < _PRESSURE_RESOURCE_MAX);
372
373 if (c->pressure[t].watch == CGROUP_PRESSURE_WATCH_YES)
374 return true;
375
376 if (c->pressure[t].watch != CGROUP_PRESSURE_WATCH_AUTO)
377 return false;
378
379 switch (t) {
380
381 case PRESSURE_MEMORY:
382 return c->memory_accounting;
383
384 case PRESSURE_CPU:
385 return c->cpu_weight != CGROUP_WEIGHT_INVALID ||
386 c->startup_cpu_weight != CGROUP_WEIGHT_INVALID ||
387 c->cpu_quota_per_sec_usec != USEC_INFINITY;
388
389 case PRESSURE_IO:
390 return c->io_accounting ||
391 c->io_weight != CGROUP_WEIGHT_INVALID ||
392 c->startup_io_weight != CGROUP_WEIGHT_INVALID ||
393 c->io_device_weights ||
394 c->io_device_latencies ||
395 c->io_device_limits;
396
397 default:
398 assert_not_reached();
399 }
400 }
401
402 static inline bool cgroup_context_has_device_policy(const CGroupContext *c) {
403 assert(c);
404
405 return c->device_policy != CGROUP_DEVICE_POLICY_AUTO ||
406 c->device_allow;
407 }
408
409 int cgroup_context_add_device_allow(CGroupContext *c, const char *dev, CGroupDevicePermissions p);
410 int cgroup_context_add_or_update_device_allow(CGroupContext *c, const char *dev, CGroupDevicePermissions p);
411 int cgroup_context_add_bpf_foreign_program(CGroupContext *c, uint32_t attach_type, const char *path);
412
413 CGroupMask unit_get_own_mask(Unit *u);
414 CGroupMask unit_get_delegate_mask(Unit *u);
415 CGroupMask unit_get_members_mask(Unit *u);
416 CGroupMask unit_get_siblings_mask(Unit *u);
417
418 CGroupMask unit_get_target_mask(Unit *u);
419 CGroupMask unit_get_enable_mask(Unit *u);
420
421 void unit_invalidate_cgroup_members_masks(Unit *u);
422
423 void unit_add_family_to_cgroup_realize_queue(Unit *u);
424
425 int unit_get_cgroup_path_with_fallback(const Unit *u, char **ret);
426
427 int unit_realize_cgroup(Unit *u);
428 void unit_prune_cgroup(Unit *u);
429 void unit_add_to_cgroup_realize_queue(Unit *u);
430
431 int unit_cgroup_is_empty(Unit *u);
432 void unit_release_cgroup(Unit *u, bool drop_cgroup_runtime);
433
434 int unit_check_oomd_kill(Unit *u);
435 int unit_check_oom(Unit *u);
436
437 int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path);
438 int unit_remove_subcgroup(Unit *u, const char *suffix_path);
439
440 int manager_setup_cgroup(Manager *m);
441 void manager_shutdown_cgroup(Manager *m, bool delete);
442
443 unsigned manager_dispatch_cgroup_realize_queue(Manager *m);
444
445 Unit* manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
446 Unit* manager_get_unit_by_pidref_cgroup(Manager *m, const PidRef *pid);
447 Unit* manager_get_unit_by_pidref_watching(Manager *m, const PidRef *pid);
448 Unit* manager_get_unit_by_pidref(Manager *m, PidRef *pid);
449
450 int unit_search_main_pid(Unit *u, PidRef *ret);
451
452 int unit_get_memory_available(Unit *u, uint64_t *ret);
453 int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uint64_t *ret);
454 int unit_get_tasks_current(Unit *u, uint64_t *ret);
455 int unit_get_cpu_usage(Unit *u, nsec_t *ret);
456 int unit_get_io_accounting(Unit *u, CGroupIOAccountingMetric metric, uint64_t *ret);
457 int unit_get_ip_accounting(Unit *u, CGroupIPAccountingMetric metric, uint64_t *ret);
458 int unit_get_effective_limit(Unit *u, CGroupLimitType type, uint64_t *ret);
459
460 int unit_reset_accounting(Unit *u);
461
462 #define UNIT_CGROUP_BOOL(u, name) \
463 ({ \
464 CGroupContext *cc = unit_get_cgroup_context(u); \
465 cc ? cc->name : false; \
466 })
467
468 bool manager_owns_host_root_cgroup(Manager *m);
469 bool unit_has_host_root_cgroup(const Unit *u);
470
471 bool unit_has_startup_cgroup_constraints(Unit *u);
472
473 bool unit_invalidate_cgroup(Unit *u, CGroupMask m);
474 void unit_invalidate_cgroup_bpf_firewall(Unit *u);
475
476 void manager_invalidate_startup_units(Manager *m);
477
478 DECLARE_STRING_TABLE_LOOKUP(cgroup_device_policy, CGroupDevicePolicy);
479
480 void unit_cgroup_catchup(Unit *u);
481
482 bool unit_cgroup_delegate(Unit *u);
483
484 void unit_cgroup_disable_all_controllers(Unit *u);
485
486 int unit_get_cpuset(Unit *u, CPUSet *cpus, const char *name);
487
488 int unit_cgroup_freezer_action(Unit *u, FreezerAction action);
489
490 DECLARE_STRING_TABLE_LOOKUP(freezer_action, FreezerAction);
491 DECLARE_STRING_TABLE_LOOKUP(cpuset_partition, CPUSetPartition);
492
493 CGroupRuntime* cgroup_runtime_new(void);
494 CGroupRuntime* cgroup_runtime_free(CGroupRuntime *crt);
495 DEFINE_TRIVIAL_CLEANUP_FUNC(CGroupRuntime*, cgroup_runtime_free);
496
497 int cgroup_runtime_serialize(Unit *u, FILE *f, FDSet *fds);
498 int cgroup_runtime_deserialize_one(Unit *u, const char *key, const char *value, FDSet *fds);
499
500 DECLARE_STRING_TABLE_LOOKUP(cgroup_pressure_watch, CGroupPressureWatch);
501
502 DECLARE_STRING_TABLE_LOOKUP(cgroup_device_permissions, CGroupDevicePermissions);
503
504 DECLARE_STRING_TABLE_LOOKUP(cgroup_ip_accounting_metric, CGroupIPAccountingMetric);
505
506 DECLARE_STRING_TABLE_LOOKUP(cgroup_io_accounting_metric, CGroupIOAccountingMetric);
507
508 DECLARE_STRING_TABLE_LOOKUP(cgroup_effective_limit_type, CGroupLimitType);
509
510 DECLARE_STRING_TABLE_LOOKUP(cgroup_memory_accounting_metric, CGroupMemoryAccountingMetric);