cpu.c 14 KB

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