Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

chen zhang and committed by
Christian Brauner
3685744a 3f320e5c

+7 -10
+7 -10
fs/char_dev.c
··· 10 10 #include <linux/kdev_t.h> 11 11 #include <linux/slab.h> 12 12 #include <linux/string.h> 13 + #include <linux/cleanup.h> 13 14 14 15 #include <linux/major.h> 15 16 #include <linux/errno.h> ··· 98 97 __register_chrdev_region(unsigned int major, unsigned int baseminor, 99 98 int minorct, const char *name) 100 99 { 101 - struct char_device_struct *cd, *curr, *prev = NULL; 100 + struct char_device_struct *cd __free(kfree) = NULL; 101 + struct char_device_struct *curr, *prev = NULL; 102 102 int ret; 103 103 int i; 104 104 ··· 119 117 if (cd == NULL) 120 118 return ERR_PTR(-ENOMEM); 121 119 122 - mutex_lock(&chrdevs_lock); 120 + guard(mutex)(&chrdevs_lock); 123 121 124 122 if (major == 0) { 125 123 ret = find_dynamic_major(); 126 124 if (ret < 0) { 127 125 pr_err("CHRDEV \"%s\" dynamic allocation region is full\n", 128 126 name); 129 - goto out; 127 + return ERR_PTR(ret); 130 128 } 131 129 major = ret; 132 130 } ··· 146 144 if (curr->baseminor >= baseminor + minorct) 147 145 break; 148 146 149 - goto out; 147 + return ERR_PTR(ret); 150 148 } 151 149 152 150 cd->major = major; ··· 162 160 prev->next = cd; 163 161 } 164 162 165 - mutex_unlock(&chrdevs_lock); 166 - return cd; 167 - out: 168 - mutex_unlock(&chrdevs_lock); 169 - kfree(cd); 170 - return ERR_PTR(ret); 163 + return_ptr(cd); 171 164 } 172 165 173 166 static struct char_device_struct *