[PR #742] [CLOSED] xmem: coalesce contiguous runs in XMemMapPhysicalPages #780

Closed
opened 2026-11-26 20:19:01 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/742
Author: @madhyn
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: perf/xmem-coalesce-map-physical-pages


📝 Commits (2)

  • 6ed318f xmem: coalesce contiguous runs in XMemMapPhysicalPages
  • f6cfa2f xmem: add range-aware unmap_range to avoid O(N^2) bulk unmap

📊 Changes

2 files changed (+163 additions, -23 deletions)

View changed files

📝 runtime/shims/xmem.cpp (+128 -23)
📝 runtime/test/xmem_test.cpp (+35 -0)

📄 Description

What and why

During retail startup (1.0.2.1), after CopyDescriptorsSimple, the title calls XMemMapPhysicalPages with 99,776 contiguous pages (6,236.0 MiB) in a single call. Mapping them one 64 KiB page at a time issues ~100k separate VirtualFree(MEM_PRESERVE_PLACEHOLDER) + MapViewOfFile3 calls, which under Wine 11.22 takes longer than 30 seconds and triggers the title's internal 30 s watchdog assert (0x142215c35).

This change coalesces contiguous runs of pages from the same Section and consecutive offsets into a single isolate_placeholder + MapViewOfFile3 call, while preserving per-page unmap/remap semantics:

  • MappedPage records view_base, view_pages, and view_protect.
  • unmap_page splits a multi-page view when a middle page is unmapped or overwritten, re-mapping the surviving prefix and suffix runs while keeping their backing data intact.
  • If a coalesced map_view_run fails (e.g. pre-split placeholders), XMemMapPhysicalPages falls back to mapping that run page-by-page.

Testing

  • Added test_coalesced_run_partial_unmap in runtime/test/xmem_test.cpp (maps 4 contiguous pages, unmaps a middle page, verifies prefix/suffix data survival, remaps the middle page back, unmaps and frees).
  • ctest -R xmem under Wine 11.22 (Arch Linux):
    • d3d12_xmem_placed: passed.
    • xmem: 223/228 checks passed, including all 11 checks of test_coalesced_run_partial_unmap (the 5 existing failures are Wine 11.22 VirtualQuery / PAGE_READONLY / commit-charge checks that also fail on main).
  • Retail gta6rt.sh --run (with #385 + #445 stack): the 99,776-page (6,236 MiB) XMemMapPhysicalPages call dropped from >30,000 ms (watchdog hang) to 14 ms, advancing boot through device slot 74, dstorage (CreateStatusArray, CreateQueue1, CPU zlib inflate), and reaching device slot 108.

AI-assisted: yes


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/742 **Author:** [@madhyn](https://github.com/madhyn) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `perf/xmem-coalesce-map-physical-pages` --- ### 📝 Commits (2) - [`6ed318f`](https://github.com/ubiquitarianism/leonida/commit/6ed318f2e3eb8467f36a4812f943b29fd58d74c2) xmem: coalesce contiguous runs in XMemMapPhysicalPages - [`f6cfa2f`](https://github.com/ubiquitarianism/leonida/commit/f6cfa2f252303cdd28d5e5d7ab54ab2013ec2735) xmem: add range-aware unmap_range to avoid O(N^2) bulk unmap ### 📊 Changes **2 files changed** (+163 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/xmem.cpp` (+128 -23) 📝 `runtime/test/xmem_test.cpp` (+35 -0) </details> ### 📄 Description ### What and why During retail startup (1.0.2.1), after `CopyDescriptorsSimple`, the title calls `XMemMapPhysicalPages` with **99,776 contiguous pages (6,236.0 MiB)** in a single call. Mapping them one 64 KiB page at a time issues ~100k separate `VirtualFree(MEM_PRESERVE_PLACEHOLDER)` + `MapViewOfFile3` calls, which under Wine 11.22 takes longer than 30 seconds and triggers the title's internal 30 s watchdog assert (`0x142215c35`). This change coalesces contiguous runs of pages from the same `Section` and consecutive offsets into a single `isolate_placeholder` + `MapViewOfFile3` call, while preserving per-page unmap/remap semantics: - `MappedPage` records `view_base`, `view_pages`, and `view_protect`. - `unmap_page` splits a multi-page view when a middle page is unmapped or overwritten, re-mapping the surviving prefix and suffix runs while keeping their backing data intact. - If a coalesced `map_view_run` fails (e.g. pre-split placeholders), `XMemMapPhysicalPages` falls back to mapping that run page-by-page. ### Testing - Added `test_coalesced_run_partial_unmap` in `runtime/test/xmem_test.cpp` (maps 4 contiguous pages, unmaps a middle page, verifies prefix/suffix data survival, remaps the middle page back, unmaps and frees). - `ctest -R xmem` under Wine 11.22 (Arch Linux): - `d3d12_xmem_placed`: passed. - `xmem`: 223/228 checks passed, including all 11 checks of `test_coalesced_run_partial_unmap` (the 5 existing failures are Wine 11.22 `VirtualQuery` / `PAGE_READONLY` / commit-charge checks that also fail on `main`). - Retail `gta6rt.sh --run` (with #385 + #445 stack): the 99,776-page (6,236 MiB) `XMemMapPhysicalPages` call dropped from **>30,000 ms (watchdog hang)** to **14 ms**, advancing boot through device slot 74, `dstorage` (`CreateStatusArray`, `CreateQueue1`, CPU zlib inflate), and reaching device slot 108. AI-assisted: yes --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:19:01 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#780
No description provided.