Re: [PATCH 2/2] perf/x86: Disable precise sampling for PERF_SAMPLE_STACK_USER
From: Mi, Dapeng
Date: Tue Sep 08 2026 - 04:56:03 EST
The patch is only validated on Intel platforms. @Ravi may help to validate
on AMD platforms. Thanks.
On 9/8/2026 3:51 PM, Dapeng Mi wrote:
> 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 <gennady.kupava@xxxxxxxxx>
> Closes: https://lore.kernel.org/all/CAPu-DQqF0aF6=GS8Z6KKWeeX_V5LiXeKU_rJQZC+uGg8zuTPNw@xxxxxxxxxxxxxx/
> Cc: Ravi Bangoria <ravi.bangoria@xxxxxxx>
> Fixes: c5ebcedb566e ("perf: Add ability to attach user stack dump to sample")
> Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
> ---
> 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)
> {