Skip to content

drm/asahi: hold an object's backing for every holder of its pages - #3193

Merged
RyanTheTide merged 5 commits into
mainfrom
asahi/gem-held-page-array
Nov 29, 2026
Merged

RyanTheTide merged 5 commits into
mainfrom
asahi/gem-held-page-array

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: drm/asahi now charges a GEM object's memory, and keeps compaction off it, whenever anything holds its pages. Before, only a device SG table did that. CPU mmaps, dma-buf pins by other drivers and kernel vmaps go through the generic shmem helpers. Those populate and pin the whole object, but the memory was not counted in the client's drm-resident-system or the creating process's RssShmem. The shmem helpers now report when an object's page array is populated and when it is released, through a new optional pages_changed callback (DriverObject::pages_changed() in Rust). drm/asahi marks the backing held for exactly that span. A KUnit case and two kselftest cases cover it.
Tested: J714s (G17S) passes. The two new selftest cases fail on the stock kernel and pass with the fix, and the whole selftest passes 9/9. Vulkan smoke and the dma-buf CTS show 52 pass, 6 not supported, 0 fail. A dma-heap buffer imports into renderD128 over PRIME. The kernel log has 0 GPU faults and 0 warnings. J773g (G16G) passes the same checks, and its gpu_memory delta is 0. G16 charges objects from creation, so that row shows no regression rather than the fix. The host build with CLIPPY=1 is clean on every changed line. CI passes, and its KUnit run includes the new pages_changed_spans_every_holder.
Risk: two-way door · every Mac with drm/asahi: all shmem-backed GPU objects, user and kernel
Not yet: rows on G13/G14, G15 and Neo

Details

The new pages_changed callback is called under the reservation lock. drm_gem_shmem_get_pages_locked() calls it right after it populates pages, and drm_gem_shmem_put_pages_locked() right after it releases them. Every holder of the array counts in pages_use_count: CPU VMAs other than revocable ones (forked copies too, through vm_open), pins (dma-buf attachments), vmaps and the device SG table. So the callback reports the first holder and the end of the last one. The Rust bindings install it only for drivers that implement DriverObject::pages_changed(), both on objects the driver creates and on objects the DRM core allocates for it.

In drm/asahi, pages_changed() calls set_mapping_held(). Four explicit calls are gone, because each one was already the start or the end of that span:

  • after taking an SG table
  • at kernel object creation
  • after idle release
  • after purge

Idle release and purge both require pages_use_count == !!sgt, so they drop the last holder. held was already idempotent, so an object held through several paths is counted once.

What does not change:

  • Revocable CPU mappings (reclaimable_cpu_mappings, VM-private objects) fault in cpu_pages one page at a time, not the page array. They stay unmarked, as before.
  • Imported objects have no shmem file, and user-memory objects use driver-supplied pages. Neither ever populates the array.
  • An asahi dma-buf imported back into asahi takes no attachment.

Kernel objects now count in kernel_resident from the time their pages are populated, not from creation.

On GPUs whose driver prepares a new object for the device (prepare_new_objects(), G15 and G16), GEM_CREATE already takes an SG table. Their user objects were held and charged from creation to free before this change, and still are. The gap this closes shows on every other GPU, which populates an object on first use.

Kernel vmaps of user objects come only from importers that vmap, and no userspace test can reach them here. The KUnit case pages_changed_spans_every_holder covers vmap and SG table at the shmem layer.

The kselftest cases use a fixture of their own that needs only an asahi render node, so they also run on GPUs without GEM_RESIDENCY:

  • objects.cpu_mappings_hold_the_backing: a one-page mmap of a 1 MiB shared object, a mapping of its dma-buf, and a forked child's copy of the mapping. Each charges the object once, until the last one is gone.
  • objects.foreign_imports_hold_the_backing: two imports of the object's dma-buf into another DRM driver, released after the client closed its handle. It skips where no other DRM driver imports the buffer.

Both cases take what a new object holds by itself as its own share: nothing, or all of it on prepared GPUs.

Thanks to erikwb for the report.

Evidence

Unfixed host kernel (7.3.0-1-28, G14), new cases only:

  • objects.cpu_mappings_hold_the_backing: not ok, Expected resident (1048576) == client_resident(self->fd) (0), and RssShmem stays flat
  • objects.foreign_imports_hold_the_backing: not ok, Expected resident (16384) == client_resident(self->fd) (0). The import into the display driver worked; only the charge is missing.

Host build of rust/, drm_gem_shmem_helper.o and drivers/gpu/drm/asahi/ with the asahi profile and CLIPPY=1: 0 errors. The 43 clippy warnings in the touched files are all on unchanged lines, and the crate's existing warnings are as before.

CI KUnit asahi: 445 tests, 441 passed, 4 skipped. rust_drm_gem_shmem passes 19/19, including pages_changed_spans_every_holder.

checkpatch: 0 errors and 0 warnings on all four commits.

J714s (M5 Pro, G17S B1), see the row comment. It ran a warm native build of main plus this PR and five other PRs that change none of this PR's files. The kernel was booted with the default command line, headless:

  • control, stock kernel: objects.cpu_mappings_hold_the_backing and objects.foreign_imports_hold_the_backing both not ok
  • with the fix: the whole residency program passes 9/9. Both object cases are ok, and the import went into a DCP card. The three asahi.* cases are ok, and the four reclaim cases skip without their env.
  • dEQP-VK.api.smoke.* + dEQP-VK.api.external.memory.dma_buf.*: 52 pass, 6 not supported, 0 fail
  • a 4 MiB dma-heap buffer imported over PRIME, mmapped, checked and freed: ok
  • 0 GPU faults, 0 WARNING:/BUG/Oops lines

J773g (G16G), kernel built from the same kernel tree as the current head, see the row comment:

  • dEQP-VK.api.smoke.* 8/8
  • dEQP-VK.api.external.memory.dma_buf.*: 44 pass, 6 not supported, 0 fail
  • a dma-heap buffer imported with PRIME, mmapped and freed: ok
  • gpu_memory user_resident and user_committed delta 0 / 0
  • 0 GPU faults, 0 WARNING:

The object cases failed there on the earlier selftest. It assumed a new object holds nothing, and on G16 it holds its backing from creation. The current head fixes the test, and on the same kernel both object cases pass (whole program: 2 pass, 7 skip). They also pass on the stock kernel there, since G16 never had the gap. The selftest builds with -Wall and no warnings under gcc and clang.

A driver that accounts the backing of its shmem objects itself sees only
the holders it creates. CPU mappings other than revocable ones, pins
(dma-buf attachments), kernel vmaps and the device SG table all count in
pages_use_count: the page array exists exactly while one of them does.

Add an optional pages_changed callback to the shmem object, called under
the reservation lock right after the page array is populated and right
after it is released.

Signed-off-by: Dj <[email protected]>
Let Rust shmem drivers follow the page array of their objects: install
the shmem helpers' pages_changed callback for a driver that implements
the new optional DriverObject::pages_changed(), on the objects it
creates and on those the DRM core allocates for it.

A KUnit case checks that the callback spans a kernel vmap and an SG
table and reports the release of the last of them.

Signed-off-by: Dj <[email protected]>
The backing of an object was marked held (inaccessible to migration and
charged to its client and creating process) only when the driver took an
SG table for it. The generic shmem helpers populate the same page array
for CPU mappings, dma-buf pins and kernel vmaps, and the backing then
stayed populated and unevictable without being marked or charged.

Follow the page array instead: implement DriverObject::pages_changed(),
which marks the backing held when the array is populated and releases
it once the last holder of the array is gone. The explicit transitions
after taking an SG table, creating a kernel object, releasing idle
backing and purging are covered by it and are removed.

Signed-off-by: Dj <[email protected]>
@DjDeveloperr

DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Update at f95b7ec63651: PASS. Since 6c44806e34b7 this PR only merged main. Its own 8 files are byte-identical: gem.rs, memacct.rs, mmu/residency.rs, drm_gem_shmem_helper.c/.h, the two Rust GEM files and residency.c.

How it was run: one guarded one-shot of main 255761f1d55f plus this PR, plus #3209 and #3227. No files overlap, and this PR's files are identical to its head.

  • residency, built from f95b7ec63651:
    • objects.cpu_mappings_hold_the_backing: ok.
    • objects.foreign_imports_hold_the_backing: ok.
    • asahi.*: 7 skip.
    • Totals: pass 2, fail 0, skip 7.
  • CTS: dEQP-VK.api.smoke.* 8/8. dEQP-VK.api.external.memory.dma_buf.* 44 pass, 6 not supported, 0 fail.
  • gpu_memory: user_resident and user_committed delta 0 / 0 across the residency program and the dma_buf CTS.
  • Health: 0 GPU faults, 0 WARNING: lines, 0 failed units, and no drm/asahi or drm_gem_shmem errors. The error-level lines are the same set as on the next boot of the same main without this PR's code paths. They all also show on main b12781353833.
  • Not repeated: the dma-heap PRIME import, because its helper wasn't staged this time. It passed at the old head, and the code is unchanged.

The G16 note below still applies: on this Mac the two object cases can't tell a fixed kernel from an unfixed one.


Earlier, at 6c44806e34b7:

J773g (M4 Mac mini, G16G) at 6c44806e34b7: PASS.

The kernel was built from 9183aca12081. That tree is identical to this head outside tools/testing: git diff --name-only lists only residency.c, and drivers/ and include/ are the same trees. Guarded one-shots on the default cmdline.

residency, rebuilt from 6c44806e34b7, on that kernel:

  • objects.cpu_mappings_hold_the_backing: ok.
  • objects.foreign_imports_hold_the_backing: ok.
  • asahi.*: all 7 skip ("no DRM_ASAHI_FEATURE_GEM_RESIDENCY"), as before.
  • Totals: pass 2, fail 0, skip 7. Run as the two named cases and as the whole program.

From the first boot of the same kernel (no need to repeat, since the kernel is unchanged):

  • dEQP-VK.api.smoke.*: 8/8.
  • dEQP-VK.api.external.memory.dma_buf.*: 44 pass, 6 not supported, 0 fail.
  • PRIME import: a /dev/dma_heap/system buffer imported into renderD128 with PRIME_FD_TO_HANDLE, mapped through the GEM mmap offset, then freed. ok.
  • gpu_memory: user_resident and user_committed delta 0 / 0.

Health on both boots: 0 GPU faults, 0 WARNING: lines, 0 failed units, and no drm/asahi or drm_gem_shmem errors. The error-level lines are the same set as on the control kernel.

Note for G16: the two object cases at this head also pass on the stock kernel 7.3.0-ARCH-m4mainefed1ae3, which doesn't have the fix. G16 prepares a new object's backing at creation, so the object is charged from GEM_CREATE until its handle closes. On this Mac these tests can't tell the fixed kernel from the old one. The test at 9183aca12081 failed on stock, on a main 61c7b88aca91 kernel and on the fixed kernel alike, because of that same creation-time charge.

Check that a CPU mapping of an object, a mapping of its dma-buf, the
copy of a mapping that a forked child inherits and the imports of its
dma-buf by another DRM driver each have the object charged to its client
(drm-resident-system) and to its creating process (RssShmem), once, until
the last of them is released. These cases need only an asahi render node.

Where the driver prepares a new object for the device, that holds the
backing from creation; the cases then expect the charge until the
object is freed.

Signed-off-by: Dj <[email protected]>
@DjDeveloperr
DjDeveloperr force-pushed the asahi/gem-held-page-array branch from 9183aca to 6c44806 Compare November 26, 2026 07:02
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at 6c44806e34b7: ready

Blockers: none

Checked:

  • The callback sits at the only two places the page array changes. drm_gem_shmem_get_pages_locked() calls it after the array is populated, and drm_gem_shmem_put_pages_locked() after it is released, both under the reservation lock. Every holder goes through them: a CPU mmap, a forked copy (vm_open only raises the use count), a dma-buf attach pin, a vmap and the SG table.
  • Removing the four explicit calls loses nothing. Idle release and purge both drop the last holder through put_pages_locked(), because both require pages_use_count == !!sgt. Kernel objects are marked when mapped_sg_table() first populates them. An object whose array was never populated was never marked before either.
  • Free order is safe. The Rust free_callback() calls drm_gem_shmem_release() before it drops the driver data. So the release-time callback still sees a live charge, and filp too, which drm_gem_object_release() drops only after the put. Charge::drop() then finds nothing left to undo.
  • Locking. The callback does atomics and add_rss_counter() only: it takes no reservation lock and no sleeping lock. Nothing else in drm/asahi reads held to make a decision, and Charge::transfer() takes &mut and is called only from tests.
  • Objects the DRM core allocates (create_object_callback()) get the callback before they are published, and drm_gem_shmem_init() does not reset it. The C drivers that use shmem allocate their objects zeroed, so for them nothing changes.
  • Merged-code rules: no runtime switch, no new constant, no SoC or board gate.
  • It closes the gap the PR describes. A one-page mmap of a 1 MiB object, a dma-buf mmap, a forked copy and foreign imports now each charge the object to its client and its creating process, once.

The merge gate still needs the G17 row listed under "Not yet". This is shared drm/asahi and shmem-helper code, so a second generation is required next to J773g. That is not a code issue.

Evidence
  • Build at 6c44806e34b7 with the asahi profile (LLVM, Rust): rust/, drivers/gpu/drm/drm_gem_shmem_helper.o and drivers/gpu/drm/asahi/, exit 0. The warnings are all on unchanged lines. None is in gem.rs, shmem.rs, mod.rs, residency.rs, drm_gem_shmem_helper.c or the changed lines of memacct.rs.

  • checkpatch --strict: 0 errors, 0 warnings, 0 checks on all four commits.

  • The selftest builds with -Wall under gcc and clang with no warnings.

  • The new cases on an unfixed kernel (this host: G14, 7.3.0-1-28), residency -f objects: 0/2 pass, failing exactly on the missing charge.

    • cpu_mappings_hold_the_backing: Expected rss (1146880) == rss_shmem() (98304)
    • foreign_imports_hold_the_backing: Expected resident (16384) == client_resident(self->fd) (0)

    So the cases catch the gap.

  • CI KUnit asahi at this head: 464 run, 460 pass, 4 skipped. rust_drm_gem_shmem passes 19/19, including pages_changed_spans_every_holder.

  • The J773g row at this head is in the earlier comment.

@DjDeveloperr

DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro, G17S B1) row at 6c44806e34b7: PASS.

  • Kernel: 7.3.0-m5-rows-sec2, a warm native build of combined candidate 3e2c44440978. That's main 40e5e29cc915 plus this PR and accel/ane: h16: check admission before importing the HWX and account the copy #3194, dmaengine: apple-sio: hold tags and ended transfers until the firmware is done with them #3195 (fb1af1e4867f), drm/asahi: g17: bound submit syncs, install repeated timeline outputs once, return closed queues' identities at close #3200, wip/media-bounds-rows 9e764ae65462 and ASoC: apple: mca: stop and sync capture DMA before freeing the buffer #3196 (MCA). Every merge was conflict-free, and none of this PR's 8 files differ from 6c44806e34b7 in the candidate.
    • The kernel sources match 9183aca12081; only residency.c is new.
    • Booted with the default command line, headless, and restored the default kernel afterwards.
  • Selftest (tools/testing/selftests/drivers/gpu/asahi-residency from this head, built with the candidate's headers_install):
    • Stock 7.3.0-m5-cdirect16, -r objects.cpu_mappings_hold_the_backing -r objects.foreign_imports_hold_the_backing: both not ok (0/2), as expected.
    • Candidate, whole selftest: 9/9. objects.cpu_mappings_hold_the_backing ok, objects.foreign_imports_hold_the_backing ok (it imported into a DCP card), the three asahi.* cases ok, and the four reclaim cases skipped without their env.
  • Vulkan:
    • vulkaninfo --summary: Apple M5 Pro (G17S B1), Borealis.
    • dEQP-VK.api.smoke.* + dEQP-VK.api.external.memory.dma_buf.*: 58 cases, 52 Pass, 6 NotSupported, 0 Fail.
  • dma-heap/PRIME: a 4 MiB /dev/dma_heap/system buffer was imported into renderD128 (PRIME_FD_TO_HANDLE), mmapped and written/read back with 0 mismatches, then closed and freed. All steps ok.
  • gpu_memory: the counters are device-wide and the desktop session's own GPU clients run at the same time, so a delta of exactly 0 can't be isolated. user_resident/user_committed went 1,628,422,144 → 1,880,457,216 during the CTS and dma-heap steps, then 1,518,125,056 at the end of the test phase, below the start. Nothing was left charged.
  • Kernel log, test boot: 0 GPU faults or recoveries, 0 WARNING:/BUG/Oops lines, and no drm/asahi or drm_gem_shmem error lines.
  • The stock boot used for the control carries one earlier GPU fault from another lane's profiler probe (06:40 UTC). It was outside this row's window, which had 0 new fault lines.

Update: linux#3235 (the "compute publication … gave up" warnings during the sync CTS) is not from this PR. The current head f95b7ec63651 only merges main, and all 8 of this PR's files are blob-identical to 6c44806e34b7.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at f95b7ec6: not ready yet

Check Detail
CI at head no failing: KUnit scaler, KUnit macsmc, KUnit clpc
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA f95b7ec6, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · every Mac with drm/asahi: all shmem-backed GPU objects, user and kernel

Areas: area:gpu, area:rust

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

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) and removed ci-failing A check failed at the PR head (bot) labels Nov 26, 2026

@aurora-silicon-review aurora-silicon-review Bot 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: MERGE at f95b7ec6 (once cancelled KUnit CI checks are rerun green)

Blockers: none (code and hardware verification are ready; rerun the 14 cancelled KUnit checks before merge).

Checked:

  • drivers/gpu/drm/drm_gem_shmem_helper.c:286,317 and include/drm/drm_gem_shmem_helper.h:64: verified pages_changed(shmem, true) runs under base.resv right after refcount_set(&shmem->pages_use_count, 1), and pages_changed(shmem, false) runs right after shmem->pages = NULL when the last reference drops.
  • rust/kernel/drm/gem/mod.rs and shmem.rs: verified DriverObject::pages_changed callback wiring in both Object::new() and create_object_callback(), plus KUnit pages_changed_spans_every_holder.
  • drivers/gpu/drm/asahi/gem.rs and mmu/residency.rs: verified all ad-hoc set_mapping_held() calls are replaced by the single AsahiObject::pages_changed hook so CPU mmaps, forked VMAs, dma-buf pins, vmaps, and SG tables are all accounted identically.
  • Verified J714s (M5 Pro, G17S) hardware row at f95b7ec6: 9/9 residency selftest (including the 2 new cases that fail on stock), 52 pass / 0 fail dma-buf CTS.
Evidence
  • Diff reviewed at f95b7ec6 (all 8 files, 586 lines).
  • CI at review time: 11 passed, 0 failed, 14 cancelled.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merge-train status at f95b7ec6 (App approval noted; rows: 14 cancelled KUnit; J714s exact-head PASS). @DjDeveloperr code-owner ping (6-point rule, point 6): merge earliest 17:15 AEDT once full CI is green (I re-ran cancelled jobs) and the second-family row point holds. Revert: git revert -m 1 <merge>.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merge-train, point 4: this changes drm_gem_shmem_helper and asahi GEM/residency, which every GPU generation runs. The J714s row covers one family. Please add a row at this head on a second (J616s, J316s M1 Pro or J413: GPU workload plus a memory-pressure or eviction case, 0 faults), then it merges.

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) and removed ci-failing A check failed at the PR head (bot) labels Nov 29, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Row (J616s, M4 Pro) at exact head f95b7ec in combined candidate cedaeff9ee2fe5396733351bb571d0a6049bd7fc on main aa76613: asahi-residency selftest (candidate source, root, RECLAIM=1) + vkcube 600 frames + glmark2-wayland under stress-ng --vm 85% + 12G root memory.reclaim during render -> PASS (residency 10/10 vs 8/10 on the saved production kernel, where both objects.* cases fail; glmark2 9719 vs 9505; 0 GPU faults/timeouts/recoveries, 0 DART faults. The opt-in DROP_CACHES case kept_objects_go_under_urgent_pressure fails identically, +164176 B resident, on production too, so it is not from this PR). Evidence: traces/kernel/20261129-closeout-j616s-combo/. Saved boot restored.

@RyanTheTide

Copy link
Copy Markdown
Contributor

code-owner ping @DjDeveloperr: closeout merge at f95b7ec6. The App approved at this head. Rows: J714s + J616s exact head (cedaeff9): asahi-residency 10/10, vkcube/glmark2 under memory pressure, 0 GPU faults. Merging under the 6-point rule 1 h from now, once hosted CI at this head is green, unless you object.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging under the 6-point rule (Ryan 09:29, permanent), at f95b7ec6. (1) The review App approved at this head. (2) Full CI is green. (3/4) Rows: J714s-+-J616s-exact-head-(cedaeff9):-asahi-residency-10/10,-vkcube/glm. (5) There's no blocking review. (6) Dj was pinged here 120 min ago with no objection. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit 3e882ea into main Nov 29, 2026
54 of 142 checks passed
@RyanTheTide
RyanTheTide deleted the asahi/gem-held-page-array branch November 29, 2026 12:37
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) area:rust Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-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