Skip to content

WTF: see the cgroup v2 limits where a cgroup v1 hierarchy exists beside it - #808

Merged
Jarred-Sumner merged 2 commits into
mainfrom
claude/cgroup-v2-beside-v1
Oct 14, 2026
Merged

Jarred-Sumner merged 2 commits into
mainfrom
claude/cgroup-v2-beside-v1

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

uv_get_constrained_cpu() and uv_get_constrained_memory() miss the cgroup v2 limits on a host that also has a cgroup v1 hierarchy. WTF::numberOfProcessorCores() then reports every core of the machine, and WTF::ramSize() all of its memory.

Cause

Both decide between v1 and v2 by the first line of /proc/self/cgroup:

/* In the case of cgroupv2, we'll only have a single entry. */
if (strncmp(buf, "0::/", 4))

The kernel lists v1 hierarchies first and 0::/ last. One v1 hierarchy is enough, even a named one that has no controllers:

1:name=systemd:/
0::/system.slice/a.service

Both functions then look for the v1 files (/sys/fs/cgroup/cpu//cpu.cfs_quota_us, /sys/fs/cgroup/memory/memory.limit_in_bytes), find none, and report no limit.

Fix

A controller is in one hierarchy. It is read from v2 if there is a 0::/ line and no v1 line names that controller.

Whether a v1 line names it is asked of one function for both controllers, which looks at each name in the list. The one for memory compared the whole list with memory, so it missed 3:cpu,cpuacct,memory:/a.

Testing

The file compiled on its own, before and after, in a private mount namespace with a made-up /sys/fs/cgroup and /proc/<pid>/cgroup:

layout CPUs before CPUs after memory before memory after
v2 only, 0::/ 2 2 1 GiB 1 GiB
v2 only, limit on a parent group 3 3 2 GiB 2 GiB
v2 only, no limits none none none none
v1 only 4 4 3 GiB 3 GiB
systemd's hybrid layout: controllers in v1, and a 0::/ line 4 4 3 GiB 3 GiB
empty file none none none none
memory mounted with cpu in v1, limit in /sys/fs/cgroup/memory, and a 0::/ line none none 3 GiB 3 GiB
memory mounted with cpu in v1, limit on the group 4 4 none 3 GiB
a named v1 hierarchy and v2, limit on a parent group none 3 none 2 GiB
a named v1 hierarchy and v2, 0::/ none 2 none 1 GiB
the same, without a line break at the end none 2 none 1 GiB
cpu in v1, memory in v2 4 4 none 5 GiB
memory in v1, cpu in v2 none 6 3 GiB 3 GiB

With nothing made up, on a machine that has a named v1 hierarchy beside v2, under systemd-run --scope -p CPUQuota=200% -p MemoryMax=1G: no limits before, 2 CPUs and 1 GiB after. Without the two properties: no limits before and after.

…de it

`uv_get_constrained_cpu` and `uv_get_constrained_memory` took the host for
cgroup v2 only if the first line of /proc/self/cgroup is "0::/". The kernel
lists cgroup v1 hierarchies first, so with one of them, even one without
controllers, both looked for the cgroup v1 files, found none and reported no
limit:

    1:name=systemd:/
    0::/system.slice/a.service

A controller is in one hierarchy. It is read from cgroup v2 if there is a "0::/"
line and no cgroup v1 line names the controller. Hosts with only cgroup v2, with
only cgroup v1, and with systemd's hybrid layout get the same answers as before.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Beyond the inline finding on the memory dispatch, I also checked whether the CPU dispatch has the same co-mounted-controller problem: uv__cgroup1_find_cpu_controller tokenizes the controller list, so a v1 line like "3:cpu,cpuacct:/..." is still matched and the CPU path stays on v1. Its early return on a line without two colons was also looked at — the kernel always writes "id:controllers:path", so it does not apply to real /proc/self/cgroup contents.

Extended reasoning...

The change touches only Source/WTF/wtf/uv_get_constrained_memory.cpp, replacing the first-line "0::/" check with a scan for the v2 line plus a check that no v1 line names the relevant controller, in both the memory and CPU dispatch. No security-sensitive surface is involved; it reads /proc/self/cgroup and sysfs. An inline finding remains on the memory path, where the v1 memory finder only matches ":memory:" at the first colon, so a human should look before merging.

…ted with others

The one for memory compared the whole list of controllers with "memory", so it
did not find it in "3:cpu,cpuacct,memory:/a". With a "0::/" line in the file the
limit was then looked for in cgroup v2, where it is not, and no longer in
/sys/fs/cgroup/memory. The one for cpu looks at each name in the list. It takes
the name of the controller now and is used for both.
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Preview build of aa91374: autobuild-preview-pr-808-aa91374a

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

The co-mounted memory controller case from the earlier round is now handled: both callers go through the same tokenizing uv__cgroup1_find_controller, so "3:cpu,cpuacct,memory:/a" routes memory to v1. Beyond the inline finding (pre-existing cpu-path behaviour), I also checked the token loop for termination and over-read — tok advances by at least one per iteration and strcspn stops at the path colon, so it cannot run past the line; the "0::/" line has an empty controller list and is skipped; uv__slurp NUL-terminates the 1024-byte buffer, so a truncated /proc/self/cgroup ends every scan; and the uninitialized n in the callers is write-only.

Extended reasoning...

The change is confined to Source/WTF/wtf/uv_get_constrained_memory.cpp, replacing the first-line "0::/" check with a scan for the v2 line plus a shared v1 controller finder, and routing each controller to v1 only when a v1 line names it. It touches no security-sensitive surface. The one inline finding is pre-existing v1 CPU behaviour rather than something this PR introduces, and the earlier co-mount concern was addressed by commit aa91374; the remaining reason for a human look is that this is platform-detection code behind ramSize()/numberOfProcessorCores() with only the author's manual test matrix as coverage.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🟣 Source/WTF/wtf/uv_get_constrained_memory.cpp — pre-existing: Bun inside a Docker container on a cgroup v1 host still sees every host core even when the container was started with --cpus. The v1 CPU path builds /sys/fs/cgroup/cpu//cpu.cfs_quota_us from the path in /proc/self/cgroup and gives up at uv_get_constrained_memory.cpp:315 when that file is missing. With Docker's default cgroupns=host on v1 hosts, /sys/fs/cgroup/cpu is already the container's own cgroup, so the nested path never exists. Fix: when the per-cgroup read fails, retry at the controller root (/sys/fs/cgroup/cpu/cpu.cfs_quota_us and cpu.cfs_period_us), mirroring the fallback the memory path has at lines 198-200.

    Why this was flagged

    A process in a Docker container on a cgroup v1 host, started with --cpus=2, has /proc/self/cgroup containing a line such as "2:cpu,cpuacct:/docker/" because Docker defaults to cgroupns=host on v1 hosts. uv_get_constrained_cpu (line 372) routes to uv__get_cgroup1_constrained_cpu (line 298); uv__cgroup1_find_controller at line 306 returns "docker/", and line 312 builds "/sys/fs/cgroup/cpu/docker//cpu.cfs_quota_us". runc bind-mounts the container's own cgroup at /sys/fs/cgroup/cpu,cpuacct (with a cpu symlink), so that nested directory does not exist; uv__slurp fails and line 315 returns 0. WTF::numberOfProcessorCores (NumberOfCores.cpp:98) then keeps the host core count. The memory path handles the identical layout: uv__get_cgroup1_memory_limits falls back to /sys/fs/cgroup/memory/memory.limit_in_bytes at lines 198-200 when the per-path reads yield 0, so ramSize() is correct while the CPU count is not. The base branch behaves the same way; this PR's fix for hybrid layouts does not reach this case.

    Verification: pre-existing. Trigger: a Docker container on a cgroup v1 host with cgroupns=host, started with --cpus. In uv_get_constrained_memory.cpp, line 312-313 builds "/sys/fs/cgroup/cpu/docker//cpu.cfs_quota_us", and line 314-315 returns 0 when that nested path does not exist. There is no fallback to the mount-point files, unlike the memory path at lines 198-200. Base branch behaves identically on this input.

@coderabbitai

coderabbitai Bot commented Oct 14, 2026

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 6414c2d7-e1e7-4483-afe2-39037ef87064
📥 Commits

Reviewing files that changed from the base of the PR and between 4b724e9 and aa91374.

📒 Files selected for processing: 1
  • Source/WTF/wtf/uv_get_constrained_memory.cpp

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

Linux memory and CPU constraint detection now locates cgroup v2 entries throughout the cgroup data and checks for matching cgroup v1 controllers. It uses cgroup v2 only when the relevant v1 controller is absent.

Changes

Linux cgroup detection

Layer / File(s) Summary
Cgroup entry parsing
Source/WTF/wtf/uv_get_constrained_memory.cpp
The code locates the cgroup v2 entry and uses a shared parser to find cgroup v1 controller entries by whole-token name. It removes the separate CPU controller parser.
Cgroup version detection and selection
Source/WTF/wtf/uv_get_constrained_memory.cpp
Memory and CPU lookups use the shared parser. Each selects cgroup v2 when a v2 entry exists and the matching v1 controller is absent; otherwise, it uses cgroup v1.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to aa913

No actionable merge-blocking issue is established; complete normal validation before merging.

Pre-merge checks: Passed 3 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Description check Warning The description clearly explains the bug, cause, fix, and testing. However, it does not include the required Bugzilla bug title and link, the reviewed-by line, or the changed-file and function list fr… Add the associated Bugzilla URL and bug title, include the required reviewed-by line, and list the changed file and relevant functions using the repository template format.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly identifies the main change: detecting cgroup v2 limits when a cgroup v1 hierarchy also exists.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description clearly explains the bug, cause, fix, and testing. However, it does not include the required Bugzilla bug title and link, the reviewed-by line, or the changed-file and function list from the repository template.

  • Fix all pre-merge checks with AI
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Jarred-Sumner
Jarred-Sumner merged commit 79f27e1 into main Oct 14, 2026
96 of 97 checks passed
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Oct 14, 2026
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Oct 14, 2026
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Oct 15, 2026
…rchy exists beside it (#45272)

### What does this PR do?

Bumps WebKit to `79f27e1945be`, the merge commit of oven-sh/WebKit#808.

#### Bun did not see the limits of cgroup v2 on a host that also has a
cgroup v1 hierarchy

`navigator.hardwareConcurrency`, `os.availableParallelism()`, the number
of threads that Bun starts (`bun fmt`, `bun lint`, `bun check`, `bun
install`, the thread pool), `process.constrainedMemory()` and the memory
that JavaScriptCore sizes its heap by were those of the whole machine.

Whether a host has v1 or v2 was decided by the first line of
`/proc/self/cgroup`. The kernel lists v1 hierarchies first, so one of
them was enough, even a named one that has no controllers:

```
1:name=systemd:/
0::/system.slice/a.service
```

Under `systemd-run --scope -p CPUQuota=200% -p MemoryMax=1G`, on a
machine with 64 cores and such a file:

| | before | after |
|---|---|---|
| `navigator.hardwareConcurrency` | 64 | 2 |
| `os.availableParallelism()` | 64 | 2 |
| `process.constrainedMemory()` | 265981812736 | 1073741824 |

Each controller is now read from v2 if there is a `0::/` line and no v1
line names it. A host with only v2, with only v1, or with systemd's
hybrid layout gets the same answers as before.

A v1 host where memory is mounted together with another controller
(`3:cpu,cpuacct,memory:/a`) did not find the limit of its group either,
and does now.

#### What else the bump brings

One change that is on the main branch of oven-sh/WebKit and not in the
build that Bun pins: oven-sh/WebKit#785, "[JSC] Array profiles should
not turn ordinary arrays into `ArrayStorage`", a cherry-pick of
WebKit/WebKit#75376.

### How did you verify your code works?

- New cases next to `process.constrainedMemory()` in
`test/js/node/process/process.test.js`: six layouts, each in a user and
mount namespace of its own, where a made-up directory is
`/sys/fs/cgroup` and a made-up file is `/proc/self/cgroup`. They need no
root, and are skipped where the host does not allow such a namespace.
Before the bump the four layouts that always worked pass and the two
above fail. In CI they run on Debian 13 (x64, aarch64, ASAN) and Alpine
3.23 (x64, aarch64), and are skipped on Ubuntu 25.04.
- The numbers in the table, with real limits from systemd. Without the
two properties it is 64, 64 and 265981812736 before and after.
`os.cpus().length` is 64 in every case.
- The rest of `process.test.js` gives the same results as on main.
- oven-sh/WebKit#808 has a table of thirteen layouts for the file
compiled on its own.
Shrub24 pushed a commit to Shrub24/Pi-Bolt that referenced this pull request Oct 17, 2026
Bun (cherry-picked onto the pinned commit, before Pi-Bolt's patch):
- startup SIGILL on Linux arm64 when SVE2 is reported without SVE (oven-sh/bun#45099)
- fetch sets up TLS once per connection (oven-sh/bun#44507)
- stdout/stderr: O_NONBLOCK cleared for spawned children, no output dropped on EAGAIN (oven-sh/bun#43868)
- a socket whose peer keeps the receive buffer full no longer stops timers (oven-sh/bun#45278)
- PipeReader after a failed read (oven-sh/bun#43900), spawnSync (oven-sh/bun#44581)
- TextEncoder.encode of a substring (oven-sh/bun#45281)
- fetch over HTTP/2 after a TLS alert or an abort (oven-sh/bun#45422)
- mimalloc 3.5.3 (oven-sh/bun#44560, oven-sh/bun#44575)

WebKit (appended to the patch series):
- a module removed while its import() is in flight (oven-sh/WebKit#748)
- cgroup v2 limits beside a cgroup v1 hierarchy (oven-sh/WebKit#808)

Test-file conflicts were resolved to the pinned tests; the source changes are as upstream.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant