cpu.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <asm/semaphore.h>
  17. /* This protects CPUs going up and down... */
  18. DECLARE_MUTEX(cpucontrol);
  19. static struct notifier_block *cpu_chain;
  20. /* Need to know about CPUs going up/down? */
  21. int register_cpu_notifier(struct notifier_block *nb)
  22. {
  23. int ret;
  24. if ((ret = down_interruptible(&cpucontrol)) != 0)
  25. return ret;
  26. ret = notifier_chain_register(&cpu_chain, nb);
  27. up(&cpucontrol);
  28. return ret;
  29. }
  30. EXPORT_SYMBOL(register_cpu_notifier);
  31. void unregister_cpu_notifier(struct notifier_block *nb)
  32. {
  33. down(&cpucontrol);
  34. notifier_chain_unregister(&cpu_chain, nb);
  35. up(&cpucontrol);
  36. }
  37. EXPORT_SYMBOL(unregister_cpu_notifier);
  38. #ifdef CONFIG_HOTPLUG_CPU
  39. static inline void check_for_tasks(int cpu)
  40. {
  41. struct task_struct *p;
  42. write_lock_irq(&tasklist_lock);
  43. for_each_process(p) {
  44. if (task_cpu(p) == cpu &&
  45. (!cputime_eq(p->utime, cputime_zero) ||
  46. !cputime_eq(p->stime, cputime_zero)))
  47. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  48. (state = %ld, flags = %lx) \n",
  49. p->comm, p->pid, cpu, p->state, p->flags);
  50. }
  51. write_unlock_irq(&tasklist_lock);
  52. }
  53. /* Take this CPU down. */
  54. static int take_cpu_down(void *unused)
  55. {
  56. int err;
  57. /* Take offline: makes arch_cpu_down somewhat easier. */
  58. cpu_clear(smp_processor_id(), cpu_online_map);
  59. /* Ensure this CPU doesn't handle any more interrupts. */
  60. err = __cpu_disable();
  61. if (err < 0)
  62. cpu_set(smp_processor_id(), cpu_online_map);
  63. else
  64. /* Force idle task to run as soon as we yield: it should
  65. immediately notice cpu is offline and die quickly. */
  66. sched_idle_next();
  67. return err;
  68. }
  69. int cpu_down(unsigned int cpu)
  70. {
  71. int err;
  72. struct task_struct *p;
  73. cpumask_t old_allowed, tmp;
  74. if ((err = lock_cpu_hotplug_interruptible()) != 0)
  75. return err;
  76. if (num_online_cpus() == 1) {
  77. err = -EBUSY;
  78. goto out;
  79. }
  80. if (!cpu_online(cpu)) {
  81. err = -EINVAL;
  82. goto out;
  83. }
  84. err = notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
  85. (void *)(long)cpu);
  86. if (err == NOTIFY_BAD) {
  87. printk("%s: attempt to take down CPU %u failed\n",
  88. __FUNCTION__, cpu);
  89. err = -EINVAL;
  90. goto out;
  91. }
  92. /* Ensure that we are not runnable on dying cpu */
  93. old_allowed = current->cpus_allowed;
  94. tmp = CPU_MASK_ALL;
  95. cpu_clear(cpu, tmp);
  96. set_cpus_allowed(current, tmp);
  97. p = __stop_machine_run(take_cpu_down, NULL, cpu);
  98. if (IS_ERR(p)) {
  99. /* CPU didn't die: tell everyone. Can't complain. */
  100. if (notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
  101. (void *)(long)cpu) == NOTIFY_BAD)
  102. BUG();
  103. err = PTR_ERR(p);
  104. goto out_allowed;
  105. }
  106. if (cpu_online(cpu))
  107. goto out_thread;
  108. /* Wait for it to sleep (leaving idle task). */
  109. while (!idle_cpu(cpu))
  110. yield();
  111. /* This actually kills the CPU. */
  112. __cpu_die(cpu);
  113. /* Move it here so it can run. */
  114. kthread_bind(p, get_cpu());
  115. put_cpu();
  116. /* CPU is completely dead: tell everyone. Too late to complain. */
  117. if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
  118. == NOTIFY_BAD)
  119. BUG();
  120. check_for_tasks(cpu);
  121. out_thread:
  122. err = kthread_stop(p);
  123. out_allowed:
  124. set_cpus_allowed(current, old_allowed);
  125. out:
  126. unlock_cpu_hotplug();
  127. return err;
  128. }
  129. #endif /*CONFIG_HOTPLUG_CPU*/
  130. int __devinit cpu_up(unsigned int cpu)
  131. {
  132. int ret;
  133. void *hcpu = (void *)(long)cpu;
  134. if ((ret = down_interruptible(&cpucontrol)) != 0)
  135. return ret;
  136. if (cpu_online(cpu) || !cpu_present(cpu)) {
  137. ret = -EINVAL;
  138. goto out;
  139. }
  140. ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
  141. if (ret == NOTIFY_BAD) {
  142. printk("%s: attempt to bring up CPU %u failed\n",
  143. __FUNCTION__, cpu);
  144. ret = -EINVAL;
  145. goto out_notify;
  146. }
  147. /* Arch-specific enabling code. */
  148. ret = __cpu_up(cpu);
  149. if (ret != 0)
  150. goto out_notify;
  151. if (!cpu_online(cpu))
  152. BUG();
  153. /* Now call notifier in preparation. */
  154. notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
  155. out_notify:
  156. if (ret != 0)
  157. notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
  158. out:
  159. up(&cpucontrol);
  160. return ret;
  161. }