From 64ecf49a80a59e71c4b5d2be1ec8c3daba98eeb6 Mon Sep 17 00:00:00 2001 From: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:50:50 -0800 Subject: [PATCH 1/3] drm/asahi: g18: wake the submitter when its job's worker finishes ApplicationOwner::work() runs a job on a worker and the submitting thread polls the ticket word with fsleep(): every 20 us for the first 2 ms of its wait, every 200 us after that. The wait starts at the spawn, so it includes queueing behind other clients' jobs, and in a desktop session most submissions were in the 200 us regime: they returned 0.10-0.14 ms (up to 0.28 ms) after their job had ended. Let the worker complete a Completion and the caller sleep on it, in 1 s slices so that a long wait (queued behind other clients' jobs and their recovery) does not trip the hung-task detector. The ticket word still carries the outcome, and the worker releases the session lock before the wake-up, since the caller or the next client's worker takes it next. The completion is signalled from a drop guard, so a worker closure that is dropped without running also wakes the caller, which then sees the ticket still running and fails with EIO rather than waiting forever. Measured on a MacBook Pro (M6, J804) from the driver's job phase timestamps, on an earlier base (integration/aurora f5fa539) with the J804 firmware support that is not part of this series, and without the 1 s slices. Not tested on J873. Signed-off-by: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- drivers/gpu/drm/asahi/g18/boot/session.rs | 36 ++++++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/asahi/g18/boot/session.rs b/drivers/gpu/drm/asahi/g18/boot/session.rs index a44fe7f872f22..9805c6e8b75c6 100644 --- a/drivers/gpu/drm/asahi/g18/boot/session.rs +++ b/drivers/gpu/drm/asahi/g18/boot/session.rs @@ -37,6 +37,10 @@ impl ApplicationOwner { let session=self.session.clone();let failed=self.failed.clone(); let quarantined=self.quarantined.clone(); let ticket=Arc::new(AtomicI32::new(TICKET_RUNNING),GFP_KERNEL)?;let done=ticket.clone(); + // The caller sleeps on `gate`; the worker's guard completes it when + // the closure ends, or is dropped unrun. + let gate=Arc::pin_init(kernel::sync::Completion::new(),GFP_KERNEL)?; + let signal=TicketSignal(gate.clone()); kernel::workqueue::system_long().try_spawn(GFP_KERNEL,move || { let mut owner=session.lock(); let result=if failed.load(Ordering::Acquire) {Err(Failure::Fatal(EIO))} else { @@ -75,16 +79,23 @@ impl ApplicationOwner { Err(Failure::Job(JobFailure::Recovered(_) | JobFailure::Unbound(..) | JobFailure::Released))=>JobOutcome::Recovered as i32, Err(Failure::Job(JobFailure::Stranded(..) | JobFailure::Retained))=>JobOutcome::Stranded as i32, },Ordering::Release); + // The outcome is stored. Release the session before waking the + // caller: it, or the next client's worker, takes that lock next. + drop(owner); + drop(signal); })?; let waited=Instant::::now(); - loop { - match ticket.load(Ordering::Acquire) { - TICKET_RUNNING => fsleep(Delta::from_micros(recovery::poll_interval_us(waited.elapsed().as_micros_ceil()))), - 0 => {recovery::SUBMIT_LATENCY.record(waited.elapsed().as_micros_ceil());return Ok(())} - code if code==JobOutcome::Recovered as i32 => return Err(Failure::Job(JobFailure::Released)), - code if code==JobOutcome::Stranded as i32 => return Err(Failure::Job(JobFailure::Retained)), - errno => return Err(Failure::Fatal(Error::from_errno(errno))), - } + // Sleep in slices: queued behind other clients and recovery, a wait + // can outlast the hung-task timeout, which a single uninterruptible + // sleep would trip. + while !gate.wait_for_completion_timeout(kernel::time::msecs_to_jiffies(1000)) {} + match ticket.load(Ordering::Acquire) { + // The worker closure was dropped without running. + TICKET_RUNNING => Err(Failure::Fatal(EIO)), + 0 => {recovery::SUBMIT_LATENCY.record(waited.elapsed().as_micros_ceil());Ok(())} + code if code==JobOutcome::Recovered as i32 => Err(Failure::Job(JobFailure::Released)), + code if code==JobOutcome::Stranded as i32 => Err(Failure::Job(JobFailure::Retained)), + errno => Err(Failure::Fatal(Error::from_errno(errno))), } } @@ -101,6 +112,15 @@ impl ApplicationOwner { } } +/// Wakes the thread waiting in [`ApplicationOwner::work`] when dropped: at the +/// end of the worker closure, or when the closure is dropped without running. +struct TicketSignal(Arc); +impl Drop for TicketSignal { + fn drop(&mut self) { + self.0.complete_all(); + } +} + /// Worker ticket values besides 0 and negative errnos. const TICKET_RUNNING: i32 = 1; #[repr(i32)] From 3fc52d095f570c842f303e2f8ad60052c15746ca Mon Sep 17 00:00:00 2001 From: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:51:03 -0800 Subject: [PATCH 2/3] drm/asahi: g18: notice a retired job sooner wait_job() polls the firmware state every 20 us for the first 2 ms of a job and every 200 us after that. Every full-panel job takes longer than 2 ms (2-10 ms), so its retirement was noticed about 0.14 ms late on average: 7 of 12 sampled long jobs show one extra 230-282 us step between the scheduler's completion and the last retirement condition. Keep the fine interval for the first 20 ms. The retirement conditions also do not become true together: the scheduler's completed count and the stage stamps lead the inner queue counters and the completion masks by 50-150 us, and each sleeping poll added 60-100 us on top. Once the render path's check sees the scheduler completed or the stamps written, re-poll with a 2 us delay instead of sleeping, for at most 200 us, then sleep as before. The retirement condition itself is unchanged. Measured on a MacBook Pro (M6, J804) from the driver's job phase timestamps, on an earlier base (integration/aurora f5fa539) with the J804 firmware support that is not part of this series. Not tested on J873. Signed-off-by: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- drivers/gpu/drm/asahi/g18/boot/recovery.rs | 26 ++++++++++++++++++++-- drivers/gpu/drm/asahi/g18/boot/session.rs | 5 +++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/asahi/g18/boot/recovery.rs b/drivers/gpu/drm/asahi/g18/boot/recovery.rs index d60912f869a6f..01849cc5abf7a 100644 --- a/drivers/gpu/drm/asahi/g18/boot/recovery.rs +++ b/drivers/gpu/drm/asahi/g18/boot/recovery.rs @@ -43,11 +43,22 @@ const JOB_WATCHDOG_MS: u32 = 10_000; const RECOVERED_RETIRE_MS: u32 = 2000; /// Poll interval for a wait that has lasted `us`: fine while a job is -/// likely to be about to retire, coarse once it has clearly not. +/// likely to be about to retire, coarse once it has clearly not. Full-panel +/// jobs take 2-10 ms, so the fine interval covers them. pub(super) const fn poll_interval_us(us: i64) -> i64 { - if us < 2_000 { 20 } else if us < 50_000 { 200 } else { 1_000 } + if us < 20_000 { 20 } else if us < 50_000 { 200 } else { 1_000 } } +/// Set by a job's retirement check once its stages have finished (the +/// scheduler's completed count or the stage stamps): the conditions that +/// remain follow within 50-150 us. One job is in flight at a time, under the +/// session lock; wait_job clears this before its first poll. +pub(super) static RETIRE_NEAR: AtomicBool = AtomicBool::new(false); +/// How long wait_job re-polls without sleeping after RETIRE_NEAR, and the +/// delay between those polls. +const RETIRE_NEAR_SPIN_US: i64 = 200; +const RETIRE_NEAR_STEP_US: i64 = 2; + /// Upper bounds, in microseconds, of the latency buckets; the last bucket is /// open-ended. const LATENCY_BOUNDS_US: [i64; 6] = [100, 300, 1_000, 3_000, 10_000, 100_000]; @@ -392,6 +403,8 @@ impl G18Session { let mut recovery: Option = None; let mut since = Instant::::now(); let mut reported = 0u32; + let mut near_since: Option = None; + RETIRE_NEAR.store(false, Ordering::Relaxed); loop { if let Some(serviced) = self.poll_faults(label)? { if recovery.is_none() { @@ -426,6 +439,15 @@ impl G18Session { since = Instant::::now(); reported = 0; } + // Stages finished and no recovery: the last conditions are a few + // tens of microseconds away, less than one sleeping poll. + if recovery.is_none() && RETIRE_NEAR.load(Ordering::Relaxed) { + let first = *near_since.get_or_insert(us); + if us - first < RETIRE_NEAR_SPIN_US { + kernel::time::delay::udelay(Delta::from_micros(RETIRE_NEAR_STEP_US)); + continue; + } + } fsleep(Delta::from_micros(poll_interval_us(us))); } } diff --git a/drivers/gpu/drm/asahi/g18/boot/session.rs b/drivers/gpu/drm/asahi/g18/boot/session.rs index 9805c6e8b75c6..7daa933896924 100644 --- a/drivers/gpu/drm/asahi/g18/boot/session.rs +++ b/drivers/gpu/drm/asahi/g18/boot/session.rs @@ -259,6 +259,11 @@ impl G18Session { && scheduler.submitted==frame.submitted() && scheduler.completed==frame.submitted() && scheduler.linked==0 && list==[0,job.job.queues().job_list()]; let completed=stamps==[frame.stamp();2] && masks[0]&0x600==0x600; + // The remaining conditions follow within 50-150 us: wait_job + // re-polls without sleeping for a bounded time. + if !recovered && (scheduler.completed==frame.submitted() || stamps==[frame.stamp();2]) { + recovery::RETIRE_NEAR.store(true,Ordering::Relaxed); + } recovery::note_phases(&mut first,&kicked,&[outer==[outer_after;3],inner==[[frame.inner_after();3];2], scheduler.submitted==frame.submitted(),scheduler.completed==frame.submitted(),scheduler.linked==0, list==[0,job.job.queues().job_list()],stamps==[frame.stamp();2],masks[0]&0x600==0x600]); From 5dbb3a85205d9f606f155dba7fdfca0484f794bc Mon Sep 17 00:00:00 2001 From: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:51:05 -0800 Subject: [PATCH 3/3] drm/asahi: g18: leave the firmware trace off unless asked for The driver writes trace mask 3 into the GFX status-A word. With it the firmware writes about 59 trace records per render job, 48 of them before the completion event and 2.6-3.0 us apart by their timestamps, and the host copies every one out inside its wait loop. Write 0 instead unless the new aurora.g18_fw_trace parameter is 1, which restores mask 3; g18_graphics_stall_trace=1 still selects 0x203. Retirement uses the event ring and the shared counters, not the trace records. This changes the default on every G18 machine: the trace journal that is dumped on G18_APP_FAIL no longer has firmware trace records unless aurora.g18_fw_trace=1 is set. Measured on a MacBook Pro (M6, J804), on an earlier base (integration/aurora f5fa539) with the J804 firmware support that is not part of this series: with mask 0 no trace records are produced for a render job, and rendering is unaffected. Not tested on J873. Signed-off-by: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- drivers/gpu/drm/asahi/g18/initdata/mod.rs | 4 +++- drivers/gpu/drm/asahi/g18/params.rs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/asahi/g18/initdata/mod.rs b/drivers/gpu/drm/asahi/g18/initdata/mod.rs index 50b8901e252a4..d098bbe185917 100644 --- a/drivers/gpu/drm/asahi/g18/initdata/mod.rs +++ b/drivers/gpu/drm/asahi/g18/initdata/mod.rs @@ -501,7 +501,9 @@ impl G18FirmwareMemory { // bit9 (global 0x1093e5 bit1), the native stall diagnostic category. // audit-g18-graphics-stall.py executes that original predicate. let graphics_stall_trace = crate::g18::params::g18_graphics_stall_trace.value() == 1; - let trace_mask = if graphics_stall_trace { 0x203 } else { 3 }; + // No per-job trace records unless asked for (g18_fw_trace). + let trace_mask = if graphics_stall_trace { 0x203 } + else if crate::g18::params::g18_fw_trace.value() != 0 { 3 } else { 0 }; self.private.write_u32(trace, trace_mask)?; // b000 refresh 0x352c/0x3688 copies region-C+0x9ac into // global 0x109488. Its compute nibble (bits8..11) selects native diff --git a/drivers/gpu/drm/asahi/g18/params.rs b/drivers/gpu/drm/asahi/g18/params.rs index 555c9959ead2f..745938e9c0bbc 100644 --- a/drivers/gpu/drm/asahi/g18/params.rs +++ b/drivers/gpu/drm/asahi/g18/params.rs @@ -99,6 +99,8 @@ g18_param!(g18_sma_minimum: u32 = 1, PARAM_OPS_U32, "Stage9/profile4 or stage10/profile1, variant0: 1 probes fixed native minimum TA/fragment resource words with unchanged owned backing. Default 1"); g18_param!(g18_graphics_stall_trace: u32 = 0, PARAM_OPS_U32, "Stage9/profile4 or stage10/profile1, variant0: 1 enables firmware category9 diagnostics and the native TA mode2 progress sampler. No timeout changes. Default 0"); +g18_param!(g18_fw_trace: u32 = 0, PARAM_OPS_U32, + "G18 firmware trace: 0 (default) writes trace mask 0, so render jobs produce no firmware trace records and G18_APP_FAIL dumps have none; 1 writes mask 3, the earlier default, which restores them. g18_graphics_stall_trace=1 still selects 0x203"); g18_param!(g18_kick_log: u32 = 0, PARAM_OPS_U32, "Stage10 application DRM log volume: 0 (default) omits the per-kick, frame-pass and per-job resource lines (G18_APP_KICK, G18_APP_FRAME_PASS, G18_APP_COMPUTE_KNOBS, G18_COMPUTE_*, G18_SHARED_COMPUTE_*, G18 UMA/compute-context lines, G18_POOL_DESCRIPTOR_CURSOR, role-0 type-1 G18_EVENT) once G18_DRM_READY is logged; 1 prints them as before. Startup, fault, recovery, refusal, wait, stall, admission and sampled summary lines are always printed"); g18_param!(g18_vdm_high_alias: u32 = 0, PARAM_OPS_U32,