From: Ravi Bangoria <ravi.bangoria@amd.com>
To: Jim Mattson <jmattson@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Jayapal Ashwin Nair <janair@amd.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Ian Rogers <irogers@google.com>, <x86@kernel.org>,
<linux-perf-users@vger.kernel.org>,
<linux-kernel@vger.kernel.org>,
Santosh Shukla <santosh.shukla@amd.com>,
Ananth Narayan <ananth.narayan@amd.com>,
Sandipan Das <sandipan.das@amd.com>,
Manali Shukla <manali.shukla@amd.com>,
Ravi Bangoria <ravi.bangoria@amd.com>
Subject: Re: [PATCH 3/3] perf/amd/ibs: Simplify event state handling with IBS_CAPS_DIS
Date: Sun, 18 Oct 2026 17:01:55 +0530 [thread overview]
Message-ID: <09e61c95-97bd-46c7-9d96-c3926e324c26@amd.com> (raw)
In-Reply-To: <CALMp9eRTG=uLkAWohYW0HOoS1xvNc_0brovYsfbCUS_mf4H3Kw@mail.gmail.com>
Jim,
Thanks for the detailed review.
>> The IBS_{FETCH|OP}_CTL2[Dis] bit allows IBS sampling to be stopped without
>> a RMW of IBS_{FETCH,OP}_CTL. Systems providing this bit therefore do not
>> require the IBS_STARTED and IBS_STOPPED states used to coordinate control
>> MSR updates with late NMIs.
>
> CTL2[Dis] doesn't make late NMIs go away. Hence, this patch needs a
> 50us poll. With the two-line fix in my original patch, the existing
> scheme handles late NMIs indefinitely: IBS_STOPPED remains set until a
> late NMI consumes it, even if the event has been restarted in the
> meantime. This patch trades that for a 50us window, after which a late
> NMI is unknown (which *is* fatal with unknown_nmi_panic). Moreover,
> since we still have to handle hardware without IBS_CAPS_DIS, we now
> have two state machines instead of one.
IBS_STOPPED only records that "a late NMI might exist". It can't tell
whether the disable actually completed or whether that late NMI has
already arrived. Trying to infer from software state is fundamentally
broken, since the WRMSR and a software state transition can't be made
atomic.
Given that Zen6 provides a clear hardware state machine, I believe using
it, rather than a software state machine, is the right approach.
I ran a fuzzer for several hours with my patch (+Manali's patch [1]) and
saw no Unknown NMIs on Zen6.
However, With your patch[2] (+Manali's patch), the same fuzzer continued
to produce quite a few Unknown NMIs on Zen6. One race I was able to
identify (with your patch applied) is described below.
---
o FETCH and OP both are enabled.
perf_ibs_stop() /* For OP */
{
set IBS_STOPPED
op state: ENABLED | STARTED |
STOPPING | STOPPED
OpVal is 0 in hardware
NMI due to FETCH
{
FETCH: Val=1, sample handled,
fetch re-armed
OP: STARTED is set, OpVal is 0 so
test_and_clear(IBS_STOPPED)
clears the STOPPED state
OP continues to run, it overflows,
sets OpVal=1 and a new NMI is
latched by the OP hardware
return handled=2
}
op state: ENABLED | STARTED |
STOPPING
WRMSR OpCtl2.Dis=1
leaves the valid sample in OpCtl
clear IBS_STARTED
op state: ENABLED | STOPPING
NMI (late) due to OP
{
FETCH: Val=0 -> return 0
OP: STARTED is clear and
STOPPED is already clear.
return 0 without reading
IbsOpCtl
return handled=0
Unknown NMI
}
---
[1]: https://lore.kernel.org/r/20260310060022.15120-2-manali.shukla@amd.com
[2]: https://lore.kernel.org/r/20261007234815.4026028-1-jmattson@google.com
[...]
>> +/* ->stop() for platforms with additional CTL2[Dis] bit. */
>> +static void perf_ibs_stop_dis(struct perf_event *event, int flags)
>> +{
>> + struct hw_perf_event *hwc = &event->hw;
>> + struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
>> + struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
>> + u64 config;
>> + u64 ctl2;
>> +
>> + WARN_ON_ONCE(test_bit(IBS_STOPPING, pcpu->state));
>> +
>> + set_bit(IBS_STOPPING, pcpu->state);
>
> This function needs to be reentrant. If an IBS event is a sibling in a
> group led by a core PMU event, and the leader is throttled,
> perf_event_throttle_group() will stop the IBS sibling from the core
> PMU's NMI handler:
>
> amd_pmu_v2_handle_irq() ->
> perf_event_overflow() ->
> __perf_event_overflow() ->
> __perf_event_account_interrupt() ->
> perf_event_throttle_group() ->
> perf_event_throttle() ->
> perf_ibs_stop() ->
> perf_ibs_stop_dis()
Core and IBS, both are of hardware PMU type and thus can not be
grouped together.
[...]
>> + if (in_nmi())
>> + wrmsrq(hwc->config_base, config);
>
> As we saw above, in_nmi() doesn't mean that we're in
> perf_ibs_handle_irq() for this sample. If IBS Val=1 and the IBS NMI is
> still pending, this clears Val. When the IBS NMI arrives,
> perf_ibs_handle_irq() finds CTL2[Dis]=1 and Val=0, and therefore
> returns 0, leading to an unknown NMI.
The back-to-back NMI handling logic in the x86 generic NMI code should
already handle this. No?
Thanks,
Ravi
prev parent reply other threads:[~2026-10-18 11:32 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-15 6:04 [PATCH 0/3] perf/amd/ibs: Assorted fixes Ravi Bangoria
2026-10-15 6:04 ` [PATCH 1/3] perf/amd/ibs: Reset CPU0 IBS state on syscore resume Ravi Bangoria
2026-10-15 20:01 ` Jim Mattson
2026-10-15 6:04 ` [PATCH 2/3] perf/amd/ibs: Fix kernel address leak via REGS_USER Ravi Bangoria
2026-10-15 20:42 ` Jim Mattson
2026-10-15 6:04 ` [PATCH 3/3] perf/amd/ibs: Simplify event state handling with IBS_CAPS_DIS Ravi Bangoria
2026-10-15 21:32 ` Jim Mattson
2026-10-18 11:31 ` Ravi Bangoria [this message]
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=09e61c95-97bd-46c7-9d96-c3926e324c26@amd.com \
--to=ravi.bangoria@amd.com \
--cc=acme@kernel.org \
--cc=ananth.narayan@amd.com \
--cc=irogers@google.com \
--cc=janair@amd.com \
--cc=jmattson@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=manali.shukla@amd.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=sandipan.das@amd.com \
--cc=santosh.shukla@amd.com \
--cc=x86@kernel.org \
/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
all inboxes | Powered by JetHome®