[PATCH 2/2] perf/x86: Disable precise sampling for PERF_SAMPLE_STACK_USER
From: Dapeng Mi <hidden>
Date: 2026-09-08 07:58:16
Also in:
lkml
Subsystem:
performance events subsystem, the rest, x86 architecture (32-bit and 64-bit) · Maintainers:
Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Linus Torvalds, Thomas Gleixner, Borislav Petkov, Dave Hansen
PERF_SAMPLE_STACK_USER needs to return the user stack and user registers to user space when the PMI exits. Since the skid from the PEBS/IBS sample and PMI delivery, the PEBS/IBS register snapshot (especially IP/SP/BP) can diverge from the user stack at PMI return. That mismatch breaks DWARF unwinding. Precise sampling provides no benefit in this case, so disable PEBS/IBS precise sampling and allow only PMI-based sampling when PERF_SAMPLE_STACK_USER is requested. Reported-by: Gennady Kupava <redacted> Closes: https://lore.kernel.org/all/CAPu-DQqF0aF6=GS8Z6KKWeeX_V5LiXeKU_rJQZC+uGg8zuTPNw@mail.gmail.com/ (local) Cc: Ravi Bangoria <redacted> Fixes: c5ebcedb566e ("perf: Add ability to attach user stack dump to sample") Signed-off-by: Dapeng Mi <redacted> --- arch/x86/events/amd/ibs.c | 3 +++ arch/x86/events/core.c | 3 +++ arch/x86/events/perf_event.h | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+)
diff --git a/arch/x86/events/amd/ibs.c b/arch/x86/events/amd/ibs.c
index 3531f9c23b8c..c67b659d64e2 100644
--- a/arch/x86/events/amd/ibs.c
+++ b/arch/x86/events/amd/ibs.c@@ -327,6 +327,9 @@ static int perf_ibs_init(struct perf_event *event) if (has_branch_stack(event)) return -EOPNOTSUPP; + if (!x86_pmu_allow_sample_user_stack(event, true)) + return -EINVAL; + /* handle exclude_{user,kernel} in the IRQ handler */ if (event->attr.exclude_host || event->attr.exclude_guest || event->attr.exclude_idle)
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 8b3ea0adb965..ceb5038d7565 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c@@ -594,6 +594,9 @@ int x86_pmu_hw_config(struct perf_event *event) /* There's no sense in having PEBS for non sampling events: */ if (!is_sampling_event(event)) return -EINVAL; + + if (!x86_pmu_allow_sample_user_stack(event, false)) + return -EINVAL; } /* * check that PEBS LBR correction does not conflict with
diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h
index 71ed5b2acea2..9fbcf2bcef51 100644
--- a/arch/x86/events/perf_event.h
+++ b/arch/x86/events/perf_event.h@@ -1274,6 +1274,25 @@ static inline bool is_counter_pair(struct hw_perf_event *hwc) return hwc->flags & PERF_X86_EVENT_PAIR; } +static inline bool +x86_pmu_allow_sample_user_stack(struct perf_event *event, bool ibs) +{ + if (!(event->attr.sample_type & PERF_SAMPLE_STACK_USER)) + return true; + + /* + * PERF_SAMPLE_STACK_USER needs to return the user stack and + * user registers to user space when the PMI exits. Since the skid + * from the PEBS/IBS sample and PMI delivery, the PEBS/IBS register + * snapshot (especially IP/SP/BP) can diverge from the user stack + * at PMI return. That mismatch breaks DWARF unwinding. + * + * Therefore, disable precise sampling for PERF_SAMPLE_STACK_USER + * and permit only PMI-based sampling for this case. + */ + return !(event->attr.precise_ip || ibs); +} + static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc, u64 enable_mask) {
--
2.34.1