From 6aee22f46165facd790b1a4f08dcd21b2acb8863 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Thu, 3 Dec 2026 00:21:25 -0500 Subject: [PATCH] drm/asahi: g17: share per-VM UAT execution context on M5 Queues belonging to the same VM on M5 were each allocated a distinct UAT execution context ID because exec_share was gated off on M5. Because GPU caches are tagged by address and context ID, graphics-stage storage image writes into Gen10 losslessly compressed surfaces on one queue could leave dirty cache lines invisible to another queue of the same client even after fence synchronization, causing cross-queue image corruption. Enable per-VM execution context sharing on M5 as on G17P, and close three edge cases in exec_share: - Register newly created contexts via exec_share::offer() under the runtime state lock so concurrent queue creation on one VM cannot allocate multiple execution contexts. - Release the UAT root in exec_share::Use::drop() after unlocking SHARES when removing the last user, so a published queue whose sibling later drops without publishing cannot strand the shared root in an idle compute binding. - Match VM instance identity via Vm::same_as() in exec_share::join() so a rebound GPU instance starting its numeric VM counter at 2 cannot join a retained context from a previous instance. Signed-off-by: Erik Bourget --- drivers/gpu/drm/asahi/g17/context.rs | 2 +- drivers/gpu/drm/asahi/g17/exec_share.rs | 11 ++++++----- drivers/gpu/drm/asahi/g17/runtime.rs | 24 ++++++++++++++---------- drivers/gpu/drm/asahi/mmu.rs | 5 +++++ 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/context.rs b/drivers/gpu/drm/asahi/g17/context.rs index e397a9f8c360a..a44564e41db3f 100644 --- a/drivers/gpu/drm/asahi/g17/context.rs +++ b/drivers/gpu/drm/asahi/g17/context.rs @@ -264,7 +264,7 @@ pub(crate) struct Context { /// profile in every record, ring, doorbell and registration. policy: Policy, /// This logical queue's use of the execution context its VM's queues share - /// (`super::exec_share`); `None` on M5. + /// (`super::exec_share`). share: Option, } impl Context { diff --git a/drivers/gpu/drm/asahi/g17/exec_share.rs b/drivers/gpu/drm/asahi/g17/exec_share.rs index 8a1a8bea6323a..39d98c828f92c 100644 --- a/drivers/gpu/drm/asahi/g17/exec_share.rs +++ b/drivers/gpu/drm/asahi/g17/exec_share.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT -//! Logical queues of one VM share one UAT execution context (G17P). +//! Logical queues of one VM share one UAT execution context. //! //! GPU caches are tagged by address and context: queues of one VM in separate contexts see //! each other's writes only after a memory-scope write-back at the end of each command, so @@ -10,8 +10,7 @@ //! The sharers keep their own scheduler pages, QoS identities and work lists; only the //! TTBAT slot (the context id) is common. Work keys are the context id plus the low six bits //! of a work-state word, so the sharers draw their work-state indices from one claim map. -//! The root is released by the last sharer's explicit release; a sharer dropped without one -//! leaves the root to the last reference, as an unshared context does. +//! The root is released when the last sharer leaves, whether by explicit release or drop. use crate::mmu::{ExecutionContext, Vm}; use core::sync::atomic::{AtomicBool, AtomicU64, Ordering}; @@ -72,7 +71,9 @@ impl Use { impl Drop for Use { fn drop(&mut self) { if !self.done.swap(true, Ordering::AcqRel) { - drop(leave(self.vm, &self.execution)); + if let Some(last) = leave(self.vm, &self.execution) { + last.release(); + } } } } @@ -96,7 +97,7 @@ pub(crate) fn join(vm: &Vm) -> Option<(Arc, Use)> { let mut shares = SHARES.lock(); let entry = shares .iter_mut() - .find(|entry| entry.vm == vm.id() && entry.execution.is_current())?; + .find(|entry| entry.execution.vm().same_as(vm) && entry.execution.is_current())?; entry.users = entry.users.checked_add(1)?; Some(( entry.execution.clone(), diff --git a/drivers/gpu/drm/asahi/g17/runtime.rs b/drivers/gpu/drm/asahi/g17/runtime.rs index 7a1510d1ebeb4..113f84455919b 100644 --- a/drivers/gpu/drm/asahi/g17/runtime.rs +++ b/drivers/gpu/drm/asahi/g17/runtime.rs @@ -1441,8 +1441,9 @@ impl Backend { }; // The new context may reuse a pooled scheduler page: clear released QoS rows first. firmware.queues.qos_ids.forget_released(|ids| firmware.port.qos_forget(ids)); - // G17P queues of one VM join its live execution context (super::exec_share). - let joined = if firmware.uat.is_m5() { None } else { super::exec_share::join(vm) }; + // Queues of one VM share its live execution context (super::exec_share) + // so caches tagged by context ID stay coherent across queues of a client. + let joined = super::exec_share::join(vm); let attempt = if let Some((execution, share)) = joined { Ok(( execution, @@ -1462,7 +1463,16 @@ impl Backend { reclaimable, installed, )?; - firmware.uat.new_execution_context(vm, super::channel::control_witnessed()).map(|execution| (execution, slot, None)) + match firmware.uat.new_execution_context(vm, super::channel::control_witnessed()) { + Ok(execution) => match super::exec_share::offer(vm, &execution) { + Ok(share) => Ok((execution, slot, Some(share))), + Err(error) => { + execution.release(); + Err(error) + } + }, + Err(error) => Err(error), + } } else { Err(ENOSPC) }; @@ -1483,8 +1493,7 @@ impl Backend { break result; } Err(error) if error == ENOSPC => { - // Every open queue holds one context; this one holds no - // slot or context any more. + // This creator holds no slot or execution context any more. let open = firmware.queues.admission.queues(); let closing = firmware.uat.execution_contexts_used().saturating_sub(open); if closing == 0 || now_ns().saturating_sub(started) >= RETAINED_CONTEXT_WAIT_NS { @@ -1514,11 +1523,6 @@ impl Backend { }; let owner_pid = u32::try_from(kernel::current!().group_leader().pid()).map_err(|_| EOVERFLOW)?; - let share = match share { - Some(share) => Some(share), - None if !uat.is_m5() => Some(super::exec_share::offer(vm, &execution)?), - None => None, - }; let context = Arc::new( Context::new( &alloc, diff --git a/drivers/gpu/drm/asahi/mmu.rs b/drivers/gpu/drm/asahi/mmu.rs index dab93b16914ab..5031689d4dbe0 100644 --- a/drivers/gpu/drm/asahi/mmu.rs +++ b/drivers/gpu/drm/asahi/mmu.rs @@ -2748,6 +2748,11 @@ impl Vm { self.id } + /// True when `self` and `other` refer to the same VM instance. + pub(crate) fn same_as(&self, other: &Vm) -> bool { + Arc::ptr_eq(&self.binding, &other.binding) + } + /// Created by an M5 GPU's UAT ([`Uat::is_m5`]). pub(crate) fn is_m5(&self) -> bool { self.m5