diff mbox series

hwmon: (asus_wmi_sensors) Save a few bytes of memory

Message ID 3a494f7049f4f9a169477d872bab0c8a7c7ec48c.1656697596.git.christophe.jaillet@wanadoo.fr (mailing list archive)
State Changes Requested
Headers show
Series hwmon: (asus_wmi_sensors) Save a few bytes of memory | expand

Commit Message

Christophe JAILLET July 1, 2022, 5:47 p.m. UTC
The first 'for' loop of asus_wmi_configure_sensor_setup() only computes
the number and type of sensors that exist in the system.

Here, the 'temp_sensor' structure is only used to store the data collected
by asus_wmi_sensor_info(). There is no point in using a devm_ variant for
this first allocation and it can be freed just after this initial loop.

So use kzalloc()/kfree() to save a few bytes of memory that would be kept
allocated for no good reason otherwise.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 drivers/hwmon/asus_wmi_sensors.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Guenter Roeck July 1, 2022, 5:52 p.m. UTC | #1
On 7/1/22 10:47, Christophe JAILLET wrote:
> The first 'for' loop of asus_wmi_configure_sensor_setup() only computes
> the number and type of sensors that exist in the system.
> 
> Here, the 'temp_sensor' structure is only used to store the data collected
> by asus_wmi_sensor_info(). There is no point in using a devm_ variant for
> this first allocation and it can be freed just after this initial loop.
> 
> So use kzalloc()/kfree() to save a few bytes of memory that would be kept
> allocated for no good reason otherwise.

Then why isn't this just allocated on the stack ? The structure isn't _that_
large.

Guenter

> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
>   drivers/hwmon/asus_wmi_sensors.c | 7 +++++--
>   1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/hwmon/asus_wmi_sensors.c b/drivers/hwmon/asus_wmi_sensors.c
> index 9e935e34c998..4873edad4996 100644
> --- a/drivers/hwmon/asus_wmi_sensors.c
> +++ b/drivers/hwmon/asus_wmi_sensors.c
> @@ -514,14 +514,16 @@ static int asus_wmi_configure_sensor_setup(struct device *dev,
>   	int i, idx;
>   	int err;
>   
> -	temp_sensor = devm_kcalloc(dev, 1, sizeof(*temp_sensor), GFP_KERNEL);
> +	temp_sensor = kzalloc(sizeof(*temp_sensor), GFP_KERNEL);
>   	if (!temp_sensor)
>   		return -ENOMEM;
>   
>   	for (i = 0; i < sensor_data->wmi.sensor_count; i++) {
>   		err = asus_wmi_sensor_info(i, temp_sensor);
> -		if (err)
> +		if (err) {
> +			kfree(temp_sensor);
>   			return err;
> +		}
>   
>   		switch (temp_sensor->data_type) {
>   		case TEMPERATURE_C:
> @@ -536,6 +538,7 @@ static int asus_wmi_configure_sensor_setup(struct device *dev,
>   			break;
>   		}
>   	}
> +	kfree(temp_sensor);
>   
>   	if (nr_count[hwmon_temp])
>   		nr_count[hwmon_chip]++, nr_types++;
diff mbox series

Patch

diff --git a/drivers/hwmon/asus_wmi_sensors.c b/drivers/hwmon/asus_wmi_sensors.c
index 9e935e34c998..4873edad4996 100644
--- a/drivers/hwmon/asus_wmi_sensors.c
+++ b/drivers/hwmon/asus_wmi_sensors.c
@@ -514,14 +514,16 @@  static int asus_wmi_configure_sensor_setup(struct device *dev,
 	int i, idx;
 	int err;
 
-	temp_sensor = devm_kcalloc(dev, 1, sizeof(*temp_sensor), GFP_KERNEL);
+	temp_sensor = kzalloc(sizeof(*temp_sensor), GFP_KERNEL);
 	if (!temp_sensor)
 		return -ENOMEM;
 
 	for (i = 0; i < sensor_data->wmi.sensor_count; i++) {
 		err = asus_wmi_sensor_info(i, temp_sensor);
-		if (err)
+		if (err) {
+			kfree(temp_sensor);
 			return err;
+		}
 
 		switch (temp_sensor->data_type) {
 		case TEMPERATURE_C:
@@ -536,6 +538,7 @@  static int asus_wmi_configure_sensor_setup(struct device *dev,
 			break;
 		}
 	}
+	kfree(temp_sensor);
 
 	if (nr_count[hwmon_temp])
 		nr_count[hwmon_chip]++, nr_types++;