mirror of
https://github.com/Dasharo/systemd.git
synced 2026-03-06 15:02:31 -08:00
Merge pull request #10894 from poettering/root-cgroup-fix
A multitude of cgroup fixes
This commit is contained in:
@@ -2368,9 +2368,9 @@ int cg_mask_supported(CGroupMask *ret) {
|
||||
CGroupMask mask;
|
||||
int r;
|
||||
|
||||
/* Determines the mask of supported cgroup controllers. Only
|
||||
* includes controllers we can make sense of and that are
|
||||
* actually accessible. */
|
||||
/* Determines the mask of supported cgroup controllers. Only includes controllers we can make sense of and that
|
||||
* are actually accessible. Only covers real controllers, i.e. not the CGROUP_CONTROLLER_BPF_xyz
|
||||
* pseudo-controllers. */
|
||||
|
||||
r = cg_all_unified();
|
||||
if (r < 0)
|
||||
@@ -2595,22 +2595,45 @@ int cg_unified_flush(void) {
|
||||
return cg_unified_update();
|
||||
}
|
||||
|
||||
int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
|
||||
int cg_enable_everywhere(
|
||||
CGroupMask supported,
|
||||
CGroupMask mask,
|
||||
const char *p,
|
||||
CGroupMask *ret_result_mask) {
|
||||
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
_cleanup_free_ char *fs = NULL;
|
||||
CGroupController c;
|
||||
CGroupMask ret = 0;
|
||||
int r;
|
||||
|
||||
assert(p);
|
||||
|
||||
if (supported == 0)
|
||||
if (supported == 0) {
|
||||
if (ret_result_mask)
|
||||
*ret_result_mask = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = cg_all_unified();
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) /* on the legacy hiearchy there's no joining of controllers defined */
|
||||
if (r == 0) {
|
||||
/* On the legacy hiearchy there's no concept of "enabling" controllers in cgroups defined. Let's claim
|
||||
* complete success right away. (If you wonder why we return the full mask here, rather than zero: the
|
||||
* caller tends to use the returned mask later on to compare if all controllers where properly joined,
|
||||
* and if not requeues realization. This use is the primary purpose of the return value, hence let's
|
||||
* minimize surprises here and reduce triggers for re-realization by always saying we fully
|
||||
* succeeded.) */
|
||||
if (ret_result_mask)
|
||||
*ret_result_mask = mask & supported & CGROUP_MASK_V2; /* If you wonder why we mask this with
|
||||
* CGROUP_MASK_V2: The 'supported' mask
|
||||
* might contain pure-V1 or BPF
|
||||
* controllers, and we never want to
|
||||
* claim that we could enable those with
|
||||
* cgroup.subtree_control */
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
|
||||
if (r < 0)
|
||||
@@ -2635,20 +2658,48 @@ int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
|
||||
|
||||
if (!f) {
|
||||
f = fopen(fs, "we");
|
||||
if (!f) {
|
||||
log_debug_errno(errno, "Failed to open cgroup.subtree_control file of %s: %m", p);
|
||||
break;
|
||||
}
|
||||
if (!f)
|
||||
return log_debug_errno(errno, "Failed to open cgroup.subtree_control file of %s: %m", p);
|
||||
}
|
||||
|
||||
r = write_string_stream(f, s, WRITE_STRING_FILE_DISABLE_BUFFER);
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
|
||||
log_debug_errno(r, "Failed to %s controller %s for %s (%s): %m",
|
||||
FLAGS_SET(mask, bit) ? "enable" : "disable", n, p, fs);
|
||||
clearerr(f);
|
||||
|
||||
/* If we can't turn off a controller, leave it on in the reported resulting mask. This
|
||||
* happens for example when we attempt to turn off a controller up in the tree that is
|
||||
* used down in the tree. */
|
||||
if (!FLAGS_SET(mask, bit) && r == -EBUSY) /* You might wonder why we check for EBUSY
|
||||
* only here, and not follow the same logic
|
||||
* for other errors such as EINVAL or
|
||||
* EOPNOTSUPP or anything else. That's
|
||||
* because EBUSY indicates that the
|
||||
* controllers is currently enabled and
|
||||
* cannot be disabled because something down
|
||||
* the hierarchy is still using it. Any other
|
||||
* error most likely means something like "I
|
||||
* never heard of this controller" or
|
||||
* similar. In the former case it's hence
|
||||
* safe to assume the controller is still on
|
||||
* after the failed operation, while in the
|
||||
* latter case it's safer to assume the
|
||||
* controller is unknown and hence certainly
|
||||
* not enabled. */
|
||||
ret |= bit;
|
||||
} else {
|
||||
/* Otherwise, if we managed to turn on a controller, set the bit reflecting that. */
|
||||
if (FLAGS_SET(mask, bit))
|
||||
ret |= bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Let's return the precise set of controllers now enabled for the cgroup. */
|
||||
if (ret_result_mask)
|
||||
*ret_result_mask = ret;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -245,7 +245,7 @@ int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_m
|
||||
int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t callback, void *userdata);
|
||||
int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t callback, void *userdata);
|
||||
int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root);
|
||||
int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p);
|
||||
int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p, CGroupMask *ret_result_mask);
|
||||
|
||||
int cg_mask_supported(CGroupMask *ret);
|
||||
int cg_mask_from_string(const char *s, CGroupMask *ret);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -137,8 +137,6 @@ void cgroup_context_init(CGroupContext *c);
|
||||
void cgroup_context_done(CGroupContext *c);
|
||||
void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
|
||||
|
||||
CGroupMask cgroup_context_get_mask(CGroupContext *c);
|
||||
|
||||
void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
|
||||
void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w);
|
||||
void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l);
|
||||
@@ -153,14 +151,12 @@ CGroupMask unit_get_delegate_mask(Unit *u);
|
||||
CGroupMask unit_get_members_mask(Unit *u);
|
||||
CGroupMask unit_get_siblings_mask(Unit *u);
|
||||
CGroupMask unit_get_subtree_mask(Unit *u);
|
||||
|
||||
CGroupMask unit_get_target_mask(Unit *u);
|
||||
CGroupMask unit_get_enable_mask(Unit *u);
|
||||
|
||||
bool unit_get_needs_bpf_firewall(Unit *u);
|
||||
CGroupMask unit_get_bpf_mask(Unit *u);
|
||||
void unit_invalidate_cgroup_members_masks(Unit *u);
|
||||
|
||||
void unit_update_cgroup_members_masks(Unit *u);
|
||||
void unit_add_to_cgroup_realize_queue(Unit *u);
|
||||
|
||||
const char *unit_get_realized_cgroup_path(Unit *u, CGroupMask mask);
|
||||
char *unit_default_cgroup_path(Unit *u);
|
||||
@@ -204,8 +200,8 @@ int unit_reset_ip_accounting(Unit *u);
|
||||
cc ? cc->name : false; \
|
||||
})
|
||||
|
||||
bool manager_owns_root_cgroup(Manager *m);
|
||||
bool unit_has_root_cgroup(Unit *u);
|
||||
bool manager_owns_host_root_cgroup(Manager *m);
|
||||
bool unit_has_host_root_cgroup(Unit *u);
|
||||
|
||||
int manager_notify_cgroup_empty(Manager *m, const char *group);
|
||||
|
||||
|
||||
@@ -145,7 +145,7 @@ int bus_mount_set_property(
|
||||
int bus_mount_commit_properties(Unit *u) {
|
||||
assert(u);
|
||||
|
||||
unit_update_cgroup_members_masks(u);
|
||||
unit_invalidate_cgroup_members_masks(u);
|
||||
unit_realize_cgroup(u);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -186,7 +186,7 @@ int bus_scope_set_property(
|
||||
int bus_scope_commit_properties(Unit *u) {
|
||||
assert(u);
|
||||
|
||||
unit_update_cgroup_members_masks(u);
|
||||
unit_invalidate_cgroup_members_masks(u);
|
||||
unit_realize_cgroup(u);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -424,7 +424,7 @@ int bus_service_set_property(
|
||||
int bus_service_commit_properties(Unit *u) {
|
||||
assert(u);
|
||||
|
||||
unit_update_cgroup_members_masks(u);
|
||||
unit_invalidate_cgroup_members_masks(u);
|
||||
unit_realize_cgroup(u);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -28,7 +28,7 @@ int bus_slice_set_property(
|
||||
int bus_slice_commit_properties(Unit *u) {
|
||||
assert(u);
|
||||
|
||||
unit_update_cgroup_members_masks(u);
|
||||
unit_invalidate_cgroup_members_masks(u);
|
||||
unit_realize_cgroup(u);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -461,7 +461,7 @@ int bus_socket_set_property(
|
||||
int bus_socket_commit_properties(Unit *u) {
|
||||
assert(u);
|
||||
|
||||
unit_update_cgroup_members_masks(u);
|
||||
unit_invalidate_cgroup_members_masks(u);
|
||||
unit_realize_cgroup(u);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -63,7 +63,7 @@ int bus_swap_set_property(
|
||||
int bus_swap_commit_properties(Unit *u) {
|
||||
assert(u);
|
||||
|
||||
unit_update_cgroup_members_masks(u);
|
||||
unit_invalidate_cgroup_members_masks(u);
|
||||
unit_realize_cgroup(u);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -330,7 +330,7 @@ static void slice_enumerate_perpetual(Manager *m) {
|
||||
assert(m);
|
||||
|
||||
r = slice_make_perpetual(m, SPECIAL_ROOT_SLICE, &u);
|
||||
if (r >= 0 && manager_owns_root_cgroup(m)) {
|
||||
if (r >= 0 && manager_owns_host_root_cgroup(m)) {
|
||||
Slice *s = SLICE(u);
|
||||
|
||||
/* If we are managing the root cgroup then this means our root slice covers the whole system, which
|
||||
|
||||
@@ -570,6 +570,14 @@ void unit_free(Unit *u) {
|
||||
if (!u)
|
||||
return;
|
||||
|
||||
if (UNIT_ISSET(u->slice)) {
|
||||
/* A unit is being dropped from the tree, make sure our parent slice recalculates the member mask */
|
||||
unit_invalidate_cgroup_members_masks(UNIT_DEREF(u->slice));
|
||||
|
||||
/* And make sure the parent is realized again, updating cgroup memberships */
|
||||
unit_add_to_cgroup_realize_queue(UNIT_DEREF(u->slice));
|
||||
}
|
||||
|
||||
u->transient_file = safe_fclose(u->transient_file);
|
||||
|
||||
if (!MANAGER_IS_RELOADING(u->manager))
|
||||
@@ -1155,17 +1163,20 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
|
||||
(void) cg_mask_to_string(u->cgroup_realized_mask, &s);
|
||||
fprintf(f, "%s\tCGroup realized mask: %s\n", prefix, strnull(s));
|
||||
}
|
||||
|
||||
if (u->cgroup_enabled_mask != 0) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
(void) cg_mask_to_string(u->cgroup_enabled_mask, &s);
|
||||
fprintf(f, "%s\tCGroup enabled mask: %s\n", prefix, strnull(s));
|
||||
}
|
||||
|
||||
m = unit_get_own_mask(u);
|
||||
if (m != 0) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
(void) cg_mask_to_string(m, &s);
|
||||
fprintf(f, "%s\tCGroup own mask: %s\n", prefix, strnull(s));
|
||||
}
|
||||
|
||||
m = unit_get_members_mask(u);
|
||||
if (m != 0) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
@@ -1173,6 +1184,13 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
|
||||
fprintf(f, "%s\tCGroup members mask: %s\n", prefix, strnull(s));
|
||||
}
|
||||
|
||||
m = unit_get_delegate_mask(u);
|
||||
if (m != 0) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
(void) cg_mask_to_string(m, &s);
|
||||
fprintf(f, "%s\tCGroup delegate mask: %s\n", prefix, strnull(s));
|
||||
}
|
||||
|
||||
SET_FOREACH(t, u->names, i)
|
||||
fprintf(f, "%s\tName: %s\n", prefix, t);
|
||||
|
||||
@@ -1537,7 +1555,8 @@ int unit_load(Unit *u) {
|
||||
if (u->job_running_timeout != USEC_INFINITY && u->job_running_timeout > u->job_timeout)
|
||||
log_unit_warning(u, "JobRunningTimeoutSec= is greater than JobTimeoutSec=, it has no effect.");
|
||||
|
||||
unit_update_cgroup_members_masks(u);
|
||||
/* We finished loading, let's ensure our parents recalculate the members mask */
|
||||
unit_invalidate_cgroup_members_masks(u);
|
||||
}
|
||||
|
||||
assert((u->load_state != UNIT_MERGED) == !u->merged_into);
|
||||
|
||||
@@ -247,11 +247,10 @@ typedef struct Unit {
|
||||
|
||||
/* Counterparts in the cgroup filesystem */
|
||||
char *cgroup_path;
|
||||
CGroupMask cgroup_realized_mask;
|
||||
CGroupMask cgroup_enabled_mask;
|
||||
CGroupMask cgroup_invalidated_mask;
|
||||
CGroupMask cgroup_subtree_mask;
|
||||
CGroupMask cgroup_members_mask;
|
||||
CGroupMask cgroup_realized_mask; /* In which hierarchies does this unit's cgroup exist? (only relevant on cgroupsv1) */
|
||||
CGroupMask cgroup_enabled_mask; /* Which controllers are enabled (or more correctly: enabled for the children) for this unit's cgroup? (only relevant on cgroupsv2) */
|
||||
CGroupMask cgroup_invalidated_mask; /* A mask specifiying controllers which shall be considered invalidated, and require re-realization */
|
||||
CGroupMask cgroup_members_mask; /* A cache for the controllers required by all children of this cgroup (only relevant for slice units) */
|
||||
int cgroup_inotify_wd;
|
||||
|
||||
/* Device Controller BPF program */
|
||||
@@ -330,7 +329,6 @@ typedef struct Unit {
|
||||
|
||||
bool cgroup_realized:1;
|
||||
bool cgroup_members_mask_valid:1;
|
||||
bool cgroup_subtree_mask_valid:1;
|
||||
|
||||
/* Reset cgroup accounting next time we fork something off */
|
||||
bool reset_accounting:1;
|
||||
|
||||
@@ -189,7 +189,7 @@ int create_subcgroup(pid_t pid, bool keep_unit, CGroupUnified unified_requested)
|
||||
}
|
||||
|
||||
/* Try to enable as many controllers as possible for the new payload. */
|
||||
(void) cg_enable_everywhere(supported, supported, cgroup);
|
||||
(void) cg_enable_everywhere(supported, supported, cgroup, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,27 @@ if grep -q cgroup2 /proc/filesystems ; then
|
||||
-w /sys/fs/cgroup/system.slice/test0.service/cgroup.subtree_control
|
||||
|
||||
systemd-run --wait --unit=test1.service -p "DynamicUser=1" -p "Delegate=memory pids" \
|
||||
grep memory /sys/fs/cgroup/system.slice/test1.service/cgroup.controllers
|
||||
grep -q memory /sys/fs/cgroup/system.slice/test1.service/cgroup.controllers
|
||||
|
||||
systemd-run --wait --unit=test2.service -p "DynamicUser=1" -p "Delegate=memory pids" \
|
||||
grep pids /sys/fs/cgroup/system.slice/test2.service/cgroup.controllers
|
||||
grep -q pids /sys/fs/cgroup/system.slice/test2.service/cgroup.controllers
|
||||
|
||||
# "io" is not among the controllers enabled by default for all units, verify that
|
||||
grep -qv io /sys/fs/cgroup/system.slice/cgroup.controllers
|
||||
|
||||
# Run a service with "io" enabled, and verify it works
|
||||
systemd-run --wait --unit=test3.service -p "IOAccounting=yes" -p "Slice=system-foo-bar-baz.slice" \
|
||||
grep -q io /sys/fs/cgroup/system.slice/system-foo.slice/system-foo-bar.slice/system-foo-bar-baz.slice/test3.service/cgroup.controllers
|
||||
|
||||
# We want to check if "io" is removed again from the controllers
|
||||
# list. However, PID 1 (rightfully) does this asynchronously. In order
|
||||
# to force synchronization on this, let's start a short-lived service
|
||||
# which requires PID 1 to refresh the cgroup tree, so that we can
|
||||
# verify that this all works.
|
||||
systemd-run --wait --unit=test4.service true
|
||||
|
||||
# And now check again, "io" should have vanished
|
||||
grep -qv io /sys/fs/cgroup/system.slice/cgroup.controllers
|
||||
else
|
||||
echo "Skipping TEST-19-DELEGATE, as the kernel doesn't actually support cgroupsv2" >&2
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user