From df5f92d11453948e3a08761c0554a5d554a60163 Mon Sep 17 00:00:00 2001 From: Lionel Landwerlin Date: Sat, 1 Nov 2025 23:39:20 +0200 Subject: [PATCH] u_trace: reserve chunk space before emitting copies Some implementations can emit tracepoints when copying u_trace buffers. It's important to reserve the slots we want to copy into before emitting the copies so that both processes don't clash with one another. Signed-off-by: Lionel Landwerlin Cc: mesa-stable Reviewed-by: Danylo Piliaiev Part-of: --- src/util/perf/u_trace.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/util/perf/u_trace.c b/src/util/perf/u_trace.c index f2cad023328..7ee737748c8 100644 --- a/src/util/perf/u_trace.c +++ b/src/util/perf/u_trace.c @@ -887,11 +887,17 @@ u_trace_clone_append(struct u_trace_iterator begin_it, if (from_chunk == end_it.chunk) to_copy = MIN2(to_copy, end_it.event_idx - from_idx); + /* Reserve space in the chunk before emitting the copy as it could also + * add its own tracepoints. + */ + unsigned to_chunk_idx = to_chunk->num_traces; + to_chunk->num_traces += to_copy; + copy_buffer(begin_it.ut->utctx, cmdstream, from_chunk->timestamps, begin_it.ut->utctx->timestamp_size_bytes * from_idx, to_chunk->timestamps, - begin_it.ut->utctx->timestamp_size_bytes * to_chunk->num_traces, + begin_it.ut->utctx->timestamp_size_bytes * to_chunk_idx, begin_it.ut->utctx->timestamp_size_bytes * to_copy); if (from_chunk->has_indirect) { @@ -899,11 +905,11 @@ u_trace_clone_append(struct u_trace_iterator begin_it, from_chunk->indirects, begin_it.ut->utctx->max_indirect_size_bytes * from_idx, to_chunk->indirects, - begin_it.ut->utctx->max_indirect_size_bytes * to_chunk->num_traces, + begin_it.ut->utctx->max_indirect_size_bytes * to_chunk_idx, begin_it.ut->utctx->max_indirect_size_bytes * to_copy); } - memcpy(&to_chunk->traces[to_chunk->num_traces], + memcpy(&to_chunk->traces[to_chunk_idx], &from_chunk->traces[from_idx], to_copy * sizeof(struct u_trace_event)); @@ -919,7 +925,6 @@ u_trace_clone_append(struct u_trace_iterator begin_it, } into->num_traces += to_copy; - to_chunk->num_traces += to_copy; from_idx += to_copy; assert(from_idx <= from_chunk->num_traces);