perf: nvidia_t410_cmem_latency: fix cpuhp state leak on init failure
PATCH 1/4 perf: nvidia_t410_cmem_latency: fix cpuhp state leak on init failure
If platform_driver_register() fails, the cpuhp multi-state registered by cpuhp_setup_state_multi() is never cleaned up. Add cpuhp_remove_multi_state() on the error path, mirroring the cleanup in cmem_lat_pmu_exit().
--- drivers/perf/nvidia_t410_cmem_latency_pmu.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/perf/nvidia_t410_cmem_latency_pmu.c +5 -1
@@ -719,7 +719,11 @@ static int __init cmem_lat_pmu_init(void) cmem_lat_pmu_cpuhp_state = ret; -return platform_driver_register (&cmem_lat_pmu_driver); + ret =platform_driver_register (&cmem_lat_pmu_driver); +if (ret) +cpuhp_remove_multi_state (cmem_lat_pmu_cpuhp_state); + +return ret; } static void __exit cmem_lat_pmu_exit(void)
Signature · 1 line
2.47.3
PATCH 2/4 perf: nvidia_t410_cmem_latency: handle PERF_EF_UPDATE in stop
cmem_lat_pmu_stop() does not read the final counter value when called with PERF_EF_UPDATE. When perf core calls pmu->del() -> pmu->stop() with PERF_EF_UPDATE, the last counter delta is lost because the event is marked stopped without reading hardware.
Add the standard PMU stop pattern: bail out if already stopped, call the event update function when PERF_EF_UPDATE is requested, then mark the event stopped.
--- drivers/perf/nvidia_t410_cmem_latency_pmu.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/perf/nvidia_t410_cmem_latency_pmu.c +6 -0
@@ -303,6 +303,12 @@ static void cmem_lat_pmu_start(struct perf_event *event, int pmu_flags)static void cmem_lat_pmu_stop (struct perf_event *event,int pmu_flags) { +if (event->hw.state &PERF_HES_STOPPED ) +return ; + +if (pmu_flags &PERF_EF_UPDATE ) +cmem_lat_pmu_event_update (event); + event->hw.state |=PERF_HES_STOPPED ; }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 2/4] perf: nvidia_t410_cmem_latency: handle PERF_EF_UPDATE in stop
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] Performing MMIO reads on clock-gated hardware in pmu->stop() will trigger a system crash…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] Performing MMIO reads on clock-gated hardware in pmu->stop() will trigger a system crash (SError).
- [High] API Contract Violation and conditional update logic error for free-running PMU counters.
- [High] Use-After-Free of PMU context during CPU hotplug teardown.
--
commit 1c17f7a02936b83432204f0c38f9d1d22f6f4acd
Author: Saurav Sachidanand <[email protected]>
perf: nvidia_t410_cmem_latency: handle PERF_EF_UPDATE in stop
This commit updates cmem_lat_pmu_stop to return early if the event is already stopped, and conditionally updates the event if the update flag is requested.
Quoted 4 lines
cmem_lat_pmu_stop()does not read the final counter value when called withPERF_EF_UPDATE. When perf core callspmu->del()->pmu->stop()withPERF_EF_UPDATE, the last counter delta is lost because the event is marked stopped without reading hardware.
Is this description accurate?
Looking at cmem_lat_pmu_disable(), it explicitly stops the hardware, reads and updates all active events, and clears the counters. Since pmu->stop() is called within the perf_pmu_disable() block, the final count should already be unconditionally read before shutting down the hardware, meaning the last counter delta might not be lost.
drivers/perf/nvidia_t410_cmem_latency_pmu.c static void cmem_lat_pmu_start(struct perf_event *event, int pmu_flags) · line 307
Quoted 9 lines
drivers/perf/nvidia_t410_cmem_latency_pmu.c +2
-0@@ -303,6 +303,12 @@ static void cmem_lat_pmu_start(struct perf_event *event, int pmu_flags)static void cmem_lat_pmu_stop (struct perf_event *event,int pmu_flags) { +if (event->hw.state &PERF_HES_STOPPED ) +return ;
Does this early return violate the perf core API contract by ignoring subsequent PERF_EF_UPDATE requests for already-stopped events?
drivers/perf/nvidia_t410_cmem_latency_pmu.c static void cmem_lat_pmu_start(struct perf_event *event, int pmu_flags) · line 310
Quoted 3 lines
drivers/perf/nvidia_t410_cmem_latency_pmu.c +3
-0+ +if (pmu_flags &PERF_EF_UPDATE ) +cmem_lat_pmu_event_update (event);
Will this trigger an asynchronous external abort (SError) and crash the system?
The perf core calls pmu->stop() within a perf_pmu_disable() block. In this driver, cmem_lat_pmu_disable() gates the module clock. Calling cmem_lat_pmu_event_update() performs MMIO reads on the PMU registers. Since this happens while the PMU clock is gated, these MMIO accesses will target unclocked registers.
Additionally, does this conditional update cause regressions for free-running PMU counters?
Since this PMU uses free-running global counters without per-event hardware stops, conditionally updating the event only when PERF_EF_UPDATE is set means calls like pmu->stop(event, 0) will fail to capture the counter delta. This allows the hardware counter to continue incrementing while the event is logically stopped, which could corrupt subsequent readings.
drivers/perf/nvidia_t410_cmem_latency_pmu.c static void cmem_lat_pmu_start(struct perf_event *event, int pmu_flags) · line 313
Quoted 3 lines
drivers/perf/nvidia_t410_cmem_latency_pmu.c +1
-0+ event->hw.state |=PERF_HES_STOPPED ; }
While reviewing this driver, I noticed an existing regression in the hotplug teardown path.
Can a use-after-free occur during CPU hotplug teardown?
In cmem_lat_pmu_device_remove(), perf_pmu_unregister() is called before cpuhp_state_remove_instance(). perf_pmu_unregister() detaches all events and frees internal core PMU resources.
When cpuhp_state_remove_instance() runs sequentially after it, it executes the hotplug teardown callback cmem_lat_pmu_cpu_teardown(), which invokes perf_pmu_migrate_context() with the already unregistered PMU pointer. This accesses partially freed perf core state, which might cause a kernel panic upon unbind or failed initialization.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RE: [PATCH 2/4] perf: nvidia_t410_cmem_latency: handle PERF_EF_UPDATE in stop
In reply to Saurav SachidanandQuoted 40 lines
-----Original Message-----
From: Saurav Sachidanand <[email protected]>
Sent: Wednesday, May 13, 2026 8:06 PM
To: Will Deacon <[email protected]>
Cc: Mark Rutland <[email protected]>; Besar Wicaksono <[email protected]>; Ilkka Koskinen <[email protected]>; Andi Shyti <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Saurav Sachidanand <[email protected]>
Subject: [PATCH 2/4] perf:nvidia_t410_cmem_latency: handlePERF_EF_UPDATEin stopExternal email: Use caution opening links or attachments
cmem_lat_pmu_stop()does not read the final counter value when called withPERF_EF_UPDATE. When perf core callspmu->del()->pmu->stop()withPERF_EF_UPDATE, the last counter delta is lost because the event is marked stopped without reading hardware.Add the standard PMU stop pattern: bail out if already stopped, call the event update function when
PERF_EF_UPDATEis requested, then mark the event stopped.Fixes 429b7638b2df ("perf: add NVIDIA Tegra410 CPU Memory LatencyPMU")
Signed-off-by Saurav Sachidanand <[email protected]>--- drivers/perf/nvidia_t410_cmem_latency_pmu.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/perf/nvidia_t410_cmem_latency_pmu.c +0
-0
drivers/perf/nvidia_t410_cmem_latency_pmu.c static void cmem_lat_pmu_start(struct perf_event · line 312
Quoted 10 lines
drivers/perf/nvidia_t410_cmem_latency_pmu.c +6
-0@@ -303,6 +303,12 @@ static void cmem_lat_pmu_start(struct perf_event *event, int pmu_flags)static void cmem_lat_pmu_stop (struct perf_event *event,int pmu_flags) { +if (event->hw.state &PERF_HES_STOPPED ) +return ; + +if (pmu_flags &PERF_EF_UPDATE ) +cmem_lat_pmu_event_update (event); +
Hi Saurav,
This call is not needed since the driver called it on pmu_disable callback. Adding it on stop callback would be redundant.
Regards,
Besar
PATCH 3/4 perf: nvidia_t410_c2c: fix cpuhp state leak on init failure
In reply to Saurav SachidanandIf platform_driver_register() fails, the cpuhp multi-state registered by cpuhp_setup_state_multi() is never cleaned up. Add cpuhp_remove_multi_state() on the error path, mirroring the cleanup in nv_c2c_pmu_exit().
--- drivers/perf/nvidia_t410_c2c_pmu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/perf/nvidia_t410_c2c_pmu.c +6 -1
@@ -1034,7 +1034,12 @@ static int __init nv_c2c_pmu_init(void)return ret; nv_c2c_pmu_cpuhp_state = ret; -return platform_driver_register (&nv_c2c_pmu_driver); + + ret =platform_driver_register (&nv_c2c_pmu_driver); +if (ret) +cpuhp_remove_multi_state (nv_c2c_pmu_cpuhp_state); + +return ret; } static void __exit nv_c2c_pmu_exit(void)
Signature · 1 line
2.47.3
RE: [PATCH 3/4] perf: nvidia_t410_c2c: fix cpuhp state leak on init failure
Quoted 48 lines
-----Original Message-----
From: Saurav Sachidanand <[email protected]>
Sent: Wednesday, May 13, 2026 8:06 PM
To: Will Deacon <[email protected]>
Cc: Mark Rutland <[email protected]>; Besar Wicaksono <[email protected]>; Ilkka Koskinen <[email protected]>; Andi Shyti <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Saurav Sachidanand <[email protected]>
Subject: [PATCH 3/4] perf:nvidia_t410_c2c: fix cpuhp state leak on init failureExternal email: Use caution opening links or attachments
If
platform_driver_register()fails, the cpuhp multi-state registered bycpuhp_setup_state_multi()is never cleaned up. Addcpuhp_remove_multi_state()on the error path, mirroring the cleanup innv_c2c_pmu_exit().Fixes 2f89b7f78c50 ("perf: add NVIDIA Tegra410 C2C PMU")Signed-off-by Saurav Sachidanand <[email protected]>--- drivers/perf/nvidia_t410_c2c_pmu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/perf/nvidia_t410_c2c_pmu.c +6
-2@@ -1034,7 +1034,12 @@ static int __init nv_c2c_pmu_init(void)return ret; nv_c2c_pmu_cpuhp_state = ret; -return platform_driver_register (&nv_c2c_pmu_driver); + + ret =platform_driver_register (&nv_c2c_pmu_driver); +if (ret) +cpuhp_remove_multi_state (nv_c2c_pmu_cpuhp_state); + +return ret; } static void __exit nv_c2c_pmu_exit(void) --
Quoted 1 line
2.47.3
PATCH 4/4 perf: nvidia_t410_c2c: handle PERF_EF_UPDATE in stop
In reply to Saurav Sachidanandnv_c2c_pmu_stop() does not read the final counter value when called with PERF_EF_UPDATE. The last counter delta is lost when perf core removes the event.
Add the standard PMU stop pattern: bail out if already stopped, call nv_c2c_pmu_event_update() when PERF_EF_UPDATE is requested, then mark the event stopped.
--- drivers/perf/nvidia_t410_c2c_pmu.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/perf/nvidia_t410_c2c_pmu.c +6 -0
@@ -394,6 +394,12 @@ static void nv_c2c_pmu_start(struct perf_event *event, int pmu_flags)static void nv_c2c_pmu_stop (struct perf_event *event,int pmu_flags) { +if (event->hw.state &PERF_HES_STOPPED ) +return ; + +if (pmu_flags &PERF_EF_UPDATE ) +nv_c2c_pmu_event_update (event); + event->hw.state |=PERF_HES_STOPPED ; }
Signature · 1 line
2.47.3
RE: [PATCH 4/4] perf: nvidia_t410_c2c: handle PERF_EF_UPDATE in stop
Quoted 37 lines
-----Original Message-----
From: Saurav Sachidanand <[email protected]>
Sent: Wednesday, May 13, 2026 8:06 PM
To: Will Deacon <[email protected]>
Cc: Mark Rutland <[email protected]>; Besar Wicaksono <[email protected]>; Ilkka Koskinen <[email protected]>; Andi Shyti <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Saurav Sachidanand <[email protected]>
Subject: [PATCH 4/4] perf:nvidia_t410_c2c: handlePERF_EF_UPDATEin stopExternal email: Use caution opening links or attachments
nv_c2c_pmu_stop()does not read the final counter value when called withPERF_EF_UPDATE. The last counter delta is lost when perf core removes the event.Add the standard PMU stop pattern: bail out if already stopped, call
nv_c2c_pmu_event_update()whenPERF_EF_UPDATEis requested, then mark the event stopped.Fixes 2f89b7f78c50 ("perf: add NVIDIA Tegra410 C2C PMU")Signed-off-by Saurav Sachidanand <[email protected]>--- drivers/perf/nvidia_t410_c2c_pmu.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/perf/nvidia_t410_c2c_pmu.c +0
-0
drivers/perf/nvidia_t410_c2c_pmu.c static void nv_c2c_pmu_start(struct perf_event · line 403
Quoted 10 lines
drivers/perf/nvidia_t410_c2c_pmu.c +6
-0@@ -394,6 +394,12 @@ static void nv_c2c_pmu_start(struct perf_event *event, int pmu_flags)static void nv_c2c_pmu_stop (struct perf_event *event,int pmu_flags) { +if (event->hw.state &PERF_HES_STOPPED ) +return ; + +if (pmu_flags &PERF_EF_UPDATE ) +nv_c2c_pmu_event_update (event); +
Hi Saurav,
This call is not needed since the driver called it on pmu_disable callback. Adding it on stop callback would be redundant.
Regards,
Besar
[email protected]
AI reviewRe: [PATCH 4/4] perf: nvidia_t410_c2c: handle PERF_EF_UPDATE in stop
In reply to Saurav SachidanandThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] Calling nv_c2c_pmu_event_update() in nv_c2c_pmu_stop() causes double-counting and massive…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] Calling nv_c2c_pmu_event_update() in nv_c2c_pmu_stop() causes double-counting and massive counter wrap-around corruption.
- [High] Logically stopping the event fails to exclude counts because the hardware counter continues running and start logic is unmodified.
- [High] nv_c2c_pmu_stop() incorrectly uses an early return on PERF_HES_STOPPED, permanently dropping final counter updates.
- [Medium] nv_c2c_pmu_stop() fails to set PERF_HES_UPTODATE after updating the event.
--
commit 1d56e58b1d1592eb3b08b91d9c3d09bd821ef6ad
Author: Saurav Sachidanand <[email protected]>
perf: nvidia_t410_c2c: handle PERF_EF_UPDATE in stop
This commit updates nv_c2c_pmu_stop() to read the final counter value when called with PERF_EF_UPDATE, aiming to prevent lost counter deltas when the perf core removes the event.
drivers/perf/nvidia_t410_c2c_pmu.c static void nv_c2c_pmu_start(struct perf_event *event, int pmu_flags) · line 398
Quoted 10 lines
drivers/perf/nvidia_t410_c2c_pmu.c +2
-0@@ -394,6 +394,12 @@ static void nv_c2c_pmu_start(struct perf_event *event, int pmu_flags)static void nv_c2c_pmu_stop (struct perf_event *event,int pmu_flags) { +if (event->hw.state &PERF_HES_STOPPED ) +return ;
Does this early return permanently drop final counter updates?
If the core stops the event without requesting an update first (for example, via pmu->stop(event, 0) during event throttling), the event is marked PERF_HES_STOPPED.
When the core later attempts to drain the final unread counts by calling pmu->stop(event, PERF_EF_UPDATE), the early return will trigger and the final update will be entirely skipped.
drivers/perf/nvidia_t410_c2c_pmu.c static void nv_c2c_pmu_start(struct perf_event *event, int pmu_flags) · line 401
Quoted 3 lines
drivers/perf/nvidia_t410_c2c_pmu.c +3
-0+ +if (pmu_flags &PERF_EF_UPDATE ) +nv_c2c_pmu_event_update (event);
Can this cause double-counting and massive counter wrap-around?
The perf core wraps event teardowns within perf_pmu_disable() and perf_pmu_enable(). For this PMU, nv_c2c_pmu_disable() halts global counters, calls nv_c2c_pmu_event_update() to accumulate final counts, and then explicitly sets event->hw.prev_count to 0.
Subsequently, pmu->del() calls nv_c2c_pmu_stop() with PERF_EF_UPDATE, which redundantly calls nv_c2c_pmu_event_update() again. Because prev_count was just reset to 0 but the stopped hardware counter still retains its final value V, the second update computes V - 0 = V and erroneously adds the full lifetime count to event->count a second time.
Additionally, this second update overwrites prev_count with V. When the PMU restarts, the physical counters clear to 0. The next update will read a small new value W, calculate W - V, and cause a massive unsigned 64-bit integer underflow.
Also, if the event is stopped logically without disabling the entire global PMU, the hardware counter continues incrementing. Since nv_c2c_pmu_start() is unmodified and does not synchronize prev_count to the new running value, won't all counts accumulated while the event was logically stopped still be incorrectly added to event->count upon the next update?
drivers/perf/nvidia_t410_c2c_pmu.c static void nv_c2c_pmu_start(struct perf_event *event, int pmu_flags) · line 403
Quoted 2 lines
drivers/perf/nvidia_t410_c2c_pmu.c +1
-0+ event->hw.state |=PERF_HES_STOPPED ;
Should this also set PERF_HES_UPTODATE?
The standard PMU stop pattern usually sets PERF_HES_UPTODATE after an update so the core knows the software counter is synchronized with the hardware.
Quoted 1 line
drivers/perf/nvidia_t410_c2c_pmu.c +0
-0}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Quoted 49 lines
-----Original Message-----
From: Saurav Sachidanand <[email protected]>
Sent: Wednesday, May 13, 2026 8:06 PM
To: Will Deacon <[email protected]>
Cc: Mark Rutland <[email protected]>; Besar Wicaksono <[email protected]>; Ilkka Koskinen <[email protected]>; Andi Shyti <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Saurav Sachidanand <[email protected]>
Subject: [PATCH 1/4] perf:nvidia_t410_cmem_latency: fix cpuhp state leak on init failureExternal email: Use caution opening links or attachments
If
platform_driver_register()fails, the cpuhp multi-state registered bycpuhp_setup_state_multi()is never cleaned up. Addcpuhp_remove_multi_state()on the error path, mirroring the cleanup incmem_lat_pmu_exit().Fixes 429b7638b2df ("perf: add NVIDIA Tegra410 CPU Memory LatencyPMU")
Signed-off-by Saurav Sachidanand <[email protected]>--- drivers/perf/nvidia_t410_cmem_latency_pmu.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/perf/nvidia_t410_cmem_latency_pmu.c +5
-2@@ -719,7 +719,11 @@ static int __init cmem_lat_pmu_init(void) cmem_lat_pmu_cpuhp_state = ret; -return platform_driver_register (&cmem_lat_pmu_driver); + ret =platform_driver_register (&cmem_lat_pmu_driver); +if (ret) +cpuhp_remove_multi_state (cmem_lat_pmu_cpuhp_state); + +return ret; } static void __exit cmem_lat_pmu_exit(void) --
Quoted 1 line
2.47.3
Will Deacon
maintainerHi Saurav,
Quoted 10 lines · Saurav Sachidanand
If
platform_driver_register()fails, the cpuhp multi-state registered bycpuhp_setup_state_multi()is never cleaned up. Addcpuhp_remove_multi_state()on the error path, mirroring the cleanup incmem_lat_pmu_exit().Fixes 429b7638b2df ("perf: add NVIDIA Tegra410 CPU Memory Latency PMU")Signed-off-by Saurav Sachidanand <[email protected]>--- drivers/perf/nvidia_t410_cmem_latency_pmu.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
Do you plan to respin this series, taking into account the comments from Besar and Sashiko? If you do, please include a cover letter next time as well.
Cheers,
Will