cpu.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. EXPORT_SYMBOL_GPL(cpucontrol);
  20. static struct notifier_block *cpu_chain;
  21. /*
  22. * Used to check by callers if they need to acquire the cpucontrol
  23. * or not to protect a cpu from being removed. Its sometimes required to
  24. * call these functions both for normal operations, and in response to
  25. * a cpu being added/removed. If the context of the call is in the same
  26. * thread context as a CPU hotplug thread, we dont need to take the lock
  27. * since its already protected
  28. * check drivers/cpufreq/cpufreq.c for its usage - Ashok Raj
  29. */
  30. int current_in_cpu_hotplug(void)
  31. {
  32. return (current->flags & PF_HOTPLUG_CPU);
  33. }
  34. EXPORT_SYMBOL_GPL(current_in_cpu_hotplug);
  35. /* Need to know about CPUs going up/down? */
  36. int register_cpu_notifier(struct notifier_block *nb)
  37. {
  38. int ret;
  39. if ((ret = down_interruptible(&cpucontrol)) != 0)
  40. return ret;
  41. ret = notifier_chain_register(&cpu_chain, nb);
  42. up(&cpucontrol);
  43. return ret;
  44. }
  45. EXPORT_SYMBOL(register_cpu_notifier);
  46. void unregister_cpu_notifier(struct notifier_block *nb)
  47. {
  48. down(&cpucontrol);
  49. notifier_chain_unregister(&cpu_chain, nb);
  50. up(&cpucontrol);
  51. }
  52. EXPORT_SYMBOL(unregister_cpu_notifier);
  53. #ifdef CONFIG_HOTPLUG_CPU
  54. static inline void check_for_tasks(int cpu)
  55. {
  56. struct task_struct *p;
  57. write_lock_irq(&tasklist_lock);
  58. for_each_process(p) {
  59. if (task_cpu(p) == cpu &&
  60. (!cputime_eq(p->utime, cputime_zero) ||
  61. !cputime_eq(p->stime, cputime_zero)))
  62. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  63. (state = %ld, flags = %lx) \n",
  64. p->comm, p->pid, cpu, p->state, p->flags);
  65. }
  66. write_unlock_irq(&tasklist_lock);
  67. }
  68. /* Take this CPU down. */
  69. static int take_cpu_down(void *unused)
  70. {
  71. int err;
  72. /* Ensure this CPU doesn't handle any more interrupts. */
  73. err = __cpu_disable();
  74. if (err < 0)
  75. return err;
  76. /* Force idle task to run as soon as we yield: it should
  77. immediately notice cpu is offline and die quickly. */
  78. sched_idle_next();
  79. return 0;
  80. }
  81. int cpu_down(unsigned int cpu)
  82. {
  83. int err;
  84. struct task_struct *p;
  85. cpumask_t old_allowed, tmp;
  86. if ((err = lock_cpu_hotplug_interruptible()) != 0)
  87. return err;
  88. if (num_online_cpus() == 1) {
  89. err = -EBUSY;
  90. goto out;
  91. }
  92. if (!cpu_online(cpu)) {
  93. err = -EINVAL;
  94. goto out;
  95. }
  96. /*
  97. * Leave a trace in current->flags indicating we are already in
  98. * process of performing CPU hotplug. Callers can check if cpucontrol
  99. * is already acquired by current thread, and if so not cause
  100. * a dead lock by not acquiring the lock
  101. */
  102. current->flags |= PF_HOTPLUG_CPU;
  103. err = notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
  104. (void *)(long)cpu);
  105. if (err == NOTIFY_BAD) {
  106. printk("%s: attempt to take down CPU %u failed\n",
  107. __FUNCTION__, cpu);
  108. err = -EINVAL;
  109. goto out;
  110. }
  111. /* Ensure that we are not runnable on dying cpu */
  112. old_allowed = current->cpus_allowed;
  113. tmp = CPU_MASK_ALL;
  114. cpu_clear(cpu, tmp);
  115. set_cpus_allowed(current, tmp);
  116. p = __stop_machine_run(take_cpu_down, NULL, cpu);
  117. if (IS_ERR(p)) {
  118. /* CPU didn't die: tell everyone. Can't complain. */
  119. if (notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
  120. (void *)(long)cpu) == NOTIFY_BAD)
  121. BUG();
  122. err = PTR_ERR(p);
  123. goto out_allowed;
  124. }
  125. if (cpu_online(cpu))
  126. goto out_thread;
  127. /* Wait for it to sleep (leaving idle task). */
  128. while (!idle_cpu(cpu))
  129. yield();
  130. /* This actually kills the CPU. */
  131. __cpu_die(cpu);
  132. /* Move it here so it can run. */
  133. kthread_bind(p, get_cpu());
  134. put_cpu();
  135. /* CPU is completely dead: tell everyone. Too late to complain. */
  136. if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
  137. == NOTIFY_BAD)
  138. BUG();
  139. check_for_tasks(cpu);
  140. out_thread:
  141. err = kthread_stop(p);
  142. out_allowed:
  143. set_cpus_allowed(current, old_allowed);
  144. out:
  145. current->flags &= ~PF_HOTPLUG_CPU;
  146. unlock_cpu_hotplug();
  147. return err;
  148. }
  149. #endif /*CONFIG_HOTPLUG_CPU*/
  150. int __devinit cpu_up(unsigned int cpu)
  151. {
  152. int ret;
  153. void *hcpu = (void *)(long)cpu;
  154. if ((ret = down_interruptible(&cpucontrol)) != 0)
  155. return ret;
  156. if (cpu_online(cpu) || !cpu_present(cpu)) {
  157. ret = -EINVAL;
  158. goto out;
  159. }
  160. /*
  161. * Leave a trace in current->flags indicating we are already in
  162. * process of performing CPU hotplug.
  163. */
  164. current->flags |= PF_HOTPLUG_CPU;
  165. ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
  166. if (ret == NOTIFY_BAD) {
  167. printk("%s: attempt to bring up CPU %u failed\n",
  168. __FUNCTION__, cpu);
  169. ret = -EINVAL;
  170. goto out_notify;
  171. }
  172. /* Arch-specific enabling code. */
  173. ret = __cpu_up(cpu);
  174. if (ret != 0)
  175. goto out_notify;
  176. if (!cpu_online(cpu))
  177. BUG();
  178. /* Now call notifier in preparation. */
  179. notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
  180. out_notify:
  181. if (ret != 0)
  182. notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
  183. out:
  184. current->flags &= ~PF_HOTPLUG_CPU;
  185. up(&cpucontrol);
  186. return ret;
  187. }