Files
metal2vulkan/crates/metal2vulkan-runtime/src/address_pool.rs
T
Anees Iqbal ab8651ae38 Run a device call again on the serving generation where its own was lost under it
tests::device_recovery::each_recovery_announces_its_own_wait failed on the M3
in 10 of 30 runs at 2fed3c2453 (m3-max-7120): a waiter thread's
`device.new_buffer(..)` returned NotImplemented(MadeBeforeRecovery) while
another thread recovered the device. A public call of a root handle took the
serving generation (Device::current -> Line::current), the recovery marked it
lost, and the call's own check of the loss (new_buffer_placed's is_lost) then
refused it through lost_refusal, whose line had gone past the generation. The
call came from the device the program holds, which Metal keeps across a
recovery: it must not be refused because the generation it took was lost
while it ran (rule 2).

The device's public calls now run through Device::at_serving: a run refused
for its generation's loss (DeviceLost, or MadeBeforeRecovery as lost_refusal
answers it) where the line went past that generation runs again on the one
serving next. A run refused so kept nothing, so the call runs whole again.
Each run is on a later generation than the one before, after its recovery,
which Line::current waits for, announced past RECOVERY_NOTICE as before; a
line that cannot recover still answers the loss, and a thread command
buffers' completion depends on, whose calls reach the lost generation, is
answered as before (no retry). A call refused for an object of an earlier
generation while its own generation serves is answered as before.

The calls that consume arguments a refused run could not hand to a second
one (host memory and its deallocator or keep-alive, a range token, a buffer
descriptor's contents) run through Device::at_serving_taking: they go to the
generation once it is not lost, else to the next, and from there a loss of
it follows the call: the thread's RunsOn scope makes Device::is_lost answer
false for that generation to the call's own checks, so the call completes on
it as one the loss came after, its object made before the recovery. The two
texture calls with a range token make the texture through at_serving and keep
the token on it after (their _now forms, used by nothing else, are gone). The
calls that wrap the consumer's own Vulkan objects (new_texture_with_vk_image,
_plane, new_buffer_with_vk_buffer) are unchanged: those objects belong to the
VkDevice the caller made them on.

Tests: device_recovery gains a call whose first run meets a recovery and runs
again on the next generation, and a taking call that completes on the
generation lost after it took its arguments. Docs: the runtime README's lost
device section, API.md's device-recovered notice, line.rs's module docs.

(cherry picked from commit 3af6741dcf79b99a608ea0a464b98734dac52ba5)
2026-11-08 00:14:43 -08:00

90 lines
3.5 KiB
Rust

//! A pool of the Metal address space whose slots buffers are placed in (the consumer's ask
//! "sub-page placement", `address.rs`' module docs).
//!
//! Metal makes each buffer of its own memory shorter than a page in a pool of its own, at a
//! 256-byte-aligned slot of `round_up(length, 256)` bytes, so several buffers share a 16-KiB page
//! (the consumer's `vaprobe smallpool` on an M3 Max under macOS 26.7). A caller that models those
//! pools reserves each ([`Device::reserve_address_pool`]) and places each small buffer at the slot
//! its model gives (`BufferDescriptor::with_pool_slot`): the buffer answers Metal's address.
use super::address::{AddressPlacement, GpuAddress};
use super::device::lock;
use super::error::Error;
use super::Device;
/// A reserved range of the Metal address space whose slots buffers are placed in
/// (`BufferDescriptor::with_pool_slot`; a runtime extension, no Metal counterpart: Metal's own
/// buffer pools, which hold every buffer shorter than a page, are not objects of its API). While
/// it lives, no other buffer or heap, pinned or not, lands in its span. Dropping it lets the span
/// go, at once where no buffer lies in a slot of it, else once the last such buffer is gone.
pub struct AddressPool {
device: Device,
address: GpuAddress,
length: u64,
}
impl AddressPool {
/// The pool's first byte: a slot's buffer at `offset` answers this plus `offset`.
pub fn gpu_address(&self) -> GpuAddress {
self.address
}
/// The pool's bytes, which its slots lie in.
pub fn length(&self) -> u64 {
self.length
}
/// The device whose address space the pool is of.
pub fn device(&self) -> &Device {
&self.device
}
}
impl std::fmt::Debug for AddressPool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AddressPool")
.field("gpu_address", &self.address)
.field("length", &self.length)
.finish()
}
}
impl Drop for AddressPool {
fn drop(&mut self) {
lock(&self.device.shared().addresses).release_pool(self.address);
}
}
impl Device {
/// Reserve a pool of `length` bytes of the Metal address space at `placement` (a runtime
/// extension, no Metal counterpart; `address.rs`' module docs): the pool holds the span a
/// buffer of `length` bytes pinned at its address holds (the pages its bytes touch and one
/// guard page, rounded up to 32 KiB), so no other placement, pin or range the runtime picks
/// lands in it while it lives. Buffers go in its slots (`BufferDescriptor::with_pool_slot`).
///
/// At `AddressPlacement::At`, refused as a pin is (`PlacementConflict`: `Held`,
/// `OffGranule`, `OutsideSpace`); at `Any` or `Outside`, at a 32-KiB granule the runtime
/// picks in the space or outside it, refused `OutOfMemory` where no span is free.
pub fn reserve_address_pool(
&self,
placement: AddressPlacement,
length: u64,
) -> Result<AddressPool, Error> {
self.at_serving(|device| device.reserve_address_pool_now(placement, length))
}
/// [`Device::reserve_address_pool`] on the generation that serves (`device/line.rs`).
pub(crate) fn reserve_address_pool_now(
&self,
placement: AddressPlacement,
length: u64,
) -> Result<AddressPool, Error> {
let address = (self.address_space_for(placement)).reserve_pool(placement, length)?;
Ok(AddressPool {
device: self.clone(),
address,
length,
})
}
}