[PATCH v2] chardev: Switch to guard(mutex) and __free(kfree)

chen zhang posted 1 patch 1 month, 3 weeks ago
fs/char_dev.c | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)
[PATCH v2] chardev: Switch to guard(mutex) and __free(kfree)
Posted by chen zhang 1 month, 3 weeks ago
Instead of using the 'goto label; mutex_unlock()' pattern use
'guard(mutex)' which will release the mutex when it goes out of scope.
Use the __free(kfree) cleanup to replace instances of manually
calling kfree(). Also make some code path simplifications that this
allows.

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

diff --git a/fs/char_dev.c b/fs/char_dev.c
index c2ddb998f3c9..74d4bdfaa9ae 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -10,6 +10,7 @@
 #include <linux/kdev_t.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/cleanup.h>
 
 #include <linux/major.h>
 #include <linux/errno.h>
@@ -97,7 +98,8 @@ static struct char_device_struct *
 __register_chrdev_region(unsigned int major, unsigned int baseminor,
 			   int minorct, const char *name)
 {
-	struct char_device_struct *cd, *curr, *prev = NULL;
+	struct char_device_struct *cd __free(kfree) = NULL;
+	struct char_device_struct *curr, *prev = NULL;
 	int ret;
 	int i;
 
@@ -117,14 +119,14 @@ __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;
+			return ERR_PTR(ret);
 		}
 		major = ret;
 	}
@@ -144,7 +146,7 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
 		if (curr->baseminor >= baseminor + minorct)
 			break;
 
-		goto out;
+		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);
+	return_ptr(cd);
 }
 
 static struct char_device_struct *
-- 
2.25.1
Re: [PATCH v2] chardev: Switch to guard(mutex) and __free(kfree)
Posted by Christian Brauner 1 month, 2 weeks ago
On Mon, 15 Dec 2025 19:15:00 +0800, 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.
> Use the __free(kfree) cleanup to replace instances of manually
> calling kfree(). Also make some code path simplifications that this
> allows.
> 
> 
> [...]

Applied to the vfs-7.0.misc branch of the vfs/vfs.git tree.
Patches in the vfs-7.0.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.0.misc

[1/1] chardev: Switch to guard(mutex) and __free(kfree)
      https://git.kernel.org/vfs/vfs/c/3685744afa4a
Re: [PATCH v2] chardev: Switch to guard(mutex) and __free(kfree)
Posted by Jan Kara 1 month, 3 weeks ago
On Mon 15-12-25 19:15:00, 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.
> Use the __free(kfree) cleanup to replace instances of manually
> calling kfree(). Also make some code path simplifications that this
> allows.
> 
> Signed-off-by: chen zhang <chenzhang@kylinos.cn>

Looks good to me. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  fs/char_dev.c | 17 +++++++----------
>  1 file changed, 7 insertions(+), 10 deletions(-)
> 
> diff --git a/fs/char_dev.c b/fs/char_dev.c
> index c2ddb998f3c9..74d4bdfaa9ae 100644
> --- a/fs/char_dev.c
> +++ b/fs/char_dev.c
> @@ -10,6 +10,7 @@
>  #include <linux/kdev_t.h>
>  #include <linux/slab.h>
>  #include <linux/string.h>
> +#include <linux/cleanup.h>
>  
>  #include <linux/major.h>
>  #include <linux/errno.h>
> @@ -97,7 +98,8 @@ static struct char_device_struct *
>  __register_chrdev_region(unsigned int major, unsigned int baseminor,
>  			   int minorct, const char *name)
>  {
> -	struct char_device_struct *cd, *curr, *prev = NULL;
> +	struct char_device_struct *cd __free(kfree) = NULL;
> +	struct char_device_struct *curr, *prev = NULL;
>  	int ret;
>  	int i;
>  
> @@ -117,14 +119,14 @@ __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;
> +			return ERR_PTR(ret);
>  		}
>  		major = ret;
>  	}
> @@ -144,7 +146,7 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
>  		if (curr->baseminor >= baseminor + minorct)
>  			break;
>  
> -		goto out;
> +		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);
> +	return_ptr(cd);
>  }
>  
>  static struct char_device_struct *
> -- 
> 2.25.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR