Skip to content

d3d12: the other views' blocks of a guarded range are made read-only by one change a run (D3D12-ALIAS-PROTECT-RUNS) - #2701

Closed
TheGeogeo wants to merge 3 commits into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-alias-protect-runs
Closed

TheGeogeo wants to merge 3 commits into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-alias-protect-runs

Conversation

@TheGeogeo

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-ALIAS-PROTECT-RUNS (#2570 (comment))

What and why

When a range of title memory is guarded together with the other views of its pages (protect, protect_claimed), cover makes every block of those other views read-only. On main, each block gets its own VirtualProtect (protect_alias): one call per 64 KiB block, and each call tells every core to forget the page. MoonEyes measured this at 232 blocks and 6.7 ms a frame on an RTX 4060 (about 29 us a block). OreoFR found the cause by reading the code.

A view shows the range's pages in a row, so the blocks wanted in one view follow each other. This change:

  • in cover, first decides for every other view's block (alias): read-only already, to be made read-only, or not now (the claim is held elsewhere, or the block is left alone or resting); these are the same tests as before;
  • then makes the wanted blocks read-only by runs of blocks that follow each other, one change per run (protect_aliases, protect_alias_run);
  • does a run whose change fails (for example one across two views) block by block with the old protect_alias;
  • when the first block of a run is not as we left it, restores that block's protection and leaves it uncovered, as before;
  • LEONIDA_ALIAS_RUNS=0 keeps one change per block. The switch also works by turns under LEONIDA_AB in diagnostics builds (toggled!).

This takes the same thing for granted as protect_here and unguard_runs: the first block's protection speaks for the run. A guarded block whose other view could not be covered is not clean, as before.

With diagnostics, the guards line now also counts the runs made read-only by one change and the runs where that change failed, next to the existing LEONIDA_UNGUARD_RUNS counts.

Tested

On Windows 11, from d3d12\:

  • New test watch::tests::another_views_blocks_are_guarded_by_one_change. It maps two views of one section, guards four blocks of the first, and checks two things:
    • the other view's four blocks become read-only through one run;
    • when the other view's first block was set to PAGE_NOACCESS, that block keeps its protection, the other three are guarded, and the guarded block it covers is told as written again.
  • With LEONIDA_ALIAS_RUNS=0 (block by block, as on main) the test fails ("the other view's four blocks were not made read-only by one change"). With the change it passes.
  • cargo test -p leonida-gpu --locked: 560 passed, 50 ignored. The watch tests also pass with --features diagnostics (56 passed).
  • cargo clippy -p leonida-gpu --all-targets --locked --features diagnostics: nothing in the changed lines. git diff --check and check_no_game_data.py are clean.
  • It merges cleanly with runtime, d3d12: memory leak fixes from one sweep (RT-LEAK-SWEEP) #2569 on watch.rs (git merge-tree); runtime, d3d12: memory leak fixes from one sweep (RT-LEAK-SWEEP) #2569 rewrites protect_here and unguard right next to it. The conflicts that git merge-tree reports against runtime, d3d12: memory leak fixes from one sweep (RT-LEAK-SWEEP) #2569 (host.rs, texture.rs and others) and D3D12-CLIPPY-LEONIDA-GPU: fix all clippy warnings in leonida-gpu #2682 (resource.rs) are the ones those pull requests already have with main.
  • In game: not measured on my machine. MoonEyes offered to run the A/B on the RTX 4060 where the 6.7 ms were measured. The new counts on the guards line, and the existing protection time in the frame line, give the before and after directly.

GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 32.0.16.2140, Windows 11 Pro 10.0.26300 (unit tests only).

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (also with runtime, d3d12: memory leak fixes from one sweep (RT-LEAK-SWEEP) #2569 on watch.rs).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)

…by one change a run (D3D12-ALIAS-PROTECT-RUNS)

A range guarded together with the other views of its pages (protect,
protect_claimed) made each of those views' blocks read-only with a
VirtualProtect of its own: a call per 64 KiB block, each telling every core
to forget the page (232 blocks a frame, 6.7 ms, measured on an RTX 4060).
A view shows the range's pages in a row, so its blocks follow each other.

cover now decides for every other view's block first (read-only already,
to be made so, or not now) and then makes the wanted ones read-only by runs
of blocks that follow each other, one change a run. A run whose change
fails (not of one view) is done block by block, as before; a first block
that is not as we left it keeps its protection. As protect_here and
unguard_runs do, the first block's protection speaks for the run.
LEONIDA_ALIAS_RUNS=0 makes a change a block, as before; with diagnostics the
guards line counts the runs and those the change failed for.
@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Reviewed by OreoFR (code read and merge checks only: leonida-gpu does not build on macOS, so no cargo test here), main e1fe82f, PR head 90aaade.

Merges: clean with main, #2566, #2692 and #2693 (git merge-tree). Against #2569 there are conflicts in host.rs, texture.rs, leonida-hle/src/modules/xmem.rs and d3d12/docs/findings.md, none in watch.rs; #2569 already conflicts with main on those files, so that is not this PR's.

Read in code, looks right:

  • cover now only classifies (alias → ReadOnly / Wanted / Not) and protects afterwards. The claim of each alias block is still taken in alias, before the deferred protection, and held until cover returns, so a fault cannot slip in between the decision and the VirtualProtect.
  • A failed block maps back to the block it covers (w.1), which loses clean as before. The same block wanted twice stays in one run (wanted[k].0 == wanted[k - 1].0).
  • A failed range call clears PROTECTED and falls back to block by block, and LEONIDA_ALIAS_RUNS=0 keeps one call a block.

One thing to look at: the run takes the first block's protection for all of it. VirtualProtect over [from, to) returns in old the protection of the first page only. The code checks old and gives the first block back if it was not ours, but every middle block is set PAGE_READONLY and noted g.protected whatever it was before. On main, protect_alias checked each block's own old.

  • If a view can have a middle block in another state (left PAGE_NOACCESS or PAGE_GUARD by the title, or PAGE_EXECUTE_*), this PR changes and forgets it, where main gave it back.
  • The doc says the same assumption is already made by protect_here and unguard_runs. If that is known to hold for xmem views, fine. If not, a cheap guard would be one VirtualQuery before the range call, using the range call only when the region it returns has one protection and covers the whole run.
  • The new test exercises a different first block only; a different block in the middle would show it.

Not covered: the A/B in the title (mooneyes-agent has it queued on the 4060).

AI-assisted: Claude Code

…has one protection

One change of protection answers with the first block's protection only,
so a block in the middle of a run that was not as we leave guarded memory
(no access, a guard page) was made read-only all the same; block by block
each was looked at and kept its protection. A run now asks Windows first
(VirtualQuery) and is changed at once only over the blocks that are one
region of committed read-write or read-only memory; any other block is done
on its own, as before. The test covers a block in the middle too.
@TheGeogeo

Copy link
Copy Markdown
Contributor Author

Thanks, OreoFR. Fixed in aa3a032.

You were right. One VirtualProtect over a run reports the old protection of the first block only. A block in the middle that was not read-write or read-only (no access, a guard page) became read-only anyway. On main, each block was checked and kept its own protection.

Now a run asks VirtualQuery first, and changes at once only the blocks that form one region of committed read-write or read-only memory (one_protection). Any other block is done on its own with the old protect_alias, as before. The query does not change protections, so it does not tell every core to forget the pages; the saving from one change per run stays.

The test now also covers a block in the middle (PAGE_NOACCESS on the other view's third block). On 90aaade that case fails: the block becomes read-only. With the fix it keeps PAGE_NOACCESS, the other three are guarded, and the guarded block it covers is told as written again.

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Re-read by OreoFR on aa3a032 (code only, no Windows build here): my point is addressed. one_protection asks VirtualQuery first and only lets a span go in one change up to the end of a single committed region that is PAGE_READWRITE or PAGE_READONLY. Windows ends a region where the attributes change, so a middle block in another state ends the span there. The end is floored to a whole block (/ BLOCK), so a block that is only partly in the region goes alone through protect_alias, as on main. The added middle-block PAGE_NOACCESS case in the test is the one I was missing. No further remarks from the code side; the A/B is with mooneyes-agent.

AI-assisted: Claude Code

@MoonEyes

Copy link
Copy Markdown
Collaborator

Verified by MoonEyes on head 6ce567d0b (a merge of main on top of aa3a0328b; the pull request's own change is the same), on main f9f3ec4c6. Windows 11, Rust toolchain of d3d12/rust-toolchain.toml.

AI-assisted (Claude Code).

@MoonEyes

Copy link
Copy Markdown
Collaborator

@TheGeogeo holding this one for now, not refusing it: on an RTX 4060 the A/B above shows no effect on the frame (-0.01 ms +-0.19, 72 pairs). If you can show the thread time it saves (the 6.7 ms you counted, with the switch by turns, or on a PC whose threads are busier), it can go in: cargo, a 600 s in-game run and CI are fine on this head.

AI-assisted (Claude Code).

@KelboLive

Copy link
Copy Markdown

Independent check on Windows (not the author), at 6ce567d0b.

  • Under heavy parallel load (four cargo test suites at once), one run had 563 passed, 1 failed: descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync, descriptor.rs:2260: assertion failed: host.made(5).
  • Isolated rerun of that test: pass.
  • Full rerun: 564 passed, 0 failed, 52 ignored (14.7 s).
  • main b17b22d33 under the same conditions: 570 passed, 0 failed.

So this is not a consistent regression from the branch; it looks like a load-sensitive flake in that descriptor test (the diff itself is copystat.rs/watch.rs). Worth a separate look only if it recurs without the four-way load.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

@TheGeogeo

Copy link
Copy Markdown
Contributor Author

Measured the thread time, as MoonEyes asked: no gain. With alias runs on, the time spent changing protections stays the same within noise.

Setup: RTX 3080 10 GB, driver 32.0.16.2140, Windows 11. One diag build of head 6ce567d, the Series S preset, menus to world, then a 150 s scripted cutscene. The switch was alternated in A B B A order with warm caches (LEONIDA_ALIAS_RUNS on, off, off, on). I dropped the first two runs after the update to main: they were still paying the one-time shader retranslation.

run alias runs (by one change) blocks made read-only, a frame ms changing protections, a frame (all threads) ms waiting for the guards, a frame fps p99 ms
on 17,529 495 8.2 10.0 17.9 154
off 0 505 8.2 9.9 18.9 131
off 0 446 7.0 7.8 21.0 126
on 14,745 426 7.4 6.9 20.7 121

The numbers per frame are the means of the frame lines over the cutscene.

What this says:

  • The path does run: about 270,000 to 390,000 guarded blocks with other views in each run. With the switch on, about 15,000 to 17,500 runs went in one change each, and none failed.
  • The time changing protections does not drop: 7.8 ms a frame with runs against 7.6 ms without. The blocks made read-only per frame are the same in both arms. So the cost here is not per call. The kernel seems to pay per page or per block whatever the call groups, so fewer calls save nothing.
  • The frame times agree with MoonEyes' result on the RTX 4060: no effect.

So the premise of this pull request, that the cost is the number of VirtualProtect calls, does not hold on these two machines. I suggest closing it rather than adding code that saves nothing. I am leaving the decision to the maintainers and will close it unless someone sees a reason to keep the switch.

@TheGeogeo

Copy link
Copy Markdown
Contributor Author

Closing: the measurements show no gain. On an RTX 3080 (A B B A with the switch) and on an RTX 4060 (MoonEyes), batching the alias VirtualProtect calls leaves the time spent changing protections and the frame times unchanged: #2701 (comment). The kernel's cost here is not per call, so the extra code would save nothing. Thanks to OreoFR for the finding and the review, and to MoonEyes for the A/B.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants