[RFC PATCH 04/26] perf/x86: Use irqsave guard in branch stack snapshotting callbacks

From: Yosry Ahmed

Date: Mon Nov 23 2026 - 19:23:35 EST


In preparation for adding an early return to these functions, use
guard(irqsave)() instead of local_irq_{save,restore}(), which also drops
the local flags variable. IRQs are no longer restored in
__intel_pmu_snapshot_branch_stack(), but in its callers.

The guard saves IRQs at the same point local_irq_save() did and restores
them at scope exit, after the PMU and LBR are re-enabled, so the
branch-free window before the LBR is frozen is unchanged.

No functional change intended.

Cc: Andrii Nakryiko <andrii@xxxxxxxxxx>
Cc: Song Liu <song@xxxxxxxxxx>
Cc: bpf@xxxxxxxxxxxxxxx
Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
arch/x86/events/amd/core.c | 4 +---
arch/x86/events/intel/core.c | 15 +++++----------
2 files changed, 6 insertions(+), 13 deletions(-)

diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c
index 49b6b8fce5664..79a1c5f91b3af 100644
--- a/arch/x86/events/amd/core.c
+++ b/arch/x86/events/amd/core.c
@@ -931,13 +931,12 @@ static int amd_pmu_handle_irq(struct pt_regs *regs)
static int amd_pmu_v2_snapshot_branch_stack(struct perf_branch_entry *entries, unsigned int cnt)
{
struct cpu_hw_events *cpuc;
- unsigned long flags;

/*
* The sequence of steps to freeze LBR should be completely inlined
* and contain no branches to minimize contamination of LBR snapshot
*/
- local_irq_save(flags);
+ guard(irqsave)();
amd_pmu_core_disable_all();
__amd_pmu_lbr_disable();

@@ -948,7 +947,6 @@ static int amd_pmu_v2_snapshot_branch_stack(struct perf_branch_entry *entries, u
memcpy(entries, cpuc->lbr_entries, sizeof(struct perf_branch_entry) * cnt);

amd_pmu_v2_enable_all(0);
- local_irq_restore(flags);

return cnt;
}
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 0a34d674df590..4992cf76c7e83 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -2817,7 +2817,7 @@ static void intel_pmu_enable_all(int added)

static noinline int
__intel_pmu_snapshot_branch_stack(struct perf_branch_entry *entries,
- unsigned int cnt, unsigned long flags)
+ unsigned int cnt)
{
struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);

@@ -2826,34 +2826,29 @@ __intel_pmu_snapshot_branch_stack(struct perf_branch_entry *entries,

memcpy(entries, cpuc->lbr_entries, sizeof(struct perf_branch_entry) * cnt);
intel_pmu_enable_all(0);
- local_irq_restore(flags);
return cnt;
}

static int
intel_pmu_snapshot_branch_stack(struct perf_branch_entry *entries, unsigned int cnt)
{
- unsigned long flags;
-
/* must not have branches... */
- local_irq_save(flags);
+ guard(irqsave)();
__intel_pmu_disable_all(false); /* we don't care about BTS */
__intel_pmu_lbr_disable();
/* ... until here */
- return __intel_pmu_snapshot_branch_stack(entries, cnt, flags);
+ return __intel_pmu_snapshot_branch_stack(entries, cnt);
}

static int
intel_pmu_snapshot_arch_branch_stack(struct perf_branch_entry *entries, unsigned int cnt)
{
- unsigned long flags;
-
/* must not have branches... */
- local_irq_save(flags);
+ guard(irqsave)();
__intel_pmu_disable_all(false); /* we don't care about BTS */
__intel_pmu_arch_lbr_disable();
/* ... until here */
- return __intel_pmu_snapshot_branch_stack(entries, cnt, flags);
+ return __intel_pmu_snapshot_branch_stack(entries, cnt);
}

/*
--
2.56.1.809.ga0b61035b5-goog