Repository navigation
drm/asahi: g17: charge a client's queue context to that client - #3683
Draft
jgoyvaerts wants to merge 2 commits into
Draft
jgoyvaerts wants to merge 2 commits into
jgoyvaerts wants to merge 2 commits into
Conversation
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
force-pushed
the
m4pro/g17-kernel-object-charge
branch
from
November 30, 2026 15:42
77c4395 to
2dc7fb5
Compare
Signed-off-by: DjDeveloperr <hey@djdev.me>
Contributor
|
Fixed cross-client reuse of charged queue backing in da65b3f. |
This was referenced Dec 2, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
Allocator::kernel, M5 pooledFamily::Scheduler)Context::new← runtime.rsBackend::new← mod.rsnew_queueFamily::WorkState, 2 pages)Backend::newis 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):
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.pychecks:Backend::new's Allocator names a client;Its mutations
drop-client,none-clientanduncharged-backingare 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 devicefor 4 clients at the cgroup limit.