From 732d813c51ad0e10fa73a118d20dbb731580d8d7 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:26:04 +1100 Subject: [PATCH 1/2] drm/asahi: g17: let an exhausted excuse end the blocker's allowance A compute command of a queue that asked for a watchdog allowance is not failed while it runs inside that allowance, and work of another VM queued behind it on the GPU is excused meanwhile. The excuse is bounded per waiting job, the longest allowance the system permits, but every command of the long queue starts an allowance of its own. When the queue keeps submitting long commands, the waiting job runs out of its excuse and its deferrals and is failed as spared, while the queue that holds the GPU keeps running. Charge the end of the excuse to the blocker instead. Once a waiting job has used its whole excuse and is deferred behind a live blocker that is still inside its allowance, the blocker loses the rest of its allowance and its other renewals; its next expiry goes straight to attribution, and the waiting job's remaining deferrals cover the teardown. The blocker is failed as spared, not as hung. The command the waiting job was first excused behind keeps its allowance: it started before the wait, and an allowance placed late (after a wait in publication) could otherwise end after the excuse. A blocker that itself waits behind older work of the waiting job's VM keeps it too, and a waiting job that is spared anyway does not take the blocker with it. Signed-off-by: Ryan Murray --- drivers/gpu/drm/asahi/g17/job.rs | 90 +++++++++---- drivers/gpu/drm/asahi/g17/queue/compute.rs | 12 ++ drivers/gpu/drm/asahi/g17/runtime/backend.rs | 11 ++ .../drm/asahi/g17/runtime/compute_control.rs | 17 ++- drivers/gpu/drm/asahi/g17/runtime/render.rs | 26 ++++ drivers/gpu/drm/asahi/g17/timeout.rs | 120 ++++++++++++++++++ include/uapi/drm/asahi_drm.h | 28 ++-- tools/asahi/g17-compute-owed-stamp-tests.py | 15 ++- 8 files changed, 281 insertions(+), 38 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/job.rs b/drivers/gpu/drm/asahi/g17/job.rs index b6536c07b443f..dacaf2a4832bc 100644 --- a/drivers/gpu/drm/asahi/g17/job.rs +++ b/drivers/gpu/drm/asahi/g17/job.rs @@ -131,6 +131,12 @@ pub(crate) struct Packet { /// When the job's watchdog allowance ends (host ns, [`super::timeout::Allowance::end`]): /// zero under the default allowance, `u64::MAX` for a longer one not yet placed. allowance_end: AtomicU64, + /// Another VM's watchdog took the rest of the allowance away + /// ([`super::timeout::Verdict::forfeits`]). + forfeited: AtomicBool, + /// The publication of the command this job was first excused behind + /// ([`super::timeout::Verdict::first_excuse`]); zero before that. + first_excuse: AtomicU64, } impl Packet { @@ -164,6 +170,8 @@ impl Packet { early: AtomicI32::new(0), backlog: AtomicBool::new(false), allowance_end: AtomicU64::new(0), + forfeited: AtomicBool::new(false), + first_excuse: AtomicU64::new(0), }, GFP_KERNEL, )?) @@ -172,7 +180,30 @@ impl Packet { /// Its own watchdog still renews the job for the allowance its queue asked for at `now` /// ([`super::timeout::Head::excused`]). pub(crate) fn within_allowance(&self, now: u64) -> bool { - now < self.allowance_end.load(Ordering::Relaxed) + !self.forfeited.load(Ordering::Relaxed) && now < self.allowance_end.load(Ordering::Relaxed) + } + + /// Ends the job's allowance and every other renewal its own watchdog would grant it + /// ([`super::timeout::Verdict::forfeits`]): its next expiry goes straight to the + /// attribution. Returns whether the job had not lost them already. + pub(crate) fn forfeit_allowance(&self) -> bool { + !self.forfeited.swap(true, Ordering::Relaxed) + } + + /// Whether another VM's watchdog took the job's allowance away. + pub(crate) fn allowance_forfeited(&self) -> bool { + self.forfeited.load(Ordering::Relaxed) + } + + /// Records the command the job was first excused behind (`published`), once. + pub(crate) fn note_first_excuse(&self, published: u64) { + let _ = self.first_excuse.compare_exchange(0, published, Ordering::Relaxed, + Ordering::Relaxed); + } + + /// The command the job was first excused behind; zero if none. + pub(crate) fn first_excuse(&self) -> u64 { + self.first_excuse.load(Ordering::Relaxed) } pub(crate) fn engine(&self) -> Engine { @@ -418,6 +449,35 @@ impl PublishedExpiry { } } +impl Timeout { + /// Attributes an expiry that no renewal of the job's own covered: deferred or excused + /// behind other work, or given up. `renews_shared` and `since_retired` are the sharing + /// inputs the renewals were decided on, for the log line. + fn settle(&mut self, renews_shared: bool, since_retired: Option) -> bool { + match self.backend.defer_timeout(&self.packet, self.victim_renewals, self.excused) { + super::timeout::Decision::Defer => { + self.victim_renewals = self.victim_renewals.saturating_add(1); + true + } + super::timeout::Decision::Excuse => { + self.excused = self.excused.saturating_add(1); + true + } + // The backend retired the job on its completion record: its fence has signalled. + super::timeout::Decision::Retire => true, + decision @ (super::timeout::Decision::Own | super::timeout::Decision::Spare) => { + // The sharing inputs are the ones the renewal above was decided on. + self.backend.gave_up(&self.packet, &super::timeout::GaveUp::new( + &self.allowance, super::now_ns(), self.packet.command.long_job(), + self.execution_renewals, renews_shared, self.shared_renewals, since_retired, + self.victim_renewals, self.excused, decision, + )); + false + } + } + } +} + impl Drop for Job { fn drop(&mut self) { // Entity teardown can free a queued job without invoking cancel. @@ -451,6 +511,12 @@ impl sched::JobImpl for Job { if finished || job.packet.completion.take_replay_timeout() { return true; } + // A job whose allowance another VM's watchdog took away gets no renewal of its own + // left; only the attribution below decides between failing it and deferring it. + if job.packet.allowance_forfeited() { + let since_retired = super::recovery::since_retired(super::now_ns()); + return job.settle(job.backend.renews_shared(), since_retired); + } // Within the allowance its queue asked for. Past it, the deferrals below still apply; // the shared renewals run alongside an allowance rather than after it. let placing = job.allowance.placing_after_wait(job.packet.is_published()); @@ -479,27 +545,7 @@ impl sched::JobImpl for Job { if super::timeout::renew_shared(job.packet.is_published(), shared, &mut job.shared_renewals) { return true; } - match job.backend.defer_timeout(&job.packet, job.victim_renewals, job.excused) { - super::timeout::Decision::Defer => { - job.victim_renewals = job.victim_renewals.saturating_add(1); - true - } - super::timeout::Decision::Excuse => { - job.excused = job.excused.saturating_add(1); - true - } - // The backend retired the job on its completion record: its fence has signalled. - super::timeout::Decision::Retire => true, - decision @ (super::timeout::Decision::Own | super::timeout::Decision::Spare) => { - // The sharing inputs are the ones the renewal above was decided on. - job.backend.gave_up(&job.packet, &super::timeout::GaveUp::new( - &job.allowance, super::now_ns(), job.packet.command.long_job(), - job.execution_renewals, renews_shared, job.shared_renewals, since_retired, - job.victim_renewals, job.excused, decision, - )); - false - } - } + job.settle(renews_shared, since_retired) } fn prepare(job: &mut Self) -> Option { diff --git a/drivers/gpu/drm/asahi/g17/queue/compute.rs b/drivers/gpu/drm/asahi/g17/queue/compute.rs index 5492a740759e5..360d8de3ffaf5 100644 --- a/drivers/gpu/drm/asahi/g17/queue/compute.rs +++ b/drivers/gpu/drm/asahi/g17/queue/compute.rs @@ -948,6 +948,18 @@ impl Queue { }) } + /// Takes the rest of its allowance from the command at [`Self::timeout_head`] if that is + /// `head` ([`crate::g17::timeout::Verdict::forfeits`]). Returns whether it lost one. + pub(crate) fn forfeit_head(&self, head: crate::g17::timeout::Head) -> bool { + if self.timeout_head() != Some(head) { + return false; + } + self.active + .iter() + .find(|active| !active.stamps_owed) + .is_some_and(|active| active.packet.forfeit_allowance()) + } + pub(crate) fn can_classify_timeout(&self, packet: &Arc) -> bool { !self.quarantined && self.owns_packet(packet) } diff --git a/drivers/gpu/drm/asahi/g17/runtime/backend.rs b/drivers/gpu/drm/asahi/g17/runtime/backend.rs index f9d2999a4e1d5..3dd048f14086b 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/backend.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/backend.rs @@ -460,6 +460,17 @@ impl job::Backend for Backend { decision, blocker.vm, blocker.published, u8::from(blocker.failed), u8::from(blocker.excused)); } + if let Some(published) = verdict.first_excuse(excuse) { + packet.note_first_excuse(published); + } + if let Some(blocker) = + firmware.queues.forfeit_blocker(&verdict, excuse, packet.first_excuse()) + { + dev_warn!(self.shared.dev.as_ref(), + "Render watchdog owner={} vm={} publication={} excused for the longest allowance: blocker_vm={} blocker_publication={} loses its allowance\n", + self.owner, verdict.own.vm, verdict.own.published, blocker.vm, + blocker.published); + } } Ok(decision) })(); diff --git a/drivers/gpu/drm/asahi/g17/runtime/compute_control.rs b/drivers/gpu/drm/asahi/g17/runtime/compute_control.rs index 2bad8071087fd..69f107b48c5f9 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/compute_control.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/compute_control.rs @@ -47,7 +47,12 @@ impl Backend { verdict.decision = self.retire_on_record(firmware, packet, &mut deferred)?; } let decision = verdict.decision; - if decision == Decision::Spare { + // A command that lost its allowance to another VM's waiting work + // ([`timeout::Verdict::forfeits`]) is failed for that, not found hung: it is spared, + // as the waiting work would have been. + if decision == Decision::Spare + || (decision == Decision::Own && packet.allowance_forfeited()) + { firmware.queues.compute(self.owner).ok_or(EIO)? .classify(&|_| Class::Spared); } @@ -58,6 +63,16 @@ impl Backend { blocker.vm, blocker.published, u8::from(blocker.failed), u8::from(blocker.excused)); } + if let Some(published) = verdict.first_excuse(excuse) { + packet.note_first_excuse(published); + } + if let Some(blocker) = + firmware.queues.forfeit_blocker(&verdict, excuse, packet.first_excuse()) + { + dev_warn!(self.shared.dev.as_ref(), + "Compute watchdog owner={} vm={} publication={} excused for the longest allowance: blocker_vm={} blocker_publication={} loses its allowance\n", + self.owner, own.vm, own.published, blocker.vm, blocker.published); + } Ok(decision) })(); deferred.finish(); diff --git a/drivers/gpu/drm/asahi/g17/runtime/render.rs b/drivers/gpu/drm/asahi/g17/runtime/render.rs index ab33c149ba96d..de94d7f7d3a17 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/render.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/render.rs @@ -1372,6 +1372,32 @@ impl Registry { ) }) } + + /// Takes the rest of its allowance from the compute command a waiting job's `verdict` + /// charges ([`crate::g17::timeout::Verdict::forfeits`]; only compute commands have one), + /// given the job's excuse and the command it was first excused behind. Returns that + /// command if it lost its allowance here. + pub(in crate::g17) fn forfeit_blocker( + &self, + verdict: &crate::g17::timeout::Verdict, + excuse: crate::g17::timeout::Excuse, + first: u64, + ) -> Option { + let computes = self + .compute + .iter() + .flatten() + .filter_map(|entry| entry.queue.as_deref()) + .filter_map(|queue| queue.timeout_head()); + let blocker = verdict.forfeits(excuse, first, + Iterator::chain(self.render_timeout_heads(), computes))?; + self.compute + .iter() + .flatten() + .filter_map(|entry| entry.queue.as_deref()) + .any(|queue| queue.forfeit_head(blocker)) + .then_some(blocker) + } } impl RenderState { diff --git a/drivers/gpu/drm/asahi/g17/timeout.rs b/drivers/gpu/drm/asahi/g17/timeout.rs index 7e1793a02a157..c253aa5a00ac0 100644 --- a/drivers/gpu/drm/asahi/g17/timeout.rs +++ b/drivers/gpu/drm/asahi/g17/timeout.rs @@ -380,6 +380,40 @@ impl Verdict { Decision::Retire => false, }) } + + /// The blocker a waiting job is first excused behind, by its publication: the command + /// that held the GPU when the waiting job started waiting. See [`Self::forfeits`]. + pub(crate) fn first_excuse(&self, excuse: Excuse) -> Option { + self.blocker + .filter(|_| self.decision == Decision::Excuse && excuse.used == 0) + .map(|blocker| blocker.published) + } + + /// The blocker that loses its allowance at this decision. + /// + /// The excuse ([`decide`]) is bounded per waiting job, but each command of a queue gets an + /// allowance of its own, so a queue kept full of long commands would excuse its blockers + /// one after the other until the waiting job is spared. Once the waiting job has used its + /// whole [`Excuse::limit`] and is deferred behind a live blocker that is still inside its + /// allowance, the blocker loses what is left of that allowance instead, so that the + /// waiting job's remaining renewals see it settled. Not the command the waiting job was + /// first excused behind (`first`, [`Self::first_excuse`]): that one started before the + /// wait and only runs on inside its own allowance. Nor a blocker that itself waits behind + /// older work of the waiting job's VM (`others`, every head), which does not hold it up. + pub(crate) fn forfeits(&self, excuse: Excuse, first: u64, + others: impl Iterator) -> Option { + let candidate = self.blocker.filter(|blocker| { + self.decision == Decision::Defer + && blocker.excused + && !blocker.failed + && excuse.used >= excuse.limit + && blocker.published != first + })?; + match blocker(candidate, others) { + Some(ahead) if ahead.vm == self.own.vm => None, + _ => Some(candidate), + } + } } pub(crate) fn attribute(own: Head, renewals: u8, excuse: Excuse, @@ -771,6 +805,92 @@ mod tests { assert_eq!(POLL_MS * (excuse_limit(30_000) + u32::from(MAX_RENEWALS)), 46_000); } #[test] + fn an_exhausted_excuse_charges_the_blocker() { + let own = head(9, 103, false); + let used = Excuse { used: FRESH.limit, ..FRESH }; + let none = || core::iter::empty::(); + // The first excuse names its blocker; inside the excuse nobody loses anything. + let inside = attribute(own, 0, FRESH, [excused(5, 100)].into_iter()); + assert_eq!(inside.decision, Decision::Excuse); + assert_eq!(inside.first_excuse(FRESH), Some(100)); + assert_eq!(inside.forfeits(FRESH, 0, none()), None); + let later = Excuse { used: 3, ..FRESH }; + assert_eq!(attribute(own, 0, later, [excused(5, 100)].into_iter()).first_excuse(later), + None); + // Once it is used up, a later blocker still inside its allowance loses it. + let spent = attribute(own, 0, used, [excused(5, 101)].into_iter()); + assert_eq!(spent.decision, Decision::Defer); + assert_eq!(spent.forfeits(used, 100, none()), Some(excused(5, 101))); + // Not the command the job was first excused behind: it runs inside its own allowance. + let first = attribute(own, 0, used, [excused(5, 100)].into_iter()); + assert_eq!(first.forfeits(used, 100, none()), None); + // A job spared anyway does not take the blocker with it. + let late = attribute(own, MAX_RENEWALS, used, [excused(5, 101)].into_iter()); + assert_eq!(late.decision, Decision::Spare); + assert_eq!(late.forfeits(used, 100, none()), None); + // A blocker without an allowance, or one that already failed, has nothing to lose. + assert_eq!(attribute(own, 0, used, [head(5, 101, false)].into_iter()) + .forfeits(used, 100, none()), None); + let failed_in_allowance = Head { excused: true, ..failed(5, 101) }; + assert_eq!(attribute(own, 0, used, [failed_in_allowance].into_iter()) + .forfeits(used, 100, none()), None); + // Finished own work is retired, whatever is ahead. + assert_eq!(attribute(head(9, 103, true), 0, used, [excused(5, 101)].into_iter()) + .forfeits(used, 100, none()), None); + } + #[test] + fn a_blocker_waiting_on_the_waiting_vm_keeps_its_allowance() { + let own = head(9, 103, false); + let used = Excuse { used: FRESH.limit, ..FRESH }; + let spent = attribute(own, 0, used, [excused(5, 101)].into_iter()); + // Older live work of the waiting job's own VM is ahead of the blocker: not its fault. + assert_eq!(spent.forfeits(used, 100, [head(9, 99, false)].into_iter()), None); + // Older work of a third VM, or newer work of the waiting VM, changes nothing. + assert_eq!(spent.forfeits(used, 100, [head(7, 99, false)].into_iter()), + Some(excused(5, 101))); + assert_eq!(spent.forfeits(used, 100, [head(9, 102, false), own].into_iter()), + Some(excused(5, 101))); + } + #[test] + fn blockers_taking_turns_lose_their_allowance_not_the_waiting_job() { + // Commands of one long queue reach the head by turns while another VM's job waits. + // With the forfeit applied, the command ahead once the excuse is used up loses its + // allowance, fails as its own at its next expiry and drains; the waiting job is never + // spared, and the command it was first excused behind keeps its allowance. + let own = head(9, 200, false); + let (mut renewals, mut excuse, mut polls) = (0u8, FRESH, 0u32); + let mut first = 0u64; + let mut forfeited: Option<(u64, u32)> = None; + loop { + let ahead = match forfeited { + None => Some(excused(5, 100 + u64::from(polls / 4))), + Some((published, at)) if polls == at + 1 => Some(head(5, published, false)), + Some((published, at)) if polls <= at + 3 => Some(failed(5, published)), + Some(_) => None, + }; + let Some(ahead) = ahead else { break }; + let verdict = attribute(own, renewals, excuse, [ahead].into_iter()); + if let Some(published) = verdict.first_excuse(excuse) { + first = published; + } + if let Some(blocker) = verdict.forfeits(excuse, first, core::iter::empty()) { + assert!(forfeited.is_none()); + assert_ne!(blocker.published, first); + forfeited = Some((blocker.published, polls)); + } + match verdict.decision { + Decision::Excuse => excuse.used += 1, + Decision::Defer => renewals += 1, + decision => panic!("waiting job failed: {:?}", decision), + } + polls += 1; + assert!(polls < 100); + } + assert_eq!(first, 100); + assert_eq!(forfeited.map(|(_, at)| at), Some(FRESH.limit)); + assert!(renewals < MAX_RENEWALS); + } + #[test] fn blockers_taking_turns_do_not_extend_the_excuse_or_the_renewals() { // Two other clients' commands, each inside its allowance, are ahead by turns for // longer than the excuse lasts. The counters belong to the waiting job: it is renewed diff --git a/include/uapi/drm/asahi_drm.h b/include/uapi/drm/asahi_drm.h index 95ff9a15ffdb2..67bb2490b4b73 100644 --- a/include/uapi/drm/asahi_drm.h +++ b/include/uapi/drm/asahi_drm.h @@ -1274,20 +1274,20 @@ struct drm_asahi_queue_create { * run alongside the allowance, not after it: a command is renewed * for the longer of the two. * - * The allowance is per command, and nothing bounds the sum: a queue - * that is kept full of long commands occupies the GPU for as long as - * it is fed. The driver does not shield the other clients from such a - * queue: they share the GPU with it as the firmware schedules them, - * and a command of another client may have to wait until a long - * command ends. The watchdog does not fail such a waiting command, - * render or compute, while the earlier command it waits behind runs - * inside its allowance; afterwards the usual deferrals apply. A - * waiting command is excused this way for at most the longest - * allowance the system permits in total, so a command that keeps - * waiting behind other clients' work longer than that and its own - * deferrals is still failed. Userspace should ask for an allowance - * only for queues whose commands need one, and keep those commands as - * short as it can. + * The allowance is per command. Other clients share the GPU with a + * queue that uses one as the firmware schedules them, and a command of + * another client may have to wait until a long command ends. The + * watchdog does not fail such a waiting command, render or compute, + * while the earlier command it waits behind runs inside its allowance; + * afterwards the usual deferrals apply. A waiting command is excused + * this way for at most the longest allowance the system permits in + * total, however many commands it waits behind in turn. Once that is + * used up, a later command it waits behind (not the one it first + * waited behind) loses what is left of its allowance and its other + * renewals. The watchdog then settles that command like any command + * past its allowance, normally failing it, so that the waiting command + * can go on. Userspace should ask for an allowance only for queues + * whose commands need one, and keep those commands as short as it can. */ __u32 compute_timeout_ms; diff --git a/tools/asahi/g17-compute-owed-stamp-tests.py b/tools/asahi/g17-compute-owed-stamp-tests.py index c857199abad99..6c57c2d89666e 100644 --- a/tools/asahi/g17-compute-owed-stamp-tests.py +++ b/tools/asahi/g17-compute-owed-stamp-tests.py @@ -78,7 +78,7 @@ def fixture(): 'pub(crate) fn quarantine(', 'pub(crate) fn retire(', 'fn classify_unexecuted(', 'pub(crate) fn owns_packet(', 'pub(crate) fn retired_held(', 'pub(crate) fn expire_stamp_wait(', 'pub(crate) fn timeout(', - 'pub(crate) fn can_classify_timeout(']) + 'pub(crate) fn can_classify_timeout(', 'pub(crate) fn forfeit_head(']) backend = '\n'.join([ item(control, 'pub(super) fn defer_compute_timeout(').replace('pub(super) ', '', 1), item(control, 'fn retire_on_record('), @@ -199,6 +199,10 @@ def fixture(): impl Packet { fn check_firmware_producers(&self) -> Result { Ok(()) } fn within_allowance(&self, _: u64) -> bool { false } + fn forfeit_allowance(&self) -> bool { true } + fn allowance_forfeited(&self) -> bool { false } + fn note_first_excuse(&self, _: u64) {} + fn first_excuse(&self) -> u64 { 0 } } pub struct WorkStateLease { drained: Cell } impl WorkStateLease { fn drained(&self) -> Result { Ok(self.drained.get()) } } @@ -263,6 +267,15 @@ def fixture(): fn render_timeout_heads(&self) -> impl Iterator + '_ { self.render.iter().copied() } + fn forfeit_blocker(&self, verdict: &g17::timeout::Verdict, excuse: g17::timeout::Excuse, + first: u64) -> Option { + let computes: Vec<_> = self.compute.iter().flatten().filter_map(|entry| entry.queue.as_deref()) + .filter_map(|queue| queue.timeout_head()).collect(); + let blocker = verdict.forfeits(excuse, first, + Iterator::chain(self.render_timeout_heads(), computes.into_iter()))?; + self.compute.iter().flatten().filter_map(|entry| entry.queue.as_deref()) + .any(|queue| queue.forfeit_head(blocker)).then_some(blocker) + } } pub struct Init; pub struct Firmware { queues: Queues, init: Init, epoch: u64 } From 223077c377168d8b042414207dd6e4276366ad25 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 27 Nov 2026 22:39:58 +1100 Subject: [PATCH 2/2] drm/asahi: g17: class a forfeited blocker as forfeited, not spared A blocker that lost its allowance to another VM's waiting work was classified spared when its own watchdog failed it. Queue::timeout() then recorded a spared quarantine, and Queue::timed_out() is false while that flag is set, so on M5 the open timeout kill never stopped it: a forfeited dispatch that never ends kept its queue until the owner closed it, where before the forfeit it was killed when its allowance ended. Give the forfeit a class of its own, recovery::Class::Forfeited. It is not spared: its timeout fails its VM as for any command past its allowance, its queue's quarantine is not a spared one, so the queue never returns to service, and on M5 the timeout kill applies. That keeps open_timeout_kill()'s premise that the owner's VM has already failed; the kill names every queue of the context, sibling channels included, so it must not reach a VM that can still submit to them. A later zero-duration record keeps the class, as it keeps a recovery's. The UAPI comment now says the settled command fails with -ETIMEDOUT, as a lost device, with the commands queued behind it. The compute owed-stamp host test now compiles the real Queue::classify() and Queue::timed_out() and checks that a forfeited blocker is classed forfeited with its active set, fails with ETIMEDOUT and fails its VM, holds a non-spared quarantine and is due for the M5 kill; two new mutations (forfeit classed spared again, or not classified) fail it. Signed-off-by: Ryan Murray --- drivers/gpu/drm/asahi/g17/completion.rs | 6 ++ drivers/gpu/drm/asahi/g17/queue/compute.rs | 10 ++- .../gpu/drm/asahi/g17/queue/compute/exit.rs | 6 ++ drivers/gpu/drm/asahi/g17/recovery.rs | 6 ++ .../drm/asahi/g17/runtime/compute_control.rs | 15 +++-- include/uapi/drm/asahi_drm.h | 8 ++- tools/asahi/g17-compute-owed-stamp-tests.py | 65 ++++++++++++++++--- tools/asahi/run-host-tests.py | 2 +- 8 files changed, 97 insertions(+), 21 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/completion.rs b/drivers/gpu/drm/asahi/g17/completion.rs index 8ae1c8be116d7..dec24bf650194 100644 --- a/drivers/gpu/drm/asahi/g17/completion.rs +++ b/drivers/gpu/drm/asahi/g17/completion.rs @@ -768,6 +768,7 @@ impl Completion { match class { Class::Spared => 1, Class::Guilty => 2, + Class::Forfeited => 3, }, Ordering::Release, ); @@ -781,6 +782,11 @@ impl Completion { self.class.load(Ordering::Acquire) == 2 } + /// See [`Class::Forfeited`]. + pub(crate) fn forfeited(&self) -> bool { + self.class.load(Ordering::Acquire) == 3 + } + /// A recovery blamed this command, whatever class it keeps: it may have caused the fault, /// so it is never published again. pub(crate) fn note_blamed(&self) { diff --git a/drivers/gpu/drm/asahi/g17/queue/compute.rs b/drivers/gpu/drm/asahi/g17/queue/compute.rs index 93c683c1631d5..7d46e87f46dd6 100644 --- a/drivers/gpu/drm/asahi/g17/queue/compute.rs +++ b/drivers/gpu/drm/asahi/g17/queue/compute.rs @@ -1046,7 +1046,9 @@ impl Queue { } /// Takes the rest of its allowance from the command at [`Self::timeout_head`] if that is - /// `head` ([`crate::g17::timeout::Verdict::forfeits`]). Returns whether it lost one. + /// `head` ([`crate::g17::timeout::Verdict::forfeits`]). Returns whether it lost one. The + /// whole head is compared, so a command whose allowance ended since the verdict (its + /// `excused` changed) keeps nothing to lose and is skipped. pub(crate) fn forfeit_head(&self, head: crate::g17::timeout::Head) -> bool { if self.timeout_head() != Some(head) { return false; @@ -1499,7 +1501,11 @@ impl Queue { for active in self.active.iter_mut() { let completion = &active.packet.completion; // A command whose stamp is owed ran and has its result. - if active.stamps_owed || completion.spared() || completion.guilty() { + if active.stamps_owed + || completion.spared() + || completion.guilty() + || completion.forfeited() + { continue; } if dropped { diff --git a/drivers/gpu/drm/asahi/g17/queue/compute/exit.rs b/drivers/gpu/drm/asahi/g17/queue/compute/exit.rs index 0812e59fff57c..04bbd8cfb2313 100644 --- a/drivers/gpu/drm/asahi/g17/queue/compute/exit.rs +++ b/drivers/gpu/drm/asahi/g17/queue/compute/exit.rs @@ -147,6 +147,12 @@ impl Queue { /// how long ago the watchdog failed it. The owner's VM has already failed, so it loses /// nothing it could still use; without the kill a command that never terminates keeps /// running, and keeps this queue, until the owner closes. + /// + /// That holds for a command whose allowance another VM's waiting work took + /// ([`crate::g17::recovery::Class::Forfeited`]) too: only spared work keeps its VM through + /// [`Self::timeout`], so the timeout of a forfeited command fails its VM as it does any + /// command past its allowance. The kill names every queue of the context, sibling + /// channels included, so it must not reach a VM that can still submit to them. pub(crate) fn open_timeout_kill(&self, now: u64) -> Option<(Arc, u64)> { if self.released || self.timed_out_at == 0 || !self.timed_out() { return None; diff --git a/drivers/gpu/drm/asahi/g17/recovery.rs b/drivers/gpu/drm/asahi/g17/recovery.rs index 51a92a0581cda..1b35a2dffa3ef 100644 --- a/drivers/gpu/drm/asahi/g17/recovery.rs +++ b/drivers/gpu/drm/asahi/g17/recovery.rs @@ -132,6 +132,12 @@ pub(super) enum Class { Spared, /// This VM owns guilty work. Guilty, + /// The watchdog failed this compute work because another VM's waiting work took the rest + /// of its allowance ([`super::timeout::Verdict::forfeits`]), not because a recovery dropped + /// it. Unlike spared work it fails as a command past its allowance does: its VM fails, and + /// its queue's quarantine is not a spared one, so the queue never returns to service and on + /// M5 its context is killed ([`super::queue::compute::Queue::timed_out`]). + Forfeited, } /// Queue operations under the device mutex. Completion signalling belongs to the caller after diff --git a/drivers/gpu/drm/asahi/g17/runtime/compute_control.rs b/drivers/gpu/drm/asahi/g17/runtime/compute_control.rs index a2da4f5942b06..f5dc94240069c 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/compute_control.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/compute_control.rs @@ -48,13 +48,16 @@ impl Backend { } let decision = verdict.decision; // A command that lost its allowance to another VM's waiting work - // ([`timeout::Verdict::forfeits`]) is failed for that, not found hung: it is spared, - // as the waiting work would have been. - if decision == Decision::Spare - || (decision == Decision::Own && packet.allowance_forfeited()) - { + // ([`timeout::Verdict::forfeits`]) is failed for that. It is not spared: a spared + // quarantine is never killed, and a forfeited command may be one that never ends. + let class = match decision { + Decision::Spare => Some(Class::Spared), + Decision::Own if packet.allowance_forfeited() => Some(Class::Forfeited), + _ => None, + }; + if let Some(class) = class { firmware.queues.compute_for_packet(self.owner, packet).ok_or(EIO)? - .classify(&|_| Class::Spared); + .classify(&|_| class); } if let Some(blocker) = verdict.reported_blocker(renewals, excuse) { dev_warn!(self.shared.dev.as_ref(), diff --git a/include/uapi/drm/asahi_drm.h b/include/uapi/drm/asahi_drm.h index 7cedb861bdfb1..4f29ea60d27ce 100644 --- a/include/uapi/drm/asahi_drm.h +++ b/include/uapi/drm/asahi_drm.h @@ -1347,9 +1347,11 @@ struct drm_asahi_queue_create { * used up, a later command it waits behind (not the one it first * waited behind) loses what is left of its allowance and its other * renewals. The watchdog then settles that command like any command - * past its allowance, normally failing it, so that the waiting command - * can go on. Userspace should ask for an allowance only for queues - * whose commands need one, and keep those commands as short as it can. + * past its allowance, normally failing it with -ETIMEDOUT as a lost + * device, together with the commands queued behind it, so that the + * waiting command can go on. Userspace should ask for an allowance only + * for queues whose commands need one, and keep those commands as short + * as it can. */ __u32 compute_timeout_ms; diff --git a/tools/asahi/g17-compute-owed-stamp-tests.py b/tools/asahi/g17-compute-owed-stamp-tests.py index 23998747c743a..c1040f8a44c51 100644 --- a/tools/asahi/g17-compute-owed-stamp-tests.py +++ b/tools/asahi/g17-compute-owed-stamp-tests.py @@ -52,6 +52,7 @@ def line(source, prefix): def fixture(): retirement = (G17 / 'queue/compute/retirement.rs').read_text() compute = (G17 / 'queue/compute.rs').read_text() + exit = (G17 / 'queue/compute/exit.rs').read_text() control = (G17 / 'runtime/compute_control.rs').read_text() timeout = (G17 / 'timeout.rs').read_text() depth = re.search(r'pub\(crate\) const COMPUTE_DEPTH: usize = (\d+);', @@ -80,7 +81,8 @@ def fixture(): 'pub(crate) fn quarantine(', 'pub(crate) fn retire(', 'fn classify_unexecuted(', 'pub(crate) fn owns_packet(', 'pub(crate) fn retired_held(', 'pub(crate) fn expire_stamp_wait(', 'pub(crate) fn timeout(', - 'pub(crate) fn can_classify_timeout(', 'pub(crate) fn forfeit_head(']) + 'pub(crate) fn can_classify_timeout(', 'pub(crate) fn forfeit_head(', + 'pub(crate) fn classify(']) + '\n' + item(exit, 'pub(crate) fn timed_out(') pairs = (G17 / 'pairs.rs').read_text() hz = re.search(r'pub\(crate\) const COMMAND_TIMESTAMP_HZ: u64 = ([0-9_]+);', (ROOT / 'hw/t8140.rs').read_text()).group(1) @@ -128,7 +130,7 @@ def fixture(): pub(crate) mod recovery { pub(crate) fn note_retired() {} #[derive(Clone, Copy, Debug, PartialEq, Eq)] - pub(crate) enum Class { Spared, Guilty } + pub(crate) enum Class { Spared, Guilty, Forfeited } } pub(crate) mod trace { pub(crate) const COMPUTE: u8 = 1; @@ -179,6 +181,7 @@ def fixture(): } fn spared(&self) -> bool { self.class.get() == Some(recovery::Class::Spared) } fn guilty(&self) -> bool { self.class.get() == Some(recovery::Class::Guilty) } + fn forfeited(&self) -> bool { self.class.get() == Some(recovery::Class::Forfeited) } fn classify(&self, class: recovery::Class) { self.class.set(Some(class)); } fn status(&self) -> &Arc { &self.status } } @@ -208,12 +211,12 @@ def fixture(): pub struct Context { vm: Vm } impl Context { fn vm(&self) -> &Vm { &self.vm } } pub struct Order { sequence: u64 } -pub struct Packet { completion: Arc, order: Order, context: Context } +pub struct Packet { completion: Arc, order: Order, context: Context, forfeited: Cell } impl Packet { fn check_firmware_producers(&self) -> Result { Ok(()) } fn within_allowance(&self, _: u64) -> bool { false } - fn forfeit_allowance(&self) -> bool { true } - fn allowance_forfeited(&self) -> bool { false } + fn forfeit_allowance(&self) -> bool { !self.forfeited.replace(true) } + fn allowance_forfeited(&self) -> bool { self.forfeited.get() } fn note_first_excuse(&self, _: u64) {} fn first_excuse(&self) -> u64 { 0 } } @@ -256,6 +259,7 @@ def fixture(): terminal: Option<(Arc, Error)>, fault_stamp: Option, timed_out_at: u64, + exit: Option<()>, } impl Queue { fn qid(&self) -> u8 { 28 } @@ -263,7 +267,6 @@ def fixture(): fn retire_configuration(&mut self, _: u64) -> Result { Ok(()) } fn valid_ticket(&self, _: &Active, _: &Ticket) -> Result { Ok(()) } fn qos_owner(&self) -> qos::Owner { qos::Owner } - fn classify(&self, _: &dyn Fn(&Arc) -> recovery::Class) {} fn clear_owner(&mut self, stamp_released_at: bool) { self.owner = None; if stamp_released_at { self.released_at = now_ns(); } @@ -347,13 +350,14 @@ def fixture(): spared_deferred: false, awaiting_witness: false, retire_pending: false, last_end: 0, previous: None, submitted: 0, retirement_ready: true, released: false, owner: Some(OWNER), released_at: 0, heavy_run: 0, terminal: None, fault_stamp: None, timed_out_at: 0, + exit: None, } } /// Publishes the command of kick `kick`; `drained`: the rest of its ioctl completed. fn publish(queue: &mut Queue, kick: u64, drained: bool) -> Arc { let packet = Arc::new(Packet { completion: Arc::new(Completion::new()), - order: Order { sequence: kick }, context: Context { vm: Vm(70) } }); + order: Order { sequence: kick }, context: Context { vm: Vm(70) }, forfeited: Cell::new(false) }); let ticket = Ticket { owner: OWNER, submission: kick, context_id: 1, qid: 28, descriptor: 0x1000 + kick, work_node: 0x2000, kick, ordinal: kick as u32, item_producer: 4, item_count: 64, epoch: EPOCH }; @@ -578,6 +582,45 @@ def fixture(): assert_eq!(b.failed.get(), Some(ETIMEDOUT)); } + #[test] + fn a_forfeited_blocker_is_failed_and_killed_on_timeout() { + // Another VM's waiting job took the rest of this command's allowance: its next expiry + // fails it as its own. It is classed as forfeited, not spared, so its VM fails and on + // M5 the open timeout kill applies (timed_out), as for any command past its allowance. + for m5 in [true, false] { + let mut q = queue(m5); + let p = publish(&mut q, 0x10, true); + let p2 = publish(&mut q, 0x11, true); + let b = backend(q, vec![]); + assert!(p.forfeit_allowance() && !p.forfeit_allowance(), "a forfeit is taken once"); + assert_eq!(b.defer_compute_timeout(&p, 0, 0), Decision::Own); + assert_eq!(class(&p), Some(recovery::Class::Forfeited), "m5 {m5}"); + assert_eq!(class(&p2), Some(recovery::Class::Forfeited), "the queue's active set"); + assert_eq!(b.timeout_compute(&p), Ok(())); + assert_eq!(result(&p), Some(Err(ETIMEDOUT))); + assert_eq!(result(&p2), Some(Err(ETIMEDOUT))); + assert_eq!(b.failed.get(), Some(ETIMEDOUT), "its VM fails, as past its allowance"); + with_queue(&b, |q| { + assert!(q.quarantined && !q.spared_quarantine, "not a spared quarantine"); + assert_eq!(q.timed_out(), m5, "M5 kills it on timeout"); + }); + } + // Without a forfeit the same expiry leaves the command unclassified, as before. + let mut q = queue(true); + let p = publish(&mut q, 0x10, true); + let b = backend(q, vec![]); + assert_eq!(b.defer_compute_timeout(&p, 0, 0), Decision::Own); + assert_eq!(class(&p), None); + // Spared work, by contrast, keeps its VM and is never killed: the forfeit must not be. + let mut q = queue(true); + let p = publish(&mut q, 0x10, true); + let b = backend(q, vec![]); + with_queue(&b, |q| q.classify(&|_| recovery::Class::Spared)); + assert_eq!(b.timeout_compute(&p), Ok(())); + assert_eq!(b.failed.get(), None); + with_queue(&b, |q| assert!(q.spared_quarantine && !q.timed_out())); + } + #[test] fn a_watchdog_behind_retired_commands_weighs_its_own_command() { let mut q = queue(false); @@ -758,8 +801,12 @@ def fixture(): 'held-stamped-pops': [('if stampless || at != 0 {', 'if stampless {')], 'crossed-behind-owed': [(' if at != 0 {\n // Only its stamp retires it now', ' if false {\n // Only its stamp retires it now')], - 'owed-classified': [('if active.stamps_owed || completion.spared() || completion.guilty() {', - 'if completion.spared() || completion.guilty() {')], + # A forfeited blocker is spared again: no VM failure, no M5 timeout kill (linux#2887 review). + 'forfeit-spared': [('Decision::Own if packet.allowance_forfeited() => Some(Class::Forfeited),', + 'Decision::Own if packet.allowance_forfeited() => Some(Class::Spared),')], + # A forfeited blocker is not classified at all. + 'forfeit-unclassified': [('Decision::Own if packet.allowance_forfeited() => Some(Class::Forfeited),', '')], + 'owed-classified': [('if active.stamps_owed\n || completion.spared()', 'if completion.spared()')], 'rebind-check-entry': [('self.previous.as_ref().is_some_and(|p| p.publication != first)', 'self.previous.as_ref().is_some_and(|p| p.publication != Some((front.packet.order.sequence, ticket.epoch)))')], 'timeout-fails-retired': [('if !queue.owns_packet(packet) || queue.retired_held(packet) {', diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index a20d283737988..6000acab64d3a 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -284,7 +284,7 @@ def mutations(flag, names, base, expect=KILL): 'no-step-past', 'pop-owed', 'head-includes-owed', 'held-stamped-pops', 'crossed-behind-owed', 'owed-classified', 'rebind-check-entry', 'timeout-fails-retired', 'no-owed-poll', 'count-recordless', - 'count-owed-twice'), []), + 'count-owed-twice', 'forfeit-spared', 'forfeit-unclassified'), []), 'g17-compute-deadline-tests.py': [([], PASS)], 'g17-compute-growth-deadline-tests.py': [([], PASS)], 'g17-compute-retired-tests.py': [([], PASS)]