fix(libkernel): walk direct-memory physical ranges by block, not page #10528
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10528"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-24T08:18:45Z.
What
ValidatePhysicalRange,AddMapping, and the backing-overlap check inCreateDirectMemoryBackingall walkedg_physPages— onestd::mapnode per 16 KiB page — on every call. A guest that remaps a large arena in a sliding window pays O(pages) per call and O(n²) overall.Add
g_physBlocks, one entry perCreateDirectMemoryBackingallocation, split atForgetDirectMemoryboundaries for partial releases. Validation, mapping, and the overlap check now run at block granularity;g_physPagesstays populated unconditionally (CreateDirectMemoryBackingfills it,ForgetDirectMemoryerases it) sinceSharedBacking/WriteSharedBackingand release-path validation still need per-page lookup — but the hot paths above no longer consult it.RemapFixedIntoRegisteredalso short-circuits when the target range is already covered by a mapping with an identical guest-to-physical relationship and native protection: the per-block backing map is reissued so the requested host protection is reapplied even whensceKernelMprotect/sceKernelMtypeprotectleft the recorded protection stale, but theg_directMappingsrecord churn is skipped, so a sliding-window remap that re-requests the same tail avoids tearing down and rebuilding the bookkeeping.Tested
Windows 11, MinGW build;
guest_memory_pool_stats,MemoryPoolTest,guest_memory,windows_address_space, andagc_driver_depth_bits_guest_memorypass (last underVK_LAYER_KHRONOS_validation, noVUID-delta).guest_memory_testsadditionally covers the same-range fast path aftersceKernelMprotectchanged the real host pages (CheckFixedRemapReprotectsAfterMprotect, verified red against the pre-fix code and green with it, checking bothsceKernelVirtualQuerymetadata andVirtualQuery/MEMORY_BASIC_INFORMATION).Grand Theft Auto: Vice City – The Definitive Edition (PPSA03530) emits ~216k fixed remaps sliding 64 KiB across a 13.5 GiB arena; previously the process wedged early in the storm. With this change the storm completes and the title reaches engine initialization (a separate guest-internal allocation issue remains, tracked in discussion https://github.com/boykopovar/AnyPS5/discussions/10520 — this PR does not address it).
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T11:10:55Z.
Review: CHANGES_REQUESTED
The new identical-physical-range shortcut skips the whole
mutation.Protectapply callback. That callback is whereAddMapping(..., nativeProtection)remaps the backing with the requested host protection.GuestAllocationsProtect_nid_postfixonly changes allocation metadata and invokes the callback; it does not callmprotect/VirtualProtectitself. Therefore, when a fixed remap reuses the same virtual-to-physical relationship but changesprot(for example RW to read-only or no-access), the shortcut leaves the old host page protection in place while recording the new guest protection. Please preserve the shortcut only when both the mapping and effective native protection already match, or explicitly apply the requested host protection on this path, and add a regression that remaps the same physical range with changed protection and checks both guest metadata and actual access behavior.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T11:49:09Z.
Review: CHANGES_REQUESTED
The optimization is useful, but the new same-range fast path skips the protection apply callback. That callback calls AddMapping with nativeProtection, which remaps the backing with the requested host protection; GuestAllocationsProtect_nid_postfix only updates allocation metadata and invokes the callback. If a fixed remap preserves the same guest-to-physical mapping while changing prot (for example RW to read-only or no-access), this return leaves the old host protection active while recording the new guest protection. Please limit the shortcut to cases where the effective native protection also matches, or apply the host protection on this path, and add a regression that checks both metadata and actual access behavior.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T13:56:36Z.
Review: CHANGES_REQUESTED
Follow-up review of exact head
52c4d77c0a3111a8145c50a1de86f850d3c9337c: the protection regression addresses my previous finding. The exact-head diff passesgit diff --check; I could not run the nativeguest_memory_testshere because this isolated worktree has uninitialized submodules and no configured build.Before merging, please resolve the newly visible overlap with open PRs:
TryForgetDirectMemoryandForgetDirectMemorystill validate each page withg_physPages.contains(...). This PR now populatesg_physPagesonly on Linux. If #10528 lands first, #7105's release path will reject every otherwise-valid direct-memory range on Windows. Please coordinate with #7105's author and update its validation to use the block-range metadata (or agree on another compatible merge order), then record the prerequisite inDepends onand add/confirm Windows coverage.DirectMemory.cpp/.hppwith this PR; itsMapPlaced/commit-granule and unmap changes need an agreed integration order. The merge-tree check also reports a test-file conflict with #6321 inGuestMemory.cpp; retain both test additions and record any required prerequisite.I checked the overlap with
git merge-treeagainst the current PR heads. This is a coordination and cross-PR compatibility request; I found no new defect in the protection-preserving fast path itself.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T14:07:26Z.
Review: CHANGES_REQUESTED
Follow-up on exact head
52c4d77c0a3111a8145c50a1de86f850d3c9337c. The protection regression still addresses my original finding. I am narrowing my previous overlap review: I withdraw thegit merge-tree-based conflict claims for #7149 and #6321; those comparisons used divergent PR histories, and the current-base-relative changes I inspected do not substantiate those conflicts. The substantive compatibility issue with #7105 remains: this PR populatesg_physPagesonly on Linux, while #7105’sTryForgetDirectMemory/ForgetDirectMemorystill require every page in that map. If #10528 lands first, valid Windows release requests will fail because that map is empty there. Please coordinate with #7105’s author on block-range validation or a compatible order, record the dependency where needed, and ensure Windows coverage. The isolated review worktree still lacks initialized submodules/configured build, so I could not independently runguest_memory_tests; the author reports its Windows Release regression passes.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T19:52:52Z.
Review: DISMISSED
Re-reviewed exact head
9d807ac9d0802dab93d01ed73b0b53b1d3c92585. The protection-aware same-range shortcut fixes the original host-permission finding, andg_physPagesis now populated unconditionally, which resolves the compatibility concern with release-path validation on other platforms. The current diff passesgit diff --checkandtools/check_conventions.py --base upstream/main; the author reports the Windowsguest_memory_testsrun passing at this head. I did not run the native library suite locally because this checkout has no configured build.One PR-body statement is now stale and materially contradicts the code:
Whatsaysg_physPagesis populated only on Linux, butCreateDirectMemoryBackingnow fills it on every platform andForgetDirectMemoryerases it on every platform. Please update that description before merge so the performance and cross-platform behavior are accurately stated.