Repository navigation
asahi: handle failed BO allocations instead of crashing - #1415
Conversation
The kernel's GPU memory policy moves to the standard model: allocations
fail with ENOMEM after the driver's own reclaim, below a device-wide
ceiling. Gallium asahi crashed on that. The batch encoder dereferenced a
NULL BO, a pool aborted ("cannot allocate"), and a dozen descriptor
builders aborted on the 0 address or missing internal program a failed
allocation leaves behind.
A pool that cannot get a backing BO now marks itself failed and hands out
zeroed host memory with GPU address 0 and no BO (agx_pool_fail), so the
caller can finish what it records without a check at every allocation.
The batch encoder, a VDM alias, the sampler heap, the geometry heap and
the internal-program cache do the same. agx_batch_alloc_failed() then
drops the batch at flush instead of submitting it, and the next
agx_get_batch() starts a fresh one, so the GPU never reads host memory or
address 0. The descriptor builders that aborted on such a value leave the
batch instead (agx_rejected_unless_oom).
A dropped batch loses its draws, like a batch the kernel refuses; the
context stays usable and later batches allocate afresh. It is not reported
as a context reset, because compositors abort on any reset. BO creation
also fails once the kernel has failed our VM; that case still retires the
batch as a failed submission and reports the reset. This replaces the
recycling of pool backings on a failed VM.
The internal-program cache (shared with Borealis) no longer stores a 0
address for a failed upload on the AGX2 path either. A unit test covers
the pool's failure and recovery.
Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Context creation asserted on the timestamp BO, screen creation and query creation dereferenced a NULL BO, and a transfer map that needs a staging resource dereferenced a failed one. Return NULL instead, the Gallium convention that GL reports as GL_OUT_OF_MEMORY or a failed eglCreateContext. The timestamp BO is allocated before the kernel queue, so a failure has nothing else to unwind. A resource that cannot get memory for a new layout (decompression, sharing) keeps its old one, the state agx_decompress() already leaves a shared resource it refuses to reallocate. Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
A variant whose executable BO could not be allocated dereferenced it, and once it is cached as refused a transient shortage would break the program for the life of the shader object. Keep the binary, treat the variant as unusable until its BO exists, and retry the upload when a draw, dispatch or fast link uses it. Variants read from the disk cache take the same path. The variant is shared by the contexts of a share group, so a retry publishes its BO once with a compare-and-swap. Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
agx_scratch_alloc() released the old scratch BO, then dereferenced the new one even when it was not allocated. Allocate first and keep the old buffer and size on failure, and return false. Gallium drops the batch that needed the larger scratch; Borealis fails the command buffer with VK_ERROR_OUT_OF_DEVICE_MEMORY. Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
agx_set_constant_buffer() uploads a user constant buffer at once. When that upload fails the slot has no buffer, and agx_const_buffer_ptr() returned address 0 for the shader to read. Mark the batch failed instead, so it is dropped before submission like any batch whose allocation failed. Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
There was a problem hiding this comment.
Verdict: HOLD at defa1189
Blockers:
- Missing second GPU-generation regression row (
Not yet). This change modifies sharedsrc/asahi/lib/(pool.c,agx_scratch.c,agx_bg_eot.c), Galliumasahi, and Borealis scratch-growth failure paths across all GPU generations, while only G17P (J616s) has been tested on hardware (AGX2 sampler heap and legacy bg/eot upload are compile-tested only). Please post a second-generation hardware row (ideally AGX2 M1/M2 or M5) alongside the J616s results before merge.
Checked:
- Full 15-file diff (
+478/-177),MERGEABLEagainstaurora-wip, focusing onpool.c,agx_scratch.c,agx_batch.c,agx_uniforms.c,agx_bg_eot.c, andhk_cmd_buffer.c. agx_scratch_realloc()allocates the replacement BO before unreferencingscratch->buf, andagx_scratch_alloc()restoresold_dwords/old_subgroupson failure so the previous scratch buffer stays valid.agx_pool_fail()records zeroed host-fallback buffers inpool->host, setspool->failed = true, and frees all host fallbacks inagx_pool_cleanup().agx_batch_drop_alloc_failed()cleanly separates kernel-failed VMs (agx_retire_failed_submitwithEIOso context reset propagates) from transient GPU OOM (clean drop viaagx_batch_mark_submitted+agx_batch_cleanupkeeping the context usable), andagx_const_buffer_ptr()/agx_set_sampler_uniforms()/agx_encoder_bind_vdm_alias()markbatch->pool.failedrather than letting the GPU read address 0.- Thorough J616s (
M4 Pro,G17P) exact-head verification: 518 injected OOM runs with 0 driver crashes, GL A/B (copy_b2i,render.draws,boprobe) within ±1.5% noise,meson364/364, GLES3 392/392 & 1308/1308,dq-fast0 differing cases, 0 GPU recoveries.
Evidence
- Code quality and J616s OOM fault-injection coverage are very strong; strictly awaiting a second GPU-generation hardware row (e.g., AGX2 M1/M2 covering sampler heap & legacy bg/eot paths, or M5).
Install scratch and executable BOs only after mapping and initialization. Keep the original upload failure immutable so shared variants publish only the completed BO on retry. Distinguish BG/EOT allocation failures from unsupported shaders and avoid allocating host sinks for failed pool uploads. Signed-off-by: DjDeveloperr <hey@djdev.me>
There was a problem hiding this comment.
Verdict: HOLD at b9be5460
Blockers:
- Exact-head build, unit tests, and hardware verification at
b9be5460missing: New commitb9be5460(asahi: preserve scratch and shader state on upload failure) modifiesagx_bg_eot.c,agx_scratch.c,pool.c,hk_cmd_draw.c,agx_disk_cache.c,agx_state.c, and addstest_scratch_map_failure()plus map-failure assertions intest-agx-bo-cache.c. Please post fresh build + unit test (test-agx-bo-cache,--suite asahi --suite poly) and hardware results atb9be5460(current evidence is fromdefa1189). - Second GPU-generation regression row missing: Current hardware evidence covers
J616s(M4 Pro, G16S) only. Because this PR modifies sharedsrc/asahi/lib/, Galliumasahi, and Borealis AGX3/G17P/M5 background/EOT error paths (hk_cmd_draw.c,agx_bg_eot.c), please add a second GPU-generation row (ideally including an M5/G17G or G17P machine) atb9be5460.
Checked:
- Audited commit
b9be5460(+175/-38across 9 files):agx_scratch_realloc()(src/asahi/lib/agx_scratch.c): maps and initializes the replacement BO before unreferencing and swappingscratch->buf/scratch->header, preserving the prior scratch buffer intact onagx_bo_map()failure.agx_pool_alloc_backing()&agx_pool_try_alloc_aligned_with_bo()(src/asahi/lib/pool.c): checksagx_bo_map(bo)before appending topool->bosor updatingpool->transient_bo, and avoids allocating a throwaway host buffer inagx_pool_upload_aligned_with_bo().agx_upload_compiled_shader(),read_shader(),agx_compile_nir_tile(), andagx_drop_for_unusable_program()(agx_state.c,agx_disk_cache.c): checksagx_bo_map(bo)beforememcpy(), setsupload_failedstrictly before publishing, and usesp_atomic_read(&cs->bo)on the drop diagnostic path.agx_get_bg_eot_shader_checked()&hk_get_bg_eot_shader()(agx_bg_eot.c,hk_cmd_draw.c): cleanly separatesAGX_BG_EOT_HOST_OOM/AGX_BG_EOT_DEVICE_OOMfrom unsupported shader shapes (!built), mapping OOM toVK_ERROR_OUT_OF_HOST_MEMORY/VK_ERROR_OUT_OF_DEVICE_MEMORYwithout breaking unsupported-path fallbacks such asstore_and_resolve.- Unit tests (
src/asahi/lib/tests/test-agx-bo-cache.c): addsmock.map_fail/mock.real_mapscoverage for bothagx_pool_upload_aligned()andtest_scratch_map_failure().
Evidence
- Head commit:
b9be5460(asahi: preserve scratch and shader state on upload failure) on top ofdefa1189; branch isMERGEABLEintoaurora-wip. - No automated CI checks attached to this branch; PR body evidence is still tagged at
defa1189onJ616s(M4 Pro) only.
|
PASS on M4 mini J773g (G16G) at Kernel: saved stock Driver unit tests (CPU only)
GL on the GPU (Gallium asahi, surfaceless EGL)
Vulkan CTS (Borealis):
Not covered on J773g
CTS: |
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Code approved at b9be5460a914. The scratch allocator maps and initializes the replacement before releasing the old buffer. Gallium keeps the shader binary for retry and publishes the executable buffer atomically. The BG/EOT cache skips failed uploads without retaining host storage, and Borealis preserves the host or device OOM through command-buffer end.
The BO-cache regression passed. The remaining Apple GPU row covers failed mappings, shared-context retry and a successful draw on the stock kernel.
|
Fixed scratch and shader upload failure handling in b9be546. |
There was a problem hiding this comment.
Verdict: MERGE at b9be5460a914
Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.
Checked: - Audited commit b9be5460 (+175/-38 across 9 files):
agx_scratch_realloc()(src/asahi/lib/agx_scratch.c): maps and initializes the replacement BO before unreferencing and swappingscratch->buf/scratch->header, preserving the prior scratch buffer intact onagx_bo_map()failure.agx_pool_alloc_backing()&agx_pool_try_alloc_aligned_with_bo()(src/asahi/lib/pool.c): checksagx_bo_map(bo)before appending topool->bosor updatingpool->transient_bo, and avoids allocating a throwaway host buffer inagx_pool_upload_aligned_with_bo().agx_upload_compiled_shader(),read_shader(),agx_compile_nir_tile(), andagx_drop_for_unusable_program()(agx_state.c,agx_disk_cache.c): checksagx_bo_map(bo)beforememcpy(), setsupload_failedstrictly before publishing, and usesp_atomic_read(&cs->bo)on the drop diagnostic path.agx_get_bg_eot_shader_checked()&hk_get_bg_eot_shader()(agx_bg_eot.c,hk_cmd_draw.c): cleanly separatesAGX_BG_EOT_HOST_OOM/AGX_BG_EOT_DEVICE_OOMfrom unsupported shader shapes (!built), mapping OOM toVK_ERROR_OUT_OF_HOST_MEMORY/VK_ERROR_OUT_OF_DEVICE_MEMORYwithout breaking unsupported-path fallbacks such asstore_and_resolve.- Unit tests (
src/asahi/lib/tests/test-agx-bo-cache.c): addsmock.map_fail/mock.real_mapscoverage for bothagx_pool_upload_aligned()andtest_scratch_map_failure().
Follow-ups (not blocking): exact-head build/unit/hardware results (2nd GPU gen not required)
|
Merging at |
What: Gallium asahi crashed when a BO allocation failed: the batch encoder dereferenced a NULL BO,
agx_poolaborted, and a dozen G17P descriptor builders aborted on the 0 address or missing internal program a failed allocation leaves behind. With the kernel's GPU memory policy moving to the standard model (allocations fail with ENOMEM after the driver's own reclaim, below a device-wide ceiling on held pages), that takes down every GL client, compositors included. Now a failed batch is dropped before submission and the context stays usable; creation paths return NULL (GL_OUT_OF_MEMORY / failed eglCreateContext); a shader upload that ran out of memory is retried; a batch whose user constant buffer could not be uploaded is dropped instead of letting the shader read address 0; Borealis gets VK_ERROR_OUT_OF_DEVICE_MEMORY when growing scratch fails.Tested: J616s M4 Pro (G17P), kernel 7.3.0-m4.212. Head
defa1189c: GL A/B against base152e449f7(render.draws, copy_b2i, upload probe; 6 interleaved rounds) within ±1.5%, no regression; its new commit (agx_uniforms.conly) compile-checked at this head and run with the same commit on m4/nextb54448cf7(+ a separate st/mesa fix, + the test hook): 518 injected runs, 0 crashes; meson 364/364; GLES3 392/392 and 1308/1308; dq-fast focus-cases and robustness2 0 cases differ, descriptor_buffer flaky on this driver and on the installed one alike (no Vulkan code changed). The first four commits atfe87e210e(plus a test-only fault-injection commit, off by default, not in this PR) against aurora-wip152e449f7: meson asahi+vulkan 360/360 on both; surfaceless GL render probe 300/300 frames right on both; under injected BO failures 518 runs with no crash in the driver (the one remaining crash is in st/mesa, see Not yet); dEQP-GLES3 392-case slice 392/392 and pbo/texture.specification 1308/1308 on both; dq-fast focus-cases 1328, robustness2 1364, descriptor_buffer 552: 0 cases differ from the base (3 robustness2 cases flaked once, then passed on both builds in reruns); 0 GPU recoveries, 0 failed units.Risk: two-way door · every Mac on Gallium asahi (all generations; only exercised on G17P) and Borealis's scratch-growth failure path. No behaviour change while allocations succeed.
Not yet: the st/mesa crash (
st_upload_constants()writing into a NULLu_upload_alloc()result) is frontend code and goes to freedesktop Mesa separately; it is not in this PR. M1/M2 paths (AGX2 sampler heap, legacy bg/eot upload) are compile-tested only. A geometry shader whose sub-program upload fails stays refused (as for a compile refusal).Details
src/asahi/lib/pool.c): a pool that cannot get a backing BO marks itselffailedand returns zeroed host memory with GPU address 0 and no BO (agx_pool_fail()), so callers finish what they record without a check at every allocation. Replaces the abort and the recycling of a backing on a failed VM. The internal-program cache (shared with Borealis) treats the 0 address as a failed lookup and caches nothing.agx_flush_batch()/agx_batch_submit()drop a marked batch instead of submitting it (agx_batch_drop_alloc_failed()), and the nextagx_get_batch()starts a fresh one, so the GPU never reads host memory or address 0. The 49agx_native_descriptor_rejected()aborts in the functions that build from such allocations (bg/eot, mixed MRT, precompiled and API compute launches, texture/sampler upload) leave the batch instead when its allocation failed (agx_rejected_unless_oom); otherwise they abort as before.agx_decompress()already leaves a shared resource in.agx_set_constant_buffer()uploads a user constant buffer at once; if that upload fails,agx_const_buffer_ptr()marks the batch failed instead of returning address 0.agx_scratch.c, shared): allocate the larger buffer before releasing the old one; on failure keep the old buffer and size and return false. Gallium drops the batch, Borealis setsVK_ERROR_OUT_OF_DEVICE_MEMORYon the command buffer.Evidence
Fault injection: a test-only commit (not in this PR) adds
AGX_BO_FAIL_EVERY=n, failing every n-th BO creation. Probe: surfaceless EGL + GLES3, 120-300 frames, each clears an FBO, uploads a fresh vertex buffer and texture, draws and reads the centre pixel back.58e5c13ac)Frames recover after failures: e.g. n=53: 300/300 right; n=211: 299/300; n=29: 299/300 with 147 dropped batches. Without injection the probe reads back 300/300 on base and PR.
Checks (all under the same boot, 0 recoveries): meson
--suite asahi --suite vulkan360/360 base152e449f7and PR; per-commit builds of the four commits compile cleanly, and the pool unit test (asahi-agx-bo-cache, new case) passes at each. dq-fast against baselines saved from the base build (aurorawip-152e449f7): focus-cases 1069 Pass / 259 NotSupported, robustness2 1101/263, descriptor_buffer 545/7, 0 cases differ; three robustness2 cases (...storage_buffer.readwrite.no_fmt_qual.len_252...vert,...storage_buffer_dynamic.readonly...len_16...comp_compute,...volatile.storage_buffer.readwrite...len_31...rgen) went Pass -> Flake once and passed on both builds in the reruns, and the whole list then showed 0 cases differ on both.Performance, base vs head (J616s M4 Pro, kernel 7.3.0-m4.212, on AC power:
macsmc-ac/online= 1 at start and end; 2026-11-30 21:05-21:10 UTC). Base = aurora-wip152e449f7, head =defa1189c, both release builds loaded per process with__EGL_VENDOR_LIBRARY_FILENAMES+LD_LIBRARY_PATH(surfaceless EGL, shader cache off). 6 rounds, interleaved ABBA; each cell is the median over the 6 rounds of each round's median. gpubench1539f84; probe = a GLES3 loop that per frame creates a VBO and a texture, sets a uniform and a current attribute, draws and reads one pixel back (3000/3000 frames right on both).All differences are below 1.5% and inside the round-to-round spread of either build (render.draws d1: base 4.83-5.28, head 4.65-5.28 Mdraws/s; copy_b2i d4: base 34.2-36.1, head 34.7-36.0 GB/s; probe: base 5256-5859, head 5337-5836 frames/s): no regression.