* [PATCH] perf/core: Check for a NULL cpc before cancelling its hrtimer
@ 2026-12-03 7:37 Hang Zhengyang
2026-12-03 7:43 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Hang Zhengyang @ 2026-12-03 7:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar
Cc: Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
James Clark, Dapeng Mi, Hanxiang Xu, linux-perf-users,
linux-kernel, stable
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 <hang.zhengyang1010@gmail.com>
---
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] perf/core: Check for a NULL cpc before cancelling its hrtimer
2026-12-03 7:37 [PATCH] perf/core: Check for a NULL cpc before cancelling its hrtimer Hang Zhengyang
@ 2026-12-03 7:43 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 7:43 UTC (permalink / raw)
To: Hang Zhengyang; +Cc: linux-perf-users
> 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 <hang.zhengyang1010@gmail.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203073728.2470032-1-hang.zhengyang1010@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 7:43 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 7:37 [PATCH] perf/core: Check for a NULL cpc before cancelling its hrtimer Hang Zhengyang
2026-12-03 7:43 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox