Skip to content

drm/asahi: g17: charge a client's queue context to that client - #3683

Draft
jgoyvaerts wants to merge 2 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-kernel-object-charge
Draft

jgoyvaerts wants to merge 2 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-kernel-object-charge

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What: On G17, the objects QUEUE_CREATE builds for a client's queue (the context's scheduler storage and work state) are charged to that client: to its driver accounting (fdinfo, its limits) and to its memory cgroup. A failure there comes back from QUEUE_CREATE as ENOMEM, a normal Vulkan error. The rest of a queue's kernel objects are built at publication, after SUBMIT has accepted the job. They stay uncharged until they are built in QUEUE_CREATE or SUBMIT (follow-up work); charging them where they are now costs a client its device when its cgroup fills.
Tested: no Mac at this head yet. KUnit (arm64 qemu, CONFIG_MEMCG=y) at 2dc7fb5: 949 passed, 1 skipped, 0 failed. Host tests at 2dc7fb5: g17-queue-object-charge-tests and its 3 mutations pass, as do the 3 fixtures this change touches; the 29 remaining failures also fail on main (sep-suspend-errors, g15-*, apple-dcp, test-m3-uapi). M4 Pro test kernel m4.213 (the earlier, unsplit form of this PR): 0 kernel errors at boot; in a 24 GB memory-capped dq-fast run its publication-time charges lost 4 devices with ENOMEM, which is why this head charges only QUEUE_CREATE's objects.
Risk: two-way door · M4 and M5 (G17); every Mac running the driver also takes one memory-cgroup reference per DRM file.
Not yet: hardware run of this head: gpu_memory user_resident/kernel_resident and the client cgroup's memory.current across queue create and close. The publication half (render pairs, rings, generations, tiler heaps, compute state, timestamps) comes with building those objects in QUEUE_CREATE or SUBMIT.

Details

Charged now (built in the QUEUE_CREATE ioctl):

Object Where
Context scheduler storage (G17P Allocator::kernel, M5 pooled Family::Scheduler) context.rs Context::new ← runtime.rs Backend::new ← mod.rs new_queue
Context work state (Family::WorkState, 2 pages) same

Backend::new is the only Allocator that names a client.

How: KernelObject::queue_backing() commits the bytes to the client whatever the limits (Ledger::commit) and counts them as its held memory. The pages are populated with the client's memory cgroup active (memacct::Client::charging, set_active_memcg). That cgroup is the one of the task that opened the DRM file, referenced for the client's lifetime. The charge belongs to the object and is released when the object is freed, also after the client's file is gone while the firmware still holds it.

Not charged yet (built at publication, after SUBMIT accepted the job):

  • the render pool and its backing, the render pair (queue state, config, support, generations, timestamps, registers, PB graph, NEO tiler-heap primary, targets, kick rings) and TVB growth;
  • compute growth, build, rearm and sibling channels, the compute timestamp page;
  • lent pairs and recovery replay.

Their Allocators say client: None, and three files build uncharged objects outside an Allocator (buffer.rs, buffer/target.rs, completion.rs). The host test pins exactly this split.

Why not all at once. The first form of this PR charged all of them. On the m4.213 test kernel, a dq-fast timeline-cases run at -j10 in a 24 GB MemoryMax cgroup filled the cgroup with GPU memory. Four clients then lost their device with ENOMEM at publication (g17/runtime/backend.rs:75): a publication-time kernel object's pages could no longer be charged. Before, those objects were charged to no cgroup when the scheduler's kworker built them. The fix is to build them where a failure can be returned from an ioctl, and charge them then.

Left uncharged (device-global): the boot-time firmware structures, heaps and pools (initdata.rs, initdata/m5.rs), the crash buffer, the PM metrics and the M5 metrics (mod.rs), and the shared compute pool.

Overlap with #2724: none any more. This head does not touch Tvb::allocate.

Evidence
  • KUnit asahi_memacct:

    • queue_objects_are_charged_to_their_client_and_released_once: the charge is taken past both limits, counts against the client's and the device's next admissions, survives the client's file, and is released once;
    • charging_restores_the_memory_cgroup.
  • tools/asahi/g17-queue-object-charge-tests.py checks:

    • only Backend::new's Allocator names a client;
    • all other Allocators say None;
    • uncharged kernel objects appear only in object.rs, the global boot files and the publication files.

    Its mutations drop-client, none-client and uncharged-backing are each caught.

  • m4.213 row (a), the earlier form of this PR: kernel log GPU work of a client failed (ENOMEM, reported at g17/runtime/backend.rs:75, ...); the client sees a lost device for 4 clients at the cgroup limit.

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>
@jgoyvaerts
jgoyvaerts force-pushed the m4pro/g17-kernel-object-charge branch from 77c4395 to 2dc7fb5 Compare November 30, 2026 15:42
@jgoyvaerts jgoyvaerts changed the title drm/asahi: g17: charge a client's queue objects to that client drm/asahi: g17: charge a client's queue context to that client Nov 30, 2026
Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Fixed cross-client reuse of charged queue backing in da65b3f.

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