[PATCH 01/13] tty: simplify tty_dev_name_to_number() using guard(mutex)

Jiri Slaby (SUSE) posted 13 patches 1 year, 4 months ago
There is a newer version of this series
[PATCH 01/13] tty: simplify tty_dev_name_to_number() using guard(mutex)
Posted by Jiri Slaby (SUSE) 1 year, 4 months ago
In tty_dev_name_to_number(), a guard can help to make the code easier to
follow. Especially how 0 is returned in the successful case. So use a
guard there.

Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
---
 drivers/tty/tty_io.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index bc9aebcb873f..267682bcfea0 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -350,22 +350,19 @@ int tty_dev_name_to_number(const char *name, dev_t *number)
 		return ret;
 
 	prefix_length = str - name;
-	mutex_lock(&tty_mutex);
+
+	guard(mutex)(&tty_mutex);
 
 	list_for_each_entry(p, &tty_drivers, tty_drivers)
 		if (prefix_length == strlen(p->name) && strncmp(name,
 					p->name, prefix_length) == 0) {
 			if (index < p->num) {
 				*number = MKDEV(p->major, p->minor_start + index);
-				goto out;
+				return 0;
 			}
 		}
 
-	/* if here then driver wasn't found */
-	ret = -ENODEV;
-out:
-	mutex_unlock(&tty_mutex);
-	return ret;
+	return -ENODEV;
 }
 EXPORT_SYMBOL_GPL(tty_dev_name_to_number);
 
-- 
2.46.0
Re: [PATCH 01/13] tty: simplify tty_dev_name_to_number() using guard(mutex)
Posted by Ilpo Järvinen 1 year, 4 months ago
On Mon, 5 Aug 2024, Jiri Slaby (SUSE) wrote:

> In tty_dev_name_to_number(), a guard can help to make the code easier to
> follow. Especially how 0 is returned in the successful case. So use a
> guard there.
> 
> Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
> ---
>  drivers/tty/tty_io.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
> index bc9aebcb873f..267682bcfea0 100644
> --- a/drivers/tty/tty_io.c
> +++ b/drivers/tty/tty_io.c
> @@ -350,22 +350,19 @@ int tty_dev_name_to_number(const char *name, dev_t *number)
>  		return ret;
>  
>  	prefix_length = str - name;
> -	mutex_lock(&tty_mutex);
> +
> +	guard(mutex)(&tty_mutex);
>  
>  	list_for_each_entry(p, &tty_drivers, tty_drivers)
>  		if (prefix_length == strlen(p->name) && strncmp(name,
>  					p->name, prefix_length) == 0) {
>  			if (index < p->num) {
>  				*number = MKDEV(p->major, p->minor_start + index);
> -				goto out;
> +				return 0;
>  			}
>  		}
>  
> -	/* if here then driver wasn't found */
> -	ret = -ENODEV;
> -out:
> -	mutex_unlock(&tty_mutex);
> -	return ret;
> +	return -ENODEV;
>  }
>  EXPORT_SYMBOL_GPL(tty_dev_name_to_number);

Should add also #include <linux/cleanup.h>. With that fixed:

Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

-- 
 i.