From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796044812; b=mTcJLZKjNthBp3IdgEdQfwujNjU+uC0UeYi9sjbrywpKdHvAuh6Sw2tvJsfQX0Qtu5g0 higpduoValpUX+OqAaLbKPqleI3DoX/3UHQ8plL0LPsIffJh+5ssBSfPOkVA5YHXrY/in 5/gsheVSfCY7FQtG75X1mmPTmFRjkp5OjNz/QKtJdoG9x0Qux3fZk7Od9oEoqYVKBg4XS ooo2f1WbtOPsiZjwW2Magw8BrP8WZgdYcQQ52grOMz0qD+KYHRC0BcPXruo4YACqQCD5t e4SWtqYgC1A8PH5mCD6kIAIuPAtSxEsliy6vKcY1aCEbqEniwRkucWLv9B3VMxxXMNg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796044812; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=ODrbeFO1sImGmuHNc24jjYjhz99Bqr+q7y3tv4P1HyQ=; b=d3Wsh+npwtmXPK42+xRoayoGrDqCrqdFGgATpzreTkABGlRuTRMiG5UlBsICL6997ZfN MJextaAgGg/q43h9HfCoPlZHJI4BJTXbN8zuEouOB7iCcYvlSZMcuyewHZJnqJyIwrbfy ZwaFxtwT5+rijTzzYpQPh3pcDXcj3eXmddJrVwScTOwZUnGDTRk8niYo780Dug8QUnUxn vzT+qxHOBN7TfEX7Cq39sHHKejzX7Wy11X6Hqsi0wSThSK/Pj6yYfLBEqY+epglrWUILD aT6u2hrCCt17LQ6Z219aZRi8dHqzVua85fOyGcwkQawMhHl8ZygA06+D3rKk/mnj0ZA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=WA+7b1EU; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91DFE606A31C for ; Mon, 30 Nov 2026 13:20:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=WA+7b1EU Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B3491F0089B; Mon, 30 Nov 2026 13:20:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796044812; bh=ODrbeFO1sImGmuHNc24jjYjhz99Bqr+q7y3tv4P1HyQ=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=WA+7b1EUiqOrebGQyUqE0ac+1sL5FLsfS9hlBic62HbEfdKB2pcybhmKTN9Vga2p3 /HwjWXMr4Qr/A7ViPA3VSXSRGYOIwp6H4dbAIhvAVKLymOG6W7Ux7Kmuo5v7qQiwhH mA6T13q6aEe8DUKcKCsSHkuPnuTiWICNM+3/yivKKUsexVA+GDTgWGh8CuCvLzsEiN 6sVUBgPnl870yq6M7j/7pMQJM1f/J1PjmkxWXSyX07coPw5kPP6w9FOvZ3iy0HImTX NZBEv4U95rFKTAbfIBNjI1J4u22/ve4Hn97+BLkm+KhGi9GXQbw96Bu9cySa/X/eY/ Y/GS5pmBv7Yxw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v5 04/19] perf cs-etm: Use per-CPU queues for per-thread mode Reply-To: sashiko-reviews@lists.linux.dev To: "James Clark" Cc: linux-perf-users@vger.kernel.org In-Reply-To: <20261130-james-cs-unformatted-per-thread-fix-v5-4-f1bcc4d18c9b@linaro.org> References: <20261130-james-cs-unformatted-per-thread-fix-v5-0-f1bcc4d18c9b@linaro.org> <20261130-james-cs-unformatted-per-thread-fix-v5-4-f1bcc4d18c9b@linaro.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 13:20:11 +0000 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] perf cs-etm: cs_etm__get_data_block() overwrites frontend_thread before flush - [Medium] perf cs-etm: per-CPU queues break cs_etm__process_timeless_queues() TID logic - [Medium] perf cs-etm: missing NULL checks in cs_etm__get_data_block() -- commit c9e5913d3c9d785e48bfa191f1dbc992958445ec Author: James Clark perf cs-etm: Use per-CPU queues for per-thread mode This commit switches per-thread CoreSight ETM decoding from a single queue to per-CPU queues indexed by the sample CPU, updating the trace ID queue's threads when the buffer TID changes on a CPU. > diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c > index 6f981ea53ca22..1045960cb1264 100644 > --- a/tools/perf/util/cs-etm.c > +++ b/tools/perf/util/cs-etm.c [ ... ] > @@ -2225,6 +2219,29 @@ static int cs_etm__get_data_block(struct cs_etm_qu= eue *etmq) > */ > cs_etm__flush_all_stack(etmq); > =20 > + /* > + * Per-thread mode still uses a queue for each CPU, but that CPU can run > + * different threads. When the TID from the AUX record on a CPU changes, > + * re-initialize the thread using the AUX record/buffer fragment TID so > + * we can start decoding even if the context ID packet was cropped or > + * they're disabled. > + */ > + if (etm->per_thread_decoding) { > + struct cs_etm_traceid_queue *tidq =3D cs_etm__etmq_get_traceid_queue(e= tmq, > + CS_ETM_PER_THREAD_TRACEID); > + > + if (thread__tid(tidq->decode_thread) !=3D etmq->buffer->tid) { > + thread__zput(tidq->frontend_thread); > + thread__zput(tidq->decode_thread); > + tidq->frontend_thread =3D machine__findnew_thread( > + &etm->session->machines.host, -1, > + etmq->buffer->tid); > + tidq->decode_thread =3D machine__findnew_thread( > + &etm->session->machines.host, -1, > + etmq->buffer->tid); > + } > + } > + > return 1; > } [Severity: Medium] Does replacing tidq->frontend_thread here corrupt the flush of the previous buffer's trailing packet and branch stack when a CPU switches between two traced threads? At the end of an AUX buffer for thread A, its last range or exception packet remains in tidq->prev_packet awaiting the next packet or a discontinuity, and in per-thread mode (CS_ETM_PIDFMT_NONE) cs_etm__flush_all_stack() above is a no-op. When cs_etm__get_data_block() fetches the next buffer on the same CPU for thread B, it overwrites tidq->frontend_thread with thread B before cs_etm__process_traceid_queue() processes the CS_ETM_DISCONTINUITY packet via cs_etm__flush(): tools/perf/util/cs-etm.c:cs_etm__flush() { ... err =3D cs_etm__synth_last_instruction_sample(etmq, tidq); if (err) return err; err =3D cs_etm__synth_branch_sample(etmq, tidq); if (err) return err; swap_packet: cs_etm__packet_swap(etm, tidq); /* Reset last branches after flush the trace */ if (etm->use_thread_stack) thread_stack__flush(tidq->frontend_thread); ... } As a result, thread A's trailing instruction and TRACE_END branch samples are synthesized with thread B's PID/TID and decoded against thread B's address space, while thread_stack__flush() flushes thread B's stack and leaves thread A's thread_stack unflushed. [Severity: Medium] Can this cause a NULL pointer dereference if cs_etm__etmq_get_traceid_queue= () or machine__findnew_thread() fails? In timestamped per-thread mode, cs_etm__queue_first_cs_timestamp() calls cs_etm__get_data_block() before any trace ID queue exists for etmq, so cs_etm__etmq_get_traceid_queue() performs the initial allocation and can return NULL, which thread__tid(tidq->decode_thread) immediately dereferences. Likewise, the machine__findnew_thread() calls below are not checked for NULL (unlike in cs_etm__init_traceid_queue()), leaving frontend_thread or decode_thread NULL while returning success. > @@ -3307,7 +3324,9 @@ static int cs_etm__queue_aux_fragment(struct perf_s= ession *session, off_t file_o > =20 > if (aux_offset >=3D auxtrace_event->offset && > aux_offset + aux_size <=3D auxtrace_event->offset + auxtrace_event-= >size) { > - struct cs_etm_queue *etmq =3D cs_etm__get_queue(etm, auxtrace_event->c= pu); > + /* Old per-thread files have no CPU (before v6.4) */ > + int queue_idx =3D sample->cpu =3D=3D (u32)-1 ? 0 : sample->cpu; > + struct cs_etm_queue *etmq =3D cs_etm__get_queue(etm, queue_idx); > =20 > if (!etmq) > return -EINVAL; > @@ -3320,6 +3339,7 @@ static int cs_etm__queue_aux_fragment(struct perf_s= ession *session, off_t file_o > auxtrace_fragment.auxtrace.size =3D aux_size; > auxtrace_fragment.auxtrace.offset =3D aux_offset; > auxtrace_fragment.auxtrace.idx =3D etmq->queue_nr; > + auxtrace_fragment.auxtrace.cpu =3D etmq->queue_nr; > file_offset +=3D aux_offset - auxtrace_event->offset + auxtrace_event-= >header.size; > =20 > pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64 [Severity: Medium] Does queueing per-thread AUX fragments into per-CPU queues break PERF_RECORD_EXIT handling in timeless decoding mode? In timeless per-thread mode, cs_etm__process_event() calls cs_etm__process_timeless_queues(etm, event->fork.tid) when a thread exits: tools/perf/util/cs-etm.c:cs_etm__process_timeless_queues() { ... if (etm->per_thread_decoding) { tidq =3D cs_etm__etmq_get_traceid_queue( etmq, CS_ETM_PER_THREAD_TRACEID); if (!tidq) continue; if (tid =3D=3D -1 || thread__tid(tidq->frontend_thread) =3D=3D tid) cs_etm__run_per_thread_timeless_decoder(etmq); } else ... } Because auxtrace_queues__queue_buffer() sets queue->tid only from the first buffer queued on each CPU, a per-CPU queue holding buffers from multiple threads will either drain and decode every queued buffer on that CPU (including buffers for other still-running threads whose MMAP2/COMM/FORK events have not been processed yet) when the first thread exits, or skip the exiting thread's buffers on CPUs where another thread ran first. In addition, for every CPU queue that received no AUX buffers, queue->tid remains 0 while etmq is pre-allocated, so cs_etm__etmq_get_traceid_queue() creates a bogus TID 0 thread in machines.host. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130-james-cs-u= nformatted-per-thread-fix-v5-0-f1bcc4d18c9b@linaro.org?part=3D4