Skip to content

Commit 0c7c3dc

Browse files
Li Huafeigregkh
Li Huafei
authored andcommitted
perf/x86: Fix smp_processor_id()-in-preemptible warnings
[ Upstream commit f73cefa ] The following bug was triggered on a system built with CONFIG_DEBUG_PREEMPT=y: # echo p > /proc/sysrq-trigger BUG: using smp_processor_id() in preemptible [00000000] code: sh/117 caller is perf_event_print_debug+0x1a/0x4c0 CPU: 3 UID: 0 PID: 117 Comm: sh Not tainted 6.11.0-rc1 torvalds#109 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0x4f/0x60 check_preemption_disabled+0xc8/0xd0 perf_event_print_debug+0x1a/0x4c0 __handle_sysrq+0x140/0x180 write_sysrq_trigger+0x61/0x70 proc_reg_write+0x4e/0x70 vfs_write+0xd0/0x430 ? handle_mm_fault+0xc8/0x240 ksys_write+0x9c/0xd0 do_syscall_64+0x96/0x190 entry_SYSCALL_64_after_hwframe+0x4b/0x53 This is because the commit d4b294b ("perf/x86: Hybrid PMU support for counters") took smp_processor_id() outside the irq critical section. If a preemption occurs in perf_event_print_debug() and the task is migrated to another cpu, we may get incorrect pmu debug information. Move smp_processor_id() back inside the irq critical section to fix this issue. Fixes: d4b294b ("perf/x86: Hybrid PMU support for counters") Signed-off-by: Li Huafei <lihuafei1@huawei.com> Reviewed-and-tested-by: K Prateek Nayak <kprateek.nayak@amd.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Kan Liang <kan.liang@linux.intel.com> Link: https://lore.kernel.org/r/20240729220928.325449-1-lihuafei1@huawei.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a9d6d46 commit 0c7c3dc

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

arch/x86/events/core.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -1521,20 +1521,23 @@ static void x86_pmu_start(struct perf_event *event, int flags)
15211521
void perf_event_print_debug(void)
15221522
{
15231523
u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
1524+
unsigned long *cntr_mask, *fixed_cntr_mask;
1525+
struct event_constraint *pebs_constraints;
1526+
struct cpu_hw_events *cpuc;
15241527
u64 pebs, debugctl;
1525-
int cpu = smp_processor_id();
1526-
struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
1527-
unsigned long *cntr_mask = hybrid(cpuc->pmu, cntr_mask);
1528-
unsigned long *fixed_cntr_mask = hybrid(cpuc->pmu, fixed_cntr_mask);
1529-
struct event_constraint *pebs_constraints = hybrid(cpuc->pmu, pebs_constraints);
1530-
unsigned long flags;
1531-
int idx;
1528+
int cpu, idx;
1529+
1530+
guard(irqsave)();
1531+
1532+
cpu = smp_processor_id();
1533+
cpuc = &per_cpu(cpu_hw_events, cpu);
1534+
cntr_mask = hybrid(cpuc->pmu, cntr_mask);
1535+
fixed_cntr_mask = hybrid(cpuc->pmu, fixed_cntr_mask);
1536+
pebs_constraints = hybrid(cpuc->pmu, pebs_constraints);
15321537

15331538
if (!*(u64 *)cntr_mask)
15341539
return;
15351540

1536-
local_irq_save(flags);
1537-
15381541
if (x86_pmu.version >= 2) {
15391542
rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
15401543
rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
@@ -1578,7 +1581,6 @@ void perf_event_print_debug(void)
15781581
pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
15791582
cpu, idx, pmc_count);
15801583
}
1581-
local_irq_restore(flags);
15821584
}
15831585

15841586
void x86_pmu_stop(struct perf_event *event, int flags)

0 commit comments

Comments
 (0)