Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Oancea Catalin" <john3@mailbox.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v4 1/4] perf/core: Do not account overflows of a throttled event
Date: Thu, 03 Dec 2026 12:01:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-247691@kernel.org> (raw)
In-Reply-To: <20261203113922.77972-2-john3@mailbox.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf/core: NMI TOCTOU in __perf_event_account_interrupt()
  clobbers throttle
- [Medium] perf/core: 32-bit torn u64 hwc->interrupts store in
  perf_event_throttle()
--

commit 6a3f8cdc41d352bcd4c6bff16e1484d89e2f36cd
Author: Oancea Catalin <john3@mailbox.org>

perf/core: Do not account overflows of a throttled event

This commit returns 1 early from __perf_event_account_interrupt() when
hwc->interrupts is already MAX_INTERRUPTS, so overflows on a throttled
event do not overwrite the throttle mark.

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 4ab3919ac78e9..4b47883e82af7 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -10773,6 +10773,15 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle,
>  	int ret = 0;
>  	u64 seq;
>  
> +	/*
> +	 * A throttled event can still report an overflow: from an NMI before
> +	 * perf_event_throttle() has stopped it, or one that was pending when
> +	 * it was stopped. Do not account it, that would overwrite the mark
> +	 * which the tick needs on the group leader to unthrottle the group.
> +	 */
> +	if (unlikely(hwc->interrupts == MAX_INTERRUPTS))
> +		return 1;
> +
>  	seq = __this_cpu_read(perf_throttled_seq);
>  	if (seq != hwc->interrupts_seq) {
>  		hwc->interrupts_seq = seq;

[Severity: Medium]
Can an NMI still clobber MAX_INTERRUPTS between this check and the
subsequent write to hwc->interrupts?

In a mixed group with a software event as group_leader and a hardware
sibling (supported by __do_sys_perf_event_open()), the software leader
runs __perf_event_account_interrupt() with NMIs enabled:

__perf_event_account_interrupt(sw_leader)
  if (hwc->interrupts == MAX_INTERRUPTS)  /* false */
  <NMI: hw sibling overflow>
    perf_event_throttle_group(hw_sibling)
      perf_event_throttle(sw_leader)
        sw_leader->hw.interrupts = MAX_INTERRUPTS
        sw_leader->pmu->stop(sw_leader, 0)
  hwc->interrupts++  <-- clobbers MAX_INTERRUPTS

On NMI return, hwc->interrupts is overwritten (with 1 if seq changed, or
wrapped on increment), so perf_adjust_freq_unthr_events() never sees
MAX_INTERRUPTS on the leader and the stopped group stays silent.

Updating hwc->interrupts with local_t or local64_t try_cmpxchg() would
abort the update and return 1 if an NMI sets MAX_INTERRUPTS in between.

[Severity: Medium]
On 32-bit architectures, will a torn 64-bit store of MAX_INTERRUPTS in
perf_event_throttle() bypass this check?

hwc->interrupts is a plain u64 and MAX_INTERRUPTS is ~0ULL, so the store
in perf_event_throttle() compiles to two 32-bit writes on 32-bit kernels
such as x86-32.  If the hardware leader's NMI arrives between them:

perf_event_throttle(hw_leader)
  write low 32 bits of hw.interrupts = 0xffffffff
  <NMI: hw_leader overflow>
    __perf_event_account_interrupt(hw_leader)
      hwc->interrupts == MAX_INTERRUPTS  /* false, torn value */
      hwc->interrupts = 1
  write high 32 bits of hw.interrupts = 0xffffffff  <-- 0xffffffff00000001
  hw_leader->pmu->stop(hw_leader, 0)

The NMI misses the MAX_INTERRUPTS check and resets hwc->interrupts, and
the second store leaves leader->hw.interrupts corrupted so the tick never
unthrottles the group.  If seq matches, the torn count exceeds
max_samples_per_tick and re-enters perf_event_throttle_group() from NMI.

Since max_samples_per_tick is an int, using a word-sized local_t (or
local64_t) for hwc->interrupts would prevent 32-bit tearing across NMIs.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203113922.77972-1-john3@mailbox.org?part=1

  reply	other threads:[~2026-12-03 12:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 11:39 [PATCH v4 0/4] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 1/4] perf/core: Do not account overflows of a throttled event Oancea Catalin
2026-12-03 12:01   ` sashiko-bot [this message]
2026-12-03 11:39 ` [PATCH v4 2/4] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 3/4] perf/x86: " Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 4/4] perf/x86/intel: " Oancea Catalin
2026-12-03 12:06 ` [PATCH v4 0/4] perf/x86: " 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-247691@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=john3@mailbox.org \
    --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