.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * kvm asynchronous fault support |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * |
---|
6 | 7 | * Author: |
---|
7 | 8 | * Gleb Natapov <gleb@redhat.com> |
---|
8 | | - * |
---|
9 | | - * This file is free software; you can redistribute it and/or modify |
---|
10 | | - * it under the terms of version 2 of the GNU General Public License |
---|
11 | | - * as published by the Free Software Foundation. |
---|
12 | | - * |
---|
13 | | - * This program is distributed in the hope that it will be useful, |
---|
14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | | - * GNU General Public License for more details. |
---|
17 | | - * |
---|
18 | | - * You should have received a copy of the GNU General Public License |
---|
19 | | - * along with this program; if not, write to the Free Software Foundation, |
---|
20 | | - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | 9 | */ |
---|
22 | 10 | |
---|
23 | 11 | #include <linux/kvm_host.h> |
---|
.. | .. |
---|
28 | 16 | |
---|
29 | 17 | #include "async_pf.h" |
---|
30 | 18 | #include <trace/events/kvm.h> |
---|
31 | | - |
---|
32 | | -static inline void kvm_async_page_present_sync(struct kvm_vcpu *vcpu, |
---|
33 | | - struct kvm_async_pf *work) |
---|
34 | | -{ |
---|
35 | | -#ifdef CONFIG_KVM_ASYNC_PF_SYNC |
---|
36 | | - kvm_arch_async_page_present(vcpu, work); |
---|
37 | | -#endif |
---|
38 | | -} |
---|
39 | | -static inline void kvm_async_page_present_async(struct kvm_vcpu *vcpu, |
---|
40 | | - struct kvm_async_pf *work) |
---|
41 | | -{ |
---|
42 | | -#ifndef CONFIG_KVM_ASYNC_PF_SYNC |
---|
43 | | - kvm_arch_async_page_present(vcpu, work); |
---|
44 | | -#endif |
---|
45 | | -} |
---|
46 | 19 | |
---|
47 | 20 | static struct kmem_cache *async_pf_cache; |
---|
48 | 21 | |
---|
.. | .. |
---|
78 | 51 | unsigned long addr = apf->addr; |
---|
79 | 52 | gpa_t cr2_or_gpa = apf->cr2_or_gpa; |
---|
80 | 53 | int locked = 1; |
---|
| 54 | + bool first; |
---|
81 | 55 | |
---|
82 | 56 | might_sleep(); |
---|
83 | 57 | |
---|
84 | 58 | /* |
---|
85 | | - * This work is run asynchromously to the task which owns |
---|
| 59 | + * This work is run asynchronously to the task which owns |
---|
86 | 60 | * mm and might be done in another context, so we must |
---|
87 | 61 | * access remotely. |
---|
88 | 62 | */ |
---|
89 | | - down_read(&mm->mmap_sem); |
---|
90 | | - get_user_pages_remote(NULL, mm, addr, 1, FOLL_WRITE, NULL, NULL, |
---|
| 63 | + mmap_read_lock(mm); |
---|
| 64 | + get_user_pages_remote(mm, addr, 1, FOLL_WRITE, NULL, NULL, |
---|
91 | 65 | &locked); |
---|
92 | 66 | if (locked) |
---|
93 | | - up_read(&mm->mmap_sem); |
---|
| 67 | + mmap_read_unlock(mm); |
---|
94 | 68 | |
---|
95 | | - kvm_async_page_present_sync(vcpu, apf); |
---|
| 69 | + if (IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC)) |
---|
| 70 | + kvm_arch_async_page_present(vcpu, apf); |
---|
96 | 71 | |
---|
97 | 72 | spin_lock(&vcpu->async_pf.lock); |
---|
| 73 | + first = list_empty(&vcpu->async_pf.done); |
---|
98 | 74 | list_add_tail(&apf->link, &vcpu->async_pf.done); |
---|
99 | 75 | apf->vcpu = NULL; |
---|
100 | 76 | spin_unlock(&vcpu->async_pf.lock); |
---|
| 77 | + |
---|
| 78 | + if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first) |
---|
| 79 | + kvm_arch_async_page_present_queued(vcpu); |
---|
101 | 80 | |
---|
102 | 81 | /* |
---|
103 | 82 | * apf may be freed by kvm_check_async_pf_completion() after |
---|
.. | .. |
---|
106 | 85 | |
---|
107 | 86 | trace_kvm_async_pf_completed(addr, cr2_or_gpa); |
---|
108 | 87 | |
---|
109 | | - if (swq_has_sleeper(&vcpu->wq)) |
---|
110 | | - swake_up_one(&vcpu->wq); |
---|
| 88 | + rcuwait_wake_up(&vcpu->wait); |
---|
111 | 89 | |
---|
112 | 90 | mmput(mm); |
---|
113 | 91 | kvm_put_kvm(vcpu->kvm); |
---|
.. | .. |
---|
161 | 139 | struct kvm_async_pf *work; |
---|
162 | 140 | |
---|
163 | 141 | while (!list_empty_careful(&vcpu->async_pf.done) && |
---|
164 | | - kvm_arch_can_inject_async_page_present(vcpu)) { |
---|
| 142 | + kvm_arch_can_dequeue_async_page_present(vcpu)) { |
---|
165 | 143 | spin_lock(&vcpu->async_pf.lock); |
---|
166 | 144 | work = list_first_entry(&vcpu->async_pf.done, typeof(*work), |
---|
167 | 145 | link); |
---|
.. | .. |
---|
169 | 147 | spin_unlock(&vcpu->async_pf.lock); |
---|
170 | 148 | |
---|
171 | 149 | kvm_arch_async_page_ready(vcpu, work); |
---|
172 | | - kvm_async_page_present_async(vcpu, work); |
---|
| 150 | + if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC)) |
---|
| 151 | + kvm_arch_async_page_present(vcpu, work); |
---|
173 | 152 | |
---|
174 | 153 | list_del(&work->queue); |
---|
175 | 154 | vcpu->async_pf.queued--; |
---|
.. | .. |
---|
177 | 156 | } |
---|
178 | 157 | } |
---|
179 | 158 | |
---|
180 | | -int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
---|
181 | | - unsigned long hva, struct kvm_arch_async_pf *arch) |
---|
| 159 | +/* |
---|
| 160 | + * Try to schedule a job to handle page fault asynchronously. Returns 'true' on |
---|
| 161 | + * success, 'false' on failure (page fault has to be handled synchronously). |
---|
| 162 | + */ |
---|
| 163 | +bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
---|
| 164 | + unsigned long hva, struct kvm_arch_async_pf *arch) |
---|
182 | 165 | { |
---|
183 | 166 | struct kvm_async_pf *work; |
---|
184 | 167 | |
---|
185 | 168 | if (vcpu->async_pf.queued >= ASYNC_PF_PER_VCPU) |
---|
186 | | - return 0; |
---|
| 169 | + return false; |
---|
187 | 170 | |
---|
188 | | - /* setup delayed work */ |
---|
| 171 | + /* Arch specific code should not do async PF in this case */ |
---|
| 172 | + if (unlikely(kvm_is_error_hva(hva))) |
---|
| 173 | + return false; |
---|
189 | 174 | |
---|
190 | 175 | /* |
---|
191 | 176 | * do alloc nowait since if we are going to sleep anyway we |
---|
.. | .. |
---|
193 | 178 | */ |
---|
194 | 179 | work = kmem_cache_zalloc(async_pf_cache, GFP_NOWAIT | __GFP_NOWARN); |
---|
195 | 180 | if (!work) |
---|
196 | | - return 0; |
---|
| 181 | + return false; |
---|
197 | 182 | |
---|
198 | 183 | work->wakeup_all = false; |
---|
199 | 184 | work->vcpu = vcpu; |
---|
.. | .. |
---|
204 | 189 | mmget(work->mm); |
---|
205 | 190 | kvm_get_kvm(work->vcpu->kvm); |
---|
206 | 191 | |
---|
207 | | - /* this can't really happen otherwise gfn_to_pfn_async |
---|
208 | | - would succeed */ |
---|
209 | | - if (unlikely(kvm_is_error_hva(work->addr))) |
---|
210 | | - goto retry_sync; |
---|
211 | | - |
---|
212 | 192 | INIT_WORK(&work->work, async_pf_execute); |
---|
213 | | - if (!schedule_work(&work->work)) |
---|
214 | | - goto retry_sync; |
---|
215 | 193 | |
---|
216 | 194 | list_add_tail(&work->queue, &vcpu->async_pf.queue); |
---|
217 | 195 | vcpu->async_pf.queued++; |
---|
218 | | - kvm_arch_async_page_not_present(vcpu, work); |
---|
219 | | - return 1; |
---|
220 | | -retry_sync: |
---|
221 | | - kvm_put_kvm(work->vcpu->kvm); |
---|
222 | | - mmput(work->mm); |
---|
223 | | - kmem_cache_free(async_pf_cache, work); |
---|
224 | | - return 0; |
---|
| 196 | + work->notpresent_injected = kvm_arch_async_page_not_present(vcpu, work); |
---|
| 197 | + |
---|
| 198 | + schedule_work(&work->work); |
---|
| 199 | + |
---|
| 200 | + return true; |
---|
225 | 201 | } |
---|
226 | 202 | |
---|
227 | 203 | int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu) |
---|
228 | 204 | { |
---|
229 | 205 | struct kvm_async_pf *work; |
---|
| 206 | + bool first; |
---|
230 | 207 | |
---|
231 | 208 | if (!list_empty_careful(&vcpu->async_pf.done)) |
---|
232 | 209 | return 0; |
---|
.. | .. |
---|
239 | 216 | INIT_LIST_HEAD(&work->queue); /* for list_del to work */ |
---|
240 | 217 | |
---|
241 | 218 | spin_lock(&vcpu->async_pf.lock); |
---|
| 219 | + first = list_empty(&vcpu->async_pf.done); |
---|
242 | 220 | list_add_tail(&work->link, &vcpu->async_pf.done); |
---|
243 | 221 | spin_unlock(&vcpu->async_pf.lock); |
---|
244 | 222 | |
---|
| 223 | + if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first) |
---|
| 224 | + kvm_arch_async_page_present_queued(vcpu); |
---|
| 225 | + |
---|
245 | 226 | vcpu->async_pf.queued++; |
---|
246 | 227 | return 0; |
---|
247 | 228 | } |
---|