process.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  93. {
  94. if (force_mwait)
  95. return 1;
  96. /* Any C1 states supported? */
  97. return c->cpuid_level >= 5 && ((cpuid_edx(5) >> 4) & 0xf) > 0;
  98. }
  99. /*
  100. * On SMP it's slightly faster (but much more power-consuming!)
  101. * to poll the ->work.need_resched flag instead of waiting for the
  102. * cross-CPU IPI to arrive. Use this option with caution.
  103. */
  104. static void poll_idle(void)
  105. {
  106. local_irq_enable();
  107. cpu_relax();
  108. }
  109. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  110. {
  111. static int selected;
  112. if (selected)
  113. return;
  114. #ifdef CONFIG_X86_SMP
  115. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  116. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  117. " performance may degrade.\n");
  118. }
  119. #endif
  120. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  121. /*
  122. * Skip, if setup has overridden idle.
  123. * One CPU supports mwait => All CPUs supports mwait
  124. */
  125. if (!pm_idle) {
  126. printk(KERN_INFO "using mwait in idle threads.\n");
  127. pm_idle = mwait_idle;
  128. }
  129. }
  130. selected = 1;
  131. }
  132. static int __init idle_setup(char *str)
  133. {
  134. if (!strcmp(str, "poll")) {
  135. printk("using polling idle threads.\n");
  136. pm_idle = poll_idle;
  137. } else if (!strcmp(str, "mwait"))
  138. force_mwait = 1;
  139. else
  140. return -1;
  141. boot_option_idle_override = 1;
  142. return 0;
  143. }
  144. early_param("idle", idle_setup);