Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions drivers/gpu/drm/asahi/g16/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,28 +1322,44 @@ impl Firmware {
complete(done, &job, Err(ECANCELED));
continue;
}
// A VM whose render pass slots were all quarantined, or that cannot acquire a
// physical PB allocator slot because the alternate slot is quarantined and the
// active slot belongs to another VM, cannot render again: cancel its queue
// immediately instead of spinning on EAGAIN and stalling unrelated compute.
// A VM whose render pass slots were all quarantined cannot render again: cancel its
// queue immediately instead of spinning on EAGAIN and stalling unrelated compute.
// One left without a physical PB allocator slot it can take (its alternate slot
// quarantined, its active slot not yet displaced) first gets a recovery slot, also
// for a render accepted before the quarantine; it is cancelled if none can be added.
if class_error.is_none()
&& matches!(command(&job, job.progress.sent()), Some(Command::Render(_)))
&& (job.resources.pb_slot_mask().is_ok_and(|(mask, home)| {
self.pools.exhausted_for_mask_mode(
&job.vm,
mask,
home,
job.resources.render_scs(),
)
}) || job
{
let passes_gone = job
.resources
.render_passes()
.is_ok_and(|n| self.slots.render_exhausted_within(n)))
{
self.engine.unpend(&job);
let _ = job.mark_terminal_retired();
complete(done, &job, Err(ECANCELED));
continue;
.is_ok_and(|n| self.slots.render_exhausted_within(n));
let pb_stranded = !passes_gone
&& job.resources.pb_slot_mask().is_ok_and(|(mask, home)| {
self.pools.exhausted_for_mask_mode(
&job.vm,
mask,
home,
job.resources.render_scs(),
)
});
if pb_stranded {
match job.resources.pb_stranded(&job.vm) {
Ok(true) => continue,
Ok(false) => {
self.engine.handoff_avoid_pipes = 0;
self.engine.handoff_avoid_pairs = 0;
break;
}
Err(_) => {}
}
}
if passes_gone || pb_stranded {
self.engine.unpend(&job);
let _ = job.mark_terminal_retired();
complete(done, &job, Err(ECANCELED));
continue;
}
}
if let Some((class, error)) = class_error {
self.mark_class_unavailable(class, error);
Expand Down
204 changes: 146 additions & 58 deletions drivers/gpu/drm/asahi/g16/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ use super::{
Shared,
};
use crate::mmu;
use core::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
use core::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicU8, Ordering};
use kernel::{
new_condvar, new_mutex, new_spinlock_irq,
new_condvar, new_mutex, new_spinlock, new_spinlock_irq,
prelude::*,
sync::{Arc, CondVar, CondVarTimeoutResult, Mutex, SetOnce, SpinLockIrq, UniqueArc},
sync::{Arc, CondVar, CondVarTimeoutResult, Mutex, SetOnce, SpinLock, SpinLockIrq, UniqueArc},
workqueue,
};

/// One pass slot index's device-lifetime render command records and the allocator that maps
Expand Down Expand Up @@ -1861,8 +1862,6 @@ struct Ready {
render_ext: [Option<Arc<RenderExtArena>>; EXT_RENDER_TIERS.len()],
/// Demand-grown contiguous PB heap extension tiers (`PB_HEAP_EXT_TIERS`).
pb_heap_ext: [Option<Arc<PbHeapExtArena>>; PB_HEAP_EXT_TIERS.len()],
/// Recovery PB/UMA allocator slots (`WARM_PB_SLOTS..PB_SLOTS`), allocated lazily after hangs.
pb_ext: [Option<Arc<PoolExtArena>>; EXT_PB_SLOTS],
/// Single-VM render pass slot target (excluding multi-VM Tier 1 expansion), gating
/// `PB_HEAP_EXT_TIERS` so concurrent light-render VMs do not allocate 256 MiB PB heaps.
single_vm_render_slots: usize,
Expand All @@ -1883,6 +1882,10 @@ impl Drop for Ready {
}
}
const MAX_PB_HEAP_SIZES: usize = 4;
/// [`Cache::late_pb`] states.
const LATE_PB_IDLE: u8 = 0;
const LATE_PB_ADDING: u8 = 1;
const LATE_PB_FAILED: u8 = 2;
const _: () = assert!(1 + PB_HEAP_EXT_TIERS.len() <= MAX_PB_HEAP_SIZES);

struct Standby {
Expand Down Expand Up @@ -2007,6 +2010,13 @@ pub(crate) struct Cache {
standby_available: CondVar,
/// The VM's client closed: no arena or surplus spare is kept here any more.
closed: AtomicBool,
/// Recovery PB/UMA allocator slots (`WARM_PB_SLOTS..PB_SLOTS`), added under `ready` after
/// hangs. The engine takes them from here for renders it accepted before they were added.
#[pin]
pb_ext: SpinLock<[Option<Arc<PoolExtArena>>; EXT_PB_SLOTS]>,
/// [`LATE_PB_IDLE`], [`LATE_PB_ADDING`] (a recovery slot for an accepted render is being
/// added off the device lock) or [`LATE_PB_FAILED`] (none could be added).
late_pb: AtomicU8,
/// Render passes across prepared jobs on this VM that have not yet retired and dropped.
active_render_passes: AtomicU32,
pub(crate) factory: Factory,
Expand Down Expand Up @@ -2037,6 +2047,8 @@ impl Cache {
),
standby_available <- new_condvar!(),
closed: AtomicBool::new(false),
pb_ext <- new_spinlock!([const { None }; EXT_PB_SLOTS], "g16::pb_ext"),
late_pb: AtomicU8::new(LATE_PB_IDLE),
active_render_passes: AtomicU32::new(0),
factory, bind, live, budget, shared }),
GFP_KERNEL,
Expand Down Expand Up @@ -2178,6 +2190,7 @@ impl Cache {
if let Some(counter) = ready.render_vm_counter.take() {
counter.fetch_sub(1, Ordering::AcqRel);
}
let pb_ext = core::mem::take(&mut *self.pb_ext.lock());
(
ready.compute.take(),
ready.compute_ext.take(),
Expand All @@ -2187,12 +2200,82 @@ impl Cache {
ready.render.take(),
core::mem::take(&mut ready.render_ext),
core::mem::take(&mut ready.pb_heap_ext),
core::mem::take(&mut ready.pb_ext),
pb_ext,
)
};
drop(ready);
}

/// Adds recovery PB/UMA allocator slots (`WARM_PB_SLOTS..PB_SLOTS`) outside `quarantined`
/// until `base`'s and their healthy slots number [`BASE_PB_SLOTS`]. Callers hold `ready`.
/// Best effort: returns how many it added.
fn grow_pb_ext(&self, base: &RenderArena, vm: &mmu::Vm, quarantined: u32) -> usize {
let healthy_slot = |slot: usize| (quarantined & (1u32 << slot)) == 0;
let present = self.pb_ext.lock().each_ref().map(Option::is_some);
let mut healthy = base
.base_pb_slots
.iter()
.filter(|&&slot| healthy_slot(slot))
.count()
+ (0..EXT_PB_SLOTS)
.filter(|&idx| present[idx] && healthy_slot(super::hw::sizes::WARM_PB_SLOTS + idx))
.count();
let mut added = 0;
for idx in 0..EXT_PB_SLOTS {
let hw_slot = super::hw::sizes::WARM_PB_SLOTS + idx;
if healthy >= BASE_PB_SLOTS {
break;
}
if present[idx] || !healthy_slot(hw_slot) {
continue;
}
// Best effort: without recovery slots, keep prior PB slots.
let Ok(ext) = PoolExtArena::new(
&self.factory,
&self.bind,
vm,
&self.live,
BASE_PB_SLOTS + idx,
hw_slot,
base.pools.heap(),
) else {
break;
};
self.pb_ext.lock()[idx] = Some(ext);
healthy += 1;
added += 1;
}
added
}

/// Adds a recovery PB/UMA allocator slot for a render of `vm` that the engine accepted
/// before a quarantine left it none to take ([`Prepared::pb_stranded`]), then has the engine
/// look at it again. Runs off the device lock. A strand leaves the VM at most one healthy
/// slot (of two healthy idle slots one is displaced), so this adds one at least, if any.
fn add_late_pb(&self, vm: &VmState) {
let added = {
let ready = self.ready.lock();
match ready.render.as_ref() {
Some(base)
if vm.status.get() == 0
&& !vm.vm.is_closed()
&& !self.closed.load(Ordering::Acquire) =>
{
let quarantined = self.shared.quarantined_pb_mask.load(Ordering::Acquire);
self.grow_pb_ext(base, &vm.vm, quarantined)
}
_ => 0,
}
};
let state = if added != 0 {
LATE_PB_IDLE
} else {
LATE_PB_FAILED
};
self.late_pb.store(state, Ordering::Release);
self.shared.wake_publish();
}

pub(crate) fn prepare(
self: &Arc<Self>,
vm: &Arc<VmState>,
Expand Down Expand Up @@ -2227,8 +2310,8 @@ impl Cache {
let compute_scs = compute_seeds.needed != 0;
let render_scs = render_seeds.needed != 0;
let (compute, compute_ext, compute_uma, render_arena, render_ext, pb_heap_ext, pb_ext) = {
// Never held by publication, completion, recovery, fence signalling or
// standby waits, so Cache::close() and same-VM compute SUBMITs never block.
// Never held by publication, completion, recovery or standby waits, so close() and
// SUBMITs never block. A held render waits for it (add_late_pb): no GPU wait under it.
let mut ready = self.ready.lock();
if vm.status.get() != 0 || vm.vm.is_closed() || self.closed.load(Ordering::Acquire) {
return Err(EIO);
Expand Down Expand Up @@ -2346,49 +2429,7 @@ impl Cache {
}
if render {
if let Some(base) = ready.render.as_ref() {
let quarantined = hints.quarantined_pb_mask;
let mut healthy = base
.base_pb_slots
.iter()
.filter(|&&slot| (quarantined & (1u32 << slot)) == 0)
.count();
for (idx, ext) in ready.pb_ext.iter().enumerate() {
let hw_slot = super::hw::sizes::WARM_PB_SLOTS + idx;
if ext.is_some() && (quarantined & (1u32 << hw_slot)) == 0 {
healthy += 1;
}
}
if healthy < BASE_PB_SLOTS {
let heap = base.pools.heap().clone();
for idx in 0..EXT_PB_SLOTS {
if healthy >= BASE_PB_SLOTS {
break;
}
let hw_slot = super::hw::sizes::WARM_PB_SLOTS + idx;
if ready.pb_ext[idx].is_some()
|| (quarantined & (1u32 << hw_slot)) != 0
{
continue;
}
let local_idx = BASE_PB_SLOTS + idx;
// Best effort: without recovery slots, keep prior PB slots.
ready.pb_ext[idx] = PoolExtArena::new(
&self.factory,
&self.bind,
&vm.vm,
&self.live,
local_idx,
hw_slot,
&heap,
)
.ok();
if ready.pb_ext[idx].is_some() {
healthy += 1;
} else {
break;
}
}
}
self.grow_pb_ext(base, &vm.vm, hints.quarantined_pb_mask);
}
let current_slots = BASE_RENDER_SLOTS
+ ready
Expand Down Expand Up @@ -2478,7 +2519,7 @@ impl Cache {
[const { None }; PB_HEAP_EXT_TIERS.len()]
};
let pb_ext = if render {
core::array::from_fn(|idx| ready.pb_ext[idx].clone())
self.pb_ext.lock().clone()
} else {
[const { None }; EXT_PB_SLOTS]
};
Expand Down Expand Up @@ -2644,7 +2685,13 @@ impl Cache {
render_scs,
render_ext,
pb_heap_ext,
pb_ext,
pb_ext: pb_ext.map(|ext| {
let cell = SetOnce::new();
if let Some(ext) = ext {
cell.populate(ext);
}
cell
}),
active_renders: AtomicU32::new(render_count),
unpublished_renders: AtomicU32::new(render_count),
pb_heap_blocks: target_heap_blocks,
Expand All @@ -2669,8 +2716,9 @@ pub(crate) struct Prepared {
pub(crate) render_ext: [Option<Arc<RenderExtArena>>; EXT_RENDER_TIERS.len()],
/// The VM's demand-grown contiguous PB heap extension tiers when the job was prepared.
pub(crate) pb_heap_ext: [Option<Arc<PbHeapExtArena>>; PB_HEAP_EXT_TIERS.len()],
/// The VM's lazily allocated recovery PB/UMA allocator slots when the job was prepared.
pub(crate) pb_ext: [Option<Arc<PoolExtArena>>; EXT_PB_SLOTS],
/// The VM's recovery PB/UMA allocator slots: those it had when the job was prepared, and
/// those added later for it ([`Self::pb_stranded`]).
pb_ext: [SetOnce<Arc<PoolExtArena>>; EXT_PB_SLOTS],
/// Render commands still accounted against `Cache::active_render_passes`.
active_renders: AtomicU32,
/// Render commands of this job whose standby reservation has not yet been committed.
Expand Down Expand Up @@ -2789,13 +2837,53 @@ impl Prepared {
}
if !self.render_scs {
for (idx, ext) in self.pb_ext.iter().enumerate() {
if ext.is_some() {
if ext.as_ref().is_some() {
mask |= 1u32 << (super::hw::sizes::WARM_PB_SLOTS + idx);
}
}
}
Ok((mask, base.base_pb_slots[0]))
}
/// The engine, drained, found no PB/UMA allocator slot this render can take: a quarantine
/// left its VM only a slot that another render has to displace first. Takes the VM's
/// recovery slots added since the job was prepared, or has one added off the device lock.
/// `Ok(true)`: look again now. `Ok(false)`: wait; the addition repolls publication.
/// `Err`: none can be added (an SCS render takes base slots only).
pub(crate) fn pb_stranded(&self, vm: &Arc<VmState>) -> Result<bool> {
let cache = self
.cache
.as_ref()
.filter(|_| !self.render_scs)
.ok_or(ENOSPC)?;
// Only the engine leaves IDLE and FAILED, under the device lock.
let state = cache.late_pb.load(Ordering::Acquire);
let mut adopted = false;
for (cell, ext) in self.pb_ext.iter().zip(cache.pb_ext.lock().iter()) {
if let (None, Some(ext)) = (cell.as_ref(), ext) {
adopted |= cell.populate(ext.clone());
}
}
if state == LATE_PB_FAILED {
cache.late_pb.store(LATE_PB_IDLE, Ordering::Release);
}
match state {
_ if adopted => Ok(true),
LATE_PB_ADDING => Ok(false),
LATE_PB_FAILED => Err(ENOSPC),
_ => {
cache.late_pb.store(LATE_PB_ADDING, Ordering::Release);
let (owner, vm) = (cache.clone(), vm.clone());
if workqueue::system_unbound()
.try_spawn(GFP_NOWAIT, move || owner.add_late_pb(&vm))
.is_err()
{
cache.late_pb.store(LATE_PB_IDLE, Ordering::Release);
return Err(ENOMEM);
}
Ok(false)
}
}
}
fn pb_allocator(&self, k: usize) -> Result<&super::pools::Allocator> {
let base = self.render()?;
if let Some(local) = base.base_pb_slots.iter().position(|&slot| slot == k) {
Expand All @@ -2814,7 +2902,7 @@ impl Prepared {
let idx = k.checked_sub(super::hw::sizes::WARM_PB_SLOTS).ok_or(EINVAL)?;
self.pb_ext
.get(idx)
.and_then(|slot| slot.as_deref())
.and_then(SetOnce::as_ref)
.map(|arena| &arena.allocator)
.ok_or(EINVAL)
}
Expand Down Expand Up @@ -2868,7 +2956,7 @@ impl Prepared {
let idx = k.checked_sub(super::hw::sizes::WARM_PB_SLOTS)?;
self.pb_ext
.get(idx)
.and_then(|slot| slot.as_ref())
.and_then(SetOnce::as_ref)
.map(|ext| PbOwnerArena::Ext(base.clone(), ext.clone(), heap_ext))
}
}
Expand Down
Loading
Loading