libkernel: every direct-memory map/remap walks the phys range page-by-page — quadratic on sliding-window remap patterns #10521

Closed
opened 2026-10-24 07:55:13 +00:00 by akkadbakkad89 · 0 comments
akkadbakkad89 commented 2026-10-24 07:55:13 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-24T07:55:13Z.


What happens

Every sceKernelMapDirectMemory / remap over a physical range iterates g_physPages — a std::map with one node per 16 KiB page — twice: once in ValidatePhysicalRange (contains per page) and once in AddMapping (at per page for backing-coalescing). A 13.5 GiB range is ~880k node lookups per call. A title that slides a 64 KiB-stepped remap window over a ~13.5 GiB arena issues ~216k such calls — O(pages²) total, the title effectively hangs inside remap fixed during boot.

Additionally, sceKernelAllocateDirectMemory itself inserts one physPages node per page in CreateDirectMemoryBacking, so the 13.5 GiB arena alloc pays ~880k map inserts up front — and on Windows nothing downstream ever reads physPages (only SharedBacking/WriteSharedBacking, which are Linux-only, consume it).

Title, test or build step

Grand Theft Auto: Vice City – The Definitive Edition, PPSA03530 01.00 — the sliding-window pass is part of its UE4 heap arena setup during boot. Reproducible with APS5_TRACE_MEMORY=1: ~216k [memory] remap fixed <va>+<len> lines with len shrinking by 64 KiB each call. Bring-up thread: https://github.com/boykopovar/AnyPS5/discussions/10520

AnyPS5 commit

Observed on 4883171f9ffc+ (the per-page bookkeeping predates this); reproduced while investigating on a stacked branch.

System

Windows 11, MinGW build, RTX 3070.

Proposed direction (already working locally, happy to PR)

Keep a block-level interval index (g_physBlocks, one entry per pool _commit, split on partial free) next to the page map: ValidatePhysicalRange and AddMapping then run O(#committed blocks) instead of O(#16 KiB pages), and CreateDirectMemoryBacking/ForgetDirectMemory maintain both. physPages stays populated only where SharedBacking/WriteSharedBacking need it (Linux).

Two further observations from the same storm:

  1. The sliding pattern calls remap fixed on a range that is already covered by a mapping with identical phys alignment — a no-op shortcut in RemapFixedIntoRegistered skips the OS teardown/rebuild entirely and is a pure win independent of the index.
  2. The fencepost the same bitmap setup exposed (guest phys-page bitmap at 0x9000000000 reading one qword past a 32 KiB map when phys reaches ~15.75 GiB) is a separate semantic question — raised in the discussion thread above rather than here.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6010773382 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/10521). Originally posted by [akkadbakkad89](https://github.com/akkadbakkad89) on 2026-10-24T07:55:13Z. --- **What happens** Every `sceKernelMapDirectMemory` / remap over a physical range iterates `g_physPages` — a `std::map` with **one node per 16 KiB page** — twice: once in `ValidatePhysicalRange` (`contains` per page) and once in `AddMapping` (`at` per page for backing-coalescing). A 13.5 GiB range is ~880k node lookups per call. A title that slides a 64 KiB-stepped remap window over a ~13.5 GiB arena issues ~216k such calls — O(pages²) total, the title effectively hangs inside `remap fixed` during boot. Additionally, `sceKernelAllocateDirectMemory` itself inserts one `physPages` node per page in `CreateDirectMemoryBacking`, so the 13.5 GiB arena alloc pays ~880k map inserts up front — and on Windows nothing downstream ever reads `physPages` (only `SharedBacking`/`WriteSharedBacking`, which are Linux-only, consume it). **Title, test or build step** Grand Theft Auto: Vice City – The Definitive Edition, `PPSA03530` 01.00 — the sliding-window pass is part of its UE4 heap arena setup during boot. Reproducible with `APS5_TRACE_MEMORY=1`: ~216k `[memory] remap fixed <va>+<len>` lines with `len` shrinking by 64 KiB each call. Bring-up thread: https://github.com/boykopovar/AnyPS5/discussions/10520 **AnyPS5 commit** Observed on `4883171f9ffc`+ (the per-page bookkeeping predates this); reproduced while investigating on a stacked branch. **System** Windows 11, MinGW build, RTX 3070. **Proposed direction** (already working locally, happy to PR) Keep a block-level interval index (`g_physBlocks`, one entry per pool `_commit`, split on partial free) next to the page map: `ValidatePhysicalRange` and `AddMapping` then run O(#committed blocks) instead of O(#16 KiB pages), and `CreateDirectMemoryBacking`/`ForgetDirectMemory` maintain both. `physPages` stays populated only where `SharedBacking`/`WriteSharedBacking` need it (Linux). Two further observations from the same storm: 1. The sliding pattern calls `remap fixed` on a range that is already covered by a mapping with identical phys alignment — a no-op shortcut in `RemapFixedIntoRegistered` skips the OS teardown/rebuild entirely and is a pure win independent of the index. 2. The fencepost the same bitmap setup exposed (guest phys-page bitmap at `0x9000000000` reading one qword past a 32 KiB map when phys reaches ~15.75 GiB) is a separate semantic question — raised in the discussion thread above rather than here.
Sign in to join this conversation.
No labels
No milestone
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.

Reference
boykopovar/AnyPS5#10521
No description provided.