process.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  103. {
  104. static int selected;
  105. if (selected)
  106. return;
  107. #ifdef CONFIG_X86_SMP
  108. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  109. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  110. " performance may degrade.\n");
  111. }
  112. #endif
  113. if (cpu_has(c, X86_FEATURE_MWAIT)) {
  114. /*
  115. * Skip, if setup has overridden idle.
  116. * One CPU supports mwait => All CPUs supports mwait
  117. */
  118. if (!pm_idle) {
  119. printk(KERN_INFO "using mwait in idle threads.\n");
  120. pm_idle = mwait_idle;
  121. }
  122. }
  123. selected = 1;
  124. }
  125. static int __init idle_setup(char *str)
  126. {
  127. if (!strcmp(str, "poll")) {
  128. printk("using polling idle threads.\n");
  129. pm_idle = poll_idle;
  130. } else if (!strcmp(str, "mwait"))
  131. force_mwait = 1;
  132. else
  133. return -1;
  134. boot_option_idle_override = 1;
  135. return 0;
  136. }
  137. early_param("idle", idle_setup);