Commit | Line | Data |
---|---|---|
775c8a3d | 1 | // SPDX-License-Identifier: GPL-2.0-only |
af585b92 GN |
2 | /* |
3 | * kvm asynchronous fault support | |
4 | * | |
5 | * Copyright 2010 Red Hat, Inc. | |
6 | * | |
7 | * Author: | |
8 | * Gleb Natapov <gleb@redhat.com> | |
af585b92 GN |
9 | */ |
10 | ||
11 | #include <linux/kvm_host.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/mmu_context.h> | |
6e84f315 | 15 | #include <linux/sched/mm.h> |
af585b92 GN |
16 | |
17 | #include "async_pf.h" | |
18 | #include <trace/events/kvm.h> | |
19 | ||
20 | static struct kmem_cache *async_pf_cache; | |
21 | ||
22 | int kvm_async_pf_init(void) | |
23 | { | |
24 | async_pf_cache = KMEM_CACHE(kvm_async_pf, 0); | |
25 | ||
26 | if (!async_pf_cache) | |
27 | return -ENOMEM; | |
28 | ||
29 | return 0; | |
30 | } | |
31 | ||
32 | void kvm_async_pf_deinit(void) | |
33 | { | |
4f52696a | 34 | kmem_cache_destroy(async_pf_cache); |
af585b92 GN |
35 | async_pf_cache = NULL; |
36 | } | |
37 | ||
38 | void kvm_async_pf_vcpu_init(struct kvm_vcpu *vcpu) | |
39 | { | |
40 | INIT_LIST_HEAD(&vcpu->async_pf.done); | |
41 | INIT_LIST_HEAD(&vcpu->async_pf.queue); | |
42 | spin_lock_init(&vcpu->async_pf.lock); | |
43 | } | |
44 | ||
45 | static void async_pf_execute(struct work_struct *work) | |
46 | { | |
af585b92 GN |
47 | struct kvm_async_pf *apf = |
48 | container_of(work, struct kvm_async_pf, work); | |
49 | struct mm_struct *mm = apf->mm; | |
50 | struct kvm_vcpu *vcpu = apf->vcpu; | |
51 | unsigned long addr = apf->addr; | |
736c291c | 52 | gpa_t cr2_or_gpa = apf->cr2_or_gpa; |
8b7457ef | 53 | int locked = 1; |
557a961a | 54 | bool first; |
af585b92 GN |
55 | |
56 | might_sleep(); | |
57 | ||
1e987790 | 58 | /* |
bdd303cb | 59 | * This work is run asynchronously to the task which owns |
1e987790 | 60 | * mm and might be done in another context, so we must |
8b7457ef | 61 | * access remotely. |
1e987790 | 62 | */ |
d8ed45c5 | 63 | mmap_read_lock(mm); |
ca5e8632 | 64 | get_user_pages_remote(mm, addr, 1, FOLL_WRITE, NULL, &locked); |
8b7457ef | 65 | if (locked) |
d8ed45c5 | 66 | mmap_read_unlock(mm); |
422eeb54 | 67 | mmput(mm); |
1e987790 | 68 | |
4425f567 PB |
69 | if (IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC)) |
70 | kvm_arch_async_page_present(vcpu, apf); | |
af585b92 GN |
71 | |
72 | spin_lock(&vcpu->async_pf.lock); | |
557a961a | 73 | first = list_empty(&vcpu->async_pf.done); |
af585b92 | 74 | list_add_tail(&apf->link, &vcpu->async_pf.done); |
22583f0d | 75 | apf->vcpu = NULL; |
af585b92 GN |
76 | spin_unlock(&vcpu->async_pf.lock); |
77 | ||
557a961a VK |
78 | if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first) |
79 | kvm_arch_async_page_present_queued(vcpu); | |
80 | ||
af585b92 GN |
81 | /* |
82 | * apf may be freed by kvm_check_async_pf_completion() after | |
83 | * this point | |
84 | */ | |
85 | ||
736c291c | 86 | trace_kvm_async_pf_completed(addr, cr2_or_gpa); |
af585b92 | 87 | |
d92a5d1c | 88 | __kvm_vcpu_wake_up(vcpu); |
3d75b8aa SC |
89 | } |
90 | ||
91 | static void kvm_flush_and_free_async_pf_work(struct kvm_async_pf *work) | |
92 | { | |
93 | /* | |
94 | * The async #PF is "done", but KVM must wait for the work item itself, | |
95 | * i.e. async_pf_execute(), to run to completion. If KVM is a module, | |
96 | * KVM must ensure *no* code owned by the KVM (the module) can be run | |
97 | * after the last call to module_put(). Note, flushing the work item | |
98 | * is always required when the item is taken off the completion queue. | |
99 | * E.g. even if the vCPU handles the item in the "normal" path, the VM | |
100 | * could be terminated before async_pf_execute() completes. | |
101 | * | |
102 | * Wake all events skip the queue and go straight done, i.e. don't | |
103 | * need to be flushed (but sanity check that the work wasn't queued). | |
104 | */ | |
105 | if (work->wakeup_all) | |
106 | WARN_ON_ONCE(work->work.func); | |
107 | else | |
108 | flush_work(&work->work); | |
109 | kmem_cache_free(async_pf_cache, work); | |
af585b92 GN |
110 | } |
111 | ||
112 | void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu) | |
113 | { | |
22583f0d PB |
114 | spin_lock(&vcpu->async_pf.lock); |
115 | ||
af585b92 GN |
116 | /* cancel outstanding work queue item */ |
117 | while (!list_empty(&vcpu->async_pf.queue)) { | |
118 | struct kvm_async_pf *work = | |
433da860 GT |
119 | list_first_entry(&vcpu->async_pf.queue, |
120 | typeof(*work), queue); | |
af585b92 | 121 | list_del(&work->queue); |
9f2ceda4 | 122 | |
22583f0d PB |
123 | /* |
124 | * We know it's present in vcpu->async_pf.done, do | |
125 | * nothing here. | |
126 | */ | |
127 | if (!work->vcpu) | |
128 | continue; | |
129 | ||
130 | spin_unlock(&vcpu->async_pf.lock); | |
9f2ceda4 DD |
131 | #ifdef CONFIG_KVM_ASYNC_PF_SYNC |
132 | flush_work(&work->work); | |
133 | #else | |
98fda169 | 134 | if (cancel_work_sync(&work->work)) { |
41c22f62 | 135 | mmput(work->mm); |
af585b92 | 136 | kmem_cache_free(async_pf_cache, work); |
28b441e2 | 137 | } |
9f2ceda4 | 138 | #endif |
22583f0d | 139 | spin_lock(&vcpu->async_pf.lock); |
af585b92 GN |
140 | } |
141 | ||
af585b92 GN |
142 | while (!list_empty(&vcpu->async_pf.done)) { |
143 | struct kvm_async_pf *work = | |
433da860 GT |
144 | list_first_entry(&vcpu->async_pf.done, |
145 | typeof(*work), link); | |
af585b92 | 146 | list_del(&work->link); |
3d75b8aa SC |
147 | |
148 | spin_unlock(&vcpu->async_pf.lock); | |
149 | kvm_flush_and_free_async_pf_work(work); | |
150 | spin_lock(&vcpu->async_pf.lock); | |
af585b92 GN |
151 | } |
152 | spin_unlock(&vcpu->async_pf.lock); | |
153 | ||
154 | vcpu->async_pf.queued = 0; | |
155 | } | |
156 | ||
157 | void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu) | |
158 | { | |
159 | struct kvm_async_pf *work; | |
160 | ||
15096ffc | 161 | while (!list_empty_careful(&vcpu->async_pf.done) && |
7c0ade6c | 162 | kvm_arch_can_dequeue_async_page_present(vcpu)) { |
15096ffc XG |
163 | spin_lock(&vcpu->async_pf.lock); |
164 | work = list_first_entry(&vcpu->async_pf.done, typeof(*work), | |
165 | link); | |
166 | list_del(&work->link); | |
167 | spin_unlock(&vcpu->async_pf.lock); | |
af585b92 | 168 | |
f2e10669 | 169 | kvm_arch_async_page_ready(vcpu, work); |
4425f567 PB |
170 | if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC)) |
171 | kvm_arch_async_page_present(vcpu, work); | |
af585b92 | 172 | |
15096ffc XG |
173 | list_del(&work->queue); |
174 | vcpu->async_pf.queued--; | |
3d75b8aa | 175 | kvm_flush_and_free_async_pf_work(work); |
15096ffc | 176 | } |
af585b92 GN |
177 | } |
178 | ||
e8c22266 VK |
179 | /* |
180 | * Try to schedule a job to handle page fault asynchronously. Returns 'true' on | |
181 | * success, 'false' on failure (page fault has to be handled synchronously). | |
182 | */ | |
183 | bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, | |
184 | unsigned long hva, struct kvm_arch_async_pf *arch) | |
af585b92 GN |
185 | { |
186 | struct kvm_async_pf *work; | |
187 | ||
188 | if (vcpu->async_pf.queued >= ASYNC_PF_PER_VCPU) | |
e8c22266 | 189 | return false; |
af585b92 | 190 | |
7863e346 VK |
191 | /* Arch specific code should not do async PF in this case */ |
192 | if (unlikely(kvm_is_error_hva(hva))) | |
e8c22266 | 193 | return false; |
af585b92 GN |
194 | |
195 | /* | |
196 | * do alloc nowait since if we are going to sleep anyway we | |
197 | * may as well sleep faulting in page | |
198 | */ | |
d7444794 | 199 | work = kmem_cache_zalloc(async_pf_cache, GFP_NOWAIT | __GFP_NOWARN); |
af585b92 | 200 | if (!work) |
e8c22266 | 201 | return false; |
af585b92 | 202 | |
f2e10669 | 203 | work->wakeup_all = false; |
af585b92 | 204 | work->vcpu = vcpu; |
736c291c | 205 | work->cr2_or_gpa = cr2_or_gpa; |
e0ead41a | 206 | work->addr = hva; |
af585b92 GN |
207 | work->arch = *arch; |
208 | work->mm = current->mm; | |
3fce371b | 209 | mmget(work->mm); |
af585b92 | 210 | |
af585b92 | 211 | INIT_WORK(&work->work, async_pf_execute); |
af585b92 GN |
212 | |
213 | list_add_tail(&work->queue, &vcpu->async_pf.queue); | |
214 | vcpu->async_pf.queued++; | |
2a18b7e7 | 215 | work->notpresent_injected = kvm_arch_async_page_not_present(vcpu, work); |
7863e346 VK |
216 | |
217 | schedule_work(&work->work); | |
218 | ||
e8c22266 | 219 | return true; |
af585b92 | 220 | } |
344d9588 GN |
221 | |
222 | int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu) | |
223 | { | |
224 | struct kvm_async_pf *work; | |
557a961a | 225 | bool first; |
344d9588 | 226 | |
64f638c7 | 227 | if (!list_empty_careful(&vcpu->async_pf.done)) |
344d9588 GN |
228 | return 0; |
229 | ||
230 | work = kmem_cache_zalloc(async_pf_cache, GFP_ATOMIC); | |
231 | if (!work) | |
232 | return -ENOMEM; | |
233 | ||
f2e10669 | 234 | work->wakeup_all = true; |
344d9588 GN |
235 | INIT_LIST_HEAD(&work->queue); /* for list_del to work */ |
236 | ||
64f638c7 | 237 | spin_lock(&vcpu->async_pf.lock); |
557a961a | 238 | first = list_empty(&vcpu->async_pf.done); |
344d9588 | 239 | list_add_tail(&work->link, &vcpu->async_pf.done); |
64f638c7 XG |
240 | spin_unlock(&vcpu->async_pf.lock); |
241 | ||
557a961a VK |
242 | if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first) |
243 | kvm_arch_async_page_present_queued(vcpu); | |
244 | ||
344d9588 GN |
245 | vcpu->async_pf.queued++; |
246 | return 0; | |
247 | } |