Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "James Clark" <james.clark@linaro.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v5 04/19] perf cs-etm: Use per-CPU queues for per-thread mode
Date: Mon, 30 Nov 2026 13:20:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-241206@kernel.org> (raw)
In-Reply-To: <20261130-james-cs-unformatted-per-thread-fix-v5-4-f1bcc4d18c9b@linaro.org>

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 <james.clark@linaro.org>

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_queue *etmq)
>  	 */
>  	cs_etm__flush_all_stack(etmq);
>  
> +	/*
> +	 * 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 = cs_etm__etmq_get_traceid_queue(etmq,
> +							CS_ETM_PER_THREAD_TRACEID);
> +
> +		if (thread__tid(tidq->decode_thread) != etmq->buffer->tid) {
> +			thread__zput(tidq->frontend_thread);
> +			thread__zput(tidq->decode_thread);
> +			tidq->frontend_thread = machine__findnew_thread(
> +				&etm->session->machines.host, -1,
> +				etmq->buffer->tid);
> +			tidq->decode_thread = 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 = cs_etm__synth_last_instruction_sample(etmq, tidq);
	if (err)
		return err;

	err = 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_session *session, off_t file_o
>  
>  	if (aux_offset >= auxtrace_event->offset &&
>  	    aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {
> -		struct cs_etm_queue *etmq = cs_etm__get_queue(etm, auxtrace_event->cpu);
> +		/* Old per-thread files have no CPU (before v6.4) */
> +		int queue_idx = sample->cpu == (u32)-1 ? 0 : sample->cpu;
> +		struct cs_etm_queue *etmq = cs_etm__get_queue(etm, queue_idx);
>  
>  		if (!etmq)
>  			return -EINVAL;
> @@ -3320,6 +3339,7 @@ static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_o
>  		auxtrace_fragment.auxtrace.size = aux_size;
>  		auxtrace_fragment.auxtrace.offset = aux_offset;
>  		auxtrace_fragment.auxtrace.idx = etmq->queue_nr;
> +		auxtrace_fragment.auxtrace.cpu = etmq->queue_nr;
>  		file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
>  
>  		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 = cs_etm__etmq_get_traceid_queue(
				etmq, CS_ETM_PER_THREAD_TRACEID);

			if (!tidq)
				continue;

			if (tid == -1 || thread__tid(tidq->frontend_thread) == 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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-james-cs-unformatted-per-thread-fix-v5-0-f1bcc4d18c9b@linaro.org?part=4

  reply	other threads:[~2026-11-30 13:20 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 12:55 [PATCH v5 00/19] perf cs-etm: Per-thread mode fixes and snapshot wrap support James Clark
2026-11-30 12:55 ` [PATCH v5 01/19] perf cs-etm: Fix nVHE per-thread decoding James Clark
2026-11-30 13:12   ` sashiko-bot
2026-11-30 12:55 ` [PATCH v5 02/19] perf cs-etm: Warn for invalid timestamp option James Clark
2026-11-30 12:55 ` [PATCH v5 03/19] perf cs-etm: Turn on context packet timestamps in per-thread mode James Clark
2026-11-30 12:55 ` [PATCH v5 04/19] perf cs-etm: Use per-CPU queues for " James Clark
2026-11-30 13:20   ` sashiko-bot [this message]
2026-11-30 12:55 ` [PATCH v5 05/19] perf test: cs-etm: Increase window size in disassembly test James Clark
2026-11-30 12:55 ` [PATCH v5 06/19] perf cs-etm: Don't drop previous target for kernel samples James Clark
2026-11-30 12:55 ` [PATCH v5 07/19] perf python: Expose sample instruction length James Clark
2026-11-30 12:55 ` [PATCH v5 08/19] perf cs-etm: Use instruction length in disassembly script James Clark
2026-11-30 12:55 ` [PATCH v5 09/19] perf cs-etm: Increase default timestamp generation period James Clark
2026-11-30 12:55 ` [PATCH v5 10/19] perf auxtrace: Turn Intel BTS snapshot search into a generic one James Clark
2026-11-30 12:55 ` [PATCH v5 11/19] perf arm-spe: Use generic snapshot search James Clark
2026-11-30 12:55 ` [PATCH v5 12/19] perf auxtrace: intel-pt: Use common snapshot pointer fixup James Clark
2026-11-30 12:55 ` [PATCH v5 13/19] perf cs-etm: Queue partial AUX records James Clark
2026-11-30 13:14   ` sashiko-bot
2026-11-30 12:55 ` [PATCH v5 14/19] perf cs-etm: Don't print missing buffers in snapshot mode James Clark
2026-11-30 12:55 ` [PATCH v5 15/19] perf auxtrace: cs-etm: Capture wrapped snapshots James Clark
2026-11-30 12:55 ` [PATCH v5 16/19] perf test: Allow infinite named_thread loops James Clark
2026-11-30 12:55 ` [PATCH v5 17/19] perf test: cs-etm: Add test for per-thread mode James Clark
2026-11-30 12:55 ` [PATCH v5 18/19] perf test: cs-etm: Test multiple per-thread threads James Clark
2026-11-30 13:13   ` sashiko-bot
2026-11-30 12:55 ` [PATCH v5 19/19] perf test: cs-etm: Increase snapshot size James Clark
2026-11-30 13:07   ` sashiko-bot
2026-11-30 13:20 ` [PATCH v5 00/19] perf cs-etm: Per-thread mode fixes and snapshot wrap support sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-241206@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=james.clark@linaro.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox