[PATCH 3/5] hwmon: (gpio-fan) Ensure lock is held during set_fan_speed()

Alexander Stein posted 5 patches 10 months, 1 week ago
[PATCH 3/5] hwmon: (gpio-fan) Ensure lock is held during set_fan_speed()
Posted by Alexander Stein 10 months, 1 week ago
Instead of just documenting by comment, ensure locking per code.

Signed-off-by: Alexander Stein <alexander.stein@ew.tq-group.com>
---
 drivers/hwmon/gpio-fan.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/gpio-fan.c b/drivers/hwmon/gpio-fan.c
index b779240328d59..322b161d5ca1f 100644
--- a/drivers/hwmon/gpio-fan.c
+++ b/drivers/hwmon/gpio-fan.c
@@ -124,9 +124,10 @@ static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
 	return ctrl_val;
 }
 
-/* Must be called with fan_data->lock held, except during initialization. */
 static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
 {
+	WARN_ON_ONCE(!mutex_is_locked(&fan_data->lock));
+
 	if (fan_data->speed_index == speed_index)
 		return;
 
-- 
2.34.1
Re: [PATCH 3/5] hwmon: (gpio-fan) Ensure lock is held during set_fan_speed()
Posted by Guenter Roeck 9 months, 2 weeks ago
On Mon, Feb 10, 2025 at 03:59:31PM +0100, Alexander Stein wrote:
> Instead of just documenting by comment, ensure locking per code.
> 
> Signed-off-by: Alexander Stein <alexander.stein@ew.tq-group.com>
> ---
>  drivers/hwmon/gpio-fan.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/gpio-fan.c b/drivers/hwmon/gpio-fan.c
> index b779240328d59..322b161d5ca1f 100644
> --- a/drivers/hwmon/gpio-fan.c
> +++ b/drivers/hwmon/gpio-fan.c
> @@ -124,9 +124,10 @@ static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
>  	return ctrl_val;
>  }
>  
> -/* Must be called with fan_data->lock held, except during initialization. */
>  static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
>  {
> +	WARN_ON_ONCE(!mutex_is_locked(&fan_data->lock));
> +

No, this just increases code size for no good reason. The comment is
perfectly fine and sufficient.

Guenter