fix(agc): run the unwatch hook before an import whose range leaves the write watch whatever the import does #7505

Merged
AlxcBTW merged 11 commits from upstream-pr/7505 into main 2026-10-26 08:31:24 +00:00
AlxcBTW commented 2026-10-18 09:59:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-18T09:59:25Z.


What

ImportWatched (GuestMemory.cpp) runs the unwatch hook for the spans of a stays-watched import that leave the write watch, before the import when the page walk before it already fails and after it when only the walk after it fails, so pending storage-image results over those spans keep saved guest bytes. The branch also tests every exit of GuestMemory that has a hook.

#6308 (93df5c42a, LeandroLP23, the hook after the import) was closed unmerged as superseded by #8172, and the branch still carries its commit and its merge commit (48a1d3453, Dean Galvin). It no longer depends on #6308. Against current main (a6dcea927; git merge-tree is clean) the net change is 3 files, +92 -3:

  • core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp: in ImportWatched, for an import that stays watched, a span whose page walk fails before the import is taken out of the span list and given to the unwatch hook before the import runs; a span whose walk with the import window fails after the import is given to the hook there. main runs the hook in neither place: both spans leave the watch without it.
  • core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp: CheckUnwatchHooks in agc_write_tracking, for Unwatch (hook once, over the range, while it is watched), an import that does not stay watched (hook before the import), an import whose walk vouches for its pages (no hook, range stays watched) and, on Linux, an import whose range loses a block during the import (hook after it).
  • docs/dev/debt/libSceAgcDriver.md: the pending-results entry names a page walk that fails as an exit that runs the hook, and drops the sentence about an import fallback that unwatched without saving.

What main already covers, not repeated here:

  • #8172 (fix(agc): keep resident DCC targets watched under aligned block imports, merged 2026-10-20) keeps the spans that were watched before a stays-watched import watched through it. That is the case of an import over a partly unwatched range that this PR first wrote a test for (agc_storage_unwalked_import); with #8172 that range stays watched, so the test was dropped from the branch (bae91578f) and the branch no longer asserts that flow.
  • #7432 (fix(agc): save pending results when a fresh write collect fails, merged) runs the hook when a failed fresh collect takes a range out of the watch (MarkWritten). Nothing in this PR touches it.

What the branch still addresses: a span of a stays-watched import that is covered by the watch but whose page walk fails, before the import (the walk's walkWrites returns false) or after it (the walk with the import window returns false). Both paths drop the span from the watch on main without the hook, so pending storage-image results over it have no saved guest bytes and storing them throws. This is read from the code; no title run shows it on current main. The ProsperoPuzzles data in #6301 shows that, on current main, the refusal this chain used to cause no longer appears for that title.

Exits that still drop a range without the hook, not changed here:

  • Linux WatchMapping (DirectMemory.cpp): a second mapping of the same direct memory unregisters both aliases. The hook takes Pending().mutex and the GPU mutex, and the driver calls SharedBacking (takes g_directLock) from code that can hold the GPU mutex, so calling the hook from libkernel's map path could invert that lock order. Not shown to deadlock; not touched.

Tested

  • Linux (Ubuntu 24.04, Mesa lavapipe), fork CI run 39681814909 at a91b68d56 (the PR head bae91578f plus the CI scaffolding commit), ctest regex agc_write_tracking|agc_storage|agc_import|agc_host_import|agc_pending|agc_recorder|agc_driver_host_import|agc_unchanged|agc_collect|agc_partial|agc_resident: 34 of 34 passed, 2 of them skipped (agc_storage_clear_hand_over, agc_host_import_sparse), among them agc_write_tracking and agc_import_watch.
  • Windows (windows-latest, WinLibs GCC, no GPU), trimmed workflow, run 39681845571: 3 of 3 passed or skipped: agc_write_watch_coverage and agc_write_tracking pass, agc_import_watch is skipped (no Vulkan device).
  • Not shown: that CheckUnwatchHooks fails on main without the GuestMemory.cpp change. By reading, its last section (Linux, a block lost during the import) is the part main does not satisfy; no CI run of the test against main was made.
  • Not run on a title with this change. The Minecraft Dungeons II lab runs (PPSA16064, Windows, RTX 5070 Laptop, driver 617.42) used stacks that contain these commits; no row compared the hook with and without them.
  • Not run: macOS, an AMD or Intel GPU, any hardware GPU for the recorder tests (lavapipe only).
  • python3 tools/check_conventions.py --base origin/main on the branch: 0 errors; one warning (debt-length, the pending-results entry in docs/dev/debt/libSceAgcDriver.md, which was already over 1000 characters on main).

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 under Silent stubs in their library's technical debt file
  • 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 (#6308 was closed as superseded by #8172; its commit is in this branch)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Mesa lavapipe, Ubuntu 24.04 (the AGC tests); no hardware GPU
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4908147074 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7505). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-18T09:59:25Z. --- ### What `ImportWatched` (`GuestMemory.cpp`) runs the unwatch hook for the spans of a stays-watched import that leave the write watch, before the import when the page walk before it already fails and after it when only the walk after it fails, so pending storage-image results over those spans keep saved guest bytes. The branch also tests every exit of `GuestMemory` that has a hook. #6308 (`93df5c42a`, LeandroLP23, the hook after the import) was closed unmerged as superseded by #8172, and the branch still carries its commit and its merge commit (`48a1d3453`, Dean Galvin). It no longer depends on #6308. Against current `main` (`a6dcea927`; `git merge-tree` is clean) the net change is 3 files, +92 -3: - `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp`: in `ImportWatched`, for an import that stays watched, a span whose page walk fails before the import is taken out of the span list and given to the unwatch hook before the import runs; a span whose walk with the import window fails after the import is given to the hook there. `main` runs the hook in neither place: both spans leave the watch without it. - `core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp`: `CheckUnwatchHooks` in `agc_write_tracking`, for `Unwatch` (hook once, over the range, while it is watched), an import that does not stay watched (hook before the import), an import whose walk vouches for its pages (no hook, range stays watched) and, on Linux, an import whose range loses a block during the import (hook after it). - `docs/dev/debt/libSceAgcDriver.md`: the pending-results entry names a page walk that fails as an exit that runs the hook, and drops the sentence about an import fallback that unwatched without saving. What `main` already covers, not repeated here: - #8172 (`fix(agc): keep resident DCC targets watched under aligned block imports`, merged 2026-10-20) keeps the spans that were watched before a stays-watched import watched through it. That is the case of an import over a partly unwatched range that this PR first wrote a test for (`agc_storage_unwalked_import`); with #8172 that range stays watched, so the test was dropped from the branch (`bae91578f`) and the branch no longer asserts that flow. - #7432 (`fix(agc): save pending results when a fresh write collect fails`, merged) runs the hook when a failed fresh collect takes a range out of the watch (`MarkWritten`). Nothing in this PR touches it. What the branch still addresses: a span of a stays-watched import that is covered by the watch but whose page walk fails, before the import (the walk's `walkWrites` returns false) or after it (the walk with the import window returns false). Both paths drop the span from the watch on `main` without the hook, so pending storage-image results over it have no saved guest bytes and storing them throws. This is read from the code; no title run shows it on current `main`. The ProsperoPuzzles data in #6301 shows that, on current `main`, the refusal this chain used to cause no longer appears for that title. Exits that still drop a range without the hook, not changed here: - Linux `WatchMapping` (`DirectMemory.cpp`): a second mapping of the same direct memory unregisters both aliases. The hook takes `Pending().mutex` and the GPU mutex, and the driver calls `SharedBacking` (takes `g_directLock`) from code that can hold the GPU mutex, so calling the hook from libkernel's map path could invert that lock order. Not shown to deadlock; not touched. ### Tested - Linux (Ubuntu 24.04, Mesa lavapipe), fork CI run 39681814909 at `a91b68d56` (the PR head `bae91578f` plus the CI scaffolding commit), ctest regex `agc_write_tracking|agc_storage|agc_import|agc_host_import|agc_pending|agc_recorder|agc_driver_host_import|agc_unchanged|agc_collect|agc_partial|agc_resident`: 34 of 34 passed, 2 of them skipped (`agc_storage_clear_hand_over`, `agc_host_import_sparse`), among them `agc_write_tracking` and `agc_import_watch`. - Windows (windows-latest, WinLibs GCC, no GPU), trimmed workflow, run 39681845571: 3 of 3 passed or skipped: `agc_write_watch_coverage` and `agc_write_tracking` pass, `agc_import_watch` is skipped (no Vulkan device). - Not shown: that `CheckUnwatchHooks` fails on `main` without the `GuestMemory.cpp` change. By reading, its last section (Linux, a block lost during the import) is the part `main` does not satisfy; no CI run of the test against `main` was made. - Not run on a title with this change. The Minecraft Dungeons II lab runs (PPSA16064, Windows, RTX 5070 Laptop, driver 617.42) used stacks that contain these commits; no row compared the hook with and without them. - Not run: macOS, an AMD or Intel GPU, any hardware GPU for the recorder tests (lavapipe only). - `python3 tools/check_conventions.py --base origin/main` on the branch: 0 errors; one warning (`debt-length`, the pending-results entry in `docs/dev/debt/libSceAgcDriver.md`, which was already over 1000 characters on `main`). ### Checklist - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 (#6308 was closed as superseded by #8172; its commit is in this branch) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Mesa lavapipe, Ubuntu 24.04 (the AGC tests); no hardware GPU - [x] AI-assisted: yes
akkadbakkad89 commented 2026-10-18 11:28:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-18T11:28:14Z.


Verified at head aaf19708f: agc_write_tracking plus the recorder import/storage sweep (agc_import_watch, agc_import_watch_other_family, agc_storage_*, agc_host_import_unmap, agc_host_import_retirement, agc_host_import_second_mapping) - 15/15 pass on NVIDIA RTX 3070 (driver 617.42) and Intel UHD 630 (ANYPS5_GPU=UHD, igvk64). Windows 11, MinGW (WinLibs GCC 15.2.0), Release.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6249369326 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7505#issuecomment-6249369326). Originally posted by [akkadbakkad89](https://github.com/akkadbakkad89) on 2026-10-18T11:28:14Z. --- Verified at head `aaf19708f`: `agc_write_tracking` plus the recorder import/storage sweep (`agc_import_watch`, `agc_import_watch_other_family`, `agc_storage_*`, `agc_host_import_unmap`, `agc_host_import_retirement`, `agc_host_import_second_mapping`) - 15/15 pass on NVIDIA RTX 3070 (driver 617.42) and Intel UHD 630 (`ANYPS5_GPU=UHD`, igvk64). Windows 11, MinGW (WinLibs GCC 15.2.0), Release.
blawesom commented 2026-10-18 16:29:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-18T16:29:58Z.


Rebase handoff — rebased onto current main (all four commits, authorship unchanged). Test-call and docs-debt conflicts unioned with main's newer coverage.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV): builds; conventions pass; full suite green apart from the two known main regressions (agc_driver_tessellation — Mesa, #5444; agc_driver_bda_device — #7584), neither from this PR.

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/7505
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 9f770c6a5.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6252310252 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7505#issuecomment-6252310252). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-18T16:29:58Z. --- **Rebase handoff** — rebased onto current `main` (all four commits, authorship unchanged). Test-call and docs-debt conflicts unioned with `main`'s newer coverage. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): builds; conventions pass; **full suite green apart from the two known `main` regressions** (`agc_driver_tessellation` — Mesa, #5444; `agc_driver_bda_device` — #7584), neither from this PR. **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/7505 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `9f770c6a5`.
AlxcBTW commented 2026-10-18 18:13:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-18T18:13:44Z.


@blawesom thanks for the rebase handoff. I updated the branch with a merge of main instead of a rebase, so the history stays fast-forward (head 0b8940ec0). Your tree was my reference for the conflicts. It differed from mine in three places, in case they matter for other handoffs:

  • docs/dev/debt/libSceAgcDriver.md: the "Pages under an import window" paragraph is missing and the older Windows paragraph appears twice. The merge keeps main's newer paragraph plus "Pages under an import window".
  • CMakeLists.txt: main's srgb test registration, which landed after your rebase.
  • tests/WriteTracking.cpp: the call order is moved. I kept main's order.

Fork CI: Linux 717/717 (run 39146868385), Windows lab tests green (run 39146868399).

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253385284 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7505#issuecomment-6253385284). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-18T18:13:44Z. --- @blawesom thanks for the rebase handoff. I updated the branch with a merge of `main` instead of a rebase, so the history stays fast-forward (head `0b8940ec0`). Your tree was my reference for the conflicts. It differed from mine in three places, in case they matter for other handoffs: - `docs/dev/debt/libSceAgcDriver.md`: the "Pages under an import window" paragraph is missing and the older Windows paragraph appears twice. The merge keeps main's newer paragraph plus "Pages under an import window". - `CMakeLists.txt`: main's srgb test registration, which landed after your rebase. - `tests/WriteTracking.cpp`: the call order is moved. I kept main's order. Fork CI: Linux 717/717 (run 39146868385), Windows lab tests green (run 39146868399). AI-assisted: yes (Claude Code)
wachi35676 commented 2026-10-19 00:13:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T00:13:20Z.


Tested at 0b8940ec0 on Windows 11 (GCC 15.2.0, Release), merged onto main 9f25d0bf5, on an RTX 5070 Ti (617.42) and an AMD Radeon iGPU, RDNA2 (1002:13c0, 32.0.21042.62, ANYPS5_GPU=AMD).

  • agc_write_tracking, agc_import_watch and agc_import_watch_other_family pass on both.
  • With c20686e11's GuestMemory.cpp hunk reversed, agc_write_tracking fails on both GPUs: an import over a partly unwatched range ran the unwatch hook after the import.
  • In the 724-test agc_|shader|recompiler set, the only new failure was a 30 s timeout of agc_driver_guest_memory_lifetime_heap on NVIDIA, which passes when rerun alone.

agc_storage_unwalked_import is registered for Linux only and wasn't run here.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6256920548 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7505#issuecomment-6256920548). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-19T00:13:20Z. --- Tested at `0b8940ec0` on Windows 11 (GCC 15.2.0, Release), merged onto `main` `9f25d0bf5`, on an RTX 5070 Ti (617.42) and an AMD Radeon iGPU, RDNA2 (`1002:13c0`, 32.0.21042.62, `ANYPS5_GPU=AMD`). - `agc_write_tracking`, `agc_import_watch` and `agc_import_watch_other_family` pass on both. - With `c20686e11`'s `GuestMemory.cpp` hunk reversed, `agc_write_tracking` fails on both GPUs: `an import over a partly unwatched range ran the unwatch hook after the import`. - In the 724-test `agc_|shader|recompiler` set, the only new failure was a 30 s timeout of `agc_driver_guest_memory_lifetime_heap` on NVIDIA, which passes when rerun alone. `agc_storage_unwalked_import` is registered for Linux only and wasn't run here. AI-assisted: yes (Claude Code); results above were run on this machine.
oneandonlydean commented 2026-10-20 14:25:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T14:25:10Z.


@AlxcBTW thanks for this. #8172 has merged and #6308 is closed as superseded, so this branch no longer fits main. Main's ImportWatched now splits a partly watched staysWatched import into spans of covered 64 KiB blocks, keeps those spans watched, and unwatches only the gaps and any span whose walk fails. The single-range fallback that #6308 and c20686e11 edit is gone. Your two commits are written on top of #6308's lines, so dropping #6308's commit won't leave a working change.

The gap you closed is still on main, though. The staysWatched path never calls beforeUnwatch, and two of its exits take watched blocks out of the watch:

  1. A covered span whose CPU walk fails before the import is dropped by the erase_if just ahead of runImport(). It then falls inside the unwatchLocked gap ranges after the import. This is your save-early case: the span will leave the watch whatever the import does, so the hook can run before the import, and the import's own page marks aren't collected as CPU writes.
  2. A span whose window walk fails after the import (continue in the span loop) is unwatched by the next unwatchLocked with no hook. @LeandroLP23 offered to cover this path in #6308's closing comment.

Could you rework this PR onto current main as the single fix for both? (Please agree with @LeandroLP23 so only one PR carries it.)

  • Before runImport(), collect the spans the erase_if drops, release the tracker lock, run beforeUnwatch over them, and relock. Those spans are unwatched in any case, and the surviving spans are walked again against the window after the import, so a tracker change while the lock is released is harmless.
  • After the import, collect the spans whose window walk fails and run beforeUnwatch over them, again with the lock released, before they're unwatched. Like MarkWritten: collect, hook outside the lock, then unwatch.
  • Make sure each span gets the hook exactly once, and that gaps that were already unwatched don't get it.
  • Tests under #8172's semantics, where a partly watched import keeps its watched blocks watched: make a covered span fail its walk before the import (and, separately, inside the import window), and check that the hook ran over that span before it left the watch, and only once. Keep the check that a vouched span gets no hook and stays watched.

As far as we know, no current title reaches these two exits: the reports in #6301 run on main with #8172. So this is a correctness fix for a rare path. If it's reached today, the result is the explicit "results pending ... without the guest bytes saved" refusal, not wrong output.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293158450 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7505#issuecomment-6293158450). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T14:25:10Z. --- @AlxcBTW thanks for this. #8172 has merged and #6308 is closed as superseded, so this branch no longer fits main. Main's `ImportWatched` now splits a partly watched `staysWatched` import into spans of covered 64 KiB blocks, keeps those spans watched, and unwatches only the gaps and any span whose walk fails. The single-range fallback that #6308 and c20686e11 edit is gone. Your two commits are written on top of #6308's lines, so dropping #6308's commit won't leave a working change. The gap you closed is still on main, though. The `staysWatched` path never calls `beforeUnwatch`, and two of its exits take watched blocks out of the watch: 1. A covered span whose CPU walk fails **before** the import is dropped by the `erase_if` just ahead of `runImport()`. It then falls inside the `unwatchLocked` gap ranges after the import. This is your save-early case: the span will leave the watch whatever the import does, so the hook can run before the import, and the import's own page marks aren't collected as CPU writes. 2. A span whose window walk fails **after** the import (`continue` in the span loop) is unwatched by the next `unwatchLocked` with no hook. @LeandroLP23 offered to cover this path in #6308's closing comment. Could you rework this PR onto current main as the single fix for both? (Please agree with @LeandroLP23 so only one PR carries it.) - Before `runImport()`, collect the spans the `erase_if` drops, release the tracker lock, run `beforeUnwatch` over them, and relock. Those spans are unwatched in any case, and the surviving spans are walked again against the window after the import, so a tracker change while the lock is released is harmless. - After the import, collect the spans whose window walk fails and run `beforeUnwatch` over them, again with the lock released, before they're unwatched. Like `MarkWritten`: collect, hook outside the lock, then unwatch. - Make sure each span gets the hook exactly once, and that gaps that were already unwatched don't get it. - Tests under #8172's semantics, where a partly watched import keeps its watched blocks watched: make a covered span fail its walk before the import (and, separately, inside the import window), and check that the hook ran over that span before it left the watch, and only once. Keep the check that a vouched span gets no hook and stays watched. As far as we know, no current title reaches these two exits: the reports in #6301 run on main with #8172. So this is a correctness fix for a rare path. If it's reached today, the result is the explicit "results pending ... without the guest bytes saved" refusal, not wrong output.
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!7505
No description provided.