xref: /linux/virt/kvm/async_pf.c (revision af585b921e5d1e919947c4b1164b59507fe7cd7b)
1*af585b92SGleb Natapov /*
2*af585b92SGleb Natapov  * kvm asynchronous fault support
3*af585b92SGleb Natapov  *
4*af585b92SGleb Natapov  * Copyright 2010 Red Hat, Inc.
5*af585b92SGleb Natapov  *
6*af585b92SGleb Natapov  * Author:
7*af585b92SGleb Natapov  *      Gleb Natapov <gleb@redhat.com>
8*af585b92SGleb Natapov  *
9*af585b92SGleb Natapov  * This file is free software; you can redistribute it and/or modify
10*af585b92SGleb Natapov  * it under the terms of version 2 of the GNU General Public License
11*af585b92SGleb Natapov  * as published by the Free Software Foundation.
12*af585b92SGleb Natapov  *
13*af585b92SGleb Natapov  * This program is distributed in the hope that it will be useful,
14*af585b92SGleb Natapov  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15*af585b92SGleb Natapov  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*af585b92SGleb Natapov  * GNU General Public License for more details.
17*af585b92SGleb Natapov  *
18*af585b92SGleb Natapov  * You should have received a copy of the GNU General Public License
19*af585b92SGleb Natapov  * along with this program; if not, write to the Free Software Foundation,
20*af585b92SGleb Natapov  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21*af585b92SGleb Natapov  */
22*af585b92SGleb Natapov 
23*af585b92SGleb Natapov #include <linux/kvm_host.h>
24*af585b92SGleb Natapov #include <linux/slab.h>
25*af585b92SGleb Natapov #include <linux/module.h>
26*af585b92SGleb Natapov #include <linux/mmu_context.h>
27*af585b92SGleb Natapov 
28*af585b92SGleb Natapov #include "async_pf.h"
29*af585b92SGleb Natapov #include <trace/events/kvm.h>
30*af585b92SGleb Natapov 
31*af585b92SGleb Natapov static struct kmem_cache *async_pf_cache;
32*af585b92SGleb Natapov 
33*af585b92SGleb Natapov int kvm_async_pf_init(void)
34*af585b92SGleb Natapov {
35*af585b92SGleb Natapov 	async_pf_cache = KMEM_CACHE(kvm_async_pf, 0);
36*af585b92SGleb Natapov 
37*af585b92SGleb Natapov 	if (!async_pf_cache)
38*af585b92SGleb Natapov 		return -ENOMEM;
39*af585b92SGleb Natapov 
40*af585b92SGleb Natapov 	return 0;
41*af585b92SGleb Natapov }
42*af585b92SGleb Natapov 
43*af585b92SGleb Natapov void kvm_async_pf_deinit(void)
44*af585b92SGleb Natapov {
45*af585b92SGleb Natapov 	if (async_pf_cache)
46*af585b92SGleb Natapov 		kmem_cache_destroy(async_pf_cache);
47*af585b92SGleb Natapov 	async_pf_cache = NULL;
48*af585b92SGleb Natapov }
49*af585b92SGleb Natapov 
50*af585b92SGleb Natapov void kvm_async_pf_vcpu_init(struct kvm_vcpu *vcpu)
51*af585b92SGleb Natapov {
52*af585b92SGleb Natapov 	INIT_LIST_HEAD(&vcpu->async_pf.done);
53*af585b92SGleb Natapov 	INIT_LIST_HEAD(&vcpu->async_pf.queue);
54*af585b92SGleb Natapov 	spin_lock_init(&vcpu->async_pf.lock);
55*af585b92SGleb Natapov }
56*af585b92SGleb Natapov 
57*af585b92SGleb Natapov static void async_pf_execute(struct work_struct *work)
58*af585b92SGleb Natapov {
59*af585b92SGleb Natapov 	struct page *page = NULL;
60*af585b92SGleb Natapov 	struct kvm_async_pf *apf =
61*af585b92SGleb Natapov 		container_of(work, struct kvm_async_pf, work);
62*af585b92SGleb Natapov 	struct mm_struct *mm = apf->mm;
63*af585b92SGleb Natapov 	struct kvm_vcpu *vcpu = apf->vcpu;
64*af585b92SGleb Natapov 	unsigned long addr = apf->addr;
65*af585b92SGleb Natapov 	gva_t gva = apf->gva;
66*af585b92SGleb Natapov 
67*af585b92SGleb Natapov 	might_sleep();
68*af585b92SGleb Natapov 
69*af585b92SGleb Natapov 	use_mm(mm);
70*af585b92SGleb Natapov 	down_read(&mm->mmap_sem);
71*af585b92SGleb Natapov 	get_user_pages(current, mm, addr, 1, 1, 0, &page, NULL);
72*af585b92SGleb Natapov 	up_read(&mm->mmap_sem);
73*af585b92SGleb Natapov 	unuse_mm(mm);
74*af585b92SGleb Natapov 
75*af585b92SGleb Natapov 	spin_lock(&vcpu->async_pf.lock);
76*af585b92SGleb Natapov 	list_add_tail(&apf->link, &vcpu->async_pf.done);
77*af585b92SGleb Natapov 	apf->page = page;
78*af585b92SGleb Natapov 	apf->done = true;
79*af585b92SGleb Natapov 	spin_unlock(&vcpu->async_pf.lock);
80*af585b92SGleb Natapov 
81*af585b92SGleb Natapov 	/*
82*af585b92SGleb Natapov 	 * apf may be freed by kvm_check_async_pf_completion() after
83*af585b92SGleb Natapov 	 * this point
84*af585b92SGleb Natapov 	 */
85*af585b92SGleb Natapov 
86*af585b92SGleb Natapov 	trace_kvm_async_pf_completed(addr, page, gva);
87*af585b92SGleb Natapov 
88*af585b92SGleb Natapov 	if (waitqueue_active(&vcpu->wq))
89*af585b92SGleb Natapov 		wake_up_interruptible(&vcpu->wq);
90*af585b92SGleb Natapov 
91*af585b92SGleb Natapov 	mmdrop(mm);
92*af585b92SGleb Natapov 	kvm_put_kvm(vcpu->kvm);
93*af585b92SGleb Natapov }
94*af585b92SGleb Natapov 
95*af585b92SGleb Natapov void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu)
96*af585b92SGleb Natapov {
97*af585b92SGleb Natapov 	/* cancel outstanding work queue item */
98*af585b92SGleb Natapov 	while (!list_empty(&vcpu->async_pf.queue)) {
99*af585b92SGleb Natapov 		struct kvm_async_pf *work =
100*af585b92SGleb Natapov 			list_entry(vcpu->async_pf.queue.next,
101*af585b92SGleb Natapov 				   typeof(*work), queue);
102*af585b92SGleb Natapov 		cancel_work_sync(&work->work);
103*af585b92SGleb Natapov 		list_del(&work->queue);
104*af585b92SGleb Natapov 		if (!work->done) /* work was canceled */
105*af585b92SGleb Natapov 			kmem_cache_free(async_pf_cache, work);
106*af585b92SGleb Natapov 	}
107*af585b92SGleb Natapov 
108*af585b92SGleb Natapov 	spin_lock(&vcpu->async_pf.lock);
109*af585b92SGleb Natapov 	while (!list_empty(&vcpu->async_pf.done)) {
110*af585b92SGleb Natapov 		struct kvm_async_pf *work =
111*af585b92SGleb Natapov 			list_entry(vcpu->async_pf.done.next,
112*af585b92SGleb Natapov 				   typeof(*work), link);
113*af585b92SGleb Natapov 		list_del(&work->link);
114*af585b92SGleb Natapov 		if (work->page)
115*af585b92SGleb Natapov 			put_page(work->page);
116*af585b92SGleb Natapov 		kmem_cache_free(async_pf_cache, work);
117*af585b92SGleb Natapov 	}
118*af585b92SGleb Natapov 	spin_unlock(&vcpu->async_pf.lock);
119*af585b92SGleb Natapov 
120*af585b92SGleb Natapov 	vcpu->async_pf.queued = 0;
121*af585b92SGleb Natapov }
122*af585b92SGleb Natapov 
123*af585b92SGleb Natapov void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)
124*af585b92SGleb Natapov {
125*af585b92SGleb Natapov 	struct kvm_async_pf *work;
126*af585b92SGleb Natapov 
127*af585b92SGleb Natapov 	if (list_empty_careful(&vcpu->async_pf.done))
128*af585b92SGleb Natapov 		return;
129*af585b92SGleb Natapov 
130*af585b92SGleb Natapov 	spin_lock(&vcpu->async_pf.lock);
131*af585b92SGleb Natapov 	work = list_first_entry(&vcpu->async_pf.done, typeof(*work), link);
132*af585b92SGleb Natapov 	list_del(&work->link);
133*af585b92SGleb Natapov 	spin_unlock(&vcpu->async_pf.lock);
134*af585b92SGleb Natapov 
135*af585b92SGleb Natapov 	kvm_arch_async_page_present(vcpu, work);
136*af585b92SGleb Natapov 
137*af585b92SGleb Natapov 	list_del(&work->queue);
138*af585b92SGleb Natapov 	vcpu->async_pf.queued--;
139*af585b92SGleb Natapov 	if (work->page)
140*af585b92SGleb Natapov 		put_page(work->page);
141*af585b92SGleb Natapov 	kmem_cache_free(async_pf_cache, work);
142*af585b92SGleb Natapov }
143*af585b92SGleb Natapov 
144*af585b92SGleb Natapov int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
145*af585b92SGleb Natapov 		       struct kvm_arch_async_pf *arch)
146*af585b92SGleb Natapov {
147*af585b92SGleb Natapov 	struct kvm_async_pf *work;
148*af585b92SGleb Natapov 
149*af585b92SGleb Natapov 	if (vcpu->async_pf.queued >= ASYNC_PF_PER_VCPU)
150*af585b92SGleb Natapov 		return 0;
151*af585b92SGleb Natapov 
152*af585b92SGleb Natapov 	/* setup delayed work */
153*af585b92SGleb Natapov 
154*af585b92SGleb Natapov 	/*
155*af585b92SGleb Natapov 	 * do alloc nowait since if we are going to sleep anyway we
156*af585b92SGleb Natapov 	 * may as well sleep faulting in page
157*af585b92SGleb Natapov 	 */
158*af585b92SGleb Natapov 	work = kmem_cache_zalloc(async_pf_cache, GFP_NOWAIT);
159*af585b92SGleb Natapov 	if (!work)
160*af585b92SGleb Natapov 		return 0;
161*af585b92SGleb Natapov 
162*af585b92SGleb Natapov 	work->page = NULL;
163*af585b92SGleb Natapov 	work->done = false;
164*af585b92SGleb Natapov 	work->vcpu = vcpu;
165*af585b92SGleb Natapov 	work->gva = gva;
166*af585b92SGleb Natapov 	work->addr = gfn_to_hva(vcpu->kvm, gfn);
167*af585b92SGleb Natapov 	work->arch = *arch;
168*af585b92SGleb Natapov 	work->mm = current->mm;
169*af585b92SGleb Natapov 	atomic_inc(&work->mm->mm_count);
170*af585b92SGleb Natapov 	kvm_get_kvm(work->vcpu->kvm);
171*af585b92SGleb Natapov 
172*af585b92SGleb Natapov 	/* this can't really happen otherwise gfn_to_pfn_async
173*af585b92SGleb Natapov 	   would succeed */
174*af585b92SGleb Natapov 	if (unlikely(kvm_is_error_hva(work->addr)))
175*af585b92SGleb Natapov 		goto retry_sync;
176*af585b92SGleb Natapov 
177*af585b92SGleb Natapov 	INIT_WORK(&work->work, async_pf_execute);
178*af585b92SGleb Natapov 	if (!schedule_work(&work->work))
179*af585b92SGleb Natapov 		goto retry_sync;
180*af585b92SGleb Natapov 
181*af585b92SGleb Natapov 	list_add_tail(&work->queue, &vcpu->async_pf.queue);
182*af585b92SGleb Natapov 	vcpu->async_pf.queued++;
183*af585b92SGleb Natapov 	kvm_arch_async_page_not_present(vcpu, work);
184*af585b92SGleb Natapov 	return 1;
185*af585b92SGleb Natapov retry_sync:
186*af585b92SGleb Natapov 	kvm_put_kvm(work->vcpu->kvm);
187*af585b92SGleb Natapov 	mmdrop(work->mm);
188*af585b92SGleb Natapov 	kmem_cache_free(async_pf_cache, work);
189*af585b92SGleb Natapov 	return 0;
190*af585b92SGleb Natapov }
191