fix(agc): publish a host write's whole units first so a unit shadow cannot land over it #17787

Merged
Mouhand-Kaddo merged 1 commit from upstream-pr/17787 into main 2026-11-14 17:35:49 +00:00
Mouhand-Kaddo commented 2026-11-09 16:00:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-11-09T16:00:42Z.


What

Since #6926, Demon's Souls (PPSA01342) crashes on Linux most times it loads into the world. It segfaults in its own code while loading (DemonsSouls.elf+0x1788ede). Reverting #6926 or setting APS5_NO_UNIT_SHADOW=1 stops the crash.

Why. A file read can lose its bytes to a unit shadow's GPU results.

  • PrepareHostWrite flushes with ReadSite::Store, so FlushForAccess uses PublishScope::PartialUnits. The units the host write covers wholly stay fresh in their slab. Per UnitShadow.hpp, that is right only if the writer then stamps them, and a host write does not stamp: the tracker only learns about it when a collect walks the written pages.
  • While the read is still copying, another thread can publish those units. In Demon's Souls this is the address-based heaps publish in GuestBufferMemory::UploadFinish. Its collect runs before the pages are written, so it judges the units fresh and its slab → import copy lands over the file bytes.
  • #6926 shadows the results over the range right before every host write into image memory, and Demon's Souls reuses image memory as file read buffers.

The fix, as proposed by @JJenkx (co-author):

  1. PrepareHostWrite flushes under its own ReadSite::HostWrite, so FlushForAccess publishes every unit over the range (PublishScope::Whole) before the write. Nothing is left fresh for a later publish to copy over the file bytes, and a short read keeps the GPU's results in the bytes it does not write. Driver stores (ReadSite::Store) keep PartialUnits. WritesGuestMemory(site) keeps host writes counted as stores in the hook sync profile.
  2. A publish on another thread can be caught midway: publishUnits marks its units published before it notes its copy as a pending write, so the host write finds nothing to publish and nothing pending yet. publishUnits now counts itself in progress for its whole body (AnyPublishInProgress), and a host write that sees one in progress takes the GPU mutex and waits for pending writes over its range. With none in progress it stays lock-free.

Tested

Linux 7.2.15, RX 6900 XT (RADV, Mesa 26.2.7), GCC, Release.

  • New test agc_driver_host_write_shadowed_unit. It shadows units of memfd-backed (dma-buf) guest memory and opens a HostWrite over them.

    • A publish while the host write is open, and one between its flush and its write: the host write's bytes survive. Both fail on main.
    • A host write started on another thread from inside a publish, after the publish marks the unit and before it notes its copy: the host write's bytes survive. It fails 5 of 5 runs without the in-progress check and passes 5 of 5 with it.
    • Short writes of 0, 1, 4096, 65535 and 65536 of 64 KiB, two-unit and unaligned ranges: the written bytes and the GPU's results past them survive.

    It skips without /dev/udmabuf or unit shadows.

  • agc_driver_host_write_shadowed_unit, agc_driver_host_write_pending_storage, agc_driver_host_write_hook and agc_driver_gcn_display_present_trigger pass, with and without APS5_PROFILE_DRAW=1.

  • Validation layers (VK_LAYER_KHRONOS_validation 1.4.363): ctest --test-dir build -R 'agc_driver_host_write|agc_driver_gcn_display_present_trigger' -V with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, on this PR and on main 1414f2c640, with and without APS5_PROFILE_DRAW=1. Every log shows Insert instance layer "VK_LAYER_KHRONOS_validation" for each test instance and none has the layer-not-found message. Neither tree logs a VUID- line. Rerun on this head after each rebase onto main: same result.

  • Demon's Souls PPSA01342 v1.004, output 1920x1080, warm shader caches. Each run presses through the menus and keeps running 45 s after loading starts, past the point where main crashes. Runs on main 1414f2c640, before that rebase, interleaved one by one in one session, with nothing else running:

    tree runs crashed
    main 3 3, all while loading (+0x1788ede)
    this PR 8 1, in the menus: a call through a null pointer
    the earlier stamp head, rebased 5 0

    Earlier runs on main faea44f22c: the Whole fix without the in-progress check crashed 2 of 20 times, both in the first 10 while another build was running on the machine (+0x10da241 in the menus, through the same game call path as the null call above, and null or misaligned index base after loading). The stamp head survived 20 of 20.

  • Host write cost: agc_driver_host_write_pending_storage_tests --cost 20000, pinned to one core, 5 runs: 73 to 90 ns per 64 KiB host write, the same as the 72 ns measured on main before. A host write takes the GPU mutex only while a publish is in progress anywhere, so it can wait out another thread's publish then; that cost was not measured.

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: none
  • Unblocks: PPSA01342 #1283
  • GPUs tested: AMD Radeon RX 6900 XT, RADV (Mesa 26.2.7), Arch Linux (kernel 7.2.15)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5226886719 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17787). Originally posted by [Mouhand-Kaddo](https://github.com/Mouhand-Kaddo) on 2026-11-09T16:00:42Z. --- ### What Since #6926, Demon's Souls (PPSA01342) crashes on Linux most times it loads into the world. It segfaults in its own code while loading (`DemonsSouls.elf+0x1788ede`). Reverting #6926 or setting `APS5_NO_UNIT_SHADOW=1` stops the crash. **Why.** A file read can lose its bytes to a unit shadow's GPU results. - `PrepareHostWrite` flushes with `ReadSite::Store`, so `FlushForAccess` uses `PublishScope::PartialUnits`. The units the host write covers wholly stay fresh in their slab. Per `UnitShadow.hpp`, that is right only if the writer then stamps them, and a host write does not stamp: the tracker only learns about it when a collect walks the written pages. - While the read is still copying, another thread can publish those units. In Demon's Souls this is the `address-based heaps` publish in `GuestBufferMemory::UploadFinish`. Its collect runs before the pages are written, so it judges the units fresh and its slab → import copy lands over the file bytes. - #6926 shadows the results over the range right before every host write into image memory, and Demon's Souls reuses image memory as file read buffers. **The fix**, as proposed by @JJenkx (co-author): 1. `PrepareHostWrite` flushes under its own `ReadSite::HostWrite`, so `FlushForAccess` publishes every unit over the range (`PublishScope::Whole`) before the write. Nothing is left fresh for a later publish to copy over the file bytes, and a short read keeps the GPU's results in the bytes it does not write. Driver stores (`ReadSite::Store`) keep `PartialUnits`. `WritesGuestMemory(site)` keeps host writes counted as stores in the hook sync profile. 2. A publish on another thread can be caught midway: `publishUnits` marks its units published before it notes its copy as a pending write, so the host write finds nothing to publish and nothing pending yet. `publishUnits` now counts itself in progress for its whole body (`AnyPublishInProgress`), and a host write that sees one in progress takes the GPU mutex and waits for pending writes over its range. With none in progress it stays lock-free. ### Tested Linux 7.2.15, RX 6900 XT (RADV, Mesa 26.2.7), GCC, Release. - **New test `agc_driver_host_write_shadowed_unit`.** It shadows units of memfd-backed (dma-buf) guest memory and opens a `HostWrite` over them. - A publish while the host write is open, and one between its flush and its write: the host write's bytes survive. Both fail on `main`. - A host write started on another thread from inside a publish, after the publish marks the unit and before it notes its copy: the host write's bytes survive. It fails 5 of 5 runs without the in-progress check and passes 5 of 5 with it. - Short writes of 0, 1, 4096, 65535 and 65536 of 64 KiB, two-unit and unaligned ranges: the written bytes and the GPU's results past them survive. It skips without `/dev/udmabuf` or unit shadows. - `agc_driver_host_write_shadowed_unit`, `agc_driver_host_write_pending_storage`, `agc_driver_host_write_hook` and `agc_driver_gcn_display_present_trigger` pass, with and without `APS5_PROFILE_DRAW=1`. - **Validation layers** (`VK_LAYER_KHRONOS_validation` 1.4.363): `ctest --test-dir build -R 'agc_driver_host_write|agc_driver_gcn_display_present_trigger' -V` with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`, on this PR and on `main` 1414f2c640, with and without `APS5_PROFILE_DRAW=1`. Every log shows `Insert instance layer "VK_LAYER_KHRONOS_validation"` for each test instance and none has the layer-not-found message. Neither tree logs a `VUID-` line. Rerun on this head after each rebase onto `main`: same result. - **Demon's Souls PPSA01342 v1.004**, output 1920x1080, warm shader caches. Each run presses through the menus and keeps running 45 s after loading starts, past the point where `main` crashes. Runs on `main` 1414f2c640, before that rebase, interleaved one by one in one session, with nothing else running: | tree | runs | crashed | |---|---|---| | `main` | 3 | 3, all while loading (`+0x1788ede`) | | this PR | 8 | 1, in the menus: a call through a null pointer | | the earlier stamp head, rebased | 5 | 0 | Earlier runs on `main` faea44f22c: the Whole fix without the in-progress check crashed 2 of 20 times, both in the first 10 while another build was running on the machine (`+0x10da241` in the menus, through the same game call path as the null call above, and `null or misaligned index base` after loading). The stamp head survived 20 of 20. - **Host write cost:** `agc_driver_host_write_pending_storage_tests --cost 20000`, pinned to one core, 5 runs: 73 to 90 ns per 64 KiB host write, the same as the 72 ns measured on `main` before. A host write takes the GPU mutex only while a publish is in progress anywhere, so it can wait out another thread's publish then; that cost was not measured. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: none - [x] Unblocks: PPSA01342 #1283 - [x] GPUs tested: AMD Radeon RX 6900 XT, RADV (Mesa 26.2.7), Arch Linux (kernel 7.2.15) - [x] AI-assisted: yes
Superseabas commented 2026-11-14 14:35:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-14T14:35:52Z.

Review: APPROVED


Windows check of head 2d2c4ec1ad on two GPUs, merged onto main 3ffcba66e0. The bug reproduces on Windows, and this PR fixes it there, using a Windows port of the new test (below). It may also be what @moong15 sees in Stellar Blade on Windows.

Setup: Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release, run from PowerShell with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and the Khronos validation layer. GPUs: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (Adrenalin 26.10.2). main doesn't build on Windows right now (#18037's setenv in UploadCacheReadOnlyFlush.cpp), so both sides carry #19899 only so they build.

No regressions: ctest -R '^agc|gpu|vulkan|video_out' on main and with this PR:

main this PR
RTX 5070 Ti 0 failed of 1828 0 failed of 1829
Radeon 1 failed of 1828 1 failed of 1829

The one Radeon failure is agc_driver_sample_order_grid_reference on both sides (fixed by #9503). The extra test is agc_driver_host_write_shadowed_unit, which skips here. No other test changes its result, and there are no validation messages on either GPU, on either side.

The new test on Windows. As it is, it skips on Windows ("udmabuf imports are Linux only"). Only its setup is Linux-specific. On Windows, a 1 MiB guest-arena block imported through a host pointer stays write-watched: the driver's import probe reports "host imports of write-watched memory keep the write watch: yes … direct host imports of private memory are settled and stay watched" on both GPUs. So I ported the setup, leaving every check as it is (patch below), and ran it 3 times per GPU:

RTX 5070 Ti Radeon
main's driver code (the PR's 5 production files reverted) fails 3 of 3 fails 3 of 3
this PR passes 3 of 3 passes 3 of 3

On main, both GPUs fail the same 4 cases. In the two "host write over a shadowed unit" cases and "host write during another thread's publish of its unit", byte 0 holds the shadow's GPU results instead of the host write's. "short host write of 65553 of 131072 bytes at +0" fails with "byte 65553 holds 51, not 190". The other 8 short-write cases pass on main too. With this PR every case passes, with no validation messages.

The Windows port of HostWriteShadowedUnit.cpp (built and run on both GPUs; macOS keeps a stub)
--- a/core/libs/prx/libSceAgcDriver/tests/execution/HostWriteShadowedUnit.cpp
+++ b/core/libs/prx/libSceAgcDriver/tests/execution/HostWriteShadowedUnit.cpp
@@ -8,6 +8,9 @@
 #include "VulkanTestDevice.hpp"
 #include "prx/libc/include/GuestArena.hpp"
 #include "prx/libc/include/GuestWriteWatch.hpp"
+#ifdef _WIN32
+#include <windows.h>
+#endif
 #ifdef __linux__
 #include <fcntl.h>
 #include <sys/mman.h>
@@ -25,9 +28,9 @@
 #include <thread>
 #include <utility>
 
-#ifndef __linux__
+#if !defined(__linux__) && !defined(_WIN32)
 int main() {
-    std::printf("skipped, udmabuf imports are Linux only\n");
+    std::printf("skipped, host imports of write-watched memory are tested on Linux and Windows only\n");
     return VulkanTestSkipped;
 }
 #else
@@ -52,6 +55,7 @@
 std::uint8_t* mapping = nullptr;
 int backing = -1;
 
+#ifdef __linux__
 bool Resolve(std::uintptr_t address, std::size_t bytes, int* file, std::uint64_t* offset) {
     const auto begin = reinterpret_cast<std::uintptr_t>(mapping);
     if (address < begin || address + bytes > begin + MappingBytes) return false;
@@ -59,6 +63,7 @@
     *offset = address - begin;
     return *file >= 0;
 }
+#endif
 
 int Skip(const char* why) {
     std::printf("skipped, %s\n", why);
@@ -213,6 +218,15 @@
 int main() {
     using namespace AgcDriver::Graphics;
     try {
+#ifdef _WIN32
+        auto device = OpenVulkanTestDevice();
+        if (!device) return VulkanTestSkipped;
+        if (!UnitShadowEnabled()) return Skip("unit shadows are off");
+        mapping = static_cast<std::uint8_t*>(GuestArena::GuestArenaAllocate_nid_postfix(MappingBytes, 65536));
+        Require(mapping != nullptr, "host write over a shadowed unit: cannot allocate the arena block");
+        GuestArena::GuestArenaCommit_nid_postfix(mapping, MappingBytes, PAGE_READWRITE, MappingBytes);
+        std::memset(mapping, AroundByte, MappingBytes);
+#else
         const int udmabuf = open("/dev/udmabuf", O_RDWR | O_CLOEXEC);
         if (udmabuf < 0) return Skip("host imports are unavailable: /dev/udmabuf is not accessible");
         close(udmabuf);
@@ -228,6 +242,7 @@
         mapping = static_cast<std::uint8_t*>(mapped);
         GuestArena::GuestArenaSetSharedBacking_nid_postfix(Resolve);
         std::memset(mapping, AroundByte, MappingBytes);
+#endif
 
         const auto address = reinterpret_cast<std::uint64_t>(mapping);
         GuestAllocations::Mutation().Add(mapping, MappingBytes, true, true, true);
@@ -237,7 +252,9 @@
         }
         Require(ranges.size() == 1, "host write over a shadowed unit: the allocation is not registered");
         Require(device->ImportGuestMemory(ranges, GuestAllocations::GuestAllocationsGeneration_nid_postfix(), false), "host write over a shadowed unit: the import made without the GPU lock was refused");
+#ifndef _WIN32
         GuestWriteWatch::GuestWriteWatchRegister_nid_postfix(mapping, MappingBytes);
+#endif
 
         const auto firstUnit = (address + Unit - 1) & ~(Unit - 1);
         const auto context = AgcDriver::VulkanDeviceTestAccess::ContextOf(*device);
@@ -246,7 +263,11 @@
             std::lock_guard lock(AgcDriver::GuestMemory::GpuMutex());
             import = HostImportFor(context, address, MappingBytes);
         }
+#ifdef _WIN32
+        if (import == nullptr) return Skip("no host import of the arena block");
+#else
         if (import == nullptr || !import->dmaBuf) return Skip("the device imported the mapping through a host pointer, which is compared, not watched");
+#endif
         if (!AgcDriver::GuestMemory::Watched(firstUnit, 8 * Unit)) return Skip("the imported units are not write-watched");
 
         landHook = AgcDriver::GuestMemory::CurrentFlushHook();
@@ -266,7 +287,9 @@
         AgcDriver::GuestMemory::SetFlushHook(landHook);
         Require(passed, "host write over a shadowed unit: a publish put a unit shadow's results over a host write, or a short host write lost the GPU's results past it");
 
+#ifndef _WIN32
         GuestWriteWatch::GuestWriteWatchUnregister_nid_postfix(mapping, MappingBytes);
+#endif
         GuestAllocations::Mutation().Remove(mapping);
         std::cout << "host write shadowed unit tests passed\n";
         return 0;

Windows arena memory is write-watched without GuestWriteWatchRegister, as in SampledCacheBudget.cpp's WatchedSurface. 1 MiB is also Windows' floor (settleFloorBytes) for keeping a settled import watched. The main failures and the PR passes in the tables above are from this patch plus one diagnostic print. The patch as shown was then built and passed once more on each GPU. I haven't built it on Linux or macOS.

I'd suggest adding the Windows setup, so the regression test also covers the platform where Stellar Blade crashes. That's optional for this PR.

Reading the change: tagging host writes ReadSite::HostWrite instead of Store is what makes FlushForAccess choose PublishScope::Whole for them. WritesGuestMemory keeps the hook-sync profiling treating them as stores. The AnyPublishInProgress wait covers the case where another thread's publish is already under way. One small, non-blocking point: PublishShadow's skip inside a completion (UnitShadow.cpp, Recorder::InCompletion() && CurrentReadSite() == ReadSite::Store) still names only Store. I found no path where a host write runs inside a completion: the driver's own GuestArena::HostWrite is under OwnHostWrite, so PrepareHostWrite returns early for it. But WritesGuestMemory there would keep the two sites alike.

Not run: Linux, macOS, and a title (I don't have Demon's Souls or Stellar Blade).

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5904977400 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17787#pullrequestreview-5904977400). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-14T14:35:52Z. **Review:** APPROVED --- Windows check of head `2d2c4ec1ad` on two GPUs, merged onto `main` `3ffcba66e0`. **The bug reproduces on Windows, and this PR fixes it there**, using a Windows port of the new test (below). It may also be what @moong15 sees in Stellar Blade on Windows. **Setup:** Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (`x86_64-ucrt-posix-seh`, r3), Release, run from PowerShell with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and the Khronos validation layer. GPUs: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (Adrenalin 26.10.2). `main` doesn't build on Windows right now (#18037's `setenv` in `UploadCacheReadOnlyFlush.cpp`), so both sides carry #19899 only so they build. **No regressions:** `ctest -R '^agc|gpu|vulkan|video_out'` on `main` and with this PR: | | `main` | this PR | |---|---|---| | RTX 5070 Ti | 0 failed of 1828 | 0 failed of 1829 | | Radeon | 1 failed of 1828 | 1 failed of 1829 | The one Radeon failure is `agc_driver_sample_order_grid_reference` on both sides (fixed by #9503). The extra test is `agc_driver_host_write_shadowed_unit`, which skips here. No other test changes its result, and there are no validation messages on either GPU, on either side. **The new test on Windows.** As it is, it skips on Windows ("udmabuf imports are Linux only"). Only its setup is Linux-specific. On Windows, a 1 MiB guest-arena block imported through a host pointer stays write-watched: the driver's import probe reports "host imports of write-watched memory keep the write watch: yes … direct host imports of private memory are settled and stay watched" on both GPUs. So I ported the setup, leaving every check as it is (patch below), and ran it 3 times per GPU: | | RTX 5070 Ti | Radeon | |---|---|---| | `main`'s driver code (the PR's 5 production files reverted) | fails 3 of 3 | fails 3 of 3 | | this PR | passes 3 of 3 | passes 3 of 3 | On `main`, both GPUs fail the same 4 cases. In the two "host write over a shadowed unit" cases and "host write during another thread's publish of its unit", byte 0 holds the shadow's GPU results instead of the host write's. "short host write of 65553 of 131072 bytes at +0" fails with "byte 65553 holds 51, not 190". The other 8 short-write cases pass on `main` too. With this PR every case passes, with no validation messages. <details> <summary>The Windows port of HostWriteShadowedUnit.cpp (built and run on both GPUs; macOS keeps a stub)</summary> ```diff --- a/core/libs/prx/libSceAgcDriver/tests/execution/HostWriteShadowedUnit.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/HostWriteShadowedUnit.cpp @@ -8,6 +8,9 @@ #include "VulkanTestDevice.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/GuestWriteWatch.hpp" +#ifdef _WIN32 +#include <windows.h> +#endif #ifdef __linux__ #include <fcntl.h> #include <sys/mman.h> @@ -25,9 +28,9 @@ #include <thread> #include <utility> -#ifndef __linux__ +#if !defined(__linux__) && !defined(_WIN32) int main() { - std::printf("skipped, udmabuf imports are Linux only\n"); + std::printf("skipped, host imports of write-watched memory are tested on Linux and Windows only\n"); return VulkanTestSkipped; } #else @@ -52,6 +55,7 @@ std::uint8_t* mapping = nullptr; int backing = -1; +#ifdef __linux__ bool Resolve(std::uintptr_t address, std::size_t bytes, int* file, std::uint64_t* offset) { const auto begin = reinterpret_cast<std::uintptr_t>(mapping); if (address < begin || address + bytes > begin + MappingBytes) return false; @@ -59,6 +63,7 @@ *offset = address - begin; return *file >= 0; } +#endif int Skip(const char* why) { std::printf("skipped, %s\n", why); @@ -213,6 +218,15 @@ int main() { using namespace AgcDriver::Graphics; try { +#ifdef _WIN32 + auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + if (!UnitShadowEnabled()) return Skip("unit shadows are off"); + mapping = static_cast<std::uint8_t*>(GuestArena::GuestArenaAllocate_nid_postfix(MappingBytes, 65536)); + Require(mapping != nullptr, "host write over a shadowed unit: cannot allocate the arena block"); + GuestArena::GuestArenaCommit_nid_postfix(mapping, MappingBytes, PAGE_READWRITE, MappingBytes); + std::memset(mapping, AroundByte, MappingBytes); +#else const int udmabuf = open("/dev/udmabuf", O_RDWR | O_CLOEXEC); if (udmabuf < 0) return Skip("host imports are unavailable: /dev/udmabuf is not accessible"); close(udmabuf); @@ -228,6 +242,7 @@ mapping = static_cast<std::uint8_t*>(mapped); GuestArena::GuestArenaSetSharedBacking_nid_postfix(Resolve); std::memset(mapping, AroundByte, MappingBytes); +#endif const auto address = reinterpret_cast<std::uint64_t>(mapping); GuestAllocations::Mutation().Add(mapping, MappingBytes, true, true, true); @@ -237,7 +252,9 @@ } Require(ranges.size() == 1, "host write over a shadowed unit: the allocation is not registered"); Require(device->ImportGuestMemory(ranges, GuestAllocations::GuestAllocationsGeneration_nid_postfix(), false), "host write over a shadowed unit: the import made without the GPU lock was refused"); +#ifndef _WIN32 GuestWriteWatch::GuestWriteWatchRegister_nid_postfix(mapping, MappingBytes); +#endif const auto firstUnit = (address + Unit - 1) & ~(Unit - 1); const auto context = AgcDriver::VulkanDeviceTestAccess::ContextOf(*device); @@ -246,7 +263,11 @@ std::lock_guard lock(AgcDriver::GuestMemory::GpuMutex()); import = HostImportFor(context, address, MappingBytes); } +#ifdef _WIN32 + if (import == nullptr) return Skip("no host import of the arena block"); +#else if (import == nullptr || !import->dmaBuf) return Skip("the device imported the mapping through a host pointer, which is compared, not watched"); +#endif if (!AgcDriver::GuestMemory::Watched(firstUnit, 8 * Unit)) return Skip("the imported units are not write-watched"); landHook = AgcDriver::GuestMemory::CurrentFlushHook(); @@ -266,7 +287,9 @@ AgcDriver::GuestMemory::SetFlushHook(landHook); Require(passed, "host write over a shadowed unit: a publish put a unit shadow's results over a host write, or a short host write lost the GPU's results past it"); +#ifndef _WIN32 GuestWriteWatch::GuestWriteWatchUnregister_nid_postfix(mapping, MappingBytes); +#endif GuestAllocations::Mutation().Remove(mapping); std::cout << "host write shadowed unit tests passed\n"; return 0; ``` Windows arena memory is write-watched without `GuestWriteWatchRegister`, as in `SampledCacheBudget.cpp`'s `WatchedSurface`. 1 MiB is also Windows' floor (`settleFloorBytes`) for keeping a settled import watched. The `main` failures and the PR passes in the tables above are from this patch plus one diagnostic print. The patch as shown was then built and passed once more on each GPU. I haven't built it on Linux or macOS. </details> I'd suggest adding the Windows setup, so the regression test also covers the platform where Stellar Blade crashes. That's optional for this PR. **Reading the change:** tagging host writes `ReadSite::HostWrite` instead of `Store` is what makes `FlushForAccess` choose `PublishScope::Whole` for them. `WritesGuestMemory` keeps the hook-sync profiling treating them as stores. The `AnyPublishInProgress` wait covers the case where another thread's publish is already under way. One small, non-blocking point: `PublishShadow`'s skip inside a completion (`UnitShadow.cpp`, `Recorder::InCompletion() && CurrentReadSite() == ReadSite::Store`) still names only `Store`. I found no path where a host write runs inside a completion: the driver's own `GuestArena::HostWrite` is under `OwnHostWrite`, so `PrepareHostWrite` returns early for it. But `WritesGuestMemory` there would keep the two sites alike. Not run: Linux, macOS, and a title (I don't have Demon's Souls or Stellar Blade). AI-assisted: yes (Claude Code)
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!17787
No description provided.