1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
5 * This module enables kernel and guest-mode vCPU access to guest physical
6 * memory with suitable invalidation mechanisms.
8 * Copyright © 2021 Amazon.com, Inc. or its affiliates.
14 #include <linux/kvm_host.h>
15 #include <linux/kvm.h>
16 #include <linux/highmem.h>
17 #include <linux/module.h>
18 #include <linux/errno.h>
23 * MMU notifier 'invalidate_range_start' hook.
25 void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start,
26 unsigned long end, bool may_block)
28 DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
29 struct gfn_to_pfn_cache *gpc;
30 bool evict_vcpus = false;
32 spin_lock(&kvm->gpc_lock);
33 list_for_each_entry(gpc, &kvm->gpc_list, list) {
34 write_lock_irq(&gpc->lock);
36 /* Only a single page so no need to care about length */
37 if (gpc->valid && !is_error_noslot_pfn(gpc->pfn) &&
38 gpc->uhva >= start && gpc->uhva < end) {
42 * If a guest vCPU could be using the physical address,
43 * it needs to be forced out of guest mode.
45 if (gpc->usage & KVM_GUEST_USES_PFN) {
48 bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS);
50 __set_bit(gpc->vcpu->vcpu_idx, vcpu_bitmap);
53 write_unlock_irq(&gpc->lock);
55 spin_unlock(&kvm->gpc_lock);
59 * KVM needs to ensure the vCPU is fully out of guest context
60 * before allowing the invalidation to continue.
62 unsigned int req = KVM_REQ_OUTSIDE_GUEST_MODE;
66 * If the OOM reaper is active, then all vCPUs should have
67 * been stopped already, so perform the request without
68 * KVM_REQUEST_WAIT and be sad if any needed to be IPI'd.
71 req &= ~KVM_REQUEST_WAIT;
73 called = kvm_make_vcpus_request_mask(kvm, req, vcpu_bitmap);
75 WARN_ON_ONCE(called && !may_block);
79 bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len)
81 struct kvm_memslots *slots = kvm_memslots(gpc->kvm);
86 if ((gpc->gpa & ~PAGE_MASK) + len > PAGE_SIZE)
89 if (gpc->generation != slots->generation || kvm_is_error_hva(gpc->uhva))
97 EXPORT_SYMBOL_GPL(kvm_gpc_check);
99 static void gpc_unmap_khva(kvm_pfn_t pfn, void *khva)
101 /* Unmap the old pfn/page if it was mapped before. */
102 if (!is_error_noslot_pfn(pfn) && khva) {
104 kunmap(pfn_to_page(pfn));
105 #ifdef CONFIG_HAS_IOMEM
112 static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_seq)
115 * mn_active_invalidate_count acts for all intents and purposes
116 * like mmu_invalidate_in_progress here; but the latter cannot
117 * be used here because the invalidation of caches in the
118 * mmu_notifier event occurs _before_ mmu_invalidate_in_progress
121 * Note, it does not matter that mn_active_invalidate_count
122 * is not protected by gpc->lock. It is guaranteed to
123 * be elevated before the mmu_notifier acquires gpc->lock, and
124 * isn't dropped until after mmu_invalidate_seq is updated.
126 if (kvm->mn_active_invalidate_count)
130 * Ensure mn_active_invalidate_count is read before
131 * mmu_invalidate_seq. This pairs with the smp_wmb() in
132 * mmu_notifier_invalidate_range_end() to guarantee either the
133 * old (non-zero) value of mn_active_invalidate_count or the
134 * new (incremented) value of mmu_invalidate_seq is observed.
137 return kvm->mmu_invalidate_seq != mmu_seq;
140 static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc)
142 /* Note, the new page offset may be different than the old! */
143 void *old_khva = gpc->khva - offset_in_page(gpc->khva);
144 kvm_pfn_t new_pfn = KVM_PFN_ERR_FAULT;
145 void *new_khva = NULL;
146 unsigned long mmu_seq;
148 lockdep_assert_held(&gpc->refresh_lock);
150 lockdep_assert_held_write(&gpc->lock);
153 * Invalidate the cache prior to dropping gpc->lock, the gpa=>uhva
154 * assets have already been updated and so a concurrent check() from a
155 * different task may not fail the gpa/uhva/generation checks.
160 mmu_seq = gpc->kvm->mmu_invalidate_seq;
163 write_unlock_irq(&gpc->lock);
166 * If the previous iteration "failed" due to an mmu_notifier
167 * event, release the pfn and unmap the kernel virtual address
168 * from the previous attempt. Unmapping might sleep, so this
169 * needs to be done after dropping the lock. Opportunistically
170 * check for resched while the lock isn't held.
172 if (new_pfn != KVM_PFN_ERR_FAULT) {
174 * Keep the mapping if the previous iteration reused
175 * the existing mapping and didn't create a new one.
177 if (new_khva != old_khva)
178 gpc_unmap_khva(new_pfn, new_khva);
180 kvm_release_pfn_clean(new_pfn);
185 /* We always request a writeable mapping */
186 new_pfn = hva_to_pfn(gpc->uhva, false, false, NULL, true, NULL);
187 if (is_error_noslot_pfn(new_pfn))
191 * Obtain a new kernel mapping if KVM itself will access the
192 * pfn. Note, kmap() and memremap() can both sleep, so this
193 * too must be done outside of gpc->lock!
195 if (gpc->usage & KVM_HOST_USES_PFN) {
196 if (new_pfn == gpc->pfn) {
198 } else if (pfn_valid(new_pfn)) {
199 new_khva = kmap(pfn_to_page(new_pfn));
200 #ifdef CONFIG_HAS_IOMEM
202 new_khva = memremap(pfn_to_hpa(new_pfn), PAGE_SIZE, MEMREMAP_WB);
206 kvm_release_pfn_clean(new_pfn);
211 write_lock_irq(&gpc->lock);
214 * Other tasks must wait for _this_ refresh to complete before
215 * attempting to refresh.
217 WARN_ON_ONCE(gpc->valid);
218 } while (mmu_notifier_retry_cache(gpc->kvm, mmu_seq));
222 gpc->khva = new_khva + (gpc->gpa & ~PAGE_MASK);
225 * Put the reference to the _new_ pfn. The pfn is now tracked by the
226 * cache and can be safely migrated, swapped, etc... as the cache will
227 * invalidate any mappings in response to relevant mmu_notifier events.
229 kvm_release_pfn_clean(new_pfn);
234 write_lock_irq(&gpc->lock);
239 static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa,
242 struct kvm_memslots *slots = kvm_memslots(gpc->kvm);
243 unsigned long page_offset = gpa & ~PAGE_MASK;
244 bool unmap_old = false;
245 unsigned long old_uhva;
251 * If must fit within a single page. The 'len' argument is
252 * only to enforce that.
254 if (page_offset + len > PAGE_SIZE)
258 * If another task is refreshing the cache, wait for it to complete.
259 * There is no guarantee that concurrent refreshes will see the same
260 * gpa, memslots generation, etc..., so they must be fully serialized.
262 mutex_lock(&gpc->refresh_lock);
264 write_lock_irq(&gpc->lock);
272 old_khva = gpc->khva - offset_in_page(gpc->khva);
273 old_uhva = gpc->uhva;
275 /* If the userspace HVA is invalid, refresh that first */
276 if (gpc->gpa != gpa || gpc->generation != slots->generation ||
277 kvm_is_error_hva(gpc->uhva)) {
278 gfn_t gfn = gpa_to_gfn(gpa);
281 gpc->generation = slots->generation;
282 gpc->memslot = __gfn_to_memslot(slots, gfn);
283 gpc->uhva = gfn_to_hva_memslot(gpc->memslot, gfn);
285 if (kvm_is_error_hva(gpc->uhva)) {
292 * If the userspace HVA changed or the PFN was already invalid,
293 * drop the lock and do the HVA to PFN lookup again.
295 if (!gpc->valid || old_uhva != gpc->uhva) {
296 ret = hva_to_pfn_retry(gpc);
299 * If the HVA→PFN mapping was already valid, don't unmap it.
300 * But do update gpc->khva because the offset within the page
303 gpc->khva = old_khva + page_offset;
310 * Invalidate the cache and purge the pfn/khva if the refresh failed.
311 * Some/all of the uhva, gpa, and memslot generation info may still be
312 * valid, leave it as is.
316 gpc->pfn = KVM_PFN_ERR_FAULT;
320 /* Detect a pfn change before dropping the lock! */
321 unmap_old = (old_pfn != gpc->pfn);
324 write_unlock_irq(&gpc->lock);
326 mutex_unlock(&gpc->refresh_lock);
329 gpc_unmap_khva(old_pfn, old_khva);
334 int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsigned long len)
336 return __kvm_gpc_refresh(gpc, gpc->gpa, len);
338 EXPORT_SYMBOL_GPL(kvm_gpc_refresh);
340 void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm,
341 struct kvm_vcpu *vcpu, enum pfn_cache_usage usage)
343 WARN_ON_ONCE(!usage || (usage & KVM_GUEST_AND_HOST_USE_PFN) != usage);
344 WARN_ON_ONCE((usage & KVM_GUEST_USES_PFN) && !vcpu);
346 rwlock_init(&gpc->lock);
347 mutex_init(&gpc->refresh_lock);
352 gpc->pfn = KVM_PFN_ERR_FAULT;
353 gpc->uhva = KVM_HVA_ERR_BAD;
355 EXPORT_SYMBOL_GPL(kvm_gpc_init);
357 int kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned long len)
359 struct kvm *kvm = gpc->kvm;
362 if (KVM_BUG_ON(gpc->valid, kvm))
365 spin_lock(&kvm->gpc_lock);
366 list_add(&gpc->list, &kvm->gpc_list);
367 spin_unlock(&kvm->gpc_lock);
370 * Activate the cache after adding it to the list, a concurrent
371 * refresh must not establish a mapping until the cache is
372 * reachable by mmu_notifier events.
374 write_lock_irq(&gpc->lock);
376 write_unlock_irq(&gpc->lock);
378 return __kvm_gpc_refresh(gpc, gpa, len);
380 EXPORT_SYMBOL_GPL(kvm_gpc_activate);
382 void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc)
384 struct kvm *kvm = gpc->kvm;
390 * Deactivate the cache before removing it from the list, KVM
391 * must stall mmu_notifier events until all users go away, i.e.
392 * until gpc->lock is dropped and refresh is guaranteed to fail.
394 write_lock_irq(&gpc->lock);
399 * Leave the GPA => uHVA cache intact, it's protected by the
400 * memslot generation. The PFN lookup needs to be redone every
401 * time as mmu_notifier protection is lost when the cache is
402 * removed from the VM's gpc_list.
404 old_khva = gpc->khva - offset_in_page(gpc->khva);
408 gpc->pfn = KVM_PFN_ERR_FAULT;
409 write_unlock_irq(&gpc->lock);
411 spin_lock(&kvm->gpc_lock);
412 list_del(&gpc->list);
413 spin_unlock(&kvm->gpc_lock);
415 gpc_unmap_khva(old_pfn, old_khva);
418 EXPORT_SYMBOL_GPL(kvm_gpc_deactivate);