From: Ravi Bangoria <ravi.bangoria@amd.com>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Dapeng Mi <dapeng1.mi@linux.intel.com>,
James Clark <james.clark@linaro.org>,
Sadasivan Shaiju <sadasivan.shaiju2@amd.com>, <x86@kernel.org>,
<linux-perf-users@vger.kernel.org>,
<linux-kernel@vger.kernel.org>,
Manali Shukla <manali.shukla@amd.com>,
Santosh Shukla <santosh.shukla@amd.com>,
Ananth Narayan <ananth.narayan@amd.com>,
Sandipan Das <sandipan.das@amd.com>,
"Stephane Eranian" <eranian@google.com>,
Ravi Bangoria <ravi.bangoria@amd.com>
Subject: Re: [PATCH v2 5/7] perf/amd/ibs: Enable RIP bit63 hardware filtering
Date: Mon, 9 Mar 2026 08:28:20 +0530 [thread overview]
Message-ID: <3b6ff125-4110-4fbd-9da9-c510b3d43106@amd.com> (raw)
In-Reply-To: <8b99652d-155a-444e-8395-d461714bf4b8@amd.com>
Hi Ian,
>> Does the bit 63 assumption hold for guest operating systems?
>
> Yes, this seems to be an issue, even with current swfilt approach. Let
> me inspect the code and get back.
All mainstream 64 bit OSes use the bit-63 set for kernel addresses and zero
for userspace addresses. This norm does not apply to 32 bit guests, but
those are rare, and profiling them with IBS would be even rarer. So, I'll
document this limitation in the perf-amd-ibs man page.
While looking at this, I found some issues in IBS. Below patch fixes it:
---
From deb6cdcbc60778b57a6eef60b2b7bd1b8e3cea74 Mon Sep 17 00:00:00 2001
From: Ravi Bangoria <ravi.bangoria@amd.com>
Date: Fri, 6 Mar 2026 04:52:00 +0000
Subject: [PATCH] perf/amd/ibs: Improve guest profiling
IBS captures the RIP but not its privilege level. Since the NMI is
delivered with delay, CPL can change between the IBS tag and NMI
delivery. Add a check to catch and discard invalid guest samples
using CPL stored in vCPU save area. This will work when there is
user/kernel CPL change in between IBS tag and NMI delivery within
the guest boundary. But it won't work when there is a guest entry
or exit in between IBS tag and NMI delivery.
When profiling a guest and the IBS RIP is valid, assign the sample
IP from the IBS-captured RIP and set PERF_SAMPLE_IP in sample_flags
so that perf_prepare_sample() do not overwrite the RIP with
perf_guest_get_ip() from the vCPU save area. This keeps the perf
sample IP consistent with IBS raw data, data_src, weight, phy_addr
etc. The privilege level in the perf "misc" field can now go out
of sync, as it is taken from the vCPU save area.
Reported-by: Ian Rogers <irogers@google.com>
Closes: https://lore.kernel.org/r/CAP-5=fV_cJskvLRZhQQXMGAcPUb_Rg_b30PDJNXzxL49JK4B5g@mail.gmail.com
Signed-off-by: Ravi Bangoria <ravi.bangoria@amd.com>
---
arch/x86/events/amd/ibs.c | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/arch/x86/events/amd/ibs.c b/arch/x86/events/amd/ibs.c
index eeb607b84dda..70408b0b1597 100644
--- a/arch/x86/events/amd/ibs.c
+++ b/arch/x86/events/amd/ibs.c
@@ -1415,6 +1415,7 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
unsigned int msr;
u64 *buf, *config, period, new_config = 0;
int br_target_idx = -1;
+ unsigned int guest_state;
if (!test_bit(IBS_STARTED, pcpu->state)) {
fail:
@@ -1526,6 +1527,42 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
regs.flags |= PERF_EFLAGS_EXACT;
}
+ guest_state = perf_guest_state();
+ if (!event->attr.exclude_guest && guest_state & PERF_GUEST_ACTIVE) {
+ /*
+ * IBS captures the RIP but not its privilege level. Since
+ * NMI arrives delayed, CPL might change in between IBS tag
+ * and the NMI delivery. Below checks can identify and filter
+ * out invalid samples when the CPL changes are within the
+ * guest boundary. However, these checks fail to handle cases
+ * where the CPU performs a guest entry or exit in between
+ * the IBS tag and the NMI delivery.
+ */
+ if (event->attr.exclude_kernel && !(guest_state & PERF_GUEST_USER)) {
+ throttle = perf_event_account_interrupt(event);
+ goto out;
+ }
+ if (event->attr.exclude_user && guest_state & PERF_GUEST_USER) {
+ throttle = perf_event_account_interrupt(event);
+ goto out;
+ }
+
+ /*
+ * Assign the IBS RIP value directly in the perf sample here
+ * to prevent perf_prepare_sample() from retrieving it from
+ * the vCPU save-area. With this, rest of the perf sample
+ * fields (raw data, data_src, weight, phy_addr, etc.) will
+ * remain in sync with sample IP. However, privilege level
+ * captured as part of perf sample "misc" field could now
+ * go out of sync since privilege level is fetched from the
+ * vCPU save area.
+ */
+ if (regs.flags & PERF_EFLAGS_EXACT) {
+ data.ip = regs.ip;
+ data.sample_flags |= PERF_SAMPLE_IP;
+ }
+ }
+
if (((ibs_caps & IBS_CAPS_BIT63_FILTER) ||
(event->attr.config2 & IBS_SW_FILTER_MASK)) &&
perf_ibs_discard_sample(perf_ibs, event, ®s, &ibs_data, br_target_idx)) {
--
2.43.0
next prev parent reply other threads:[~2026-03-09 2:58 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-16 4:25 [PATCH v2 0/7] perf/amd/ibs: Future enhancements Ravi Bangoria
2026-02-16 4:25 ` [PATCH v2 1/7] perf/amd/ibs: Define macro for ldlat mask and shift Ravi Bangoria
2026-02-28 10:56 ` [tip: perf/core] " tip-bot2 for Ravi Bangoria
2026-02-16 4:25 ` [PATCH v2 2/7] perf/amd/ibs: Add new MSRs and CPUID bits definitions Ravi Bangoria
2026-02-28 10:56 ` [tip: perf/core] " tip-bot2 for Ravi Bangoria
2026-02-16 4:25 ` [PATCH v2 3/7] perf/amd/ibs: Support IBS_{FETCH|OP}_CTL2[Dis] to eliminate RMW race Ravi Bangoria
2026-02-28 10:56 ` [tip: perf/core] " tip-bot2 for Ravi Bangoria
2026-02-28 11:01 ` Peter Zijlstra
2026-02-28 11:07 ` [tip: perf/core] perf/amd/ibs: Support IBS_{FETCH|OP}_CTL2[Dis] " tip-bot2 for Ravi Bangoria
2026-02-16 4:25 ` [PATCH v2 4/7] perf/amd/ibs: Enable fetch latency filtering Ravi Bangoria
2026-02-28 10:56 ` [tip: perf/core] " tip-bot2 for Ravi Bangoria
2026-02-28 11:07 ` tip-bot2 for Ravi Bangoria
2026-02-16 4:25 ` [PATCH v2 5/7] perf/amd/ibs: Enable RIP bit63 hardware filtering Ravi Bangoria
2026-02-24 17:47 ` Ian Rogers
2026-02-26 9:20 ` Ravi Bangoria
2026-02-26 16:53 ` Ian Rogers
2026-03-09 2:58 ` Ravi Bangoria [this message]
2026-03-09 15:57 ` Ian Rogers
2026-02-28 10:56 ` [tip: perf/core] " tip-bot2 for Ravi Bangoria
2026-02-28 11:07 ` tip-bot2 for Ravi Bangoria
2026-02-16 4:25 ` [PATCH v2 6/7] perf/amd/ibs: Enable streaming store filter Ravi Bangoria
2026-02-28 10:56 ` [tip: perf/core] " tip-bot2 for Ravi Bangoria
2026-02-28 11:07 ` tip-bot2 for Ravi Bangoria
2026-02-16 4:25 ` [PATCH v2 7/7] perf/amd/ibs: Advertise remote socket capability Ravi Bangoria
2026-02-28 10:56 ` [tip: perf/core] " tip-bot2 for Ravi Bangoria
2026-02-28 11:07 ` tip-bot2 for Ravi Bangoria
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=3b6ff125-4110-4fbd-9da9-c510b3d43106@amd.com \
--to=ravi.bangoria@amd.com \
--cc=acme@kernel.org \
--cc=ananth.narayan@amd.com \
--cc=dapeng1.mi@linux.intel.com \
--cc=eranian@google.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--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=sadasivan.shaiju2@amd.com \
--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®