fix(libc): close two write-watch report races on Linux #24008

Merged
chitters2004 merged 2 commits from upstream-pr/24008 into main 2026-11-27 05:58:45 +00:00
chitters2004 commented 2026-11-20 00:51:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-20T00:51:55Z.


What

Two ordering bugs in the Linux write watch (core/libs/prx/libc/src/GuestWriteWatch.cpp, userfaultfd write protection), one commit each.

takeFresh reported a never-armed range as written and only then write-protected it. A guest store between the report and the protect went through unfaulted and the protect then hid it, so the next collect did not list the page and a consumer that had already re-read the range kept stale data. It now protects every fresh piece first and reports afterwards: a store before the protect is covered by the fresh report, a store after it sets the written state the next scan lists. The protect-failure path is unchanged (marks put back, range reported).

A collect resolved its alias plan before takeFresh and the scan, and fanned every report through that snapshot. An Alias that linked the views in between left it stale: the scan cleared the written state of a store made through one view after the link and reported it to that view only, so the partner view never heard of it. Link changes are now counted (_linkVersion), and each Fanout compares its version at report time, which is after the scan that consumed the pages, and re-resolves its translations under the shared lock when they differ. Until then it points at the plan's translations, so a collect that sees no link change allocates and copies nothing (one acquire load per report). The protect-failure report in takeFresh moved out of the lock, since a report can now take it.

Tests in core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp (agc_write_tracking, Linux only): CheckWriteAfterFreshReport stores from inside the fresh-range report callback and requires the store to be listed; CheckLinkDuringCollect links two views from inside the first collect's report callback, stores through the first view and requires the same collect to report the store at the second.

Not covered, by design: a store made before an Alias call is not fanned out by this change (that is #23976, Watch::Alias marking both views fresh).

Tested

Checklist

  • Based on current main (8e158a83d9); 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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (write watch is CPU and kernel only; tests ran on fork CI Linux, lavapipe)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5405692808 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24008). Originally posted by [chitters2004](https://github.com/chitters2004) on 2026-11-20T00:51:55Z. --- ### What Two ordering bugs in the Linux write watch (`core/libs/prx/libc/src/GuestWriteWatch.cpp`, userfaultfd write protection), one commit each. `takeFresh` reported a never-armed range as written and only then write-protected it. A guest store between the report and the protect went through unfaulted and the protect then hid it, so the next collect did not list the page and a consumer that had already re-read the range kept stale data. It now protects every fresh piece first and reports afterwards: a store before the protect is covered by the fresh report, a store after it sets the written state the next scan lists. The protect-failure path is unchanged (marks put back, range reported). A collect resolved its alias plan before `takeFresh` and the scan, and fanned every report through that snapshot. An `Alias` that linked the views in between left it stale: the scan cleared the written state of a store made through one view after the link and reported it to that view only, so the partner view never heard of it. Link changes are now counted (`_linkVersion`), and each `Fanout` compares its version at report time, which is after the scan that consumed the pages, and re-resolves its translations under the shared lock when they differ. Until then it points at the plan's translations, so a collect that sees no link change allocates and copies nothing (one acquire load per report). The protect-failure report in `takeFresh` moved out of the lock, since a report can now take it. Tests in `core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp` (`agc_write_tracking`, Linux only): `CheckWriteAfterFreshReport` stores from inside the fresh-range report callback and requires the store to be listed; `CheckLinkDuringCollect` links two views from inside the first collect's report callback, stores through the first view and requires the same collect to report the store at the second. Not covered, by design: a store made before an `Alias` call is not fanned out by this change (that is #23976, `Watch::Alias` marking both views fresh). ### Tested - Fork CI on this head (`895760f025`, Linux gate on lavapipe and TSan), `agc_write_tracking` and neighbours: 3 passed, same as main; the same 3 pass under TSan with no report: https://github.com/chitters2004/AnyPS5/actions/runs/44647093464 - Negative controls on main `8e158a83d9`, the new tests without the fixes: both tests in `draft/ci-ww-control`, fails with "a store made after a fresh range was reported was never reported": https://github.com/chitters2004/AnyPS5/actions/runs/44644271669. With only the first commit's fix plus both tests, fails with "a store through a view linked during a collect was not reported at its alias": https://github.com/chitters2004/AnyPS5/actions/runs/44645409577 - Merges cleanly with #23976 (`Watch::Alias` fresh marking); conflicts textually with #12792 in `Collect` and `Fanout` (it adds an `unreached` argument to the same calls), mechanical to resolve. - Windows and macOS use other watch implementations and are unaffected. Not run on a physical GPU. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main` (8e158a83d9); 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) and the licence notice of code adapted from upstream (see AGENTS.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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a (write watch is CPU and kernel only; tests ran on fork CI Linux, lavapipe) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-20 06:15:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T06:15:01Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the Linux write-watch ordering fixes and their regression tests look sound based on the source reviewed.

What I ran

Reviewed head 895760f025bf4ea3737d2f53cb8d2f9be32cc4b4 and inspected the prepared diff and targeted source ranges. No commands or tests were run by me; CI results in the description are author-reported.

Results

Check Result Note
Diff and changed code passed Both changed files inspected
Tests not run Linux CI results are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run locally. The full repository files were not read; the review covered the prepared diff and targeted ranges from GuestWriteWatch.cpp and WriteTracking.cpp. Physical GPU behavior was not verified. The description reports Linux lavapipe and TSan CI results, which I did not independently confirm.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5988337651 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24008#pullrequestreview-5988337651). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T06:15:01Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the Linux write-watch ordering fixes and their regression tests look sound based on the source reviewed. ## What I ran Reviewed head `895760f025bf4ea3737d2f53cb8d2f9be32cc4b4` and inspected the prepared diff and targeted source ranges. No commands or tests were run by me; CI results in the description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Both changed files inspected | | Tests | not run | Linux CI results are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests were run locally. The full repository files were not read; the review covered the prepared diff and targeted ranges from `GuestWriteWatch.cpp` and `WriteTracking.cpp`. Physical GPU behavior was not verified. The description reports Linux lavapipe and TSan CI results, which I did not independently confirm. ## To get this to a good state Nothing is required.
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!24008
No description provided.