pfncache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kernel-based Virtual Machine driver for Linux
  4. *
  5. * This module enables kernel and guest-mode vCPU access to guest physical
  6. * memory with suitable invalidation mechanisms.
  7. *
  8. * Copyright © 2021 Amazon.com, Inc. or its affiliates.
  9. *
  10. * Authors:
  11. * David Woodhouse <[email protected]>
  12. */
  13. #include <linux/kvm_host.h>
  14. #include <linux/kvm.h>
  15. #include <linux/highmem.h>
  16. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include "kvm_mm.h"
  19. /*
  20. * MMU notifier 'invalidate_range_start' hook.
  21. */
  22. void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start,
  23. unsigned long end, bool may_block)
  24. {
  25. DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
  26. struct gfn_to_pfn_cache *gpc;
  27. bool evict_vcpus = false;
  28. spin_lock(&kvm->gpc_lock);
  29. list_for_each_entry(gpc, &kvm->gpc_list, list) {
  30. write_lock_irq(&gpc->lock);
  31. /* Only a single page so no need to care about length */
  32. if (gpc->valid && !is_error_noslot_pfn(gpc->pfn) &&
  33. gpc->uhva >= start && gpc->uhva < end) {
  34. gpc->valid = false;
  35. /*
  36. * If a guest vCPU could be using the physical address,
  37. * it needs to be forced out of guest mode.
  38. */
  39. if (gpc->usage & KVM_GUEST_USES_PFN) {
  40. if (!evict_vcpus) {
  41. evict_vcpus = true;
  42. bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS);
  43. }
  44. __set_bit(gpc->vcpu->vcpu_idx, vcpu_bitmap);
  45. }
  46. }
  47. write_unlock_irq(&gpc->lock);
  48. }
  49. spin_unlock(&kvm->gpc_lock);
  50. if (evict_vcpus) {
  51. /*
  52. * KVM needs to ensure the vCPU is fully out of guest context
  53. * before allowing the invalidation to continue.
  54. */
  55. unsigned int req = KVM_REQ_OUTSIDE_GUEST_MODE;
  56. bool called;
  57. /*
  58. * If the OOM reaper is active, then all vCPUs should have
  59. * been stopped already, so perform the request without
  60. * KVM_REQUEST_WAIT and be sad if any needed to be IPI'd.
  61. */
  62. if (!may_block)
  63. req &= ~KVM_REQUEST_WAIT;
  64. called = kvm_make_vcpus_request_mask(kvm, req, vcpu_bitmap);
  65. WARN_ON_ONCE(called && !may_block);
  66. }
  67. }
  68. bool kvm_gfn_to_pfn_cache_check(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
  69. gpa_t gpa, unsigned long len)
  70. {
  71. struct kvm_memslots *slots = kvm_memslots(kvm);
  72. if (!gpc->active)
  73. return false;
  74. if ((gpa & ~PAGE_MASK) + len > PAGE_SIZE)
  75. return false;
  76. if (gpc->gpa != gpa || gpc->generation != slots->generation ||
  77. kvm_is_error_hva(gpc->uhva))
  78. return false;
  79. if (!gpc->valid)
  80. return false;
  81. return true;
  82. }
  83. EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_check);
  84. static void gpc_unmap_khva(struct kvm *kvm, kvm_pfn_t pfn, void *khva)
  85. {
  86. /* Unmap the old pfn/page if it was mapped before. */
  87. if (!is_error_noslot_pfn(pfn) && khva) {
  88. if (pfn_valid(pfn))
  89. kunmap(pfn_to_page(pfn));
  90. #ifdef CONFIG_HAS_IOMEM
  91. else
  92. memunmap(khva);
  93. #endif
  94. }
  95. }
  96. static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_seq)
  97. {
  98. /*
  99. * mn_active_invalidate_count acts for all intents and purposes
  100. * like mmu_invalidate_in_progress here; but the latter cannot
  101. * be used here because the invalidation of caches in the
  102. * mmu_notifier event occurs _before_ mmu_invalidate_in_progress
  103. * is elevated.
  104. *
  105. * Note, it does not matter that mn_active_invalidate_count
  106. * is not protected by gpc->lock. It is guaranteed to
  107. * be elevated before the mmu_notifier acquires gpc->lock, and
  108. * isn't dropped until after mmu_invalidate_seq is updated.
  109. */
  110. if (kvm->mn_active_invalidate_count)
  111. return true;
  112. /*
  113. * Ensure mn_active_invalidate_count is read before
  114. * mmu_invalidate_seq. This pairs with the smp_wmb() in
  115. * mmu_notifier_invalidate_range_end() to guarantee either the
  116. * old (non-zero) value of mn_active_invalidate_count or the
  117. * new (incremented) value of mmu_invalidate_seq is observed.
  118. */
  119. smp_rmb();
  120. return kvm->mmu_invalidate_seq != mmu_seq;
  121. }
  122. static kvm_pfn_t hva_to_pfn_retry(struct kvm *kvm, struct gfn_to_pfn_cache *gpc)
  123. {
  124. /* Note, the new page offset may be different than the old! */
  125. void *old_khva = gpc->khva - offset_in_page(gpc->khva);
  126. kvm_pfn_t new_pfn = KVM_PFN_ERR_FAULT;
  127. void *new_khva = NULL;
  128. unsigned long mmu_seq;
  129. lockdep_assert_held(&gpc->refresh_lock);
  130. lockdep_assert_held_write(&gpc->lock);
  131. /*
  132. * Invalidate the cache prior to dropping gpc->lock, the gpa=>uhva
  133. * assets have already been updated and so a concurrent check() from a
  134. * different task may not fail the gpa/uhva/generation checks.
  135. */
  136. gpc->valid = false;
  137. do {
  138. mmu_seq = kvm->mmu_invalidate_seq;
  139. smp_rmb();
  140. write_unlock_irq(&gpc->lock);
  141. /*
  142. * If the previous iteration "failed" due to an mmu_notifier
  143. * event, release the pfn and unmap the kernel virtual address
  144. * from the previous attempt. Unmapping might sleep, so this
  145. * needs to be done after dropping the lock. Opportunistically
  146. * check for resched while the lock isn't held.
  147. */
  148. if (new_pfn != KVM_PFN_ERR_FAULT) {
  149. /*
  150. * Keep the mapping if the previous iteration reused
  151. * the existing mapping and didn't create a new one.
  152. */
  153. if (new_khva != old_khva)
  154. gpc_unmap_khva(kvm, new_pfn, new_khva);
  155. kvm_release_pfn_clean(new_pfn);
  156. cond_resched();
  157. }
  158. /* We always request a writeable mapping */
  159. new_pfn = hva_to_pfn(gpc->uhva, false, NULL, true, NULL);
  160. if (is_error_noslot_pfn(new_pfn))
  161. goto out_error;
  162. /*
  163. * Obtain a new kernel mapping if KVM itself will access the
  164. * pfn. Note, kmap() and memremap() can both sleep, so this
  165. * too must be done outside of gpc->lock!
  166. */
  167. if (gpc->usage & KVM_HOST_USES_PFN) {
  168. if (new_pfn == gpc->pfn) {
  169. new_khva = old_khva;
  170. } else if (pfn_valid(new_pfn)) {
  171. new_khva = kmap(pfn_to_page(new_pfn));
  172. #ifdef CONFIG_HAS_IOMEM
  173. } else {
  174. new_khva = memremap(pfn_to_hpa(new_pfn), PAGE_SIZE, MEMREMAP_WB);
  175. #endif
  176. }
  177. if (!new_khva) {
  178. kvm_release_pfn_clean(new_pfn);
  179. goto out_error;
  180. }
  181. }
  182. write_lock_irq(&gpc->lock);
  183. /*
  184. * Other tasks must wait for _this_ refresh to complete before
  185. * attempting to refresh.
  186. */
  187. WARN_ON_ONCE(gpc->valid);
  188. } while (mmu_notifier_retry_cache(kvm, mmu_seq));
  189. gpc->valid = true;
  190. gpc->pfn = new_pfn;
  191. gpc->khva = new_khva + (gpc->gpa & ~PAGE_MASK);
  192. /*
  193. * Put the reference to the _new_ pfn. The pfn is now tracked by the
  194. * cache and can be safely migrated, swapped, etc... as the cache will
  195. * invalidate any mappings in response to relevant mmu_notifier events.
  196. */
  197. kvm_release_pfn_clean(new_pfn);
  198. return 0;
  199. out_error:
  200. write_lock_irq(&gpc->lock);
  201. return -EFAULT;
  202. }
  203. int kvm_gfn_to_pfn_cache_refresh(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
  204. gpa_t gpa, unsigned long len)
  205. {
  206. struct kvm_memslots *slots = kvm_memslots(kvm);
  207. unsigned long page_offset = gpa & ~PAGE_MASK;
  208. bool unmap_old = false;
  209. unsigned long old_uhva;
  210. kvm_pfn_t old_pfn;
  211. void *old_khva;
  212. int ret;
  213. /*
  214. * If must fit within a single page. The 'len' argument is
  215. * only to enforce that.
  216. */
  217. if (page_offset + len > PAGE_SIZE)
  218. return -EINVAL;
  219. /*
  220. * If another task is refreshing the cache, wait for it to complete.
  221. * There is no guarantee that concurrent refreshes will see the same
  222. * gpa, memslots generation, etc..., so they must be fully serialized.
  223. */
  224. mutex_lock(&gpc->refresh_lock);
  225. write_lock_irq(&gpc->lock);
  226. if (!gpc->active) {
  227. ret = -EINVAL;
  228. goto out_unlock;
  229. }
  230. old_pfn = gpc->pfn;
  231. old_khva = gpc->khva - offset_in_page(gpc->khva);
  232. old_uhva = gpc->uhva;
  233. /* If the userspace HVA is invalid, refresh that first */
  234. if (gpc->gpa != gpa || gpc->generation != slots->generation ||
  235. kvm_is_error_hva(gpc->uhva)) {
  236. gfn_t gfn = gpa_to_gfn(gpa);
  237. gpc->gpa = gpa;
  238. gpc->generation = slots->generation;
  239. gpc->memslot = __gfn_to_memslot(slots, gfn);
  240. gpc->uhva = gfn_to_hva_memslot(gpc->memslot, gfn);
  241. if (kvm_is_error_hva(gpc->uhva)) {
  242. ret = -EFAULT;
  243. goto out;
  244. }
  245. }
  246. /*
  247. * If the userspace HVA changed or the PFN was already invalid,
  248. * drop the lock and do the HVA to PFN lookup again.
  249. */
  250. if (!gpc->valid || old_uhva != gpc->uhva) {
  251. ret = hva_to_pfn_retry(kvm, gpc);
  252. } else {
  253. /*
  254. * If the HVA→PFN mapping was already valid, don't unmap it.
  255. * But do update gpc->khva because the offset within the page
  256. * may have changed.
  257. */
  258. gpc->khva = old_khva + page_offset;
  259. old_pfn = KVM_PFN_ERR_FAULT;
  260. old_khva = NULL;
  261. ret = 0;
  262. }
  263. out:
  264. /*
  265. * Invalidate the cache and purge the pfn/khva if the refresh failed.
  266. * Some/all of the uhva, gpa, and memslot generation info may still be
  267. * valid, leave it as is.
  268. */
  269. if (ret) {
  270. gpc->valid = false;
  271. gpc->pfn = KVM_PFN_ERR_FAULT;
  272. gpc->khva = NULL;
  273. }
  274. /* Detect a pfn change before dropping the lock! */
  275. unmap_old = (old_pfn != gpc->pfn);
  276. out_unlock:
  277. write_unlock_irq(&gpc->lock);
  278. mutex_unlock(&gpc->refresh_lock);
  279. if (unmap_old)
  280. gpc_unmap_khva(kvm, old_pfn, old_khva);
  281. return ret;
  282. }
  283. EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_refresh);
  284. void kvm_gfn_to_pfn_cache_unmap(struct kvm *kvm, struct gfn_to_pfn_cache *gpc)
  285. {
  286. void *old_khva;
  287. kvm_pfn_t old_pfn;
  288. mutex_lock(&gpc->refresh_lock);
  289. write_lock_irq(&gpc->lock);
  290. gpc->valid = false;
  291. old_khva = gpc->khva - offset_in_page(gpc->khva);
  292. old_pfn = gpc->pfn;
  293. /*
  294. * We can leave the GPA → uHVA map cache intact but the PFN
  295. * lookup will need to be redone even for the same page.
  296. */
  297. gpc->khva = NULL;
  298. gpc->pfn = KVM_PFN_ERR_FAULT;
  299. write_unlock_irq(&gpc->lock);
  300. mutex_unlock(&gpc->refresh_lock);
  301. gpc_unmap_khva(kvm, old_pfn, old_khva);
  302. }
  303. EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_unmap);
  304. void kvm_gpc_init(struct gfn_to_pfn_cache *gpc)
  305. {
  306. rwlock_init(&gpc->lock);
  307. mutex_init(&gpc->refresh_lock);
  308. }
  309. EXPORT_SYMBOL_GPL(kvm_gpc_init);
  310. int kvm_gpc_activate(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
  311. struct kvm_vcpu *vcpu, enum pfn_cache_usage usage,
  312. gpa_t gpa, unsigned long len)
  313. {
  314. WARN_ON_ONCE(!usage || (usage & KVM_GUEST_AND_HOST_USE_PFN) != usage);
  315. if (!gpc->active) {
  316. gpc->khva = NULL;
  317. gpc->pfn = KVM_PFN_ERR_FAULT;
  318. gpc->uhva = KVM_HVA_ERR_BAD;
  319. gpc->vcpu = vcpu;
  320. gpc->usage = usage;
  321. gpc->valid = false;
  322. spin_lock(&kvm->gpc_lock);
  323. list_add(&gpc->list, &kvm->gpc_list);
  324. spin_unlock(&kvm->gpc_lock);
  325. /*
  326. * Activate the cache after adding it to the list, a concurrent
  327. * refresh must not establish a mapping until the cache is
  328. * reachable by mmu_notifier events.
  329. */
  330. write_lock_irq(&gpc->lock);
  331. gpc->active = true;
  332. write_unlock_irq(&gpc->lock);
  333. }
  334. return kvm_gfn_to_pfn_cache_refresh(kvm, gpc, gpa, len);
  335. }
  336. EXPORT_SYMBOL_GPL(kvm_gpc_activate);
  337. void kvm_gpc_deactivate(struct kvm *kvm, struct gfn_to_pfn_cache *gpc)
  338. {
  339. if (gpc->active) {
  340. /*
  341. * Deactivate the cache before removing it from the list, KVM
  342. * must stall mmu_notifier events until all users go away, i.e.
  343. * until gpc->lock is dropped and refresh is guaranteed to fail.
  344. */
  345. write_lock_irq(&gpc->lock);
  346. gpc->active = false;
  347. write_unlock_irq(&gpc->lock);
  348. spin_lock(&kvm->gpc_lock);
  349. list_del(&gpc->list);
  350. spin_unlock(&kvm->gpc_lock);
  351. kvm_gfn_to_pfn_cache_unmap(kvm, gpc);
  352. }
  353. }
  354. EXPORT_SYMBOL_GPL(kvm_gpc_deactivate);