cpu.c 8.1 KB

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