Re: [PATCH 06/22] Input: kxtj9 - use guard notation when acquiring mutex/disabling irq

From: Javier Carrasco
Date: Wed Sep 04 2024 - 15:03:45 EST


On 04/09/2024 06:42, Dmitry Torokhov wrote:
> Using guard notation makes the code more compact and error handling
> more robust by ensuring that mutexes are released and interrupts are
> re-enabled in all code paths when control leaves critical section.
>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
> ---
> drivers/input/misc/kxtj9.c | 14 ++++----------
> 1 file changed, 4 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/input/misc/kxtj9.c b/drivers/input/misc/kxtj9.c
> index 837682cb2a7d..c6146bcee9f9 100644
> --- a/drivers/input/misc/kxtj9.c
> +++ b/drivers/input/misc/kxtj9.c
> @@ -314,9 +314,8 @@ static ssize_t kxtj9_set_poll(struct device *dev, struct device_attribute *attr,
> return error;
>
> /* Lock the device to prevent races with open/close (and itself) */
> - mutex_lock(&input_dev->mutex);
> -
> - disable_irq(client->irq);
> + guard(mutex)(&input_dev->mutex);
> + guard(disable_irq)(&client->irq);
>
> /*
> * Set current interval to the greater of the minimum interval or
> @@ -326,9 +325,6 @@ static ssize_t kxtj9_set_poll(struct device *dev, struct device_attribute *attr,
>
> kxtj9_update_odr(tj9, tj9->last_poll_interval);
>
> - enable_irq(client->irq);
> - mutex_unlock(&input_dev->mutex);
> -
> return count;
> }
>
> @@ -504,12 +500,11 @@ static int kxtj9_suspend(struct device *dev)
> struct kxtj9_data *tj9 = i2c_get_clientdata(client);
> struct input_dev *input_dev = tj9->input_dev;
>
> - mutex_lock(&input_dev->mutex);
> + guard(mutex)(&input_dev->mutex);
>
> if (input_device_enabled(input_dev))
> kxtj9_disable(tj9);
>
> - mutex_unlock(&input_dev->mutex);
> return 0;
> }
>
> @@ -519,12 +514,11 @@ static int kxtj9_resume(struct device *dev)
> struct kxtj9_data *tj9 = i2c_get_clientdata(client);
> struct input_dev *input_dev = tj9->input_dev;
>
> - mutex_lock(&input_dev->mutex);
> + guard(mutex)(&input_dev->mutex);
>
> if (input_device_enabled(input_dev))
> kxtj9_enable(tj9);
>
> - mutex_unlock(&input_dev->mutex);
> return 0;
> }
>

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@xxxxxxxxx>