Skip to content

drm/asahi: keep timestamp buffers out of the shared firmware range, charge binds to their client, and bound per-file window share - #2531

Open
funkzy wants to merge 3 commits into
aurora-silicon:mainfrom
funkzy:pr/asahi-timestamp-window
Open

funkzy wants to merge 3 commits into
aurora-silicon:mainfrom
funkzy:pr/asahi-timestamp-window

Conversation

@funkzy

@funkzy funkzy commented Nov 20, 2026 •

Copy link
Copy Markdown

Change of approach

This update replaces the earlier 4096-object count cap with two complementary bounds and rebases onto integration/aurora (c91527272e90):

  1. Per-bind ledger charge instead of a fixed count cap (commit 2). The earlier second commit refused a bind past 4096 bound objects per file with EBUSY. That count stood in for the kernel memory each bind keeps, which was the one per-bind cost not charged to the client. This version charges every bind the size of its records against the client's and the device's memory limits, through the same Ledger::admit() that object creation uses, and drops the count. A bind past the client's limit now fails with ENOMEM, as an object creation would. G13/G14, G15 and M3 commands now retain Arc<timestamp::Bound> (with firmware_mapping() returning a borrowed &mmu::KernelMapping), matching G16, G17 and G18, so both the bind charge and the window share below stay held until every referencing command retires.
  2. Per-file 1/16 share of the firmware timestamp window (commit 3). Addressing RyanTheTide's and DjDeveloperr's review notes on the shared 64 MiB firmware timestamp windows (G13/G14, G15, M3, and non-M5 G17), each file may now hold at most 1/16 of its backend's fixed timestamp window (4 MiB of a 64 MiB window) at once, so one client cannot exhaust the window for other clients. The share is per file, matching the review ask and the existing per-file accounting model; a single-client-full-window policy like queue_share can be considered separately if large query pools hit 4 MiB.

The first commit is unchanged.

Problem

On M5, and on the other G17 GPUs once the 64 MiB timestamp window is full, GEM_BIND_OBJECT mapped a timestamp buffer into the kernel VM's dynamic range, the range every firmware object (queues, kick rings, render state) is allocated from. The driver never gave the firmware that address: outside the window the host writes every stamp through the buffer's CPU mapping. So the mapping only held shared address space for as long as the buffer stayed bound.

Across all families, two shared-bind gaps remained:

  • The record each bind keeps in the file's object table was not charged to the client, and the number of binds per file was not limited.
  • On every family that aliases timestamp buffers into a fixed 64 MiB firmware timestamp range (G13/G14, G15, M3, and non-M5 G17), that range was shared first-come across all files, so a single client could fill it with a few large binds and cause other clients' timestamp binds to fail with ENOSPC (or spill out of the window on G17).

Why this shape

  • Where the policy lived. Each GPU backend decides where a timestamp buffer goes in its own map_timestamp_buffer(). G13/G14 (gpu.rs), G15 (g15/mod.rs) and M3 (m3/m3_drm.rs) map it into their fixed 64 MiB firmware timestamp range because their firmware writes the stamps. G16 and G18 keep only a CPU mapping (Buffer::cpu), because the host writes them. The separate M5 backend (m5/, T8142) refuses timestamp buffers (ENOTSUPP), so only the bind's charge below is new there. G17 maps into the 64 MiB window when the buffer fits; otherwise, and always on the G17 code's M5 firmware graph (uat.is_m5()), it mapped the buffer into the kernel VM's dynamic range instead, although the host writes those stamps and the firmware is never given that address. The per-file object table in file.rs, which every family shares, accepted any number of binds without charging their records or bounding a file's share of the firmware timestamp window.
  • The narrower alternatives: a size or count limit on G17's dynamic-range timestamp mappings, a larger window, a fixed per-file object count cap, or separate per-backend quota checks. The first two would still spend firmware address space on buffers the firmware never reads; a fixed count bounds the number of entries but neither the memory cost nor large-range window consumption; per-backend quotas would duplicate the policy across four drivers.
  • The source used. A G17 buffer outside the window takes the host-written path, with a CPU mapping only, as G16 and G18 already do; the choice turns on whether the window has room, not on the chip or firmware version. Each bind's record memory is charged through the driver's existing memory ledger (Ledger::admit()), whose limits come from the machine's RAM; the object's pages and shared shmem CPU mapping remain charged to the object at creation. For the firmware timestamp window, each backend reports its fixed range size via timestamp_window_size() (0 when none), and do_gem_bind_object() enforces a single 1/16 per-file share (memacct::Client::take_window()) before calling map_timestamp_buffer(..., window).
  • What it gives. Non-M5 G17 buffers past a full window (or past a file's 4 MiB share), and every timestamp buffer on the M5 graph, take no firmware address space. On G13/G14, G15 and M3, a single file can hold at most 4 MiB of the 64 MiB firmware timestamp window, leaving room for other clients, and past its share fails with ENOSPC just as for a full window. On every family a file's binds are bounded by its memory limit, failing with ENOMEM past it.

Change

  1. Timestamp buffers outside the window take no firmware address space (G17). Such a buffer gets only its CPU mapping (timestamp::Buffer::Host), as G16 and G18 keep theirs. It refuses the same ranges and VM-private objects as before. Buffers mapped in the window, and every other GPU family, are unchanged.
  2. Each bind is charged to its client. GEM_BIND_OBJECT charges the bind's records (BOUND_OBJECT_BYTES: the table entry, the Arc count, timestamp::Bound including Option<WindowShare>, and timestamp::Buffer::HEAP_BYTES, computed from the types' sizes — 152 bytes on arm64 in the default all-backends config after storing KernelMapping directly in FirmwareBuffer without a redundant inner Arc) through Ledger::admit() before reserving the table slot or mapping anything. A bind past the client's or the device's limit fails with ENOMEM with nothing mapped. Buffer::firmware_mapping() returns a borrowed &mmu::KernelMapping and G13/G14, G15 and M3 commands now retain Arc<timestamp::Bound> alongside G16, G17 and G18, so the charge (and any window share below) stays held until every referencing command retires. The records and table nodes are allocated with GFP_KERNEL_ACCOUNT.
  3. Per-file 1/16 share of the firmware timestamp window. Each backend reports its fixed firmware timestamp range size through Gpu::timestamp_window_size() (IOVA_KERN_TIMESTAMP_RANGE on G13/G14, TIMESTAMPS_SIZE on G15, TIMESTAMP_RANGE on M3, TIMESTAMP_WINDOW on non-M5 G17 via a single Firmware::timestamp_window() helper; 0 on G16, G18, M5 and G17's M5 graph). do_gem_bind_object() reserves the bound range from the file's 1/16 share (memacct::Client::take_window(), 4 MiB of a 64 MiB window) before mapping and passes window: bool to map_timestamp_buffer(). Past a file's share, G17 falls back to Buffer::Host and G13/G14, G15 and M3 return ENOSPC; any buffer not mapped into the window releases the share immediately. The share is per file, matching the review ask and the existing per-file accounting model; a single-client-full-window policy like queue_share can be considered separately if large query pools hit 4 MiB.

This PR does not depend on #2530: it uses the ledger already in the base, not #2530's page-table charge, and the two merge cleanly in either order.

Testing

KUnit and all-backends build on this head (13f0a8aa2b26, base integration/aurora c91527272e90):

  • KUnit (kit pr2531-r4k3, built with CONFIG_KUNIT=y and CONFIG_DRM_ASAHI=y, run under kunit-vz): 80 suites; 395 of 397 cases pass, 0 fail, 2 skip (apple_mbox_g17_stall/apple_g17_rx_util_min_test, which needs CONFIG_UCLAMP_TASK, and asahi_fwid/j713_accepts_all_loaded_images).
    • asahi_bound_object_charge: 2 of 2 pass (a_bind_is_charged_its_records_and_refused_past_the_client_limit, a_file_holds_at_most_its_share_of_the_timestamp_window).
    • asahi_timestamp_buffer: 2 of 2 pass (a_bound_range_is_whole_pages_inside_the_object, a_host_stamp_lies_inside_the_bound_range).
  • All-backends modular build (kit pr2531-r4m3, CONFIG_DRM_ASAHI=m with G15, G16, G18, M3_T8122 and M5 all enabled): builds cleanly with 0 new warnings in any touched file; compiled BOUND_OBJECT_BYTES is 152 bytes on arm64.

A fresh own-base J775D hardware A/B run on this 3-commit head (13f0a8aa2b26 on integration/aurora c91527272e90 + #1973) is pending under Still to run.

Extra evidence, not this PR's own row (earlier 2-commit head ce717ff67cce on integration/aurora 95a13cf618b1 + #1973, kits pr2531-r3 and pr2531-r3b, J775D Mac Studio M5 Ultra, 27.0 firmware, Aura main dff1235d, default command line, read-only root; covers commits 1 and 2 prior to adding host_offset() KUnit and commit 3):

J775D, extra evidence (pr2531-r3b vs pr2531-r3, base 95a13cf618b1) without this PR with commits 1–2 (ce717ff67cce)
4112 binds of a 16 KiB object in one file 4112 accepted, +0 bytes committed 4112 accepted, +625024 bytes committed (152 per bind before commit 3), all given back at close
the next bind once the client's objects fill its 48293 MiB limit accepted (256 of 256 tried) ENOMEM; the ledger's refused count goes from 0 to 1
256 binds of a 256 MiB timestamp buffer (64 GiB of ranges) 223 accepted, then ENOSPC 256 accepted, +0 MiB client and kernel memory
a second client's queue creation after those binds succeeds succeeds
Vulkan CTS timestamp cases (1149) 1093 Pass, 2 Fail, 54 NotSupported the same, with the same two failures
GL GL_TIMESTAMP / GL_TIME_ELAPSED (3 runs of 10 rounds) 30 of 30 rounds pass 30 of 30
render CTS subset (628) 571 Pass, 57 NotSupported the same
host tests (run-host-tests.py) 461 of 461 on the base 461 of 461, no result differs
GPU errors, recoveries, faults, oopses 0 0
failed units / kernel error lines 2 / 8 the same 2 / the same 8

Regression run on those kits (ALARM has no SELinux, so there is no AVC count): 0 new failed units (iwd.service and systemd-networkd-persistent-storage.service fail on the read-only root in both boots) and 0 new kernel error lines; the 8 are the same in both boots.

Extra evidence from the first version (commit 1 + the earlier 4096 count cap at 60e5bdaae4ce, base b21dcc633b32) is in the details below.

Still to run

  • J775D (M5 Ultra) own-base A/B row on this 3-commit head (13f0a8aa2b26) on integration/aurora c91527272e90 plus arm64: dts: apple: t6050-j775d: Give the Mac Studio (M5 Ultra) a working GPU, and its M and P core capacities (754/1024) to the scheduler #1973 (the J775D GPU node): bind-charge accounting and ENOMEM refusal at the client limit, timestamp binds, Vulkan/GL timestamp tests, host tests, 0 new failed units and 0 new kernel errors.
  • Non-M5 G17 (J616s or Neo) spillover row: verify timestamp binds spilling past a file's 4 MiB window share (and past a full 64 MiB window) onto Buffer::Host, with Vulkan and GL timestamp queries passing, KUnit pass counts per suite, 0 failed units and 0 new kernel errors.
  • G13/G14 (e.g. M1 Pro), G15, or M3 row: verify normal timestamp queries are unchanged and that one file is capped at its 1/16 (4 MiB) share of the 64 MiB firmware timestamp window (ENOSPC past the share) while another file can still bind its own timestamp buffers.
  • Rows from RyanTheTide's lab or owners of non-M5 G17 and G13/G14/G15/M3 Macs are welcome; the M5 Ultra has no firmware timestamp window and takes the host-written path for every bind.
Full description of the three commits
  1. Keep timestamp buffers out of the shared firmware range (G17). A buffer that does not fit the 64 MiB timestamp window is bound as timestamp::Buffer::Host: it keeps a CPU mapping of the bound range and no firmware alias. The host writes its stamps through that mapping (the only consumer is the G17 completion path, which takes object offsets). It refuses the same ranges and VM-private objects the alias did. The bound-range check is shared with Buffer::cpu and the VM-private test with gem::validate_vm_binding; asahi_timestamp_buffer tests the range check.
  2. Charge each bound object to its client. GEM_BIND_OBJECT admits BOUND_OBJECT_BYTES (the table entry, the Arc count, timestamp::Bound and timestamp::Buffer::HEAP_BYTES, computed from the types' sizes — 152 bytes on arm64 with commit 3's Option<WindowShare> included) through memacct::LEDGER.admit(), then reserves the table slot, then maps the buffer, then fills the slot. The returned Commit is held in timestamp::Bound next to the buffer. FirmwareBuffer owns mmu::KernelMapping directly (no redundant inner Arc) and Buffer::firmware_mapping() returns a borrowed &mmu::KernelMapping; G13/G14 (fw::job::UserTimestamp), G15 (g15::command/g15::job) and M3 (m3::m3_submit::Destination) now retain Arc<timestamp::Bound>, matching G16, G17 and G18, so no command on any family can keep a firmware mapping alive after unbind without also keeping its bind charge and window share alive until retirement. A refusal (ENOMEM) or a failed mapping drops the charge and the slot with nothing mapped. The record, the buffer and the table's nodes stay on GFP_KERNEL_ACCOUNT. asahi_bound_object_charge tests the charge and ENOMEM refusal past the client limit; asahi_timestamp_buffer also tests host_offset() bounds via a_host_stamp_lies_inside_the_bound_range.
  3. Cap each file to 1/16 of the firmware timestamp window. memacct::Client tracks per-file timestamp_window bytes with an RAII WindowShare token acquired via Client::take_window() (atomic CAS via try_add_within(), no overshoot under concurrency) and stored on timestamp::Bound. Each backend reports its fixed window size through Gpu::timestamp_window_size() (64 MiB on G13/G14, G15, M3, and non-M5 G17 via Firmware::timestamp_window(); 0 on G16, G18, M5, and G17's M5 graph). do_gem_bind_object() computes window_limit = window_size / 16, takes the file's share before mapping, passes limit == 0 || share.is_some() to map_timestamp_buffer(), and drops share immediately if buffer.window_bytes() == 0 (e.g. G17 falling back to Buffer::Host when the global window is full). asahi_bound_object_charge::a_file_holds_at_most_its_share_of_the_timestamp_window tests single-file limit enforcement, cross-file isolation, release on drop, and zero-limit behavior.
Earlier extra evidence details

Extra evidence, not this PR's own row: 2-commit head (ce717ff67cce) (kit pr2531-r3: integration/aurora 95a13cf618b1 + #1973 + commits 1–2; baseline kit pr2531-r3b: the same without this PR), Mac Studio (M5 Ultra), 27.0 firmware, read-only root, one boot each, 2026-11-22:

  • Charge: 4112 binds of a 16 KiB object in one file. Without the PR all are accepted and the device's committed bytes do not move; with it all are accepted and committed grows by 625024 bytes (152 per bind before commit 3 added WindowShare to timestamp::Bound), returning to its starting value once the file is closed.
  • At the limit: a new file creates a 16 KiB object and a second object sized to fill its 48293 MiB per-client limit (admission only: the large object's pages are never allocated), then binds the small object. Without the PR all 256 binds tried are accepted. With it the first bind is refused with ENOMEM and the ledger's refused count goes from 0 to 1.
  • Timestamp buffers: 256 binds of a 256 MiB object (64 GiB of ranges). Without the PR 223 are accepted and the 224th fails with ENOSPC; with it all 256 are accepted, with +0 MiB in the client's committed memory, the kernel's resident memory and the client's fdinfo. A second client's QUEUE_CREATE after the binds succeeds in both, and again after the binding file is closed.
  • Vulkan CTS timestamp cases: every case in the mustpass (vulkan-cts-1.4.7.1) whose name contains timestamp or calibrat, 1149 in all. Both kernels: 1093 Pass, 2 Fail, 54 NotSupported (pipeline.monolithic.timestamp.misc_tests.timestamp_stage_top_of_pipe_stage and timestamp_stage_host_stage fail on both kernels; the 54 NotSupported are present-timing cases requiring a window system).
  • GL: desktop GL 4.6 core on the asahi Gallium driver, EGL surfaceless: 10 rounds of GL_TIME_ELAPSED around 1 to 512 full-target triangles with GL_TIMESTAMP queries before and after; 3 runs, 30 of 30 rounds pass on both kernels.
  • Render CTS subset (628 cases): 571 Pass, 57 NotSupported on both.
  • KUnit on 95a13cf618b1: with commits 1–2, 79 suites, 380 of 382 pass, 2 skipped; without it 77 suites, 378 of 380, the same two skips.
  • Host tests: tools/asahi/run-host-tests.py on 95a13cf618b1 and on the base with arm64: dts: apple: t6050-j775d: Give the Mac Studio (M5 Ultra) a working GPU, and its M and P core capacities (754/1024) to the scheduler #1973 and commits 1–2: 461 of 461 on both, no result differs.
  • Kernel log and units: 0 GPU recoveries, firmware restarts, timeouts, faults or WARN/BUG/Oops in both boots; the same 8 kernel error lines and 2 failed units (iwd.service and systemd-networkd-persistent-storage.service) in both boots.

Extra evidence, not this PR's own row: first version (60e5bdaae4ce) (second commit used the 4096 count cap; kits pr2531-1 and pr2531-1b on integration/aurora b21dcc633b32 + #1973, 27.0 firmware, Aura main 3efa4a81 with aura#407 and aura#408, both since merged). Commit 1 is unchanged: 256 of 256 timestamp binds at +0 MiB against 223 then ENOSPC, the same timestamp CTS, GL and render CTS results with and without the PR, 0 new failed units and 0 new kernel errors. Its 4096-then-EBUSY cap row no longer applies.

Thanks to ewb, who reported the unbounded binds and suggested the fix, and to RyanTheTide and DjDeveloperr for the reviews.

  • Clean-room: no Apple code, strings, firmware, RE output or tuning values
  • No chip/board/firmware-version gate where a hardware property can decide
  • Works on the default kernel command line; no hacks, delays or lab-only paths
  • Safety limits come from the machine, not from constants in code
  • Hardware safety rules followed for every test boot
  • Third-party commits unchanged; reporters and testers credited
  • Commits: human Signed-off-by only; no co-author or Assisted-by trailers
  • Tested on the affected hardware; no regression (KUnit passed on 13f0a8aa2b26; J775D own-base A/B re-run on 13f0a8aa2b26, non-M5 G17 spillover row, and G13/G14/G15/M3 window-share row are under Still to run)

Assisted-by: LLM (Claude:Opus 5.5, Gemini:Neon)

@funkzy
funkzy force-pushed the pr/asahi-timestamp-window branch from df120a7 to d92d363 Compare November 20, 2026 21:24
@RyanTheTide

Copy link
Copy Markdown

No blockers.

What I checked at d92d363. On G17 the only consumer of a bound timestamp buffer is drivers/gpu/drm/asahi/g17/completion.rs:50-66. It goes through window_va(), which is None for Host, through timestamp_offset() (timestamp.rs:157, which bounds the offset against the bound range and returns the object offset) and through read/write on the vmap, which take object offsets. The paths that call firmware_mapping() (queue/common.rs:36, g15/command.rs:369, m3/m3_submit.rs:22) never see a Host buffer. A Host buffer holds no KernelMappingPin, but it can't be idle-reclaimed or purged under the vmap: Buffer::host refuses VM-private objects (timestamp.rs:75), and reclaim and purge only walk a VM's private BOs (mmu/residency.rs:481, :519, :615). bound_size keeps the same range checks as the old Buffer::cpu, and the KUnit cases cover the edges.

Nit: file.rs:1593-1602 checks the 4096 cap only after map_timestamp_buffer(), so a bind past the cap still takes a window alias or a vmap for a moment before it is dropped. The result is the same, but checking the table count first would avoid the churn.

HW row: the run lists the M5 Ultra only. The Host fallback also runs on the non-M5 G17s once the 64 MiB window is full, and the 4096 cap applies to every family. Please add: J616s or Neo on this build, with the window filled (bind timestamp buffers until binds land outside it), dEQP-VK.query_pool.*timestamp* and a GL GL_TIMESTAMP query test pass with the same results before and after, with binds outside the window succeeding and their stamps read back monotonic; one G13 Mac (M1/M2) running the same timestamp tests with no change; KUnit pass counts per suite; 0 AVC, 0 failed units, 0 new kernel errors on each.

Assisted-by: LLM (Claude:Opus 5.5)

@funkzy
funkzy force-pushed the pr/asahi-timestamp-window branch from d92d363 to 60e5bda Compare November 21, 2026 19:14
@funkzy

funkzy commented Nov 21, 2026

Copy link
Copy Markdown
Author

Thanks, Ryan. The nit is fixed in the second commit, now at 60e5bda: do_gem_bind_object() now reserves the object-table slot (reserve_limit, as VM_CREATE and queue creation do) before map_timestamp_buffer(), and fills it after. A bind past 4096 is refused before the buffer gets a window alias or a vmap, and a failed mapping just releases the slot. The commit message says so, and I've added your Suggested-by. git range-diff: the first commit is unchanged; the second changes only in do_gem_bind_object() and that message paragraph.

J775D on this head (stacked with #1973 and our other three open drm/asahi PRs on integration/aurora cc14570, 27.0 firmware, default command line; the kernel also carries a local fix for an appledrm boot oops, see #1973):

  • 4112 binds of a 16 KiB object in one file: 4096 accepted, then EBUSY.
  • 256 binds of a 256 MiB timestamp buffer (64 GiB of ranges, all past the window): 256 accepted, with +0 MiB in the client's committed memory, the kernel's resident memory and fdinfo, and a second client's queue creation still succeeds.
  • Render CTS subset: 571 Pass, 57 NotSupported, the same as without the series.
  • KUnit, per suite: 79 suites, 366 of 368 cases pass (two skips, as on the base: apple_g17_rx_util_min_test without UCLAMP and j713_accepts_all_loaded_images); asahi_timestamp_buffer 1 of 1. The base has 75 suites, 353 of 355. Host tests: 436 of 436 on base and head.
  • Regression: 0 new failed units (iwd.service and systemd-networkd-persistent-storage.service fail on the read-only test root with and without the series) and 0 new kernel error lines. ALARM has no SELinux, so there is no AVC count.

Rows we can't run: we only have the M5 Ultra, where every bind takes the host path. Could your lab, or a J616s or Neo owner, run this head with the timestamp window filled (bind timestamp buffers until binds land outside it)? The row we need is dEQP-VK.query_pool.*timestamp* and a GL GL_TIMESTAMP query test with the same results before and after, binds outside the window succeeding with their stamps read back monotonic, KUnit pass counts per suite, and 0 failed units and 0 new kernel errors. The same timestamp tests on one G13 Mac (M1 or M2), unchanged, would cover the cap on the families without the window.

@RyanTheTide

Copy link
Copy Markdown

Re-review of 60e5bda: No blockers.

  1. Cap checked after map_timestamp_buffer() (nit): resolved. drivers/gpu/drm/asahi/file.rs:1596-1607 now takes reserve_limit(1..=MAX_BOUND_OBJECTS_PER_FILE) before the mapping and fills it after, the same pattern as VM_CREATE (file.rs:737) and queue creation (file.rs:1725). A bind past 4096 fails before any window alias or vmap, a failed mapping releases the slot through the Reservation drop, and fill() takes the XArray lock itself, so a rejected object is dropped outside it.

The first commit is unchanged since the last review; the second changes only in do_gem_bind_object() and its message.

HW row: the new numbers are from the M5 Ultra, which takes only the host path, on an integration stack with other open PRs and a local appledrm fix. Still needed: J616s or Neo on this head with the 64 MiB window filled, dEQP-VK.query_pool.*timestamp* and a GL GL_TIMESTAMP query test unchanged before and after, binds outside the window succeeding with monotonic stamps; one G13 Mac (M1/M2) running the same timestamp tests unchanged; KUnit pass counts; 0 failed units, 0 new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

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

Adversarial pre-PR checklist review (§1–§6) on 60e5bdaae4cec3e495996a83d412b5bf8e3ef61b: 0 code, policy, attribution, naming, or CI blockers; holding for §5 hardware proof rows on non-M5 G17 and G13.

Verified against surrounding source:

  • §1 No model gating: Buffer::Host vs Buffer::Firmware (window: true) is selected by whether the buffer fits in the timestamp window (firmware.uat.is_m5() was pre-existing), and the 4096-object cap lives in shared drivers/gpu/drm/asahi/file.rs across every GPU family.
  • §2 / §3 Code & safety verification:
    • bound_size() (drivers/gpu/drm/asahi/timestamp.rs:16-22) enforces non-empty, UAT_PGMSK-aligned, in-bounds ranges with overflow checks and KUnit coverage (asahi_timestamp_buffer).
    • Buffer::host() (timestamp.rs:72-83) refuses is_vm_private() with EINVAL, maps via TimestampVmap::new(&bo.gem) -> owned_vmap::<0>() (which refuses external/user_memory BOs at rust/kernel/drm/gem/shmem.rs:261 and pins shmem pages via drm_gem_shmem_vmap_locked), and wraps the buffer in cleanup::Deferred so dma_resv_lock-taking drop runs off fence-producer paths. Verified drivers/gpu/drm/asahi/mmu/residency.rs:481, 519, 615 only walk VM-private BOs (for_each_private_bo), so omitting KernelMappingPin on HostBuffer cannot race with idle reclaim or purge.
    • Buffer::timestamp_offset() (timestamp.rs:153-166) checks offset + 8 <= buffer.size (ERANGE/EOVERFLOW) and returns buffer.start + offset; window_va() returns None and firmware_mapping() / timestamp_mapping() return Err(EINVAL). Verified drivers/gpu/drm/asahi/g17/completion.rs:45-65, 160-188 routes window_va == None exclusively to write_timestamp().
    • do_gem_bind_object() (drivers/gpu/drm/asahi/file.rs:1595-1609) reserves the XArray slot (1..=4096, GFP_KERNEL_ACCOUNT) before map_timestamp_buffer(), so binds past 4096 fail with EBUSY before creating any window alias or vmap; failed mapping releases the slot on Reservation drop; resv.fill(obj)? unlocks the XArray before dropping any rejected StoreError. Verified rust/kernel/xarray.rs:321, 558: remove() on an unfilled reservation coerces XA_ZERO_ENTRY to NULL (ENOENT), and any concurrent race fails cleanly with EBUSY. (cleanup::Deferred::new at cleanup.rs:93 still uses GFP_KERNEL for the work item wrapper, which is pre-existing, small, and now bounded by the 4096/file cap — non-blocking.)
  • §4 Clean-room & attribution: Both commits (a9d3d1658565, 60e5bdaae4ce) carry human author/committer + human Signed-off-by (plus Reported-by/Suggested-by) and zero AI trailers; PR body includes Assisted-by:.
  • §6 Naming: Clean.
  • CI: host-tests passed (sole workflow matching drivers/gpu/drm/asahi/**).

Holding for §5 hardware proof rows on 60e5bdaae4ce:
Your J775D (M5 Ultra) run covers the host-only path, cap enforcement, KUnit, and render CTS, though on a stacked branch with other open PRs + a local appledrm fix, and M5 Ultra does not exercise the 64 MiB timestamp-window spillover path. Still needed:

  1. Non-M5 G17 (J616s or Neo) on this head with the 64 MiB timestamp window filled so binds spill into Buffer::Host: dEQP-VK.query_pool.*timestamp* and a GL GL_TIMESTAMP query test unchanged before/after, out-of-window binds succeeding with monotonic timestamps, KUnit pass counts, 0 failed units, and 0 new kernel errors.
  2. One G13 Mac (M1/M2) running the same timestamp tests unchanged (covering the shared 4096-cap path), with clean regression counts.

Assisted-by: LLM (Gemini 4 Neon)

@funkzy

funkzy commented Nov 22, 2026

Copy link
Copy Markdown
Author

Thanks, dj. Here is the J775D row on this PR's own base: integration/aurora b21dcc6 plus this PR, and only what the board needs for a GPU, which is #1973 (the J775D GPU node). The baseline is the same kernel without this PR. 27.0 firmware, default command line, read-only root, one boot each.

J775D without this PR with this PR
4112 binds of a 16 KiB object in one file 4112 accepted 4096, then EBUSY
256 binds of a 256 MiB timestamp buffer 223, then ENOSPC 256, +0 MiB
Vulkan CTS timestamp cases (1149) 1093 Pass, 2 Fail, 54 NotSupported the same, with the same two failures
dEQP-VK.query_pool.*timestamp* 0 cases in this CTS 0 cases
GL GL_TIMESTAMP (3 x 10 rounds) 30 of 30 30 of 30
render CTS subset (628) 571 Pass, 57 NotSupported the same
KUnit 76 suites, 375 of 377, 2 skips 77 suites, 376 of 378, 2 skips (asahi_timestamp_buffer 1 of 1)
failed units / new kernel errors 2 the same 2 / 0 (only USB enumeration retries differ, before any test)

The two CTS failures are the stage-stamp ordering cases (timestamp_stage_top_of_pipe_stage, timestamp_stage_host_stage). They fail the same way without this PR, as does our own compute-timestamp check (0 of 10 rounds on both). vulkan-cts-1.4.7.1 has no query_pool timestamp group, so I ran every mustpass case naming timestamp or calibrat instead. ALARM has no SELinux, so there is no AVC count.

On 27.2 firmware, on this branch's merge-base 61295c2, the cap, timestamp-buffer, CTS and GL rows came out the same, with and without this PR. Those kernels also need #1650 and two of our GPU-driver commits that aren't public yet, so the body lists them only as extra evidence, as it does the earlier stacked row.

The spill path isn't exercised here. The M5 Ultra has no timestamp window, so all of its timestamp binds take the host-written path, which is what the rows above test. The body now has a "Why this shape" section, and its Testing section leads with this row.

On cleanup::Deferred::new: the firmware-mapped path already allocated one Deferred per bound timestamp buffer with GFP_KERNEL before this PR, and the host-written path now takes the same one. It is shared cleanup code that the G16 paths use too, and it is now bounded at 4096 per file. Moving it to GFP_KERNEL_ACCOUNT for every caller would be a separate change.

RyanTheTide, could you run the two open rows: this head on your J616s or J700 with the timestamp window held full, and the same timestamp tests on your M1 Pro? We only have the M5 Ultra, which has no window. To hold the window: it is one 64 MiB range per device, so one process that keeps more than 64 MiB of timestamp binds open (for example five 16 MiB buffers bound with DRM_ASAHI_BIND_OBJECT_USAGE_TIMESTAMPS) sends every query pool created meanwhile to the host-written path. While it holds them, run the dEQP-VK timestamp cases and a GL GL_TIMESTAMP test, before and after this PR; the rows to compare are those results, KUnit per suite, failed units and new kernel errors. I can send the small hold program and our GL timer test if that helps. The hold works on the J775D (five 16 MiB binds held while those tests ran, then released), though with no window there it only checks the procedure.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown

Review notes (non-author):

  • The 64 MiB timestamp window used on G13/G14 (IOVA_KERN_TIMESTAMP_RANGE), G15 (g15/timestamp.rs) and M3 (m3_runtime.rs) is still first-come: please bound the bytes one file may hold in it (a per-file quota or a per-buffer range cap) before merging, so these generations get the same protection as G17/M5.
  • asahi_timestamp_buffer exercises the refactored range check only; please add a case for the per-file cap (and the host-buffer path if it can be reached from KUnit).
  • Until the first point lands, please trim the 'Still to run' description of the hold scenario from the body.

Alyx-ML added a commit to Alyx-ML/linux that referenced this pull request Nov 23, 2026
Several ioctls size a kernel allocation straight from userspace:

- SUBMIT copies cmdbuf_size bytes (a u32, so up to 4 GiB) into a KVec.
  Only G16 bounds it before the copy (4 MiB); every other GPU kept the
  trait default of usize::MAX.
- SUBMIT allocates in_sync_count + out_sync_count sync items before any
  per-GPU check.
- VM_BIND copies stride * num_binds bytes and then reserves num_binds
  prepared operations. Without a GPU batch limit (bind_batch_limit()
  returns None), both are up to u32::MAX.

A request past the page allocator's largest order hits
WARN_ON_ONCE_GFP() in __alloc_pages(). Any user with the render node can
trigger it, and with panic_on_warn it takes the machine down. Short of
that limit, a valid but large request still needs high-order contiguous
pages: an order-3 allocation of the sync array failed on J616s under
deqp-vk.

For SUBMIT:

- Copy the command stream and build the sync array in KVVec with
  GFP_KERNEL_ACCOUNT. kvmalloc() falls back to vmalloc() instead of
  failing on fragmented memory, and the memory is charged to the
  caller's memcg. The sync array keeps this type through every
  generation's submit() (G13/G14, G15, G16, G17, G18, M3, M5); the G17
  job vectors are converted in aurora-silicon#2858.
- kvmalloc() refuses, and warns about, more than INT_MAX bytes. The
  trait default for max_cmdbuf_size() is now INT_MAX, so a larger
  command stream fails with E2BIG. This rejects no stream any GPU could
  accept before. A sync array larger than INT_MAX bytes fails with
  E2BIG too.
- G17 returns its own bound: the largest valid G17 stream,
  MAX_COMMANDS_PER_SUBMISSION * (1 + 5) *
  (size_of::<drm_asahi_cmd_header>() + u16::MAX) = 25,168,512 bytes,
  checked at build time to be at most INT_MAX.
- G15 returns its 4 MiB MAX_CMDBUF, as G16 already does. Its parser
  refused a longer stream before, but only after the copy, with EINVAL.
  Such a stream now fails before the copy, with E2BIG.
- G18, M3 and M5 use the shared default until their own largest stream
  is known.

For VM_BIND, pass __GFP_NOWARN on the user-sized allocations, so a
request that is too large fails with ENOMEM and no warning. Its sizes
are not changed. The timestamp mapping limit is left to
aurora-silicon#2531.

Checked by reading the call paths (file.rs submit, parse_array, vm_bind,
vm_bind_g17, mmu/bind.rs, and each generation's submit()). rustfmt is
clean on the changed code. The kernel Rust code was not compiled here.
What userspace can see: on G17, a stream over 25,168,512 bytes, which
its parser cannot accept, now fails before the copy; on G15, a stream
over 4 MiB fails with E2BIG before the copy instead of EINVAL after it;
on G13, G14, G18, M3 and M5, only streams over 2 GiB fail early. The
allocation path changes on every GPU, so deqp-vk or CTS runs on G13, G14
and G17 (J616s among them) are needed before this is merged.

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

Formal §1–§6 re-review on 60e5bdaae4ce:

What passes

  • G17 Buffer::Host & shared object-table cap (file.rs, g17/mod.rs, gem.rs, timestamp.rs): Code correctness, locking, is_vm_private() guard, TimestampVmap lifetime + cleanup::Deferred drop, and reserving the XArray slot (1..=4096, GFP_KERNEL_ACCOUNT) before map_timestamp_buffer() are all sound.
  • §4 Attribution & §6 Naming: Clean — both commits (a9d3d1658565, 60e5bdaae4ce) carry human author/committer + human Signed-off-by: only (zero AI commit trailers), and the PR body carries Assisted-by:.
  • Clean-base J775D (M5 Ultra) row & live CI: The updated J775D A/B run on integration/aurora b21dcc633b32 + #1973 cleanly covers the M5 host-only path, cap enforcement, Vulkan/GL timestamps, and KUnit; live host-tests CI is green on 60e5bdaae4ce.

Blocking findings

  1. §1 Cross-generation shared timestamp window protection (per @RyanTheTide's open review note): The 64 MiB shared firmware timestamp window on G13/G14 (IOVA_KERN_TIMESTAMP_RANGE in drivers/gpu/drm/asahi/gpu.rs:141,1698), G15 (drivers/gpu/drm/asahi/g15/timestamp.rs:27), and M3 (drivers/gpu/drm/asahi/m3/m3_runtime.rs) is still first-come with no per-file byte quota or per-buffer range cap. Even with the 4096-object cap, a single client can exhaust the 64 MiB window with a handful of large binds (e.g., 4 × 16 MiB). Please bound the bytes one file may hold in the firmware timestamp window so G13/G14/G15/M3 receive equivalent protection before merge, and trim the hold-scenario description from "Still to run" in the PR body until that lands.
  2. KUnit coverage: asahi_timestamp_buffer currently tests bound_size() only. Please add KUnit coverage for the per-file cap (and window quota / host-buffer bounds where reachable from KUnit).
  3. §5 Hardware proof rows still open: M5 Ultra (uat.is_m5()) has no 64 MiB timestamp window, so the non-M5 G17 spillover path (J616s or Neo spilling into Buffer::Host once the window is full) and a G13 (M1/M2) regression row covering the shared cap/quota path still need to be run on the updated head.

Assisted-by: LLM (Gemini 4 Neon)

On M5, and on the other G17 GPUs once the 64 MiB timestamp window is
full, GEM_BIND_OBJECT mapped a timestamp buffer into the kernel VM's
dynamic range, the range every firmware object (queues, kick rings,
render state) is allocated from. The driver never gives the firmware
that address: outside the window the host writes every stamp through
the buffer's CPU mapping. The mapping only held shared address space,
as much as the bound range, for as long as the buffer stayed bound.

Give such a buffer only its CPU mapping (timestamp::Buffer::Host), as
G16 and G18 keep theirs without a firmware alias. It refuses the same
ranges and VM-private objects the alias did. Buffers mapped in the
window, and every other GPU family, are unchanged.

Share the bound-range check with Buffer::cpu, and the VM-private test
with gem::validate_vm_binding, and test the range check in a new
asahi_timestamp_buffer KUnit suite.

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
…stay within its memory limit

GEM_BIND_OBJECT stores a record in the file's object table for every
bind, and the number of records per file was not limited. The record and
the timestamp buffer it names are small kernel allocations that no GEM
object accounts for, and they were not charged to the client. The
buffer's pages and its CPU mapping belong to the object: the shmem
helper shares one vmap between every mapping of an object, and the
object's size is charged when it is created.

Charge every bind the size of what it keeps (the table entry, the buffer
record and the buffer's heap records) against the client's and the
device's memory limits, through the Ledger::admit() that object creation
uses, as a user object of that size would be. A bind past either limit
fails with ENOMEM. The charge is held with the bound buffer and given
back with its last reference: after the unbind, or when the file is
closed, once no command still uses the buffer.

To ensure no command can keep a firmware timestamp mapping alive after
unbind without also keeping its Bound record (and charge) alive, store
the KernelMapping directly in FirmwareBuffer, return a borrowed
&mmu::KernelMapping from firmware_mapping() instead of cloning an Arc,
and have G13/G14 (fw::job::UserTimestamp), G15 (g15::command/job) and M3
(m3::m3_submit::Destination) retain Arc<timestamp::Bound>, matching G16,
G17 and G18.

The bind charges and takes its table slot before it maps the buffer, so
a refused bind gets no window alias or CPU mapping at all. The record,
the buffer and the table's nodes are allocated with GFP_KERNEL_ACCOUNT,
so they also count towards the memory cgroup of the task that binds.

Also add a host_offset() KUnit case
(a_host_stamp_lies_inside_the_bound_range) in asahi_timestamp_buffer
alongside the new asahi_bound_object_charge suite.

This applies to every GPU family and to every GEM_BIND_OBJECT usage: the
table is shared code.

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Ryan Murray <ryan@aurorasilicon.org>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
…dow, so other clients' timestamp binds still fit

Every family that aliases timestamp buffers for its firmware does so in
one fixed range per device: IOVA_KERN_TIMESTAMP_RANGE on G13/G14, the
timestamp arena on G15, TIMESTAMP_RANGE on M3 and the timestamp window
on G17 (64 MiB each). Binds from every file share it, and one file
could fill it. Every other client's timestamp bind then failed with
ENOSPC on G13/G14, G15 and M3, which Mesa reports as out of device
memory for a query pool, or fails GL context creation with.

Give each file a share of that range: a sixteenth of its size, 4 MiB of
a 64 MiB window. Mesa binds one small buffer per GL context, one per
timestamped query pool and one debug buffer. A bind takes its range
from the file's share before it maps anything, and the share comes back
with the buffer's last reference. Past its share a file's buffer may
not be mapped in the window: G17 gives it a host-written buffer, as for
a full window, and the families whose firmware writes the stamps refuse
the bind with ENOSPC, as for a full window. Every other file keeps its
own share.

Each backend reports its range's size (timestamp_window_size(), 0 for
none) and is told whether the buffer may go there, so the policy lives
in one place, the bind. G16, G18 and the M5 backend alias nothing and
are unchanged, as is G17 on the M5 firmware graph, which has no window.

The share is counted in the file's memacct::Client, next to its
committed bytes.

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Ryan Murray <ryan@aurorasilicon.org>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
@funkzy
funkzy force-pushed the pr/asahi-timestamp-window branch from 60e5bda to 13f0a8a Compare November 24, 2026 00:31
@funkzy funkzy changed the title drm/asahi: keep timestamp buffers out of the shared firmware range, and cap bound objects per file drm/asahi: keep timestamp buffers out of the shared firmware range, charge binds to their client, and bound per-file window share Nov 24, 2026
@funkzy

funkzy commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks for the review, @DjDeveloperr and @RyanTheTide. This update rebases onto integration/aurora (c91527272e90, new head 13f0a8aa2b26) and addresses the blocking items:

  1. Cross-generation shared firmware timestamp window quota (new commit 3, 13f0a8aa2b26). Each backend now reports the size of its fixed firmware timestamp window through Gpu::timestamp_window_size() (IOVA_KERN_TIMESTAMP_RANGE on G13/G14, TIMESTAMPS_SIZE on G15, TIMESTAMP_RANGE on M3, TIMESTAMP_WINDOW on non-M5 G17 via a single Firmware::timestamp_window() helper; 0 on G16, G18, M5, and G17's M5 graph). do_gem_bind_object() reserves the bound range from a per-file 1/16 share (memacct::Client::take_window(), 4 MiB of a 64 MiB window) before calling map_timestamp_buffer(..., window). Past a file's share, G17 falls back to Buffer::Host and G13/G14, G15 and M3 return ENOSPC, leaving the rest of the window available to other clients; buffers mapped outside the window drop the share immediately. The share is per file, matching the review ask and the existing per-file accounting model; a single-client-full-window policy like queue_share can be considered separately if large query pools hit 4 MiB.
  2. Per-bind ledger charge replacing the 4096 count cap (updated commit 2, 5bdb6e744374). Instead of a fixed 4096-object cap returning EBUSY, every bind now charges its kernel record memory (BOUND_OBJECT_BYTES, computed from the types' sizes — 152 bytes on arm64 in the default all-backends config including commit 3's Option<WindowShare>, after storing mmu::KernelMapping directly in FirmwareBuffer without a redundant inner Arc) through Ledger::admit() before taking the table slot or mapping anything, failing with ENOMEM past the client's or device's memory limit. Buffer::firmware_mapping() now returns a borrowed &mmu::KernelMapping, and G13/G14 (fw::job::UserTimestamp), G15 (g15::command/g15::job), and M3 (m3::m3_submit::Destination) commands now retain Arc<timestamp::Bound> alongside G16, G17, and G18, so both the bind charge and the 1/16 window share stay held until every referencing command retires. Commit 1 (2e017801f7bd) is unchanged.
  3. KUnit coverage and all-backends build. Both new suites now cover the range check, host-buffer offset bounds, per-bind ledger charge/refusal, and per-file timestamp-window quota:
    • asahi_timestamp_buffer: a_bound_range_is_whole_pages_inside_the_object and a_host_stamp_lies_inside_the_bound_range (2 of 2 pass).
    • asahi_bound_object_charge: a_bind_is_charged_its_records_and_refused_past_the_client_limit and a_file_holds_at_most_its_share_of_the_timestamp_window (2 of 2 pass).
    • Full KUnit run on this head (13f0a8aa2b26 on c91527272e90, kit pr2531-r4k3, kunit-vz): 80 suites, 395 of 397 cases pass, 0 fail, 2 skips (apple_mbox_g17_stall/apple_g17_rx_util_min_test needing CONFIG_UCLAMP_TASK, and asahi_fwid/j713_accepts_all_loaded_images).
    • All-backends modular build (pr2531-r4m3, G15/G16/G18/M3_T8122/M5 all enabled): builds cleanly with 0 new warnings.
  4. PR body and title updated: the hold-scenario description has been trimmed from "Still to run" and the title updated to match the ledger charge and per-file window share.

Hardware rows still to run on 13f0a8aa2b26: our J775D (M5 Ultra) own-base A/B re-run on c91527272e90 + #1973, plus a non-M5 G17 (J616s or Neo) spillover row and a G13/G14/G15/M3 window-share regression row from machines with a firmware timestamp window.

Assisted-by: LLM (Claude:Opus 5.5, Gemini:Neon)

@funkzy

funkzy commented Nov 24, 2026

Copy link
Copy Markdown
Author

Fresh own-base J775D (Mac Studio, M5 Ultra) A/B hardware row at live head 13f0a8aa2b26 on bare integration/aurora 5746ef8a5afd (no extra PRs needed now that #1973 is merged):

  • Baseline arm: kit pr2531-r4b (window r2531@20261123-1856) — bare integration/aurora 5746ef8a5afd.
  • PR arm: kit pr2531-r4 (window r2531@20261123-1856) — 5746ef8a5afd + this PR at 13f0a8aa2b26.
  • Environment: 27.0 firmware stub (26A428), published Aura main (f13056d95af6, aura-main-f13056d9), read-only root, default kernel command line.
Check pr2531-r4b (base) pr2531-r4 (PR at 13f0a8aa2b26)
4112 binds of a 16 KiB object, one file 4112/4112 accepted, committed +0 bytes (0.0/bind) 4112/4112 accepted, committed +625024 bytes (152.0/bind), released on close
Binds with 16384 bytes left of the 48293 MiB client limit 256/256 accepted, refused 0 107/256 accepted, then ENOMEM (expected 107 at 152 B each), gpu_memory refused 0 -> 1
256 binds of a 256 MiB timestamp buffer (64 GiB of ranges) 223/256, then ENOSPC 256/256 accepted, +0 MiB; client B QUEUE_CREATE ok before and after close
Timestamp CTS (477 + 672 cases, Borealis) 421 Pass / 2 Fail / 54 NS; 672 Pass 421 Pass / 2 Fail / 54 NS (same 2 baseline cases); 672 Pass
GL timer test (gltimer, 3 × 10 rounds) 30/30 Pass 30/30 Pass
Render CTS subset (628 cases, Borealis) 571 Pass, 57 NotSupported, 0 Fail 571 Pass, 57 NotSupported, 0 Fail
GPU recoveries / faults / WARNING / BUG / Oops 0 0
Failed units / kernel errors (ALARM, no SELinux) 2 / 10 same 2 / 11 (1 unrelated DP boot timeout dcp_dptx_connect: timed out waiting for port 0 link configuration; 0 GPU errors)

Assisted-by: LLM (Gemini:Neon)

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