Skip to contentSkip to conversation

perf: starfive: Fix potential per-CPU memory leak on probe failure

PATCH New Guangshuo Li 2 participants 2 messages Nov 11

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: starfive: Fix potential per-CPU memory leak on probe failure

The per-CPU hardware events remain allocated when a later probe step fails, although the device-managed PMU structure is released.

Allocate the events with devm_alloc_percpu() so failed probing releases them after the interrupt handlers have been removed.

This issue was found by our static analysis tool under development.

Fixes c2b24812f7bc ("perf: starfive: Add StarLink PMU support")
---
 drivers/perf/starfive_starlink_pmu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/perf/starfive_starlink_pmu.c +2 -2
index d9843645554b..25d57b904028 100644
@@ -519,8 +519,8 @@ static int starlink_pmu_probe(struct platform_device *pdev)
 	if (IS_ERR(starlink_pmu->pmu_base))
 		return PTR_ERR(starlink_pmu->pmu_base);
 
-	starlink_pmu->hw_events = alloc_percpu_gfp(struct starlink_hw_events,
-						   GFP_KERNEL);
+	starlink_pmu->hw_events = devm_alloc_percpu(&pdev->dev,
+						    struct starlink_hw_events);
 	if (!starlink_pmu->hw_events) {
 		dev_err(&pdev->dev, "Failed to allocate per-cpu PMU data\n");
 		return -ENOMEM;

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

The per-CPU hardware events remain allocated when a later probe step fails, although the device-managed PMU structure is released.

Allocate the events with devm_alloc_percpu() so failed probing releases them after the interrupt handlers have been removed.

This issue was found by our static analysis tool under development.

Fixes c2b24812f7bc ("perf: starfive: Add StarLink PMU support")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1