Skip to content

drm/asahi: evict idle GPU backing past a ceiling on held memory - #3685

Open
jgoyvaerts wants to merge 9 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-held-ceiling
Open

jgoyvaerts wants to merge 9 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-held-ceiling

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What: GPU memory is bounded the way TTM bounds it. Past a device-wide ceiling on pages the GPU holds (half of RAM by default, TTM's rule), a user VM bind first evicts the idle backing of user VMs, then populates anyway. Only a real allocation failure fails the bind: GEM pages are __GFP_RETRY_MAYFAIL, so that is ENOMEM, never the OOM killer. The fixed shares of allocated bytes (67% device, 50% per client) become opt-in and are off by default. It reports this as DRM_ASAHI_FEATURE_IDLE_VM_RECLAIM (uAPI bit 49) on the backends whose user VMs reclaim (G17, M5), so Borealis can drop its own free-memory reserve.
Tested: J616s M4 Pro at e8c9a1a, as a backport on test kernel m4.223 (identical apart from the asahi: prefix our tree adds to three boot-log lines; booted 2026-12-02 with asahi.mem_limit_pct=25): GET_PARAMS features has bit 49; an idle Vulkan client's 4 GiB was evicted when another populated 10 GiB past the 11.9 GiB threshold, then restored at its next submission with its contents intact; nothing refused; 0 recoveries. dEQP-VK (dq-fast, 11,851 cases of the all and mem lists, 4 jobs) on that boot: 0 differ from the system Mesa's baseline. KUnit (arm64 qemu) at e8c9a1a: 952 passed, 1 skipped; host tests: only main's own failures.
Risk: one-way door (uAPI: feature bit 49) · all Macs running the driver; reclaim and the bit only on G17 and M5. User-visible defaults change: no per-client or device limit is set by default, and asahi.mem_limit_pct only moves the idle-reclaim threshold, never refusing.
Not yet: a game run past the threshold (the ceiling itself has been in our test kernels since m4.214 at the default half of RAM).

Details

Commits:

  1. Jonathan Goyvaerts: the ceiling and the eviction.
  2. DjDeveloperr: bit 49 on G17, and evict_idle try_locks the eviction list so a bind never waits behind another walk. It also had an explicit asahi.mem_limit_pct set a refusing device limit.
  3. Jonathan Goyvaerts: bit 49 decided in mmu::idle_vm_reclaim_feature() and reported by G17 and M5, the backends whose new_vm gives user VMs a residency gate (Vm::with_status()), with KUnit and host coverage.
  4. Jonathan Goyvaerts: bit 49's comment in the house form (name first, standard closing sentence).
  5. Jonathan Goyvaerts, follow-up to commit 2 on the owner's rule: asahi.mem_limit_pct only moves the threshold and never refuses (configured()). Refusal stays with asahi.client_mem_limit_pct, the gpu_mem_limit_pct sysfs file and the memory cgroup. Bit 49's comment says so instead of "An explicit allocation limit still applies".

Why this shape. No driver checks free memory when a buffer is created. TTM, i915, msm, panfrost, WDDM and macOS all do two things instead: they let the real allocation fail after the driver has evicted its own idle buffers, and they bound how many pages are held at once. TTM's bound is ttm_pages_limit, half of RAM by default (ttm_global_init()). ttm_tt_populate() calls ttm_global_swapout() while over it, and allocates anyway once nothing is left to swap. The ceiling here is the same: an eviction trigger, never a refusal.

The ceiling. The ceiling is checked against user_resident + kernel_resident from memacct: everything the GPU holds, user and kernel objects alike. Ledger::populate() runs at the one place a user object's pages are populated for the GPU, held_user_sg_table(), which both VM_BIND paths call before taking any GPUVM lock. Past the ceiling it calls mmu::evict_idle(excess), then populates.

  • What is never checked: objects already held, imports and user-memory objects.
  • Counted but never evicting: a submission's restore of an evicted VM, kernel objects and CPU mapping faults. Refusing those would lose the device.

Eviction. mmu::evict_idle() walks a device-wide list of the VMs that have shrinkers. A VM joins the list when its shrinkers are registered and leaves it before they are freed. Each VM is asked reclaim_idle_pages(target, false): what its first shrinker releases under memory pressure, which is only VMs idle for 2 s with no lease. Objects userspace asked to keep resident (GEM_RESIDENCY) stay.

  • Everything is taken with try-locks, so a bind that evicts can't wait on another VM.

Where the bind can still fail. Only in the page allocator or the memory-cgroup charge. Both fail with ENOMEM under __GFP_RETRY_MAYFAIL (memcontrol.c try_charge_memcg goes to nomem before mem_cgroup_oom). Borealis turns a VM_BIND ENOMEM during BO allocation into VK_ERROR_OUT_OF_DEVICE_MEMORY (agx_device.c, hk_device_memory.c). Gallium returns a NULL BO, and some of its callers do not check for it (agx_batch.c, pool.c). That is unchanged by this PR.

Knobs.

knob default what it does
asahi.mem_limit_pct 0 where eviction starts: 0 is half of RAM, 1-99 is that share, 100 or more is never
asahi.client_mem_limit_pct 0 opt-in per-client limit, refusing; 0 is none
gpu_mem_limit_pct (sysfs) unchanged still sets both refusing limits, for tests

With no limit, GET_PARAMS reports memory_limit 0 and client_memory_limit 0. Borealis then sizes its Vulkan heap at half of RAM (hk_resolve_uma_sysmem_heap()), the size the old 50% client limit gave it.

What it changes for the dq-fast OOM. Every dEQP device is active, so there is nothing idle to evict there. The ceiling helps by releasing background clients' idle VMs early. The bind failing with ENOMEM at the memory cgroup's or the system's real limit is what reaches the dEQP clients, as OUT_OF_DEVICE_MEMORY, not as an OOM kill.

Related: #3683 charges G17 queue objects to their client; independent of this PR.

Evidence
  • KUnit asahi_memacct:
    • past_the_ceiling_idle_backing_is_evicted_and_population_proceeds: within the ceiling nothing is evicted; past it the excess is asked of idle backing, which releases it, and population runs; with nothing idle it still runs; population's own ENOMEM fails it, with 0 refusals counted; no ceiling, no eviction;
    • the_held_ceiling_is_half_of_ram_by_default: 8, 16, 24, 48 and 64 GB, plus the override;
    • only_unheld_user_objects_add_held_memory.
  • tools/asahi/asahi-held-ceiling-tests.py fails on main (no populate on the bind path) and passes at head. Its mutations skip-populate, no-unregister, late-unregister and refuse are each caught.

Hardware row, m4.223 (J616s M4 Pro, 48 GB), 2026-12-02 14:15-14:21 UTC:

  • Boot with asahi.mem_limit_pct=25: dmesg asahi: GPU memory: per-client limit 0 MiB (0 = none), idle reclaim above 11910 MiB held (0 = disabled); gpu_memory held_limit 12488880128.
  • GET_PARAMS features 0x4003197a6001dc37: bit 49 (IDLE_VM_RECLAIM) set; memory_limit 0, client_memory_limit 0.
  • Ceiling row, in a memory-capped unit: client A holds 4 GiB and goes idle; client B populates 10 GiB past the threshold. A's drm-resident-system went 4,328,108,512 -> 32,895,456 bytes (evicted); B finished; A then verified its contents through the CPU mapping and submitted again: "restored ok". Second pass, B alone past the threshold: finished. gpu_memory refused 0 -> 0; no asahi failure lines; 0 recoveries.
  • dEQP-VK with dq-fast on the same boot, system Mesa (fbdc69663) against its stored baseline: all (7106) and mem (4745) lists, 0 differ, 0 recoveries.

jgoyvaerts and others added 2 commits November 30, 2026 01:18
GEM pages the GPU holds are pinned: unevictable, and released only by
the driver's own idle reclaim. Two things bounded them:
- **Fixed limits:** 67% of RAM per device and 50% per
  client, counted on allocated bytes. A fixed share of allocated bytes
  is not how other drivers bound GPU memory, and it caps a game at half
  of RAM.
- **Global reclaim:** the per-VM shrinkers release an idle VM's backing
  in step with the rest of reclaim. That is interleaved with swapping
  out other processes.

On an M4 Pro (48 GB), a dq-fast timeline-cases run at -j10 had clients
whose GPU memory, together with the desktop, used up RAM and swap.
systemd-oomd then killed the desktop's apps, while the driver refused
nothing.

Bound held memory the way TTM does instead:
- **The ceiling:** the bytes the GPU holds at once, user and kernel
  objects together, have a device-wide ceiling. It defaults to half of
  RAM, TTM's default limit on the system pages GPU objects may hold
  (ttm_global_init(), ttm.pages_limit).
- **Eviction first:** a user VM bind that populates an object past the
  ceiling first has the idle backing of user VMs released, up to the
  excess, as their shrinkers would release it under memory pressure.
  This uses a device-wide list of the VMs that have shrinkers. Objects
  userspace asked to keep resident are left alone.
- **No refusal:** the bind then populates whatever eviction freed. As
  in ttm_tt_populate(), which calls ttm_global_swapout() while over
  ttm_pages_limit and allocates anyway once nothing is left to swap,
  the ceiling only starts eviction.
- **Where a bind still fails:** only where the page allocator or the
  memory cgroup fails it. GEM shmem pages are allocated with
  __GFP_RETRY_MAYFAIL | __GFP_NOWARN (drm_gem_shmem_init()), so both
  return ENOMEM rather than invoke the OOM killer, and Borealis reports
  VK_ERROR_OUT_OF_DEVICE_MEMORY.
- **Not checked:** restoring an evicted VM at its next submission,
  kernel objects and CPU mapping faults. They count towards the ceiling
  but evict nothing.

The fixed limits become opt-in:
- **asahi.client_mem_limit_pct** now defaults to 0, no limit.
- **asahi.mem_limit_pct** now sets where eviction starts: 0, the
  default, is half of RAM, and 100 or more means never. It no longer
  sets a device limit that refuses.
- **gpu_mem_limit_pct** (sysfs) still sets both refusing limits, for
  testing.

With no limit, GET_PARAMS reports 0 and 0. Borealis then sizes its
Vulkan heap at half of RAM (hk_resolve_uma_sysmem_heap()), the size the
old 50% client limit gave it. gpu_memory reports the ceiling as
held_limit.

Tests:
- **KUnit:** under the ceiling nothing is evicted; past it the excess is
  asked of idle backing and population runs; with nothing idle it still
  runs; only population's own ENOMEM fails it, and that counts no
  refusal; the ceiling is half of RAM at 8, 16, 24, 48 and 64 GB.
- **Host test** (asahi-held-ceiling-tests.py): a user bind populates
  through the ceiling; a VM's shrinker owner joins the eviction list and
  leaves it before its shrinkers are freed; the ceiling refuses
  nothing. Each is checked by a mutation. g15-probe-init-tests also
  checks that the eviction list is ready before the adapters register.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Contributor

J700 (Neo, G17P, 7.3 GiB) boot row at head ba10491f

Kernel: one trial boot (boot 1abbcccc). Stock Fedora -41 was restored and verified afterwards.

Results

  • GET_PARAMS: bit 49 (IDLE_VM_RECLAIM) is set, bit 48 is clear. memory_limit and client_memory_limit read 0 and 0: no refusing limits by default.
  • gpu_memory: held_limit 3,917,193,216 bytes, half of this Neo's RAM; limit and client_limit 0; refused 0 throughout.
  • Explicit cap: writing gpu_mem_limit_pct = 40 set both refusing limits to 3,133,754,572 bytes (40 %) in GET_PARAMS and gpu_memory. mem.buffer (4 KiB-64 MiB allocate/bind/fill/free) still ran under it. Writing back 0 restored the defaults, 0 and 0.
  • Normal submits all completed with correct output:
    • q4 render triangle 42.4 µs and compute.light d1/d16, readbacks ok
    • 4-queue render; 4-queue and 4-process compute
    • concurrent render + compute
    • cross-queue semaphores; queue churn (CRC ok)
  • Multi-queue CTS: sync2 16,680 Pass / 7,080 NotSupported / 0 Fail; sync 12,798 Pass / 9 NotSupported / 0 Fail. Identical to the other boots.
  • Two clients in separate cgroups, run, closed and then reused: kernel_resident 308 → 453 → 298 MB six seconds after close. user_resident went back to its starting level.
  • Health: 0 WARN, 0 GPU faults, 0 AVC, 0 failed units. The only flagged line is the out-of-tree taint from building the module with M=.

Not covered:

DjDeveloperr
DjDeveloperr previously approved these changes Dec 1, 2026

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

Reviewed ba10491. An explicit device cap reaches admission, and a competing bind skips an active reclaim walk. The seven targeted checks passed. The remaining rows cover G17 eviction and restoration with bit 49 set, plus the explicit cap on G16 with the bit clear.

@DjDeveloperr

Copy link
Copy Markdown
Contributor

Restored explicit device memory caps and exposed G17 idle-VM reclaim as feature bit 49 in ba10491.

DRM_ASAHI_FEATURE_IDLE_VM_RECLAIM (bit 49) says the kernel attempts
idle-VM reclaim before populating new user backing. Userspace can then
drop reserves of its own against free memory. G17 reported it inline
whenever the held-memory ceiling is set. M5 user VMs get the same
residency gate and shrinkers from `Vm::with_status()`, so a bind there
evicts idle backing the same way, but M5 did not report the bit.

Decide it in one place, `mmu::idle_vm_reclaim_feature()`, next to the
eviction it describes:
- it reports the bit while the ceiling is set;
- G17 and M5 call it from get_params;
- every backend without user-VM reclaim reports nothing.

Tests:
- **KUnit:** the bit is set only with a ceiling.
- **Host test** (asahi-idle-vm-reclaim-feature-tests.py): every backend
  whose new_vm reaches `with_status()` reports the bit, and no other
  backend does. Three injected faults are caught.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Every feature bit defined outside enum drm_asahi_feature opens its
comment with its name and closes with "A feature bit like those of enum
drm_asahi_feature, defined here like bit 32." Bit 49 had neither. Add
both, keeping the description as it was, so userspace headers mirror one
settled text.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
jgoyvaerts added a commit to jgoyvaerts/mesa that referenced this pull request Dec 1, 2026
Copy the DRM_ASAHI_FEATURE_IDLE_VM_RECLAIM comment word for word from
aurora-silicon/linux#3685 (86241547ea66, include/uapi/drm/asahi_drm.h
lines 628-635). Comment only.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
"drm/asahi: honor explicit device limits alongside idle VM reclaim" had an
explicit asahi.mem_limit_pct set two things: the held-memory threshold
where idle reclaim and eviction start, and a device limit that refuses
GEM objects past the same percentage. The owner's rule (11-30) is that
it only sets where reclaim starts and never refuses: past the threshold
the kernel reclaims idle backing and then allocates, as TTM does past
its pages limit.

Keep the threshold and drop the refusal:
- `configured()` derives both settings: no device limit from the
  percentage, the threshold from it as before. Its default (0) is
  unchanged: half of RAM, no device limit.
- **What still refuses:** asahi.client_mem_limit_pct (opt-in per client),
  the gpu_mem_limit_pct sysfs file (sets device and client limits), and
  the caller's memory cgroup.
- **Bit 49's header comment** says so plainly, in place of "An explicit
  allocation limit still applies."

The other parts of that commit stay: evict_idle's try_lock, and bit 49
on G17.

Tests:
- **KUnit:** an explicit percentage moves the threshold, sets no limit,
  and a population past it asks for eviction and is refused nothing. The
  per-client limit stays the one that refuses.
- **Host test** (asahi-held-ceiling-tests.py) now requires the percentage
  to reach only the threshold; the mem-pct-refuses fault is caught.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
jgoyvaerts added a commit to jgoyvaerts/mesa that referenced this pull request Dec 2, 2026
Copy the DRM_ASAHI_FEATURE_IDLE_VM_RECLAIM comment word for word from
aurora-silicon/linux#3685 (e8c9a1aed023, include/uapi/drm/asahi_drm.h
lines 628-636): the held-memory threshold itself never refuses an
allocation. Comment only.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
@jgoyvaerts
jgoyvaerts marked this pull request as ready for review December 2, 2026 14:22
@jgoyvaerts
jgoyvaerts requested review from a team as code owners December 2, 2026 14:22
@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:one-way Revertible or not, from Merge risk (bot) merge-conflict Conflicts with the base branch (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Merge readiness at 3cf6a87e: not ready yet

Check Detail
CI at head ok 9 checks passed
Review no approved by DjDeveloperr, but GitHub does not count it yet (code owner from Core or Omarchy Mac needed, or approval went stale)
Requested changes ok none
Hardware row at head no no line with the head SHA 3cf6a87e, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: one-way · (uAPI: feature bit 49) · all Macs running the driver; reclaim and the bit only on G17 and M5. User-visible defaults change: no per-client or device limit is set by default, and asahi.memlimitpct only moves the idle-reclaim threshold, never refusing.

Areas: area:gpu

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

Conflict in drivers/gpu/drm/asahi/memacct.rs: main counts an object's held
bytes instead of a held flag, since CPU faults in revocable mappings can
hold some of its pages. Keep both: adds_held() compares the held bytes
with the object's size.

Signed-off-by: DjDeveloperr <hey@djdev.me>
CPU faults in a revocable mapping hold single pages of an object, which
are counted as held already. A bind that populates the rest counted the
whole object again, and asked idle backing for more than the bind adds.

Have Charge::adds_held() return the bytes that are not held yet, and pass
those to Ledger::populate().

Signed-off-by: DjDeveloperr <hey@djdev.me>
The held-memory ceiling replaces the default limits (67% of RAM for the
device, 50% per client) with eviction. Eviction only releases objects
private to a user VM that has idle reclaim. Without the limits, nothing
bounds the rest:

- On G13 to G16, G18 and M3, user VMs have no idle reclaim. The ceiling
  evicts nothing there, and one client could take all memory.
- GEM_USERPTR pins are not charged to RLIMIT_MEMLOCK on the grounds that
  the GPU memory limits bound them.
- Page tables, GPUVA records, bound objects, heap blocks and queue
  objects grow with a client's requests and are charged to no process,
  so the OOM killer does not find their owner.

Keep the old default shares for what idle reclaim never releases, counted
separately from the objects it does release. GEM_CREATE already knows
whether the object's VM has idle reclaim: such an object is subject only
to the opt-in limits, as before. asahi.mem_limit_pct still refuses
nothing, and a G17 or M5 game's own buffers have no default limit.
asahi.client_mem_limit_pct, if set, replaces the default per client.

GET_PARAMS reports the limits on a client's objects: none where idle
reclaim is reported, the default shares elsewhere, as without the
ceiling. gpu_mem_limit_pct sets every limit, so 0 still disables all.
gpu_memory shows the new totals.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Past the ceiling, every bind that populates pages looked for idle backing
in every user VM. A look visits every mapping of each VM and holds that
VM's reservation and the UAT lock meanwhile, also for a VM in use, whose
advised contents it may purge. A game past the ceiling with nothing idle
left paid for that at each new buffer.

A VM in use is not idle before RECLAIM_QUIET_NS after its last job. Once
a look has released less than it was asked for, populate right away for
that long before looking again.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@github-actions github-actions Bot added needs-hw-row No hardware row at the PR head (bot) and removed merge-conflict Conflicts with the base branch (bot) labels Dec 3, 2026

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

Reviewed at 3cf6a87e1138: the author's five commits, merged with main 13dc5a79c0ec, plus three small commits of ours. No finding is left in the code. The hardware rows below are owed.

Checked

  • Merge: one conflict. main now counts the held bytes of an object, not a flag (CPU faults can hold single pages); both sides are kept.
  • Bit 49: free on main, in every open linux PR that touches the header (48, 50 and 51 are taken) and in Mesa. Only mesa#1416 uses it, for this.
  • Locks: a bind that evicts takes only try-locks on other VMs and on the list, so it never waits for a busy VM. Its own lease keeps its own VM out of eviction. VM create and close can wait for a running walk; nothing in reclaim takes the list lock.
  • Firmware and an evicted VM: same path as the shrinkers on main. A submit takes its lease before parsing, every accepted command keeps one until exact retirement (queued and held-back ones too), and a failed published job quarantines the VM, which can_reclaim() refuses.

Found, and fixed on the branch

  1. c6468ddcb987: an object with some pages already held was counted whole against the ceiling. Only the missing part counts now.
  2. 5ce50e0ea12f: with the default limits gone, nothing bounded what eviction can't release: every object on G13 to G16, G18 and M3, and on all Macs GEM_USERPTR pins (no RLIMIT_MEMLOCK), shareable objects, page tables, GPUVA records and a client's kernel memory. That memory keeps the old 67 % / 50 % shares, counted separately. A G17 or M5 client's VM-private buffers still have no default limit, and asahi.mem_limit_pct still refuses nothing.
  3. 3cf6a87e1138: past the ceiling, every new buffer walked every mapping of every VM under its reservation, even with nothing idle. A walk that came up short is not repeated for the 2 s a VM needs to become idle.

For the author: please check that (2) fits what you want, and update the PR text ("no per-client or device limit is set by default" is only true for VM-private objects on G17/M5). Bit 49's comment gained a clause, so mesa#1416 needs the new copy.

Ran: arm64 LLVM W=1 of drivers/gpu/drm/asahi/: 0 new warnings (complete main baseline at 5f32aa72fa7e; the one at 13dc5a79c0ec printed the same warnings and was stopped in code generation). A build with KUNIT=y compiles; the asahi_memacct tests pass built for the host with stubs (19/19, without the mm test). KUnit did not run on arm64. Host tests on the merged tree: g17 337/337, m5 114/114, g15 18/18, asahi 14/14, g18 4/4, g16 2/2. A full run just before #3681 landed: 988 ok, 55 not, all 55 because the checkout had no history for their old-commit controls; the GPU ones passed once it had. checkpatch --strict: clean on our commits.

Owed before merge

  • J700 and J714s (G17): idle client evicted past the ceiling and restored with its contents checked, also across a sleep; a busy client never evicted; 0 recoveries; CTS against the recorded base. On J714s also GTA V, with one pass past the threshold.
  • J773g (G16, bit clear): limits equal to base, the same refusal at 50 % of RAM, no OOM kill, CTS and mem.* no-regression.
  • The rows at ba10491f and e8c9a1ae don't carry, since the limits changed.
  • We pushed last, so someone other than DjDeveloperr has to approve our commits.

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:all Macs affected, from Merge risk (bot) door:one-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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants