diff --git a/crates/victory-gpu/src/lib.rs b/crates/victory-gpu/src/lib.rs index db53f110b1..97503a7d46 100644 --- a/crates/victory-gpu/src/lib.rs +++ b/crates/victory-gpu/src/lib.rs @@ -62,5 +62,7 @@ pub use host::on_failure; pub use inplace::{title_memory_freeing, title_memory_section, title_memory_view}; #[cfg(windows)] pub use watch::{ - title_memory_reading, title_memory_remapped, title_memory_remapping, writing as kernel_will_write, + title_memory_reading, title_memory_reading_now, title_memory_reading_range, + title_memory_remapped, title_memory_remapping, + title_memory_remapping_range, writing as kernel_will_write, }; diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index f81539369c..69717e7b3b 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -668,12 +668,12 @@ impl Staging { // Not waited for: while the runtime remaps title memory the // texture is read through the kernel instead, whole or not at // all (`no_wait`). - let Some(mapped) = crate::watch::title_memory_reading_now() else { + let Some(mapped) = crate::watch::title_memory_reading_range_now(start, size) else { return self.refresh_unlocked(last, size); }; mapped } else { - waited(2, crate::watch::title_memory_reading) + waited(2, || crate::watch::title_memory_reading_range(start, size)) }; if !committed(self.memory, size) { // Partly mapped: a streamed texture with only some mips' pages diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index 60089a7e82..5105f8be1f 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -599,42 +599,117 @@ pub fn writing(at: usize, len: usize) { /// had asked whether the texture was all mapped and been told yes. Asking /// cannot help; not changing the mapping while such a read is under way /// does. -static MAPPING: std::sync::RwLock<()> = std::sync::RwLock::new(()); - -/// What the runtime holds while it changes what title memory shows -/// ([`title_memory_remapping`]). -pub struct Remapping(#[allow(dead_code)] std::sync::RwLockWriteGuard<'static, ()>); - -/// The runtime is about to map, unmap or move physical pages of title -/// memory: until what it returns is dropped, no read of title memory in -/// place is under way and none begins ([`title_memory_reading`]). Taken -/// around the whole change, the unmapping and the mapping again. -pub fn title_memory_remapping() -> Remapping { - Remapping(MAPPING.write().unwrap_or_else(|e| e.into_inner())) +// A missing range is global, including overflow and unknown extents. +type MappingRange = Option<(usize, usize)>; +fn mapping_range(at: usize, len: usize) -> MappingRange { + if len == 0 { return None; } + at.checked_add(len).map(|end| (at, end)) } - -/// What a read of title memory in place holds ([`title_memory_reading`]). -pub struct Reading(#[allow(dead_code)] std::sync::RwLockReadGuard<'static, ()>); - -/// For reading title memory in place: until what it returns is dropped, -/// the runtime changes no mapping, so memory found mapped stays mapped. -/// Not to be taken twice by one thread, nor held across anything that -/// maps title memory. -pub fn title_memory_reading() -> Reading { - Reading(MAPPING.read().unwrap_or_else(|e| e.into_inner())) +fn mapping_overlaps(a: MappingRange, b: MappingRange) -> bool { + match (a, b) { + (Some((a, b)), Some((c, d))) => a < d && c < b, + _ => true, + } +} +#[derive(Default)] +struct MappingState { + next: u64, + readers: Vec<(u64, MappingRange)>, + // Pending and active writers share one ordered queue. New readers wait + // behind an overlapping queued writer to prevent writer starvation. + writers: Vec<(u64, MappingRange, bool)>, +} +static MAPPING: std::sync::Mutex = std::sync::Mutex::new(MappingState { + next: 0, readers: Vec::new(), writers: Vec::new(), +}); +static MAPPING_MOVED: std::sync::Condvar = std::sync::Condvar::new(); +fn mapping_state() -> std::sync::MutexGuard<'static, MappingState> { + MAPPING.lock().unwrap_or_else(|e| e.into_inner()) +} +fn mapping_id(state: &mut MappingState) -> u64 { + let id = state.next; + state.next = id.checked_add(1).expect("mapping guard identifiers exhausted"); + id } -/// [`title_memory_reading`] if it can be had at once: `None` while the -/// runtime changes a mapping (or waits to), for a caller that has another -/// way to read and would rather not wait. -pub fn title_memory_reading_now() -> Option { - match MAPPING.try_read() { - Ok(guard) => Some(Reading(guard)), - Err(std::sync::TryLockError::Poisoned(e)) => Some(Reading(e.into_inner())), - Err(std::sync::TryLockError::WouldBlock) => None, +/// Guard for a mapping change. Dropping it wakes overlapping waiters. +pub struct Remapping(u64); +impl Drop for Remapping { + fn drop(&mut self) { + let mut state = mapping_state(); + state.writers.retain(|writer| writer.0 != self.0); + MAPPING_MOVED.notify_all(); } } +fn mapping_write(range: MappingRange) -> Remapping { + let mut state = mapping_state(); + let id = mapping_id(&mut state); + state.writers.push((id, range, false)); + loop { + let busy_readers = state.readers.iter().any(|&(_, other)| mapping_overlaps(range, other)); + let busy_writers = state.writers.iter().any(|&(other_id, other, active)| { + other_id != id && (active || other_id < id) && mapping_overlaps(range, other) + }); + if !busy_readers && !busy_writers { break; } + state = MAPPING_MOVED.wait(state).unwrap_or_else(|e| e.into_inner()); + } + state.writers.iter_mut().find(|writer| writer.0 == id).unwrap().2 = true; + Remapping(id) +} +/// Excludes all in-place readers during a mapping change of unknown extent. +pub fn title_memory_remapping() -> Remapping { mapping_write(None) } +/// Excludes readers overlapping the full range affected by a mapping change. +pub fn title_memory_remapping_range(at: usize, len: usize) -> Remapping { + mapping_write(mapping_range(at, len)) +} +/// Guard for an in-place title-memory read. +pub struct Reading(u64); +impl Drop for Reading { + fn drop(&mut self) { + let mut state = mapping_state(); + state.readers.retain(|reader| reader.0 != self.0); + if !state.writers.is_empty() { + MAPPING_MOVED.notify_all(); + } + } +} +fn mapping_read(range: MappingRange, wait: bool) -> Option { + let mut state = if wait { + mapping_state() + } else { + match MAPPING.try_lock() { + Ok(state) => state, + Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(), + Err(std::sync::TryLockError::WouldBlock) => return None, + } + }; + while state.writers.iter().any(|&(_, other, _)| mapping_overlaps(range, other)) { + if !wait { return None; } + state = MAPPING_MOVED.wait(state).unwrap_or_else(|e| e.into_inner()); + } + let id = mapping_id(&mut state); + state.readers.push((id, range)); + Some(Reading(id)) +} +/// Global read, for callers whose read extent is not known. Do not acquire +/// a second read or map title memory while a read guard is held. +pub fn title_memory_reading() -> Reading { mapping_read(None, true).unwrap() } +/// Nonblocking global read. +pub fn title_memory_reading_now() -> Option { mapping_read(None, false) } +/// Read only the given range; overflow falls back to global exclusion. +pub fn title_memory_reading_range(at: usize, len: usize) -> Reading { + mapping_read(mapping_range(at, len), true).unwrap() +} +/// Nonblocking version of the range read. +pub fn title_memory_reading_range_now(at: usize, len: usize) -> Option { + mapping_read(mapping_range(at, len), false) +} +#[cfg(test)] +fn mapping_writer_pending_for_test(at: usize, len: usize) -> bool { + let range = mapping_range(at, len); + mapping_state().writers.iter().any(|&(_, other, active)| !active && mapping_overlaps(range, other)) +} /// The runtime mapped or unmapped title memory `[at, at + len)`: what it /// shows is new and read-write, so it is no longer guarded (nor known /// unmapped). @@ -1063,11 +1138,53 @@ mod mapping_tests { use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, mpsc}; use std::time::Duration; + static SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + #[test] + fn disjoint_reader_can_run_while_a_range_is_remapped() { + let _serial = SERIAL.lock().unwrap(); + let _writer = super::title_memory_remapping_range(0x10000, 0x1000); + assert!(super::title_memory_reading_range_now(0x20000, 0x1000).is_some()); + assert!(super::title_memory_reading_range_now(0x10800, 1).is_none()); + assert!(super::title_memory_reading_now().is_none()); + assert!(super::title_memory_reading_range_now(usize::MAX, 2).is_none()); + } + + #[test] + fn nonblocking_read_does_not_wait_for_registry_mutex() { + let _serial = SERIAL.lock().unwrap(); + let _registry = super::mapping_state(); + assert!(super::title_memory_reading_range_now(0x20000, 0x1000).is_none()); + assert!(super::title_memory_reading_now().is_none()); + } + + #[test] + fn waiting_overlapping_writer_prevents_new_readers() { + let _serial = SERIAL.lock().unwrap(); + let reader = super::title_memory_reading_range(0x30000, 0x1000); + let (started, wait) = mpsc::channel(); + let thread = std::thread::spawn(move || { + started.send(()).unwrap(); + let _writer = super::title_memory_remapping_range(0x30000, 0x1000); + }); + wait.recv().unwrap(); + // Wait for the writer to register, rather than relying on scheduling. + let deadline = std::time::Instant::now() + Duration::from_secs(3); + while !super::mapping_writer_pending_for_test(0x30000, 0x1000) { + assert!(std::time::Instant::now() < deadline, "writer failed to register"); + std::thread::yield_now(); + } + assert!(super::title_memory_reading_range_now(0x30000, 1).is_none()); + assert!(super::title_memory_reading_range_now(0x40000, 1).is_some()); + drop(reader); + thread.join().unwrap(); + } /// A change of mapping waits for a read in place that is under way, and /// a read in place waits for a change of mapping. #[test] fn reads_in_place_and_remappings_wait_for_each_other() { + let _serial = SERIAL.lock().unwrap(); // A reader holds; the change does not begin until it lets go. let changed = Arc::new(AtomicBool::new(false)); let reading = title_memory_reading(); diff --git a/crates/victory-hle/src/modules/xmem.rs b/crates/victory-hle/src/modules/xmem.rs index 82e14ef3d0..e57c9f8c43 100644 --- a/crates/victory-hle/src/modules/xmem.rs +++ b/crates/victory-hle/src/modules/xmem.rs @@ -603,7 +603,9 @@ pub extern "system" fn XMemMapPhysicalPages( // maps what stays of them again, and a mapped view is a reservation // until it is committed. A read of title memory in place must not fall // into that gap (`victory_gpu::title_memory_remapping`). - let _changing = victory_gpu::title_memory_remapping(); + let _changing = n.checked_mul(PAGE) + .and_then(|len| mapping_extent(&physical, base, len)) + .map_or_else(victory_gpu::title_memory_remapping, |(at, len)| victory_gpu::title_memory_remapping_range(at, len)); unmap(&mut physical, base, n * PAGE); if pages.is_null() { return address; @@ -672,7 +674,8 @@ pub fn map_alias(to: u64, from: u64, len: u64) -> bool { let (to, from, len) = (to as usize, from as usize, len as usize); let mut physical = physical(); // As in `XMemMapPhysicalPages`: no read in place meets the gap. - let _changing = victory_gpu::title_memory_remapping(); + let _changing = mapping_extent(&physical, to, len) + .map_or_else(victory_gpu::title_memory_remapping, |(at, len)| victory_gpu::title_memory_remapping_range(at, len)); unmap(&mut physical, to, len); for k in (0..len).step_by(PAGE) { if from == 0 { @@ -792,6 +795,28 @@ fn commit_limit_message( message } +/// The full views `unmap` temporarily takes away, including their parts +/// outside the requested range. Overflow or an empty range stays global. +fn mapping_extent(physical: &Physical, base: usize, len: usize) -> Option<(usize, usize)> { + if len == 0 || !base.is_multiple_of(PAGE) || !len.is_multiple_of(PAGE) { return None; } + let end = base.checked_add(len)?; + let mut low = base; + let mut high = end; + if let Some((&at, &(size, _))) = physical.views.range(..=base).next_back() { + let view_end = at.checked_add(size)?; + if view_end > base { + low = at; + high = high.max(view_end); + } + } + // Views do not overlap, so only the last view starting inside the + // request can extend beyond its end. + if let Some((&at, &(size, _))) = physical.views.range(base..end).next_back() { + high = high.max(at.checked_add(size)?); + } + Some((low, high.checked_sub(low)?)) +} + /// Returns `[base, base + len)` to placeholders; views reaching past it /// are mapped again outside it. fn unmap(physical: &mut Physical, base: usize, len: usize) { @@ -1039,6 +1064,52 @@ mod tests { assert_eq!(before.title_limit - after.title_limit, 128 * MIB); } + #[test] + fn mapping_extent_includes_the_entire_old_view() { + let mut pages = [0usize; 3]; + let mut n = pages.len(); + assert_eq!(XMemAllocatePhysicalPages(0, &mut n, pages.as_mut_ptr()), 1); + let base = XMemVirtualAlloc(std::ptr::null_mut(), 3 * PAGE, MEM_RESERVE | PAGES_64K, 0, PAGE_READWRITE); + assert!(!base.is_null()); + assert_eq!(XMemMapPhysicalPages(base, 3, pages.as_ptr()), base); + let extent = mapping_extent(&physical(), base as usize + PAGE, PAGE); + assert_eq!(extent, Some((base as usize, 3 * PAGE))); + assert_eq!(mapping_extent(&physical(), base as usize + 1, PAGE), None); + assert_eq!(mapping_extent(&physical(), base as usize, PAGE - 1), None); + assert_eq!(mapping_extent(&physical(), usize::MAX - PAGE + 1, PAGE), None); + assert_eq!(XMemMapPhysicalPages(base, 3, std::ptr::null()), base); + assert_eq!(XMemFreePhysicalPages(3, pages.as_ptr()), 1); + } + + #[test] + fn remapping_middle_waits_for_reader_of_old_view_edge() { + use std::sync::mpsc; + use std::time::Duration; + let mut pages = [0usize; 3]; + let mut n = pages.len(); + assert_eq!(XMemAllocatePhysicalPages(0, &mut n, pages.as_mut_ptr()), 1); + let base = XMemVirtualAlloc(std::ptr::null_mut(), 3 * PAGE, MEM_RESERVE | PAGES_64K, 0, PAGE_READWRITE); + assert!(!base.is_null()); + assert_eq!(XMemMapPhysicalPages(base, 3, pages.as_ptr()), base); + let reader = victory_gpu::title_memory_reading_range(base as usize, 1); + let (started, wait_started) = mpsc::channel(); + let (finished, wait_finished) = mpsc::channel(); + let middle = base as usize + PAGE; + let page = pages[1]; + let writer = std::thread::spawn(move || { + started.send(()).unwrap(); + let result = XMemMapPhysicalPages(middle as *mut c_void, 1, &page); + finished.send(result as usize).unwrap(); + }); + wait_started.recv().unwrap(); + assert!(wait_finished.recv_timeout(Duration::from_millis(100)).is_err()); + drop(reader); + assert_eq!(wait_finished.recv_timeout(Duration::from_secs(3)).unwrap(), middle); + writer.join().unwrap(); + assert_eq!(XMemMapPhysicalPages(base, 3, std::ptr::null()), base); + assert_eq!(XMemFreePhysicalPages(3, pages.as_ptr()), 1); + } + #[test] fn statistics_have_the_console_layout() { assert_eq!(size_of::(), 0x50); @@ -1173,7 +1244,7 @@ mod tests { std::thread::spawn(move || { let mut reads = 0u64; while !stop.load(Ordering::Relaxed) { - let _reading = victory_gpu::title_memory_reading(); + let _reading = victory_gpu::title_memory_reading_range(first, 1); // SAFETY: the page is mapped whenever no change of // mapping is under way, and none is while `_reading` // is held.