Skip to content

bus_cgroup_set_tasks_max_scale: TasksMax percentage serialized 5-10x too low due to units mismatch #41009

Closed

Description

@Cyrus-Xi

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

  1. added
    bug 🐛
    on Mar 9, 2026
  2. added 6 commits that reference this issue on Mar 9, 2026
    f0dbd84
    c36f02c
    9df1b5f
    635f004
    135ad9f
    a822dcf
  3. added a commit that references this issue on Mar 10, 2026
    9d15e09
  4. added 3 commits that reference this issue on Mar 11, 2026
    e227368
    7a7d74f
    7102649
  5. added 5 commits that reference this issue on Mar 12, 2026
    843b3e8
    8498799
    8ce6182
    1790f4c
    03dd208
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛pid1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions