All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Leo Yan" <leo.yan@arm.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v7 5/8] perf cs-etm: Flush thread stacks after decoder reset
Date: Tue, 23 Jun 2026 19:58:35 +0000	[thread overview]
Message-ID: <20260623195835.6C0451F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260611-b4-arm_cs_callchain_support_v1-v7-5-1ba770c862ae@arm.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Temporal and spatial violation causing global thread stack destruction on local queue replenishment.
- [High] Guest machine thread stacks are bypassed during flush when context IDs are disabled (`CS_ETM_PIDFMT_NONE`).
--

commit 44f2b7bdbbeef1f4cf7936476255084d1e513f39
Author: Leo Yan <leo.yan@arm.com>
perf cs-etm: Flush thread stacks after decoder reset

This commit introduces a mechanism to flush all host and guest thread
stacks when the CoreSight decoder is reset due to trace discontinuities.
This is intended to prevent stale call/return histories from corrupting
synthesized callchains.

> diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
> index 8798bf0471faf..7069b4990e610 100644
> --- a/tools/perf/util/cs-etm.c
> +++ b/tools/perf/util/cs-etm.c
[ ... ]
> @@ -1997,6 +1997,37 @@ static int cs_etm__end_block(struct cs_etm_queue *etmq,
>  
>  	return 0;
>  }
> +
> +static int cs_etm__flush_stack_cb(struct thread *thread,
> +				  void *data __maybe_unused)
> +{
> +	thread_stack__flush(thread);
> +	return 0;
> +}
> +
> +static void cs_etm__flush_machine_stack(struct cs_etm_queue *etmq, pid_t pid)
> +{
> +	struct machine *machine;
> +
> +	machine = machines__find(&etmq->etm->session->machines, pid);
> +	if (machine)
> +		machine__for_each_thread(machine, cs_etm__flush_stack_cb, NULL);
> +}
> +
> +static void cs_etm__flush_all_stack(struct cs_etm_queue *etmq)
> +{
> +	enum cs_etm_pid_fmt pid_fmt = cs_etm__get_pid_fmt(etmq);
> +
> +	if (!etmq->etm->synth_opts.last_branch)
> +		return;
> +
> +	cs_etm__flush_machine_stack(etmq, HOST_KERNEL_ID);
> +
> +	/* Clear the guest stack if virtualization is supported */
> +	if (pid_fmt == CS_ETM_PIDFMT_CTXTID2)
> +		cs_etm__flush_machine_stack(etmq, DEFAULT_GUEST_KERNEL_ID);

[Severity: High]
Does this logic bypass populated guest machine thread stacks when tracing
with CS_ETM_PIDFMT_NONE on hardware where the kernel runs at EL1?

Looking at cs_etm__get_machine(), traces with exception level ocsd_EL1 are
assigned to the guest machine even when pid_fmt is CS_ETM_PIDFMT_NONE.

By restricting the guest machine flush strictly to CS_ETM_PIDFMT_CTXTID2
here, it seems these populated guest thread stacks are skipped during the
trace discontinuity flush, which could allow stale branch histories to
persist.

> +}
> +
[ ... ]
> @@ -2019,6 +2050,12 @@ static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
>  		ret = cs_etm_decoder__reset(etmq->decoder);
>  		if (ret)
>  			return ret;
> +
> +		/*
> +		 * Since the decoder is reset, this causes a global trace
> +		 * discontinuity. Flush all thread stacks.
> +		 */
> +		cs_etm__flush_all_stack(etmq);

[Severity: High]
Could this global flush cause a temporal and spatial violation by destroying
thread states for events that haven't been processed yet?

When a local auxtrace queue runs out of data and triggers a read-ahead via
cs_etm__get_data_block(), it unconditionally calls cs_etm__flush_all_stack().
Because CoreSight auxtrace operates using a min-heap to chronologically
interleave events across multiple independent CPU queues, events from other
queues are still chronologically sorted in the min-heap waiting to be
processed.

Iterating over machine__for_each_thread() and flushing all thread stacks
globally during the read-ahead phase of a single queue appears to
prematurely destroy the thread state for events that happened before the
discontinuity on the current queue, as well as for threads tracked by
other CPU queues.

Is it safe to perform this synchronous global flush ahead of the
chronological min-heap processing?

>  	}
>  
>  	return etmq->buf_len;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260611-b4-arm_cs_callchain_support_v1-v7-0-1ba770c862ae@arm.com?part=5

  reply	other threads:[~2026-06-23 19:58 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-11  7:56 [PATCH v7 0/8] perf cs-etm: Support thread stack and callchain Leo Yan
2026-06-11  7:56 ` [PATCH v7 1/8] perf cs-etm: Filter synthesized branch samples Leo Yan
2026-06-11  8:58   ` James Clark
2026-06-11  7:56 ` [PATCH v7 2/8] perf cs-etm: Decode ETE exception packets Leo Yan
2026-06-11  7:56 ` [PATCH v7 3/8] perf cs-etm: Refactor instruction size handling Leo Yan
2026-06-11  7:56 ` [PATCH v7 4/8] perf cs-etm: Use thread-stack for last branch entries Leo Yan
2026-06-11  9:01   ` James Clark
2026-06-23 19:46   ` sashiko-bot
2026-06-11  7:56 ` [PATCH v7 5/8] perf cs-etm: Flush thread stacks after decoder reset Leo Yan
2026-06-23 19:58   ` sashiko-bot [this message]
2026-06-11  7:57 ` [PATCH v7 6/8] perf cs-etm: Support call indentation Leo Yan
2026-06-23 20:11   ` sashiko-bot
2026-06-11  7:57 ` [PATCH v7 7/8] perf cs-etm: Synthesize callchains for instruction samples Leo Yan
2026-06-23 20:24   ` sashiko-bot
2026-06-11  7:57 ` [PATCH v7 8/8] perf test: Add Arm CoreSight callchain test Leo Yan
2026-06-11  9:11   ` James Clark
2026-06-11 12:42     ` Leo Yan
2026-06-23 20:31   ` 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=20260623195835.6C0451F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=leo.yan@arm.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.