Skip to content

drm/asahi: charge GPU page tables to their client and cap VMs per file - #2530

Open
funkzy wants to merge 3 commits into
aurora-silicon:mainfrom
funkzy:pr/asahi-pagetable-charge
Open

funkzy wants to merge 3 commits into
aurora-silicon:mainfrom
funkzy:pr/asahi-pagetable-charge

Conversation

@funkzy

@funkzy funkzy commented Nov 20, 2026 •

Copy link
Copy Markdown

Problem

A client VM's page tables, and the GPUVA nodes that record its mappings, were not counted by the GPU memory limits, not shown in the client's fdinfo and not charged to any memory cgroup, and the number of VMs one file could create was not limited. Sparse and single-page binds map a small object over a large range, so the table memory a client causes was not bounded by the memory it holds.

Why this shape

Page-table memory was outside every limit the driver keeps: client buffers are admitted against the client and device ledgers (memacct), shown in fdinfo and charged to the memory cgroup, while the tables a bind creates were none of these. A narrower change would cap table pages or the bound range per VM with a new constant, which bounds nothing a client can't get around with more VMs and says nothing in fdinfo or the cgroup. Here the tables go through the ledger buffers already use, admitted like a held buffer of the same size and allocated with GFP_KERNEL_ACCOUNT, so the existing limits, fdinfo and the cgroup see them. The GPUVA nodes are charged to the cgroup the same way. The one new number is the per-file VM cap, a policy bound like the memory limits; it is asked about below. Every GPU family takes the same accounting.

Change

Three commits in drivers/gpu/drm/asahi: page tables are charged to the client that created the VM (admitted against its limits once allocated, after every other step that can fail, refusals fail the bind with ENOMEM, allocated with GFP_KERNEL_ACCOUNT); GPUVA nodes are allocated with GFP_KERNEL_ACCOUNT; and VM_CREATE past 64 VMs in one file fails with EBUSY. Mesa creates one VM per file, and Borealis's sparse space on M5 needs 8 MiB of tables (16 MiB with its read-only shadow).

It applies to the client VMs of every GPU family; there is no chip, board or firmware condition. The 64-VM cap is a new per-file limit, like the existing memory limits, a policy bound rather than a hardware value (see the questions below).

Since RyanTheTide's review: a table is now charged only after the last step that can fail, so a failed table leaves no charge behind. The first commit has a new KUnit case for the refusal on a root that records its own tables. The third commit's message says the cap is per file.

Testing

On the J775D (Mac Studio, M5 Ultra), on this PR's own base: integration/aurora b21dcc6 plus #1973, which the J775D needs for its GPU node and G17D binding, and nothing else. Kit pr2530-1 is that base with this PR at 6386898; kit pr2530-1b is the same base without it. One read-only one-shot boot of each through the 27.0 firmware stub, default command line, 0 GPU client failures at boot on both. Aura was main 82ee5d6fdbdf with the commits of aura#262 and aura#264, both merged since.

without (pr2530-1b) with this PR (pr2530-1)
bind of one page over 1 TiB (512.25 MiB of tables) fdinfo and GPU memory counters +0.0 MiB, cgroup -1.2 MiB (no charge); free memory -486 MiB fdinfo and GPU memory counters +512.2 MiB, cgroup +512.0 MiB, kept across the unbind; after VM_DESTROY -0.1, -0.1 and -1.1 MiB
the same bind with SPARSE_TILE fdinfo and GPU memory counters +0.0 MiB, cgroup +0.0 MiB (no charge) +512.2, +512.2 and +512.0 MiB, then back to within 0.1 MiB
VMs in one file 100 created (the test stops there) 64 created, then EBUSY
render CTS subset (628 cases) 571 Pass, 57 NotSupported the same

Regression run (ALARM has no SELinux, so there is no AVC count): 0 new failed units (iwd.service and systemd-networkd-persistent-storage.service fail on the read-only test root on both kits) and 0 new kernel error lines: both boots log the same 7 error lines once timestamps and numbers are removed.

Extra evidence, not this PR's own row: the earlier run on integration/aurora cc14570 with #1973, our three other open drm/asahi PRs and a local appledrm fix gave the same charge, release and cap. dj's review asked for the run above in its place.

Still to run

  • A G13 or G14 Mac (M1 or M2) on this head: dEQP-VK.sparse_resources.* and the render CTS subset with the same results as the base, fdinfo showing the sparse table charge and its release, 0 new kernel errors and 0 failed units. Rows from RyanTheTide's lab or an M1/M2 owner welcome.
  • Other GPU families (G15, G16, G17P, G18), which take the same charge: not run. On non-M5 parts Borealis advertises a 512 GiB sparse space, which with its shadow would be charged up to 512 MiB of tables if filled completely.
Full description of the three commits
  1. Charge page tables to the client. The root and the tables that exist when the VM gets its client are counted at once. Each later table is admitted against the client's and the device's memory limits, as a held buffer of the same size would be. It is charged after every other step that can fail: the page is allocated and cleaned, its slot in the driver-tables list is reserved, the table is charged, and only then recorded in that slot. A refusal frees the table and fails the bind or kernel mapping that needed it with ENOMEM. The tables are also allocated with GFP_KERNEL_ACCOUNT. The charge is released when the VM is destroyed. Kernel VMs are not charged.
  2. Charge GPUVA nodes to the memory cgroup. The nodes that binds and unbinds allocate use GFP_KERNEL_ACCOUNT.
  3. At most 64 VMs per file. VM_CREATE past that fails with EBUSY, the ID allocator's own error. Mesa creates one VM per file. The cap is per file, as the memory limits are: a client that opens the device again gets another 64 VMs.

Legitimate use stays far below the limits: Borealis's sparse address space on M5 is 16 GiB per device (8 MiB of tables, 16 MiB with its read-only shadow).

This applies to every GPU family. On non-M5 parts Borealis advertises a 512 GiB sparse space, which with its shadow would be charged up to 512 MiB of tables if filled completely.

Full test results

On the Mac Studio (M5 Ultra), 27.0 firmware, read-only test root, kits pr2530-1 (integration/aurora b21dcc6 + #1973 + this PR) and pr2530-1b (the same without this PR):

  • Single-page bind of one page over 1 TiB (32768 leaf and 16 level-1 tables, 512.25 MiB), with this PR: after the bind fdinfo +512.2 MiB, GPU memory counters +512.2 MiB committed and +512.3 MiB resident, memory cgroup +512.0 MiB; the same after the unbind; after VM_DESTROY -0.1, -0.1 and -1.1 MiB. With SPARSE_TILE: +512.2, +512.2 and +512.0 MiB, then -0.1, -0.1 and +0.1 MiB. Without it: fdinfo and the GPU memory counters +0.0 MiB for both binds, and the cgroup -1.2 and +0.0 MiB after them (no charge), while free memory fell 486 and 515 MiB.
  • VMs in one file: with this PR 64 created, then EBUSY; without it 100 created, where the test stops.
  • Render CTS subset (628 cases, list sha256 372186cfedde): 571 Pass, 57 NotSupported on both kits.
  • KUnit: with this PR 77 suites, 379 of 381 cases pass, 0 fail, 2 skipped; on the base 76 suites, 375 of 377, the same two skips (apple_g17_rx_util_min_test, which needs CONFIG_UCLAMP_TASK, and j713_accepts_all_loaded_images). Counted per case from the KTAP lines. asahi_pgtable_charge 3 of 3, including a_refused_table_is_neither_charged_nor_recorded; asahi_memacct 12 of 12 (11 of 11 on the base).
  • Host tests: upstream tools/asahi/run-host-tests.py in the build VM: 461 of 461 on integration/aurora b21dcc6 and on that base with arm64: dts: apple: t6050-j775d: Give the Mac Studio (M5 Ultra) a working GPU, and its M and P core capacities (754/1024) to the scheduler #1973 and this PR; 0 of 461 runs differ.
  • Kernel log and units: the error-level kernel lines of both boots, compared after removing timestamps and numbers, are the same 7. 0 client failures, recoveries, faults or refused submissions in either boot. Failed units: iwd.service and systemd-networkd-persistent-storage.service on both.
  • Temperature: the Mac Studio's SMC sensors stayed at or below 75.2 C (DC input at most 174 W) through both boots.
Questions
  • Mappings the kernel makes into a client VM can now be refused like the client's own binds. Would you rather count them but never refuse them, as kernel objects are?
  • Is 64 VMs per file the right cap, and is EBUSY the right error (or ENOSPC)? Should it apply to every family, as here, or only to G17?
  • Freeing empty leaf tables on unmap is left out: it does not bound anything while a mapping stays. Worth doing for long-lived sparse clients?

Thanks to ewb, who reported the uncharged page-table memory and suggested the charge, and to RyanTheTide for the review.

  • Clean-room: no Apple code, strings, firmware, RE output or tuning values
  • No chip/board/firmware-version gate where a hardware property can decide
  • Works on the default kernel command line; no hacks, delays or lab-only paths
  • Safety limits come from the machine, not from constants in code
  • Hardware safety rules followed for every test boot
  • Third-party commits unchanged; reporters and testers credited
  • Commits: human Signed-off-by only; no co-author or Assisted-by trailers
  • Tested on the affected hardware; no regression

Assisted-by: LLM (Claude:Opus 5.5)

@funkzy
funkzy force-pushed the pr/asahi-pagetable-charge branch 2 times, most recently from 7c99f6e to 22b9bb2 Compare November 21, 2026 06:20
@RyanTheTide

Copy link
Copy Markdown

No blockers.

Checked the ledger split (Ledger::reserve / Usage), the charge and release paths in pgtable.rs, the GFP_KERNEL_ACCOUNT changes and the 64-VM xarray cap. The charge is released when the UatPageTable drops (also on the retained early return, where the tables are deliberately leaked), and free_table is only reached from Drop (free=true at pgtable.rs:1172), so the "charged until the VM goes" model holds.

  1. drivers/gpu/drm/asahi/pgtable.rs:711-731: the table page is charged (charge.try_add) before the two later fallible steps, clean_poc's with_pointer_into_page and driver_tables.push. If either fails, the page is freed but the charge stays until the VM is destroyed, so a client hitting allocation failures accumulates phantom charge against its own limit. Either charge after the last fallible step, or add a Usage::sub and give the page back on those error paths. (The doc comment at charge_to says each table is admitted "before it is allocated"; the code allocates first and frees on refusal, which is fine, but the comment should match.)
  2. Nit: file.rs:403 says each VM holds uncharged memory; worth noting in the commit that the 64 cap is per file, so a client can still open more files. That is the existing per-file model, just make it explicit.

HW row: one G13 or G14 Mac (M1/M2) on this head: dEQP-VK.sparse_resources.* and the render CTS subset with the same results as the base, fdinfo showing the sparse table charge and its release, 0 new kernel errors, 0 failed units.

Assisted-by: LLM (Claude:Opus 5.5)

@funkzy
funkzy force-pushed the pr/asahi-pagetable-charge branch from 22b9bb2 to 6386898 Compare November 21, 2026 19:14
@funkzy

funkzy commented Nov 21, 2026

Copy link
Copy Markdown
Author

Thanks, Ryan. Both points are fixed in the commits, now at 6386898.

  1. Phantom charge. The first commit now charges a table only after every step that can fail. The page is allocated and cleaned, a slot in the driver-tables list is reserved, the table is charged, and only then is it recorded in the reserved slot, which cannot fail. A failure at any step frees the page with nothing charged, so Usage needs no sub. The charge_to comment and the commit message now say that a table is charged once it is allocated and ready to install. New KUnit case a_refused_table_is_neither_charged_nor_recorded: on a borrowed root that records its own tables, a refusal leaves the charge equal to the tables kept, and the refused table is not recorded. The existing refusal case now checks the table count too. KUnit can't inject the allocation failures themselves.
  2. Per-file cap. The third commit's message now says the cap is per file, like the existing memory limits: a client that opens the device again gets another 64 VMs, and each file is accounted on its own.

git range-diff against the current head: the first commit changes only in that ordering, the comment, the new test and its message; the second is unchanged; the third changes only its message. I've added your Suggested-by to the first and third.

J775D on this head (stacked with #1973 and our other three open drm/asahi PRs on integration/aurora cc14570, 27.0 firmware, default command line; the kernel also carries a local fix for an appledrm boot oops, see #1973):

  • A single-page bind of one page over 1 TiB: fdinfo, gpu_memory and the memory cgroup each grow by 512.2 MiB, the size of the tables, and keep it across the unbind. After VM_DESTROY fdinfo and gpu_memory are back to their starting values (within 0.1 MiB) and the cgroup is 1.9 MiB below its start. A sparse-tile bind reads the same.
  • VM_CREATE in one file: 64 created, then EBUSY.
  • Render CTS subset: 571 Pass, 57 NotSupported, the same as without the series.
  • KUnit: 366 of 368 pass (two skips, as on the base). Host tests: 436 of 436 on base and head.
  • Regression: 0 new failed units (iwd.service and systemd-networkd-persistent-storage.service fail on the read-only test root with and without the series) and 0 new kernel error lines. ALARM has no SELinux, so there is no AVC count.

Rows we can't run: could your lab, or an M1 or M2 owner, run this head on a G13 or G14 Mac? The useful row is dEQP-VK.sparse_resources.* and the render CTS subset with the same results as the base, fdinfo showing the sparse table charge during a sparse bind and its release at VM destroy, 0 new kernel errors and 0 failed units. The page-table code is shared, so that row covers the families we don't have.

@RyanTheTide

Copy link
Copy Markdown

Re-review of 6386898: No blockers.

  1. Phantom charge on a failed table install (pgtable.rs:711-731 before): resolved. drivers/gpu/drm/asahi/pgtable.rs:712-736 now allocates, cleans, reserves the driver_tables slot, and only then calls charge.try_add; the record is push_within_capacity into the reserved slot, so every step that can fail runs before the charge and a refused or failed table is freed with nothing charged. The charge_to comment (pgtable.rs:557-569) now matches, and a_refused_table_is_neither_charged_nor_recorded checks charge, table count and recorded list after a refusal.
  2. Per-file cap nit: resolved; the third commit's message now says the 64 cap is per file and a reopened file gets its own.

Nothing else changed since the last review (second commit unchanged, third only in its message). Trailers are a human Signed-off-by plus Suggested-by only.

HW row: still only the M5 Ultra, on an integration stack with other open PRs and a local appledrm fix. Needed: one G13 or G14 Mac (M1/M2) on this head alone, dEQP-VK.sparse_resources.* and the render CTS subset unchanged from the base, fdinfo showing the sparse table charge during a sparse bind and its release at VM destroy, 0 new kernel errors, 0 failed units.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pre-PR checklist review (§1–§6) on live head 63868987c8536c7612d1c4cdf198c39ad37a8b50 (host-tests: pass).

Checklist Summary

  • §1 No model gating: Pass — shared file.rs, memacct.rs, mmu.rs, mmu/bind.rs, and pgtable.rs paths apply uniformly across all GPU families with zero chip/SoC/board/firmware gates.
  • §2 Production-ready, no hacks: Pass — works on default kernel command line, no new module parameters, no sleeps/retries/lab-only paths.
  • §3 Safety: Pass — no hardcoded hardware safety constants; MAX_VMS_PER_FILE = 64 is a per-file DoS/resource bound matching the existing per-file memory-limit model.
  • §4 Clean-room & attribution: Pass — all 3 commits (7d82a3723bc8, 62174055d25f, 63868987c853) carry human author/committer and human Signed-off-by/Reported-by/Suggested-by trailers only; PR body includes Assisted-by:.
  • §6 Naming: Pass — uses Borealis and Aurora naming.
  • §5 Evidence: Holding for cross-family hardware proof.

Code Audit (0 blockers)

  • memacct.rs: Ledger::reserve() split from admit() preserves rollback-on-device-limit-failure semantics; Usage::{new, try_add, add, hold} and Drop keep client.{committed,resident} and ledger.{user_committed,user_resident} balanced.
  • pgtable.rs: UatPageTable::with_pages() allocates with GFP_KERNEL_ACCOUNT once charged, runs clean_poc and driver_tables.reserve(1, GFP_KERNEL) before charge.try_add(UAT_PGSZ as u64), and only then records via push_within_capacity and increments tables. Any failure frees the page with no phantom charge. Verified free_table() is only reachable from UatPageTable::drop(). KUnit asahi_pgtable_charge covers normal lifecycle, client-limit refusal, and borrowed-root refusal.
  • mmu.rs & mmu/bind.rs: Verified all three GpuVaAlloc::new allocation sites use GFP_KERNEL_ACCOUNT.
  • file.rs: Verified vm_xa.lock().reserve_limit(1..=MAX_VMS_PER_FILE, GFP_KERNEL) returns -EBUSY (__xa_alloc in lib/xarray.c) when full, and failed_vm cleans up the newly created vm (releasing its initial root-table charge) on refusal.

Holding For (§5)

0 code/policy/attribution/naming/CI blockers. Holding approval solely for the missing cross-family hardware row on this head (this change applies to all GPU families, and the J775D run was stacked with #1973 and other open PRs):

  • One non-M5 Mac (e.g. G13/G14 M1 or M2) running this head: dEQP-VK.sparse_resources.* and render CTS subset matching baseline, fdinfo sparse page-table charge and release on VM_DESTROY, 0 SELinux denials (if enabled), 0 failed units, and 0 new kernel errors.

Assisted-by: LLM (Gemini 4 Neon)

@funkzy

funkzy commented Nov 22, 2026

Copy link
Copy Markdown
Author

Thanks. The J775D row is now on this PR's own base: integration/aurora b21dcc6 plus #1973 (the J775D's GPU node and G17D binding), with and without this PR, nothing else. Kits pr2530-1 and pr2530-1b, 27.0 firmware, default command line:

  • A one-page bind over 1 TiB (512.25 MiB of tables): +512.2 MiB in fdinfo and the GPU memory counters and +512.0 MiB in the memory cgroup, kept across the unbind and released at VM_DESTROY. On the base the same bind charges nothing: fdinfo and the GPU memory counters stay at +0.0 MiB and the cgroup moves -1.2 MiB. SPARSE_TILE reads the same.
  • VMs in one file: 64, then EBUSY. The base creates 100 without a refusal, where the test stops.
  • Render CTS subset: 571 Pass, 57 NotSupported on both.
  • KUnit: 379 of 381 in 77 suites with this PR, 375 of 377 in 76 on the base, the same two skips; asahi_pgtable_charge 3 of 3.
  • Host tests: 461 of 461 on the base (b21dcc6) and with arm64: dts: apple: t6050-j775d: Give the Mac Studio (M5 Ultra) a working GPU, and its M and P core capacities (754/1024) to the scheduler #1973 and this PR, no result differs.
  • 0 new failed units and 0 new kernel error lines against the base. ALARM has no SELinux.

The Testing section now leads with this row; the earlier stacked run stays only as labelled extra evidence with its base. The cross-family row (an M1 or M2 with dEQP-VK.sparse_resources.*) is still open, as you noted.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown

Review notes (non-author):

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review at 6386898. Accounting client page tables changes AGX2 as well as newer GPUs. Please show charge and release for failed map, teardown, and closing a file at the VM limit while another file continues to use its VMs. The G14G sparse-resource row should include reuse after close and report the final fdinfo charge.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Formal §1–§6 review at verified live head 63868987c8536c7612d1c4cdf198c39ad37a8b50 (live integration/aurora 70a00e583b03, not superseded; CI: host-tests green).

Checklist Summary

  • §1 No model gating, §3 Safety, §4 Attribution, §6 Naming: PASS — shared drivers/gpu/drm/asahi accounting applies uniformly across all GPU families; UatPageTable::with_pages() cleanly allocates, cleans, reserves driver_tables capacity, and charges last (charge.try_add()) so refused/failed tables leave zero phantom charge; all 3 commits (7d82a3723bc8, 62174055d25f, 63868987c853) carry human author/committer and human Signed-off-by:/Reported-by:/Suggested-by: only; PR body carries Assisted-by:. The new standalone J775D own-base row (b21dcc633b32 + #1973 + 63868987c853) cleanly validates M5 Ultra.

Remaining Blockers

  1. §2 Early VM-slot reservation on the refusal path (drivers/gpu/drm/asahi/file.rs:713-734): vm_xa.lock().reserve_limit(1..=MAX_VMS_PER_FILE, GFP_KERNEL)? still runs after gpu.new_vm_for_file() and with_memory_client(). Because reserve_limit() drops the xarray lock immediately and returns an owned Reservation that releases the slot automatically on error unwind, reserving the slot before new_vm_for_file() avoids allocating a root page table, initializing GpuVm, registering G16 backend state, mutating LEDGER, and running full ScopeGuard teardown on every refused VM_CREATE past the 64-VM cap (matching Ryan's note; a KUnit case for the cap would also strengthen coverage).
  2. §5 Missing cross-family hardware row (G13/G14 M1 or M2): This series changes shared page-table and VM lifecycle accounting for every GPU family, and only J775D (M5 Ultra, ALARM without SELinux) has been run. Still needed on a non-M5 Mac (G13/G14): dEQP-VK.sparse_resources.* and render CTS subset unchanged vs baseline, fdinfo charge & release across failed map, teardown, and close/reuse at the 64-VM limit while another file uses its VMs, plus the full §5 regression tuple (0 SELinux AVC denials on an enforcing host, 0 failed units, 0 new kernel errors).
  3. Open accounting question from review (GPUVA nodes): Please either also account GpuVaAlloc nodes in memacct (so client limits and fdinfo reflect kernel memory from large numbers of small binds) or explicitly answer why memcg-only (GFP_KERNEL_ACCOUNT) is the intended boundary.

Assisted-by: LLM (Gemini 4 Neon)

A client VM's page tables are allocated as the client maps and kept
until the VM is destroyed, but they were not counted by the GPU memory
limits, not shown in the client's fdinfo and not charged to any memory
cgroup. Because sparse and single-page binds map a small object over a
large range, the table memory a client causes was not bounded by the
memory it holds.

Charge the tables to the client that created the VM, as a held user
buffer of the same size would be. The root and the tables that exist
when the VM gets its client are counted at once. Each later table is
admitted against the client's and the device's memory limits once it is
allocated, after every other step that can fail, so a table that is not
installed leaves no charge behind. A refusal frees the table and fails
the bind or kernel mapping that needed it with ENOMEM. From then on the
tables are also allocated with GFP_KERNEL_ACCOUNT, so they count towards
the memory cgroup of the task that maps. The charge is released when the
page table is freed with the VM. Kernel VMs are not charged.

Legitimate use stays far below the limits: the Borealis Vulkan driver's
sparse address space on M5 is 16 GiB per device, 8 MiB of tables (16 MiB
with its read-only shadow).

This applies to the client VMs of every GPU family.

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Ryan Murray <ryan@aurorasilicon.org>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
…roup that create them

Each mapping in a VM is recorded in a GPUVA node until it is unmapped,
and those nodes were not charged anywhere. Mapping one page after
another needs a page table only every 2048 pages but a node for every
mapping, so the nodes can outweigh the tables the previous commit
charges many times over.

Allocate the nodes that binds and unbinds use with GFP_KERNEL_ACCOUNT,
so they count towards the memory cgroup of the task that asks for the
operation. In a VM charged to a client, also charge each node's size
against the client's and the device's memory limits, through the same
memacct::Usage that the previous commit uses for page tables, so both
committed and resident totals in fdinfo and gpu_memory reflect them. A
map operation reserves one node's charge before preallocating nodes or
changing mappings; a recorded mapping keeps its charge until removed,
and an unused charge is returned when the operation ends.

Whole-mapping and truncating unmaps never fail on memory limits: unmap
preparation reserves no charge, and a truncating remap transfers the
removed node's charge to the single surviving piece before dropping the
old node (net zero). Only an operation that increases the node count --
a new map, including kernel maps into a client VM, or a remap that
splits one mapping into two -- checks the client and device limits and
can fail with ENOMEM, before modifying any page table.

Use the existing allocation-free unmap_user_ranges() path in Vm::drop()
for every GPU family, so tearing down an untracked VM while its client
is at its memory limit never needs to preallocate split nodes.

This applies to every GPU family.

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Ryan Murray <ryan@aurorasilicon.org>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
…rk stay bounded

Besides its page tables, each VM holds kernel memory that is not charged
to its client: its GPUVM, its kernel allocators and per-VM objects. A
G17 user VM also registers two shrinkers of its own, and every reclaim
pass walks all registered shrinkers. The number of VMs one file could
create was not limited.

Refuse VM_CREATE past 64 VMs in one file with EBUSY, as the ID allocator
reports a full range. VM_CREATE takes its table slot before it builds
anything of the VM, so a refused create costs nothing. Mesa creates
one VM per file.

The limit is a count, not a charge against the client's memory limit:
the VM's memory could be charged as its page tables are, but the
shrinker registrations are not memory, and the time reclaim spends
walking them grows with their number. Neither the machine nor a memory
limit bounds that. Shrinkers registered once per device, walking the
device's VMs, would make the count unnecessary.

The cap is per file, as the existing memory limits are: a client that
opens the device again gets another 64 VMs in the new file, each file
accounted on its own.

This applies to every GPU family.

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Ryan Murray <ryan@aurorasilicon.org>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
@funkzy
funkzy force-pushed the pr/asahi-pagetable-charge branch from 6386898 to 39f1f06 Compare November 24, 2026 00:17
@funkzy

funkzy commented Nov 24, 2026

Copy link
Copy Markdown
Author

Updated the series (rebased onto integration/aurora 1b1304ff2a67, head 39f1f067e77a) to address both code blockers from @DjDeveloperr's review and @RyanTheTide's notes:

  1. Early VM-slot reservation (39f1f067e77a, drivers/gpu/drm/asahi/file.rs): vm_create() now calls reserve_vm_slot() (reserving 1..=MAX_VMS_PER_FILE in the file's XArray) before admit_vm(), gpu.new_vm_for_file(), and with_memory_client(). A refused VM_CREATE past 64 VMs returns EBUSY before building or tearing down any VM state, and any later error drops the Reservation to release the slot. Added KUnit suite asahi_vm_table (a_file_holds_at_most_max_vms_per_file) covering the 64-VM cap, EBUSY refusal, slot release and reuse, and per-file isolation with a second table.
  2. GPUVA node accounting in memacct (df9812344d81, drivers/gpu/drm/asahi/memacct.rs, mmu.rs, mmu/bind.rs, file.rs): In addition to GFP_KERNEL_ACCOUNT for memcg, GpuVaAlloc nodes in a client VM are now charged through memacct::Usage (size_of::<gpuvm::GpuVa<VmInner>>() per mapping), so client/device limits and both committed and resident fdinfo / gpu_memory totals reflect them. Map operations reserve one node's charge up front before allocating nodes or mutating mappings; live mappings hold their charge in VmGpuVa until removed, unused charges return when the operation ends, and VmGpuVa::clone() for MappingSnapshot copies metadata without duplicating the charge. Whole-mapping and truncating unmaps never fail on memory limits (unmap preparation takes no charge, and a truncating remap hands the removed node's charge to the surviving piece before dropping the old node, net zero); only operations that increase the node count — new maps (including kernel maps into a client VM) and hole-punch unmap splits that turn one mapping into two — check limits and can fail with ENOMEM before changing any page table. Vm::drop() now uses the existing allocation-free unmap_user_ranges() path on every GPU family so tearing down an untracked VM (G13/G14) while its client is at its memory limit succeeds without preallocating split nodes. Added KUnit suite asahi_gpuva_charge (3 cases: charge/snapshot/release, refusal at client_limit, and unmap/truncating remap succeeding at client_limit while a hole-punch split refuses with ENOMEM).

KUnit (built-in kunit-vz on 1b1304ff2a67 + 39f1f067e77a): 81 suites, 399 of 401 cases pass, 0 fail, 2 baseline skips (apple_mbox_g17_stall/apple_g17_rx_util_min_test, asahi_fwid/j713_accepts_all_loaded_images); asahi_memacct (12/12), asahi_pgtable_charge (3/3), asahi_gpuva_charge (3/3), and asahi_vm_table (1/1) all pass.

Still to run on this head: a fresh own-base J775D hardware row after push (J775D runs ALARM, so no SELinux; units and kernel error checks apply there), and the requested cross-family G13/G14 row (dEQP-VK.sparse_resources.*, render CTS subset, fdinfo charge/release across failed map, teardown, and close/reuse at the 64-VM limit while another file uses its VMs, plus unit/dmesg regression checks and SELinux AVC checks on an enforcing host).

Assisted-by: LLM (Claude:Opus 5.5, Gemini:Neon)

@funkzy

funkzy commented Nov 24, 2026

Copy link
Copy Markdown
Author

Fresh own-base J775D (Mac Studio, M5 Ultra) A/B hardware row at live head 39f1f067e77a on bare integration/aurora 5746ef8a5afd (no extra PRs needed now that #1973 is merged):

  • Baseline arm: kit pr2530-r5b (window r2530@20261123-1850) — bare integration/aurora 5746ef8a5afd.
  • PR arm: kit pr2530-r5 (window r2530@20261123-1850) — 5746ef8a5afd + this PR at 39f1f067e77a.
  • Environment: 27.0 firmware stub (26A428), published Aura main (f13056d95af6, aura-main-f13056d9), read-only root, default kernel command line.
Check pr2530-r5b (base) pr2530-r5 (PR at 39f1f067e77a)
pgtrepro bind 1024 (1 page over 1 TiB, 512.25 MiB tables) MemFree -486.4 MiB; fdinfo / gpu_memory / cgroup +0.0 MiB fdinfo +512.3/+512.3 MiB, gpu_memory +512.3/+512.3 MiB, cgroup +510.7/+510.3 MiB; released after VM_DESTROY (-0.1/-0.0 MiB)
pgtrepro bind 1024 --sparse-tile MemFree -511.5 MiB; fdinfo / gpu_memory +0.0 MiB fdinfo +512.3/+512.3 MiB, gpu_memory +512.3/+512.3 MiB, cgroup +513.0/+512.3 MiB; released after VM_DESTROY (-0.1/-0.0 MiB)
pgtrepro vms 100 created (stopped there) 64 created, then EBUSY (Device or resource busy)
Render CTS subset (628 cases, Borealis) 571 Pass, 57 NotSupported, 0 Fail 571 Pass, 57 NotSupported, 0 Fail
GPU recoveries / faults / WARNING / BUG / Oops 0 0
Failed units / kernel errors (ALARM, no SELinux) 2 / 8 same 2 / same 8 (0 new)

Assisted-by: LLM (Gemini:Neon)

@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) merge-conflict Conflicts with the base branch (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: HOLD at 39f1f067e77a

Blockers:

  • drivers/gpu/drm/asahi/mmu.rs (EOF after mod execution_context_tests): merge conflict (CONFLICTING / dirty) against main (a9cd131e8207), which appended FirmwareWindow (CONFIG_DRM_ASAHI_G14_V27_2) in the same location as mod gpuva_charge_tests. Rebase onto main and refresh exact-head verification.
  • drivers/gpu/drm/asahi/mmu.rs:926-944 and 3367-3380 (sm_step_remap / unmap_prepared_cleanup): an already-accepted deferred hole-punch unmap (has_prev && has_next) queued under quarantine (defer_bind_batch() / defer_unmap()) calls charge.try_fork(GPUVA_BYTES) during drain_mappings(). If the client or device is at its memacct limit at drain time, ENOMEM trips state.cleanup_failed / state.blocked in mmu/lifetime.rs:753-786 and leaves stale page-table leaves after VM_BIND returned 0 and untrack_context_ranges() already ran. On the unmap_prepared_cleanup() path (whose split GpuVaAlloc nodes were already preallocated before deferring), charge the extra split node unconditionally (e.g. via transfer_copy() gated by a flag on StepContext).
  • §4 Attribution (PR body): remove the AI-attribution footer line at the bottom of the PR body (and omit attribution footers from future PR comments). All three commits are clean.
  • §5 PR structure & cross-family HW evidence: update the PR body with the required **What:**, **Tested:**, **Risk:**, and **Not yet:** header lines and refresh the stale ## Change / ## Testing text (still describing 63868987c853 and memcg-only GPUVAs) to match the rebased head; add a cross-family G13/G14 (M1 or M2) exact-head row (dEQP-VK.sparse_resources.* and render CTS subset vs baseline, fdinfo charge/release across failed map, teardown, and close/reuse at the 64-VM limit while another file uses its VMs, plus 0 SELinux AVC denials on an enforcing host, 0 failed units, and 0 new kernel errors — especially important now that Vm::drop() routes untracked G13/G14 VMs through unmap_user_ranges()).

Checked: Full diff and 63868987c853..39f1f067e77a interdiff (file.rs, memacct.rs, mmu.rs, mmu/bind.rs, pgtable.rs), 3-way merge check against main (a9cd131e8207), commit/PR hygiene (§1–§6), CI (host-tests green), and J775D exact-head row (39f1f067e77a).

Evidence

Resolution of prior 63868987c853 code blockers

  • Early VM-slot reservation (39f1f067e77a, file.rs:68-72, 675-677): Resolved. reserve_vm_slot(vm_xa.get_ref())? now runs before admit_vm(), gpu.new_vm_for_file(), and with_memory_client(). Refused VM_CREATE past 64 VMs returns EBUSY before building or tearing down any VM state, and any later error unwinds resv cleanly. Covered by new KUnit suite asahi_vm_table (a_file_holds_at_most_max_vms_per_file).
  • GPUVA node memacct accounting (df9812344d81, memacct.rs, mmu.rs, mmu/bind.rs, file.rs): Mostly solid. VmOpResources::with_gpuvas() reserves GPUVA_BYTES up front; sm_step_map() moves ctx.preallocated_charge() into VmGpuVa only at op.insert() after all fallible steps so failed maps leave zero phantom charge; VmGpuVa::clone() drops _charge to None for snapshots; whole/truncating unmaps never fail on limits (for_unmap() takes no charge and sm_step_remap() uses transfer_copy() before dropping remap_ret.unmapped_va); Vm::drop() unifies untracked G13/G14 VMs onto allocation-free unmap_user_ranges() -> unmap_all(VmInner::unmap_gpuva) (verified all VMs use DRM_GPUVM_IMMEDIATE_MODE, kernel_alloc_node is skipped, and kernel mappings use mm::Node). One remaining edge case noted in Blockers above (unmap_prepared_cleanup hole-punch split under drain_mappings()).
  • Page-table charging (ee1f1a60c453, pgtable.rs): Unchanged from 63868987c853; allocates with GFP_KERNEL_ACCOUNT once charged, cleans PoC, reserves driver_tables, charges last (charge.try_add()), then records via push_within_capacity().

Pre-PR Checklist (§1–§6)

  • §1 No model gating: Pass — applies uniformly across all GPU families with no chip/SoC/board/firmware gates.
  • §2 Production-ready, no hacks: Hold for the unmap_prepared_cleanup() deferred split-charge bug above; otherwise clean (default cmdline, no new module params, no sleeps/retries/lab paths).
  • §3 Safety: Pass — no hardcoded hardware safety constants; MAX_VMS_PER_FILE = 64 is a per-file DoS/shrinker bound matching the existing per-file memacct model.
  • §4 Clean-room & attribution: Fail on PR body (carries an AI-attribution footer; recent comments also carried footers). Commits pass: all 3 commits carry human author/committer (Jeff Queisser) and human Reported-by: / Suggested-by: / Signed-off-by: trailers only; no copyrighted Apple material.
  • §5 Evidence & PR structure: Fail — PR body lacks required **What:** / **Tested:** / **Risk:** / **Not yet:** headers and still reflects 63868987c853. J775D (M5 Ultra, ALARM, no SELinux) exact-head A/B row at 39f1f067e77a on bare integration/aurora 5746ef8a5afd (published aura f13056d95af6) passed in comment 7121659967 (+512.3 MiB bind charge/release, 64-VM EBUSY, 571/57/0 Borealis render CTS, 0 new failed units / 0 new kernel errors), and kunit-vz passed 399/401 (2 baseline skips; asahi_memacct 12/12, asahi_pgtable_charge 3/3, asahi_gpuva_charge 3/3, asahi_vm_table 1/1). Cross-family G13/G14 exact-head row with SELinux enforcing tuple is still missing.
  • §6 Naming: Pass — uses Borealis and aura throughout.

Commits & Mergeability

  • ee1f1a60c453: drm/asahi: charge a VM's page tables to its client's memory limit
  • df9812344d81: drm/asahi: charge GPU mapping records to the client and the memory cgroup that create them
  • 39f1f067e77a: drm/asahi: allow at most 64 VMs per file, so VM memory and reclaim work stay bounded
  • 3-way merge check vs main (a9cd131e8207): file.rs, memacct.rs, mmu/bind.rs, pgtable.rs merge cleanly; drivers/gpu/drm/asahi/mmu.rs conflicts only at EOF (mod gpuva_charge_tests vs FirmwareWindow).

CI

  • host-tests: SUCCESS on 39f1f067e77a (runs/45295968115/job/138116291384).
  • Note: after rebasing onto main, pr-text will also enforce Tested:/Risk: and zero AI attribution in the PR body.

Non-blocking Notes

  • Commit df9812344d81 message mentions "including kernel maps into a client VM" for GPUVA node accounting, whereas map_in_range_with_mode() / map_at_with_guard_size() / map_io() use mm::Node rather than drm_gpuva (they only allocate page tables, covered by commit 1). Minor wording nit only.

@github-actions github-actions Bot removed the merge-conflict Conflicts with the base branch (bot) label Nov 24, 2026
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) changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants