From 9577326c1603296861fe23721e2e421008cb9417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Thu, 3 Dec 2026 00:20:55 +0100 Subject: [PATCH 1/2] pvr/g5p: count control stream blocks at their real size before continuing a render MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit g5p_cmd_draw() continues a render in a new one (a mid-render barrier: the first stores, the second loads) before its VDM control stream passes PVR_G5P_MAX_VDM_BYTES, which pvr_arch_g5p_vdm_stream() lays out as one range. It counted PVR_CMD_BUFFER_CSB_BO_SIZE (4 KiB) per pvr_csb block, but pvr_csb_buffer_extend() fills a block up to its BO's size, which the kernel rounds up to its page: 16 KiB on the iPad. The continuation came only after 255 blocks (about 4 MiB), so long renders were refused at 1 MiB instead ("a control stream of 1373708 bytes in 85 blocks (at most 1048576)", Speedometer 3.1 in Firefox) and skipped. Sum the blocks' BO sizes and keep the largest block in reserve. The 1 MiB limit is the driver's, not the hardware's: the A9 VDM follows STREAM_LINK (Apple's streams use it), but only flattened streams have run on the device so far. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- src/imagination/vulkan/pvr_arch_g5p_gfx.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_g5p_gfx.c b/src/imagination/vulkan/pvr_arch_g5p_gfx.c index 6fe858b5cc7..a68f5f0ae73 100644 --- a/src/imagination/vulkan/pvr_arch_g5p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g5p_gfx.c @@ -2480,9 +2480,15 @@ PVR_PER_ARCH(g5p_cmd_draw)(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g * relocation slack. This also provides old-kernel compatibility without * changing the application's render pass. */ struct pvr_csb *stream = &cs->current_sub_cmd->gfx.control_stream; - const uint64_t occupied = (uint64_t)list_length(&stream->pvr_bo_list) * - PVR_CMD_BUFFER_CSB_BO_SIZE; - if (occupied + PVR_CMD_BUFFER_CSB_BO_SIZE > PVR_G5P_MAX_VDM_BYTES) { + /* A block holds its whole allocation (pvr_csb_buffer_extend(): csb->end is the BO's size), which the kernel + * rounds up to its page: 16 KiB blocks on the iPad, not PVR_CMD_BUFFER_CSB_BO_SIZE. Counting 4 KiB per block + * let a render reach 85 blocks (1.37 MB) and be refused by pvr_arch_g5p_vdm_stream(). */ + uint64_t occupied = 0, block = PVR_CMD_BUFFER_CSB_BO_SIZE; + list_for_each_entry (const struct pvr_bo, b, &stream->pvr_bo_list, link) { + occupied += b->bo->size; + block = MAX2(block, b->bo->size); + } + if (occupied + block > PVR_G5P_MAX_VDM_BYTES) { result = pvr_cmd_buffer_insert_mid_frag_barrier_event(cmd_buffer, PVR_PIPELINE_STAGE_FRAG_BIT, PVR_PIPELINE_STAGE_GEOM_BIT); From 09bbd38a9a5d02a6aedc5a3712478428b1c10332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Thu, 3 Dec 2026 00:20:55 +0100 Subject: [PATCH 2/2] util/tc: let the previous batch finish before moving rolled-over rp infos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a renderpass continues across a batch flush, the flushed batch's last tc_batch_rp_info links to infos[0] of the new batch (full_copy in tc_batch_increment_renderpass_info()). While the flushed batch executes, the driver thread follows that link in threaded_context_get_renderpass_info(), and after executing it writes through it (tc_batch_execute() clears next->prev). If the new batch then records ten more renderpasses, tc_batch_renderpass_infos_resize() reallocates its array: the old infos[0] is freed while the driver thread may still read its ready fence and next pointer, or write its prev pointer. Firefox (WebRender through Zink) crashed in threaded_context_get_renderpass_info() from zink_batch_rp() with a wild pointer made of float data (SIGSEGV at 0x44c2600044800028) during a render of several thousand draws. Before moving an array whose infos[0] was linked from the previous batch, wait for that batch's fence. infos[0] has ended and been signalled by then, so the driver thread is never waiting on the recording thread. This happens only for a batch that started inside a renderpass and holds more than ten renderpasses. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- src/gallium/auxiliary/util/u_threaded_context.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/gallium/auxiliary/util/u_threaded_context.c b/src/gallium/auxiliary/util/u_threaded_context.c index b25d6aa341a..95814c82dc2 100644 --- a/src/gallium/auxiliary/util/u_threaded_context.c +++ b/src/gallium/auxiliary/util/u_threaded_context.c @@ -218,6 +218,21 @@ tc_batch_renderpass_infos_resize(struct threaded_context *tc, struct tc_batch *b unsigned old_idx = batch->renderpass_info_idx - 1; bool redo = tc->renderpass_info_recording && tc->renderpass_info_recording == &infos[old_idx].info; + /* A renderpass that rolled over from the previous batch links that batch's + * last info to infos[0] (tc_batch_increment_renderpass_info()). While the + * previous batch executes, the driver thread follows that link + * (threaded_context_get_renderpass_info()) and, when it is done, writes + * through it (tc_batch_execute() clears infos[0].prev). Moving the array + * under it reads and writes freed memory, so let the previous batch finish + * first. infos[0] has ended (this is not index 0) and is signalled, so the + * driver thread cannot be waiting on this thread. + */ + if (infos && batch->renderpass_info_idx > 0 && infos[0].prev && + util_queue_fence_is_signalled(&infos[0].ready)) { + const unsigned idx = batch - tc->batch_slots; + struct tc_batch *prev = &tc->batch_slots[(idx + TC_MAX_BATCHES - 1) % TC_MAX_BATCHES]; + util_queue_fence_wait(&prev->fence); + } if (!util_dynarray_resize(&batch->renderpass_infos, struct tc_batch_rp_info, cur_num + 10)) mesa_loge("tc: memory alloc fail!");