All of lore.kernel.org
 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 11/14] gtrace: Add perf driver for tracing using perf tool
Date: Thu, 26 Nov 2026 10:23:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-235274@kernel.org> (raw)
In-Reply-To: <52d43c11157ac39610889fed1d7192c9f5a82a0c.1795512532.git.mayuresh.chitale@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Critical] gtrace: NULL pointer dereference of event->owner in
  gtrace_setup_aux()
- [High] gtrace: gtrace_event_init() returns -EINVAL instead of -ENOENT
  on type mismatch
- [High] gtrace: unclosed AUX handle and leaked refcount in
  gtrace_event_stop()
- [High] gtrace: missing PERF_PMU_CAP_NO_EXCLUDE allows unprivileged
  kernel tracing
- [Medium] gtrace: missing perf_event_itrace_started() call in
  gtrace_event_start()
- [Medium] gtrace: race between gtrace_path_stop() and
  gtrace_path_copyto_auxbuf()
--

commit 3ba5b474daf5f255e16143a11ae06c6b2b9c9dd7
Author: Mayuresh Chitale <mayuresh.chitale@oss.qualcomm.com>

gtrace: Add perf driver for tracing using perf tool

This commit adds a perf PMU driver for the gtrace hardware tracing framework
to record trace data into perf AUX buffers.

> diff --git a/drivers/hwtracing/gtrace/gtrace-perf.c b/drivers/hwtracing/gtrace/gtrace-perf.c
> new file mode 100644
> index 0000000000000..3ed0003f29e5a
> --- /dev/null
> +++ b/drivers/hwtracing/gtrace/gtrace-perf.c
> @@ -0,0 +1,368 @@

[ ... ]

> +static void *gtrace_setup_aux(struct perf_event *event, void **pages,
> +			       int nr_pages, bool overwrite)
> +{
> +	struct gtrace_event_data *event_data = NULL;
> +	struct page **pagelist;
> +	int cpu = event->cpu, i;
> +	cpumask_t *mask;
> +
> +	event_data = alloc_event_data(cpu);
> +	if (!event_data)
> +		return NULL;
> +
> +	mask = &event_data->mask;
> +	/*
> +	 * Create the path for each CPU in the mask. In case of any failure skip the CPU
> +	 */
> +	for_each_cpu(cpu, mask) {
> +		struct gtrace_component *src;
> +		struct gtrace_path *path;
> +
> +		src = gtrace_cpu_source(cpu);
> +		if (!src) {
> +			cpumask_clear_cpu(cpu, mask);
> +			continue;
> +		}
> +
> +		path = gtrace_create_path(src, NULL, GTRACE_COMPONENT_MODE_PERF);
> +		gtrace_put_component(src);
> +		if (IS_ERR(path)) {
> +			cpumask_clear_cpu(cpu, mask);
> +			continue;
> +		}
> +
> +		path->owner = task_pid_nr(event->owner);

[Severity: Critical]
Can task_pid_nr(event->owner) dereference a NULL or freed task_struct here?

If the thread that called perf_event_open() exits while another thread or
child process still holds the event file descriptor and calls mmap() to set
up the AUX buffer, perf_event_exit_task() clears event->owner to NULL before
the exiting task_struct is freed via RCU.  task_pid_nr() unconditionally
dereferences tsk->pid, and gtrace_setup_aux() reads event->owner across
sleeping calls without RCU protection or a NULL check.

[ ... ]

> +static int gtrace_event_init(struct perf_event *event)
> +{
> +	if (event->attr.type != gtrace_pmu.type)
> +		return -EINVAL;
> +
> +	return 0;
> +}

[Severity: High]
Should gtrace_event_init() return -ENOENT instead of -EINVAL when
event->attr.type does not match gtrace_pmu.type?

When perf_init_event() walks the global pmus list to resolve an event, it
stops iterating as soon as any PMU callback returns an error other than
-ENOENT, so returning -EINVAL here causes unrelated perf_event_open() calls
to fail prematurely.

> +
> +static void gtrace_event_start(struct perf_event *event, int flags)
> +{
> +	struct gtrace_ctxt *ctxt = this_cpu_ptr(&gtrace_ctxt);
> +	struct perf_output_handle *handle = &ctxt->handle;
> +	struct gtrace_event_data *event_data;
> +	int cpu = smp_processor_id();
> +	struct gtrace_path *path;
> +
> +	if (WARN_ON(ctxt->event_data))
> +		goto fail;
> +
> +	/*
> +	 * Deal with the ring buffer API and get a handle on the
> +	 * session's information.
> +	 */
> +	event_data = perf_aux_output_begin(handle, event);
> +	if (!event_data)
> +		goto fail;
> +
> +	if (!cpumask_test_cpu(cpu, &event_data->mask))
> +		goto out;
> +
> +	event_data->buf.pos = handle->head % event_data->buf.length;
> +	path = *gtrace_event_cpu_path_ptr(event_data, cpu);
> +	if (!path) {
> +		dev_err_ratelimited(gtrace_pmu.dev, "Error. Path not found on cpu %d\n", cpu);
> +		goto fail_end;
> +	}
> +
> +	if (gtrace_path_start(path)) {
> +		dev_err_ratelimited(gtrace_pmu.dev, "Error. Tracing not started on cpu %d\n", cpu);
> +		goto fail_end;
> +	}
> +
> +	/* Report the hardware source ID once per CPU for this AUX buffer. */
> +	if (path->trace_id != GTRACE_INVALID_TRACE_ID &&
> +	    !cpumask_test_cpu(cpu, &event_data->aux_hwid_done)) {
> +		cpumask_set_cpu(cpu, &event_data->aux_hwid_done);
> +		perf_report_aux_output_id(event, path->trace_id);
> +	}
> +
> +out:
> +	/* Tell the perf core the event is alive */
> +	event->hw.state = 0;

[Severity: Medium]
Should gtrace_event_start() call perf_event_itrace_started(event)?

Because gtrace_pmu sets PERF_PMU_CAP_ITRACE, perf_log_itrace_start() checks
PERF_ATTACH_ITRACE on every event_sched_in().  Without
perf_event_itrace_started() setting PERF_ATTACH_ITRACE, perf emits a
PERF_RECORD_ITRACE_START record into the main ring buffer on every context
switch instead of only once when tracing starts.

> +	ctxt->event_data = event_data;
> +	return;
> +fail_end:
> +	perf_aux_output_end(handle, 0);
> +fail:
> +	event->hw.state = PERF_HES_STOPPED;
> +}
> +
> +static void gtrace_event_stop(struct perf_event *event, int mode)
> +{
> +	struct gtrace_ctxt *ctxt = this_cpu_ptr(&gtrace_ctxt);
> +	struct perf_output_handle *handle = &ctxt->handle;
> +	u64 aux_flags = PERF_AUX_FLAG_GTRACE_FORMAT_UNKNOWN;
> +	struct gtrace_event_data *event_data;
> +	int ret, cpu = smp_processor_id();
> +	struct gtrace_path *path;
> +	size_t size = 0;
> +
> +	if (event->hw.state == PERF_HES_STOPPED)
> +		return;
> +
> +	if (handle->event && WARN_ON(perf_get_aux(handle) != ctxt->event_data))
> +		return;
> +
> +	event_data = ctxt->event_data;
> +	ctxt->event_data = NULL;
> +
> +	if (WARN_ON(!event_data))
> +		return;
> +
> +	if (!cpumask_test_cpu(cpu, &event_data->mask))
> +		goto out_end;
> +
> +	/* stop tracing */
> +	path = *gtrace_event_cpu_path_ptr(event_data, cpu);
> +	if (!path) {
> +		dev_err_ratelimited(gtrace_pmu.dev, "Error. Path not found on cpu %d\n", cpu);
> +		goto out_end;
> +	}
> +
> +	if (gtrace_path_stop(path)) {
> +		dev_err_ratelimited(gtrace_pmu.dev, "Error. Tracing not stopped on cpu %d\n", cpu);
> +		goto out_end;
> +	}
> +
> +	event->hw.state = PERF_HES_STOPPED;
> +	if (handle->event && (mode & PERF_EF_UPDATE)) {
> +		if (WARN_ON_ONCE(handle->event != event))
> +			return;
> +		size = event_data->overwrite ? event_data->buf.length : handle->size;
> +		ret = gtrace_path_copyto_auxbuf(path, &event_data->buf, &size, &aux_flags);

[Severity: Medium]
Can another CPU start or stop a path sharing the same sink in the window
between gtrace_path_stop() and gtrace_path_copyto_auxbuf()?

CPU0                                  CPU1
gtrace_event_stop()
  gtrace_path_stop(path)
    __gtrace_comp_stop(sink)
      raw_spin_lock(&sink->lock)
      sink->start_count-- (becomes 0)
      gtdrv->stop(sink)
      raw_spin_unlock(&sink->lock)
                                      gtrace_event_start()
                                        gtrace_path_start(path)
                                          __gtrace_comp_start(sink)
                                            raw_spin_lock(&sink->lock)
                                            sink->start_count++ (becomes 1)
                                            gtdrv->start(sink)
                                            raw_spin_unlock(&sink->lock)
  gtrace_path_copyto_auxbuf()
    raw_spin_lock(&sink->lock)
    if (sink->start_count)
      *size = 0;  <-- trace data dropped

Similarly, if two CPUs sharing a sink stop concurrently, both can finish
gtrace_path_stop() and decrement sink->start_count to 0 before either enters
gtrace_path_copyto_auxbuf(), causing both CPUs to see sink->start_count == 0
and copy from the sink.

> +		WARN_ON_ONCE(ret);
> +		aux_flags &= ~PERF_AUX_FLAG_PMU_FORMAT_TYPE_MASK;
> +		aux_flags |= gtrace_path_aux_format(path);
> +		if (READ_ONCE(handle->event)) {
> +			/* Snapshot overwrites are expected; TRUNCATED would disable the event. */
> +			if (event_data->overwrite) {
> +				aux_flags &= ~PERF_AUX_FLAG_TRUNCATED;
> +				handle->head += size;
> +			}
> +			/* Tag the AUX data with the source's trace format. */
> +			perf_aux_output_flag(handle, aux_flags);
> +			perf_aux_output_end(handle, size);
> +		} else
> +			WARN_ON(size);
> +	}
> +
> +	return;
> +
> +out_end:
> +	event->hw.state = PERF_HES_STOPPED;
> +	if (handle->event && (mode & PERF_EF_UPDATE))
> +		perf_aux_output_end(handle, 0);
> +}

[Severity: High]
Does gtrace_event_stop() leave the AUX output handle open and leak
rb->aux_refcount when called without PERF_EF_UPDATE?

When gtrace_event_stop() is called with mode == 0 (for example, when a
grouped software sampling event throttles via perf_event_throttle_group()),
it clears ctxt->event_data and sets event->hw.state = PERF_HES_STOPPED
without calling perf_aux_output_end().  When gtrace_event_del() later calls
gtrace_event_stop(event, PERF_EF_UPDATE), it returns immediately because
event->hw.state is already PERF_HES_STOPPED, leaving handle->event active
and leaking the AUX buffer and event_data.

[ ... ]

> +int __init gtrace_perf_init(void)
> +{
> +	gtrace_pmu.capabilities	= (PERF_PMU_CAP_EXCLUSIVE | PERF_PMU_CAP_ITRACE);

[Severity: High]
Should gtrace_pmu.capabilities include PERF_PMU_CAP_NO_EXCLUDE (or
gtrace_event_init() enforce perf_allow_kernel())?

Neither gtrace nor the encoder interface implements privilege-level trace
filtering.  Without PERF_PMU_CAP_NO_EXCLUDE, an unprivileged user can pass
attr.exclude_kernel = 1 to perf_event_open() to bypass the
perf_allow_kernel() check in sys_perf_event_open() while the hardware
still records kernel execution into the user's AUX buffer.

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

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

Thread overview: 43+ 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 ` Mayuresh Chitale
2026-11-26  9:55 ` [PATCH v7 01/14] dt-bindings: Add RISC-V trace component bindings Mayuresh Chitale
2026-11-26  9:55   ` 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   ` 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  9:55   ` 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  9:55   ` 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  9:55   ` 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  9:55   ` 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  9:55   ` 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   ` Mayuresh Chitale
2026-11-26  9:55 ` [PATCH v7 09/14] gtrace: Add RISC-V Trace ramsink driver Mayuresh Chitale
2026-11-26  9:55   ` Mayuresh Chitale
2026-11-26 10:13   ` sashiko-bot
2026-12-01  9:54   ` Leo Liang
2026-12-01  9:54     ` Leo Liang
2026-11-26  9:55 ` [PATCH v7 10/14] riscv: Enable DMA_RESTRICTED_POOL in defconfig Mayuresh Chitale
2026-11-26  9:55   ` 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   ` Mayuresh Chitale
2026-11-26 10:23   ` sashiko-bot [this message]
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   ` 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  9:55   ` 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
2026-11-26  9:55   ` 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-235274@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 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.