Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Matt Turner" <mattst88@gmail.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 5/5] alpha/perf: optionally randomize the ProfileMe sampling interval
Date: Wed, 28 Oct 2026 18:21:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-188275@kernel.org> (raw)
In-Reply-To: <20261028-alpha-profileme-v1-5-d44b52bf2500@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] alpha/perf: 64-bit overflow in alpha_perf_event_set_period() jitter math
--

commit f5fe32a707f112845a6641dd8d8b1fcd7a1ddb70
Author: Matt Turner <mattst88@gmail.com>

alpha/perf: optionally randomize the ProfileMe sampling interval

This commit adds a randomize configuration bit for Alpha ProfileMe raw
perf events to vary each sampling interval by up to 1/16 of the period in
either direction using a per-CPU prandom state.

> diff --git a/arch/alpha/kernel/perf_event.c b/arch/alpha/kernel/perf_event.c
> index 06c9afeb7c05e..da8262c74d043 100644
> --- a/arch/alpha/kernel/perf_event.c
> +++ b/arch/alpha/kernel/perf_event.c

[ ... ]

> @@ -360,6 +368,20 @@ static int alpha_perf_event_set_period(struct perf_event *event,
>  		ret = 1;
>  	}
>  
> +	/*
> +	 * Vary each new interval by up to 1/16 of the period either way, so
> +	 * the samples cannot phase-lock with a loop whose length divides the
> +	 * period. The count stays exact because prev_count records the value
> +	 * actually written.
> +	 */
> +	if (ret && (hwc->flags & ALPHA_PERF_FLAG_RANDOMIZE) && period >= 8) {
> +		long jitter = period / 8;
> +		u32 r = prandom_u32_state(this_cpu_ptr(&alpha_perf_rnd));
> +
> +		left += (long)((u64)r * jitter >> 32) - jitter / 2;
> +		local64_set(&hwc->period_left, left);
> +	}

[Severity: Medium]
Can the multiplication (u64)r * jitter overflow 64 bits in
alpha_perf_event_set_period() in arch/alpha/kernel/perf_event.c when a user
specifies a large 64-bit sample_period?

In __perf_event_open() in kernel/events/core.c, userspace can supply any
63-bit attr.sample_period:

	if (attr.freq) {
		if (attr.sample_freq > sysctl_perf_event_sample_rate)
			return -EINVAL;
	} else {
		if (attr.sample_period & (1ULL << 63))
			return -EINVAL;
	}

When hwc->sample_period is at least 1UL << 35, jitter (period / 8) is at
least 1UL << 32, so (u64)r * jitter overflows 64-bit arithmetic and wraps.
Because the 64-bit product is then shifted right by 32, the positive term
is bounded to U32_MAX while jitter / 2 remains a full 64-bit value, causing
every interval to be systematically shortened by roughly period / 16 rather
than varying symmetrically within [-period / 16, +period / 16). In addition,
if sample_period is near LONG_MAX, adding a positive 1/16 offset to left can
overflow signed long into a negative period_left.

Would using mul_u64_u32_shr(jitter, r, 32) avoid the multiplication overflow?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261028-alpha-profileme-v1-0-d44b52bf2500@gmail.com?part=5

  reply	other threads:[~2026-10-28 18:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 18:09 [PATCH 0/5] alpha/perf: add ProfileMe sampling Matt Turner
2026-10-28 18:09 ` [PATCH 1/5] alpha/perf: pass PERFMON_CMD_LOGGING_ALL to the logging options call Matt Turner
2026-10-28 18:12   ` sashiko-bot
2026-10-28 18:09 ` [PATCH 2/5] alpha/perf: add sysfs format and event attributes Matt Turner
2026-10-28 18:13   ` sashiko-bot
2026-10-28 18:09 ` [PATCH 3/5] alpha/perf: implement ProfileMe sampling Matt Turner
2026-10-28 18:28   ` sashiko-bot
2026-10-29 15:18   ` Magnus Lindholm
2026-10-28 18:09 ` [PATCH 4/5] alpha/perf: attach ProfileMe status as PERF_SAMPLE_RAW data Matt Turner
2026-10-28 18:26   ` sashiko-bot
2026-10-28 18:09 ` [PATCH 5/5] alpha/perf: optionally randomize the ProfileMe sampling interval Matt Turner
2026-10-28 18:21   ` sashiko-bot [this message]
2026-10-29 15:01   ` Magnus Lindholm
2026-10-29  9:11 ` [PATCH 0/5] alpha/perf: add ProfileMe sampling Peter Zijlstra
2026-10-29 15:34   ` Matt Turner

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-188275@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mattst88@gmail.com \
    --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