[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <3ff97fb3-27e0-496e-a8b0-0c2d69deeff2@gmail.com>
Date: Wed, 4 Sep 2024 21:27:52 +0200
From: Javier Carrasco <javier.carrasco.cruz@...il.com>
To: Dmitry Torokhov <dmitry.torokhov@...il.com>, linux-input@...r.kernel.org
Cc: Michael Hennerich <michael.hennerich@...log.com>,
Ville Syrjala <syrjala@....fi>,
Support Opensource <support.opensource@...semi.com>,
Eddie James <eajames@...ux.ibm.com>, Andrey Moiseev <o2g.org.ru@...il.com>,
Hans de Goede <hdegoede@...hat.com>, Jeff LaBundy <jeff@...undy.com>,
linux-kernel@...r.kernel.org,
Javier Carrasco Cruz <javier.carrasco.cruz@...il.com>
Subject: Re: [PATCH 20/22] Input: regulator-haptic - use guard notation when
acquiring mutex
On 04/09/2024 06:49, Dmitry Torokhov wrote:
> Using guard notation makes the code more compact and error handling
> more robust by ensuring that mutexes are released in all code paths
> when control leaves critical section.
>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@...il.com>
> ---
> drivers/input/misc/regulator-haptic.c | 23 ++++++++---------------
> 1 file changed, 8 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/input/misc/regulator-haptic.c b/drivers/input/misc/regulator-haptic.c
> index 02f73b7c0462..41af6aefaa07 100644
> --- a/drivers/input/misc/regulator-haptic.c
> +++ b/drivers/input/misc/regulator-haptic.c
> @@ -83,12 +83,10 @@ static void regulator_haptic_work(struct work_struct *work)
> struct regulator_haptic *haptic = container_of(work,
> struct regulator_haptic, work);
>
> - mutex_lock(&haptic->mutex);
> + guard(mutex)(&haptic->mutex);
>
> if (!haptic->suspended)
> regulator_haptic_set_voltage(haptic, haptic->magnitude);
> -
> - mutex_unlock(&haptic->mutex);
> }
>
> static int regulator_haptic_play_effect(struct input_dev *input, void *data,
> @@ -207,17 +205,14 @@ static int regulator_haptic_suspend(struct device *dev)
> struct regulator_haptic *haptic = platform_get_drvdata(pdev);
error became an unused variable and can be dropped.
> int error;
>
> - error = mutex_lock_interruptible(&haptic->mutex);
> - if (error)
> - return error;
> -
> - regulator_haptic_set_voltage(haptic, 0);
> -
> - haptic->suspended = true;
> + scoped_guard(mutex_intr, &haptic->mutex) {
> + regulator_haptic_set_voltage(haptic, 0);
> + haptic->suspended = true;
>
> - mutex_unlock(&haptic->mutex);
> + return 0;
> + }
>
> - return 0;
> + return -EINTR;
> }
>
> static int regulator_haptic_resume(struct device *dev)
> @@ -226,7 +221,7 @@ static int regulator_haptic_resume(struct device *dev)
> struct regulator_haptic *haptic = platform_get_drvdata(pdev);
> unsigned int magnitude;
>
> - mutex_lock(&haptic->mutex);
> + guard(mutex)(&haptic->mutex);
>
> haptic->suspended = false;
>
> @@ -234,8 +229,6 @@ static int regulator_haptic_resume(struct device *dev)
> if (magnitude)
> regulator_haptic_set_voltage(haptic, magnitude);
>
> - mutex_unlock(&haptic->mutex);
> -
> return 0;
> }
>
Powered by blists - more mailing lists