kvm: fix excessive pages un-pinning in kvm_iommu_map error path.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / virt / kvm / iommu.c
CommitLineData
62c476c7
BAY
1/*
2 * Copyright (c) 2006, Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Copyright (C) 2006-2008 Intel Corporation
18 * Copyright IBM Corporation, 2008
221d059d
AK
19 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
20 *
62c476c7
BAY
21 * Author: Allen M. Kay <allen.m.kay@intel.com>
22 * Author: Weidong Han <weidong.han@intel.com>
23 * Author: Ben-Ami Yassour <benami@il.ibm.com>
24 */
25
26#include <linux/list.h>
27#include <linux/kvm_host.h>
51441d43 28#include <linux/module.h>
62c476c7 29#include <linux/pci.h>
799fd8b2 30#include <linux/stat.h>
62c476c7 31#include <linux/dmar.h>
19de40a8 32#include <linux/iommu.h>
62c476c7
BAY
33#include <linux/intel-iommu.h>
34
90ab5ee9 35static bool allow_unsafe_assigned_interrupts;
3f68b031
AW
36module_param_named(allow_unsafe_assigned_interrupts,
37 allow_unsafe_assigned_interrupts, bool, S_IRUGO | S_IWUSR);
38MODULE_PARM_DESC(allow_unsafe_assigned_interrupts,
39 "Enable device assignment on platforms without interrupt remapping support.");
40
62c476c7
BAY
41static int kvm_iommu_unmap_memslots(struct kvm *kvm);
42static void kvm_iommu_put_pages(struct kvm *kvm,
43 gfn_t base_gfn, unsigned long npages);
44
d5661048 45static pfn_t kvm_pin_pages(struct kvm_memory_slot *slot, gfn_t gfn,
3ea61129 46 unsigned long npages)
fcd95807
JR
47{
48 gfn_t end_gfn;
49 pfn_t pfn;
50
d5661048 51 pfn = gfn_to_pfn_memslot(slot, gfn);
3ea61129 52 end_gfn = gfn + npages;
fcd95807
JR
53 gfn += 1;
54
81c52c56 55 if (is_error_noslot_pfn(pfn))
fcd95807
JR
56 return pfn;
57
58 while (gfn < end_gfn)
d5661048 59 gfn_to_pfn_memslot(slot, gfn++);
fcd95807
JR
60
61 return pfn;
62}
63
6e0db2f1
MT
64static void kvm_unpin_pages(struct kvm *kvm, pfn_t pfn, unsigned long npages)
65{
66 unsigned long i;
67
68 for (i = 0; i < npages; ++i)
69 kvm_release_pfn_clean(pfn + i);
70}
71
3ad26d81 72int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
62c476c7 73{
fcd95807 74 gfn_t gfn, end_gfn;
62c476c7 75 pfn_t pfn;
fcd95807 76 int r = 0;
19de40a8 77 struct iommu_domain *domain = kvm->arch.iommu_domain;
522c68c4 78 int flags;
62c476c7
BAY
79
80 /* check if iommu exists and in use */
81 if (!domain)
82 return 0;
83
fcd95807
JR
84 gfn = slot->base_gfn;
85 end_gfn = gfn + slot->npages;
86
d47510e2
AW
87 flags = IOMMU_READ;
88 if (!(slot->flags & KVM_MEM_READONLY))
89 flags |= IOMMU_WRITE;
522c68c4
SY
90 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
91 flags |= IOMMU_CACHE;
92
fcd95807
JR
93
94 while (gfn < end_gfn) {
95 unsigned long page_size;
96
97 /* Check if already mapped */
98 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) {
99 gfn += 1;
100 continue;
101 }
102
103 /* Get the page size we could use to map */
104 page_size = kvm_host_page_size(kvm, gfn);
105
106 /* Make sure the page_size does not exceed the memslot */
107 while ((gfn + (page_size >> PAGE_SHIFT)) > end_gfn)
108 page_size >>= 1;
109
110 /* Make sure gfn is aligned to the page size we want to map */
111 while ((gfn << PAGE_SHIFT) & (page_size - 1))
112 page_size >>= 1;
113
ac18077a
GE
114 /* Make sure hva is aligned to the page size we want to map */
115 while (__gfn_to_hva_memslot(slot, gfn) & (page_size - 1))
116 page_size >>= 1;
117
fcd95807
JR
118 /*
119 * Pin all pages we are about to map in memory. This is
120 * important because we unmap and unpin in 4kb steps later.
121 */
3ea61129 122 pfn = kvm_pin_pages(slot, gfn, page_size >> PAGE_SHIFT);
81c52c56 123 if (is_error_noslot_pfn(pfn)) {
fcd95807 124 gfn += 1;
62c476c7 125 continue;
fcd95807 126 }
62c476c7 127
fcd95807
JR
128 /* Map into IO address space */
129 r = iommu_map(domain, gfn_to_gpa(gfn), pfn_to_hpa(pfn),
7d3002cc 130 page_size, flags);
e5fcfc82 131 if (r) {
260782bc 132 printk(KERN_ERR "kvm_iommu_map_address:"
5689cc53 133 "iommu failed to map pfn=%llx\n", pfn);
3ea61129 134 kvm_unpin_pages(kvm, pfn, page_size >> PAGE_SHIFT);
62c476c7
BAY
135 goto unmap_pages;
136 }
fcd95807
JR
137
138 gfn += page_size >> PAGE_SHIFT;
139
140
62c476c7 141 }
fcd95807 142
62c476c7
BAY
143 return 0;
144
145unmap_pages:
6e0db2f1 146 kvm_iommu_put_pages(kvm, slot->base_gfn, gfn - slot->base_gfn);
62c476c7
BAY
147 return r;
148}
149
150static int kvm_iommu_map_memslots(struct kvm *kvm)
151{
be6ba0f0 152 int idx, r = 0;
46a26bf5 153 struct kvm_memslots *slots;
be6ba0f0 154 struct kvm_memory_slot *memslot;
62c476c7 155
95c87e2b 156 idx = srcu_read_lock(&kvm->srcu);
90d83dc3 157 slots = kvm_memslots(kvm);
46a26bf5 158
be6ba0f0
XG
159 kvm_for_each_memslot(memslot, slots) {
160 r = kvm_iommu_map_pages(kvm, memslot);
62c476c7
BAY
161 if (r)
162 break;
163 }
95c87e2b 164 srcu_read_unlock(&kvm->srcu, idx);
682edb4c 165
62c476c7
BAY
166 return r;
167}
168
260782bc
WH
169int kvm_assign_device(struct kvm *kvm,
170 struct kvm_assigned_dev_kernel *assigned_dev)
62c476c7
BAY
171{
172 struct pci_dev *pdev = NULL;
19de40a8 173 struct iommu_domain *domain = kvm->arch.iommu_domain;
522c68c4 174 int r, last_flags;
62c476c7 175
260782bc
WH
176 /* check if iommu exists and in use */
177 if (!domain)
178 return 0;
179
180 pdev = assigned_dev->dev;
181 if (pdev == NULL)
62c476c7 182 return -ENODEV;
260782bc 183
19de40a8 184 r = iommu_attach_device(domain, &pdev->dev);
260782bc 185 if (r) {
d151f63f 186 dev_err(&pdev->dev, "kvm assign device failed ret %d", r);
260782bc 187 return r;
62c476c7
BAY
188 }
189
522c68c4
SY
190 last_flags = kvm->arch.iommu_flags;
191 if (iommu_domain_has_cap(kvm->arch.iommu_domain,
192 IOMMU_CAP_CACHE_COHERENCY))
193 kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
194
195 /* Check if need to update IOMMU page table for guest memory */
196 if ((last_flags ^ kvm->arch.iommu_flags) ==
197 KVM_IOMMU_CACHE_COHERENCY) {
198 kvm_iommu_unmap_memslots(kvm);
199 r = kvm_iommu_map_memslots(kvm);
200 if (r)
201 goto out_unmap;
202 }
203
6777829c
GR
204 pdev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
205
ab9f4ecb
ZE
206 printk(KERN_DEBUG "assign device %x:%x:%x.%x\n",
207 assigned_dev->host_segnr,
260782bc
WH
208 assigned_dev->host_busnr,
209 PCI_SLOT(assigned_dev->host_devfn),
210 PCI_FUNC(assigned_dev->host_devfn));
62c476c7 211
260782bc 212 return 0;
522c68c4
SY
213out_unmap:
214 kvm_iommu_unmap_memslots(kvm);
215 return r;
260782bc 216}
62c476c7 217
0a920356
WH
218int kvm_deassign_device(struct kvm *kvm,
219 struct kvm_assigned_dev_kernel *assigned_dev)
220{
19de40a8 221 struct iommu_domain *domain = kvm->arch.iommu_domain;
0a920356
WH
222 struct pci_dev *pdev = NULL;
223
224 /* check if iommu exists and in use */
225 if (!domain)
226 return 0;
227
228 pdev = assigned_dev->dev;
229 if (pdev == NULL)
230 return -ENODEV;
231
19de40a8 232 iommu_detach_device(domain, &pdev->dev);
0a920356 233
6777829c
GR
234 pdev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
235
ab9f4ecb
ZE
236 printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n",
237 assigned_dev->host_segnr,
0a920356
WH
238 assigned_dev->host_busnr,
239 PCI_SLOT(assigned_dev->host_devfn),
240 PCI_FUNC(assigned_dev->host_devfn));
241
242 return 0;
243}
244
260782bc
WH
245int kvm_iommu_map_guest(struct kvm *kvm)
246{
247 int r;
248
a1b60c1c 249 if (!iommu_present(&pci_bus_type)) {
19de40a8 250 printk(KERN_ERR "%s: iommu not found\n", __func__);
62c476c7
BAY
251 return -ENODEV;
252 }
253
21a1416a
AW
254 mutex_lock(&kvm->slots_lock);
255
905d66c1 256 kvm->arch.iommu_domain = iommu_domain_alloc(&pci_bus_type);
21a1416a
AW
257 if (!kvm->arch.iommu_domain) {
258 r = -ENOMEM;
259 goto out_unlock;
260 }
62c476c7 261
3f68b031
AW
262 if (!allow_unsafe_assigned_interrupts &&
263 !iommu_domain_has_cap(kvm->arch.iommu_domain,
264 IOMMU_CAP_INTR_REMAP)) {
265 printk(KERN_WARNING "%s: No interrupt remapping support,"
266 " disallowing device assignment."
267 " Re-enble with \"allow_unsafe_assigned_interrupts=1\""
268 " module option.\n", __func__);
269 iommu_domain_free(kvm->arch.iommu_domain);
270 kvm->arch.iommu_domain = NULL;
21a1416a
AW
271 r = -EPERM;
272 goto out_unlock;
3f68b031
AW
273 }
274
62c476c7
BAY
275 r = kvm_iommu_map_memslots(kvm);
276 if (r)
21a1416a 277 kvm_iommu_unmap_memslots(kvm);
62c476c7 278
21a1416a
AW
279out_unlock:
280 mutex_unlock(&kvm->slots_lock);
62c476c7
BAY
281 return r;
282}
283
284static void kvm_iommu_put_pages(struct kvm *kvm,
260782bc 285 gfn_t base_gfn, unsigned long npages)
62c476c7 286{
fcd95807
JR
287 struct iommu_domain *domain;
288 gfn_t end_gfn, gfn;
62c476c7 289 pfn_t pfn;
260782bc
WH
290 u64 phys;
291
fcd95807
JR
292 domain = kvm->arch.iommu_domain;
293 end_gfn = base_gfn + npages;
294 gfn = base_gfn;
295
260782bc
WH
296 /* check if iommu exists and in use */
297 if (!domain)
298 return;
62c476c7 299
fcd95807
JR
300 while (gfn < end_gfn) {
301 unsigned long unmap_pages;
7d3002cc 302 size_t size;
fcd95807
JR
303
304 /* Get physical address */
19de40a8 305 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
16b854c8
XG
306
307 if (!phys) {
308 gfn++;
309 continue;
310 }
311
fcd95807
JR
312 pfn = phys >> PAGE_SHIFT;
313
314 /* Unmap address from IO address space */
7d3002cc
OBC
315 size = iommu_unmap(domain, gfn_to_gpa(gfn), PAGE_SIZE);
316 unmap_pages = 1ULL << get_order(size);
260782bc 317
fcd95807
JR
318 /* Unpin all pages we just unmapped to not leak any memory */
319 kvm_unpin_pages(kvm, pfn, unmap_pages);
320
321 gfn += unmap_pages;
322 }
62c476c7
BAY
323}
324
32f6daad
AW
325void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
326{
327 kvm_iommu_put_pages(kvm, slot->base_gfn, slot->npages);
328}
329
62c476c7
BAY
330static int kvm_iommu_unmap_memslots(struct kvm *kvm)
331{
be6ba0f0 332 int idx;
46a26bf5 333 struct kvm_memslots *slots;
be6ba0f0 334 struct kvm_memory_slot *memslot;
46a26bf5 335
95c87e2b 336 idx = srcu_read_lock(&kvm->srcu);
90d83dc3 337 slots = kvm_memslots(kvm);
682edb4c 338
be6ba0f0 339 kvm_for_each_memslot(memslot, slots)
32f6daad 340 kvm_iommu_unmap_pages(kvm, memslot);
be6ba0f0 341
95c87e2b 342 srcu_read_unlock(&kvm->srcu, idx);
62c476c7
BAY
343
344 return 0;
345}
346
347int kvm_iommu_unmap_guest(struct kvm *kvm)
348{
19de40a8 349 struct iommu_domain *domain = kvm->arch.iommu_domain;
62c476c7
BAY
350
351 /* check if iommu exists and in use */
352 if (!domain)
353 return 0;
354
21a1416a 355 mutex_lock(&kvm->slots_lock);
62c476c7 356 kvm_iommu_unmap_memslots(kvm);
21a1416a
AW
357 kvm->arch.iommu_domain = NULL;
358 mutex_unlock(&kvm->slots_lock);
359
19de40a8 360 iommu_domain_free(domain);
62c476c7
BAY
361 return 0;
362}