cpu.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "base.h"
  16. struct bus_type cpu_subsys = {
  17. .name = "cpu",
  18. .dev_name = "cpu",
  19. };
  20. EXPORT_SYMBOL_GPL(cpu_subsys);
  21. static DEFINE_PER_CPU(struct device *, cpu_sys_devices);
  22. #ifdef CONFIG_HOTPLUG_CPU
  23. static void change_cpu_under_node(struct cpu *cpu,
  24. unsigned int from_nid, unsigned int to_nid)
  25. {
  26. int cpuid = cpu->dev.id;
  27. unregister_cpu_under_node(cpuid, from_nid);
  28. register_cpu_under_node(cpuid, to_nid);
  29. cpu->node_id = to_nid;
  30. }
  31. static ssize_t show_online(struct device *dev,
  32. struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct cpu *cpu = container_of(dev, struct cpu, dev);
  36. return sprintf(buf, "%u\n", !!cpu_online(cpu->dev.id));
  37. }
  38. static ssize_t __ref store_online(struct device *dev,
  39. struct device_attribute *attr,
  40. const char *buf, size_t count)
  41. {
  42. struct cpu *cpu = container_of(dev, struct cpu, dev);
  43. int cpuid = cpu->dev.id;
  44. int from_nid, to_nid;
  45. ssize_t ret;
  46. cpu_hotplug_driver_lock();
  47. switch (buf[0]) {
  48. case '0':
  49. ret = cpu_down(cpuid);
  50. if (!ret)
  51. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  52. break;
  53. case '1':
  54. from_nid = cpu_to_node(cpuid);
  55. ret = cpu_up(cpuid);
  56. /*
  57. * When hot adding memory to memoryless node and enabling a cpu
  58. * on the node, node number of the cpu may internally change.
  59. */
  60. to_nid = cpu_to_node(cpuid);
  61. if (from_nid != to_nid)
  62. change_cpu_under_node(cpu, from_nid, to_nid);
  63. if (!ret)
  64. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  65. break;
  66. default:
  67. ret = -EINVAL;
  68. }
  69. cpu_hotplug_driver_unlock();
  70. if (ret >= 0)
  71. ret = count;
  72. return ret;
  73. }
  74. static DEVICE_ATTR(online, 0644, show_online, store_online);
  75. static struct attribute *hotplug_cpu_attrs[] = {
  76. &dev_attr_online.attr,
  77. NULL
  78. };
  79. static struct attribute_group hotplug_cpu_attr_group = {
  80. .attrs = hotplug_cpu_attrs,
  81. };
  82. void unregister_cpu(struct cpu *cpu)
  83. {
  84. int logical_cpu = cpu->dev.id;
  85. unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
  86. device_unregister(&cpu->dev);
  87. per_cpu(cpu_sys_devices, logical_cpu) = NULL;
  88. return;
  89. }
  90. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  91. static ssize_t cpu_probe_store(struct device *dev,
  92. struct device_attribute *attr,
  93. const char *buf,
  94. size_t count)
  95. {
  96. return arch_cpu_probe(buf, count);
  97. }
  98. static ssize_t cpu_release_store(struct device *dev,
  99. struct device_attribute *attr,
  100. const char *buf,
  101. size_t count)
  102. {
  103. return arch_cpu_release(buf, count);
  104. }
  105. static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
  106. static DEVICE_ATTR(release, S_IWUSR, NULL, cpu_release_store);
  107. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  108. #endif /* CONFIG_HOTPLUG_CPU */
  109. #ifdef CONFIG_KEXEC
  110. #include <linux/kexec.h>
  111. static ssize_t show_crash_notes(struct device *dev, struct device_attribute *attr,
  112. char *buf)
  113. {
  114. struct cpu *cpu = container_of(dev, struct cpu, dev);
  115. ssize_t rc;
  116. unsigned long long addr;
  117. int cpunum;
  118. cpunum = cpu->dev.id;
  119. /*
  120. * Might be reading other cpu's data based on which cpu read thread
  121. * has been scheduled. But cpu data (memory) is allocated once during
  122. * boot up and this data does not change there after. Hence this
  123. * operation should be safe. No locking required.
  124. */
  125. addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
  126. rc = sprintf(buf, "%Lx\n", addr);
  127. return rc;
  128. }
  129. static DEVICE_ATTR(crash_notes, 0400, show_crash_notes, NULL);
  130. static ssize_t show_crash_notes_size(struct device *dev,
  131. struct device_attribute *attr,
  132. char *buf)
  133. {
  134. ssize_t rc;
  135. rc = sprintf(buf, "%zu\n", sizeof(note_buf_t));
  136. return rc;
  137. }
  138. static DEVICE_ATTR(crash_notes_size, 0400, show_crash_notes_size, NULL);
  139. static struct attribute *crash_note_cpu_attrs[] = {
  140. &dev_attr_crash_notes.attr,
  141. &dev_attr_crash_notes_size.attr,
  142. NULL
  143. };
  144. static struct attribute_group crash_note_cpu_attr_group = {
  145. .attrs = crash_note_cpu_attrs,
  146. };
  147. #endif
  148. static const struct attribute_group *common_cpu_attr_groups[] = {
  149. #ifdef CONFIG_KEXEC
  150. &crash_note_cpu_attr_group,
  151. #endif
  152. NULL
  153. };
  154. static const struct attribute_group *hotplugable_cpu_attr_groups[] = {
  155. #ifdef CONFIG_KEXEC
  156. &crash_note_cpu_attr_group,
  157. #endif
  158. #ifdef CONFIG_HOTPLUG_CPU
  159. &hotplug_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 * 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. int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *(ca->map));
  176. buf[n++] = '\n';
  177. buf[n] = '\0';
  178. return n;
  179. }
  180. #define _CPU_ATTR(name, map) \
  181. { __ATTR(name, 0444, show_cpus_attr, NULL), map }
  182. /* Keep in sync with cpu_subsys_attrs */
  183. static struct cpu_attr cpu_attrs[] = {
  184. _CPU_ATTR(online, &cpu_online_mask),
  185. _CPU_ATTR(possible, &cpu_possible_mask),
  186. _CPU_ATTR(present, &cpu_present_mask),
  187. };
  188. /*
  189. * Print values for NR_CPUS and offlined cpus
  190. */
  191. static ssize_t print_cpus_kernel_max(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
  195. return n;
  196. }
  197. static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
  198. /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
  199. unsigned int total_cpus;
  200. static ssize_t print_cpus_offline(struct device *dev,
  201. struct device_attribute *attr, char *buf)
  202. {
  203. int n = 0, len = PAGE_SIZE-2;
  204. cpumask_var_t offline;
  205. /* display offline cpus < nr_cpu_ids */
  206. if (!alloc_cpumask_var(&offline, GFP_KERNEL))
  207. return -ENOMEM;
  208. cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
  209. n = cpulist_scnprintf(buf, len, offline);
  210. free_cpumask_var(offline);
  211. /* display offline cpus >= nr_cpu_ids */
  212. if (total_cpus && nr_cpu_ids < total_cpus) {
  213. if (n && n < len)
  214. buf[n++] = ',';
  215. if (nr_cpu_ids == total_cpus-1)
  216. n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids);
  217. else
  218. n += snprintf(&buf[n], len - n, "%d-%d",
  219. nr_cpu_ids, total_cpus-1);
  220. }
  221. n += snprintf(&buf[n], len - n, "\n");
  222. return n;
  223. }
  224. static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
  225. static void cpu_device_release(struct device *dev)
  226. {
  227. /*
  228. * This is an empty function to prevent the driver core from spitting a
  229. * warning at us. Yes, I know this is directly opposite of what the
  230. * documentation for the driver core and kobjects say, and the author
  231. * of this code has already been publically ridiculed for doing
  232. * something as foolish as this. However, at this point in time, it is
  233. * the only way to handle the issue of statically allocated cpu
  234. * devices. The different architectures will have their cpu device
  235. * code reworked to properly handle this in the near future, so this
  236. * function will then be changed to correctly free up the memory held
  237. * by the cpu device.
  238. *
  239. * Never copy this way of doing things, or you too will be made fun of
  240. * on the linux-kernel list, you have been warned.
  241. */
  242. }
  243. /*
  244. * register_cpu - Setup a sysfs device for a CPU.
  245. * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
  246. * sysfs for this CPU.
  247. * @num - CPU number to use when creating the device.
  248. *
  249. * Initialize and register the CPU device.
  250. */
  251. int __cpuinit register_cpu(struct cpu *cpu, int num)
  252. {
  253. int error;
  254. cpu->node_id = cpu_to_node(num);
  255. memset(&cpu->dev, 0x00, sizeof(struct device));
  256. cpu->dev.id = num;
  257. cpu->dev.bus = &cpu_subsys;
  258. cpu->dev.release = cpu_device_release;
  259. #ifdef CONFIG_ARCH_HAS_CPU_AUTOPROBE
  260. cpu->dev.bus->uevent = arch_cpu_uevent;
  261. #endif
  262. cpu->dev.groups = common_cpu_attr_groups;
  263. if (cpu->hotpluggable)
  264. cpu->dev.groups = hotplugable_cpu_attr_groups;
  265. error = device_register(&cpu->dev);
  266. if (!error)
  267. per_cpu(cpu_sys_devices, num) = &cpu->dev;
  268. if (!error)
  269. register_cpu_under_node(num, cpu_to_node(num));
  270. return error;
  271. }
  272. struct device *get_cpu_device(unsigned cpu)
  273. {
  274. if (cpu < nr_cpu_ids && cpu_possible(cpu))
  275. return per_cpu(cpu_sys_devices, cpu);
  276. else
  277. return NULL;
  278. }
  279. EXPORT_SYMBOL_GPL(get_cpu_device);
  280. #ifdef CONFIG_ARCH_HAS_CPU_AUTOPROBE
  281. static DEVICE_ATTR(modalias, 0444, arch_print_cpu_modalias, NULL);
  282. #endif
  283. static struct attribute *cpu_root_attrs[] = {
  284. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  285. &dev_attr_probe.attr,
  286. &dev_attr_release.attr,
  287. #endif
  288. &cpu_attrs[0].attr.attr,
  289. &cpu_attrs[1].attr.attr,
  290. &cpu_attrs[2].attr.attr,
  291. &dev_attr_kernel_max.attr,
  292. &dev_attr_offline.attr,
  293. #ifdef CONFIG_ARCH_HAS_CPU_AUTOPROBE
  294. &dev_attr_modalias.attr,
  295. #endif
  296. NULL
  297. };
  298. static struct attribute_group cpu_root_attr_group = {
  299. .attrs = cpu_root_attrs,
  300. };
  301. static const struct attribute_group *cpu_root_attr_groups[] = {
  302. &cpu_root_attr_group,
  303. NULL,
  304. };
  305. bool cpu_is_hotpluggable(unsigned cpu)
  306. {
  307. struct device *dev = get_cpu_device(cpu);
  308. return dev && container_of(dev, struct cpu, dev)->hotpluggable;
  309. }
  310. EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
  311. #ifdef CONFIG_GENERIC_CPU_DEVICES
  312. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  313. #endif
  314. static void __init cpu_dev_register_generic(void)
  315. {
  316. #ifdef CONFIG_GENERIC_CPU_DEVICES
  317. int i;
  318. for_each_possible_cpu(i) {
  319. if (register_cpu(&per_cpu(cpu_devices, i), i))
  320. panic("Failed to register CPU device");
  321. }
  322. #endif
  323. }
  324. void __init cpu_dev_init(void)
  325. {
  326. if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
  327. panic("Failed to register CPU subsystem");
  328. cpu_dev_register_generic();
  329. }