halt-polling.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===========================
  3. The KVM halt polling system
  4. ===========================
  5. The KVM halt polling system provides a feature within KVM whereby the latency
  6. of a guest can, under some circumstances, be reduced by polling in the host
  7. for some time period after the guest has elected to no longer run by cedeing.
  8. That is, when a guest vcpu has ceded, or in the case of powerpc when all of the
  9. vcpus of a single vcore have ceded, the host kernel polls for wakeup conditions
  10. before giving up the cpu to the scheduler in order to let something else run.
  11. Polling provides a latency advantage in cases where the guest can be run again
  12. very quickly by at least saving us a trip through the scheduler, normally on
  13. the order of a few micro-seconds, although performance benefits are workload
  14. dependant. In the event that no wakeup source arrives during the polling
  15. interval or some other task on the runqueue is runnable the scheduler is
  16. invoked. Thus halt polling is especially useful on workloads with very short
  17. wakeup periods where the time spent halt polling is minimised and the time
  18. savings of not invoking the scheduler are distinguishable.
  19. The generic halt polling code is implemented in:
  20. virt/kvm/kvm_main.c: kvm_vcpu_block()
  21. The powerpc kvm-hv specific case is implemented in:
  22. arch/powerpc/kvm/book3s_hv.c: kvmppc_vcore_blocked()
  23. Halt Polling Interval
  24. =====================
  25. The maximum time for which to poll before invoking the scheduler, referred to
  26. as the halt polling interval, is increased and decreased based on the perceived
  27. effectiveness of the polling in an attempt to limit pointless polling.
  28. This value is stored in either the vcpu struct:
  29. kvm_vcpu->halt_poll_ns
  30. or in the case of powerpc kvm-hv, in the vcore struct:
  31. kvmppc_vcore->halt_poll_ns
  32. Thus this is a per vcpu (or vcore) value.
  33. During polling if a wakeup source is received within the halt polling interval,
  34. the interval is left unchanged. In the event that a wakeup source isn't
  35. received during the polling interval (and thus schedule is invoked) there are
  36. two options, either the polling interval and total block time[0] were less than
  37. the global max polling interval (see module params below), or the total block
  38. time was greater than the global max polling interval.
  39. In the event that both the polling interval and total block time were less than
  40. the global max polling interval then the polling interval can be increased in
  41. the hope that next time during the longer polling interval the wake up source
  42. will be received while the host is polling and the latency benefits will be
  43. received. The polling interval is grown in the function grow_halt_poll_ns() and
  44. is multiplied by the module parameters halt_poll_ns_grow and
  45. halt_poll_ns_grow_start.
  46. In the event that the total block time was greater than the global max polling
  47. interval then the host will never poll for long enough (limited by the global
  48. max) to wakeup during the polling interval so it may as well be shrunk in order
  49. to avoid pointless polling. The polling interval is shrunk in the function
  50. shrink_halt_poll_ns() and is divided by the module parameter
  51. halt_poll_ns_shrink, or set to 0 iff halt_poll_ns_shrink == 0.
  52. It is worth noting that this adjustment process attempts to hone in on some
  53. steady state polling interval but will only really do a good job for wakeups
  54. which come at an approximately constant rate, otherwise there will be constant
  55. adjustment of the polling interval.
  56. [0] total block time:
  57. the time between when the halt polling function is
  58. invoked and a wakeup source received (irrespective of
  59. whether the scheduler is invoked within that function).
  60. Module Parameters
  61. =================
  62. The kvm module has 3 tuneable module parameters to adjust the global max
  63. polling interval as well as the rate at which the polling interval is grown and
  64. shrunk. These variables are defined in include/linux/kvm_host.h and as module
  65. parameters in virt/kvm/kvm_main.c, or arch/powerpc/kvm/book3s_hv.c in the
  66. powerpc kvm-hv case.
  67. +-----------------------+---------------------------+-------------------------+
  68. |Module Parameter | Description | Default Value |
  69. +-----------------------+---------------------------+-------------------------+
  70. |halt_poll_ns | The global max polling | KVM_HALT_POLL_NS_DEFAULT|
  71. | | interval which defines | |
  72. | | the ceiling value of the | |
  73. | | polling interval for | (per arch value) |
  74. | | each vcpu. | |
  75. +-----------------------+---------------------------+-------------------------+
  76. |halt_poll_ns_grow | The value by which the | 2 |
  77. | | halt polling interval is | |
  78. | | multiplied in the | |
  79. | | grow_halt_poll_ns() | |
  80. | | function. | |
  81. +-----------------------+---------------------------+-------------------------+
  82. |halt_poll_ns_grow_start| The initial value to grow | 10000 |
  83. | | to from zero in the | |
  84. | | grow_halt_poll_ns() | |
  85. | | function. | |
  86. +-----------------------+---------------------------+-------------------------+
  87. |halt_poll_ns_shrink | The value by which the | 0 |
  88. | | halt polling interval is | |
  89. | | divided in the | |
  90. | | shrink_halt_poll_ns() | |
  91. | | function. | |
  92. +-----------------------+---------------------------+-------------------------+
  93. These module parameters can be set from the debugfs files in:
  94. /sys/module/kvm/parameters/
  95. Note: that these module parameters are system wide values and are not able to
  96. be tuned on a per vm basis.
  97. Any changes to these parameters will be picked up by new and existing vCPUs the
  98. next time they halt, with the notable exception of VMs using KVM_CAP_HALT_POLL
  99. (see next section).
  100. KVM_CAP_HALT_POLL
  101. =================
  102. KVM_CAP_HALT_POLL is a VM capability that allows userspace to override halt_poll_ns
  103. on a per-VM basis. VMs using KVM_CAP_HALT_POLL ignore halt_poll_ns completely (but
  104. still obey halt_poll_ns_grow, halt_poll_ns_grow_start, and halt_poll_ns_shrink).
  105. See Documentation/virt/kvm/api.rst for more information on this capability.
  106. Further Notes
  107. =============
  108. - Care should be taken when setting the halt_poll_ns module parameter as a large value
  109. has the potential to drive the cpu usage to 100% on a machine which would be almost
  110. entirely idle otherwise. This is because even if a guest has wakeups during which very
  111. little work is done and which are quite far apart, if the period is shorter than the
  112. global max polling interval (halt_poll_ns) then the host will always poll for the
  113. entire block time and thus cpu utilisation will go to 100%.
  114. - Halt polling essentially presents a trade off between power usage and latency and
  115. the module parameters should be used to tune the affinity for this. Idle cpu time is
  116. essentially converted to host kernel time with the aim of decreasing latency when
  117. entering the guest.
  118. - Halt polling will only be conducted by the host when no other tasks are runnable on
  119. that cpu, otherwise the polling will cease immediately and schedule will be invoked to
  120. allow that other task to run. Thus this doesn't allow a guest to denial of service the
  121. cpu.