Skip to content

drm/asahi: charge and reclaim the CPU-faulted pages of revocable mappings - #3224

Merged
RyanTheTide merged 5 commits into
mainfrom
asahi/cpu-pages-held
Dec 1, 2026
Merged

RyanTheTide merged 5 commits into
mainfrom
asahi/cpu-pages-held

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: CPU-faulted pages of VM-private objects are now charged, and idle reclaim releases them. These objects have revocable CPU mappings on G17: while no device mapping holds an object's page array, each CPU fault populates and pins one page. Those pages stayed unevictable but were not charged to the client (drm-resident-system) or to the creating process (RssShmem). Idle reclaim never released them either, since it considered only objects with a device SG table.

  • The shmem helpers' pages_changed callback (from drm/asahi: hold an object's backing for every holder of its pages #3193) now reports how many pages back an object: all of them while the page array exists, otherwise the CPU-faulted pages.
  • drm/asahi charges exactly that many pages.
  • Idle reclaim counts and revokes the CPU pages of idle bound objects.
  • Selftests and KUnit cases cover the charge and the reclaim.

Tested: the host build with the asahi profile and CLIPPY=1 is clean: 0 errors and no warning on any changed line. J773g (G16) passes as a no-regression row: the residency selftest, Vulkan smoke, the dma-buf CTS and the gpu_memory leak check are clean, and the kernel log has no faults. The new cases need G17 and skip there. The G17 row is pending.
Risk: two-way door · every Mac with drm/asahi, since the shmem callback and the charge type change for all; the new charges and reclaim act only on G17 (M5), the only GPU whose user VMs have revocable mappings

Not yet

  • Neo J700: finish the G17 residency row and dma-buf CTS. Recheck the existing M4 mini row after resolving base conflicts and rerun the canceled CI checks.

CI; the G17 row

Details

This PR is stacked on #3193 and changes the callback that PR adds. It retargets to main once #3193 merges.

shmem helper. drm_gem_shmem_pages_changed() reports pages ? npages : cpu_pages_count, under the reservation lock, at four points:

  • after the page array is populated
  • after the page array is released
  • after a CPU fault adds a page
  • after the CPU pages are released

The Rust binding passes the count on. It also gains resident_pages_locked(), which reads the same number.

Charging.

  • memacct::Charge now counts held bytes instead of a held flag. set_held_bytes() counts the difference to what it counted before, up to the object's size. set_held() stays all-or-nothing for kernel and user-memory objects.
  • set_mapping_held() marks the mapping held while any page is held, and charges resident * PAGE_SIZE. A one-page fault in a large heap therefore charges one page, not the whole heap.

Reclaim.

  • When no lease remains, idle_reclaim_count() and reclaim_idle_pages() now take objects that have either an SG table or CPU pages (resident_pages_locked() != 0). They count the pages that actually back the object.
  • release_idle_pages_locked() already zaps revocable VMAs and drops their pages before it returns early for an object without an SG table.
  • Unbound VM-private objects have no GPUVA on the VM, so the reclaim walk still can't see them. Their CPU pages are bounded by the object's admitted size, which counts against the client's commit limit. They are now also charged to the client and the process, so the OOM killer attributes them.

Unevictable LRU. Releasing the page array goes through drm_gem_put_pages(). That clears AS_UNEVICTABLE and moves the folios back to the evictable LRU before the callback clears AS_INACCESSIBLE. Releasing the CPU pages does the same when no array remains. The reclaim selftest checks that Unevictable in /proc/meminfo drops after the release.

Tests.

  • asahi.cpu_faults_charge_their_pages: an unbound, 64-page VM-private object takes faults at 3 distinct pages. The client and RssShmem grow by exactly 3 pages and return to baseline at munmap. Before this change they stay at baseline.
  • asahi.reclaim_revokes_cpu_faulted_pages (needs ASAHI_RESIDENCY_DROP_CACHES=1 and root):
    1. A bound 256 MiB object goes idle, and dropping caches releases it. Unevictable drops by at least half its size.
    2. Two CPU faults then charge 2 pages.
    3. A second drop revokes them, back to baseline. The contents survive and fault back in.
  • KUnit:
    • rust_drm_gem_shmem pages_changed_spans_every_holder checks the reported counts against resident_pages_locked().
    • asahi_memacct adds some_pages_are_charged_as_they_are_held.
Evidence

Host build of rust/, drm_gem_shmem_helper.o and drivers/gpu/drm/asahi/ with the asahi profile and CLIPPY=1: 0 errors. The 14 clippy warnings in the touched files are all on unchanged lines. checkpatch reports 0 errors and 0 warnings on all four commits. The selftest builds with -Wall and no warnings under gcc and clang.

J773g (M4 mini, G16G), combined boot of main with this PR, see the row comment:

  • residency: 2 pass, 9 skip. The two asahi.* cases are new and need GEM_RESIDENCY, which G16 lacks.
  • 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
  • kernel log clean

A charge counted the object's backing as held in full or not at all.
Record the bytes held instead, so that a part of the backing can be
counted: set_held_bytes() counts the difference to what was counted
before, up to the object's size, and set_held() holds all or nothing as
before.

Signed-off-by: DjDeveloperr <[email protected]>
CPU faults in revocable mappings populate an object's pages one at a
time while no device mapping holds its page array. Those pages stay
referenced and the mapping unevictable until the mappings are revoked
or closed, but pages_changed() reported only the page array, so a
driver could not account for them.

Report the number of pages that back the object instead of whether its
array exists: all of its pages while the array does, otherwise the
populated CPU pages, after each change to either. The Rust binding
passes the count on and gains resident_pages_locked(), which reads the
same number under the reservation lock.

drm/asahi charges the pages reported, so the client and the process
that created a VM-private object are charged for the pages its CPU
mappings fault in, and the mapping is marked held while any are.

Signed-off-by: DjDeveloperr <[email protected]>
Idle reclaim counted and released only objects with a device SG table.
Once it had released an object's table, pages that CPU faults then
populated in the object's revocable mappings stayed held until the VM's
next job or until the mappings were closed.

Count the pages that back an idle object, its page array or its CPU
pages, and release whichever it has: releasing idle backing revokes the
CPU mappings as well.

Signed-off-by: DjDeveloperr <[email protected]>
Check that each page CPU faults populate in a VM-private object is
charged to its client and creating process until the mapping goes.

With ASAHI_RESIDENCY_DROP_CACHES=1, check that reclaim releasing an idle
object's backing moves it off the unevictable LRU, that it then revokes
the pages CPU faults populate in the object as well, and that their
contents fault back in.

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

Copy link
Copy Markdown
Contributor Author

J773g (M4 Mac mini, G16G) at fbd98d12d6bd: PASS, no regression on G16. The new charges and reclaim act only on G17. On this Mac the row covers the shared shmem helper and the accounting.

How it was run: one guarded one-shot of main 255761f1d55f plus this PR, plus #3232 (mm/ksm.c, no overlapping files). This PR's 8 files are identical to its head. It carries #3193 at f95b7ec63651, which had its own boot just before.

  1. residency, built from fbd98d12d6bd:
    • objects.cpu_mappings_hold_the_backing: ok.
    • objects.foreign_imports_hold_the_backing: ok.
    • asahi.*: 9 skip ("no DRM_ASAHI_FEATURE_GEM_RESIDENCY"), including the new cpu_faults_charge_their_pages and reclaim_revokes_cpu_faulted_pages.
    • Totals: pass 2, fail 0, skip 9.
  2. CTS: dEQP-VK.api.smoke.* 8/8. dEQP-VK.api.external.memory.dma_buf.* 44 pass, 6 not supported, 0 fail.
  3. gpu_memory: user_resident 101,662,720 and user_committed 101,664,088 before steps 1–2. Identical after (delta 0 / 0).
  4. journalctl -k -b:

Also on this boot: one s2idle passed, and Wi-Fi did 200 MB and 50 MB each way with 0 rx errors. The Mac went back to the stock kernel, and the kit was removed from it.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

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

@github-actions github-actions Bot added merge-conflict Conflicts with the base branch (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Merge readiness at f10faee7: not ready yet

Check Detail
CI at head no failing: apple-build, host-tests, source, pr-text / pr-text, KUnit ane-boot, rust-build
Review ok approved by RyanTheTide
Requested changes ok none
Hardware row at head ok comment at f10faee7
Conflicts ok none
PR text ok Risk: two-way · every Mac with drm/asahi, since the shmem callback and the charge type change for all; the new charges and reclaim act only on G17 (M5), the only GPU whose user VMs have revocable mappings

Areas: area:gpu, area:rust

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

@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: REQUEST CHANGES at fbd98d12

Blockers:

  1. Branch is CONFLICTING with main (and CI did not complete green). Rebase/merge current main across the 8 touched files (+275/-85) so the PR is MERGEABLE, verify all build/KUnit/host test CI checks pass, and confirm exact-head hardware verification at the rebased head.

Checked:

  • Metadata and diff summary (+275/-85, 8 files), CONFLICTING against main.
Evidence
  • Resolve CONFLICTING against main so CI builds/KUnit can execute cleanly on the current tree.

Resolve memacct.rs tests: keep the partial-hold case from this branch beside the usage-limit case from main.

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

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

The forward merge resolves the conflict and retains the partial-hold and usage-limit tests. The earlier J714s and J773g results support the reclaim path. Current checks still need to exercise the combined accounting code inherited from main, particularly partial holds unwinding without a charge leak.

@RyanTheTide RyanTheTide 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 the conflict-pass merge commit f10faee only: single-file union, no hand edits. The resolution keeps what each side had and adds nothing outside the conflict. Approved for the merge-up; merging still waits on CI and the rows at this head.

@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) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 1, 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 f10faee7a935

Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.

Checked: Code reviewed in the earlier App review at this head (6122660634).

Follow-ups (not blocking): checks exercising combined accounting, partial-hold unwind without charge leak

@RyanTheTide

Copy link
Copy Markdown
Contributor

J616s (MacBook Pro 14", M4 Pro, T6040, G16) at f10faee7a93532b1bb3723bc08dd4a32403a6675: PASS (no-regression row; the G17 reclaim claim needs an M5) - asahi-residency selftest with ASAHI_RESIDENCY_DROP_CACHES=1: 7 pass, 2 fail, 3 skip. The Mac's default kernel (7.3.0-aurora-edge 933f822) gives 4 pass, 5 fail, 3 skip. asahi.cpu_faults_charge_their_pages goes from not ok to ok, and objects.cpu_mappings_hold_the_backing and objects.foreign_imports_hold_the_backing also pass now. Both remaining failures fail the same way on the default kernel, so neither is new. asahi.reclaim_revokes_cpu_faulted_pages fails at its first check, after the bind and before any reclaim: the client reads 268697768 bytes resident against 268550144 expected, identical on both kernels. asahi.kept_objects_go_under_urgent_pressure is about 160 KiB over on both. dEQP-VK.api.smoke.* 8/8; dma_buf.* 44 pass, 6 not supported, 0 fail. glmark2 17669 and 17715. 0 GPU faults, 0 WARN/Oops, s2idle 3/3. Kernel = main 27d585c8dfb3 + #2161 #3224 #3117 #3217 #3413 (exact heads), one grub2-reboot one-shot boot, default command line and Stage 2.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging at f10faee7a935 (B2 merge train, 12-01 merge bar). Approval at this head: RyanTheTide,aurora-silicon-review[bot]. Row: J616s (M4 Pro) row at this head (B2 rows kernel = main 27d585c + #2161 #3224 #3117 #3217 #3413), posted on this PR. CI: linux train L7 = main 27d585c + #2161 #3224 #3117 #3217: Apple driver compile pass, planned KUnit dcp-topology/jpeg/scaler/asahi/sep/aop pass. Revert: git revert -m 1 <merge commit>.

@RyanTheTide
RyanTheTide merged commit a9e77d0 into main Dec 1, 2026
10 of 37 checks passed
@RyanTheTide
RyanTheTide deleted the asahi/cpu-pages-held branch December 1, 2026 11:05
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants