Skip to content

Commit 5b87c05

Browse files
ivanorlov2206gregkh
authored andcommitted
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>
1 parent f4a5fbf commit 5b87c05

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

arch/x86/kernel/msr.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#include <asm/cpufeature.h>
4040
#include <asm/msr.h>
4141

42-
static struct class *msr_class;
4342
static enum cpuhp_state cpuhp_msr_state;
4443

4544
enum allow_write_msrs {
@@ -235,26 +234,31 @@ static const struct file_operations msr_fops = {
235234
.compat_ioctl = msr_ioctl,
236235
};
237236

237+
static char *msr_devnode(const struct device *dev, umode_t *mode)
238+
{
239+
return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
240+
}
241+
242+
static const struct class msr_class = {
243+
.name = "msr",
244+
.devnode = msr_devnode,
245+
};
246+
238247
static int msr_device_create(unsigned int cpu)
239248
{
240249
struct device *dev;
241250

242-
dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
251+
dev = device_create(&msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
243252
"msr%d", cpu);
244253
return PTR_ERR_OR_ZERO(dev);
245254
}
246255

247256
static int msr_device_destroy(unsigned int cpu)
248257
{
249-
device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
258+
device_destroy(&msr_class, MKDEV(MSR_MAJOR, cpu));
250259
return 0;
251260
}
252261

253-
static char *msr_devnode(const struct device *dev, umode_t *mode)
254-
{
255-
return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
256-
}
257-
258262
static int __init msr_init(void)
259263
{
260264
int err;
@@ -263,12 +267,9 @@ static int __init msr_init(void)
263267
pr_err("unable to get major %d for msr\n", MSR_MAJOR);
264268
return -EBUSY;
265269
}
266-
msr_class = class_create("msr");
267-
if (IS_ERR(msr_class)) {
268-
err = PTR_ERR(msr_class);
270+
err = class_register(&msr_class);
271+
if (err)
269272
goto out_chrdev;
270-
}
271-
msr_class->devnode = msr_devnode;
272273

273274
err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online",
274275
msr_device_create, msr_device_destroy);
@@ -278,7 +279,7 @@ static int __init msr_init(void)
278279
return 0;
279280

280281
out_class:
281-
class_destroy(msr_class);
282+
class_unregister(&msr_class);
282283
out_chrdev:
283284
__unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
284285
return err;
@@ -288,7 +289,7 @@ module_init(msr_init);
288289
static void __exit msr_exit(void)
289290
{
290291
cpuhp_remove_state(cpuhp_msr_state);
291-
class_destroy(msr_class);
292+
class_unregister(&msr_class);
292293
__unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
293294
}
294295
module_exit(msr_exit)

0 commit comments

Comments
 (0)