linux-acpi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ACPI: PM: s2idle: Fix memory leak in lpi_device_get_constraints()
@ 2025-08-19  7:35 Kaushlendra Kumar
  2025-08-25 17:55 ` Rafael J. Wysocki
  0 siblings, 1 reply; 2+ messages in thread
From: Kaushlendra Kumar @ 2025-08-19  7:35 UTC (permalink / raw)
  To: rafael, lenb; +Cc: linux-acpi, Kaushlendra Kumar

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@intel.com>
---
 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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ACPI: PM: s2idle: Fix memory leak in lpi_device_get_constraints()
  2025-08-19  7:35 [PATCH] ACPI: PM: s2idle: Fix memory leak in lpi_device_get_constraints() Kaushlendra Kumar
@ 2025-08-25 17:55 ` Rafael J. Wysocki
  0 siblings, 0 replies; 2+ messages in thread
From: Rafael J. Wysocki @ 2025-08-25 17:55 UTC (permalink / raw)
  To: Kaushlendra Kumar; +Cc: rafael, lenb, linux-acpi

On Tue, Aug 19, 2025 at 9:36 AM Kaushlendra Kumar
<kaushlendra.kumar@intel.com> wrote:
>
> Add proper cleanup of lpi_constraints_table to prevent memory leaks
> when  the driver is reloaded

Which driver do you mean?

> or lpi_device_get_constraints() is called multiple times.

How exactly?

> 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?

> 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!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-08-25 17:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-19  7:35 [PATCH] ACPI: PM: s2idle: Fix memory leak in lpi_device_get_constraints() Kaushlendra Kumar
2025-08-25 17:55 ` Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).