fix(agc): note BDA-written pages at full address width and read back only the claimed ones #3175

Merged
Raghav314 merged 2 commits from upstream-pr/3175 into main 2026-10-11 08:00:07 +00:00
Raghav314 commented 2026-10-11 00:11:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-11T00:11:35Z.


What

A shader store through the BDA table records its 4 KiB page in the BDA fault buffer, and after the use the driver marks those pages written (markWrittenPages), so the CPU sees what the GPU stored. Two problems with the record:

  • The page number was cut to 32 bits (address >> 12 plus one), so a store above 16 TiB was recorded, and marked, as a page at another address. On Linux the guest's own image and libraries sit far above that: in PPSA36082 the BDA table has writable ranges up to 0x562… (about 86 TiB). In my runs no shader stored there, but nothing kept one from doing it.
  • The driver scanned all 4096 slots after every use, 16 KiB of host-visible memory, also when nothing was stored. Standing in PPSA36082's hub that scan was 7.4% of the queue worker's time.

Now:

  • A slot holds a 128 KiB chunk, its number plus one (0 marks an empty slot, and 32 bits cover any address below 512 TiB), and a mask of its 32 pages. A store sets its page's bit, and loads the mask first so a page already noted costs no atomic write.
  • The shader that claims a slot appends it to a list and counts it. The driver reads the count, decodes only the claimed slots, marks each run of pages once and clears only those slots. Nothing stored means two reads (the overflow flag and the count).
  • Building a BDA table refuses a writable range that ends past 512 TiB, instead of recording its pages under the wrong chunk.
  • Running out of probes still raises the existing "not implemented" error. There are now 2048 slots of 128 KiB instead of 4096 of 4 KiB, so it takes 16 times as much written memory to get there.
  • The fault buffer grows from 16 to 24 KiB, but a new one still zeroes only 16 KiB: the claim list is read only up to its count.

ForEachWrittenRange (in BdaAbi.hpp) is the decoding, a plain function the tests call too. It checks every claim before it marks any.

Tested

Linux, RTX 4090, NVIDIA 615.71.09, local run of the Linux CI job on main 69685646 (SPIRV tools on): ctest 778/779. The failure, agc_driver_dcc_keys_first_write, fails the same way on main.

  • agc_driver_bda_device: the decoding of an empty record, pages near the top of the address space and above 16 TiB, whole, first-page and last-page chunks, and bad slots or counts. The dword-write cases now check that exactly the stored pages are noted and that each chunk is claimed once, also when 64 workgroups store to the same page.
  • agc_driver_flat_store (Linux) stores into write-watched guest blocks at 8 TiB and at 32 TiB, twice each, and checks that the stored block is stamped written and the other one is not. On main the 32 TiB block fails ("the stored block is not stamped as written") and the 8 TiB block passes. Where host imports are compared instead of write-watched (RADV), the case prints that and skips.

I ran PPSA36082 on my machine (main aa79dbdf plus local multisampled-target support it needs to get in game), standing still in the hub, 20 s of fps and 10 s of perf, one run each in the same session. This was an earlier revision of the branch with 4096 slots; the changes since only shrink the fault buffer and check the record before marking:

fps p95 frame markWrittenPages, share of the queue worker
main 24.7 42.5 ms 7.36%
this PR 25.1 42.4 ms 0.24%

The worker stays about 97% busy in both, so the frame rate change is within noise; the time goes to the next hot spots (shader memory captures, allocation).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4815389300 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3175). Originally posted by [Raghav314](https://github.com/Raghav314) on 2026-10-11T00:11:35Z. --- ### What A shader store through the BDA table records its 4 KiB page in the BDA fault buffer, and after the use the driver marks those pages written (`markWrittenPages`), so the CPU sees what the GPU stored. Two problems with the record: - The page number was cut to 32 bits (`address >> 12` plus one), so a store above 16 TiB was recorded, and marked, as a page at another address. On Linux the guest's own image and libraries sit far above that: in PPSA36082 the BDA table has writable ranges up to `0x562…` (about 86 TiB). In my runs no shader stored there, but nothing kept one from doing it. - The driver scanned all 4096 slots after every use, 16 KiB of host-visible memory, also when nothing was stored. Standing in PPSA36082's hub that scan was 7.4% of the queue worker's time. Now: - A slot holds a 128 KiB chunk, its number plus one (0 marks an empty slot, and 32 bits cover any address below 512 TiB), and a mask of its 32 pages. A store sets its page's bit, and loads the mask first so a page already noted costs no atomic write. - The shader that claims a slot appends it to a list and counts it. The driver reads the count, decodes only the claimed slots, marks each run of pages once and clears only those slots. Nothing stored means two reads (the overflow flag and the count). - Building a BDA table refuses a writable range that ends past 512 TiB, instead of recording its pages under the wrong chunk. - Running out of probes still raises the existing "not implemented" error. There are now 2048 slots of 128 KiB instead of 4096 of 4 KiB, so it takes 16 times as much written memory to get there. - The fault buffer grows from 16 to 24 KiB, but a new one still zeroes only 16 KiB: the claim list is read only up to its count. `ForEachWrittenRange` (in `BdaAbi.hpp`) is the decoding, a plain function the tests call too. It checks every claim before it marks any. ### Tested Linux, RTX 4090, NVIDIA 615.71.09, local run of the Linux CI job on main 69685646 (SPIRV tools on): ctest 778/779. The failure, `agc_driver_dcc_keys_first_write`, fails the same way on main. - `agc_driver_bda_device`: the decoding of an empty record, pages near the top of the address space and above 16 TiB, whole, first-page and last-page chunks, and bad slots or counts. The dword-write cases now check that exactly the stored pages are noted and that each chunk is claimed once, also when 64 workgroups store to the same page. - `agc_driver_flat_store` (Linux) stores into write-watched guest blocks at 8 TiB and at 32 TiB, twice each, and checks that the stored block is stamped written and the other one is not. On main the 32 TiB block fails ("the stored block is not stamped as written") and the 8 TiB block passes. Where host imports are compared instead of write-watched (RADV), the case prints that and skips. I ran PPSA36082 on my machine (main aa79dbdf plus local multisampled-target support it needs to get in game), standing still in the hub, 20 s of fps and 10 s of `perf`, one run each in the same session. This was an earlier revision of the branch with 4096 slots; the changes since only shrink the fault buffer and check the record before marking: | | fps | p95 frame | `markWrittenPages`, share of the queue worker | |---|---|---|---| | main | 24.7 | 42.5 ms | 7.36% | | this PR | 25.1 | 42.4 ms | 0.24% | The worker stays about 97% busy in both, so the frame rate change is within noise; the time goes to the next hot spots (shader memory captures, allocation). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Dextroid17 commented 2026-10-11 00:27:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-11T00:27:56Z.

Review: COMMENTED


This is the most careful fix in the batch, and it is careful in the way that matters: both problems are stated with the evidence that makes them problems, and one of them is explicitly labelled as latent.

The 32-bit truncation is a real bug, and the write-up says how it was found and how narrow it is. A page number cut to 32 bits covers 16 TiB of 4 KiB pages, so a store above that was recorded and marked at a page number which wrapped — and PPSA36082's BDA table has writable ranges up to about 86 TiB. "In my runs no shader stored there, but nothing kept one from doing it" is the honest form of that finding, and the test makes it checkable rather than argued: on main the 32 TiB block fails and the 8 TiB one passes, which draws the boundary exactly where the arithmetic puts it.

The redesign removes the scan rather than shortening it, and the details are the ones that decide whether that holds. A slot holding a 128 KiB chunk, its number and a 32-page mask; claiming through a list the driver reads only up to its count; loading the mask before writing a bit so an already-noted page costs no atomic write; reading only the claimed slots and clearing only those; and refusing a writable range past 512 TiB at build time instead of recording its pages under the wrong chunk. ForEachWrittenRange checking every claim before it marks any is the part that makes the decode safe on a record that arrives from the guest.

Refusing to claim the frame rate is the right call. 7.36% of the queue worker becoming 0.24%, with the frame rate inside noise and the author saying so and pointing at the next hot spots, is more useful than a figure that would not survive a second run — and the discrimination at 8 and 32 TiB is where the argument actually lives. Noting that the one failing ctest fails identically on main, and that the profile predates the final slot layout, is the same discipline.

The race the redesign has to survive is covered, and it is worth pointing at. A slot is claimed by an atomic compare-exchange on the chunk key — zero to the key, first writer wins — with the count raised by an atomic add and the page bit set through an atomic load-and-or, which is also what lets an already-noted page cost no atomic write. The 64-workgroup case against one page asserts the claimed count is exactly the number of chunks rather than merely non-zero, so the assertion is as strong as the mechanism. That is the part of this change I would point a future reader at first.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481336107 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3175#pullrequestreview-5481336107). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-11T00:27:56Z. **Review:** COMMENTED --- This is the most careful fix in the batch, and it is careful in the way that matters: both problems are stated with the evidence that makes them problems, and one of them is explicitly labelled as latent. **The 32-bit truncation is a real bug, and the write-up says how it was found and how narrow it is.** A page number cut to 32 bits covers 16 TiB of 4 KiB pages, so a store above that was recorded and marked at a page number which wrapped — and PPSA36082's BDA table has writable ranges up to about 86 TiB. "In my runs no shader stored there, but nothing kept one from doing it" is the honest form of that finding, and the test makes it checkable rather than argued: on `main` the 32 TiB block fails and the 8 TiB one passes, which draws the boundary exactly where the arithmetic puts it. **The redesign removes the scan rather than shortening it, and the details are the ones that decide whether that holds.** A slot holding a 128 KiB chunk, its number and a 32-page mask; claiming through a list the driver reads only up to its count; loading the mask before writing a bit so an already-noted page costs no atomic write; reading only the claimed slots and clearing only those; and refusing a writable range past 512 TiB at build time instead of recording its pages under the wrong chunk. `ForEachWrittenRange` checking every claim before it marks any is the part that makes the decode safe on a record that arrives from the guest. **Refusing to claim the frame rate is the right call.** 7.36% of the queue worker becoming 0.24%, with the frame rate inside noise and the author saying so and pointing at the next hot spots, is more useful than a figure that would not survive a second run — and the discrimination at 8 and 32 TiB is where the argument actually lives. Noting that the one failing ctest fails identically on `main`, and that the profile predates the final slot layout, is the same discipline. **The race the redesign has to survive is covered, and it is worth pointing at.** A slot is claimed by an atomic compare-exchange on the chunk key — zero to the key, first writer wins — with the count raised by an atomic add and the page bit set through an atomic load-and-or, which is also what lets an already-noted page cost no atomic write. The 64-workgroup case against one page asserts the claimed count is *exactly* the number of chunks rather than merely non-zero, so the assertion is as strong as the mechanism. That is the part of this change I would point a future reader at first. Written with AI assistance.
Silverash-i8 commented 2026-10-11 01:31:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T01:31:52Z.


Windows run, since the PR was tested on Linux: Windows 11, WinLibs GCC 15.2.0 r7, Release, head f0f02cca merged onto main d31b57a4 (alone, and in a batch with #3039, #3134 and #3194).

agc_driver_bda_device and agc_driver_bda_span_reads pass, but agc_driver_flat_store fails on both GPUs here (RTX 5060 Laptop and the Arrow Lake iGPU), in the new _WIN32 branch of RunStampedBlocks:

[gpu] deferred write-back failed: BDA access failed: address=0x21191fe0080 instruction=0x4c bytes=4 stage=4 reason=2; the store hit 0x21191fe0000+0x10000, read-only in the BDA table: stores through GPU-selected descriptors and FLAT/GLOBAL addresses reach only writable ranges imported in place, not ones served by a mirror or a copy (past APS5_HOST_IMPORT_MIB, or refused by the driver)
AGC graphics: flat store: the watched guest blocks are not collected

So on Windows the write-watched GuestArena block isn't imported in place: the driver serves it through a mirror and puts it in the BDA table read-only, the first Dispatch store into it is refused, and the CollectWrites that follows returns 0. The same test passes on main here, where the watched block doesn't exist. The rest of the agc_* suite shows nothing else new from this PR on either GPU.

If a GPU store into a write-watched block can't happen on Windows by design, maybe the stamped-block case belongs in the Linux-only branch like the fixed-address blocks, or the Windows branch could expect the refusal instead. I haven't changed anything on your branch.

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104220864 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3175#issuecomment-6104220864). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T01:31:52Z. --- Windows run, since the PR was tested on Linux: Windows 11, WinLibs GCC 15.2.0 r7, Release, head `f0f02cca` merged onto `main` d31b57a4 (alone, and in a batch with #3039, #3134 and #3194). `agc_driver_bda_device` and `agc_driver_bda_span_reads` pass, but `agc_driver_flat_store` fails on both GPUs here (RTX 5060 Laptop and the Arrow Lake iGPU), in the new `_WIN32` branch of `RunStampedBlocks`: ``` [gpu] deferred write-back failed: BDA access failed: address=0x21191fe0080 instruction=0x4c bytes=4 stage=4 reason=2; the store hit 0x21191fe0000+0x10000, read-only in the BDA table: stores through GPU-selected descriptors and FLAT/GLOBAL addresses reach only writable ranges imported in place, not ones served by a mirror or a copy (past APS5_HOST_IMPORT_MIB, or refused by the driver) AGC graphics: flat store: the watched guest blocks are not collected ``` So on Windows the write-watched `GuestArena` block isn't imported in place: the driver serves it through a mirror and puts it in the BDA table read-only, the first `Dispatch` store into it is refused, and the `CollectWrites` that follows returns 0. The same test passes on `main` here, where the watched block doesn't exist. The rest of the `agc_*` suite shows nothing else new from this PR on either GPU. If a GPU store into a write-watched block can't happen on Windows by design, maybe the stamped-block case belongs in the Linux-only branch like the fixed-address blocks, or the Windows branch could expect the refusal instead. I haven't changed anything on your branch. AI-assisted (Claude Code); I ran this on my machine.
Raghav314 commented 2026-10-11 01:36:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-11T01:36:55Z.


@Silverash-i8 thanks for testing on Windows. On Windows the arena block gets mirrored instead of imported in place, so it ends up read-only in the BDA table and the store never lands, which is why nothing gets stamped. That's just how Windows handles arena memory right now, so I moved the watched-block case to Linux only, next to the fixed-address blocks (8 TiB and 32 TiB). The Windows side of the test is the same as main again.

LLM assistance used.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104256778 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3175#issuecomment-6104256778). Originally posted by [Raghav314](https://github.com/Raghav314) on 2026-10-11T01:36:55Z. --- @Silverash-i8 thanks for testing on Windows. On Windows the arena block gets mirrored instead of imported in place, so it ends up read-only in the BDA table and the store never lands, which is why nothing gets stamped. That's just how Windows handles arena memory right now, so I moved the watched-block case to Linux only, next to the fixed-address blocks (8 TiB and 32 TiB). The Windows side of the test is the same as main again. LLM assistance used.
Silverash-i8 commented 2026-10-11 02:00:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T02:00:30Z.


@Raghav314 confirmed on the new head bb6ac3e2 (merged onto main d31b57a4, same Windows 11 / WinLibs GCC 15.2.0 setup): agc_driver_flat_store, agc_driver_bda_device, agc_driver_bda_span_reads and agc_driver_dcc_keys_first_write all pass on both the RTX 5060 and the Arrow Lake iGPU.

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104422575 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3175#issuecomment-6104422575). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T02:00:30Z. --- @Raghav314 confirmed on the new head `bb6ac3e2` (merged onto `main` d31b57a4, same Windows 11 / WinLibs GCC 15.2.0 setup): `agc_driver_flat_store`, `agc_driver_bda_device`, `agc_driver_bda_span_reads` and `agc_driver_dcc_keys_first_write` all pass on both the RTX 5060 and the Arrow Lake iGPU. AI-assisted (Claude Code); I ran this on my machine.
adriadam10 commented 2026-10-11 07:06:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:06:13Z.

Review: CHANGES_REQUESTED


Thanks, the design looks right to me: the decoding validates every claim before marking, the slot/list bounds hold, and agc_driver_bda_device passes here.

One problem with the new agc_driver_flat_store case (tests/execution/FlatStore.cpp, RunStampedBlocks):

  • On RADV (Mesa, Linux) it fails with flat store: the watched guest blocks are not collected. After the first Dispatch the mmap'd block is no longer write-watched (GuestWriteWatchCovers returns false), because on that driver host imports resolve write protection ([write-watch] host imports resolve write protection: yes ... imported ranges are compared). So CollectWrites returns 0 and the test asserts instead of skipping. It passes on lavapipe and, per your run, on NVIDIA, where the imports stay watched.
  • Fix: run the stamped-block case only when the imported range stays watched (check GuestMemory::Watched after the warm-up Dispatch, and skip that case when it is false), or check the stamping through the compared-range path as well, so the test doesn't depend on the driver.

Rest of the checks (build, other libSceAgc/AgcDriver tests) green on RADV.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5482275617 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3175#pullrequestreview-5482275617). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:06:13Z. **Review:** CHANGES_REQUESTED --- Thanks, the design looks right to me: the decoding validates every claim before marking, the slot/list bounds hold, and `agc_driver_bda_device` passes here. One problem with the new `agc_driver_flat_store` case (`tests/execution/FlatStore.cpp`, `RunStampedBlocks`): - On RADV (Mesa, Linux) it fails with `flat store: the watched guest blocks are not collected`. After the first `Dispatch` the mmap'd block is no longer write-watched (`GuestWriteWatchCovers` returns false), because on that driver host imports resolve write protection (`[write-watch] host imports resolve write protection: yes ... imported ranges are compared`). So `CollectWrites` returns 0 and the test asserts instead of skipping. It passes on lavapipe and, per your run, on NVIDIA, where the imports stay watched. - Fix: run the stamped-block case only when the imported range stays watched (check `GuestMemory::Watched` after the warm-up `Dispatch`, and skip that case when it is false), or check the stamping through the compared-range path as well, so the test doesn't depend on the driver. Rest of the checks (build, other libSceAgc/AgcDriver tests) green on RADV.
Raghav314 commented 2026-10-11 07:28:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-11T07:28:15Z.


@adriadam10 thanks, good catch. I went with your first option: after the warm-up Dispatch, the stamped-block case checks GuestMemory::Watched over both blocks and skips with a message if the import isn't watched, same as the snapshot-reuse test in Recorder.cpp does. On NVIDIA it still runs both the 8 TiB and 32 TiB blocks. I don't have an AMD card, so I couldn't run it on RADV myself.

Also rebased onto current main (69685646); the full Linux CI job passes locally except agc_driver_dcc_keys_first_write, which fails on main too.

LLM assistance used.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106677867 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3175#issuecomment-6106677867). Originally posted by [Raghav314](https://github.com/Raghav314) on 2026-10-11T07:28:15Z. --- @adriadam10 thanks, good catch. I went with your first option: after the warm-up `Dispatch`, the stamped-block case checks `GuestMemory::Watched` over both blocks and skips with a message if the import isn't watched, same as the snapshot-reuse test in `Recorder.cpp` does. On NVIDIA it still runs both the 8 TiB and 32 TiB blocks. I don't have an AMD card, so I couldn't run it on RADV myself. Also rebased onto current main (69685646); the full Linux CI job passes locally except `agc_driver_dcc_keys_first_write`, which fails on main too. LLM assistance used.
adriadam10 commented 2026-10-11 08:00:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T08:00:04Z.

Review: APPROVED


Checked the decoding (ForEachWrittenRange validates every claim before marking), the slot/list bounds and the 512 TiB limit on writable ranges; build + agc_driver_bda_device, agc_driver_bda_span_reads and agc_driver_flat_store pass on RADV here (flat_store skips the stamped-block case there, as intended), and the new decode tests don't compile without the change. Windows run by @Silverash-i8 and the NVIDIA run by the author cover the rest. The RADV skip from my earlier review is addressed; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5482458085 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3175#pullrequestreview-5482458085). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T08:00:04Z. **Review:** APPROVED --- Checked the decoding (`ForEachWrittenRange` validates every claim before marking), the slot/list bounds and the 512 TiB limit on writable ranges; build + `agc_driver_bda_device`, `agc_driver_bda_span_reads` and `agc_driver_flat_store` pass on RADV here (flat_store skips the stamped-block case there, as intended), and the new decode tests don't compile without the change. Windows run by @Silverash-i8 and the NVIDIA run by the author cover the rest. The RADV skip from my earlier review is addressed; merging.
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
boykopovar/AnyPS5!3175
No description provided.