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.
11 * David Woodhouse <dwmw2@infradead.org>
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);
54 * We cannot call mark_page_dirty() from here because
55 * this physical CPU might not have an active vCPU
56 * with which to do the KVM dirty tracking.
58 * Neither is there any point in telling the kernel MM
59 * that the underlying page is dirty. A vCPU in guest
60 * mode might still be writing to it up to the point
61 * where we wake them a few lines further down anyway.
63 * So all the dirty marking happens on the unmap.
66 write_unlock_irq(&gpc->lock);
68 spin_unlock(&kvm->gpc_lock);
72 * KVM needs to ensure the vCPU is fully out of guest context
73 * before allowing the invalidation to continue.
75 unsigned int req = KVM_REQ_OUTSIDE_GUEST_MODE;
79 * If the OOM reaper is active, then all vCPUs should have
80 * been stopped already, so perform the request without
81 * KVM_REQUEST_WAIT and be sad if any needed to be IPI'd.
84 req &= ~KVM_REQUEST_WAIT;
86 called = kvm_make_vcpus_request_mask(kvm, req, vcpu_bitmap);
88 WARN_ON_ONCE(called && !may_block);
92 bool kvm_gfn_to_pfn_cache_check(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
93 gpa_t gpa, unsigned long len)
95 struct kvm_memslots *slots = kvm_memslots(kvm);
97 if ((gpa & ~PAGE_MASK) + len > PAGE_SIZE)
100 if (gpc->gpa != gpa || gpc->generation != slots->generation ||
101 kvm_is_error_hva(gpc->uhva))
109 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_check);
111 static void __release_gpc(struct kvm *kvm, kvm_pfn_t pfn, void *khva,
112 gpa_t gpa, bool dirty)
114 /* Unmap the old page if it was mapped before, and release it */
115 if (!is_error_noslot_pfn(pfn)) {
118 kunmap(pfn_to_page(pfn));
119 #ifdef CONFIG_HAS_IOMEM
125 kvm_release_pfn(pfn, dirty);
127 mark_page_dirty(kvm, gpa);
131 static kvm_pfn_t hva_to_pfn_retry(struct kvm *kvm, unsigned long uhva)
133 unsigned long mmu_seq;
138 mmu_seq = kvm->mmu_notifier_seq;
141 /* We always request a writeable mapping */
142 new_pfn = hva_to_pfn(uhva, false, NULL, true, NULL);
143 if (is_error_noslot_pfn(new_pfn))
146 KVM_MMU_READ_LOCK(kvm);
147 retry = mmu_notifier_retry_hva(kvm, mmu_seq, uhva);
148 KVM_MMU_READ_UNLOCK(kvm);
158 int kvm_gfn_to_pfn_cache_refresh(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
159 gpa_t gpa, unsigned long len, bool dirty)
161 struct kvm_memslots *slots = kvm_memslots(kvm);
162 unsigned long page_offset = gpa & ~PAGE_MASK;
163 kvm_pfn_t old_pfn, new_pfn;
164 unsigned long old_uhva;
167 bool old_valid, old_dirty;
171 * If must fit within a single page. The 'len' argument is
172 * only to enforce that.
174 if (page_offset + len > PAGE_SIZE)
177 write_lock_irq(&gpc->lock);
181 old_khva = gpc->khva - offset_in_page(gpc->khva);
182 old_uhva = gpc->uhva;
183 old_valid = gpc->valid;
184 old_dirty = gpc->dirty;
186 /* If the userspace HVA is invalid, refresh that first */
187 if (gpc->gpa != gpa || gpc->generation != slots->generation ||
188 kvm_is_error_hva(gpc->uhva)) {
189 gfn_t gfn = gpa_to_gfn(gpa);
193 gpc->generation = slots->generation;
194 gpc->memslot = __gfn_to_memslot(slots, gfn);
195 gpc->uhva = gfn_to_hva_memslot(gpc->memslot, gfn);
197 if (kvm_is_error_hva(gpc->uhva)) {
198 gpc->pfn = KVM_PFN_ERR_FAULT;
203 gpc->uhva += page_offset;
207 * If the userspace HVA changed or the PFN was already invalid,
208 * drop the lock and do the HVA to PFN lookup again.
210 if (!old_valid || old_uhva != gpc->uhva) {
211 unsigned long uhva = gpc->uhva;
212 void *new_khva = NULL;
214 /* Placeholders for "hva is valid but not yet mapped" */
215 gpc->pfn = KVM_PFN_ERR_FAULT;
219 write_unlock_irq(&gpc->lock);
221 new_pfn = hva_to_pfn_retry(kvm, uhva);
222 if (is_error_noslot_pfn(new_pfn)) {
227 if (gpc->usage & KVM_HOST_USES_PFN) {
228 if (new_pfn == old_pfn) {
230 old_pfn = KVM_PFN_ERR_FAULT;
232 } else if (pfn_valid(new_pfn)) {
233 new_khva = kmap(pfn_to_page(new_pfn));
234 #ifdef CONFIG_HAS_IOMEM
236 new_khva = memremap(pfn_to_hpa(new_pfn), PAGE_SIZE, MEMREMAP_WB);
240 new_khva += page_offset;
246 write_lock_irq(&gpc->lock);
249 gpc->pfn = KVM_PFN_ERR_FAULT;
252 /* At this point, gpc->valid may already have been cleared */
254 gpc->khva = new_khva;
257 /* If the HVA→PFN mapping was already valid, don't unmap it. */
258 old_pfn = KVM_PFN_ERR_FAULT;
268 write_unlock_irq(&gpc->lock);
270 __release_gpc(kvm, old_pfn, old_khva, old_gpa, old_dirty);
274 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_refresh);
276 void kvm_gfn_to_pfn_cache_unmap(struct kvm *kvm, struct gfn_to_pfn_cache *gpc)
283 write_lock_irq(&gpc->lock);
287 old_khva = gpc->khva - offset_in_page(gpc->khva);
288 old_dirty = gpc->dirty;
293 * We can leave the GPA → uHVA map cache intact but the PFN
294 * lookup will need to be redone even for the same page.
297 gpc->pfn = KVM_PFN_ERR_FAULT;
299 write_unlock_irq(&gpc->lock);
301 __release_gpc(kvm, old_pfn, old_khva, old_gpa, old_dirty);
303 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_unmap);
306 int kvm_gfn_to_pfn_cache_init(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
307 struct kvm_vcpu *vcpu, enum pfn_cache_usage usage,
308 gpa_t gpa, unsigned long len, bool dirty)
310 WARN_ON_ONCE(!usage || (usage & KVM_GUEST_AND_HOST_USE_PFN) != usage);
313 rwlock_init(&gpc->lock);
316 gpc->pfn = KVM_PFN_ERR_FAULT;
317 gpc->uhva = KVM_HVA_ERR_BAD;
323 spin_lock(&kvm->gpc_lock);
324 list_add(&gpc->list, &kvm->gpc_list);
325 spin_unlock(&kvm->gpc_lock);
327 return kvm_gfn_to_pfn_cache_refresh(kvm, gpc, gpa, len, dirty);
329 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_init);
331 void kvm_gfn_to_pfn_cache_destroy(struct kvm *kvm, struct gfn_to_pfn_cache *gpc)
334 spin_lock(&kvm->gpc_lock);
335 list_del(&gpc->list);
336 spin_unlock(&kvm->gpc_lock);
338 kvm_gfn_to_pfn_cache_unmap(kvm, gpc);
342 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_destroy);