From 6f211eb63519d4004dc30dc2df8b0b4350483902 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Mon, 30 Nov 2026 01:38:20 -0300 Subject: [PATCH 1/6] asahi: drop a batch whose GPU memory allocation fails 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 --- src/asahi/lib/agx_bg_eot.c | 7 + src/asahi/lib/pool.c | 61 ++++---- src/asahi/lib/pool.h | 10 ++ src/asahi/lib/tests/test-agx-bo-cache.c | 45 +++++- src/gallium/drivers/asahi/agx_batch.c | 67 ++++++++- src/gallium/drivers/asahi/agx_pipe.c | 5 + src/gallium/drivers/asahi/agx_state.c | 183 +++++++++++++++-------- src/gallium/drivers/asahi/agx_state.h | 18 +++ src/gallium/drivers/asahi/agx_uniforms.c | 2 + 9 files changed, 303 insertions(+), 95 deletions(-) diff --git a/src/asahi/lib/agx_bg_eot.c b/src/asahi/lib/agx_bg_eot.c index 3e0d08b4890b..27b326642bd7 100644 --- a/src/asahi/lib/agx_bg_eot.c +++ b/src/asahi/lib/agx_bg_eot.c @@ -311,6 +311,13 @@ agx_upload_bg_eot_part(struct agx_bg_eot_cache *cache, &cache->pool, bin->binary, bin->info.binary_size, 128, &res->bo); } free(bin->binary); + + /* Out of memory (agx_pool_fail): do not cache a 0 address */ + if (!res->ptr) { + ralloc_free(res); + return NULL; + } + return res; } diff --git a/src/asahi/lib/pool.c b/src/asahi/lib/pool.c index 6506df2ccdc2..3c63a28d036e 100644 --- a/src/asahi/lib/pool.c +++ b/src/asahi/lib/pool.c @@ -42,6 +42,7 @@ agx_pool_init(struct agx_pool *pool, struct agx_device *dev, const char *label, pool->create_flags = create_flags; pool->label = label; pool->bos = UTIL_DYNARRAY_INIT; + pool->host = UTIL_DYNARRAY_INIT; if (prealloc) agx_pool_alloc_backing(pool, POOL_SLAB_SIZE); @@ -55,6 +56,31 @@ agx_pool_cleanup(struct agx_pool *pool) } util_dynarray_fini(&pool->bos); + + util_dynarray_foreach(&pool->host, void *, mem) { + free(*mem); + } + + util_dynarray_fini(&pool->host); +} + +/* Out of GPU memory: mark the pool failed and return zeroed host memory of sz + * bytes, so the caller can finish what it is recording without a check at + * every allocation. Its GPU address is 0 and it has no BO. The owner drops + * whatever it recorded since the failure (an OpenGL batch) or treats a 0 + * address as the error (a long-lived cache), so the GPU never reads it. + * Host allocation failure is fatal, as everywhere else in Mesa. + */ +void * +agx_pool_fail(struct agx_pool *pool, size_t sz) +{ + void *mem = calloc(1, MAX2(sz, 1)); + if (!mem) + abort(); + + util_dynarray_append(&pool->host, mem); + pool->failed = true; + return mem; } struct agx_ptr @@ -73,33 +99,10 @@ agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz, pool, ALIGN_POT(MAX2(POOL_SLAB_SIZE, sz), 16384)); if (unlikely(fresh == NULL)) { - /* BO creation fails once the kernel has failed this VM (a GPU - * recovery blamed our work: the context already reports a reset). - * Recycle a backing that fits rather than dereferencing NULL: what - * is written here is never executed on a failed VM, and the - * application sees the reset through GL_ARB_robustness and rebuilds - * its context. - * - * On a healthy VM the failure is real memory exhaustion, and a - * submitted batch that has not completed may still read any of our - * backings. Recycling one would corrupt it, so abort instead. - */ - if (agx_device_vm_failed(pool->dev)) { - util_dynarray_foreach(&pool->bos, struct agx_bo *, it) { - if ((*it)->size >= sz) { - fresh = *it; - break; - } - } - } - - if (fresh == NULL) { - fprintf(stderr, "agx: pool %s cannot allocate %zu bytes\n", - pool->label ? pool->label : "(unnamed)", sz); - abort(); - } - - pool->transient_bo = fresh; + if (out_bo) + *out_bo = NULL; + + return (struct agx_ptr){.cpu = agx_pool_fail(pool, sz), .gpu = 0}; } bo = fresh; @@ -135,7 +138,7 @@ agx_pool_upload_aligned_with_bo(struct agx_pool *pool, const void *data, struct agx_ptr transfer = agx_pool_alloc_aligned_with_bo(pool, sz, alignment, out_bo); memcpy(transfer.cpu, data, sz); - agx_bo_assert_range(*out_bo, - transfer.gpu - (*out_bo)->va->addr, sz); + if (*out_bo) + agx_bo_assert_range(*out_bo, transfer.gpu - (*out_bo)->va->addr, sz); return transfer.gpu; } diff --git a/src/asahi/lib/pool.h b/src/asahi/lib/pool.h index eab4c0ddd419..7941c052abe8 100644 --- a/src/asahi/lib/pool.h +++ b/src/asahi/lib/pool.h @@ -34,6 +34,14 @@ struct agx_pool { /* Label for pool allocations */ const char *label; + + /* Set when an allocation could not get GPU memory (agx_pool_fail), or by + * the owner when its own GPU allocation failed. The owner must not let the + * GPU see anything recorded since. Cleared only by the owner. */ + bool failed; + + /* Host memory handed out after a failure, freed by agx_pool_cleanup */ + struct util_dynarray host; }; void agx_pool_init(struct agx_pool *pool, struct agx_device *dev, @@ -41,6 +49,8 @@ void agx_pool_init(struct agx_pool *pool, struct agx_device *dev, void agx_pool_cleanup(struct agx_pool *pool); +void *agx_pool_fail(struct agx_pool *pool, size_t sz); + struct agx_ptr agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz, unsigned alignment, struct agx_bo **bo); diff --git a/src/asahi/lib/tests/test-agx-bo-cache.c b/src/asahi/lib/tests/test-agx-bo-cache.c index 568253e60398..d4b8b9c5140d 100644 --- a/src/asahi/lib/tests/test-agx-bo-cache.c +++ b/src/asahi/lib/tests/test-agx-bo-cache.c @@ -13,9 +13,10 @@ #include #include +#include "util/os_time.h" #include "agx_bo.h" #include "agx_device.h" -#include "util/os_time.h" +#include "pool.h" #define MIB (1024ull * 1024ull) @@ -37,6 +38,7 @@ struct mock_state { unsigned mmap_calls; unsigned munmap_calls; bool alloc_va; + bool alloc_fail; }; static struct mock_state mock; @@ -46,6 +48,9 @@ mock_bo_alloc(struct agx_device *dev, size_t size, size_t align, enum agx_bo_flags flags) { mock.alloc_calls++; + if (mock.alloc_fail) + return NULL; + const uint32_t handle = ++mock.next_handle; simple_mtx_lock(&dev->bo_map_lock); @@ -734,6 +739,43 @@ test_mmap_offset_cache_propagation(void) finish_mock_device(&dev); } +/* Out of GPU memory, a pool hands out zeroed host memory at GPU address 0 + * with no BO and marks itself failed, instead of aborting; it allocates BOs + * again once memory is back. */ +static void +test_pool_alloc_failure(void) +{ + struct agx_device dev; + init_mock_device(&dev); + mock.alloc_va = true; + + struct agx_pool pool; + agx_pool_init(&pool, &dev, "test", 0, false); + + struct agx_bo *bo = NULL; + struct agx_ptr ok = agx_pool_alloc_aligned_with_bo(&pool, 64, 64, &bo); + assert(bo && ok.gpu && !pool.failed); + + const size_t big = 1024 * 1024; + mock.alloc_fail = true; + struct agx_ptr sink = agx_pool_alloc_aligned_with_bo(&pool, big, 64, &bo); + assert(pool.failed && !bo && sink.cpu && sink.gpu == 0); + for (size_t i = 0; i < big; ++i) + assert(((const uint8_t *)sink.cpu)[i] == 0); + memset(sink.cpu, 0xff, big); + + static uint8_t data[1024 * 1024]; + assert(agx_pool_upload_aligned(&pool, data, sizeof(data), 64) == 0); + + mock.alloc_fail = false; + pool.failed = false; + struct agx_ptr again = agx_pool_alloc_aligned_with_bo(&pool, big, 64, &bo); + assert(bo && again.gpu && !pool.failed); + + agx_pool_cleanup(&pool); + finish_mock_device(&dev); +} + int main(void) { @@ -747,6 +789,7 @@ main(void) test_create_checked_evicts_cache_before_refusing(); test_large_buckets_flag_isolation_and_cross_pot_reuse(); test_mmap_offset_cache_propagation(); + test_pool_alloc_failure(); printf("agx bo cache: ok\n"); return 0; diff --git a/src/gallium/drivers/asahi/agx_batch.c b/src/gallium/drivers/asahi/agx_batch.c index 7c560df6cb66..4b77702b5d8a 100644 --- a/src/gallium/drivers/asahi/agx_batch.c +++ b/src/gallium/drivers/asahi/agx_batch.c @@ -121,8 +121,17 @@ agx_batch_mark_complete(struct agx_batch *batch) struct agx_encoder agx_encoder_allocate(struct agx_batch *batch, struct agx_device *dev) { - struct agx_bo *bo = agx_bo_create(dev, 0x80000, 0, 0, "Encoder"); - uint8_t *map = agx_bo_map(bo); + const size_t size = 0x80000; + struct agx_bo *bo = agx_bo_create(dev, size, 0, 0, "Encoder"); + uint8_t *map = bo ? agx_bo_map(bo) : NULL; + + /* Without a BO, record into host memory and drop the batch at flush. */ + if (unlikely(!map)) { + agx_bo_unreference(dev, bo); + map = agx_pool_fail(&batch->pool, size); + return (struct agx_encoder){.current = map, .end = map + size}; + } + return (struct agx_encoder){.bo = bo, .current = map, .end = map + bo->size}; } @@ -164,13 +173,16 @@ agx_batch_get_g17p_vdm_alias(struct agx_batch *batch, struct agx_bo *bo, } static void -agx_encoder_bind_vdm_alias(struct agx_device *dev, struct agx_encoder *encoder) +agx_encoder_bind_vdm_alias(struct agx_batch *batch, struct agx_device *dev, + struct agx_encoder *encoder) { - if (!agx_chip_native_agx3(dev->chip)) + if (!agx_chip_native_agx3(dev->chip) || !encoder->bo) return; + /* Mapping the alias takes VA and a bind, which can fail like a BO. */ encoder->g17p_vdm_alias = agx_bo_get_g17p_vdm_alias(dev, encoder->bo); - assert(encoder->g17p_vdm_alias); + if (!encoder->g17p_vdm_alias) + batch->pool.failed = true; } void @@ -406,7 +418,7 @@ agx_batch_init(struct agx_context *ctx, memset(&batch->vdm, 0, sizeof(batch->vdm)); } else { batch->vdm = agx_encoder_allocate(batch, dev); - agx_encoder_bind_vdm_alias(dev, &batch->vdm); + agx_encoder_bind_vdm_alias(batch, dev, &batch->vdm); memset(&batch->cdm, 0, sizeof(batch->cdm)); } @@ -715,6 +727,12 @@ agx_get_batch_for_framebuffer(struct agx_context *ctx, struct agx_batch *candidate = &ctx->batches.slots[i]; if (util_framebuffer_state_equal(&candidate->key, state)) { + /* Its allocation failed, so it is lost: start a fresh one. */ + if (unlikely(agx_batch_alloc_failed(candidate))) { + agx_flush_batch(ctx, candidate); + break; + } + /* We found a match, increase the seqnum for the LRU * eviction logic. */ @@ -771,6 +789,9 @@ agx_get_batch_for_framebuffer(struct agx_context *ctx, struct agx_batch * agx_get_batch(struct agx_context *ctx) { + if (ctx->batch && unlikely(agx_batch_alloc_failed(ctx->batch))) + agx_flush_batch(ctx, ctx->batch); + if (!ctx->batch || agx_batch_is_compute(ctx->batch)) { ctx->batch = agx_get_batch_for_framebuffer(ctx, &ctx->framebuffer); agx_dirty_all(ctx); @@ -1406,6 +1427,34 @@ agx_retire_failed_submit(struct agx_context *ctx, struct agx_batch *batch, errno = error; } +/* Drop a batch whose allocation failed (agx_batch_alloc_failed) instead of + * submitting it. Its draws are lost, like a batch the kernel refuses, but the + * context stays usable: the next batch allocates afresh. A context reset would + * be the louder signal, yet a compositor aborts on any reset, so it is kept + * for the case where BO creation failed because the kernel failed our VM. + */ +void +agx_batch_drop_alloc_failed(struct agx_context *ctx, struct agx_batch *batch) +{ + struct agx_device *dev = agx_device(ctx->base.screen); + + if (agx_device_vm_failed(dev)) { + struct util_dynarray cmdbuf = UTIL_DYNARRAY_INIT; + agx_retire_failed_submit(ctx, batch, NULL, NULL, 0, NULL, &cmdbuf, EIO, 0, + 0, 0); + return; + } + + batch_debug(batch, "DROP"); + mesa_loge("asahi: out of GPU memory: dropping batch %u, nothing was " + "submitted", + agx_batch_idx(batch)); + if (ctx->batch == batch) + ctx->batch = NULL; + agx_batch_mark_submitted(batch); + agx_batch_cleanup(ctx, batch, false); +} + /* Drop a batch on a context that has faulted, without encoding it. This is * the retirement agx_batch_submit uses for the same case: the batch may write * BOs, so it needs the full cleanup that releases its writer entries, which @@ -1667,6 +1716,12 @@ agx_batch_submit(struct agx_context *ctx, struct agx_batch *batch, const unsigned cdm_dispatch_count = util_dynarray_num_elements(&batch->cdm_dispatch, struct agx_cdm_dispatch); + /* Encoding the batch for submission can run out of memory too. */ + if (unlikely(agx_batch_alloc_failed(batch))) { + agx_batch_drop_alloc_failed(ctx, batch); + return; + } + /* Nothing from a batch that requested state without a proven native * encoding may reach the hardware. Retire it here without a context * reset: the draws simply never executed, which the application (and a diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index bcb0cd203280..1758ea18731b 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -2703,6 +2703,11 @@ agx_flush_batch(struct agx_context *ctx, struct agx_batch *batch) agx_try_fast_clear_batch(ctx, batch); + if (unlikely(agx_batch_alloc_failed(batch))) { + agx_batch_drop_alloc_failed(ctx, batch); + return; + } + if (batch->cdm.bo) { agx_flush_compute(ctx, batch, &compute); has_cdm = true; diff --git a/src/gallium/drivers/asahi/agx_state.c b/src/gallium/drivers/asahi/agx_state.c index 7fc8707d4097..0e985dd8d71d 100644 --- a/src/gallium/drivers/asahi/agx_state.c +++ b/src/gallium/drivers/asahi/agx_state.c @@ -840,6 +840,20 @@ agx_native_descriptor_rejected(struct agx_device *dev, const char *operation) abort(); } +/* agx_native_descriptor_rejected() for a check that a failed allocation can + * also fail, with a 0 address, a missing BO or a missing internal program. A + * batch whose allocation failed is dropped at flush and never submitted + * (agx_batch_alloc_failed), so stop recording it instead of aborting. The + * optional argument is the return value. + */ +#define agx_rejected_unless_oom(batch, operation, ...) \ + do { \ + if (agx_batch_alloc_failed(batch)) \ + return __VA_ARGS__; \ + agx_native_descriptor_rejected(agx_device((batch)->ctx->base.screen), \ + operation); \ + } while (0) + /* A chip-level geometry that the driver does not know at all. This cannot * happen for a recognised AGX3 identity and is not recoverable per batch. */ static void @@ -7390,7 +7404,7 @@ agx_upload_textures(struct agx_batch *batch, struct agx_compiled_shader *cs, batch->g17p_emrt_heap = agx_upload_g17p_spilled_rt_table( batch, out, agx_nr_tex_descriptors_without_spilled_rts(cs)); if (!batch->g17p_emrt_heap) - agx_native_descriptor_rejected(dev, "native spilled-RT table"); + agx_rejected_unless_oom(batch, "native spilled-RT table"); } } @@ -7413,6 +7427,10 @@ agx_sampler_heap_add(struct agx_device *dev, struct agx_sampler_heap *heap, 0, AGX_BO_WRITEBACK, "Sampler heap"); assert(heap->count == 0); + + /* The caller drops the batch (agx_batch_alloc_failed). */ + if (!heap->bo) + return 0; } /* TODO search */ @@ -7703,7 +7721,7 @@ agx_upload_samplers(struct agx_batch *batch, struct agx_compiled_shader *cs, }; if (!agx_gallium_pack_native_sampler(dev->chip, (void *)out, &null_state)) - agx_native_descriptor_rejected(dev, "null sampler"); + agx_rejected_unless_oom(batch, "null sampler"); } } @@ -10776,6 +10794,10 @@ agx_launch_internal(struct agx_batch *batch, struct agx_grid grid, struct agx_context *ctx = batch->ctx; struct agx_device *dev = agx_device(ctx->base.screen); + /* The encoder had no memory: the batch is dropped (agx_batch_alloc_failed) */ + if (unlikely(!batch->cdm.bo)) + return; + /* TODO: Ensure space if we allow multiple kernels in a batch */ uint32_t *out = (uint32_t *)batch->cdm.current; const uint8_t *base = agx_bo_map(batch->cdm.bo); @@ -11322,7 +11344,7 @@ agx_launch_precomp(struct agx_batch *batch, struct agx_grid grid, cs ? (unsigned)cs->rodata_offset : 0, cs ? cs->info.printf_uniform : 0, cs ? (unsigned)cs->info.agx3_staging_known : 0, arg_size); - agx_native_descriptor_rejected(dev, "precompiled compute program"); + agx_rejected_unless_oom(batch, "precompiled compute program"); } /* Mode 2 reads the whole grid -- three global thread counts, then @@ -11343,7 +11365,7 @@ agx_launch_precomp(struct agx_batch *batch, struct agx_grid grid, cs->info.printf_uniform ? MAX2(arg_size, printf_offset + 8) : arg_size, 4); struct agx_ptr uploaded = agx_pool_alloc_aligned(&batch->pool, upload_size, 4); if (!uploaded.cpu || upload_size / 2 > UINT16_MAX) - agx_native_descriptor_rejected(dev, "precompiled argument upload"); + agx_rejected_unless_oom(batch, "precompiled argument upload"); memcpy(uploaded.cpu, args, arg_size); memset((uint8_t *)uploaded.cpu + arg_size, 0, upload_size - arg_size); if (cs->info.printf_uniform) @@ -11378,7 +11400,7 @@ agx_launch_precomp(struct agx_batch *batch, struct agx_grid grid, if (!uploaded_data || !agx3_cdm_direct_template_sources_compute(false, false, false, &launch_sources)) - agx_native_descriptor_rejected(dev, "precompiled compute resources"); + agx_rejected_unless_oom(batch, "precompiled compute resources"); const struct agx3_g17p_compute_program_desc desc = { .shader_va = cs->ptr + cs->entry_offset, @@ -11415,7 +11437,7 @@ agx_launch_precomp(struct agx_batch *batch, struct agx_grid grid, if (!agx_g17p_compute_program_emit_cached( batch, &batch->ctx->g17p_precomp_templates[program], &batch->ctx->g17p_precomp_templates_generated, &desc, &launch)) - agx_native_descriptor_rejected(dev, "precompiled compute Ioto"); + agx_rejected_unless_oom(batch, "precompiled compute Ioto"); const struct agx3_compute_direct_grid direct = { .source = AGX3_CDM_DIRECT_GRID_GLOBAL_THREADS, @@ -11441,7 +11463,7 @@ agx_launch_precomp(struct agx_batch *batch, struct agx_grid grid, &launch, grid.ptr, barrier_flags, &sink) : !agx3_g17p_native_compute_dispatch_append_with_barrier( &launch, &direct, barrier_flags, &sink)) - agx_native_descriptor_rejected(dev, "precompiled compute launch"); + agx_rejected_unless_oom(batch, "precompiled compute launch"); batch->cdm.current += sink.length; agx_batch_add_bo(batch, cs->bo); return; @@ -11858,11 +11880,13 @@ agx_g17p_build_mixed_bg_eot(struct agx_batch *batch, /* Multisample targets are stored unresolved; a resolve or a * single-sample WSI reload needs the single-target programs. */ if (samples > 1 && (batch->key.resolve || framebuffer_key->single_sample_reload)) - agx_native_descriptor_rejected(dev, "multisample mixed MRT resolve"); + agx_rejected_unless_oom(batch, "multisample mixed MRT resolve", + (struct asahi_bg_eot){0}); if (descriptor_size != AGX_IOTO_G17P_RT0_PBE_ARGUMENT_SIZE || agx_gallium_pbe_descriptor_size(descriptor_generation) != AGX_IOTO_G17P_RT0_PBE_ARGUMENT_SIZE) - agx_native_descriptor_rejected(dev, "mixed MRT descriptor geometry"); + agx_rejected_unless_oom(batch, "mixed MRT descriptor geometry", + (struct asahi_bg_eot){0}); struct agx_bg_eot_key key = *framebuffer_key; memset(key.tib.logical_format, 0, sizeof(key.tib.logical_format)); @@ -11887,7 +11911,8 @@ agx_g17p_build_mixed_bg_eot(struct agx_batch *batch, const struct pipe_surface *surf = &batch->key.cbufs[rt]; if (!surf->texture || util_res_sample_count(surf->texture) != samples) - agx_native_descriptor_rejected(dev, "mixed MRT attachment"); + agx_rejected_unless_oom(batch, "mixed MRT attachment", + (struct asahi_bg_eot){0}); key.tib.logical_format[count] = framebuffer_key->tib.logical_format[rt]; @@ -11919,7 +11944,8 @@ agx_g17p_build_mixed_bg_eot(struct agx_batch *batch, store ? "end-of-tile" : "background", shader ? shader->info.push_count : 0, key.reserved_preamble, shader ? shader->info.uses_txf : 0, count, reloads, samples); - agx_native_descriptor_rejected(dev, "mixed MRT shader"); + agx_rejected_unless_oom(batch, "mixed MRT shader", + (struct asahi_bg_eot){0}); } agx_batch_add_bo(batch, shader->bo); @@ -11940,7 +11966,8 @@ agx_g17p_build_mixed_bg_eot(struct agx_batch *batch, if (!program_ptr.cpu || !program_bo || !agx_va_in_usc_window(dev, program_ptr.gpu) || !agx_va_in_usc_window(dev, shader->ptr + shader->info.main_offset)) - agx_native_descriptor_rejected(dev, "mixed MRT program allocation"); + agx_rejected_unless_oom(batch, "mixed MRT program allocation", + (struct asahi_bg_eot){0}); const struct agx_ioto_g17p_imageblock imageblock = { .sample_size_B = key.tib.sample_size_B, @@ -11988,7 +12015,8 @@ agx_g17p_build_mixed_bg_eot(struct agx_batch *batch, struct agx3_g17p_rt0_eot_program native; if (!agx3_g17p_rt0_eot_program_generate(dev->chip, &desc, &native) || native.length > capacity) - agx_native_descriptor_rejected(dev, "mixed MRT end-of-tile program"); + agx_rejected_unless_oom(batch, "mixed MRT end-of-tile program", + (struct asahi_bg_eot){0}); memcpy(program_ptr.cpu, native.bytes, native.length); program_length = native.length; resource_spec = AGX_IOTO_G17P_EOT_RESOURCE_SPEC; @@ -12011,14 +12039,16 @@ agx_g17p_build_mixed_bg_eot(struct agx_batch *batch, union pipe_color_union clear_values[PIPE_MAX_COLOR_BUFS]; for (unsigned i = reloads; i < count; ++i) { if (!batch->uploaded_clear_color[rts[i]]) - agx_native_descriptor_rejected(dev, "mixed MRT clear value"); + agx_rejected_unless_oom(batch, "mixed MRT clear value", + (struct asahi_bg_eot){0}); clear_values[i - reloads] = batch->clear_color[rts[i]]; } desc.clear_value_va = agx_pool_upload_aligned( &batch->pool, clear_values, sizeof(clear_values[0]) * (count - reloads), 16); if (!desc.clear_value_va) - agx_native_descriptor_rejected(dev, "mixed MRT clear upload"); + agx_rejected_unless_oom(batch, "mixed MRT clear upload", + (struct asahi_bg_eot){0}); } if (reloads) { @@ -12043,11 +12073,13 @@ agx_g17p_build_mixed_bg_eot(struct agx_batch *batch, const unsigned sampler_size = agx_chip_sampler_heap_element_size(dev->chip); if (sampler_size != AGX_GALLIUM_SAMPLER_AGX3_SIZE) - agx_native_descriptor_rejected(dev, "mixed MRT sampler geometry"); + agx_rejected_unless_oom(batch, "mixed MRT sampler geometry", + (struct asahi_bg_eot){0}); struct agx_ptr sampler = agx_pool_alloc_aligned(&batch->pool, sampler_size, 64); if (!agx_gallium_pack_g17p_txf_sampler(dev->chip, sampler.cpu)) - agx_native_descriptor_rejected(dev, "mixed MRT sampler"); + agx_rejected_unless_oom(batch, "mixed MRT sampler", + (struct asahi_bg_eot){0}); const uint64_t pointers[2] = {textures.gpu, sampler.gpu}; const uint64_t table = agx_pool_upload_aligned( @@ -12060,7 +12092,8 @@ agx_g17p_build_mixed_bg_eot(struct agx_batch *batch, if (!agx3_g17p_rt0_bg_program_generate(dev->chip, &desc, &native) || native.length > capacity || !agx3_g17p_rt0_bg_resource_spec(dev->chip, bg_op, &resource_spec)) - agx_native_descriptor_rejected(dev, "mixed MRT background program"); + agx_rejected_unless_oom(batch, "mixed MRT background program", + (struct asahi_bg_eot){0}); memcpy(program_ptr.cpu, native.bytes, native.length); program_length = native.length; } @@ -12644,7 +12677,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) tib->_offset_B[rt], tib->spilled[rt] ? " (spilled)" : ""); } - agx_native_descriptor_rejected(dev, "background tile layout"); + agx_rejected_unless_oom(batch, "background tile layout", + (struct asahi_bg_eot){0}); } const enum agx_g17p_mrt_layout mrt = @@ -12693,7 +12727,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) if (store ? (op != AGX_EOT_STORE && op != AGX_BG_EOT_NONE) : (op != AGX_BG_CLEAR && op != AGX_BG_LOAD && op != AGX_BG_EOT_NONE)) - agx_native_descriptor_rejected(dev, "background operation"); + agx_rejected_unless_oom(batch, "background operation", + (struct asahi_bg_eot){0}); if (!store && op == AGX_BG_CLEAR) key.reserved_preamble = @@ -12746,7 +12781,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) 1u), shader ? shader->info.main_size : 0, shader ? shader->info.main_offset : 0); - agx_native_descriptor_rejected(dev, "background shader"); + agx_rejected_unless_oom(batch, "background shader", + (struct asahi_bg_eot){0}); } agx_batch_add_bo(batch, shader->bo); @@ -12778,7 +12814,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) !agx_va_in_usc_window(dev, program_ptr.gpu) || !agx_va_in_usc_window(dev, shader->ptr + shader->info.main_offset)) - agx_native_descriptor_rejected(dev, "background program allocation"); + agx_rejected_unless_oom(batch, "background program allocation", + (struct asahi_bg_eot){0}); size_t program_length = 0; uint64_t resource_spec = 0; @@ -12787,18 +12824,21 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) struct pipe_surface resolve_surf; if (key.tib.nr_samples > 1 && !key.multisample_store) { if (!agx_g17p_direct_resolve_surface(batch, &resolve_surf)) - agx_native_descriptor_rejected(dev, "direct colour resolve"); + agx_rejected_unless_oom(batch, "direct colour resolve", + (struct asahi_bg_eot){0}); surf = &resolve_surf; } const struct pipe_surface *surf1 = dual_mrt_active ? &batch->key.cbufs[1] : NULL; if (!surf->texture || (dual_mrt_active && !surf1->texture) || descriptor_size != AGX_IOTO_G17P_RT0_PBE_ARGUMENT_SIZE) - agx_native_descriptor_rejected(dev, "end-of-tile attachment"); + agx_rejected_unless_oom(batch, "end-of-tile attachment", + (struct asahi_bg_eot){0}); if (key.multisample_store && (dual_mrt_active || util_res_sample_count(surf->texture) != key.tib.nr_samples)) - agx_native_descriptor_rejected(dev, "multisample store attachment"); + agx_rejected_unless_oom(batch, "multisample store attachment", + (struct asahi_bg_eot){0}); const size_t pbe_size = descriptor_size * attachment_count; struct agx_ptr pbe = @@ -12808,7 +12848,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) const struct pipe_surface *attachment = rt == first_rt ? surf : &batch->key.cbufs[rt]; if (!attachment->texture) - agx_native_descriptor_rejected(dev, "end-of-tile MRT attachment"); + agx_rejected_unless_oom(batch, "end-of-tile MRT attachment", + (struct asahi_bg_eot){0}); struct pipe_image_view view = image_view_for_surface(attachment); view.format = agx_g17p_rt0_pbe_format(view.format); const bool no_compress = @@ -12822,7 +12863,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) if (key.store_and_resolve) { if (!agx_g17p_direct_resolve_surface(batch, &resolve_surf)) - agx_native_descriptor_rejected(dev, "source-preserving resolve"); + agx_rejected_unless_oom(batch, "source-preserving resolve", + (struct asahi_bg_eot){0}); struct pipe_image_view view = image_view_for_surface(&resolve_surf); view.format = agx_g17p_rt0_pbe_format(view.format); struct agx_gallium_pbe_descriptor *argument = @@ -12873,7 +12915,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) struct agx3_g17p_rt0_eot_program native; if (!agx3_g17p_rt0_eot_program_generate(dev->chip, &desc, &native) || native.length > capacity) - agx_native_descriptor_rejected(dev, "end-of-tile Ioto program"); + agx_rejected_unless_oom(batch, "end-of-tile Ioto program", + (struct asahi_bg_eot){0}); memcpy(program_ptr.cpu, native.bytes, native.length); program_length = native.length; resource_spec = AGX_IOTO_G17P_EOT_RESOURCE_SPEC; @@ -12899,7 +12942,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) native.length > capacity || !agx3_g17p_rt0_bg_resource_spec( dev->chip, AGX3_G17P_RT0_BG_NONE, &resource_spec)) - agx_native_descriptor_rejected(dev, "empty background Ioto program"); + agx_rejected_unless_oom(batch, "empty background Ioto program", + (struct asahi_bg_eot){0}); memcpy(program_ptr.cpu, native.bytes, native.length); program_length = native.length; } else { @@ -12907,12 +12951,13 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) struct pipe_surface resolve_surf; if (key.single_sample_reload) { if (!agx_g17p_direct_resolve_surface(batch, &resolve_surf)) - agx_native_descriptor_rejected(dev, - "single-sample WSI reload"); + agx_rejected_unless_oom(batch, "single-sample WSI reload", + (struct asahi_bg_eot){0}); surf = &resolve_surf; } if (!surf->texture) - agx_native_descriptor_rejected(dev, "background attachment"); + agx_rejected_unless_oom(batch, "background attachment", + (struct asahi_bg_eot){0}); struct agx3_g17p_rt0_bg_program_desc desc = { .program_va = program_ptr.gpu, @@ -12934,12 +12979,15 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) if (op == AGX_BG_CLEAR) { if (!batch->uploaded_clear_color[first_rt]) - agx_native_descriptor_rejected(dev, "background clear value"); + agx_rejected_unless_oom(batch, "background clear value", + (struct asahi_bg_eot){0}); if (attachment_count > 1 || first_rt != 0) { union pipe_color_union clear_values[PIPE_MAX_COLOR_BUFS] = {0}; u_foreach_bit(rt, attachment_mask) { if (!batch->uploaded_clear_color[rt]) - agx_native_descriptor_rejected(dev, "background MRT clear value"); + agx_rejected_unless_oom(batch, + "background MRT clear value", + (struct asahi_bg_eot){0}); clear_values[rt] = batch->clear_color[rt]; } const size_t clear_size = @@ -12947,8 +12995,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) desc.clear_value_va = agx_pool_upload_aligned( &batch->pool, clear_values, clear_size, 16); if (!desc.clear_value_va) - agx_native_descriptor_rejected(dev, - "background MRT clear upload"); + agx_rejected_unless_oom(batch, "background MRT clear upload", + (struct asahi_bg_eot){0}); } else { desc.clear_value_va = batch->uploaded_clear_color[0]; } @@ -12962,7 +13010,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) const struct pipe_surface *attachment = rt == first_rt ? surf : &batch->key.cbufs[rt]; if (!attachment->texture) - agx_native_descriptor_rejected(dev, "background MRT attachment"); + agx_rejected_unless_oom(batch, "background MRT attachment", + (struct asahi_bg_eot){0}); struct pipe_sampler_view sampler_view = sampler_view_for_surface(attachment); struct agx_gallium_texture_descriptor *texture = @@ -12977,7 +13026,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) const unsigned sampler_size = agx_chip_sampler_heap_element_size(dev->chip); if (sampler_size != AGX_GALLIUM_SAMPLER_AGX3_SIZE) - agx_native_descriptor_rejected(dev, "background sampler geometry"); + agx_rejected_unless_oom(batch, "background sampler geometry", + (struct asahi_bg_eot){0}); /* Staged multisample texel fetches select sampler one. Ordinary * reloads use zero; both entries have the native txf state. */ const unsigned sampler_count = @@ -12985,7 +13035,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) struct agx_ptr sampler = agx_pool_alloc_aligned( &batch->pool, sampler_count * sampler_size, 64); if (!agx_gallium_pack_g17p_txf_sampler(dev->chip, sampler.cpu)) - agx_native_descriptor_rejected(dev, "background texel-fetch sampler"); + agx_rejected_unless_oom(batch, "background texel-fetch sampler", + (struct asahi_bg_eot){0}); if (sampler_count > 1) memcpy((uint8_t *)sampler.cpu + sampler_size, sampler.cpu, @@ -13003,7 +13054,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) native.length > capacity || !agx3_g17p_rt0_bg_resource_spec(dev->chip, desc.op, &resource_spec)) - agx_native_descriptor_rejected(dev, "background Ioto program"); + agx_rejected_unless_oom(batch, "background Ioto program", + (struct asahi_bg_eot){0}); memcpy(program_ptr.cpu, native.bytes, native.length); program_length = native.length; } @@ -13138,7 +13190,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) key.reserved_preamble = uniforms; struct agx_bg_eot_shader *shader = agx_get_bg_eot_shader(&ctx->bg_eot, &key); if (!shader) - agx_native_descriptor_rejected(dev, "background shader"); + agx_rejected_unless_oom(batch, "background shader", + (struct asahi_bg_eot){0}); agx_batch_add_bo(batch, shader->bo); assert(shader->info.rodata.size_16 == 0); @@ -15976,19 +16029,28 @@ agx_ensure_cmdbuf_has_space(struct agx_batch *batch, struct agx_encoder *enc, /* Jump from the old command buffer to the new command buffer */ struct agx_device *dev = agx_device(batch->ctx->base.screen); - if (vdm && agx_chip_native_agx3(dev->chip)) { + if (unlikely(!target_bo)) { + /* Out of memory: keep recording into the pool's host memory, the batch + * is dropped (agx_batch_alloc_failed). */ + } else if (vdm && agx_chip_native_agx3(dev->chip)) { const uint64_t alias = agx_batch_get_g17p_vdm_alias(batch, target_bo, T.gpu); uint32_t compact = 0; struct agx_vdm_stream_link_core link; - const bool packed = agx_g17p_vdm_alias_compact(alias, &compact) && - agx_vdm_stream_link_core_pack(dev->chip, compact, - &link); - assert(packed); - if (!packed) - return; - memcpy(enc->current, link.bytes, link.length); + /* Mapping the alias can fail like a BO. */ + if (!alias) { + batch->pool.failed = true; + } else { + const bool packed = + agx_g17p_vdm_alias_compact(alias, &compact) && + agx_vdm_stream_link_core_pack(dev->chip, compact, &link); + assert(packed); + if (!packed) + return; + + memcpy(enc->current, link.bytes, link.length); + } batch->g17p_bind_tail_latched = false; batch->g17p_have_latched_vs = false; } else if (vdm && agx_chip_g17_m5(dev->chip)) { @@ -16081,6 +16143,12 @@ agx_batch_heap(struct agx_batch *batch) if (!ctx->heap) { ctx->heap = pipe_buffer_create(ctx->base.screen, PIPE_BIND_GLOBAL, PIPE_USAGE_DEFAULT, size); + + /* The batch is dropped (agx_batch_alloc_failed) */ + if (!ctx->heap) { + batch->pool.failed = true; + return 0; + } } struct poly_heap heap = { @@ -20002,8 +20070,7 @@ agx_launch(struct agx_batch *batch, struct agx_grid grid, !batch->samplers[stage] || (batch->ctx->stage[stage].custom_borders && !cs->g17p_custom_border_emulation)) - agx_native_descriptor_rejected(dev, - "compute texture resources"); + agx_rejected_unless_oom(batch, "compute texture resources"); const uint64_t pointers[2] = { batch->stage_uniforms[stage].texture_base, @@ -20012,8 +20079,7 @@ agx_launch(struct agx_batch *batch, struct agx_grid grid, const uint64_t pointer_va = agx_pool_upload_aligned( &batch->pool, pointers, sizeof(pointers), 8); if (!pointer_va) - agx_native_descriptor_rejected(dev, - "compute texture pointers"); + agx_rejected_unless_oom(batch, "compute texture pointers"); ranges[range_count++] = (struct agx3_ioto_uniform_range){ .start_halfs = 0, @@ -20021,7 +20087,7 @@ agx_launch(struct agx_batch *batch, struct agx_grid grid, .address = pointer_va, }; } else if (cs->g17p_texture_resources) { - agx_native_descriptor_rejected(dev, "compute texture table"); + agx_rejected_unless_oom(batch, "compute texture table"); } for (unsigned i = 0; i < cs->push_range_count; ++i) { @@ -20031,11 +20097,10 @@ agx_launch(struct agx_batch *batch, struct agx_grid grid, continue; if (resource_pair && push->uniform < AGX_ABI_G17P_TEXTURE_POINTER_HALFS) - agx_native_descriptor_rejected(dev, - "compute texture ABI overlap"); + agx_rejected_unless_oom(batch, "compute texture ABI overlap"); if (!table || (push->uniform & 1) || (push->length & 1) || !push->length || range_count == ARRAY_SIZE(ranges)) - agx_native_descriptor_rejected(dev, "compute uniform range"); + agx_rejected_unless_oom(batch, "compute uniform range"); ranges[range_count++] = (struct agx3_ioto_uniform_range){ .start_halfs = push->uniform, .size_halfs = push->length, @@ -20051,7 +20116,7 @@ agx_launch(struct agx_batch *batch, struct agx_grid grid, range_count == ARRAY_SIZE(ranges) || rodata->offset > cs->bo->size || size_B > cs->bo->size - rodata->offset) - agx_native_descriptor_rejected(dev, "compute constant range"); + agx_rejected_unless_oom(batch, "compute constant range"); ranges[range_count++] = (struct agx3_ioto_uniform_range){ .start_halfs = rodata->base_uniform, .size_halfs = rodata->size_16, @@ -20062,7 +20127,7 @@ agx_launch(struct agx_batch *batch, struct agx_grid grid, if (!agx_g17p_append_compute(batch, cs, &cs->b.info, cs->b.binary, cs->bo, linked, grid, wg, ranges, range_count, variable_shared_mem)) - agx_native_descriptor_rejected(dev, "compute Ioto dispatch"); + agx_rejected_unless_oom(batch, "compute Ioto dispatch"); return; } diff --git a/src/gallium/drivers/asahi/agx_state.h b/src/gallium/drivers/asahi/agx_state.h index 7299de90a05e..10112aabb9c9 100644 --- a/src/gallium/drivers/asahi/agx_state.h +++ b/src/gallium/drivers/asahi/agx_state.h @@ -2076,11 +2076,29 @@ agx_batch_is_compute(struct agx_batch *batch) return batch->key.width == AGX_COMPUTE_BATCH_WIDTH; } +/* An allocation for the batch ran out of GPU memory (agx_pool_fail): it went + * on recording into host memory and is dropped at flush, never submitted. An + * upload into the context's internal-program cache fails its lookup, which + * runs for the batch recording now, so charge that to this batch. + */ +static inline bool +agx_batch_alloc_failed(struct agx_batch *batch) +{ + if (unlikely(batch->ctx->bg_eot.pool.failed)) { + batch->ctx->bg_eot.pool.failed = false; + batch->pool.failed = true; + } + + return batch->pool.failed || batch->pipeline_pool.failed; +} + struct agx_batch *agx_get_batch(struct agx_context *ctx); struct agx_batch *agx_get_compute_batch(struct agx_context *ctx); void agx_batch_reset(struct agx_context *ctx, struct agx_batch *batch); void agx_batch_retire_faulted(struct agx_context *ctx, struct agx_batch *batch); +void agx_batch_drop_alloc_failed(struct agx_context *ctx, + struct agx_batch *batch); int agx_cleanup_batches(struct agx_context *ctx); void agx_batch_add_timestamp_query(struct agx_batch *batch, diff --git a/src/gallium/drivers/asahi/agx_uniforms.c b/src/gallium/drivers/asahi/agx_uniforms.c index a75a63e81e5c..d55e59a54dc2 100644 --- a/src/gallium/drivers/asahi/agx_uniforms.c +++ b/src/gallium/drivers/asahi/agx_uniforms.c @@ -268,6 +268,8 @@ agx_set_sampler_uniforms(struct agx_batch *batch, mesa_shader_stage stage) agx_sampler_heap_add( dev, &batch->sampler_heap, &st->samplers[s]->desc_without_custom_border); + if (!batch->sampler_heap.bo) + batch->pool.failed = true; } } } From d3574429ba0b7ad064ec08a10fe16cbc389ba45d Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Mon, 30 Nov 2026 01:38:20 -0300 Subject: [PATCH 2/6] asahi: fail creation cleanly when a BO cannot be allocated 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 --- src/gallium/drivers/asahi/agx_pipe.c | 78 ++++++++++++++++++--------- src/gallium/drivers/asahi/agx_query.c | 5 ++ 2 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index 1758ea18731b..1558c0c7e53b 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -1511,9 +1511,24 @@ agx_transfer_map(struct pipe_context *pctx, struct pipe_resource *resource, simple_mtx_unlock(&range->write_mutex); } + struct pipe_resource *staging_rsrc = NULL; + if (staging_blit) { + /* Should never happen for buffers, and it's not safe */ + assert(resource->target != PIPE_BUFFER); + + struct agx_resource *staging = + agx_alloc_staging(pctx->screen, rsrc, level, box); + if (!staging) + return NULL; + + staging_rsrc = &staging->base.b; + } + struct agx_transfer *transfer = agx_alloc_transfer(ctx, usage); - if (!transfer) + if (!transfer) { + pipe_resource_reference(&staging_rsrc, NULL); return NULL; + } transfer->base.b.level = level; transfer->base.b.usage = usage; transfer->base.b.box = *box; @@ -1522,12 +1537,7 @@ agx_transfer_map(struct pipe_context *pctx, struct pipe_resource *resource, *out_transfer = &transfer->base.b; if (staging_blit) { - /* Should never happen for buffers, and it's not safe */ - assert(resource->target != PIPE_BUFFER); - - struct agx_resource *staging = - agx_alloc_staging(pctx->screen, rsrc, level, box); - assert(staging); + struct agx_resource *staging = agx_resource(staging_rsrc); /* Staging resources have one LOD: level 0. Query the strides * on this LOD. @@ -1830,14 +1840,19 @@ agx_clear(struct pipe_context *pctx, unsigned buffers, assert((batch->draw & slowclear) == slowclear); } -static void +/* False without memory for the new layout: rsrc keeps its old one, the same + * state as a shared resource agx_decompress() refuses to reallocate. */ +static bool transition_resource(struct pipe_context *pctx, struct agx_resource *rsrc, struct pipe_resource *templ) { struct agx_resource *new_res = agx_resource(pctx->screen->resource_create(pctx->screen, templ)); - assert(new_res); + if (!new_res) { + mesa_loge("asahi: no memory to reallocate a resource"); + return false; + } assert(!(rsrc->base.b.bind & PIPE_BIND_SHARED) && !agx_resource_is_shared_interchange(rsrc) && "cannot swap BOs if shared"); @@ -1873,6 +1888,7 @@ transition_resource(struct pipe_context *pctx, struct agx_resource *rsrc, /* Free the new resource, which now owns the old BO */ pipe_resource_reference((struct pipe_resource **)&new_res, NULL); + return true; } void @@ -1921,7 +1937,8 @@ agx_flush_resource(struct pipe_context *pctx, struct pipe_resource *pres) struct pipe_resource templ = *pres; templ.bind |= PIPE_BIND_SHARED; - transition_resource(pctx, rsrc, &templ); + if (!transition_resource(pctx, rsrc, &templ)) + return; } else { /* Otherwise just claim it's already shared */ pres->bind |= PIPE_BIND_SHARED; @@ -2983,6 +3000,23 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) } pctx->const_uploader = pctx->stream_uploader; + struct agx_device *dev = agx_device(screen); + if (screen->caps.query_timestamp || screen->caps.query_time_elapsed) { + size_t timestamps_size = sizeof(struct agx_timestamps) * AGX_MAX_BATCHES; + + /* The kernel requires that timestamp buffers are SHARED. */ + ctx->timestamps = + agx_bo_create(dev, timestamps_size, 0, + AGX_BO_WRITEBACK | AGX_BO_SHARED, "Timestamp buffer"); + if (!ctx->timestamps || + agx_bind_timestamps(dev, ctx->timestamps, &ctx->timestamp_handle)) { + agx_bo_unreference(dev, ctx->timestamps); + u_upload_destroy(pctx->stream_uploader); + ralloc_free(ctx); + return NULL; + } + } + enum drm_asahi_priority priority = DRM_ASAHI_PRIORITY_MEDIUM; if (flags & PIPE_CONTEXT_LOW_PRIORITY) @@ -3002,6 +3036,10 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) agx_device(screen), priority, &ctx->async_submit); } if (ctx->queue_id == UINT32_MAX) { + if (ctx->timestamps) { + dev->ops.bo_unbind_object(dev, ctx->timestamp_handle); + agx_bo_unreference(dev, ctx->timestamps); + } u_upload_destroy(pctx->stream_uploader); ralloc_free(ctx); return NULL; @@ -3085,20 +3123,6 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) ctx->blitter = util_blitter_create(pctx); ctx->compute_blitter.blit_cs = asahi_blit_key_table_create(ctx); - struct agx_device *dev = agx_device(screen); - if (screen->caps.query_timestamp || screen->caps.query_time_elapsed) { - size_t timestamps_size = sizeof(struct agx_timestamps) * AGX_MAX_BATCHES; - - /* The kernel requires that timestamp buffers are SHARED. */ - ctx->timestamps = - agx_bo_create(dev, timestamps_size, 0, AGX_BO_WRITEBACK | AGX_BO_SHARED, - "Timestamp buffer"); - assert(ctx->timestamps); - - ret = agx_bind_timestamps(dev, ctx->timestamps, &ctx->timestamp_handle); - assert(!ret); - } - /* Sync object/FD used for NATIVE_FENCE_FD. */ ctx->in_sync_fd = -1; ret = drmSyncobjCreate(agx_device(screen)->fd, 0, &ctx->in_sync_obj); @@ -4001,6 +4025,12 @@ agx_screen_create(int fd, struct renderonly *ro, { struct agx_bo *bo = agx_bo_create(&agx_screen->dev, 16384, 0, 0, "Rodata"); + if (!bo) { + /* The caller keeps renderonly when screen creation fails. */ + agx_screen->dev.ro = NULL; + agx_destroy_screen(screen); + return NULL; + } agx_pack_txf_sampler((struct agx_sampler_packed *)agx_bo_map(bo)); diff --git a/src/gallium/drivers/asahi/agx_query.c b/src/gallium/drivers/asahi/agx_query.c index 87537a37d1b7..b9181061ea25 100644 --- a/src/gallium/drivers/asahi/agx_query.c +++ b/src/gallium/drivers/asahi/agx_query.c @@ -193,6 +193,11 @@ agx_create_query(struct pipe_context *ctx, unsigned query_type, unsigned index) */ query->bo = agx_bo_create(agx_device(ctx->screen), sizeof(uint64_t) * 2, 0, AGX_BO_WRITEBACK, "Query"); + if (!query->bo) { + free(query); + return NULL; + } + query->ptr = (struct agx_ptr){ .gpu = query->bo->va->addr, .cpu = agx_bo_map(query->bo), From ce8060ea7f126109b6ea106bef98628cd4a1e97c Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Mon, 30 Nov 2026 01:38:21 -0300 Subject: [PATCH 3/6] asahi: retry a shader executable upload that ran out of memory 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 --- src/gallium/drivers/asahi/agx_disk_cache.c | 43 ++++++++--------- src/gallium/drivers/asahi/agx_state.c | 54 ++++++++++++++++------ src/gallium/drivers/asahi/agx_state.h | 9 ++++ 3 files changed, 68 insertions(+), 38 deletions(-) diff --git a/src/gallium/drivers/asahi/agx_disk_cache.c b/src/gallium/drivers/asahi/agx_disk_cache.c index a13e59767e0f..5c20687fcd96 100644 --- a/src/gallium/drivers/asahi/agx_disk_cache.c +++ b/src/gallium/drivers/asahi/agx_disk_cache.c @@ -115,33 +115,23 @@ read_shader(struct agx_screen *screen, struct blob_reader *blob, blob_copy_bytes(blob, &binary->b.info, sizeof(binary->b.info)); size_t size = binary->b.info.binary_size; - if (uncompiled->type == MESA_SHADER_VERTEX || - uncompiled->type == MESA_SHADER_TESS_EVAL || - uncompiled->type == MESA_SHADER_FRAGMENT) { - + /* Graphics mains are fast-linked from their CPU copy, and G17P's native + * compute Ioto program is built from the shader bytes at dispatch time. + * Every variant keeps the copy while its upload may still be retried. */ + const bool keep_binary = uncompiled->type == MESA_SHADER_VERTEX || + uncompiled->type == MESA_SHADER_TESS_EVAL || + uncompiled->type == MESA_SHADER_FRAGMENT || + agx_chip_native_or_m5(screen->dev.chip); + + if (size || keep_binary) { binary->b.binary = malloc(size); blob_copy_bytes(blob, binary->b.binary, size); + } - if (size) { - binary->bo = agx_bo_create(&screen->dev, size, 0, - AGX_BO_EXEC | AGX_BO_LOW_VA, "Executable"); - memcpy(agx_bo_map(binary->bo), binary->b.binary, size); - } - } else if (size) { - /* G17P's native compute Ioto program is built from the shader bytes at - * dispatch time, so a cached compute shader needs the CPU copy as well - * as its executable BO. Keeping only the BO made the uncached dispatch - * work and the identical cached dispatch fail descriptor validation. */ - binary->bo = agx_bo_create(&screen->dev, size, 0, - AGX_BO_EXEC | AGX_BO_LOW_VA, "Executable"); - if (agx_chip_native_or_m5(screen->dev.chip)) { - /* Native compute Ioto programs need the cached CPU shader bytes. */ - binary->b.binary = malloc(size); - blob_copy_bytes(blob, binary->b.binary, size); - memcpy(agx_bo_map(binary->bo), binary->b.binary, size); - } else { - blob_copy_bytes(blob, agx_bo_map(binary->bo), size); - } + if (size && agx_upload_compiled_shader(&screen->dev, binary) && + !keep_binary) { + free(binary->b.binary); + binary->b.binary = NULL; } blob_copy_bytes(blob, &binary->uvs, sizeof(binary->uvs)); @@ -181,6 +171,11 @@ read_shader(struct agx_screen *screen, struct blob_reader *blob, if (blob_read_uint8(blob)) binary->gs_count = read_shader(screen, blob, uncompiled, false); + + /* As agx_compile_variant(): the variant is only as usable as its parts */ + binary->failed |= binary->pre_gs->failed || + (binary->gs_copy && binary->gs_copy->failed) || + (binary->gs_count && binary->gs_count->failed); } return binary; diff --git a/src/gallium/drivers/asahi/agx_state.c b/src/gallium/drivers/asahi/agx_state.c index 0e985dd8d71d..bb250e8bafe5 100644 --- a/src/gallium/drivers/asahi/agx_state.c +++ b/src/gallium/drivers/asahi/agx_state.c @@ -3238,13 +3238,8 @@ agx_compile_nir_tile(struct agx_device *dev, nir_shader *nir, agx2_stats_util_debug(debug, _mesa_shader_stage_to_abbrev(nir->info.stage), &compiled->b.info.stats); - if (compiled->b.info.binary_size && !secondary) { - compiled->bo = agx_bo_create(dev, compiled->b.info.binary_size, 0, - AGX_BO_EXEC | AGX_BO_LOW_VA, "Executable"); - - memcpy(agx_bo_map(compiled->bo), compiled->b.binary, - compiled->b.info.binary_size); - } + if (compiled->b.info.binary_size && !secondary) + agx_upload_compiled_shader(dev, compiled); agx_compute_shader_upload_sizes(compiled); return compiled; @@ -3257,6 +3252,28 @@ agx_build_meta_shader_internal(struct agx_context *ctx, unsigned cf_base, unsigned nr_samples, bool internal_kernel); +/* Out of memory, the variant is skipped like a refused one until a use + * retries the upload (asahi_fast_link, agx_drop_for_unusable_program). The + * variant is shared by every context of a share group, so a retry publishes + * the filled BO once and readers test the BO (agx_program_usable). */ +bool +agx_upload_compiled_shader(struct agx_device *dev, + struct agx_compiled_shader *cs) +{ + struct agx_bo *bo = agx_bo_create(dev, cs->b.info.binary_size, 0, + AGX_BO_EXEC | AGX_BO_LOW_VA, "Executable"); + if (!bo) { + cs->upload_failed = true; + return false; + } + + memcpy(agx_bo_map(bo), cs->b.binary, cs->b.info.binary_size); + if (p_atomic_cmpxchg(&cs->bo, NULL, bo) != NULL) + agx_bo_unreference(dev, bo); + + return true; +} + static void agx_gallium_preprocess_nir(struct agx_device *dev, nir_shader *nir) { @@ -5282,7 +5299,8 @@ agx_update_shader(struct agx_context *ctx, struct agx_compiled_shader **out, static bool agx_program_usable(const struct agx_compiled_shader *cs) { - return cs != NULL && !cs->failed; + return cs != NULL && !cs->failed && + (!cs->upload_failed || p_atomic_read(&cs->bo)); } /* @@ -5295,6 +5313,9 @@ agx_drop_for_unusable_program(struct agx_context *ctx, struct agx_compiled_shader *cs, bool linked, const char *stage) { + if (cs && cs->upload_failed && !p_atomic_read(&cs->bo)) + agx_upload_compiled_shader(agx_device(ctx->base.screen), cs); + if (agx_program_usable(cs) && linked) return false; @@ -5305,12 +5326,14 @@ agx_drop_for_unusable_program(struct agx_context *ctx, cs->refusal_reported = true; mesa_loge("asahi: dropping work: the %s program is unusable (%s). This " - "is a driver gap; run with AGX_MESA_DEBUG=shaders to see the " - "instruction the backend refused.", + "is a driver gap unless out of memory; run with " + "AGX_MESA_DEBUG=shaders to see the instruction the backend " + "refused.", stage, - cs == NULL ? "no compiled variant" - : cs->failed ? "the backend refused to encode it" - : "it could not be linked"); + cs == NULL ? "no compiled variant" + : cs->upload_failed && !cs->bo ? "no memory for its executable" + : cs->failed ? "the backend refused to encode it" + : "it could not be linked"); } return true; @@ -5785,7 +5808,10 @@ asahi_fast_link(struct agx_context *ctx, struct agx_uncompiled_shader *so, * runs no API code at all. Refusing here keeps ctx->linked.* NULL, which * the draw path already treats as "this draw cannot be issued". */ - if (key->main == NULL || key->main->failed) + if (key->main && key->main->upload_failed && !p_atomic_read(&key->main->bo)) + agx_upload_compiled_shader(agx_device(ctx->base.screen), key->main); + + if (!agx_program_usable(key->main)) return NULL; struct agx_compiled_shader *prolog = NULL, *epilog = NULL; diff --git a/src/gallium/drivers/asahi/agx_state.h b/src/gallium/drivers/asahi/agx_state.h index 10112aabb9c9..63c62293e139 100644 --- a/src/gallium/drivers/asahi/agx_state.h +++ b/src/gallium/drivers/asahi/agx_state.h @@ -302,6 +302,12 @@ struct agx_compiled_shader { */ bool failed; + /* The executable BO could not be allocated. Until bo is set the variant + * is unusable (agx_program_usable); the binary is kept and + * agx_upload_compiled_shader() retried when the program is used. + */ + bool upload_failed; + /* Set once the draw/dispatch path has reported that this program is * unusable, so a refused shader logs once instead of once per draw. */ @@ -1399,6 +1405,9 @@ void agx_init_meta_shaders(struct agx_context *ctx); void agx_destroy_meta_shaders(struct agx_context *ctx); +bool agx_upload_compiled_shader(struct agx_device *dev, + struct agx_compiled_shader *cs); + struct agx_compiled_shader *agx_build_meta_shader(struct agx_context *ctx, meta_shader_builder_t builder, void *data, size_t data_size); From fe87e210ed8f1b3ebec03ac4014247bc09c51ecc Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Mon, 30 Nov 2026 01:38:21 -0300 Subject: [PATCH 4/6] asahi, borealis: keep the scratch buffer when growing it fails 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 --- src/asahi/lib/agx_scratch.c | 31 ++++++++++++++++++--------- src/asahi/lib/agx_scratch.h | 2 +- src/asahi/vulkan/hk_cmd_buffer.c | 6 +++++- src/asahi/vulkan/hk_device.h | 5 +++-- src/gallium/drivers/asahi/agx_state.c | 14 ++++++++---- 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/asahi/lib/agx_scratch.c b/src/asahi/lib/agx_scratch.c index 049ecf6463d2..741796b1d294 100644 --- a/src/asahi/lib/agx_scratch.c +++ b/src/asahi/lib/agx_scratch.c @@ -69,12 +69,9 @@ agx_scratch_get_bucket(uint32_t dwords) 1 + util_logbase2_ceil(DIV_ROUND_UP(dwords, AGX_SPILL_UNIT_DWORDS))); } -static void +static bool agx_scratch_realloc(struct agx_scratch *scratch) { - if (scratch->buf) - agx_bo_unreference(scratch->dev, scratch->buf); - struct spill_size size = agx_scratch_get_spill_size(scratch->size_dwords); if (scratch->dev->debug & AGX_DBG_SCRATCH) @@ -111,8 +108,13 @@ agx_scratch_realloc(struct agx_scratch *scratch) #ifdef SCRATCH_DEBUG flags = AGX_BO_WRITEBACK; #endif - scratch->buf = agx_bo_create(scratch->dev, total_alloc, block_size_bytes, - flags, "Scratch"); + struct agx_bo *buf = agx_bo_create(scratch->dev, total_alloc, + block_size_bytes, flags, "Scratch"); + if (!buf) + return false; + + agx_bo_unreference(scratch->dev, scratch->buf); + scratch->buf = buf; void *map = agx_bo_map(scratch->buf); memset(map, 0, blocks_off); @@ -181,16 +183,21 @@ agx_scratch_realloc(struct agx_scratch *scratch) if (scratch->dev->debug & AGX_DBG_SCRATCH) fprintf(stderr, "New Scratch @ 0x%" PRIx64 " (size: 0x%zx)\n", scratch->buf->va->addr, scratch->buf->size); + return true; } -void +/* False when the larger buffer could not be allocated: the old one stays, too + * small for the caller's shader. */ +bool agx_scratch_alloc(struct agx_scratch *scratch, unsigned dwords, size_t subgroups) { bool realloc = false; + const uint32_t old_dwords = scratch->size_dwords; + const uint32_t old_subgroups = scratch->subgroups; if (!dwords) - return; + return true; assert(dwords <= AGX_MAX_SCRATCH_DWORDS && "Scratch size too large"); @@ -209,9 +216,13 @@ agx_scratch_alloc(struct agx_scratch *scratch, unsigned dwords, realloc = true; } - if (realloc) { - agx_scratch_realloc(scratch); + if (realloc && !agx_scratch_realloc(scratch)) { + scratch->size_dwords = old_dwords; + scratch->subgroups = old_subgroups; + return false; } + + return true; } void diff --git a/src/asahi/lib/agx_scratch.h b/src/asahi/lib/agx_scratch.h index eab82f5d5793..7b9991ae2a5f 100644 --- a/src/asahi/lib/agx_scratch.h +++ b/src/asahi/lib/agx_scratch.h @@ -35,5 +35,5 @@ void agx_scratch_debug_pre(struct agx_scratch *scratch); void agx_scratch_debug_post(struct agx_scratch *scratch); uint32_t agx_scratch_get_bucket(uint32_t dwords); -void agx_scratch_alloc(struct agx_scratch *scratch, uint32_t dwords, +bool agx_scratch_alloc(struct agx_scratch *scratch, uint32_t dwords, size_t subgroups); diff --git a/src/asahi/vulkan/hk_cmd_buffer.c b/src/asahi/vulkan/hk_cmd_buffer.c index 8592ce8b549b..fbc6e3d791a6 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.c +++ b/src/asahi/vulkan/hk_cmd_buffer.c @@ -2674,7 +2674,11 @@ hk_reserve_scratch_slow(struct hk_cmd_buffer *cmd, struct hk_cs *cs, unsigned preamble_size = (s->b.info.preamble_scratch_size > 0) ? 1 : 0; /* Note: this uses the hardware stage, not the software stage */ - hk_device_alloc_scratch(dev, s->b.info.stage, max_scratch_size); + if (!hk_device_alloc_scratch(dev, s->b.info.stage, max_scratch_size)) { + vk_command_buffer_set_error(&cmd->vk, VK_ERROR_OUT_OF_DEVICE_MEMORY); + return; + } + perf_debug(cmd, "Reserving %u (%u) bytes of scratch for stage %s", s->b.info.scratch_size, s->b.info.preamble_scratch_size, _mesa_shader_stage_to_abbrev(s->b.info.stage)); diff --git a/src/asahi/vulkan/hk_device.h b/src/asahi/vulkan/hk_device.h index 7e1b4d0f0fed..9a7e691e217c 100644 --- a/src/asahi/vulkan/hk_device.h +++ b/src/asahi/vulkan/hk_device.h @@ -699,13 +699,14 @@ hk_device_scratch_locked(struct hk_device *dev, mesa_shader_stage stage) } } -static inline void +static inline bool hk_device_alloc_scratch(struct hk_device *dev, mesa_shader_stage stage, unsigned size) { simple_mtx_lock(&dev->scratch.lock); - agx_scratch_alloc(hk_device_scratch_locked(dev, stage), size, 0); + bool ok = agx_scratch_alloc(hk_device_scratch_locked(dev, stage), size, 0); simple_mtx_unlock(&dev->scratch.lock); + return ok; } /* frg-codegen v4: device-lifetime cache of single-attachment G17P BG/EOT diff --git a/src/gallium/drivers/asahi/agx_state.c b/src/gallium/drivers/asahi/agx_state.c index bb250e8bafe5..794f05355e5d 100644 --- a/src/gallium/drivers/asahi/agx_state.c +++ b/src/gallium/drivers/asahi/agx_state.c @@ -9558,22 +9558,28 @@ agx_build_pipeline(struct agx_batch *batch, struct agx_compiled_shader *cs, if (max_scratch_size > 0) { unsigned preamble_size = (cs->b.info.preamble_scratch_size > 0) ? 1 : 0; - + /* Without scratch for this shader, drop the batch */ switch (phys_stage) { case MESA_SHADER_FRAGMENT: - agx_scratch_alloc(&ctx->scratch_fs, max_scratch_size, max_subgroups); + if (!agx_scratch_alloc(&ctx->scratch_fs, max_scratch_size, + max_subgroups)) + batch->pool.failed = true; batch->fs_scratch = true; batch->fs_preamble_scratch = MAX2(batch->fs_preamble_scratch, preamble_size); break; case MESA_SHADER_VERTEX: - agx_scratch_alloc(&ctx->scratch_vs, max_scratch_size, max_subgroups); + if (!agx_scratch_alloc(&ctx->scratch_vs, max_scratch_size, + max_subgroups)) + batch->pool.failed = true; batch->vs_scratch = true; batch->vs_preamble_scratch = MAX2(batch->vs_preamble_scratch, preamble_size); break; default: - agx_scratch_alloc(&ctx->scratch_cs, max_scratch_size, max_subgroups); + if (!agx_scratch_alloc(&ctx->scratch_cs, max_scratch_size, + max_subgroups)) + batch->pool.failed = true; batch->cs_scratch = true; batch->cs_preamble_scratch = MAX2(batch->cs_preamble_scratch, preamble_size); From defa1189cb2edb605baa42739308ba01c81d1a4e Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Mon, 30 Nov 2026 12:23:40 -0300 Subject: [PATCH 5/6] asahi: drop a batch whose user constant buffer could not be uploaded 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 --- src/gallium/drivers/asahi/agx_uniforms.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/gallium/drivers/asahi/agx_uniforms.c b/src/gallium/drivers/asahi/agx_uniforms.c index d55e59a54dc2..83bc89381e89 100644 --- a/src/gallium/drivers/asahi/agx_uniforms.c +++ b/src/gallium/drivers/asahi/agx_uniforms.c @@ -46,9 +46,15 @@ agx_const_buffer_ptr(struct agx_batch *batch, mesa_shader_stage stage, if (stage != MESA_SHADER_FRAGMENT && stage != MESA_SHADER_COMPUTE) agx_batch_note_vtx_read(batch, rsrc); return agx_map_gpu(rsrc) + cb->buffer_offset; - } else { - return 0; } + + /* agx_set_constant_buffer() could not upload a user buffer: the shader + * would read address 0, so drop the batch (agx_batch_alloc_failed). + */ + if (cb->user_buffer) + batch->pool.failed = true; + + return 0; } void From b9be5460a9146252153b0e93666ec449d29981e8 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Mon, 30 Nov 2026 16:24:40 -0500 Subject: [PATCH 6/6] asahi: preserve scratch and shader state on upload failure 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 --- src/asahi/lib/agx_bg_eot.c | 81 ++++++++++++++++------ src/asahi/lib/agx_bg_eot.h | 11 +++ src/asahi/lib/agx_scratch.c | 19 +++-- src/asahi/lib/pool.c | 28 ++++++-- src/asahi/lib/tests/test-agx-bo-cache.c | 46 ++++++++++++ src/asahi/vulkan/hk_cmd_draw.c | 31 ++++++--- src/gallium/drivers/asahi/agx_disk_cache.c | 4 +- src/gallium/drivers/asahi/agx_state.c | 14 ++-- src/gallium/drivers/asahi/agx_state.h | 3 +- 9 files changed, 187 insertions(+), 50 deletions(-) diff --git a/src/asahi/lib/agx_bg_eot.c b/src/asahi/lib/agx_bg_eot.c index 27b326642bd7..0728412e4d9d 100644 --- a/src/asahi/lib/agx_bg_eot.c +++ b/src/asahi/lib/agx_bg_eot.c @@ -89,7 +89,8 @@ agx_g17p_rt0_eot_text_supported(const struct agx_shader_part *bin) static bool agx_g17p_upload_bg_eot(struct agx_bg_eot_cache *cache, const struct agx_shader_part *bin, - struct agx_bg_eot_shader *res) + struct agx_bg_eot_shader *res, + enum agx_bg_eot_error *error) { /* Match the standalone allocation lead already used for G17P main * shaders. Native packed CODE blocks do not each own such a lead. */ @@ -122,8 +123,10 @@ agx_g17p_upload_bg_eot(struct agx_bg_eot_cache *cache, const size_t size = lead_size + block_size; uint8_t *image = malloc(size); - if (!image) + if (!image) { + *error = AGX_BG_EOT_HOST_OOM; return false; + } if (cache->dev->chip == AGX_CHIP_G18G) { if (!agx3_build_g18g_container_lead(image, lead_size)) { @@ -153,6 +156,8 @@ agx_g17p_upload_bg_eot(struct agx_bg_eot_cache *cache, &res->bo) : 0; if (!allocation) { + if (built) + *error = AGX_BG_EOT_DEVICE_OOM; free(image); return false; } @@ -288,9 +293,20 @@ agx_compile_bg_eot_nir(const struct agx_device *dev, nir_shader *shader, return true; } +static struct agx_bg_eot_shader * +agx_alloc_bg_eot_shader(struct agx_bg_eot_cache *cache, + enum agx_bg_eot_error *error) +{ + struct agx_bg_eot_shader *res = rzalloc(cache->ht, struct agx_bg_eot_shader); + if (!res) + *error = AGX_BG_EOT_HOST_OOM; + return res; +} + static struct agx_bg_eot_shader * agx_upload_bg_eot_part(struct agx_bg_eot_cache *cache, - struct agx_shader_part *bin, bool check_rt0_eot) + struct agx_shader_part *bin, bool check_rt0_eot, + enum agx_bg_eot_error *error) { if (check_rt0_eot && agx_chip_native_agx3(cache->dev->chip) && !agx_g17p_rt0_eot_text_supported(bin)) { @@ -298,10 +314,14 @@ agx_upload_bg_eot_part(struct agx_bg_eot_cache *cache, return NULL; } - struct agx_bg_eot_shader *res = rzalloc(cache->ht, struct agx_bg_eot_shader); + struct agx_bg_eot_shader *res = agx_alloc_bg_eot_shader(cache, error); + if (!res) { + free(bin->binary); + return NULL; + } res->info = bin->info; if (agx_chip_agx3_path(cache->dev->chip)) { - if (!agx_g17p_upload_bg_eot(cache, bin, res)) { + if (!agx_g17p_upload_bg_eot(cache, bin, res, error)) { free(bin->binary); ralloc_free(res); return NULL; @@ -312,8 +332,9 @@ agx_upload_bg_eot_part(struct agx_bg_eot_cache *cache, } free(bin->binary); - /* Out of memory (agx_pool_fail): do not cache a 0 address */ + /* Do not cache a failed upload. */ if (!res->ptr) { + *error = AGX_BG_EOT_DEVICE_OOM; ralloc_free(res); return NULL; } @@ -324,12 +345,13 @@ agx_upload_bg_eot_part(struct agx_bg_eot_cache *cache, static struct agx_bg_eot_shader * agx_compile_bg_eot_shader(struct agx_bg_eot_cache *cache, nir_shader *shader, struct agx_shader_key *key, - struct agx_tilebuffer_layout *tib) + struct agx_tilebuffer_layout *tib, + enum agx_bg_eot_error *error) { struct agx_shader_part bin = {0}; if (!agx_compile_bg_eot_nir(cache->dev, shader, key, tib, &bin)) return NULL; - return agx_upload_bg_eot_part(cache, &bin, tib == NULL); + return agx_upload_bg_eot_part(cache, &bin, tib == NULL, error); } static nir_def * @@ -553,7 +575,8 @@ agx_compile_background_shader(const struct agx_device *dev, static struct agx_bg_eot_shader * agx_build_background_shader(struct agx_bg_eot_cache *cache, - struct agx_bg_eot_key *key) + struct agx_bg_eot_key *key, + enum agx_bg_eot_error *error) { const bool g18g = cache->dev->chip == AGX_CHIP_G18G; if (agx_chip_hal300_path(cache->dev->chip) && !key->tib.sample_size_B && @@ -593,8 +616,8 @@ agx_build_background_shader(struct agx_bg_eot_cache *cache, }, }; struct agx_bg_eot_shader *res = - rzalloc(cache->ht, struct agx_bg_eot_shader); - if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res)) { + agx_alloc_bg_eot_shader(cache, error); + if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res, error)) { ralloc_free(res); return NULL; } @@ -606,12 +629,13 @@ agx_build_background_shader(struct agx_bg_eot_cache *cache, struct agx_shader_part bin = {0}; if (!agx_compile_background_shader(cache->dev, key, &bin)) return NULL; - return agx_upload_bg_eot_part(cache, &bin, false); + return agx_upload_bg_eot_part(cache, &bin, false, error); } static struct agx_bg_eot_shader * agx_build_end_of_tile_shader(struct agx_bg_eot_cache *cache, - struct agx_bg_eot_key *key) + struct agx_bg_eot_key *key, + enum agx_bg_eot_error *error) { const bool g18g = cache->dev->chip == AGX_CHIP_G18G; const unsigned g18_mrt = cache->dev->chip == AGX_CHIP_G18G @@ -733,8 +757,8 @@ agx_build_end_of_tile_shader(struct agx_bg_eot_cache *cache, .agx3_staging_halfs = agx3_m5_mrt_eot_staging_halfs(m5_stores), }, }; - struct agx_bg_eot_shader *res = rzalloc(cache->ht, struct agx_bg_eot_shader); - if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res)) { + struct agx_bg_eot_shader *res = agx_alloc_bg_eot_shader(cache, error); + if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res, error)) { ralloc_free(res); return NULL; } @@ -767,8 +791,8 @@ agx_build_end_of_tile_shader(struct agx_bg_eot_cache *cache, .agx3_staging_halfs = native.staging_halfs, }, }; - struct agx_bg_eot_shader *res = rzalloc(cache->ht, struct agx_bg_eot_shader); - if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res)) { + struct agx_bg_eot_shader *res = agx_alloc_bg_eot_shader(cache, error); + if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res, error)) { ralloc_free(res); return NULL; } @@ -968,12 +992,13 @@ agx_build_end_of_tile_shader(struct agx_bg_eot_cache *cache, agx_chip_native_agx3(cache->dev->chip) && agx_g17p_classify_mrt(&key->tib) == AGX_G17P_MRT_R11G11B10; - return agx_compile_bg_eot_shader(cache, b.shader, &compiler_key, NULL); + return agx_compile_bg_eot_shader(cache, b.shader, &compiler_key, NULL, error); } static struct agx_bg_eot_shader * agx_get_bg_eot_shader_locked(struct agx_bg_eot_cache *cache, - struct agx_bg_eot_key *key) + struct agx_bg_eot_key *key, + enum agx_bg_eot_error *error) { struct hash_entry *ent = _mesa_hash_table_search(cache->ht, key); if (ent) @@ -985,13 +1010,13 @@ agx_get_bg_eot_shader_locked(struct agx_bg_eot_cache *cache, for (unsigned rt = 0; rt < ARRAY_SIZE(key->op); ++rt) { if (key->op[rt] == AGX_EOT_STORE) { has_store = true; - ret = agx_build_end_of_tile_shader(cache, key); + ret = agx_build_end_of_tile_shader(cache, key, error); break; } } if (!ret && !has_store) - ret = agx_build_background_shader(cache, key); + ret = agx_build_background_shader(cache, key, error); if (!ret) return NULL; @@ -1009,9 +1034,21 @@ agx_get_bg_eot_shader_locked(struct agx_bg_eot_cache *cache, struct agx_bg_eot_shader * agx_get_bg_eot_shader(struct agx_bg_eot_cache *cache, struct agx_bg_eot_key *key) +{ + return agx_get_bg_eot_shader_checked(cache, key, NULL); +} + +struct agx_bg_eot_shader * +agx_get_bg_eot_shader_checked(struct agx_bg_eot_cache *cache, + struct agx_bg_eot_key *key, + enum agx_bg_eot_error *error) { simple_mtx_lock(&cache->lock); - struct agx_bg_eot_shader *ret = agx_get_bg_eot_shader_locked(cache, key); + enum agx_bg_eot_error lookup_error = AGX_BG_EOT_NO_ERROR; + struct agx_bg_eot_shader *ret = + agx_get_bg_eot_shader_locked(cache, key, &lookup_error); + if (error) + *error = lookup_error; simple_mtx_unlock(&cache->lock); return ret; } diff --git a/src/asahi/lib/agx_bg_eot.h b/src/asahi/lib/agx_bg_eot.h index e28d0e913a7c..009e3297e144 100644 --- a/src/asahi/lib/agx_bg_eot.h +++ b/src/asahi/lib/agx_bg_eot.h @@ -26,6 +26,12 @@ struct agx_precompiled_shader { struct agx_precompiled_kernel_info info; }; +enum agx_bg_eot_error { + AGX_BG_EOT_NO_ERROR, + AGX_BG_EOT_HOST_OOM, + AGX_BG_EOT_DEVICE_OOM, +}; + struct agx_bg_eot_cache { struct agx_device *dev; struct agx_pool pool; @@ -202,6 +208,11 @@ agx_g17g_rt0_eot_physical_format_supported(enum pipe_format physical) struct agx_shader_part; +struct agx_bg_eot_shader * +agx_get_bg_eot_shader_checked(struct agx_bg_eot_cache *cache, + struct agx_bg_eot_key *key, + enum agx_bg_eot_error *error); + struct agx_bg_eot_shader *agx_get_bg_eot_shader(struct agx_bg_eot_cache *cache, struct agx_bg_eot_key *key); diff --git a/src/asahi/lib/agx_scratch.c b/src/asahi/lib/agx_scratch.c index 741796b1d294..4f19527b651e 100644 --- a/src/asahi/lib/agx_scratch.c +++ b/src/asahi/lib/agx_scratch.c @@ -113,25 +113,27 @@ agx_scratch_realloc(struct agx_scratch *scratch) if (!buf) return false; - agx_bo_unreference(scratch->dev, scratch->buf); - scratch->buf = buf; - void *map = agx_bo_map(scratch->buf); + void *map = agx_bo_map(buf); + if (!map) { + agx_bo_unreference(scratch->dev, buf); + return false; + } + memset(map, 0, blocks_off); struct agx_helper_header *hdr = map; - scratch->header = hdr; - uint64_t blocklist_gpu = scratch->buf->va->addr + blocklist_off; + uint64_t blocklist_gpu = buf->va->addr + blocklist_off; struct agx_helper_block *blocklist_cpu = (struct agx_helper_block *)((uint8_t *)map + blocklist_off); #ifdef SCRATCH_DEBUG scratch->blocklist = blocklist_cpu; - scratch->data = scratch->buf->map + blocks_off; + scratch->data = (uint8_t *)map + blocks_off; scratch->core_size = block_size_bytes * block_count * scratch->subgroups; #endif - uint64_t blocks_gpu = scratch->buf->va->addr + blocks_off; + uint64_t blocks_gpu = buf->va->addr + blocks_off; hdr->subgroups = scratch->subgroups; @@ -176,6 +178,9 @@ agx_scratch_realloc(struct agx_scratch *scratch) blocklist_gpu += sizeof(struct agx_helper_block) * scratch->subgroups; blocklist_cpu += scratch->subgroups; } + agx_bo_unreference(scratch->dev, scratch->buf); + scratch->buf = buf; + scratch->header = hdr; scratch->max_core_id = core_id; assert(num_cores == scratch->num_cores); (void)num_cores; diff --git a/src/asahi/lib/pool.c b/src/asahi/lib/pool.c index 3c63a28d036e..37fd657277ad 100644 --- a/src/asahi/lib/pool.c +++ b/src/asahi/lib/pool.c @@ -26,6 +26,11 @@ agx_pool_alloc_backing(struct agx_pool *pool, size_t bo_sz) if (unlikely(bo == NULL)) return NULL; + if (!agx_bo_map(bo)) { + agx_bo_unreference(pool->dev, bo); + return NULL; + } + util_dynarray_append(&pool->bos, bo); pool->transient_bo = bo; pool->transient_offset = 0; @@ -83,8 +88,8 @@ agx_pool_fail(struct agx_pool *pool, size_t sz) return mem; } -struct agx_ptr -agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz, +static struct agx_ptr +agx_pool_try_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz, unsigned alignment, struct agx_bo **out_bo) { assert(alignment == util_next_power_of_two(alignment)); @@ -102,7 +107,8 @@ agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz, if (out_bo) *out_bo = NULL; - return (struct agx_ptr){.cpu = agx_pool_fail(pool, sz), .gpu = 0}; + pool->failed = true; + return (struct agx_ptr){0}; } bo = fresh; @@ -122,6 +128,17 @@ agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz, return ret; } +struct agx_ptr +agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz, + unsigned alignment, struct agx_bo **out_bo) +{ + struct agx_ptr ptr = + agx_pool_try_alloc_aligned_with_bo(pool, sz, alignment, out_bo); + if (!ptr.cpu) + ptr.cpu = agx_pool_fail(pool, sz); + return ptr; +} + uint64_t agx_pool_upload(struct agx_pool *pool, const void *data, size_t sz) { @@ -136,7 +153,10 @@ agx_pool_upload_aligned_with_bo(struct agx_pool *pool, const void *data, struct agx_bo *local_bo = NULL; struct agx_bo **out_bo = bo != NULL ? bo : &local_bo; struct agx_ptr transfer = - agx_pool_alloc_aligned_with_bo(pool, sz, alignment, out_bo); + agx_pool_try_alloc_aligned_with_bo(pool, sz, alignment, out_bo); + if (!transfer.cpu) + return 0; + memcpy(transfer.cpu, data, sz); if (*out_bo) agx_bo_assert_range(*out_bo, transfer.gpu - (*out_bo)->va->addr, sz); diff --git a/src/asahi/lib/tests/test-agx-bo-cache.c b/src/asahi/lib/tests/test-agx-bo-cache.c index d4b8b9c5140d..d2ad77b74c1e 100644 --- a/src/asahi/lib/tests/test-agx-bo-cache.c +++ b/src/asahi/lib/tests/test-agx-bo-cache.c @@ -17,6 +17,7 @@ #include "agx_bo.h" #include "agx_device.h" #include "pool.h" +#include "agx_scratch.h" #define MIB (1024ull * 1024ull) @@ -39,6 +40,8 @@ struct mock_state { unsigned munmap_calls; bool alloc_va; bool alloc_fail; + bool map_fail; + bool real_maps; }; static struct mock_state mock; @@ -88,12 +91,19 @@ mock_bo_mmap(struct agx_device *dev, struct agx_bo *bo, void *fixed_addr, { assert(bo->_map == NULL); mock.mmap_calls++; + if (mock.map_fail) + return; if (!bo->has_mmap_offset) { mock.mmap_offset_queries++; bo->mmap_offset = UINT64_C(0x100000000) + (uint64_t)bo->handle * 0x10000ull; bo->has_mmap_offset = true; } bo->_map_size = length ?: bo->size; + if (mock.real_maps) { + bo->_map = calloc(1, bo->_map_size); + assert(bo->_map != NULL); + return; + } bo->_map = fixed_addr ? fixed_addr : (void *)(uintptr_t)(0x20000000ull + bo->handle * 0x10000ull); } @@ -101,6 +111,8 @@ static void mock_bo_munmap(struct agx_device *dev, struct agx_bo *bo) { mock.munmap_calls++; + if (mock.real_maps) + free(bo->_map); bo->_map_size = 0; } @@ -767,7 +779,13 @@ test_pool_alloc_failure(void) static uint8_t data[1024 * 1024]; assert(agx_pool_upload_aligned(&pool, data, sizeof(data), 64) == 0); + assert(util_dynarray_num_elements(&pool.host, void *) == 1); mock.alloc_fail = false; + mock.map_fail = true; + assert(agx_pool_upload_aligned(&pool, data, sizeof(data), 64) == 0); + assert(util_dynarray_num_elements(&pool.host, void *) == 1); + assert(pool.transient_bo->va->addr == ok.gpu); + mock.map_fail = false; pool.failed = false; struct agx_ptr again = agx_pool_alloc_aligned_with_bo(&pool, big, 64, &bo); assert(bo && again.gpu && !pool.failed); @@ -776,6 +794,33 @@ test_pool_alloc_failure(void) finish_mock_device(&dev); } +static void +test_scratch_map_failure(void) +{ + struct agx_device dev; + init_mock_device(&dev); + mock.alloc_va = true; + mock.real_maps = true; + dev.params.num_cores_per_cluster = dev.params.num_clusters_total = 1; + dev.params.core_masks[0] = 1; + + struct agx_scratch scratch; + agx_scratch_init(&dev, &scratch); + assert(agx_scratch_alloc(&scratch, 8, 1)); + struct agx_bo *old = scratch.buf; + void *old_header = scratch.header; + unsigned old_size = scratch.size_dwords; + mock.map_fail = true; + assert(!agx_scratch_alloc(&scratch, old_size * 2, 2)); + assert(scratch.buf == old && scratch.header == old_header); + assert(scratch.size_dwords == old_size && scratch.subgroups == 1); + mock.map_fail = false; + assert(agx_scratch_alloc(&scratch, old_size * 2, 2)); + assert(scratch.buf != old); + agx_scratch_fini(&scratch); + finish_mock_device(&dev); +} + int main(void) { @@ -790,6 +835,7 @@ main(void) test_large_buckets_flag_isolation_and_cross_pot_reuse(); test_mmap_offset_cache_propagation(); test_pool_alloc_failure(); + test_scratch_map_failure(); printf("agx bo cache: ok\n"); return 0; diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index 7b71977d2b93..fa049f37eebb 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -411,6 +411,19 @@ is_attachment_stored(const VkRenderingAttachmentInfo *att) att->resolveMode != VK_RESOLVE_MODE_NONE; } +static struct agx_bg_eot_shader * +hk_get_bg_eot_shader(struct hk_cmd_buffer *cmd, struct agx_bg_eot_key *key) +{ + enum agx_bg_eot_error error; + struct agx_bg_eot_shader *shader = agx_get_bg_eot_shader_checked( + &hk_cmd_buffer_device(cmd)->bg_eot, key, &error); + if (error == AGX_BG_EOT_HOST_OOM) + vk_command_buffer_set_error(&cmd->vk, VK_ERROR_OUT_OF_HOST_MEMORY); + else if (error == AGX_BG_EOT_DEVICE_OOM) + vk_command_buffer_set_error(&cmd->vk, VK_ERROR_OUT_OF_DEVICE_MEMORY); + return shader; +} + /* LDSHDR's fetch span saturates from 321 bytes of text on, so a longer * program, such as a multi-target mixed-format reload, loads exactly like the * main fragment shaders of any size. */ @@ -552,7 +565,7 @@ hk_build_g17p_mixed_bg_eot(struct hk_cmd_buffer *cmd, if (clears) key.reserved_preamble = 4 + 8 * count; - struct agx_bg_eot_shader *shader = agx_get_bg_eot_shader(&dev->bg_eot, &key); + struct agx_bg_eot_shader *shader = hk_get_bg_eot_shader(cmd, &key); if (!hk_g17p_bg_eot_shader_supported(shader) || (!store && (shader->info.push_count != key.reserved_preamble || shader->info.uses_txf != (reloads > 0)))) { @@ -1371,12 +1384,12 @@ hk_build_g17p_bg_eot(struct hk_cmd_buffer *cmd, attachment_count == 1 && !dual_mrt_active && !multi_mrt_active; struct agx_bg_eot_shader *shader = - agx_get_bg_eot_shader(&dev->bg_eot, key); + hk_get_bg_eot_shader(cmd, key); if (key->store_and_resolve && !hk_g17p_bg_eot_shader_supported(shader)) { /* Without the two-output program the pass stores its samples and * resolves them after the pass, as it did before the fused path. */ key->store_and_resolve = false; - shader = agx_get_bg_eot_shader(&dev->bg_eot, key); + shader = hk_get_bg_eot_shader(cmd, key); } const bool shader_resources_match = shader && @@ -2433,7 +2446,7 @@ hk_build_m5_mrt_bg_eot(struct hk_cmd_buffer *cmd, struct agx_bg_eot_key *key, const unsigned clear_base = loads ? 8 : 4; if (clears) key->reserved_preamble = clear_base + 8 * count; - shader = agx_get_bg_eot_shader(&dev->bg_eot, key); + shader = hk_get_bg_eot_shader(cmd, key); if (!shader || !shader->info.nr_gprs || !shader->info.agx3_staging_known || shader->info.scratch_size || shader->info.has_preamble || shader->info.push_count != key->reserved_preamble || @@ -2588,7 +2601,7 @@ hk_build_m5_empty_bg_eot(struct hk_cmd_buffer *cmd, shader_key.tib.tile_size = 32 * 32; shader_key.tib.layered = false; struct agx_bg_eot_shader *shader = - agx_get_bg_eot_shader(&dev->bg_eot, &shader_key); + hk_get_bg_eot_shader(cmd, &shader_key); if (!shader || shader->info.binary_size != 4 || shader->info.nr_gprs != 2 || !shader->info.agx3_staging_known || shader->info.agx3_staging_halfs || shader->info.scratch_size || @@ -2738,7 +2751,7 @@ hk_build_m5_bg_eot(struct hk_cmd_buffer *cmd, struct agx_bg_eot_key *key, (agx_tilebuffer_offset_B(&key->tib, 0) & 3) == 0 ? 1u : 0u; if (op == AGX_BG_CLEAR) key->reserved_preamble = AGX3_G17P_RT0_CLEAR_PUSH_HALFS; - shader = agx_get_bg_eot_shader(&dev->bg_eot, key); + shader = hk_get_bg_eot_shader(cmd, key); if (!shader || !shader->info.nr_gprs || !shader->info.agx3_staging_known || shader->info.scratch_size || shader->info.has_preamble) goto unsupported; @@ -3181,7 +3194,7 @@ hk_build_bg_eot_program(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, /* Get the shader */ key.reserved_preamble = uniforms; /* XXX: locking? */ - struct agx_bg_eot_shader *shader = agx_get_bg_eot_shader(&dev->bg_eot, &key); + struct agx_bg_eot_shader *shader = hk_get_bg_eot_shader(cmd, &key); if (!shader) { vk_command_buffer_set_error(&cmd->vk, VK_ERROR_FEATURE_NOT_PRESENT); return (struct hk_bg_eot){0}; @@ -7341,7 +7354,7 @@ hk_build_agx3_store_and_resolve_eot( key.store_and_resolve = true; struct agx_bg_eot_shader *shader = - agx_get_bg_eot_shader(&dev->bg_eot, &key); + hk_get_bg_eot_shader(cmd, &key); if (!hk_g17p_bg_eot_shader_supported(shader)) return (struct hk_bg_eot){0}; @@ -7404,7 +7417,7 @@ hk_build_agx3_store_and_resolve_eot( key.m5_store_and_resolve = true; struct agx_bg_eot_shader *shader = - agx_get_bg_eot_shader(&dev->bg_eot, &key); + hk_get_bg_eot_shader(cmd, &key); if (!shader || !shader->info.nr_gprs || !shader->info.agx3_staging_known || shader->info.scratch_size || shader->info.has_preamble) diff --git a/src/gallium/drivers/asahi/agx_disk_cache.c b/src/gallium/drivers/asahi/agx_disk_cache.c index 5c20687fcd96..fa0f748b622c 100644 --- a/src/gallium/drivers/asahi/agx_disk_cache.c +++ b/src/gallium/drivers/asahi/agx_disk_cache.c @@ -128,8 +128,8 @@ read_shader(struct agx_screen *screen, struct blob_reader *blob, blob_copy_bytes(blob, binary->b.binary, size); } - if (size && agx_upload_compiled_shader(&screen->dev, binary) && - !keep_binary) { + binary->upload_failed = size && !agx_upload_compiled_shader(&screen->dev, binary); + if (size && !binary->upload_failed && !keep_binary) { free(binary->b.binary); binary->b.binary = NULL; } diff --git a/src/gallium/drivers/asahi/agx_state.c b/src/gallium/drivers/asahi/agx_state.c index 794f05355e5d..87ce9275fe89 100644 --- a/src/gallium/drivers/asahi/agx_state.c +++ b/src/gallium/drivers/asahi/agx_state.c @@ -3239,7 +3239,7 @@ agx_compile_nir_tile(struct agx_device *dev, nir_shader *nir, &compiled->b.info.stats); if (compiled->b.info.binary_size && !secondary) - agx_upload_compiled_shader(dev, compiled); + compiled->upload_failed = !agx_upload_compiled_shader(dev, compiled); agx_compute_shader_upload_sizes(compiled); return compiled; @@ -3262,12 +3262,16 @@ agx_upload_compiled_shader(struct agx_device *dev, { struct agx_bo *bo = agx_bo_create(dev, cs->b.info.binary_size, 0, AGX_BO_EXEC | AGX_BO_LOW_VA, "Executable"); - if (!bo) { - cs->upload_failed = true; + if (!bo) + return false; + + void *map = agx_bo_map(bo); + if (!map) { + agx_bo_unreference(dev, bo); return false; } - memcpy(agx_bo_map(bo), cs->b.binary, cs->b.info.binary_size); + memcpy(map, cs->b.binary, cs->b.info.binary_size); if (p_atomic_cmpxchg(&cs->bo, NULL, bo) != NULL) agx_bo_unreference(dev, bo); @@ -5331,7 +5335,7 @@ agx_drop_for_unusable_program(struct agx_context *ctx, "refused.", stage, cs == NULL ? "no compiled variant" - : cs->upload_failed && !cs->bo ? "no memory for its executable" + : cs->upload_failed && !p_atomic_read(&cs->bo) ? "no memory for its executable" : cs->failed ? "the backend refused to encode it" : "it could not be linked"); } diff --git a/src/gallium/drivers/asahi/agx_state.h b/src/gallium/drivers/asahi/agx_state.h index 63c62293e139..d68261772ca9 100644 --- a/src/gallium/drivers/asahi/agx_state.h +++ b/src/gallium/drivers/asahi/agx_state.h @@ -302,7 +302,8 @@ struct agx_compiled_shader { */ bool failed; - /* The executable BO could not be allocated. Until bo is set the variant + /* Set before publishing a variant whose first executable upload failed. + * Retries publish only bo. Until bo is set the variant * is unusable (agx_program_usable); the binary is kept and * agx_upload_compiled_shader() retried when the program is used. */