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);
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_gfn_to_pfn_cache_check(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
80 gpa_t gpa, unsigned long len)
82 struct kvm_memslots *slots = kvm_memslots(kvm);
84 if ((gpa & ~PAGE_MASK) + len > PAGE_SIZE)
87 if (gpc->gpa != gpa || gpc->generation != slots->generation ||
88 kvm_is_error_hva(gpc->uhva))
96 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_check);
98 static void __release_gpc(struct kvm *kvm, kvm_pfn_t pfn, void *khva, gpa_t gpa)
100 /* Unmap the old page if it was mapped before, and release it */
101 if (!is_error_noslot_pfn(pfn)) {
104 kunmap(pfn_to_page(pfn));
105 #ifdef CONFIG_HAS_IOMEM
111 kvm_release_pfn(pfn, false);
115 static kvm_pfn_t hva_to_pfn_retry(struct kvm *kvm, unsigned long uhva)
117 unsigned long mmu_seq;
122 mmu_seq = kvm->mmu_notifier_seq;
125 /* We always request a writeable mapping */
126 new_pfn = hva_to_pfn(uhva, false, NULL, true, NULL);
127 if (is_error_noslot_pfn(new_pfn))
130 KVM_MMU_READ_LOCK(kvm);
131 retry = mmu_notifier_retry_hva(kvm, mmu_seq, uhva);
132 KVM_MMU_READ_UNLOCK(kvm);
142 int kvm_gfn_to_pfn_cache_refresh(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
143 gpa_t gpa, unsigned long len)
145 struct kvm_memslots *slots = kvm_memslots(kvm);
146 unsigned long page_offset = gpa & ~PAGE_MASK;
147 kvm_pfn_t old_pfn, new_pfn;
148 unsigned long old_uhva;
155 * If must fit within a single page. The 'len' argument is
156 * only to enforce that.
158 if (page_offset + len > PAGE_SIZE)
161 write_lock_irq(&gpc->lock);
165 old_khva = gpc->khva - offset_in_page(gpc->khva);
166 old_uhva = gpc->uhva;
167 old_valid = gpc->valid;
169 /* If the userspace HVA is invalid, refresh that first */
170 if (gpc->gpa != gpa || gpc->generation != slots->generation ||
171 kvm_is_error_hva(gpc->uhva)) {
172 gfn_t gfn = gpa_to_gfn(gpa);
175 gpc->generation = slots->generation;
176 gpc->memslot = __gfn_to_memslot(slots, gfn);
177 gpc->uhva = gfn_to_hva_memslot(gpc->memslot, gfn);
179 if (kvm_is_error_hva(gpc->uhva)) {
180 gpc->pfn = KVM_PFN_ERR_FAULT;
185 gpc->uhva += page_offset;
189 * If the userspace HVA changed or the PFN was already invalid,
190 * drop the lock and do the HVA to PFN lookup again.
192 if (!old_valid || old_uhva != gpc->uhva) {
193 unsigned long uhva = gpc->uhva;
194 void *new_khva = NULL;
196 /* Placeholders for "hva is valid but not yet mapped" */
197 gpc->pfn = KVM_PFN_ERR_FAULT;
201 write_unlock_irq(&gpc->lock);
203 new_pfn = hva_to_pfn_retry(kvm, uhva);
204 if (is_error_noslot_pfn(new_pfn)) {
209 if (gpc->usage & KVM_HOST_USES_PFN) {
210 if (new_pfn == old_pfn) {
212 old_pfn = KVM_PFN_ERR_FAULT;
214 } else if (pfn_valid(new_pfn)) {
215 new_khva = kmap(pfn_to_page(new_pfn));
216 #ifdef CONFIG_HAS_IOMEM
218 new_khva = memremap(pfn_to_hpa(new_pfn), PAGE_SIZE, MEMREMAP_WB);
222 new_khva += page_offset;
228 write_lock_irq(&gpc->lock);
231 gpc->pfn = KVM_PFN_ERR_FAULT;
234 /* At this point, gpc->valid may already have been cleared */
236 gpc->khva = new_khva;
239 /* If the HVA→PFN mapping was already valid, don't unmap it. */
240 old_pfn = KVM_PFN_ERR_FAULT;
245 write_unlock_irq(&gpc->lock);
247 __release_gpc(kvm, old_pfn, old_khva, old_gpa);
251 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_refresh);
253 void kvm_gfn_to_pfn_cache_unmap(struct kvm *kvm, struct gfn_to_pfn_cache *gpc)
259 write_lock_irq(&gpc->lock);
263 old_khva = gpc->khva - offset_in_page(gpc->khva);
268 * We can leave the GPA → uHVA map cache intact but the PFN
269 * lookup will need to be redone even for the same page.
272 gpc->pfn = KVM_PFN_ERR_FAULT;
274 write_unlock_irq(&gpc->lock);
276 __release_gpc(kvm, old_pfn, old_khva, old_gpa);
278 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_unmap);
281 int kvm_gfn_to_pfn_cache_init(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
282 struct kvm_vcpu *vcpu, enum pfn_cache_usage usage,
283 gpa_t gpa, unsigned long len)
285 WARN_ON_ONCE(!usage || (usage & KVM_GUEST_AND_HOST_USE_PFN) != usage);
288 rwlock_init(&gpc->lock);
291 gpc->pfn = KVM_PFN_ERR_FAULT;
292 gpc->uhva = KVM_HVA_ERR_BAD;
298 spin_lock(&kvm->gpc_lock);
299 list_add(&gpc->list, &kvm->gpc_list);
300 spin_unlock(&kvm->gpc_lock);
302 return kvm_gfn_to_pfn_cache_refresh(kvm, gpc, gpa, len);
304 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_init);
306 void kvm_gfn_to_pfn_cache_destroy(struct kvm *kvm, struct gfn_to_pfn_cache *gpc)
309 spin_lock(&kvm->gpc_lock);
310 list_del(&gpc->list);
311 spin_unlock(&kvm->gpc_lock);
313 kvm_gfn_to_pfn_cache_unmap(kvm, gpc);
317 EXPORT_SYMBOL_GPL(kvm_gfn_to_pfn_cache_destroy);