From cdd27c5b135e93074c10c8115c08d5f19e05b2ef Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Sun, 22 Nov 2026 19:26:24 +0100 Subject: [PATCH 1/2] drm/asahi: g18: say which timeline points the input error check sees asahi_g18_fence_error() walks a dma_fence_chain with dma_fence_chain_walk(), which unlinks the earlier points that have signalled. A job's inputs have all signalled when it runs, so of a timeline only the head point's own fence is checked, and an error on an earlier point is not seen. The comment said every fence was checked. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/asahi/g18/sync_input.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/asahi/g18/sync_input.c b/drivers/gpu/drm/asahi/g18/sync_input.c index f443ff776f11d..53d50bae534db 100644 --- a/drivers/gpu/drm/asahi/g18/sync_input.c +++ b/drivers/gpu/drm/asahi/g18/sync_input.c @@ -37,7 +37,11 @@ int asahi_g18_wait_foreign_fences(struct dma_fence *head, * too, or 0. A container does not carry its fences' errors: a * dma_fence_chain (a timeline point) never copies them, and a * dma_fence_array whose signalling was never enabled clears its pending - * error when it is found signalled. So each fence is checked itself. + * error when it is found signalled. So each fence the walk reaches is + * checked itself. Of a timeline, the walk reaches only the head point: + * dma_fence_chain_walk() unlinks the earlier points that have signalled, and + * a job's inputs have all signalled when it runs, so an error on an earlier + * point is not seen. */ int asahi_g18_fence_error(struct dma_fence *head) { From 676064c49b98365cac91ac0662c9524d8d3b63f8 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Sun, 22 Nov 2026 19:26:24 +0100 Subject: [PATCH 2/2] drm/asahi: g18: bound, rate-limit and drain the async job queue Three follow-ups to the asynchronous submit path (#2679): - A failed job is reported at most once a second, with the number of failures not reported in between, unless VerboseFaults is set, as for GPU faults. A client that submits failing jobs in a loop could otherwise flood the log; the output fence and the queue's next submit still carry every error. - The device holds at most 256 jobs waiting to run, over all queues and files. Each queue was bounded at 32, but a file can open any number of queues. A submit takes one of the 256 slots after it has waited for other drivers' fences and before it publishes anything, so a signal restarts the ioctl as the existing waits do. The slot becomes the job, or is given back if the submit fails. The bound is device-wide on purpose: one worker runs the jobs in the order they were accepted, so a client blocked by a full device would only have waited on its fence instead. - Jobs run on the device's own unbound workqueue instead of system_unbound, with WQ_MEM_RECLAIM, since memory reclaim can wait for the fences its jobs signal. The worker is a work item that is part of the job queue, and the queue of waiting jobs is allocated for 256 up front, so nothing is allocated under the state lock, which the worker takes while fences are pending. When G18DrmGpu is dropped, the queue is taken out of the job state and destroyed, which waits for the worker to return, so no job work item outlives the device. A submit after that is refused with -ENODEV. On the J873 this happens only on the probe error path today: once the firmware session starts, the module is pinned until reboot and the platform driver has no bind attributes. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/asahi/g18/drm.rs | 168 +++++++++++++++++++++++++++---- 1 file changed, 148 insertions(+), 20 deletions(-) diff --git a/drivers/gpu/drm/asahi/g18/drm.rs b/drivers/gpu/drm/asahi/g18/drm.rs index e011c75f6ba2b..14a726b24fc4c 100644 --- a/drivers/gpu/drm/asahi/g18/drm.rs +++ b/drivers/gpu/drm/asahi/g18/drm.rs @@ -24,7 +24,9 @@ use core::ops::Range; use core::sync::atomic::{AtomicI32, Ordering}; use kernel::{bindings, macros::vtable, new_condvar, new_mutex, prelude::*, static_lock_class, dma_fence::{Fence, FenceContexts, FenceObject, FenceOps, RawDmaFence, UserFence}, - sync::{Arc, CondVar, Mutex, SpinLock}, uapi, workqueue, xarray}; + sync::{Arc, CondVar, Mutex, SpinLock}, time::{Delta, Instant, Monotonic}, uapi, workqueue, + xarray}; +use crate::debug::{debug_enabled, DebugFlags}; use crate::{alloc, driver::AsahiRegData, file, g18::uapi::command as g17_uapi, g18::hw, g18::uapi::application::{self as g18_application, G18ApplicationRefusal, G18RenderJobSink, G18RenderRetirement}, gem, gpu, g18::fw::vm_layout as m5_vm, mmu, queue, stats, stats_raw, timestamp}; @@ -154,6 +156,14 @@ impl G18DrmGpu { } } +impl Drop for G18DrmGpu { + /// Every file has let go of the device: finish the accepted jobs and + /// destroy the worker queue before the module can be unloaded. + fn drop(&mut self) { + self.jobs.shut_down(); + } +} + // Module 1134 reached G18 through its `DrmGpu` trait (drm_gpu.rs) and // computed GET_PARAMS in file.rs from `DrmGpu::params`; here the same facts // fill `drm_asahi_params_global` through `gpu::Gpu::get_params`. The shared @@ -340,6 +350,22 @@ impl Drop for JobFence { /// Accepted jobs a queue may have waiting or running; a further submit /// waits for the oldest. const QUEUE_DEPTH: usize = 32; +/// Jobs waiting to run on the device, over all its queues and files, counting +/// the submits that hold a [`Room`]; a further submit waits for room before +/// it publishes anything. A file may open any number of queues, and each may +/// hold [`QUEUE_DEPTH`]. A waiting job holds its translated commands, its +/// input fences and its VM job guard, which keeps the mappings it uses +/// pinned, so this bounds the kernel memory and the mappings that work not +/// yet run can hold. 256 is eight queues at [`QUEUE_DEPTH`]. +/// +/// The bound is device-wide, so a file that fills it makes another client's +/// submit wait in the ioctl. That is intended: one worker runs the jobs in +/// the order they were accepted, so that client's job would run after those +/// 256 anyway, and the cap only moves its wait from the output fence into +/// the ioctl. A per-file share would change who waits, not when work runs. +const DEVICE_DEPTH: usize = 256; +/// The shortest time between two reports of a failed asynchronous job. +const FAILURE_REPORT_INTERVAL: Delta = Delta::from_millis(1000); /// One accepted submission. Dropping it signals its output fence. struct G18Job { @@ -362,7 +388,7 @@ struct G18Job { } impl G18Job { - fn run(mut job: KBox) { + fn run(mut job: KBox, jobs: &JobQueue) { let result = job.execute(); // Release the mappings before the fence signals, as a synchronous // submit did. @@ -373,9 +399,14 @@ impl G18Job { // The ioctl returned long ago and cannot report this; the output // fence carries the error and the queue's next submit returns it. (None, Err(error)) => { - let len = job.comm.iter().position(|&c| c == 0).unwrap_or(job.comm.len()); - pr_warn!("G18 job failed: submission {} pid {} ({}): {:?}\n", job.submission, - job.pid, kernel::str::BStr::from_bytes(&job.comm[..len]), error); + if let Some(unreported) = jobs.failure_report() { + if unreported > 0 { + pr_warn!("G18: {} more failed jobs not reported\n", unreported); + } + let len = job.comm.iter().position(|&c| c == 0).unwrap_or(job.comm.len()); + pr_warn!("G18 job failed: submission {} pid {} ({}): {:?}\n", job.submission, + job.pid, kernel::str::BStr::from_bytes(&job.comm[..len]), error); + } let _ = job.queue_failed.compare_exchange(0, error.to_errno(), Ordering::AcqRel, Ordering::Acquire); } @@ -442,27 +473,59 @@ impl G18Job { struct JobQueue { #[pin] state: Mutex, - /// Notified whenever `completed` advances. + /// Notified whenever `completed` advances, or a [`Room`] is given back. #[pin] retired: CondVar, + /// The worker, queued on `workers` to drain `pending`. It is part of the + /// queue, so starting it allocates nothing under the state lock, which + /// the fence-signalling worker takes. + #[pin] + work: workqueue::Work, +} + +kernel::impl_has_work! { + impl HasWork for JobQueue { self.work } +} + +impl workqueue::WorkItem for JobQueue { + type Pointer = Arc; + + fn run(this: Arc) { + this.drain(); + } } struct JobQueueState { + /// Allocated for [`DEVICE_DEPTH`] jobs up front: nothing is allocated + /// under the state lock. pending: KVec>, /// A worker is draining `pending`. running: bool, /// Sequence numbers of the last accepted and the last finished job. submitted: u64, completed: u64, + /// Submits holding a [`Room`]: they may each add one job. + reserved: usize, + /// The device's own worker queue; taken, and so drained, when the device + /// goes away. Without it no job is accepted. Its jobs signal fences that + /// memory reclaim can wait for, so it has a rescuer (WQ_MEM_RECLAIM). + workers: Option, + /// When the last failed job was reported, and how many have not been since. + reported: Option>, + unreported: u32, } impl JobQueue { fn new() -> Result> { + let workers = workqueue::OwnedQueue::new_unbound_reclaim(c"aurora_g18_jobs", false)?; + let pending = KVec::with_capacity(DEVICE_DEPTH, GFP_KERNEL)?; Arc::pin_init(pin_init!(Self { state <- new_mutex!(JobQueueState { - pending: KVec::new(), running: false, submitted: 0, completed: 0, + pending, running: false, submitted: 0, completed: 0, + reserved: 0, workers: Some(workers), reported: None, unreported: 0, }, "G18 jobs"), retired <- new_condvar!("G18 job retired"), + work <- kernel::new_work!("G18 job worker"), }), GFP_KERNEL) } @@ -471,21 +534,24 @@ impl JobQueue { /// cannot be queued. The fence is installed under the lock, once the /// job has its place: a submission that finds it in a syncobj is /// accepted later and so runs after the job that signals it. - fn push(self: &Arc, publication: OutputPublication, + fn push(self: &Arc, room: Room, publication: OutputPublication, job: impl FnOnce(JobFence) -> KBox) - -> core::result::Result { + -> core::result::Result { + // On a refusal `room`, a parameter, is dropped after `state` and gives + // its slot back then; an accepted job takes it over under the lock. let mut state = self.state.lock(); - if state.pending.reserve(1, GFP_KERNEL).is_err() { return Err(publication); } + let Some(workers) = state.workers.as_ref() else { return Err((publication, ENODEV)) }; + // The room keeps pending.len() below DEVICE_DEPTH, the capacity. + if state.pending.len() >= state.pending.capacity() { return Err((publication, ENOMEM)); } if !state.running { - let this = self.clone(); - if workqueue::system_unbound().try_spawn(GFP_KERNEL, move || this.drain()).is_err() { - return Err(publication); - } + // Already queued means a drain is about to run anyway. + let _ = workers.enqueue(self.clone()); state.running = true; } let job = job(publication.accept()); - // Cannot fail: room was reserved above. + // Cannot fail: the capacity was checked above, under the same lock. let _ = state.pending.push_within_capacity(job); + room.consume(&mut state); state.submitted += 1; Ok(state.submitted) } @@ -499,12 +565,48 @@ impl JobQueue { Err(_) => { state.running = false; return; } } }; - G18Job::run(job); + G18Job::run(job, self); self.state.lock().completed += 1; self.retired.notify_all(); } } + /// Wait, interruptibly, until the jobs waiting to run and the [`Room`]s + /// taken are fewer than [`DEVICE_DEPTH`], then take one. Called before + /// anything is published, so a signal restarts the ioctl. + fn wait_room(self: &Arc) -> Result { + let mut state = self.state.lock(); + while state.pending.len() + state.reserved >= DEVICE_DEPTH { + if self.retired.wait_interruptible(&mut state) { return Err(ERESTARTSYS); } + } + state.reserved += 1; + Ok(Room(Some(self.clone()))) + } + + /// Whether to report a failed job now, with how many were not reported + /// since the last report: at most one report per [`FAILURE_REPORT_INTERVAL`]. + /// With `VerboseFaults` set every failure is reported, as GPU faults are. + fn failure_report(&self) -> Option { + let mut state = self.state.lock(); + let recent = state.reported.as_ref() + .is_some_and(|last| last.elapsed() < FAILURE_REPORT_INTERVAL); + if recent && !debug_enabled(DebugFlags::VerboseFaults) { + state.unreported = state.unreported.saturating_add(1); + return None; + } + state.reported = Some(Instant::now()); + Some(core::mem::take(&mut state.unreported)) + } + + /// Stop accepting jobs and wait for the worker to finish the accepted + /// ones, so that no work item of this module runs after it goes away. + fn shut_down(&self) { + let workers = self.state.lock().workers.take(); + // Dropping the queue destroys it, which drains it; the worker takes the + // state lock, so it must not be held here. + drop(workers); + } + /// Wait until job `seq` has finished and its fence has signalled. fn wait(&self, seq: u64, interruptible: bool) -> Result { let mut state = self.state.lock(); @@ -519,6 +621,28 @@ impl JobQueue { } } +/// A slot taken by [`JobQueue::wait_room`]: its submit may add one job. +/// [`JobQueue::push`] turns it into that job; dropping it gives the slot back. +struct Room(Option>); + +impl Room { + /// The job is in `pending` now and holds the slot itself: no waiter can + /// get in, so none is woken. + fn consume(mut self, state: &mut JobQueueState) { + state.reserved -= 1; + self.0 = None; + } +} + +impl Drop for Room { + fn drop(&mut self) { + if let Some(jobs) = self.0.take() { + jobs.state.lock().reserved -= 1; + jobs.retired.notify_all(); + } + } +} + struct UserAddressSpace<'a> { vm: &'a mmu::Vm } impl g17_uapi::GpuAddressSpace for UserAddressSpace<'_> { fn covers(&self, address: u64, size: u64, access: g17_uapi::GpuAccess) -> bool { @@ -593,6 +717,9 @@ impl queue::Queue for G18Queue { let fence: UserFence = self.fences.new_fence(0, G18SubmissionFence)?.into(); wait_foreign_inputs(&fence, &syncs[..in_sync_count])?; + // Taken after the foreign inputs, so that a submit waiting for + // another driver's fence holds no slot meanwhile. + let room = self.jobs.wait_room()?; let synchronous = !self.asynchronous || syncs.len() == in_sync_count; let mut publication = OutputPublication { fence: Some(fence), syncs, input_count: in_sync_count, result: Err(ECANCELED), @@ -611,6 +738,7 @@ impl queue::Queue for G18Queue { let (slot, result, inputs, vm_job, commands) = match accepted { Ok(accepted) => accepted, Err(error) => { + drop(room); // The runtime records and quarantines a failed published VM // before this output fence is installed/signalled by the // publication guard, after the queue's earlier fences. @@ -629,13 +757,13 @@ impl queue::Queue for G18Queue { fence, queue_failed: self.failed.clone(), submission: submission_id, pid: task.pid(), comm, }); - let seq = match self.jobs.push(publication, job) { + let seq = match self.jobs.push(room, publication, job) { Ok(seq) => seq, - Err(mut publication) => { + Err((mut publication, error)) => { // As above: the fence signals after the queue's earlier ones. - publication.result = Err(ENOMEM); + publication.result = Err(error); self.jobs.wait(self.last, false)?; - return Err(ENOMEM); + return Err(error); } }; self.recent[self.next] = seq;