process.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/mm.h>
  4. #include <linux/smp.h>
  5. #include <linux/slab.h>
  6. #include <linux/sched.h>
  7. #include <linux/module.h>
  8. #include <linux/pm.h>
  9. struct kmem_cache *task_xstate_cachep;
  10. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  11. {
  12. *dst = *src;
  13. if (src->thread.xstate) {
  14. dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
  15. GFP_KERNEL);
  16. if (!dst->thread.xstate)
  17. return -ENOMEM;
  18. WARN_ON((unsigned long)dst->thread.xstate & 15);
  19. memcpy(dst->thread.xstate, src->thread.xstate, xstate_size);
  20. }
  21. return 0;
  22. }
  23. void free_thread_xstate(struct task_struct *tsk)
  24. {
  25. if (tsk->thread.xstate) {
  26. kmem_cache_free(task_xstate_cachep, tsk->thread.xstate);
  27. tsk->thread.xstate = NULL;
  28. }
  29. }
  30. void free_thread_info(struct thread_info *ti)
  31. {
  32. free_thread_xstate(ti->task);
  33. free_pages((unsigned long)ti, get_order(THREAD_SIZE));
  34. }
  35. void arch_task_cache_init(void)
  36. {
  37. task_xstate_cachep =
  38. kmem_cache_create("task_xstate", xstate_size,
  39. __alignof__(union thread_xstate),
  40. SLAB_PANIC, NULL);
  41. }
  42. static void do_nothing(void *unused)
  43. {
  44. }
  45. /*
  46. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  47. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  48. * handler on SMP systems.
  49. *
  50. * Caller must have changed pm_idle to the new value before the call. Old
  51. * pm_idle value will not be used by any CPU after the return of this function.
  52. */
  53. void cpu_idle_wait(void)
  54. {
  55. smp_mb();
  56. /* kick all the CPUs so that they exit out of pm_idle */
  57. smp_call_function(do_nothing, NULL, 0, 1);
  58. }
  59. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  60. /*
  61. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  62. * which can obviate IPI to trigger checking of need_resched.
  63. * We execute MONITOR against need_resched and enter optimized wait state
  64. * through MWAIT. Whenever someone changes need_resched, we would be woken
  65. * up from MWAIT (without an IPI).
  66. *
  67. * New with Core Duo processors, MWAIT can take some hints based on CPU
  68. * capability.
  69. */
  70. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  71. {
  72. if (!need_resched()) {
  73. __monitor((void *)&current_thread_info()->flags, 0, 0);
  74. smp_mb();
  75. if (!need_resched())
  76. __mwait(ax, cx);
  77. }
  78. }
  79. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  80. static void mwait_idle(void)
  81. {
  82. if (!need_resched()) {
  83. __monitor((void *)&current_thread_info()->flags, 0, 0);
  84. smp_mb();
  85. if (!need_resched())
  86. __sti_mwait(0, 0);
  87. else
  88. local_irq_enable();
  89. } else
  90. local_irq_enable();
  91. }
  92. /*
  93. * On SMP it's slightly faster (but much more power-consuming!)
  94. * to poll the ->work.need_resched flag instead of waiting for the
  95. * cross-CPU IPI to arrive. Use this option with caution.
  96. */
  97. static void poll_idle(void)
  98. {
  99. local_irq_enable();
  100. cpu_relax();
  101. }
  102. /*
  103. * mwait selection logic:
  104. *
  105. * It depends on the CPU. For AMD CPUs that support MWAIT this is
  106. * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
  107. * then depend on a clock divisor and current Pstate of the core. If
  108. * all cores of a processor are in halt state (C1) the processor can
  109. * enter the C1E (C1 enhanced) state. If mwait is used this will never
  110. * happen.
  111. *
  112. * idle=mwait overrides this decision and forces the usage of mwait.
  113. */
  114. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  115. {
  116. if (force_mwait)
  117. return 1;
  118. if (c->x86_vendor == X86_VENDOR_AMD) {
  119. switch(c->x86) {
  120. case 0x10:
  121. case 0x11:
  122. return 0;
  123. }
  124. }
  125. return 1;
  126. }
  127. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  128. {
  129. static int selected;
  130. if (selected)
  131. return;
  132. #ifdef CONFIG_X86_SMP
  133. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  134. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  135. " performance may degrade.\n");
  136. }
  137. #endif
  138. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  139. /*
  140. * Skip, if setup has overridden idle.
  141. * One CPU supports mwait => All CPUs supports mwait
  142. */
  143. if (!pm_idle) {
  144. printk(KERN_INFO "using mwait in idle threads.\n");
  145. pm_idle = mwait_idle;
  146. }
  147. }
  148. selected = 1;
  149. }
  150. static int __init idle_setup(char *str)
  151. {
  152. if (!strcmp(str, "poll")) {
  153. printk("using polling idle threads.\n");
  154. pm_idle = poll_idle;
  155. } else if (!strcmp(str, "mwait"))
  156. force_mwait = 1;
  157. else
  158. return -1;
  159. boot_option_idle_override = 1;
  160. return 0;
  161. }
  162. early_param("idle", idle_setup);