Skip to content

Skip decommit for large pages and add fake large pages test mode#127290

Merged
janvorli merged 3 commits intodotnet:mainfrom
cshung:fix/gc-largepages-skip-tail-decommit
Apr 28, 2026
Merged

Skip decommit for large pages and add fake large pages test mode#127290
janvorli merged 3 commits intodotnet:mainfrom
cshung:fix/gc-largepages-skip-tail-decommit

Conversation

@cshung
Copy link
Copy Markdown
Contributor

@cshung cshung commented Apr 22, 2026

With large pages, VirtualDecommit is a no-op since large pages cannot be partially decommitted. PR #126929 fixed the resulting stale data corruption by adding memclr in virtual_decommit, but this approach has downsides: the memory is never returned to the OS, yet we pay for the clearing and produce misleading committed/used bookkeeping.

Instead, skip the decommit entirely for large pages:

  1. distribute_free_regions: skip the aggressive tail-region decommit (the committed-but-unallocated tail of in-use regions). This was the path that caused the heap corruption in GC heap corruption with GCLargePages #126903.

  2. decommit_heap_segment: skip the whole-segment decommit used for segment hoarding and BGC segment deletion. Same class of issue: committed/used are lowered but physical memory retains stale data.

  3. decommit_region: bypass virtual_decommit and call reduce_committed_bytes directly, since decommit_region already handles large pages correctly by clearing memory itself.

  4. virtual_decommit: add an assert that it is never called for heap memory when large pages are on. This catches any future caller that forgets to handle the large pages case. The end_of_data parameter and no-op ternary added by fix for largepages with agressive decommit logic #126929 are removed.

Add GCLargePages=2 mode that simulates large pages using small pages: sets use_large_pages_p=true but reserves with normal pages and commits everything upfront. This exercises all large page GC code paths without requiring OS large page setup or privileges, enabling CI testing.

Fix #126903

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Apr 22, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/gc
See info in area-owners.md if you want to be subscribed.

@mangod9
Copy link
Copy Markdown
Member

mangod9 commented Apr 22, 2026

@janvorli. Thanks @cshung for making the change, I like that we can now force largePages codepath within CI.

Comment thread src/coreclr/gc/gc.cpp Outdated
Comment thread src/coreclr/gc/memory.cpp
Comment thread src/coreclr/gc/memory.cpp
@VSadov
Copy link
Copy Markdown
Member

VSadov commented Apr 22, 2026

The test fails on x86. Perhaps just make the test incompatible with 32bit?

cshung added 3 commits April 24, 2026 10:35
With large pages, VirtualDecommit is a no-op since large pages cannot be
partially decommitted. PR dotnet#126929 fixed the resulting stale data corruption
by adding memclr in virtual_decommit, but this approach has downsides:
the memory is never returned to the OS, yet we pay for the clearing and
produce misleading committed/used bookkeeping.

Instead, skip the decommit entirely for large pages:

1. distribute_free_regions: skip the aggressive tail-region decommit
   (the committed-but-unallocated tail of in-use regions). This was the
   path that caused the heap corruption in dotnet#126903.

2. decommit_heap_segment: skip the whole-segment decommit used for
   segment hoarding and BGC segment deletion. Same class of issue:
   committed/used are lowered but physical memory retains stale data.

3. decommit_region: bypass virtual_decommit and call
   reduce_committed_bytes directly, since decommit_region already
   handles large pages correctly by clearing memory itself.

4. virtual_decommit: add an assert that it is never called for heap
   memory when large pages are on. This catches any future caller that
   forgets to handle the large pages case. The end_of_data parameter
   and no-op ternary added by dotnet#126929 are removed.

Add GCLargePages=2 mode that simulates large pages using small pages:
sets use_large_pages_p=true but reserves with normal pages and commits
everything upfront. This exercises all large page GC code paths without
requiring OS large page setup or privileges, enabling CI testing.

Fix dotnet#126903
Address review feedback from mangod9 and janvorli.
Rename large_pages_fake_mode_p to large_pages_emulation_mode_p and
update comments to use emulation terminology throughout.

Disable test on 32-bit: GCHeapHardLimit=0xC0000000 exceeds the virtual
address space and GCLargePages is gated by HOST_64BIT.
Comment thread src/tests/GC/API/GC/Collect_Aggressive_LargePages.csproj
Copy link
Copy Markdown
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@janvorli janvorli merged commit 158bbf4 into dotnet:main Apr 28, 2026
109 of 113 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-GC-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GC heap corruption with GCLargePages

5 participants