Skip to content

Fix CPU and memory limit detection for Linux cgroup paths with whitespace or escapes - #137981

Open
krwq wants to merge 6 commits into
mainfrom
krwq-native-cgroup-path-decoding
Open

krwq wants to merge 6 commits into
mainfrom
krwq-native-cgroup-path-decoding

Conversation

@krwq

@krwq krwq commented Nov 20, 2026

Copy link
Copy Markdown
Member

Note

This pull request was generated with GitHub Copilot.

Fixes #137978.

Summary

Fix native Linux cgroup resource-file lookup when membership paths contain literal spaces or tabs, or mountinfo roots/mount points contain escaped path characters. Incorrect lookup can affect Environment.ProcessorCount, automatic GC memory budgeting, and values exposed by GC.GetGCMemoryInfo().

  • Decode mountinfo root and mount point in place using one C-compatible minipal helper. Decode only \040, \011, \012 and \134, once; preserve unknown and truncated sequences.
  • Preserve raw cgroup membership text up to LF instead of stopping at whitespace. Do not unescape membership text or turn literal systemd \x2d pathname text into a hyphen.
  • Apply the fix to the shared minipal cgroup reader used by CoreCLR and NativeAOT after Deduplicate GC and PAL cgroup implementations #137578, and to Mono's remaining separate copy. Do not restore the removed GC/PAL/NativeAOT reader files.
  • Add six native minipal tests covering ordinary paths, all supported escapes, decode-once behavior, carriage returns, malformed/capacity inputs and long paths. Register the mountinfo suite in the existing native driver and managed launcher.

The change repairs discovery, not CPU/memory policy or kernel enforcement. It does not change DriveInfo or the already-merged managed mountinfo parser. No public API is added.

Validation

Revalidated after porting onto 8867da578c52d4833b09e13b43971badc6614e09, which includes the upstream cgroup consolidation.

Windows command Result
.\build.cmd clr+libs+host Clean current-main baseline passed; 0 warnings/errors.
.\build.cmd clr.native Post-fix native build passed; 0 warnings/errors.
src\tests\build.cmd -Tree native\minipal x64 Debug /p:LibrariesConfiguration=Debug Native/managed test build passed; 0 warnings/errors.
src\tests\build.cmd -GenerateLayoutOnly x64 Debug /p:LibrariesConfiguration=Debug Matching runtime layout passed; 0 warnings/errors.
NativeMinipalTestsDriver.exe Passed 32/32.
corerun.exe NativeMinipalTests.dll using that layout Passed all three suites, 6+12+14; exit 100 and suite totals checked.

Builds ran serially through a short worktree path with the installed Visual Studio environment initialized. The portable helper also compiled as C11 and C++17 with /W4 /WX.

On Ubuntu 24.04 x64, rebuilt the actual native coreclrpal, minipal and aotminipal targets and ran the expanded minipal driver: all builds passed and the driver passed 32/32.

A separate Linux source-fixture matrix compiled the current shared cgroup reader with its real regular-minipal and aotminipal configurations, plus the unchanged Mono reader. It supplies controlled procfs/resource files rather than live cgroup limits:

Reader configuration Without fix With fix
Shared minipal C reader 23/52 passed, 29 expected regressions 52/52 passed
Shared aotminipal C reader 23/52 passed, 29 expected regressions 52/52 passed
Mono CPU/memory reader 23/52 passed, 29 expected regressions 52/52 passed

The shared C reader also passed all 52 cases with GCC ASan+UBSan and leak detection, with no sanitizer diagnostic. Cases cover cgroup v1/v2, raw membership spaces/tabs/trailing spaces, escaped roots/mount points, literal systemd backslashes, decode-once/unknown/truncated sequences, ordinary/private roots, and tighter child versus tighter parent limits. The fixtures require reading the correct child's files so a coincidentally correct ancestor value cannot falsely pass. All seven current source/test file hashes were checked against the local patch.

Remaining integration limit: full Linux CoreCLR/NativeAOT/Mono product builds and a live resource-limited managed application have not been completed. The remote environment had package-restore connectivity, disk-capacity and controller-delegation limits. The issue's numerical managed outputs remain source-derived predictions, not observed application results.

Compatibility and scope

Correct discovery can change processor-count-dependent defaults and automatic GC budgets for the affected path configurations. Explicit processor-count and GC settings retain their existing precedence. Existing cgroup-v2 ancestor-minimum behavior and Mono's existing child-only memory lookup are preserved.

The released-version range and real-world prevalence of the triggering path layouts are not established. This PR targets main; it does not change the managed mount-path backports.

Resolves #137978

Decode mountinfo roots and mount points once in a shared helper, and preserve whitespace in raw cgroup membership paths. Update the consolidated minipal reader and the remaining Mono copy, with portable minipal regression coverage.

Co-authored-by: Copilot App <[email protected]>
@github-actions github-actions Bot added the area-PAL-coreclr only for closed issues label Nov 20, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@krwq krwq changed the title Fix native Linux cgroup path decoding Fix CPU and memory limit detection for Linux cgroup paths with whitespace or escapes Nov 20, 2026
Comment thread src/native/minipal/mountinfo.h Resolved

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.

🔵 Needs a closer look

Linux CPU-count and automatic GC-budget changes span three runtimes, and incomplete product builds and live resource-limited managed validation leave an integration gap requiring maintainer review.

No open issues 🎉

What changed in this PR

Fixes Linux cgroup path parsing so CPU and memory limit discovery handles whitespace and escaped mount paths correctly. The change covers the shared minipal reader used by CoreCLR and NativeAOT, plus Mono’s separate reader, without changing resource-limit policy or adding public APIs.

Changes:

  • Add a shared helper that decodes supported mountinfo escapes exactly once, preserving unknown and truncated sequences.
  • Preserve cgroup membership paths through the line ending instead of truncating at whitespace.
  • Add six native parsing tests and register them in the native driver and managed launcher.
File Description
src/​tests/​native/​minipal/​NativeMinipalTests.cs Adds the mountinfo suite to the managed launcher.
src/​tests/​native/​minipal/​native-minipal-tests-driver.c Registers the native mountinfo test group.
src/​tests/​native/​minipal/​mountinfo-tests.c Tests escapes, decode-once behavior, malformed inputs, capacity limits, and long paths.
src/​tests/​native/​minipal/​CMakeLists.txt Includes the new native test source.
src/​native/​minipal/​mountinfo.h Adds shared mountinfo path extraction and decoding.
src/​native/​minipal/​cgroup.c Uses decoded mount paths and preserves membership whitespace.
src/​mono/​mono/​utils/​mono-cgroup.c Applies the same parsing fixes to Mono.

Review effort: Balanced

Keep the parser declaration in the C-compatible header and compile its unchanged implementation once per minipal library. Keep the unescape helper private to mountinfo.c and include the source in regular and AOT minipal builds.

Co-authored-by: Copilot App <[email protected]>
Comment thread src/mono/mono/utils/mono-cgroup.c Resolved
Comment thread src/native/minipal/mountinfo.h Resolved
krwq and others added 3 commits November 25, 2026 10:34
Move the unchanged mountinfo parsing helpers into the shared cgroup reader. Remove the separate helper source/header and the Mono changes. Exercise the existing native cgroup getters with Linux-only procfs fixtures.

Co-authored-by: Copilot App <[email protected]>
Compile the real cgroup reader with test-local entrypoints and I/O substitutions instead of GNU linker interposition. Preserve all fixture cases, remove ABI-specific wrappers and the special build definition, and scope includes to the test target.

Co-authored-by: Copilot App <[email protected]>
Comment thread src/native/minipal/mountinfo.c Resolved
Use a space-delimited scanset for mountinfo fields and keep decoding private. Focus procfs fixtures on resolved CPU/memory paths, retain decode-once and truncated-escape coverage, and restore the original CMake include setup.

Co-authored-by: Copilot App <[email protected]>

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

area-PAL-coreclr only for closed issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Environment.ProcessorCount and GC.GetGCMemoryInfo can overestimate resources for some Linux cgroup paths

3 participants