Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion drivers/gpu/drm/asahi/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ impl EventManager {
});

for wq in owners {
wq.fail_all(error);
if wq.fail_all(error) {
// Keep the queue and its ring until reboot; pinned commands retain their
// VM guards and bindings because firmware DMA has not been ruled out.
core::mem::forget(wq);
}
}
}
}
Expand Down
19 changes: 8 additions & 11 deletions drivers/gpu/drm/asahi/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ struct Vm {
vm: mmu::Vm,
user_range: Range<u64>,
kernel_range: Range<u64>,
_dummy_mapping: mmu::KernelMapping,
gpu: Arc<dyn gpu::Gpu>,
}

Expand All @@ -177,20 +176,17 @@ impl Drop for Vm {
// object must not outlive the client's VM handle.
status.release_mirror();
}
// Tracked jobs can outlive their file's VM handle: their last guard runs the
// teardown once no accepted work can still dereference the mappings. On
// untracked VMs this unmaps every user GPUVA at once without allocating or
// Accepted jobs retain mappings until firmware completion or GPU stop.
// On idle VMs this unmaps every user GPUVA at once without allocating or
// charging split nodes, so teardown succeeds even at the client's memory limit.
if let Err(error) = self
.vm
.unmap_user_ranges(self.user_range.clone(), self.kernel_range.clone())
{
pr_err!("Vm::Drop: user unmap failed: {:?}\n", error);
pr_err!("Vm::Drop: deferred user unmap failed: {:?}\n", error);
}
self.vm.bo_deferred_cleanup();
if self.vm.status().is_some() {
self.gpu.vm_closed(&self.vm);
}
self.gpu.vm_closed(&self.vm);
}
}

Expand Down Expand Up @@ -798,13 +794,14 @@ impl File {
let file_id = file.inner().id;
let vm = gpu
.new_vm_for_file(kernel_range.clone(), file_id, &file.inner().mem)?
.with_memory_client(file.inner().mem.clone());
.with_memory_client(file.inner().mem.clone())
.with_job_lifetime()?;
// G16 registers a retained backend owner before returning this VM. Any later
// allocation can fail before file::Vm exists to run its normal close path.
let failed_vm = ScopeGuard::new_with_data(
(vm.clone(), user_range.clone(), kernel_range.clone()),
|(vm, user_range, kernel_range)| {
if gpu.tracked_submission() && vm.status().is_some() && !vm.is_closed() {
if !vm.is_closed() {
vm.close_idle_reclaim();
if let Err(error) = vm.unmap_user_ranges(user_range, kernel_range) {
pr_err!("VM create: closing unpublished VM failed: {:?}\n", error);
Expand Down Expand Up @@ -870,6 +867,7 @@ impl File {
mmu::PROT_GPU_SHARED_RW,
true,
)?;
vm.retain_mapping(dummy_mapping)?;

mod_dev_dbg!(device, "[File {} VM {}]: VM created\n", file_id, id);
resv.fill(KBox::new(
Expand All @@ -884,7 +882,6 @@ impl File {
vm,
user_range,
kernel_range,
_dummy_mapping: dummy_mapping,
gpu: gpu.clone(),
},
GFP_KERNEL,
Expand Down
21 changes: 20 additions & 1 deletion drivers/gpu/drm/asahi/mmu/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub(super) struct VmLifetime {
changed: CondVar,
#[pin]
driver: Mutex<Option<DriverMappings>>,
#[pin]
retained: Mutex<KVec<KernelMapping>>,
}

impl VmLifetime {
Expand All @@ -106,6 +108,7 @@ impl VmLifetime {
closed: AtomicBool::new(false),
changed <- new_condvar!("VM mapping admission"),
driver <- new_mutex!(None, "VM driver mappings"),
retained <- new_mutex!(KVec::new(), "VM retained mappings"),
}),
GFP_KERNEL,
)
Expand All @@ -120,7 +123,9 @@ pub(crate) struct VmJobGuard {
}

impl VmJobGuard {
/// Blocks mapping commits before the error fence can become visible to userspace.
/// Blocks mapping commits until existing commits drain, before the error fence becomes visible.
/// May sleep; callers must run in sleepable context. This runs in the crash callback with no
/// timeout, so the mapping-commit window must never wait on the firmware.
pub(crate) fn quarantine(&self) {
if let Some(lifetime) = self.vm.lifetime.as_ref() {
let mut state = lifetime.state.lock();
Expand Down Expand Up @@ -181,6 +186,11 @@ impl Vm {
}
Ok(())
}
/// Keeps fixed kernel aliases alive while accepted work can still reference them.
pub(crate) fn retain_mapping(&self, mapping: KernelMapping) -> Result {
let lifetime = self.lifetime.as_ref().ok_or(EINVAL)?;
Ok(lifetime.retained.lock().push(mapping, GFP_KERNEL)?)
}

/// Move a drained graph's pool mappings for `slot` out of this VM
/// (so another graph of the same VM can install there); `put` returns them
Expand Down Expand Up @@ -391,6 +401,15 @@ impl Vm {
.is_some_and(|lifetime| lifetime.closed.load(Ordering::Acquire))
}

/// Track accepted work on the legacy submission path. Called for every backend; a no-op where
/// tracking already exists (G15 and later attach it when they create the VM).
pub(crate) fn with_job_lifetime(mut self) -> Result<Self> {
if self.lifetime.is_none() {
self.lifetime = Some(VmLifetime::new()?);
}
Ok(self)
}

/// Tests mappings and reservations which userspace must never replace.
pub(crate) fn driver_range_overlaps(&self, range: Range<u64>) -> bool {
let Some(lifetime) = self.lifetime.as_ref() else {
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/asahi/queue/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl super::QueueInner::ver {
// but it's unclear *which* slot...
let slot_client_seq: u8 = (self.id & 0xff) as u8;

let vm_bind = job.vm_bind.clone();
let vm_bind = job.vm_bind.as_ref().clone();

mod_dev_dbg!(
self.dev,
Expand Down
36 changes: 27 additions & 9 deletions drivers/gpu/drm/asahi/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,16 @@ struct SubQueue {

#[versions(AGX)]
impl SubQueue::ver {
fn new_job(&mut self, fence: dma_fence::Fence) -> SubQueueJob::ver {
fn new_job(
&mut self,
fence: dma_fence::Fence,
vm_job: Arc<mmu::VmJobGuard>,
vm_bind: Arc<mmu::VmBind>,
) -> SubQueueJob::ver {
SubQueueJob::ver {
wq: self.wq.clone(),
vm_job,
vm_bind,
fence: Some(fence),
job: None,
}
Expand All @@ -227,6 +234,8 @@ impl SubQueue::ver {
#[versions(AGX)]
struct SubQueueJob {
wq: Arc<workqueue::WorkQueue::ver>,
vm_job: Arc<mmu::VmJobGuard>,
vm_bind: Arc<mmu::VmBind>,
job: Option<workqueue::Job::ver>,
fence: Option<dma_fence::Fence>,
}
Expand All @@ -236,8 +245,13 @@ impl SubQueueJob::ver {
fn get(&mut self) -> Result<&mut workqueue::Job::ver> {
if self.job.is_none() {
mod_pr_debug!("SubQueueJob: Creating {:?} job\n", self.wq.pipe_type());
self.job
.replace(self.wq.new_job(self.fence.take().unwrap())?);
self.job.replace(
self.wq.new_job(
self.fence.take().unwrap(),
self.vm_job.clone(),
self.vm_bind.clone(),
)?,
);
}
Ok(self.job.as_mut().expect("expected a Job"))
}
Expand Down Expand Up @@ -353,7 +367,9 @@ impl dma_fence::FenceOps for JobFence::ver {
#[versions(AGX)]
pub(crate) struct QueueJob {
dev: AsahiDevRef,
vm_bind: mmu::VmBind,
vm_bind: Arc<mmu::VmBind>,
// Retain the VM when no sub-queue job exists; otherwise child jobs hold clones.
_vm_job: Arc<mmu::VmJobGuard>,
op_guard: Option<gpu::OpGuard>,
sj_vtx: Option<SubQueueJob::ver>,
sj_frag: Option<SubQueueJob::ver>,
Expand Down Expand Up @@ -1263,7 +1279,8 @@ impl Queue for Queue::ver {
GFP_KERNEL,
)?;

let vm_bind = gpu.bind_vm(&self.vm)?;
let vm_job = Arc::new(self.vm.retain_first_job()?, GFP_KERNEL)?;
let vm_bind = Arc::new(gpu.bind_vm(&self.vm)?, GFP_KERNEL)?;
let vm_slot = vm_bind.slot();

mod_dev_dbg!(self.dev, "[Submission {}] Creating job\n", id);
Expand Down Expand Up @@ -1292,20 +1309,21 @@ impl Queue for Queue::ver {
1,
QueueJob::ver {
dev: self.dev.clone(),
vm_bind,
vm_bind: vm_bind.clone(),
_vm_job: vm_job.clone(),
op_guard,
sj_vtx: self
.q_vtx
.as_mut()
.map(|a| a.new_job(Fence::from_fence(&fence))),
.map(|a| a.new_job(Fence::from_fence(&fence), vm_job.clone(), vm_bind.clone())),
sj_frag: self
.q_frag
.as_mut()
.map(|a| a.new_job(Fence::from_fence(&fence))),
.map(|a| a.new_job(Fence::from_fence(&fence), vm_job.clone(), vm_bind.clone())),
sj_comp: self
.q_comp
.as_mut()
.map(|a| a.new_job(Fence::from_fence(&fence))),
.map(|a| a.new_job(Fence::from_fence(&fence), vm_job.clone(), vm_bind.clone())),
fence,
notifier: self.inner.notifier.clone(),

Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/asahi/queue/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl super::QueueInner::ver {
}
let scene = Arc::new(scene, GFP_KERNEL)?;

let vm_bind = job.vm_bind.clone();
let vm_bind = job.vm_bind.as_ref().clone();

mod_dev_dbg!(
self.dev,
Expand Down
38 changes: 33 additions & 5 deletions drivers/gpu/drm/asahi/workqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::{
event,
fw,
gpu,
mmu,
regs, //
};
use core::any::Any;
Expand Down Expand Up @@ -359,13 +360,16 @@ where
vm_slot: u32,
callback: Option<C>,
fence: dma_fence::Fence,
_vm_job: Arc<mmu::VmJobGuard>,
_vm_bind: Arc<mmu::VmBind>,
}

pub(crate) trait GenSubmittedWork: Send + Sync {
fn gpu_va(&self) -> NonZeroU64;
fn value(&self) -> event::EventValue;
fn wptr(&self) -> u32;
fn set_wptr(&mut self, wptr: u32);
fn quarantine_vm(&self);
fn mark_error(&mut self, error: WorkError);
fn complete(&mut self);
fn get_fence(&self) -> dma_fence::Fence;
Expand Down Expand Up @@ -416,6 +420,10 @@ impl<O: OpaqueCommandObject, C: FnOnce(Option<WorkError>) + Send + Sync> GenSubm
self.wptr = wptr;
}

fn quarantine_vm(&self) {
self._vm_job.quarantine();
}

fn complete(&mut self) {
if let Some(cb) = self.callback.take() {
cb(self.error);
Expand Down Expand Up @@ -517,6 +525,8 @@ pub(crate) struct Job {
submitted: bool,
event_count: usize,
fence: dma_fence::Fence,
vm_job: Arc<mmu::VmJobGuard>,
vm_bind: Arc<mmu::VmBind>,
}

#[versions(AGX)]
Expand Down Expand Up @@ -565,7 +575,8 @@ impl Job::ver {

let fence = self.fence.clone();
let value = self.event_info.value.next();

let vm_job = self.vm_job.clone();
let vm_bind = self.vm_bind.clone();
self.pending.push(
KBox::try_pin_init(
try_pin_init!(SubmittedWorkContainer {
Expand All @@ -578,6 +589,8 @@ impl Job::ver {
wptr: 0,
vm_slot,
fence,
_vm_job: vm_job,
_vm_bind: vm_bind,
}, GFP_KERNEL)?
}),
GFP_KERNEL,
Expand Down Expand Up @@ -884,6 +897,7 @@ impl<'a> Drop for JobSubmission::ver<'a> {
mod_pr_debug!("WorkQueue({:?}): Dropping JobSubmission\n", inner.pipe_type);

let new_len = inner.pending.len() - self.command_count;
// Job retains its guard until this submission drops the queue lock.
inner.pending.truncate(new_len);

let pipe_type = inner.pipe_type;
Expand Down Expand Up @@ -1089,7 +1103,12 @@ impl WorkQueue::ver {
self.inner.lock().order.has_published(event_seq)
}

pub(crate) fn new_job(self: &Arc<Self>, fence: dma_fence::Fence) -> Result<Job::ver> {
pub(crate) fn new_job(
self: &Arc<Self>,
fence: dma_fence::Fence,
vm_job: Arc<mmu::VmJobGuard>,
vm_bind: Arc<mmu::VmBind>,
) -> Result<Job::ver> {
let mut inner = self.inner.lock();

if inner.event.is_none() {
Expand Down Expand Up @@ -1134,6 +1153,8 @@ impl WorkQueue::ver {
committed: false,
submitted: false,
fence,
vm_job,
vm_bind,
})
}

Expand Down Expand Up @@ -1171,7 +1192,9 @@ pub(crate) trait WorkQueue {

fn signal(&self) -> bool;
fn mark_error(&self, value: event::EventValue, error: WorkError);
fn fail_all(&self, error: WorkError);
/// Fails all pending commands. Returns true when commands were left pinned: the caller
/// must then keep its reference to this queue, whose ring they still point at.
fn fail_all(&self, error: WorkError) -> bool;
}

#[versions(AGX)]
Expand Down Expand Up @@ -1328,7 +1351,7 @@ impl WorkQueue for WorkQueue::ver {
}

/// Mark all of this queue's work as having failed, and complete it.
fn fail_all(&self, error: WorkError) {
fn fail_all(&self, error: WorkError) -> bool {
// If anything is marked completed, we can consider it successful
// at this point, even if we didn't get the signal event yet.
self.signal();
Expand All @@ -1341,7 +1364,7 @@ impl WorkQueue for WorkQueue::ver {
if inner.order.pending_jobs > 0 || !inner.pending.is_empty() {
pr_crit!("WorkQueue: fail_all() called with no event and pending jobs.\n");
}
return;
return false;
}

mod_pr_debug!(
Expand All @@ -1366,10 +1389,15 @@ impl WorkQueue for WorkQueue::ver {
core::mem::drop(inner);
core::mem::drop(held);

let pinned = !cmds.is_empty();
for mut cmd in cmds {
cmd.as_mut().inner_mut().quarantine_vm();
cmd.as_mut().inner_mut().mark_error(error);
cmd.as_mut().inner_mut().complete();
// No confirmed GPU stop exists after a firmware crash; retain command resources until reboot.
core::mem::forget(cmd);
}
pinned
}
}

Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ TARGETS += drivers/gpu/apple-afkv2
TARGETS += drivers/gpu/apple-dcp
TARGETS += drivers/gpu/asahi-m4
TARGETS += drivers/gpu/asahi-residency
TARGETS += drivers/gpu/asahi-sigkill
TARGETS += drivers/gpu/asahi-userptr
TARGETS += drivers/ntsync
TARGETS += drivers/s390x/uvdevice
Expand Down
4 changes: 4 additions & 0 deletions tools/testing/selftests/drivers/gpu/asahi-sigkill/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only OR MIT
TEST_PROGS := run.sh
TEST_FILES := comp.comp vkrepro.c
include ../../../lib.mk
Loading
Loading