Skip to contentSkip to conversation

PM: EM: Fix memory leak in em_create_pd() error path

PATCH Applied Malaya Kumar Rout 3 participants 3 messages Jan 5 – Jan 8

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

PATCH 1/1 PM: EM: Fix memory leak in em_create_pd() error path

When ida_alloc() fails in em_create_pd(), the function returns without freeing the previously allocated 'pd' structure, leading to a memory leak. The 'pd' pointer is allocated either at line 436 (for CPU devices with cpumask) or line 442 (for other devices) using kzalloc().

Additionally, the function incorrectly returns -ENOMEM when ida_alloc() fails, ignoring the actual error code returned by ida_alloc(), which can fail for reasons other than memory exhaustion.

Fix both issues by:
1. Freeing the 'pd' structure with kfree() when ida_alloc() fails
2. Returning the actual error code from ida_alloc() instead of -ENOMEM

This ensures proper cleanup on the error path and accurate error reporting.

Fixes cbe5aeedecc7 ("PM: EM: Assign a unique ID when creating a performance domain")
---
 kernel/power/energy_model.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/power/energy_model.c +4 -2
index 11af9f64aa82..5b055cbe5341 100644
@@ -449,8 +449,10 @@ static int em_create_pd(struct device *dev, int nr_states,
 	INIT_LIST_HEAD(&pd->node);
 
 	id = ida_alloc(&em_pd_ida, GFP_KERNEL);
-	if (id < 0)
-		return -ENOMEM;
+	if (id < 0) {
+		kfree(pd);
+		return id;
+	}
 	pd->id = id;
 
 	em_table = em_table_alloc(pd);
Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw

Thanks, Malaya, for the bug fix. This makes sense to me.

Quoted 38 lines · Malaya Kumar Rout

When ida_alloc() fails in em_create_pd(), the function returns without freeing the previously allocated 'pd' structure, leading to a memory leak. The 'pd' pointer is allocated either at line 436 (for CPU devices with cpumask) or line 442 (for other devices) using kzalloc().

Additionally, the function incorrectly returns -ENOMEM when ida_alloc() fails, ignoring the actual error code returned by ida_alloc(), which can fail for reasons other than memory exhaustion.

Fix both issues by:
1. Freeing the 'pd' structure with kfree() when ida_alloc() fails
2. Returning the actual error code from ida_alloc() instead of -ENOMEM

This ensures proper cleanup on the error path and accurate error reporting.

Fixes cbe5aeedecc7 ("PM: EM: Assign a unique ID when creating a performance domain")
---
  kernel/power/energy_model.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
kernel/power/energy_model.c +4 -2
index 11af9f64aa82..5b055cbe5341 100644
@@ -449,8 +449,10 @@ static int em_create_pd(struct device *dev, int nr_states,
  	INIT_LIST_HEAD(&pd->node);
 
  	id = ida_alloc(&em_pd_ida, GFP_KERNEL);
-	if (id < 0)
-		return -ENOMEM;
+	if (id < 0) {
+		kfree(pd);
+		return id;
+	}
  	pd->id = id;
 
  	em_table = em_table_alloc(pd);
maintainer Applied
#2 lore raw
Quoted 4 lines · Changwoo Min

Thanks, Malaya, for the bug fix. This makes sense to me.

Applied as 6.19-rc material, thanks!

Quoted 41 lines · Malaya Kumar Rout
Quoted 38 lines

When ida_alloc() fails in em_create_pd(), the function returns without freeing the previously allocated 'pd' structure, leading to a memory leak. The 'pd' pointer is allocated either at line 436 (for CPU devices with cpumask) or line 442 (for other devices) using kzalloc().

Additionally, the function incorrectly returns -ENOMEM when ida_alloc() fails, ignoring the actual error code returned by ida_alloc(), which can fail for reasons other than memory exhaustion.

Fix both issues by:
1. Freeing the 'pd' structure with kfree() when ida_alloc() fails
2. Returning the actual error code from ida_alloc() instead of -ENOMEM

This ensures proper cleanup on the error path and accurate error reporting.

Fixes cbe5aeedecc7 ("PM: EM: Assign a unique ID when creating a performance domain")
---
  kernel/power/energy_model.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
kernel/power/energy_model.c +4 -2
index 11af9f64aa82..5b055cbe5341 100644
@@ -449,8 +449,10 @@ static int em_create_pd(struct device *dev, int nr_states,
      INIT_LIST_HEAD(&pd->node);
 
      id = ida_alloc(&em_pd_ida, GFP_KERNEL);
-     if (id < 0)
-             return -ENOMEM;
+     if (id < 0) {
+             kfree(pd);
+             return id;
+     }
      pd->id = id;
 
      em_table = em_table_alloc(pd);