[PATCH v2 2/2] platform/x86: thinkpad_acpi: use __free(kfree) for automatic cleanup
From: Dmitry Torokhov
Date: Wed Sep 30 2026 - 01:02:10 EST
Use __free(kfree) for local pointer allocations in
dispatch_proc_write(), tpacpi_evaluate_bcl(), and auxmac_init().
This ensures automatic memory cleanup when exiting function scope and
removes explicit kfree() calls on exit paths.
Assisted-by: LLM
Reviewed-by: Mark Pearson <mpearson-lenovo@xxxxxxxxx>
Tested-by: Ali Ahmet Memiş <aliamemis@xxxxxxxxxxx>
Tested-by: Mark Pearson <mpearson-lenovo@xxxxxxxxx>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
drivers/platform/x86/lenovo/thinkpad_acpi.c | 42 ++++++++++-------------------
1 file changed, 14 insertions(+), 28 deletions(-)
diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 645df773fe63..93fc69a7dd63 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -891,7 +891,6 @@ static ssize_t dispatch_proc_write(struct file *file,
size_t count, loff_t *pos)
{
struct ibm_struct *ibm = pde_data(file_inode(file));
- char *kernbuf;
int ret;
if (!ibm || !ibm->write)
@@ -899,16 +898,15 @@ static ssize_t dispatch_proc_write(struct file *file,
if (count > PAGE_SIZE - 1)
return -EINVAL;
- kernbuf = memdup_user_nul(userbuf, count);
+ char *kernbuf __free(kfree) = memdup_user_nul(userbuf, count);
if (IS_ERR(kernbuf))
return PTR_ERR(kernbuf);
- ret = ibm->write(kernbuf);
- if (ret == 0)
- ret = count;
- kfree(kernbuf);
+ ret = ibm->write(kernbuf);
+ if (ret)
+ return ret;
- return ret;
+ return count;
}
static const struct proc_ops dispatch_proc_ops = {
@@ -6711,26 +6709,21 @@ static const struct backlight_ops ibm_backlight_data = {
static int __init tpacpi_evaluate_bcl(struct acpi_device *adev, void *not_used)
{
struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
- union acpi_object *obj;
acpi_status status;
- int rc;
status = acpi_evaluate_object(adev->handle, "_BCL", NULL, &buffer);
if (ACPI_FAILURE(status))
return 0;
- obj = buffer.pointer;
+ union acpi_object *obj __free(kfree) = buffer.pointer;
if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
acpi_handle_info(adev->handle,
"Unknown _BCL data, please report this to %s\n",
TPACPI_MAIL);
- rc = 0;
- } else {
- rc = obj->package.count;
+ return 0;
}
- kfree(obj);
- return rc;
+ return obj->package.count;
}
/*
@@ -11075,24 +11068,23 @@ static int auxmac_init(struct ibm_init_struct *iibm)
{
acpi_status status;
struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
- union acpi_object *obj;
status = acpi_evaluate_object(NULL, "\\MACA", NULL, &buffer);
-
if (ACPI_FAILURE(status))
return -ENODEV;
- obj = buffer.pointer;
-
- if (obj->type != ACPI_TYPE_STRING || obj->string.length != AUXMAC_STRLEN) {
+ union acpi_object *obj __free(kfree) = buffer.pointer;
+ if (!obj || obj->type != ACPI_TYPE_STRING || obj->string.length != AUXMAC_STRLEN) {
pr_info("Invalid buffer for MAC address pass-through.\n");
- goto auxmacinvalid;
+ strscpy(auxmac, "unavailable", sizeof(auxmac));
+ return 0;
}
if (obj->string.pointer[AUXMAC_BEGIN_MARKER] != '#' ||
obj->string.pointer[AUXMAC_END_MARKER] != '#') {
pr_info("Invalid header for MAC address pass-through.\n");
- goto auxmacinvalid;
+ strscpy(auxmac, "unavailable", sizeof(auxmac));
+ return 0;
}
if (strncmp(obj->string.pointer + AUXMAC_START, "XXXXXXXXXXXX", AUXMAC_LEN) != 0)
@@ -11100,13 +11092,7 @@ static int auxmac_init(struct ibm_init_struct *iibm)
else
strscpy(auxmac, "disabled", sizeof(auxmac));
-free:
- kfree(obj);
return 0;
-
-auxmacinvalid:
- strscpy(auxmac, "unavailable", sizeof(auxmac));
- goto free;
}
static struct ibm_struct auxmac_data = {
--
2.56.0.rc1.315.gc6ed9934b7-goog