Skip to content

Commit f4a5fbf

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

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

arch/x86/kernel/cpuid.c

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

43-
static struct class *cpuid_class;
4443
static enum cpuhp_state cpuhp_cpuid_state;
4544

4645
struct cpuid_regs_done {
@@ -124,26 +123,31 @@ static const struct file_operations cpuid_fops = {
124123
.open = cpuid_open,
125124
};
126125

126+
static char *cpuid_devnode(const struct device *dev, umode_t *mode)
127+
{
128+
return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
129+
}
130+
131+
static const struct class cpuid_class = {
132+
.name = "cpuid",
133+
.devnode = cpuid_devnode,
134+
};
135+
127136
static int cpuid_device_create(unsigned int cpu)
128137
{
129138
struct device *dev;
130139

131-
dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
140+
dev = device_create(&cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
132141
"cpu%d", cpu);
133142
return PTR_ERR_OR_ZERO(dev);
134143
}
135144

136145
static int cpuid_device_destroy(unsigned int cpu)
137146
{
138-
device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
147+
device_destroy(&cpuid_class, MKDEV(CPUID_MAJOR, cpu));
139148
return 0;
140149
}
141150

142-
static char *cpuid_devnode(const struct device *dev, umode_t *mode)
143-
{
144-
return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
145-
}
146-
147151
static int __init cpuid_init(void)
148152
{
149153
int err;
@@ -154,12 +158,9 @@ static int __init cpuid_init(void)
154158
CPUID_MAJOR);
155159
return -EBUSY;
156160
}
157-
cpuid_class = class_create("cpuid");
158-
if (IS_ERR(cpuid_class)) {
159-
err = PTR_ERR(cpuid_class);
161+
err = class_register(&cpuid_class);
162+
if (err)
160163
goto out_chrdev;
161-
}
162-
cpuid_class->devnode = cpuid_devnode;
163164

164165
err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/cpuid:online",
165166
cpuid_device_create, cpuid_device_destroy);
@@ -170,7 +171,7 @@ static int __init cpuid_init(void)
170171
return 0;
171172

172173
out_class:
173-
class_destroy(cpuid_class);
174+
class_unregister(&cpuid_class);
174175
out_chrdev:
175176
__unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
176177
return err;
@@ -180,7 +181,7 @@ module_init(cpuid_init);
180181
static void __exit cpuid_exit(void)
181182
{
182183
cpuhp_remove_state(cpuhp_cpuid_state);
183-
class_destroy(cpuid_class);
184+
class_unregister(&cpuid_class);
184185
__unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
185186
}
186187
module_exit(cpuid_exit);

0 commit comments

Comments
 (0)