Skip to content

asahi: look up and build background/end-of-tile shaders under the cache lock - #470

Merged
RyanTheTide merged 3 commits into
aurora-silicon:aurora-wipfrom
jgoyvaerts:pr/asahi-bg-eot-cache-lock
Nov 19, 2026
Merged

RyanTheTide merged 3 commits into
aurora-silicon:aurora-wipfrom
jgoyvaerts:pr/asahi-bg-eot-cache-lock

Conversation

@jgoyvaerts

Copy link
Copy Markdown

What

Honeykrisp keeps one agx_bg_eot_cache per device (dev->bg_eot), and command buffers record on any thread. agx_get_bg_eot_shader() searched and inserted into the cache's hash table, allocated from it (rzalloc(cache->ht)) and uploaded to its pool without a lock. Two threads recording render passes with different attachment sets corrupt the table or the pool: double frees, heap corruption or a segfault, with nothing submitted. agx_get_precompiled() already takes cache->lock for uploads to the same pool.

This is the race of upstream Mesa issue 16812 (MR 46059). That MR locks only inside agx_compile_bg_eot_shader(). Here the G17P and M5 native builders also allocate and upload from their own early-return paths, so this change takes the lock around the whole lookup, build and insert instead.

How

agx_get_bg_eot_shader() now takes cache->lock and calls the old body. Nothing under the build path takes the lock again. Neither agx_get_precompiled() nor agx_helper_program() is reached from the builders, and hk's own bg_eot.lock user (hk_cmd_draw.c, M5 tile templates) does not call agx_get_bg_eot_shader() while holding it. A cache miss now compiles with the lock held, so only the first use of each key is serialized. Gallium's caches are per context and only pay an uncontended lock.

New shim test hk-g17p-bg-eot-threads: 16 threads record dynamic-rendering passes over 255 subsets of eight colour attachments with mixed load/store ops, on 20 fresh devices, and nothing is submitted.

Testing

MacBook Pro 16" M4 Pro (J616s, T6040, G17P), GPU firmware 25G83, Linux 7.3.0-m4.171, on AC. Release builds from one build directory with the same meson options:

Guarded runs, 0 GPU recoveries.

aurora-wip 57df4db this PR
hk-g17p-bg-eot-threads (16 threads x 20 devices, no-op DRM shim), 10 runs 10/10 crashed 10/10 pass
the same recording loop on the GPU (16 threads x 5 devices, record only), 10 runs 8/10 crashed (SIGABRT "double free or corruption", SIGSEGV) 0/10 crashed
single-thread recording, 50 devices, wall time (median of 5 alternating) 1.887 s 1.891 s (+0.2 %, within run-to-run spread of 1.82-1.92 s)
meson --suite asahi --suite vulkan - 200/200

The single-thread row shows that the uncontended lock costs nothing measurable when recording.

Commands
# shim, CPU only (the test as meson runs it, with 16 threads and 20 devices)
ASAHI_SHIM_CHIP=g17p VK_DRIVER_FILES=<build>/vk.json LD_PRELOAD=<build>/libasahi_noop_drm_shim.so \
  hk-vulkan-bg-eot-threads 16 20
# GPU, record only: the same program (src/asahi/vulkan/tests/hk_vulkan_bg_eot_threads.c)
VK_DRIVER_FILES=<build>/vk.json hk-vulkan-bg-eot-threads 16 5      # 10 runs per build
time VK_DRIVER_FILES=<build>/vk.json hk-vulkan-bg-eot-threads 1 50 # 5 runs per build, alternating
Raw output
shim:  aurora-wip 10/10 failed (Segmentation fault x5, Aborted x5); this PR 0/10 failed
GPU:   race w0-base: 8/10 crashed      race w2-bgeot: 0/10 crashed
rec1 base 1.894 1.922 1.856 1.823 1.887 s
rec1 pr   1.891 1.923 1.906 1.874 1.854 s

🤖 Generated with Claude Code

jgoyvaerts and others added 2 commits November 19, 2026 03:21
…he lock

Honeykrisp keeps one agx_bg_eot_cache per device (dev->bg_eot), and
command buffers record on any thread, but agx_get_bg_eot_shader() searched
and inserted into its hash table, allocated from it and uploaded to its
pool without a lock. Two threads recording render passes with different
attachments corrupt the table or the pool: double frees and heap
corruption, no submission needed. agx_get_precompiled() already takes
cache->lock for uploads to the same pool.

Take the lock around the whole lookup, build and insert. That also covers
the G17P and M5 native builders, which allocate and upload from their own
early-return paths. Nothing under the build path takes the lock again. A
cache miss now compiles with the lock held, which only serializes the
first use of each key. Gallium's caches are per context and only pay an
uncontended lock.

New shim test hk-g17p-bg-eot-threads (16 threads, 255 attachment subsets,
20 fresh devices): fails 3 of 3 runs before (double free or corruption,
malloc(): unsorted double linked list corrupted, SIGSEGV), passes 10 of 10
after. On the M4 Pro itself the same recording loop crashed 5 of 10 runs.

Same race as upstream Mesa issue 16812 (MR 46059, which locks only inside
agx_compile_bg_eot_shader() and would miss the native builders here).

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Assisted-by: LLM
Keep both sets of shim tests in src/asahi/meson.build and
src/asahi/vulkan/meson.build next to the ones aurora-silicon#444 added.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown

After #444 landed this branch conflicted with aurora-wip in src/asahi/meson.build and src/asahi/vulkan/meson.build only: #444 added its own shim test blocks at the same spot. I merged aurora-wip into the branch with both sets of blocks kept whole. Nothing else changed, and your commits are untouched. The x86 build plus asahi suite passes with both this and #470 on top of aurora-wip. Next is a G17P CTS row on our M4 Pro, then merge. Whichever of the two lands second will need the same small meson update again, and I can do that too.

Keep this branch's shim test blocks next to the ones aurora-silicon#436 added in
src/asahi/meson.build and src/asahi/vulkan/meson.build.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide
RyanTheTide merged commit 67066c9 into aurora-silicon:aurora-wip Nov 19, 2026
@RyanTheTide

Copy link
Copy Markdown

Merged. Rows: M4 Pro (J616s, G17P) in the same overlay as #436, and M1 Pro (G13S) A/B on aurora-wip 9e85c78 with and without this head. dEQP-VK 374,140 cases identical. GLES2/3/31 + KHR 106,812 cases identical apart from one flush_finish timing case, which passes 30/30 standalone in both arms. 0 firmware crashes.

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.

2 participants