From 3006c2a7b616bba76d46e6f5efd5335803727c56 Mon Sep 17 00:00:00 2001 From: Boris Brezillon Date: Thu, 28 Nov 2024 16:26:23 +0100 Subject: [PATCH] pan/cs: Fix cs_builder allocation failure robustness There's more than one error-path out of cs_alloc_ins_block(), but only one of them got the discar_instr_slot treatment. Instead of plugging this in one more time, let's move this handling up to cs_alloc_ins(), where we can easily whack two birds with one stone. This makes us consistently return NULL on error here. At the same time, we need to patch up cs_flush_block_instrs() here, because we don't actually set the buffer invalid here. So let's check for NULL here instead, which is the new contract. Fixes: 0e6aaab00ae ("pan/cs: add block to handle registers backup in exception handler") Signed-off-by: Boris Brezillon Tested-by: Erik Faye-Lund Reviewed-by: Eric R. Smith Part-of: --- src/panfrost/lib/genxml/cs_builder.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/panfrost/lib/genxml/cs_builder.h b/src/panfrost/lib/genxml/cs_builder.h index f4e32e27fc8..cb7448cd9b0 100644 --- a/src/panfrost/lib/genxml/cs_builder.h +++ b/src/panfrost/lib/genxml/cs_builder.h @@ -535,7 +535,7 @@ cs_alloc_ins_block(struct cs_builder *b, uint32_t num_instrs) return util_dynarray_grow(&b->blocks.instrs, uint64_t, num_instrs); if (!cs_reserve_instrs(b, num_instrs)) - return &b->discard_instr_slot; + return NULL; assert(b->cur_chunk.size + num_instrs - 1 < b->cur_chunk.buffer.capacity); uint32_t pos = b->cur_chunk.pos; @@ -564,7 +564,7 @@ cs_flush_block_instrs(struct cs_builder *b) void *buffer = cs_alloc_ins_block(b, num_instrs); - if (likely(cs_is_valid(b))) { + if (likely(buffer != NULL)) { /* If we have a LOAD_IP chain, we need to patch each LOAD_IP * instruction before we copy the block to the final memory * region. */ @@ -654,7 +654,7 @@ cs_alloc_ins(struct cs_builder *b) * causing further cs_else_start() instructions to be invalid. */ cs_flush_pending_if(b); - return cs_alloc_ins_block(b, 1); + return cs_alloc_ins_block(b, 1) ?: &b->discard_instr_slot; } /* Call this when you are done building a command stream and want to prepare