fix(libSceAgcDriver): keep CPU flips in submission order #17279

Closed
amazedturnip wants to merge 1 commit from upstream-pr/17279 into main
amazedturnip commented 2026-11-08 17:10:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by amazedturnip on 2026-11-08T17:10:12Z.


What

A CPU flip that arrives while the queue worker is handing an earlier flip to its video output could be handed over first, so flips on one port reached VideoOut out of order. That is the intermittent back-to-back flip status is wrong failure of video_out_flip_backtoback (#16956): the last completion carried the first flip's flipArg and currentBuffer.

The race:

  • A flip submitted before any flip is in flight goes through the queue worker and raises flipsAhead (Submission.cpp).
  • When the worker reaches the flip packet it called flipHold.Release(), which drops flipsAhead, and only then GpuReady on the request.
  • A later flip from a thread that never submitted queue-0 work checks flipsAhead == 0 under the driver mutex and hands its own request over directly. If the worker was preempted between Release() and GpuReady, the later flip's request was queued first.

The fix adds a flipsLanding count. Release(true) raises it in the same locked step that drops flipsAhead, and it drops again once GpuReady has returned (also on the exception path through the FlipHold destructor). The direct hand-over now also requires flipsLanding == 0; otherwise the flip is queued behind the worker as before. Calling GpuReady before Release() instead would keep flipsAhead raised while GpuReady blocks, which holds CPU flips behind a gated flip they are meant to overtake. No other ordering changes.

agc_driver_cpu_flip_order forces the interleaving without timing: its output parks the first flip inside GpuReady, then submits a second CPU flip and releases the first. Both must reach the output as 1, 2.

Tested

  • Linux (WSL2, Release, Ninja), on aeaaf7367d. upstream/main has since moved to e0af82d615; the files it changed are in libSceLibreSsl, libSceNpCommon, libSceNpManager, libSceWebBrowserDialog, core/libs/tests and docs/dev/debt, none of which the driver or these tests use.
  • ctest -R '^agc_driver_cpu_flip_order$':
    • with this change: 20 of 20 pass
    • with the flipsLanding == 0 condition removed from the direct hand-over: 10 of 10 fail with a later CPU flip reached its output before an earlier one
  • Regression suites: tools/select_tests.py selection for the five changed files (2550 of 4549 tests; 114 skipped for lack of a Vulkan device, none failed) and ctest -R 'video_out|agc' (1484 tests), 3 runs each, all passed.
  • video_out_flip_* skip in this WSL environment (no Vulkan in the offscreen SDL driver), so video_out_flip_backtoback itself was not run; the new test reproduces its ordering failure at the driver.
  • tools/pr_overlap.py fails closed here: 17 open PRs that share the touched files conflict with main, so it cannot compare them. I compared their diffs by hand: none changes flipsAhead, FlipHold, releaseFlipHold or the flip hand-over (#13863, #13920 and #15136 only add lines next to them, #7642 changes none of the three driver files' flip code).
  • Windows: not run.

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
  • Fixes: #16956
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5213333743 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17279). Originally posted by [amazedturnip](https://github.com/amazedturnip) on 2026-11-08T17:10:12Z. --- ### What A CPU flip that arrives while the queue worker is handing an earlier flip to its video output could be handed over first, so flips on one port reached VideoOut out of order. That is the intermittent `back-to-back flip status is wrong` failure of `video_out_flip_backtoback` (#16956): the last completion carried the first flip's `flipArg` and `currentBuffer`. The race: - A flip submitted before any flip is in flight goes through the queue worker and raises `flipsAhead` (`Submission.cpp`). - When the worker reaches the flip packet it called `flipHold.Release()`, which drops `flipsAhead`, and only then `GpuReady` on the request. - A later flip from a thread that never submitted queue-0 work checks `flipsAhead == 0` under the driver mutex and hands its own request over directly. If the worker was preempted between `Release()` and `GpuReady`, the later flip's request was queued first. The fix adds a `flipsLanding` count. `Release(true)` raises it in the same locked step that drops `flipsAhead`, and it drops again once `GpuReady` has returned (also on the exception path through the `FlipHold` destructor). The direct hand-over now also requires `flipsLanding == 0`; otherwise the flip is queued behind the worker as before. Calling `GpuReady` before `Release()` instead would keep `flipsAhead` raised while `GpuReady` blocks, which holds CPU flips behind a gated flip they are meant to overtake. No other ordering changes. `agc_driver_cpu_flip_order` forces the interleaving without timing: its output parks the first flip inside `GpuReady`, then submits a second CPU flip and releases the first. Both must reach the output as 1, 2. ### Tested - Linux (WSL2, Release, Ninja), on aeaaf7367d. `upstream/main` has since moved to e0af82d615; the files it changed are in libSceLibreSsl, libSceNpCommon, libSceNpManager, libSceWebBrowserDialog, `core/libs/tests` and `docs/dev/debt`, none of which the driver or these tests use. - `ctest -R '^agc_driver_cpu_flip_order$'`: - with this change: 20 of 20 pass - with the `flipsLanding == 0` condition removed from the direct hand-over: 10 of 10 fail with `a later CPU flip reached its output before an earlier one` - Regression suites: `tools/select_tests.py` selection for the five changed files (2550 of 4549 tests; 114 skipped for lack of a Vulkan device, none failed) and `ctest -R 'video_out|agc'` (1484 tests), 3 runs each, all passed. - `video_out_flip_*` skip in this WSL environment (no Vulkan in the offscreen SDL driver), so `video_out_flip_backtoback` itself was not run; the new test reproduces its ordering failure at the driver. - `tools/pr_overlap.py` fails closed here: 17 open PRs that share the touched files conflict with `main`, so it cannot compare them. I compared their diffs by hand: none changes `flipsAhead`, `FlipHold`, `releaseFlipHold` or the flip hand-over (#13863, #13920 and #15136 only add lines next to them, #7642 changes none of the three driver files' flip code). - Windows: not run. ### 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] Fixes: #16956 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-11-12 19:41:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-11-12T19:41:49Z.

Review: CHANGES_REQUESTED


The root cause and the new agc_driver_cpu_flip_order look right, but this change regresses an existing test.

  • flipsLanding is a single driver-wide counter held for the whole of GpuReady (PacketExecution.cpp:472-474), and the shortcut in Submit (Submission.cpp:646) now requires it to be 0 for every port. video_out_flip_cpuflip (libSceVideoOut/tests/Flip.cpp:229-258) parks a gate inside GpuReady on port 7 and requires a CPU flip on a different port to complete meanwhile. With this head that flip waits behind the gate and the test fails after 8 s (a flip from a thread that never submitted queue-0 work waited for a held queue-0 submission). @NathanFahey23 measured it on Windows RTX 4080: 0 of 15 pass, vs 15 of 15 on main; video_out_flip_backtoback goes from 11/15 to 15/15.
  • Fix: track the landing per video-out handle (a count or set keyed by the flip's handle) and make only a CPU flip on the same port take the ordered path. Keep agc_driver_cpu_flip_order and also run video_out_flip_cpuflip and video_out_flip_backtoback. These need a display/Vulkan device, so they skip on the Linux CI. Please post the result from a machine that runs them.
  • Related: #18269 takes the per-port approach and passes both tests; coordinate with it so only one lands.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5880931925 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17279#pullrequestreview-5880931925). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-11-12T19:41:49Z. **Review:** CHANGES_REQUESTED --- The root cause and the new `agc_driver_cpu_flip_order` look right, but this change regresses an existing test. - `flipsLanding` is a single driver-wide counter held for the whole of `GpuReady` (`PacketExecution.cpp:472-474`), and the shortcut in `Submit` (`Submission.cpp:646`) now requires it to be 0 for every port. `video_out_flip_cpuflip` (`libSceVideoOut/tests/Flip.cpp:229-258`) parks a gate inside `GpuReady` on port 7 and requires a CPU flip on a different port to complete meanwhile. With this head that flip waits behind the gate and the test fails after 8 s (`a flip from a thread that never submitted queue-0 work waited for a held queue-0 submission`). @NathanFahey23 measured it on Windows RTX 4080: 0 of 15 pass, vs 15 of 15 on `main`; `video_out_flip_backtoback` goes from 11/15 to 15/15. - Fix: track the landing per video-out handle (a count or set keyed by the flip's handle) and make only a CPU flip on the same port take the ordered path. Keep `agc_driver_cpu_flip_order` and also run `video_out_flip_cpuflip` and `video_out_flip_backtoback`. These need a display/Vulkan device, so they skip on the Linux CI. Please post the result from a machine that runs them. - Related: #18269 takes the per-port approach and passes both tests; coordinate with it so only one lands.

Pull request closed

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!17279
No description provided.