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 04/14] gtrace: Add functions to create/destroy a trace component path
Date: Thu, 26 Nov 2026 10:07:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-235241@kernel.org> (raw)
In-Reply-To: <7647f6d7e4435523423e5e392131f102da8ca56d.1795512532.git.mayuresh.chitale@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] gtrace: NULL pointer dereference in
  __gtrace_walk_output_components()
- [Medium] gtrace: unpaired put_trace_id() calls in
  gtrace_unassign_trace_id()
- [Medium] gtrace: unpinned component driver across gtrace_create_path()
  and path lifetime
--

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

gtrace: Add functions to create/destroy a trace component path

This commit adds functions to walk output components and create or destroy
a trace component path from a source to a sink, including trace ID
assignment.

> diff --git a/drivers/hwtracing/gtrace/gtrace-core.c b/drivers/hwtracing/gtrace/gtrace-core.c
> index 57554d5eb98d3..728235fc9bfe2 100644
> --- a/drivers/hwtracing/gtrace/gtrace-core.c
> +++ b/drivers/hwtracing/gtrace/gtrace-core.c

[ ... ]

> @@ -230,6 +231,62 @@ int gtrace_reset_component(struct gtrace_component *comp)
>  }
>  EXPORT_SYMBOL_GPL(gtrace_reset_component);
>  
> +static int __gtrace_walk_output_components(struct gtrace_component *comp,
> +					   bool *stop, void *priv,
> +					   int (*fn)(struct gtrace_component *comp, bool *stop,
> +						     struct gtrace_connection *stop_conn,
> +						     void *priv))
> +{
> +	struct gtrace_comp_priv *cpriv = to_gtrace_comp_priv(comp);
> +	struct gtrace_connection *conn, *stop_conn = NULL;
> +	struct gtrace_platform_data *pdata = comp->pdata;
> +	int i, ret;
> +
> +	if (cpriv->visited) {
> +		dev_err(&comp->dev, "loop detected, please fix the firmware\n");
> +		return -ELOOP;
> +	}
> +	cpriv->visited = true;
> +
> +	for (i = 0; i < pdata->nr_outconns; i++) {
> +		conn = pdata->outconns[i];
> +		ret = __gtrace_walk_output_components(conn->dest_comp, stop, priv, fn);

[Severity: High]
Can conn->dest_comp be NULL here, leading to a NULL pointer dereference of
comp->pdata and cpriv->visited on the recursive call?

When a downstream component is unregistered, gtrace_cleanup_inconns() sets
conn->dest_comp = NULL on the shared connection entry that stays in the
upstream component's pdata->outconns[] array:

drivers/hwtracing/gtrace/gtrace-core.c:gtrace_cleanup_inconns() {
	...
		pdata->inconns[i] = NULL;
		conn->dest_comp = NULL;
		gtrace_put_component(comp);
	...
}

[ ... ]

> @@ -496,6 +553,206 @@ void gtrace_unregister_component(struct gtrace_component *comp)
>  }
>  EXPORT_SYMBOL_GPL(gtrace_unregister_component);

[ ... ]

> +static int gtrace_assign_trace_id(struct gtrace_path *path)
> +{
> +	const struct gtrace_driver *gtdrv;
> +	struct gtrace_component *comp;
> +	struct gtrace_path_node *node;
> +	int trace_id;
> +
> +	list_for_each_entry(node, &path->comp_list, head) {
> +		comp = node->comp;
> +		device_lock(&comp->dev);
> +		gtdrv = to_gtrace_driver(comp->dev.driver);
> +
> +		if (!gtdrv || !gtdrv->get_trace_id) {
> +			device_unlock(&comp->dev);
> +			continue;
> +		}
> +
> +		trace_id = gtdrv->get_trace_id(comp, path->mode);
> +		device_unlock(&comp->dev);
> +		if (trace_id == -ENODATA)
> +			continue;
> +		if (trace_id < 0)
> +			return trace_id;
> +
> +		path->trace_id = trace_id;
> +		return 0;
> +	}
> +
> +	return 0;
> +}
> +
> +static void gtrace_unassign_trace_id(struct gtrace_path *path)
> +{
> +	const struct gtrace_driver *gtdrv;
> +	struct gtrace_component *comp;
> +	struct gtrace_path_node *node;
> +
> +	if (path->trace_id == GTRACE_INVALID_TRACE_ID)
> +		return;
> +
> +	list_for_each_entry(node, &path->comp_list, head) {
> +		comp = node->comp;\
> +		device_lock(&comp->dev);
> +		gtdrv = to_gtrace_driver(comp->dev.driver);
> +
> +		if (!gtdrv || !gtdrv->put_trace_id) {
> +			device_unlock(&comp->dev);
> +			continue;
> +		}
> +
> +		gtdrv->put_trace_id(comp, path->mode, path->trace_id);
> +		device_unlock(&comp->dev);
> +	}
> +}

[Severity: Medium]
Can gtrace_unassign_trace_id() call put_trace_id() on components that never
allocated path->trace_id?

gtrace_assign_trace_id() skips components whose get_trace_id() returns
-ENODATA and stops at the first component that returns a valid ID, whereas
gtrace_unassign_trace_id() calls put_trace_id() on every component in
path->comp_list that defines the callback.  Any component in the path that
returned -ENODATA or sat after the supplier will receive an unpaired
put_trace_id() call, corrupting its trace ID accounting.

> +
> +static bool gtrace_path_ready(struct gtrace_path *path)
> +{
> +	struct gtrace_comp_priv *cpriv;
> +	struct gtrace_path_node *node;
> +
> +	list_for_each_entry(node, &path->comp_list, head) {
> +		device_lock(&node->comp->dev);
> +		cpriv = to_gtrace_comp_priv(node->comp);
> +		if (!cpriv->ready) {
> +			device_unlock(&node->comp->dev);
> +			return false;
> +		}
> +		device_unlock(&node->comp->dev);
> +	}
> +
> +	return true;
> +}

[ ... ]

> +	if (!gtrace_path_ready(path)) {
> +		ret = -EOPNOTSUPP;
> +		goto err_release_path_nodes;
> +	}
> +
> +	ret = gtrace_assign_trace_id(path);

[Severity: Medium]
What prevents a component driver from being unbound after
gtrace_path_ready() drops device_lock(&node->comp->dev)?

CPU0                              CPU1
gtrace_create_path()
  gtrace_path_ready()
    device_lock(&comp->dev)
    cpriv->ready == true
    device_unlock(&comp->dev)
                                  device_release_driver(&comp->dev)
                                    gtrace_remove()
                                      cpriv->ready = false
                                    comp->dev.driver = NULL
  gtrace_assign_trace_id()
    gtdrv == NULL -> continue

Because build_path_walk_fn() only takes a struct device reference via
gtrace_get_component() without pinning the bound driver, an unbind after
gtrace_path_ready() causes gtrace_assign_trace_id() to see !gtdrv and
silently skip the component, allowing gtrace_create_path() to succeed with
an unbound component (or skip put_trace_id() if the driver is unbound
before gtrace_destroy_path()).

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

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

Thread overview: 22+ 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  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 [this message]
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
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  9:55 ` [PATCH v7 12/14] perf tools: Add RISC-V trace PMU record capabilities Mayuresh Chitale
2026-11-26  9:55 ` [PATCH v7 13/14] perf tools: Initial support for gtrace decoder Mayuresh Chitale
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-235241@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