[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <f0944d58-c53c-4d31-98f3-dbe21d27d285@wanadoo.fr>
Date: Wed, 3 Jul 2024 20:03:15 +0200
From: Christophe JAILLET <christophe.jaillet@...adoo.fr>
To: Krzysztof Kozlowski <krzysztof.kozlowski@...aro.org>,
Pali Rohár <pali@...nel.org>,
Jean Delvare <jdelvare@...e.com>, Guenter Roeck <linux@...ck-us.net>,
linux-hwmon@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] hwmon: (dell-smm) Simplify with cleanup.h
Le 03/07/2024 à 10:31, Krzysztof Kozlowski a écrit :
> Allocate memory, which is being freed at end of the scope, to make the
> code a bit simpler.
>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@...aro.org>
> ---
> drivers/hwmon/dell-smm-hwmon.c | 7 +++----
> 1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/hwmon/dell-smm-hwmon.c b/drivers/hwmon/dell-smm-hwmon.c
> index 0362a13f6525..e72e26db6e10 100644
> --- a/drivers/hwmon/dell-smm-hwmon.c
> +++ b/drivers/hwmon/dell-smm-hwmon.c
> @@ -14,6 +14,7 @@
>
> #include <linux/acpi.h>
> #include <linux/capability.h>
> +#include <linux/cleanup.h>
> #include <linux/cpu.h>
> #include <linux/ctype.h>
> #include <linux/delay.h>
> @@ -1095,9 +1096,9 @@ static int dell_smm_init_cdev(struct device *dev, u8 fan_num)
> struct thermal_cooling_device *cdev;
> struct dell_smm_cooling_data *cdata;
> int ret = 0;
> - char *name;
>
> - name = kasprintf(GFP_KERNEL, "dell-smm-fan%u", fan_num + 1);
> + char *name __free(kfree) = kasprintf(GFP_KERNEL, "dell-smm-fan%u",
> + fan_num + 1);
> if (!name)
> return -ENOMEM;
>
> @@ -1115,8 +1116,6 @@ static int dell_smm_init_cdev(struct device *dev, u8 fan_num)
> ret = -ENOMEM;
> }
>
> - kfree(name);
> -
> return ret;
> }
>
Hi,
going one step further, this could even be:
cdata = devm_kmalloc(dev, sizeof(*cdata), GFP_KERNEL);
if (!cdata)
return -ENOMEM;
cdata->fan_num = fan_num;
cdata->data = data;
cdev = devm_thermal_of_cooling_device_register(dev, NULL, name, cdata,
&dell_smm_cooling_ops);
if (IS_ERR(cdev)) {
devm_kfree(dev, cdata);
return PTR_ERR(cdev);
}
return 0;
This reduces indentation and is slightly more common when testing after
devm_kmalloc()
Just my 2c,
CJ
Powered by blists - more mailing lists