perf/x86: Prevent NULL event deref in handle_pmi_common()
Subsystems (2)
PATCH 1/1 perf/x86: Prevent NULL event deref in handle_pmi_common()
From: Evan Li <[email protected]>
handle_pmi_common() may observe an active bit set in cpuc->active_mask while the corresponding cpuc->events[] entry has already been cleared, which leads to a NULL pointer dereference.
This can happen when interrupt throttling stops all events in a group while PEBS processing is still in progress. perf_event_overflow() can trigger perf_event_throttle_group(), which stops the group and clears the cpuc->events[] entry, but the active bit may still be set when handle_pmi_common() iterates over the events.
The following change:
7e772a93 ("perf/x86: Fix NULL event access and potential PEBS record loss")
moved cpuc->events[] clearing from x86_pmu_stop() to x86_pmu_del() and relied on cpuc->active_mask/pebs_enabled checks. However, handle_pmi_common() can still encounter a NULL cpuc->events[] entry despite the active bit being set.
Add an explicit NULL check on the event pointer before using it to avoid dereferencing a cleared cpuc->events[] slot.
--- arch/x86/events/intel/core.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/events/intel/core.c +3 -0
@@ -3378,6 +3378,9 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)if (!test_bit (bit, cpuc->active_mask))continue ; +/* Check if event is NULL to prevent null pointer dereference */ +if (!event) +continue ;/* * There may be unprocessed PEBS records in the PEBS buffer,
Signature · 1 line
2.43.7
[tip: perf/urgent] perf/x86/intel: Fix NULL event dereference crash in handle_pmi_common()
The following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 9415f749d34b926b9e4853da1462f4d941f89a0d Gitweb: https://git.kernel.org/tip/9415f749d34b926b9e4853da1462f4d941f89a0d Author: Evan Li <[email protected]> AuthorDate: Fri, 12 Dec 2025 16:49:43 +08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 12 Dec 2025 09:57:39 +01:00
perf/x86/intel: Fix NULL event dereference crash in handle_pmi_common()
handle_pmi_common() may observe an active bit set in cpuc->active_mask while the corresponding cpuc->events[] entry has already been cleared, which leads to a NULL pointer dereference.
This can happen when interrupt throttling stops all events in a group while PEBS processing is still in progress. perf_event_overflow() can trigger perf_event_throttle_group(), which stops the group and clears the cpuc->events[] entry, but the active bit may still be set when handle_pmi_common() iterates over the events.
The following recent fix:
7e772a93eb61 ("perf/x86: Fix NULL event access and potential PEBS record loss")
moved the cpuc->events[] clearing from x86_pmu_stop() to x86_pmu_del() and relied on cpuc->active_mask/pebs_enabled checks. However, handle_pmi_common() can still encounter a NULL cpuc->events[] entry despite the active bit being set.
Add an explicit NULL check on the event pointer before using it, to cover this legitimate scenario and avoid the NULL dereference crash.
--- arch/x86/events/intel/core.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/events/intel/core.c +3 -0
@@ -3378,6 +3378,9 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)if (!test_bit (bit, cpuc->active_mask))continue ; +/* Event may have already been cleared: */ +if (!event) +continue ;/* * There may be unprocessed PEBS records in the PEBS buffer,
Peter Zijlstra
maintainerRe: [tip: perf/urgent] perf/x86/intel: Fix NULL event dereference crash in handle_pmi_common()
Quoted 49 lines · On Fri, Dec 12, 2025 at 09:04:41AM -0000, tip-bot2 for Evan Li wrote:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 9415f749d34b926b9e4853da1462f4d941f89a0d Gitweb: https://git.kernel.org/tip/9415f749d34b926b9e4853da1462f4d941f89a0d Author: Evan Li <[email protected]> AuthorDate: Fri, 12 Dec 2025 16:49:43 +08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 12 Dec 2025 09:57:39 +01:00perf/x86/intel: Fix NULL event dereference crash in
handle_pmi_common()
handle_pmi_common()may observe an active bit set incpuc->active_maskwhile the corresponding cpuc->events[] entry has already been cleared, which leads to a NULL pointer dereference.This can happen when interrupt throttling stops all events in a group while PEBS processing is still in progress.
perf_event_overflow()can triggerperf_event_throttle_group(), which stops the group and clears the cpuc->events[] entry, but the active bit may still be set whenhandle_pmi_common()iterates over the events.The following recent fix:
7e772a93eb61("perf/x86: Fix NULL event access and potential PEBS record loss")moved the cpuc->events[] clearing from
x86_pmu_stop()tox86_pmu_del()and relied on cpuc->active_mask/pebs_enabled checks. However,handle_pmi_common()can still encounter a NULL cpuc->events[] entry despite the active bit being set.Add an explicit NULL check on the event pointer before using it, to cover this legitimate scenario and avoid the NULL dereference crash.
Fixes 7e772a93eb61 ("perf/x86: Fix NULL event access and potential PEBS record loss")Reported-by kitta <[email protected]>Co-developed-by kitta <[email protected]>Signed-off-by Evan Li <[email protected]>Signed-off-by Ingo Molnar <[email protected]>--- arch/x86/events/intel/core.c | 3 +++ 1 file changed, 3 insertions(+)arch/x86/events/intel/core.c +0
-0
arch/x86/events/intel/core.c static int handle_pmi_common(struct pt_regs *regs, u64 status) · line 3383
Quoted 6 lines
arch/x86/events/intel/core.c +3
-0@@ -3378,6 +3378,9 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)if (!test_bit (bit, cpuc->active_mask))continue ; +/* Event may have already been cleared: */ +if (!event) +continue ;
I still hate this commit -- it doesn't actually explain anything, at best it papers over an issue elsewhere :-(
Peter Zijlstra
maintainerQuoted 12 lines · On Fri, Dec 12, 2025 at 04:49:43PM +0800, [email protected] wrote:
From: Evan Li <[email protected]>
handle_pmi_common()may observe an active bit set incpuc->active_maskwhile the corresponding cpuc->events[] entry has already been cleared, which leads to a NULL pointer dereference.This can happen when interrupt throttling stops all events in a group while PEBS processing is still in progress.
perf_event_overflow()can triggerperf_event_throttle_group(), which stops the group and clears the cpuc->events[] entry, but the active bit may still be set whenhandle_pmi_common()iterates over the events.
Quoted 8 lines
The following change:
7e772a93("perf/x86: Fix NULL event access and potential PEBS record loss")moved cpuc->events[] clearing from
x86_pmu_stop()tox86_pmu_del()and relied on cpuc->active_mask/pebs_enabled checks. However,handle_pmi_common()can still encounter a NULL cpuc->events[] entry despite the active bit being set.
How? What is doing del() concurrently with the pmi?
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · On Fri, Dec 12, 2025 at 04:49:43PM +0800, [email protected] wrote:
From: Evan Li <[email protected]>
handle_pmi_common()may observe an active bit set incpuc->active_maskwhile the corresponding cpuc->events[] entry has already been cleared, which leads to a NULL pointer dereference.This can happen when interrupt throttling stops all events in a group while PEBS processing is still in progress.
perf_event_overflow()can triggerperf_event_throttle_group(), which stops the group and clears the cpuc->events[] entry, but the active bit may still be set whenhandle_pmi_common()iterates over the events.The following change:
Quoted 7 lines
Quoted 6 lines
7e772a93("perf/x86: Fix NULL event access and potential PEBS record loss")moved cpuc->events[] clearing from
x86_pmu_stop()tox86_pmu_del()and relied on cpuc->active_mask/pebs_enabled checks. However,handle_pmi_common()can still encounter a NULL cpuc->events[] entry despite the active bit being set.How? What is doing
del()concurrently with the pmi?
As long as stop() is called before del(), the corresponding bit in active_mask should be cleared. Perhaps it's because x86_pmu_start() is called first and x86_pmu_enable() is still not called and PMI arrives? Anyway I can't figure out why this issue could happen. @kitta @evan could you please share how to reproduce this issue? I would try to reproduce this issue locally. Thanks.