Repository navigation
fix(guest-arena): place guest address space in the PS5 window on Linux - #861
Conversation
|
@jeffrey12cali I don't have an arena PR in flight, so please keep #861. What I described in #692 was only a local experiment. Tested #861 ( Every traced mapping is inside the window. Edit: the first version of this comment showed a trace from a run where the patched prx in |
|
Thanks for testing on a second title — that is the strongest thing this PR could get, and the On PPSA28997 this PR is what the boot stands on: with the window in place the title reaches on-screen video at about 60 FPS with the allocator counters matching the Windows reference built from the same commit; without it the bundled player/JSON chain asserts on the rejected address. Good catch on the stale |
|
Thank you for the data point, and sorry it is a mixed one. Reading it carefully: without #861 the title segfaults during init, and with it the title reaches the main loop and then fails at First, a theory I tested and discarded, so nobody chases it: I suspected the arena size ( What would actually pin it down:
I can only test on AMD here (PPSA28997 on RADV: on-screen video at ~60 FPS with every mapping inside |
|
Two corrections, then measurements from an actual NVIDIA device, because it turns out I can test this half after all. I was wrong twice in my previous comment. I wrote that I could only test on AMD. This machine has both GPUs, and the engine has always selected the discrete one - our own startup log says I wrote a small probe (
The two results that actually matter for your report:
What I would need to go further, and I think it is enough to settle it: for the failing calls, Two control tests worth running on your side: whether the failure persists with nothing else on the GPU, and whether it moves at all when the allocation is forced onto the host-visible type. On my side, PPSA28997 with this arena in place is unaffected - the largest GPU-side block I can observe is 3407872 bytes, so this title never approaches the sizes in question, which is why I could not feel your failure from it. Nothing here makes me think #861 causes your problem, but I now have a probe that can test your exact numbers, and I will keep it pointed at this. Say the word and I will paste the whole ~130-line file into a comment, or add it to the repo under |
|
@jeffrey12cali Yes, please send the docs PR. The trap cost me one wrong comment here, so a line in the contributor notes would have saved it. On the NVIDIA failure: @fisherjoey traced it in #1022, and #1051 is the fix. On NVIDIA, a refused |
|
That closes it, and #1022/#1051 explain what I could not: a refused For the record, the measurements I did get are consistent with that and rule the other theory out. On an idle NVIDIA device here (RTX 4070 Laptop, 6185 driver line, 8188 MiB device-local heap, 23528 MiB host heap): single Docs PR for the stale |
adriadam10
left a comment
There was a problem hiding this comment.
Built and ran ctest on this PR merged onto current main (Linux, GCC): all green, guest_memory included. No CI ran on c9ce0789 (no checks on the head); the PR needs green CI, including the Windows/MinGW job, because the header moves the Commit/Reset declarations out of #ifdef _WIN32.
Before this can go in:
- Code comments.
GuestArena.cpp:291-297(// PAGE_*on each case) andGuestMemory.cpp:452(// PAGE_READWRITE). CONTRIBUTING says no code comments, and the description says there are none. - The
TryMarkUsedresult is ignored (GuestArena.cpp:318). If the range partly overlaps a used one, it returnstrueand the commitMAP_FIXEDs over it anyway, whereMarkUsedwould throw. That case should throw too. Also, on failure thecatchat:322releases the whole range, including the part the caller had already reserved withGuestArenaAllocate(theGuestHeappath). MAP_FIXEDon arena-chosen addresses (GuestArena.cpp:303,DirectMemory.cpp:406). The used map is seeded from/proc/self/mapsonce, on the firstArena()call. A host mapping created later inside the window (driver, another library, a hintedmmap) isn't in the map, so the arena can hand out its range andMAP_FIXEDsilently replaces it. The objection toMAP_FIXED_NOREPLACEwas that it returnsEEXISTon reserved space, but nothing is reserved up front anymore. For ranges the arena picked and forGuestMapNoOverwrite,MAP_FIXED_NOREPLACEturns that case into an error instead of corruption.- Eager reservation. You dropped the up-front
PROT_NONEreservation because the kernel's top-downmmap"picks addresses just below the stack inside the range". On x86-64 with 47-bit user space, the stack andmmap_basesit near0x7ff..., far above0xFC_0000_0000. Could you share the failing case (kernel,ulimit -v, the/proc/self/mapsat the failure)? If aMAP_NORESERVEreservation works, it also removes the seed race above. - Unknown protection becomes RW (
GuestArena.cpp:298). Throw for values the switch doesn't model ("exact or throw"). - Tests don't pin the fix itself. Both re-enabled checks map at fixed addresses. Nothing checks that a direct or flexible mapping with
addr == NULLlands in[0x2_0000_0000, 0xFC_0000_0000), which is #692, or thatmunmapfollowed by a same-size map reuses the freed range (claimed under "Hole reuse"). Linux tests for both, plus the hinted path (addr != NULLwithoutMAP_FIXED), would cover what the PR changes.
|
Thank you - the seed-race point is the important one and it changes the design, so I am taking it rather than defending it.
Eager reservation: I will re-measure and hand you the data. My note that top-down
Unknown protection becoming RW is wrong; it will throw. "Exact or throw" is the rule and I broke it. Code comments removed - Tests do not pin the fix - agreed, and this is the part I under-served. Both re-enabled checks map at fixed addresses, so neither would catch a regression of the actual bug. I will add Linux tests for: a direct and a flexible mapping with CI. Noted on the missing checks for |
|
On current
Setup: Linux (Arch), GCC, Release. Found while rebuilding our PPSA14632 test branch on current |
The arena constructor was Windows-only, so on Linux Available() was false: the guest heap fell back to the host allocator and direct memory landed wherever the kernel pleased. Titles that require a low virtual address rejected the mapping and re-issued the identical request, which surfaced as a start-up assert inside their own allocator. Seed the window from the mappings that already exist instead of reserving it up front, because a PROT_NONE reservation of the whole range starves the kernel's own top-down mmap, which picks addresses just below the stack inside that range. Commit, reset and set-protection become portable so the arena owns its used ranges on both platforms, and fixed mappings are checked against the arena's used ranges rather than against EEXIST from a reservation. The window-placement and no-overwrite checks in guest_memory were compiled only for Windows; they now run on Linux too, and the window check fails without this change. Fixes boykopovar#692
The Linux implementation of GuestArenaReset released the range in addition to resetting the memory, while the Windows implementation only resets the memory and leaves accounting to the caller. Two call sites already release explicitly after a reset, so every one of them released the same range twice: sceKernelMunmap through ArenaForget, and GuestHeap::release through GuestArenaRelease. The two releases acquire the arena lock separately. Contains is span-based, so the second release still runs, and Release erases every overlapping entry. A thread that allocates the range between the two calls therefore has its entry erased, and the arena hands the same guest address range out twice. Delete the release from the Linux reset so both platforms restore the documented contract. Every existing caller already pairs the reset with an explicit release.
CheckUnmappedArenaRangeStaysReserved uses mmap, PROT_READ, PROT_WRITE, MAP_FIXED_NOREPLACE and EEXIST directly, so the Windows build fails to compile it. The probe exists to show that an unmapped arena range still rejects a hostile MAP_FIXED_NOREPLACE mapping, which is Linux-specific behaviour. Guard the function and its call site with __linux__. The other tests in the file compile on both platforms.
An unmapped range inside the guest arena stays a PROT_NONE host reservation, so host munlock succeeds on it. Check the guest mapping state first, as msync does, and return ENOMEM for ranges the guest has not mapped or reserved.
Throw when /proc/self/maps cannot be read or parsed, and when the kernel places a reservation chunk at another address (MAP_FIXED_NOREPLACE treated as a hint). Remove the unused PageSize and keep the POSIX-only headers in the non-Windows block.
3c54adf to
cf85a72
Compare
|
@jeffrey12cali @adriadam10 Rebased onto current main and pushed to the branch; your three commits are unchanged apart from the test-file conflict with
Linux, GCC, Release: ctest 694/695 on this head, |
as per comment
|
Thank you for the rebase and for fixing Two notes from the far end of this change, on the title #692 was filed for (
One process note against myself: the Windows For the maintainers coordinating the queue: with #861 merged, #1810 and #2660 are no longer blocked by the |
|
Ratchet & Clank: Rift Apart (PPSA01474) on Linux faults about 11 s in since this merged (3 of 3 runs on Tested and written by Claude (AI assistant) on a contributor's machine. |
…re-reading /proc/self/maps describePages looks pages up in a per-span bitmap before it queries the kernel, and on Linux two things stopped that bitmap from ever being filled. First, PageStates::initialize asks GuestArenaRange for the guest window, but the Arena constructor in libc is entirely inside #ifdef _WIN32, so the range comes back empty, PageSpan::allocate returns false, and spanOf always answers null. The fix falls back to the window libkernel actually reserves on Linux, 0x200000000 with a size of 0x400000000, so the bitmap exists. Second, the Linux branch queried mappings and stored nothing, while the Windows branch stores its VirtualQuery results under an allocation-generation check. The fix stores the same way, and invalidates the two munmap trims in DirectMemory that run outside any GuestAllocations::Mutation, so no readable bit can outlive the mapping it came from. Measured on this title: the map-parse path went from 1,172,045 parses per run to zero, 42,772 verifications now cost 0.1 s, forgets 715 covering 2377 MiB confirms invalidation is live, and the CPU-bound frame rate went from 7.4 to 60.3 FPS with no fatal lines over a 13 minute soak. Enabling the span also activates the invalidator, which was never registered before because it is only installed when a span allocates. The store-back half of this is already proposed upstream as boykopovar/AnyPS5#1810 for boykopovar/AnyPS5#1791. The span fallback here is only needed while this tree predates boykopovar/AnyPS5#861, which makes the span non-empty upstream; drop it once that lands.
|
Author of the arena here, and the bisect is right: this is my change, not something the title was already doing. What I got wrong. #861 places an address-less reservation at the lowest free address of the arena. For Status, for anyone reading this later. The fix is already on The constraint from the other side, which the fix has to keep. What I can test, and what I cannot. I cannot run Thanks for the bisect and for writing it up with the trace - the |
What
Guest address space on Linux is placed inside the PS5 window instead of wherever the kernel's top-down
mmaphappens to land, so a title's allocator accepts the mapping.sceKernelAllocateDirectMemory/sceKernelMapDirectMemorywithaddr == NULLnow return addresses in[0x2_0000_0000, 0xFC_0000_0000), which is the boundsceLibcMspaceCreateenforces.The arena reserves the free intervals of that window up front (
PROT_NONE | MAP_NORESERVE, 1 GiB chunks), so no later host mapping - driver, library, hintedmmap- can appear inside it, and commit can keepMAP_FIXEDsemantics, which is what committing over the arena's own reservation requires.Also in this change, from review:
EEXISTis recorded as occupied, so the arena cannot hand it out. Any other failure throws, with the address and the size.PROT_NONEreplacement thatGuestArenaResetuses.addr == NULL, not only the arena entry points.GuestArenaReset_nid_postfixno longer releases address-space accounting on Linux (@blawesom). The Linux variant released the range in addition to resetting the memory, while the Windows variant only resets and leaves accounting to the caller. Two call sites released again after the reset, andContainsis span-based, so both released twice under two separate lock acquisitions: a thread that allocated the range in between had its entry erased and the arena handed the range out twice. Every caller already releases explicitly, so the contract is restored rather than duplicated.mmap,PROT_READ,PROT_WRITE,MAP_FIXED_NOREPLACEandEEXISTdirectly, which does not compile under MinGW.Fixes #692.
Tested
Arch Linux, kernel 7.2.8-arch1-2, GCC,
Release,cmake -DBUILD_TESTING=ON:ctest491/491 on this head, rebased onto currentmain. The slowest test in the suite takes 4.19 s and the two tests added here take 0.01 s each, so nothing approaches the 30 s limit.The Windows build cannot be compiled here (no MinGW). The guard was verified by tracking preprocessor nesting: before the change the probe body and its call site are outside every
__linux__block, after it both are inside. CI has to confirm the build itself.The new kernel-API tests were verified to fail without the change. Making
ArenaAvailable()return false gives0% tests passed, 1 tests failed, and restoring it gives100%.Reservation feasibility was measured before adopting it. With an unlimited address space, 1000 GiB of the window reserves in 1 GiB
PROT_NONE | MAP_NORESERVE | MAP_FIXED_NOREPLACEchunks, with zero failures on kernel 7.2.8-arch1-2. Underulimit -v 67108864(64 GiB) the arena aborts withguest arena: cannot reserve 0x19c0000000+0x40000000 (Cannot allocate memory)instead of passing in silence, and at 128 GiB it fails at0x29c0000000.The reservation does not reduce allocatable device memory. A probe that reserves the same 997 GiB window and then allocates
VK_MEMORY_HEAP_DEVICE_LOCAL_BITmemory gives identical totals with and without the reservation: 22 GiB beforeVK_ERROR_OUT_OF_DEVICE_MEMORYon an RTX 4070 Laptop, and 4 GiB on the AMD 780M, at both 1 GiB and 256 MiB step sizes. This is reported against #861's head in the discussion below.Title evidence (PPSA28997, native Linux relink): the window placement is what the boot stands on.
sceKernelGetDirectMemorySizereports0x360000000on both platforms, and with the window in place the bundled player/JSON chain stops rejecting the address and the title reaches on-screen video.Independent reports on other titles:
sceKernelMapNamedFlexibleMemory→sceLibcMspaceCreatenow lands inside[0x400000, 0xFC00000001)andCommon::initializestops failing with0x80553504.0x7fff94400000into0x200000000/0x200300000, and the title passes the allocator fault._dl_initin 2 of 2 runs; with headc2e15098it boots, plays the attract movie at about 30 fps, and exits with code 0 in 2 of 2 runs.#1370 (partly overlapped allocations) has since merged and this head is rebased on top of it. #1360 remains an alternative to the eager reservation; I will rebase onto it or drop the reservation on the maintainer's call.
Checklist
main(rebased; the head is3c54adf0)