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:integration/aurorafrom
funkzy:pr/asahi-pagetable-charge
Open

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

Conversation

@funkzy

@funkzy funkzy commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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.

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), 27.0 firmware, integration/aurora cc14570 with #1973 and our other three open drm/asahi PRs, default command line, one boot (the kernel also carries a local fix for an appledrm oops at boot on current integration, not part of this PR). A bind of one page over 1 TiB is charged to the client in fdinfo, the GPU memory counters and its memory cgroup (512.2 MiB each, the size of the tables) until the VM is destroyed, then all three return to their starting values; a sparse-tile bind reads the same. The 65th VM in one file is refused with EBUSY. The render CTS subset gives 571 Pass and 57 NotSupported, as on the same base without the series. KUnit (366 of 368 pass, two skips as on the base) and the upstream host tests (436 of 436 on base and head, no result differs) pass.

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 with and without the series) and 0 new kernel error lines against the boot without the series.

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, with and without the series (the boot without it is integration/aurora cc14570 with #1973):

  • Single-page bind of one page over 1 TiB (32768 leaf and 16 level-1 tables, 512.25 MiB): after the bind fdinfo +512.2 MiB, GPU memory counters +512.2 MiB committed and +512.3 MiB resident, memory cgroup +511.0 MiB; the same after the unbind; after VM_DESTROY -0.1, -0.1 and -1.9 MiB. With SPARSE_TILE: +512.2, +512.2 and +513.0 MiB, then back to within 1.1 MiB.
  • VMs in one file: 64 created, then EBUSY.
  • Render CTS subset (628 cases): 571 Pass, 57 NotSupported, with and without the series.
  • KUnit: 79 suites, 366 of 368 cases pass, 2 skipped (75 suites, 353 of 355, on the base); asahi_pgtable_charge 3 of 3, including the new a_refused_table_is_neither_charged_nor_recorded.
  • Host tests: 436 of 436 on base and head, no result differs.
  • Kernel log and units: kernel lines compared after removing timestamps and numbers; the series adds no error line beyond the intended ENOMEM refusals of the M5 render tests run in the same boot. The failed units are iwd.service and systemd-networkd-persistent-storage.service in 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
Contributor

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)

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>
…es them

Each mapping in a VM is recorded in a GPUVA node until it is unmapped,
and those nodes were not charged anywhere.

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.

This applies to every GPU family.

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
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. 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. Mesa creates one VM per file.

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 22b9bb2 to 6386898 Compare November 21, 2026 19:14
@funkzy

funkzy commented Nov 21, 2026

Copy link
Copy Markdown
Contributor 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
Contributor

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
Contributor

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)

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.

3 participants