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

x86/MSR: make msr_class a static const structure

Now that the driver core allows for struct class to be in read-only
memory, move the msr_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-5-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Ivan Orlov and committed by
Greg Kroah-Hartman
5b87c058 f4a5fbfa

+16 -15
+16 -15
arch/x86/kernel/msr.c
··· 39 39 #include <asm/cpufeature.h> 40 40 #include <asm/msr.h> 41 41 42 - static struct class *msr_class; 43 42 static enum cpuhp_state cpuhp_msr_state; 44 43 45 44 enum allow_write_msrs { ··· 234 235 .compat_ioctl = msr_ioctl, 235 236 }; 236 237 238 + static char *msr_devnode(const struct device *dev, umode_t *mode) 239 + { 240 + return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt)); 241 + } 242 + 243 + static const struct class msr_class = { 244 + .name = "msr", 245 + .devnode = msr_devnode, 246 + }; 247 + 237 248 static int msr_device_create(unsigned int cpu) 238 249 { 239 250 struct device *dev; 240 251 241 - dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL, 252 + dev = device_create(&msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL, 242 253 "msr%d", cpu); 243 254 return PTR_ERR_OR_ZERO(dev); 244 255 } 245 256 246 257 static int msr_device_destroy(unsigned int cpu) 247 258 { 248 - device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu)); 259 + device_destroy(&msr_class, MKDEV(MSR_MAJOR, cpu)); 249 260 return 0; 250 - } 251 - 252 - static char *msr_devnode(const struct device *dev, umode_t *mode) 253 - { 254 - return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt)); 255 261 } 256 262 257 263 static int __init msr_init(void) ··· 267 263 pr_err("unable to get major %d for msr\n", MSR_MAJOR); 268 264 return -EBUSY; 269 265 } 270 - msr_class = class_create("msr"); 271 - if (IS_ERR(msr_class)) { 272 - err = PTR_ERR(msr_class); 266 + err = class_register(&msr_class); 267 + if (err) 273 268 goto out_chrdev; 274 - } 275 - msr_class->devnode = msr_devnode; 276 269 277 270 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online", 278 271 msr_device_create, msr_device_destroy); ··· 279 278 return 0; 280 279 281 280 out_class: 282 - class_destroy(msr_class); 281 + class_unregister(&msr_class); 283 282 out_chrdev: 284 283 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr"); 285 284 return err; ··· 289 288 static void __exit msr_exit(void) 290 289 { 291 290 cpuhp_remove_state(cpuhp_msr_state); 292 - class_destroy(msr_class); 291 + class_unregister(&msr_class); 293 292 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr"); 294 293 } 295 294 module_exit(msr_exit)