Skip to content

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

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

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

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Dec 2, 2026 •

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 (a7551b3, which merges main at 9b8e29f). KUnit (arm64 qemu) at a7551b3: 1060 run, 1059 passed, 1 skipped, 0 failed. Host tests --only g17- at a7551b3: 388 of 388 pass. Before the merge: 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 4 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>
Main at 9b8e29f. Conflicts resolved keeping both sides:
- Makefile: main's object list (g17/mesh_exclusion.o is gone) plus memcg_scope.o.
- runtime.rs: DeferredBatch::is_idle() is main's pub(super) form with the object_pool check.
- object.rs: main made a retained object's fence optional (None: kept until firmware stop); the reap of charged objects uses Retained::ready().
- g17-object-fence-tests: main() runs both client_pool_retirement() and cached_queue_lifetimes(); main's new test builds its Allocator with client: None.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
@jgoyvaerts
jgoyvaerts marked this pull request as ready for review December 4, 2026 03:39
@jgoyvaerts
jgoyvaerts requested review from a team and DjDeveloperr as code owners December 4, 2026 03:39
@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Merge readiness at a7551b33: not ready yet

Check Detail
CI at head ok 11 checks passed
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head no no line with the head SHA a7551b33, a Mac and a result in the PR body or a comment
Conflicts no merge conflicts with main: merge it in (no force push)
PR text ok Risk: two-way · 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 clientbacking(): the client's memory limit could already refuse it.

Areas: area:gpu

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving the code at a7551b33c04b. I'll merge after the J714s and J700 rows pass. They use the same combined kit as #3704 and #3976.

client_backing() now goes through charged(), so tiler heap growth and M5 render targets put their pages in the client's memory cgroup even when a worker builds them. The admission and the commit are unchanged. The main merge resolves its three conflicts the same way #3704 does (Makefile, DeferredBatch::is_idle, the object-fence test) and picks up main's entry.ready().

One consequence to keep in mind: at the client's cgroup limit, a worker's allocation can now fail where it used to go to the root cgroup. The J714s row has a memory-capped phase that checks that a client under a cap only ever stops with an out-of-memory error or a memcg kill, never a lost device.

Local checks, on current main 35ddf947:

  • main + this PR merges cleanly (tree 257710b6). run-host-tests.py --only g17- in the host-tests container: 410/410 pass.
  • On main a16156f8, the driver of #3704 + #3976 + this PR together built with no new W=1 warnings.
  • Hosted CI is green.

For landing: this conflicts with #3704 and #3976 only in tools/asahi/g17-queue-object-charge-tests.py and run-host-tests.py; the driver merges cleanly. If #3704 lands first, this PR needs a forward merge that keeps both sides:

  • the docstring, the --mutation choices and the run-host-tests.py entry list all seven mutations (drop-client, uncharged-backing, none-client, uncharged-compute, charged-kit, uncharged-pages, root-populate);
  • sources() keeps both pairs of mutation branches;
  • check() keeps this PR's client_backing()/charged() check and takes #3704's allocator list (Backend::new and Backend::provision_backing) instead of "only one allocator".

I tried exactly that resolution on main 35ddf947 + all three PRs. The g17 host tests pass 427/427, including the queue-object test and all seven mutations. The resolved files are blobs a76992a9d4ff (g17-queue-object-charge-tests.py) and cac6cf8e331e (run-host-tests.py). If you make the same resolution, they should match.

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Dec 4, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Not involved: the J714s row of all three PRs lost the device at boot because of #3704's commit 936c70c9dbdd; cause and fix are on #3704. This PR's own change is not on that path. Its M5 row will be re-run once #3704 is fixed.

@github-actions github-actions Bot added merge-conflict Conflicts with the base branch (bot) and removed merge-conflict Conflicts with the base branch (bot) labels Dec 4, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

@jgoyvaerts This branch now conflicts with main (1da341136110) in:

  • drivers/gpu/drm/asahi/memacct.rs
  • tools/asahi/run-host-tests.py

Note: this branch sits on #3683 and shares the same two conflict files with it.

Could you forward-merge main into this branch (normal git merge, no rebase/force-push)? If you haven't had a chance within 24 hours, a maintainer can forward-merge main on top for you.

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

area:gpu Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) merge-conflict Conflicts with the base branch (bot) needs-hw-row No hardware row at the PR head (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants