Skip to content
Merged
89 changes: 81 additions & 8 deletions drivers/gpu/drm/asahi/g17/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,56 @@ pub(crate) struct FirmwarePages {
/// The queue record still names the context's execution identity and QoS ID.
_hold: Arc<mmu::ContextHold>,
_qos: Arc<QosLease>,
/// The context's [`Kill`], so that a queue it was detached from still sees a kill of it that
/// the firmware has not finished ([`Self::kill_unfinished`]).
kill: Arc<Kill>,
}

impl FirmwarePages {
/// As [`Context::kill_unfinished`], for the context these pages belong to.
pub(crate) fn kill_unfinished(&self) -> bool {
self.kill.unfinished()
}
/// As [`Context::acknowledge_kill`].
pub(crate) fn acknowledge_kill(&self, cookie: u64) {
self.kill.acknowledge(cookie)
}
}

/// A context's kill: the cookie of its published ContextKill (0 before; cookies start at 1), and
/// whether the firmware acknowledged that cookie (`Event::ContextKilled`). A context is killed
/// at most once.
pub(crate) struct Kill {
cookie: AtomicU64,
acknowledged: AtomicBool,
}

impl Kill {
fn new() -> Self {
Self { cookie: AtomicU64::new(0), acknowledged: AtomicBool::new(false) }
}
fn publish(&self, cookie: u64) {
self.cookie.store(cookie, Ordering::Release);
}
fn published(&self) -> bool {
self.cookie.load(Ordering::Acquire) != 0
}
/// The firmware acknowledged ContextKill `cookie`: this kill is finished if it is this one.
fn acknowledge(&self, cookie: u64) {
if cookie != 0 && self.cookie.load(Ordering::Acquire) == cookie {
self.acknowledged.store(true, Ordering::Release);
}
}
/// Published, and the firmware has not acknowledged it. The scheduler page's killed word is
/// no witness of this kill: on the M4 Pro (25G83) it read set 4 us after a kill was
/// published, in the same host pass, before the host could have seen its acknowledgement.
fn unfinished(&self) -> bool {
self.published() && !self.acknowledged.load(Ordering::Acquire)
}
/// Published, and the firmware acknowledged it.
fn finished(&self) -> bool {
self.published() && self.acknowledged.load(Ordering::Acquire)
}
}

pub(crate) struct Context {
Expand All @@ -211,9 +261,9 @@ pub(crate) struct Context {
scheduler: Arc<SchedulerStorage>,
scheduler_generation: u32,
published: AtomicBool,
/// A kill of this context was published, through a compute queue or on its render pairs'
/// behalf. A context is killed at most once.
kill_published: AtomicBool,
/// The kill of this context, through a compute queue or on its render pairs' behalf. Shared
/// with the [`FirmwarePages`] a queue keeps after its owner exited.
kill: Arc<Kill>,
/// The host's recovery generation when it first saw the kill of this context done (its
/// scheduler page's killed state), `u64::MAX` before ([`Self::restarted_since_kill`]).
kill_seen: AtomicU64,
Expand Down Expand Up @@ -371,6 +421,7 @@ impl Context {
Arc::pin_init(new_mutex!(render_id, "G17 context render pool identity"), GFP_KERNEL)?;
let order =
Arc::pin_init(new_mutex!(Order::new(), "G17 context admission order"), GFP_KERNEL)?;
let kill = Arc::new(Kill::new(), GFP_KERNEL)?;
let order_wait =
Arc::pin_init(new_condvar!("G17 context admission order wait"), GFP_KERNEL)?;
Ok(Self {
Expand All @@ -381,7 +432,7 @@ impl Context {
scheduler,
scheduler_generation,
published: AtomicBool::new(false),
kill_published: AtomicBool::new(false),
kill,
kill_seen: AtomicU64::new(u64::MAX),
closed_generation: AtomicU64::new(u64::MAX),
closed_for_open_client: AtomicBool::new(false),
Expand Down Expand Up @@ -551,6 +602,7 @@ impl Context {
_work: self.work.clone(),
_hold: self.hold.clone(),
_qos: self.qos.clone(),
kill: self.kill.clone(),
}
}
/// Called once the firmware consumed this context's scheduler release. If its work list
Expand Down Expand Up @@ -610,14 +662,35 @@ impl Context {
Ok(head)
}

/// Record that a kill of this context was published (see `kill_published`).
pub(crate) fn note_kill_published(&self) {
self.kill_published.store(true, Ordering::Release);
/// Record that the kill of this context, ContextKill `cookie`, was published.
pub(crate) fn note_kill_published(&self, cookie: u64) {
self.kill.publish(cookie);
}

/// A kill of this context was published.
pub(crate) fn kill_published(&self) -> bool {
self.kill_published.load(Ordering::Acquire)
self.kill.published()
}

/// The firmware acknowledged ContextKill `cookie` (`Event::ContextKilled`); a no-op unless it
/// is this context's kill.
pub(crate) fn acknowledge_kill(&self, cookie: u64) {
self.kill.acknowledge(cookie)
}

/// A kill of this context was published and the firmware has not acknowledged it yet. The
/// firmware handles a kill after its publication and walks the context's queues meanwhile,
/// so none of them may have its pool released before then (`compute::Queue::vacate_candidate`).
pub(crate) fn kill_unfinished(&self) -> bool {
self.kill.unfinished()
}

/// A kill of this context was published and the firmware acknowledged it (its own
/// ContextKilled event). The scheduler page's killed word does not prove this kill finished.
// No caller yet: for ending a closed context's pending kill on its acknowledgement.
#[allow(dead_code)]
pub(crate) fn kill_acknowledged(&self) -> bool {
self.kill.finished()
}

/// This context's queue closed at the host's recovery `generation`, under the device mutex:
Expand Down
17 changes: 17 additions & 0 deletions drivers/gpu/drm/asahi/g17/queue/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,15 @@ impl Queue {
// Borrow acquisition is serialized by the device mutex. Avoid
// touching the separate loan counter for an otherwise busy queue.
&& !self.source_hint.has_readers(&self.source_readers)
// A successor of the same VM would take this pool without a release while the
// firmware still walks the killed context's queues (see `vacate_candidate`).
&& !self.kill_unfinished()
}
/// The context this queue's record names has a kill published that the firmware has not
/// finished, whether its owner is still bound or exited and left only its pages.
fn kill_unfinished(&self) -> bool {
self.context().is_some_and(|context| context.kill_unfinished())
|| self.retained_owner.as_ref().is_some_and(|pages| pages.kill_unfinished())
}
pub(crate) fn bind_owner(&mut self, owner: u64, context: Arc<Context>) -> Result {
self.bind_owner_channel(owner, context, 0)
Expand Down Expand Up @@ -2257,11 +2266,19 @@ impl Queue {
})
}
/// An idle retained queue whose USC backing may be given back.
///
/// Never while the context this queue's record names has a kill published that the firmware
/// has not finished, whether its owner is still bound or exited and left only its pages. The
/// firmware handles a kill after its publication and walks the context's queues meanwhile;
/// an idle one is no exit candidate, so no kill marks it. When its FreeListRelease went out
/// within about 2 ms of the kill, the M4 Pro firmware crashed in agx_scheduler handling the
/// kill (2026-11-29 and the earlier crashes on that path).
pub(crate) fn vacate_candidate(&self) -> bool {
self.pool_phase == PoolPhase::Populated
&& self.pool.populated()
&& (self.reusable() || self.recycled_pending())
&& !self.source_hint.has_readers(&self.source_readers)
&& !self.kill_unfinished()
}
/// A recycled killed queue still holding the backing its killed work used.
pub(crate) fn recycled_pending(&self) -> bool {
Expand Down
11 changes: 11 additions & 0 deletions drivers/gpu/drm/asahi/g17/queue/compute/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ impl Queue {
&& now.saturating_sub(exit.announced.unwrap_or(exit.published)) < POLL_NS
})
}
/// The firmware acknowledged ContextKill `cookie`. If it is the kill of the context this
/// queue's record names, bound or retained after its owner exited, that kill is finished
/// (`Context::kill_unfinished`): every kill's acknowledgement passes every compute queue.
pub(crate) fn acknowledge_context_kill(&self, cookie: u64) {
if let Some(context) = self.context() {
context.acknowledge_kill(cookie);
}
if let Some(pages) = &self.retained_owner {
pages.acknowledge_kill(cookie);
}
}
/// Event consumption and record_exit are serialized by the device mutex.
/// A kill pins this exact queue transaction through its final witness;
/// duplicate, unknown and terminal cookies cannot acknowledge another owner.
Expand Down
5 changes: 3 additions & 2 deletions drivers/gpu/drm/asahi/g17/runtime/compute_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl super::Registry {
let mut matched = false;
for entry in self.compute.iter_mut().flatten() {
if let Some(mut queue) = entry.queue.as_deref_mut() {
queue.acknowledge_context_kill(cookie);
matched |= queue.acknowledge_exit(cookie);
}
}
Expand Down Expand Up @@ -376,7 +377,7 @@ impl Firmware {
// killed before anything below can fail. Keep the killed graph until processor
// stop, and the held render identities until this context's scheduler release
// is consumed or processors stop.
context.note_kill_published();
context.note_kill_published(cookie);
self.note_guilty_kill(context, hold, "compute exit");
let mut queue = self.queues.compute[index]
.as_mut()
Expand Down Expand Up @@ -603,7 +604,7 @@ impl Firmware {
// Publication is irrevocable, so mark the context killed before anything below
// can fail. The held pairs stay held until this context's scheduler release is
// consumed (`release_killed_slots` at detach).
context.note_kill_published();
context.note_kill_published(cookie);
self.note_guilty_kill(context, hold, "render kill");
self.queues.teardown.record_render_kill(index, cookie, now)?;
record_kill_wait(self.queues.teardown.take_kill_wait(index, now));
Expand Down
2 changes: 2 additions & 0 deletions tools/asahi/g17-capacity-fault-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def fixture():
channel_error: bool, heavy_run: u8, pending_failed: KVec<(u64, CompletionFence)>,
}
impl Queue {
// No context of these cases has a kill pending (`Queue::kill_unfinished`).
fn kill_unfinished(&self) -> bool { false }
fn valid_ticket(&self, _: &Active, _: &Ticket) -> Result { Ok(()) }
fn qos_owner(&self) -> u64 { 1 }
fn context(&self) -> Option<&Arc<Context>> { self.binding.as_ref().map(|binding| &binding.context) }
Expand Down
1 change: 1 addition & 0 deletions tools/asahi/g17-growth-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def fixture():
active:Fifo{len:0},replays:Fifo{len:0},quarantined:false,released:false,retirement_ready:true,
retired_by_teardown:false,released_at:0,exit:None,needs_fresh_backing:false,source_hint:ReaderHint,source_readers:AtomicU32::new(0)}}
impl Queue {
fn kill_unfinished(&self)->bool{false}
fn clear_binding_note(&mut self){if self.binding_noted{if let Some(binding)=&self.binding{binding.context.note_compute_binding(false);}self.binding_noted=false;}}
fn clear_owner(&mut self,stamp:bool){self.clear_binding_note();self.owner=None;self.channel_index=0;if stamp{self.released_at=now_ns();}}
''' + item(source,'pub(crate) fn release_owner(') + item(source,'pub(crate) fn reusable(') + '\n}\n'
Expand Down
Loading
Loading