Skip to content

hk: give M5 barriers memory scope only while a host import may alias - #552

Draft
DjDeveloperr wants to merge 8 commits into
m5/pack-strong-flush-tailfrom
m5/host-import-alias-scope
Draft

DjDeveloperr wants to merge 8 commits into
m5/pack-strong-flush-tailfrom
m5/host-import-alias-scope

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 19, 2026 •

Copy link
Copy Markdown

Rebased 11-19 23:4x onto the rebased #506 (c3c7b08c, on #488 ee3de2b5). The head is now 0cde1823 (pushed before: 9d9275f6 on #488 532fbf4d, and c9644e73). Range-diff against c9644e73:

Local checks ran per commit, all 8. Each got a host build at -j2, an asserts-on syntax check of the changed files, hk_pack_test (145/145) and hk_host_alias, each plain and with ASan/UBSan. A focused independent re-review (Opus 5.5, xhigh) was CLEAN. Its minor findings were in #506 and are fixed there.

On the M5: slot s8 runs this head. After #488's packing gate and the packing off/on timing, it runs the alias A/B (static vs per-resource rule). That covers Geekbench ABBA, packtest's alias cases under both rules, and dEQP alias.txt under both rules.

Earlier rebase (11-19 22:1x, head c9644e73)

Onto the rebased #506 (0e5d304f, on #488 2c4692477bb6 / aurora-wip af7ec5f4). The head is now c9644e73, previously 4394e0b2. Range-diff:

  • Unchanged: commits 1, 4, 5, 6 and 8.
  • Commit 2 (167eaff3): now registers hk_cdm_note_alias_site() in aurora-wip's new ordering-only compute-barrier path, replacing the old hk_cdm_visibility_flush_needed(..., alias_scope) gate.
  • Commits 3 (e0346f4e) and 7 (acaac171): adapted to the same upstream code.
  • Commit 2 also meets two upstream changes merged since the old base:
    • a787ee3c (hk: give G17P dependencies memory scope while a writable import lives #472): counts fd and writable host imports in va_alias_resources through alias_import, for G17P. Its lines are kept next to ours (host_alias_tracked, alias_fd), so G17P behaves as upstream and M5's count is unchanged.
    • 1142bcb9 (opt-in HK_PERFTEST=weakbarrier): keeps the launch's weak record instead of a flush when no alias scope is asked for. In the per-resource mode that record now registers a patch site (hk_cmd_alias_scope() plus hk_cdm_note_alias_site()), so it gets the memory scope if an alias appears later. Outside that mode nothing changes.

The sibling-queue guard is kept, so packed submits stay off sibling queues. Local checks on the new head: host build at -j2, an asserts-on syntax check, and the hk_host_alias unit test, plain and with ASan/UBSan. The focused independent re-review of the four changed resolutions was CLEAN.

Draft. Stacked on #506 (base m5/pack-strong-flush-tail), which is based on aurora-wip (#488 merged as 11ee6231). Commits 1–5 decide the memory scope from the live host imports; commits 6–8 widen that to every live resource through which memory has a second GPU address (see "Widening (commits 6–8)"). Commits 6–8 ran on the M5 Pro in the lane's form (results below); this exact port has not.

What and why

On M5, a strong CDM flush needs memory scope only where a dependency's write and read reach the same pages through two GPU addresses. That was measured on hardware in 23644ad6e3c ("hk: decide per device whether M5 barrier flushes need memory scope"): without the scope (0x60001560) such a read returned stale data for every word, and with it (0x6000157c) exact data. That commit decides the question once per device. A device that enabled any feature through which memory could get a second GPU address keeps the memory-scoped flush at every application dependency for its whole life. One such feature is VK_EXT_external_memory_host with GPU-writable imports.

Geekbench 6 enables VK_EXT_external_memory_host. On the M5 Pro the memory scope costs 10–14% of GPU time:

  • Background Blur: 8179 → 7035 µs/iter without it.
  • Face Detection: 13572 → 12168 µs/iter without it.

Both were measured with a measurement-only switch that turns the scope off (HK_DEBUG_M5_NO_ALIAS_SCOPE=1). That switch is not part of this PR. A host import gives its pages a second GPU address only if they already have one: they are imported again, or mapped at another virtual address that something else imports. This PR makes the M5 rule follow the host imports that actually exist, instead of the extension being enabled.

The rule

The device starts without memory scope and counts its live host imports that may alias (host_alias_live). This applies only when writable host imports are the device's only reason for memory scope (host_alias.dynamic), and only on M5.

Unaliased imports. An import is unaliased when both of these hold:

  • Its mappings allow no other address of its pages. For a GPU-writable import, its whole range must be private anonymous memory of the process. For a GPU-read-only import, any private mapping is enough, anonymous or of a file.
  • It overlaps no other live import of the device of which one of the two is GPU-writable. Two read-only imports of the same pages never count, since the GPU writes neither.

Private anonymous memory means a /proc/self/maps entry that is private with no pathname, [heap] or [anon:...]. The maps are read before the kernel pins the pages and again after, and the worse class counts. Overlapping imports alias together until one of them is freed. hk_host_alias.c holds the pure classifier and the import set, with a host unit test (tests/test-hk-host-alias.c).

Patch sites. Every application dependency between compute and transfer work that is recorded without memory scope because of this rule records a patch site. A site is the stream word of its barrier record: the strong flush, or the stream's last ordering record at its end. The memory-scoped strong flush is written over it:

  • At submission, while an import may alias, before submit packing copies the streams. Packing copies under host_alias.patch_lock held shared, and only around the copy; the kernel ioctl runs after the lock is released.
  • For every executable command buffer, when the count of aliasing imports leaves 0 (hk_device_patch_alias_sites(), which takes the lock exclusively).
  • At vkEndCommandBuffer when an import may alias by then.

A site stays a barrier record: an application dispatch after it no longer drops it, and an inline stream merge moves it with the copied bytes. Writes only add memory scope, until the command buffer is reset. Command memory on M5 is uncached, so the writes need a fence, not cache maintenance.

End-of-command flush. In dynamic mode, every M5 compute stream that holds launches ends with the memory-scoped strong flush at vkEndCommandBuffer. Patch sites cover reads by work recorded before an import. The end flush covers writes made before an import, which may later be read through:

  • a new import of the same pages,
  • an import that replaced a freed one, or
  • another VkDevice's or API's import of the same pages, which Vulkan orders after this work only at a submission boundary.

#488's packing keeps that flush only at a packet's end while no import may alias.

Paths that keep the old rule:

  • Events. A command buffer that uses events keeps memory scope throughout, because a host-set event could let an execution that started before an import reach it.
  • Graphics. Graphics paths record no site and keep the scope while the mode is on: transfers ordered before a draw, and task launches. Graphics commands get no end-of-command flush, as under the old rule.
  • Other features (commits 1–5 only). A device that enabled sparse binding or residency, capture/replay, VK_KHR_external_memory_fd or VK_EXT_external_memory_dma_buf keeps the per-device rule. Commits 6–8 replace this with the per-resource count (see "Widening (commits 6–8)").
  • Non-M5 chips are unchanged. G17P keeps its rule, and G16G (hk: pack runs of plain compute command buffers into one command #488's other packing target) has no memory-scoped form and never enters the mode.

Switches:

  • ASAHI_M5_HOST_ALIAS_STATIC=1 restores the per-device rule for host imports, for an A/B on the same build and as a fallback.
  • ASAHI_M5_HOST_ALIAS_TRACE=1 prints each import's class, the live count, and how many command buffers were patched.

Known limits. Both need a kernel bit, DRM_ASAHI_GEM_USERPTR reporting that a page it pinned was already pinned long-term (folio_maybe_dma_pinned()), which would then count as aliasing:

  • An application that imports a range, moves it with mremap, and imports the new address too has the first import's pinned pages under a private anonymous mapping that overlaps no import. Within one submission, a write through one import, a barrier and a read through the other would read stale data. Across submissions, the end-of-command flush still covers it. The plain move leaves the first import's range unmapped, which the extension does not allow while the import lives. This is documented in hk_host_alias.h.
  • The race between reading the maps and the pin is narrowed (maps read before and after the pin, worse class wins), not closed.

Review history

An independent code-only review of the first two commits found it NOT CLEAN: 2 BLOCKER, 1 MAJOR and 4 MINOR.

  • BLOCKERs: writes made before an aliasing import were never flushed with memory scope, and the same pages could be imported by another VkDevice or API, which the per-device set cannot see. Both are fixed by the end-of-command flush (3rd commit).
  • MAJOR: mremap can give pinned pages a second virtual address. This cannot be fixed without the kernel, so it is documented as a known limit (3rd commit).
  • MINORs: the maps were read only after the pin, and the pack lock was held across the ioctl; both are fixed in the 4th commit. Another MINOR concerned an opt-in fold path that is not part of this tree.

A re-check of the fixes was CLEAN. Remaining MINORs:

  • the mremap limit and the narrowed maps/pin race above;
  • graphics commands get no end-of-command flush, as before (the 5th commit corrects the comment that said otherwise);
  • the end-of-command flush costs one memory-scoped flush per unpacked compute command.

Hardware validation

Setup: M5 Pro (J714s, T6050, G17S, 20-core GPU), Arch Linux ARM, kernel 7.3.0-m5-intg16. Slot s4c, 2026-11-19 14:01–14:11.

Mesa revision tested: 214f4ea9f71 of the M5 driver-overhead lane's local branch, driver sha256 0f62d8438ab8…. That branch carries the same five commits on top of the lane's own packing commits, which #488 ports, and the repeated-recording fix (#444 fixes that bug publicly). It also carries opt-in switches that are off by default and not part of this tree: light barrier, overlapped dispatches, extra frame-anatomy stamps, and a whole-submission packer. This PR's exact port has not run on hardware. The port differs from the tested commits only where that absent code was touched (listed below).

packtest (the lane's Vulkan compute submission test; each case's output is compared word for word with a CPU model, including guard words; 4 iterations per case):

  • 23/23 cases PASS (plus sem and dup) in three arms: packing off with the new rule, default packing with the new rule (with ASAHI_M5_HOST_ALIAS_TRACE=1), and the static rule (ASAHI_M5_HOST_ALIAS_STATIC=1).
  • That includes the host-import cases:
    • hostpriv: a dependent chain on an imported private allocation.
    • hostalias: two imports of the same pages, the second created before the command buffers were recorded.
    • hostalias-late: the same, with the second import created after recording, so it exercises the patch path.
    • hostalias-xsub: a write before an import, read through it in a later submission.
  • Negative control with the memory scope forced off (HK_DEBUG_M5_NO_ALIAS_SCOPE=1, lane-only): hostalias and hostalias-late MISMATCH in every iteration, as designed. The test therefore detects a missing memory scope on this hardware, and the new rule's scope is what makes those cases pass. hostalias-xsub passed even without the scope; the end-of-command flush stays, because it covers that case by construction.

dEQP-VK alias gate (the kit's cts147 build, 16 jobs): 6,197 mustpass cases from memory.external_memory_host, api.external.memory, memory.binding, sparse_resources.buffer, buffer-device-address capture/replay, memory.pipeline_barrier, synchronization{,2}.basic, synchronization2.op.single_queue.barrier, compute.pipeline.basic, and 1/8 of memory_model.message_passing.

  • Result under both the new and the static rule: 4,730 Pass, 1,467 NotSupported, 0 Fail, 0 Crash.
  • The two runs are identical case for case.

No gain yet

Geekbench 6.7.1 compute (Vulkan), GPU time per iteration, static rule vs new rule in the same slot:

  • Background Blur: 8282 → 8150 µs (−1.6%).
  • Face Detection: 13604 → 13545 µs (−0.4%).
  • All other workloads within ±2%.

The import trace printed nothing, so Geekbench's device never entered the dynamic mode. The likely reason, not yet confirmed: the M5 exposes sparseBinding, sparseResidencyBuffer and bufferDeviceAddressCaptureReplay, and a device that enables them keeps the per-device rule whatever its imports.

Widening plan

  • (a) Trace the reason. Done in commit 6.
  • (b) Make the other features dynamic. Done in commits 7–8.
  • (c) Validate. The independent review is done and the lane's link check passed; the hardware gate is still pending (see below).

Widening (commits 6–8)

What and why. In slot s4c, Geekbench's device kept the per-device rule: no import was traced, and its GPU time did not change with the per-import rule. The likely reason is that it enables sparse binding or buffer device address capture/replay, which the M5 exposes.

  • Commit 6 adds, with ASAHI_M5_HOST_ALIAS_TRACE=1, a line at device creation that names every enabled feature or extension through which memory can get a second GPU address, and the rule the device took. Under the per-device rule it also traces each host import, so a trace shows whether an application imports at all. A slot is pending that runs Geekbench with this trace to confirm which features its device enables.
  • Commits 7–8 decide the scope per resource on every M5 device that enabled any of these features (host_alias.dynamic): sparse binding and residency, buffer-device-address, descriptor-buffer or acceleration-structure capture/replay, fd/dma-buf memory, and writable host imports. Memory scope applies while the device's live aliasing count, alias_live, is not 0. G17P keeps its record-time rule, and other chips are unchanged.

The per-resource count (alias_live). It counts:

  • Sparse buffers and sparse image planes, from their creation (before memory can be bound to them) to their destruction, including when a later step of the image's creation fails.
  • Capture/replay buffers (each has a GPU address of its own over other memory), and memory mapped again in the capture/replay region for descriptor buffers (replay_va). Together with the sparse resources these are the existing va_alias_resources, which G17P still reads on its own.
  • fd and dma-buf imports, each for its lifetime, because another import (of this device, or of another device or process) may map the same pages.
  • Host imports that may alias, as in commits 1–5. They are added and removed under host_alias.lock, together with the import set.

Acceleration-structure capture/replay adds no address on M5: a structure's address is its buffer's, also on replay. Image views and samplers replay descriptor indices only.

Every increment goes through one helper, hk_device_alias_add(), which returns whether this call took the count from 0. When it did, hk_device_alias_began() writes the patch sites of every live command buffer before the call that creates the resource returns; host imports call it after releasing the lock. Each subtraction follows its own addition, so the count never wraps.

Everything else from commits 1–5 applies unchanged to every dynamic M5 device: patch sites, the end-of-command flush, the submit-time patch and the pack read lock. Two more changes:

  • Commit 7 also fixes a pre-existing leak: a failed shadow bind or host-import classification no longer leaks a capture/replay mapping and its count.
  • ASAHI_M5_HOST_ALIAS_STATIC=1 now restores the per-device rule for all of these features. With ASAHI_M5_HOST_ALIAS_TRACE=1, the trace prints rule=per-resource|per-device|none, and each time the count leaves 0 it prints the cause (import, sparse, capture-replay or fd) and how many command buffers were patched.

Exports are not counted. Exporting memory (vkGetMemoryFdKHR, or an allocation made exportable) gives it no second address in this device. Another device or process that imports it reaches it only across a submission boundary: an external semaphore or fence, or a host wait. Every compute command on a dynamic M5 device ends with the memory-scoped flush, so this device's writes are in memory by then. The importer counts its own import: an M5 fd import counts, and a per-device importer flushes every dependency. Importing the fd back into this device goes through the import path, which counts.

Review history. An independent code-only review of commit 7 was NOT CLEAN, with one BLOCKER (B1):

  • The host-import path computed how many imports began to alias under host_alias.lock, but added that number to the count only after releasing the lock, while freeing an import subtracted under the lock.
  • So a free of an overlapping import could subtract first and wrap alias_live. A sparse, capture/replay or fd resource created at that moment would then take the count back to 0 instead of from 0. No patch ran, and the count read 0 while the resource was live.

Commit 8 adds the host imports' count under the lock again. The re-check was CLEAN. The review also asked for more coverage, which the sparsealias-xsub, cralias and cralias-pending cases below provide.

New packtest cases. They run on a third test device that enables sparse binding, buffer-device-address capture/replay and update-after-bind sets where the device has them. Each case SKIPs without its features.

  • sparsealias: a sparse buffer is fully bound to the slot memory before recording. Dependent dispatches alternate between the slot buffer and the sparse buffer, then copy what the sparse buffer wrote through the slot buffer.

  • sparsealias-late: the same, but the sparse buffer is created, bound and written into the descriptor set after recording and before submission (update-after-bind).

  • sparsealias-xsub: one command buffer writes through the slot buffer and completes. Only then is the sparse buffer created, and a second submission reads every slot through it. This is the end-of-command-flush case, for sparse.

  • cralias: like sparsealias, with a capture/replay buffer bound to the slot memory.

  • cralias-pending: the command buffers are submitted behind a timeline semaphore that the host signals. The capture/replay buffer is created after vkQueueSubmit returns and before the signal, and the shaders reach it through a device address they load.

  • sparsealias-pending: the same with a sparse capture/replay buffer, bound on a second queue (one queue cannot: the bind would wait behind the pending submission).

Every case that creates the aliasing resource after recording re-records its command buffers each iteration, so each iteration exercises the 0 -> N transition.

Hardware results for commits 6–8 (lane form)

M5 Pro (J714s), kernel 7.3.0-m5-intg26, the lane's build of these commits (195aa0790d8, driver sha256 26365cb8…), slot s6, 2026-11-19 16:40–16:50. This PR's exact port has not run on hardware.

Why Geekbench gained nothing before: the device trace (previous slot, s5t) showed that Geekbench 6.7.1's Vulkan devices enable sparseBinding sparseResidencyBuffer sparseResidencyImage2D sparseResidencyAliased, plus writable VK_EXT_external_memory_host on one of them. The sparse features kept the per-device rule. With commits 6–8, every Geekbench device runs rule=per-resource may_alias=0. Its four host imports (95 MB and 24 MB ranges) are private anonymous memory with no alias, and no aliasing resource is created during the run.

Geekbench 6.7.1 compute (Vulkan), new rule vs ASAHI_M5_HOST_ALIAS_STATIC=1 in the same slot:

Workload Iteration time (two runs each) GPU time per iteration
Background Blur 9544 -> 8428 µs (−11.7%) 8177 -> 7031 µs (−14.0%)
Face Detection 13995 -> 12800 µs (−8.5%) 13566 -> 12202 µs (−10.1%)
Particle Physics 25051 -> 24233 µs (−3.3%) 24550 -> 23934 µs (−2.5%)
Feature Matching −2.0% −0.2%
Stereo Matching −1.4% −4.4%
Gaussian Blur / Edge Detection −0.2% / +0.4% −0.2% / −1.0%
Horizon Detection +9.5%: its CPU phase is bimodal between runs (static 12.0/14.0 ms, new 16.5/12.0 ms) −0.4%

Known measurement artifact: Horizon Detection. Its +9.5% is not an effect of this change. Its GPU time is unchanged (−0.4%), and its iteration time is dominated by Geekbench's own CPU phase between GPU waits, which is bimodal from run to run regardless of driver configuration. In this slot the static-rule runs took 12.0 and 14.0 ms per iteration and the new-rule runs 16.5 and 12.0 ms. The same swing appears between identical configurations in earlier slots (13.9 vs 16.8 ms). Read Horizon from GPU time, not iteration time.

Correctness:

  • packtest (31 cases, word for word): all pass with packing off, with the new rule and with the static rule, including the ten alias cases above.
  • Negative control (memory scope removed): hostalias, hostalias-late, sparsealias, sparsealias-late, cralias, cralias-pending and sparsealias-pending mismatch in every iteration, so each detects a missing scope on this hardware. The -xsub cases pass even then; the end-of-command flush covers them by construction.
  • dEQP-VK alias gate (6,197 cases: external/host memory, memory binding, sparse buffers, BDA capture/replay, pipeline barriers, synchronization, compute, memory-model sample): 4,730 Pass, 1,467 NotSupported, 0 Fail or Crash under both rules.
  • gpubench: its devices enable no aliasing feature, so both rules run identical driver paths. Its compute rows were unchanged within run-to-run noise.

Port from the tested commits

These are the only differences from the tested lane commits:

  • The light-barrier branch in hk_CmdPipelineBarrier2(), and its mentions in comments and messages, are dropped. This tree has no light barrier, so a site is always a strong flush or a launch's ordering record.
  • The HK_DEBUG_M5_NO_ALIAS_SCOPE block in hk_CreateDevice() and its comment are dropped; that switch is lane-only.
  • A lane-only frame-anatomy helper and the submit entry stamp in the commit-2 context are dropped.
  • The pack lock wraps hk: pack runs of plain compute command buffers into one command #488's queue_submit() → hk_pack_runs() instead of the lane's queue_submit_unique() → hk_pack_submit()/hk_pack_runs(). It is still held shared only around the copy.
  • The ReLU6/padding fold gate (try_relu6) is dropped, so the 4th commit is titled "...classification and pack locking".

Commits 6–8 needed these adaptations:

  • The device-trace call goes right after the per-device decision in hk_CreateDevice(), because the lane-only HK_DEBUG_M5_NO_ALIAS_SCOPE block it followed is absent.
  • A comment hunk on the absent light barrier and the lane-only frame-anatomy stamp in the commit-7 context are dropped.
  • In the messages, references to lane SHAs became commit subjects or "the previous commit", and "HK_DEBUG_M5_NO_ALIAS_SCOPE is unchanged" is dropped.
  • Everything else applied as in the lane, including the capture/replay leak fix, whose failure paths exist in this tree.

#488's hk_pack_runs() M5 branch needed no change: it already strips memory scope between members only while hk_cdm_alias_scope() is false and keeps it at the packet's end. G16G is untouched.

Not run: this PR's exact port on any hardware; commits 6–8 on any hardware, in any form; G16G (by construction it never enters the mode); other M5 parts.

Host checks (all eight commits):

  • release build of the Vulkan driver (clang), with no warnings in changed files;
  • the changed files syntax-checked with asserts enabled;
  • the host unit test test-hk-host-alias.c built standalone and run, plain and with ASan/UBSan.

packtest is the lane's test program and is not published yet. Raw logs of every arm are kept with the lane.

Environment switches

Environment switches and the cleanup in #551: aurora-wip is removing driver environment switches (#551). This stack relies on HK_PACK_CDM_SUBMIT, HK_PACK_ATOMICS and HK_PACK_CDM_TRACE from #488, and on ASAHI_M5_HOST_ALIAS_STATIC and ASAHI_M5_HOST_ALIAS_TRACE from #552. They stay until each PR is merged and verified on hardware, as the A/B and fallback for that verification. One follow-up PR then removes them all.

🤖 Generated with Claude Code

@DjDeveloperr

Copy link
Copy Markdown
Author

Independent code reviews

Two independent reviewers (Opus 5.5, xhigh), each given only the code and the hardware rule from 23644ad6e3c. Neither had our conclusions.

Commits 1–5 (per-import rule)

  • First review: NOT CLEAN.
    • B1 (BLOCKER): a write made before an aliasing import was never written back with memory scope. A command buffer that ended in a plain barrier, followed later by a second import of the same pages and a read through it, read stale data.
    • B2 (BLOCKER): the import set is per VkDevice, so it misses another device or API that imports the same pages.
    • MAJOR: mremap can give pinned pages a second virtual address.
    • MINORs: a maps/pin race, the patch-lock hold time, the ReLU6 fold disjointness check, and the classification cost.
  • Fixes:
    • Every M5 compute stream with launches now ends memory-scoped while the dynamic rule is in force; packets keep that only at their end. This fixes B1, and B2, since devices are only ordered at submission boundaries.
    • /proc/self/maps is read both before and after the pin, and the worse result is used.
    • The pack lock now covers only the copies.
    • The mremap case is a documented limit; closing it needs a kernel bit that reports already-pinned pages.
  • Re-check: CLEAN, no BLOCKER or MAJOR.

Commits 6–8 (per-resource widening)

  • First review: NOT CLEAN.
    • B1 (BLOCKER): a host-import race. The import path added to the alias count after releasing its lock, while the free path subtracted under it. A concurrent free could wrap the count, so a sparse buffer created at that moment left the count at 0 while the buffer was live.
    • m1: with that wrap, the 0→N global patch could be skipped.
    • m2: no negative control, and the late cases exercised the 0→N patch only in iteration 0.
    • m3: the reason given for omitting a sparse pending case held only for one queue.
  • Fix (commit 8): the add happens under the import set's lock again, and the all-command-buffer patch runs after the lock is released.
  • Re-check: CLEAN.
    • B1 and m1 are closed.
    • There is no new race or lock-order problem.
    • No add/subtract pair can wrap.
    • The test cases do put the write and the read through two GPU addresses.
  • Test follow-ups (all in packtest):
    • Command buffers are re-recorded every iteration.
    • A two-queue sparsealias-pending case was added.
    • The negative control was run on hardware (see the body: every alias case mismatches with the scope removed).

🤖 Generated with Claude Code

The pages of a host import have another GPU address only if they are
mapped at another virtual address the GPU also reaches, or imported
again. Add the pure functions an M5 device needs to tell
(hk_host_alias.c):

- a parser of /proc/self/maps lines into private anonymous memory (no
  pathname, [heap], or a [anon:...] name, without an inode), private
  mappings of a file, and everything else: shared mappings of any kind,
  /dev and anonymous-inode mappings, [stack] and other special regions;
- a walk that folds the lines over a range in their ascending order,
  skipping the lines below it and stopping at the line that decides it,
  where a gap, an unparseable line or a line past the end of the file
  decide against the range;
- the set of a device's live imports, which counts for each import the
  live imports overlapping it of which one of the two is GPU-writable;
  two read-only imports of the same pages do not count, as the GPU
  writes neither.

Nothing uses them yet. The host test runs them on synthetic maps text,
on the test process's own private, shared and holed mappings, and on
overlapping writable and read-only imports.

Signed-off-by: Diljit Singh <hey@djdev.me>
An M5 device that enabled VK_EXT_external_memory_host without
hk_host_memory_import_read_only flushed with memory scope (0x6000157c)
at every application dependency between compute and transfer work
(hk_device_may_alias_memory()), instead of the plain strong flush
(0x60001560). Memory scope is needed
where a dependency's write and read go through two GPU addresses of the
same pages, but an import gives its pages a second address only if they
have one already: imported again, or mapped at another virtual address
that something else imports. Geekbench 6 enables the extension, and on
the M5 Pro the memory scope cost 10-14% of GPU time in Background Blur
(8179 -> 7035 us/iter without it, measured with a measurement-only
switch, HK_DEBUG_M5_NO_ALIAS_SCOPE=1, that this tree does not carry) and
Face (13572 -> 12168).

Where writable host imports are the device's only feature that can give
memory a second address, decide it from the imports that exist
(host_alias.dynamic): the device starts without the scope and counts its
live imports that may alias (host_alias_live). An import may alias
unless it is GPU-read-only, or its whole range lies in private anonymous
mappings of the process (/proc/self/maps: private, and no pathname,
[heap] or [anon:...]), and it overlaps no other live import of the
device of which one of the two is GPU-writable; a read-only import needs
only private mappings. Overlapping imports alias together until one of
them is freed. Sparse binding and residency, capture/replay, fd and
dma-buf imports keep the per-device rule, and G17P and other chips are
unchanged.

The scope must still cover work recorded or submitted before an import
that aliases, which can reach it through a device address it loads or a
descriptor updated after bind. Every application dependency recorded
without memory scope because of this rule is a patch site: the stream
word of its barrier record (the strong flush, or a stream's last
ordering record at its end), over which the memory-scoped
strong flush is written once an import may alias. A site stays a barrier
record: an application dispatch after it no longer drops it, and an
inline stream merge moves it with the copied bytes. The sites of a
command buffer are written

- at submission, while an import may alias, before submit packing copies
  the streams (hk_queue_submit()). Work behind a semaphore the host
  signals reaches the driver only once the signal is submitted, since
  the runtime holds it on a submit thread until its waits are
  (VK_SYNC_WAIT_PENDING), so this sees every import the work may reach;
- for every executable command buffer, when the count leaves 0
  (hk_device_patch_alias_sites());
- at vkEndCommandBuffer when an import may alias by then, or the command
  buffer uses events: a host-set event could let an execution that
  started before an import reach it, so such command buffers keep memory
  scope throughout, as do the dependencies of graphics paths, which
  record no site.

Writes only add memory scope, until the command buffer is reset. The
first write of a command buffer's sites excludes submit packing
(host_alias.patch_lock), so a copy never takes half a word, and each
word is stored whole for the GPU. Command memory on M5 is uncached, so
the words need a fence, not cache maintenance.

ASAHI_M5_HOST_ALIAS_STATIC=1 restores the per-device rule for host
imports, for an A/B on the same build and as a fallback.
ASAHI_M5_HOST_ALIAS_TRACE=1 prints each import's class and the count.

Run on the M5 Pro in the lane's form of this series (packtest incl. host-import cases,
the negative control and a 6,197-case dEQP-VK alias gate, see the pull request); this
port itself has not run on hardware.

The ordering-only compute barrier of HK_PERFTEST=weakbarrier keeps the
launch's weak record instead of a flush; while the scope follows the
imports, that record is a patch site as well.

Signed-off-by: Diljit Singh <hey@djdev.me>
With the scope following the live host imports (previous commit), the patch
sites give memory scope to reads recorded before an import that aliases,
but not to writes made before it. An application barrier at the end of a
command buffer leaves the strong flush without memory scope (0x60001560),
which vkEndCommandBuffer did not upgrade, so a command could end with its
writes still in the GPU caches under the first import's address. Pages
written through one import in one submission, then imported a second
time, and read through the new import in a later submission, were never
flushed with memory scope in between. The same holds for a read-only
import added over pages a writable import wrote, for an import that
replaced a freed one of the same pages, and for the same pages imported
by another VkDevice or API of the process, which the device's set of
imports cannot see.

With host_alias.dynamic, end every compute stream holding work with the
memory-scoped strong flush at vkEndCommandBuffer, upgrading a strong
flush without memory scope in place as a weak tail already was, or
appending it where the last record is no barrier. Every command then
writes back what it wrote by its end, as the memory scope of each
barrier did under the per-device rule, whatever imports exist later.
Vulkan orders the work of two devices, or of Vulkan and another API,
only at submission boundaries, each of which now follows a memory-scoped
flush. Launch tails are weak records, upgraded in place. Submit packing
(hk_pack_runs()) still strips the scope between packet members while no
import may alias and keeps it at the packet's end, so a packet pays one
memory-scoped flush.

Moving an imported range with mremap and importing the new address is
not seen: the first import's pinned pages are then under a private
anonymous mapping that overlaps no import. Document it where the classes
are defined (hk_host_alias.h, which called mremap safe), with why the
extension rules it out and the kernel check that would see it.

Signed-off-by: Diljit Singh <hey@djdev.me>
Two follow-ups to the M5 host-import alias scope ("hk: give M5 barriers
memory scope only while a host import may alias"):

- The mappings over a host import were read after the kernel pinned its
  pages, so an application that replaced the mapping between the two was
  classified by the later one only. Read /proc/self/maps before the pin
  as well and take the worse class.
- A submission held host_alias.patch_lock shared through the whole
  submit, kernel ioctl included, so the first aliasing import waited for
  every submission in flight. Only submit packing reads stream words on
  the CPU (hk_pack_runs(), which copies them); hold it just around that.
  The GPU reads each word whole.

Signed-off-by: Diljit Singh <hey@djdev.me>
…asing

The comment on hk_cdm_alias_scope() said every command holding compute
or transfer work ends with the memory-scoped flush while host imports
decide the scope. Only compute streams with launches do; graphics
commands, including transfers done through a render pass, get no such
flush, exactly as under the per-device rule, which never gave M5
graphics memory scope. The M5 member check of submit packing
(hk_pack_run_member()) now also says that under host_alias.dynamic
every stream with launches ends memory-scoped, whatever its last
record. Comments only.

Signed-off-by: Diljit Singh <hey@djdev.me>
With ASAHI_M5_HOST_ALIAS_TRACE=1, name at device creation every enabled
feature or extension through which memory can get a second GPU address
(sparse binding and residency, buffer device address, descriptor buffer
and acceleration structure capture/replay, fd and dma-buf memory,
writable host imports), and whether the device decides the barrier
memory scope per import or per device. Under the per-device rule, also
say when a host import is made, so a trace shows whether an application
imports at all.

On the M5 Pro, Geekbench 6's device took the per-device rule (no import
was traced and its GPU time did not change with the per-import rule);
this names the reason.

Signed-off-by: Diljit Singh <hey@djdev.me>
The per-import rule ("hk: give M5 barriers memory scope only while a
host import may alias") applied only to devices whose sole feature that
can give memory a second GPU address was writable host imports. On the M5 Pro, Geekbench 6's device enables sparse binding and
residency and buffer device address capture/replay (both exposed), so
it kept the per-device rule and the memory-scoped flush (0x6000157c) at
every application dependency between compute and transfer work.

Decide it per resource on every M5 device that enabled such a feature
(host_alias.dynamic): memory scope while the device's live aliasing
count (alias_live) is not 0. It counts

- buffers and image planes with a GPU address of their own over other
  memory, sparse ones and capture/replay buffers, and memory mapped again
  in the capture/replay region for descriptor buffers: the existing
  va_alias_resources, each from its creation, before memory can be bound
  to it, to its destruction, also on every failure path. A failed shadow
  bind or host-import classification no longer leaks a capture/replay
  mapping and its count;
- fd and dma-buf imports, each for its lifetime, since another import of
  this or another device or process may map the same pages;
- host imports that may alias, as before.

Acceleration structure capture/replay adds no address on M5: a
structure's address is its buffer's, also on replay
(vk_common_CreateAccelerationStructureKHR()), and image views and
samplers replay descriptor indices only. Both are dynamic with nothing
of their own to count. An export (vkGetMemoryFdKHR) reaches another
device or process only across a submission boundary, after the
memory-scoped flush that ends every command; imported back here it
counts as an import.

Every increment goes through hk_device_alias_get(), which runs the patch
of all live command buffers (hk_device_patch_alias_sites()) when the
count leaves 0, before the creating call returns; the count is atomic,
exactly one caller sees it leave 0, and a creation that adds to a count
another caller is still patching for is covered by the check at
submission. The rest of the per-import machinery applies unchanged:
patch sites, the memory-scoped flush that ends every command, the pack
read lock. G17P keeps its record-time rule and other chips are
unchanged. ASAHI_M5_HOST_ALIAS_STATIC=1 restores the per-device rule for
all of these features (A/B and fallback). With ASAHI_M5_HOST_ALIAS_TRACE=1 the device trace names the
rule (per-resource, per-device or none), and each time the count leaves
0 its cause (import, sparse, capture-replay, fd) and how many command
buffers were patched.

Signed-off-by: Diljit Singh <hey@djdev.me>
The previous commit moved the add of the host imports that began to
alias to after host_alias.lock was released, while their removal still subtracts
under it. A free of an overlapping import could then subtract first and
wrap alias_live. A sparse, capture/replay or fd resource created at that
moment took the count back to 0 instead of from 0: no patch of the live
command buffers ran, the count read 0 while the resource was live, and a
command buffer recorded then wrote through one address, flushed without
memory scope, and read through the resource's (an import B over two
imports A and C: B's add of 3 delayed, A's free subtracting 1, a sparse
buffer's add returning 0). For the same reason the add that took the
count from 0 could miss seeing n, and the patch was skipped.

Add the imports' count under host_alias.lock again, together with the
set that says how many of them alias, through hk_device_alias_add(),
which returns whether this call took the count from 0; run the patch of
every live command buffer (hk_device_alias_began()) after the lock is
released when it did. The other callers keep hk_device_alias_get(). The
count now never wraps: each sparse, capture/replay and fd resource
subtracts only in the destruction that follows the call that created it
and added, and host imports add and subtract under the set's lock, so
exactly the add that leaves 0 sees n. Lock order is unchanged.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the m5/pack-strong-flush-tail branch from 97635e9 to c3c7b08 Compare November 19, 2026 23:53
@DjDeveloperr
DjDeveloperr force-pushed the m5/host-import-alias-scope branch from 9d9275f to 0cde182 Compare November 19, 2026 23:53
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.

1 participant