/dev/mem: make mem_class a static const structure

Now that the driver core allows for struct class to be in read-only
memory, move the mem_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-13-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Ivan Orlov 2023-06-20 16:37:55 +02:00 committed by Greg Kroah-Hartman
parent 03bcd4d8e9
commit 7671284b6c
1 changed files with 9 additions and 6 deletions

View File

@ -753,20 +753,23 @@ static char *mem_devnode(const struct device *dev, umode_t *mode)
return NULL; return NULL;
} }
static struct class *mem_class; static const struct class mem_class = {
.name = "mem",
.devnode = mem_devnode,
};
static int __init chr_dev_init(void) static int __init chr_dev_init(void)
{ {
int retval;
int minor; int minor;
if (register_chrdev(MEM_MAJOR, "mem", &memory_fops)) if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
printk("unable to get major %d for memory devs\n", MEM_MAJOR); printk("unable to get major %d for memory devs\n", MEM_MAJOR);
mem_class = class_create("mem"); retval = class_register(&mem_class);
if (IS_ERR(mem_class)) if (retval)
return PTR_ERR(mem_class); return retval;
mem_class->devnode = mem_devnode;
for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) { for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
if (!devlist[minor].name) if (!devlist[minor].name)
continue; continue;
@ -777,7 +780,7 @@ static int __init chr_dev_init(void)
if ((minor == DEVPORT_MINOR) && !arch_has_dev_port()) if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
continue; continue;
device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor), device_create(&mem_class, NULL, MKDEV(MEM_MAJOR, minor),
NULL, devlist[minor].name); NULL, devlist[minor].name);
} }