Skip to content

drm/asahi: g17: allocate compute objects in SUBMIT, not at publication - #3704

Draft
jgoyvaerts wants to merge 7 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-compute-reserve
Draft

jgoyvaerts wants to merge 7 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-compute-reserve

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What: Stacked on #3683; review only the top three commits. On G17, compute publication no longer allocates. A compute SUBMIT allocates what its publication would need, and a refusal is that SUBMIT's ENOMEM or ENOSPC, before the command is queued. Before, a refusal at publication (the scheduler's kworker, after SUBMIT returned) failed the job, and the client lost its device.

  • Commit 1: a compute queue's objects are allocated apart from the QID they are bound to; binding them only stores.
  • Commit 2: G17P's shared compute aperture and scheduler are allocated in SUBMIT and installed at publication.
  • Commit 3: each logical queue (owner) keeps a reserve of compute backing. SUBMIT provisions it: the pool pages and descriptor ring are charged to the client, and the kit is device memory. Publication takes from the reserve. Backing a queue gives back returns to its owner until the owner closes.

Tested: no Mac at this head yet (936c70c). A partial hardware run on an M4 Pro (J616s) at 2c8dfe7, before the two newest commits: see Evidence. KUnit (arm64 qemu) at 2c8dfe7: with CONFIG_MEMCG=y 955 run, 954 passed, 1 skipped, 0 failed; g17-compute-reserve host test and its 8 mutations pass. 2c8dfe7 differs from c769558 only in building Spares::is_empty and take_all for KUnit alone (a non-KUnit build warned they were unused); at c769558, with drivers/gpu/drm/asahi/.kunitconfig (no memory cgroups) 521 run, 517 passed, 4 skipped. Host tests at 9a9efbe (this head but for one KUnit assertion): 845 runs pass; the 29 that fail also fail at #3683's head (apple-dcp, test-m3-uapi, g15-pb-growth x2, g15-retention --mutations, g15-stats-drain x2, sep-suspend-errors x22).

Risk: two-way door · M4 and M5 (G17). Every Mac running the driver: compute publication paths change. A compute owner that has commands keeps its backing, at least 23 MiB of pool pages on G17P, while they are pending. Idle owners hold what they held before.

Not yet:

  • Hardware: a full-length memory-capped (MemoryMax) compute-heavy run at this head, at the cgroup limit, with 0 lost devices reported from compute publication and SUBMIT refusals observed (the run above is partial).
  • Small kernel allocations at publication stay: the queue's lists, its box and its client views (page tables). They are GFP_KERNEL and not memcg-charged, so a cgroup limit cannot refuse them. Stage 3e's lockdep annotation will show them.
  • Render: pairs, rings, generations, tiler heaps and render timestamps are still built at publication (stage 3b), and so are the LowerVa placements (3c).
Details

Where compute publication allocated (main), and where it moved:

Object Before Now
Pool pages (Pages::for_compute), descriptor ring publication: rearm_vacant, build_compute, try_add_compute_sibling, grow_compute SUBMIT, Backend::provision_backing, charged to the client
Pool control/state, kick ring, graph objects (compute::Kit) publication: PoolBacking::new, Backing::new, Queue::new SUBMIT, device memory (an installed queue outlives its owner)
ComputeShared (22 MiB on G17P), ComputeScheduler first compute publication SUBMIT, device memory, installed at publication as before

QID split (commit 1). Only stores depend on the QID: the pool control header's buffer slot (ComputePool::bind), the kick ring's id (kick::Queue::compute), and the owner's queue record (Graph::new). Kit::new allocates without a QID.

Device memory. Objects that outlive the client that caused them (the kit, the shared aperture and scheduler) are allocated in the client's ioctl. So that their pages are not charged to that client's memory cgroup, they are allocated under memacct::device_memory, which makes the root cgroup the active one. That matches what happened when the kworker allocated them.

The reserve (commit 3).

  • Provision: Registry::compute_lacks checks whether the owner's spares, or the queues holding its backing (also while they give it back), have pages that fit the command and a ring. If not, SUBMIT allocates them. An owner without a queue of its own also gets a kit.
  • Take: publication takes from the reserve. A fresh queue is built and installed in one device-mutex section, from the kit and the set.
  • Return: a queue records whose backing it holds (backing_owner). When it gives the backing back, the pages and ring return to that owner's spares, reset and cleared, while the owner has compute commands. A backing comes back from:
    • a pool release the firmware consumed;
    • the smaller pages after a pool grows;
    • a recycled ring;
    • a never-published queue's rollback.
  • Other owners: another open owner's backing is never bound. A queue that holds it is given back first, as a queue holding another VM's pool is.
  • Claims: each compute command holds a claim on its owner, from before its SUBMIT provisions until the command is dropped. Without claims an owner is idle: what its queues give back is freed, and the pool pass frees its spares. So an idle owner holds what it held before, and close frees the rest.
  • Siblings (M5): best effort, as before. A sibling that finds no spares asks the next SUBMIT for a set.

No deadlock: why a publication waiting for its backing always gets it.

  1. The command's SUBMIT made sure a set that fits exists among the owner's spares or the queues holding the owner's backing. The command's claim keeps that set from being freed until the command is dropped, and only the owner's own publications take from its spares.
  2. Owner publications run one at a time: run_job is serial per scheduler, and an early publication happens only without a backlog. So no earlier publication of the same owner holds the set at the same time.
  3. That leaves four places the set can be:
    • in the owner's spares: taken at once;
    • in a queue the owner holds: rebind binds it (Rebind::Done), or growth takes the queue's pages after draining its siblings;
    • in an idle queue that holds the owner's backing: bind_reusable binds it, or growth asks the queue to give it back (recall_backing);
    • in a queue that was taken from the owner or is giving it back: its release is published by the event pass (vacate_idle_pools forces queues marked for fresh backing).
  4. A release is consumed by the firmware's control ring. That does not depend on any client's fences. The event pass that observes the consumed release gives the set back (keep_backing) and wakes channel and progress waiters.
  5. The waiting publication holds no device mutex or preparation lease while it waits (wait_channels, wait_release).

So the wait does not depend on its own client: not on a fence the client waits for, nor on a later SUBMIT. A kill or a cancel ends it through the admission check. During recovery, the admission is extended as for every other publication wait. The 30 s admission deadline still bounds it, as it bounds every publication wait.

Evidence
  • KUnit asahi_g17_compute_reserve (4 cases): spares move without being freed; the smallest fitting pages are taken; a full owner frees only covered pages; each spare is freed once.
  • KUnit asahi_memacct: device_memory_is_charged_to_the_root_cgroup.
  • Host tests, red at drm/asahi: g17: charge a client's queue context to that client #3683's head and green here, each with mutations that inject the fault and must be caught:
    • g17-compute-reserve-tests:

      • bind steps and the publication paths allocate no device memory;
      • SUBMIT charges pages and ring, and allocates the kit as device memory;
      • the claim precedes the provision, and the command is queued after it;
      • every give-back keeps the backing for its owner, and close and the pool pass free spares;
      • another open owner's backing is never bound;
      • compiled: the production keep_backing, stock and trim_reserves free every spare exactly once, also when the owner closes with its reserve unused.

      Mutations: 7.

    • g17-compute-constructor-tests: SUBMIT prepares the shared objects once as device memory, and a refusal fails it. Publication never builds them, and keeps them on cancel, a stale lease or a crash. Mutations: 3.

    • g17-admission-order-tests: every valid compute SUBMIT provisions, and a refusal fails it.

    • g17-queue-object-charge-tests: exactly QUEUE_CREATE's context and SUBMIT's compute backing name the client. Mutations: 2 new.

  • Fixtures updated for the changed functions: the g17 compute growth, retired, growth, uninstalled cleanup, M5 sibling compute, gate and rearm, object fence and render fast-done tests.
  • Hardware, partial (containment and a clean run; the reserve under memory pressure did not run): M4 Pro (J616s), the commits up to 2c8dfe7 backported onto a test kernel (not da65b3f or 936c70c), 2026-12-02. dEQP-VK timeline-semaphore cases at 10 jobs with the system Borealis, in a MemoryMax cgroup of 12.99 GB without swap. The cgroup reached its limit 2.5 s in and stayed there about 10 s, until a memcg OOM kill. The kill came from an ordinary GFP_NOFS allocation, and its victim was a deqp-vk with 1.89 GB shmem-rss. systemd then stopped the run, 13 s in. The compute list never started, and no SUBMIT was refused.
    • GPU memory stayed in the cgroup: the OOM report's memory.stat shows shmem 11.78 GB of the 12.69 GB limit. Shmem of every other cgroup stayed at its starting 3.8 GB.
    • 0 GPU recoveries, 0 "GPU work of a client failed", 0 refused submits, 0 allocation failures, 0 warnings.
    • System Shmem was back to its starting level within about 1.6 s of the kill.

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>
@jgoyvaerts
jgoyvaerts force-pushed the m4pro/g17-compute-reserve branch from 9a9efbe to c769558 Compare November 30, 2026 18:58
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>
@jgoyvaerts
jgoyvaerts force-pushed the m4pro/g17-compute-reserve branch from c769558 to 2c8dfe7 Compare November 30, 2026 21:05
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Fixed compute backing ownership, channel rollback and concurrent shared-aperture allocation in 936c70c.

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