Skip to contentSkip to conversation

ACPI: PM: s2idle: Fix memory leak in lpi_device_get_constraints()

PATCH In review Kaushlendra Kumar 2 participants 2 messages Aug 19, 2025 – Aug 25, 2025 ACPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ACPI: PM: s2idle: Fix memory leak in lpi_device_get_constraints()

Add proper cleanup of lpi_constraints_table to prevent memory leaks when the driver is reloaded or lpi_device_get_constraints() is called multiple times.

The function lpi_device_get_constraints() allocates memory for lpi_constraints_table using kcalloc() but never frees any previously allocated memory. This leads to a memory leak on subsequent calls to the function.

Fix this by:
1. Adding a new helper function lpi_constraints_table_free() that properly frees the allocated memory and resets the table pointer and size
2. Calling this cleanup function before allocating new memory in lpi_device_get_constraints()

This ensures that any previously allocated lpi_constraints_table is properly freed before allocating a new one, preventing memory leaks.

---
 drivers/acpi/x86/s2idle.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/acpi/x86/s2idle.c +9 -0
index dd0b40b9bbe8..1ae954c0ef15 100644
@@ -189,6 +189,13 @@ static void lpi_device_get_constraints_amd(void)
 	ACPI_FREE(out_obj);
 }
 
+static void lpi_constraints_table_free(void)
+{
+	kfree(lpi_constraints_table);
+	lpi_constraints_table = NULL;
+	lpi_constraints_table_size = 0;
+}
+
 static void lpi_device_get_constraints(void)
 {
 	union acpi_object *out_obj;
@@ -203,6 +210,8 @@ static void lpi_device_get_constraints(void)
 
 	if (!out_obj)
 		return;
+	/* Function to free lpi_constraints_table before allocating a new one */
+	lpi_constraints_table_free();
 
 	lpi_constraints_table = kcalloc(out_obj->package.count,
 					sizeof(*lpi_constraints_table),
Signature · 1 line
2.34.1
#1 lore raw
Quoted 3 lines · Kaushlendra Kumar

Add proper cleanup of lpi_constraints_table to prevent memory leaks when the driver is reloaded

Which driver do you mean?

Quoted 1 line

or lpi_device_get_constraints() is called multiple times.

How exactly?

Quoted 4 lines

The function lpi_device_get_constraints() allocates memory for lpi_constraints_table using kcalloc() but never frees any previously allocated memory. This leads to a memory leak on subsequent calls to the function.

What subsequent calls?

Quoted 8 lines

Fix this by:
1. Adding a new helper function lpi_constraints_table_free() that properly frees the allocated memory and resets the table pointer and size
2. Calling this cleanup function before allocating new memory in lpi_device_get_constraints()

This ensures that any previously allocated lpi_constraints_table is properly freed before allocating a new one, preventing memory leaks.

No, really.

You need to say how exactly the memory is leaked, or there is nothing to fix.

Thanks!