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. Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@xxxxxxxxx> --- drivers/acpi/x86/s2idle.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/acpi/x86/s2idle.c b/drivers/acpi/x86/s2idle.c index dd0b40b9bbe8..1ae954c0ef15 100644 --- a/drivers/acpi/x86/s2idle.c +++ b/drivers/acpi/x86/s2idle.c @@ -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), -- 2.34.1