dirty_ring.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * KVM dirty ring implementation
  4. *
  5. * Copyright 2019 Red Hat, Inc.
  6. */
  7. #include <linux/kvm_host.h>
  8. #include <linux/kvm.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/kvm_dirty_ring.h>
  11. #include <trace/events/kvm.h>
  12. #include "kvm_mm.h"
  13. int __weak kvm_cpu_dirty_log_size(struct kvm *kvm)
  14. {
  15. return 0;
  16. }
  17. u32 kvm_dirty_ring_get_rsvd_entries(struct kvm *kvm)
  18. {
  19. return KVM_DIRTY_RING_RSVD_ENTRIES + kvm_cpu_dirty_log_size(kvm);
  20. }
  21. bool kvm_use_dirty_bitmap(struct kvm *kvm)
  22. {
  23. lockdep_assert_held(&kvm->slots_lock);
  24. return !kvm->dirty_ring_size || kvm->dirty_ring_with_bitmap;
  25. }
  26. #ifndef CONFIG_NEED_KVM_DIRTY_RING_WITH_BITMAP
  27. bool kvm_arch_allow_write_without_running_vcpu(struct kvm *kvm)
  28. {
  29. return false;
  30. }
  31. #endif
  32. static u32 kvm_dirty_ring_used(struct kvm_dirty_ring *ring)
  33. {
  34. return READ_ONCE(ring->dirty_index) - READ_ONCE(ring->reset_index);
  35. }
  36. static bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
  37. {
  38. return kvm_dirty_ring_used(ring) >= ring->soft_limit;
  39. }
  40. static bool kvm_dirty_ring_full(struct kvm_dirty_ring *ring)
  41. {
  42. return kvm_dirty_ring_used(ring) >= ring->size;
  43. }
  44. static void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot, u64 offset, u64 mask)
  45. {
  46. struct kvm_memory_slot *memslot;
  47. int as_id, id;
  48. as_id = slot >> 16;
  49. id = (u16)slot;
  50. if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS)
  51. return;
  52. memslot = id_to_memslot(__kvm_memslots(kvm, as_id), id);
  53. if (!memslot || (offset + __fls(mask)) >= memslot->npages)
  54. return;
  55. KVM_MMU_LOCK(kvm);
  56. kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, offset, mask);
  57. KVM_MMU_UNLOCK(kvm);
  58. }
  59. int kvm_dirty_ring_alloc(struct kvm *kvm, struct kvm_dirty_ring *ring,
  60. int index, u32 size)
  61. {
  62. ring->dirty_gfns = vzalloc(size);
  63. if (!ring->dirty_gfns)
  64. return -ENOMEM;
  65. ring->size = size / sizeof(struct kvm_dirty_gfn);
  66. ring->soft_limit = ring->size - kvm_dirty_ring_get_rsvd_entries(kvm);
  67. ring->dirty_index = 0;
  68. ring->reset_index = 0;
  69. ring->index = index;
  70. return 0;
  71. }
  72. static inline void kvm_dirty_gfn_set_invalid(struct kvm_dirty_gfn *gfn)
  73. {
  74. smp_store_release(&gfn->flags, 0);
  75. }
  76. static inline void kvm_dirty_gfn_set_dirtied(struct kvm_dirty_gfn *gfn)
  77. {
  78. gfn->flags = KVM_DIRTY_GFN_F_DIRTY;
  79. }
  80. static inline bool kvm_dirty_gfn_harvested(struct kvm_dirty_gfn *gfn)
  81. {
  82. return smp_load_acquire(&gfn->flags) & KVM_DIRTY_GFN_F_RESET;
  83. }
  84. int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
  85. int *nr_entries_reset)
  86. {
  87. /*
  88. * To minimize mmu_lock contention, batch resets for harvested entries
  89. * whose gfns are in the same slot, and are within N frame numbers of
  90. * each other, where N is the number of bits in an unsigned long. For
  91. * simplicity, process the current set of entries when the next entry
  92. * can't be included in the batch.
  93. *
  94. * Track the current batch slot, the gfn offset into the slot for the
  95. * batch, and the bitmask of gfns that need to be reset (relative to
  96. * offset). Note, the offset may be adjusted backwards, e.g. so that
  97. * a sequence of gfns X, X-1, ... X-N-1 can be batched.
  98. */
  99. u32 cur_slot, next_slot;
  100. u64 cur_offset, next_offset;
  101. unsigned long mask = 0;
  102. struct kvm_dirty_gfn *entry;
  103. /*
  104. * Ensure concurrent calls to KVM_RESET_DIRTY_RINGS are serialized,
  105. * e.g. so that KVM fully resets all entries processed by a given call
  106. * before returning to userspace. Holding slots_lock also protects
  107. * the various memslot accesses.
  108. */
  109. lockdep_assert_held(&kvm->slots_lock);
  110. while (likely((*nr_entries_reset) < INT_MAX)) {
  111. if (signal_pending(current))
  112. return -EINTR;
  113. entry = &ring->dirty_gfns[ring->reset_index & (ring->size - 1)];
  114. if (!kvm_dirty_gfn_harvested(entry))
  115. break;
  116. next_slot = READ_ONCE(entry->slot);
  117. next_offset = READ_ONCE(entry->offset);
  118. /* Update the flags to reflect that this GFN is reset */
  119. kvm_dirty_gfn_set_invalid(entry);
  120. ring->reset_index++;
  121. (*nr_entries_reset)++;
  122. if (mask) {
  123. /*
  124. * While the size of each ring is fixed, it's possible
  125. * for the ring to be constantly re-dirtied/harvested
  126. * while the reset is in-progress (the hard limit exists
  127. * only to guard against the count becoming negative).
  128. */
  129. cond_resched();
  130. /*
  131. * Try to coalesce the reset operations when the guest
  132. * is scanning pages in the same slot.
  133. */
  134. if (next_slot == cur_slot) {
  135. s64 delta = next_offset - cur_offset;
  136. if (delta >= 0 && delta < BITS_PER_LONG) {
  137. mask |= 1ull << delta;
  138. continue;
  139. }
  140. /* Backwards visit, careful about overflows! */
  141. if (delta > -BITS_PER_LONG && delta < 0 &&
  142. (mask << -delta >> -delta) == mask) {
  143. cur_offset = next_offset;
  144. mask = (mask << -delta) | 1;
  145. continue;
  146. }
  147. }
  148. /*
  149. * Reset the slot for all the harvested entries that
  150. * have been gathered, but not yet fully processed.
  151. */
  152. kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
  153. }
  154. /*
  155. * The current slot was reset or this is the first harvested
  156. * entry, (re)initialize the metadata.
  157. */
  158. cur_slot = next_slot;
  159. cur_offset = next_offset;
  160. mask = 1;
  161. }
  162. /*
  163. * Perform a final reset if there are harvested entries that haven't
  164. * been processed, which is guaranteed if at least one harvested was
  165. * found. The loop only performs a reset when the "next" entry can't
  166. * be batched with the "current" entry(s), and that reset processes the
  167. * _current_ entry(s); i.e. the last harvested entry, a.k.a. next, will
  168. * always be left pending.
  169. */
  170. if (mask)
  171. kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
  172. /*
  173. * The request KVM_REQ_DIRTY_RING_SOFT_FULL will be cleared
  174. * by the VCPU thread next time when it enters the guest.
  175. */
  176. trace_kvm_dirty_ring_reset(ring);
  177. return 0;
  178. }
  179. void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset)
  180. {
  181. struct kvm_dirty_ring *ring = &vcpu->dirty_ring;
  182. struct kvm_dirty_gfn *entry;
  183. /* It should never get full */
  184. WARN_ON_ONCE(kvm_dirty_ring_full(ring));
  185. entry = &ring->dirty_gfns[ring->dirty_index & (ring->size - 1)];
  186. entry->slot = slot;
  187. entry->offset = offset;
  188. /*
  189. * Make sure the data is filled in before we publish this to
  190. * the userspace program. There's no paired kernel-side reader.
  191. */
  192. smp_wmb();
  193. kvm_dirty_gfn_set_dirtied(entry);
  194. ring->dirty_index++;
  195. trace_kvm_dirty_ring_push(ring, slot, offset);
  196. if (kvm_dirty_ring_soft_full(ring))
  197. kvm_make_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu);
  198. }
  199. bool kvm_dirty_ring_check_request(struct kvm_vcpu *vcpu)
  200. {
  201. /*
  202. * The VCPU isn't runnable when the dirty ring becomes soft full.
  203. * The KVM_REQ_DIRTY_RING_SOFT_FULL event is always set to prevent
  204. * the VCPU from running until the dirty pages are harvested and
  205. * the dirty ring is reset by userspace.
  206. */
  207. if (kvm_check_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu) &&
  208. kvm_dirty_ring_soft_full(&vcpu->dirty_ring)) {
  209. kvm_make_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu);
  210. vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL;
  211. trace_kvm_dirty_ring_exit(vcpu);
  212. return true;
  213. }
  214. return false;
  215. }
  216. struct page *kvm_dirty_ring_get_page(struct kvm_dirty_ring *ring, u32 offset)
  217. {
  218. return vmalloc_to_page((void *)ring->dirty_gfns + offset * PAGE_SIZE);
  219. }
  220. void kvm_dirty_ring_free(struct kvm_dirty_ring *ring)
  221. {
  222. vfree(ring->dirty_gfns);
  223. ring->dirty_gfns = NULL;
  224. }