Repository navigation
WTF: see the cgroup v2 limits where a cgroup v1 hierarchy exists beside it - #808
Conversation
…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.
There was a problem hiding this comment.
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.
|
Preview build of aa91374: |
There was a problem hiding this comment.
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughLinux 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. ChangesLinux cgroup detection
Priority: ⬇️ Low Merge Risk: ⚪ Minimal · up to
|
…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.
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.
uv_get_constrained_cpu()anduv_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, andWTF::ramSize()all of its memory.Cause
Both decide between v1 and v2 by the first line of
/proc/self/cgroup:The kernel lists v1 hierarchies first and
0::/last. One v1 hierarchy is enough, even a named one that has no controllers: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 missed3: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/cgroupand/proc/<pid>/cgroup:0::/0::/line/sys/fs/cgroup/memory, and a0::/line0::/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.