GPU: keep a title guard page inside a range; change guards in runs #68

Open
zocram4cc wants to merge 2 commits from upstream-pr/68 into main
zocram4cc commented 2026-11-26 01:28:48 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by zocram4cc on 2026-11-26T01:28:48Z.


Promised to @itsonewi on #14 (review of "copies of title memory"): a separate PR for this bug, with its own test. It does not depend on #14: this branch is cut from avatarmanz/VIctory-7900XTX main and contains none of #14's code.

Bug: a title guard page inside a protected block loses its guard

protect_here (a range) and protect_alias (another view of a guarded page) made PAGE_READONLY over a whole 64 KiB block, or over a whole range, after looking at the first page's old protection only. A page the title guards itself (PAGE_GUARD) further on in that block lost its guard: nothing faulted and nothing was seen written, so the title's own guard never fired.

Now a change of protection covers only blocks that Windows says are alike (committed, one region, read-only or read-write throughout). A block that is not gets no call and stays as it is, with VICTORY_GUARD_RUNS on or off.

Tests (the two bug tests run with the switch off and on):

  • watch::tests::a_page_the_title_guards_inside_a_range_keeps_its_guard: on v7900/main the guarded page reads PAGE_READONLY (left: 2) instead of PAGE_READWRITE | PAGE_GUARD (right: 260).
  • watch::tests::a_page_the_title_guards_in_another_view_keeps_its_guard: left: 2, right: 260 before the protect_alias check; green with it.

Change guards in runs (VICTORY_GUARD_RUNS, on by default)

Each change of protection is a kernel call; under Wine it is an mprotect and a flush of every processor's address cache. The guards changed protection one block at a call, after asking for a whole range first where it could be refused across views. With the switch on:

  • giving write access back: blocks that follow one another in one view are opened with one call;
  • making read-only: one call per run of alike blocks within each view the runtime told of (a call across two views is refused, so runs are cut there);
  • the other views of a run of guarded pages are made read-only with one call per run.

With VICTORY_GUARD_RUNS=0 a range is asked for whole only when it is one alike region; otherwise each block gets its own call. The refused call across views is no longer made: runs_of_guarded_blocks_are_cut_where_views_meet expects 6 calls there, not 7.

In-game counter (Proton Experimental, v7900 base, one scripted run)

Frame-line counter (diagnostics builds), two steady windows, per frame:

window giving write access back: calls / blocks making read-only: calls / blocks
54 frames 428.7 / 428.7 316.0 / 430.9
55 frames 425.3 / 425.3 309.4 / 424.8
  • Read-only: 316 calls for 431 blocks, 27 % fewer calls than blocks (309 for 425 in the second window, also 27 %). This is calls against blocks in one run, not a before/after figure: that run did not include VICTORY_GUARD_RUNS=0.
  • Giving access back: no runs formed in this run (428.7 calls for 428.7 blocks), so this change did not batch that path there. Why the opens come one block at a time was not investigated.
  • Refused calls: 0.00 per frame in both windows.

No frame-time change measured. The only two frame-time runs (98.0 ms/frame median in an earlier run without the bug fix or the runs, 100.9 ms in the run with both) are not an A/B: they differ in more than this change. I make no frame-time claim either way.

Credits

  • Marvisak/VIctory PR #2849 (Therecalloftheonion): one VirtualProtect per run of guarded pages, the idea behind the runs.
  • Marvisak/VIctory PR #2848 item 6 (pes7): guarded pages given back in runs.

As we read their code, their runs ignore view boundaries and the call's result, so it does not fit this tree; this is a new implementation.

Testing

  • Build: cargo xwin build --profile diag -p victory --features diagnostics --target x86_64-pc-windows-msvc passes.
  • Clippy -D warnings (cargo xwin clippy -p victory-gpu --all-targets, default and --all-features): every diagnostic it reports is on a line this PR does not change.
  • Red before green: on v7900/main, with the six tests of the first batch added and the protection calls counted (behaviour unchanged), five fail (the run-count tests and the range bug test); other_memory_inside_a_run_keeps_its_protection passes there too, since the old code already kept other memory's protection. The other-view test fails on the port before the protect_alias check and passes after it.
  • watch:: module under Proton: the same three tests fail on the unchanged base and here (holes_are_known_until_the_runtime_tells, no_mapping_is_lost_between_askers, what_is_left_alone_is_no_known_hole). No new failures.
  • Windows untested: every run was under Proton (Wine) on Linux; nothing ran on native Windows.

AI-assisted: yes. Drafted with an AI coding assistant; the tests were run under Proton; the counter numbers come from the in-game run above.

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:issue:6617422206 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/68). Originally posted by [zocram4cc](https://github.com/zocram4cc) on 2026-11-26T01:28:48Z. --- Promised to @itsonewi on #14 (review of "copies of title memory"): a separate PR for this bug, with its own test. It does not depend on #14: this branch is cut from `avatarmanz/VIctory-7900XTX` `main` and contains none of #14's code. ## Bug: a title guard page inside a protected block loses its guard `protect_here` (a range) and `protect_alias` (another view of a guarded page) made `PAGE_READONLY` over a whole 64 KiB block, or over a whole range, after looking at the first page's old protection only. A page the title guards itself (`PAGE_GUARD`) further on in that block lost its guard: nothing faulted and nothing was seen written, so the title's own guard never fired. Now a change of protection covers only blocks that Windows says are alike (committed, one region, read-only or read-write throughout). A block that is not gets no call and stays as it is, with `VICTORY_GUARD_RUNS` on or off. Tests (the two bug tests run with the switch off and on): - `watch::tests::a_page_the_title_guards_inside_a_range_keeps_its_guard`: on v7900/main the guarded page reads `PAGE_READONLY` (`left: 2`) instead of `PAGE_READWRITE | PAGE_GUARD` (`right: 260`). - `watch::tests::a_page_the_title_guards_in_another_view_keeps_its_guard`: `left: 2`, `right: 260` before the `protect_alias` check; green with it. ## Change guards in runs (`VICTORY_GUARD_RUNS`, on by default) Each change of protection is a kernel call; under Wine it is an `mprotect` and a flush of every processor's address cache. The guards changed protection one block at a call, after asking for a whole range first where it could be refused across views. With the switch on: - giving write access back: blocks that follow one another in one view are opened with one call; - making read-only: one call per run of alike blocks within each view the runtime told of (a call across two views is refused, so runs are cut there); - the other views of a run of guarded pages are made read-only with one call per run. With `VICTORY_GUARD_RUNS=0` a range is asked for whole only when it is one alike region; otherwise each block gets its own call. The refused call across views is no longer made: `runs_of_guarded_blocks_are_cut_where_views_meet` expects 6 calls there, not 7. ### In-game counter (Proton Experimental, v7900 base, one scripted run) Frame-line counter (diagnostics builds), two steady windows, per frame: | window | giving write access back: calls / blocks | making read-only: calls / blocks | |---|---|---| | 54 frames | 428.7 / 428.7 | 316.0 / 430.9 | | 55 frames | 425.3 / 425.3 | 309.4 / 424.8 | - Read-only: 316 calls for 431 blocks, 27 % fewer calls than blocks (309 for 425 in the second window, also 27 %). This is calls against blocks in one run, not a before/after figure: that run did not include `VICTORY_GUARD_RUNS=0`. - Giving access back: no runs formed in this run (428.7 calls for 428.7 blocks), so this change did not batch that path there. Why the opens come one block at a time was not investigated. - Refused calls: 0.00 per frame in both windows. **No frame-time change measured.** The only two frame-time runs (98.0 ms/frame median in an earlier run without the bug fix or the runs, 100.9 ms in the run with both) are not an A/B: they differ in more than this change. I make no frame-time claim either way. ## Credits - Marvisak/VIctory PR #2849 (Therecalloftheonion): one `VirtualProtect` per run of guarded pages, the idea behind the runs. - Marvisak/VIctory PR #2848 item 6 (pes7): guarded pages given back in runs. As we read their code, their runs ignore view boundaries and the call's result, so it does not fit this tree; this is a new implementation. ## Testing - Build: `cargo xwin build --profile diag -p victory --features diagnostics --target x86_64-pc-windows-msvc` passes. - Clippy `-D warnings` (`cargo xwin clippy -p victory-gpu --all-targets`, default and `--all-features`): every diagnostic it reports is on a line this PR does not change. - Red before green: on v7900/main, with the six tests of the first batch added and the protection calls counted (behaviour unchanged), five fail (the run-count tests and the range bug test); `other_memory_inside_a_run_keeps_its_protection` passes there too, since the old code already kept other memory's protection. The other-view test fails on the port before the `protect_alias` check and passes after it. - `watch::` module under Proton: the same three tests fail on the unchanged base and here (`holes_are_known_until_the_runtime_tells`, `no_mapping_is_lost_between_askers`, `what_is_left_alone_is_no_known_hole`). No new failures. - Windows untested: every run was under Proton (Wine) on Linux; nothing ran on native Windows. AI-assisted: yes. Drafted with an AI coding assistant; the tests were run under Proton; the counter numbers come from the in-game run above.
avatarmanz commented 2026-11-26 07:33:48 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by avatarmanz on 2026-11-26T07:33:48Z.


Left open: conflicts with main (host.rs), and VICTORY_GUARD_RUNS is on by default; needs a rebase and an in-game check.

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7185591145 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/68#issuecomment-7185591145). Originally posted by [avatarmanz](https://github.com/avatarmanz) on 2026-11-26T07:33:48Z. --- Left open: conflicts with main (host.rs), and VICTORY_GUARD_RUNS is on by default; needs a rebase and an in-game check.
zocram4cc commented 2026-11-26 08:12:53 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by zocram4cc on 2026-11-26T08:12:53Z.


Rebased onto current main (the 5 s lines moved into meter_lines; the guard counter line is called there now) and VICTORY_GUARD_RUNS is off by default now (=1 turns the batching on). The guard-page fix does not depend on the switch: with it off, a range is asked for whole only when it is one alike region, and a block that is not alike is left as it is.

In-game check on main + this branch (Proton, RX 6900 XT) follows; I will post the numbers here.

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7186325244 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/68#issuecomment-7186325244). Originally posted by [zocram4cc](https://github.com/zocram4cc) on 2026-11-26T08:12:53Z. --- Rebased onto current `main` (the 5 s lines moved into `meter_lines`; the guard counter line is called there now) and `VICTORY_GUARD_RUNS` is **off by default** now (`=1` turns the batching on). The guard-page fix does not depend on the switch: with it off, a range is asked for whole only when it is one alike region, and a block that is not alike is left as it is. In-game check on `main` + this branch (Proton, RX 6900 XT) follows; I will post the numbers here.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/68:upstream-pr/68'
git switch 'upstream-pr/68'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/68'
git switch 'upstream-pr/68'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/68'
git switch 'upstream-pr/68'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/68'
git switch 'main'
git merge --squash 'upstream-pr/68'
git switch 'main'
git merge --ff-only 'upstream-pr/68'
git switch 'main'
git merge 'upstream-pr/68'
git push origin 'main'
Sign in to join this conversation.
No reviewers
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
avatarmanz/VIctory-7900XTX!68
No description provided.