diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-30 04:34:17 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-30 04:34:17 -0500 |
| commit | efc1929908a728d95fa09ffef0a743d2a649b86d (patch) | |
| tree | 9e2b3a70ef49d7ec265301cf85606686f371ac57 /runtime/shims/xmem.cpp | |
| parent | 1aefed0dacd492a040909c9a423f270ebdab84f4 (diff) | |
runtime: eliminate Windows 11 full-space commit_pages_locked broadcast storm, ShadowPages/TitleTextureCells guard conflict, and streaming budget bursts
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 247bcbdce..0d32d27a9 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -50,6 +50,7 @@ | |||
| 50 | #include "../src/title_mirror_space.h" | 50 | #include "../src/title_mirror_space.h" |
| 51 | #include "../src/title_page_watch.h" | 51 | #include "../src/title_page_watch.h" |
| 52 | #include "../src/title_geometry_watch.h" | 52 | #include "../src/title_geometry_watch.h" |
| 53 | #include "../src/title_texture_refresh.h" | ||
| 53 | #include "../src/title_va_alias.h" | 54 | #include "../src/title_va_alias.h" |
| 54 | #include "../src/value_write_sink.h" | 55 | #include "../src/value_write_sink.h" |
| 55 | 56 | ||
| @@ -453,6 +454,7 @@ struct Section { | |||
| 453 | void* scratch; | 454 | void* scratch; |
| 454 | bool file_backed; | 455 | bool file_backed; |
| 455 | std::vector<uint32_t, leonida::host_heap::Allocator<uint32_t>> frames; | 456 | std::vector<uint32_t, leonida::host_heap::Allocator<uint32_t>> frames; |
| 457 | leonida::PageCommitBits<leonida::host_heap::Allocator<std::uint64_t>> commit_bits; | ||
| 456 | Section(HANDLE h, bool use_file = false); | 458 | Section(HANDLE h, bool use_file = false); |
| 457 | Section(const Section&) = delete; | 459 | Section(const Section&) = delete; |
| 458 | Section& operator=(const Section&) = delete; | 460 | Section& operator=(const Section&) = delete; |
| @@ -1539,12 +1541,15 @@ PageCommit commit_mapped_page(uintptr_t address, uintptr_t accessed, DWORD& prot | |||
| 1539 | return PageCommit::not_ours; | 1541 | return PageCommit::not_ours; |
| 1540 | } | 1542 | } |
| 1541 | // The fault handler needs the current protection of the accessed host page to decide whether to retry. | 1543 | // The fault handler needs the current protection of the accessed host page to decide whether to retry. |
| 1544 | const size_t page_idx = static_cast<size_t>((it->second.offset + within) / kPhysicalPageSize); | ||
| 1542 | if (sections_committed() || info.State == MEM_COMMIT) { | 1545 | if (sections_committed() || info.State == MEM_COMMIT) { |
| 1546 | it->second.section->commit_bits.mark_committed(page_idx); | ||
| 1543 | protect = info.Protect; | 1547 | protect = info.Protect; |
| 1544 | return PageCommit::committed_before; | 1548 | return PageCommit::committed_before; |
| 1545 | } | 1549 | } |
| 1546 | void* const backing = static_cast<uint8_t*>(it->second.section->scratch) + it->second.offset + within; | 1550 | void* const backing = static_cast<uint8_t*>(it->second.section->scratch) + it->second.offset + within; |
| 1547 | if (!VirtualAlloc(backing, kPhysicalPageSize, MEM_COMMIT, PAGE_READWRITE)) return PageCommit::failed; | 1551 | if (!VirtualAlloc(backing, kPhysicalPageSize, MEM_COMMIT, PAGE_READWRITE)) return PageCommit::failed; |
| 1552 | it->second.section->commit_bits.mark_committed(page_idx); | ||
| 1548 | leonida::geometry_writes::notify(address, address + kPhysicalPageSize, leonida::geometry_writes::Event::mapping); | 1553 | leonida::geometry_writes::notify(address, address + kPhysicalPageSize, leonida::geometry_writes::Event::mapping); |
| 1549 | leonida::title_memory_map::changed(address, address + kPhysicalPageSize); | 1554 | leonida::title_memory_map::changed(address, address + kPhysicalPageSize); |
| 1550 | return PageCommit::committed_now; | 1555 | return PageCommit::committed_now; |
| @@ -2073,6 +2078,7 @@ BOOL WINAPI XMemAllocatePhysicalPages(uint64_t attributes, uint64_t* page_count, | |||
| 2073 | std::fprintf(stderr, "[rt] xmem: %llu physical pages, scratch view %p size %llu\n", | 2078 | std::fprintf(stderr, "[rt] xmem: %llu physical pages, scratch view %p size %llu\n", |
| 2074 | static_cast<unsigned long long>(count), section->scratch, static_cast<unsigned long long>(bytes)); | 2079 | static_cast<unsigned long long>(count), section->scratch, static_cast<unsigned long long>(bytes)); |
| 2075 | section->frames.resize(count); | 2080 | section->frames.resize(count); |
| 2081 | section->commit_bits.init(static_cast<std::size_t>(count), use_file || sections_committed()); | ||
| 2076 | for (uint64_t i = 0; i < count; ++i) { | 2082 | for (uint64_t i = 0; i < count; ++i) { |
| 2077 | uint64_t slot; | 2083 | uint64_t slot; |
| 2078 | if (!g_free_slots.empty()) { | 2084 | if (!g_free_slots.empty()) { |
| @@ -3338,6 +3344,27 @@ static bool commit_pages_locked(const uint64_t* page_array, size_t count) { | |||
| 3338 | SetLastError(ERROR_INVALID_PARAMETER); | 3344 | SetLastError(ERROR_INVALID_PARAMETER); |
| 3339 | return false; | 3345 | return false; |
| 3340 | } | 3346 | } |
| 3347 | const bool all_committed = sections_committed(); | ||
| 3348 | leonida::host_heap::vector<uintptr_t> notify_vas; | ||
| 3349 | struct NotifyCommitRuns { | ||
| 3350 | leonida::host_heap::vector<uintptr_t>& vas; | ||
| 3351 | ~NotifyCommitRuns() { | ||
| 3352 | if (vas.empty()) return; | ||
| 3353 | // Notify only the exact title VAs currently mapping these newly-committed physical pages (coalesced into | ||
| 3354 | // contiguous runs), never a full-space title_memory_map::changed() that wakes every watched texture and | ||
| 3355 | // invalidates every ShadowPages / VirtualQuery cache across the process. | ||
| 3356 | leonida::coalesce_page_runs(vas, kPhysicalPageSize, [](std::uintptr_t lo, std::uintptr_t hi) { | ||
| 3357 | leonida::geometry_writes::notify(lo, hi, leonida::geometry_writes::Event::mapping); | ||
| 3358 | leonida::title_memory_map::changed(lo, hi); | ||
| 3359 | }); | ||
| 3360 | } | ||
| 3361 | } notify_guard{notify_vas}; | ||
| 3362 | const auto collect_page_vas = [&](uint64_t page) { | ||
| 3363 | if (const auto mapped = g_page_addresses.find(page); mapped != g_page_addresses.end()) | ||
| 3364 | for (const uintptr_t va : mapped->second) notify_vas.push_back(va); | ||
| 3365 | if (const auto tables = g_page_tables.find(page); tables != g_page_tables.end()) | ||
| 3366 | for (const uintptr_t va : tables->second) notify_vas.push_back(va); | ||
| 3367 | }; | ||
| 3341 | size_t first = 0; | 3368 | size_t first = 0; |
| 3342 | while (first < count) { | 3369 | while (first < count) { |
| 3343 | const auto it = g_pages.find(page_array[first]); | 3370 | const auto it = g_pages.find(page_array[first]); |
| @@ -3345,10 +3372,18 @@ static bool commit_pages_locked(const uint64_t* page_array, size_t count) { | |||
| 3345 | SetLastError(ERROR_INVALID_PARAMETER); | 3372 | SetLastError(ERROR_INVALID_PARAMETER); |
| 3346 | return false; | 3373 | return false; |
| 3347 | } | 3374 | } |
| 3348 | if (sections_committed()) { | 3375 | if (all_committed) { |
| 3349 | ++first; // Already committed, but transfer_physical still needs every page number validated. | 3376 | ++first; // Already committed, but transfer_physical still needs every page number validated. |
| 3350 | continue; | 3377 | continue; |
| 3351 | } | 3378 | } |
| 3379 | Section& sec = *it->second.section; | ||
| 3380 | const size_t idx0 = static_cast<size_t>(it->second.offset / kPhysicalPageSize); | ||
| 3381 | if (sec.file_backed || sec.commit_bits.is_committed(idx0)) { | ||
| 3382 | if (!sec.file_backed && sec.commit_bits.mark_committed_and_notified(idx0)) | ||
| 3383 | collect_page_vas(page_array[first]); | ||
| 3384 | ++first; | ||
| 3385 | continue; | ||
| 3386 | } | ||
| 3352 | size_t last = first + 1; | 3387 | size_t last = first + 1; |
| 3353 | while (last < count) { | 3388 | while (last < count) { |
| 3354 | const auto next = g_pages.find(page_array[last]); | 3389 | const auto next = g_pages.find(page_array[last]); |
| @@ -3356,12 +3391,16 @@ static bool commit_pages_locked(const uint64_t* page_array, size_t count) { | |||
| 3356 | next->second.offset != it->second.offset + (last - first) * kPhysicalPageSize) { | 3391 | next->second.offset != it->second.offset + (last - first) * kPhysicalPageSize) { |
| 3357 | break; | 3392 | break; |
| 3358 | } | 3393 | } |
| 3394 | const size_t next_idx = static_cast<size_t>(next->second.offset / kPhysicalPageSize); | ||
| 3395 | if (sec.commit_bits.is_committed(next_idx)) break; | ||
| 3359 | ++last; | 3396 | ++last; |
| 3360 | } | 3397 | } |
| 3361 | void* const backing = static_cast<uint8_t*>(it->second.section->scratch) + it->second.offset; | 3398 | void* const backing = static_cast<uint8_t*>(sec.scratch) + it->second.offset; |
| 3362 | if (!it->second.section->file_backed && !VirtualAlloc(backing, (last - first) * kPhysicalPageSize, MEM_COMMIT, PAGE_READWRITE)) return false; | 3399 | if (!VirtualAlloc(backing, (last - first) * kPhysicalPageSize, MEM_COMMIT, PAGE_READWRITE)) return false; |
| 3363 | // Committing the scratch backing affects every title alias. Their addresses are unknown here. | 3400 | for (size_t k = first; k < last; ++k) { |
| 3364 | leonida::title_memory_map::changed(); | 3401 | if (sec.commit_bits.mark_committed_and_notified(idx0 + (k - first))) |
| 3402 | collect_page_vas(page_array[k]); | ||
| 3403 | } | ||
| 3365 | first = last; | 3404 | first = last; |
| 3366 | } | 3405 | } |
| 3367 | return true; | 3406 | return true; |