vgpu: stage overlapping texture copies instead of refusing them
Component: crates/reims-vgpu, runtime/blit_exec. Both backends compile it; measured on x86 macOS / Linux Vulkan, macos-13 rail, PCI attach, page shift 12. `exec_copy_texture_to_texture` refused a same-allocation copy whose source and destination texel rectangles intersect, returning `BlitStatus::Overlap` and the `t2t_overlap` reason to a blit encoder that does not fail on real hardware. The copy was dropped and the guest went on treating it as complete. It fires on live traffic: a driven Maps interaction produced five distinct `(task, src_ref, dst_ref)` across the three recorded boots at the parents of this chain. The refusal's own comment called the shape "undefined in Metal". The parent commit measured that on the named oracle and it is not undefined: an overlapping `copyFromTexture:toTexture:` produces source-snapshot semantics, the whole source region read before any destination byte is written. `self_copy_overlap_row_shift_14` and `self_copy_overlap_column_shift_12` record that, pass natively, and failed on the identified guest control with the destination still holding its full pre-copy contents. The change: where the rectangles intersect, snapshot every source plane into a host staging buffer before writing any destination plane, then write the destination from the snapshot. A row-at-a-time loop can otherwise overwrite bytes a later row or depth plane still has to read, which is exactly what both live shapes do. The prior refusal and its `T2T_OVERLAP_SEEN` diagnostic side state are removed; the staging path carries its own typed capacity refusals (`t2t_overlap_plane_overflow`, `t2t_overlap_total_overflow`, `t2t_overlap_alloc`) where an extent or a host allocation cannot be served. Non-overlapping copies are untouched: the intersection test that used to select the refusal now selects the staging, and everything that did not reach it still takes the direct row path. Tests. `t2t_overlapping_self_copy_stages_the_source_region` and `t2t_vertical_overlap_reads_every_source_row_before_writing` drive the two live shapes through `execute_blit` and assert every destination row came from the pre-copy source row. Probe-verified red: restoring the refusal makes both fail `Overlap` against an expected `Ok`; restored by copying the file back, never via git. 2356 lib tests pass serially on backend-vulkan,host-window, 0 failed. Clippy `-D warnings --all-targets` clean on all three arms. cargo fmt --all applied, --check clean. Conformance, macos-13, complete battery: three candidates against the parent's two-run control. Every candidate is identical per case to the control on all 288 other cases, and the two repaired cases move `FAIL` to `PASS` in all three runs. Counts 187 PASS / 77 FAIL / 29 SKIP against the control's 185/79/29 -- the two new passes are the whole difference, and 77 is the parent chain's own failure count, so the control-unstable `srt_blit_iosurface_source_1920x1080_x4` did not fire in any of the three. No catastrophic run. Visual, macos-13. Driven Maps, fail log cleared (`grep -c vk_caps` = 1), 85 phases over 45 s of sustained pan and zoom. Windowed before and full-screen after both render correctly with the type layer intact at two very different zoom levels. All eight silent-loss classes zero, no device loss, no fence wedge, no panic. `blit t2t_overlap` did not fire once on this boot, against the five distinct firings recorded on the boots at this chain's earlier parents: those copies now land instead of being dropped. Not verified. The oracle measurement is Apple silicon only; no Intel Mac was measured and nothing here claims the semantics are architecture independent. The three capacity refusals are healthy zeros and have not fired. Bytes staged are not measured. One visual interaction. Only the macos-13 rail and the x86 macOS / Linux Vulkan pathway were booted; the Metal arm compiles and its tests do not run on this host. No `REIMS_VGPU_GUEST_IMPORT=off` arm was run for this change: the staging is a host-side buffer on a path that does not reach the guest-RAM import. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
596e37c7e3
commit
fa1f321493
2 files changed
+144
-107
No files matched your search
@@ -218,49 +218,6 @@ fn note_t5_decode_fail(sid: u32, bytes: &[u8]) {
|
||||
));
|
||||
}
|
||||
|
||||
/// Dedup set for the `t2t_overlap` enrichment, keyed by `(task, src_ref, dst_ref)`.
|
||||
static T2T_OVERLAP_SEEN: std::sync::OnceLock<
|
||||
std::sync::Mutex<std::collections::HashSet<(u32, u32, u32)>>,
|
||||
> = std::sync::OnceLock::new();
|
||||
|
||||
/// Emit ONE always-on `blit t2t_overlap` line per distinct
|
||||
/// `(task, src_ref, dst_ref)` carrying the load-bearing overlap geometry so a
|
||||
/// genuine self-overlap (same allocation, live bytes collide — undefined in
|
||||
/// Metal, correctly rejected) can be told apart from a false positive.
|
||||
///
|
||||
/// The reject uses a COMPRESSED bounding span (`row_bytes*copy_h*copy_d`) that
|
||||
/// ignores `row_stride` gaps, so it can misjudge a strided sub-rect copy in
|
||||
/// either direction. Logging `row_bytes` vs `row_stride` and both offsets lets
|
||||
/// a future boot decide whether the check needs to become stride-precise.
|
||||
/// Deduped so a per-draw repeat cannot flood. Diagnostic only.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn note_t2t_overlap(
|
||||
task_id: u32,
|
||||
src_ref: u32,
|
||||
dst_ref: u32,
|
||||
src_off: u64,
|
||||
dst_off: u64,
|
||||
row_bytes: u64,
|
||||
row_stride: u64,
|
||||
copy_h: u64,
|
||||
copy_d: u64,
|
||||
) -> bool {
|
||||
let set = T2T_OVERLAP_SEEN.get_or_init(|| std::sync::Mutex::new(Default::default()));
|
||||
if let Ok(mut g) = set.lock() {
|
||||
if !g.insert((task_id, src_ref, dst_ref)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
let span = row_bytes.saturating_mul(copy_h).saturating_mul(copy_d);
|
||||
let strided = if row_bytes < row_stride { 1 } else { 0 };
|
||||
crate::observe::fail(format!(
|
||||
"blit t2t_overlap task={task_id} src_ref={src_ref} dst_ref={dst_ref} \
|
||||
src_off={src_off} dst_off={dst_off} row_bytes={row_bytes} \
|
||||
row_stride={row_stride} copy_h={copy_h} copy_d={copy_d} span={span} strided={strided}"
|
||||
));
|
||||
true
|
||||
}
|
||||
|
||||
/// `(task, side, format)` — what `repack_storage_assumed` reports once per.
|
||||
type RepackAssumedKey = (u32, &'static str, u16);
|
||||
|
||||
@@ -3452,23 +3409,17 @@ fn exec_copy_texture_to_texture<M: HostMemory + HostOps>(
|
||||
// onto themselves changes nothing, so succeed without work rather than
|
||||
// rejecting it as Overlap (which returned a spurious error to the guest
|
||||
// blit encoder and dropped a copy the guest treats as complete). A
|
||||
// genuinely-shifted overlap (src_gva != dst_gva, undefined in Metal)
|
||||
// still falls through to the reject below.
|
||||
// genuinely-shifted overlap falls through to source-before-destination
|
||||
// staging below.
|
||||
if src_gva == dst_gva && sl.row_stride == dl.row_stride && src_bpi == dst_bpi {
|
||||
return BlitStatus::Ok;
|
||||
}
|
||||
// Same allocation (self-copy or aliased view) but a different region:
|
||||
// safe unless the source and destination TEXEL rectangles actually
|
||||
// intersect. Two axis-aligned rectangles overlap iff they overlap on
|
||||
// EVERY axis — the correct model when src/dst share the texel layout
|
||||
// (same row stride + per-image stride, guaranteed for a same-texture
|
||||
// self-copy). The prior byte-span test collapsed row_stride into one
|
||||
// contiguous span and produced phantom overlaps for strided sub-rect
|
||||
// column copies: a 1-wide column shifted N texels right (live: src_off=0
|
||||
// dst_off=64, 4-byte rows 1024 apart) never collides row-to-row, yet the
|
||||
// span test flagged it and dropped a legitimate copy. If the layouts
|
||||
// differ (exotic aliased views with mismatched strides), texel grids are
|
||||
// incomparable — keep the conservative byte-span reject there.
|
||||
// direct row-by-row execution is safe only when the source and
|
||||
// destination texel rectangles do not intersect. Two axis-aligned
|
||||
// rectangles overlap iff they overlap on every axis. If the layouts
|
||||
// differ, their texel grids are incomparable, so the byte spans give
|
||||
// the conservative answer.
|
||||
if sl.base_gva == dl.base_gva {
|
||||
let same_layout = sl.row_stride == dl.row_stride && src_bpi == dst_bpi;
|
||||
let overlaps = if same_layout {
|
||||
@@ -3489,18 +3440,88 @@ fn exec_copy_texture_to_texture<M: HostMemory + HostOps>(
|
||||
)
|
||||
};
|
||||
if overlaps {
|
||||
note_t2t_overlap(
|
||||
// The serialized copy record carries the complete source and
|
||||
// destination regions and does not define an overlap refusal.
|
||||
// Snapshot every source plane before writing any destination
|
||||
// plane: a row-at-a-time loop can overwrite bytes a later row
|
||||
// or depth plane still has to read.
|
||||
let plane_bytes = match row_bytes.checked_mul(copy_h) {
|
||||
Some(v) => v,
|
||||
None => return br(BlitStatus::Capacity, "t2t_overlap_plane_overflow"),
|
||||
};
|
||||
let total_bytes = match plane_bytes.checked_mul(copy_d) {
|
||||
Some(v) => v,
|
||||
None => return br(BlitStatus::Capacity, "t2t_overlap_total_overflow"),
|
||||
};
|
||||
let Some(total_len) = host_alloc_len(total_bytes) else {
|
||||
return br(BlitStatus::Capacity, "t2t_overlap_alloc");
|
||||
};
|
||||
let allowed = match texture_region_window(
|
||||
state,
|
||||
host,
|
||||
task_id,
|
||||
cmd.source,
|
||||
cmd.destination,
|
||||
src_off,
|
||||
dst_off,
|
||||
row_bytes,
|
||||
sl.row_stride,
|
||||
&dst,
|
||||
Point {
|
||||
x: dox,
|
||||
y: doy,
|
||||
z: doz,
|
||||
},
|
||||
copy_w as u32,
|
||||
copy_h,
|
||||
copy_d,
|
||||
copy_bpp,
|
||||
) {
|
||||
Ok(v) => v,
|
||||
Err(st) => return st,
|
||||
};
|
||||
let mut staged = vec![0u8; total_len];
|
||||
for z in 0..copy_d {
|
||||
let start = (z * plane_bytes) as usize;
|
||||
let end = start + plane_bytes as usize;
|
||||
if let Err(st) = read_texture_rect(
|
||||
state,
|
||||
host,
|
||||
task_id,
|
||||
&src,
|
||||
Point {
|
||||
x: sox,
|
||||
y: soy,
|
||||
z: soz + z,
|
||||
},
|
||||
row_bytes,
|
||||
copy_h,
|
||||
&mut staged[start..end],
|
||||
) {
|
||||
return st;
|
||||
}
|
||||
}
|
||||
for z in 0..copy_d {
|
||||
let start = (z * plane_bytes) as usize;
|
||||
let end = start + plane_bytes as usize;
|
||||
if let Err(st) = write_texture_rect(
|
||||
state,
|
||||
host,
|
||||
task_id,
|
||||
&dst,
|
||||
Point {
|
||||
x: dox,
|
||||
y: doy,
|
||||
z: doz + z,
|
||||
},
|
||||
row_bytes,
|
||||
copy_h,
|
||||
&staged[start..end],
|
||||
allowed.as_ref(),
|
||||
) {
|
||||
return st;
|
||||
}
|
||||
}
|
||||
crate::runtime::drain::note_store_route("blit_t2t_overlap_staged");
|
||||
crate::runtime::drain::note_store_route_n(
|
||||
"blit_t2t_overlap_staged_bytes",
|
||||
total_bytes,
|
||||
);
|
||||
return br(BlitStatus::Overlap, "t2t_overlap");
|
||||
return BlitStatus::Ok;
|
||||
}
|
||||
}
|
||||
return match copy_row_region(
|
||||
|
||||
@@ -2192,11 +2192,6 @@ fn t2t_identity_self_copy_is_noop_ok() {
|
||||
gva_mem::read_task_gva(&host, &state.tasks[1], gva, &mut back, PAGE_SHIFT_ARM64E).is_ok()
|
||||
);
|
||||
assert_eq!(back, pat, "identity self-copy left the bytes unchanged");
|
||||
// No overlap enrichment line was emitted (it's not a genuine overlap).
|
||||
assert!(
|
||||
note_t2t_overlap(1, 2, 2, 0, 0, 16, 16, 2, 1),
|
||||
"identity path must not have consumed the overlap dedup slot"
|
||||
);
|
||||
}
|
||||
|
||||
/// Regression guard for the strided-column false positive: a self-copy of a
|
||||
@@ -2241,22 +2236,18 @@ fn t2t_shifted_column_self_copy_moves_bytes() {
|
||||
let dst_texel = &back[r * 16 + 8..r * 16 + 12];
|
||||
assert_eq!(dst_texel, src_texel, "row {r} column x=2 holds moved src");
|
||||
}
|
||||
// No overlap enrichment (this is not a genuine overlap).
|
||||
assert!(
|
||||
note_t2t_overlap(9, 2, 2, 0, 8, 4, 16, 4, 1),
|
||||
"shifted-column path must not have consumed the overlap dedup slot"
|
||||
);
|
||||
}
|
||||
|
||||
/// Regression guard: a GENUINELY overlapping self-copy (src rect x[0,2),
|
||||
/// dst rect x[1,3) — overlap on x) is undefined in Metal and must still be
|
||||
/// rejected as Overlap, with the enrichment line emitted for diagnosis.
|
||||
/// An overlapping same-texture region is admitted by the serialized copy
|
||||
/// contract. Snapshot the whole source before the first destination write so
|
||||
/// the traversal direction cannot change the bytes copied.
|
||||
#[test]
|
||||
fn t2t_overlapping_self_copy_still_rejected() {
|
||||
fn t2t_overlapping_self_copy_stages_the_source_region() {
|
||||
let (mut host, mut state) = blit_device();
|
||||
// Unique ref (4) so the (task,src,dst) enrichment key is globally
|
||||
// distinct from the identity/shifted tests' probes.
|
||||
install_linear_rgba(&mut host, &mut state, 4, 4, 4, 4, 16);
|
||||
let gva = 4u64 << RESOURCE_PAGE_SHIFT;
|
||||
let pat: Vec<u8> = (0..64).map(|i| i as u8).collect();
|
||||
write_task_gva_arm64e(&mut host, &state.tasks[1], gva, &pat);
|
||||
let mut cmd = copy_cmd(CopyKind::TextureToTexture, 4, 4);
|
||||
cmd.destination_origin.x = 1; // src x[0,2), dst x[1,3) overlap at x=1
|
||||
cmd.source_size = Size {
|
||||
@@ -2264,16 +2255,58 @@ fn t2t_overlapping_self_copy_still_rejected() {
|
||||
height: 4,
|
||||
depth: 1,
|
||||
};
|
||||
assert_eq!(
|
||||
execute_blit(&mut state, &mut host, 1, &cmd),
|
||||
BlitStatus::Overlap,
|
||||
"genuinely overlapping self-copy must be rejected"
|
||||
);
|
||||
// The enrichment slot WAS consumed (a real overlap was logged).
|
||||
assert!(
|
||||
!note_t2t_overlap(1, 4, 4, 0, 4, 8, 16, 4, 1),
|
||||
"the reject path must have logged the overlap enrichment once"
|
||||
);
|
||||
assert_eq!(execute_blit(&mut state, &mut host, 1, &cmd), BlitStatus::Ok);
|
||||
let mut back = vec![0u8; 64];
|
||||
gva_mem::read_task_gva(&host, &state.tasks[1], gva, &mut back, PAGE_SHIFT_ARM64E)
|
||||
.expect("copied texture remains readable");
|
||||
for row in 0..4usize {
|
||||
assert_eq!(
|
||||
&back[row * 16 + 4..row * 16 + 12],
|
||||
&pat[row * 16..row * 16 + 8],
|
||||
"row {row} came from the pre-copy source"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The strided vertical overlap emitted by the UI workload: eighteen one-pixel
|
||||
/// rows move down twelve rows in one texture. Rows 12..17 are both source and
|
||||
/// destination, so staging a row at a time would feed written pixels back into
|
||||
/// the tail of the copy.
|
||||
#[test]
|
||||
fn t2t_vertical_overlap_reads_every_source_row_before_writing() {
|
||||
const WIDTH: u32 = 256;
|
||||
const HEIGHT: u32 = 30;
|
||||
const STRIDE: u32 = WIDTH * 4;
|
||||
let (mut host, mut state) = blit_device();
|
||||
install_linear_rgba(&mut host, &mut state, 5, 5, WIDTH, HEIGHT, STRIDE);
|
||||
let gva = 5u64 << RESOURCE_PAGE_SHIFT;
|
||||
let mut pat = vec![0u8; STRIDE as usize * HEIGHT as usize];
|
||||
for row in 0..HEIGHT as usize {
|
||||
pat[row * STRIDE as usize..row * STRIDE as usize + 4]
|
||||
.copy_from_slice(&(row as u32).to_le_bytes());
|
||||
}
|
||||
write_task_gva_arm64e(&mut host, &state.tasks[1], gva, &pat);
|
||||
|
||||
let mut cmd = copy_cmd(CopyKind::TextureToTexture, 5, 5);
|
||||
cmd.destination_origin.y = 12;
|
||||
cmd.source_size = Size {
|
||||
width: 1,
|
||||
height: 18,
|
||||
depth: 1,
|
||||
};
|
||||
assert_eq!(execute_blit(&mut state, &mut host, 1, &cmd), BlitStatus::Ok);
|
||||
|
||||
let mut back = vec![0u8; pat.len()];
|
||||
gva_mem::read_task_gva(&host, &state.tasks[1], gva, &mut back, PAGE_SHIFT_ARM64E)
|
||||
.expect("copied texture remains readable");
|
||||
for source_row in 0..18usize {
|
||||
let destination_row = source_row + 12;
|
||||
assert_eq!(
|
||||
&back[destination_row * STRIDE as usize..destination_row * STRIDE as usize + 4],
|
||||
&pat[source_row * STRIDE as usize..source_row * STRIDE as usize + 4],
|
||||
"destination row {destination_row} came from the pre-copy source row {source_row}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2636,23 +2669,6 @@ fn tex_wrong_type_enrichment_dedups_per_ref_and_type() {
|
||||
));
|
||||
}
|
||||
|
||||
/// Regression guard: the `t2t_overlap` enrichment dedups per
|
||||
/// `(task, src_ref, dst_ref)` — a self-overlapping copy re-issued every
|
||||
/// frame must not flood — while a distinct src/dst pair reports once so a
|
||||
/// genuine drop stays diagnosable.
|
||||
#[test]
|
||||
fn t2t_overlap_enrichment_dedups_per_pair() {
|
||||
// Unique task namespace (3) so the process-global dedup set never
|
||||
// collides with other tests; the set starts empty so first-insert is
|
||||
// deterministic without a reset.
|
||||
assert!(note_t2t_overlap(3, 0x10, 0x10, 0, 4096, 256, 1024, 8, 1));
|
||||
for _ in 0..20 {
|
||||
assert!(!note_t2t_overlap(3, 0x10, 0x10, 0, 4096, 256, 1024, 8, 1));
|
||||
}
|
||||
// A distinct destination ref is a distinct failure -> reports once.
|
||||
assert!(note_t2t_overlap(3, 0x10, 0x11, 0, 4096, 256, 1024, 8, 1));
|
||||
}
|
||||
|
||||
/// The precondition the `repack_storage_assumed` enrichment exists for.
|
||||
///
|
||||
/// The texture-to-texture format check deliberately admits a zero on either
|
||||
|
||||
Reference in new issue
Block a user