Skip to content
Closed
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
5 changes: 4 additions & 1 deletion drivers/gpu/drm/asahi/event.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! GPU event manager
Expand Down Expand Up @@ -251,7 +251,10 @@
});

for wq in owners {
wq.fail_all(error);
if wq.fail_all(error) {
// The pinned commands still point at this queue's ring; keep it until reboot.
core::mem::forget(wq);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: this keeps the queue, and with it QueueInfo, the ring, notifier list and GPU context (fw/workqueue.rs:176-182), for as long as the pinned commands. Please make the body say "until reboot" to match.

— Marcelo's agent

}
}
}
}
47 changes: 11 additions & 36 deletions drivers/gpu/drm/asahi/file.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
#![allow(clippy::unusual_byte_groupings)]

Expand Down Expand Up @@ -70,7 +70,6 @@
vm: mmu::Vm,
user_range: Range<u64>,
kernel_range: Range<u64>,
_dummy_mapping: mmu::KernelMapping,
gpu: Arc<dyn gpu::Gpu>,
}

Expand All @@ -83,41 +82,15 @@
// object must not outlive the client's VM handle.
status.release_mirror();
}
if self.vm.status().is_some() {
// Tracked jobs can outlive their file's VM handle. Their last guard performs
// these unmaps once no accepted work can still dereference the mappings.
if let Err(error) = self
.vm
.unmap_user_ranges(self.user_range.clone(), self.kernel_range.clone())
{
pr_err!("Vm::Drop: deferred user unmap failed: {:?}\n", error);
}
self.vm.bo_deferred_cleanup();
self.gpu.vm_closed(&self.vm);
return;
}
// When the user Vm is dropped, verify that no user mappings exists.
let left_range = self.user_range.start..self.kernel_range.start;
let right_range = self.kernel_range.end..self.user_range.end;

if !left_range.is_empty()
&& self
.vm
.unmap_range(None, left_range.start, left_range.range())
.is_err()
{
pr_err!("Vm::Drop: vm.unmap_range() failed\n");
}
if !right_range.is_empty()
&& self
.vm
.unmap_range(None, right_range.start, right_range.range())
.is_err()
// Accepted jobs retain mappings until firmware completion or GPU stop.
if let Err(error) = self
.vm
.unmap_user_ranges(self.user_range.clone(), self.kernel_range.clone())
{
pr_err!("Vm::Drop: vm.unmap_range() failed\n");
pr_err!("Vm::Drop: deferred user unmap failed: {:?}\n", error);
}

self.vm.bo_deferred_cleanup();
self.gpu.vm_closed(&self.vm);
}
}

Expand Down Expand Up @@ -555,13 +528,15 @@

let gpu = &reg_data.gpu;
let file_id = file.inner().id;
let vm = gpu.new_vm_for_file(kernel_range.clone(), file_id)?;
let vm = gpu
.new_vm_for_file(kernel_range.clone(), file_id)?
.with_job_lifetime()?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On G13/G14 this switches on deferred GEM cleanup. Closed objects are then held until the VM fully idles (mmu/lifetime.rs:615-621), with no bound under sustained load.

— Marcelo's agent

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is real, and I did not change it in this push.

Objects closed while the VM has accepted jobs wait in VmLifetime until the VM next goes idle (mmu/lifetime.rs, drop_mappings() and VmJobGuard::drop). G15 and later already follow that rule. On G13/G14 this patch applies it for the first time, because with_job_lifetime() attaches tracking there. A VM that is never idle keeps every closed object, so the retained memory is bounded only by the time between idles.

Unmapping an object as soon as the jobs accepted before its close retire would bound it. That needs a per-job sequence and a partial drain, and it breaks the drain loop's rule that only one drainer runs and only at zero active jobs. I will not change that state machine without a stress run on G13 hardware. Would you rather have that here, or as a follow-up PR with the stress result?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On your question: the scope is wider than closed objects on one VM. unbind_gem_object() queues the BO on every VM of the file (file.rs:1095-1107), mapped or not (mmu/lifetime.rs:601-623). Whether the bound lands here or in a follow-up is Marcelo's call, but either way please state this scope in the body.

— Marcelo's agent

// 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 @@ -631,6 +606,7 @@
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 @@ -645,7 +621,6 @@
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
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! Keeps user mappings alive until accepted work has relinquished firmware ownership.
Expand Down Expand Up @@ -74,6 +74,8 @@
changed: CondVar,
#[pin]
driver: Mutex<Option<DriverMappings>>,
#[pin]
retained: Mutex<KVec<KernelMapping>>,
}

impl VmLifetime {
Expand All @@ -97,6 +99,7 @@
unmaps:KVec::new(), objects:KVec::new() }, "VM job lifetime"),
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 @@ -111,7 +114,9 @@
}

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 @@ -169,6 +174,11 @@
}
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)?)
}

pub(crate) fn render_pool_tvb_blocks(&self, slot: u8, id: u64) -> Option<usize> {
if id == 0 {
Expand Down Expand Up @@ -351,6 +361,15 @@
.is_some_and(|lifetime| lifetime.state.lock().closed)
}

/// 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
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
#![allow(clippy::unusual_byte_groupings)]

Expand Down Expand Up @@ -62,7 +62,7 @@
// 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
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! Submission queue management
Expand Down Expand Up @@ -89,9 +89,16 @@

#[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 @@ -101,6 +108,8 @@
#[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 @@ -110,8 +119,13 @@
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 @@ -201,7 +215,9 @@
#[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 @@ -715,7 +731,8 @@
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 @@ -790,20 +807,21 @@
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
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
#![allow(clippy::unusual_byte_groupings)]

Expand Down Expand Up @@ -294,7 +294,7 @@

let scene = Arc::new(buffer.new_scene(kalloc, &tile_info)?, 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
Loading