agc: a host read on another thread does not wait for an image the queue-idle store is writing back #7744

Closed
opened 2026-10-19 05:42:38 +00:00 by jotitadev · 0 comments
jotitadev commented 2026-10-19 05:42:38 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T05:42:38Z.


StorageTexture::FlushPending takes the overlapping images out of pending.textures into pending.flushing, then writes them back under the GPU mutex. A second FlushPending for the same range on another thread (the flush hook of GuestMemory::Read, a file write since #6234) scans only pending.textures. It finds nothing and returns false without waiting, so the caller reads guest memory before the first write-back lands.

The queue-idle store (VulkanDevice::StoreIdleResults: GPU mutex, then FlushAllPending("idle")) is the usual other flusher. Any host read that overlaps it can see the old bytes.

Repro (a temporary variant of StorageFileWrite.cpp, patch below): produce the pending surface, start a thread that takes the GPU mutex and calls FlushAllPending, wait until FindPending on the surface returns null, then GuestMemory::Read the surface.

  • lavapipe (Mesa 26.0.3): the read sees all zeros in 190 of 200 iterations.
  • RTX 3070 (Linux 595.99.02): 191 of 200.
  • After the thread joins, the bytes are correct in 200 of 200.

agc_driver_storage_file_write on main shows the same thing under load as the GPU producer wrote only zeros (3 of 100 runs on lavapipe and 5 of 50 on the RTX 3070 with SIGSTOP 70 ms every 20 ms).

Possible fix: in FlushPending, when nothing is found in textures but an overlapping image is in flushing and this thread does not hold the GPU mutex, take the GPU mutex and check again until no overlapping image is left in flushing. flushing is empty almost always, so the hot path only pays one more scan of an empty list. When this thread already holds the mutex, no other thread can be in the middle of a write-back.

Repro patch (test-only, not for merging)
diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp
index 3edb8d363..1f954d0af 100644
--- a/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp
+++ b/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp
@@ -20,6 +20,9 @@
 #include <string>
 #include <stdexcept>
 #include <vector>
+#include <thread>
+#include <atomic>
+#include <cstdlib>
 #ifdef _WIN32
 #ifndef NOMINMAX
 #define NOMINMAX
@@ -267,6 +270,31 @@ int main() {
         GuestSurface surface;
         GuestSurface unrelated;
         Produce(unrelated.Data());
+        if (std::getenv("RACE")) {
+            int zeros = 0, missed = 0, n = std::atoi(std::getenv("RACE"));
+            for (int i = 0; i < n; ++i) {
+                Produce(surface.Data());
+                std::atomic<bool> go{false};
+                std::thread idle([&] {
+                    go = true;
+                    std::lock_guard gpu(AgcDriver::GuestMemory::GpuMutex());
+                    AgcDriver::Graphics::StorageTexture::FlushAllPending("race idle");
+                });
+                while (!go) {}
+                std::vector<std::byte> seen(SurfaceBytes);
+                // spin until the idle thread took the image out of the pending list
+                while (AgcDriver::Graphics::StorageTexture::FindPending(reinterpret_cast<std::uintptr_t>(surface.Data()), SurfaceBytes) != nullptr) {}
+                AgcDriver::GuestMemory::Read(reinterpret_cast<std::uintptr_t>(surface.Data()), seen);
+                if (std::none_of(seen.begin(), seen.end(), [](std::byte v) { return v != std::byte{}; })) ++zeros;
+                idle.join();
+                std::vector<std::byte> after(SurfaceBytes);
+                AgcDriver::GuestMemory::Read(reinterpret_cast<std::uintptr_t>(surface.Data()), after);
+                if (std::none_of(after.begin(), after.end(), [](std::byte v) { return v != std::byte{}; })) ++missed;
+            }
+            std::printf("race: host read saw zeros %d/%d; zeros after idle store finished %d/%d\n", zeros, n, missed, n);
+            AgcDriverShutdown_nid_postfix();
+            return 0;
+        }
         bool correct = true;
         const std::array<std::pair<Api, const char*>, 12> cases{{
             {Api::Write, "sceKernelWrite"}, {Api::PosixWrite, "write"}, {Api::CrtWrite, "_write"},

Machine: Intel i7-8700, Ubuntu 26.04, main 3904ce6cf.
GPUs tested: NVIDIA RTX 3070 (Linux), lavapipe.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5918517906 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7744). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-19T05:42:38Z. --- `StorageTexture::FlushPending` takes the overlapping images out of `pending.textures` into `pending.flushing`, then writes them back under the GPU mutex. A second `FlushPending` for the same range on another thread (the flush hook of `GuestMemory::Read`, a file write since #6234) scans only `pending.textures`. It finds nothing and returns false without waiting, so the caller reads guest memory before the first write-back lands. The queue-idle store (`VulkanDevice::StoreIdleResults`: GPU mutex, then `FlushAllPending("idle")`) is the usual other flusher. Any host read that overlaps it can see the old bytes. Repro (a temporary variant of `StorageFileWrite.cpp`, patch below): produce the pending surface, start a thread that takes the GPU mutex and calls `FlushAllPending`, wait until `FindPending` on the surface returns null, then `GuestMemory::Read` the surface. - lavapipe (Mesa 26.0.3): the read sees all zeros in 190 of 200 iterations. - RTX 3070 (Linux 595.99.02): 191 of 200. - After the thread joins, the bytes are correct in 200 of 200. `agc_driver_storage_file_write` on `main` shows the same thing under load as `the GPU producer wrote only zeros` (3 of 100 runs on lavapipe and 5 of 50 on the RTX 3070 with `SIGSTOP` 70 ms every 20 ms). Possible fix: in `FlushPending`, when nothing is found in `textures` but an overlapping image is in `flushing` and this thread does not hold the GPU mutex, take the GPU mutex and check again until no overlapping image is left in `flushing`. `flushing` is empty almost always, so the hot path only pays one more scan of an empty list. When this thread already holds the mutex, no other thread can be in the middle of a write-back. <details><summary>Repro patch (test-only, not for merging)</summary> ```diff diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp index 3edb8d363..1f954d0af 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp @@ -20,6 +20,9 @@ #include <string> #include <stdexcept> #include <vector> +#include <thread> +#include <atomic> +#include <cstdlib> #ifdef _WIN32 #ifndef NOMINMAX #define NOMINMAX @@ -267,6 +270,31 @@ int main() { GuestSurface surface; GuestSurface unrelated; Produce(unrelated.Data()); + if (std::getenv("RACE")) { + int zeros = 0, missed = 0, n = std::atoi(std::getenv("RACE")); + for (int i = 0; i < n; ++i) { + Produce(surface.Data()); + std::atomic<bool> go{false}; + std::thread idle([&] { + go = true; + std::lock_guard gpu(AgcDriver::GuestMemory::GpuMutex()); + AgcDriver::Graphics::StorageTexture::FlushAllPending("race idle"); + }); + while (!go) {} + std::vector<std::byte> seen(SurfaceBytes); + // spin until the idle thread took the image out of the pending list + while (AgcDriver::Graphics::StorageTexture::FindPending(reinterpret_cast<std::uintptr_t>(surface.Data()), SurfaceBytes) != nullptr) {} + AgcDriver::GuestMemory::Read(reinterpret_cast<std::uintptr_t>(surface.Data()), seen); + if (std::none_of(seen.begin(), seen.end(), [](std::byte v) { return v != std::byte{}; })) ++zeros; + idle.join(); + std::vector<std::byte> after(SurfaceBytes); + AgcDriver::GuestMemory::Read(reinterpret_cast<std::uintptr_t>(surface.Data()), after); + if (std::none_of(after.begin(), after.end(), [](std::byte v) { return v != std::byte{}; })) ++missed; + } + std::printf("race: host read saw zeros %d/%d; zeros after idle store finished %d/%d\n", zeros, n, missed, n); + AgcDriverShutdown_nid_postfix(); + return 0; + } bool correct = true; const std::array<std::pair<Api, const char*>, 12> cases{{ {Api::Write, "sceKernelWrite"}, {Api::PosixWrite, "write"}, {Api::CrtWrite, "_write"}, ``` </details> Machine: Intel i7-8700, Ubuntu 26.04, main 3904ce6cf. GPUs tested: NVIDIA RTX 3070 (Linux), lavapipe. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Sign in to join this conversation.
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#7744
No description provided.