cpu.c 18 KB

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