Skip to content

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

Open
cshung wants to merge 1 commit intodotnet:mainfrom
cshung:fix/gc-largepages-skip-tail-decommit
Open

Skip decommit for large pages and add fake large pages test mode#127290
cshung wants to merge 1 commit 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

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
@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
heap_segment* gc_heap::segment_standby_list;
#endif //USE_REGIONS
bool gc_heap::use_large_pages_p = 0;
bool gc_heap::large_pages_fake_mode_p = 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

probably better to name this large_pages_force_mode or something.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd prefer large_pages_simulation_mode_p or large_pages_emulation_mode_p

Comment thread src/coreclr/gc/memory.cpp
{
memclr ((uint8_t*)address, (uint8_t*)end_of_data - (uint8_t*)address);
}
bool decommit_succeeded_p = GCToOSInterface::VirtualDecommit (address, size);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

think you need a similar fix in gc_heap::decommit_region ?

Comment thread src/coreclr/gc/memory.cpp
// VirtualDecommit is a no-op for large pages so skip it and update
// committed bookkeeping directly. Memory clearing is handled below.
decommit_succeeded_p = true;
reduce_committed_bytes (page_start, decommit_size, bucket, h_number, true);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If decommit is a noop, why are we reducing comitted bytes?

@VSadov
Copy link
Copy Markdown
Member

VSadov commented Apr 22, 2026

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

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

4 participants