Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Athira Rajeev" <atrajeev@linux.ibm.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 2/5] powerpc/htm: Add support to setup and free aux buffer for capturing HTM data
Date: Wed, 01 Jul 2026 08:50:57 +0000	[thread overview]
Message-ID: <20260701085058.DEC851F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260701083806.79358-3-atrajeev@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The driver violates the perf AUX ring buffer API by writing trace data to independent offsets, ignoring the core perf ring buffer head.
- [Medium] Signed integer overflow when calculating the AUX buffer size leads to a massive 64-bit value.
--

commit 10ce4df3e9571041276c72fea1e10e3955d5eea1
Author: Athira Rajeev <atrajeev@linux.ibm.com>
Subject: powerpc/htm: Add support to setup and free aux buffer for capturing HTM data

This commit adds PMU callbacks for setup_aux and free_aux to enable 
capturing HTM trace data into a perf auxiliary buffer. It allocates 
memory and records trace data directly into the provided pages for 
later post-processing.

> diff --git a/arch/powerpc/perf/htm-perf.c b/arch/powerpc/perf/htm-perf.c
> index e22a7fdce2f5d..ae7f469b6840d 100644
> --- a/arch/powerpc/perf/htm-perf.c
> +++ b/arch/powerpc/perf/htm-perf.c
[ ... ]
> @@ -126,6 +143,74 @@ static ssize_t htm_return_check(int rc)
>  	return -EINVAL;
>  }
>  
> +static int htm_dump_sample_data(struct perf_event *event)
> +{
[ ... ]
> +	/*
> +	 * Invoke H_HTM call with:
> +	 * - operation as htm dump (H_HTM_OP_DUMP_DATA)
> +	 * - last three values are address, size and offset
> +	 */
> +	if (aux_buf->collect_htm_trace) {
> +		do {
> +			rc = htm_hcall_wrapper(htmflags, nodeindex, nodalchipindex, coreindexonchip,
> +				htmtype, H_HTM_OP_DUMP_DATA, virt_to_phys(aux_buf->base),
> +				(aux_buf->nr_pages * PAGE_SIZE), aux_buf->head);
> +			ret = htm_return_check(rc);
> +		} while (ret == -EBUSY && ++retries < 100);
> +
> +		if (ret > 0) {
> +			aux_buf->head += (aux_buf->nr_pages * PAGE_SIZE);
> +			perf_aux_output_end(&htm_ctx->handle, (aux_buf->nr_pages * PAGE_SIZE));

[Severity: High]
Does this code write trace data to independent offsets, ignoring the core 
perf ring buffer head?

If the user does not consume data fast enough, perf_aux_output_end() will 
truncate the write to the available space. Because the driver ignores 
htm_ctx->handle.head (the true write offset within the ring buffer) and 
tracks its own offsets via aux_buf->base and aux_buf->head, could any 
truncation desynchronize the driver from the perf core?

This could lead to subsequent writes going to the wrong offset while 
userspace is told the data is at a different offset, resulting in silent 
corruption of the perf AUX trace data.

> +		} else {
> +			aux_buf->collect_htm_trace = 0;
> +			perf_aux_output_end(&htm_ctx->handle, 0);
> +		}
> +	}
> +
> +	return ret;
> +}
[ ... ]
> +static void *htm_setup_aux(struct perf_event *event, void **pages,
> +		int nr_pages, bool snapshot)
> +{
> +	int cpu = event->cpu;
> +	struct htm_pmu_buf *buf;
> +
> +	/* We need at least one page for this to work. */
> +	if (!nr_pages)
> +		return NULL;
> +
> +	if (cpu == -1)
> +		cpu = raw_smp_processor_id();
> +
> +	buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, cpu_to_node(cpu));
> +	if (!buf)
> +		return NULL;
> +
> +	buf->base = pages[0];
> +
> +	if (!buf->base) {
> +		kfree(buf);
> +		return NULL;
> +	}
> +
> +	buf->nr_pages = nr_pages;
> +	buf->snapshot = false;
> +	buf->size = nr_pages << PAGE_SHIFT;

[Severity: Medium]
Can the size calculation overflow for large values of nr_pages?

Since both nr_pages and PAGE_SHIFT evaluate as 32-bit signed integers, 
a large AUX buffer allocation (e.g., nr_pages = 32768) could overflow into 
the sign bit. When assigned to the u64 size field, this would sign-extend 
to a massive 64-bit value. 

Could this result in bypassing internal bounds checks when the size is 
later returned to the perf subsystem?

> +	buf->head = 0;
> +	buf->head_size = 0;
> +	buf->htm_stopped = 0;
> +	buf->collect_htm_trace = 1;
> +	return buf;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260701083806.79358-1-atrajeev@linux.ibm.com?part=2

  reply	other threads:[~2026-07-01  8:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-01  8:38 [PATCH 0/5] powerpc/htm: Add interface to expose HTM trace data via perf Athira Rajeev
2026-07-01  8:38 ` [PATCH 1/5] " Athira Rajeev
2026-07-01  8:50   ` sashiko-bot
2026-07-01  8:38 ` [PATCH 2/5] powerpc/htm: Add support to setup and free aux buffer for capturing HTM data Athira Rajeev
2026-07-01  8:50   ` sashiko-bot [this message]
2026-07-01  8:38 ` [PATCH 3/5] powerpc/perf: Capture the HTM memory configuration as part of perf data Athira Rajeev
2026-07-01  9:02   ` sashiko-bot
2026-07-01  8:38 ` [PATCH 4/5] docs: ABI: sysfs-bus-event_source-devices-htm: Document sysfs event format entries for htm pmu Athira Rajeev
2026-07-01  8:38 ` [PATCH 5/5] powerpc/perf/htm: Add documentation for Hardware Trace Macro PMU Athira Rajeev

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=20260701085058.DEC851F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=atrajeev@linux.ibm.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox