cpu.c 15 KB

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