libkernel: anonymous mmap (MAP_PRIVATE|MAP_ANON) is not charged as flexible memory #7120

Closed
opened 2026-10-17 12:20:10 +00:00 by oneandonlydean · 6 comments
oneandonlydean commented 2026-10-17 12:20:10 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T12:20:10Z.


Two gaps in flexible-memory accounting, found while reviewing #7112. Main (c9e6c45de) behaves the same way, so neither is a regression from that PR, and neither is in the paths it changes.

1. MAP_FIXED over flexible pages keeps their charge. sceKernelMmap is a thin wrapper over mmap_nid_postfix. A MAP_PRIVATE|MAP_ANON|MAP_FIXED map inside the arena without MAP_EXCL (the DoMapAnon / GuestMapFixed placement), or a file map with MAP_FIXED, can replace pages that were mapped as flexible. Neither path touches g_flexibleRanges (DirectMemory/Export.cpp), so the replaced pages stay charged until munmap, and the used figure reads higher than what is actually mapped as flexible. It's the same overstated-used class that #7112 fixes for direct and reserved memory.

2. Anonymous mmap is never charged as flexible. Plain anonymous mmap doesn't add to g_flexibleRanges on main. If anonymous maps draw from the flexible budget on real hardware, the used figure reads lower than a title would see. I haven't confirmed the hardware behaviour, so treat this point as a question.

sceKernelJitMapSharedMemory isn't affected by point 1: DoMapShared calls MapPlaced(*addr, len, PROT_NONE, 0, PS5_PAGE_SIZE) in DirectMemory/DirectMemory.cpp, so the address is only a hint, never MAP_FIXED, and it can't land on a live flexible range.

Why it matters: titles read the flexible figure at boot, so counting too much or too little can change what a title decides to allocate. I don't know of a title that hits either gap yet; this comes from reading the code.

Questions for whoever owns the kernel memory code: should MAP_FIXED over a flexible range release the replaced pages' charge? And should anonymous mmap be recorded in g_flexibleRanges the way the flexible map call is?

Related: #5260 (munmap release of flexible memory), #7112 (release scoping).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5898257420 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7120). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T12:20:10Z. --- Two gaps in flexible-memory accounting, found while reviewing #7112. Main (c9e6c45de) behaves the same way, so neither is a regression from that PR, and neither is in the paths it changes. **1. MAP_FIXED over flexible pages keeps their charge.** `sceKernelMmap` is a thin wrapper over `mmap_nid_postfix`. A `MAP_PRIVATE|MAP_ANON|MAP_FIXED` map inside the arena without `MAP_EXCL` (the `DoMapAnon` / `GuestMapFixed` placement), or a file map with `MAP_FIXED`, can replace pages that were mapped as flexible. Neither path touches `g_flexibleRanges` (`DirectMemory/Export.cpp`), so the replaced pages stay charged until `munmap`, and the used figure reads higher than what is actually mapped as flexible. It's the same overstated-used class that #7112 fixes for direct and reserved memory. **2. Anonymous mmap is never charged as flexible.** Plain anonymous `mmap` doesn't add to `g_flexibleRanges` on main. If anonymous maps draw from the flexible budget on real hardware, the used figure reads lower than a title would see. I haven't confirmed the hardware behaviour, so treat this point as a question. `sceKernelJitMapSharedMemory` isn't affected by point 1: `DoMapShared` calls `MapPlaced(*addr, len, PROT_NONE, 0, PS5_PAGE_SIZE)` in `DirectMemory/DirectMemory.cpp`, so the address is only a hint, never `MAP_FIXED`, and it can't land on a live flexible range. Why it matters: titles read the flexible figure at boot, so counting too much or too little can change what a title decides to allocate. I don't know of a title that hits either gap yet; this comes from reading the code. Questions for whoever owns the kernel memory code: should `MAP_FIXED` over a flexible range release the replaced pages' charge? And should anonymous `mmap` be recorded in `g_flexibleRanges` the way the flexible map call is? Related: #5260 (munmap release of flexible memory), #7112 (release scoping).
MarcZX10 commented 2026-10-17 12:20:27 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T12:20:27Z.


I checked the current PR inventory and source paths. The MAP_FIXED replacement accounting is already implemented by open #6491, and the current overlap report for #6491 shows shared edits with #7112 in the same flexible-range accounting helpers. I would avoid a competing patch there; #6491 needs to be coordinated/rebased with #7112.

The anonymous mmap point remains a separate question, but the issue says its flexible-budget behavior on retail hardware is unconfirmed. Before changing accounting, could you provide a console/API observation or a title repro showing that anonymous mappings consume the flexible-memory budget? Without that evidence, charging every anonymous map risks changing the budget semantics based only on host mapping behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233809093 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7120#issuecomment-6233809093). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-17T12:20:27Z. --- I checked the current PR inventory and source paths. The `MAP_FIXED` replacement accounting is already implemented by open [#6491](https://github.com/boykopovar/AnyPS5/pull/6491), and the current overlap report for #6491 shows shared edits with #7112 in the same flexible-range accounting helpers. I would avoid a competing patch there; #6491 needs to be coordinated/rebased with #7112. The anonymous `mmap` point remains a separate question, but the issue says its flexible-budget behavior on retail hardware is unconfirmed. Before changing accounting, could you provide a console/API observation or a title repro showing that anonymous mappings consume the flexible-memory budget? Without that evidence, charging every anonymous map risks changing the budget semantics based only on host mapping behavior.
MarcZX10 commented 2026-10-17 18:46:17 +00:00 (Migrated from github.com)
Author
Contributor

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


Correction to my earlier overlap note: after inspecting the full #6491 diff, it fixes fixed replacement through sceKernelMapFlexibleMemory itself; it does not touch mmap_nid_postfix or the successful MAP_FIXED anonymous/file-backed paths described in point 1 here. So #6491 is related interval-accounting work, but it is not the same POSIX mmap fix. My current draft addresses only those successful fixed mmap paths (the unconfirmed anonymous-mmap charging question remains out of scope). It still shares the internal accounting helper/file with open #7112, so I have asked that PR author about sequencing before publishing.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6238858649 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7120#issuecomment-6238858649). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-17T18:46:17Z. --- Correction to my earlier overlap note: after inspecting the full #6491 diff, it fixes fixed replacement through `sceKernelMapFlexibleMemory` itself; it does not touch `mmap_nid_postfix` or the successful `MAP_FIXED` anonymous/file-backed paths described in point 1 here. So #6491 is related interval-accounting work, but it is not the same POSIX mmap fix. My current draft addresses only those successful fixed mmap paths (the unconfirmed anonymous-mmap charging question remains out of scope). It still shares the internal accounting helper/file with open #7112, so I have asked that PR author about sequencing before publishing.
MarcZX10 commented 2026-10-17 18:52:05 +00:00 (Migrated from github.com)
Author
Contributor

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


Opened PR #7242 for the confirmed successful MAP_FIXED anonymous/file-backed mmap replacement gap. It releases the replaced flexible-accounting interval only after a successful mapping and adds accounting/content regressions for middle-page replacement. The separate question about charging ordinary anonymous mmap remains unchanged because its retail behavior is unconfirmed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6238954145 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7120#issuecomment-6238954145). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-17T18:52:05Z. --- Opened [PR #7242](https://github.com/boykopovar/AnyPS5/pull/7242) for the confirmed successful `MAP_FIXED` anonymous/file-backed mmap replacement gap. It releases the replaced flexible-accounting interval only after a successful mapping and adds accounting/content regressions for middle-page replacement. The separate question about charging ordinary anonymous mmap remains unchanged because its retail behavior is unconfirmed.
MarcZX10 commented 2026-10-18 05:42:20 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T05:42:20Z.


Update: #7242 was merged at 5a8677066, resolving the confirmed MAP_FIXED replacement-accounting gap in point 1. The separate question in point 2—whether ordinary anonymous mappings consume the flexible-memory budget on retail hardware—still lacks evidence, so I am leaving that behavior unclaimed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6246204251 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7120#issuecomment-6246204251). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T05:42:20Z. --- Update: #7242 was merged at `5a8677066`, resolving the confirmed `MAP_FIXED` replacement-accounting gap in point 1. The separate question in point 2—whether ordinary anonymous mappings consume the flexible-memory budget on retail hardware—still lacks evidence, so I am leaving that behavior unclaimed.
oneandonlydean commented 2026-10-20 06:52:27 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T06:52:27Z.


Gap 1 (MAP_FIXED keeping the old flexible charge) was fixed by #7242 (5a8677066). Gap 2, anonymous mmap not being charged as flexible, is still open as far as I can tell. Is that right, or did #7242 or another change cover it? If it's still open I'll keep this issue for gap 2 only.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6285200114 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7120#issuecomment-6285200114). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T06:52:27Z. --- Gap 1 (MAP_FIXED keeping the old flexible charge) was fixed by #7242 (5a8677066). Gap 2, anonymous mmap not being charged as flexible, is still open as far as I can tell. Is that right, or did #7242 or another change cover it? If it's still open I'll keep this issue for gap 2 only.
MarcZX10 commented 2026-10-21 04:15:02 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T04:15:02Z.


I checked the current mmap_nid_postfix path on main after #7242. Successful fixed anonymous and file-backed mappings call ReleaseFlexibleMappingRange, so the confirmed replacement-accounting gap is resolved. Ordinary MAP_PRIVATE|MAP_ANON still returns from DoMapAnon without being added to the flexible-range tracker. So gap 2 remains open as a code observation, but its console budget semantics are still unverified; I would keep it as a question and not change accounting until we have a hardware/API measurement.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6307886624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7120#issuecomment-6307886624). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T04:15:02Z. --- I checked the current `mmap_nid_postfix` path on main after #7242. Successful fixed anonymous and file-backed mappings call `ReleaseFlexibleMappingRange`, so the confirmed replacement-accounting gap is resolved. Ordinary `MAP_PRIVATE|MAP_ANON` still returns from `DoMapAnon` without being added to the flexible-range tracker. So gap 2 remains open as a code observation, but its console budget semantics are still unverified; I would keep it as a question and not change accounting until we have a hardware/API measurement.
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#7120
No description provided.