cpu.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /* Ensure this CPU doesn't handle any more interrupts. */
  58. err = __cpu_disable();
  59. if (err < 0)
  60. return err;
  61. /* Force idle task to run as soon as we yield: it should
  62. immediately notice cpu is offline and die quickly. */
  63. sched_idle_next();
  64. return 0;
  65. }
  66. int cpu_down(unsigned int cpu)
  67. {
  68. int err;
  69. struct task_struct *p;
  70. cpumask_t old_allowed, tmp;
  71. if ((err = lock_cpu_hotplug_interruptible()) != 0)
  72. return err;
  73. if (num_online_cpus() == 1) {
  74. err = -EBUSY;
  75. goto out;
  76. }
  77. if (!cpu_online(cpu)) {
  78. err = -EINVAL;
  79. goto out;
  80. }
  81. err = notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
  82. (void *)(long)cpu);
  83. if (err == NOTIFY_BAD) {
  84. printk("%s: attempt to take down CPU %u failed\n",
  85. __FUNCTION__, cpu);
  86. err = -EINVAL;
  87. goto out;
  88. }
  89. /* Ensure that we are not runnable on dying cpu */
  90. old_allowed = current->cpus_allowed;
  91. tmp = CPU_MASK_ALL;
  92. cpu_clear(cpu, tmp);
  93. set_cpus_allowed(current, tmp);
  94. p = __stop_machine_run(take_cpu_down, NULL, cpu);
  95. if (IS_ERR(p)) {
  96. /* CPU didn't die: tell everyone. Can't complain. */
  97. if (notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
  98. (void *)(long)cpu) == NOTIFY_BAD)
  99. BUG();
  100. err = PTR_ERR(p);
  101. goto out_allowed;
  102. }
  103. if (cpu_online(cpu))
  104. goto out_thread;
  105. /* Wait for it to sleep (leaving idle task). */
  106. while (!idle_cpu(cpu))
  107. yield();
  108. /* This actually kills the CPU. */
  109. __cpu_die(cpu);
  110. /* Move it here so it can run. */
  111. kthread_bind(p, get_cpu());
  112. put_cpu();
  113. /* CPU is completely dead: tell everyone. Too late to complain. */
  114. if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
  115. == NOTIFY_BAD)
  116. BUG();
  117. check_for_tasks(cpu);
  118. out_thread:
  119. err = kthread_stop(p);
  120. out_allowed:
  121. set_cpus_allowed(current, old_allowed);
  122. out:
  123. unlock_cpu_hotplug();
  124. return err;
  125. }
  126. #endif /*CONFIG_HOTPLUG_CPU*/
  127. int __devinit cpu_up(unsigned int cpu)
  128. {
  129. int ret;
  130. void *hcpu = (void *)(long)cpu;
  131. if ((ret = down_interruptible(&cpucontrol)) != 0)
  132. return ret;
  133. if (cpu_online(cpu) || !cpu_present(cpu)) {
  134. ret = -EINVAL;
  135. goto out;
  136. }
  137. ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
  138. if (ret == NOTIFY_BAD) {
  139. printk("%s: attempt to bring up CPU %u failed\n",
  140. __FUNCTION__, cpu);
  141. ret = -EINVAL;
  142. goto out_notify;
  143. }
  144. /* Arch-specific enabling code. */
  145. ret = __cpu_up(cpu);
  146. if (ret != 0)
  147. goto out_notify;
  148. if (!cpu_online(cpu))
  149. BUG();
  150. /* Now call notifier in preparation. */
  151. notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
  152. out_notify:
  153. if (ret != 0)
  154. notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
  155. out:
  156. up(&cpucontrol);
  157. return ret;
  158. }