From 2afd233145460a8de7672f0e2bb7416a16d0f176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ol=C5=A1=C3=A1k?= Date: Mon, 8 Jul 2024 08:39:04 -0400 Subject: [PATCH] radeonsi: lock a mutex when updating scratch_va for compute shaders Fixes: 3b0bfd254f7 - radeonsi/gfx11: make flat_scratch changes for compute Reviewed-by: Qiang Yu Part-of: --- src/gallium/drivers/radeonsi/si_compute.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/gallium/drivers/radeonsi/si_compute.c b/src/gallium/drivers/radeonsi/si_compute.c index e6efa1bf691..b1a569d780e 100644 --- a/src/gallium/drivers/radeonsi/si_compute.c +++ b/src/gallium/drivers/radeonsi/si_compute.c @@ -218,6 +218,7 @@ static void *si_create_compute_state(struct pipe_context *ctx, const struct pipe pipe_reference_init(&sel->base.reference, 1); sel->stage = MESA_SHADER_COMPUTE; sel->screen = sscreen; + simple_mtx_init(&sel->mutex, mtx_plain); sel->const_and_shader_buf_descriptors_index = si_const_and_shader_buffer_descriptors_idx(PIPE_SHADER_COMPUTE); sel->sampler_and_images_descriptors_index = @@ -480,6 +481,16 @@ static bool si_switch_compute_shader(struct si_context *sctx, struct si_compute } if (config->scratch_bytes_per_wave) { + /* Prevent race conditions for accesses to shader->scratch_va and shader->bo, which + * can change when scratch_va is updated. Any accesses to shader->bo must also be inside + * the lock. + * + * TODO: This lock could be removed if the scratch address was passed via user SGPRs instead + * of the shader binary. + */ + if (!sctx->screen->info.has_scratch_base_registers) + simple_mtx_lock(&shader->selector->mutex); + /* Update max_seen_compute_scratch_bytes_per_wave and compute_tmpring_size. */ ac_get_scratch_tmpring_size(&sctx->screen->info, config->scratch_bytes_per_wave, @@ -503,6 +514,12 @@ static bool si_switch_compute_shader(struct si_context *sctx, struct si_compute radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, shader->bo, RADEON_USAGE_READ | RADEON_PRIO_SHADER_BINARY); + /* shader->bo can't be used after this if the scratch address is inserted into the shader + * binary. + */ + if (config->scratch_bytes_per_wave && !sctx->screen->info.has_scratch_base_registers) + simple_mtx_unlock(&shader->selector->mutex); + if (sctx->gfx_level >= GFX12) { unsigned rsrc3 = S_00B8A0_INST_PREF_SIZE_GFX12(si_get_shader_prefetch_size(shader)); @@ -1307,6 +1324,7 @@ void si_destroy_compute(struct si_compute *program) si_shader_destroy(&program->shader); ralloc_free(program->sel.nir); + simple_mtx_destroy(&sel->mutex); FREE(program); }