Blame view
virt/kvm/async_pf.c
5.83 KB
af585b921 KVM: Halt vcpu if... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* * kvm asynchronous fault support * * Copyright 2010 Red Hat, Inc. * * Author: * Gleb Natapov <gleb@redhat.com> * * This file is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ #include <linux/kvm_host.h> #include <linux/slab.h> #include <linux/module.h> #include <linux/mmu_context.h> #include "async_pf.h" #include <trace/events/kvm.h> |
e0ead41a6 KVM: async_pf: Pr... |
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
static inline void kvm_async_page_present_sync(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) { #ifdef CONFIG_KVM_ASYNC_PF_SYNC kvm_arch_async_page_present(vcpu, work); #endif } static inline void kvm_async_page_present_async(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) { #ifndef CONFIG_KVM_ASYNC_PF_SYNC kvm_arch_async_page_present(vcpu, work); #endif } |
af585b921 KVM: Halt vcpu if... |
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
static struct kmem_cache *async_pf_cache; int kvm_async_pf_init(void) { async_pf_cache = KMEM_CACHE(kvm_async_pf, 0); if (!async_pf_cache) return -ENOMEM; return 0; } void kvm_async_pf_deinit(void) { |
4f52696a6 KVM-async_pf: Del... |
58 |
kmem_cache_destroy(async_pf_cache); |
af585b921 KVM: Halt vcpu if... |
59 60 61 62 63 64 65 66 67 68 69 70 |
async_pf_cache = NULL; } void kvm_async_pf_vcpu_init(struct kvm_vcpu *vcpu) { INIT_LIST_HEAD(&vcpu->async_pf.done); INIT_LIST_HEAD(&vcpu->async_pf.queue); spin_lock_init(&vcpu->async_pf.lock); } static void async_pf_execute(struct work_struct *work) { |
af585b921 KVM: Halt vcpu if... |
71 72 73 74 75 76 77 78 |
struct kvm_async_pf *apf = container_of(work, struct kvm_async_pf, work); struct mm_struct *mm = apf->mm; struct kvm_vcpu *vcpu = apf->vcpu; unsigned long addr = apf->addr; gva_t gva = apf->gva; might_sleep(); |
1e9877902 mm/gup: Introduce... |
79 80 81 82 83 |
/* * This work is run asynchromously to the task which owns * mm and might be done in another context, so we must * use FOLL_REMOTE. */ |
d4944b0ec mm: remove write/... |
84 85 |
__get_user_pages_unlocked(NULL, mm, addr, 1, NULL, FOLL_WRITE | FOLL_REMOTE); |
1e9877902 mm/gup: Introduce... |
86 |
|
e0ead41a6 KVM: async_pf: Pr... |
87 |
kvm_async_page_present_sync(vcpu, apf); |
af585b921 KVM: Halt vcpu if... |
88 89 90 |
spin_lock(&vcpu->async_pf.lock); list_add_tail(&apf->link, &vcpu->async_pf.done); |
22583f0d9 KVM: async_pf: av... |
91 |
apf->vcpu = NULL; |
af585b921 KVM: Halt vcpu if... |
92 93 94 95 96 97 |
spin_unlock(&vcpu->async_pf.lock); /* * apf may be freed by kvm_check_async_pf_completion() after * this point */ |
f2e106692 KVM: Drop FOLL_GE... |
98 |
trace_kvm_async_pf_completed(addr, gva); |
af585b921 KVM: Halt vcpu if... |
99 |
|
6003a4201 kvm: fix waitqueu... |
100 101 102 103 |
/* * This memory barrier pairs with prepare_to_wait's set_current_state() */ smp_mb(); |
8577370fb KVM: Use simple w... |
104 105 |
if (swait_active(&vcpu->wq)) swake_up(&vcpu->wq); |
af585b921 KVM: Halt vcpu if... |
106 |
|
41c22f626 KVM: async_pf: mm... |
107 |
mmput(mm); |
af585b921 KVM: Halt vcpu if... |
108 109 110 111 112 |
kvm_put_kvm(vcpu->kvm); } void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu) { |
22583f0d9 KVM: async_pf: av... |
113 |
spin_lock(&vcpu->async_pf.lock); |
af585b921 KVM: Halt vcpu if... |
114 115 116 |
/* cancel outstanding work queue item */ while (!list_empty(&vcpu->async_pf.queue)) { struct kvm_async_pf *work = |
433da8602 KVM: async_pf: us... |
117 118 |
list_first_entry(&vcpu->async_pf.queue, typeof(*work), queue); |
af585b921 KVM: Halt vcpu if... |
119 |
list_del(&work->queue); |
9f2ceda49 KVM: async_pf: Al... |
120 |
|
22583f0d9 KVM: async_pf: av... |
121 122 123 124 125 126 127 128 |
/* * We know it's present in vcpu->async_pf.done, do * nothing here. */ if (!work->vcpu) continue; spin_unlock(&vcpu->async_pf.lock); |
9f2ceda49 KVM: async_pf: Al... |
129 130 131 |
#ifdef CONFIG_KVM_ASYNC_PF_SYNC flush_work(&work->work); #else |
98fda1692 kvm: remove .done... |
132 |
if (cancel_work_sync(&work->work)) { |
41c22f626 KVM: async_pf: mm... |
133 |
mmput(work->mm); |
28b441e24 kvm: free resourc... |
134 |
kvm_put_kvm(vcpu->kvm); /* == work->vcpu->kvm */ |
af585b921 KVM: Halt vcpu if... |
135 |
kmem_cache_free(async_pf_cache, work); |
28b441e24 kvm: free resourc... |
136 |
} |
9f2ceda49 KVM: async_pf: Al... |
137 |
#endif |
22583f0d9 KVM: async_pf: av... |
138 |
spin_lock(&vcpu->async_pf.lock); |
af585b921 KVM: Halt vcpu if... |
139 |
} |
af585b921 KVM: Halt vcpu if... |
140 141 |
while (!list_empty(&vcpu->async_pf.done)) { struct kvm_async_pf *work = |
433da8602 KVM: async_pf: us... |
142 143 |
list_first_entry(&vcpu->async_pf.done, typeof(*work), link); |
af585b921 KVM: Halt vcpu if... |
144 |
list_del(&work->link); |
af585b921 KVM: Halt vcpu if... |
145 146 147 148 149 150 151 152 153 154 |
kmem_cache_free(async_pf_cache, work); } spin_unlock(&vcpu->async_pf.lock); vcpu->async_pf.queued = 0; } void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu) { struct kvm_async_pf *work; |
15096ffce KVM: handle more ... |
155 156 157 158 159 160 161 |
while (!list_empty_careful(&vcpu->async_pf.done) && kvm_arch_can_inject_async_page_present(vcpu)) { spin_lock(&vcpu->async_pf.lock); work = list_first_entry(&vcpu->async_pf.done, typeof(*work), link); list_del(&work->link); spin_unlock(&vcpu->async_pf.lock); |
af585b921 KVM: Halt vcpu if... |
162 |
|
f2e106692 KVM: Drop FOLL_GE... |
163 |
kvm_arch_async_page_ready(vcpu, work); |
1179ba539 KVM: async_pf: Ad... |
164 |
kvm_async_page_present_async(vcpu, work); |
af585b921 KVM: Halt vcpu if... |
165 |
|
15096ffce KVM: handle more ... |
166 167 |
list_del(&work->queue); vcpu->async_pf.queued--; |
15096ffce KVM: handle more ... |
168 169 |
kmem_cache_free(async_pf_cache, work); } |
af585b921 KVM: Halt vcpu if... |
170 |
} |
e0ead41a6 KVM: async_pf: Pr... |
171 |
int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva, |
af585b921 KVM: Halt vcpu if... |
172 173 174 175 176 177 178 179 180 181 182 183 184 |
struct kvm_arch_async_pf *arch) { struct kvm_async_pf *work; if (vcpu->async_pf.queued >= ASYNC_PF_PER_VCPU) return 0; /* setup delayed work */ /* * do alloc nowait since if we are going to sleep anyway we * may as well sleep faulting in page */ |
d7444794a KVM: async_pf: do... |
185 |
work = kmem_cache_zalloc(async_pf_cache, GFP_NOWAIT | __GFP_NOWARN); |
af585b921 KVM: Halt vcpu if... |
186 187 |
if (!work) return 0; |
f2e106692 KVM: Drop FOLL_GE... |
188 |
work->wakeup_all = false; |
af585b921 KVM: Halt vcpu if... |
189 190 |
work->vcpu = vcpu; work->gva = gva; |
e0ead41a6 KVM: async_pf: Pr... |
191 |
work->addr = hva; |
af585b921 KVM: Halt vcpu if... |
192 193 |
work->arch = *arch; work->mm = current->mm; |
41c22f626 KVM: async_pf: mm... |
194 |
atomic_inc(&work->mm->mm_users); |
af585b921 KVM: Halt vcpu if... |
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
kvm_get_kvm(work->vcpu->kvm); /* this can't really happen otherwise gfn_to_pfn_async would succeed */ if (unlikely(kvm_is_error_hva(work->addr))) goto retry_sync; INIT_WORK(&work->work, async_pf_execute); if (!schedule_work(&work->work)) goto retry_sync; list_add_tail(&work->queue, &vcpu->async_pf.queue); vcpu->async_pf.queued++; kvm_arch_async_page_not_present(vcpu, work); return 1; retry_sync: kvm_put_kvm(work->vcpu->kvm); |
41c22f626 KVM: async_pf: mm... |
212 |
mmput(work->mm); |
af585b921 KVM: Halt vcpu if... |
213 214 215 |
kmem_cache_free(async_pf_cache, work); return 0; } |
344d9588a KVM: Add PV MSR t... |
216 217 218 219 |
int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu) { struct kvm_async_pf *work; |
64f638c7c KVM: fix the race... |
220 |
if (!list_empty_careful(&vcpu->async_pf.done)) |
344d9588a KVM: Add PV MSR t... |
221 222 223 224 225 |
return 0; work = kmem_cache_zalloc(async_pf_cache, GFP_ATOMIC); if (!work) return -ENOMEM; |
f2e106692 KVM: Drop FOLL_GE... |
226 |
work->wakeup_all = true; |
344d9588a KVM: Add PV MSR t... |
227 |
INIT_LIST_HEAD(&work->queue); /* for list_del to work */ |
64f638c7c KVM: fix the race... |
228 |
spin_lock(&vcpu->async_pf.lock); |
344d9588a KVM: Add PV MSR t... |
229 |
list_add_tail(&work->link, &vcpu->async_pf.done); |
64f638c7c KVM: fix the race... |
230 |
spin_unlock(&vcpu->async_pf.lock); |
344d9588a KVM: Add PV MSR t... |
231 232 233 |
vcpu->async_pf.queued++; return 0; } |