Refuse stream-pool mappings shorter than the requested span

DadaCap identified the short-mapping case in the review of PR #1920. Reuse and allocation now require full coverage, preserving both live aliases when a request spans a boundary.
This commit is contained in:
lixonik
2026-11-27 11:32:29 +03:00
parent 7557fe87f4
commit d78b0e90ab
4 changed files with 55 additions and 25 deletions

View File

@ -3366,26 +3366,24 @@ struct DeviceState {
// held.
bool resolve_pool_memory(std::uint64_t va, std::uint64_t bytes, std::uint64_t& out) {
leonida::title_gpu_views::Range view{};
if (!leonida::title_gpu_views::view_at(static_cast<std::uintptr_t>(va),
static_cast<std::uintptr_t>(std::max<std::uint64_t>(bytes, 1)), view))
return false;
const std::uint64_t offset = va >= view.gpu && va - view.gpu < view.size ? va - view.gpu : va - view.cpu;
// Rule B: the CPU view where the host reads the frames, the GPU-only pages only when that view has none.
const std::uint64_t source = leonida::title_gpu_views::to_host_view(static_cast<std::uintptr_t>(va),
static_cast<std::uintptr_t>(
std::max<std::uint64_t>(bytes, 1)));
const std::uint64_t twin = leonida::title_gpu_views::other_view(static_cast<std::uintptr_t>(source));
const std::uint64_t twin_view = twin ? twin : source; // the pair always has two addresses
auto& aliases = leonida::title_va_aliases();
const auto other = source == va ? twin_view : source;
const auto max_size = std::min<std::uint64_t>(view.size - offset, kPoolExtentMax);
const auto reuse = [&] { return aliases.reuse_pool_mapping(va, other, max_size, out); };
if (!leonida::title_gpu_views::view_at(static_cast<std::uintptr_t>(va), 1, view)) return false;
const auto want = std::max<std::uint64_t>(bytes, 1);
const auto unbacked = [&] {
out = 0;
++unbacked_requests;
note_unbacked("stream pool", va, bytes);
return true;
};
const std::uint64_t offset = va >= view.gpu && va - view.gpu < view.size ? va - view.gpu : va - view.cpu;
if (want > view.size - offset) return unbacked();
// Rule B: the CPU view where the host reads the frames, the GPU-only pages only when that view has none.
const std::uint64_t source = leonida::title_gpu_views::to_host_view(static_cast<std::uintptr_t>(va), want);
const std::uint64_t twin = leonida::title_gpu_views::other_view(static_cast<std::uintptr_t>(source));
const std::uint64_t twin_view = twin ? twin : source; // the pair always has two addresses
auto& aliases = leonida::title_va_aliases();
const auto other = source == va ? twin_view : source;
const auto max_size = std::min<std::uint64_t>(view.size - offset, kPoolExtentMax);
const auto reuse = [&] { return aliases.reuse_pool_mapping(va, other, want, max_size, out); };
if (reuse()) return true;
if (!readable_page(source)) return unbacked();
const std::uint64_t pool_end = (source >= view.gpu && source - view.gpu < view.size ? view.gpu : view.cpu) +
@ -3401,6 +3399,7 @@ struct DeviceState {
aliases.gap(twin_view, twin_lo, twin_hi);
if (!leonida::clip_title_pool_extent(extent, source, twin_view, source_lo, source_hi, twin_lo, twin_hi))
return reuse() || unbacked();
if (want > std::min(extent.size - (source - extent.start), run_end - source)) return reuse() || unbacked();
const auto twin_start = twin_view - (source - extent.start);
std::uint64_t shader = 0;
if (geometry.cap() && watch_shadow_writes() && !context->knows_buffer_va(extent.start, extent.size))
@ -3490,12 +3489,18 @@ struct DeviceState {
}
const std::uint64_t want = std::max<std::uint64_t>(bytes, 1);
std::lock_guard lock(title_window_mutex);
if (const std::uint64_t made = aliases.shader_va(va); made != va) { // another thread made it meanwhile
std::uint64_t made = 0, available = 0;
const bool has_mapping = aliases.range_of(va, made, available) && made != va;
if (has_mapping && available >= want) {
out = made;
return true;
}
// A stream pool's address: one mapping for its readable run, shared by both of its views.
if (resolve_pool_memory(va, bytes, out)) return true;
if (has_mapping) {
out = made;
return true;
}
if (readable_page(va)) {
std::uint64_t lo = 0, hi = UINT64_MAX;
aliases.gap(va, lo, hi); // never over a slot-133 buffer's range or another window

View File

@ -76,12 +76,15 @@ public:
return true;
}
bool reuse_pool_mapping(std::uint64_t va, std::uint64_t twin, std::uint64_t max_size, std::uint64_t& shader_va) {
bool reuse_pool_mapping(std::uint64_t va, std::uint64_t twin, std::uint64_t want, std::uint64_t max_size,
std::uint64_t& shader_va) {
if (!want) return false;
std::unique_lock lock(mutex_);
const auto next = ranges_.upper_bound(va);
if (next != ranges_.begin()) {
const auto existing = std::prev(next);
if (va < existing->second.end) {
if (want > existing->second.end - va) return false;
shader_va = existing->second.shader_va + (va - existing->first);
return true;
}
@ -92,7 +95,7 @@ public:
if (twin >= other->second.end) return false;
const auto end = next == ranges_.end() ? UINT64_MAX : next->first;
const auto bytes = std::min({max_size, other->second.end - twin, end - va});
if (!bytes) return false;
if (bytes < want) return false;
const auto shader = other->second.shader_va + (twin - other->first);
decltype(ranges_) pending;
pending.try_emplace(va, Range{va + bytes, shader});
@ -218,11 +221,12 @@ public:
// read or write). Counted by kind: translated, or in the title's memory and left as it was.
std::uint64_t resolve(std::uint64_t va, std::uint64_t bytes, Kind kind) {
if (!va) return va;
std::uint64_t to = shader_va(va);
if (to != va) {
std::uint64_t mapped = va, available = 0;
const bool has_mapping = range_of(va, mapped, available) && mapped != va;
if (has_mapping && bytes <= available) {
translated_[kind].fetch_add(1, std::memory_order_relaxed);
note_reached(va, bytes);
return to;
return mapped;
}
Resolver resolver;
void* context;
@ -231,9 +235,13 @@ public:
resolver = resolver_;
context = hook_context_;
}
if (!resolver || !resolver(context, va, bytes, to)) return va;
std::uint64_t to = va;
if (!resolver || !resolver(context, va, bytes, to)) {
if (!has_mapping) return va;
to = mapped;
}
(to != va ? translated_ : untranslated_)[kind].fetch_add(1, std::memory_order_relaxed);
if (to != va) note_reached(va, bytes);
if (to && to != va) note_reached(va, bytes);
return to;
}

View File

@ -963,6 +963,18 @@ int check_stream_pool(Context& c, int& checks) {
aliases.add_title_memory(gpu_va, pool_bytes);
EXPECT(leonida::title_gpu_views::to_host_view(gpu_va + 0x2000, 0x100) == cpu_va + 0x2000);
constexpr UINT64 seam = pool_bytes / 2;
constexpr UINT64 existing = 0x100000;
const auto root = leonida::TitleVaAliases::root_view;
EXPECT(aliases.add(cpu_va + seam, 0x1000, existing));
const auto ranges_at_seam = aliases.size();
EXPECT(aliases.resolve(gpu_va + seam - 0x100, 0x200, root) == 0);
EXPECT(aliases.resolve(gpu_va + seam, 0x2000, root) == 0);
EXPECT(aliases.resolve(cpu_va + seam, 0x2000, root) == 0);
EXPECT(aliases.size() == ranges_at_seam);
EXPECT(aliases.shader_va(cpu_va + seam) == existing);
aliases.remove(cpu_va + seam, existing);
const D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0};
Ref<ID3D12DescriptorHeap> heap;
@ -984,6 +996,8 @@ int check_stream_pool(Context& c, int& checks) {
EXPECT(aliases.shader_va(gpu_va + 0x2000) == aliases.shader_va(cpu_va + 0x2000));
// Two ranges (the two views' addresses), each a page-aligned run of the pool: one mapping, not one per view.
EXPECT(aliases.size() == ranges_before + 2);
EXPECT(aliases.resolve(gpu_va + pool_bytes - 0x100, 0x200, root) == 0);
EXPECT(aliases.size() == ranges_before + 2);
std::printf("stream pool: GPU-only alias 0x%llx and CPU view 0x%llx share one %llu KiB mapping (V# base "
"0x%llx); a second view naming the twin made no mapping\n",
static_cast<unsigned long long>(gpu_va), static_cast<unsigned long long>(cpu_va),

View File

@ -159,14 +159,17 @@ int main() {
a.remove(title, 0x5000000);
EXPECT(a.add(title + size / 2, size, 0x7000000));
EXPECT(a.reuse_pool_mapping(title, twin, size, to) && to == 0x5000000);
EXPECT(!a.reuse_pool_mapping(title, twin, size, size, to));
EXPECT(a.shader_va(title) == title);
EXPECT(a.reuse_pool_mapping(title, twin, size / 2, size, to) && to == 0x5000000);
EXPECT(!a.reuse_pool_mapping(title, twin, size / 2 + 1, size, to));
EXPECT(a.range_of(title, to, room) && room == size / 2);
EXPECT(a.shader_va(title + size / 2) == 0x7000000);
EXPECT(a.reuse_pool_mapping(title + size / 2, twin + size / 2, size, to) && to == 0x7000000);
EXPECT(a.reuse_pool_mapping(title + size / 2, twin + size / 2, size, size, to) && to == 0x7000000);
EXPECT(!a.add_twin(title + 4 * size, title + 5 * size, size * 2, arena));
EXPECT(a.add(title + 4 * size, size, 0x8000000));
EXPECT(a.add(twin + 5 * size - 16, 16, 0x9000000));
EXPECT(a.reuse_pool_mapping(twin + 4 * size + 16, title + 4 * size + 16, size, to) && to == 0x8000010);
EXPECT(a.reuse_pool_mapping(twin + 4 * size + 16, title + 4 * size + 16, size - 32, size, to) && to == 0x8000010);
a.remove(title + 4 * size, 0x8000000);
EXPECT(a.shader_side(0x8000010));
a.forget_copy(twin + 4 * size, size, 0x8000000);