From 87cd5f064a0948a34e4587cc4a6b717adc1afc8e Mon Sep 17 00:00:00 2001 From: stoltembergg Date: Thu, 26 Nov 2026 06:52:57 +0000 Subject: [PATCH 1/2] GPU: copy title memory in place by default Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- README.md | 8 ++++---- crates/victory-gpu/src/plaincopy.rs | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b70d7ac..04fa578 100644 --- a/README.md +++ b/README.md @@ -219,10 +219,10 @@ What did it, on top of the list below: sync looks at a changed page's records under one lock, fingerprint first; guarded view memory is left out of a submission's write-watch askings ahead (after triippywhale). - - `VICTORY_COPY_PLAIN=1`: copies of title memory made in place by this - process, with a handler that falls back to the kernel's copy on a - missing page. Its first form also took the layer's one-byte "is this - page there" reads, 1,000 to 1,300 faults a frame, and won nothing; + - Copies of title memory are made in place by this process by default + (`VICTORY_COPY_PLAIN=0` restores the kernel's copy), with a handler + that falls back to the kernel's copy on a missing page. Its first + form also took the layer's one-byte "is this page there" reads, 1,000 to 1,300 faults a frame, and won nothing; reads under 256 bytes now stay with the kernel and a source that faulted is left alone for two seconds (4 to 9 faults a frame). The submit thread's kernel time fell from 44 to 20 ms a frame; the diff --git a/crates/victory-gpu/src/plaincopy.rs b/crates/victory-gpu/src/plaincopy.rs index c649099..8a09714 100644 --- a/crates/victory-gpu/src/plaincopy.rs +++ b/crates/victory-gpu/src/plaincopy.rs @@ -1,5 +1,5 @@ //! Copies of title memory made in place, by this process, with a way back -//! when a page is not there (`VICTORY_COPY_PLAIN=1`; off by default). +//! when a page is not there (on by default; `VICTORY_COPY_PLAIN=0` turns it off). //! //! Every copy of title memory into a host buffer goes through the kernel //! (`ReadProcessMemory` on our own process, `crate::host::copy_mapped`): @@ -55,16 +55,16 @@ unsafe extern "C" { } /// Whether copies of title memory into host memory are made in place -/// (`VICTORY_COPY_PLAIN=1`). +/// (on by default; `VICTORY_COPY_PLAIN=0` turns them off). pub(crate) fn on() -> bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); *ON.get_or_init(|| { - let on = cfg!(target_arch = "x86_64") && std::env::var_os("VICTORY_COPY_PLAIN").is_some_and(|v| v == "1"); + let on = cfg!(target_arch = "x86_64") && std::env::var_os("VICTORY_COPY_PLAIN").is_none_or(|v| v != "0"); if on { install(); tracing::info!( "copies of title memory into host buffers are made in place, with the kernel's copy behind them for a \ - page that is not there (VICTORY_COPY_PLAIN=1)" + page that is not there (VICTORY_COPY_PLAIN=0 turns them off)" ); } on @@ -229,7 +229,7 @@ pub(crate) fn line(frames: f64) { let [copies, bytes, gone, lately, small] = [0, 1, 2, 3, 4].map(|i| COUNT[i].swap(0, Relaxed) as f64 / frames); tracing::info!( - "copies of title memory made in place per frame (VICTORY_COPY_PLAIN=1): {copies:.0}, {:.1} MB; {gone:.1} met a page \ + "copies of title memory made in place per frame (VICTORY_COPY_PLAIN=0 turns them off): {copies:.0}, {:.1} MB; {gone:.1} met a page \ that was not there and went through the kernel's copy; left to the kernel's copy untried: {lately:.0} whose source \ faulted in the last two seconds, {small:.0} of under {LEAST} bytes", bytes / 1e6 From 8b355d2c876a3f3aecca5643a625457c9bcab73e Mon Sep 17 00:00:00 2001 From: stoltembergg Date: Thu, 26 Nov 2026 06:55:34 +0000 Subject: [PATCH 2/2] GPU: fall back when copy guard registration fails Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/victory-gpu/src/plaincopy.rs | 34 ++++++++++++++++++----------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/crates/victory-gpu/src/plaincopy.rs b/crates/victory-gpu/src/plaincopy.rs index 8a09714..ee250ba 100644 --- a/crates/victory-gpu/src/plaincopy.rs +++ b/crates/victory-gpu/src/plaincopy.rs @@ -60,14 +60,20 @@ pub(crate) fn on() -> bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); *ON.get_or_init(|| { let on = cfg!(target_arch = "x86_64") && std::env::var_os("VICTORY_COPY_PLAIN").is_none_or(|v| v != "0"); - if on { - install(); - tracing::info!( - "copies of title memory into host buffers are made in place, with the kernel's copy behind them for a \ - page that is not there (VICTORY_COPY_PLAIN=0 turns them off)" + if !on { + return false; + } + if !install() { + tracing::warn!( + "no vectored handler for copies of title memory in place; the kernel's copy is used" ); + return false; } - on + tracing::info!( + "copies of title memory into host buffers are made in place, with the kernel's copy behind them for a \ + page that is not there (VICTORY_COPY_PLAIN=0 turns them off)" + ); + true }) } @@ -140,7 +146,7 @@ fn note_fault(from: usize, len: usize) { /// The handler that turns a read fault inside [`guarded`] into its return /// of false; put first in the line, once. #[cfg(target_arch = "x86_64")] -fn install() { +fn install() -> bool { use windows_sys::Win32::Foundation::{ EXCEPTION_ACCESS_VIOLATION, EXCEPTION_GUARD_PAGE, EXCEPTION_IN_PAGE_ERROR, }; @@ -167,15 +173,15 @@ fn install() { } EXCEPTION_CONTINUE_SEARCH } - static ONCE: std::sync::Once = std::sync::Once::new(); + static INSTALLED: std::sync::OnceLock = std::sync::OnceLock::new(); // SAFETY: a handler that reads the record it is given and nothing else. - ONCE.call_once(|| unsafe { - AddVectoredExceptionHandler(1, Some(on_fault)); - }); + *INSTALLED.get_or_init(|| unsafe { !AddVectoredExceptionHandler(1, Some(on_fault)).is_null() }) } #[cfg(not(target_arch = "x86_64"))] -fn install() {} +fn install() -> bool { + false +} /// Copies `len` bytes from `from` to `to` in place; false if a page of the /// source was not there (then some of `to` may have been written). @@ -184,7 +190,9 @@ fn install() {} /// `from` may be anything. #[cfg(target_arch = "x86_64")] fn guarded(to: *mut u8, from: *const u8, len: usize) -> bool { - install(); + if !install() { + return false; + } // SAFETY: the destination is the caller's to write; a source page that // is not there ends the copy with 0 instead of a fault ([`install`]). unsafe { victory_copy_guarded(to, from, len) != 0 }