d3d12: hold the title's address range below 2^37 (D3D12-TITLE-RANGE-RESERVE) #3154
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3154"
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 ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-02T14:34:19Z.
Claimed in the coordination issue as: D3D12-TITLE-RANGE-RESERVE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7343459657)
What and why
A translated shader that reads or writes title memory by address finds it through the directory of 64 KiB pages below 2^37 (
leonida_shader'sMEMORY_PAGES; the last page is the empty one an access out of range lands on). So the title's memory (XMemVirtualAlloc) and the lists' upload arenas (leonida-gpu/src/upload.rs, behindallocate_upload) have to lie in [4 GiB, 2^37 - 64 KiB). Nothing kept that range for them: they got it because nothing else in the process asked for it first. A library that reserves address space early can take it. NVIDIA'scuInitreserves 68 GiB and 28 GiB at the first free 4 GiB-aligned places from 8 GiB, and a third-party DLSS frame generation unlocker calls it on RTX 30 cards. With it, 2 of the first 2 launches stopped while the world loaded (2 of 6 over the day, see In-game): the upload arenas fell from 29 to 8,allocate_uploadreturned null without a log line, and the title wrote through it. The same run with 64-128 GiB reserved in the process beforecuInit(from outside, no access, nothing written) reached the world and ran its 420 s with 29 arenas.This change:
leonida-gpu/src/title_range.rs(new): at the start ofleonida run(apps/leonida/src/main.rs), every free place of [4 GiB, 2^37 - 64 KiB) is reserved with no access, in plain reservations of at most 1 GiB. Nothing is committed. A request takes the lowest place held that fits (the highest withMEM_TOP_DOWN, which the title does not use): the pieces under it are released, the request is made inside them at once, and what it leaves of them is reserved again; what the title frees in the range is held again too. If another thread takes the place between the release and the request, the next place is tried, 4 times at most. A request not placed in the range (no room, taken by other threads, or refused in a free place) is said in the log, rate-limited, and made of the host as before; one refused at its own address fails with its own error. A warning says when much of the range was taken before it was held. Plain reservations rather than placeholders: write-watched memory made in a placeholder cannot be turned back into one when it is freed (the host refuses it withERROR_INVALID_PARAMETER, measured on Windows 11), and the title makes placeholders of its own in the range.leonida-hle/src/modules/xmem.rs:XMemVirtualAllocreservations of an address of our choice whose bounds take in the range, or at an address the range holds, are made there. The title's ownVirtualAllocis now ours (it was the host's, and still is for everything else): a reservation at an address the range holds is made there, so what the title frees and asks for again at the same address still works. ItsVirtualFreegives what it releases in the range back to it.leonida-gpu/src/upload.rs: the arenas come from the range, and end before the directory's empty last page (they could take it before). When no arena can be had, an upload comes from the process heap with one log line instead of null: shaders that read it by address read zeros, but the title no longer writes through null.LEONIDA_TITLE_RANGE=0: nothing is held, and every request goes to the host as before. The arenas' end before the empty page and their heap fallback stay (they are right either way).d3d12/docs/switches.mdregenerated (tools/d3d12_switches.py --out, then--check).The title's images are mapped by the loader with ASLR far above 2^37 (checked in the runs' logs), so holding the range moves none of them.
Tested
Rust, on
854fc3441(one commit onmainbf7886746), fromd3d12/,--target x86_64-pc-windows-msvc:cargo test -p leonida-gpu --lib --locked -- title_range upload::cargo test -p leonida-hle --lib --locked -- xmem::tests::title_requests xmem::tests::without_a_held xmem::tests::the_titles_virtual_alloccargo test -p leonida-hle --lockedcargo test -p leonida-gpu --lockedcargo test -p leonida-gpu --features diagnostics --lockedcargo clippy -p leonida-gpu -p leonida-hle -p leonida --all-targets --lockedhost.rs)--features leonida/diagnosticsRed and green. The new API makes the tests fail to compile on
mainitself, so they were first run on a seam with no change of behaviour (the range type holding nothing,XMemVirtualAllocand the arenas going to the host as onmain, an upload with no arena handed out as null). Each failed for the reason it states:a_held_range_keeps_other_reservations_out_until_dropped;title_requests_take_the_held_range_and_others_are_kept_out, ascuInitasks); an arena came from 4 GiB rather than the held range (arenas_take_the_held_range_and_keep_others_out);a_request_takes_the_lowest_place_and_the_rest_stays_held,a_request_at_a_held_address_is_made_there,a_place_taken_first_by_another_thread_is_tried_again_elsewhere,a_request_refused_in_a_free_place_is_not_made_elsewhere,the_lowest_place_that_fits_is_found,top_down_finds_the_highest_place);uploads_come_from_the_heap_when_no_arena_can_be_had);the_range_ends_where_the_directory_does);LEONIDA_TITLE_RANGE=0held the range (the_switch_is_on_unless_0).Tests added after the reviews:
the_titles_virtual_alloc_reserves_at_held_addressesfails without theVirtualAllocexport (the host refuses the held address);a_refusal_at_an_address_keeps_its_errorpasses with and without the change and guards the error a refusal at the title's own address reports.without_a_held_range_*(xmem and upload) pass onmaintoo: they check that, the range not held, requests go to the host as before. No test depends on global state: each holds its own range at its own place and releases what it reserves.Thirteen mutations, each built and run on
854fc3441, are each caught by at least one test:leonida-gpu, 2 inleonida-hle);give_backdoing nothing; the title'sVirtualFreenot giving back);XMemVirtualAllocnot placed in the range; the title'sVirtualAllocnot routed to it; the arenas not placed in it.Not covered by a unit test: that a refusal at the title's own address is final rather than asked of the host again (it would need the host to refuse a commit for want of room); the reviewers checked it by reading.
python -I scripts/check_no_game_data.pypasses on the changed files.In-game
RTX 3090, NVIDIA 621.40, Series S settings at 1080 lines,
-FrameGen 2, the third-party unlocker installed aswinmm.dllbeside the executable, no outside reservation; one executable of this commit (diagnostics,dlss,mimalloc), the switch on (default) or off (LEONIDA_TITLE_RANGE=0). Runs of 420 s, or stopped 120 s after reaching the world; nothing measured beyond what is listed.mainandmain+ #3062 this morning; this commit with=0this afternoon): 6allocate_uploadcan no longer place.Overlaps
xmem.rs, open) caps graphics reservations below the same end (GPU_DIRECTORY_END= 2^37 - 64 KiB) and falls back to the console's range when that fails. The two agree: a graphics request's bounds are the held range, so it is placed there; with no room,in_title_rangegivesNone, the host's 37-bit attempt fails and #2856's fallback applies. Textually, the second to merge resolves three spots inxmem.rs: both insert afteraddress_range(keep both blocks), both change theVirtualAlloc2call (keeplet held = in_title_range(...), make itlet mut at = held.unwrap_or_else(|| unsafe { VirtualAlloc2(...) });, then #2856's fallback block unchanged), and both insert at the start ofmod tests(keep both). After both,GPU_DIRECTORY_ENDcan betitle_range::HIGH, which a test ties toMEMORY_PAGES. One difference of intent: #2856 leaves CPU-only memory to the whole console range, while the held range serves it first, as the host placed it onmain(lowest first from 4 GiB).main.rsand adds a heap fallback inupload.rs; whichever merges second has to take the other into account.GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40, Windows 11 Pro (build 26300): the Rust suites and the in-game runs above.
AI-assisted: yes (Claude Code, Claude Opus 5.5; reviews by two independent agents, Claude Opus 5.5 and Claude Fable 5.2)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (No translator change.)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T15:55:49Z.
Ran natively on Windows 11, head
854fc3441merged into currentmain(83046a713, clean merge).mainhas since moved to3c106eb2f. The PR still merges cleanly there, and none of the six files it touches changed in between. CPU-only tests, debug build unless noted.Suites on the merge
cargo test -p leonida-gpu --lib -- title_range upload::cargo test -p leonida-hle --lib(the 3 new xmem tests)cargo test -p leonida-gpu -p leonida-hlecargo test -p leonida-gpu --features diagnosticscargo test -p leonidacargo clippy -p leonida-gpu -p leonida-hle -p leonida --all-targetsunused_mutinhost.rs--features leonida/diagnosticsExtra probes
Throwaway tests, not pushed. Each holds its own test range far from the others, or the real [4 GiB, 2^37 - 64 KiB) alone in the process.
The stated case holds up. A cuInit-like request tries fixed 4 GiB-aligned addresses from 8 GiB, as the description says.
main)Range::reserve(LOW, HIGH)keep()would fireThree things I found while probing the edges.
give_backholds again only the free run that starts at the freed address. The title'sVirtualFreeruns beforefreed()takes the lock, so another thread can reserve in between. Measured with a 1 MiB block.VirtualFreeandgive_backThe gap stays unheld until a later free next to it happens to cover it. Holding again over the freed allocation's whole span (its size, queried before the free) would use
hold's own skip of what others took. It needs another thread to reserve in that short window, so it should be rare.takereleases every piece under the place, and pieces are up to 1 GiB. A 64 KiB request opened a 1 GiB window. A foreign 64 KiB reservation made 900 MiB away from the request, inside the callback, was let in. Everything else was held again afterwards (0 KiB lost). Then a stress run with 4 title threads taking 64 KiB-1 MiB blocks and one thread reserving 64 KiB anywhere in a 4 GiB test range in a loop (adversarial, nothing like a one-timecuInit).Refused, the place was free again when checked, so it would go to the host)So the range keeps out a reservation made while no title request is in flight, not one made alongside the title's own allocations. Splitting only the piece under the request (or a smaller
STEP) would narrow the window.VirtualAlloc(at, 64 KiB, MEM_COMMIT, PAGE_READWRITE)with noMEM_RESERVEgoes to the host and commits inside our own no-access piece.VirtualAlloc(virtual_alloc_in), range not held (as onmain)VirtualAlloc(virtual_alloc_in), range heldVirtualAllocon a held test rangeVirtualQuery), a byte writtentakein the same 1 GiB pieceOnly buggy code commits without reserving, so this is low priority. But the new behaviour is "works, then silently loses the page" where
mainfails at once.Smaller notes, measured.
takecost grows with the piece count. Release build 2 us at 8 pieces, 11 us at 1,008, 24 us at 4,008 (debug 3, 28, 112 us). That is a model of fragmentation. The real range starts at 124 pieces, and how many a long session reaches is not measured.Where::At(HIGH - 64 KiB)of 128 KiB is made and ends at 2^37, so it covers the directory's empty page. Only an address the title itself asks for can do that (the arenas andAnywhererequests stop belowHIGH).Not checked: any in-game run, the title's own
VirtualAlloc2orMapViewOfFile3at held addresses (not routed through the range), other GPU vendors' drivers, and the merge with #2856 or #2643.GPUs tested: n/a (CPU-only tests).
AI-assisted: yes (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T16:24:49Z.
Verified here: it applies, and all ten of the new
title_rangetests pass.What the set covers is the right set for an address reservation: where the range ends, lowest and highest fit, a held range keeping other reservations out until dropped, a request at an address already held, two threads racing for the same place, and the refusal that must not be retried elsewhere - the last one is the one that would corrupt a title's memory if it were wrong, and it is a test.
the_switch_is_on_unless_0is worth naming for a merge: it says this is on by default, so what a player gets is the reserved range, not the old behaviour - the opposite of the usual "off until measured" and worth a line in the body if it is deliberate.What it does not cover: the title's real mappings. Everything above is the reservation bookkeeping; whether holding the range below 2^37 keeps the title's own load addresses inside it needs a run, and this machine cannot start the title. A
-Logrun that shows the range line would settle it.AI-assisted: yes - DeepSeek v4.1 (flash).
Written with an AI assistant.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T18:18:15Z.
Verification of #3154 (third party, code review on Linux; not built: Windows-only
leonida-gpu/leonida-hle)854fc3441c70fcafc108d900e21fed8ffeebed66(6 files, +1013/-18, newtitle_range.rs).bf7886746.git merge-tree --write-tree origin/main pr/3154againstmain45dc41f95is clean(83 commits behind). CI on the head: 6/6 success.
python -I scripts/check_no_game_data.pyon the 6 files (on the merge): exit 0.git merge-tree pr/3154 pr/2856conflicts inleonida-hle/src/modules/xmem.rs. The body's"Overlaps" section explains how the two agree in behaviour. Whichever lands second needs a textual merge. It
merges cleanly with #2982 and #3113.
Review
library (the body names CUDA's
cuInit) from reserving that range first. Reserving the free parts of[4 GiB, 2^37 - 64 KiB)at start (keep(), called frommain.rs:248) and carving allocations out of thereservation fixes the cause.
the_range_ends_where_the_directory_doestiesHIGHtoleonida_shader::emit::MEMORY_PAGES, so the two cannot drift apart.Range::take(title_range.rs) releases the held pieces that overlap the target, allocates, then re-holdsthe remainders. Two points:
releasefrees whole held pieces, up to 1 GiB each (STEP), not just[start, stop). Between thatVirtualFreeand the re-hold, any thread'sVirtualAlloc(NULL, ..)or alibrary's placed reservation can land anywhere in that GiB, which is what the range exists to prevent. The
ATTEMPTSloop recovers for the caller but not for the stranger who got in. Splitting the piece first, sothat only the requested span is released (
VirtualFree(MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER), orplaceholders with
VirtualAlloc2(MEM_REPLACE_PLACEHOLDER)), would close it. Non-blocking if the authorsjudge the window rare, but the code should say so.
XMemVirtualAllocand every upload arena now runs under the oneKEPTmutex withfit(it rebuilds aVecof runs over all pieces on each call), plusVirtualFree,VirtualAllocand oneVirtualQueryper re-held piece. The queries are on free or reserved regions, not on the committed pools that#3113 measured at 21-36 ms, so they should be cheap. A per-call timing in
findings.mdwould confirm it.Fragmentation grows
piecesover a session, andfitis linear in it.LEONIDA_TITLE_RANGE=0turns it off, asswitches.mddocuments. Theheld < 15/16warning makes a librarythat loaded first visible in the log.
title_rangetests (fit lowest and top-down, foreign reservations refused at and inside a heldrange, give-back) and upload tests that place arenas inside the range. Kal0s-design and fiestorra ran them
natively. The body's in-game runs show the crash case.
Process
Verdict: ready on review (not built here). The release window (point 1) is worth a follow-up or a code
comment. Coordinate the
xmem.rsmerge with #2856.AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.
Generated by Claude Code
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T19:23:27Z.
RUN (in game) -- unit tests + 300 s render-path gate run for PR #3154:
854fc3441mainSHA:f1a559851(git merge --no-ff, clean merge)python -I scripts/check_no_game_data.pyPASSpython -I tools/d3d12_switches.py --check d3d12/docs/switches.mdPASScargo test -p leonida-gpu --features diagnostics --lib --locked title_range::-- 10 passed, 0 failedcargo test -p leonida-hle --lib --locked xmem::-- 27 passed, 0 failed, 1 ignoreddiagprofile (diagnostics,dlss,mimalloc), Series S profile, DLSS on, Frame Gen off, same restored save snapshotINFO leonida_gpu::title_range: title range held: the free places of [4 GiB, 128 GiB - 64 KiB) reserved with no access, for the title's memory and the lists' upload arenas (LEONIDA_TITLE_RANGE) held_mb=126975 pieces=124854fc3441onf1a559851)mainalone (f1a559851)Loaded cleanly into the world with
held_mb=126975reserved below2^37, stayed up past 300 s in-world with 0 device lost / 0 crashes / 0ERRORlines, and closed cleanly. Rendered FPS matchesmain(27.0vs27.2fps).Verdict: PASS.
AI-assisted: yes (Antigravity).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T22:17:41Z.
Merging. Head
854fc3441merges cleanly withmainf3865cae8and does not force a rebase of #2566. In-game run on this head by lixonik (NVIDIA GeForce RTX 5070 (12 GB), driver 595.79, Windows 10 x64, 315 s in the world, 0 device lost: https://github.com/ProjectLeonida/mandy/pull/3154#issuecomment-7356097140) and an independent red/green on this head, both checked by a separate judge. Merged by MoonEyes.AI-assisted (Claude Code).