Repository navigation
d3d12: slot-133 shadows pass over the title's reserved pages until the journal names them (RT-SHADOW-RESERVATION-SKIP) #2391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
azsekazvamdimitar
wants to merge
1
commit into
ProjectLeonida:main
Choose a base branch
from
azsekazvamdimitar:shadow-reservation-skip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,9 @@ | |
| // faulting page. | ||
| // | ||
| // Successful title memory map changes also invalidate the shadows' cached protection state. The shadows' own | ||
| // protection changes stay local to their trackers. | ||
| // protection changes stay local to their trackers. A reserved page of the title's placeholders and physical-page | ||
| // windows (title_reservations, below) changes only through such a change, so a shadow doesn't ask about it again | ||
| // until one names it. | ||
| // | ||
| // The shadows themselves are brought up to date in one place, DeviceState::refresh_shadows in | ||
| // d3d12_vulkan_device.cpp, which VulkanQueue::execute_now calls (VulkanContext::set_before_execute) before every | ||
|
|
@@ -720,6 +722,85 @@ std::uint64_t ask(std::uint64_t reader, const Runs& runs, Dirty&& dirty, Refused | |
|
|
||
| } // namespace leonida::title_write_watch | ||
|
|
||
| // 7. The title's xmem reservations (title_reservations): its placeholders and physical-page windows, from the | ||
| // reservation to its release. Nothing but xmem and the title's own memory calls changes their pages, and those | ||
| // record every change in title_memory_map (a placeholder refuses a plain commit, and the title doesn't commit | ||
| // inside a window with VirtualAlloc: see the windows in xmem.cpp). A shadow takes a page there that is reserved | ||
| // and not mapped as zero until the journal names it, instead of asking VirtualQuery at every submission: under | ||
| // Wine a query waits for the virtual-memory lock, and the world's thousands of 64 KiB placeholders made the | ||
| // queries most of a refresh. | ||
| namespace leonida::title_reservations { | ||
|
|
||
| // Allocates from host_heap, as title_write_watch does: the D3D12 layer asks under a tracker's lock. | ||
| struct State { | ||
| std::shared_mutex mutex; // ranges | ||
| host_heap::map<std::uintptr_t, std::uintptr_t> ranges; // base -> end; apart, never touching | ||
| std::atomic<std::size_t> count{0}; // ranges.size(), read without the lock | ||
| std::atomic<std::uint64_t> generation{1}; // moved whenever a range was released or dropped | ||
| }; | ||
|
|
||
| inline State& state() { | ||
| static State s; | ||
| return s; | ||
| } | ||
|
|
||
| // xmem: [base, base + size) was reserved as a placeholder or a physical-page window of the title's. Inside one range | ||
| // already, it is part of that reservation (a window replacing part of a placeholder). A range it only overlaps was | ||
| // released without xmem seeing it, and goes whole, as xmem's tables drop it: nothing outside the new reservation is | ||
| // known to be one of the title's any more. | ||
| inline void add(std::uintptr_t base, std::uintptr_t size) { | ||
| if (!size) return; | ||
| State& s = state(); | ||
| const std::uintptr_t end = base + size; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor defensive guard: |
||
| std::unique_lock lock(s.mutex); | ||
| auto it = s.ranges.upper_bound(base); | ||
| if (it != s.ranges.begin() && std::prev(it)->second > base) --it; | ||
| if (it != s.ranges.end() && it->first <= base && it->second >= end) return; // inside one range already | ||
| bool erased = false; | ||
| while (it != s.ranges.end() && it->first < end) { | ||
| it = s.ranges.erase(it); | ||
| erased = true; | ||
| } | ||
| s.ranges.emplace(base, end); | ||
| s.count.store(s.ranges.size(), std::memory_order_release); | ||
| if (erased) s.generation.fetch_add(1, std::memory_order_release); | ||
| } | ||
|
|
||
| // xmem: [start, end) was released. | ||
| inline void forget(std::uintptr_t start, std::uintptr_t end) { | ||
| State& s = state(); | ||
| if (start >= end || !s.count.load(std::memory_order_acquire)) return; | ||
| std::unique_lock lock(s.mutex); | ||
| auto it = s.ranges.lower_bound(start); | ||
| if (it != s.ranges.begin() && std::prev(it)->second > start) --it; | ||
| bool erased = false; | ||
| while (it != s.ranges.end() && it->first < end) { | ||
| const std::uintptr_t base = it->first, until = it->second; | ||
| it = s.ranges.erase(it); | ||
| erased = true; | ||
| if (base < start) s.ranges.emplace(base, start); | ||
| if (until > end) it = s.ranges.emplace(end, until).first; | ||
| } | ||
| if (!erased) return; | ||
| s.count.store(s.ranges.size(), std::memory_order_release); | ||
| s.generation.fetch_add(1, std::memory_order_release); | ||
| } | ||
|
|
||
| // Moved whenever a reservation was released: what a shadow keeps of the reserved pages starts again. (A reservation | ||
| // made covers pages no shadow had settled: they were in no range until then, or in one released since.) | ||
| inline std::uint64_t generation() { return state().generation.load(std::memory_order_acquire); } | ||
|
|
||
| // Whether [begin, end) is inside one of the title's xmem reservations. | ||
| inline bool contains(std::uintptr_t begin, std::uintptr_t end) { | ||
| State& s = state(); | ||
| if (!s.count.load(std::memory_order_acquire)) return false; | ||
| std::shared_lock lock(s.mutex); | ||
| auto it = s.ranges.upper_bound(begin); | ||
| return it != s.ranges.begin() && (--it)->second >= end; | ||
| } | ||
|
|
||
| } // namespace leonida::title_reservations | ||
|
|
||
| namespace leonida::kernel_writes { | ||
|
|
||
| // The layer's side: makes every watched page in [begin, end) writable. Set once the first shadow exists. | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor race note: for tracked windows/placeholders,
title_release()frees the pages and dropsg_fault_windows/g_title_placeholdersunderg_phys_mutex+FaultLockExclusive, then unlocks before returning here. If another thread immediately reserves the freed range viaXMemVirtualAllocand callstitle_reservations::add()before this line runs,forget(start, release_end)will drop the new reservation (safe -- shadows just fall back to querying it -- only loses the skip optimization for that range). Callingtitle_reservations::forget(start, end)insidetitle_release()underFaultLockExclusive(while keeping this fallback for untracked releases) would close that window.