Skip to content
Open
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
2 changes: 1 addition & 1 deletion drivers/gpu/drm/asahi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ aurora-$(CONFIG_DRM_ASAHI_UAT_ADOPTION) += g15/uat_glue.o

aurora-$(CONFIG_DRM_ASAHI_G16) += g16/glue.o

aurora-y += retain.o sleep_pd.o cpu_capacity.o g17/mesh_tvb_floor.o rtkit_dump.o
aurora-y += memcg_scope.o retain.o sleep_pd.o cpu_capacity.o g17/mesh_tvb_floor.o rtkit_dump.o
aurora-$(CONFIG_DRM_ASAHI_G18) += g18/sync_input.o
aurora-$(CONFIG_DRM_ASAHI_G18) += g18/demand.o
95 changes: 79 additions & 16 deletions drivers/gpu/drm/asahi/g17/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ impl KernelObject {
/// Write-combined [`Self::backing`] that a client's commands size, charged to that client
/// (`client`) as a user buffer of the same size would be: admitted against its and the
/// device's memory limits before it is allocated, failing with ENOMEM over either, and then
/// counted as its held memory instead of kernel memory, until the object is dropped.
/// counted as its held memory instead of kernel memory, and its pages charged to the client's
/// memory cgroup, until the object is dropped. Callers run in workers too (tiler heap growth at
/// publication), so the pages go to the client's cgroup whatever task populates them.
/// Without a client (a kernel VM's object) it is kernel memory, as [`Self::backing`].
pub(crate) fn client_backing(
dev: &AsahiDrmDevice,
Expand All @@ -126,8 +128,39 @@ impl KernelObject {
};
let bytes = gem::checked_object_size(size)? as u64;
let commit = crate::memacct::LEDGER.admit(client, bytes)?;
let mut object =
Self::with_object(gem::new_kernel_object_wc_user_charged(dev, size)?, size)?;
Self::charged(dev, size, CpuMap::WriteCombined, client, commit)
}

/// [`Self::backing`] that `client`'s queues need, charged to it whatever the limits: counted
/// as its held memory instead of kernel memory, and its pages charged to the client's memory
/// cgroup, until the object is dropped, also after the client is gone. Without a client it is
/// kernel memory, as [`Self::backing`].
pub(crate) fn queue_backing(
dev: &AsahiDrmDevice,
size: usize,
cpu: CpuMap,
client: Option<&kernel::sync::Arc<crate::memacct::Client>>,
) -> Result<Self> {
let Some(client) = client else {
return Self::backing(dev, size, cpu);
};
let bytes = gem::checked_object_size(size)? as u64;
Self::charged(dev, size, cpu, client, crate::memacct::LEDGER.commit_kernel(client, bytes))
}

fn charged(
dev: &AsahiDrmDevice,
size: usize,
cpu: CpuMap,
client: &kernel::sync::Arc<crate::memacct::Client>,
commit: crate::memacct::Commit,
) -> Result<Self> {
let obj = match cpu {
CpuMap::WriteBack => gem::new_kernel_object_user_charged(dev, size)?,
CpuMap::WriteCombined => gem::new_kernel_object_wc_user_charged(dev, size)?,
};
// `with_object()` populates the pages.
let mut object = client.charging(|| Self::with_object(obj, size))?;
let charge = crate::memacct::Charge::user(commit, None);
charge.set_held(true);
object.charge = Some(charge);
Expand Down Expand Up @@ -402,12 +435,32 @@ impl KernelObject {

/// Allocation domains shared by the G17 resource builders. Dynamic objects
/// retain a guard page; aliases keep the existing backing and its exact VA.
/// `client`, if there is one, is the client whose queues the objects are for; they are charged
/// to it ([`KernelObject::queue_backing`]).
pub(crate) struct Allocator<'a> {
pub(crate) dev: &'a AsahiDrmDevice,
pub(crate) uat: &'a mmu::Uat,
pub(crate) client: Option<&'a kernel::sync::Arc<crate::memacct::Client>>,
}

impl Allocator<'_> {
/// Allocates fresh backing, charged to the allocator's client.
pub(crate) fn backing(&self, size: usize, cpu: CpuMap) -> Result<KernelObject> {
KernelObject::queue_backing(self.dev, size, cpu, self.client)
}

/// [`KernelObject::new`] with backing charged to the allocator's client.
pub(crate) fn object(
&self,
vm: &mmu::Vm,
placement: Placement,
size: usize,
prot: mmu::Prot,
cpu: CpuMap,
) -> Result<KernelObject> {
self.backing(size, cpu)?.placed(vm, placement, prot)
}

/// Allocates in the dynamic part of the firmware's upper address space.
pub(crate) fn kernel(
&self,
Expand All @@ -417,14 +470,7 @@ impl Allocator<'_> {
cpu: CpuMap,
) -> Result<KernelObject> {
let range = self.uat.kernel_dynamic_range();
KernelObject::new(
self.dev,
self.uat.kernel_vm(),
Placement::In(range, align),
size,
prot,
cpu,
)
self.object(self.uat.kernel_vm(), Placement::In(range, align), size, prot, cpu)
}

/// Allocates a canonical low mapping, which can be aliased at the same
Expand All @@ -436,8 +482,7 @@ impl Allocator<'_> {
prot: mmu::Prot,
cpu: CpuMap,
) -> Result<KernelObject> {
KernelObject::new(
self.dev,
self.object(
self.uat.kernel_lower_vm(),
Placement::In(self.uat.kernel_lower_dynamic_range(), align),
size,
Expand Down Expand Up @@ -476,7 +521,7 @@ impl Allocator<'_> {
if size == 0 || size % mmu::UAT_PGSZ != 0 {
return Err(EINVAL);
}
let mut object = KernelObject::backing(self.dev, size, cpu)?;
let mut object = self.backing(size, cpu)?;
let mut range = self.uat.kernel_lower_dynamic_range();
let mut searching = false;
loop {
Expand Down Expand Up @@ -847,6 +892,8 @@ impl Pool {
for (index, entry) in inner.returned[begin..begin + POOL_SLOTS].iter().enumerate() {
let Some(entry) = entry.as_ref().filter(|entry| {
!entry.poisoned
&& alloc.client.is_none()
&& entry.object.charge.is_none()
&& entry.object.size() == size
&& entry.prefix == prefix
&& entry.cpu == cpu
Expand Down Expand Up @@ -917,7 +964,10 @@ impl Pool {
result.defer = Some(self.clone());
return Ok(result);
}
if prefix == 0 {
// Converted mappings can retain firmware cache state until processor stop.
// Keep client pages uncached so a release witness suffices to unmap them
// and drop both their ledger charge and their physical memcg charge.
if prefix == 0 || alloc.client.is_some() {
return Ok(result);
}
let (proof, mutated) = match result.mapping.as_ref() {
Expand Down Expand Up @@ -994,6 +1044,8 @@ impl Pool {
order,
fence,
});
drop(inner);
self.reap_deferred(super::channel::control_witnessed());
return;
}
}
Expand Down Expand Up @@ -1064,7 +1116,18 @@ impl Pool {
}

/// Unmaps the deferred objects whose fence the firmware has met.
fn reap_deferred(&self, witnessed: u64) {
pub(super) fn reap_deferred(&self, witnessed: u64) {
loop {
let mut inner = self.inner.lock();
let Some(index) = inner.returned.iter().position(|slot| {
slot.as_ref().is_some_and(|entry| entry.object.charge.is_some() && entry.ready(witnessed))
}) else { break };
let entry = inner.returned[index].take().unwrap();
inner.reserved[index / POOL_SLOTS] -= 1;
inner.bytes -= entry.object.size();
drop(inner);
drop(entry);
}
loop {
let entry = {
let mut inner = self.inner.lock();
Expand Down
3 changes: 1 addition & 2 deletions drivers/gpu/drm/asahi/g17/queue/render/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ impl Generation {
let (placement, layout) = if alloc.uat.is_m5() {
(PairObject::SlotState.placement()?, Layout::m5(board.ok_or(EINVAL)?))
} else { (Placement::In(vm.client_private_range(), cfg::TVB_PAGE), Layout::NEO) };
KernelObject::new(alloc.dev, vm, placement, layout.bytes,
mmu::PROT_GPU_FW_SHARED_RW, CpuMap::WriteCombined)
alloc.object(vm, placement, layout.bytes, mmu::PROT_GPU_FW_SHARED_RW, CpuMap::WriteCombined)
}

#[inline(never)]
Expand Down
10 changes: 4 additions & 6 deletions drivers/gpu/drm/asahi/g17/queue/render/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl Compact {
/// M5: a shared-RW object of `size` bytes mapped in the client's compact window as `object`.
fn m5(alloc: &Allocator<'_>, vm: &mmu::Vm, object: PairObject, size: usize) -> Result<Self> {
let size = size.checked_next_multiple_of(mmu::UAT_PGSZ).ok_or(EOVERFLOW)?;
let backing = KernelObject::backing(alloc.dev, size, CpuMap::WriteCombined)?;
let backing = alloc.backing(size, CpuMap::WriteCombined)?;
let mapping = object.map(&backing, vm, 0..size, mmu::PROT_GPU_SHARED_RW)?;
Ok(Self { address: mapping.iova(), mapping: Some(mapping), _backing: backing })
}
Expand All @@ -379,7 +379,7 @@ impl Compact {
if alloc.uat.is_m5() {
return Err(EINVAL);
}
let backing = KernelObject::backing(alloc.dev, size, CpuMap::WriteCombined)?;
let backing = alloc.backing(size, CpuMap::WriteCombined)?;
let mapping = buffer::compact_alias(
&backing,
vm,
Expand Down Expand Up @@ -460,8 +460,7 @@ pub(super) struct NeoGeneration {
}
impl NeoGeneration {
fn allocate_state(alloc: &Allocator<'_>, vm: &mmu::Vm) -> Result<KernelObject> {
KernelObject::new(
alloc.dev,
alloc.object(
vm,
crate::g17::object::Placement::In(vm.client_private_range(), cfg::TVB_PAGE),
storage::STATE_SIZE,
Expand Down Expand Up @@ -672,8 +671,7 @@ impl Memory {
)
})?;
}
let registers = if m5 { None } else { Some(KernelObject::new(
alloc.dev,
let registers = if m5 { None } else { Some(alloc.object(
vm,
crate::g17::object::Placement::In(vm.client_private_range(), mmu::UAT_PGSZ as u64),
mmu::UAT_PGSZ,
Expand Down
17 changes: 15 additions & 2 deletions drivers/gpu/drm/asahi/g17/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1529,9 +1529,12 @@ impl Backend {
}
};
let dev = shared.drm()?;
// QUEUE_CREATE builds the context here, in the client's ioctl: a failure comes back from
// it, so the context's objects are charged to the client.
let alloc = super::object::Allocator {
dev: &dev,
uat: &uat,
client: vm.memory_client(),
};
let owner_pid =
u32::try_from(kernel::current!().group_leader().pid()).map_err(|_| EOVERFLOW)?;
Expand Down Expand Up @@ -1785,7 +1788,7 @@ impl Backend {
let lease = self.shared.preparations.enter_until(admission)?;
let dev = self.shared.drm()?;
let uat = self.shared.state.lock().as_ref().ok_or(ENODEV)?.uat.clone();
let alloc = super::object::Allocator { dev: &dev, uat: &uat };
let alloc = super::object::Allocator { dev: &dev, uat: &uat, client: None };
let mut pages = Some(super::freelist::Pages::for_compute(&alloc, required,
self.context.vm().memory_client())?);
let mut alias = Some(pages.as_mut().ok_or(EIO)?.map_client(self.context.vm())?);
Expand Down Expand Up @@ -2007,6 +2010,7 @@ impl Backend {
let alloc = super::object::Allocator {
dev: &dev,
uat: &uat,
client: None,
};
let reservation = match reservation {
Ok(reservation) => reservation,
Expand Down Expand Up @@ -2186,6 +2190,7 @@ impl Backend {
let alloc = super::object::Allocator {
dev: &dev,
uat: &uat,
client: None,
};
let reservation = match source {
Ok(reservation) => reservation,
Expand Down Expand Up @@ -2280,6 +2285,7 @@ impl Backend {
/// The physical namespace bounds the batch independently of open logical queues.
/// Collection failure leaves the engine's Active owner intact for a stopped retry.
pub(super) struct DeferredBatch {
object_pool: Option<Arc<super::object::Pool>>,
has_source_completions: bool,
values: KVVec<super::completion::Deferred>,
/// Exited owners detached from idle retained compute queues and the backing
Expand Down Expand Up @@ -2320,6 +2326,7 @@ impl DeferredBatch {
/// No room at all: allocates nothing.
fn empty() -> Self {
Self {
object_pool: None,
has_source_completions: false,
values: KVVec::new(),
bindings: KVVec::new(),
Expand All @@ -2330,7 +2337,8 @@ impl DeferredBatch {
}
/// Holds nothing: finished, or never used.
pub(super) fn is_idle(&self) -> bool {
!self.has_source_completions
self.object_pool.is_none()
&& !self.has_source_completions
&& self.values.is_empty()
&& self.bindings.is_empty()
&& self.vacated.is_empty()
Expand All @@ -2344,6 +2352,7 @@ impl DeferredBatch {
0
};
Ok(Self {
object_pool: None,
has_source_completions: false,
values: KVVec::with_capacity(capacity, GFP_KERNEL)?,
bindings: KVVec::with_capacity(detached, GFP_KERNEL)?,
Expand Down Expand Up @@ -2473,6 +2482,9 @@ impl DeferredBatch {
for release in self.tvb.drain_all() {
drop(release);
}
if let Some(pool) = self.object_pool.take() {
pool.reap_deferred(super::channel::control_witnessed());
}
}
}

Expand Down Expand Up @@ -4525,6 +4537,7 @@ impl super::Firmware {
freed |= busy && queue.channel_free();
releasing |= queue.pool_release_pending();
}
deferred.object_pool = Some(self.pool.clone());
freed |= self.queues.observe_pool_releases(&self.init, deferred)? != 0;
let shared = &self.primary.state.shared;
// Edge notifications cover the transitions this driver makes; every
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/g17/runtime/render/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl Backend {
let alloc = Allocator {
dev: &dev,
uat: &loan.uat,
client: None,
};
pair.prepare(
&alloc,
Expand Down
5 changes: 5 additions & 0 deletions drivers/gpu/drm/asahi/g17/runtime/render/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl super::super::Backend {
let alloc = Allocator {
dev: &dev,
uat: &resources.uat,
client: None,
};
let pool = match existing {
Some(pool) => pool,
Expand Down Expand Up @@ -239,6 +240,7 @@ impl super::super::Backend {
let alloc = Allocator {
dev: &dev,
uat: &resources.uat,
client: None,
};
pool.ensure_backing(&alloc, &resources.global, client)
}
Expand Down Expand Up @@ -315,6 +317,7 @@ impl super::super::Backend {
let alloc = Allocator {
dev: &dev,
uat: &resources.uat,
client: None,
};
if resources.uat.is_m5() {
return self.prepare_pool_backing(
Expand Down Expand Up @@ -394,6 +397,7 @@ impl super::super::Backend {
let alloc = Allocator {
dev: &dev,
uat: &resources.uat,
client: None,
};
let fresh_graph = match &build {
Build::Fresh(..) | Build::Parked { .. } => true,
Expand Down Expand Up @@ -934,6 +938,7 @@ impl super::super::Backend {
let alloc = Allocator {
dev: &dev,
uat: &uat,
client: None,
};
let mut growth = None;
let busy = matches!(growth_target, Some((_, _, None)));
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/g17/runtime/render/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl Firmware {
let alloc = Allocator {
dev: &dev,
uat: &uat,
client: None,
};
pair.prepare(&alloc, &packet, addresses, 0)
});
Expand Down
Loading
Loading