mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] platform/x86/amd: Fix memory leak in wbrf_record()
@ 2026-01-03 12:21 Zilin Guan
  2026-01-03 13:16 ` Markus Elfring
  2026-01-05 13:52 ` Ilpo Järvinen
  0 siblings, 2 replies; 5+ messages in thread
From: Zilin Guan @ 2026-01-03 12:21 UTC (permalink / raw)
  To: hansg
  Cc: ilpo.jarvinen, platform-driver-x86, linux-kernel, Markus.Elfring,
	Zilin Guan, Jianhao Xu

The tmp buffer is allocated using kcalloc() but is not freed if
acpi_evaluate_dsm() fails. This causes a memory leak in the error path.

Fix this by using the scope-based cleanup helper __free() for automatic
resource cleanup. This ensures that both the tmp buffer and the ACPI
object are automatically freed when they go out of scope, simplifying
error handling and preventing leaks.

Fixes: 58e82a62669d ("platform/x86/amd: Add support for AMD ACPI based Wifi band RFI mitigation feature")\
Suggested-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Suggested-by: Markus Elfring <Markus.Elfring@web.de>
Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
Changes in v2:
- using scope-based cleanup helper __free() for automatic resource cleanup.

 drivers/platform/x86/amd/wbrf.c | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/drivers/platform/x86/amd/wbrf.c b/drivers/platform/x86/amd/wbrf.c
index dd197b3aebe0..4517d139d768 100644
--- a/drivers/platform/x86/amd/wbrf.c
+++ b/drivers/platform/x86/amd/wbrf.c
@@ -39,11 +39,11 @@ static const guid_t wifi_acpi_dsm_guid =
  */
 static BLOCKING_NOTIFIER_HEAD(wbrf_chain_head);
 
+DEFINE_FREE(acpi_object, union acpi_object *, if (_T) ACPI_FREE(_T))
+
 static int wbrf_record(struct acpi_device *adev, uint8_t action, struct wbrf_ranges_in_out *in)
 {
 	union acpi_object argv4;
-	union acpi_object *tmp;
-	union acpi_object *obj;
 	u32 num_of_ranges = 0;
 	u32 num_of_elements;
 	u32 arg_idx = 0;
@@ -74,7 +74,7 @@ static int wbrf_record(struct acpi_device *adev, uint8_t action, struct wbrf_ran
 	 */
 	num_of_elements = 2 * num_of_ranges + 2;
 
-	tmp = kcalloc(num_of_elements, sizeof(*tmp), GFP_KERNEL);
+	union acpi_object *tmp __free(kfree) = kcalloc(num_of_elements, sizeof(*tmp), GFP_KERNEL);
 	if (!tmp)
 		return -ENOMEM;
 
@@ -101,25 +101,20 @@ static int wbrf_record(struct acpi_device *adev, uint8_t action, struct wbrf_ran
 		tmp[arg_idx++].integer.value = in->band_list[i].end;
 	}
 
-	obj = acpi_evaluate_dsm(adev->handle, &wifi_acpi_dsm_guid,
-				WBRF_REVISION, WBRF_RECORD, &argv4);
+	union acpi_object *obj __free(acpi_object) =
+		acpi_evaluate_dsm(adev->handle, &wifi_acpi_dsm_guid,
+				  WBRF_REVISION, WBRF_RECORD, &argv4);
 
 	if (!obj)
 		return -EINVAL;
 
-	if (obj->type != ACPI_TYPE_INTEGER) {
-		ret = -EINVAL;
-		goto out;
-	}
+	if (obj->type != ACPI_TYPE_INTEGER)
+		return -EINVAL;
 
 	ret = obj->integer.value;
 	if (ret)
 		ret = -EINVAL;
 
-out:
-	ACPI_FREE(obj);
-	kfree(tmp);
-
 	return ret;
 }
 
-- 
2.34.1


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

end of thread, other threads:[~2026-01-06  9:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-03 12:21 [PATCH v2] platform/x86/amd: Fix memory leak in wbrf_record() Zilin Guan
2026-01-03 13:16 ` Markus Elfring
2026-01-04  5:27   ` Zilin Guan
2026-01-05 13:52 ` Ilpo Järvinen
2026-01-06  9:11   ` Zilin Guan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®