perf/amd/ibs: Assorted fixes
Subsystems (2)
v2 cover perf/amd/ibs: Assorted fixes
#1 Set IBS MSRs in syscore->resume() callback
#2 Fix kernel address leak through REGS_USER and REGS_INTR #3 Fix races by reducing SW states when IBS_CAPS_DIS is present
v1: https://lore.kernel.org/r/[email protected]v1->v2:
- Use interrupt registers when preparing REGS_USER and REGS_INTR. In addition to fixing the kernel address leak, this change also ensures GPR snapshot consistency, which resolves the DWARF unwinding issue with IBS [1].
[1] https://lore.kernel.org/r/CAPu-DQqF0aF6=GS8Z6KKWeeX_V5LiXeKU_rJQZC+uGg8zuTPNw@mail.gmail.com
Ravi Bangoria (3):
perf/amd/ibs: Reset CPU0 IBS state on syscore resume perf/amd/ibs: Fix kernel address leak via REGS_USER and REGS_INTR perf/amd/ibs: Simplify event state handling with IBS_CAPS_DIS
arch/x86/events/amd/ibs.c | 197 ++++++++++++++++++++++++++++++++----- include/linux/perf_event.h | 6 ++ kernel/events/core.c | 10 +- 3 files changed, 184 insertions(+), 29 deletions(-)
Signature · 1 line
2.55.0
PATCH v2 1/3 perf/amd/ibs: Reset CPU0 IBS state on syscore resume
During a suspend/resume cycle, all CPUs except CPU0 go through hotplug callbacks, which sets up IBS state. CPU0 bypasses this path, leaving its IBS state stale after resume. Fix this by explicitly setting CPU0's IBS state in the syscore ->resume() callback.
--- arch/x86/events/amd/ibs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/amd/ibs.c +1 -1
@@ -1948,7 +1948,7 @@ static int perf_ibs_suspend(void *data)static void perf_ibs_resume (void *data) {ibs_eilvt_setup (); -setup_APIC_ibs (); +x86_pmu_amd_ibs_starting_cpu (smp_processor_id ()); }static const struct syscore_ops perf_ibs_syscore_ops = {
Signature · 1 line
2.55.0
[tip: perf/urgent] perf/amd/ibs: Reset CPU0 IBS state on syscore resume
The following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 2c1d35bd95b13b50b43a0cb2167305dabfb54c07 Gitweb: https://git.kernel.org/tip/2c1d35bd95b13b50b43a0cb2167305dabfb54c07 Author: Ravi Bangoria <[email protected]> AuthorDate: Mon, 26 Oct 2026 04:18:35 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 06 Nov 2026 09:40:55 +01:00
perf/amd/ibs: Reset CPU0 IBS state on syscore resume
During a suspend/resume cycle, all CPUs except CPU0 go through hotplug callbacks, which sets up IBS state. CPU0 bypasses this path, leaving its IBS state stale after resume.
Fix this bug by explicitly setting CPU0's IBS state in the syscore ->resume() callback.
--- arch/x86/events/amd/ibs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/amd/ibs.c +1 -1
@@ -1948,7 +1948,7 @@ static int perf_ibs_suspend(void *data)static void perf_ibs_resume (void *data) {ibs_eilvt_setup (); -setup_APIC_ibs (); +x86_pmu_amd_ibs_starting_cpu (smp_processor_id ()); }static const struct syscore_ops perf_ibs_syscore_ops = {
PATCH v2 2/3 perf/amd/ibs: Fix kernel address leak via REGS_USER and REGS_INTR
In reply to Ravi BangoriaIBS hardware captures a sample and raises an NMI. Since the NMI can be delivered with a delay, a privilege change may occur between sample capture and NMI delivery. Because IBS only provides the sample RIP, the kernel records GPRs from the NMI pt_regs, which may reflect kernel mode if a privilege switch occurred in the interim. This can leak kernel addresses to unprivileged users through PERF_SAMPLE_REGS_USER and/or PERF_SAMPLE_REGS_INTR.
Fix this by using interrupt regs while preparing PERF_SAMPLE_REGS_USER and PERF_SAMPLE_REGS_INTR. With the change, when IBS samples a user mode instruction but the NMI is delivered in kernel mode, PERF_SAMPLE_REGS_USER will source its register state from task_pt_regs(current) and PERF_SAMPLE_REGS_INTR will be omitted from the data sent to userspace.
In addition to fixing the kernel address leak, this change also ensures GPR snapshot consistency, which resolves the DWARF unwinding issue with IBS.
--- arch/x86/events/amd/ibs.c | 50 ++++++++++++++++++++++++++++++++++++++ include/linux/perf_event.h | 6 +++++ kernel/events/core.c | 10 ++++---- 3 files changed, 61 insertions(+), 5 deletions(-)
arch/x86/events/amd/ibs.c +50 -0
@@ -1533,6 +1533,56 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) throttle =perf_event_account_interrupt (event); goto out; } + +/* +* IBS captured RIP is out of sync with NMI pt_regs due to NMI +* delivery latency. Use NMI pt_regs for PERF_SAMPLE_REGS_USER. +* +* Note: Valid user regs are required for PERF_SAMPLE_STACK_USER. +*/ +if (event->attr.sample_type & (PERF_SAMPLE_REGS_USER | +PERF_SAMPLE_STACK_USER )) { +perf_sample_regs_user (&data.regs_user, iregs); + +/* +* Don't update dyn_size if user has not requrested for +* PERF_SAMPLE_REGS_USER. +*/ +if (event->attr.sample_type &PERF_SAMPLE_REGS_USER ) { +/* regs dump ABI info */ +int size =sizeof (u64); + +if (data.regs_user.regs) { +u64 mask = event->attr.sample_regs_user; + size +=hweight64 (mask) *sizeof (u64); + } + + data.dyn_size += size; + } + + data.sample_flags |=PERF_SAMPLE_REGS_USER ; + } + +/* +* IBS captured RIP is out of sync with NMI pt_regs due to NMI +* delivery latency. Use NMI pt_regs for PERF_SAMPLE_REGS_INTR. +*/ +if (event->attr.sample_type &PERF_SAMPLE_REGS_INTR ) { +/* regs dump ABI info */ +int size =sizeof (u64); + +perf_sample_regs_intr (&data.regs_intr, iregs, + event->attr.exclude_kernel); + +if (data.regs_intr.regs) { +u64 mask = event->attr.sample_regs_intr; + size +=hweight64 (mask) *sizeof (u64); + } + + data.dyn_size += size; + data.sample_flags |=PERF_SAMPLE_REGS_INTR ; + } +/* * Prevent leaking physical addresses to unprivileged users. Skip * PERF_SAMPLE_PHYS_ADDR check since generic code prevents it for
include/linux/perf_event.h +6 -0
@@ -1791,6 +1791,12 @@ extern unsigned long perf_misc_flags(struct perf_event *event, struct pt_regs *rextern unsigned long perf_instruction_pointer (struct perf_event *event,struct pt_regs *regs); +extern void perf_sample_regs_user (struct perf_regs *regs_user, +struct pt_regs *regs); +extern void perf_sample_regs_intr (struct perf_regs *regs_intr, +struct pt_regs *regs, +bool exclude_kernel); +#ifndef perf_arch_misc_flags# define perf_arch_misc_flags (regs) \ (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
kernel/events/core.c +5 -5
@@ -7846,8 +7846,8 @@ perf_output_sample_regs(struct perf_output_handle *handle, } } -static void perf_sample_regs_user (struct perf_regs *regs_user, -struct pt_regs *regs) +void perf_sample_regs_user (struct perf_regs *regs_user, +struct pt_regs *regs) {if (user_mode (regs)) { regs_user->abi =perf_reg_abi (current);
@@ -7860,9 +7860,9 @@ static void perf_sample_regs_user(struct perf_regs *regs_user, } } -static void perf_sample_regs_intr (struct perf_regs *regs_intr, -struct pt_regs *regs, -bool exclude_kernel) +void perf_sample_regs_intr (struct perf_regs *regs_intr, +struct pt_regs *regs, +bool exclude_kernel) {/* * Hardware skid can lead to a scenario where a PMI is
Signature · 1 line
2.55.0
Re: [PATCH v2 2/3] perf/amd/ibs: Fix kernel address leak via REGS_USER and REGS_INTR
Quoted 18 lines · Ravi Bangoria
IBS hardware captures a sample and raises an NMI. Since the NMI can be delivered with a delay, a privilege change may occur between sample capture and NMI delivery. Because IBS only provides the sample RIP, the kernel records GPRs from the NMI
pt_regs, which may reflect kernel mode if a privilege switch occurred in the interim. This can leak kernel addresses to unprivileged users throughPERF_SAMPLE_REGS_USERand/orPERF_SAMPLE_REGS_INTR.Fix this by using interrupt regs while preparing
PERF_SAMPLE_REGS_USERandPERF_SAMPLE_REGS_INTR. With the change, when IBS samples a user mode instruction but the NMI is delivered in kernel mode,PERF_SAMPLE_REGS_USERwill source its register state fromtask_pt_regs(current)andPERF_SAMPLE_REGS_INTRwill be omitted from the data sent to userspace.In addition to fixing the kernel address leak, this change also ensures GPR snapshot consistency, which resolves the DWARF unwinding issue with IBS.
Quoted 5 lines
Reported-by Gennady Kupava <[email protected]>Closes https://lore.kernel.org/r/CAPu-DQqF0aF6=GS8Z6KKWeeX_V5LiXeKU_rJQZC+uGg8zuTPNw@mail.gmail.com open hereReported-by Jayapal Ashwin Nair <[email protected]>Fixes d29e744c7167 ("perf/x86: Relax privilege filter restriction on AMD IBS")Signed-off-by Ravi Bangoria <[email protected]>
Re: [PATCH v2 2/3] perf/amd/ibs: Fix kernel address leak via REGS_USER and REGS_INTR
In reply to Ravi BangoriaLGTM. Thanks.
Quoted 135 lines · Ravi Bangoria
IBS hardware captures a sample and raises an NMI. Since the NMI can be delivered with a delay, a privilege change may occur between sample capture and NMI delivery. Because IBS only provides the sample RIP, the kernel records GPRs from the NMI
pt_regs, which may reflect kernel mode if a privilege switch occurred in the interim. This can leak kernel addresses to unprivileged users throughPERF_SAMPLE_REGS_USERand/orPERF_SAMPLE_REGS_INTR.Fix this by using interrupt regs while preparing
PERF_SAMPLE_REGS_USERandPERF_SAMPLE_REGS_INTR. With the change, when IBS samples a user mode instruction but the NMI is delivered in kernel mode,PERF_SAMPLE_REGS_USERwill source its register state fromtask_pt_regs(current)andPERF_SAMPLE_REGS_INTRwill be omitted from the data sent to userspace.In addition to fixing the kernel address leak, this change also ensures GPR snapshot consistency, which resolves the DWARF unwinding issue with IBS.
Reported-by Gennady Kupava <[email protected]>Closes https://lore.kernel.org/r/CAPu-DQqF0aF6=GS8Z6KKWeeX_V5LiXeKU_rJQZC+uGg8zuTPNw@mail.gmail.com open hereReported-by Jayapal Ashwin Nair <[email protected]>Fixes d29e744c7167 ("perf/x86: Relax privilege filter restriction on AMD IBS")Signed-off-by Ravi Bangoria <[email protected]>--- arch/x86/events/amd/ibs.c | 50 ++++++++++++++++++++++++++++++++++++++ include/linux/perf_event.h | 6 +++++ kernel/events/core.c | 10 ++++---- 3 files changed, 61 insertions(+), 5 deletions(-)arch/x86/events/amd/ibs.c +50
-0@@ -1533,6 +1533,56 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) throttle =perf_event_account_interrupt (event); goto out; } + +/* +* IBS captured RIP is out of sync with NMI pt_regs due to NMI +* delivery latency. Use NMI pt_regs for PERF_SAMPLE_REGS_USER. +* +* Note: Valid user regs are required for PERF_SAMPLE_STACK_USER. +*/ +if (event->attr.sample_type & (PERF_SAMPLE_REGS_USER | +PERF_SAMPLE_STACK_USER )) { +perf_sample_regs_user (&data.regs_user, iregs); + +/* +* Don't update dyn_size if user has not requrested for +* PERF_SAMPLE_REGS_USER. +*/ +if (event->attr.sample_type &PERF_SAMPLE_REGS_USER ) { +/* regs dump ABI info */ +int size =sizeof (u64); + +if (data.regs_user.regs) { +u64 mask = event->attr.sample_regs_user; + size +=hweight64 (mask) *sizeof (u64); + } + + data.dyn_size += size; + } + + data.sample_flags |=PERF_SAMPLE_REGS_USER ; + } + +/* +* IBS captured RIP is out of sync with NMI pt_regs due to NMI +* delivery latency. Use NMI pt_regs for PERF_SAMPLE_REGS_INTR. +*/ +if (event->attr.sample_type &PERF_SAMPLE_REGS_INTR ) { +/* regs dump ABI info */ +int size =sizeof (u64); + +perf_sample_regs_intr (&data.regs_intr, iregs, + event->attr.exclude_kernel); + +if (data.regs_intr.regs) { +u64 mask = event->attr.sample_regs_intr; + size +=hweight64 (mask) *sizeof (u64); + } + + data.dyn_size += size; + data.sample_flags |=PERF_SAMPLE_REGS_INTR ; + } +/* * Prevent leaking physical addresses to unprivileged users. Skip * PERF_SAMPLE_PHYS_ADDR check since generic code prevents it for include/linux/perf_event.h +6
-0@@ -1791,6 +1791,12 @@ extern unsigned long perf_misc_flags(struct perf_event *event, struct pt_regs *rextern unsigned long perf_instruction_pointer (struct perf_event *event,struct pt_regs *regs); +extern void perf_sample_regs_user (struct perf_regs *regs_user, +struct pt_regs *regs); +extern void perf_sample_regs_intr (struct perf_regs *regs_intr, +struct pt_regs *regs, +bool exclude_kernel); +#ifndef perf_arch_misc_flags# define perf_arch_misc_flags (regs) \ (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)kernel/events/core.c +5
-5@@ -7846,8 +7846,8 @@ perf_output_sample_regs(struct perf_output_handle *handle, } } -static void perf_sample_regs_user (struct perf_regs *regs_user, -struct pt_regs *regs) +void perf_sample_regs_user (struct perf_regs *regs_user, +struct pt_regs *regs) {if (user_mode (regs)) { regs_user->abi =perf_reg_abi (current);@@ -7860,9 +7860,9 @@ static void perf_sample_regs_user(struct perf_regs *regs_user, } } -static void perf_sample_regs_intr (struct perf_regs *regs_intr, -struct pt_regs *regs, -bool exclude_kernel) +void perf_sample_regs_intr (struct perf_regs *regs_intr, +struct pt_regs *regs, +bool exclude_kernel) {/* * Hardware skid can lead to a scenario where a PMI is
Re: [PATCH v2 2/3] perf/amd/ibs: Fix kernel address leak via REGS_USER and REGS_INTR
In reply to Ravi BangoriaQuoted 19 lines · Ravi Bangoria
IBS hardware captures a sample and raises an NMI. Since the NMI can be delivered with a delay, a privilege change may occur between sample capture and NMI delivery. Because IBS only provides the sample RIP, the kernel records GPRs from the NMI
pt_regs, which may reflect kernel mode if a privilege switch occurred in the interim. This can leak kernel addresses to unprivileged users throughPERF_SAMPLE_REGS_USERand/orPERF_SAMPLE_REGS_INTR.Fix this by using interrupt regs while preparing
PERF_SAMPLE_REGS_USERandPERF_SAMPLE_REGS_INTR. With the change, when IBS samples a user mode instruction but the NMI is delivered in kernel mode,PERF_SAMPLE_REGS_USERwill source its register state fromtask_pt_regs(current)andPERF_SAMPLE_REGS_INTRwill be omitted from the data sent to userspace.In addition to fixing the kernel address leak, this change also ensures GPR snapshot consistency, which resolves the DWARF unwinding issue with IBS.
Quoted 5 lines
Reported-by Gennady Kupava <[email protected]>Closes https://lore.kernel.org/r/CAPu-DQqF0aF6=GS8Z6KKWeeX_V5LiXeKU_rJQZC+uGg8zuTPNw@mail.gmail.com open hereReported-by Jayapal Ashwin Nair <[email protected]>Fixes d29e744c7167 ("perf/x86: Relax privilege filter restriction on AMD IBS")Signed-off-by Ravi Bangoria <[email protected]>
[tip: perf/urgent] perf/amd/ibs: Fix kernel address leak via REGS_USER and REGS_INTR
In reply to Ravi BangoriaThe following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: b61c12527223423ab66891b2e8131da0d66537c4 Gitweb: https://git.kernel.org/tip/b61c12527223423ab66891b2e8131da0d66537c4 Author: Ravi Bangoria <[email protected]> AuthorDate: Mon, 26 Oct 2026 04:18:36 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 06 Nov 2026 09:45:25 +01:00
perf/amd/ibs: Fix kernel address leak via REGS_USER and REGS_INTR
IBS hardware captures a sample and raises an NMI. Since the NMI can be delivered with a delay, a privilege change may occur between sample capture and NMI delivery. Because IBS only provides the sample RIP, the kernel records GPRs from the NMI pt_regs, which may reflect kernel mode if a privilege switch occurred in the interim. This can leak kernel addresses to unprivileged users through PERF_SAMPLE_REGS_USER and/or PERF_SAMPLE_REGS_INTR.
Fix this by using interrupt regs while preparing PERF_SAMPLE_REGS_USER and PERF_SAMPLE_REGS_INTR. With the change, when IBS samples a user mode instruction but the NMI is delivered in kernel mode, PERF_SAMPLE_REGS_USER will source its register state from task_pt_regs(current) and PERF_SAMPLE_REGS_INTR will be omitted from the data sent to userspace.
In addition to fixing the kernel address leak, this change also ensures GPR snapshot consistency, which resolves the DWARF unwinding issue with IBS.
--- arch/x86/events/amd/ibs.c | 50 +++++++++++++++++++++++++++++++++++++- include/linux/perf_event.h | 6 ++++- kernel/events/core.c | 10 +++---- 3 files changed, 61 insertions(+), 5 deletions(-)
arch/x86/events/amd/ibs.c +50 -0
@@ -1533,6 +1533,56 @@ fail: throttle =perf_event_account_interrupt (event); goto out; } + +/* +* IBS captured RIP is out of sync with NMI pt_regs due to NMI +* delivery latency. Use NMI pt_regs for PERF_SAMPLE_REGS_USER. +* +* Note: Valid user regs are required for PERF_SAMPLE_STACK_USER. +*/ +if (event->attr.sample_type & (PERF_SAMPLE_REGS_USER | +PERF_SAMPLE_STACK_USER )) { +perf_sample_regs_user (&data.regs_user, iregs); + +/* +* Don't update dyn_size if user has not requested for +* PERF_SAMPLE_REGS_USER. +*/ +if (event->attr.sample_type &PERF_SAMPLE_REGS_USER ) { +/* regs dump ABI info */ +int size =sizeof (u64); + +if (data.regs_user.regs) { +u64 mask = event->attr.sample_regs_user; + size +=hweight64 (mask) *sizeof (u64); + } + + data.dyn_size += size; + } + + data.sample_flags |=PERF_SAMPLE_REGS_USER ; + } + +/* +* IBS captured RIP is out of sync with NMI pt_regs due to NMI +* delivery latency. Use NMI pt_regs for PERF_SAMPLE_REGS_INTR. +*/ +if (event->attr.sample_type &PERF_SAMPLE_REGS_INTR ) { +/* regs dump ABI info */ +int size =sizeof (u64); + +perf_sample_regs_intr (&data.regs_intr, iregs, + event->attr.exclude_kernel); + +if (data.regs_intr.regs) { +u64 mask = event->attr.sample_regs_intr; + size +=hweight64 (mask) *sizeof (u64); + } + + data.dyn_size += size; + data.sample_flags |=PERF_SAMPLE_REGS_INTR ; + } +/* * Prevent leaking physical addresses to unprivileged users. Skip * PERF_SAMPLE_PHYS_ADDR check since generic code prevents it for
include/linux/perf_event.h +6 -0
@@ -1814,6 +1814,12 @@ extern unsigned long perf_misc_flags(struct perf_event *event, struct pt_regs *rextern unsigned long perf_instruction_pointer (struct perf_event *event,struct pt_regs *regs); +extern void perf_sample_regs_user (struct perf_regs *regs_user, +struct pt_regs *regs); +extern void perf_sample_regs_intr (struct perf_regs *regs_intr, +struct pt_regs *regs, +bool exclude_kernel); +#ifndef perf_arch_misc_flags# define perf_arch_misc_flags (regs) \ (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
kernel/events/core.c +5 -5
@@ -7919,8 +7919,8 @@ perf_output_sample_simd_regs(struct perf_output_handle *handle, } } -static void perf_sample_regs_user (struct perf_regs *regs_user, -struct pt_regs *regs) +void perf_sample_regs_user (struct perf_regs *regs_user, +struct pt_regs *regs) {if (user_mode (regs)) { regs_user->abi =perf_reg_abi (current);
@@ -7933,9 +7933,9 @@ static void perf_sample_regs_user(struct perf_regs *regs_user, } } -static void perf_sample_regs_intr (struct perf_regs *regs_intr, -struct pt_regs *regs, -bool exclude_kernel) +void perf_sample_regs_intr (struct perf_regs *regs_intr, +struct pt_regs *regs, +bool exclude_kernel) {/* * Hardware skid can lead to a scenario where a PMI is
PATCH v3 3/3 perf/amd/ibs: Simplify event state handling with IBS_CAPS_DIS
In reply to Ravi BangoriaThe 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.
Add a separate stop path for these systems. Preserve Val while disabling the event so that a delayed NMI can be identified and consumed, and wait briefly for such an NMI before restarting the event.
Leave the existing handling unchanged on systems without IBS_CAPS_DIS.
--- arch/x86/events/amd/ibs.c | 145 ++++++++++++++++++++++++++++++++------ 1 file changed, 122 insertions(+), 23 deletions(-)
arch/x86/events/amd/ibs.c +122 -23
@@ -28,6 +28,7 @@ static u32 ibs_caps;#include <linux/hardirq.h> #include <asm/nmi.h> +#include <asm/delay.h> #include <asm/amd/ibs.h> /* attr.config2 */
@@ -73,7 +74,12 @@ static u32 ibs_caps;* * XXX: we could probably be using !atomic bitops for all this. */ - +/* +* NOTE: IBS_CAPS_DIS eliminates the RMW race on the IBS_{FETCH|OP}_CTL, making +* IBS_STARTED and IBS_STOPPED states unnecessary. We use only IBS_ENABLED and +* IBS_STOPPING states when IBS_CAPS_DIS is present. Semantics of IBS_STOPPING +* changes a bit as well. +*/ enum ibs_states {IBS_ENABLED =0 ,IBS_STARTED =1 ,
@@ -557,7 +563,9 @@ static void perf_ibs_start(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); +unsigned int delay =50 ;u64 period, config =0 ; +u64 ctl;if (WARN_ON_ONCE (!(hwc->state &PERF_HES_STOPPED )))return ;
@@ -575,23 +583,87 @@ static void perf_ibs_start(struct perf_event *event, int flags) } config |= period >>4 ; -/* -* Reset the IBS_{FETCH|OP}_CTL MSR before updating pcpu->state. -* Doing so prevents a race condition in which an NMI due to other -* source might accidentally activate the event before we enable -* it ourselves. -*/ -perf_ibs_disable_event (perf_ibs, hwc,0 ); +if (ibs_caps &IBS_CAPS_DIS ) { +/* +* IBS HW SW +* +* o Capture sample o ->stop() +* o Set VAL bit CTL2[Dis] = 1 +* o Raise NMI --. o ... +* \ o ->start() +* \ wrmsr(IBS_{FETCH|OP}_CTL, new value); +* \ (wrmsr clears the VAL bit) +* `--> o Receives delayed NMI +* (Unknown NMI since VAL bit is cleared) +* +* Handle this with induced delay. +*/ +rdmsrq (hwc->config_base, ctl); +while ((ctl & perf_ibs->valid_mask) && delay--) { +udelay (1 ); +rdmsrq (hwc->config_base, ctl); + } + }else { +/* +* Reset the IBS_{FETCH|OP}_CTL MSR before updating pcpu->state. +* Doing so prevents a race condition in which an NMI due to +* other source might accidentally activate the event before we +* enable it ourselves. +*/ +perf_ibs_disable_event (perf_ibs, hwc,0 ); +/* +* Set STARTED before enabling the hardware, such that a +* subsequent NMI must observe it. +*/ +set_bit (IBS_STARTED , pcpu->state); +clear_bit (IBS_STOPPING , pcpu->state); + } +perf_ibs_enable_event (perf_ibs, hwc, config); + +perf_event_update_userpage (event); +} + +/* ->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); + +rdmsrq (hwc->extra_reg.reg, ctl2); +if (ctl2 & perf_ibs->disable_mask) + goto reset_state; + +wrmsrq (hwc->extra_reg.reg, perf_ibs->disable_mask); + +WARN_ON_ONCE (hwc->state &PERF_HES_STOPPED ); + hwc->state |=PERF_HES_STOPPED ; + +if (hwc->state &PERF_HES_UPTODATE ) + goto reset_state; + +rdmsrq (hwc->config_base, config); + + config &= ~perf_ibs->valid_mask;/* -* Set STARTED before enabling the hardware, such that a subsequent NMI -* must observe it. +* Clear the Val bit otherwise ->start() will unnecessarily +* wait for 50us. */ -set_bit (IBS_STARTED , pcpu->state); -clear_bit (IBS_STOPPING , pcpu->state); -perf_ibs_enable_event (perf_ibs, hwc, config); +if (in_nmi ()) +wrmsrq (hwc->config_base, config); -perf_event_update_userpage (event); +perf_ibs_event_update (perf_ibs, event, &config); + hwc->state |=PERF_HES_UPTODATE ; + +reset_state: +clear_bit (IBS_STOPPING , pcpu->state); }static void perf_ibs_stop (struct perf_event *event,int flags)
@@ -602,6 +674,9 @@ static void perf_ibs_stop(struct perf_event *event, int flags)u64 config;int stopping; +if (ibs_caps &IBS_CAPS_DIS ) +return perf_ibs_stop_dis (event, flags); +if (test_and_set_bit (IBS_STOPPING , pcpu->state))return ;
@@ -1416,19 +1491,43 @@ 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 ; +u64 ctl2, ctl; -if (!test_bit (IBS_STARTED , pcpu->state)) { -fail: +if (ibs_caps &IBS_CAPS_DIS ) {/* -* Catch spurious interrupts after stopping IBS: After -* disabling IBS there could be still incoming NMIs -* with samples that even have the valid bit cleared. -* Mark all this NMIs as handled. +* ->stop() { +* set_bit(IBS_STOPPING); +* <-- NMI (race 1) +* CTL2[Dis]=1; +* ... <-- NMI (race 2) +* } */ -if (test_and_clear_bit (IBS_STOPPED , pcpu->state)) -return 1 ; +rdmsrq (perf_ibs->msr2, ctl2); +if (test_bit (IBS_STOPPING , pcpu->state)/* race 1 */ || + (ctl2 & perf_ibs->disable_mask)/* race 2 */ ) { +rdmsrq (perf_ibs->msr, ctl); +if (ctl & perf_ibs->valid_mask) { + ctl &= ~(perf_ibs->enable_mask | + perf_ibs->valid_mask); +wrmsrq (perf_ibs->msr, ctl); +return 1 ; + } +return 0 ; + } + }else { +if (!test_bit (IBS_STARTED , pcpu->state)) { +fail: +/* +* Catch spurious interrupts after stopping IBS: After +* disabling IBS there could be still incoming NMIs +* with samples that even have the valid bit cleared. +* Mark all this NMIs as handled. +*/ +if (test_and_clear_bit (IBS_STOPPED , pcpu->state)) +return 1 ; -return 0 ; +return 0 ; + } }if (WARN_ON_ONCE (!event))
Signature · 1 line
2.55.0
[tip: perf/urgent] perf/amd/ibs: Simplify event state handling with IBS_CAPS_DIS
The following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 927f9c5cdadbc4ac6973bcd1690107fcb392612c Gitweb: https://git.kernel.org/tip/927f9c5cdadbc4ac6973bcd1690107fcb392612c Author: Ravi Bangoria <[email protected]> AuthorDate: Mon, 26 Oct 2026 04:18:37 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 06 Nov 2026 09:50:21 +01:00
perf/amd/ibs: Simplify event state handling with IBS_CAPS_DIS
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.
Add a separate stop path for these systems. Preserve the Val bit while disabling the event so that a delayed NMI can be identified and consumed, and wait briefly for such an NMI before restarting the event.
Leave the existing handling unchanged on systems without IBS_CAPS_DIS.
--- arch/x86/events/amd/ibs.c | 145 +++++++++++++++++++++++++++++++------ 1 file changed, 122 insertions(+), 23 deletions(-)
arch/x86/events/amd/ibs.c +122 -23
@@ -28,6 +28,7 @@ static u32 ibs_caps;#include <linux/hardirq.h> #include <asm/nmi.h> +#include <asm/delay.h> #include <asm/amd/ibs.h> /* attr.config2 */
@@ -73,7 +74,12 @@ static u32 ibs_caps;* * XXX: we could probably be using !atomic bitops for all this. */ - +/* +* NOTE: IBS_CAPS_DIS eliminates the RMW race on the IBS_{FETCH|OP}_CTL, making +* IBS_STARTED and IBS_STOPPED states unnecessary. We use only IBS_ENABLED and +* IBS_STOPPING states when IBS_CAPS_DIS is present. Semantics of IBS_STOPPING +* changes a bit as well. +*/ enum ibs_states {IBS_ENABLED =0 ,IBS_STARTED =1 ,
@@ -557,7 +563,9 @@ static void perf_ibs_start(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); +unsigned int delay =50 ;u64 period, config =0 ; +u64 ctl;if (WARN_ON_ONCE (!(hwc->state &PERF_HES_STOPPED )))return ;
@@ -575,23 +583,87 @@ static void perf_ibs_start(struct perf_event *event, int flags) } config |= period >>4 ; -/* -* Reset the IBS_{FETCH|OP}_CTL MSR before updating pcpu->state. -* Doing so prevents a race condition in which an NMI due to other -* source might accidentally activate the event before we enable -* it ourselves. -*/ -perf_ibs_disable_event (perf_ibs, hwc,0 ); +if (ibs_caps &IBS_CAPS_DIS ) { +/* +* IBS HW SW +* +* o Capture sample o ->stop() +* o Set VAL bit CTL2[Dis] = 1 +* o Raise NMI --. o ... +* \ o ->start() +* \ wrmsr(IBS_{FETCH|OP}_CTL, new value); +* \ (wrmsr clears the VAL bit) +* `--> o Receives delayed NMI +* (Unknown NMI since VAL bit is cleared) +* +* Handle this with induced delay. +*/ +rdmsrq (hwc->config_base, ctl); +while ((ctl & perf_ibs->valid_mask) && delay--) { +udelay (1 ); +rdmsrq (hwc->config_base, ctl); + } + }else { +/* +* Reset the IBS_{FETCH|OP}_CTL MSR before updating pcpu->state. +* Doing so prevents a race condition in which an NMI due to +* other source might accidentally activate the event before we +* enable it ourselves. +*/ +perf_ibs_disable_event (perf_ibs, hwc,0 ); +/* +* Set STARTED before enabling the hardware, such that a +* subsequent NMI must observe it. +*/ +set_bit (IBS_STARTED , pcpu->state); +clear_bit (IBS_STOPPING , pcpu->state); + } +perf_ibs_enable_event (perf_ibs, hwc, config); + +perf_event_update_userpage (event); +} + +/* ->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); + +rdmsrq (hwc->extra_reg.reg, ctl2); +if (ctl2 & perf_ibs->disable_mask) + goto reset_state; + +wrmsrq (hwc->extra_reg.reg, perf_ibs->disable_mask); + +WARN_ON_ONCE (hwc->state &PERF_HES_STOPPED ); + hwc->state |=PERF_HES_STOPPED ; + +if (hwc->state &PERF_HES_UPTODATE ) + goto reset_state; + +rdmsrq (hwc->config_base, config); + + config &= ~perf_ibs->valid_mask;/* -* Set STARTED before enabling the hardware, such that a subsequent NMI -* must observe it. +* Clear the Val bit otherwise ->start() will unnecessarily +* wait for 50us. */ -set_bit (IBS_STARTED , pcpu->state); -clear_bit (IBS_STOPPING , pcpu->state); -perf_ibs_enable_event (perf_ibs, hwc, config); +if (in_nmi ()) +wrmsrq (hwc->config_base, config); -perf_event_update_userpage (event); +perf_ibs_event_update (perf_ibs, event, &config); + hwc->state |=PERF_HES_UPTODATE ; + +reset_state: +clear_bit (IBS_STOPPING , pcpu->state); }static void perf_ibs_stop (struct perf_event *event,int flags)
@@ -602,6 +674,9 @@ static void perf_ibs_stop(struct perf_event *event, int flags)u64 config;int stopping; +if (ibs_caps &IBS_CAPS_DIS ) +return perf_ibs_stop_dis (event, flags); +if (test_and_set_bit (IBS_STOPPING , pcpu->state))return ;
@@ -1416,19 +1491,43 @@ 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 ; +u64 ctl2, ctl; -if (!test_bit (IBS_STARTED , pcpu->state)) { -fail: +if (ibs_caps &IBS_CAPS_DIS ) {/* -* Catch spurious interrupts after stopping IBS: After -* disabling IBS there could be still incoming NMIs -* with samples that even have the valid bit cleared. -* Mark all this NMIs as handled. +* ->stop() { +* set_bit(IBS_STOPPING); +* <-- NMI (race 1) +* CTL2[Dis]=1; +* ... <-- NMI (race 2) +* } */ -if (test_and_clear_bit (IBS_STOPPED , pcpu->state)) -return 1 ; +rdmsrq (perf_ibs->msr2, ctl2); +if (test_bit (IBS_STOPPING , pcpu->state)/* race 1 */ || + (ctl2 & perf_ibs->disable_mask)/* race 2 */ ) { +rdmsrq (perf_ibs->msr, ctl); +if (ctl & perf_ibs->valid_mask) { + ctl &= ~(perf_ibs->enable_mask | + perf_ibs->valid_mask); +wrmsrq (perf_ibs->msr, ctl); +return 1 ; + } +return 0 ; + } + }else { +if (!test_bit (IBS_STARTED , pcpu->state)) { +fail: +/* +* Catch spurious interrupts after stopping IBS: After +* disabling IBS there could be still incoming NMIs +* with samples that even have the valid bit cleared. +* Mark all this NMIs as handled. +*/ +if (test_and_clear_bit (IBS_STOPPED , pcpu->state)) +return 1 ; -return 0 ; +return 0 ; + } }if (WARN_ON_ONCE (!event))