Repository navigation
Description
systemd version the issue has been seen with
259
Used distribution
N/A (bug is in upstream source, confirmed on custom build from v259)
Linux kernel version used
6.9.0
CPU architectures issue was seen on
x86_64
Component
No response
Expected behaviour you didn't see
systemctl set-property --runtime user.slice TasksMax=40% should persist TasksMax=40.00% in the runtime drop-in file, and daemon-reload should re-read the same 40% value.
Unexpected behaviour you saw
The drop-in file is written as TasksMax=4.0% instead of TasksMax=40.00% — 5-10x too low (exactly 10x for inputs ≥ 5%). On the next daemon-reload, daemon-reexec, or reboot, the effective TasksMax limit drops without any warning in the journal.
The bug is in bus_cgroup_set_tasks_max_scale() (src/core/dbus-cgroup.c, ~line 1011). The function computes scaled = DIV_ROUND_UP(v * 100, UINT32_MAX), producing a whole-percent value (e.g., 40 for 40%). But the format string scaled/10 . scaled%10 %% treats scaled as tenths of percent, outputting 4.0% instead of 40.0%.
The adjacent memory property handlers (MemoryMax, MemoryHigh, MemoryLow) use UINT32_SCALE_TO_PERMYRIAD + PERMYRIAD_AS_PERCENT_FORMAT_STR and produce correct output. Both paths are in the same file, writing to the same drop-in directory:
$ cat /run/systemd/system.control/user.slice.d/50-MemoryLowScale.conf [Slice] MemoryLow=15.00% ← CORRECT (uses PERMYRIAD macros)
$ cat /run/systemd/system.control/user.slice.d/50-TasksMaxScale.conf
[Slice]
TasksMax=4.0% ← WRONG (hand-rolled formula)
Buggy code (src/core/dbus-cgroup.c, ~line 1011):
uint32_t scaled = DIV_ROUND_UP((uint64_t) v * 100U, (uint64_t) UINT32_MAX);
unit_write_settingf(u, flags, name, "%s=%" PRIu32 ".%" PRIu32 "%%", "TasksMax",
scaled / 10, scaled % 10);
Correct code (other scaled property handlers, ~line 904, via BUS_DEFINE_SET_CGROUP_LIMIT):
int scaled = UINT32_SCALE_TO_PERMYRIAD(raw);
unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR,
(int)(strlen(name) - strlen("Scale")), name,
PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled));
Error table:
| Intended | Written (buggy) | Read back as | Effective reduction |
|---|---|---|---|
| 1% | 0.2% | 0.20% | 5x |
| 5% | 0.5% | 0.50% | 10x |
| 10% | 1.0% | 1.00% | 10x |
| 40% | 4.0% | 4.00% | 10x |
| 100% | 10.0% | 10.00% | 10x |
This bug affects runtime property changes via systemctl set-property (and systemd-run), not static unit file configurations — those go through config_parse_tasks_max() which uses a different (correct) code path.
Impact: The effective TasksMax is silently reduced by ~10x after daemon-reload. On systems where the actual thread count exceeds the reduced limit, this causes fork rejections across all processes in the affected slice (cgroup: fork rejected by pids controller in /user.slice/.../scope). The in-memory value is correct immediately after set-property, masking the file error until the next daemon-reload.
Suggested fix: Replace the hand-rolled formula with the existing PERMYRIAD macros, consistent with the other scaled property handlers:
int scaled = UINT32_SCALE_TO_PERMYRIAD(v);
assert_se(endswith(name, "Scale"));
unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR,
(int)(strlen(name) - strlen("Scale")), name,
PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled));
Workaround: Use absolute values instead of percentages:
THREADS_MAX=$(cat /proc/sys/kernel/threads-max)
sudo systemctl set-property --runtime user.slice "TasksMax=$((THREADS_MAX * 40 / 100))"
Steps to reproduce the problem
# Set TasksMax to 40%
sudo systemctl set-property --runtime user.slice TasksMax=40%
# In-memory value is correct
systemctl show user.slice -p TasksMax
# TasksMax=352909 (40% of threads-max on this machine)
# Drop-in file is wrong
cat /run/systemd/system.control/user.slice.d/50-TasksMaxScale.conf
# TasksMax=4.0% (should be 40.00%)
# Compare with memory property (correct)
cat /run/systemd/system.control/user.slice.d/50-MemoryLowScale.conf
# MemoryLow=15.00% (correct — uses PERMYRIAD macros)
# After daemon-reload, limit drops to 4%
sudo systemctl daemon-reload
systemctl show user.slice -p TasksMax
# TasksMax=35290 (4% of threads-max — 10x reduction)Additional program output to the terminal or log subsystem illustrating the issue
# Before daemon-reload (correct in-memory value):
$ systemctl show user.slice -p TasksMax
TasksMax=352909
# The buggy drop-in file:
$ cat /run/systemd/system.control/user.slice.d/50-TasksMaxScale.conf
[Slice]
TasksMax=4.0%
# After daemon-reload (wrong — 10x reduction):
$ systemctl show user.slice -p TasksMax
TasksMax=35290
# The correct memory property drop-in for comparison:
$ cat /run/systemd/system.control/user.slice.d/50-MemoryLowScale.conf
[Slice]
MemoryLow=15.00%
Activity
core/cgroup: fix TasksMaxScale percentage serialization
core/dbus-cgroup: fix TasksMax percentage serialization
core/dbus-cgroup: fix TasksMax percentage serialization
core/dbus-cgroup: fix TasksMax percentage serialization
core/dbus-cgroup: fix TasksMax percentage serialization
core/dbus-cgroup: fix TasksMax percentage serialization
core/cgroup: fix TasksMaxScale percentage serialization (systemd#41011)
core/cgroup: fix TasksMaxScale percentage serialization (systemd#41011)
core/cgroup: fix TasksMaxScale percentage serialization (systemd#41011)
core/cgroup: fix TasksMaxScale percentage serialization (#41011)
core/cgroup: fix TasksMaxScale percentage serialization (systemd#41011)
core/cgroup: fix TasksMaxScale percentage serialization (#41011)
core/cgroup: fix TasksMaxScale percentage serialization (systemd#41011)
core/cgroup: fix TasksMaxScale percentage serialization (systemd#41011)
core/cgroup: fix TasksMaxScale percentage serialization (#41011)