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 v5 8/8] perf/core: Stop an event which overflows while it is throttled
Date: Sat, 05 Dec 2026 14:24:58 +0000	[thread overview]
Message-ID: <sashiko-outbox-251625@kernel.org> (raw)
In-Reply-To: <20261205133221.3294-9-john3@mailbox.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf/core: perf_throttling guard misses non-throttle
  x86_pmu_stop() callers
- [Medium] perf/core: unthrottle-vs-NMI race still starts events with
  hw.throttled set
--

commit 4a662d074649b317ca9a70b8242cd52b4a4419b2
Author: Oancea Catalin <john3@mailbox.org>

perf/core: Stop an event which overflows while it is throttled

This commit stops a throttled event in __perf_event_account_interrupt()
when it overflows outside perf_event_throttle_group(), using a per-CPU
perf_throttling counter to avoid nesting stops during a group throttle
walk.  It also moves perf_event_unthrottle_group() before
event->pmu->start() in __perf_event_period().

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index fdc7335c7321b..9706c2ea69f4f 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c

[ ... ]

> @@ -6581,15 +6586,17 @@ static void __perf_event_period(struct perf_event *event,
>  	local64_set(&event->hw.period_left, 0);
>  
>  	if (active) {
> -		event->pmu->start(event, PERF_EF_RELOAD);
>  		/*
>  		 * Once the period is force-reset, the event starts immediately.
>  		 * But the event/group could be throttled. Unthrottle the
>  		 * event/group now to avoid the next tick trying to unthrottle
> -		 * while we already re-started the event/group.
> +		 * while we already re-started the event/group. Do that before
> +		 * the start: an event which overflows while it is throttled is
> +		 * stopped, and nothing would start it again.
>  		 */
>  		if (event->hw.throttled)
>  			perf_event_unthrottle_group(event, true);
> +		event->pmu->start(event, PERF_EF_RELOAD);
>  		perf_pmu_enable(event->pmu_ctx->pmu);
>  	}
>  }

[Severity: Medium]
Does moving perf_event_unthrottle_group() before event->pmu->start() open
the same unthrottle-vs-NMI window here, while stopping reactively on
overflow still leaves events running with hw.throttled set?

An NMI that throttles the group between clearing hw.throttled and calling
pmu->start() (in perf_event_unthrottle() or __perf_event_period()) starts
the event with hw.throttled set.  Non-sampling siblings return early at
!is_sampling_event(event) in __perf_event_overflow(), so they never reach
__perf_event_account_interrupt() and keep counting across the throttled
interval, skewing group counts.

Whenever the next tick or __perf_event_period() unthrottles the group
before an overflow (or for any non-sampling sibling),
perf_event_unthrottle() calls pmu->start() on an already-running event and
hits WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)) in
x86_pmu_start().

Closing the window at the unthrottle and start sites instead -- for
example, WRITE_ONCE(event->hw.throttled, 0) and barrier() before
pmu->start(), followed by re-checking READ_ONCE(event->hw.throttled) and
calling pmu->stop(event, 0) while perf_pmu_disable() is held -- would
avoid leaving events running while throttled.

[ ... ]

> @@ -10788,9 +10795,16 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle,
>  	 * perf_event_throttle() has stopped it, or one that was pending when
>  	 * it was stopped. Do not account it, that could throttle the group a
>  	 * second time.
> +	 *
> +	 * Nothing would throttle an event which counts with the mark set, so
> +	 * stop it here. Not inside a throttle on this CPU: that one stops the
> +	 * events it has marked itself.
>  	 */
> -	if (unlikely(hwc->throttled))
> +	if (unlikely(hwc->throttled)) {
> +		if (!__this_cpu_read(perf_throttling))
> +			event->pmu->stop(event, 0);
>  		return 1;
> +	}

[Severity: Medium]
Can this re-enter x86_pmu_stop() from an NMI when a non-throttle caller is
already stopping the event?

perf_throttling is only incremented in perf_event_throttle_group(),
leaving callers such as __perf_event_period() and event_sched_out()
unguarded.  In x86_pmu_stop(), cpuc->active_mask remains set across
x86_pmu_disable() before PERF_HES_STOPPED is set, and on AMD
amd_pmu_disable_event() explicitly waits for a pending overflow NMI there
via amd_pmu_wait_on_overflow():

__perf_event_period()
  x86_pmu_stop(event, PERF_EF_UPDATE)
    test_bit(hwc->idx, cpuc->active_mask)
    x86_pmu_disable(event)
      <NMI>
        perf_event_overflow()
          __perf_event_account_interrupt()
            /* hwc->throttled != 0, perf_throttling == 0 */
            x86_pmu_stop(event, 0)
              x86_pmu_disable(event)
              __clear_bit(hwc->idx, cpuc->active_mask)
              hwc->state |= PERF_HES_STOPPED
    __clear_bit(hwc->idx, cpuc->active_mask)
    WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED)  <-- fires

On return from the NMI, the outer x86_pmu_stop() triggers
WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED).  On Intel PEBS and BTS
events, the nested intel_pmu_disable_event() also re-enters non-reentrant
disable and drain paths.

Separately, because perf_throttling is a single per-CPU counter rather
than scoped to the group, throttling one group from a software event
suppresses this stop for an unrelated throttled group whose NMI arrives on
the same CPU during that walk.

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

  reply	other threads:[~2026-12-05 14:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 13:32 [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 1/8] perf/core: Disable the PMU of the context in __perf_event_period() Oancea Catalin
2026-12-05 13:58   ` sashiko-bot
2026-12-05 13:32 ` [PATCH v5 2/8] perf/core: Do not account overflows of a throttled event Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 3/8] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 4/8] perf/x86: " Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 5/8] perf/x86/intel: " Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 6/8] perf/core: Keep the throttle mark out of hwc->interrupts Oancea Catalin
2026-12-05 13:57   ` sashiko-bot
2026-12-05 13:32 ` [PATCH v5 7/8] perf/core: Throttle an event only once Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 8/8] perf/core: Stop an event which overflows while it is throttled Oancea Catalin
2026-12-05 14:24   ` sashiko-bot [this message]
2026-12-05 14:24 ` [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle 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-251625@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