diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f477a11939..5b375515f3 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3967,6 +3967,9 @@ if(TARGET Vulkan::Headers) # handler asks the tracker whose copy faulted too, which must not take its lock again on that thread (#2418). add_test(NAME d3d12_title_window_write_back_fault COMMAND d3d12_title_va_test --window-writeback-fault) set_tests_properties(d3d12_title_window_write_back_fault PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120) + # The same fault with a later run of the write-back in the tile it reopened: that run's GPU words go back too. + add_test(NAME d3d12_title_window_write_back_fault_tile COMMAND d3d12_title_va_test --window-writeback-fault-tile) + set_tests_properties(d3d12_title_window_write_back_fault_tile PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120) # The same return copy for a slot-133 buffer's shadow (the write-back of a compute result the title reads again). add_test(NAME d3d12_placed_write_back COMMAND d3d12_title_va_test --placed-writeback) set_tests_properties(d3d12_placed_write_back PROPERTIES SKIP_RETURN_CODE 77) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index da630d06cb..64ff2e0567 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -446,10 +446,17 @@ class ShadowPages { // A page the title wrote since it was last copied holds the newer bytes already: its GPU result is dropped and // the refresh's own copy carries the title's. So is a page whose mapping changed, one a kernel write may be // filling, and one xmem maps at another address too (a write there is seen by no guard and no watch here). - const auto held = [&](std::size_t i) { + // Decided for every page before the first copy: a copy below can fault on another guard, and the handler then + // reopens the whole 64 KiB tile here on this thread (reopen_range), which clears kClean and kProtected there. + // Read live, that made the later runs in the tile count as written by the title, and their GPU results were + // dropped from a batch already taken off the queue. A page the title wrote before this point is held still. + const std::size_t p0 = (from - first_) / kPage, p1 = (to - first_ + kPage - 1) / kPage; + held_pages_.resize(p1 - p0); + for (std::size_t i = p0; i < p1; ++i) { const std::uint8_t s = state_[i]; - return (s & (kZero | kRequery | kPinned | kAliased)) || (!(s & kProtected) && !(s & kClean)); - }; + held_pages_[i - p0] = (s & (kZero | kRequery | kPinned | kAliased)) || (!(s & kProtected) && !(s & kClean)); + } + const auto held = [&](std::size_t i) { return held_pages_[i - p0] != 0; }; constexpr std::uintptr_t kPiece = 256; // compared and copied in pieces this size std::size_t written = 0; for (std::uintptr_t at = from & ~(kPage - 1); at < ((to + kPage - 1) & ~(kPage - 1));) { @@ -1415,6 +1422,7 @@ class ShadowPages { std::uintptr_t first_ = 0, last_ = 0; // the pages covered SRWLOCK lock_ = SRWLOCK_INIT; // state_, cover_, the targets, and the protection of the pages std::atomic writer_{0}; // the thread in write_back, which holds lock_ while it copies into the title + Vector held_pages_; // write_back's per-page decision, taken before its copies (under lock_) Vector state_; // per 4 KiB page Vector cover_; // per 4 KiB page: the targets that read it Vector targets_; // by start diff --git a/runtime/test/d3d12_title_va_test.cpp b/runtime/test/d3d12_title_va_test.cpp index 38a4a675c3..51d529abab 100644 --- a/runtime/test/d3d12_title_va_test.cpp +++ b/runtime/test/d3d12_title_va_test.cpp @@ -524,8 +524,9 @@ struct WriteBack { UINT64 gpu; }; -// One submission that dispatches through a root UAV at `va` and waits for it. False on a failed call. -int submit_write(WriteBack& s, std::uint64_t va) { +// One submission that dispatches through a root UAV at `va` (and at `va2` too, when given) and waits for it. False +// on a failed call. +int submit_write(WriteBack& s, std::uint64_t va, std::uint64_t va2 = 0) { EXPECT(s.allocator->Reset() == S_OK); EXPECT(s.list->Reset(s.allocator, s.c->pso) == S_OK); ID3D12DescriptorHeap* heaps[] = {s.heap}; @@ -536,6 +537,10 @@ int submit_write(WriteBack& s, std::uint64_t va) { s.list->SetComputeRootUnorderedAccessView(1, va); s.list->SetComputeRootDescriptorTable(2, D3D12_GPU_DESCRIPTOR_HANDLE{s.gpu}); s.list->Dispatch(1, 1, 1); + if (va2) { + s.list->SetComputeRootUnorderedAccessView(1, va2); + s.list->Dispatch(1, 1, 1); + } D3D12_RESOURCE_BARRIER to_copy{}; to_copy.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; to_copy.Transition.pResource = s.out; @@ -711,7 +716,13 @@ int check_window_write_back(Context& c, bool geometry) { // and copies the page forward again; // 3. a geometry cell over the ring's first 64 KiB makes the hot page read-only, unknown to the tracker; // 4. the next submission's write-back faults on that page, and must return with the GPU's words in the pages. -int check_window_write_back_fault(Context& c) { +// With `same_tile`, a second run in the same 64 KiB tile: step 2's submission dispatches at the third page too, and +// the title then writes the second page (hot, so no fault: the write watch has it). Step 4's write-back (of step 2's +// results) then runs over [page 0] (faults; the geometry cell reopens the whole tile, and the tracker's pages there +// with it), [page 1] (the title's: held) and [page 2 ...]. The tile's reopening on the write-back's own thread used to +// make page 2 count as written by the title, so its GPU words were dropped and the forward copy put the title's zeros +// over them. The title's word on page 1 must still win. +int check_window_write_back_fault(Context& c, bool same_tile) { if (!leonida::title_write_watch::enabled()) { std::fprintf(stderr, "SKIP: write watching is off (LEONIDA_WRITE_WATCH=0, Wine, or no host heap)\n"); return 77; @@ -762,13 +773,18 @@ int check_window_write_back_fault(Context& c) { const std::uint64_t title_faults = rt::d3d12::title_shadow_watch(c.inner).faults - faults_before; EXPECT(title_faults == 1); EXPECT(rt::d3d12::title_shadow_watch(c.inner).watched_bytes > 0); // the window's pages are write-watched - EXPECT(submit_write(s, ring_va) == 0); + constexpr UINT kPageWords = 4096 / 4; + EXPECT(submit_write(s, ring_va, same_tile ? ring_va + 2 * 4096 : 0) == 0); UINT dropped = 0; for (UINT i = 0; i < kElements; ++i) dropped += ring[i] == 0; EXPECT(dropped == kElements); // the title wrote that page since it was copied: the GPU's words were not taken const DWORD hot = protection(ring); EXPECT(hot == PAGE_READWRITE); // asked about, not guarded EXPECT(rt::d3d12::title_shadow_watch(c.inner).hot_bytes > 0); + if (same_tile) { + EXPECT(protection(ring + kPageWords) == PAGE_READWRITE); // the fault made the whole block hot + ring[kPageWords + 500] = 0x5EED0002u; // no fault: the write watch has it + } // Step 3: what a geometry view over these pages arms (GeometryViews), standing in for the other guard. const auto tile = std::uintptr_t(ring_va); @@ -808,6 +824,16 @@ int check_window_write_back_fault(Context& c) { ring[1000] == 0x5EED0001u ? "kept" : "lost"); EXPECT(nonzero > 0); EXPECT(ring[1000] == 0x5EED0001u); + if (same_tile) { + // The run past the title's page, in the tile the faulting copy reopened: its GPU words went back too. + UINT later = 0; + for (UINT i = 0; i < kElements; ++i) later += ring[2 * kPageWords + i] != 0; + std::printf("write back fault, same tile: %u of %u words the second dispatch wrote on the third page are in " + "the title's pages, the title's word on the second page %s\n", + later, kElements, ring[kPageWords + 500] == 0x5EED0002u ? "kept" : "lost"); + EXPECT(later == nonzero); // the same shader, the same words + EXPECT(ring[kPageWords + 500] == 0x5EED0002u); + } leonida::geometry_writes::release(tile); aliases.remove_title_memory(ring_va); @@ -1344,7 +1370,9 @@ int main(int argc, char** argv) { const bool rebind = api_copy || mirror_off || second_device || (argc == 2 && std::strcmp(argv[1], "--rebound-tables") == 0); const bool window_cap = argc == 2 && std::strcmp(argv[1], "--window-cap") == 0; const bool window_write_back = argc == 2 && std::strcmp(argv[1], "--window-writeback") == 0; - const bool window_write_back_fault = argc == 2 && std::strcmp(argv[1], "--window-writeback-fault") == 0; + const bool window_write_back_tile = argc == 2 && std::strcmp(argv[1], "--window-writeback-fault-tile") == 0; + const bool window_write_back_fault = + window_write_back_tile || (argc == 2 && std::strcmp(argv[1], "--window-writeback-fault") == 0); const bool placed_write_back = argc == 2 && std::strcmp(argv[1], "--placed-writeback") == 0; const bool keep_refill = argc == 2 && std::strcmp(argv[1], "--shadow-keep-refill") == 0; if (window_cap) { @@ -1479,7 +1507,7 @@ int main(int argc, char** argv) { return 0; } if (window_write_back_fault) { - if (const int result = check_window_write_back_fault(c)) return result; + if (const int result = check_window_write_back_fault(c, window_write_back_tile)) return result; if (d.validation_errors()) { std::fprintf(stderr, "%u validation errors\n", d.validation_errors()); return 1;