cpu.c 8.6 KB

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