Merge local/next (e6f492d) into local/tests

Blocks the kernel writes, or the runtime is unmapping, are no known holes
and not published; foreign blocks are never held; freed memory forgets its
known holes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 23:47:04 -06:00
commit cd37859b39
20 files changed
+3521 -181

No files matched your search

+129 -6
View File
@@ -382,6 +382,11 @@ pub(crate) fn card_dirty(sent_as: u64) -> Vec<(Arc<Shadow>, Vec<(usize, usize)>)
out
}
/// The copies in the card's memory (diagnostics: `crate::host::upcheck`).
pub(crate) fn carded_copies() -> Vec<Arc<Shadow>> {
CARDED.lock().unwrap_or_else(|e| e.into_inner()).iter().filter_map(std::sync::Weak::upgrade).collect()
}
/// Ranges in order, those that overlap or touch as one.
fn joined(mut ranges: Vec<(usize, usize)>) -> Vec<(usize, usize)> {
ranges.sort_unstable();
@@ -449,9 +454,16 @@ fn card_piece_of(start: usize, len: usize, span: &CardSpan, i: u32) -> (usize, u
(lo.max(span.from as usize), hi.min(span.to as usize))
}
/// The offsets [`Shadow::card_hashed`] gives, for a card's buffer of `size`
/// bytes for title memory from `start` on.
fn card_hashed_of(start: usize, size: usize, piece: usize) -> (usize, usize) {
let (lo, hi) = piece_at(start, size, piece);
(lo & !15, hi.next_multiple_of(16))
}
#[cfg(test)]
mod card_tests {
use super::{CardSpan, PIECE, card_piece_of, card_span_of, joined, joined_piece_runs};
use super::{CardSpan, PIECE, card_hashed_of, card_piece_of, card_span_of, joined, joined_piece_runs};
/// Joined runs hold every piece of the runs given, once, and no other.
#[test]
@@ -507,6 +519,25 @@ mod card_tests {
}
/// Changed ranges are joined where they overlap or touch, and only there.
/// What is fetched of a piece to hash it is what the shader hashes.
#[test]
fn a_piece_is_hashed_here_as_on_the_card() {
for start in [0usize, 0x10_0000, 0xd_5edb_c780, 0x12_3456_7008] {
for size in [1usize << 16, 5 << 16, 0x20_0000] {
let span = card_span_of(start, 0, size);
for i in 0..span.pieces {
// As `CARD_COMPARE` has it.
let (piece, skew, size) = (span.first + i, span.skew, size as u32);
let lo = (piece * 65536).saturating_sub(skew);
let hi = ((piece + 1) * 65536 - skew).min(size);
let shader = (lo & !15, (hi + 15) & !15);
let here = card_hashed_of(start, size as usize, piece as usize);
assert_eq!((here.0 as u32, here.1 as u32), shader, "start {start:#x} size {size:#x} piece {piece}");
}
}
}
}
#[test]
fn ranges_join_where_they_touch() {
assert_eq!(joined(vec![(8, 12), (0, 4), (4, 6), (10, 20), (30, 31)]), [(0, 6), (8, 20), (30, 31)]);
@@ -908,6 +939,8 @@ fn give_back(v: Vec<u8>) {
}
const NOT_WATCHED: u8 = 2;
/// Watched, not by the kernel but by the guards, when last asked about.
const GUARDED: u8 = 3;
/// Where `a` and `b` (of one length) first differ, compared eight bytes at
/// a time.
@@ -969,6 +1002,16 @@ mod difference_tests {
/// The unit shader writes are looked for and put back in.
const PIECE: usize = 64 << 10;
/// Calls `f` with `[from, to)` cut at multiples of [`PIECE`].
fn for_pieces_of(from: usize, to: usize, mut f: impl FnMut(usize, usize)) {
let mut at = from;
while at < to {
let next = ((at / PIECE + 1) * PIECE).min(to);
f(at, next);
at = next;
}
}
/// Copies `len` bytes, on several threads if they are many: a snapshot of
/// the world's big heap is 100-170 MB per submission, 2-4 GB a frame, and
/// one thread copies some 8 GB/s.
@@ -2011,6 +2054,14 @@ impl Shadow {
self.watched.load(Ordering::Relaxed) == NOT_WATCHED
}
/// Whether a submission asks the kernel about the copy's ranges ahead:
/// not of memory that is not watched, nor of view memory, which the
/// kernel would refuse (and with it the ranges asked about together).
pub(crate) fn asked_ahead(&self) -> bool {
let watched = self.watched.load(Ordering::Relaxed);
watched != NOT_WATCHED && (watched != GUARDED || !crate::watch::light())
}
/// The copy's own number (see the field).
pub(crate) fn id(&self) -> u64 {
self.id
@@ -2614,6 +2665,11 @@ impl Shadow {
note_stale(0);
return true;
}
// The declared ranges overlap (views of the title's whole heap from
// dozens of offsets): as one range each page is asked about and
// compared once. Every byte of it is in a declared range, and of a
// copy in the PC's memory in the snapshot.
let busy = if crate::watch::light() { joined(busy) } else { busy };
let base = start / PAGE;
let mut title = vec![0u8; PAGE];
for (a, e) in busy {
@@ -2763,7 +2819,7 @@ impl Shadow {
if from >= to {
return true;
}
if self.watched.load(Ordering::Relaxed) != 1 {
if !matches!(self.watched.load(Ordering::Relaxed), 1 | GUARDED) {
return false;
}
// Around ranges shaders are writing the refresh asks gap by gap.
@@ -2793,7 +2849,8 @@ impl Shadow {
fn update_free(&self, from: usize, to: usize) -> usize {
if self.watched.load(Ordering::Relaxed) != NOT_WATCHED {
if let Some((now, written)) = crate::watch::writes(from, to, true) {
self.watched.store(1, Ordering::Relaxed);
let guarded = crate::watch::guarded(from, to);
self.watched.store(if guarded { GUARDED } else { 1 }, Ordering::Relaxed);
let copied = self.copy_stale(from, to, now, &written);
self.refresh_check(from, to);
#[cfg(feature = "diagnostics")]
@@ -3008,7 +3065,16 @@ impl Shadow {
let (from, to) = (from.max(start), to.min(end));
// SAFETY: inside the mapping (`from` and `to` are within the buffer).
let at = unsafe { self.data.add(from - start) };
crate::host::copy_mapped(at, from as *const u8, to - from, false);
if self.card.is_some() && from < to && crate::host::upcheck::on() {
for_pieces_of(from - start, to - start, |a, b| {
let was = self.card_held(a, b - a);
// SAFETY: inside the mapping, as above.
crate::host::copy_mapped(unsafe { self.data.add(a) }, (start + a) as *const u8, b - a, false);
crate::host::upcheck::note(self.id, a, &was, &self.card_held(a, b - a));
});
} else {
crate::host::copy_mapped(at, from as *const u8, to - from, false);
}
// After the bytes are in: a snapshot taken meanwhile is not current.
self.changed(from - start, to - start);
self.card_changed(from - start, to - start);
@@ -3037,6 +3103,24 @@ impl Shadow {
}
}
/// Diagnostics: of a copy in the card's memory, what its CPU side holds
/// at offsets `[at, at + len)` (cut to the copy).
pub(crate) fn card_held(&self, at: usize, len: usize) -> Vec<u8> {
let to = (at + len).min(self.len as usize);
if self.card.is_none() || at >= to {
return Vec::new();
}
// SAFETY: inside the mapping; the caller holds the submission lock,
// under which it is written but for what is being landed.
unsafe { std::slice::from_raw_parts(self.data.add(at).cast_const(), to - at) }.to_vec()
}
/// Diagnostics: the offsets whose bytes have not gone to the card's side
/// yet.
pub(crate) fn card_dirty_now(&self) -> Vec<(usize, usize)> {
self.card.as_ref().map_or_else(Vec::new, |c| c.dirty.lock().unwrap_or_else(|e| e.into_inner()).clone())
}
/// Of a copy in the card's memory: the CPU's side (PC memory), the
/// card side's bytes from before if made yet, and their size.
pub(crate) fn card_sides(&self) -> Option<(&ID3D12Resource, Option<ID3D12Resource>, u64)> {
@@ -3084,6 +3168,19 @@ impl Shadow {
card_piece_of(self.start as usize, self.len as usize, span, i)
}
/// The piece that holds offset `at`.
pub(crate) fn card_piece_of(&self, at: usize) -> u32 {
self.pieces(at, at + 1).start as u32
}
/// The offsets the compare on the card hashes of a piece: what lies of
/// it in the card's buffer (which may be longer than the copy), widened
/// to 16 bytes.
pub(crate) fn card_hashed(&self, piece: u32) -> (usize, usize) {
let size = self.card.as_ref().map_or(self.len, |c| c.size) as usize;
card_hashed_of(self.start as usize, size, piece as usize)
}
/// The walls trace (`crate::host::walls_trace`): a piece of `fetched`
/// bytes of this copy came back from the card and `written` of them
/// went into title memory. Summed per buffer and logged once a frame:
@@ -3328,7 +3425,7 @@ impl Shadow {
// Where title memory is not mapped the CPU's side keeps what
// the card wrote, as before.
let merged =
unsafe { crate::land::piece(self.start as usize + lo, now as *const u8, self.data.add(lo), &parts, true, notes) };
unsafe { crate::land::piece(self.start as usize + lo, now as *const u8, self.data.add(lo), &parts, true, notes, self) };
self.walls_merged(hi - lo, merged as usize);
put += merged;
}
@@ -3358,7 +3455,33 @@ impl Shadow {
// keeps out what else writes them. Title memory that is not there
// is looked at again when the next flight that declared the range
// lands.
unsafe { crate::land::piece(self.start as usize + at, self.data.add(at), held.add(at), &[(0, n)], false, notes) }
unsafe { crate::land::piece(self.start as usize + at, self.data.add(at), held.add(at), &[(0, n)], false, notes, self) }
}
/// What shaders wrote in 64 KiB block `block` (of title memory) of this
/// copy was put into title memory beside the guard, which was noted as a
/// write with number `now`, `before` the number of the block's last write
/// before. If the copy held title memory's bytes of the block ahead of
/// that it holds them still, and is noted to ([`crate::land`] says why);
/// whether it was.
///
/// That it did: it took the whole block after that write, and no other
/// came that is not noted yet. Looked at after the note, whatever comes
/// later has a number above it: a write of the title's takes the block's
/// bit, until an asking has noted it.
pub(crate) fn landed(&self, block: usize, before: u64, now: u64) -> bool {
let Some(page) = block.checked_sub(self.start as usize / PAGE) else {
return false;
};
let mut copied = self.copied.lock().unwrap_or_else(|e| e.into_inner());
let Some(at) = copied.get_mut(page) else {
return false;
};
let held = *at != 0 && *at >= before && crate::watch::block_published(block);
if held {
*at = (*at).max(now);
}
held
}
}