cpu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /* CPU control.
  2. * (C) 2001, 2002, 2003, 2004 Rusty Russell
  3. *
  4. * This code is licenced under the GPL.
  5. */
  6. #include <linux/proc_fs.h>
  7. #include <linux/smp.h>
  8. #include <linux/init.h>
  9. #include <linux/notifier.h>
  10. #include <linux/sched.h>
  11. #include <linux/unistd.h>
  12. #include <linux/cpu.h>
  13. #include <linux/module.h>
  14. #include <linux/kthread.h>
  15. #include <linux/stop_machine.h>
  16. #include <linux/mutex.h>
  17. /*
  18. * Represents all cpu's present in the system
  19. * In systems capable of hotplug, this map could dynamically grow
  20. * as new cpu's are detected in the system via any platform specific
  21. * method, such as ACPI for e.g.
  22. */
  23. cpumask_t cpu_present_map __read_mostly;
  24. EXPORT_SYMBOL(cpu_present_map);
  25. #ifndef CONFIG_SMP
  26. /*
  27. * Represents all cpu's that are currently online.
  28. */
  29. cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
  30. EXPORT_SYMBOL(cpu_online_map);
  31. cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
  32. EXPORT_SYMBOL(cpu_possible_map);
  33. #else /* CONFIG_SMP */
  34. /* Serializes the updates to cpu_online_map, cpu_present_map */
  35. static DEFINE_MUTEX(cpu_add_remove_lock);
  36. static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
  37. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  38. * Should always be manipulated under cpu_add_remove_lock
  39. */
  40. static int cpu_hotplug_disabled;
  41. static struct {
  42. struct task_struct *active_writer;
  43. struct mutex lock; /* Synchronizes accesses to refcount, */
  44. /*
  45. * Also blocks the new readers during
  46. * an ongoing cpu hotplug operation.
  47. */
  48. int refcount;
  49. } cpu_hotplug;
  50. void __init cpu_hotplug_init(void)
  51. {
  52. cpu_hotplug.active_writer = NULL;
  53. mutex_init(&cpu_hotplug.lock);
  54. cpu_hotplug.refcount = 0;
  55. }
  56. cpumask_t cpu_active_map;
  57. #ifdef CONFIG_HOTPLUG_CPU
  58. void get_online_cpus(void)
  59. {
  60. might_sleep();
  61. if (cpu_hotplug.active_writer == current)
  62. return;
  63. mutex_lock(&cpu_hotplug.lock);
  64. cpu_hotplug.refcount++;
  65. mutex_unlock(&cpu_hotplug.lock);
  66. }
  67. EXPORT_SYMBOL_GPL(get_online_cpus);
  68. void put_online_cpus(void)
  69. {
  70. if (cpu_hotplug.active_writer == current)
  71. return;
  72. mutex_lock(&cpu_hotplug.lock);
  73. if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
  74. wake_up_process(cpu_hotplug.active_writer);
  75. mutex_unlock(&cpu_hotplug.lock);
  76. }
  77. EXPORT_SYMBOL_GPL(put_online_cpus);
  78. #endif /* CONFIG_HOTPLUG_CPU */
  79. /*
  80. * The following two API's must be used when attempting
  81. * to serialize the updates to cpu_online_map, cpu_present_map.
  82. */
  83. void cpu_maps_update_begin(void)
  84. {
  85. mutex_lock(&cpu_add_remove_lock);
  86. }
  87. void cpu_maps_update_done(void)
  88. {
  89. mutex_unlock(&cpu_add_remove_lock);
  90. }
  91. /*
  92. * This ensures that the hotplug operation can begin only when the
  93. * refcount goes to zero.
  94. *
  95. * Note that during a cpu-hotplug operation, the new readers, if any,
  96. * will be blocked by the cpu_hotplug.lock
  97. *
  98. * Since cpu_hotplug_begin() is always called after invoking
  99. * cpu_maps_update_begin(), we can be sure that only one writer is active.
  100. *
  101. * Note that theoretically, there is a possibility of a livelock:
  102. * - Refcount goes to zero, last reader wakes up the sleeping
  103. * writer.
  104. * - Last reader unlocks the cpu_hotplug.lock.
  105. * - A new reader arrives at this moment, bumps up the refcount.
  106. * - The writer acquires the cpu_hotplug.lock finds the refcount
  107. * non zero and goes to sleep again.
  108. *
  109. * However, this is very difficult to achieve in practice since
  110. * get_online_cpus() not an api which is called all that often.
  111. *
  112. */
  113. static void cpu_hotplug_begin(void)
  114. {
  115. cpu_hotplug.active_writer = current;
  116. for (;;) {
  117. mutex_lock(&cpu_hotplug.lock);
  118. if (likely(!cpu_hotplug.refcount))
  119. break;
  120. __set_current_state(TASK_UNINTERRUPTIBLE);
  121. mutex_unlock(&cpu_hotplug.lock);
  122. schedule();
  123. }
  124. }
  125. static void cpu_hotplug_done(void)
  126. {
  127. cpu_hotplug.active_writer = NULL;
  128. mutex_unlock(&cpu_hotplug.lock);
  129. }
  130. /* Need to know about CPUs going up/down? */
  131. int __ref register_cpu_notifier(struct notifier_block *nb)
  132. {
  133. int ret;
  134. cpu_maps_update_begin();
  135. ret = raw_notifier_chain_register(&cpu_chain, nb);
  136. cpu_maps_update_done();
  137. return ret;
  138. }
  139. #ifdef CONFIG_HOTPLUG_CPU
  140. EXPORT_SYMBOL(register_cpu_notifier);
  141. void __ref unregister_cpu_notifier(struct notifier_block *nb)
  142. {
  143. cpu_maps_update_begin();
  144. raw_notifier_chain_unregister(&cpu_chain, nb);
  145. cpu_maps_update_done();
  146. }
  147. EXPORT_SYMBOL(unregister_cpu_notifier);
  148. static inline void check_for_tasks(int cpu)
  149. {
  150. struct task_struct *p;
  151. write_lock_irq(&tasklist_lock);
  152. for_each_process(p) {
  153. if (task_cpu(p) == cpu &&
  154. (!cputime_eq(p->utime, cputime_zero) ||
  155. !cputime_eq(p->stime, cputime_zero)))
  156. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  157. (state = %ld, flags = %x) \n",
  158. p->comm, task_pid_nr(p), cpu,
  159. p->state, p->flags);
  160. }
  161. write_unlock_irq(&tasklist_lock);
  162. }
  163. struct take_cpu_down_param {
  164. unsigned long mod;
  165. void *hcpu;
  166. };
  167. /* Take this CPU down. */
  168. static int __ref take_cpu_down(void *_param)
  169. {
  170. struct take_cpu_down_param *param = _param;
  171. int err;
  172. raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
  173. param->hcpu);
  174. /* Ensure this CPU doesn't handle any more interrupts. */
  175. err = __cpu_disable();
  176. if (err < 0)
  177. return err;
  178. /* Force idle task to run as soon as we yield: it should
  179. immediately notice cpu is offline and die quickly. */
  180. sched_idle_next();
  181. return 0;
  182. }
  183. /* Requires cpu_add_remove_lock to be held */
  184. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
  185. {
  186. int err, nr_calls = 0;
  187. struct task_struct *p;
  188. cpumask_t old_allowed, tmp;
  189. void *hcpu = (void *)(long)cpu;
  190. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  191. struct take_cpu_down_param tcd_param = {
  192. .mod = mod,
  193. .hcpu = hcpu,
  194. };
  195. if (num_online_cpus() == 1)
  196. return -EBUSY;
  197. if (!cpu_online(cpu))
  198. return -EINVAL;
  199. cpu_hotplug_begin();
  200. err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
  201. hcpu, -1, &nr_calls);
  202. if (err == NOTIFY_BAD) {
  203. nr_calls--;
  204. __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
  205. hcpu, nr_calls, NULL);
  206. printk("%s: attempt to take down CPU %u failed\n",
  207. __func__, cpu);
  208. err = -EINVAL;
  209. goto out_release;
  210. }
  211. /* Ensure that we are not runnable on dying cpu */
  212. old_allowed = current->cpus_allowed;
  213. cpus_setall(tmp);
  214. cpu_clear(cpu, tmp);
  215. set_cpus_allowed_ptr(current, &tmp);
  216. p = __stop_machine_run(take_cpu_down, &tcd_param, cpu);
  217. if (IS_ERR(p) || cpu_online(cpu)) {
  218. /* CPU didn't die: tell everyone. Can't complain. */
  219. if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
  220. hcpu) == NOTIFY_BAD)
  221. BUG();
  222. if (IS_ERR(p)) {
  223. err = PTR_ERR(p);
  224. goto out_allowed;
  225. }
  226. goto out_thread;
  227. }
  228. /* Wait for it to sleep (leaving idle task). */
  229. while (!idle_cpu(cpu))
  230. yield();
  231. /* This actually kills the CPU. */
  232. __cpu_die(cpu);
  233. /* CPU is completely dead: tell everyone. Too late to complain. */
  234. if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
  235. hcpu) == NOTIFY_BAD)
  236. BUG();
  237. check_for_tasks(cpu);
  238. out_thread:
  239. err = kthread_stop(p);
  240. out_allowed:
  241. set_cpus_allowed_ptr(current, &old_allowed);
  242. out_release:
  243. cpu_hotplug_done();
  244. if (!err) {
  245. if (raw_notifier_call_chain(&cpu_chain, CPU_POST_DEAD | mod,
  246. hcpu) == NOTIFY_BAD)
  247. BUG();
  248. }
  249. return err;
  250. }
  251. int __ref cpu_down(unsigned int cpu)
  252. {
  253. int err = 0;
  254. cpu_maps_update_begin();
  255. if (cpu_hotplug_disabled) {
  256. err = -EBUSY;
  257. goto out;
  258. }
  259. cpu_clear(cpu, cpu_active_map);
  260. /*
  261. * Make sure the all cpus did the reschedule and are not
  262. * using stale version of the cpu_active_map.
  263. * This is not strictly necessary becuase stop_machine()
  264. * that we run down the line already provides the required
  265. * synchronization. But it's really a side effect and we do not
  266. * want to depend on the innards of the stop_machine here.
  267. */
  268. synchronize_sched();
  269. err = _cpu_down(cpu, 0);
  270. if (cpu_online(cpu))
  271. cpu_set(cpu, cpu_active_map);
  272. out:
  273. cpu_maps_update_done();
  274. return err;
  275. }
  276. EXPORT_SYMBOL(cpu_down);
  277. #endif /*CONFIG_HOTPLUG_CPU*/
  278. /* Requires cpu_add_remove_lock to be held */
  279. static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
  280. {
  281. int ret, nr_calls = 0;
  282. void *hcpu = (void *)(long)cpu;
  283. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  284. if (cpu_online(cpu) || !cpu_present(cpu))
  285. return -EINVAL;
  286. cpu_hotplug_begin();
  287. ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
  288. -1, &nr_calls);
  289. if (ret == NOTIFY_BAD) {
  290. nr_calls--;
  291. printk("%s: attempt to bring up CPU %u failed\n",
  292. __func__, cpu);
  293. ret = -EINVAL;
  294. goto out_notify;
  295. }
  296. /* Arch-specific enabling code. */
  297. ret = __cpu_up(cpu);
  298. if (ret != 0)
  299. goto out_notify;
  300. BUG_ON(!cpu_online(cpu));
  301. /* Now call notifier in preparation. */
  302. raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
  303. out_notify:
  304. if (ret != 0)
  305. __raw_notifier_call_chain(&cpu_chain,
  306. CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
  307. cpu_hotplug_done();
  308. return ret;
  309. }
  310. int __cpuinit cpu_up(unsigned int cpu)
  311. {
  312. int err = 0;
  313. if (!cpu_isset(cpu, cpu_possible_map)) {
  314. printk(KERN_ERR "can't online cpu %d because it is not "
  315. "configured as may-hotadd at boot time\n", cpu);
  316. #if defined(CONFIG_IA64) || defined(CONFIG_X86_64) || defined(CONFIG_S390)
  317. printk(KERN_ERR "please check additional_cpus= boot "
  318. "parameter\n");
  319. #endif
  320. return -EINVAL;
  321. }
  322. cpu_maps_update_begin();
  323. if (cpu_hotplug_disabled) {
  324. err = -EBUSY;
  325. goto out;
  326. }
  327. err = _cpu_up(cpu, 0);
  328. if (cpu_online(cpu))
  329. cpu_set(cpu, cpu_active_map);
  330. out:
  331. cpu_maps_update_done();
  332. return err;
  333. }
  334. #ifdef CONFIG_PM_SLEEP_SMP
  335. static cpumask_t frozen_cpus;
  336. int disable_nonboot_cpus(void)
  337. {
  338. int cpu, first_cpu, error = 0;
  339. cpu_maps_update_begin();
  340. first_cpu = first_cpu(cpu_online_map);
  341. /* We take down all of the non-boot CPUs in one shot to avoid races
  342. * with the userspace trying to use the CPU hotplug at the same time
  343. */
  344. cpus_clear(frozen_cpus);
  345. printk("Disabling non-boot CPUs ...\n");
  346. for_each_online_cpu(cpu) {
  347. if (cpu == first_cpu)
  348. continue;
  349. error = _cpu_down(cpu, 1);
  350. if (!error) {
  351. cpu_set(cpu, frozen_cpus);
  352. printk("CPU%d is down\n", cpu);
  353. } else {
  354. printk(KERN_ERR "Error taking CPU%d down: %d\n",
  355. cpu, error);
  356. break;
  357. }
  358. }
  359. if (!error) {
  360. BUG_ON(num_online_cpus() > 1);
  361. /* Make sure the CPUs won't be enabled by someone else */
  362. cpu_hotplug_disabled = 1;
  363. } else {
  364. printk(KERN_ERR "Non-boot CPUs are not disabled\n");
  365. }
  366. cpu_maps_update_done();
  367. return error;
  368. }
  369. void __ref enable_nonboot_cpus(void)
  370. {
  371. int cpu, error;
  372. /* Allow everyone to use the CPU hotplug again */
  373. cpu_maps_update_begin();
  374. cpu_hotplug_disabled = 0;
  375. if (cpus_empty(frozen_cpus))
  376. goto out;
  377. printk("Enabling non-boot CPUs ...\n");
  378. for_each_cpu_mask_nr(cpu, frozen_cpus) {
  379. error = _cpu_up(cpu, 1);
  380. if (!error) {
  381. printk("CPU%d is up\n", cpu);
  382. continue;
  383. }
  384. printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
  385. }
  386. cpus_clear(frozen_cpus);
  387. out:
  388. cpu_maps_update_done();
  389. }
  390. #endif /* CONFIG_PM_SLEEP_SMP */
  391. #endif /* CONFIG_SMP */
  392. /* 64 bits of zeros, for initializers. */
  393. #if BITS_PER_LONG == 32
  394. #define Z64 0, 0
  395. #else
  396. #define Z64 0
  397. #endif
  398. /* Initializer macros. */
  399. #define CMI0(n) { .bits = { 1UL << (n) } }
  400. #define CMI(n, ...) { .bits = { __VA_ARGS__, 1UL << ((n) % BITS_PER_LONG) } }
  401. #define CMI8(n, ...) \
  402. CMI((n), __VA_ARGS__), CMI((n)+1, __VA_ARGS__), \
  403. CMI((n)+2, __VA_ARGS__), CMI((n)+3, __VA_ARGS__), \
  404. CMI((n)+4, __VA_ARGS__), CMI((n)+5, __VA_ARGS__), \
  405. CMI((n)+6, __VA_ARGS__), CMI((n)+7, __VA_ARGS__)
  406. #if BITS_PER_LONG == 32
  407. #define CMI64(n, ...) \
  408. CMI8((n), __VA_ARGS__), CMI8((n)+8, __VA_ARGS__), \
  409. CMI8((n)+16, __VA_ARGS__), CMI8((n)+24, __VA_ARGS__), \
  410. CMI8((n)+32, 0, __VA_ARGS__), CMI8((n)+40, 0, __VA_ARGS__), \
  411. CMI8((n)+48, 0, __VA_ARGS__), CMI8((n)+56, 0, __VA_ARGS__)
  412. #else
  413. #define CMI64(n, ...) \
  414. CMI8((n), __VA_ARGS__), CMI8((n)+8, __VA_ARGS__), \
  415. CMI8((n)+16, __VA_ARGS__), CMI8((n)+24, __VA_ARGS__), \
  416. CMI8((n)+32, __VA_ARGS__), CMI8((n)+40, __VA_ARGS__), \
  417. CMI8((n)+48, __VA_ARGS__), CMI8((n)+56, __VA_ARGS__)
  418. #endif
  419. #define CMI256(n, ...) \
  420. CMI64((n), __VA_ARGS__), CMI64((n)+64, Z64, __VA_ARGS__), \
  421. CMI64((n)+128, Z64, Z64, __VA_ARGS__), \
  422. CMI64((n)+192, Z64, Z64, Z64, __VA_ARGS__)
  423. #define Z256 Z64, Z64, Z64, Z64
  424. #define CMI1024(n, ...) \
  425. CMI256((n), __VA_ARGS__), \
  426. CMI256((n)+256, Z256, __VA_ARGS__), \
  427. CMI256((n)+512, Z256, Z256, __VA_ARGS__), \
  428. CMI256((n)+768, Z256, Z256, Z256, __VA_ARGS__)
  429. #define Z1024 Z256, Z256, Z256, Z256
  430. /* We want this statically initialized, just to be safe. We try not
  431. * to waste too much space, either. */
  432. static const cpumask_t cpumask_map[]
  433. #ifdef CONFIG_HAVE_CPUMASK_OF_CPU_MAP
  434. __initdata
  435. #endif
  436. = {
  437. CMI0(0), CMI0(1), CMI0(2), CMI0(3),
  438. #if NR_CPUS > 4
  439. CMI0(4), CMI0(5), CMI0(6), CMI0(7),
  440. #endif
  441. #if NR_CPUS > 8
  442. CMI0(8), CMI0(9), CMI0(10), CMI0(11),
  443. CMI0(12), CMI0(13), CMI0(14), CMI0(15),
  444. #endif
  445. #if NR_CPUS > 16
  446. CMI0(16), CMI0(17), CMI0(18), CMI0(19),
  447. CMI0(20), CMI0(21), CMI0(22), CMI0(23),
  448. CMI0(24), CMI0(25), CMI0(26), CMI0(27),
  449. CMI0(28), CMI0(29), CMI0(30), CMI0(31),
  450. #endif
  451. #if NR_CPUS > 32
  452. #if BITS_PER_LONG == 32
  453. CMI(32, 0), CMI(33, 0), CMI(34, 0), CMI(35, 0),
  454. CMI(36, 0), CMI(37, 0), CMI(38, 0), CMI(39, 0),
  455. CMI(40, 0), CMI(41, 0), CMI(42, 0), CMI(43, 0),
  456. CMI(44, 0), CMI(45, 0), CMI(46, 0), CMI(47, 0),
  457. CMI(48, 0), CMI(49, 0), CMI(50, 0), CMI(51, 0),
  458. CMI(52, 0), CMI(53, 0), CMI(54, 0), CMI(55, 0),
  459. CMI(56, 0), CMI(57, 0), CMI(58, 0), CMI(59, 0),
  460. CMI(60, 0), CMI(61, 0), CMI(62, 0), CMI(63, 0),
  461. #else
  462. CMI0(32), CMI0(33), CMI0(34), CMI0(35),
  463. CMI0(36), CMI0(37), CMI0(38), CMI0(39),
  464. CMI0(40), CMI0(41), CMI0(42), CMI0(43),
  465. CMI0(44), CMI0(45), CMI0(46), CMI0(47),
  466. CMI0(48), CMI0(49), CMI0(50), CMI0(51),
  467. CMI0(52), CMI0(53), CMI0(54), CMI0(55),
  468. CMI0(56), CMI0(57), CMI0(58), CMI0(59),
  469. CMI0(60), CMI0(61), CMI0(62), CMI0(63),
  470. #endif /* BITS_PER_LONG == 64 */
  471. #endif
  472. #if NR_CPUS > 64
  473. CMI64(64, Z64),
  474. #endif
  475. #if NR_CPUS > 128
  476. CMI64(128, Z64, Z64), CMI64(192, Z64, Z64, Z64),
  477. #endif
  478. #if NR_CPUS > 256
  479. CMI256(256, Z256),
  480. #endif
  481. #if NR_CPUS > 512
  482. CMI256(512, Z256, Z256), CMI256(768, Z256, Z256, Z256),
  483. #endif
  484. #if NR_CPUS > 1024
  485. CMI1024(1024, Z1024),
  486. #endif
  487. #if NR_CPUS > 2048
  488. CMI1024(2048, Z1024, Z1024), CMI1024(3072, Z1024, Z1024, Z1024),
  489. #endif
  490. #if NR_CPUS > 4096
  491. #error NR_CPUS too big. Fix initializers or set CONFIG_HAVE_CPUMASK_OF_CPU_MAP
  492. #endif
  493. };
  494. const cpumask_t *cpumask_of_cpu_map = cpumask_map;