Skip to content

drm/asahi: g17: charge client_backing() pages to the client's memory cgroup - #3956

Draft
jgoyvaerts wants to merge 3 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-client-backing-memcg
Draft

jgoyvaerts wants to merge 3 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-client-backing-memcg

Conversation

@jgoyvaerts

Copy link
Copy Markdown
Contributor

What: Stacked on #3683; review only the top commit. On G17, KernelObject::client_backing() charges an object to its client's limits and held memory, but it filled the pages in whatever task called it. Its callers include kernel workers: tiler heap growth at publication and M5 render targets. A worker has no memory cgroup, so those pages went to the root cgroup while the driver counted them as the client's. Now client_backing() builds the object through charged(), which fills the pages under client.charging(), so they are charged to the client's memory cgroup whichever task builds them. client_backing() comes from f587245 ("drm/asahi: g17: M5: account render target memory to the client that sizes it", Jeff Queisser) and is used for tiler heaps since a64f33a ("drm/asahi: g17: charge tiler-heap growth through KernelObject::client_backing()").

Tested: no Mac at this head yet (d3c2096). KUnit (arm64 qemu) at d3c2096: 950 run, 949 passed, 1 skipped, 0 failed. Host tests --only g17- at d3c2096: 297 of 297 pass; the new check in g17-queue-object-charge-tests fails on the unfixed source and catches its two mutations.

Risk: two-way door · M4 and M5 (G17). Tiler heap growth and M5 render targets of a client in a memory-capped cgroup now count against that cgroup, so growth can fail with ENOMEM at the cgroup's limit where its pages used to go to the root cgroup. The growth paths already handle an error from client_backing(): the client's memory limit could already refuse it.

Not yet:

  • Hardware: a memory-capped run showing tiler growth pages in the client's cgroup. Our test runs on G17 under memory pressure found shmem charged to the root cgroup by kernel workers (see Evidence); this commit closes one path into that, and the remainder is still being traced.
  • Same pattern elsewhere: Allocator::lower_user_charged() (compute pool pages, wrapped at its call site in drm/asahi: g17: allocate compute objects in SUBMIT, not at publication #3704's stack), and the G14-G16 and G18 user-charged kernel objects (alloc.rs, g16/object.rs, g18/buffer/object.rs), which are not changed here.
Details
  • client_backing() keeps its admission (LEDGER.admit) and then calls Self::charged(dev, size, CpuMap::WriteCombined, client, commit), the helper queue_backing() already uses: the same write-combined user-charged object, with_object() inside client.charging(..), Charge::user(commit) held. On a failure the commit is dropped as before.
  • Callers: buffer.rs Tvb::allocate (reached from runtime/render/grow.rs and runtime/render/prepare.rs, which run in workers) and buffer/target.rs Object::new for M5 targets.
Evidence
  • g17-queue-object-charge-tests: client_backing() goes through charged(), and charged() fills its pages inside client.charging(). Mutations uncharged-pages (fill in the calling task again) and root-populate (drop client.charging()) must be caught, and are.
  • On a G17 test kernel (M4 Pro, J616s) with a capped dEQP-VK run, a kprobe on charge_memcg filtered to the root memory cgroup saw all root charges come from unbound workqueue workers, up to 2.2 M per second while system shmem rose about 16 GB above what the cgroups held. Root charges stopped when the test's cgroup was killed. This commit fixes one worker path that charges client memory to the root cgroup; it is not yet shown to be the whole of that.

jgoyvaerts and others added 3 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>
Signed-off-by: DjDeveloperr <hey@djdev.me>
…cgroup

KernelObject::client_backing() admits an object to its client's limits
and counts it as the client's held memory, but it populated the pages in
whatever task called it. Its callers run in workers too: tiler heap
growth at publication (runtime/render/grow.rs, runtime/render/prepare.rs)
and M5 render targets. A worker has no memory cgroup of its own, so
those pages were charged to the root cgroup while the driver's ledger
said they were the client's: a MemoryMax limit on the client did not
bound them, and the OOM killer and systemd-oomd could not see them in
the client's cgroup.

Build the object through charged(), as queue_backing() does: it
populates the pages under client.charging(), so they are charged to the
memory cgroup of the task that opened the client, whichever task builds
the object.

g17-queue-object-charge-tests now checks that client_backing() goes
through charged() and that charged() populates inside client.charging(),
with two mutations that restore each fault.

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