[PATCH 05/15] serial: sc16is7xx: use guards for simple mutex locks

Hugo Villeneuve posted 15 patches 2 months, 3 weeks ago
There is a newer version of this series
[PATCH 05/15] serial: sc16is7xx: use guards for simple mutex locks
Posted by Hugo Villeneuve 2 months, 3 weeks ago
From: Hugo Villeneuve <hvilleneuve@dimonoff.com>

Guards can help to make the code more readable, so use them wherever they
do so.

In many places, labels and 'ret' locals are eliminated completely.

Signed-off-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>

---

Based on patch by Jiri Slaby (SUSE) <jirislaby@kernel.org>:
commit("serial: use guards for simple mutex locks")
---
 drivers/tty/serial/sc16is7xx.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/drivers/tty/serial/sc16is7xx.c b/drivers/tty/serial/sc16is7xx.c
index 72e4c4f80f7f5..7af09535a1563 100644
--- a/drivers/tty/serial/sc16is7xx.c
+++ b/drivers/tty/serial/sc16is7xx.c
@@ -783,13 +783,11 @@ static bool sc16is7xx_port_irq(struct sc16is7xx_port *s, int portno)
 	struct uart_port *port = &s->p[portno].port;
 	struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
 
-	mutex_lock(&one->lock);
+	guard(mutex)(&one->lock);
 
 	iir = sc16is7xx_port_read(port, SC16IS7XX_IIR_REG);
-	if (iir & SC16IS7XX_IIR_NO_INT_BIT) {
-		rc = false;
-		goto out_port_irq;
-	}
+	if (iir & SC16IS7XX_IIR_NO_INT_BIT)
+		return false;
 
 	iir &= SC16IS7XX_IIR_ID_MASK;
 
@@ -829,9 +827,6 @@ static bool sc16is7xx_port_irq(struct sc16is7xx_port *s, int portno)
 		break;
 	}
 
-out_port_irq:
-	mutex_unlock(&one->lock);
-
 	return rc;
 }
 
@@ -874,9 +869,8 @@ static void sc16is7xx_tx_proc(struct kthread_work *ws)
 	    (port->rs485.delay_rts_before_send > 0))
 		msleep(port->rs485.delay_rts_before_send);
 
-	mutex_lock(&one->lock);
+	guard(mutex)(&one->lock);
 	sc16is7xx_handle_tx(port);
-	mutex_unlock(&one->lock);
 }
 
 static void sc16is7xx_reconf_rs485(struct uart_port *port)
@@ -943,9 +937,8 @@ static void sc16is7xx_ms_proc(struct kthread_work *ws)
 	struct sc16is7xx_port *s = dev_get_drvdata(one->port.dev);
 
 	if (one->port.state) {
-		mutex_lock(&one->lock);
+		guard(mutex)(&one->lock);
 		sc16is7xx_update_mlines(one);
-		mutex_unlock(&one->lock);
 
 		kthread_queue_delayed_work(&s->kworker, &one->ms_work, HZ);
 	}
-- 
2.39.5
Re: [PATCH 05/15] serial: sc16is7xx: use guards for simple mutex locks
Posted by Jiri Slaby 2 months, 2 weeks ago
On 24. 09. 25, 17:37, Hugo Villeneuve wrote:
> --- a/drivers/tty/serial/sc16is7xx.c
> +++ b/drivers/tty/serial/sc16is7xx.c
...
> @@ -829,9 +827,6 @@ static bool sc16is7xx_port_irq(struct sc16is7xx_port *s, int portno)
>   		break;
>   	}
>   
> -out_port_irq:
> -	mutex_unlock(&one->lock);
> -
>   	return rc;

No need for rc now AFAICT.

>   }
>   
> @@ -874,9 +869,8 @@ static void sc16is7xx_tx_proc(struct kthread_work *ws)
>   	    (port->rs485.delay_rts_before_send > 0))
>   		msleep(port->rs485.delay_rts_before_send);
>   
> -	mutex_lock(&one->lock);
> +	guard(mutex)(&one->lock);
>   	sc16is7xx_handle_tx(port);
> -	mutex_unlock(&one->lock);
>   }
>   
>   static void sc16is7xx_reconf_rs485(struct uart_port *port)
> @@ -943,9 +937,8 @@ static void sc16is7xx_ms_proc(struct kthread_work *ws)
>   	struct sc16is7xx_port *s = dev_get_drvdata(one->port.dev);
>   
>   	if (one->port.state) {
> -		mutex_lock(&one->lock);
> +		guard(mutex)(&one->lock);
>   		sc16is7xx_update_mlines(one);
> -		mutex_unlock(&one->lock);
>   
>   		kthread_queue_delayed_work(&s->kworker, &one->ms_work, HZ);

Now the lock is held till here. R U sure it is OK?

thanks,
-- 
js
suse labs
Re: [PATCH 05/15] serial: sc16is7xx: use guards for simple mutex locks
Posted by Hugo Villeneuve 2 months, 2 weeks ago
Hi Jiri,

On Mon, 29 Sep 2025 08:09:12 +0200
Jiri Slaby <jirislaby@kernel.org> wrote:

> On 24. 09. 25, 17:37, Hugo Villeneuve wrote:
> > --- a/drivers/tty/serial/sc16is7xx.c
> > +++ b/drivers/tty/serial/sc16is7xx.c
> ...
> > @@ -829,9 +827,6 @@ static bool sc16is7xx_port_irq(struct sc16is7xx_port *s, int portno)
> >   		break;
> >   	}
> >   
> > -out_port_irq:
> > -	mutex_unlock(&one->lock);
> > -
> >   	return rc;
> 
> No need for rc now AFAICT.

You are right, I will remove it in v2.

> 
> >   }
> >   
> > @@ -874,9 +869,8 @@ static void sc16is7xx_tx_proc(struct kthread_work *ws)
> >   	    (port->rs485.delay_rts_before_send > 0))
> >   		msleep(port->rs485.delay_rts_before_send);
> >   
> > -	mutex_lock(&one->lock);
> > +	guard(mutex)(&one->lock);
> >   	sc16is7xx_handle_tx(port);
> > -	mutex_unlock(&one->lock);
> >   }
> >   
> >   static void sc16is7xx_reconf_rs485(struct uart_port *port)
> > @@ -943,9 +937,8 @@ static void sc16is7xx_ms_proc(struct kthread_work *ws)
> >   	struct sc16is7xx_port *s = dev_get_drvdata(one->port.dev);
> >   
> >   	if (one->port.state) {
> > -		mutex_lock(&one->lock);
> > +		guard(mutex)(&one->lock);
> >   		sc16is7xx_update_mlines(one);
> > -		mutex_unlock(&one->lock);
> >   
> >   		kthread_queue_delayed_work(&s->kworker, &one->ms_work, HZ);
> 
> Now the lock is held till here. R U sure it is OK?

Now that you mention it, I am sure its not OK :)

I will restore this one to the original lock/unlock code in V2.

Thank you,
Hugo.
Re: [PATCH 05/15] serial: sc16is7xx: use guards for simple mutex locks
Posted by Jiri Slaby 2 months, 2 weeks ago
On 30. 09. 25, 2:27, Hugo Villeneuve wrote:
>>> @@ -943,9 +937,8 @@ static void sc16is7xx_ms_proc(struct kthread_work *ws)
>>>    	struct sc16is7xx_port *s = dev_get_drvdata(one->port.dev);
>>>    
>>>    	if (one->port.state) {
>>> -		mutex_lock(&one->lock);
>>> +		guard(mutex)(&one->lock);
>>>    		sc16is7xx_update_mlines(one);
>>> -		mutex_unlock(&one->lock);
>>>    
>>>    		kthread_queue_delayed_work(&s->kworker, &one->ms_work, HZ);
>>
>> Now the lock is held till here. R U sure it is OK?
> 
> Now that you mention it, I am sure its not OK :)
> 
> I will restore this one to the original lock/unlock code in V2.

Or use a scoped lock ;).

-- 
js
suse labs
Re: [PATCH 05/15] serial: sc16is7xx: use guards for simple mutex locks
Posted by Hugo Villeneuve 2 months, 2 weeks ago
Hi Jiri,

On Tue, 30 Sep 2025 06:01:35 +0200
Jiri Slaby <jirislaby@kernel.org> wrote:

> On 30. 09. 25, 2:27, Hugo Villeneuve wrote:
> >>> @@ -943,9 +937,8 @@ static void sc16is7xx_ms_proc(struct kthread_work *ws)
> >>>    	struct sc16is7xx_port *s = dev_get_drvdata(one->port.dev);
> >>>    
> >>>    	if (one->port.state) {
> >>> -		mutex_lock(&one->lock);
> >>> +		guard(mutex)(&one->lock);
> >>>    		sc16is7xx_update_mlines(one);
> >>> -		mutex_unlock(&one->lock);
> >>>    
> >>>    		kthread_queue_delayed_work(&s->kworker, &one->ms_work, HZ);
> >>
> >> Now the lock is held till here. R U sure it is OK?
> > 
> > Now that you mention it, I am sure its not OK :)
> > 
> > I will restore this one to the original lock/unlock code in V2.
> 
> Or use a scoped lock ;).

Cool, will use that.

Thank you,
Hugo.