Skip to content

Commit 9d15e09

Browse files
authored
core/cgroup: fix TasksMaxScale percentage serialization (#41011)
bus_cgroup_set_tasks_max_scale() used a hand-rolled percentage format that produced values ~10x too small (e.g., "TasksMax=4.0%" instead of "TasksMax=40.00%"). On daemon-reload, the incorrect value was re-read, silently reducing the effective TasksMax by ~10x and causing fork rejections on systems with high thread counts. Fix by using the existing PERMYRIAD macros, consistent with memory property handlers (MemoryMax, MemoryHigh, MemoryLow, etc.). Fixes: #41009
1 parent f6857cf commit 9d15e09

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎src/core/dbus-cgroup.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,9 +1010,9 @@ static int bus_cgroup_set_tasks_max_scale(
10101010
*p = (CGroupTasksMax) { v, UINT32_MAX }; /* .scale is not 0, so this is interpreted as v/UINT32_MAX. */
10111011
unit_invalidate_cgroup(u, CGROUP_MASK_PIDS);
10121012

1013-
uint32_t scaled = DIV_ROUND_UP((uint64_t) v * 100U, (uint64_t) UINT32_MAX);
1014-
unit_write_settingf(u, flags, name, "%s=%" PRIu32 ".%" PRIu32 "%%", "TasksMax",
1015-
scaled / 10, scaled % 10);
1013+
int scaled = UINT32_SCALE_TO_PERMYRIAD(v);
1014+
unit_write_settingf(u, flags, name, "TasksMax=" PERMYRIAD_AS_PERCENT_FORMAT_STR,
1015+
PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled));
10161016
}
10171017

10181018
return 1;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
set -ex
4+
set -o pipefail
5+
6+
# shellcheck source=test/units/test-control.sh
7+
. "$(dirname "$0")"/test-control.sh
8+
# shellcheck source=test/units/util.sh
9+
. "$(dirname "$0")"/util.sh
10+
11+
testcase_tasks_max_scale_serialize() {
12+
# Regression test for https://github.com/systemd/systemd/issues/41009
13+
# TasksMaxScale was serialized as 4.0% instead of 40.00%, causing 10x reduction on daemon-reload
14+
15+
local unit="test-tasks-max.slice"
16+
local unit_file="/run/systemd/system/${unit}"
17+
local dropin_dir="/run/systemd/system.control/${unit}.d"
18+
19+
# shellcheck disable=SC2329
20+
cleanup() (
21+
set +e
22+
rm -rf "${dropin_dir}"
23+
rm -f "${unit_file}"
24+
systemctl daemon-reload
25+
)
26+
trap cleanup RETURN
27+
28+
printf '[Slice]\n' >"${unit_file}"
29+
30+
systemctl daemon-reload
31+
32+
# Set TasksMax=40% via D-Bus — exercises bus_cgroup_set_tasks_max_scale()
33+
systemctl set-property --runtime "${unit}" TasksMax=40%
34+
35+
# Verify drop-in file contains correct percentage (40.00%, not 4.0%)
36+
grep -q '^TasksMax=40\.00%$' "${dropin_dir}/50-TasksMaxScale.conf"
37+
38+
# Capture value before daemon-reload
39+
local tasks_max_before
40+
tasks_max_before=$(systemctl show -P TasksMax "${unit}")
41+
42+
# Reload and verify value is preserved (the actual bug: value dropped 10x here)
43+
systemctl daemon-reload
44+
45+
local tasks_max_after
46+
tasks_max_after=$(systemctl show -P TasksMax "${unit}")
47+
assert_eq "${tasks_max_before}" "${tasks_max_after}"
48+
}
49+
50+
run_testcases

0 commit comments

Comments
 (0)