Skip to content

drm/asahi: g17: re-arm a vacated render pool at SUBMIT, not at publication - #3976

Draft
jgoyvaerts wants to merge 8 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-render-rearm-submit
Draft

jgoyvaerts wants to merge 8 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-render-rearm-submit

Conversation

@jgoyvaerts

Copy link
Copy Markdown
Contributor

What: Stacked on #3704 (and #3683); review only the top commit. On G17, a render pool that pressure reclaim gave back (vacate_evicted_render_pools(), from b138b1c "drm/asahi: give back the render pool backing of evicted idle clients") is re-armed in the client's SUBMIT, under its memory cgroup, instead of at publication in the scheduler's worker. A refusal (ENOMEM at the cgroup's limit, ENOSPC without lower kernel VA) is that SUBMIT's error, before the command is queued. Publication only takes the pool. This is D2 stage 3b for the render pool, the render counterpart of #3704's compute reserve.

Tested: no Mac at this head yet (4b2eace). KUnit (arm64 qemu) at 4b2eace: 955 run, 954 passed, 1 skipped, 0 failed. Host tests --only g17- at 4b2eace: 312 of 312 pass, including the new g17-render-rearm-submit-tests and its 6 mutations; that test fails on the base (936c70c).

Risk: two-way door · M4 Pro and other G17P (NEO layout) Macs. A render SUBMIT of a client whose pool was vacated now allocates the pool's backing itself: under a memory-capped cgroup it can fail with ENOMEM where the backing used to be charged to the root cgroup, and with ENOSPC when lower kernel VA is exhausted where publication used to wait. M5 is unchanged.

Not yet:

  • Hardware: a memory-capped dEQP-VK run on this head, sampling the root cgroup's own shmem: no root-only shmem episodes, ENOMEM/ENOSPC refusals at SUBMIT, 0 publication failures, 0 recoveries.
  • M5: its pools are still armed by the pair that pins them (prepare_pool_backing), at publication.
  • First pool: a context's first render pool is still built at its first publication; moving it would turn render-id exhaustion from a wait into a SUBMIT refusal.
  • Fence-signalling annotations (dma_fence_begin_signalling()/lockdep) for the publication path: stage 3e.
  • Late freeing: backing dropped in a kernel worker frees its pages only when the global delayed_fput work runs (fs/file_table.c, __fput_deferred()), and lower_with_aliases() fills a backing's pages before it places it, so a placement refused for lack of lower VA drops a full backing. Under churn this frees far behind; after this change those pages count in the client's cgroup, not the root's.
  • Reusing a vacated G17P backing as a spare (as M5's cache_reclaimed() does), only where that keeps the vacate's purpose.
Details

Before. vacate_evicted_render_pools() vacates the pool of a client VM that pressure reclaim evicted while idle and hands the backing to the worker's DeferredBatch. On NEO, RenderPool::ensure_backing() gave a vacant pool fresh backing at its next use: in Context::render_pool() and in build_render_pair()'s matching-pair branch, both reached from drm_sched_run_job_work → Backend::publish → render_with_lease → ensure_render. Under pressure that repeated: evict, vacate, re-arm in the worker, free later.

Now.

  • Queue::enqueue() calls the new Backend::provision_render() for every render command, after its block check and before it prunes or queues anything. Other backends keep the default (nothing).
  • The G17 backend's provision_render_pool() returns at once unless the context's pool is vacant (and not M5). Otherwise it enters a preparation lease (recovery joins it before replacing the firmware whose VMs the backing maps into), takes the build resources under the device lock if the lease is current (EAGAIN otherwise; libdrm retries), and re-arms through Context::rearm_render_pool() inside client.charging().
  • Context::render_pool() and build_render_pair() no longer call ensure_backing() on NEO. A vacant pool there is logged once and fails with EIO.

Why a pool re-armed at SUBMIT is still armed at publication. vacate_evicted_render_pools() has two loops. The first vacates a pool only if pool.client_evicted(), which is the client VM's residency gate reporting evicted && !busy && users == 0. Every accepted command holds a VmJobGuard from prepare_submission(), before enqueue(), until the job is dropped, and that guard holds a residency lease (users != 0). While it is held, Gate::try_reclaim() does not evict the VM and Gate::evicted() is false; entering the gate restored an evicted VM and cleared the flag. So between this SUBMIT's re-arm and the command's publication the first loop cannot vacate the pool. The second loop vacates only the pools of kept graphs, which runtime/render/teardown.rs keeps for contexts whose release was consumed: no further publications. A VM without a residency gate is never reported evicted. The EIO at publication is therefore a broken invariant, not a load case.

No deadlock, and out of the fence path. The re-arm runs in the client's ioctl, holding its queue lock and a preparation lease, in the same order build_render_pair() takes them (preparation lease, then the device lock, then the context's pool lock). It waits on nothing the GPU must signal. Before, RenderBacking::new() → drm_gem_get_pages() → shmem_read_folio_gfp() ran in drm_sched_run_job_work, inside the dma-fence signalling path: under reclaim that allocation could wait on a shrinker that waits on a fence this very job must signal. The re-arm is no longer on that path.

Evidence
  • g17-render-rearm-submit-tests: enqueue() re-arms for render commands before it queues; the re-arm is charged to the client and checks its preparation lease; Context::render_pool() and build_render_pair() no longer re-arm; the remaining ensure_backing() callers are M5 paths. Mutations no-submit-rearm, late-rearm, uncharged-rearm, no-lease-check, publication-rearms, pair-rearms must be caught, and are.
  • Before this change, on an M4 Pro (J616s) test kernel with drm/asahi: g17: allocate compute objects in SUBMIT, not at publication #3704, a capped dEQP-VK run with a kprobe on charge_memcg filtered to the root memory cgroup (validated with a write from a process placed in the root cgroup and two from user units): root-only shmem reached 13.95 GiB while the driver's held kernel objects stayed at 1.3-1.85 GiB. 11054 of the kernel-worker stacks were drm_sched_run_job_work → Backend::publish → render_with_lease → ensure_render → build_render_pair → RenderPool::ensure_backing → RenderBacking::new → drm_gem_get_pages.

jgoyvaerts and others added 8 commits November 30, 2026 12:41
The firmware objects of a G17 client's queues were kernel memory: counted
towards the device total (kernel_resident in gpu_memory) but owned by no
client, and owned by no memory cgroup when a kernel thread populated them.
Under a dq-fast timeline-cases load on an M4 Pro, kernel_resident peaked
at 2.4 GB.

This charges the objects that QUEUE_CREATE builds, the context's scheduler
storage and work state, to the client that creates the queue:
- **Driver accounting:** they count as the client's held memory (fdinfo
  drm-resident-system, its limits) instead of kernel memory.
- **Memory cgroup:** their pages are charged to the client's memory
  cgroup. That is the cgroup of the task that opened the DRM file, made
  the active memory cgroup while the pages are populated.
- **Failure:** QUEUE_CREATE builds them in the client's ioctl, so a
  failed allocation comes back from it as ENOMEM, which Borealis reports
  as VK_ERROR_OUT_OF_HOST_MEMORY.
- **Lifetime:** the charge belongs to the object. It is released when the
  object is freed, also after the client's file is gone while the
  firmware still holds the object.

The rest of a queue's objects stay uncharged, as before: render pairs,
kick rings, generations, tiler heaps, compute state and timestamps. G17
builds them when a submission is published, after SUBMIT accepted it. A
memory cgroup at its limit would fail them there, and a failure there
costs the client its device: a test kernel with all of them charged lost
4 devices with ENOMEM when its cgroup filled.
- They are charged once they are built in QUEUE_CREATE or SUBMIT.
- The Allocator now names its client: QUEUE_CREATE's names it, the
  publication paths say None.
- `KernelObject::queue_backing()` and `Allocator::backing()`/`object()`
  are the way in for both.

Device-global objects stay uncharged kernel memory: the boot-time firmware
structures and heaps (initdata.rs, initdata/m5.rs), the crash buffer, the
PM metrics and the M5 metrics in mod.rs, and the shared compute pool.

Tests:
- **KUnit:** a queue charge is taken past both limits, counts against the
  client's and the device's next objects, outlives the client's file and
  is released once with its object. A memory-cgroup scope restores what
  it replaced.
- **Host test** (g17-queue-object-charge-tests.py):
  - only QUEUE_CREATE's Allocator names a client, and every other one
    says None;
  - uncharged kernel objects appear only in the global boot files and in
    the three publication files that build objects outside an Allocator
    (buffer.rs, buffer/target.rs, completion.rs);
  - three injected faults are caught.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
A fresh G17 compute queue was built for the QID publication reserves: the
pool's control header names the QID's pool slot, and the kick ring carries
the QID. So its objects could only be allocated once the QID was known, at
publication, after SUBMIT accepted the job.

Only stores depend on the QID. Split construction in two:
- **Allocate:** `compute::Kit::new` allocates the pool's control and state
  (`freelist::PoolObjects`), the kick ring storage (`kick::ComputeRing`) and
  the graph's objects (`GraphObjects`), with no QID. The pool pages and the
  descriptor ring are allocated as before.
- **Bind:** `compute::Queue::new_channel(kit, pages, ring, id, ...)` binds
  them to the QID: `ComputePool::bind` writes the control header,
  `kick::Queue::compute` keeps the id and `Graph::new` writes the owner's
  queue record. No device memory is allocated.

Publication still calls both, in the same order of effects: the pool's row
is installed before the queue is built. A later change allocates in SUBMIT.

tools/asahi/g17-compute-reserve-tests.py checks that the bind steps allocate
nothing, with two mutations that inject an allocation.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
G17P's device-wide compute aperture (ComputeShared, 22 blocks of 1 MiB plus
its lists) and compute scheduler were built by the first compute
publication that needed them, on the scheduler's kworker, after SUBMIT had
accepted the job. A refused allocation there failed the job, and the
client lost its device.

A compute SUBMIT now allocates them while they are neither installed nor
prepared (`Backend::provision_compute`, from `ensure_compute`). A refusal is
that SUBMIT's error. The publication that needs them takes the prepared
object and installs it, as before; it allocates nothing, so its builder
flags, the waits for another builder and the condition variable they
waited on go. Installation keeps its timing:
the aperture with the first publication's lease, the scheduler with the
first fresh queue's pool row.

Both stay device memory: they serve every client until processor stop.
Allocated in a client's ioctl, their pages would be charged to that
client's memory cgroup, so `memacct::device_memory` charges them to the
root cgroup, as when the kworker allocated them.

M5 has neither and allocates nothing.

Tests:
- g17-compute-constructor-tests now runs the production SUBMIT and
  publication halves (prepared once, a refusal fails the SUBMIT and
  prepares nothing, publication never builds and keeps the prepared
  object on cancel, a stale lease or a crash) with three mutations.
- g17-admission-order-tests checks that every valid compute SUBMIT
  provisions and that a refusal fails it.
- g17-m5-render-fast-done-tests checks that note_changed wakes render
  waiters only.
- KUnit asahi_memacct: device_memory_is_charged_to_the_root_cgroup.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
A G17 compute queue's backing (the pool pages, at least 23 MiB on G17P,
and the descriptor ring) and a fresh queue's kit were allocated when a
command was published, on the scheduler's kworker, after SUBMIT had
accepted it. A refusal there failed the job, and the client lost its
device. With the backing charged to the client's memory cgroup, a client
at its cgroup limit lost it that way.

Each logical queue (owner) now has a reserve of compute backing:
- **SUBMIT provisions.** A compute SUBMIT allocates what its publication
  would lack (`Backend::provision_backing`): pages that fit the command
  and a ring, unless the owner's spares or the queues holding its backing
  have them, and a kit while the owner has no queue of its own. Pages and
  ring are charged to the client through its
  Allocator; the kit is device memory (`memacct::device_memory`), since an
  installed queue outlives the owner. A refusal is that SUBMIT's ENOMEM or
  ENOSPC, before the command is queued.
- **Publication takes.** Re-arming a vacant queue, building a fresh one
  (now in one device-mutex section with its install), growing a pool and
  adding an M5 sibling take from the owner's spares and allocate no device
  memory. A sibling that finds none asks the next SUBMIT for a set.
- **Backing is the owner's until it closes.** A queue records whose
  backing it holds. When it gives the backing back (a pool release the
  firmware consumed, a grown pool's smaller pages, a recycled ring, a
  never-published queue's rollback), the pages and ring go back to that
  owner's spares, reset, while the owner has compute commands. Another
  open owner's backing is never bound: a queue holding it is given back
  first, as another VM's is.
- **Claims.** Each compute command holds a claim on its owner from before
  its SUBMIT provisions until the command is dropped. An owner without
  claims is idle: what its queues give back is freed, and the pool pass
  frees its spares, so idle memory is what it was. Close frees the rest.

A publication whose backing is on its way back waits as it waits for a
channel today, and the event pass that observes the consumed release
returns the backing and wakes it. That release is published by the pool
pass and consumed by the firmware, whatever the waiting client does.

Tests:
- KUnit asahi_g17_compute_reserve: spares move without being freed, the
  smallest fitting pages are taken, a full owner frees only covered pages,
  and each spare is freed once.
- g17-compute-reserve-tests: publication paths allocate nothing; SUBMIT
  charges pages and ring and allocates the kit as device memory; the claim
  precedes it and the command is queued after; every give-back keeps the
  backing for its owner; the production keep_backing, stock and
  trim_reserves free every spare exactly once; a queue-less owner always
  gets a kit. Six more mutations.
- g17-queue-object-charge-tests: SUBMIT's compute backing is the second
  allocator that names the client; two more mutations.
- Fixture updates for the changed functions in the g17 compute, growth,
  sibling and cleanup tests.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Signed-off-by: DjDeveloperr <hey@djdev.me>
Signed-off-by: DjDeveloperr <hey@djdev.me>
…ation

Under memory pressure, vacate_evicted_render_pools() gives back the
render pool backing of a client whose VM pressure reclaim evicted while
idle (b138b1c). On NEO, the next publication that needed the pool
re-armed it with fresh backing, in the scheduler's worker. A worker has
no memory cgroup, so those pages were charged to the root cgroup: a
client's MemoryMax did not bound them, and the OOM killer and
systemd-oomd could not see them in the client's cgroup. Evict, vacate
and re-arm repeated under pressure, filling backing in the worker far
faster than the vacated backing was freed.

Re-arm in the client's SUBMIT instead. Queue::enqueue() calls the new
Backend::provision_render() for every render command before it queues
anything; on G17 it re-arms the context's pool if it is vacant, under
client.charging() and with a preparation lease, so the pages go to the
client's cgroup and a refusal (ENOMEM at the cgroup's limit, ENOSPC
without lower kernel VA) is this SUBMIT's.

The command's VmJobGuard holds the VM's residency lease from before
enqueue until the job is dropped. While a lease is held, pressure
reclaim does not evict the VM and the residency gate does not report it
evicted, so vacate_evicted_render_pools() cannot vacate the pool again
before the job is published. Its other loop vacates only the pools of
kept graphs, which are graphs of torn-down contexts. Publication
(Context::render_pool(), build_render_pair()) therefore only takes the
pool; a vacant one there is a broken invariant, logged once and failed
with EIO, never re-armed in the worker.

Unchanged: a context's first pool is still built at its first
publication, and M5 pools are still armed by the pair that pins them.

g17-render-rearm-submit-tests checks the SUBMIT call and its order, the
charge and the lease check, and that publication no longer re-arms on
NEO, with a mutation for each.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants