Repository navigation
drm/asahi: hold an object's backing for every holder of its pages - #3193
Conversation
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]>
|
Update at How it was run: one guarded one-shot of main
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 J773g (M4 Mac mini, G16G) at The kernel was built from
From the first boot of the same kernel (no need to repeat, since the kernel is unchanged):
Health on both boots: 0 GPU faults, 0 Note for G16: the two object cases at this head also pass on the stock kernel |
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]>
9183aca to
6c44806
Compare
|
Verdict at Blockers: none Checked:
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
|
|
J714s (MacBook Pro 14" M5 Pro, G17S B1) row at
Update: linux#3235 (the "compute publication … gave up" warnings during the sync CTS) is not from this PR. The current head
|
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
There was a problem hiding this comment.
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,317andinclude/drm/drm_gem_shmem_helper.h:64: verifiedpages_changed(shmem, true)runs underbase.resvright afterrefcount_set(&shmem->pages_use_count, 1), andpages_changed(shmem, false)runs right aftershmem->pages = NULLwhen the last reference drops.rust/kernel/drm/gem/mod.rsandshmem.rs: verifiedDriverObject::pages_changedcallback wiring in bothObject::new()andcreate_object_callback(), plus KUnitpages_changed_spans_every_holder.drivers/gpu/drm/asahi/gem.rsandmmu/residency.rs: verified all ad-hocset_mapping_held()calls are replaced by the singleAsahiObject::pages_changedhook 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.
|
Merge-train status at |
|
Merge-train, point 4: this changes |
|
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. |
|
code-owner ping @DjDeveloperr: closeout merge at |
|
Merging under the 6-point rule (Ryan 09:29, permanent), at |
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-systemor the creating process'sRssShmem. The shmem helpers now report when an object's page array is populated and when it is released, through a new optionalpages_changedcallback (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_memorydelta is 0. G16 charges objects from creation, so that row shows no regression rather than the fix. The host build withCLIPPY=1is clean on every changed line. CI passes, and its KUnit run includes the newpages_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_changedcallback is called under the reservation lock.drm_gem_shmem_get_pages_locked()calls it right after it populatespages, anddrm_gem_shmem_put_pages_locked()right after it releases them. Every holder of the array counts inpages_use_count: CPU VMAs other than revocable ones (forked copies too, throughvm_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 implementDriverObject::pages_changed(), both on objects the driver creates and on objects the DRM core allocates for it.In drm/asahi,
pages_changed()callsset_mapping_held(). Four explicit calls are gone, because each one was already the start or the end of that span:Idle release and purge both require
pages_use_count == !!sgt, so they drop the last holder.heldwas already idempotent, so an object held through several paths is counted once.What does not change:
reclaimable_cpu_mappings, VM-private objects) fault incpu_pagesone page at a time, not the page array. They stay unmarked, as before.Kernel objects now count in
kernel_residentfrom 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_CREATEalready 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_holdercovers 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), andRssShmemstays flatobjects.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.oanddrivers/gpu/drm/asahi/with the asahi profile andCLIPPY=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_shmempasses 19/19, includingpages_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:
objects.cpu_mappings_hold_the_backingandobjects.foreign_imports_hold_the_backingbothnot okresidencyprogram passes 9/9. Both object cases are ok, and the import went into a DCP card. The threeasahi.*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 failWARNING:/BUG/Oops linesJ773g (G16G), kernel built from the same kernel tree as the current head, see the row comment:
dEQP-VK.api.smoke.*8/8dEQP-VK.api.external.memory.dma_buf.*: 44 pass, 6 not supported, 0 failgpu_memoryuser_resident and user_committed delta 0 / 0WARNING: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
-Walland no warnings under gcc and clang.