fix(libkernel): map AMM pages from a fragmented pool #11838

Merged
oneandonlydean merged 9 commits from upstream-pr/11838 into main 2026-10-28 07:11:33 +00:00
oneandonlydean commented 2026-10-27 09:25:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-27T09:25:13Z.


What

A map into the AMM from the pool used to need one contiguous free run, and threw AMM: no free run of N bytes in the direct memory given to the mapper when the pool was fragmented, which aborts the process. The PS5 maps the mapper's pages through page tables, so a fragmented pool still serves the map. When no single run fits, the map now takes pages from several runs (first fit) and maps each contiguous group. If the pool holds fewer bytes than the map needs, it throws the same message as before.

The page bookkeeping lives in AmmPool.hpp. A map plans its pages without changing the pool, stages the page and reference records in their own maps, maps the runs, and only then splits the pool and moves the records in with no allocation, so a failed map (an allocation failure or a host map failure after some runs) leaves the pool and the records as they were. Adding a page inserts its reference record first and undoes it if the page record throws. Allocating pages for an unbacked part of a PRT range goes through the same staging, and puts the zero pages back if it fails.

Based on work by @BiladeiroSupremo in AnyPS5Wolverine (github.com/BiladeiroSupremo/AnyPS5Wolverine@c205785bab). The first commit is theirs; the others are ours: the original's log line and comment removed, a regression test for the map, and a fix that makes the page bookkeeping exception-safe (see below). The author reported the abort on an 11 MB map in a title (PPSA03671) about four minutes into a load.

Tested

  • Linux, Release build of head 74ad6b1a5f on main 5bc84038c4: full ninja and ninja libs build rc 0; ctest -R '^(guest_amm|guest_ampr)|public_headers': rc 0, 5 AMM/AMPR tests (including guest_amm_pool and the happy-path scattered map in guest_ampr_markers) and 65 of 65 public_headers tests passed.
  • guest_ampr_markers also allocates more pages for a PRT range than the pool holds and checks the range still reads zeros with the PRT protection afterwards, then allocates one page and writes it; with the previous behaviour (range left reserved) that check fails.
  • guest_amm_pool drives the bookkeeping directly with no host calls and replaces operator new to fail each allocation in turn: a three-page scatter over three single-page runs, a scatter that splits a run, a host map that fails after each run (including a PRT-style allocation over runs of 2, 1 and 2 pages), and page insertion. After every failure the pool and the page and reference records equal their snapshot and a following map succeeds; a map of one page more than the pool holds is still refused. The same test aborts on the earlier algorithm (a page released without its reference record under ASan, records changed after a failed allocation without it) and passes under ASan, UBSan and leak checking.
  • tools/check_conventions.py passes. Not tested: Windows and macOS (left to CI), and the title run the author reports.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source (not applicable)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5031564230 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11838). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-27T09:25:13Z. --- ### What A map into the AMM from the pool used to need one contiguous free run, and threw `AMM: no free run of N bytes in the direct memory given to the mapper` when the pool was fragmented, which aborts the process. The PS5 maps the mapper's pages through page tables, so a fragmented pool still serves the map. When no single run fits, the map now takes pages from several runs (first fit) and maps each contiguous group. If the pool holds fewer bytes than the map needs, it throws the same message as before. The page bookkeeping lives in `AmmPool.hpp`. A map plans its pages without changing the pool, stages the page and reference records in their own maps, maps the runs, and only then splits the pool and moves the records in with no allocation, so a failed map (an allocation failure or a host map failure after some runs) leaves the pool and the records as they were. Adding a page inserts its reference record first and undoes it if the page record throws. Allocating pages for an unbacked part of a PRT range goes through the same staging, and puts the zero pages back if it fails. Based on work by @BiladeiroSupremo in AnyPS5Wolverine (https://github.com/BiladeiroSupremo/AnyPS5Wolverine/commit/c205785bab3df838f49fe915d2b0642fcb5b98b4). The first commit is theirs; the others are ours: the original's log line and comment removed, a regression test for the map, and a fix that makes the page bookkeeping exception-safe (see below). The author reported the abort on an 11 MB map in a title (PPSA03671) about four minutes into a load. ### Tested - Linux, Release build of head `74ad6b1a5f` on main `5bc84038c4`: full `ninja` and `ninja libs` build rc 0; `ctest -R '^(guest_amm|guest_ampr)|public_headers'`: rc 0, 5 AMM/AMPR tests (including `guest_amm_pool` and the happy-path scattered map in `guest_ampr_markers`) and 65 of 65 `public_headers` tests passed. - `guest_ampr_markers` also allocates more pages for a PRT range than the pool holds and checks the range still reads zeros with the PRT protection afterwards, then allocates one page and writes it; with the previous behaviour (range left reserved) that check fails. - `guest_amm_pool` drives the bookkeeping directly with no host calls and replaces `operator new` to fail each allocation in turn: a three-page scatter over three single-page runs, a scatter that splits a run, a host map that fails after each run (including a PRT-style allocation over runs of 2, 1 and 2 pages), and page insertion. After every failure the pool and the page and reference records equal their snapshot and a following map succeeds; a map of one page more than the pool holds is still refused. The same test aborts on the earlier algorithm (a page released without its reference record under ASan, records changed after a failed allocation without it) and passes under ASan, UBSan and leak checking. - `tools/check_conventions.py` passes. Not tested: Windows and macOS (left to CI), and the title run the author reports. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source (not applicable) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-27 09:32:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T09:32:42Z.

Review: CHANGES_REQUESTED


I reviewed the fragmented-pool path and the new regression case. One failure path needs another pass: if _mapRun succeeds for a scattered run but a later _addPage throws while registering that run, mapped still points to the beginning of the run. The catch first calls _forgetPages, which releases already-registered pages from that run back into state.pool, then returns every page from mapped onward again. That can insert duplicate/overlapping free ranges and make later allocations reuse the same physical pages. Please make the rollback track exactly which pages were registered/released (including partial registration within a run), and add a focused failure-injection test if the allocator boundary can be injected. The ordinary successful fragmented-map test does not exercise this cleanup path. I have not approved; Windows is still pending and the Linux product test is skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5664916550 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11838#pullrequestreview-5664916550). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T09:32:42Z. **Review:** CHANGES_REQUESTED --- I reviewed the fragmented-pool path and the new regression case. One failure path needs another pass: if `_mapRun` succeeds for a scattered run but a later `_addPage` throws while registering that run, `mapped` still points to the beginning of the run. The catch first calls `_forgetPages`, which releases already-registered pages from that run back into `state.pool`, then returns every page from `mapped` onward again. That can insert duplicate/overlapping free ranges and make later allocations reuse the same physical pages. Please make the rollback track exactly which pages were registered/released (including partial registration within a run), and add a focused failure-injection test if the allocator boundary can be injected. The ordinary successful fragmented-map test does not exercise this cleanup path. I have not approved; Windows is still pending and the Linux product test is skipped.
Sign in to join this conversation.
No reviewers
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!11838
No description provided.