Commit 95c87e2b4460a488ec7ce42f273893e410ab385a

Authored by Sheng Yang
Committed by Avi Kivity
1 parent fef093bec0

KVM: Fix IOMMU memslot reference warning

This patch fixes the following warning.

===================================================
[ INFO: suspicious rcu_dereference_check() usage. ]
---------------------------------------------------
include/linux/kvm_host.h:259 invoked rcu_dereference_check() without
protection!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 0
no locks held by qemu-system-x86/29679.

stack backtrace:
Pid: 29679, comm: qemu-system-x86 Not tainted 2.6.35-rc3+ #200
Call Trace:
 [<ffffffff810a224e>] lockdep_rcu_dereference+0xa8/0xb1
 [<ffffffffa018a06f>] kvm_iommu_unmap_memslots+0xc9/0xde [kvm]
 [<ffffffffa018a0c4>] kvm_iommu_unmap_guest+0x40/0x4e [kvm]
 [<ffffffffa018f772>] kvm_arch_destroy_vm+0x1a/0x186 [kvm]
 [<ffffffffa01800d0>] kvm_put_kvm+0x110/0x167 [kvm]
 [<ffffffffa0180ecc>] kvm_vcpu_release+0x18/0x1c [kvm]
 [<ffffffff81156f5d>] fput+0x22a/0x3a0
 [<ffffffff81152288>] filp_close+0xb4/0xcd
 [<ffffffff8106599f>] put_files_struct+0x1b7/0x36b
 [<ffffffff81065830>] ? put_files_struct+0x48/0x36b
 [<ffffffff8131ee59>] ? do_raw_spin_unlock+0x118/0x160
 [<ffffffff81065bc0>] exit_files+0x6d/0x75
 [<ffffffff81068348>] do_exit+0x47d/0xc60
 [<ffffffff8177e7b5>] ? _raw_spin_unlock_irq+0x30/0x36
 [<ffffffff81068bfa>] do_group_exit+0xcf/0x134
 [<ffffffff81080790>] get_signal_to_deliver+0x732/0x81d
 [<ffffffff81095996>] ? cpu_clock+0x4e/0x60
 [<ffffffff81002082>] do_notify_resume+0x117/0xc43
 [<ffffffff810a2fa3>] ? trace_hardirqs_on+0xd/0xf
 [<ffffffff81080d79>] ? sys_rt_sigtimedwait+0x2b5/0x3bf
 [<ffffffff8177d9f2>] ? trace_hardirqs_off_thunk+0x3a/0x3c
 [<ffffffff81003221>] ? sysret_signal+0x5/0x3d
 [<ffffffff8100343b>] int_signal+0x12/0x17

Signed-off-by: Sheng Yang <sheng@linux.intel.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Showing 1 changed file with 6 additions and 2 deletions Inline Diff

1 /* 1 /*
2 * Copyright (c) 2006, Intel Corporation. 2 * Copyright (c) 2006, Intel Corporation.
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify it 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, 5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation. 6 * version 2, as published by the Free Software Foundation.
7 * 7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT 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 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details. 11 * more details.
12 * 12 *
13 * You should have received a copy of the GNU General Public License along with 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 14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA. 15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 * 16 *
17 * Copyright (C) 2006-2008 Intel Corporation 17 * Copyright (C) 2006-2008 Intel Corporation
18 * Copyright IBM Corporation, 2008 18 * Copyright IBM Corporation, 2008
19 * Copyright 2010 Red Hat, Inc. and/or its affiliates. 19 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
20 * 20 *
21 * Author: Allen M. Kay <allen.m.kay@intel.com> 21 * Author: Allen M. Kay <allen.m.kay@intel.com>
22 * Author: Weidong Han <weidong.han@intel.com> 22 * Author: Weidong Han <weidong.han@intel.com>
23 * Author: Ben-Ami Yassour <benami@il.ibm.com> 23 * Author: Ben-Ami Yassour <benami@il.ibm.com>
24 */ 24 */
25 25
26 #include <linux/list.h> 26 #include <linux/list.h>
27 #include <linux/kvm_host.h> 27 #include <linux/kvm_host.h>
28 #include <linux/pci.h> 28 #include <linux/pci.h>
29 #include <linux/dmar.h> 29 #include <linux/dmar.h>
30 #include <linux/iommu.h> 30 #include <linux/iommu.h>
31 #include <linux/intel-iommu.h> 31 #include <linux/intel-iommu.h>
32 32
33 static int kvm_iommu_unmap_memslots(struct kvm *kvm); 33 static int kvm_iommu_unmap_memslots(struct kvm *kvm);
34 static void kvm_iommu_put_pages(struct kvm *kvm, 34 static void kvm_iommu_put_pages(struct kvm *kvm,
35 gfn_t base_gfn, unsigned long npages); 35 gfn_t base_gfn, unsigned long npages);
36 36
37 static pfn_t kvm_pin_pages(struct kvm *kvm, struct kvm_memory_slot *slot, 37 static pfn_t kvm_pin_pages(struct kvm *kvm, struct kvm_memory_slot *slot,
38 gfn_t gfn, unsigned long size) 38 gfn_t gfn, unsigned long size)
39 { 39 {
40 gfn_t end_gfn; 40 gfn_t end_gfn;
41 pfn_t pfn; 41 pfn_t pfn;
42 42
43 pfn = gfn_to_pfn_memslot(kvm, slot, gfn); 43 pfn = gfn_to_pfn_memslot(kvm, slot, gfn);
44 end_gfn = gfn + (size >> PAGE_SHIFT); 44 end_gfn = gfn + (size >> PAGE_SHIFT);
45 gfn += 1; 45 gfn += 1;
46 46
47 if (is_error_pfn(pfn)) 47 if (is_error_pfn(pfn))
48 return pfn; 48 return pfn;
49 49
50 while (gfn < end_gfn) 50 while (gfn < end_gfn)
51 gfn_to_pfn_memslot(kvm, slot, gfn++); 51 gfn_to_pfn_memslot(kvm, slot, gfn++);
52 52
53 return pfn; 53 return pfn;
54 } 54 }
55 55
56 int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot) 56 int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
57 { 57 {
58 gfn_t gfn, end_gfn; 58 gfn_t gfn, end_gfn;
59 pfn_t pfn; 59 pfn_t pfn;
60 int r = 0; 60 int r = 0;
61 struct iommu_domain *domain = kvm->arch.iommu_domain; 61 struct iommu_domain *domain = kvm->arch.iommu_domain;
62 int flags; 62 int flags;
63 63
64 /* check if iommu exists and in use */ 64 /* check if iommu exists and in use */
65 if (!domain) 65 if (!domain)
66 return 0; 66 return 0;
67 67
68 gfn = slot->base_gfn; 68 gfn = slot->base_gfn;
69 end_gfn = gfn + slot->npages; 69 end_gfn = gfn + slot->npages;
70 70
71 flags = IOMMU_READ | IOMMU_WRITE; 71 flags = IOMMU_READ | IOMMU_WRITE;
72 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY) 72 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
73 flags |= IOMMU_CACHE; 73 flags |= IOMMU_CACHE;
74 74
75 75
76 while (gfn < end_gfn) { 76 while (gfn < end_gfn) {
77 unsigned long page_size; 77 unsigned long page_size;
78 78
79 /* Check if already mapped */ 79 /* Check if already mapped */
80 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) { 80 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) {
81 gfn += 1; 81 gfn += 1;
82 continue; 82 continue;
83 } 83 }
84 84
85 /* Get the page size we could use to map */ 85 /* Get the page size we could use to map */
86 page_size = kvm_host_page_size(kvm, gfn); 86 page_size = kvm_host_page_size(kvm, gfn);
87 87
88 /* Make sure the page_size does not exceed the memslot */ 88 /* Make sure the page_size does not exceed the memslot */
89 while ((gfn + (page_size >> PAGE_SHIFT)) > end_gfn) 89 while ((gfn + (page_size >> PAGE_SHIFT)) > end_gfn)
90 page_size >>= 1; 90 page_size >>= 1;
91 91
92 /* Make sure gfn is aligned to the page size we want to map */ 92 /* Make sure gfn is aligned to the page size we want to map */
93 while ((gfn << PAGE_SHIFT) & (page_size - 1)) 93 while ((gfn << PAGE_SHIFT) & (page_size - 1))
94 page_size >>= 1; 94 page_size >>= 1;
95 95
96 /* 96 /*
97 * Pin all pages we are about to map in memory. This is 97 * Pin all pages we are about to map in memory. This is
98 * important because we unmap and unpin in 4kb steps later. 98 * important because we unmap and unpin in 4kb steps later.
99 */ 99 */
100 pfn = kvm_pin_pages(kvm, slot, gfn, page_size); 100 pfn = kvm_pin_pages(kvm, slot, gfn, page_size);
101 if (is_error_pfn(pfn)) { 101 if (is_error_pfn(pfn)) {
102 gfn += 1; 102 gfn += 1;
103 continue; 103 continue;
104 } 104 }
105 105
106 /* Map into IO address space */ 106 /* Map into IO address space */
107 r = iommu_map(domain, gfn_to_gpa(gfn), pfn_to_hpa(pfn), 107 r = iommu_map(domain, gfn_to_gpa(gfn), pfn_to_hpa(pfn),
108 get_order(page_size), flags); 108 get_order(page_size), flags);
109 if (r) { 109 if (r) {
110 printk(KERN_ERR "kvm_iommu_map_address:" 110 printk(KERN_ERR "kvm_iommu_map_address:"
111 "iommu failed to map pfn=%lx\n", pfn); 111 "iommu failed to map pfn=%lx\n", pfn);
112 goto unmap_pages; 112 goto unmap_pages;
113 } 113 }
114 114
115 gfn += page_size >> PAGE_SHIFT; 115 gfn += page_size >> PAGE_SHIFT;
116 116
117 117
118 } 118 }
119 119
120 return 0; 120 return 0;
121 121
122 unmap_pages: 122 unmap_pages:
123 kvm_iommu_put_pages(kvm, slot->base_gfn, gfn); 123 kvm_iommu_put_pages(kvm, slot->base_gfn, gfn);
124 return r; 124 return r;
125 } 125 }
126 126
127 static int kvm_iommu_map_memslots(struct kvm *kvm) 127 static int kvm_iommu_map_memslots(struct kvm *kvm)
128 { 128 {
129 int i, r = 0; 129 int i, idx, r = 0;
130 struct kvm_memslots *slots; 130 struct kvm_memslots *slots;
131 131
132 idx = srcu_read_lock(&kvm->srcu);
132 slots = kvm_memslots(kvm); 133 slots = kvm_memslots(kvm);
133 134
134 for (i = 0; i < slots->nmemslots; i++) { 135 for (i = 0; i < slots->nmemslots; i++) {
135 r = kvm_iommu_map_pages(kvm, &slots->memslots[i]); 136 r = kvm_iommu_map_pages(kvm, &slots->memslots[i]);
136 if (r) 137 if (r)
137 break; 138 break;
138 } 139 }
140 srcu_read_unlock(&kvm->srcu, idx);
139 141
140 return r; 142 return r;
141 } 143 }
142 144
143 int kvm_assign_device(struct kvm *kvm, 145 int kvm_assign_device(struct kvm *kvm,
144 struct kvm_assigned_dev_kernel *assigned_dev) 146 struct kvm_assigned_dev_kernel *assigned_dev)
145 { 147 {
146 struct pci_dev *pdev = NULL; 148 struct pci_dev *pdev = NULL;
147 struct iommu_domain *domain = kvm->arch.iommu_domain; 149 struct iommu_domain *domain = kvm->arch.iommu_domain;
148 int r, last_flags; 150 int r, last_flags;
149 151
150 /* check if iommu exists and in use */ 152 /* check if iommu exists and in use */
151 if (!domain) 153 if (!domain)
152 return 0; 154 return 0;
153 155
154 pdev = assigned_dev->dev; 156 pdev = assigned_dev->dev;
155 if (pdev == NULL) 157 if (pdev == NULL)
156 return -ENODEV; 158 return -ENODEV;
157 159
158 r = iommu_attach_device(domain, &pdev->dev); 160 r = iommu_attach_device(domain, &pdev->dev);
159 if (r) { 161 if (r) {
160 printk(KERN_ERR "assign device %x:%x:%x.%x failed", 162 printk(KERN_ERR "assign device %x:%x:%x.%x failed",
161 pci_domain_nr(pdev->bus), 163 pci_domain_nr(pdev->bus),
162 pdev->bus->number, 164 pdev->bus->number,
163 PCI_SLOT(pdev->devfn), 165 PCI_SLOT(pdev->devfn),
164 PCI_FUNC(pdev->devfn)); 166 PCI_FUNC(pdev->devfn));
165 return r; 167 return r;
166 } 168 }
167 169
168 last_flags = kvm->arch.iommu_flags; 170 last_flags = kvm->arch.iommu_flags;
169 if (iommu_domain_has_cap(kvm->arch.iommu_domain, 171 if (iommu_domain_has_cap(kvm->arch.iommu_domain,
170 IOMMU_CAP_CACHE_COHERENCY)) 172 IOMMU_CAP_CACHE_COHERENCY))
171 kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY; 173 kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
172 174
173 /* Check if need to update IOMMU page table for guest memory */ 175 /* Check if need to update IOMMU page table for guest memory */
174 if ((last_flags ^ kvm->arch.iommu_flags) == 176 if ((last_flags ^ kvm->arch.iommu_flags) ==
175 KVM_IOMMU_CACHE_COHERENCY) { 177 KVM_IOMMU_CACHE_COHERENCY) {
176 kvm_iommu_unmap_memslots(kvm); 178 kvm_iommu_unmap_memslots(kvm);
177 r = kvm_iommu_map_memslots(kvm); 179 r = kvm_iommu_map_memslots(kvm);
178 if (r) 180 if (r)
179 goto out_unmap; 181 goto out_unmap;
180 } 182 }
181 183
182 printk(KERN_DEBUG "assign device %x:%x:%x.%x\n", 184 printk(KERN_DEBUG "assign device %x:%x:%x.%x\n",
183 assigned_dev->host_segnr, 185 assigned_dev->host_segnr,
184 assigned_dev->host_busnr, 186 assigned_dev->host_busnr,
185 PCI_SLOT(assigned_dev->host_devfn), 187 PCI_SLOT(assigned_dev->host_devfn),
186 PCI_FUNC(assigned_dev->host_devfn)); 188 PCI_FUNC(assigned_dev->host_devfn));
187 189
188 return 0; 190 return 0;
189 out_unmap: 191 out_unmap:
190 kvm_iommu_unmap_memslots(kvm); 192 kvm_iommu_unmap_memslots(kvm);
191 return r; 193 return r;
192 } 194 }
193 195
194 int kvm_deassign_device(struct kvm *kvm, 196 int kvm_deassign_device(struct kvm *kvm,
195 struct kvm_assigned_dev_kernel *assigned_dev) 197 struct kvm_assigned_dev_kernel *assigned_dev)
196 { 198 {
197 struct iommu_domain *domain = kvm->arch.iommu_domain; 199 struct iommu_domain *domain = kvm->arch.iommu_domain;
198 struct pci_dev *pdev = NULL; 200 struct pci_dev *pdev = NULL;
199 201
200 /* check if iommu exists and in use */ 202 /* check if iommu exists and in use */
201 if (!domain) 203 if (!domain)
202 return 0; 204 return 0;
203 205
204 pdev = assigned_dev->dev; 206 pdev = assigned_dev->dev;
205 if (pdev == NULL) 207 if (pdev == NULL)
206 return -ENODEV; 208 return -ENODEV;
207 209
208 iommu_detach_device(domain, &pdev->dev); 210 iommu_detach_device(domain, &pdev->dev);
209 211
210 printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n", 212 printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n",
211 assigned_dev->host_segnr, 213 assigned_dev->host_segnr,
212 assigned_dev->host_busnr, 214 assigned_dev->host_busnr,
213 PCI_SLOT(assigned_dev->host_devfn), 215 PCI_SLOT(assigned_dev->host_devfn),
214 PCI_FUNC(assigned_dev->host_devfn)); 216 PCI_FUNC(assigned_dev->host_devfn));
215 217
216 return 0; 218 return 0;
217 } 219 }
218 220
219 int kvm_iommu_map_guest(struct kvm *kvm) 221 int kvm_iommu_map_guest(struct kvm *kvm)
220 { 222 {
221 int r; 223 int r;
222 224
223 if (!iommu_found()) { 225 if (!iommu_found()) {
224 printk(KERN_ERR "%s: iommu not found\n", __func__); 226 printk(KERN_ERR "%s: iommu not found\n", __func__);
225 return -ENODEV; 227 return -ENODEV;
226 } 228 }
227 229
228 kvm->arch.iommu_domain = iommu_domain_alloc(); 230 kvm->arch.iommu_domain = iommu_domain_alloc();
229 if (!kvm->arch.iommu_domain) 231 if (!kvm->arch.iommu_domain)
230 return -ENOMEM; 232 return -ENOMEM;
231 233
232 r = kvm_iommu_map_memslots(kvm); 234 r = kvm_iommu_map_memslots(kvm);
233 if (r) 235 if (r)
234 goto out_unmap; 236 goto out_unmap;
235 237
236 return 0; 238 return 0;
237 239
238 out_unmap: 240 out_unmap:
239 kvm_iommu_unmap_memslots(kvm); 241 kvm_iommu_unmap_memslots(kvm);
240 return r; 242 return r;
241 } 243 }
242 244
243 static void kvm_unpin_pages(struct kvm *kvm, pfn_t pfn, unsigned long npages) 245 static void kvm_unpin_pages(struct kvm *kvm, pfn_t pfn, unsigned long npages)
244 { 246 {
245 unsigned long i; 247 unsigned long i;
246 248
247 for (i = 0; i < npages; ++i) 249 for (i = 0; i < npages; ++i)
248 kvm_release_pfn_clean(pfn + i); 250 kvm_release_pfn_clean(pfn + i);
249 } 251 }
250 252
251 static void kvm_iommu_put_pages(struct kvm *kvm, 253 static void kvm_iommu_put_pages(struct kvm *kvm,
252 gfn_t base_gfn, unsigned long npages) 254 gfn_t base_gfn, unsigned long npages)
253 { 255 {
254 struct iommu_domain *domain; 256 struct iommu_domain *domain;
255 gfn_t end_gfn, gfn; 257 gfn_t end_gfn, gfn;
256 pfn_t pfn; 258 pfn_t pfn;
257 u64 phys; 259 u64 phys;
258 260
259 domain = kvm->arch.iommu_domain; 261 domain = kvm->arch.iommu_domain;
260 end_gfn = base_gfn + npages; 262 end_gfn = base_gfn + npages;
261 gfn = base_gfn; 263 gfn = base_gfn;
262 264
263 /* check if iommu exists and in use */ 265 /* check if iommu exists and in use */
264 if (!domain) 266 if (!domain)
265 return; 267 return;
266 268
267 while (gfn < end_gfn) { 269 while (gfn < end_gfn) {
268 unsigned long unmap_pages; 270 unsigned long unmap_pages;
269 int order; 271 int order;
270 272
271 /* Get physical address */ 273 /* Get physical address */
272 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn)); 274 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
273 pfn = phys >> PAGE_SHIFT; 275 pfn = phys >> PAGE_SHIFT;
274 276
275 /* Unmap address from IO address space */ 277 /* Unmap address from IO address space */
276 order = iommu_unmap(domain, gfn_to_gpa(gfn), 0); 278 order = iommu_unmap(domain, gfn_to_gpa(gfn), 0);
277 unmap_pages = 1ULL << order; 279 unmap_pages = 1ULL << order;
278 280
279 /* Unpin all pages we just unmapped to not leak any memory */ 281 /* Unpin all pages we just unmapped to not leak any memory */
280 kvm_unpin_pages(kvm, pfn, unmap_pages); 282 kvm_unpin_pages(kvm, pfn, unmap_pages);
281 283
282 gfn += unmap_pages; 284 gfn += unmap_pages;
283 } 285 }
284 } 286 }
285 287
286 static int kvm_iommu_unmap_memslots(struct kvm *kvm) 288 static int kvm_iommu_unmap_memslots(struct kvm *kvm)
287 { 289 {
288 int i; 290 int i, idx;
289 struct kvm_memslots *slots; 291 struct kvm_memslots *slots;
290 292
293 idx = srcu_read_lock(&kvm->srcu);
291 slots = kvm_memslots(kvm); 294 slots = kvm_memslots(kvm);
292 295
293 for (i = 0; i < slots->nmemslots; i++) { 296 for (i = 0; i < slots->nmemslots; i++) {
294 kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn, 297 kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn,
295 slots->memslots[i].npages); 298 slots->memslots[i].npages);
296 } 299 }
300 srcu_read_unlock(&kvm->srcu, idx);
297 301
298 return 0; 302 return 0;
299 } 303 }
300 304
301 int kvm_iommu_unmap_guest(struct kvm *kvm) 305 int kvm_iommu_unmap_guest(struct kvm *kvm)
302 { 306 {
303 struct iommu_domain *domain = kvm->arch.iommu_domain; 307 struct iommu_domain *domain = kvm->arch.iommu_domain;
304 308
305 /* check if iommu exists and in use */ 309 /* check if iommu exists and in use */
306 if (!domain) 310 if (!domain)
307 return 0; 311 return 0;
308 312
309 kvm_iommu_unmap_memslots(kvm); 313 kvm_iommu_unmap_memslots(kvm);
310 iommu_domain_free(domain); 314 iommu_domain_free(domain);
311 return 0; 315 return 0;
312 } 316 }
313 317