Skip to content

drm/asahi: g16: share the live VMs between processes and charge VM arenas to their client - #2895

Open
RyanTheTide wants to merge 2 commits into
integration/aurorafrom
pr/g16-vm-share-arena-charge
Open

RyanTheTide wants to merge 2 commits into
integration/aurorafrom
pr/g16-vm-share-arena-charge

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On G16 (M4), every user VM holds one of the device's 63 TTBAT slots for its lifetime, and the live-VM limit is counted per open file only. A process can open the render node any number of times, so that limit doesn't bound how many slots one process holds. Separately, a VM's private arena (about 0.7 GiB for a rendering VM) is made of kernel objects that count only towards the device's kernel total. The client's memory limit and fdinfo never see them.

Change

  • Share the live VMs between processes. VMs are now also counted against the creating process, by the rule queue_share already applies to queues: a process may always hold half of the VMs, and more only while a reserve stays free for the others or while it is the only process holding any. The 8-per-file limit stays. queue_share gains Shares, the same per-process count under the owner's lock.
  • Charge the arenas to the client. new_vm_for_file() now receives the file's memacct client, and G16 sets it on the VM before the registry and the arenas see it. A client VM's private objects are admitted against that client's limit (ENOMEM past it) and held as its user memory instead of kernel memory, so they are counted once.

No new parameters, no chip or board gates. Other backends ignore the new new_vm_for_file() argument, as before.

Testing

  • New host test tools/asahi/g16-vm-share-tests.py, registered in run-host-tests.py. It compiles the production Registry::reserve/release and the queue_share rule against mocks. It checks that VMs stay available to a new process and to the compositor while one process holds many, that a VM given back can be created again, and that a process alone can still use every VM. --mutations: the previous reservation and a share that is never given back both fail (2/2).
  • aurora.o builds with the fleet release config (all backends), with no new warnings.

Still to run

  • J773g (M4 Mac mini) row on the default cmdline, same checks as the J713 row below.
  • On both: VM exhaustion with several clients beside the compositor, two-client VA isolation and fault attribution, the 8 GiB per-client limit, and multi-context dEQP plus the regression check (0 SELinux denials, 0 failed units, 0 new kernel errors).
  • J713 (M4) row on the default cmdline: the desktop and two GPU clients still work. Before and after this series, check fdinfo drm-total-system around a client's first render: it should grow by the arena size with the series and not without it. Also check gpu_memory.
Notes for review
  • The process key is the creator's mm, as in queue_share; a reference to the mm keeps the key from being reused while VMs count against it. VMs created without a process context (new_vm, file id 0) count under key 0, as they counted under file 0 before.
  • The registry takes the processes lock inside files; release() drops the returned process (and its mm reference) after both locks.
  • Arena charges use Charge::user(commit, None) + set_held(true), as the G17 shared pools do (lower_user_charged), with new_kernel_object_wc_user_charged so the device total does not count them twice. The charge lives in the arena's Objects and is released with its backing.
  • A refused arena (ENOMEM) fails the submission that needed it; the VM and its other work are unaffected.

Assisted-by: LLM (Claude:Opus 5.5)

…iles

Every G16 user VM holds one of the device's 63 TTBAT slots for its
lifetime. The live-VM limit was counted per open file only, so it did
not bound how many of the slots one process holds, and VM creation by
other clients could fail with ENOSPC until that process exits.

Count the live VMs against the process that created them as well, by
the rule the queues already use (queue_share): a process may always
hold half of the VMs, and more only while a reserve stays free for the
others, or while it is the only process holding any. The count follows
the VM's live token, so a VM whose handle is closed but whose binding
is still deferred keeps counting. The eight-per-file limit stays.

queue_share gains Shares, the same per-process count kept under its
owner's lock, so the registry can apply the rule to its own resource.

Add a host test that runs the production reservation and release
against mocks: VMs stay available to a new process and the compositor
beside a process holding many, a VM given back can be created again,
and a process alone may still use every VM. The previous reservation
and a share that is never given back are mutants.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
A G16 VM's first render or compute submission builds its private arena
(the two PB heaps, TPC, tilemap, pools and slot storage: about 0.7 GiB
for a rendering VM) from kernel objects. They were counted only in the
device's kernel total, never against the client, so neither the
per-client limit nor fdinfo saw them.

Hand the client's memory accounting to the backend when the VM is
created (new_vm_for_file() gets the file's memacct client) and set it
on the VM before the registry and the arenas see it. Private objects
of a client VM are then admitted against that client's limit (ENOMEM
past it, failing the submission that needed the arena) and held as its
user memory instead of kernel memory, so they are counted once. The
session's own objects and VMs without a client are unchanged.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at 4915f4b.

Read through at 4915f4b. Isolation is unchanged. Each user VM still binds its own TTBAT slot for its whole life (LiveToken.bind, g16/vm.rs), and Registry::live still caps the total at MAX_VMS. The series only changes who is allowed to reserve a slot and who pays for the arena. Nothing new shares an address space or reuses a slot. "Share" in the title reads like address-space sharing; "divide the live VMs between processes" would say what it does.

  1. queue_share.rs:55, held == total: a process that is the only VM holder can take all 63 slots. A queue can be given back, but a VM holds its slot until the process closes it. So a GPU client that starts before anything else holds a VM (a headless or compute daemon at boot, before the compositor) keeps the desktop's VM_CREATE at ENOSPC until it exits. That is the case the reserve exists for. Please either keep reserve(max) free even for a lone holder, or explain why the escape is acceptable for slots that are never reclaimed.
  2. The key is the mm, so fork() gets past the rule: each child may take up to max/2 (held < 31), and three children exhaust the slots. That is an improvement on the per-file count, but the "VMs stay available to a new process and to the compositor" claim only holds for one mm. Please say so in the commit message.
  3. g16/vm.rs:240-249: a GFP_NOWAIT failure of the Shares push is reported as ENOSPC (Err(_)), so a transient allocation failure looks like "no VM slots". Propagate ENOMEM and keep ENOSPC for the EBUSY refusal. The lock is a mutex, so GFP_KERNEL would also work here.
  4. g16/object.rs:426-441: arenas now go through LEDGER.admit, so the device limit also refuses them, where kernel objects never were. Once another client fills the device limit, a newcomer's first render fails with ENOMEM. On 8 GB machines (client limit 4 GiB, about 0.7 GiB per rendering VM) a single file with 8 rendering VMs passes the per-client limit. Both are reasonable, but neither has been run. One small fix: the charge is pushed before registration_guard(), vmap and the map calls, so if any of them fails the charge stays held until the whole Objects is dropped. Push it after the last fallible step, or note why the early hold is safe.
  5. file.rs:713 still calls .with_memory_client(...) after G16 has already set it in new_vm. It is harmless, but please add a comment or drop it for G16.
  6. The arena charge has no test. The host test covers only the registry and share rule. A kunit test or host test that checks admit/refuse/release around alloc_private_in (and the leftover charge from 4) would cover it.
  7. Hardware rows needed at this head before merge: J713 and J773g on the default cmdline:
    (a) the desktop plus two clients, where one opens the render node about 40 times and creates VMs until refused, and the compositor and a third process can still create VMs;
    (b) a two-client isolation check: client B binds and submits against a VA it never mapped, or one inside A's range, and must fault, with the fault attributed to B only;
    (c) fdinfo drm-total-system and sysfs gpu_memory before and after the first render, with and without the series;
    (d) dEQP multi-context (dEQP-EGL.functional.multicontext.* / multithread.*) and the gpubench delta.
    On drm/asahi: g16 + shared SUBMIT: Wave 6 R2 — demand-grown 32 render slots / 18 compute units, fast SUBMIT path, stamp-only barriers, and batched render release #2826 and drm/asahi: g16: Wave 7 — barrier elision, timestamp-free render microsequences, and 48 render pass slots #2852: git merge-tree reports no textual conflict with either. The drm/asahi: g16 + shared SUBMIT: Wave 6 R2 — demand-grown 32 render slots / 18 compute units, fast SUBMIT path, stamp-only barriers, and batched render release #2826 extension tiers (RenderExtArena, PoolExtArena, ComputeArena ext) are allocated through alloc_private_in, so they are charged too. They are best effort (.ok()), so a client at its limit keeps its base slots. It does retry admit on every submission, though, and that bumps refused each time.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at 4915f4b (non-author). Changes requested on the first commit. The second reads right but has no evidence yet.

29e6583 (share VMs per process)

  • This breaks two registered host tests in tools/testing/selftests/drivers/gpu/asahi-m4. test-vm-registry.py and test-resources.py extract Registry/reserve/release/LiveToken from g16/vm.rs, and they no longer compile (new processes field, third Reservation field, release(file_id, process)). run-host-tests.py --only asahi-m4 passes 23/23 with the base driver sources and 21/23 at this commit and at the head (suite exit 1 here). Please update both mocks in this commit.
  • The count is keyed on the creator's mm, and a process below half is granted without regard to the reserve. The reserve is therefore only kept against one process: a second one (a child, or the same program after exec) can take what is left. The commit message and description say a reserve stays free for the others. Please state this limit, or count on a key that survives fork/exec.
  • The "only process holding any" clause lets the process that starts first hold all 63, so a compositor that starts or restarts later gets none. Is that clause needed for VMs? Without it, a single process still reaches 56.
  • reserve() maps every take_current() error to ENOSPC, including an allocation failure of the NOWAIT push. Please keep ENOMEM.
  • Shares records tgid/comm, but nothing prints them; the capacity snapshot only shows the queue share. Either print the VM share there or drop the fields.
  • The new test mocks current_key(), so the process identity itself is not exercised. A case with two processes filling the device would document the intended bound.

4915f4b (charge arenas)

  • The code reads right to me: one user charge per private object, the GEM object uncounted, the charge dropped with the arena, and ENOMEM leaves the VM intact. new_vm_for_file has one override and one caller.
  • There is no test, and the J713 row is still open, so nothing shows the charge yet. Please add the fdinfo before/after row (the first render grows drm-total-system by the arena size, and closing the VM returns it). Also add a row at the client limit showing how the submission failure reaches userspace.
  • The charge also covers the per-render-command page-list/record objects (PbEpoch), not only the arenas. Please say so in the message.
  • with_memory_client is now applied twice (backend and file.rs); one can go.
  • Small: the charge is pushed as held before prepare_new_object/vmap/map can fail. A failure there keeps it until the Objects is dropped. That is fine with today's callers, but worth a comment.

Slot release on every path (including release from another process), key reuse (the entry holds an mm reference), the lock order (files then processes, never nested with vms or memacct) and the arithmetic at 63 (half 31, reserve 7) all check out. No gates or new constants.

Assisted-by: LLM (Claude:Opus 5.5)

@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.

Formal §1–§6 pre-PR checklist review at live head 4915f4b23ac42feaa7065fb6dc1493c7e2cf0117.

Live CI Status

  • host-tests: FAILED (462/463 passed; run 45084522436). While the new g16-vm-share-tests (control + --mutations) passed, the existing asahi-m4 selftest tools/testing/selftests/drivers/gpu/asahi-m4/test-vm-registry.py failed to compile with 4 rustc errors (E0061, E0063, E0433, E0609, including missing processes field in Registry initializer) because g16/vm.rs changed Registry, Reservation, and release() without updating test-vm-registry.py.

Blockers

  1. CI failure (tools/testing/selftests/drivers/gpu/asahi-m4/test-vm-registry.py): Update the existing asahi-m4 registry selftest harness to match the updated Registry (processes: Mutex<Shares>), Reservation, and release(file_id, process) signatures so host-tests is green.
  2. Lone-holder DoS gap for non-reclaimable TTBAT slots (drivers/gpu/drm/asahi/queue_share.rs:55, g16/vm.rs:240): queue_share::may_take allows held == total, so a single GPU client starting before the compositor (e.g., a headless/compute daemon at boot) can claim all 63 exclusive TTBAT slots (MAX_VMS). Unlike queues, G16 VMs pin their TTBAT slot until closed (and deferred/failed VMs retain it longer), locking out the compositor and any subsequent process with ENOSPC. Either preserve reserve(max) even for a lone holder for non-reclaimable VM slots, or justify why the escape is safe (while still ensuring robust_isolation with limit == 1 works). Also clarify in the commit message that keying by mm means fork()ed children each get a fresh mm and 3 children can still exhaust all 63 slots.
  3. GFP_NOWAIT allocation errors masked as ENOSPC (drivers/gpu/drm/asahi/g16/vm.rs:206,236,240-250): Shares::new() is not preallocated (unlike files at L200), yet take_current(limit, GFP_NOWAIT) allocates inside a sleepable Mutex, and L245-249 collapses every Err(_) (including ENOMEM) into ENOSPC. Preallocate Shares with MAX_VMS capacity or allocate with GFP_KERNEL, and map only EBUSY to ENOSPC while propagating ENOMEM.
  4. Early Charge push before fallible steps (drivers/gpu/drm/asahi/g16/object.rs:426-445): charge.set_held(true) and self.charges.push(charge, GFP_KERNEL)? happen before registration_guard(), prepare_new_object(), vmap(), the 3 map_in_range/map_at calls, and Arc::new(). If any of those fail on an Objects container that is not immediately dropped, backing is freed while charge remains held as phantom resident/committed bytes. Since self.charges.reserve(1, GFP_KERNEL)? already ran at L428, defer charge.set_held(true) and self.charges.push(...) until right before self.retained.push(...) at L537.
  5. Missing arena-charge test coverage: tools/asahi/g16-vm-share-tests.py covers only Registry::reserve/release + queue_share; there is no host or KUnit test verifying alloc_private_in admit/refuse/release and rollback accounting.
  6. §5 Evidence (zero hardware runs): No hardware rows have been run yet. Before merge, provide J713 (M4) and J773g (M4 Mac mini) default-cmdline rows covering: (a) multi-client VM exhaustion beside compositor + third client, (b) two-client VA isolation / fault attribution, (c) before/after fdinfo (drm-total-system) and sysfs gpu_memory around first render (plus 8 GiB client-limit sanity check), (d) multi-context dEQP + standard regression run (0 SELinux denials, 0 failed units, 0 new kernel errors).

Checked & Clean + djdev/g16-* Stack Overlap Notes

  • Locking & refcounts (g16/vm.rs:231-270): Lock ordering (files → processes) is consistent across reserve() and release(), and release() drops Option<Process> (and its Option<ARef<Mm>>) outside both locks. Objects field drop order (retained, mappings before charges) releases backing before uncharging.
  • §1/§2/§3/§4/§6: Pass (no model gating, default cmdline, no hardcoded safety limits; both commits carry human Signed-off-by only with zero AI trailers and Assisted-by in PR body; clean naming). Minor nit: file.rs:713 calls .with_memory_client(...) after G16 already set it in g16::vm::new_vm (needed for non-G16 backends; a short comment avoids confusion).
  • Overlap with djdev/g16-* stack (#2787, #2826, #2852):
    • File overlap: drivers/gpu/drm/asahi/file.rs overlaps with #2826 (Wave 6: SyncItem/Object cmdbuf reuse & SUBMIT path; disjoint from #2895 L712 vm_create). drivers/gpu/drm/asahi/g16/mod.rs overlaps with #2787 (Wave 2–5) and #2826 (Wave 6) in Shared/Driver/FAST_POLL_WORK (disjoint from #2895 L619–629 new_vm/new_vm_for_file). Zero file overlap with #2852 (Wave 7). No textual merge conflicts expected.
    • Symbol/semantic interaction with #2826: #2826's demand-grown extension tiers (RenderExtArena, PoolExtArena, ComputeArena ext) allocate through factory.objects(vm) → Objects::alloc_private_in(), so they automatically inherit client charging via LEDGER.admit. Best-effort .ok() growth preserves base slots at the limit, though note repeated submissions at the limit will re-hit admit and increment LEDGER.refused.

Assisted-by: LLM (Gemini 4 Neon)

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