1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CPU subsystem support
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/cpu.h>
11 #include <linux/topology.h>
12 #include <linux/device.h>
13 #include <linux/node.h>
14 #include <linux/gfp.h>
15 #include <linux/slab.h>
16 #include <linux/percpu.h>
17 #include <linux/acpi.h>
18 #include <linux/of.h>
19 #include <linux/cpufeature.h>
20 #include <linux/tick.h>
21 #include <linux/pm_qos.h>
22 #include <linux/delay.h>
23 #include <linux/sched/isolation.h>
24 
25 #include "base.h"
26 
27 static DEFINE_PER_CPU(struct device *, cpu_sys_devices);
28 
cpu_subsys_match(struct device *dev, struct device_driver *drv)29 static int cpu_subsys_match(struct device *dev, struct device_driver *drv)
30 {
31 	/* ACPI style match is the only one that may succeed. */
32 	if (acpi_driver_match_device(dev, drv))
33 		return 1;
34 
35 	return 0;
36 }
37 
38 #ifdef CONFIG_HOTPLUG_CPU
change_cpu_under_node(struct cpu *cpu, unsigned int from_nid, unsigned int to_nid)39 static void change_cpu_under_node(struct cpu *cpu,
40 			unsigned int from_nid, unsigned int to_nid)
41 {
42 	int cpuid = cpu->dev.id;
43 	unregister_cpu_under_node(cpuid, from_nid);
44 	register_cpu_under_node(cpuid, to_nid);
45 	cpu->node_id = to_nid;
46 }
47 
cpu_subsys_online(struct device *dev)48 static int cpu_subsys_online(struct device *dev)
49 {
50 	struct cpu *cpu = container_of(dev, struct cpu, dev);
51 	int cpuid = dev->id;
52 	int from_nid, to_nid;
53 	int ret;
54 	int retries = 0;
55 
56 	from_nid = cpu_to_node(cpuid);
57 	if (from_nid == NUMA_NO_NODE)
58 		return -ENODEV;
59 
60 retry:
61 	ret = cpu_device_up(dev);
62 
63 	/*
64 	 * If -EBUSY is returned, it is likely that hotplug is temporarily
65 	 * disabled when cpu_hotplug_disable() was called. This condition is
66 	 * transient. So we retry after waiting for an exponentially
67 	 * increasing delay up to a total of at least 620ms as some PCI
68 	 * device initialization can take quite a while.
69 	 */
70 	if (ret == -EBUSY) {
71 		retries++;
72 		if (retries > 5)
73 			return ret;
74 		msleep(10 * (1 << retries));
75 		goto retry;
76 	}
77 
78 	/*
79 	 * When hot adding memory to memoryless node and enabling a cpu
80 	 * on the node, node number of the cpu may internally change.
81 	 */
82 	to_nid = cpu_to_node(cpuid);
83 	if (from_nid != to_nid)
84 		change_cpu_under_node(cpu, from_nid, to_nid);
85 
86 	return ret;
87 }
88 
cpu_subsys_offline(struct device *dev)89 static int cpu_subsys_offline(struct device *dev)
90 {
91 	return cpu_device_down(dev);
92 }
93 
unregister_cpu(struct cpu *cpu)94 void unregister_cpu(struct cpu *cpu)
95 {
96 	int logical_cpu = cpu->dev.id;
97 
98 	unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
99 
100 	device_unregister(&cpu->dev);
101 	per_cpu(cpu_sys_devices, logical_cpu) = NULL;
102 	return;
103 }
104 
105 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
cpu_probe_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)106 static ssize_t cpu_probe_store(struct device *dev,
107 			       struct device_attribute *attr,
108 			       const char *buf,
109 			       size_t count)
110 {
111 	ssize_t cnt;
112 	int ret;
113 
114 	ret = lock_device_hotplug_sysfs();
115 	if (ret)
116 		return ret;
117 
118 	cnt = arch_cpu_probe(buf, count);
119 
120 	unlock_device_hotplug();
121 	return cnt;
122 }
123 
cpu_release_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)124 static ssize_t cpu_release_store(struct device *dev,
125 				 struct device_attribute *attr,
126 				 const char *buf,
127 				 size_t count)
128 {
129 	ssize_t cnt;
130 	int ret;
131 
132 	ret = lock_device_hotplug_sysfs();
133 	if (ret)
134 		return ret;
135 
136 	cnt = arch_cpu_release(buf, count);
137 
138 	unlock_device_hotplug();
139 	return cnt;
140 }
141 
142 static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
143 static DEVICE_ATTR(release, S_IWUSR, NULL, cpu_release_store);
144 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
145 #endif /* CONFIG_HOTPLUG_CPU */
146 
147 #ifdef CONFIG_KEXEC_CORE
148 #include <linux/kexec.h>
149 
crash_notes_show(struct device *dev, struct device_attribute *attr, char *buf)150 static ssize_t crash_notes_show(struct device *dev,
151 				struct device_attribute *attr,
152 				char *buf)
153 {
154 	struct cpu *cpu = container_of(dev, struct cpu, dev);
155 	unsigned long long addr;
156 	int cpunum;
157 
158 	cpunum = cpu->dev.id;
159 
160 	/*
161 	 * Might be reading other cpu's data based on which cpu read thread
162 	 * has been scheduled. But cpu data (memory) is allocated once during
163 	 * boot up and this data does not change there after. Hence this
164 	 * operation should be safe. No locking required.
165 	 */
166 	addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
167 
168 	return sysfs_emit(buf, "%llx\n", addr);
169 }
170 static DEVICE_ATTR_ADMIN_RO(crash_notes);
171 
crash_notes_size_show(struct device *dev, struct device_attribute *attr, char *buf)172 static ssize_t crash_notes_size_show(struct device *dev,
173 				     struct device_attribute *attr,
174 				     char *buf)
175 {
176 	return sysfs_emit(buf, "%zu\n", sizeof(note_buf_t));
177 }
178 static DEVICE_ATTR_ADMIN_RO(crash_notes_size);
179 
180 static struct attribute *crash_note_cpu_attrs[] = {
181 	&dev_attr_crash_notes.attr,
182 	&dev_attr_crash_notes_size.attr,
183 	NULL
184 };
185 
186 static const struct attribute_group crash_note_cpu_attr_group = {
187 	.attrs = crash_note_cpu_attrs,
188 };
189 #endif
190 
191 #ifdef CONFIG_CPU_ISOLATION_OPT
isolate_show(struct device *dev, struct device_attribute *attr, char *buf)192 static ssize_t isolate_show(struct device *dev,
193 			    struct device_attribute *attr, char *buf)
194 {
195 	struct cpu *cpu = container_of(dev, struct cpu, dev);
196 	ssize_t rc;
197 	int cpuid = cpu->dev.id;
198 	unsigned int isolated = cpu_isolated(cpuid);
199 
200 	rc = sysfs_emit(buf, "%d\n", isolated);
201 
202 	return rc;
203 }
204 
205 static DEVICE_ATTR_RO(isolate);
206 
207 static struct attribute *cpu_isolated_attrs[] = {
208 	&dev_attr_isolate.attr,
209 	NULL
210 };
211 
212 static struct attribute_group cpu_isolated_attr_group = {
213 	.attrs = cpu_isolated_attrs,
214 };
215 #endif
216 
217 static const struct attribute_group *common_cpu_attr_groups[] = {
218 #ifdef CONFIG_KEXEC_CORE
219 	&crash_note_cpu_attr_group,
220 #endif
221 #ifdef CONFIG_CPU_ISOLATION_OPT
222 	&cpu_isolated_attr_group,
223 #endif
224 	NULL
225 };
226 
227 static const struct attribute_group *hotplugable_cpu_attr_groups[] = {
228 #ifdef CONFIG_KEXEC_CORE
229 	&crash_note_cpu_attr_group,
230 #endif
231 #ifdef CONFIG_CPU_ISOLATION_OPT
232 	&cpu_isolated_attr_group,
233 #endif
234 	NULL
235 };
236 
237 /*
238  * Print cpu online, possible, present, and system maps
239  */
240 
241 struct cpu_attr {
242 	struct device_attribute attr;
243 	const struct cpumask *const map;
244 };
245 
show_cpus_attr(struct device *dev, struct device_attribute *attr, char *buf)246 static ssize_t show_cpus_attr(struct device *dev,
247 			      struct device_attribute *attr,
248 			      char *buf)
249 {
250 	struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
251 
252 	return cpumap_print_to_pagebuf(true, buf, ca->map);
253 }
254 
255 #define _CPU_ATTR(name, map) \
256 	{ __ATTR(name, 0444, show_cpus_attr, NULL), map }
257 
258 /* Keep in sync with cpu_subsys_attrs */
259 static struct cpu_attr cpu_attrs[] = {
260 	_CPU_ATTR(online, &__cpu_online_mask),
261 	_CPU_ATTR(possible, &__cpu_possible_mask),
262 	_CPU_ATTR(present, &__cpu_present_mask),
263 #ifdef CONFIG_CPU_ISOLATION_OPT
264 	_CPU_ATTR(core_ctl_isolated, &__cpu_isolated_mask),
265 #endif
266 };
267 
268 /*
269  * Print values for NR_CPUS and offlined cpus
270  */
print_cpus_kernel_max(struct device *dev, struct device_attribute *attr, char *buf)271 static ssize_t print_cpus_kernel_max(struct device *dev,
272 				     struct device_attribute *attr, char *buf)
273 {
274 	return sysfs_emit(buf, "%d\n", NR_CPUS - 1);
275 }
276 static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
277 
278 /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
279 unsigned int total_cpus;
280 
print_cpus_offline(struct device *dev, struct device_attribute *attr, char *buf)281 static ssize_t print_cpus_offline(struct device *dev,
282 				  struct device_attribute *attr, char *buf)
283 {
284 	int len = 0;
285 	cpumask_var_t offline;
286 
287 	/* display offline cpus < nr_cpu_ids */
288 	if (!alloc_cpumask_var(&offline, GFP_KERNEL))
289 		return -ENOMEM;
290 	cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
291 	len += sysfs_emit_at(buf, len, "%*pbl", cpumask_pr_args(offline));
292 	free_cpumask_var(offline);
293 
294 	/* display offline cpus >= nr_cpu_ids */
295 	if (total_cpus && nr_cpu_ids < total_cpus) {
296 		len += sysfs_emit_at(buf, len, ",");
297 
298 		if (nr_cpu_ids == total_cpus-1)
299 			len += sysfs_emit_at(buf, len, "%u", nr_cpu_ids);
300 		else
301 			len += sysfs_emit_at(buf, len, "%u-%d",
302 					     nr_cpu_ids, total_cpus - 1);
303 	}
304 
305 	len += sysfs_emit_at(buf, len, "\n");
306 
307 	return len;
308 }
309 static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
310 
print_cpus_isolated(struct device *dev, struct device_attribute *attr, char *buf)311 static ssize_t print_cpus_isolated(struct device *dev,
312 				  struct device_attribute *attr, char *buf)
313 {
314 	int len;
315 	cpumask_var_t isolated;
316 
317 	if (!alloc_cpumask_var(&isolated, GFP_KERNEL))
318 		return -ENOMEM;
319 
320 	cpumask_andnot(isolated, cpu_possible_mask,
321 		       housekeeping_cpumask(HK_TYPE_DOMAIN));
322 	len = sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(isolated));
323 
324 	free_cpumask_var(isolated);
325 
326 	return len;
327 }
328 static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL);
329 
330 #ifdef CONFIG_NO_HZ_FULL
print_cpus_nohz_full(struct device *dev, struct device_attribute *attr, char *buf)331 static ssize_t print_cpus_nohz_full(struct device *dev,
332 				    struct device_attribute *attr, char *buf)
333 {
334 	return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask));
335 }
336 static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL);
337 #endif
338 
339 #ifdef CONFIG_CRASH_HOTPLUG
crash_hotplug_show(struct device *dev, struct device_attribute *attr, char *buf)340 static ssize_t crash_hotplug_show(struct device *dev,
341 				     struct device_attribute *attr,
342 				     char *buf)
343 {
344 	return sysfs_emit(buf, "%d\n", crash_hotplug_cpu_support());
345 }
346 static DEVICE_ATTR_ADMIN_RO(crash_hotplug);
347 #endif
348 
cpu_device_release(struct device *dev)349 static void cpu_device_release(struct device *dev)
350 {
351 	/*
352 	 * This is an empty function to prevent the driver core from spitting a
353 	 * warning at us.  Yes, I know this is directly opposite of what the
354 	 * documentation for the driver core and kobjects say, and the author
355 	 * of this code has already been publically ridiculed for doing
356 	 * something as foolish as this.  However, at this point in time, it is
357 	 * the only way to handle the issue of statically allocated cpu
358 	 * devices.  The different architectures will have their cpu device
359 	 * code reworked to properly handle this in the near future, so this
360 	 * function will then be changed to correctly free up the memory held
361 	 * by the cpu device.
362 	 *
363 	 * Never copy this way of doing things, or you too will be made fun of
364 	 * on the linux-kernel list, you have been warned.
365 	 */
366 }
367 
368 #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
print_cpu_modalias(struct device *dev, struct device_attribute *attr, char *buf)369 static ssize_t print_cpu_modalias(struct device *dev,
370 				  struct device_attribute *attr,
371 				  char *buf)
372 {
373 	int len = 0;
374 	u32 i;
375 
376 	len += sysfs_emit_at(buf, len,
377 			     "cpu:type:" CPU_FEATURE_TYPEFMT ":feature:",
378 			     CPU_FEATURE_TYPEVAL);
379 
380 	for (i = 0; i < MAX_CPU_FEATURES; i++)
381 		if (cpu_have_feature(i)) {
382 			if (len + sizeof(",XXXX\n") >= PAGE_SIZE) {
383 				WARN(1, "CPU features overflow page\n");
384 				break;
385 			}
386 			len += sysfs_emit_at(buf, len, ",%04X", i);
387 		}
388 	len += sysfs_emit_at(buf, len, "\n");
389 	return len;
390 }
391 
cpu_uevent(const struct device *dev, struct kobj_uevent_env *env)392 static int cpu_uevent(const struct device *dev, struct kobj_uevent_env *env)
393 {
394 	char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
395 	if (buf) {
396 		print_cpu_modalias(NULL, NULL, buf);
397 		add_uevent_var(env, "MODALIAS=%s", buf);
398 		kfree(buf);
399 	}
400 	return 0;
401 }
402 #endif
403 
404 struct bus_type cpu_subsys = {
405 	.name = "cpu",
406 	.dev_name = "cpu",
407 	.match = cpu_subsys_match,
408 #ifdef CONFIG_HOTPLUG_CPU
409 	.online = cpu_subsys_online,
410 	.offline = cpu_subsys_offline,
411 #endif
412 #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
413 	.uevent = cpu_uevent,
414 #endif
415 };
416 EXPORT_SYMBOL_GPL(cpu_subsys);
417 
418 /*
419  * register_cpu - Setup a sysfs device for a CPU.
420  * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
421  *	  sysfs for this CPU.
422  * @num - CPU number to use when creating the device.
423  *
424  * Initialize and register the CPU device.
425  */
register_cpu(struct cpu *cpu, int num)426 int register_cpu(struct cpu *cpu, int num)
427 {
428 	int error;
429 
430 	cpu->node_id = cpu_to_node(num);
431 	memset(&cpu->dev, 0x00, sizeof(struct device));
432 	cpu->dev.id = num;
433 	cpu->dev.bus = &cpu_subsys;
434 	cpu->dev.release = cpu_device_release;
435 	cpu->dev.offline_disabled = !cpu->hotpluggable;
436 	cpu->dev.offline = !cpu_online(num);
437 	cpu->dev.of_node = of_get_cpu_node(num, NULL);
438 	cpu->dev.groups = common_cpu_attr_groups;
439 	if (cpu->hotpluggable)
440 		cpu->dev.groups = hotplugable_cpu_attr_groups;
441 	error = device_register(&cpu->dev);
442 	if (error) {
443 		put_device(&cpu->dev);
444 		return error;
445 	}
446 
447 	per_cpu(cpu_sys_devices, num) = &cpu->dev;
448 	register_cpu_under_node(num, cpu_to_node(num));
449 	dev_pm_qos_expose_latency_limit(&cpu->dev,
450 					PM_QOS_RESUME_LATENCY_NO_CONSTRAINT);
451 
452 	return 0;
453 }
454 
get_cpu_device(unsigned int cpu)455 struct device *get_cpu_device(unsigned int cpu)
456 {
457 	if (cpu < nr_cpu_ids && cpu_possible(cpu))
458 		return per_cpu(cpu_sys_devices, cpu);
459 	else
460 		return NULL;
461 }
462 EXPORT_SYMBOL_GPL(get_cpu_device);
463 
device_create_release(struct device *dev)464 static void device_create_release(struct device *dev)
465 {
466 	kfree(dev);
467 }
468 
469 __printf(4, 0)
470 static struct device *
__cpu_device_create(struct device *parent, void *drvdata, const struct attribute_group **groups, const char *fmt, va_list args)471 __cpu_device_create(struct device *parent, void *drvdata,
472 		    const struct attribute_group **groups,
473 		    const char *fmt, va_list args)
474 {
475 	struct device *dev = NULL;
476 	int retval = -ENOMEM;
477 
478 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
479 	if (!dev)
480 		goto error;
481 
482 	device_initialize(dev);
483 	dev->parent = parent;
484 	dev->groups = groups;
485 	dev->release = device_create_release;
486 	device_set_pm_not_required(dev);
487 	dev_set_drvdata(dev, drvdata);
488 
489 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
490 	if (retval)
491 		goto error;
492 
493 	retval = device_add(dev);
494 	if (retval)
495 		goto error;
496 
497 	return dev;
498 
499 error:
500 	put_device(dev);
501 	return ERR_PTR(retval);
502 }
503 
cpu_device_create(struct device *parent, void *drvdata, const struct attribute_group **groups, const char *fmt, ...)504 struct device *cpu_device_create(struct device *parent, void *drvdata,
505 				 const struct attribute_group **groups,
506 				 const char *fmt, ...)
507 {
508 	va_list vargs;
509 	struct device *dev;
510 
511 	va_start(vargs, fmt);
512 	dev = __cpu_device_create(parent, drvdata, groups, fmt, vargs);
513 	va_end(vargs);
514 	return dev;
515 }
516 EXPORT_SYMBOL_GPL(cpu_device_create);
517 
518 #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
519 static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL);
520 #endif
521 
522 static struct attribute *cpu_root_attrs[] = {
523 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
524 	&dev_attr_probe.attr,
525 	&dev_attr_release.attr,
526 #endif
527 	&cpu_attrs[0].attr.attr,
528 	&cpu_attrs[1].attr.attr,
529 	&cpu_attrs[2].attr.attr,
530 #ifdef CONFIG_CPU_ISOLATION_OPT
531 	&cpu_attrs[3].attr.attr,
532 #endif
533 	&dev_attr_kernel_max.attr,
534 	&dev_attr_offline.attr,
535 	&dev_attr_isolated.attr,
536 #ifdef CONFIG_NO_HZ_FULL
537 	&dev_attr_nohz_full.attr,
538 #endif
539 #ifdef CONFIG_CRASH_HOTPLUG
540 	&dev_attr_crash_hotplug.attr,
541 #endif
542 #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
543 	&dev_attr_modalias.attr,
544 #endif
545 	NULL
546 };
547 
548 static const struct attribute_group cpu_root_attr_group = {
549 	.attrs = cpu_root_attrs,
550 };
551 
552 static const struct attribute_group *cpu_root_attr_groups[] = {
553 	&cpu_root_attr_group,
554 	NULL,
555 };
556 
cpu_is_hotpluggable(unsigned int cpu)557 bool cpu_is_hotpluggable(unsigned int cpu)
558 {
559 	struct device *dev = get_cpu_device(cpu);
560 	return dev && container_of(dev, struct cpu, dev)->hotpluggable
561 		&& tick_nohz_cpu_hotpluggable(cpu);
562 }
563 EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
564 
565 #ifdef CONFIG_GENERIC_CPU_DEVICES
566 static DEFINE_PER_CPU(struct cpu, cpu_devices);
567 #endif
568 
cpu_dev_register_generic(void)569 static void __init cpu_dev_register_generic(void)
570 {
571 #ifdef CONFIG_GENERIC_CPU_DEVICES
572 	int i;
573 
574 	for_each_possible_cpu(i) {
575 		if (register_cpu(&per_cpu(cpu_devices, i), i))
576 			panic("Failed to register CPU device");
577 	}
578 #endif
579 }
580 
581 #ifdef CONFIG_GENERIC_CPU_VULNERABILITIES
cpu_show_not_affected(struct device *dev, struct device_attribute *attr, char *buf)582 static ssize_t cpu_show_not_affected(struct device *dev,
583 			      struct device_attribute *attr, char *buf)
584 {
585 	return sysfs_emit(buf, "Not affected\n");
586 }
587 
588 #define CPU_SHOW_VULN_FALLBACK(func)					\
589 	ssize_t cpu_show_##func(struct device *,			\
590 				  struct device_attribute *, char *)	\
591 		 __attribute__((weak, alias("cpu_show_not_affected")))
592 
593 CPU_SHOW_VULN_FALLBACK(meltdown);
594 CPU_SHOW_VULN_FALLBACK(spectre_v1);
595 CPU_SHOW_VULN_FALLBACK(spectre_v2);
596 CPU_SHOW_VULN_FALLBACK(spec_store_bypass);
597 CPU_SHOW_VULN_FALLBACK(l1tf);
598 CPU_SHOW_VULN_FALLBACK(mds);
599 CPU_SHOW_VULN_FALLBACK(tsx_async_abort);
600 CPU_SHOW_VULN_FALLBACK(itlb_multihit);
601 CPU_SHOW_VULN_FALLBACK(srbds);
602 CPU_SHOW_VULN_FALLBACK(mmio_stale_data);
603 CPU_SHOW_VULN_FALLBACK(retbleed);
604 CPU_SHOW_VULN_FALLBACK(spec_rstack_overflow);
605 CPU_SHOW_VULN_FALLBACK(gds);
606 CPU_SHOW_VULN_FALLBACK(reg_file_data_sampling);
607 
608 static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
609 static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
610 static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
611 static DEVICE_ATTR(spec_store_bypass, 0444, cpu_show_spec_store_bypass, NULL);
612 static DEVICE_ATTR(l1tf, 0444, cpu_show_l1tf, NULL);
613 static DEVICE_ATTR(mds, 0444, cpu_show_mds, NULL);
614 static DEVICE_ATTR(tsx_async_abort, 0444, cpu_show_tsx_async_abort, NULL);
615 static DEVICE_ATTR(itlb_multihit, 0444, cpu_show_itlb_multihit, NULL);
616 static DEVICE_ATTR(srbds, 0444, cpu_show_srbds, NULL);
617 static DEVICE_ATTR(mmio_stale_data, 0444, cpu_show_mmio_stale_data, NULL);
618 static DEVICE_ATTR(retbleed, 0444, cpu_show_retbleed, NULL);
619 static DEVICE_ATTR(spec_rstack_overflow, 0444, cpu_show_spec_rstack_overflow, NULL);
620 static DEVICE_ATTR(gather_data_sampling, 0444, cpu_show_gds, NULL);
621 static DEVICE_ATTR(reg_file_data_sampling, 0444, cpu_show_reg_file_data_sampling, NULL);
622 
623 static struct attribute *cpu_root_vulnerabilities_attrs[] = {
624 	&dev_attr_meltdown.attr,
625 	&dev_attr_spectre_v1.attr,
626 	&dev_attr_spectre_v2.attr,
627 	&dev_attr_spec_store_bypass.attr,
628 	&dev_attr_l1tf.attr,
629 	&dev_attr_mds.attr,
630 	&dev_attr_tsx_async_abort.attr,
631 	&dev_attr_itlb_multihit.attr,
632 	&dev_attr_srbds.attr,
633 	&dev_attr_mmio_stale_data.attr,
634 	&dev_attr_retbleed.attr,
635 	&dev_attr_spec_rstack_overflow.attr,
636 	&dev_attr_gather_data_sampling.attr,
637 	&dev_attr_reg_file_data_sampling.attr,
638 	NULL
639 };
640 
641 static const struct attribute_group cpu_root_vulnerabilities_group = {
642 	.name  = "vulnerabilities",
643 	.attrs = cpu_root_vulnerabilities_attrs,
644 };
645 
cpu_register_vulnerabilities(void)646 static void __init cpu_register_vulnerabilities(void)
647 {
648 	struct device *dev = bus_get_dev_root(&cpu_subsys);
649 
650 	if (dev) {
651 		if (sysfs_create_group(&dev->kobj, &cpu_root_vulnerabilities_group))
652 			pr_err("Unable to register CPU vulnerabilities\n");
653 		put_device(dev);
654 	}
655 }
656 
657 #else
cpu_register_vulnerabilities(void)658 static inline void cpu_register_vulnerabilities(void) { }
659 #endif
660 
cpu_dev_init(void)661 void __init cpu_dev_init(void)
662 {
663 	if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
664 		panic("Failed to register CPU subsystem");
665 
666 	cpu_dev_register_generic();
667 	cpu_register_vulnerabilities();
668 }
669