Buffer::held_parts: a name apart from Buffer::held, and the test's hook on its own thread

Buffer::held (the reference a command buffer holds a buffer by) was there
already: the parts' hold is Buffer::held_parts. The test imports PixelFormat
and TextureUsage from metal2vulkan::metal, as the other tests do.

The hook that parks the call is thread-local, set on the call's thread just
before the call, so no other thread's hold (a device thread's texture
placement, say) takes it.

Verification: the lavapipe and Iris runs that follow.
(cherry picked from commit 3b4443dc3b43b444016a0be76583ddddbd446761)
This commit is contained in:
Anees Iqbal committed 2026-11-09 05:03:58 -08:00
1 parent 15ea040ef0
commit dc936fbb33
9 files changed
+59 -52

No files matched your search

@@ -196,8 +196,8 @@ impl Buffer {
/// Metal answers where its validation stops every call there, a query included. Fails only when
/// zeroing a `Managed` buffer's bytes on a device without unified memory fails.
pub fn set_purgeable_state(&self, state: PurgeableState) -> Result<PurgeableState, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
if self.heap().is_some() {
return Ok(PurgeableState::NonVolatile);
}
+18 -15
View File
@@ -122,7 +122,7 @@ struct BufferShared {
/// `next` is set: read at one relaxed load where nothing was carried ([`Buffer::s`]), `next`
/// with its own ordering where it was.
carried: AtomicBool,
/// The calls outside a command buffer's work under way on these parts ([`Buffer::held`]).
/// The calls outside a command buffer's work under way on these parts ([`Buffer::held_parts`]).
calls: Mutex<LinkCalls>,
}
@@ -765,7 +765,7 @@ impl LostParts for LostBuffer {
{
let mut calls = lock(&buffer.calls);
if calls.under_way > 0 {
// The last call under way on them lets them go (`Buffer::held`).
// The last call under way on them lets them go (`Buffer::held_parts`).
calls.waiting = Some(self);
return;
}
@@ -785,14 +785,14 @@ impl LostBuffer {
}
self.pages.release_lost();
// SAFETY: its parts on the generation went, no command buffer of the generation records,
// no call under way reads them (`Buffer::held`), and every other reach of the buffer goes
// to where it was carried (`Buffer::carried`).
// no call under way reads them (`Buffer::held_parts`), and every other reach of the
// buffer goes to where it was carried (`Buffer::carried`).
unsafe { buffer.device.let_go() };
}
}
/// The calls outside a command buffer's work under way on a buffer's parts on one generation
/// ([`Buffer::held`]), and the release of those parts that waits for them to end, where the
/// ([`Buffer::held_parts`]), and the release of those parts that waits for them to end, where the
/// buffer was carried from that generation and it came to rest (`device/carry.rs`).
#[derive(Default)]
struct LinkCalls {
@@ -801,7 +801,7 @@ struct LinkCalls {
}
/// A call outside a command buffer's work on a buffer's parts on one generation
/// ([`Buffer::held`]), until it goes: the parts stay while it lives, and every read of the
/// ([`Buffer::held_parts`]), until it goes: the parts stay while it lives, and every read of the
/// buffer's parts on this thread meanwhile answers them.
pub(super) struct HeldParts<'a> {
link: &'a Arc<BufferShared>,
@@ -2043,7 +2043,7 @@ impl Filled for BufferShared {
impl BufferShared {
/// Its parts on its lost generation were marked let go of, the buffer carried to the next one
/// and that generation at rest (`LostBuffer::mark`): no call takes them from now on
/// ([`Buffer::held`]), and they go once none under way holds them.
/// ([`Buffer::held_parts`]), and they go once none under way holds them.
fn released(&self) -> bool {
match &self.bytes {
Bytes::Own(OwnBytes {
@@ -3352,7 +3352,7 @@ impl Buffer {
let mut shared = &self.0;
loop {
// Not its parts on a lost generation that went: a call under way that holds them
// keeps them until it ends (`Buffer::held`), and no command buffer is made there
// keeps them until it ends (`Buffer::held_parts`), and no command buffer is made there
// after its rest (`Carry::refuses`).
if records == Some(shared.generation) && !shared.freed() {
return shared;
@@ -3376,7 +3376,7 @@ impl Buffer {
/// the next (`RuntimeGap::MadeBeforeRecovery`), as a call that took its arguments before
/// the loss (`Device::at_serving_taking`). Not for command buffers' work, whose queue slot
/// holds the generation (`device/carry.rs`).
pub(super) fn held(&self) -> HeldParts<'_> {
pub(super) fn held_parts(&self) -> HeldParts<'_> {
let mut link = self.s();
loop {
{
@@ -4585,16 +4585,19 @@ impl Buffer {
}
}
/// A test's hook into [`Buffer::held`], run once by the first call that holds a buffer's parts
/// after the test set it: the test parks that call there across a recovery
/// (`tests/device_recovery_carry.rs`).
#[cfg(test)]
pub(crate) static HELD_HOOK: Mutex<Option<Box<dyn FnOnce() + Send>>> = Mutex::new(None);
thread_local! {
/// A test's hook into [`Buffer::held_parts`], run once by this thread's next call that holds
/// a buffer's parts after the test set it: the test parks that call there across a recovery
/// (`tests/device_recovery_carry.rs`).
pub(crate) static HELD_HOOK: std::cell::RefCell<Option<Box<dyn FnOnce()>>> =
const { std::cell::RefCell::new(None) };
}
/// Run [`HELD_HOOK`], where a test set it.
/// Run this thread's [`HELD_HOOK`], where a test set it.
#[cfg(test)]
fn held_hook() {
let hook = lock(&HELD_HOOK).take();
let hook = HELD_HOOK.with(|hook| hook.borrow_mut().take());
if let Some(hook) = hook {
hook();
}
@@ -416,8 +416,8 @@ impl Buffer {
offset: u64,
bytes_per_row: u64,
) -> Result<Texture, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.texture_over(
"new_texture",
descriptor,
@@ -466,8 +466,8 @@ impl Buffer {
bytes_per_row: u64,
_bytes_per_image: u64,
) -> Result<Texture, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.texture_over(
"new_linear_texture",
descriptor,
@@ -529,8 +529,8 @@ impl Buffer {
offset: u64,
bytes_per_row: u64,
) -> Result<Texture, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.texture_over(
"new_tiled_texture",
descriptor,
@@ -696,8 +696,8 @@ impl Buffer {
descriptor: &TextureDescriptor,
plane: IoSurfacePlane,
) -> Result<Texture, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
plane.refuse_client_direction()?;
let texture = self.texture_over(
"new_texture_with_iosurface_plane",
@@ -750,8 +750,8 @@ impl Buffer {
offset: u64,
bytes_per_row: u64,
) -> Result<TexturePlacement, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
let over = (offset, bytes_per_row);
let asked = (BufferTextureCall::Texture, Whose::Program, Goal::Ask);
self.placement_over("new_texture", descriptor, over, asked, None)
@@ -768,8 +768,8 @@ impl Buffer {
offset: u64,
bytes_per_row: u64,
) -> Result<TexturePlacement, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
let over = (offset, bytes_per_row);
let asked = (BufferTextureCall::Tiled, Whose::Program, Goal::Ask);
self.placement_over("new_tiled_texture", descriptor, over, asked, None)
@@ -785,8 +785,8 @@ impl Buffer {
descriptor: &TextureDescriptor,
plane: IoSurfacePlane,
) -> Result<TexturePlacement, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
plane.refuse_client_direction()?;
let over = (plane.offset, plane.bytes_per_row);
let form = BufferTextureCall::IoSurfacePlane {
@@ -3009,7 +3009,7 @@ impl NoticesFor {
/// This thread's reads of a carried object's parts answer those of the generation at
/// `generation` (the address of its `DeviceShared`), until the scope goes, inside whatever
/// command buffer's work it does: a call outside command buffers' work that holds a buffer's
/// parts there (`Buffer::held`).
/// parts there (`Buffer::held_parts`).
pub(crate) fn of_generation(generation: usize) -> NoticesFor {
let outer = NOTICES_FOR.with(|current| {
let outer = current.get();
@@ -40,7 +40,7 @@
//! that waits for or signals it after the carry refuses it, as its device is the next
//! generation (`Device::foreign`). A call outside command buffers' work that reaches a buffer's
//! Vulkan objects (a texture over its bytes, an export of its memory, an upload, a handle
//! handed out) holds the parts it read (`Buffer::held`): their release at the generation's
//! handed out) holds the parts it read (`Buffer::held_parts`): their release at the generation's
//! rest waits for the call to end, and the call completes on the generation it began on. A
//! handle handed out (`Buffer::vk_buffer`, `Event::vk_semaphore`), which the caller may use
//! while the object lives, keeps its parts, and the lost `VkDevice`, until the object goes.
@@ -439,8 +439,8 @@ impl Buffer {
/// (`NotImplemented(MadeBeforeRecovery)`) or on a lost device (`DeviceLost`), and the driver's
/// failure.
pub fn export_memory_fd(&self) -> Result<ExportedMemory, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.export_memory("export_memory_fd", MemoryExport::OpaqueFd)
}
@@ -460,8 +460,8 @@ impl Buffer {
/// (`NotImplemented(MadeBeforeRecovery)`) or on a lost device (`DeviceLost`), and the driver's
/// failure.
pub fn export_memory_dma_buf(&self) -> Result<ExportedMemory, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.export_memory("export_memory_dma_buf", MemoryExport::DmaBuf)
}
@@ -546,8 +546,8 @@ impl Buffer {
/// before its device's recovery (`NotImplemented(MadeBeforeRecovery)`) or on a lost device
/// (`DeviceLost`), and the driver's failure.
pub fn external_write_done(&self) -> Result<(), Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
const CALL: &str = "external_write_done";
let device = self.device();
if device.is_lost() {
+4 -4
View File
@@ -230,8 +230,8 @@ impl Buffer {
/// The `VkBuffer` stays the runtime's: the caller does not destroy it, and uses it only while
/// the buffer lives, ordered against the runtime's work on it.
pub unsafe fn vk_buffer(&self) -> (vk::Buffer, u64) {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.hand_out();
let handle = self.at_for(0, ReadApart::VkBuffer);
// A failed submission gives the work back, to the device's next submission; the caller
@@ -274,8 +274,8 @@ impl Buffer {
/// [`SharedEvent::vk_wait`], [`SharedEvent::reserve_vk_signal`]) and against the host's
/// writes to the pages, as for any use of the buffer.
pub unsafe fn vk_host_memory(&self) -> Option<VkHostMemory> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.host_memory_at(0)
}
}
+2 -2
View File
@@ -37,8 +37,8 @@ impl Buffer {
/// buffers, in range and past it; Metal's validation stops it). Refused for a managed
/// buffer's range past the buffer.
pub fn did_modify_range(&self, range: Range<u64>) -> Result<(), Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held`).
let _held = self.held();
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
const CALL: &str = "did_modify_range";
if self.storage_mode() != StorageMode::Managed {
return Ok(());
@@ -11,9 +11,10 @@ use super::device_recovery::{
};
use crate::fresh_pages::FRESH_PAGES_PAST;
use crate::{
Buffer, CommandBufferStatus, CommandQueue, Device, Error, EventRef, PixelFormat,
ResourceOptions, RuntimeGap, SharedEvent, SharedEventListener, TextureDescriptor, TextureUsage,
Buffer, CommandBufferStatus, CommandQueue, Device, Error, EventRef, ResourceOptions,
RuntimeGap, SharedEvent, SharedEventListener, TextureDescriptor,
};
use metal2vulkan::metal::{PixelFormat, TextureUsage};
use std::sync::mpsc;
use std::time::{Duration, Instant};
@@ -274,7 +275,7 @@ fn seven_recoveries_end_with_one_live_vk_device() {
}
/// A call outside command buffers' work that reaches a carried buffer's Vulkan objects (here a
/// texture over its bytes) holds the parts it read (`Buffer::held`): parked there while the
/// texture over its bytes) holds the parts it read (`Buffer::held_parts`): parked there while the
/// recovery carries the buffer and the lost generation comes to rest, it keeps the buffer's
/// parts there, which go as it ends. The buffer serves on the next generation meanwhile.
#[test]
@@ -288,10 +289,6 @@ fn a_call_under_way_keeps_the_parts_it_read_past_the_lost_generation_s_rest() {
let made_on = buffer.device().clone();
let (parked, at_hook) = mpsc::channel();
let (go, wait) = mpsc::channel::<()>();
*crate::device::lock(&crate::buffer::HELD_HOOK) = Some(Box::new(move || {
let _ = parked.send(());
let _ = wait.recv();
}));
let call = {
let buffer = buffer.clone();
std::thread::spawn(move || {
@@ -304,6 +301,13 @@ fn a_call_under_way_keeps_the_parts_it_read_past_the_lost_generation_s_rest() {
.minimum_linear_texture_alignment_for_pixel_format(PixelFormat::RGBA8Unorm)
.unwrap()
.max(16);
// This thread's call parks inside its hold of the buffer's parts.
crate::buffer::HELD_HOOK.with(|hook| {
*hook.borrow_mut() = Some(Box::new(move || {
let _ = parked.send(());
let _ = wait.recv();
}));
});
buffer
.new_texture(&descriptor, 0, row)
.map(|texture| texture.device().generation_serves())