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

x86/cpuid: make cpuid_class a static const structure

Now that the driver core allows for struct class to be in read-only
memory, move the cpuid_class structure to be declared at build time
placing it into read-only memory, instead of having to be dynamically
allocated at boot time.

Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: x86@kernel.org
Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Link: https://lore.kernel.org/r/20230620144431.583290-4-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Ivan Orlov and committed by
Greg Kroah-Hartman
f4a5fbfa 32944855

+16 -15
+16 -15
arch/x86/kernel/cpuid.c
··· 40 40 #include <asm/processor.h> 41 41 #include <asm/msr.h> 42 42 43 - static struct class *cpuid_class; 44 43 static enum cpuhp_state cpuhp_cpuid_state; 45 44 46 45 struct cpuid_regs_done { ··· 123 124 .open = cpuid_open, 124 125 }; 125 126 127 + static char *cpuid_devnode(const struct device *dev, umode_t *mode) 128 + { 129 + return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt)); 130 + } 131 + 132 + static const struct class cpuid_class = { 133 + .name = "cpuid", 134 + .devnode = cpuid_devnode, 135 + }; 136 + 126 137 static int cpuid_device_create(unsigned int cpu) 127 138 { 128 139 struct device *dev; 129 140 130 - dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL, 141 + dev = device_create(&cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL, 131 142 "cpu%d", cpu); 132 143 return PTR_ERR_OR_ZERO(dev); 133 144 } 134 145 135 146 static int cpuid_device_destroy(unsigned int cpu) 136 147 { 137 - device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu)); 148 + device_destroy(&cpuid_class, MKDEV(CPUID_MAJOR, cpu)); 138 149 return 0; 139 - } 140 - 141 - static char *cpuid_devnode(const struct device *dev, umode_t *mode) 142 - { 143 - return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt)); 144 150 } 145 151 146 152 static int __init cpuid_init(void) ··· 158 154 CPUID_MAJOR); 159 155 return -EBUSY; 160 156 } 161 - cpuid_class = class_create("cpuid"); 162 - if (IS_ERR(cpuid_class)) { 163 - err = PTR_ERR(cpuid_class); 157 + err = class_register(&cpuid_class); 158 + if (err) 164 159 goto out_chrdev; 165 - } 166 - cpuid_class->devnode = cpuid_devnode; 167 160 168 161 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/cpuid:online", 169 162 cpuid_device_create, cpuid_device_destroy); ··· 171 170 return 0; 172 171 173 172 out_class: 174 - class_destroy(cpuid_class); 173 + class_unregister(&cpuid_class); 175 174 out_chrdev: 176 175 __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid"); 177 176 return err; ··· 181 180 static void __exit cpuid_exit(void) 182 181 { 183 182 cpuhp_remove_state(cpuhp_cpuid_state); 184 - class_destroy(cpuid_class); 183 + class_unregister(&cpuid_class); 185 184 __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid"); 186 185 } 187 186 module_exit(cpuid_exit);