Merge agent/textures: lazy pool views (VICTORY_LAZY_VIEWS, off by default); mapping takes the remapping lock first

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
readmet3xt-labandClaude Opus 5.5 committed 2026-11-26 19:28:26 +05:30
commit 72a546b0f2
5 files changed
+408 -15

No files matched your search

+7 -1
View File
@@ -8287,7 +8287,9 @@ pub(crate) fn copy_mapped(dst: *mut u8, src: *const u8, len: usize, title_dst: b
return;
}
if title_dst {
// The kernel writes the destination: it must not be guarded.
// The kernel writes the destination: it must not be guarded, and it
// must be a mapping of the host's.
crate::watch::materialize(dst as usize, len);
crate::watch::writing(dst as usize, len);
}
#[cfg(feature = "diagnostics")]
@@ -8440,6 +8442,9 @@ pub(crate) fn read_there(src: *const u8, dst: &mut [u8]) -> usize {
fn read(src: *const u8, dst: *mut u8, len: usize) -> bool {
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
// (The kernel's read does not fault into the handler that makes lazily
// mapped title memory real.)
crate::watch::materialize(src as usize, len);
if crate::safecopy::on() {
// SAFETY: the destination is mapped writable memory of `len` bytes;
// a source page that is not there fails the copy.
@@ -8481,6 +8486,7 @@ fn direct_writes() -> bool {
pub(crate) fn write_mapped(dst: *mut u8, src: *const u8, len: usize) {
use windows_sys::Win32::System::Diagnostics::Debug::WriteProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
crate::watch::materialize(dst as usize, len);
let direct = direct_writes();
let since = crate::watch::changes();
let write = |at: usize, n: usize| {
+1 -1
View File
@@ -116,6 +116,6 @@ pub use shadow::title_memory_write_combined;
#[cfg(windows)]
pub use watch::{
kernel_writing, title_memory_allocated, title_memory_freed, title_memory_reading, title_memory_remapped,
title_memory_remapping, title_memory_splitting, title_memory_unmapping, title_memory_written,
title_memory_remapping, title_memory_splitting, title_memory_unmapping, title_memory_written, use_lazy_views,
writing as kernel_will_write,
};
+3
View File
@@ -1753,6 +1753,9 @@ pub(crate) fn staged(resource: This, ask: bool) -> Option<(u64, bool)> {
/// Whether `[at, at + len)` is committed memory (streamed textures may be
/// unmapped until their pages arrive).
pub(crate) fn committed(at: *const u8, len: usize) -> bool {
// Memory the runtime mapped lazily is committed as far as the title
// goes: made real before Windows is asked.
crate::watch::materialize(at as usize, len);
if by_query() {
return committed_by_query(at, len);
}
+38
View File
@@ -659,6 +659,11 @@ pub(crate) fn asked(start: usize, end: usize) -> Option<(u64, Vec<(usize, u64)>)
/// kernel just reported. `None` if the memory is not write-watched (nor,
/// with `guarded`, view memory, which is then guarded).
pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Vec<(usize, u64)>)> {
// Guarding asks Windows about the pages and protects them: a lazy view
// is none to it yet.
if guarded {
materialize(start, end.saturating_sub(start));
}
let (first, last) = (start / PAGE, end.div_ceil(PAGE));
let of = |j: &Journal| -> Vec<(usize, u64)> { j.written.range(first..last).map(|(&p, &w)| (p, w)).collect() };
// Asked ahead with the submission's other ranges: the journal has it.
@@ -2065,6 +2070,8 @@ pub struct KernelWrite(usize, usize);
/// write fail (the kernel does not fault into our handler): a read cut
/// short, which the title takes for what the file holds.
pub fn kernel_writing(at: usize, len: usize) -> KernelWrite {
// The kernel's write does not fault into the handler.
materialize(at, len);
let (first, last) = (at / BLOCK, at.saturating_add(len).div_ceil(BLOCK));
{
let mut g = guards_timed();
@@ -2555,6 +2562,29 @@ fn title_memory_changed(at: usize, len: usize) -> std::sync::MutexGuard<'static,
g
}
/// The runtime's way to make title memory it mapped only in its books
/// real (`xmem`, `VICTORY_LAZY_VIEWS=1`): `(address, bytes)` makes every
/// such view that overlaps the range a mapping of the host's, and says
/// whether there was one (and all of them could be made).
static LAZY_VIEWS: std::sync::OnceLock<fn(usize, usize) -> bool> = std::sync::OnceLock::new();
/// The runtime maps title memory lazily: views it tells of (as mapped, by
/// [`title_memory_remapped`] and `title_memory_view`) may be no mapping of
/// the host's until something touches them. `materialize` makes them one.
pub fn use_lazy_views(materialize: fn(usize, usize) -> bool) {
let _ = LAZY_VIEWS.set(materialize);
}
/// Makes title memory `[at, at + len)` that the runtime mapped lazily a
/// mapping of the host's ([`use_lazy_views`]); whether any of it was lazy.
/// Called before every touch of title memory the fault handler below does
/// not see: reads and writes through the kernel, protection changes, and
/// questions to Windows about the pages. (A fault in title memory, the
/// title's or ours, comes to [`install`]'s handler, which asks first.)
pub(crate) fn materialize(at: usize, len: usize) -> bool {
LAZY_VIEWS.get().is_some_and(|f| f(at, len.max(1)))
}
/// Registers the handler that takes writes to guarded blocks, once, ahead
/// of the crash reporter's.
pub(crate) fn install() {
@@ -2579,6 +2609,14 @@ pub(crate) fn install() {
unsafe { (*context).Rip = exit as u64 };
EXCEPTION_CONTINUE_EXECUTION
};
// Memory the runtime mapped lazily: mapped now, and the access is
// made again (any access, the title's or ours).
if r.ExceptionCode == EXCEPTION_ACCESS_VIOLATION as NTSTATUS
&& r.NumberParameters >= 2
&& materialize(r.ExceptionInformation[1], 1)
{
return EXCEPTION_CONTINUE_EXECUTION;
}
if r.ExceptionCode != EXCEPTION_ACCESS_VIOLATION as NTSTATUS
|| r.NumberParameters < 2
|| r.ExceptionInformation[0] != WRITE