Skip to content

Commit 3685744

Browse files
chen zhangbrauner
authored andcommitted
chardev: Switch to guard(mutex) and __free(kfree)
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> Link: https://patch.msgid.link/20251215111500.159243-1-chenzhang@kylinos.cn Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 3f320e5 commit 3685744

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

fs/char_dev.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/kdev_t.h>
1111
#include <linux/slab.h>
1212
#include <linux/string.h>
13+
#include <linux/cleanup.h>
1314

1415
#include <linux/major.h>
1516
#include <linux/errno.h>
@@ -97,7 +98,8 @@ static struct char_device_struct *
9798
__register_chrdev_region(unsigned int major, unsigned int baseminor,
9899
int minorct, const char *name)
99100
{
100-
struct char_device_struct *cd, *curr, *prev = NULL;
101+
struct char_device_struct *cd __free(kfree) = NULL;
102+
struct char_device_struct *curr, *prev = NULL;
101103
int ret;
102104
int i;
103105

@@ -117,14 +119,14 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
117119
if (cd == NULL)
118120
return ERR_PTR(-ENOMEM);
119121

120-
mutex_lock(&chrdevs_lock);
122+
guard(mutex)(&chrdevs_lock);
121123

122124
if (major == 0) {
123125
ret = find_dynamic_major();
124126
if (ret < 0) {
125127
pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
126128
name);
127-
goto out;
129+
return ERR_PTR(ret);
128130
}
129131
major = ret;
130132
}
@@ -144,7 +146,7 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
144146
if (curr->baseminor >= baseminor + minorct)
145147
break;
146148

147-
goto out;
149+
return ERR_PTR(ret);
148150
}
149151

150152
cd->major = major;
@@ -160,12 +162,7 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
160162
prev->next = cd;
161163
}
162164

163-
mutex_unlock(&chrdevs_lock);
164-
return cd;
165-
out:
166-
mutex_unlock(&chrdevs_lock);
167-
kfree(cd);
168-
return ERR_PTR(ret);
165+
return_ptr(cd);
169166
}
170167

171168
static struct char_device_struct *

0 commit comments

Comments
 (0)