From f5bde6b5a80925491749429a9cfb6e9d386ee1b4 Mon Sep 17 00:00:00 2001 From: henriazz10 <100179000+henriazz10@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:27:36 -0300 Subject: [PATCH 1/4] victory-gpu: unlock write-fault opening by default and hoist soft_runs --- crates/victory-gpu/src/watch.rs | 41 ++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index 151de0903..1a7f7accd 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -1070,8 +1070,8 @@ static OPENED: Mutex> = Mutex::new(Vec::new()); static TO_SETTLE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); /// Whether a write fault opens a guarded block without the guards' lock -/// (`VICTORY_FAULT_UNLOCKED=1`; off unless asked for, or flipped while the -/// title runs by `VICTORY_AB`). +/// (`VICTORY_FAULT_UNLOCKED=0` turns it off; on by default, or flipped while +/// the title runs by `VICTORY_AB`). /// /// Under the lock the title's threads waited for it more than they did /// anything else in a fault: of 281 ms in the handler in 14 frames of play @@ -1085,12 +1085,17 @@ static TO_SETTLE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsiz /// block another thread is opening waits for that one (100 microseconds at /// most), and one with every view of its page to open /// (`VICTORY_OPEN_ONE_VIEW=0`) goes the locked way. +fn unlocked_from_env(val: Option<&std::ffi::OsStr>) -> bool { + val.is_none_or(|v| v != "0") +} + fn unlocked() -> bool { #[cfg(test)] if UNLOCKED_IN_TEST.get() { return true; } - crate::asked_for!("VICTORY_FAULT_UNLOCKED") + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| unlocked_from_env(std::env::var_os("VICTORY_FAULT_UNLOCKED").as_deref())) } #[cfg(test)] @@ -2014,6 +2019,7 @@ fn guard(start: usize, end: usize) -> Option> { came.push((block, is_view(block * BLOCK, (block + 1) * BLOCK))); } } + let soft = soft_runs(first * BLOCK, (last - first) * BLOCK); let rest = rest(); let mut g = guards_timed(); #[cfg(feature = "diagnostics")] @@ -2036,7 +2042,7 @@ fn guard(start: usize, end: usize) -> Option> { if let Some((from, to)) = run && to != block { - protect(&mut g, from, to); + protect(&mut g, from, to, &soft); run = None; // Faults of the title's wait for the lock: each block is done on // its own, and looked at anew below. @@ -2048,7 +2054,7 @@ fn guard(start: usize, end: usize) -> Option> { // is (`OPENING`). if unsettled(block, block + 1) { if let Some((from, to)) = run.take() { - protect(&mut g, from, to); + protect(&mut g, from, to, &soft); } continue; } @@ -2061,7 +2067,7 @@ fn guard(start: usize, end: usize) -> Option> { // because one could not be made so. (Or clean, and bytes were put // in beside the guard: it stays.) if let Some((from, to)) = run.take() { - protect(&mut g, from, to); + protect(&mut g, from, to, &soft); } if g.clean.contains(block) { continue; @@ -2072,11 +2078,11 @@ fn guard(start: usize, end: usize) -> Option> { } else if !g.foreign.contains(&block) && !g.left_alone(block) && !g.rests(block, rest) { run = Some((run.map_or(block, |r| r.0), block + 1)); } else if let Some((from, to)) = run.take() { - protect(&mut g, from, to); + protect(&mut g, from, to, &soft); } } if let Some((from, to)) = run { - protect(&mut g, from, to); + protect(&mut g, from, to, &soft); } drop(g); #[cfg(feature = "diagnostics")] @@ -2134,10 +2140,10 @@ fn is_view(start: usize, end: usize) -> bool { /// at every other view of them are made read-only too: a write through /// one of those is this memory written, and only a fault can say so. /// `VICTORY_VIEW_GUARD=0` leaves the other views as they are, as before. -fn protect(g: &mut Guards, from: usize, to: usize) { +fn protect(g: &mut Guards, from: usize, to: usize, soft: &[(usize, usize)]) { #[cfg(feature = "diagnostics")] crate::faultbook::note(crate::faultbook::What::Taken, from, to, 0); - protect_here(g, from, to); + protect_here(g, from, to, soft); cover(g, from, to); } @@ -2249,7 +2255,7 @@ fn protect_alias(g: &mut Guards, block: usize) -> bool { } /// [`protect`] for one range, without the other views of its pages. -fn protect_here(g: &mut Guards, from: usize, to: usize) { +fn protect_here(g: &mut Guards, from: usize, to: usize, soft: &[(usize, usize)]) { use windows_sys::Win32::System::Memory::{PAGE_READONLY, PAGE_READWRITE, VirtualProtect}; let set = |from: usize, to: usize| { let mut old = 0; @@ -2289,9 +2295,9 @@ fn protect_here(g: &mut Guards, from: usize, to: usize) { // and the block would count as not there. Nothing writes through one // before a fault makes it real, which opens it // ([`title_memory_made_real`]). - let soft = soft_runs(from * BLOCK, (to - from) * BLOCK); + let any_soft = soft.iter().any(|&(at, n)| at < to * BLOCK && at + n > from * BLOCK); let in_books = |block: usize| soft.iter().any(|&(at, n)| at <= block * BLOCK && (block + 1) * BLOCK <= at + n); - if soft.is_empty() && set(from, to) { + if !any_soft && set(from, to) { note(g, from, to); return; } @@ -5281,4 +5287,13 @@ mod many_askers_tests { assert!(!BITS.any(0, 1 << 31)); assert!(!BITS.swap_on(70) && BITS.swap_on(70) && BITS.all(70, 71)); } + + #[test] + fn unlocked_defaults_on_unless_zero() { + use std::ffi::OsStr; + assert!(super::unlocked_from_env(None)); + assert!(super::unlocked_from_env(Some(OsStr::new("")))); + assert!(super::unlocked_from_env(Some(OsStr::new("1")))); + assert!(!super::unlocked_from_env(Some(OsStr::new("0")))); + } } From e29efec11d703e1333e48d99b5129feae8b93d85 Mon Sep 17 00:00:00 2001 From: henriazz10 <100179000+henriazz10@users.noreply.github.com> Date: Fri, 27 Nov 2026 23:46:50 -0300 Subject: [PATCH 2/4] victory-gpu: read soft runs under the guards lock; FAULT_UNLOCKED via toggled! Review of #516 (Machad3x): guard() read the soft-view runs before taking GUARDS. A view made real in between (title_memory_made_real opens it under that lock) was still taken as soft, so the block stayed guarded and clean with no protection on it, and writes through it went unseen. The runs are now read once, after the lock is taken (as protect_here did before #516, under the same lock). VICTORY_FAULT_UNLOCKED goes through crate::toggled! again: same rule (on unless "0"), and VICTORY_AB can flip it in diagnostics builds. The helper unlocked_from_env and its test, both added in #516, go with it. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/watch.rs | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index 1a7f7accd..b7ac8a0f0 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -1085,17 +1085,12 @@ static TO_SETTLE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsiz /// block another thread is opening waits for that one (100 microseconds at /// most), and one with every view of its page to open /// (`VICTORY_OPEN_ONE_VIEW=0`) goes the locked way. -fn unlocked_from_env(val: Option<&std::ffi::OsStr>) -> bool { - val.is_none_or(|v| v != "0") -} - fn unlocked() -> bool { #[cfg(test)] if UNLOCKED_IN_TEST.get() { return true; } - static ON: std::sync::OnceLock = std::sync::OnceLock::new(); - *ON.get_or_init(|| unlocked_from_env(std::env::var_os("VICTORY_FAULT_UNLOCKED").as_deref())) + crate::toggled!("VICTORY_FAULT_UNLOCKED") } #[cfg(test)] @@ -2019,9 +2014,11 @@ fn guard(start: usize, end: usize) -> Option> { came.push((block, is_view(block * BLOCK, (block + 1) * BLOCK))); } } - let soft = soft_runs(first * BLOCK, (last - first) * BLOCK); let rest = rest(); let mut g = guards_timed(); + // Read under the lock: a view made real meanwhile (`title_memory_made_real`, which + // opens it under this lock) must not be taken as still soft, guarded with no protection. + let soft = soft_runs(first * BLOCK, (last - first) * BLOCK); #[cfg(feature = "diagnostics")] tally(4, 1); for &(block, view) in &came { @@ -5287,13 +5284,4 @@ mod many_askers_tests { assert!(!BITS.any(0, 1 << 31)); assert!(!BITS.swap_on(70) && BITS.swap_on(70) && BITS.all(70, 71)); } - - #[test] - fn unlocked_defaults_on_unless_zero() { - use std::ffi::OsStr; - assert!(super::unlocked_from_env(None)); - assert!(super::unlocked_from_env(Some(OsStr::new("")))); - assert!(super::unlocked_from_env(Some(OsStr::new("1")))); - assert!(!super::unlocked_from_env(Some(OsStr::new("0")))); - } } From 843f9de627d8a31f9daf09fabea15db05912fa5f Mon Sep 17 00:00:00 2001 From: "GANESH.G R" <343559437+GRG-DA@users.noreply.github.com> Date: Sat, 28 Nov 2026 13:03:33 +0530 Subject: [PATCH 3/4] Watch: a late lock-free fault no longer undoes the guard taken since A write fault that checked PROTECTED, and reached open_unlocked only after another fault had opened the block and that was settled, found the block writable already but queued its opening anyway. A refresh that had looked at the block before guarded it again (read-only, protected, clean); the next settle then took the stale entry, removed the block from the guards and cleared PROTECTED while the memory stayed read-only. The title's next write there was an access violation no handler took. open_unlocked records took = !(done && old == PAGE_READWRITE): whether that fault's own VirtualProtect made the block writable. settle leaves an entry that did not take the block alone, apart from its OPENING and TOLD bits, so the guard taken since survives. Test a_late_fault_leaves_the_guard_taken_since. Reported in #817 by @Machad3x, who published the fix as commit 3175c5dc in Machad3x/VIctory-nvidia-RTX. Ported onto current main with a longer test. (cherry picked from commit 48afe3e8d22ffed9470a9b8ed9ab2953dee79dec) --- crates/victory-gpu/src/watch.rs | 78 +++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index b7ac8a0f0..421ebe514 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -1063,6 +1063,9 @@ struct Opened { at: std::time::Instant, others: Vec, hot: Option, + /// The fault made the block writable; false when another fault opened it + /// and it was settled in between, so a newer guard must be kept. + took: bool, } /// Those whose notes are not settled yet ([`settle`]), and how many. @@ -1144,6 +1147,9 @@ fn open_unlocked(block: usize) -> bool { let mut old = 0; // SAFETY: a block we made read-only. let mut done = unsafe { VirtualProtect((block * BLOCK) as *const _, BLOCK, PAGE_READWRITE, &mut old) } != 0; + // `old == PAGE_READWRITE`: another fault had already made it writable, + // and this one found it so; a guard taken since must be kept. + let took = !(done && old == PAGE_READWRITE); if done && !matches!(old, PAGE_READONLY | PAGE_READWRITE) { // Not what we left there (as in `unguard`): other memory's, which // keeps its protection. @@ -1156,7 +1162,7 @@ fn open_unlocked(block: usize) -> bool { // After the protection: who settles it may let the block be guarded again. { let mut opened = OPENED.lock().unwrap_or_else(|e| e.into_inner()); - opened.push(Opened { block, at, others, hot: hot() }); + opened.push(Opened { block, at, others, hot: hot(), took }); TO_SETTLE.store(opened.len(), SeqCst); } #[cfg(feature = "diagnostics")] @@ -1186,7 +1192,18 @@ fn settle(g: &mut Guards) { TO_SETTLE.store(0, SeqCst); std::mem::take(&mut *opened) }; - for Opened { block, at, mut others, hot } in opened { + for Opened { block, at, mut others, hot, took } in opened { + if !took { + // Another fault opened this block and was settled in between, so + // a guard taken since is still the one that owns the memory. + // Leave it, its notes and PUBLISHED alone: clear only this + // entry's bits, or `unsettled` would stay true for ever. + for &other in &others { + TOLD.clear(other); + } + OPENING.clear(block); + continue; + } if g.aliased.contains(block) { if others.is_empty() { // Guarded with its other views after the fault looked: they @@ -4653,6 +4670,61 @@ mod tests { drop_views(section, &[one, two], BLOCK); } + /// A late entry of a fault that found the block already writable must not + /// undo a guard taken since: the block stays read-only with its PROTECTED + /// bit and clean notes, and the title's next write still faults into the + /// handler and is reported at the next asking. + #[test] + fn a_late_fault_leaves_the_guard_taken_since() { + let (section, one, two) = two_views(BLOCK); + let block = |view: usize| view / BLOCK; + assert_eq!(guard(one, one + BLOCK), Some(vec![block(one)])); + assert_eq!(guard(two, two + BLOCK), Some(vec![block(two)])); + assert!(!writable(one) && !writable(two)); + { + let g = guards(); + assert!(g.protected.contains(block(one)) && g.protected.contains(block(two))); + assert!(g.clean.contains(block(one)) && g.clean.contains(block(two))); + } + assert!(PROTECTED.any(block(one), block(one) + 1) && PROTECTED.any(block(two), block(two) + 1)); + assert!(!OPENING.any(block(two), block(two) + 1) && !unsettled(block(two), block(two) + 1)); + + // The entry of a fault that found the block already writable, settled + // after this guard was taken: its own VirtualProtect did not change + // the protection, so the memory is still read-only. + assert!(!OPENING.swap_on(block(two))); + { + let mut opened = OPENED.lock().unwrap(); + opened.push(Opened { block: block(two), at: std::time::Instant::now(), others: Vec::new(), hot: None, took: false }); + TO_SETTLE.store(opened.len(), std::sync::atomic::Ordering::SeqCst); + } + assert!(unsettled(block(two), block(two) + 1)); + assert!(!writable(two)); + { + // Whoever takes the lock next settles it: only its own bit goes. + let g = guards(); + assert!(g.protected.contains(block(two)) && g.clean.contains(block(two))); + assert!(PROTECTED.any(block(two), block(two) + 1)); + assert!(!OPENING.any(block(two), block(two) + 1) && !unsettled(block(two), block(two) + 1)); + } + assert!(!writable(one) && !writable(two), "the guard taken since is still there"); + + // The block is still ours: a genuine fault opens it, and the next + // asking reports it once and then nothing. + UNLOCKED_IN_TEST.set(true); + // SAFETY: the guarded view, opened by the handler. + unsafe { ((two + 16) as *mut u8).write(2) }; + UNLOCKED_IN_TEST.set(false); + assert!(writable(two)); + { + let g = guards(); + assert!(!g.protected.contains(block(two)) && !PROTECTED.any(block(two), block(two) + 1)); + } + assert_eq!(guard(one, one + BLOCK), Some(vec![block(one)])); + assert_eq!(guard(one, one + BLOCK), Some(Vec::new())); + drop_views(section, &[one, two], BLOCK); + } + /// What the holders of the lock do with a block a fault is opening /// without it, before that is settled: it is told as written and left as /// it is, and another view of its page is not counted read-only for it, @@ -4686,7 +4758,7 @@ mod tests { unsafe { windows_sys::Win32::System::Memory::VirtualProtect(two as *const _, BLOCK, PAGE_READWRITE, &mut old) }; { let mut opened = OPENED.lock().unwrap(); - opened.push(Opened { block: block(two), at: std::time::Instant::now(), others: vec![block(one)], hot: None }); + opened.push(Opened { block: block(two), at: std::time::Instant::now(), others: vec![block(one)], hot: None, took: true }); TO_SETTLE.store(opened.len(), SeqCst); } assert_eq!(guard(one, one + BLOCK), Some(vec![block(one)])); From 694b97dbcd808b4d7fd463a6167e2d35b15c3bd3 Mon Sep 17 00:00:00 2001 From: "GANESH.G R" <343559437+GRG-DA@users.noreply.github.com> Date: Sat, 28 Nov 2026 13:12:14 +0530 Subject: [PATCH 4/4] Watch: cover the TOLD path in the late-fault regression test The regression test queued a stale entry with no `others`, so the `TOLD.clear(other)` branch added by the fix was never exercised. Let the entry tell the other view as written, as `open_unlocked` does, and assert that after settling TOLD and OPENING are gone while both views keep their guard notes and PROTECTED bit. On the unfixed code the other view's notes are removed, so this also detects the bug on that path. (cherry picked from commit fd31efd1344ce1f6d436b2db3334c147de2e54d7) --- crates/victory-gpu/src/watch.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index 421ebe514..278e28b22 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -4673,7 +4673,8 @@ mod tests { /// A late entry of a fault that found the block already writable must not /// undo a guard taken since: the block stays read-only with its PROTECTED /// bit and clean notes, and the title's next write still faults into the - /// handler and is reported at the next asking. + /// handler and is reported at the next asking. The other view the fault + /// told as written keeps its guard notes too. #[test] fn a_late_fault_leaves_the_guard_taken_since() { let (section, one, two) = two_views(BLOCK); @@ -4691,20 +4692,32 @@ mod tests { // The entry of a fault that found the block already writable, settled // after this guard was taken: its own VirtualProtect did not change - // the protection, so the memory is still read-only. + // the protection, so the memory is still read-only. As `open_unlocked` + // does, tell the other view as written and mark both views beside a + // guard, clearing PUBLISHED, before queuing the entry. assert!(!OPENING.swap_on(block(two))); + TOLD.set(block(one)); + BESIDE.set(block(one)); + BESIDE.set(block(two)); + PUBLISHED.clear(block(one)); + PUBLISHED.clear(block(two)); { let mut opened = OPENED.lock().unwrap(); - opened.push(Opened { block: block(two), at: std::time::Instant::now(), others: Vec::new(), hot: None, took: false }); + opened.push(Opened { block: block(two), at: std::time::Instant::now(), others: vec![block(one)], hot: None, took: false }); TO_SETTLE.store(opened.len(), std::sync::atomic::Ordering::SeqCst); } + assert!(unsettled(block(one), block(one) + 1)); assert!(unsettled(block(two), block(two) + 1)); assert!(!writable(two)); { - // Whoever takes the lock next settles it: only its own bit goes. + // Whoever takes the lock next settles it: only this entry's bits + // go; the guard taken since keeps its notes at both views. let g = guards(); assert!(g.protected.contains(block(two)) && g.clean.contains(block(two))); - assert!(PROTECTED.any(block(two), block(two) + 1)); + assert!(g.protected.contains(block(one)) && g.clean.contains(block(one))); + assert!(PROTECTED.any(block(two), block(two) + 1) && PROTECTED.any(block(one), block(one) + 1)); + assert!(!TOLD.any(block(one), block(one) + 1)); + assert!(!unsettled(block(one), block(one) + 1)); assert!(!OPENING.any(block(two), block(two) + 1) && !unsettled(block(two), block(two) + 1)); } assert!(!writable(one) && !writable(two), "the guard taken since is still there");