Skip to content

Commit eafd52e

Browse files
ivanorlov2206gregkh
authored andcommitted
char: misc: make misc_class a static const structure
Now that the driver core allows for struct class to be in read-only memory, move the misc_class structure to be declared at build time placing it into read-only memory, instead of having to be dynamically allocated at load time. Cc: Arnd Bergmann <arnd@arndb.de> Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com> Link: https://lore.kernel.org/r/20230620143751.578239-14-gregkh@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7671284 commit eafd52e

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

drivers/char/misc.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,21 @@ static int misc_open(struct inode *inode, struct file *file)
168168
return err;
169169
}
170170

171-
static struct class *misc_class;
171+
static char *misc_devnode(const struct device *dev, umode_t *mode)
172+
{
173+
const struct miscdevice *c = dev_get_drvdata(dev);
174+
175+
if (mode && c->mode)
176+
*mode = c->mode;
177+
if (c->nodename)
178+
return kstrdup(c->nodename, GFP_KERNEL);
179+
return NULL;
180+
}
181+
182+
static const struct class misc_class = {
183+
.name = "misc",
184+
.devnode = misc_devnode,
185+
};
172186

173187
static const struct file_operations misc_fops = {
174188
.owner = THIS_MODULE,
@@ -226,7 +240,7 @@ int misc_register(struct miscdevice *misc)
226240
dev = MKDEV(MISC_MAJOR, misc->minor);
227241

228242
misc->this_device =
229-
device_create_with_groups(misc_class, misc->parent, dev,
243+
device_create_with_groups(&misc_class, misc->parent, dev,
230244
misc, misc->groups, "%s", misc->name);
231245
if (IS_ERR(misc->this_device)) {
232246
if (is_dynamic) {
@@ -263,43 +277,30 @@ void misc_deregister(struct miscdevice *misc)
263277

264278
mutex_lock(&misc_mtx);
265279
list_del(&misc->list);
266-
device_destroy(misc_class, MKDEV(MISC_MAJOR, misc->minor));
280+
device_destroy(&misc_class, MKDEV(MISC_MAJOR, misc->minor));
267281
misc_minor_free(misc->minor);
268282
mutex_unlock(&misc_mtx);
269283
}
270284
EXPORT_SYMBOL(misc_deregister);
271285

272-
static char *misc_devnode(const struct device *dev, umode_t *mode)
273-
{
274-
const struct miscdevice *c = dev_get_drvdata(dev);
275-
276-
if (mode && c->mode)
277-
*mode = c->mode;
278-
if (c->nodename)
279-
return kstrdup(c->nodename, GFP_KERNEL);
280-
return NULL;
281-
}
282-
283286
static int __init misc_init(void)
284287
{
285288
int err;
286289
struct proc_dir_entry *ret;
287290

288291
ret = proc_create_seq("misc", 0, NULL, &misc_seq_ops);
289-
misc_class = class_create("misc");
290-
err = PTR_ERR(misc_class);
291-
if (IS_ERR(misc_class))
292+
err = class_register(&misc_class);
293+
if (err)
292294
goto fail_remove;
293295

294296
err = -EIO;
295297
if (register_chrdev(MISC_MAJOR, "misc", &misc_fops))
296298
goto fail_printk;
297-
misc_class->devnode = misc_devnode;
298299
return 0;
299300

300301
fail_printk:
301302
pr_err("unable to get major %d for misc devices\n", MISC_MAJOR);
302-
class_destroy(misc_class);
303+
class_unregister(&misc_class);
303304
fail_remove:
304305
if (ret)
305306
remove_proc_entry("misc", NULL);

0 commit comments

Comments
 (0)