Skip to content

drm/asahi: g17: scale the tiler heap's normal maximum with memory - #2724

Draft
jgoyvaerts wants to merge 4 commits into
aurora-silicon:integration/aurorafrom
jgoyvaerts:m4pro/g17-tvb-normal-max-v2
Draft

jgoyvaerts wants to merge 4 commits into
aurora-silicon:integration/aurorafrom
jgoyvaerts:m4pro/g17-tvb-normal-max-v2

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Supersedes #2186. The sizing is rewritten as this driver's own policy.

On 25G83 the tiler heap grows on the host only. Before a render, the host grows it to 3x the last high-water, capped at the heap's normal maximum. The driver also writes that cap to the firmware in BM+0x80. A scene bigger than the cap partial-renders on every frame. Today the cap is a fixed 0x45e (1,118 blocks, 140 MiB) on every NEO-layout heap, whatever the memory.

Series

  1. Grow the tiler heap in extensions of at most 8 MiB. A growth used to be one backing object, and a shrink only gives back whole trailing ones. A 900 -> 2,700 jump was one 225 MiB extension. The active shrink returned it only after 450 credit periods, and the idle shrink not at all while its floor stayed above 900. Now the active shrink gives back 8 MiB every 16 periods, and the idle shrink stops within 8 MiB of its floor. A full table is 53 extensions.
  2. Grow only in the two tiler-heap growth arenas, as far as they go. Growth used to spill from the growth arenas (3,321 + 3,353 blocks per VM) into the render-alias and target-record windows. The VM's next target record or growth then failed with ENOSPC. Growth now stays in the growth arenas (cfg::tvb_growth_ranges()). A growth that runs out of arena space or memory keeps the whole extensions it made, and the pass partial-renders within that heap, as it does after a refused firmware grow request. An idle growth still fails if the heap would stay below what the render's layout requires. After a growth stops short, the host grows beyond the required blocks again only a shrink period later (the existing busy retry, now retry).
  3. Charge tiler-heap growth to the pair's client. Growth used to be kernel objects: charged to no client, never refused, and pinned. Each extension of a client VM's pair is now admitted with memacct::LEDGER.admit() against the client's and the device's limits (defaults 50% and 67% of RAM) and counted as the client's held memory (fdinfo drm-resident-system, gpu_memory), as the M5 pools are. A refusal ends the growth there, as in 2. Pages are not added to a process's MM_SHMEMPAGES, as for the M5 pools, because growth also runs from the firmware grow worker. memcg charging of the shmem pages is unchanged. The primary backing, sized by the render target, stays a kernel object.
  4. Scale the normal maximum with memory, the change reviewed before:

Policy

The normal maximum is a 64th of si_meminfo() memory, clamped to fw::NORMAL_MAX_BLOCKS..=cfg::TVB_MAX_BLOCKS (1,118..=3,353 blocks). There is no rounding. The cap is per render pair; 1-3 bound what all pairs of a client and of the device hold together. BM+0x7c (the heap capacity, 3,354 slots) is unchanged. The memory is memacct::total_ram_bytes(), which also sizes the memory limits.

Memory (si_meminfo) Normal maximum
up to 8.7 GiB 1,118 (floor: BM+0x80 stays 0x45e * 4 pages)
16 GiB 2,048
24 GiB 3,072
from 26.2 GiB 3,353 (block table)

The A18 Pro (8 GiB) keeps 1,118, and its mesh floor (mesh_floor()) stays pinned to fw::NORMAL_MAX_BLOCKS. An M4 Max gets 3,353, the value #2021 sets for it by hand.

Why a 64th

All three reasons come from our own measurements:

  • Upper bound: on the 48 GiB J616s, macOS 26.6.2 runs the GPU with BM+0x7c = 19,737 and BM+0x80 = 6,579 blocks (read live). That is 1/60 of the memory. The fixed 1,118 is 1/59 of 8 GiB. A 64th stays below both. Above the floor, BM+0x80 is never a larger share of memory than the firmware is known to run with.
  • Floor: at 8 GiB a 64th would be 1,024 blocks, so the clamp keeps today's 1,118.
  • The J616s's measured behaviour: 46.6 GiB usable gives 5,959 blocks, clamped to 3,353. That is the cap the sweep below measured. A 1/60 share would give the same values on the J616s and the A18 Pro. The exact share only matters between 8.7 and 26.2 GiB, which nobody has measured yet, and there a 64th errs low.

Cleanups: the duplicate memory helper is gone, and Layout::initialize() (M5 graph) now reads GraphArgs::normal_blocks, as fw::Graph::init() does.

Measured on the J616s (M4 Pro, 48 GiB, GPU firmware 25G83)

The J616s runs the macOS 26.6.2 GPU firmware (GPU firmware: macOS 26.6.2 (25G83, iBoot mBoot-18000.161.10) in the kernel log). All measurements here are from 25G83.

The test is off-screen GLES: N screen-spanning triangles into a stored 3456x2234 RGBA8 + D24S8 target, waiting for each frame (glFinish). There were 0 GPU recoveries. "Before" is a 1,118-block cap and "after" is a 3,353-block cap. Both were measured on our m4.166 test kernel with the earlier revision (#2186), which wrote the same 3,353 on this Mac. m4.166 also carries #1902 and #1986, which only affect pairs that never go idle.

N Before (1,118) After (3,353)
10,000 32 ms (3.2 us/tri) 32 ms
15,000 146 ms (9.7 us/tri) 41 ms (2.8 us/tri)
20,000 193 ms 55 ms
30,000 281 ms 83 ms
40,000 395 ms 110-138 ms
45,000 not run 417 ms (past 3,353 blocks: partial renders)

On this head (001d7834eb78, on integration/aurora 606d37ec77a5), no hardware run yet:

  • Compile check, each of the 4 commits: arm64 defconfig + asahi.config, LLVM=1, W=1, drivers/gpu/drm/asahi. Clean; no warnings in the changed files.
  • KUnit, arm64 qemu: 734 of 736 passed, 1 skipped (needs CONFIG_UCLAMP_TASK), 1 failed: apple-isp-hw.isp_test_sensor_sysfs, an ISP test outside this series. asahi_g17_tvb_limits passes.
  • tools/asahi/run-host-tests.py --only g17: 90/90.

Pending: J616s boot with this head (next test kernel)

All GPU runs go through our GPU queue. 0 GPU recoveries, 0 AVC, 0 failed units and no new kernel warnings in every row.

  1. BM+0x7c / BM+0x80 of a live NEO heap: 13,416 / 13,412 pages (3,354 slots, 3,353 blocks).
  2. The single-client triangle sweep above (N = 10k ... 45k): about 32 / 41 / 55 / 83 / 110-138 / 417 ms.
  3. Several heavy clients at once: four triangle-test processes at N = 30,000, plus one process with three render queues in one VM. Read gpu_memory and each client's fdinfo drm-resident-system: tiler growth now shows up per client. In the one-VM case, the third queue's growth stops at the arena edge, and its frames keep rendering (slower) without ENOSPC.
  4. Client limit: the same test with gpu_mem_limit_pct lowered so the client limit bites. refused rises, the heap stops short of 3,353, frames keep rendering, no warnings. The limit is restored afterwards.
  5. Shrink: after the sweep, the heap returns in 8 MiB steps while active (trace HEAP_SHRINK), and to its floor after 10 s idle.

HW rows still wanted (machines we don't have)

  • 16 GiB M4 (Mac mini J773g or a 16 GiB MacBook Pro), the scaled range: the tiler test with BM+0x80 read back as 8,192 pages (2,048 blocks), and rows 3 and 4 if possible. @RyanTheTide, could you run it?
  • J700 (A18 Pro, 8 GiB): the BM+0x80 readback (4,472 pages, 1,118 blocks), plus 0 GPU recoveries / AVC / failed units, as you suggested. Series 1-3 also apply there, so a GLES run with a heavy scene would cover the charged growth.
  • J616s under the 27.2 stub (26B123): needs a T6040 26B123 GPU build first.

Not done

  • The heap's capacity stays at 3,354 slots. Raising it needs a memory-sized NEO graph and VA layout.
  • The growth arenas are not resized.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Good writeup. The sizing comes from a memory share measured on the machine, not a per-model table, and the 15k-40k triangle numbers make the case. An adversarial read of 0a2a5c2:

  1. The cap is per heap, so it multiplies. Layout::neo(memory_bytes()?) is evaluated per buffer::Manager, i.e. per render queue's tiler heap. So 1/64 of RAM is the ceiling for each heavy client, not for the GPU. On a 16 GiB Mac, a few heavy GL/Vulkan clients (a game, a browser compositor, a benchmark) can each hold 256 MiB of heap where they held 140 MiB before. Combined with the shrink policy (one 512 KiB step per second, only after 10 s idle), a heap that peaked at 3,353 blocks takes about nine minutes to return to the old cap. Two things would settle it: say whether the growth pages are charged to the client (memacct/memcg), and add a row with several clients at once on a smaller-memory Mac, or argue why that isn't a concern.

  2. The 9-26 GiB range is the untested part, and it's where the formula changes behaviour. At 8 GiB the clamp gives the old value, and at 26+ GiB the block table caps it, both covered by your J616s run. The scaled range in between is 16 and 24 GiB M4 and M4 Pro machines. A 16 GiB M4 (Mac mini J773g or a Mac16 MacBook Pro) running your tiler test, with BM+0x80 read back as 2,048, would show the scaled path itself working.

  3. On the J700: neo_normal_blocks() of anything at or below 8 GiB is NORMAL_MAX_BLOCKS (your KUnit case), and si_meminfo() on an 8 GiB Mac reports less than 8 GiB. So the A18 Pro write is unchanged by construction. A J700 row only needs the BM+0x80 readback and the usual no-regression checks, not the full CTS subset.

Assisted-by: LLM (Claude:Opus 5.5)

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

Adversarial review of head 0a2a5c2b8f6543f30ab09bf035d9f621f0581b16 against the pre-PR checklist. Holding approval and requesting changes on the memory-accounting/VA-exhaustion implications and missing hardware evidence on this head.

Code & resource-limit findings

  1. Per-heap RAM multiplication is unattributed to clients and bypasses GPU memory admission limits.
    Following up on Ryan's point #1 directly from gem.rs and memacct.rs: Tvb::prepare_growth() allocates each extension via KernelObject::backing() -> gem::new_kernel_object_wc() -> memacct::Charge::kernel(&memacct::LEDGER, size) (gem.rs:324). In this driver:

    • Kernel objects are not charged to any client (Client) or process mm (MM_SHMEMPAGES), so the OOM killer does not score the offending client for them.
    • Kernel objects bypass both LEDGER.limit and LEDGER.client_limit (memacct.rs:215: "Kernel objects share the device limit but are never refused"), while increasing kernel_resident and shrinking the budget left for other clients' user BOs.
    • set_mapping_held(&gem, true) pins the shmem pages as AS_INACCESSIBLE | AS_UNEVICTABLE (unswappable, unmigratable), and off-lock growth runs in scheduler/workqueue context rather than reliably under the submitting task's memcg.
      Because Layout::neo(memory_bytes()?) applies per physical render pair (RENDER_SLOTS = 64, and one logical queue can hold 2 pairs), raising the cap from 140 MiB to 256 MiB (16 GiB Mac), 384 MiB (24 GiB), or 419 MiB (≥26.2 GiB) multiplies per active heavy pair — e.g., 4–8 heavy pairs on a 16 GiB Mac can pin 1–2 GiB of unswappable, unattributed, limit-bypassing kernel memory.
  2. Whole-extension shrink interacts poorly with the larger 3× growth steps.
    In buffer.rs:1012-1050, 1266-1285:

    • While a client stays active on light frames (now - last_busy < 10s), shrink_target() earns SHRINK_STEP_BLOCKS = 4 (512 KiB/s) and only shrinks once credit >= last (the entire last extension), resetting credit = 0 on any period above 1/4 capacity. A single 3× growth jump from e.g. 900 -> 2,700 blocks creates a 1,800-block (225 MiB) extension that takes 7.5 minutes of uninterrupted low load to release while active.
    • Because Manager::shrink() can only drop whole trailing KernelObject extensions (kept - size < target), if shrink_floor() sits anywhere above the pre-jump size (e.g. 950 blocks after a 900 -> 2,700 jump), that 225 MiB extension cannot be shrunk even after 10 s idle until busy_peak drops below 450 blocks.
      Raising normal_blocks up to 3× makes both the peak footprint and this coarse-granularity retention substantially heavier. Please either bound/attribute TVB growth (or cap individual extension chunk size so shrink can reclaim incrementally) or demonstrate multi-client pressure behavior on a 16 GiB Mac.
  3. Multi-pair compact-VA spillover into TARGETS within a single VM (hw/t8140.rs:346-355).
    compact_ranges() provides TVB_MAX_BLOCKS slots in TVB_GROWTH (pair 0) and TVB_MAX_BLOCKS in second (pair 1), and then falls back to TARGETS (320 MiB, shared with render targets). Previously at 1,118 blocks/pair, up to 6 render pairs in one VM fit in the two TVB arenas without touching TARGETS. At 3,353 blocks/pair (≥26.2 GiB Macs), just 2 heavy pairs in one VM fill both TVB arenas completely, so a 3rd growing render pair in the same VM spills TVB blocks into TARGETS and then fails with ENOSPC.

  4. Minor cleanups:

    • buffer::memory_bytes() (buffer.rs:319) duplicates memacct::total_ram_bytes() (memacct.rs:104).
    • GraphArgs.normal_blocks is populated for both NEO and M5 (buffer.rs:819), validated/used by fw::Graph::init(), and silently ignored by Layout::initialize() (which reads self.normal_blocks instead).

Evidence & checklist blockers

  • No hardware boot or regression run on current head 0a2a5c2b (Checklist §5): the J616s numbers in the PR body are from an older superseded revision (#2186) on m4.166 (which also carried #1902 and #1986), and no regression run (0 SELinux denials, 0 failed units, 0 new kernel errors) is reported for this head.
  • No conformance run for a performance change (Checklist §5): checklist requires a same-machine A/B with conformance unchanged on every GPU generation the change applies to.
  • Untested scaled range (9–26 GiB) & A18 Pro: as noted by Ryan and in the PR body, no 16 GiB / 24 GiB Mac has exercised the non-clamped formula (BM+0x80 = 2,048 / 3,072), and the J700 (A18 Pro) readback/no-regression row is still pending.
  • CI: host-tests passed on 0a2a5c2b.
  • Checklist §1–§4 & §6: Pass on code (RAM-scaled policy with no chip/board/firmware gates, default cmdline, human-only Signed-off-by in commit, clean-room live measurements). Minor attribution nit: PR body ends with Assisted-by: LLM (Claude:Opus 5.5) instead of Anthropic:Claude Opus 5.5.

Assisted-by: LLM (Google:Gemini 4 Neon)

A growth of the tiler heap is one backing object, and a shrink can only
give back whole trailing growth objects. A heap that grew from 900 to
2,700 blocks in one step holds a single 1,800-block (225 MiB) extension:
the active shrink returns it only after 450 periods of shrink credit,
and the idle shrink cannot return it at all while its floor sits above
900 blocks.

Build each growth from extensions of at most 64 blocks (8 MiB). The
active shrink then gives back 8 MiB every 16 periods of credit, and the
idle shrink stops within 8 MiB of its floor. A full block table is 53
extensions.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
… they go

Tiler-heap blocks are placed in the first of the compact client arenas
with room: the render-alias window, the two tiler-heap growth arenas
(3,321 and 3,353 blocks), then the target-record window. Growth that
fills both growth arenas, for instance three pairs of one VM at 3,353
blocks, then takes target-record space, and the next target record or
growth of that VM fails with ENOSPC, and the render with it.

Place growth only in the two growth arenas. A growth that runs out of
them or of memory keeps the whole extensions it made, and the tiling
pass partial-renders within that heap, as it does when a firmware grow
request is refused. An idle growth still fails if the heap would stay
below the blocks the render's layout requires. After a growth stopped
short, the host grows the heap beyond those blocks again only a shrink
period later, as the busy growth already does after a refusal: every
attempt allocates and maps afresh.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Tiler-heap growth is allocated as kernel objects: they count towards the
device total, but are charged to no client, are never refused by the
device or the per-client limit, and are pinned. The client sizes this
growth through its renders, and every render pair of every client can
hold its own, so many heavy pairs can pin a lot of memory that no limit
bounds and no client is accountable for.

Admit each growth extension of a client VM's pair against that client's
and the device's limits, and count it as the client's held memory, as
the M5 pools are. A refused extension ends the growth there: the tiling
pass partial-renders within the heap it has, and an idle growth fails
with ENOMEM only if the heap would stay below what the render's layout
requires. Kernel pairs keep kernel objects.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
The tiler heap of a 25G83 render grows on the host only: before a render
the host grows it to three times the last high-water, capped at the heap's
normal maximum, which also goes to the firmware in BM+0x80. A scene larger
than that cap partial-renders on every frame. The cap is the fixed 0x45e
(1,118 blocks, 140 MiB) on every NEO-layout heap, whatever the memory.

Make the cap a 64th of the memory si_meminfo() reports (memacct's
total_ram_bytes(), which also sizes the memory limits), clamped to
NORMAL_MAX_BLOCKS..=TVB_MAX_BLOCKS (the block table). BM+0x7c, the
heap's capacity, is unchanged. The cap holds for each render pair; the
growth up to it is charged to the pair's client and bounded by its and
the device's memory limits, and by the VM's growth arenas. Both graph
layouts take the normal maximum from GraphArgs.

The share comes from three measurements:

- On the 48 GiB J616s, macOS 26.6.2 runs the GPU with BM+0x80 = 6,579
  blocks (read live), 1/60 of the memory. The fixed 1,118 is 1/59 of
  8 GiB. A 64th stays below both, so above the floor the cap is never a
  larger share of memory than the firmware is known to run with.
- At 8.7 GiB and below the clamp keeps 1,118: BM+0x80 stays
  0x45e * 4 pages, and the 8 GiB A18 Pro (and its mesh floor) is
  unchanged.
- On the J616s (46.6 GiB usable) a 64th is 5,959 blocks, clamped to the
  table's 3,353. An off-screen GLES test draws N screen-spanning
  triangles into a stored 3456x2234 RGBA8 + D24S8 target and waits for
  each frame. With 1,118, a triangle costs 3.0 us up to N = 14,500 and
  9.7 us from N = 15,000. With 3,353, 3.0 us holds up to N = 40,000:
  N = 20,000 takes 55 ms a frame instead of 193 ms, N = 30,000 83 ms
  instead of 281 ms. At N = 45,000 the scene passes 3,353 blocks and
  partial-renders as before.

A 16 GiB Mac gets 2,048 blocks, a 24 GiB Mac 3,072; from about 26.2 GiB
the table's 3,353 is the limit.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
@jgoyvaerts
jgoyvaerts force-pushed the m4pro/g17-tvb-normal-max-v2 branch from 0a2a5c2 to 001d783 Compare November 22, 2026 02:10
@jgoyvaerts
jgoyvaerts marked this pull request as draft November 22, 2026 02:11
@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Thanks both. New head 001d7834eb78 (rebased on integration/aurora 606d37ec77a5) adds three commits in front of the scaling commit:

  1. Shrink granularity: growth is built from extensions of at most 64 blocks (8 MiB), g17/buffer.rs:403. The active shrink gives back 8 MiB every 16 credit periods, and the idle shrink stops within 8 MiB of its floor. Before, 900 -> 2,700 was one 225 MiB extension that took 450 periods to return.
  2. VA: growth is placed only in the two growth arenas (hw/t8140.rs:347, 3,321 + 3,353 blocks per VM), never in RENDER_ALIASES/TARGETS. A growth that runs out of arenas or memory keeps its whole extensions, and the pass partial-renders (g17/buffer.rs:1264). An idle growth still fails only below the layout's required blocks (HostGrowth::Idle { required, target }). Growth beyond that is retried a shrink period later (defer_growth, g17/buffer.rs:1112). A third heavy pair in one VM no longer reaches ENOSPC.
  3. Accounting: each extension of a client VM's pair is admitted against the client's and the device's limits and counted as the client's held memory, as the M5 pools are (g17/buffer.rs:521). A refusal ends the growth there. To Ryan's question: memacct, yes; process RSS, no (as for M5 pools, because the firmware grow worker also grows); memcg charging of the shmem pages is unchanged.

Cleanups: memacct::total_ram_bytes() replaces the duplicate helper (g17/buffer.rs:867), and Layout::initialize() reads args.normal_blocks (g17/buffer/layout.rs:82).

Tests on this head: each commit compile-checked with arm64 defconfig + asahi.config, LLVM=1, W=1; KUnit arm64 qemu 734/736 (1 skip UCLAMP, 1 unrelated ISP sysfs failure); host tests --only g17 90/90. The J616s boot is pending; its plan is in the description (BM+0x80 readback, the sweep, several heavy clients at once, a client-limit refusal run, shrink steps). The PR is a draft until that row lands.

@RyanTheTide, we have no 16 GiB M4 or J700. Could you run the 16 GiB tiler test with BM+0x80 read back as 2,048 blocks, and on the J700 the BM+0x80 = 1,118 readback with 0 recoveries/AVC/failed units?

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks, this answers the earlier review. At 001d783:

  • Charging and the per-pair multiplication: fixed. Each extension is admitted against the client's and the device's limits and counted as held memory (buffer.rs:521-532), so the sum over a client's pairs is bounded by the ledger, not by 1/64 of RAM per pair. The 8 MiB extensions (buffer.rs:403) fix the shrink granularity, and growth no longer spills into the target-record window (t8140.rs:347, buffer.rs:563). The unwind order on a stop-short growth is right: mappings are dropped before their extensions, and a firmware request that gets 0 blocks is answered as refused (grow.rs).
  • The scaled range: my earlier ask was wrong. NEO heaps exist only on the G17 backend (apple,agx-t8140, t6040, t6041; driver.rs:171-179). The M4 (T8132) has no backend here, so no 16 GiB Mac reaches this code, and the 16 GiB row in the table and in "HW rows still wanted" can go. The only real configuration between the floor and the block table is the 24 GiB M4 Pro, which lands at roughly 2,900-3,000 blocks, a little below the 3,353 the J616s sweep measured. A 24 GiB M4 Pro row would be nice to have, but it shouldn't block this.
  • J700: the readback is still wanted, and the series now changes the A18 Pro too: growth in 8 MiB charged extensions, placed only in the growth arenas, which can now be refused. Please add a heavy GLES scene that grows the heap on the J700 at 001d783, with BM+0x80 = 4,472 pages, drm-resident-system rising with the growth, and 0 GPU recoveries.
  • Nothing at this head has run on hardware yet. The J616s list in the body (rows 1-5) covers the new paths. Row 4 (client limit bites, heap stops short, frames keep rendering) is the only test of the stop-short path, because the KUnit tests cover only neo_normal_blocks().

No new code issues found.

Assisted-by: LLM (Claude:Opus 5.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants