[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <f74cb5d9-3489-4aef-a43e-0989132cf344@gmail.com>
Date: Wed, 4 Sep 2024 21:32:35 +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 21/22] Input: rotary_encoder - 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/rotary_encoder.c | 23 ++++++++---------------
> 1 file changed, 8 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c
> index 6628fe540834..52761da9f999 100644
> --- a/drivers/input/misc/rotary_encoder.c
> +++ b/drivers/input/misc/rotary_encoder.c
> @@ -113,7 +113,7 @@ static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
> struct rotary_encoder *encoder = dev_id;
> unsigned int state;
>
> - mutex_lock(&encoder->access_mutex);
> + guard(mutex)(&encoder->access_mutex);
>
> state = rotary_encoder_get_state(encoder);
>
> @@ -136,8 +136,6 @@ static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
> break;
> }
>
> - mutex_unlock(&encoder->access_mutex);
> -
> return IRQ_HANDLED;
> }
>
> @@ -146,7 +144,7 @@ static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
> struct rotary_encoder *encoder = dev_id;
> unsigned int state;
>
> - mutex_lock(&encoder->access_mutex);
> + guard(mutex)(&encoder->access_mutex);
>
> state = rotary_encoder_get_state(encoder);
>
> @@ -159,8 +157,6 @@ static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
> }
> }
>
> - mutex_unlock(&encoder->access_mutex);
> -
> return IRQ_HANDLED;
> }
>
> @@ -169,22 +165,19 @@ static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
> struct rotary_encoder *encoder = dev_id;
> unsigned int state;
>
> - mutex_lock(&encoder->access_mutex);
> + guard(mutex)(&encoder->access_mutex);
>
> state = rotary_encoder_get_state(encoder);
>
> - if ((encoder->last_stable + 1) % 4 == state)
> + if ((encoder->last_stable + 1) % 4 == state) {
> encoder->dir = 1;
> - else if (encoder->last_stable == (state + 1) % 4)
> + rotary_encoder_report_event(encoder);
> + } else if (encoder->last_stable == (state + 1) % 4) {
> encoder->dir = -1;
> - else
> - goto out;
> -
> - rotary_encoder_report_event(encoder);
> + rotary_encoder_report_event(encoder);
> + }
>
> -out:
> encoder->last_stable = state;
> - mutex_unlock(&encoder->access_mutex);
>
> return IRQ_HANDLED;
> }
Reviewed-by: Javier Carrasco <javier.carrasco.cruz@...il.com>
Powered by blists - more mailing lists