Repository navigation
drm/asahi: evict idle GPU backing past a ceiling on held memory - #3685
jgoyvaerts wants to merge 10 commits into
Conversation
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>
|
J700 (Neo, G17P, 7.3 GiB) boot row at head Kernel: one trial boot (boot
Results
Not covered:
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
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.
|
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>
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>
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>
|
Merge readiness at
Areas: 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>
DjDeveloperr
left a comment
There was a problem hiding this comment.
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.
mainnow 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, whichcan_reclaim()refuses.
Found, and fixed on the branch
c6468ddcb987: an object with some pages already held was counted whole against the ceiling. Only the missing part counts now.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, andasahi.mem_limit_pctstill refuses nothing.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
ba10491fande8c9a1aedon't carry, since the limits changed. - We pushed last, so someone other than DjDeveloperr has to approve our commits.
RyanTheTide
left a comment
There was a problem hiding this comment.
The new quiet interval can still admit a duplicate full walk under concurrent binds. A bind can pass the NEXT_LOOK check and be descheduled before try_lock. If another bind then scans, comes up short and publishes the quiet deadline, the first bind can resume, acquire the lock and immediately scan again. I reproduced two walks in one quiet interval using the exact evict_idle body with a controlled pause. A deadline check after acquiring the existing lock would enforce the intended interval.
|
J773g (Apple M4 Mac mini, T8132, G16G B0, 24 GB RAM, os-fw 27.2) row at head
|
evict_idle() checks NEXT_LOOK before it takes the list lock. A bind can pass that check and be scheduled out before try_lock(). If another bind then looks, comes up short and sets NEXT_LOOK, the first one takes the lock and walks every mapping of every user VM again at once, although nothing it could release is idle yet. NEXT_LOOK is stored with the lock held, so check it again after taking the lock. The check before the lock stays: it keeps binds within the quiet time off the lock. Signed-off-by: DjDeveloperr <hey@djdev.me>
|
Reply to this review: confirmed, and fixed the way you suggested. Thanks.
Earlier rows
|
There was a problem hiding this comment.
Verdict: MERGE at 7ae56e5
Checked: The original review's overlapping-bind case now rechecks the quiet deadline while holding the mutex that protects its update. In a deterministic host run of the exact eviction routine, two overlapping binds produced one idle walk during the quiet interval, and a bind after expiry produced the next walk. Removing the new recheck led to two walks during the quiet interval. The held-ceiling suite passed and detected all seven mutations. Hosted Apple build, host tests and Asahi KUnit passed at this head. The existing J773g row names the earlier head 3cf6a87.
Follow-ups: Please keep future changes of this size split into independently reviewable patches where practical.
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) no longer apply to VM-private buffers on G17 and M5, which idle reclaim can release; by default they stay on everything it never releases (all objects on the other backends, and GEM_USERPTR pins, shareable objects, page tables, GPUVA records and client kernel memory everywhere). It reports this asDRM_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 withasahi.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: VM-private buffers on G17 and M5 have no default per-client or device limit, everything else keeps the 67 % / 50 % shares (counted separately), and
asahi.mem_limit_pctonly moves the idle-reclaim threshold, never refusing.Not yet: hardware at the current head: the rows above are from before the limits on unreclaimable memory were added. Also 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:
asahi.mem_limit_pctset a refusing device limit.mmu::idle_vm_reclaim_feature()and reported by G17 and M5, the backends whosenew_vmgives user VMs a residency gate (Vm::with_status()), with KUnit and host coverage.asahi.mem_limit_pctonly moves the threshold and never refuses (configured()). Refusal stays withasahi.client_mem_limit_pct, thegpu_mem_limit_pctsysfs 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()callsttm_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_residentfrommemacct: 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 callsmmu::evict_idle(excess), then populates.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 askedreclaim_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.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.ctry_charge_memcggoes tonomembeforemem_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.
asahi.mem_limit_pctasahi.client_mem_limit_pctgpu_mem_limit_pct(sysfs)On G17 and M5 (bit 49 set) with no limit set, GET_PARAMS reports
memory_limit0 andclient_memory_limit0; the other backends report the limits on unreclaimable memory, which there is all of it. 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
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.pyfails on main (no populate on the bind path) and passes at head. Its mutationsskip-populate,no-unregister,late-unregisterandrefuseare each caught.Hardware row, m4.223 (J616s M4 Pro, 48 GB), 2026-12-02 14:15-14:21 UTC:
asahi.mem_limit_pct=25: dmesgasahi: GPU memory: per-client limit 0 MiB (0 = none), idle reclaim above 11910 MiB held (0 = disabled); gpu_memory held_limit 12488880128.refused0 -> 0; no asahi failure lines; 0 recoveries.