Re: [PATCH 3/3] platform/x86: thinkpad_acpi: use __free(kfree) for automatic cleanup

From: Mark Pearson

Date: Wed Aug 12 2026 - 15:06:42 EST


Thanks Dmitry,

On Thu, Aug 6, 2026, at 2:19 AM, Dmitry Torokhov wrote:
> Use __free(kfree) for local pointer allocations in dispatch_proc_write(),
> tpacpi_brightness_get_ecnvram(), and auxmac_init().
>
> This ensures automatic memory cleanup when exiting function scope and
> removes explicit kfree() calls on exit paths.
>
> Assisted-by: Antigravity:gemini-3.6-flash
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
> ---
> drivers/platform/x86/lenovo/thinkpad_acpi.c | 40 +++++++--------------
> 1 file changed, 13 insertions(+), 27 deletions(-)
>
> diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c
> b/drivers/platform/x86/lenovo/thinkpad_acpi.c
> index 0d0d6fe7eecd..200e20f90a4b 100644
> --- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
> +++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
> @@ -885,7 +885,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)
> @@ -893,16 +892,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 = {
> @@ -6628,26 +6626,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;
> }
>
> /*
> @@ -10989,24 +10982,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);
> + strscpy(auxmac, "unavailable", sizeof(auxmac));
>
> + status = acpi_evaluate_object(NULL, "\\MACA", NULL, &buffer);
> if (ACPI_FAILURE(status))
> return -ENODEV;
>
> - obj = buffer.pointer;
> -
> + union acpi_object *obj __free(kfree) = buffer.pointer;
> if (obj->type != ACPI_TYPE_STRING || obj->string.length != AUXMAC_STRLEN) {
> pr_info("Invalid buffer for MAC address pass-through.\n");
> - goto auxmacinvalid;
> + 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;
> + return 0;
> }
>
> if (strncmp(obj->string.pointer + AUXMAC_START, "XXXXXXXXXXXX",
> AUXMAC_LEN) != 0)
> @@ -11014,13 +11006,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.55.0.679.g6767b8d81c-goog

Another kernel implementation I didn't know about.
It all looks good to me, and gives some nice clean-up. Thanks

Reviewed-by: Mark Pearson <mpearson-lenovo@xxxxxxxxx>
Mark