haltpoll.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * haltpoll.c - haltpoll idle governor
  4. *
  5. * Copyright 2019 Red Hat, Inc. and/or its affiliates.
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2. See
  8. * the COPYING file in the top-level directory.
  9. *
  10. * Authors: Marcelo Tosatti <mtosatti@redhat.com>
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/cpuidle.h>
  14. #include <linux/time.h>
  15. #include <linux/ktime.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/tick.h>
  18. #include <linux/sched.h>
  19. #include <linux/module.h>
  20. #include <linux/kvm_para.h>
  21. #include <trace/events/power.h>
  22. static unsigned int guest_halt_poll_ns __read_mostly = 200000;
  23. module_param(guest_halt_poll_ns, uint, 0644);
  24. /* division factor to shrink halt_poll_ns */
  25. static unsigned int guest_halt_poll_shrink __read_mostly = 2;
  26. module_param(guest_halt_poll_shrink, uint, 0644);
  27. /* multiplication factor to grow per-cpu poll_limit_ns */
  28. static unsigned int guest_halt_poll_grow __read_mostly = 2;
  29. module_param(guest_halt_poll_grow, uint, 0644);
  30. /* value in us to start growing per-cpu halt_poll_ns */
  31. static unsigned int guest_halt_poll_grow_start __read_mostly = 50000;
  32. module_param(guest_halt_poll_grow_start, uint, 0644);
  33. /* allow shrinking guest halt poll */
  34. static bool guest_halt_poll_allow_shrink __read_mostly = true;
  35. module_param(guest_halt_poll_allow_shrink, bool, 0644);
  36. /**
  37. * haltpoll_select - selects the next idle state to enter
  38. * @drv: cpuidle driver containing state data
  39. * @dev: the CPU
  40. * @stop_tick: indication on whether or not to stop the tick
  41. */
  42. static int haltpoll_select(struct cpuidle_driver *drv,
  43. struct cpuidle_device *dev,
  44. bool *stop_tick)
  45. {
  46. if (cpuidle_governor_latency_req(dev->cpu) == 0) {
  47. *stop_tick = false;
  48. return 0;
  49. }
  50. if (dev->poll_limit_ns == 0)
  51. return 1;
  52. /* Last state was poll? */
  53. if (dev->last_state_idx == 0) {
  54. /* Halt if no event occurred on poll window */
  55. if (dev->poll_time_limit == true)
  56. return 1;
  57. *stop_tick = false;
  58. /* Otherwise, poll again */
  59. return 0;
  60. }
  61. *stop_tick = false;
  62. /* Last state was halt: poll */
  63. return 0;
  64. }
  65. static void adjust_poll_limit(struct cpuidle_device *dev, u64 block_ns)
  66. {
  67. unsigned int val;
  68. /* Grow cpu_halt_poll_us if
  69. * cpu_halt_poll_us < block_ns < guest_halt_poll_us
  70. */
  71. if (block_ns > dev->poll_limit_ns && block_ns <= guest_halt_poll_ns) {
  72. val = dev->poll_limit_ns * guest_halt_poll_grow;
  73. if (val < guest_halt_poll_grow_start)
  74. val = guest_halt_poll_grow_start;
  75. if (val > guest_halt_poll_ns)
  76. val = guest_halt_poll_ns;
  77. trace_guest_halt_poll_ns_grow(val, dev->poll_limit_ns);
  78. dev->poll_limit_ns = val;
  79. } else if (block_ns > guest_halt_poll_ns &&
  80. guest_halt_poll_allow_shrink) {
  81. unsigned int shrink = guest_halt_poll_shrink;
  82. val = dev->poll_limit_ns;
  83. if (shrink == 0) {
  84. val = 0;
  85. } else {
  86. val /= shrink;
  87. /* Reset value to 0 if shrunk below grow_start */
  88. if (val < guest_halt_poll_grow_start)
  89. val = 0;
  90. }
  91. trace_guest_halt_poll_ns_shrink(val, dev->poll_limit_ns);
  92. dev->poll_limit_ns = val;
  93. }
  94. }
  95. /**
  96. * haltpoll_reflect - update variables and update poll time
  97. * @dev: the CPU
  98. * @index: the index of actual entered state
  99. */
  100. static void haltpoll_reflect(struct cpuidle_device *dev, int index)
  101. {
  102. dev->last_state_idx = index;
  103. if (index != 0)
  104. adjust_poll_limit(dev, dev->last_residency_ns);
  105. }
  106. /**
  107. * haltpoll_enable_device - scans a CPU's states and does setup
  108. * @drv: cpuidle driver
  109. * @dev: the CPU
  110. */
  111. static int haltpoll_enable_device(struct cpuidle_driver *drv,
  112. struct cpuidle_device *dev)
  113. {
  114. dev->poll_limit_ns = 0;
  115. return 0;
  116. }
  117. static struct cpuidle_governor haltpoll_governor = {
  118. .name = "haltpoll",
  119. .rating = 9,
  120. .enable = haltpoll_enable_device,
  121. .select = haltpoll_select,
  122. .reflect = haltpoll_reflect,
  123. };
  124. static int __init init_haltpoll(void)
  125. {
  126. if (kvm_para_available())
  127. return cpuidle_register_governor(&haltpoll_governor);
  128. return 0;
  129. }
  130. postcore_initcall(init_haltpoll);