cpu.c 9.2 KB

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