perf_pmu_register() allocates the per-CPU contexts one CPU at a time. When one of these allocations fails, it returns -ENOMEM and perf_pmu_free() runs with NULL entries for the CPUs that are not allocated yet. The loop in perf_pmu_free() skips those entries, but commit 35fbc5a85891 ("perf/core: Cancel hrtimer on perf_pmu_unregister()") put the hrtimer_cancel() before the NULL check. Such a failure then oopses instead of returning -ENOMEM: KASAN: null-ptr-deref in range [0x0000000000000108-0x000000000000010f] RIP: 0010:hrtimer_active+0xc1/0x350 Call Trace: hrtimer_cancel+0x15/0x40 perf_pmu_free+0x1cd/0x320 perf_pmu_register+0x4e1/0xb40 The allocations are small GFP_KERNEL ones, so this needs fault injection or a registering task that the OOM killer has picked. Found with failslab fault injection on the allocations in perf_pmu_register(). Cancel the hrtimer after the NULL check. Fixes: 35fbc5a85891 ("perf/core: Cancel hrtimer on perf_pmu_unregister()") Cc: stable@vger.kernel.org Signed-off-by: Hang Zhengyang --- Notes: Tested with a module that sets current->fail_nth before each perf_pmu_register() call, in a KVM guest with 4 CPUs, KASAN and failslab, on tip/perf/urgent 67b34211a9eb: the oops above without the patch; with it, fail_nth 1-4 return -ENOMEM and fail_nth 5 registers the PMU. I used Claude Code to find the issue, write the patch and run the test. I reviewed the patch. kernel/events/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 4ab3919ac78e..726ed5538397 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -12999,9 +12999,9 @@ static void perf_pmu_free(struct pmu *pmu) struct perf_cpu_pmu_context *cpc; cpc = *per_cpu_ptr(pmu->cpu_pmu_context, cpu); - hrtimer_cancel(&cpc->hrtimer); if (!cpc) continue; + hrtimer_cancel(&cpc->hrtimer); if (cpc->epc.embedded) { /* refcount managed */ put_pmu_ctx(&cpc->epc); base-commit: 67b34211a9eb103aba474bc179fab3dd2912f07e -- 2.53.0