blob: 9bfe1d6f6529a2ff67094b66688a697e4605f11b [file] [log] [blame]
Thomas Gleixner775c8a32019-06-04 10:11:37 +02001// SPDX-License-Identifier: GPL-2.0-only
Gleb Natapovaf585b92010-10-14 11:22:46 +02002/*
3 * kvm asynchronous fault support
4 *
5 * Copyright 2010 Red Hat, Inc.
6 *
7 * Author:
8 * Gleb Natapov <gleb@redhat.com>
Gleb Natapovaf585b92010-10-14 11:22:46 +02009 */
10
11#include <linux/kvm_host.h>
12#include <linux/slab.h>
13#include <linux/module.h>
14#include <linux/mmu_context.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010015#include <linux/sched/mm.h>
Gleb Natapovaf585b92010-10-14 11:22:46 +020016
17#include "async_pf.h"
18#include <trace/events/kvm.h>
19
20static struct kmem_cache *async_pf_cache;
21
22int 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
32void kvm_async_pf_deinit(void)
33{
Markus Elfring4f526962015-11-15 10:40:36 +010034 kmem_cache_destroy(async_pf_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +020035 async_pf_cache = NULL;
36}
37
38void 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
45static void async_pf_execute(struct work_struct *work)
46{
Gleb Natapovaf585b92010-10-14 11:22:46 +020047 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;
Sean Christopherson736c2912019-12-06 15:57:14 -080052 gpa_t cr2_or_gpa = apf->cr2_or_gpa;
Lorenzo Stoakes8b7457e2016-12-14 15:06:55 -080053 int locked = 1;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020054 bool first;
Gleb Natapovaf585b92010-10-14 11:22:46 +020055
56 might_sleep();
57
Dave Hansen1e987792016-02-12 13:01:54 -080058 /*
Wei Yangbdd303c2018-11-05 14:45:03 +080059 * This work is run asynchronously to the task which owns
Dave Hansen1e987792016-02-12 13:01:54 -080060 * mm and might be done in another context, so we must
Lorenzo Stoakes8b7457e2016-12-14 15:06:55 -080061 * access remotely.
Dave Hansen1e987792016-02-12 13:01:54 -080062 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070063 mmap_read_lock(mm);
Peter Xu64019a22020-08-11 18:39:01 -070064 get_user_pages_remote(mm, addr, 1, FOLL_WRITE, NULL, NULL,
Lorenzo Stoakes8b7457e2016-12-14 15:06:55 -080065 &locked);
66 if (locked)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070067 mmap_read_unlock(mm);
Dave Hansen1e987792016-02-12 13:01:54 -080068
Paolo Bonzini4425f562020-01-20 16:14:37 +010069 if (IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC))
70 kvm_arch_async_page_present(vcpu, apf);
Gleb Natapovaf585b92010-10-14 11:22:46 +020071
72 spin_lock(&vcpu->async_pf.lock);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020073 first = list_empty(&vcpu->async_pf.done);
Gleb Natapovaf585b92010-10-14 11:22:46 +020074 list_add_tail(&apf->link, &vcpu->async_pf.done);
Paolo Bonzini22583f02016-11-17 15:55:45 +010075 apf->vcpu = NULL;
Gleb Natapovaf585b92010-10-14 11:22:46 +020076 spin_unlock(&vcpu->async_pf.lock);
77
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +020078 if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first)
79 kvm_arch_async_page_present_queued(vcpu);
80
Gleb Natapovaf585b92010-10-14 11:22:46 +020081 /*
82 * apf may be freed by kvm_check_async_pf_completion() after
83 * this point
84 */
85
Sean Christopherson736c2912019-12-06 15:57:14 -080086 trace_kvm_async_pf_completed(addr, cr2_or_gpa);
Gleb Natapovaf585b92010-10-14 11:22:46 +020087
Sean Christophersond92a5d12021-10-08 19:12:12 -070088 __kvm_vcpu_wake_up(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +020089
Oleg Nesterov41c22f62014-04-21 15:26:01 +020090 mmput(mm);
Gleb Natapovaf585b92010-10-14 11:22:46 +020091 kvm_put_kvm(vcpu->kvm);
92}
93
94void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu)
95{
Paolo Bonzini22583f02016-11-17 15:55:45 +010096 spin_lock(&vcpu->async_pf.lock);
97
Gleb Natapovaf585b92010-10-14 11:22:46 +020098 /* cancel outstanding work queue item */
99 while (!list_empty(&vcpu->async_pf.queue)) {
100 struct kvm_async_pf *work =
Geliang Tang433da862016-01-01 19:47:15 +0800101 list_first_entry(&vcpu->async_pf.queue,
102 typeof(*work), queue);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200103 list_del(&work->queue);
Dominik Dingel9f2ceda2013-09-03 12:31:16 +0200104
Paolo Bonzini22583f02016-11-17 15:55:45 +0100105 /*
106 * We know it's present in vcpu->async_pf.done, do
107 * nothing here.
108 */
109 if (!work->vcpu)
110 continue;
111
112 spin_unlock(&vcpu->async_pf.lock);
Dominik Dingel9f2ceda2013-09-03 12:31:16 +0200113#ifdef CONFIG_KVM_ASYNC_PF_SYNC
114 flush_work(&work->work);
115#else
Radim Krčmář98fda162013-09-04 22:32:24 +0200116 if (cancel_work_sync(&work->work)) {
Oleg Nesterov41c22f62014-04-21 15:26:01 +0200117 mmput(work->mm);
Radim Krčmář28b441e2013-09-04 22:32:23 +0200118 kvm_put_kvm(vcpu->kvm); /* == work->vcpu->kvm */
Gleb Natapovaf585b92010-10-14 11:22:46 +0200119 kmem_cache_free(async_pf_cache, work);
Radim Krčmář28b441e2013-09-04 22:32:23 +0200120 }
Dominik Dingel9f2ceda2013-09-03 12:31:16 +0200121#endif
Paolo Bonzini22583f02016-11-17 15:55:45 +0100122 spin_lock(&vcpu->async_pf.lock);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200123 }
124
Gleb Natapovaf585b92010-10-14 11:22:46 +0200125 while (!list_empty(&vcpu->async_pf.done)) {
126 struct kvm_async_pf *work =
Geliang Tang433da862016-01-01 19:47:15 +0800127 list_first_entry(&vcpu->async_pf.done,
128 typeof(*work), link);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200129 list_del(&work->link);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200130 kmem_cache_free(async_pf_cache, work);
131 }
132 spin_unlock(&vcpu->async_pf.lock);
133
134 vcpu->async_pf.queued = 0;
135}
136
137void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)
138{
139 struct kvm_async_pf *work;
140
Xiao Guangrong15096ff2010-11-02 17:35:35 +0800141 while (!list_empty_careful(&vcpu->async_pf.done) &&
Vitaly Kuznetsov7c0ade62020-05-25 16:41:18 +0200142 kvm_arch_can_dequeue_async_page_present(vcpu)) {
Xiao Guangrong15096ff2010-11-02 17:35:35 +0800143 spin_lock(&vcpu->async_pf.lock);
144 work = list_first_entry(&vcpu->async_pf.done, typeof(*work),
145 link);
146 list_del(&work->link);
147 spin_unlock(&vcpu->async_pf.lock);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200148
chai wenf2e10662013-10-14 22:22:33 +0800149 kvm_arch_async_page_ready(vcpu, work);
Paolo Bonzini4425f562020-01-20 16:14:37 +0100150 if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC))
151 kvm_arch_async_page_present(vcpu, work);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200152
Xiao Guangrong15096ff2010-11-02 17:35:35 +0800153 list_del(&work->queue);
154 vcpu->async_pf.queued--;
Xiao Guangrong15096ff2010-11-02 17:35:35 +0800155 kmem_cache_free(async_pf_cache, work);
156 }
Gleb Natapovaf585b92010-10-14 11:22:46 +0200157}
158
Vitaly Kuznetsove8c22262020-06-15 14:13:34 +0200159/*
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 */
163bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
164 unsigned long hva, struct kvm_arch_async_pf *arch)
Gleb Natapovaf585b92010-10-14 11:22:46 +0200165{
166 struct kvm_async_pf *work;
167
168 if (vcpu->async_pf.queued >= ASYNC_PF_PER_VCPU)
Vitaly Kuznetsove8c22262020-06-15 14:13:34 +0200169 return false;
Gleb Natapovaf585b92010-10-14 11:22:46 +0200170
Vitaly Kuznetsov7863e342020-06-10 19:55:31 +0200171 /* Arch specific code should not do async PF in this case */
172 if (unlikely(kvm_is_error_hva(hva)))
Vitaly Kuznetsove8c22262020-06-15 14:13:34 +0200173 return false;
Gleb Natapovaf585b92010-10-14 11:22:46 +0200174
175 /*
176 * do alloc nowait since if we are going to sleep anyway we
177 * may as well sleep faulting in page
178 */
Christian Borntraegerd7444792016-02-19 13:11:46 +0100179 work = kmem_cache_zalloc(async_pf_cache, GFP_NOWAIT | __GFP_NOWARN);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200180 if (!work)
Vitaly Kuznetsove8c22262020-06-15 14:13:34 +0200181 return false;
Gleb Natapovaf585b92010-10-14 11:22:46 +0200182
chai wenf2e10662013-10-14 22:22:33 +0800183 work->wakeup_all = false;
Gleb Natapovaf585b92010-10-14 11:22:46 +0200184 work->vcpu = vcpu;
Sean Christopherson736c2912019-12-06 15:57:14 -0800185 work->cr2_or_gpa = cr2_or_gpa;
Dominik Dingele0ead412013-06-06 15:32:37 +0200186 work->addr = hva;
Gleb Natapovaf585b92010-10-14 11:22:46 +0200187 work->arch = *arch;
188 work->mm = current->mm;
Vegard Nossum3fce3712017-02-27 14:30:10 -0800189 mmget(work->mm);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200190 kvm_get_kvm(work->vcpu->kvm);
191
Gleb Natapovaf585b92010-10-14 11:22:46 +0200192 INIT_WORK(&work->work, async_pf_execute);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200193
194 list_add_tail(&work->queue, &vcpu->async_pf.queue);
195 vcpu->async_pf.queued++;
Vitaly Kuznetsov2a18b7e2020-06-10 19:55:32 +0200196 work->notpresent_injected = kvm_arch_async_page_not_present(vcpu, work);
Vitaly Kuznetsov7863e342020-06-10 19:55:31 +0200197
198 schedule_work(&work->work);
199
Vitaly Kuznetsove8c22262020-06-15 14:13:34 +0200200 return true;
Gleb Natapovaf585b92010-10-14 11:22:46 +0200201}
Gleb Natapov344d9582010-10-14 11:22:50 +0200202
203int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu)
204{
205 struct kvm_async_pf *work;
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +0200206 bool first;
Gleb Natapov344d9582010-10-14 11:22:50 +0200207
Xiao Guangrong64f638c2010-11-01 17:03:44 +0800208 if (!list_empty_careful(&vcpu->async_pf.done))
Gleb Natapov344d9582010-10-14 11:22:50 +0200209 return 0;
210
211 work = kmem_cache_zalloc(async_pf_cache, GFP_ATOMIC);
212 if (!work)
213 return -ENOMEM;
214
chai wenf2e10662013-10-14 22:22:33 +0800215 work->wakeup_all = true;
Gleb Natapov344d9582010-10-14 11:22:50 +0200216 INIT_LIST_HEAD(&work->queue); /* for list_del to work */
217
Xiao Guangrong64f638c2010-11-01 17:03:44 +0800218 spin_lock(&vcpu->async_pf.lock);
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +0200219 first = list_empty(&vcpu->async_pf.done);
Gleb Natapov344d9582010-10-14 11:22:50 +0200220 list_add_tail(&work->link, &vcpu->async_pf.done);
Xiao Guangrong64f638c2010-11-01 17:03:44 +0800221 spin_unlock(&vcpu->async_pf.lock);
222
Vitaly Kuznetsov557a9612020-05-25 16:41:21 +0200223 if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first)
224 kvm_arch_async_page_present_queued(vcpu);
225
Gleb Natapov344d9582010-10-14 11:22:50 +0200226 vcpu->async_pf.queued++;
227 return 0;
228}