fix(agc): answer a collect memo hit with the generation of the last finished tracker update #6169

Merged
mitul72 merged 1 commit from upstream-pr/6169 into main 2026-10-23 14:18:38 +00:00
mitul72 commented 2026-10-15 00:10:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mitul72 on 2026-10-15T00:10:53Z.


What

Race: a per-thread collect memo hit read the tracker generation without the tracker mutex. A walk on another thread bumps the generation before it stamps the blocks it finds written, so the hit could return a value that walk then stamps blocks with. A caller that recorded it as its verified generation then trusts those blocks in every later UnchangedSince and never refreshes them.

The tracker now publishes the generation each time its mutex is released (TrackerLock), after every stamp of that update is in place, and a memo hit returns the published value. Completed MarkWritten bumps are still seen at once; a value older than the live one only makes a check fail and refresh.

Test agc_collect_memo_generation is deterministic. It builds GuestMemory.cpp into its own executable (Linux only, CMake NOT WIN32) together with a fake write watch that defines the libc write-watch functions itself, so the test executable's definitions override the ones in libc. The fake holds a walk after its generation bump, a memo hit is taken on another thread meanwhile, and the test checks that the write the walk then stamps is not trusted against the generation the hit returned. Overriding the functions is what lets the test hold a walk at an exact point, which the real write watch does not allow; on Windows the walk calls the GuestArena functions instead, hence Linux only.

Tested

Linux, RX 9060 XT (RADV GFX12, local Mesa). Full ctest passes except agc_driver_tessellation, which segfaults inside RADV while linking pipeline libraries on unmodified upstream 703aa8f63 as well.

agc_collect_memo_generation fails 5 of 5 runs with the previous GuestMemory.cpp and passes 20 of 20 with the fix. It is registered for Linux only: on Windows the collect walk goes through the GuestArena functions instead of the libc write watch, and overriding those through MinGW import libraries is not verified.

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
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 9060 XT (RADV GFX12, local Mesa build), Linux
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4866257094 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6169). Originally posted by [mitul72](https://github.com/mitul72) on 2026-10-15T00:10:53Z. --- ### What Race: a per-thread collect memo hit read the tracker generation without the tracker mutex. A walk on another thread bumps the generation before it stamps the blocks it finds written, so the hit could return a value that walk then stamps blocks with. A caller that recorded it as its verified generation then trusts those blocks in every later `UnchangedSince` and never refreshes them. The tracker now publishes the generation each time its mutex is released (`TrackerLock`), after every stamp of that update is in place, and a memo hit returns the published value. Completed `MarkWritten` bumps are still seen at once; a value older than the live one only makes a check fail and refresh. Test `agc_collect_memo_generation` is deterministic. It builds `GuestMemory.cpp` into its own executable (Linux only, CMake `NOT WIN32`) together with a fake write watch that defines the libc write-watch functions itself, so the test executable's definitions override the ones in `libc`. The fake holds a walk after its generation bump, a memo hit is taken on another thread meanwhile, and the test checks that the write the walk then stamps is not trusted against the generation the hit returned. Overriding the functions is what lets the test hold a walk at an exact point, which the real write watch does not allow; on Windows the walk calls the GuestArena functions instead, hence Linux only. ### Tested Linux, RX 9060 XT (RADV GFX12, local Mesa). Full `ctest` passes except `agc_driver_tessellation`, which segfaults inside RADV while linking pipeline libraries on unmodified upstream 703aa8f63 as well. `agc_collect_memo_generation` fails 5 of 5 runs with the previous `GuestMemory.cpp` and passes 20 of 20 with the fix. It is registered for Linux only: on Windows the collect walk goes through the GuestArena functions instead of the libc write watch, and overriding those through MinGW import libraries is not verified. ### 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 - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 9060 XT (RADV GFX12, local Mesa build), Linux - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dextroid17 commented 2026-10-15 01:02:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-15T01:02:46Z.


Ran this on PR head 077fec730 (merge base ab9a47684, which is current main).

Setup: PR head checked out as-is (not rebased), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), Linux. The test is CPU-only; no GPU was used.

  • agc_collect_memo_generation_tests on the PR: 3 of 3 passed ("collect memo generation test passed").
  • Mutation check: kept the PR's test, put back main's Execution/src/GuestMemory.cpp, rebuilt, ran 3 times: 3 of 3 failed with the test's own message, "collect memo generation test failed: a memoized collect returned the generation a walk in progress then stamped a write with".

So on this setup the test fails without the change and passes with it.

Not checked: Windows (the PR has a _WIN32 branch), other platforms, or any title. Many open PRs also touch GuestMemory.cpp; I checked overlap only by file, not by function.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6181959805 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6169#issuecomment-6181959805). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-15T01:02:46Z. --- Ran this on PR head 077fec730 (merge base ab9a47684, which is current main). Setup: PR head checked out as-is (not rebased), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), Linux. The test is CPU-only; no GPU was used. - agc_collect_memo_generation_tests on the PR: 3 of 3 passed ("collect memo generation test passed"). - Mutation check: kept the PR's test, put back main's Execution/src/GuestMemory.cpp, rebuilt, ran 3 times: 3 of 3 failed with the test's own message, "collect memo generation test failed: a memoized collect returned the generation a walk in progress then stamped a write with". So on this setup the test fails without the change and passes with it. Not checked: Windows (the PR has a _WIN32 branch), other platforms, or any title. Many open PRs also touch GuestMemory.cpp; I checked overlap only by file, not by function. AI-assisted: yes
nicolasfalesy commented 2026-10-16 08:52:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-16T08:52:30Z.


Tested on Linux (Arch), c++ (GCC) 16.2.1 20260810, Release, BUILD_TESTING=ON, main at 5d9dcc42d2ec5401aebd089028b8d4d7410a77b4.

agc_collect_memo_generation (this PR's test) fails on main plus this PR (2 of 2). It passes on the PR head as submitted (merge base ab9a47684).

collect memo generation test failed: a memoized collect returned the generation a walk in progress then stamped a write with

Most likely cause: since the merge base, main (3b34d1fdb) added a second memo path in collectWrites, the per-epoch map (if (epochCollectMapEnabled()), GuestMemory.cpp:1191 on main). Its hit still returns tracker.generation.load(std::memory_order_relaxed) (GuestMemory.cpp:1195 on main). This PR only changes the thread memo hit (GuestMemory.cpp:1192 on its head) to return tracker.stable, so the epoch-map hit still returns the generation a walk in progress later stamps a write with.

Suggested fix: return tracker.stable.load(std::memory_order_acquire) in the epoch-map hit as well. I tried that on the merge and the test passes.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6211819306 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6169#issuecomment-6211819306). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-16T08:52:30Z. --- Tested on Linux (Arch), `c++ (GCC) 16.2.1 20260810`, Release, `BUILD_TESTING=ON`, `main` at `5d9dcc42d2ec5401aebd089028b8d4d7410a77b4`. `agc_collect_memo_generation` (this PR's test) fails on `main` plus this PR (2 of 2). It passes on the PR head as submitted (merge base ab9a47684). ``` collect memo generation test failed: a memoized collect returned the generation a walk in progress then stamped a write with ``` Most likely cause: since the merge base, `main` (3b34d1fdb) added a second memo path in `collectWrites`, the per-epoch map (`if (epochCollectMapEnabled())`, `GuestMemory.cpp:1191` on `main`). Its hit still returns `tracker.generation.load(std::memory_order_relaxed)` (`GuestMemory.cpp:1195` on `main`). This PR only changes the thread memo hit (`GuestMemory.cpp:1192` on its head) to return `tracker.stable`, so the epoch-map hit still returns the generation a walk in progress later stamps a write with. Suggested fix: return `tracker.stable.load(std::memory_order_acquire)` in the epoch-map hit as well. I tried that on the merge and the test passes. AI-assisted: yes
adriadam10 commented 2026-10-18 16:52:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-18T16:52:09Z.

Review: CHANGES_REQUESTED


Thanks for the fix and the deterministic test. It is not complete against current main:

  • core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:1249: the per-epoch map hit (epochCollectMapEnabled(), added to main after your merge base) still returns tracker.generation.load(std::memory_order_relaxed), so it has the same race. Return tracker.stable.load(std::memory_order_acquire) there too.
  • As a result agc_collect_memo_generation fails on main + this PR ("a memoized collect returned the generation a walk in progress then stamped a write with"), as @nicolasfalesy reported. Please rebase on current main, apply the change and confirm the test passes on the merged tree.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5563944129 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6169#pullrequestreview-5563944129). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-18T16:52:09Z. **Review:** CHANGES_REQUESTED --- Thanks for the fix and the deterministic test. It is not complete against current `main`: - `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:1249`: the per-epoch map hit (`epochCollectMapEnabled()`, added to `main` after your merge base) still returns `tracker.generation.load(std::memory_order_relaxed)`, so it has the same race. Return `tracker.stable.load(std::memory_order_acquire)` there too. - As a result `agc_collect_memo_generation` fails on `main` + this PR ("a memoized collect returned the generation a walk in progress then stamped a write with"), as @nicolasfalesy reported. Please rebase on current `main`, apply the change and confirm the test passes on the merged tree.
mitul72 commented 2026-10-20 18:02:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mitul72 on 2026-10-20T18:02:17Z.


Thanks @Dextroid17 and @nicolasfalesy for testing, and @adriadam10 for the review. I've been a little busy lately, but I'll update this against current main today.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6297649918 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6169#issuecomment-6297649918). Originally posted by [mitul72](https://github.com/mitul72) on 2026-10-20T18:02:17Z. --- Thanks @Dextroid17 and @nicolasfalesy for testing, and @adriadam10 for the review. I've been a little busy lately, but I'll update this against current `main` today.
mitul72 commented 2026-10-21 01:23:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mitul72 on 2026-10-21T01:23:22Z.


@adriadam10 Rebased onto main (d97361bd0) and applied it: the per-epoch map hit now returns tracker.stable as well.

One more change was needed on the new base: MarkWritten now drops the tracker mutex around beforeUnwatch and stamps again after relocking, so publishing on that release would hand out a generation the same update then stamps with. That release now goes through Suspend/Resume, which don't publish; every other release still does. The test also links PerformanceTraceThread.cpp now, like the other tests that build GuestMemory.cpp.

On the merged tree agc_collect_memo_generation passes, and it fails again with the epoch-map line reverted. Of the agc tests, 836/839 pass; the three failures (guest_memory_lifetime, image_array_window_past_layer_limit, tessellation) fail the same way on plain main on this machine (RX 9060 XT, RADV).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6305675038 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6169#issuecomment-6305675038). Originally posted by [mitul72](https://github.com/mitul72) on 2026-10-21T01:23:22Z. --- @adriadam10 Rebased onto `main` (d97361bd0) and applied it: the per-epoch map hit now returns `tracker.stable` as well. One more change was needed on the new base: `MarkWritten` now drops the tracker mutex around `beforeUnwatch` and stamps again after relocking, so publishing on that release would hand out a generation the same update then stamps with. That release now goes through `Suspend`/`Resume`, which don't publish; every other release still does. The test also links `PerformanceTraceThread.cpp` now, like the other tests that build `GuestMemory.cpp`. On the merged tree `agc_collect_memo_generation` passes, and it fails again with the epoch-map line reverted. Of the `agc` tests, 836/839 pass; the three failures (`guest_memory_lifetime`, `image_array_window_past_layer_limit`, `tessellation`) fail the same way on plain `main` on this machine (RX 9060 XT, RADV).
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!6169
No description provided.