From 7cf06de21f7699df115bdd6c6bbf8f0b473a813d Mon Sep 17 00:00:00 2001 From: AxxzyWasTaken Date: Tue, 24 Nov 2026 14:26:33 +0000 Subject: [PATCH] drm/asahi: g17: trim compute completion critical-path locks and batch clear Reduce per-completion overhead on G17 compute retirement without moving sleeping locks or VM pin release into interrupt context: 1. In Completion::release_healthy() and complete_with_compute(), skip the unused render-lease mutex on compute commands, move feed.note_pass() and feed.note_work() spinlocks after output.finish() signals the exported fence, and return whether healthy pins were already released so Deferred::Retired and Deferred::RetiredCompute skip redundant vm_job and render mutex re-acquisitions in release_after_retirement(). Keep tools/asahi/g17-quarantine-drain-tests.py in sync with the updated Completion helper signature. 2. Track touched QIDs in retirement::Batch (touched_slots and touched_frontiers u128 bitmasks) so Batch::clear() resets only active QIDs instead of zeroing 3 KiB of slots and frontier arrays on every completion, and extend the asahi_g17_completion_index KUnit suite to cover multi-batch clear reuse. Signed-off-by: AxxzyWasTaken --- drivers/gpu/drm/asahi/g17/completion.rs | 46 +++++++++++------- .../drm/asahi/g17/queue/compute/retirement.rs | 47 ++++++++++++++++++- tools/asahi/g17-quarantine-drain-tests.py | 11 +++-- 3 files changed, 81 insertions(+), 23 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/completion.rs b/drivers/gpu/drm/asahi/g17/completion.rs index 41e8b3c09fc9a..ddaf0d9cb7ba5 100644 --- a/drivers/gpu/drm/asahi/g17/completion.rs +++ b/drivers/gpu/drm/asahi/g17/completion.rs @@ -813,14 +813,17 @@ impl Completion { if let Some(sources) = &self.sources { sources.release(); } - drop(self.render.lock().take()); + if !self.is_compute { + drop(self.render.lock().take()); + } drop(self.vm_job.lock().take()); } /// Exact retirement permits the physical lease and VM pin to be released /// before waking userspace. A failure fence alone never establishes this. + /// Returns true if this call claimed signalling and released healthy pins. #[track_caller] - pub(crate) fn complete(&self, result: Result<[u64; 4]>) { + pub(crate) fn complete(&self, result: Result<[u64; 4]>) -> bool { // M5 compute feeds the refill detector only from its exact execution record // (RetiredCompute); M5 compute is the compute without pooled timestamps. let execution = if self.is_compute && self.compute.is_none() { @@ -828,20 +831,24 @@ impl Completion { } else { result.as_ref().ok().map(|ticks| [ticks[0], ticks[1]]) }; - self.complete_with_compute(result, execution); + self.complete_with_compute(result, execution) } /// Compute feedback is separate from public timestamps, which may contain /// a synthesized retirement envelope when the execution record is absent. #[track_caller] - fn complete_with_compute(&self, mut result: Result<[u64; 4]>, compute: Option<[u64; 2]>) { + fn complete_with_compute( + &self, + mut result: Result<[u64; 4]>, + compute: Option<[u64; 2]>, + ) -> bool { if result.is_ok() { if let Err(error) = self.check_source_inputs() { result = Err(error); } } if self.signalled.swap(true, Ordering::AcqRel) { - return; + return false; } if result.is_ok() && self.interrupted() { result = Err(ECANCELED); @@ -856,6 +863,15 @@ impl Completion { self.usage.add(Engine::Fragment, ticks[2], ticks[3], hz); } } + if let Err(error) = result { + // Spared work cancelled after another VM's recovery is not this client's device loss. + self.report(error); + } + self.release_healthy(); + self.signal_terminal(result); + self.output.finish(result); + // Feed detector and busy-run tracking are internal host heuristics: update them + // after signalling the fence so their spinlocks stay off the wakeup critical path. if let (Some(feed), Ok(ticks)) = (&self.feed, &result) { // Compute reports its dispatch envelope in the first pair, a // render its fragment pass in the second. @@ -871,13 +887,7 @@ impl Completion { feed.note_work(self.context_id, start.unwrap_or(0), ticks[1].max(ticks[3])); } } - if let Err(error) = result { - // Spared work cancelled after another VM's recovery is not this client's device loss. - self.report(error); - } - self.release_healthy(); - self.signal_terminal(result); - self.output.finish(result); + true } /// A rejected, never-published command cannot poison the VM for contention. @@ -941,18 +951,20 @@ impl Deferred { pub(crate) fn finish(self) { match self { Self::RetiredCompute(completion, ticks, execution) => { - completion.complete_with_compute(Ok(ticks), execution); - completion.release_after_retirement(); + if !completion.complete_with_compute(Ok(ticks), execution) { + completion.release_after_retirement(); + } } Self::Retired(completion, result) => { - completion.complete(result); // An earlier failure may already have signalled this fence while // retaining the pin. Retirement is a separate release witness. - completion.release_after_retirement(); + if !completion.complete(result) { + completion.release_after_retirement(); + } } Self::Drained(completion, result) => { completion.release_after_retirement(); - completion.complete(result); + let _ = completion.complete(result); } Self::FailedOwned(completion, error) => completion.fail_while_owned(error), Self::Unpublished(completion, error) => completion.fail_unpublished(error), diff --git a/drivers/gpu/drm/asahi/g17/queue/compute/retirement.rs b/drivers/gpu/drm/asahi/g17/queue/compute/retirement.rs index e6d70105dae16..15a98a80ba482 100644 --- a/drivers/gpu/drm/asahi/g17/queue/compute/retirement.rs +++ b/drivers/gpu/drm/asahi/g17/queue/compute/retirement.rs @@ -292,10 +292,14 @@ pub(crate) struct Batch { /// ([`NO_FRONTIER`] for a QID without a staged queue). A record at or past it belongs /// to a command published during the scan, which a later batch stages. frontiers: [u64; QIDS], + /// Bitmasks of QIDs touched in `slots` and `frontiers` so `clear()` touches only active QIDs. + touched_slots: u128, + touched_frontiers: u128, } const NO_FRONTIER: u64 = u64::MAX; const KICK_MASK: u64 = (1 << 40) - 1; +const _: () = assert!(QIDS <= 128); impl Batch { pub(crate) fn new() -> Result { @@ -311,19 +315,34 @@ impl Batch { unobserved: 0, covered: 0, frontiers: [NO_FRONTIER; QIDS], + touched_slots: 0, + touched_frontiers: 0, }) } pub(crate) fn clear(&mut self) { self.requests.clear(); - self.slots.fill((0, 0)); + let mut slots = self.touched_slots; + while slots != 0 { + let qid = slots.trailing_zeros() as usize; + slots &= slots - 1; + self.slots[qid] = (0, 0); + } + self.touched_slots = 0; self.open = None; self.unobserved = 0; self.covered = 0; - self.frontiers.fill(NO_FRONTIER); + let mut frontiers = self.touched_frontiers; + while frontiers != 0 { + let qid = frontiers.trailing_zeros() as usize; + frontiers &= frontiers - 1; + self.frontiers[qid] = NO_FRONTIER; + } + self.touched_frontiers = 0; } /// Records the first kick `qid`'s queue had not yet published when this batch was staged. pub(crate) fn stage_frontier(&mut self, qid: u8, next: u64) -> Result { *self.frontiers.get_mut(usize::from(qid)).ok_or(EINVAL)? = next & KICK_MASK; + self.touched_frontiers |= 1u128 << qid; Ok(()) } /// A record of `qid` with `kick` names a command published after this batch was staged. @@ -372,6 +391,7 @@ impl Batch { .map_err(|_| ENOSPC)?; *self.heads.get_mut(bucket).ok_or(EIO)? = index; *slot = (start, slot.1 + 1); + self.touched_slots |= 1u128 << ticket.qid; self.open = Some(ticket.qid); self.unobserved += 1; self.covered += usize::from(covered); @@ -813,4 +833,27 @@ mod index_tests { assert!(batch.fold_reader(|word| Ok(other[word])).unwrap()); assert!(!batch.complete() && !batch.covered_pending()); } + #[test] + fn clear_resets_touched_slots_and_frontiers_across_batches() { + let mut batch = Batch::new().unwrap(); + let low = ticket(0, 11, 0x1000); + let high = ticket((QIDS - 1) as u8, 22, 0x2000); + batch.stage_frontier(0, 12).unwrap(); + batch.stage_frontier((QIDS - 1) as u8, 23).unwrap(); + batch.request(&low, true).unwrap(); + batch.request(&high, false).unwrap(); + batch.clear(); + assert!(batch.complete() && !batch.covered_pending()); + assert_eq!(batch.touched_slots, 0); + assert_eq!(batch.touched_frontiers, 0); + assert!(!batch.published_after_staging(0, 12)); + assert!(!batch.published_after_staging(QIDS - 1, 23)); + // Re-staging the same QIDs with new kicks succeeds cleanly after clear(). + let next = ticket((QIDS - 1) as u8, 25, 0x3000); + batch.request(&next, true).unwrap(); + let words = record(&next, [50, 60]); + assert!(batch.fold_reader(|word| Ok(words[word])).unwrap()); + assert!(batch.complete()); + assert_eq!(batch.take(&next).unwrap().map(|obs| obs.timestamps), Some([50, 60])); + } } diff --git a/tools/asahi/g17-quarantine-drain-tests.py b/tools/asahi/g17-quarantine-drain-tests.py index 53204a179edd6..395bdc441f16c 100644 --- a/tools/asahi/g17-quarantine-drain-tests.py +++ b/tools/asahi/g17-quarantine-drain-tests.py @@ -73,7 +73,7 @@ def fixture(): 'pair.retire(None, generation, state, host,', 'pair.fail_after_observation(packet, error, host,') # A first signal gives the lease back without ending a quarantine (the `complete` mock). ordered(item(completion, 'fn complete_with_compute('), - 'if self.signalled.swap(true, Ordering::AcqRel) {\n return;\n }', + 'if self.signalled.swap(true, Ordering::AcqRel) {\n return false;\n }', 'self.release_healthy();') # The drain release runs once the drain proof, event service and accounting passed. ordered(item(settle, 'fn drain_render_slot('), 'pair.firmware_drained(host)', 'self.service_runtime_events(deferred)', @@ -175,6 +175,7 @@ def fixture(): sources: Option>, vm_job: Mutex>, render: Mutex>, + is_compute: bool, signalled: AtomicBool, accepted: AtomicBool, spared: AtomicBool, @@ -188,13 +189,14 @@ def fixture(): fn report(&self, _: Error) {} fn signal_terminal(&self, result: Result<[u64; 4]>) { *self.result.lock() = Some(result); } /// The lease handling of the real complete_with_compute (checked against it above). - fn complete(&self, result: Result<[u64; 4]>) { self.complete_with_compute(result, None) } - fn complete_with_compute(&self, result: Result<[u64; 4]>, _: Option<[u64; 2]>) { + fn complete(&self, result: Result<[u64; 4]>) -> bool { self.complete_with_compute(result, None) } + fn complete_with_compute(&self, result: Result<[u64; 4]>, _: Option<[u64; 2]>) -> bool { if self.signalled.swap(true, Ordering::AcqRel) { - return; + return false; } self.release_healthy(); self.signal_terminal(result); + true } ''' + command + '\n}\n' + item(completion, 'pub(crate) enum Deferred {') + '\n' + item(completion, 'impl Deferred {') + r''' @@ -290,6 +292,7 @@ def fixture(): let completion = Arc::new(Completion { status: Arc::new(Status), output: Output, sources: Some(Arc::new(Sources)), vm_job: Mutex::new(Some(guard)), render: Mutex::new(Some(lease)), + is_compute: false, signalled: AtomicBool::new(false), accepted: AtomicBool::new(true), spared: AtomicBool::new(false), result: Mutex::new(None), });