vcpu-requests.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. =================
  2. KVM VCPU Requests
  3. =================
  4. Overview
  5. ========
  6. KVM supports an internal API enabling threads to request a VCPU thread to
  7. perform some activity. For example, a thread may request a VCPU to flush
  8. its TLB with a VCPU request. The API consists of the following functions::
  9. /* Check if any requests are pending for VCPU @vcpu. */
  10. bool kvm_request_pending(struct kvm_vcpu *vcpu);
  11. /* Check if VCPU @vcpu has request @req pending. */
  12. bool kvm_test_request(int req, struct kvm_vcpu *vcpu);
  13. /* Clear request @req for VCPU @vcpu. */
  14. void kvm_clear_request(int req, struct kvm_vcpu *vcpu);
  15. /*
  16. * Check if VCPU @vcpu has request @req pending. When the request is
  17. * pending it will be cleared and a memory barrier, which pairs with
  18. * another in kvm_make_request(), will be issued.
  19. */
  20. bool kvm_check_request(int req, struct kvm_vcpu *vcpu);
  21. /*
  22. * Make request @req of VCPU @vcpu. Issues a memory barrier, which pairs
  23. * with another in kvm_check_request(), prior to setting the request.
  24. */
  25. void kvm_make_request(int req, struct kvm_vcpu *vcpu);
  26. /* Make request @req of all VCPUs of the VM with struct kvm @kvm. */
  27. bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req);
  28. Typically a requester wants the VCPU to perform the activity as soon
  29. as possible after making the request. This means most requests
  30. (kvm_make_request() calls) are followed by a call to kvm_vcpu_kick(),
  31. and kvm_make_all_cpus_request() has the kicking of all VCPUs built
  32. into it.
  33. VCPU Kicks
  34. ----------
  35. The goal of a VCPU kick is to bring a VCPU thread out of guest mode in
  36. order to perform some KVM maintenance. To do so, an IPI is sent, forcing
  37. a guest mode exit. However, a VCPU thread may not be in guest mode at the
  38. time of the kick. Therefore, depending on the mode and state of the VCPU
  39. thread, there are two other actions a kick may take. All three actions
  40. are listed below:
  41. 1) Send an IPI. This forces a guest mode exit.
  42. 2) Waking a sleeping VCPU. Sleeping VCPUs are VCPU threads outside guest
  43. mode that wait on waitqueues. Waking them removes the threads from
  44. the waitqueues, allowing the threads to run again. This behavior
  45. may be suppressed, see KVM_REQUEST_NO_WAKEUP below.
  46. 3) Nothing. When the VCPU is not in guest mode and the VCPU thread is not
  47. sleeping, then there is nothing to do.
  48. VCPU Mode
  49. ---------
  50. VCPUs have a mode state, ``vcpu->mode``, that is used to track whether the
  51. guest is running in guest mode or not, as well as some specific
  52. outside guest mode states. The architecture may use ``vcpu->mode`` to
  53. ensure VCPU requests are seen by VCPUs (see "Ensuring Requests Are Seen"),
  54. as well as to avoid sending unnecessary IPIs (see "IPI Reduction"), and
  55. even to ensure IPI acknowledgements are waited upon (see "Waiting for
  56. Acknowledgements"). The following modes are defined:
  57. OUTSIDE_GUEST_MODE
  58. The VCPU thread is outside guest mode.
  59. IN_GUEST_MODE
  60. The VCPU thread is in guest mode.
  61. EXITING_GUEST_MODE
  62. The VCPU thread is transitioning from IN_GUEST_MODE to
  63. OUTSIDE_GUEST_MODE.
  64. READING_SHADOW_PAGE_TABLES
  65. The VCPU thread is outside guest mode, but it wants the sender of
  66. certain VCPU requests, namely KVM_REQ_TLB_FLUSH, to wait until the VCPU
  67. thread is done reading the page tables.
  68. VCPU Request Internals
  69. ======================
  70. VCPU requests are simply bit indices of the ``vcpu->requests`` bitmap.
  71. This means general bitops, like those documented in [atomic-ops]_ could
  72. also be used, e.g. ::
  73. clear_bit(KVM_REQ_UNHALT & KVM_REQUEST_MASK, &vcpu->requests);
  74. However, VCPU request users should refrain from doing so, as it would
  75. break the abstraction. The first 8 bits are reserved for architecture
  76. independent requests, all additional bits are available for architecture
  77. dependent requests.
  78. Architecture Independent Requests
  79. ---------------------------------
  80. KVM_REQ_TLB_FLUSH
  81. KVM's common MMU notifier may need to flush all of a guest's TLB
  82. entries, calling kvm_flush_remote_tlbs() to do so. Architectures that
  83. choose to use the common kvm_flush_remote_tlbs() implementation will
  84. need to handle this VCPU request.
  85. KVM_REQ_MMU_RELOAD
  86. When shadow page tables are used and memory slots are removed it's
  87. necessary to inform each VCPU to completely refresh the tables. This
  88. request is used for that.
  89. KVM_REQ_PENDING_TIMER
  90. This request may be made from a timer handler run on the host on behalf
  91. of a VCPU. It informs the VCPU thread to inject a timer interrupt.
  92. KVM_REQ_UNHALT
  93. This request may be made from the KVM common function kvm_vcpu_block(),
  94. which is used to emulate an instruction that causes a CPU to halt until
  95. one of an architectural specific set of events and/or interrupts is
  96. received (determined by checking kvm_arch_vcpu_runnable()). When that
  97. event or interrupt arrives kvm_vcpu_block() makes the request. This is
  98. in contrast to when kvm_vcpu_block() returns due to any other reason,
  99. such as a pending signal, which does not indicate the VCPU's halt
  100. emulation should stop, and therefore does not make the request.
  101. KVM_REQUEST_MASK
  102. ----------------
  103. VCPU requests should be masked by KVM_REQUEST_MASK before using them with
  104. bitops. This is because only the lower 8 bits are used to represent the
  105. request's number. The upper bits are used as flags. Currently only two
  106. flags are defined.
  107. VCPU Request Flags
  108. ------------------
  109. KVM_REQUEST_NO_WAKEUP
  110. This flag is applied to requests that only need immediate attention
  111. from VCPUs running in guest mode. That is, sleeping VCPUs do not need
  112. to be awaken for these requests. Sleeping VCPUs will handle the
  113. requests when they are awaken later for some other reason.
  114. KVM_REQUEST_WAIT
  115. When requests with this flag are made with kvm_make_all_cpus_request(),
  116. then the caller will wait for each VCPU to acknowledge its IPI before
  117. proceeding. This flag only applies to VCPUs that would receive IPIs.
  118. If, for example, the VCPU is sleeping, so no IPI is necessary, then
  119. the requesting thread does not wait. This means that this flag may be
  120. safely combined with KVM_REQUEST_NO_WAKEUP. See "Waiting for
  121. Acknowledgements" for more information about requests with
  122. KVM_REQUEST_WAIT.
  123. VCPU Requests with Associated State
  124. ===================================
  125. Requesters that want the receiving VCPU to handle new state need to ensure
  126. the newly written state is observable to the receiving VCPU thread's CPU
  127. by the time it observes the request. This means a write memory barrier
  128. must be inserted after writing the new state and before setting the VCPU
  129. request bit. Additionally, on the receiving VCPU thread's side, a
  130. corresponding read barrier must be inserted after reading the request bit
  131. and before proceeding to read the new state associated with it. See
  132. scenario 3, Message and Flag, of [lwn-mb]_ and the kernel documentation
  133. [memory-barriers]_.
  134. The pair of functions, kvm_check_request() and kvm_make_request(), provide
  135. the memory barriers, allowing this requirement to be handled internally by
  136. the API.
  137. Ensuring Requests Are Seen
  138. ==========================
  139. When making requests to VCPUs, we want to avoid the receiving VCPU
  140. executing in guest mode for an arbitrary long time without handling the
  141. request. We can be sure this won't happen as long as we ensure the VCPU
  142. thread checks kvm_request_pending() before entering guest mode and that a
  143. kick will send an IPI to force an exit from guest mode when necessary.
  144. Extra care must be taken to cover the period after the VCPU thread's last
  145. kvm_request_pending() check and before it has entered guest mode, as kick
  146. IPIs will only trigger guest mode exits for VCPU threads that are in guest
  147. mode or at least have already disabled interrupts in order to prepare to
  148. enter guest mode. This means that an optimized implementation (see "IPI
  149. Reduction") must be certain when it's safe to not send the IPI. One
  150. solution, which all architectures except s390 apply, is to:
  151. - set ``vcpu->mode`` to IN_GUEST_MODE between disabling the interrupts and
  152. the last kvm_request_pending() check;
  153. - enable interrupts atomically when entering the guest.
  154. This solution also requires memory barriers to be placed carefully in both
  155. the requesting thread and the receiving VCPU. With the memory barriers we
  156. can exclude the possibility of a VCPU thread observing
  157. !kvm_request_pending() on its last check and then not receiving an IPI for
  158. the next request made of it, even if the request is made immediately after
  159. the check. This is done by way of the Dekker memory barrier pattern
  160. (scenario 10 of [lwn-mb]_). As the Dekker pattern requires two variables,
  161. this solution pairs ``vcpu->mode`` with ``vcpu->requests``. Substituting
  162. them into the pattern gives::
  163. CPU1 CPU2
  164. ================= =================
  165. local_irq_disable();
  166. WRITE_ONCE(vcpu->mode, IN_GUEST_MODE); kvm_make_request(REQ, vcpu);
  167. smp_mb(); smp_mb();
  168. if (kvm_request_pending(vcpu)) { if (READ_ONCE(vcpu->mode) ==
  169. IN_GUEST_MODE) {
  170. ...abort guest entry... ...send IPI...
  171. } }
  172. As stated above, the IPI is only useful for VCPU threads in guest mode or
  173. that have already disabled interrupts. This is why this specific case of
  174. the Dekker pattern has been extended to disable interrupts before setting
  175. ``vcpu->mode`` to IN_GUEST_MODE. WRITE_ONCE() and READ_ONCE() are used to
  176. pedantically implement the memory barrier pattern, guaranteeing the
  177. compiler doesn't interfere with ``vcpu->mode``'s carefully planned
  178. accesses.
  179. IPI Reduction
  180. -------------
  181. As only one IPI is needed to get a VCPU to check for any/all requests,
  182. then they may be coalesced. This is easily done by having the first IPI
  183. sending kick also change the VCPU mode to something !IN_GUEST_MODE. The
  184. transitional state, EXITING_GUEST_MODE, is used for this purpose.
  185. Waiting for Acknowledgements
  186. ----------------------------
  187. Some requests, those with the KVM_REQUEST_WAIT flag set, require IPIs to
  188. be sent, and the acknowledgements to be waited upon, even when the target
  189. VCPU threads are in modes other than IN_GUEST_MODE. For example, one case
  190. is when a target VCPU thread is in READING_SHADOW_PAGE_TABLES mode, which
  191. is set after disabling interrupts. To support these cases, the
  192. KVM_REQUEST_WAIT flag changes the condition for sending an IPI from
  193. checking that the VCPU is IN_GUEST_MODE to checking that it is not
  194. OUTSIDE_GUEST_MODE.
  195. Request-less VCPU Kicks
  196. -----------------------
  197. As the determination of whether or not to send an IPI depends on the
  198. two-variable Dekker memory barrier pattern, then it's clear that
  199. request-less VCPU kicks are almost never correct. Without the assurance
  200. that a non-IPI generating kick will still result in an action by the
  201. receiving VCPU, as the final kvm_request_pending() check does for
  202. request-accompanying kicks, then the kick may not do anything useful at
  203. all. If, for instance, a request-less kick was made to a VCPU that was
  204. just about to set its mode to IN_GUEST_MODE, meaning no IPI is sent, then
  205. the VCPU thread may continue its entry without actually having done
  206. whatever it was the kick was meant to initiate.
  207. One exception is x86's posted interrupt mechanism. In this case, however,
  208. even the request-less VCPU kick is coupled with the same
  209. local_irq_disable() + smp_mb() pattern described above; the ON bit
  210. (Outstanding Notification) in the posted interrupt descriptor takes the
  211. role of ``vcpu->requests``. When sending a posted interrupt, PIR.ON is
  212. set before reading ``vcpu->mode``; dually, in the VCPU thread,
  213. vmx_sync_pir_to_irr() reads PIR after setting ``vcpu->mode`` to
  214. IN_GUEST_MODE.
  215. Additional Considerations
  216. =========================
  217. Sleeping VCPUs
  218. --------------
  219. VCPU threads may need to consider requests before and/or after calling
  220. functions that may put them to sleep, e.g. kvm_vcpu_block(). Whether they
  221. do or not, and, if they do, which requests need consideration, is
  222. architecture dependent. kvm_vcpu_block() calls kvm_arch_vcpu_runnable()
  223. to check if it should awaken. One reason to do so is to provide
  224. architectures a function where requests may be checked if necessary.
  225. Clearing Requests
  226. -----------------
  227. Generally it only makes sense for the receiving VCPU thread to clear a
  228. request. However, in some circumstances, such as when the requesting
  229. thread and the receiving VCPU thread are executed serially, such as when
  230. they are the same thread, or when they are using some form of concurrency
  231. control to temporarily execute synchronously, then it's possible to know
  232. that the request may be cleared immediately, rather than waiting for the
  233. receiving VCPU thread to handle the request in VCPU RUN. The only current
  234. examples of this are kvm_vcpu_block() calls made by VCPUs to block
  235. themselves. A possible side-effect of that call is to make the
  236. KVM_REQ_UNHALT request, which may then be cleared immediately when the
  237. VCPU returns from the call.
  238. References
  239. ==========
  240. .. [atomic-ops] Documentation/core-api/atomic_ops.rst
  241. .. [memory-barriers] Documentation/memory-barriers.txt
  242. .. [lwn-mb] https://lwn.net/Articles/573436/