Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Leo Yan" <leo.yan@arm.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v2 5/7] perf: Assemble split thread maps in script and stat report
Date: Fri, 04 Dec 2026 12:39:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-249819@kernel.org> (raw)
In-Reply-To: <20261204-perf_fix_large_threads-v2-5-a6c077f3037d@arm.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf: integer overflow in xyarray__new() with split thread
  maps
--

commit 5da116763d7a7d4ff839c7c950a57030bfbeb322
Author: Leo Yan <leo.yan@arm.com>

perf: Assemble split thread maps in script and stat report

This commit adds thread_map__append_event() to assemble split thread map
records in perf script and perf stat report, keeping the thread map
private until the final record arrives before publishing it to the evlist
and allocating counter stats.

> diff --git a/tools/perf/util/thread_map.c b/tools/perf/util/thread_map.c
> index 918ab31802320..4b3e0cd022fb7 100644
> --- a/tools/perf/util/thread_map.c
> +++ b/tools/perf/util/thread_map.c
> @@ -336,28 +336,57 @@ void thread_map__read_comms(struct perf_thread_map *threads)
>  		comm_init(threads, i);
>  }
>  
> -static void thread_map__copy_event(struct perf_thread_map *threads,
> -				   struct perf_record_thread_map *event)
> +int thread_map__append_event(struct perf_thread_map **map,
> +			    const struct perf_record_thread_map *event)
>  {
> -	unsigned i;
> +	struct perf_thread_map *threads = *map;
> +	bool new_map = !threads;
> +	u64 old_nr = threads ? threads->nr : 0, nr, i;
>  
> -	threads->nr = (int) event->nr;
> +	/*
> +	 * Trust event->nr to describe chunks of a valid thread map, whose total
> +	 * count and allocation size are representable without overflow.
> +	 */
> +	nr = old_nr + event->nr;

[Severity: Medium]
Can unbounded thread counts across split PERF_RECORD_THREAD_MAP records
overflow the allocation size in xyarray__new()?

Before this patch, a single PERF_RECORD_THREAD_MAP record held at most
2,729 threads, keeping xyarray__new() below 2^32 bytes even at 32,768
CPUs.  Split records remove that ceiling, so set_maps() ->
evlist__alloc_stats() -> perf_counts__new() -> xyarray__new() can overflow:

tools/lib/perf/xyarray.c:xyarray__new() {
	size_t row_size = ylen * entry_size;
	struct xyarray *xy = zalloc(sizeof(*xy) + xlen * row_size);

	if (xy != NULL) {
		xy->entry_size = entry_size;
		xy->row_size   = row_size;
		xy->entries    = xlen * ylen;
		xy->max_x      = xlen;
		xy->max_y      = ylen;
	}
}

On 32-bit builds, xlen * row_size wraps size_t at 3,277 threads across
two records (with 32,768 CPUs and 40-byte struct perf_counts_values),
producing an undersized buffer while max_x and max_y stay large.

Subsequent PERF_RECORD_STAT events then pass xyarray__entry() bounds
checks in perf_event__process_stat_event() and write out of bounds on
the heap.

On 64-bit builds, xlen * ylen overflows signed 32-bit int at 65,536
threads (with 32,768 CPUs), sign-extending xy->entries to a huge size_t
in xyarray__reset().

Adding overflow checks in xyarray__new() would protect both paths.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-perf_fix_large_threads-v2-0-a6c077f3037d@arm.com?part=5

  reply	other threads:[~2026-12-04 12:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 11:23 [PATCH v2 0/7] perf: Support thread maps larger than one record Leo Yan
2026-12-04 11:23 ` [PATCH v2 1/7] perf stat: Release aggregation maps on report exit Leo Yan
2026-12-04 11:23 ` [PATCH v2 2/7] perf stat: Drop CPU and thread map references " Leo Yan
2026-12-04 11:23 ` [PATCH v2 3/7] perf stat: Release the initial evlist before opening the session Leo Yan
2026-12-04 11:23 ` [PATCH v2 4/7] perf synthetic-events: Split oversized thread maps into records Leo Yan
2026-12-04 12:15   ` sashiko-bot
2026-12-04 12:31     ` Leo Yan
2026-12-04 11:23 ` [PATCH v2 5/7] perf: Assemble split thread maps in script and stat report Leo Yan
2026-12-04 12:39   ` sashiko-bot [this message]
2026-12-04 11:23 ` [PATCH v2 6/7] perf test: Cover multi-record thread map synthesis Leo Yan
2026-12-04 13:22   ` sashiko-bot
2026-12-04 11:23 ` [PATCH v2 7/7] perf docs: Describe multi-record thread maps Leo Yan
2026-12-04 13:22 ` [PATCH v2 0/7] perf: Support thread maps larger than one record sashiko-bot

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-249819@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=leo.yan@arm.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