linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Javier Carrasco <javier.carrasco.cruz@gmail.com>
To: Dmitry Torokhov <dmitry.torokhov@gmail.com>, linux-input@vger.kernel.org
Cc: Michael Hennerich <michael.hennerich@analog.com>,
	Ville Syrjala <syrjala@sci.fi>,
	Support Opensource <support.opensource@diasemi.com>,
	Eddie James <eajames@linux.ibm.com>,
	Andrey Moiseev <o2g.org.ru@gmail.com>,
	Hans de Goede <hdegoede@redhat.com>,
	Jeff LaBundy <jeff@labundy.com>,
	linux-kernel@vger.kernel.org,
	Javier Carrasco Cruz <javier.carrasco.cruz@gmail.com>
Subject: Re: [PATCH 03/22] Input: cm109 - use guard notation when acquiring mutex and spinlock
Date: Wed, 4 Sep 2024 21:44:54 +0200	[thread overview]
Message-ID: <83199f0f-67a9-4fe7-9cbe-f3821ad4c041@gmail.com> (raw)
In-Reply-To: <20240904044244.1042174-4-dmitry.torokhov@gmail.com>

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 locks are released in all code paths
> when control leaves critical section.
> 
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/misc/cm109.c | 167 ++++++++++++++++++-------------------
>  1 file changed, 79 insertions(+), 88 deletions(-)
> 
> diff --git a/drivers/input/misc/cm109.c b/drivers/input/misc/cm109.c
> index 728325a2d574..0cfe5d4a573c 100644
> --- a/drivers/input/misc/cm109.c
> +++ b/drivers/input/misc/cm109.c
> @@ -355,6 +355,35 @@ static void cm109_submit_buzz_toggle(struct cm109_dev *dev)
>  			__func__, error);
>  }
>  
> +static void cm109_submit_ctl(struct cm109_dev *dev)
> +{
> +	int error;
> +
> +	guard(spinlock_irqsave)(&dev->ctl_submit_lock);
> +
> +	dev->irq_urb_pending = 0;
> +
> +	if (unlikely(dev->shutdown))
> +		return;
> +
> +	if (dev->buzzer_state)
> +		dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
> +	else
> +		dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
> +
> +	dev->ctl_data->byte[HID_OR1] = dev->keybit;
> +	dev->ctl_data->byte[HID_OR2] = dev->keybit;
> +
> +	dev->buzzer_pending = 0;
> +	dev->ctl_urb_pending = 1;
> +
> +	error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
> +	if (error)
> +		dev_err(&dev->intf->dev,
> +			"%s: usb_submit_urb (urb_ctl) failed %d\n",
> +			__func__, error);
> +}
> +
>  /*
>   * IRQ handler
>   */
> @@ -362,8 +391,6 @@ static void cm109_urb_irq_callback(struct urb *urb)
>  {
>  	struct cm109_dev *dev = urb->context;
>  	const int status = urb->status;
> -	int error;
> -	unsigned long flags;
>  
>  	dev_dbg(&dev->intf->dev, "### URB IRQ: [0x%02x 0x%02x 0x%02x 0x%02x] keybit=0x%02x\n",
>  	     dev->irq_data->byte[0],
> @@ -401,32 +428,7 @@ static void cm109_urb_irq_callback(struct urb *urb)
>  	}
>  
>   out:
> -
> -	spin_lock_irqsave(&dev->ctl_submit_lock, flags);
> -
> -	dev->irq_urb_pending = 0;
> -
> -	if (likely(!dev->shutdown)) {
> -
> -		if (dev->buzzer_state)
> -			dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
> -		else
> -			dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
> -
> -		dev->ctl_data->byte[HID_OR1] = dev->keybit;
> -		dev->ctl_data->byte[HID_OR2] = dev->keybit;
> -
> -		dev->buzzer_pending = 0;
> -		dev->ctl_urb_pending = 1;
> -
> -		error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
> -		if (error)
> -			dev_err(&dev->intf->dev,
> -				"%s: usb_submit_urb (urb_ctl) failed %d\n",
> -				__func__, error);
> -	}
> -
> -	spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
> +	cm109_submit_ctl(dev);
>  }
>  
>  static void cm109_urb_ctl_callback(struct urb *urb)
> @@ -434,7 +436,6 @@ static void cm109_urb_ctl_callback(struct urb *urb)
>  	struct cm109_dev *dev = urb->context;
>  	const int status = urb->status;
>  	int error;
> -	unsigned long flags;
>  
>  	dev_dbg(&dev->intf->dev, "### URB CTL: [0x%02x 0x%02x 0x%02x 0x%02x]\n",
>  	     dev->ctl_data->byte[0],
> @@ -449,35 +450,31 @@ static void cm109_urb_ctl_callback(struct urb *urb)
>  				    __func__, status);
>  	}
>  
> -	spin_lock_irqsave(&dev->ctl_submit_lock, flags);
> +	guard(spinlock_irqsave)(&dev->ctl_submit_lock);
>  
>  	dev->ctl_urb_pending = 0;
>  
> -	if (likely(!dev->shutdown)) {
> -
> -		if (dev->buzzer_pending || status) {
> -			dev->buzzer_pending = 0;
> -			dev->ctl_urb_pending = 1;
> -			cm109_submit_buzz_toggle(dev);
> -		} else if (likely(!dev->irq_urb_pending)) {
> -			/* ask for key data */
> -			dev->irq_urb_pending = 1;
> -			error = usb_submit_urb(dev->urb_irq, GFP_ATOMIC);
> -			if (error)
> -				dev_err(&dev->intf->dev,
> -					"%s: usb_submit_urb (urb_irq) failed %d\n",
> -					__func__, error);
> -		}
> -	}
> +	if (unlikely(dev->shutdown))
> +		return;
>  
> -	spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
> +	if (dev->buzzer_pending || status) {
> +		dev->buzzer_pending = 0;
> +		dev->ctl_urb_pending = 1;
> +		cm109_submit_buzz_toggle(dev);
> +	} else if (likely(!dev->irq_urb_pending)) {
> +		/* ask for key data */
> +		dev->irq_urb_pending = 1;
> +		error = usb_submit_urb(dev->urb_irq, GFP_ATOMIC);
> +		if (error)
> +			dev_err(&dev->intf->dev,
> +				"%s: usb_submit_urb (urb_irq) failed %d\n",
> +				__func__, error);
> +	}
>  }
>  
>  static void cm109_toggle_buzzer_async(struct cm109_dev *dev)
>  {
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(&dev->ctl_submit_lock, flags);
> +	guard(spinlock_irqsave)(&dev->ctl_submit_lock);
>  
>  	if (dev->ctl_urb_pending) {
>  		/* URB completion will resubmit */
> @@ -486,8 +483,6 @@ static void cm109_toggle_buzzer_async(struct cm109_dev *dev)
>  		dev->ctl_urb_pending = 1;
>  		cm109_submit_buzz_toggle(dev);
>  	}
> -
> -	spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
>  }
>  
>  static void cm109_toggle_buzzer_sync(struct cm109_dev *dev, int on)
> @@ -556,32 +551,30 @@ static int cm109_input_open(struct input_dev *idev)
>  		return error;
>  	}
>  
> -	mutex_lock(&dev->pm_mutex);
> -
> -	dev->buzzer_state = 0;
> -	dev->key_code = -1;	/* no keys pressed */
> -	dev->keybit = 0xf;
> +	scoped_guard(mutex, &dev->pm_mutex) {
> +		dev->buzzer_state = 0;
> +		dev->key_code = -1;	/* no keys pressed */
> +		dev->keybit = 0xf;
>  
> -	/* issue INIT */
> -	dev->ctl_data->byte[HID_OR0] = HID_OR_GPO_BUZ_SPDIF;
> -	dev->ctl_data->byte[HID_OR1] = dev->keybit;
> -	dev->ctl_data->byte[HID_OR2] = dev->keybit;
> -	dev->ctl_data->byte[HID_OR3] = 0x00;
> +		/* issue INIT */
> +		dev->ctl_data->byte[HID_OR0] = HID_OR_GPO_BUZ_SPDIF;
> +		dev->ctl_data->byte[HID_OR1] = dev->keybit;
> +		dev->ctl_data->byte[HID_OR2] = dev->keybit;
> +		dev->ctl_data->byte[HID_OR3] = 0x00;
>  
> -	dev->ctl_urb_pending = 1;
> -	error = usb_submit_urb(dev->urb_ctl, GFP_KERNEL);
> -	if (error) {
> -		dev->ctl_urb_pending = 0;
> -		dev_err(&dev->intf->dev, "%s: usb_submit_urb (urb_ctl) failed %d\n",
> -			__func__, error);
> -	} else {
> -		dev->open = 1;
> +		dev->ctl_urb_pending = 1;
> +		error = usb_submit_urb(dev->urb_ctl, GFP_KERNEL);
> +		if (!error) {
> +			dev->open = 1;
> +			return 0;
> +		}
>  	}
>  
> -	mutex_unlock(&dev->pm_mutex);
> +	dev->ctl_urb_pending = 0;
> +	usb_autopm_put_interface(dev->intf);
>  
> -	if (error)
> -		usb_autopm_put_interface(dev->intf);
> +	dev_err(&dev->intf->dev, "%s: usb_submit_urb (urb_ctl) failed %d\n",
> +		__func__, error);
>  
>  	return error;
>  }
> @@ -590,17 +583,15 @@ static void cm109_input_close(struct input_dev *idev)
>  {
>  	struct cm109_dev *dev = input_get_drvdata(idev);
>  
> -	mutex_lock(&dev->pm_mutex);
> -
> -	/*
> -	 * Once we are here event delivery is stopped so we
> -	 * don't need to worry about someone starting buzzer
> -	 * again
> -	 */
> -	cm109_stop_traffic(dev);
> -	dev->open = 0;
> -
> -	mutex_unlock(&dev->pm_mutex);
> +	scoped_guard(mutex, &dev->pm_mutex) {
> +		/*
> +		 * Once we are here event delivery is stopped so we
> +		 * don't need to worry about someone starting buzzer
> +		 * again
> +		 */
> +		cm109_stop_traffic(dev);
> +		dev->open = 0;
> +	}
>  
>  	usb_autopm_put_interface(dev->intf);
>  }
> @@ -823,9 +814,9 @@ static int cm109_usb_suspend(struct usb_interface *intf, pm_message_t message)
>  
>  	dev_info(&intf->dev, "cm109: usb_suspend (event=%d)\n", message.event);
>  
> -	mutex_lock(&dev->pm_mutex);
> +	guard(mutex)(&dev->pm_mutex);
> +
>  	cm109_stop_traffic(dev);
> -	mutex_unlock(&dev->pm_mutex);
>  
>  	return 0;
>  }
> @@ -836,9 +827,9 @@ static int cm109_usb_resume(struct usb_interface *intf)
>  
>  	dev_info(&intf->dev, "cm109: usb_resume\n");
>  
> -	mutex_lock(&dev->pm_mutex);
> +	guard(mutex)(&dev->pm_mutex);
> +
>  	cm109_restore_state(dev);
> -	mutex_unlock(&dev->pm_mutex);
>  
>  	return 0;
>  }

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

  reply	other threads:[~2024-09-04 19:44 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-04  4:42 [PATCH 00/22] Convert misc input drivers to use new cleanup facilities Dmitry Torokhov
2024-09-04  4:42 ` [PATCH 01/22] Input: ad714x - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 18:47   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 02/22] Input: ati_remote2 " Dmitry Torokhov
2024-09-04 18:49   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 03/22] Input: cm109 - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
2024-09-04 19:44   ` Javier Carrasco [this message]
2024-09-04  4:42 ` [PATCH 04/22] Input: cma3000_d0x - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 18:51   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 05/22] Input: da7280 - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
2024-09-04 19:02   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 06/22] Input: kxtj9 - use guard notation when acquiring mutex/disabling irq Dmitry Torokhov
2024-09-04 19:03   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 07/22] Input: drv260x - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 19:05   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 08/22] Input: drv2665 " Dmitry Torokhov
2024-09-04 19:07   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 09/22] Input: drv2667 " Dmitry Torokhov
2024-09-04 19:08   ` Javier Carrasco
2024-09-04  4:42 ` [PATCH 10/22] Input: ideapad_slidebar - use guard notation when acquiring spinlock Dmitry Torokhov
2024-09-04 19:09   ` Javier Carrasco
2024-09-04  4:47 ` [PATCH 11/22] Input: ibm-panel " Dmitry Torokhov
2024-09-04 19:11   ` Javier Carrasco
2024-09-04 21:56   ` Eddie James
2024-09-04  4:47 ` [PATCH 12/22] Input: iqs269a - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 13:53   ` Javier Carrasco
2024-09-04 18:21     ` Dmitry Torokhov
2024-09-04 18:41       ` Javier Carrasco
2024-09-04 18:53         ` Dmitry Torokhov
2024-09-08 22:05   ` Jeff LaBundy
2024-09-04  4:48 ` [PATCH 13/22] Input: iqs269a - use cleanup facility for fwnodes Dmitry Torokhov
2024-09-04 11:13   ` Javier Carrasco
2024-09-08 22:08   ` Jeff LaBundy
2024-09-04  4:48 ` [PATCH 14/22] Input: iqs626a " Dmitry Torokhov
2024-09-04 11:10   ` Javier Carrasco
2024-09-09  0:02   ` Jeff LaBundy
2024-09-09  1:31     ` Dmitry Torokhov
2024-09-10 15:12       ` Jeff LaBundy
2024-09-04  4:48 ` [PATCH 15/22] Input: iqs7222 " Dmitry Torokhov
2024-09-04 10:50   ` Javier Carrasco
2024-09-04 18:26     ` Dmitry Torokhov
2024-09-04 18:46       ` Javier Carrasco
2024-09-09  0:12   ` Jeff LaBundy
2024-09-09  1:34     ` Dmitry Torokhov
2024-09-10 15:14       ` Jeff LaBundy
2024-09-04  4:48 ` [PATCH 16/22] Input: max8997_haptic - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 19:12   ` Javier Carrasco
2024-09-04  4:48 ` [PATCH 17/22] Input: pegasus_notetaker " Dmitry Torokhov
2024-09-04 19:52   ` Javier Carrasco
2024-09-04 20:59     ` [PATCH v2 " Dmitry Torokhov
2024-09-04  4:49 ` [PATCH 18/22] Input: powermate - use guard notation when acquiring spinlock Dmitry Torokhov
2024-09-04 19:16   ` Javier Carrasco
2024-09-04  4:49 ` [PATCH 19/22] Input: pwm-beeper " Dmitry Torokhov
2024-09-04 19:22   ` Javier Carrasco
2024-09-04  4:49 ` [PATCH 20/22] Input: regulator-haptic - use guard notation when acquiring mutex Dmitry Torokhov
2024-09-04 19:27   ` Javier Carrasco
2024-09-04 20:55     ` [PATCH v2 " Dmitry Torokhov
2024-09-04 21:41       ` Javier Carrasco
2024-09-07  3:40   ` [PATCH " kernel test robot
2024-09-04  4:49 ` [PATCH 21/22] Input: rotary_encoder " Dmitry Torokhov
2024-09-04 19:32   ` Javier Carrasco
2024-09-04  4:49 ` [PATCH 22/22] Input: sparcspkr - use guard notation when acquiring spinlock Dmitry Torokhov
2024-09-04 19:33   ` Javier Carrasco

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=83199f0f-67a9-4fe7-9cbe-f3821ad4c041@gmail.com \
    --to=javier.carrasco.cruz@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=eajames@linux.ibm.com \
    --cc=hdegoede@redhat.com \
    --cc=jeff@labundy.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.hennerich@analog.com \
    --cc=o2g.org.ru@gmail.com \
    --cc=support.opensource@diasemi.com \
    --cc=syrjala@sci.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).