Skip to content

fix(libcgroups): send IOWeight for the io weight - #3923

Open
yjjnls wants to merge 3 commits into
youki-dev:mainfrom
yjjnls:feat/systemd-blockio-weight
Open

yjjnls wants to merge 3 commits into
youki-dev:mainfrom
yjjnls:feat/systemd-blockio-weight

Conversation

@yjjnls

@yjjnls yjjnls commented Nov 22, 2026 •

Copy link
Copy Markdown

Fixes #3897.

With --systemd-cgroup the io controller only reads the four throttle lists, so
resources.blockIO.weight never reaches the unit as an IOWeight property. A weight that is
written to cgroupfs instead (a plain io.bfq.weight in resources.unified, or a hand-written
io.weight) does not survive: the next time systemd applies the unit's cgroup properties - any
set-property, including one for an unrelated property such as TasksMax - the weight is reset
to the IOWeight systemd holds, or to the default when the unit has none. As noted in the
issue, runc has the reset half of this too, because its systemd driver does not send IOWeight
either.

This sends IOWeight as the pre-image of systemd's BFQ scaling, so what systemd ends up writing
to io.bfq.weight is exactly the spec weight. The scaling in BFQ_WEIGHT()
(src/basic/cgroup-util.h) is identity up to 100 and 100 + (io_weight - 100) * 900 / 9900
above it, which is exactly 100 + (io_weight - 100) / 11; the inverse 100 + (weight - 100) * 11
is integer-exact for every weight in [1, 1000], so nothing is rounded. ARMeeru confirmed the
resulting values on systemd 262 for 10, 101, 500, 750 and 1000, including that they stay after an
unrelated set-property; the round-trip is asserted over the whole range in a unit test here.

Decisions worth pointing out:

  • One writer per weight. resources.blockIO.weight is only sent as IOWeight; the driver
    does not also write io.bfq.weight or io.weight itself for it. systemd writes both files
    from IOWeight (set_io_weight()), so they cannot disagree with each other and the value
    survives further applies. On a BFQ host io.bfq.weight at create time matches what runc
    writes for it (and stays, where runc resets it); io.weight differs, since this sets 4500
    for a weight of 500 where runc leaves it at 100. On a host without BFQ, io.weight gets the
    IOWeight value (4500 for 500) instead of the cgroupfs driver's linear conversion
    (4950 for 500); both are valid values in [1, 10000], the difference is the scale of the
    non-BFQ proportional share.
  • systemd 250 floor. The pre-image is exact from systemd 250. Older releases derive
    io.bfq.weight from IOWeight differently (243-247 write it as is, which the kernel rejects
    above 1000, and 248-249 write (weight + 9) / 10), so the mapping is not meaningful below
    250; the contest group skips older systemd. Io::apply already receives the systemd version,
    so a version-dependent map could be added if it is ever wanted, but it would only fit those
    broken older semantics.
  • resources.unified. A plain io.bfq.weight value maps through the same pre-image;
    anything else (default 500, per-device lines, out of range, non-numeric) is left to the
    cgroupfs write as before. io.weight is deliberately left alone: a unified key applies
    verbatim to the file it names, and mapping it to IOWeight would additionally make systemd
    derive io.bfq.weight from the value. It behaves as before, so a later systemd apply can
    still reset it; mapping it would be a natural follow-up, but it changes what a verbatim key
    means on BFQ hosts, so it is not part of this fix.
  • A weight of 0 stays a no-op and values outside [10, 1000] are rejected with the same error
    as the cgroupfs driver instead of being dropped silently. Per-device weights
    (weightDevice) remain unsupported on the systemd path, as before.

Testing:

  • cargo test -p libcgroups --all-features --lib passes (272 tests), including the new cases.
    A full cargo test --lib --bins --all --all-targets --all-features run on this host only
    fails the two root-requiring libcontainer --test as_sibling tests, which fail identically on
    the base commit (EPERM without root).
  • New unit tests cover the inverse map at 1, 10, 100, 101, 500, 750 and 1000, a round-trip
    through systemd's BFQ_WEIGHT() for every weight in [1, 1000], the property and error cases
    for resources.blockIO.weight, and the io.bfq.weight unified key (plain value mapped, other
    forms left to cgroupfs).
  • New contest tests in the cgroup_systemd_io group
    (tests/contest/contest/src/tests/cgroups/systemd/io.rs) cover the persistence half end to
    end: a weight from resources.blockIO.weight (above and below the default of 100) and from a
    plain io.bfq.weight unified key has to reach the unit as IOWeight with the expected
    pre-image, systemd has to write the expected io.bfq.weight/io.weight values, and the weight
    has to survive a set-property that makes systemd reapply the unit. The group needs root, cgroup
    v2 with the io controller and systemd 250 or later, and skips on runc, whose systemd driver
    does not send IOWeight. ARMeeru ran it on Fedora (systemd 262): 4 ok on this PR, 4 failing on
    main (IOWeight=[not set]) and 4 skipped with RUNTIME_KIND=runc. cargo build -p contest and
    workspace clippy pass locally.
  • Live check on this machine against the running systemd 255 user manager (rootless, cgroup v2),
    driving the systemd manager directly: blockIO.weight: 500 sets IOWeight=4500 as read back
    with systemctl show -p IOWeight, it stays 4500 after an unrelated
    set-property TasksMax=500, and a unified io.bfq.weight: "750" sets IOWeight=7250. The
    same check on the base commit reports IOWeight as not set for blockIO.weight: 500.
  • Local limits: the io controller is not delegated to the user manager on this host and the
    kernel was built without BFQ, so the io.weight/io.bfq.weight file contents could not be
    read back here. That side is covered by the unit-test round-trip, by systemd's
    set_io_weight()/BFQ_WEIGHT() (writes default <IOWeight> to io.weight and
    BFQ_WEIGHT(IOWeight) to io.bfq.weight), and by ARMeeru's measurements on systemd 262.

@saku3 saku3 added the kind/bug label Nov 23, 2026
@saku3

saku3 commented Nov 23, 2026

Copy link
Copy Markdown
Member

Thank you for PR.

Could you add integration tests for this change under https://github.com/youki-dev/youki/tree/main/tests/contest/contest/src/tests/cgroups/systemd?

@yjjnls

yjjnls commented Nov 23, 2026

Copy link
Copy Markdown
Author

Added integration tests for this in commit 1575797, as a new cgroup_systemd_io group under tests/contest/contest/src/tests/cgroups/systemd. They check that a weight reaches the unit as the IOWeight property holding the pre-image of systemd's BFQ scaling — from resources.blockIO.weight (above and below the default of 100) and from a plain io.bfq.weight value in resources.unified — that systemd derives the expected io.weight/io.bfq.weight values from it, and that the weight survives systemd applying the unit again. runc's systemd driver does not send IOWeight at all, so the tests are skipped when validating them on runc.

@saku3

saku3 commented Nov 24, 2026

Copy link
Copy Markdown
Member

@ARMeeru
I would appreciate it if you could review this.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the systemd I/O weight changes.

No open issues 🎉

What changed in this PR

Addresses #3897 by sending supported I/O weights through systemd’s IOWeight property, preserving them when systemd reapplies a container unit’s settings.

Changes:

  • Adds BFQ-compatible weight conversion and validates structured weights, keeping zero as a no-op.
  • Maps plain unified io.bfq.weight values to IOWeight, while leaving other forms and io.weight on the existing cgroupfs path.
  • Adds unit and integration tests for conversion, property values, and persistence after unrelated property updates.
File Description
tests/​contest/​contest/​src/​tests/​cgroups/​systemd/​mod.rs Exposes the systemd I/O test module.
tests/​contest/​contest/​src/​tests/​cgroups/​systemd/​io.rs Tests weight application and persistence, with runtime and kernel capability checks.
tests/​contest/​contest/​src/​main.rs Registers the systemd I/O test group.
crates/​libcgroups/​src/​systemd/​unified.rs Maps plain unified BFQ weights to IOWeight and tests fallback behavior.
crates/​libcgroups/​src/​systemd/​io.rs Adds weight validation, BFQ conversion, property emission, and unit tests.

Review effort: Balanced

@ARMeeru ARMeeru left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this at 1575797 on Fedora (systemd 262, kernel 7.3, aarch64, BFQ available), against main and runc 1.5.3.

  • The new cgroup_systemd_io group, as root: 4 ok on this PR, 4 not ok on main (IOWeight=[not set]), 4 skipped with RUNTIME_KIND=runc. Without the runc gate all 4 fail on runc, so the gate is needed. cgroup_systemd_unified, cgroup_v2_io and cgroup_systemd_devices still pass with this PR.
  • Rootless with the io controller delegated: blockIO.weight: 500 gives io.bfq.weight 500, io.weight 4500 and IOWeight 4500, and all three stay after an unrelated set-property. Weight 50 gives 50 for all three.
  • cargo test -p libcgroups --all-features --lib (272), clippy and fmt pass.

One point I think needs a change, inline on bfq_weight_to_io_weight: the pre-image is only exact from systemd 250.

Two smaller ones on the description:

  • It still offers the contest test as a follow-up. 1575797 adds it, so the Testing section could describe the new group instead.
  • "matches what runc writes" holds for io.bfq.weight. io.weight is 4500 here, where runc leaves it at 100.

/// The property is also what keeps the weight after systemd applies the unit again: a value
/// written to cgroupfs only is reset to the `IOWeight` systemd holds, or to the default when
/// the unit has no `IOWeight`.
pub fn bfq_weight_to_io_weight(weight: u16) -> u64 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inverse matches BFQ_WEIGHT(), which systemd has had since 250. Older versions derive io.bfq.weight from IOWeight differently (src/core/cgroup.c at the tags):

  • 243 to 247 write IOWeight as is. The kernel rejects anything above 1000 (echo 4500 > io.bfq.weight gives Numerical result out of range here), so weight 500 would leave io.bfq.weight at 100.
  • 248 and 249 write (weight + 9) / 10, so 500 gives 450 and 50 gives 5.

I couldn't measure these, since this host only has 262. Ubuntu 22.04 ships 249 and Debian 11 ships 247. Io::apply already receives the systemd version (the _: u32), so the map could take it: w up to 247, w * 10 for 248 and 249, and the current formula from 250. The unified tests that pass 245 would then need 250 or later. If you'd rather not support those versions, could the doc comment and the description state the 250 floor, with the contest group skipping below it, like the version checks in the contest's systemd/unified.rs?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I went with the floor: bfq_weight_to_io_weight now documents that the pre-image is exact from systemd 250, and the contest group skips below 250 (same version gate as the other systemd tests). Version-dependent mapping would only fit the older semantics, so I left it out; the PR description now states the floor too. Fixed in baa4806.

@yjjnls

yjjnls commented Nov 24, 2026

Copy link
Copy Markdown
Author

@ARMeeru Thanks for the measurements and the review. Updated in baa4806: the mapping declares the systemd 250 floor and the contest group skips older systemd, the Testing section now describes the new cgroup_systemd_io group instead of a follow-up, and the runc comparison now says io.bfq.weight matches while io.weight differs (4500 vs 100).

@ARMeeru

ARMeeru commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Thanks, all three look good to me in baa4806. I have nothing more from my side; the rest is up to the maintainers.

@nayuta723

Copy link
Copy Markdown
Contributor

@yjjnls
We've changed how contest works in #3918, which has been merged. Could you rebase onto main and update your PR?

With the systemd cgroup driver the io controller only read the throttle
device lists, so resources.blockIO.weight never reached the unit: no
IOWeight property was set for it, and nothing was written to cgroupfs
either. A weight written to cgroupfs instead, such as a plain
io.bfq.weight value in resources.unified, was reset to the default as
soon as systemd applied the unit's cgroup properties again, which any
set-property triggers, even for an unrelated property such as TasksMax.

Send IOWeight for resources.blockIO.weight and for a plain io.bfq.weight
value in resources.unified, as the pre-image of systemd's BFQ scaling
(BFQ_WEIGHT() in src/basic/cgroup-util.h: identity up to 100, then
100 + (io_weight - 100) / 11, so the inverse is integer exact). systemd
then writes exactly the spec weight to io.bfq.weight and the IOWeight
value itself to io.weight, and the value survives further applies. The
driver does not write either file for the structured weight, so systemd
stays the single writer of the io controller weights.

A weight of 0 stays a no-op, and weights outside [10, 1000] are rejected
as in the cgroupfs driver instead of being dropped silently. A
resources.unified io.weight is left as it is: a unified key applies
verbatim to the file it names, and mapping it to IOWeight would also
make systemd derive io.bfq.weight from the value.

Signed-off-by: yjjnls <yjjnls@users.noreply.github.com>
Check that a weight under the systemd cgroup driver reaches the unit as
the IOWeight property holding the pre-image of systemd's BFQ scaling, so
systemd writes the spec weight itself to io.bfq.weight and the property
value to io.weight. A structured weight above the default of 100 is sent
as the BFQ pre-image, one up to it is sent as it is, and a plain
io.bfq.weight value in resources.unified is a weight of the same scale,
so it maps the same way. The weight also has to survive systemd applying
the unit's cgroup settings again, which any set-property triggers, since
that is what resets a weight that did not reach the unit as IOWeight.

The systemd driver of runc 1.5.2 does not send IOWeight at all, so the
tests are skipped when validating them on runc.

Signed-off-by: yjjnls <yjjnls@users.noreply.github.com>
The pre-image of systemd's BFQ_WEIGHT() is only exact from systemd 250.
Older releases derive io.bfq.weight from IOWeight differently: 243 to
247 write it as is, so the kernel rejects values above 1000, and 248 and
249 write (weight + 9) / 10. Document the floor on
bfq_weight_to_io_weight and skip the contest io weight group below 250.
Version-dependent mapping is left out because those older semantics are
themselves broken.

Signed-off-by: yjjnls <yjjnls@users.noreply.github.com>
@yjjnls
yjjnls force-pushed the feat/systemd-blockio-weight branch from baa4806 to 816415c Compare November 25, 2026 02:13
@yjjnls

yjjnls commented Nov 25, 2026

Copy link
Copy Markdown
Author

@nayuta723 Rebased onto main (816415c) and switched the systemd io group to the #3918 inventory registration: it now self-registers via register_test_group! and main.rs keeps the automatic group collection. fmt, clippy and libcgroups tests pass; contest only compiled locally since it needs root/systemd.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: with the systemd cgroup driver, io weight is dropped or reset by systemd

5 participants