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 03/14] gtrace: Add RISC-V platform driver for the gtrace framework
Date: Thu, 26 Nov 2026 10:12:12 +0000	[thread overview]
Message-ID: <sashiko-outbox-235253@kernel.org> (raw)
In-Reply-To: <b5b3dc4a1ca378ea74498d1adb0207e281e6ad01.1795512532.git.mayuresh.chitale@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] gtrace: device reference leak on dest_comp in
  gtrace_of_parse_outconns()
- [Medium] gtrace: TOCTOU race between gtrace_of_parse_outconns() and
  registration
- [Low] gtrace: CONFIG_RVTRACE module name mismatch between Kconfig and
  Makefile
--

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

gtrace: Add RISC-V platform driver for the gtrace framework

This commit adds device-tree connection parsing helpers and a RISC-V
platform driver for the gtrace framework.

> diff --git a/drivers/hwtracing/gtrace/Kconfig b/drivers/hwtracing/gtrace/Kconfig
> index bcc3b4169a764..04986f2165491 100644
> --- a/drivers/hwtracing/gtrace/Kconfig
> +++ b/drivers/hwtracing/gtrace/Kconfig
> @@ -13,3 +13,18 @@ menuconfig GTRACE
>  
>  	  To compile this driver as a module, choose M here: the module
>  	  will be called gtrace.
> +
> +config RVTRACE
> +	tristate "RISC-V Trace Support"
> +	depends on RISCV
> +	depends on OF
> +	depends on GTRACE
> +	default RISCV
> +	help
> +	  This provides the RISC-V backend for the generic hardware trace
> +	  (gtrace) framework. It parses the RISC-V trace component topology,
> +	  provides the RISC-V register-level control ops and registers each
> +	  component with the gtrace core.
> +
> +	  To compile this driver as a module, choose M here: the module
> +	  will be called rvtrace.
> diff --git a/drivers/hwtracing/gtrace/Makefile b/drivers/hwtracing/gtrace/Makefile
> index 3f90fb99c5149..20f67a78b8abc 100644
> --- a/drivers/hwtracing/gtrace/Makefile
> +++ b/drivers/hwtracing/gtrace/Makefile
> @@ -2,3 +2,6 @@
>  
>  obj-$(CONFIG_GTRACE) += gtrace.o
>  gtrace-y := gtrace-core.o
> +gtrace-$(CONFIG_OF) += gtrace-of.o
> +
> +obj-$(CONFIG_RVTRACE) += rvtrace-platform.o

[Severity: Low]
The Kconfig help text states that the module will be called rvtrace, but
building CONFIG_RVTRACE=m produces rvtrace-platform.ko.

> diff --git a/drivers/hwtracing/gtrace/gtrace-of.c b/drivers/hwtracing/gtrace/gtrace-of.c
> new file mode 100644
> index 0000000000000..4ffeda32c4a38
> --- /dev/null
> +++ b/drivers/hwtracing/gtrace/gtrace-of.c
> @@ -0,0 +1,133 @@

[ ... ]

> +static struct gtrace_connection *
> +gtrace_of_parse_outconn(struct gtrace_platform_data *pdata,
> +			struct device_node *ep_node)
> +{
> +	struct device_node *rep_node, *rdev_node;
> +	struct gtrace_connection *conn;
> +	struct of_endpoint ep, rep;
> +	int ret;
> +
> +	conn = devm_kzalloc(pdata->dev, sizeof(*conn), GFP_KERNEL);
> +	if (!conn)
> +		return ERR_PTR(-ENOMEM);
> +
> +	ret = of_graph_parse_endpoint(ep_node, &ep);
> +	if (ret)
> +		return ERR_PTR(ret);
> +
> +	rep_node = of_graph_get_remote_endpoint(ep_node);
> +	if (!rep_node)
> +		return ERR_PTR(-ENODEV);
> +
> +	rdev_node = of_graph_get_port_parent(rep_node);
> +
> +	ret = of_graph_parse_endpoint(rep_node, &rep);
> +	if (ret)
> +		goto out;
> +
> +	conn->src_port = ep.port;
> +	conn->src_fwnode = dev_fwnode(pdata->dev);
> +	/* The 'src_comp' is set by gtrace_register_component() */
> +	conn->src_comp = NULL;
> +	conn->dest_port = rep.port;
> +	conn->dest_fwnode = of_fwnode_handle(rdev_node);
> +	fwnode_handle_get(conn->dest_fwnode);
> +	conn->dest_comp = gtrace_find_by_fwnode(conn->dest_fwnode);

[Severity: Medium]
Can conn->dest_comp become stale if the destination component is
unregistered before gtrace_register_component() runs?

CPU0                                  CPU1
rvtrace_platform_probe() [src]
  gtrace_of_parse_outconns()
    gtrace_of_parse_outconn()
      gtrace_find_by_fwnode()
        mutex_lock(&gtrace_mutex)
        gtrace_get_component(dest)
        mutex_unlock(&gtrace_mutex)
                                      rvtrace_platform_remove() [dest]
                                        gtrace_unregister_component(dest)
                                          mutex_lock(&gtrace_mutex)
                                          gtrace_cleanup_inconns(dest, NULL)
                                          device_unregister(&dest->dev)
                                          mutex_unlock(&gtrace_mutex)
                                      /* devres frees dest->pdata */
  gtrace_register_component()
    mutex_lock(&gtrace_mutex)
    gtrace_setup_inconns_from_outconns()
      bus_for_each_dev(..., gtrace_setup_inconn)
      /* dest is off bus; returns 0 */
    mutex_unlock(&gtrace_mutex)

Because conn has not been installed in dest->pdata->inconns[] yet when
CPU1 runs gtrace_cleanup_inconns(), conn->dest_comp is not cleared.  Then
in gtrace_setup_inconns_from_outconns(), bus_for_each_dev() returns 0
because dest->dev is no longer on gtrace_bustype, and checking only
ret < 0 treats that as success:

drivers/hwtracing/gtrace/gtrace-core.c:gtrace_setup_inconns_from_outconns() {
    ...
		ret = bus_for_each_dev(&gtrace_bustype, NULL, conn, gtrace_setup_inconn);
		if (ret < 0) {
			gtrace_cleanup_inconns_from_outconns(comp);
			return ret;
		}
    ...
}

Registration then succeeds with conn->dest_comp pointing to an
unregistered component whose devm-allocated dest->pdata has already been
freed, leading to a use-after-free when walking output connections.

> +	if (!conn->dest_comp) {
> +		fwnode_handle_put(conn->dest_fwnode);
> +		ret = -EPROBE_DEFER;
> +	}
> +
> +out:
> +	of_node_put(rdev_node);
> +	of_node_put(rep_node);
> +	return ret ? ERR_PTR(ret) : conn;
> +}
> +
> +/*
> + * Parse the "out-ports" graph of the component's device tree node and fill
> + * pdata->outconns.
> + * Return: 0 on success or when there are no output ports
> + *         -EPROBE_DEFER if a destination is not registered yet
> + *         negative error code otherwise.
> + */
> +int gtrace_of_parse_outconns(struct gtrace_platform_data *pdata)
> +{
> +	struct device_node *parent, *ep_node;
> +	struct gtrace_connection *conn;
> +	int ret = 0, i = 0;
> +
> +	parent = of_get_child_by_name(dev_of_node(pdata->dev), "out-ports");
> +	if (!parent)
> +		return 0;
> +
> +	pdata->nr_outconns = of_graph_get_endpoint_count(parent);
> +	pdata->outconns = devm_kcalloc(pdata->dev, pdata->nr_outconns,
> +				       sizeof(*pdata->outconns), GFP_KERNEL);
> +	if (!pdata->outconns) {
> +		ret = -ENOMEM;
> +		goto done;
> +	}
> +
> +	for_each_endpoint_of_node(parent, ep_node) {
> +		conn = gtrace_of_parse_outconn(pdata, ep_node);
> +		if (IS_ERR(conn)) {
> +			ret = PTR_ERR(conn);
> +			of_node_put(ep_node);
> +			break;
> +		}
> +
> +		pdata->outconns[i] = conn;
> +		i++;
> +	}
> +
> +done:
> +	if (ret) {
> +		for (i = 0; i < pdata->nr_outconns && pdata->outconns; i++) {
> +			conn = pdata->outconns[i];
> +			if (conn && conn->dest_fwnode)
> +				fwnode_handle_put(conn->dest_fwnode);
> +		}
> +	}

[Severity: Medium]
Does this error path leak the conn->dest_comp device reference acquired by
gtrace_find_by_fwnode()?

When a component has multiple output endpoints and parsing a later
endpoint fails (such as with -EPROBE_DEFER), the loop only drops
conn->dest_fwnode on the earlier connections and rvtrace_platform_probe()
returns directly without calling gtrace_release_outconns(pdata).  Calling
gtrace_release_outconns(pdata) here when pdata->outconns is non-NULL would
release both conn->dest_comp and conn->dest_fwnode.

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

  reply	other threads:[~2026-11-26 10:12 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 [this message]
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
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-235253@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