fix(libkernel): scope flexible release to tracked ranges with release-then-record #7112

Merged
Pouare514 merged 1 commit from upstream-pr/7112 into main 2026-10-18 06:22:55 +00:00
Pouare514 commented 2026-10-17 12:05:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T12:05:09Z.


What

  • Scope flexible-memory release to tracked ranges in core/libs/prx/libkernel/DirectMemory/Export.cpp: extracted _releaseFlexibleLocked() (same interval erase/split), _releaseFlexible() locks + delegates. sceKernelMunmap of a non-flexible (direct/reserved) range no longer over-releases the flexible budget.
  • Release-then-record on successful fixed placements: _mapFlexible (flexible-over-flexible, fixes double-charge/leak), sceKernelMapDirectMemory/2, sceKernelMapNamedDirectMemory, sceKernelReserveVirtualRange, sceKernelBatchMap2/OpMapDirect (direct/reserved-over-flexible clears stale entries, so a later munmap of that range leaves Available unchanged).
  • Motivation: TLOU Part I asserts actual = total - flexAvailable - table[6] vs budget 140 MB; the log shows It is 17592186044416.xxx (= 2^64, wrapped negative), i.e. AvailableFlexible is overstated by untracked releases. No title-specific code, no budget-math changes, no new tracing infra.

Tested

  • New CheckFlexibleFixedReplacement + CheckMunmapScopedFlexibleRelease in guest_memory: flexible map drops Available by N; fixed-remap keeps before-4p; munmap of direct/reserve leaves Available unchanged; flexible subrange munmap reclaims exactly. Fail without (abort 0xc0000409) / pass with. guest_memory green post-rebase (WinLibs GCC 15.2, Release); neighbors guest_kernel_sys, guest_memory_pool_stats, guest_jit_shared_memory, guest_flexible_memory_* green.
  • check_conventions.py clean, git diff --check clean.
  • TLOU game-run proof pending (repro: drop rebuilt libkernel.prx into run dir, run tlou.exe, the Code and Data line must stop wrapping 2^64).

Checklist

  • Based on current main; no other open PR implements the same change
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • No new unimplemented paths or silent stubs
  • No notes, investigation files or images added to the repository
  • New third-party code: none
  • Generic behaviour, not specific to one title
  • Depends on: none (interaction noted with #7105 _mapFlexible hunk and #6491 budget replaced subtraction — both open)
  • Unblocks: none
  • GPUs tested: n/a for this libkernel accounting change
  • AI-assisted: yes; I am responsible for this change
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4899903133 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7112). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-17T12:05:09Z. --- ### What - Scope flexible-memory release to tracked ranges in `core/libs/prx/libkernel/DirectMemory/Export.cpp`: extracted `_releaseFlexibleLocked()` (same interval erase/split), `_releaseFlexible()` locks + delegates. `sceKernelMunmap` of a non-flexible (direct/reserved) range no longer over-releases the flexible budget. - Release-then-record on successful fixed placements: `_mapFlexible` (flexible-over-flexible, fixes double-charge/leak), `sceKernelMapDirectMemory/2`, `sceKernelMapNamedDirectMemory`, `sceKernelReserveVirtualRange`, `sceKernelBatchMap2/OpMapDirect` (direct/reserved-over-flexible clears stale entries, so a later munmap of that range leaves `Available` unchanged). - Motivation: TLOU Part I asserts `actual = total - flexAvailable - table[6]` vs budget 140 MB; the log shows `It is 17592186044416.xxx` (= 2^64, wrapped negative), i.e. `AvailableFlexible` is overstated by untracked releases. No title-specific code, no budget-math changes, no new tracing infra. ### Tested - New `CheckFlexibleFixedReplacement` + `CheckMunmapScopedFlexibleRelease` in `guest_memory`: flexible map drops `Available` by N; fixed-remap keeps `before-4p`; munmap of direct/reserve leaves `Available` unchanged; flexible subrange munmap reclaims exactly. **Fail without** (abort `0xc0000409`) / **pass with**. `guest_memory` green post-rebase (WinLibs GCC 15.2, Release); neighbors `guest_kernel_sys`, `guest_memory_pool_stats`, `guest_jit_shared_memory`, `guest_flexible_memory_*` green. - `check_conventions.py` clean, `git diff --check` clean. - TLOU game-run proof pending (repro: drop rebuilt `libkernel.prx` into run dir, run `tlou.exe`, the `Code and Data` line must stop wrapping 2^64). ### Checklist - [x] Based on current `main`; no other open PR implements the same change - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] No new unimplemented paths or silent stubs - [x] No notes, investigation files or images added to the repository - [x] New third-party code: none - [x] Generic behaviour, not specific to one title - [x] Depends on: none (interaction noted with #7105 `_mapFlexible` hunk and #6491 budget `replaced` subtraction — both open) - [x] Unblocks: none - [x] GPUs tested: n/a for this libkernel accounting change - [x] AI-assisted: yes; I am responsible for this change
Pouare514 commented 2026-10-17 12:05:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T12:05:33Z.


Manual overlap check for this PR's files (Export.cpp, GuestMemory.cpp): _releaseFlexible scoping + release-then-record. _releaseFlexibleLocked helper duplicates #6491's same helper textually - #6491's budget replaced subtraction deliberately NOT duplicated here (tight-budget same-range remap still needs #6491); whichever lands first, the other rebases the helper. #6311 routes POSIX munmap through sceKernelMunmap (PosixMemory.cpp) - complementary, different path, rebase-only. #7105 shares the _mapFlexible success-insert hunk textually (lock-split vs release-then-record) - coordinate at merge; all other hunks disjoint. Correction to the PR body checklist: #6515 (open) implements the same direct-over-flexible release idea for the 4 MapDirect callsites via a _mapDirect helper - partial overlap, not full duplication (this PR additionally scopes _releaseFlexible to tracked ranges, covers _mapFlexible + ReserveVirtualRange, and pins behavior in guest_memory). Rebase plan: if #6515 lands first, this PR adopts the _mapDirect helper shape and keeps scoping + tests; if this lands first, #6515 rebases onto it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233680498 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7112#issuecomment-6233680498). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-17T12:05:33Z. --- Manual overlap check for this PR's files (Export.cpp, GuestMemory.cpp): _releaseFlexible scoping + release-then-record. _releaseFlexibleLocked helper duplicates #6491's same helper textually - #6491's budget replaced subtraction deliberately NOT duplicated here (tight-budget same-range remap still needs #6491); whichever lands first, the other rebases the helper. #6311 routes POSIX munmap through sceKernelMunmap (PosixMemory.cpp) - complementary, different path, rebase-only. #7105 shares the _mapFlexible success-insert hunk textually (lock-split vs release-then-record) - coordinate at merge; all other hunks disjoint. Correction to the PR body checklist: #6515 (open) implements the same direct-over-flexible release idea for the 4 MapDirect callsites via a _mapDirect helper - partial overlap, not full duplication (this PR additionally scopes _releaseFlexible to tracked ranges, covers _mapFlexible + ReserveVirtualRange, and pins behavior in guest_memory). Rebase plan: if #6515 lands first, this PR adopts the _mapDirect helper shape and keeps scoping + tests; if this lands first, #6515 rebases onto it.
github-actions[bot] commented 2026-10-17 12:17:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T12:17:03Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6491 core/libs/prx/libkernel/DirectMemory/Export.cpp L107, L118, L124-134
PR 6515 core/libs/prx/libkernel/DirectMemory/Export.cpp L231, L236, L247, L507

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4449 core/libs/tests/GuestMemory.cpp
PR 5021 core/libs/prx/libkernel/DirectMemory/Export.cpp
core/libs/tests/GuestMemory.cpp
PR 6044 core/libs/tests/GuestMemory.cpp
PR 6222 core/libs/prx/libkernel/DirectMemory/Export.cpp
PR 6565 core/libs/tests/GuestMemory.cpp
PR 7201 core/libs/prx/libkernel/DirectMemory/Export.cpp
PR 7332 core/libs/prx/libkernel/DirectMemory/Export.cpp
core/libs/tests/GuestMemory.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233779457 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7112#issuecomment-6233779457). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T12:17:03Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 6491](https://www.github.com/boykopovar/AnyPS5/pull/6491) | `core/libs/prx/libkernel/DirectMemory/Export.cpp` [L107](https://github.com/boykopovar/AnyPS5/blob/ba38dccb3dc8cd4aa349ddfff86c05f4a6cfc614/core/libs/prx/libkernel/DirectMemory/Export.cpp#L107), [L118](https://github.com/boykopovar/AnyPS5/blob/ba38dccb3dc8cd4aa349ddfff86c05f4a6cfc614/core/libs/prx/libkernel/DirectMemory/Export.cpp#L118), [L124-134](https://github.com/boykopovar/AnyPS5/blob/ba38dccb3dc8cd4aa349ddfff86c05f4a6cfc614/core/libs/prx/libkernel/DirectMemory/Export.cpp#L124-L134) | | | | [PR 6515](https://www.github.com/boykopovar/AnyPS5/pull/6515) | `core/libs/prx/libkernel/DirectMemory/Export.cpp` [L231](https://github.com/boykopovar/AnyPS5/blob/ba38dccb3dc8cd4aa349ddfff86c05f4a6cfc614/core/libs/prx/libkernel/DirectMemory/Export.cpp#L231), [L236](https://github.com/boykopovar/AnyPS5/blob/ba38dccb3dc8cd4aa349ddfff86c05f4a6cfc614/core/libs/prx/libkernel/DirectMemory/Export.cpp#L236), [L247](https://github.com/boykopovar/AnyPS5/blob/ba38dccb3dc8cd4aa349ddfff86c05f4a6cfc614/core/libs/prx/libkernel/DirectMemory/Export.cpp#L247), [L507](https://github.com/boykopovar/AnyPS5/blob/ba38dccb3dc8cd4aa349ddfff86c05f4a6cfc614/core/libs/prx/libkernel/DirectMemory/Export.cpp#L507) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 4449](https://www.github.com/boykopovar/AnyPS5/pull/4449) | `core/libs/tests/GuestMemory.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`core/libs/tests/GuestMemory.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestMemory.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/DirectMemory/Export.cpp` | | [PR 6565](https://www.github.com/boykopovar/AnyPS5/pull/6565) | `core/libs/tests/GuestMemory.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/DirectMemory/Export.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`core/libs/tests/GuestMemory.cpp` |
Pouare514 commented 2026-10-17 12:41:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T12:41:35Z.


Manual overlap check for this PR's files (Export.cpp release paths, GuestMemory.cpp tests): #6491 true partial overlap - identical _releaseFlexibleLocked helper + identical release-then-record in _mapFlexible (theirs adds budget replaced subtraction, deliberately not duplicated here); whoever lands first, the other drops the duplicated hunk. #6515 true partial overlap - same direct-over-flexible release (their _mapDirect helper vs my inline calls); heads-up already exchanged both ways. #5021 same main() region (their GuestArenaAvailable guards vs my 2 test calls) - textual conflict possible, mechanical merge. #6044 moves GuestMemory.cpp to memory/ - path move only, my tests follow on rebase. #4449/#6565 additive tests under different names - clean. #6222 macOS VirtualQuery + includes - disjoint hunks. #6311 different file and path (PosixMemory munmap routing) - complementary. Depends on stays none (no build dependency; merge-order coordination as above). On #7120: gap 1 (MAP_FIXED via sceKernelMmap/file maps over flexible keeps its charge) is the same bug class in paths this PR doesn't touch - happy to take it as a follow-up once this lands. Gap 2 (anonymous mmap charging) needs the hardware-behavior answer first.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6234000453 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7112#issuecomment-6234000453). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-17T12:41:35Z. --- Manual overlap check for this PR's files (Export.cpp release paths, GuestMemory.cpp tests): #6491 true partial overlap - identical _releaseFlexibleLocked helper + identical release-then-record in _mapFlexible (theirs adds budget replaced subtraction, deliberately not duplicated here); whoever lands first, the other drops the duplicated hunk. #6515 true partial overlap - same direct-over-flexible release (their _mapDirect helper vs my inline calls); heads-up already exchanged both ways. #5021 same main() region (their GuestArenaAvailable guards vs my 2 test calls) - textual conflict possible, mechanical merge. #6044 moves GuestMemory.cpp to memory/ - path move only, my tests follow on rebase. #4449/#6565 additive tests under different names - clean. #6222 macOS VirtualQuery + includes - disjoint hunks. #6311 different file and path (PosixMemory munmap routing) - complementary. Depends on stays none (no build dependency; merge-order coordination as above). On #7120: gap 1 (MAP_FIXED via sceKernelMmap/file maps over flexible keeps its charge) is the same bug class in paths this PR doesn't touch - happy to take it as a follow-up once this lands. Gap 2 (anonymous mmap charging) needs the hardware-behavior answer first.
Pouare514 commented 2026-10-17 12:43:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T12:43:32Z.


CI note: conventions/eligibility/intake green; unit pins (guest_memory + flexible-budget neighbors) verified green on Windows locally. The full Linux + Windows builds need collaborator dispatch (automatic full builds only run on main) - requesting dispatch when review allows. A TLOU game-run proof (Code-and-Data wrap check) is in progress and will be reported here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6234019830 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7112#issuecomment-6234019830). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-17T12:43:32Z. --- CI note: conventions/eligibility/intake green; unit pins (guest_memory + flexible-budget neighbors) verified green on Windows locally. The full Linux + Windows builds need collaborator dispatch (automatic full builds only run on main) - requesting dispatch when review allows. A TLOU game-run proof (Code-and-Data wrap check) is in progress and will be reported here.
Pouare514 commented 2026-10-17 12:49:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T12:49:26Z.


Game-run proof (replacing the pending note in the body): TLOU run dir with PR-head libs (fresh nid-patched libkernel.prx + libc.prx; libc refresh was loader-compat only - fresh libkernel imports AddPathProvider_nid_no_patch, absent from the stale deployed libc). BEFORE (stale libs): Code and Data section is too large. It is 17592186044416.016 and should be 140.000, then FATAL 0xc0000005 at tlou.exe+0x1a5ffaf. AFTER: no assert fires, sane Code And Global Data: 97.28 MB / Free Flex Mem: 60.72 MB, execution continues past 0x1A5FFAF/0x1A600B1 to tlou.exe+0x1a69806, where it stops on the pre-existing unrelated sceSslLoadCert throwing stub (merged in #519). Wrap fixed; next wall is a known stub, not a memory regression. Logs: tlou-base.log / tlou-flex.log in the run dir.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6234077706 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7112#issuecomment-6234077706). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-17T12:49:26Z. --- Game-run proof (replacing the pending note in the body): TLOU run dir with PR-head libs (fresh nid-patched libkernel.prx + libc.prx; libc refresh was loader-compat only - fresh libkernel imports AddPathProvider_nid_no_patch, absent from the stale deployed libc). BEFORE (stale libs): Code and Data section is too large. It is 17592186044416.016 and should be 140.000, then FATAL 0xc0000005 at tlou.exe+0x1a5ffaf. AFTER: no assert fires, sane Code And Global Data: 97.28 MB / Free Flex Mem: 60.72 MB, execution continues past 0x1A5FFAF/0x1A600B1 to tlou.exe+0x1a69806, where it stops on the pre-existing unrelated sceSslLoadCert throwing stub (merged in #519). Wrap fixed; next wall is a known stub, not a memory regression. Logs: tlou-base.log / tlou-flex.log in the run dir.
MarcZX10 commented 2026-10-17 18:44:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T18:44:55Z.


I am evaluating the separate #7120 gap: successful MAP_FIXED anonymous/file mappings replace flexible pages without updating g_flexibleRanges. Your current diff handles fixed flexible/direct/reserved mappings but not the POSIX mmap paths. I have a minimal draft calling the existing interval release helper only after successful fixed mmap and a regression for replacing the middle of a three-page flexible range. Since this touches DirectMemory/Export.cpp/accounting helpers, would you prefer I hold it for a follow-up based on your merged head, or do you see a conflict with your intended changes? I have not published or pushed it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6238836206 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7112#issuecomment-6238836206). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-17T18:44:55Z. --- I am evaluating the separate #7120 gap: successful `MAP_FIXED` anonymous/file mappings replace flexible pages without updating `g_flexibleRanges`. Your current diff handles fixed flexible/direct/reserved mappings but not the POSIX mmap paths. I have a minimal draft calling the existing interval release helper only after successful fixed mmap and a regression for replacing the middle of a three-page flexible range. Since this touches `DirectMemory/Export.cpp`/accounting helpers, would you prefer I hold it for a follow-up based on your merged head, or do you see a conflict with your intended changes? I have not published or pushed it.
Pouare514 commented 2026-10-18 00:33:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-18T00:33:20Z.


Thanks for checking first, and for scoping it to the POSIX mmap paths this PR deliberately doesn't touch. Hold it for a follow-up based on the merged head: your draft (interval release after successful fixed mmap + middle-of-three-pages regression) is complementary, not conflicting. Note it will build on this PR's _releaseFlexibleLocked helper - I'll keep that hunk stable; if this PR changes shape during review I'll flag you. Happy to review the draft whenever ready.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6243767518 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7112#issuecomment-6243767518). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-18T00:33:20Z. --- Thanks for checking first, and for scoping it to the POSIX mmap paths this PR deliberately doesn't touch. Hold it for a follow-up based on the merged head: your draft (interval release after successful fixed mmap + middle-of-three-pages regression) is complementary, not conflicting. Note it will build on this PR's _releaseFlexibleLocked helper - I'll keep that hunk stable; if this PR changes shape during review I'll flag you. Happy to review the draft whenever ready.
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!7112
No description provided.