From 4d5fcb73db27f6f2cd8bd4d41f10722d532bf33f Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Thu, 26 Nov 2026 14:36:35 -0500 Subject: [PATCH 1/3] drm/asahi: g17: wait interruptibly when submission order window is full When a context submits more than ORDER_WINDOW (1024) ioctls ahead of its oldest waiting ioctl (for example during a compositor atomic-commit retry burst), WorkStateLease::deferred() previously failed immediately with -EBUSY. Userspace Vulkan drivers treat a submit ioctl error as device loss, triggering GL_GUILTY_CONTEXT_RESET and aborting the compositor. Track full-window submitter waits separately in identity::Order and pair context.order with a CondVar so WorkStateLease::deferred() waits interruptibly without heap allocations for up to 1 s (re-checking VM status on each slice and returning -ERESTARTSYS on signals) before falling back to -EBUSY. Signed-off-by: Erik Bourget --- drivers/gpu/drm/asahi/g17/context.rs | 71 +++++++++++++++---- drivers/gpu/drm/asahi/g17/context/identity.rs | 57 +++++++++------ 2 files changed, 95 insertions(+), 33 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/context.rs b/drivers/gpu/drm/asahi/g17/context.rs index 6e377d0f29a42..2d61f4bc0d24e 100644 --- a/drivers/gpu/drm/asahi/g17/context.rs +++ b/drivers/gpu/drm/asahi/g17/context.rs @@ -15,16 +15,16 @@ use super::{ }; use crate::mmu::{self, ExecutionContext, Vm, UAT_PGSZ}; use core::sync::atomic::{fence, AtomicBool, AtomicU32, AtomicU8, AtomicUsize, Ordering}; -use identity::{Order, RenderSlots, WorkStates, FREE, NODE_STRIDE, STATE_COUNT}; +use identity::{Order, OrderWake, RenderSlots, WorkStates, FREE, NODE_STRIDE, STATE_COUNT}; /// Both of a context's render channels are bound to other pairs whose publications have not /// all retired: the publisher waits for that retirement. pub(crate) const RENDER_CHANNEL_PENDING: Error = ERECALLCONFLICT; use super::job::Engine; use kernel::{ - new_mutex, + new_condvar, new_mutex, prelude::*, - sync::{Arc, Mutex}, + sync::{Arc, CondVar, CondVarTimeoutResult, Mutex}, }; const QOS_IDS: usize = 128; @@ -232,6 +232,8 @@ pub(crate) struct Context { compute_returns: Arc, /// The order in which this context's ioctls take work-state nodes. order: Arc>, + /// Woken when `order`'s turn moves after a submitter waited on a full order window. + order_wait: Arc, work: Arc>, work_base: u64, /// How far the work storage's address reached the firmware, kept up to date by every @@ -343,6 +345,8 @@ 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 order_wait = + Arc::pin_init(new_condvar!("G17 context admission order wait"), GFP_KERNEL)?; Ok(Self { m5: alloc.uat.is_m5(), execution, @@ -364,6 +368,7 @@ impl Context { state_returns, compute_returns, order, + order_wait, work, work_base, work_mark, @@ -674,11 +679,46 @@ pub(crate) struct WorkStateLease { } impl WorkStateLease { pub(super) fn status(&self) -> &Arc { self.context.status() } - /// The lease of a new ioctl, ordered after every earlier one of its context. EBUSY while - /// too many of the context's ioctls are queued behind its oldest waiting one - /// (see `identity::Order`). + /// The lease of a new ioctl, ordered after every earlier one of its context. Waits + /// interruptibly while too many of the context's ioctls are queued behind its oldest + /// waiting one (see `identity::Order`), returning EBUSY only if the turn does not move + /// within the backpressure window. pub(crate) fn deferred(context: Arc) -> Result { - let ordinal = context.order.lock().assign().ok_or(EBUSY)?; + let mut order = context.order.lock(); + if let Some(ordinal) = order.assign() { + drop(order); + return Ok(Self { + context, + word: AtomicU32::new(FREE), + engine: AtomicU8::new(0), + ordinal, + }); + } + const ORDER_WAIT_NS: u64 = 1_000_000_000; + let deadline = super::now_ns().saturating_add(ORDER_WAIT_NS); + let ordinal = loop { + if context.status().get() != 0 { + return Err(EIO); + } + if let Some(ordinal) = order.assign() { + break ordinal; + } + let remaining = deadline.saturating_sub(super::now_ns()); + if remaining == 0 { + return Err(EBUSY); + } + let slice_ms = (remaining.div_ceil(1_000_000) as u32).clamp(1, 50); + let jiffies = kernel::time::msecs_to_jiffies(slice_ms); + if matches!( + context + .order_wait + .wait_interruptible_timeout(&mut order, jiffies), + CondVarTimeoutResult::Signal { .. } + ) { + return Err(ERESTARTSYS); + } + }; + drop(order); Ok(Self { context, word: AtomicU32::new(FREE), @@ -716,9 +756,7 @@ impl WorkStateLease { let wake = order.admit(self.ordinal); drop(order); drop(work); - if wake { - self.context.pass_turn(); - } + self.context.notify_turn(wake); Ok(candidate) } /// The domain a publisher samples before acquire() and waits on after EBUSY. @@ -813,8 +851,9 @@ impl Drop for WorkStateLease { let word = self.word(); if word != FREE { self.context.release_state(word, usize::from(self.engine.load(Ordering::Relaxed))); - } else if self.context.order.lock().leave(self.ordinal) { - self.context.pass_turn(); + } else { + let wake = self.context.order.lock().leave(self.ordinal); + self.context.notify_turn(wake); } } } @@ -833,6 +872,14 @@ impl Context { self.state_returns.notify(); self.compute_returns.notify(); } + fn notify_turn(&self, wake: OrderWake) { + if wake.publishers { + self.pass_turn(); + } + if wake.submitters { + self.order_wait.notify_all(); + } + } } impl QosIds { diff --git a/drivers/gpu/drm/asahi/g17/context/identity.rs b/drivers/gpu/drm/asahi/g17/context/identity.rs index fabed7ae649d3..fd89b0d6010b1 100644 --- a/drivers/gpu/drm/asahi/g17/context/identity.rs +++ b/drivers/gpu/drm/asahi/g17/context/identity.rs @@ -118,6 +118,13 @@ pub(super) const ORDER_WINDOW: u64 = 1024; /// it. Taking nodes strictly in turn keeps the words in submission order. Each engine admits /// from its own scheduler, so the turn costs a scheduler round trip whenever the other engine's /// ioctl is ready first. +/// Which waiters to wake after [`Order::admit`] or [`Order::leave`] advances the turn. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub(super) struct OrderWake { + pub(super) publishers: bool, + pub(super) submitters: bool, +} + pub(super) struct Order { /// The next ordinal to hand out. next: u64, @@ -125,8 +132,10 @@ pub(super) struct Order { turn: u64, /// Ordinals past the turn that left, as bits at ordinal % ORDER_WINDOW. done: [u64; (ORDER_WINDOW / 64) as usize], - /// An ioctl was refused since the turn last moved: its publisher waits for the next move. + /// A scheduler publisher was refused since the turn last moved. refused: bool, + /// A submitter hit the full window since the turn last moved. + submitter_waiting: bool, } impl Order { pub(super) const fn new() -> Self { @@ -135,12 +144,15 @@ impl Order { turn: 0, done: [0; (ORDER_WINDOW / 64) as usize], refused: false, + submitter_waiting: false, } } /// The ordinal of a new ioctl, in submission order. None while [`ORDER_WINDOW`] ioctls of - /// the context are past the oldest one still waiting for its node. + /// the context are past the oldest one still waiting for its node; remembers the refusal + /// so the next turn advance wakes blocked submitters. pub(super) fn assign(&mut self) -> Option { if self.next - self.turn >= ORDER_WINDOW { + self.submitter_waiting = true; return None; } let ordinal = self.next; @@ -154,28 +166,30 @@ impl Order { self.refused |= !admits; admits } - /// The ioctl `ordinal` took its node at its turn. Returns whether a refused ioctl may now be - /// admitted. - pub(super) fn admit(&mut self, ordinal: u64) -> bool { + /// The ioctl `ordinal` took its node at its turn. Returns which waiters may now retry. + pub(super) fn admit(&mut self, ordinal: u64) -> OrderWake { self.finish(ordinal) } /// The ioctl `ordinal` left without taking a node (it failed or was cancelled first). - /// Returns whether a refused ioctl may now be admitted. - pub(super) fn leave(&mut self, ordinal: u64) -> bool { + /// Returns which waiters may now retry. + pub(super) fn leave(&mut self, ordinal: u64) -> OrderWake { self.finish(ordinal) } - fn finish(&mut self, ordinal: u64) -> bool { + fn finish(&mut self, ordinal: u64) -> OrderWake { if ordinal < self.turn { - return false; + return OrderWake::default(); } if ordinal == self.turn { self.pass(); - return core::mem::take(&mut self.refused); + return OrderWake { + publishers: core::mem::take(&mut self.refused), + submitters: core::mem::take(&mut self.submitter_waiting), + }; } // assign() keeps every ordinal within the window past the turn. let bit = ordinal % ORDER_WINDOW; self.done[(bit / 64) as usize] |= 1 << (bit % 64); - false + OrderWake::default() } /// Moves the turn past the current ordinal and every following one already done. fn pass(&mut self) { @@ -344,13 +358,13 @@ mod tests { } assert!(order.admits(ordinals[0])); // Its admission moves the turn and wakes the refused. - assert!(order.admit(ordinals[0])); + assert_eq!(order.admit(ordinals[0]), OrderWake { publishers: true, submitters: false }); assert!(order.admits(ordinals[1])); // Nobody was refused since, so the next admission wakes nobody. - assert!(!order.admit(ordinals[1])); + assert_eq!(order.admit(ordinals[1]), OrderWake::default()); assert!(!order.admits(ordinals[3])); assert!(order.admits(ordinals[2])); - assert!(order.admit(ordinals[2])); + assert_eq!(order.admit(ordinals[2]), OrderWake { publishers: true, submitters: false }); assert!(order.admits(ordinals[3])); } @@ -364,14 +378,14 @@ mod tests { order.assign().unwrap(), ); // b and c fail before their turn; while a waits, d waits too. - assert!(!order.leave(b)); - assert!(!order.leave(c)); + assert_eq!(order.leave(b), OrderWake::default()); + assert_eq!(order.leave(c), OrderWake::default()); assert!(!order.admits(d)); // Once a takes its node, the turn passes b and c as well, and the refused one may retry. - assert!(order.admit(a)); + assert_eq!(order.admit(a), OrderWake { publishers: true, submitters: false }); assert!(order.admits(d)); // An ordinal already passed leaves without effect. - assert!(!order.leave(a)); + assert_eq!(order.leave(a), OrderWake::default()); } #[test] @@ -384,9 +398,10 @@ mod tests { } assert!(order.assign().is_none()); // An ioctl leaving far ahead is remembered within the window. - assert!(!order.leave(last)); + assert_eq!(order.leave(last), OrderWake::default()); assert!(order.admits(first)); - assert!(!order.admit(first)); + // Only the refused submitter waiting on the full window is woken once the turn advances. + assert_eq!(order.admit(first), OrderWake { publishers: false, submitters: true }); assert!(order.assign().is_some()); } @@ -396,7 +411,7 @@ mod tests { for _ in 0..24 { let ordinal = order.assign().unwrap(); assert!(order.admits(ordinal)); - assert!(!order.admit(ordinal)); + assert_eq!(order.admit(ordinal), OrderWake::default()); } } From b01dc0e4613966e2809b0bdaa0d328a38fca802d Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 02:34:31 -0500 Subject: [PATCH 2/3] drm/asahi: g17: leave output syncobjs untouched on -ERESTARTSYS When WorkStateLease::deferred() is interrupted by a signal while waiting on a full ORDER_WINDOW, it returns -ERESTARTSYS so the kernel can restart the submit ioctl. Previously WorkStateLease::deferred() ran inside Outputs::publish(), and Outputs::drop() installed and signalled a fallback error fence on the resolved output syncobjs. For timeline syncobjs, a waiter could observe -ERESTARTSYS before the restarted ioctl attempted to publish the same timeline point again. Move WorkStateLease::deferred() before Outputs::publish() in Queue::submit(), and skip fallback error-fence publication in Outputs::output_fence() and Outputs::drop() when returning -ERESTARTSYS before publication. Add a KUnit test covering full-window signal interruption with timeline and cumulative syncobjs and subsequent ioctl restart. Signed-off-by: Erik Bourget --- drivers/gpu/drm/asahi/g17/context.rs | 113 +++++++++++------ drivers/gpu/drm/asahi/g17/fence.rs | 153 ++++++++++++++++++++++-- drivers/gpu/drm/asahi/g17/queue/mod.rs | 15 ++- drivers/gpu/drm/asahi/g17/sync_output.c | 17 +++ drivers/gpu/drm/asahi/g17/sync_output.h | 4 + rust/kernel/drm/syncobj.rs | 13 ++ 6 files changed, 266 insertions(+), 49 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/context.rs b/drivers/gpu/drm/asahi/g17/context.rs index 89e4dabdc58d6..5775365a54bcd 100644 --- a/drivers/gpu/drm/asahi/g17/context.rs +++ b/drivers/gpu/drm/asahi/g17/context.rs @@ -721,6 +721,83 @@ impl Context { } } +/// Waits interruptibly for an admission ordinal while `ORDER_WINDOW` ioctls are +/// queued ahead of the oldest waiting one, returning `EBUSY` if the turn does +/// not move within 1 s, `EIO` on VM loss, or `ERESTARTSYS` on signals. +fn assign_order(order: &Mutex, order_wait: &CondVar, status: &VmStatus) -> Result { + let mut guard = order.lock(); + if let Some(ordinal) = guard.assign() { + return Ok(ordinal); + } + const ORDER_WAIT_NS: u64 = 1_000_000_000; + let deadline = super::now_ns().saturating_add(ORDER_WAIT_NS); + loop { + if status.get() != 0 { + return Err(EIO); + } + if let Some(ordinal) = guard.assign() { + return Ok(ordinal); + } + let remaining = deadline.saturating_sub(super::now_ns()); + if remaining == 0 { + return Err(EBUSY); + } + let slice_ms = (remaining.div_ceil(1_000_000) as u32).clamp(1, 50); + let jiffies = kernel::time::msecs_to_jiffies(slice_ms); + if matches!( + order_wait.wait_interruptible_timeout(&mut guard, jiffies), + CondVarTimeoutResult::Signal { .. } + ) { + return Err(ERESTARTSYS); + } + } +} + +#[cfg(CONFIG_KUNIT = "y")] +pub(super) const ORDER_WINDOW: u64 = identity::ORDER_WINDOW; + +#[cfg(CONFIG_KUNIT = "y")] +pub(super) struct TestOrderWindow { + order: core::pin::Pin>>, + wait: core::pin::Pin>, + status: Arc, + first: Option, +} + +#[cfg(CONFIG_KUNIT = "y")] +impl TestOrderWindow { + pub(super) fn new_full() -> Result { + let order = KBox::pin_init(new_mutex!(Order::new(), "G17 test order"), GFP_KERNEL)?; + let wait = KBox::pin_init(new_condvar!("G17 test order wait"), GFP_KERNEL)?; + let status = Arc::pin_init(VmStatus::new(), GFP_KERNEL)?; + let first = { + let mut guard = order.lock(); + let first = guard.assign().ok_or(EBUSY)?; + for _ in 1..identity::ORDER_WINDOW { + let _ = guard.assign().ok_or(EBUSY)?; + } + first + }; + Ok(Self { + order, + wait, + status, + first: Some(first), + }) + } + + pub(super) fn assign(&self) -> Result { + assign_order(&self.order, &self.wait, &self.status) + } + + pub(super) fn leave_oldest(&mut self) -> bool { + let Some(first) = self.first.take() else { + return false; + }; + self.order.lock().leave(first).submitters + } +} + /// Shared by all engines of one ioctl. Queued work initially consumes no node. pub(crate) struct WorkStateLease { context: Arc, @@ -737,41 +814,7 @@ impl WorkStateLease { /// waiting one (see `identity::Order`), returning EBUSY only if the turn does not move /// within the backpressure window. pub(crate) fn deferred(context: Arc) -> Result { - let mut order = context.order.lock(); - if let Some(ordinal) = order.assign() { - drop(order); - return Ok(Self { - context, - word: AtomicU32::new(FREE), - engine: AtomicU8::new(0), - ordinal, - }); - } - const ORDER_WAIT_NS: u64 = 1_000_000_000; - let deadline = super::now_ns().saturating_add(ORDER_WAIT_NS); - let ordinal = loop { - if context.status().get() != 0 { - return Err(EIO); - } - if let Some(ordinal) = order.assign() { - break ordinal; - } - let remaining = deadline.saturating_sub(super::now_ns()); - if remaining == 0 { - return Err(EBUSY); - } - let slice_ms = (remaining.div_ceil(1_000_000) as u32).clamp(1, 50); - let jiffies = kernel::time::msecs_to_jiffies(slice_ms); - if matches!( - context - .order_wait - .wait_interruptible_timeout(&mut order, jiffies), - CondVarTimeoutResult::Signal { .. } - ) { - return Err(ERESTARTSYS); - } - }; - drop(order); + let ordinal = assign_order(&context.order, &context.order_wait, context.status())?; Ok(Self { context, word: AtomicU32::new(FREE), diff --git a/drivers/gpu/drm/asahi/g17/fence.rs b/drivers/gpu/drm/asahi/g17/fence.rs index d47b4f2f88fd8..21318eaa0a3bd 100644 --- a/drivers/gpu/drm/asahi/g17/fence.rs +++ b/drivers/gpu/drm/asahi/g17/fence.rs @@ -5,7 +5,7 @@ //! These fences can complete out of order, so each has an independent timeline. //! Signalling belongs outside the device lock, after timestamp and VM-status writes. -use super::context::{Context, WorkStateLease}; +use super::context::WorkStateLease; use crate::file::SyncItem; use core::sync::atomic::{AtomicBool, AtomicI32, AtomicU32, AtomicU64, AtomicU8, AtomicUsize, Ordering}; use kernel::{ @@ -270,10 +270,6 @@ impl Submission { fn encode_epoch(epoch: u64) -> u64 { epoch.wrapping_add(1) } - fn new(context: Arc, firmware_source: bool) -> Result> { - let work = Arc::new(WorkStateLease::deferred(context)?, GFP_KERNEL)?; - Self::with_work(Some(work), firmware_source) - } fn with_work(work: Option>, firmware_source: bool) -> Result> { let vm_status = work.as_ref().map(|work| work.status().clone()); Arc::pin_init( @@ -587,6 +583,130 @@ mod tests { } assert_eq!(source.owners.load(Ordering::Acquire), 0); } + + #[cfg(CONFIG_KUNIT = "y")] + fn timeline_item(syncobj: kernel::drm::syncobj::SyncObj, point: u64) -> Result { + Ok(SyncItem { + syncobj, + fence: None, + chain_fence: Some(kernel::dma_fence::FenceChain::new()?), + timeline_value: point, + cumulative: None, + timeline: true, + }) + } + + #[cfg(CONFIG_KUNIT = "y")] + fn cumulative_item(syncobj: kernel::drm::syncobj::SyncObj, point: u64) -> SyncItem { + SyncItem { + syncobj, + fence: None, + chain_fence: None, + timeline_value: point, + cumulative: None, + timeline: true, + } + } + + #[test] + fn full_window_signal_leaves_timeline_untouched_for_restart() -> Result { + unsafe extern "C" { + fn asahi_g17_kunit_raise_sigterm(); + fn asahi_g17_kunit_clear_sigterm(); + } + let contexts = contexts(); + let allocation_failure = allocation_failure(&contexts)?; + let syncobj = kernel::drm::syncobj::SyncObj::new_kunit()?; + let point1 = independent(&contexts)?; + syncobj.add_point( + kernel::dma_fence::FenceChain::new()?, + &Fence::from_fence(&point1), + 1, + ); + point1.signal(); + + let mut window = super::super::context::TestOrderWindow::new_full()?; + let mut syncs = KVec::with_capacity(1, GFP_KERNEL)?; + syncs.push(timeline_item(syncobj.clone(), 2)?, GFP_KERNEL)?; + let mut outputs = Outputs::new(&contexts, syncs, 0, &allocation_failure)?; + + // SAFETY: Test-only KUnit helper raises SIGTERM on the current test kthread + // and clears it immediately after `assign()` observes `-ERESTARTSYS`. + unsafe { asahi_g17_kunit_raise_sigterm() }; + let wait_result = window.assign(); + // SAFETY: Always flush and disallow SIGTERM before leaving the test thread. + unsafe { asahi_g17_kunit_clear_sigterm() }; + assert_eq!(wait_result, Err(ERESTARTSYS)); + + outputs.set_result(Err(ERESTARTSYS)); + assert!(outputs.output_fence().is_none()); + drop(outputs); + + // A timeline waiter for point 2 must not observe an error fence after -ERESTARTSYS. + let head = syncobj.fence_get().unwrap(); + assert_eq!(head.chain_find_seqno(2).err(), Some(EINVAL)); + let p1 = syncobj.fence_get().unwrap().chain_find_seqno(1)?.unwrap(); + assert!(super::super::job::fence_status(&p1) > 0); + + // Cumulative SignalBundle outputs must likewise stay untouched on -ERESTARTSYS. + let anchor = kernel::drm::syncobj::SyncObj::new_kunit()?; + let p1_anchor = independent(&contexts)?; + anchor.add_point( + kernel::dma_fence::FenceChain::new()?, + &Fence::from_fence(&p1_anchor), + 1, + ); + p1_anchor.signal(); + let mut cum_syncs = KVec::with_capacity(2, GFP_KERNEL)?; + cum_syncs.push(cumulative_item(anchor.clone(), 2), GFP_KERNEL)?; + cum_syncs.push(timeline_item(syncobj.clone(), 2)?, GFP_KERNEL)?; + let bundle = super::super::sync_output::SignalBundle::new(&cum_syncs, 0)?; + cum_syncs[0].cumulative = Some(bundle); + let mut cum_outputs = Outputs::new(&contexts, cum_syncs, 0, &allocation_failure)?; + cum_outputs.set_result(Err(ERESTARTSYS)); + assert!(cum_outputs.output_fence().is_none()); + drop(cum_outputs); + assert_eq!(anchor.fence_get().unwrap().chain_find_seqno(2).err(), Some(EINVAL)); + assert_eq!(syncobj.fence_get().unwrap().chain_find_seqno(2).err(), Some(EINVAL)); + + // Simulate kernel ioctl restart once the oldest ordinal moves: point 2 is + // published exactly once and a timeline waiter sees it transition pending -> done. + assert!(window.leave_oldest()); + let restarted_ordinal = window.assign()?; + assert_eq!(restarted_ordinal, super::super::context::ORDER_WINDOW); + + let mut restart_syncs = KVec::with_capacity(1, GFP_KERNEL)?; + restart_syncs.push(timeline_item(syncobj.clone(), 2)?, GFP_KERNEL)?; + let mut restart_outputs = Outputs::new(&contexts, restart_syncs, 0, &allocation_failure)?; + let aggregate = restart_outputs.publish_with(None, false, false)?; + let member = aggregate.member(); + let point2 = syncobj.fence_get().unwrap().chain_find_seqno(2)?.unwrap(); + assert_eq!(super::super::job::fence_status(&point2), 0); + // SAFETY: `point2` owns a live dma_fence reference for this zero-timeout poll. + assert_eq!(unsafe { bindings::dma_fence_wait_timeout(point2.raw(), false, 0) }, 0); + + restart_outputs.set_result(Ok(())); + drop(restart_outputs); + assert_eq!(super::super::job::fence_status(&point2), 0); + member.complete(Ok(())); + assert!(super::super::job::fence_status(&point2) > 0); + // SAFETY: `point2` owns a live dma_fence reference for this zero-timeout poll. + assert!(unsafe { bindings::dma_fence_wait_timeout(point2.raw(), false, 0) } > 0); + drop(member); + + // Non-ERESTARTSYS failures (e.g. full-window timeout -> -EBUSY) still publish + // an error fence. + let mut fail_syncs = KVec::with_capacity(1, GFP_KERNEL)?; + fail_syncs.push(timeline_item(syncobj.clone(), 3)?, GFP_KERNEL)?; + let mut fail_outputs = Outputs::new(&contexts, fail_syncs, 0, &allocation_failure)?; + fail_outputs.set_result(Err(EBUSY)); + let fail_fence = fail_outputs.output_fence().unwrap(); + drop(fail_outputs); + let point3 = syncobj.fence_get().unwrap().chain_find_seqno(3)?.unwrap(); + assert!(super::super::job::fence_status(&point3) > 0); + assert_eq!(super::super::job::fence_status(&fail_fence), EBUSY.to_errno()); + Ok(()) + } } impl Drop for Member { fn drop(&mut self) { @@ -649,6 +769,9 @@ impl Outputs { } /// The fence installed, or about to be installed on error, in every output sync. pub(crate) fn output_fence(&self) -> Option { + if self.submission.is_none() && self.result == Err(ERESTARTSYS) { + return None; + } self.submission .as_ref() .map(Aggregate::fence) @@ -656,11 +779,19 @@ impl Outputs { } pub(crate) fn publish( &mut self, - context: Arc, + work: Arc, firmware_source: bool, compute_only: bool, ) -> Result { - let source = Submission::new(context, firmware_source)?; + self.publish_with(Some(work), firmware_source, compute_only) + } + fn publish_with( + &mut self, + work: Option>, + firmware_source: bool, + compute_only: bool, + ) -> Result { + let source = Submission::with_work(work, firmware_source)?; let mut fence = self.fallback.take().ok_or(EINVAL)?; fence.source = Some(source); fence.scoped = self.cumulative.is_some(); @@ -675,9 +806,8 @@ impl Outputs { } Ok(submission) } - pub(crate) fn finish(mut self, result: Result) -> Result { + pub(crate) fn set_result(&mut self, result: Result) { self.result = result; - result } } impl Drop for Outputs { @@ -690,6 +820,11 @@ impl Drop for Outputs { return; } if let Some(fallback) = self.fallback.take() { + // No work was accepted and no fence was published; leave output + // syncobjs untouched so a restarted ioctl can publish them once. + if self.result == Err(ERESTARTSYS) { + return; + } if let Err(error) = self.result { fallback.set_error(error); } diff --git a/drivers/gpu/drm/asahi/g17/queue/mod.rs b/drivers/gpu/drm/asahi/g17/queue/mod.rs index f480b7cda3dec..0e2b5858666f4 100644 --- a/drivers/gpu/drm/asahi/g17/queue/mod.rs +++ b/drivers/gpu/drm/asahi/g17/queue/mod.rs @@ -566,9 +566,6 @@ impl crate::queue::Queue for Queue { let has_render = pending .iter() .any(|(command, _, _)| matches!(command, Validated::Render { .. })); - if has_render { - self.context.note_render(); - } let compute_only = !has_render && !pending.is_empty(); if super::timeout::select_submission(pending.iter().map(|(cmd, _, _)| cmd.long_job())) { for (command, _, _) in pending.iter_mut() { @@ -597,7 +594,14 @@ impl crate::queue::Queue for Queue { if *visible_completion || !visible_required) && !pending[0].2.has_outputs() && !self.has_pending_publication(&pending[0].1); - let aggregate = outputs.publish(self.context.clone(), firmware_source, compute_only)?; + let work = Arc::new( + super::context::WorkStateLease::deferred(self.context.clone())?, + GFP_KERNEL, + )?; + if has_render { + self.context.note_render(); + } + let aggregate = outputs.publish(work, firmware_source, compute_only)?; let output_fence = aggregate.fence(); super::trace::submit( 1, @@ -630,6 +634,7 @@ impl crate::queue::Queue for Queue { self.backend.fail_vm(error); } } + outputs.set_result(result); let output_fence = outputs.output_fence(); super::trace::submit( 2, @@ -640,7 +645,7 @@ impl crate::queue::Queue for Queue { result.as_ref().err().map_or(0, |error| error.to_errno()), output_fence.as_ref(), ); - outputs.finish(result) + result } } diff --git a/drivers/gpu/drm/asahi/g17/sync_output.c b/drivers/gpu/drm/asahi/g17/sync_output.c index 191ebdfc04cdb..243dfcb57521a 100644 --- a/drivers/gpu/drm/asahi/g17/sync_output.c +++ b/drivers/gpu/drm/asahi/g17/sync_output.c @@ -137,3 +137,20 @@ void asahi_g17_signals_fail(struct asahi_g17_signal_bundle *bundle, asahi_g17_signal_install(&bundle->targets[i], failure); bundle->published = true; } + +#ifdef CONFIG_KUNIT +#include +#include + +void asahi_g17_kunit_raise_sigterm(void) +{ + allow_signal(SIGTERM); + send_sig(SIGTERM, current, 1); +} + +void asahi_g17_kunit_clear_sigterm(void) +{ + flush_signals(current); + disallow_signal(SIGTERM); +} +#endif diff --git a/drivers/gpu/drm/asahi/g17/sync_output.h b/drivers/gpu/drm/asahi/g17/sync_output.h index cee1146372fc6..ad8edca47c604 100644 --- a/drivers/gpu/drm/asahi/g17/sync_output.h +++ b/drivers/gpu/drm/asahi/g17/sync_output.h @@ -20,4 +20,8 @@ int asahi_g17_signals_publish(struct asahi_g17_signal_bundle *bundle, struct dma_fence *submitted, bool compute_only); void asahi_g17_signals_fail(struct asahi_g17_signal_bundle *bundle, struct dma_fence *failure); +#ifdef CONFIG_KUNIT +void asahi_g17_kunit_raise_sigterm(void); +void asahi_g17_kunit_clear_sigterm(void); +#endif #endif diff --git a/rust/kernel/drm/syncobj.rs b/rust/kernel/drm/syncobj.rs index 72ba50c57286b..41c074a6dd2a1 100644 --- a/rust/kernel/drm/syncobj.rs +++ b/rust/kernel/drm/syncobj.rs @@ -23,6 +23,19 @@ impl SyncObj { self.ptr } + /// Creates an empty sync object for KUnit tests. + #[cfg(CONFIG_KUNIT = "y")] + pub fn new_kunit() -> Result { + let mut ptr = core::ptr::null_mut(); + // SAFETY: `ptr` is valid for writing the created sync object pointer. + let ret = unsafe { bindings::drm_syncobj_create(&mut ptr, 0, core::ptr::null_mut()) }; + if ret != 0 { + Err(Error::from_errno(ret)) + } else { + Ok(SyncObj { ptr }) + } + } + /// Looks up a sync object by its handle for a given `File`. pub fn lookup_handle( file: &drm::File, From 5d9bcf092530c961a103089a647891cee4fa6e44 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 13:26:15 -0500 Subject: [PATCH 3/3] drm/asahi: g17: use KVVec in signal interruption KUnit test Commit fad5d04a1fa8 ("drm/asahi: keep a submission's syncs and command copy off contiguous pages") changed Outputs::new() to accept KVVec instead of KVec. Update the sync vectors in full_window_signal_leaves_timeline_untouched_for_restart() to match so KUnit builds cleanly after merging main. Signed-off-by: Erik Bourget --- drivers/gpu/drm/asahi/g17/fence.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/fence.rs b/drivers/gpu/drm/asahi/g17/fence.rs index bcd6c5abfd0c0..430d57e8b2544 100644 --- a/drivers/gpu/drm/asahi/g17/fence.rs +++ b/drivers/gpu/drm/asahi/g17/fence.rs @@ -626,7 +626,7 @@ mod tests { point1.signal(); let mut window = super::super::context::TestOrderWindow::new_full()?; - let mut syncs = KVec::with_capacity(1, GFP_KERNEL)?; + let mut syncs = KVVec::with_capacity(1, GFP_KERNEL)?; syncs.push(timeline_item(syncobj.clone(), 2)?, GFP_KERNEL)?; let mut outputs = Outputs::new(&contexts, syncs, 0, &allocation_failure)?; @@ -657,7 +657,7 @@ mod tests { 1, ); p1_anchor.signal(); - let mut cum_syncs = KVec::with_capacity(2, GFP_KERNEL)?; + let mut cum_syncs = KVVec::with_capacity(2, GFP_KERNEL)?; cum_syncs.push(cumulative_item(anchor.clone(), 2), GFP_KERNEL)?; cum_syncs.push(timeline_item(syncobj.clone(), 2)?, GFP_KERNEL)?; let bundle = super::super::sync_output::SignalBundle::new(&cum_syncs, 0)?; @@ -675,7 +675,7 @@ mod tests { let restarted_ordinal = window.assign()?; assert_eq!(restarted_ordinal, super::super::context::ORDER_WINDOW); - let mut restart_syncs = KVec::with_capacity(1, GFP_KERNEL)?; + let mut restart_syncs = KVVec::with_capacity(1, GFP_KERNEL)?; restart_syncs.push(timeline_item(syncobj.clone(), 2)?, GFP_KERNEL)?; let mut restart_outputs = Outputs::new(&contexts, restart_syncs, 0, &allocation_failure)?; let aggregate = restart_outputs.publish_with(None, false, false)?; @@ -696,7 +696,7 @@ mod tests { // Non-ERESTARTSYS failures (e.g. full-window timeout -> -EBUSY) still publish // an error fence. - let mut fail_syncs = KVec::with_capacity(1, GFP_KERNEL)?; + let mut fail_syncs = KVVec::with_capacity(1, GFP_KERNEL)?; fail_syncs.push(timeline_item(syncobj.clone(), 3)?, GFP_KERNEL)?; let mut fail_outputs = Outputs::new(&contexts, fail_syncs, 0, &allocation_failure)?; fail_outputs.set_result(Err(EBUSY));