Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mayuresh Chitale" <mayuresh.chitale@oss.qualcomm.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v7 07/14] gtrace: Add function to copy into perf AUX buffer
Date: Thu, 26 Nov 2026 10:05:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-235231@kernel.org> (raw)
In-Reply-To: <115ef070f1edfe8be3e7862dc4c90ef9eb19d568.1795512532.git.mayuresh.chitale@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] gtrace: TOCTOU race between gtrace_path_stop() and
  gtrace_path_copyto_auxbuf()
--

commit 316c0ae2e4a3dbc8091e2e7c9a0a691103a47556
Author: Anup Patel <anup.patel@oss.qualcomm.com>

gtrace: Add function to copy into perf AUX buffer

This commit adds gtrace_path_copyto_auxbuf() and the corresponding
copyto_auxbuf() driver callback to copy trace data from a sink into a perf
AUX buffer.

> diff --git a/drivers/hwtracing/gtrace/gtrace-core.c b/drivers/hwtracing/gtrace/gtrace-core.c
> index 754c5f71168ba..af282cc95b551 100644
> --- a/drivers/hwtracing/gtrace/gtrace-core.c
> +++ b/drivers/hwtracing/gtrace/gtrace-core.c
> @@ -818,6 +818,48 @@ int gtrace_path_stop(struct gtrace_path *path)
>  }
>  EXPORT_SYMBOL_GPL(gtrace_path_stop);
>  
> +int gtrace_path_copyto_auxbuf(struct gtrace_path *path,
> +			      struct gtrace_perf_auxbuf *buf,
> +			      size_t *size, u64 *aux_flags)
> +{
> +	struct gtrace_component *comp = gtrace_path_sink(path);
> +	struct gtrace_comp_priv *sink = to_gtrace_comp_priv(comp);
> +	const struct gtrace_driver *gtdrv;
> +	int ret = -EOPNOTSUPP;
> +	unsigned long flags;
> +
> +	/*
> +	 * Copy only after every path using the sink has stopped; otherwise the
> +	 * hardware may overwrite data being copied, or the same data may be copied
> +	 * to aux buffer twice. The last path to stop does the copy. Holding the
> +	 * sink's lock also stops another path from starting the sink before copy
> +	 * completes.
> +	 */
> +	raw_spin_lock_irqsave(&sink->lock, flags);
> +	if (sink->start_count) {
> +		*size = 0;
> +		ret = 0;
> +		goto out;
> +	}
> +
> +	if (!sink->ready) {
> +		ret = -ENODEV;
> +		goto out;
> +	}
> +
> +	gtdrv = to_gtrace_driver(comp->dev.driver);
> +	if (gtdrv && gtdrv->copyto_auxbuf) {
> +		*size = gtdrv->copyto_auxbuf(comp, buf, *size, aux_flags);
> +		ret = 0;
> +	}

[Severity: Medium]
Can two paths sharing a sink both copy the same trace data, or miss the
copy entirely, because sink->lock is dropped between gtrace_path_stop()
and gtrace_path_copyto_auxbuf()?

__gtrace_comp_stop() decrements sink->start_count and releases sink->lock
before the caller reaches gtrace_path_copyto_auxbuf():

CPU0                                  CPU1
gtrace_path_stop()
  __gtrace_comp_stop(sink)
    lock(&sink->lock)
    sink->start_count-- (2 -> 1)
    unlock(&sink->lock)
                                      gtrace_path_stop()
                                        __gtrace_comp_stop(sink)
                                          lock(&sink->lock)
                                          sink->start_count-- (1 -> 0)
                                          gtdrv->stop(comp)
                                          unlock(&sink->lock)
gtrace_path_copyto_auxbuf()
  lock(&sink->lock)
  sink->start_count == 0
  gtdrv->copyto_auxbuf(...)
  unlock(&sink->lock)
                                      gtrace_path_copyto_auxbuf()
                                        lock(&sink->lock)
                                        sink->start_count == 0
                                        gtdrv->copyto_auxbuf(...)
                                          <-- duplicate copy

Conversely, if another path sharing the sink calls gtrace_path_start() in
that window after the last active path drops sink->lock in
__gtrace_comp_stop(), sink->start_count becomes 1 and the sink restarts
before gtrace_path_copyto_auxbuf() runs, so the stopped path sees
sink->start_count != 0 and skips the copy.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795512532.git.mayuresh.chitale@oss.qualcomm.com?part=7

  reply	other threads:[~2026-11-26 10:05 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  9:55 [PATCH v7 00/14] Linux RISC-V trace framework and drivers Mayuresh Chitale
2026-11-26  9:55 ` [PATCH v7 01/14] dt-bindings: Add RISC-V trace component bindings Mayuresh Chitale
2026-11-26 10:07   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 02/14] hwtracing: gtrace: Initial implementation of gtrace framework Mayuresh Chitale
2026-11-26 10:15   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 03/14] gtrace: Add RISC-V platform driver for the " Mayuresh Chitale
2026-11-26 10:12   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 04/14] gtrace: Add functions to create/destroy a trace component path Mayuresh Chitale
2026-11-26 10:07   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 05/14] gtrace: Add functions to start/stop tracing on a " Mayuresh Chitale
2026-11-26 10:05   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 06/14] gtrace: Add RISC-V Trace encoder driver Mayuresh Chitale
2026-11-26 10:09   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 07/14] gtrace: Add function to copy into perf AUX buffer Mayuresh Chitale
2026-11-26 10:05   ` sashiko-bot [this message]
2026-11-26  9:55 ` [PATCH v7 08/14] perf: Add gtrace AUX buffer trace format type Mayuresh Chitale
2026-11-26  9:55 ` [PATCH v7 09/14] gtrace: Add RISC-V Trace ramsink driver Mayuresh Chitale
2026-11-26 10:13   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 10/14] riscv: Enable DMA_RESTRICTED_POOL in defconfig Mayuresh Chitale
2026-11-26  9:55 ` [PATCH v7 11/14] gtrace: Add perf driver for tracing using perf tool Mayuresh Chitale
2026-11-26 10:23   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 12/14] perf tools: Add RISC-V trace PMU record capabilities Mayuresh Chitale
2026-11-26 10:19   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 13/14] perf tools: Initial support for gtrace decoder Mayuresh Chitale
2026-11-26 10:17   ` sashiko-bot
2026-11-26  9:55 ` [PATCH v7 14/14] MAINTAINERS: Add entry for RISC-V trace framework Mayuresh Chitale

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-235231@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=mayuresh.chitale@oss.qualcomm.com \
    --cc=robh@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