Runtime: the arena's block take runs off the arena's lock
arena::take held the device's arena lock across Core::take_memory. A take that no free range fits makes an allocation on the dispatching thread and runs the consumer's BlockMappings::map there, so the hook ran under the arena's lock: another thread's dispatch whose kernel keeps an arena waited for that lock, and a hook that waited for such a thread never returned. Now the lock is taken once to read the device's last block (the fast path is unchanged: one lock and the Weak's upgrade, nothing added where a block fits) and once more, after the take, to store the new block's Weak. Two dispatches that miss at once each take their own block and keep it, held by its command buffer until it retires; the device keeps the longer as its last. The arena stays within its bound: each block is its own dispatch's, at most the budget or one workgroup's slots where those pass it, and a race adds one such block for each dispatch that missed, never a longer one. A block read under the lock and found too short is dropped off the lock. BlockMappings::map and ::unmap now say "never under the pool's or the arena's lock", and that a hook never waits for a lock a thread inside a runtime call holds. Callers checked for map (memory.rs Pool::map): Pool::own from Pool::take, reached through Core::take_memory by Device::take_block (no lock), places.rs texture places (blocks lock dropped first), the visible function table (no lock), arena::take (fixed here), the indirect template (templates guard ends with its if-let) and indirect command buffers (no lock); ahead() on the block-maker thread (pool lock dropped first) and stock() in Device::new (no lock). For unmap: Pool::map's late unmap (after its drop), free_idle on the block-maker thread (pool lock dropped first), unmap_all from halt (quiesce and the last drop's teardown, the threads lock released first) and free_all at the core's drop (&mut, no lock). "No runtime lock" is not claimed: a device's last drop, which runs unmap_all, can happen wherever a handle drops, and those sites are not all audited. Verified by the device test of the next commit on lavapipe, and by the same test over the unfixed take as a negative control (jobs in the lane report).
This commit is contained in:
1 parent
d0664f2c10
commit
243657ef33
2 files changed
+36
-9
No files matched your search
@@ -70,14 +70,24 @@ pub(crate) fn piece(
|
||||
/// the dispatch took it new: the device's last, where a command buffer still keeps it and it holds
|
||||
/// `len`, else a block of `len` rounded up to a power of two, within `budget` where `len` is.
|
||||
///
|
||||
/// The arena's lock is held only to read the device's last block and to store a new one, never
|
||||
/// across the take: a take may make an allocation and run the consumer's
|
||||
/// [`BlockMappings::map`](crate::BlockMappings::map) on this thread, and another thread's dispatch
|
||||
/// goes on meanwhile. Two dispatches that find no block holding their slots at once each take
|
||||
/// their own and keep it, held by its command buffer until it retires, even where the other's,
|
||||
/// stored meanwhile, would hold their slots; the device keeps the longer of the two as its last.
|
||||
/// So the arena stays within its bound: each block is its own dispatch's, at most `budget`, or
|
||||
/// one workgroup's slots where those pass it, as a block taken with no race is, and a race adds
|
||||
/// one such block for each dispatch that missed at once, never a longer one.
|
||||
///
|
||||
/// Err: what [`Core::take_memory`] returns.
|
||||
pub(crate) fn take(
|
||||
core: &Arc<Core>,
|
||||
len: u64,
|
||||
budget: u64,
|
||||
) -> Result<(Arc<memory::Block>, bool), Error> {
|
||||
let mut last = lock(&core.arena);
|
||||
if let Some(block) = last.upgrade().filter(|block| block.memory.len >= len) {
|
||||
let last = lock(&core.arena).upgrade();
|
||||
if let Some(block) = last.filter(|block| block.memory.len >= len) {
|
||||
return Ok((block, false));
|
||||
}
|
||||
let len = len
|
||||
@@ -86,7 +96,17 @@ pub(crate) fn take(
|
||||
.min(budget.max(len));
|
||||
let memory = core.take_memory(MemoryClass::Private, len, Site::PrivateArena)?;
|
||||
let block = Arc::new(memory::Block::new(memory, Arc::clone(core)));
|
||||
*last = Arc::downgrade(&block);
|
||||
let mut last = lock(&core.arena);
|
||||
let kept = last.upgrade();
|
||||
if kept
|
||||
.as_ref()
|
||||
.is_none_or(|kept| kept.memory.len < block.memory.len)
|
||||
{
|
||||
*last = Arc::downgrade(&block);
|
||||
}
|
||||
drop(last);
|
||||
// Where it was the last holder of the block it read, the block goes back here, off the lock.
|
||||
drop(kept);
|
||||
core.counters.count(Metric::PrivateArenaBlocks);
|
||||
Ok((block, true))
|
||||
}
|
||||
|
||||
@@ -320,10 +320,16 @@ pub trait BlockMappings: Send + Sync {
|
||||
///
|
||||
/// It runs where the allocation is made: on the block-maker thread for the allocations made
|
||||
/// ahead of need, on the thread in [`Device::new`](crate::Device::new) for each class's first,
|
||||
/// and inside [`Device::take_block`](crate::Device::take_block), on the consumer's thread, for
|
||||
/// a take that no free range fits. Never under the pool's lock. It makes no call into the
|
||||
/// runtime and drops no runtime handle. `allocation` states when the make began and how long
|
||||
/// it took ([`AllocationInfo::make_start`], [`AllocationInfo::make_nanos`]).
|
||||
/// and on the consumer's thread inside a call whose take no free range fits:
|
||||
/// [`Device::take_block`](crate::Device::take_block), and the calls that take a block of their
|
||||
/// own (a texture's place, a visible function table, an indirect command buffer, a dispatch
|
||||
/// whose kernel's private memory takes a new block of the device's arena). Never under the
|
||||
/// pool's or the arena's lock. It makes no call into the runtime and drops no runtime handle,
|
||||
/// and it never waits for a lock that a thread inside a runtime call holds: that call may be
|
||||
/// waiting for the hook (a call that waits for a make of the block-maker thread, which runs the
|
||||
/// hooks between its makes, or the device's last drop), so such a wait never ends.
|
||||
/// `allocation` states when the make began and how long it took
|
||||
/// ([`AllocationInfo::make_start`], [`AllocationInfo::make_nanos`]).
|
||||
fn map(&self, allocation: &AllocationInfo) -> bool;
|
||||
|
||||
/// Returns only once every mapping of `allocation` the consumer made is gone and none of its
|
||||
@@ -333,8 +339,9 @@ pub trait BlockMappings: Send + Sync {
|
||||
/// It runs once per allocation `map` accepted: on the block-maker thread before an idle
|
||||
/// allocation's free; in [`Device::quiesce`](crate::Device::quiesce), on the thread that calls
|
||||
/// it, for every allocation still mapped; or at teardown, on the thread that ends it, once the
|
||||
/// runtime's other threads are joined. Never under the pool's lock. It makes no call into the
|
||||
/// runtime and drops no runtime handle.
|
||||
/// runtime's other threads are joined. Never under the pool's or the arena's lock. It makes no
|
||||
/// call into the runtime and drops no runtime handle, and, as [`map`](Self::map), it never
|
||||
/// waits for a lock that a thread inside a runtime call holds.
|
||||
fn unmap(&self, allocation: AllocationId);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user