[PATCH] chardev: Switch to guard(mutex)

chen zhang posted 1 patch 20 hours ago
fs/char_dev.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)
[PATCH] chardev: Switch to guard(mutex)
Posted by chen zhang 20 hours ago
Instead of using the 'goto label; mutex_unlock()' pattern use
'guard(mutex)' which will release the mutex when it goes out of scope.

Signed-off-by: chen zhang <chenzhang@kylinos.cn>
---
 fs/char_dev.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/fs/char_dev.c b/fs/char_dev.c
index c2ddb998f3c9..ca6037304e19 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -117,14 +117,15 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
 	if (cd == NULL)
 		return ERR_PTR(-ENOMEM);
 
-	mutex_lock(&chrdevs_lock);
+	guard(mutex)(&chrdevs_lock);
 
 	if (major == 0) {
 		ret = find_dynamic_major();
 		if (ret < 0) {
 			pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
 			       name);
-			goto out;
+			kfree(cd);
+			return ERR_PTR(ret);
 		}
 		major = ret;
 	}
@@ -144,7 +145,8 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
 		if (curr->baseminor >= baseminor + minorct)
 			break;
 
-		goto out;
+		kfree(cd);
+		return ERR_PTR(ret);
 	}
 
 	cd->major = major;
@@ -160,12 +162,7 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
 		prev->next = cd;
 	}
 
-	mutex_unlock(&chrdevs_lock);
 	return cd;
-out:
-	mutex_unlock(&chrdevs_lock);
-	kfree(cd);
-	return ERR_PTR(ret);
 }
 
 static struct char_device_struct *
-- 
2.25.1
Re: [PATCH] chardev: Switch to guard(mutex)
Posted by Jan Kara 16 hours ago
On Mon 15-12-25 14:06:57, chen zhang wrote:
> Instead of using the 'goto label; mutex_unlock()' pattern use
> 'guard(mutex)' which will release the mutex when it goes out of scope.
> 
> Signed-off-by: chen zhang <chenzhang@kylinos.cn>

Thanks for the patch. I agree guards can simplify this function but why not
handle 'cd' variable with __free as well then? The kfree() calls you have
to add otherwise kind of defeat the purpose of guards...

								Honza

> ---
>  fs/char_dev.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/fs/char_dev.c b/fs/char_dev.c
> index c2ddb998f3c9..ca6037304e19 100644
> --- a/fs/char_dev.c
> +++ b/fs/char_dev.c
> @@ -117,14 +117,15 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
>  	if (cd == NULL)
>  		return ERR_PTR(-ENOMEM);
>  
> -	mutex_lock(&chrdevs_lock);
> +	guard(mutex)(&chrdevs_lock);
>  
>  	if (major == 0) {
>  		ret = find_dynamic_major();
>  		if (ret < 0) {
>  			pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
>  			       name);
> -			goto out;
> +			kfree(cd);
> +			return ERR_PTR(ret);
>  		}
>  		major = ret;
>  	}
> @@ -144,7 +145,8 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
>  		if (curr->baseminor >= baseminor + minorct)
>  			break;
>  
> -		goto out;
> +		kfree(cd);
> +		return ERR_PTR(ret);
>  	}
>  
>  	cd->major = major;
> @@ -160,12 +162,7 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
>  		prev->next = cd;
>  	}
>  
> -	mutex_unlock(&chrdevs_lock);
>  	return cd;
> -out:
> -	mutex_unlock(&chrdevs_lock);
> -	kfree(cd);
> -	return ERR_PTR(ret);
>  }
>  
>  static struct char_device_struct *
> -- 
> 2.25.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR