🐛 Drop a command naming a made cache buffer that lacks its pages

A straight download copies a cache's dirty shapes into the pages alone and
marks the buffer as lacking them, whether its memory is made or not.
Caches::memory_of looked for a lacking buffer only where its memory was not
made, so a command that named a made one read the buffer's older bytes, not
the pages: the state every straight download now leaves.

A command that names a cache's buffer that lacks the pages, its memory made
or not, is now dropped as the unmade case was: its command buffer fails with
Metal's internal error, counted as rt-shape-lacking-named. A fill asked and
not made (a texture over the buffer, a video begin) keeps a made buffer, as
the command's buffer waits for it on the GPU.

For the guest: a command buffer naming such a buffer fails with
MTLCommandBufferErrorInternal where it read stale bytes before. The bundle
names no cache's buffer in a command, so no guest reaches it today.
This commit is contained in:
Anees Iqbal committed 2026-12-02 22:53:50 -08:00
1 parent b177728ad1
commit 2090684177
4 files changed
+104 -38

No files matched your search

+3 -3
View File
@@ -8040,9 +8040,9 @@ impl<P: Platform> GraphicsDecoder<P> {
/// cache's module doc, "Deferred memory"): where the platform refuses it, the raise is
/// [`refused::out_of_memory`]'s, which drops the op, and the command buffer fails with
/// Metal's out-of-memory error ([`Self::failed`]); where the buffer lacks its surface's pages,
/// which no copy can bring before the op (the cache's module doc, "Straight fills"),
/// [`refused::lacking_pages`]'s, which drops it too, and the command buffer fails with
/// Metal's internal error.
/// its memory made or not, which no copy can bring before the op (the cache's module doc,
/// "Straight fills" and "Straight downloads"), [`refused::lacking_pages`]'s, which drops it
/// too, and the command buffer fails with Metal's internal error.
fn cache_memory(&self, id: u64, entry: &Entry<P>, buffer: &P::Buffer) -> RaiseResult<()> {
if !entry.over_cache {
return Ok(());
@@ -520,11 +520,14 @@
//! begin having readied it is met inside its encoder, where no copy can go before it: it is
//! dropped, the buffer's memory left unmade, and its command buffer fails with Metal's
//! `MTLCommandBufferErrorInternal`, counted ([`shapes::LACKING_NAMED`], [`Caches::memory_of`]),
//! where it read the zeros of the memory's make before; the bundle names the buffer only in
//! `TEXTURE_FROM_IOSURFACE` creates. A cache that lacks the pages never takes a shape's fill as
//! zeros ([`SurfaceCache::fills_zeros`]). A refill reads the pages as they are then, not as the
//! straight fill read them: where the CPU wrote them since without a read-write lock, the buffer,
//! and a copy out of all of it after a refill, holds those later bytes beside the shapes' writes.
//! where it read the zeros of the memory's make before; so is one that names a buffer whose
//! memory is made and that lacks the pages, as a straight download leaves it ("Straight
//! downloads"), with no fill asked to bring them, where it read the buffer's older bytes
//! before. The bundle names the buffer only in `TEXTURE_FROM_IOSURFACE` creates. A cache that
//! lacks the pages never takes a shape's fill as zeros ([`SurfaceCache::fills_zeros`]). A refill
//! reads the pages as they are then, not as the straight fill read them: where the CPU wrote
//! them since without a read-write lock, the buffer, and a copy out of all of it after a refill,
//! holds those later bytes beside the shapes' writes.
//! The straight fills, the shapes they filled, their bytes, copies and split rows, and the
//! refills, are on the caches' straight fills line.
//!
@@ -544,7 +547,8 @@
//! out of the buffer moves one GPU copy more of each byte (the write-back, then the copy out),
//! and makes the buffer's memory where it holds none. The shapes then hold no write the next
//! copy out must take, and the buffer lacks the pages ([`SurfaceCache::lacks`]), as after a
//! straight fill: a copy through it takes them first. The shapes copied and their bytes are
//! straight fill: a copy through it takes them first, and a command that names it is dropped
//! ([`Caches::memory_of`]), its memory made or not. The shapes copied and their bytes are
//! counted ([`shapes::DOWNLOADS`], [`shapes::DOWNLOAD_BYTES`]), the copies through pieces among
//! the staged downloads too (`rt-downloads-cache-view`). A shape in place over its pages
//! ([`shapes`]' "In place") is never dirty, as its write is in them: the straight download is
@@ -833,8 +837,11 @@ pub(crate) struct SurfaceCache<P: Platform> {
/// and copied through it since ([`BufferMemory`]).
memory: BufferMemory,
/// The buffer lacks the pages' bytes its shapes hold: a fill made while its memory was not
/// made copied the pages into the shapes' images alone (the module doc, "Straight fills").
/// A copy through the buffer fills it from the pages first ([`refill`]), which ends it.
/// made copied the pages into the shapes' images alone (the module doc, "Straight fills"), a
/// download copied its dirty shapes into the pages alone, its memory made or not ("Straight
/// downloads"), or a shape in place over the pages took a write there ([`shapes`]' "In
/// place"). A copy through the buffer fills it from the pages first ([`refill`]), which ends
/// it; a command that names it meanwhile is dropped ([`Caches::memory_of`]).
lacks: bool,
/// In the per-shape form, what the guest's textures that lie over the buffer itself hold
/// ([`Unshaped`]): while one lives, the buffer is kept as current as the shapes (the module
@@ -2757,7 +2764,8 @@ pub(crate) struct CacheCounts {
pub(crate) buffer_refilled_bytes: u64,
pub(crate) buffer_refills_named: u64,
/// Commands that named a cache's buffer that lacked the pages with no begin having readied
/// it, dropped, their command buffers failed ([`shapes::LACKING_NAMED`]).
/// it, its memory made or not, dropped, their command buffers failed
/// ([`shapes::LACKING_NAMED`]).
pub(crate) lacking_named: u64,
/// Downloads, and the bytes they moved.
pub(crate) downloads: u64,
@@ -7282,11 +7290,12 @@ impl<P: Platform> SurfaceCaches<P> {
self.fill_shapes(device, block).map(|_| ())
}
/// Whether the block at `block`'s cache's buffer lacks the pages as a command names it, its
/// memory not made ([`Caches::memory_of`]): counted, for the command dropped
/// ([`shapes::LACKING_NAMED`]).
fn named_lacking(&mut self, block: u64) -> bool {
let lacking = self.lacks(block);
/// Whether the block at `block`'s cache's buffer lacks the pages as a command names it
/// ([`Caches::memory_of`]): where its memory is `made`, with no fill asked and not made to
/// bring them ([`Self::fill_deferred`]), which the command's buffer waits for on the GPU.
/// Counted, for the command dropped ([`shapes::LACKING_NAMED`]).
fn named_lacking(&mut self, block: u64, made: bool) -> bool {
let lacking = self.lacks(block) && !(made && self.fill_deferred(block));
self.counts.lacking_named += u64::from(lacking);
lacking
}
@@ -10961,29 +10970,32 @@ impl<P: Platform> Caches<P> {
/// ([`MemoryOf::Made`]), the platform refused it ([`MemoryOf::Refused`]), or it lacks the
/// pages, which nothing can copy into it before the command ([`MemoryOf::Lacks`]).
///
/// A buffer whose memory is not made may lack the pages, which a fill copied into its shapes
/// alone (the module doc, "Straight fills"), where no begin readied it: the begin of a buffer
/// whose video ops name it makes its memory and has it take the pages
/// ([`Reach::named`]), and a texture made over it has it take them as it is made
/// ([`Self::texture_over`]). Any other command that names it is met inside its encoder, where
/// no copy can go before it: it would read the zeros of the memory's make, not the pages. Its
/// memory is left unmade, the command dropped and its command buffer failed with Metal's
/// internal error by the caller, counted ([`shapes::LACKING_NAMED`],
/// [`CacheCounts::lacking_named`]), its first logged. The bundle never names such a buffer:
/// its id goes out only in `TEXTURE_FROM_IOSURFACE` creates, and `RgfxTexture.buffer` is nil
/// for an IOSurface texture, so no render, compute or blit op of the guest's names it.
/// A buffer may lack the pages ([`SurfaceCache::lacks`]): one whose memory is not made, which
/// a fill copied into its shapes alone (the module doc, "Straight fills"), and one whose
/// memory is made, whose shapes' writes a download copied straight into the pages ("Straight
/// downloads"). The begin of a buffer whose video ops name it makes its memory and has it take
/// the pages ([`Reach::named`]), and a texture made over it has it take them as it is made
/// ([`Self::texture_over`]); a fill so asked and not made yet keeps a buffer whose memory is
/// made, as the command's buffer waits for it on the GPU. Any other command that names a
/// buffer that lacks them is met inside its encoder, where no copy can go before it: it would
/// read the zeros of the memory's make, or the buffer's older bytes, not the pages. The memory
/// is left as it is, the command dropped and its command buffer failed with Metal's internal
/// error by the caller, counted ([`shapes::LACKING_NAMED`], [`CacheCounts::lacking_named`]),
/// its first logged. The bundle never names such a buffer: its id goes out only in
/// `TEXTURE_FROM_IOSURFACE` creates, and `RgfxTexture.buffer` is nil for an IOSurface texture,
/// so no render, compute or blit op of the guest's names it.
pub(crate) fn memory_of(&self, block: Option<u64>, buffer: &P::Buffer) -> MemoryOf {
if !buffer.memory_made()
&& let Some(block) = block
&& self.lock().named_lacking(block)
if let Some(block) = block
&& self.lock().named_lacking(block, buffer.memory_made())
{
reims_vgpu_vulkan::answer::count_kind(shapes::LACKING_NAMED);
if !LACKING_LOGGED.swap(true, Ordering::Relaxed) {
log::warn!(
target: "reims_vgpu::graphics", kind:% = shapes::LACKING_NAMED;
"a command names the buffer of the IOSurface block at {block:#x}'s device \
cache, which lacks the pages its shapes took: the command is dropped, and \
its command buffer fails with the internal error ({}; the rest are counted)",
cache, which lacks the pages' bytes its shapes took: the command is dropped, \
and its command buffer fails with the internal error ({}; the rest are \
counted)",
shapes::LACKING_NAMED
);
}
@@ -456,11 +456,12 @@ pub(crate) const UNPLACED_UNFILLED: &str = "rt-shape-unplaced-unfilled";
pub(crate) const ZERO_TAKEN: &str = "rt-shape-zero-taken";
pub(crate) const UNPLACED_ZEROS: &str = "rt-shape-unplaced-zeros";
/// The counts' code of a command that named a cache's buffer that lacked the pages its shapes
/// took straight from them, with no begin having readied it (`SurfaceCaches`' module doc,
/// "Straight fills"): it is dropped, and its command buffer fails with Metal's internal error,
/// as it would read the zeros of the buffer's memory's make, not the pages
/// (`Caches::memory_of`). The bundle never names such a buffer.
/// The counts' code of a command that named a cache's buffer that lacked the pages' bytes its
/// shapes took, with no begin having readied it: straight from the pages, its memory not made
/// (`SurfaceCaches`' module doc, "Straight fills"), or into them, its memory made or not
/// ("Straight downloads"). It is dropped, and its command buffer fails with Metal's internal
/// error, as it would read the zeros of the buffer's memory's make, or the buffer's older bytes,
/// not the pages (`Caches::memory_of`). The bundle never names such a buffer.
pub(crate) const LACKING_NAMED: &str = "rt-shape-lacking-named";
/// The counts' codes of the shapes made in place over their surface's pages, and their bytes
@@ -2682,6 +2682,59 @@ mod tests {
assert_eq!(r.dec.caches().counts().buffer_refills, 1);
}
/// A straight download leaves a buffer whose memory is made without the shape's write the
/// pages took (`surface_cache`'s module doc, "Straight downloads"): a command that names it
/// then, with no fill asked to bring the pages, is dropped as one naming a buffer whose
/// memory is not made is, where it read the buffer's older bytes before. Its command buffer
/// fails with Metal's internal error, counted (`rt-shape-lacking-named`). Its early copy, the
/// writer's after a download (`surface_cache`'s `SurfaceCaches::early_copies`), goes through
/// the buffer, which takes the pages first
#[test]
fn a_command_naming_a_made_buffer_a_straight_download_left_lacking_is_dropped() {
use reims_vgpu_vulkan::core::testkit::impls::command::{FAIL_INTERNAL, FAIL_OUT_OF_MEMORY};
let mut r = shaped();
buffer_made(&r);
let (cache, pages) = (cache_id(&r), pages_id(&r));
let image = texture_image(&mut r, 25, 80, (32, 8));
write(&mut r, 0x100, 25, (32, 8));
r.p.world().complete_all(CompleteOn::Caller);
let mark = r.p.call_log().mark();
let downloaded = download(&r, BLOCK);
r.p.world().complete_all(CompleteOn::Caller);
assert!(downloaded.load(Ordering::SeqCst));
let calls = r.p.call_log().since(mark);
assert_eq!(written_back(&calls), [(image, pages, 0)], "straight");
assert!(
copies_out(&calls, cache).is_empty(),
"none out of the buffer"
);
assert!(r.dec.caches().buffer(BLOCK).unwrap().memory_made());
// The guest's blit from buffer 10 into the shape's texture: the shape is clean and owes
// no fill, so the begin copies nothing into the buffer ahead of it.
let was = counted(shapes::LACKING_NAMED);
let mark = r.p.call_log().mark();
write_from(&mut r, 0x101, 25, (32, 8), 10);
r.p.world().complete_all(CompleteOn::Caller);
let calls = r.p.call_log().since(mark);
let id = |a: &Arg| a.as_obj().map_or(0, |o| o.0);
assert!(
!(calls.iter()).any(|c| c.sel == FILL && id(&c.args[0]) == cache),
"the command dropped"
);
let failed = |sel: &str| calls.iter().filter(|c| c.sel == sel).count();
assert_eq!((failed(FAIL_INTERNAL), failed(FAIL_OUT_OF_MEMORY)), (1, 0));
let counts = r.dec.caches().counts();
assert_eq!(counts.lacking_named, 1);
assert_eq!(counted(shapes::LACKING_NAMED) - was, 1);
assert_eq!(
copies(&calls),
[(pages, cache), (cache, pages)],
"the early copy takes the pages first"
);
assert_eq!((counts.early, counts.buffer_refills), (1, 1));
}
/// A texture over a cache's buffer itself (`TEXTURE_FROM_BUFFER` of buffer 10) made while
/// the buffer lacks the pages its shapes took makes the buffer's memory and copies the pages
/// into it, so the texture reads the surface, counted among the buffers that took them for a