* [PATCH v6 0/2] perf: Add some null ptr checks @ 2026-09-02 14:18 Vinay Belgaumkar 2026-09-02 14:18 ` [PATCH v6 1/2] perf: Fix null pointer access in is_include_guest_event() Vinay Belgaumkar 2026-09-02 14:18 ` [PATCH v6 2/2] perf: Add checks to prevent null ptr access Vinay Belgaumkar 0 siblings, 2 replies; 5+ messages in thread From: Vinay Belgaumkar @ 2026-09-02 14:18 UTC (permalink / raw) To: intel-xe; +Cc: dri-devel, Vinay Belgaumkar, Dapeng Mi This series fixes a crash seen when the PMU ptr is freed while there is an open perf connection. Cc: Dapeng Mi <dapeng1.mi@linux.intel.com> Signed-off-by: Vinay Belgaumkar <vinay.belgaumkar@intel.com> Vinay Belgaumkar (2): perf: Fix null pointer access in is_include_guest_event() perf: Add checks to prevent null ptr access kernel/events/core.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) -- 2.38.1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v6 1/2] perf: Fix null pointer access in is_include_guest_event() 2026-09-02 14:18 [PATCH v6 0/2] perf: Add some null ptr checks Vinay Belgaumkar @ 2026-09-02 14:18 ` Vinay Belgaumkar 2026-09-02 14:35 ` sashiko-bot 2026-09-02 14:18 ` [PATCH v6 2/2] perf: Add checks to prevent null ptr access Vinay Belgaumkar 1 sibling, 1 reply; 5+ messages in thread From: Vinay Belgaumkar @ 2026-09-02 14:18 UTC (permalink / raw) To: intel-xe; +Cc: dri-devel, Vinay Belgaumkar, Alexander Kanevskiy, Dapeng Mi A typical module unload occurring event when there is an active perf connection leads to freeing of the pmu pointer. The call log is something like: .. __pmu_detach_event pmu_detach_event pmu_detach_events perf_pmu_unregister .. __pmu_detach_event() sets event->pmu to null. When the perf connection finally is closed, the following stack trace is observed: Oops: general protection fault, kernel NULL pointer dereference ... RIP: 0010:_free_event+0x3e/0x370 ... Call Trace: ... perf_event_release_kernel+0x260/0x2d0 perf_release+0x12/0x20 A call to mediated_pmu_unaccount_event() inside _free_event() is the root cause of this crash. Adding a check inside is_include_guest_event() ensures we don't accidentally access a null pmu ptr. In addition to this, we will now call mediated_pmu_unaccount_event() before clearing the pmu ptr so that nr_include_guest_events counts are maintained correctly. v2: Add another check to prevent null access (sashiko) v3: Use READ_ONCE for checking pmu state (sashiko) v4: READ_ONCE for the pmu check as well (sashiko) v5: Improve commit message and split patch into 2 (Dapeng) Fixes: eff95e170275 ("perf: Add APIs to create/release mediated guest vPMUs") Cc: Alexander Kanevskiy <alexander.kanevskiy@intel.com> Cc: Dapeng Mi <dapeng1.mi@linux.intel.com> Assisted-by: Claude:Claude-Sonnet-5 Signed-off-by: Vinay Belgaumkar <vinay.belgaumkar@intel.com> --- kernel/events/core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/events/core.c b/kernel/events/core.c index 94ca277e0819..f19d27ac411d 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6350,6 +6350,9 @@ static DEFINE_MUTEX(perf_mediated_pmu_mutex); /* !exclude_guest event of PMU with PERF_PMU_CAP_MEDIATED_VPMU */ static inline bool is_include_guest_event(struct perf_event *event) { + if (!event->pmu) + return false; + if ((event->pmu->capabilities & PERF_PMU_CAP_MEDIATED_VPMU) && !event->attr.exclude_guest) return true; @@ -12977,6 +12980,7 @@ static void __pmu_detach_event(struct pmu *pmu, struct perf_event *event, exclusive_event_destroy(event); module_put(pmu->module); + mediated_pmu_unaccount_event(event); event->pmu = NULL; /* force fault instead of UAF */ } -- 2.38.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v6 1/2] perf: Fix null pointer access in is_include_guest_event() 2026-09-02 14:18 ` [PATCH v6 1/2] perf: Fix null pointer access in is_include_guest_event() Vinay Belgaumkar @ 2026-09-02 14:35 ` sashiko-bot 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-09-02 14:35 UTC (permalink / raw) To: Vinay Belgaumkar; +Cc: dri-devel, intel-xe Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit message claims that `READ_ONCE` was added to `is_include_guest_event()` for checking the `pmu` state, but the code lacks this instruction. -- commit 7c0504a46d493eec58eae8ca000ce324320508fd Author: Vinay Belgaumkar <vinay.belgaumkar@intel.com> perf: Fix null pointer access in is_include_guest_event() This commit addresses a null pointer dereference in is_include_guest_event() caused by a module unload occurring while a perf connection is active. It adds a null check for the pmu pointer and calls mediated_pmu_unaccount_event() before clearing the pointer to maintain the event counts correctly. > diff --git a/kernel/events/core.c b/kernel/events/core.c > index a6c8e38a31104..7777e82aad5e7 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -6350,6 +6350,9 @@ static DEFINE_MUTEX(perf_mediated_pmu_mutex); > /* !exclude_guest event of PMU with PERF_PMU_CAP_MEDIATED_VPMU */ > static inline bool is_include_guest_event(struct perf_event *event) > { > + if (!event->pmu) > + return false; > + > if ((event->pmu->capabilities & PERF_PMU_CAP_MEDIATED_VPMU) && [Severity: Medium] The commit message claims that READ_ONCE was added for checking the pmu state, but the code lacks this instruction here. Without READ_ONCE(), the compiler is free to issue two independent memory loads of event->pmu: one for the null check and one for accessing capabilities. Could this leave a potential double-read hazard if event->pmu is concurrently nullified by __pmu_detach_event() while is_include_guest_event() is executing? -- Sashiko AI review · https://sashiko.dev/#/patchset/20260902141824.733112-1-vinay.belgaumkar@intel.com?part=1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v6 2/2] perf: Add checks to prevent null ptr access 2026-09-02 14:18 [PATCH v6 0/2] perf: Add some null ptr checks Vinay Belgaumkar 2026-09-02 14:18 ` [PATCH v6 1/2] perf: Fix null pointer access in is_include_guest_event() Vinay Belgaumkar @ 2026-09-02 14:18 ` Vinay Belgaumkar 2026-09-02 14:41 ` sashiko-bot 1 sibling, 1 reply; 5+ messages in thread From: Vinay Belgaumkar @ 2026-09-02 14:18 UTC (permalink / raw) To: intel-xe; +Cc: dri-devel, Vinay Belgaumkar, Dapeng Mi Sashiko recommended some additional checks to prevent null pointer access. Check for revoked states inside perf_event_read_local(), as the pmu event may have already been freed at this point. Add a null check inside __perf_event_read_cpu() as well before accessing the pmu ptr. Cc: Dapeng Mi <dapeng1.mi@linux.intel.com> Signed-off-by: Vinay Belgaumkar <vinay.belgaumkar@intel.com> --- kernel/events/core.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index f19d27ac411d..bb90034a2054 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -4788,14 +4788,19 @@ static inline const struct cpumask *perf_scope_cpu_topology_cpumask(unsigned int static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) { + struct pmu *pmu = READ_ONCE(event->pmu); int local_cpu = smp_processor_id(); u16 local_pkg, event_pkg; if ((unsigned)event_cpu >= nr_cpu_ids) return event_cpu; + if (!pmu) + return -ENODEV; + if (event->group_caps & PERF_EV_CAP_READ_SCOPE) { - const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(event->pmu->scope, event_cpu); + const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(pmu->scope, + event_cpu); if (cpumask && cpumask_test_cpu(local_cpu, cpumask)) return local_cpu; @@ -4917,6 +4922,11 @@ int perf_event_read_local(struct perf_event *event, u64 *value, goto out; } + if (READ_ONCE(event->state) <= PERF_EVENT_STATE_REVOKED) { + ret = -ENODEV; + goto out; + } + /* * Get the event CPU numbers, and adjust them to local if the event is * a per-package event that can be read locally -- 2.38.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v6 2/2] perf: Add checks to prevent null ptr access 2026-09-02 14:18 ` [PATCH v6 2/2] perf: Add checks to prevent null ptr access Vinay Belgaumkar @ 2026-09-02 14:41 ` sashiko-bot 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-09-02 14:41 UTC (permalink / raw) To: Vinay Belgaumkar; +Cc: dri-devel, intel-xe Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: Pre-existing issues: - [High] The lockless checks for revoked state and NULL PMU pointers are insufficient to prevent a Use-After-Free of the PMU structure or a NULL pointer dereference during concurrent unregistration. -- commit 4a985bf8ccfd6853c89a279d4b7181e83c33bcef Author: Vinay Belgaumkar <vinay.belgaumkar@intel.com> perf: Add checks to prevent null ptr access This commit adds a check for revoked states in perf_event_read_local() and a null pointer check for the PMU in __perf_event_read_cpu(). > diff --git a/kernel/events/core.c b/kernel/events/core.c > index 7777e82aad5e7..059f82f0cadd4 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -4788,14 +4788,19 @@ static inline const struct cpumask *perf_scope_cpu_topology_cpumask(unsigned int > > static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) > { > + struct pmu *pmu = READ_ONCE(event->pmu); > int local_cpu = smp_processor_id(); > u16 local_pkg, event_pkg; > > if ((unsigned)event_cpu >= nr_cpu_ids) > return event_cpu; > > + if (!pmu) > + return -ENODEV; > + [Severity: High] This is a pre-existing issue, but does this lockless check provide a false sense of security against concurrent PMU unregistration? A concurrent perf_pmu_unregister() can set event->pmu = NULL and free the PMU memory. Since this check relies on a cached pointer without holding locks that serialize against unregistration (e.g., ctx->lock), the PMU can be freed immediately after this check passes. > if (event->group_caps & PERF_EV_CAP_READ_SCOPE) { > - const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(event->pmu->scope, event_cpu); > + const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(pmu->scope, > + event_cpu); [Severity: High] This is a pre-existing issue, but can this lead to a Use-After-Free when accessing pmu->scope? If the PMU is unregistered and freed by another CPU after we cache it but before we dereference it here, pmu->scope will read from freed memory. [ ... ] > @@ -4917,6 +4922,11 @@ int perf_event_read_local(struct perf_event *event, u64 *value, > goto out; > } > > + if (READ_ONCE(event->state) <= PERF_EVENT_STATE_REVOKED) { > + ret = -ENODEV; > + goto out; > + } > + [Severity: High] This is a pre-existing issue, but is this check vulnerable to a TOCTOU race? The event state can change immediately after this check if the event context is inactive or belongs to a different CPU, allowing a concurrent unregistration to detach the event and free the PMU while perf_event_read_local() continues executing. This leaves the subsequent call to event->pmu->read(event) unprotected, potentially resulting in a NULL pointer dereference or Use-After-Free. > /* > * Get the event CPU numbers, and adjust them to local if the event is > * a per-package event that can be read locally -- Sashiko AI review · https://sashiko.dev/#/patchset/20260902141824.733112-1-vinay.belgaumkar@intel.com?part=2 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-09-02 14:41 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-09-02 14:18 [PATCH v6 0/2] perf: Add some null ptr checks Vinay Belgaumkar 2026-09-02 14:18 ` [PATCH v6 1/2] perf: Fix null pointer access in is_include_guest_event() Vinay Belgaumkar 2026-09-02 14:35 ` sashiko-bot 2026-09-02 14:18 ` [PATCH v6 2/2] perf: Add checks to prevent null ptr access Vinay Belgaumkar 2026-09-02 14:41 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox