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