aboutsummaryrefslogtreecommitdiffstats
path: root/virt
diff options
context:
space:
mode:
Diffstat (limited to 'virt')
-rw-r--r--virt/kvm/assigned-dev.c8
-rw-r--r--virt/kvm/coalesced_mmio.c6
-rw-r--r--virt/kvm/ioapic.c30
-rw-r--r--virt/kvm/ioapic.h2
-rw-r--r--virt/kvm/iommu.c117
-rw-r--r--virt/kvm/kvm_main.c70
6 files changed, 157 insertions, 76 deletions
diff --git a/virt/kvm/assigned-dev.c b/virt/kvm/assigned-dev.c
index 02ff2b19dbe2..4d10b1e047f4 100644
--- a/virt/kvm/assigned-dev.c
+++ b/virt/kvm/assigned-dev.c
@@ -316,12 +316,16 @@ static int assigned_device_enable_host_msix(struct kvm *kvm,
316 kvm_assigned_dev_intr, 0, 316 kvm_assigned_dev_intr, 0,
317 "kvm_assigned_msix_device", 317 "kvm_assigned_msix_device",
318 (void *)dev); 318 (void *)dev);
319 /* FIXME: free requested_irq's on failure */
320 if (r) 319 if (r)
321 return r; 320 goto err;
322 } 321 }
323 322
324 return 0; 323 return 0;
324err:
325 for (i -= 1; i >= 0; i--)
326 free_irq(dev->host_msix_entries[i].vector, (void *)dev);
327 pci_disable_msix(dev->dev);
328 return r;
325} 329}
326 330
327#endif 331#endif
diff --git a/virt/kvm/coalesced_mmio.c b/virt/kvm/coalesced_mmio.c
index 36e258029649..53850177163f 100644
--- a/virt/kvm/coalesced_mmio.c
+++ b/virt/kvm/coalesced_mmio.c
@@ -120,8 +120,10 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
120 return ret; 120 return ret;
121 121
122out_free_dev: 122out_free_dev:
123 kvm->coalesced_mmio_dev = NULL;
123 kfree(dev); 124 kfree(dev);
124out_free_page: 125out_free_page:
126 kvm->coalesced_mmio_ring = NULL;
125 __free_page(page); 127 __free_page(page);
126out_err: 128out_err:
127 return ret; 129 return ret;
@@ -139,7 +141,7 @@ int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
139 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev; 141 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
140 142
141 if (dev == NULL) 143 if (dev == NULL)
142 return -EINVAL; 144 return -ENXIO;
143 145
144 mutex_lock(&kvm->slots_lock); 146 mutex_lock(&kvm->slots_lock);
145 if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) { 147 if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
@@ -162,7 +164,7 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
162 struct kvm_coalesced_mmio_zone *z; 164 struct kvm_coalesced_mmio_zone *z;
163 165
164 if (dev == NULL) 166 if (dev == NULL)
165 return -EINVAL; 167 return -ENXIO;
166 168
167 mutex_lock(&kvm->slots_lock); 169 mutex_lock(&kvm->slots_lock);
168 170
diff --git a/virt/kvm/ioapic.c b/virt/kvm/ioapic.c
index 03a5eb22da2b..7c79c1d76d0c 100644
--- a/virt/kvm/ioapic.c
+++ b/virt/kvm/ioapic.c
@@ -197,7 +197,7 @@ int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
197 union kvm_ioapic_redirect_entry entry; 197 union kvm_ioapic_redirect_entry entry;
198 int ret = 1; 198 int ret = 1;
199 199
200 mutex_lock(&ioapic->lock); 200 spin_lock(&ioapic->lock);
201 if (irq >= 0 && irq < IOAPIC_NUM_PINS) { 201 if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
202 entry = ioapic->redirtbl[irq]; 202 entry = ioapic->redirtbl[irq];
203 level ^= entry.fields.polarity; 203 level ^= entry.fields.polarity;
@@ -214,7 +214,7 @@ int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
214 } 214 }
215 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0); 215 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
216 } 216 }
217 mutex_unlock(&ioapic->lock); 217 spin_unlock(&ioapic->lock);
218 218
219 return ret; 219 return ret;
220} 220}
@@ -238,9 +238,9 @@ static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
238 * is dropped it will be put into irr and will be delivered 238 * is dropped it will be put into irr and will be delivered
239 * after ack notifier returns. 239 * after ack notifier returns.
240 */ 240 */
241 mutex_unlock(&ioapic->lock); 241 spin_unlock(&ioapic->lock);
242 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i); 242 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
243 mutex_lock(&ioapic->lock); 243 spin_lock(&ioapic->lock);
244 244
245 if (trigger_mode != IOAPIC_LEVEL_TRIG) 245 if (trigger_mode != IOAPIC_LEVEL_TRIG)
246 continue; 246 continue;
@@ -259,9 +259,9 @@ void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
259 smp_rmb(); 259 smp_rmb();
260 if (!test_bit(vector, ioapic->handled_vectors)) 260 if (!test_bit(vector, ioapic->handled_vectors))
261 return; 261 return;
262 mutex_lock(&ioapic->lock); 262 spin_lock(&ioapic->lock);
263 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode); 263 __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
264 mutex_unlock(&ioapic->lock); 264 spin_unlock(&ioapic->lock);
265} 265}
266 266
267static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev) 267static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
@@ -287,7 +287,7 @@ static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
287 ASSERT(!(addr & 0xf)); /* check alignment */ 287 ASSERT(!(addr & 0xf)); /* check alignment */
288 288
289 addr &= 0xff; 289 addr &= 0xff;
290 mutex_lock(&ioapic->lock); 290 spin_lock(&ioapic->lock);
291 switch (addr) { 291 switch (addr) {
292 case IOAPIC_REG_SELECT: 292 case IOAPIC_REG_SELECT:
293 result = ioapic->ioregsel; 293 result = ioapic->ioregsel;
@@ -301,7 +301,7 @@ static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
301 result = 0; 301 result = 0;
302 break; 302 break;
303 } 303 }
304 mutex_unlock(&ioapic->lock); 304 spin_unlock(&ioapic->lock);
305 305
306 switch (len) { 306 switch (len) {
307 case 8: 307 case 8:
@@ -338,7 +338,7 @@ static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
338 } 338 }
339 339
340 addr &= 0xff; 340 addr &= 0xff;
341 mutex_lock(&ioapic->lock); 341 spin_lock(&ioapic->lock);
342 switch (addr) { 342 switch (addr) {
343 case IOAPIC_REG_SELECT: 343 case IOAPIC_REG_SELECT:
344 ioapic->ioregsel = data; 344 ioapic->ioregsel = data;
@@ -356,7 +356,7 @@ static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
356 default: 356 default:
357 break; 357 break;
358 } 358 }
359 mutex_unlock(&ioapic->lock); 359 spin_unlock(&ioapic->lock);
360 return 0; 360 return 0;
361} 361}
362 362
@@ -386,7 +386,7 @@ int kvm_ioapic_init(struct kvm *kvm)
386 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL); 386 ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
387 if (!ioapic) 387 if (!ioapic)
388 return -ENOMEM; 388 return -ENOMEM;
389 mutex_init(&ioapic->lock); 389 spin_lock_init(&ioapic->lock);
390 kvm->arch.vioapic = ioapic; 390 kvm->arch.vioapic = ioapic;
391 kvm_ioapic_reset(ioapic); 391 kvm_ioapic_reset(ioapic);
392 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops); 392 kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
@@ -419,9 +419,9 @@ int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
419 if (!ioapic) 419 if (!ioapic)
420 return -EINVAL; 420 return -EINVAL;
421 421
422 mutex_lock(&ioapic->lock); 422 spin_lock(&ioapic->lock);
423 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state)); 423 memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
424 mutex_unlock(&ioapic->lock); 424 spin_unlock(&ioapic->lock);
425 return 0; 425 return 0;
426} 426}
427 427
@@ -431,9 +431,9 @@ int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
431 if (!ioapic) 431 if (!ioapic)
432 return -EINVAL; 432 return -EINVAL;
433 433
434 mutex_lock(&ioapic->lock); 434 spin_lock(&ioapic->lock);
435 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state)); 435 memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
436 update_handled_vectors(ioapic); 436 update_handled_vectors(ioapic);
437 mutex_unlock(&ioapic->lock); 437 spin_unlock(&ioapic->lock);
438 return 0; 438 return 0;
439} 439}
diff --git a/virt/kvm/ioapic.h b/virt/kvm/ioapic.h
index 8a751b78a430..0b190c34ccc3 100644
--- a/virt/kvm/ioapic.h
+++ b/virt/kvm/ioapic.h
@@ -45,7 +45,7 @@ struct kvm_ioapic {
45 struct kvm_io_device dev; 45 struct kvm_io_device dev;
46 struct kvm *kvm; 46 struct kvm *kvm;
47 void (*ack_notifier)(void *opaque, int irq); 47 void (*ack_notifier)(void *opaque, int irq);
48 struct mutex lock; 48 spinlock_t lock;
49 DECLARE_BITMAP(handled_vectors, 256); 49 DECLARE_BITMAP(handled_vectors, 256);
50}; 50};
51 51
diff --git a/virt/kvm/iommu.c b/virt/kvm/iommu.c
index 80fd3ad3b2de..d2f06be63354 100644
--- a/virt/kvm/iommu.c
+++ b/virt/kvm/iommu.c
@@ -32,12 +32,30 @@ static int kvm_iommu_unmap_memslots(struct kvm *kvm);
32static void kvm_iommu_put_pages(struct kvm *kvm, 32static void kvm_iommu_put_pages(struct kvm *kvm,
33 gfn_t base_gfn, unsigned long npages); 33 gfn_t base_gfn, unsigned long npages);
34 34
35static pfn_t kvm_pin_pages(struct kvm *kvm, struct kvm_memory_slot *slot,
36 gfn_t gfn, unsigned long size)
37{
38 gfn_t end_gfn;
39 pfn_t pfn;
40
41 pfn = gfn_to_pfn_memslot(kvm, slot, gfn);
42 end_gfn = gfn + (size >> PAGE_SHIFT);
43 gfn += 1;
44
45 if (is_error_pfn(pfn))
46 return pfn;
47
48 while (gfn < end_gfn)
49 gfn_to_pfn_memslot(kvm, slot, gfn++);
50
51 return pfn;
52}
53
35int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot) 54int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
36{ 55{
37 gfn_t gfn = slot->base_gfn; 56 gfn_t gfn, end_gfn;
38 unsigned long npages = slot->npages;
39 pfn_t pfn; 57 pfn_t pfn;
40 int i, r = 0; 58 int r = 0;
41 struct iommu_domain *domain = kvm->arch.iommu_domain; 59 struct iommu_domain *domain = kvm->arch.iommu_domain;
42 int flags; 60 int flags;
43 61
@@ -45,31 +63,62 @@ int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
45 if (!domain) 63 if (!domain)
46 return 0; 64 return 0;
47 65
66 gfn = slot->base_gfn;
67 end_gfn = gfn + slot->npages;
68
48 flags = IOMMU_READ | IOMMU_WRITE; 69 flags = IOMMU_READ | IOMMU_WRITE;
49 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY) 70 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
50 flags |= IOMMU_CACHE; 71 flags |= IOMMU_CACHE;
51 72
52 for (i = 0; i < npages; i++) { 73
53 /* check if already mapped */ 74 while (gfn < end_gfn) {
54 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) 75 unsigned long page_size;
76
77 /* Check if already mapped */
78 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) {
79 gfn += 1;
80 continue;
81 }
82
83 /* Get the page size we could use to map */
84 page_size = kvm_host_page_size(kvm, gfn);
85
86 /* Make sure the page_size does not exceed the memslot */
87 while ((gfn + (page_size >> PAGE_SHIFT)) > end_gfn)
88 page_size >>= 1;
89
90 /* Make sure gfn is aligned to the page size we want to map */
91 while ((gfn << PAGE_SHIFT) & (page_size - 1))
92 page_size >>= 1;
93
94 /*
95 * Pin all pages we are about to map in memory. This is
96 * important because we unmap and unpin in 4kb steps later.
97 */
98 pfn = kvm_pin_pages(kvm, slot, gfn, page_size);
99 if (is_error_pfn(pfn)) {
100 gfn += 1;
55 continue; 101 continue;
102 }
56 103
57 pfn = gfn_to_pfn_memslot(kvm, slot, gfn); 104 /* Map into IO address space */
58 r = iommu_map_range(domain, 105 r = iommu_map(domain, gfn_to_gpa(gfn), pfn_to_hpa(pfn),
59 gfn_to_gpa(gfn), 106 get_order(page_size), flags);
60 pfn_to_hpa(pfn),
61 PAGE_SIZE, flags);
62 if (r) { 107 if (r) {
63 printk(KERN_ERR "kvm_iommu_map_address:" 108 printk(KERN_ERR "kvm_iommu_map_address:"
64 "iommu failed to map pfn=%lx\n", pfn); 109 "iommu failed to map pfn=%lx\n", pfn);
65 goto unmap_pages; 110 goto unmap_pages;
66 } 111 }
67 gfn++; 112
113 gfn += page_size >> PAGE_SHIFT;
114
115
68 } 116 }
117
69 return 0; 118 return 0;
70 119
71unmap_pages: 120unmap_pages:
72 kvm_iommu_put_pages(kvm, slot->base_gfn, i); 121 kvm_iommu_put_pages(kvm, slot->base_gfn, gfn);
73 return r; 122 return r;
74} 123}
75 124
@@ -78,7 +127,7 @@ static int kvm_iommu_map_memslots(struct kvm *kvm)
78 int i, r = 0; 127 int i, r = 0;
79 struct kvm_memslots *slots; 128 struct kvm_memslots *slots;
80 129
81 slots = rcu_dereference(kvm->memslots); 130 slots = kvm_memslots(kvm);
82 131
83 for (i = 0; i < slots->nmemslots; i++) { 132 for (i = 0; i < slots->nmemslots; i++) {
84 r = kvm_iommu_map_pages(kvm, &slots->memslots[i]); 133 r = kvm_iommu_map_pages(kvm, &slots->memslots[i]);
@@ -189,27 +238,47 @@ out_unmap:
189 return r; 238 return r;
190} 239}
191 240
241static void kvm_unpin_pages(struct kvm *kvm, pfn_t pfn, unsigned long npages)
242{
243 unsigned long i;
244
245 for (i = 0; i < npages; ++i)
246 kvm_release_pfn_clean(pfn + i);
247}
248
192static void kvm_iommu_put_pages(struct kvm *kvm, 249static void kvm_iommu_put_pages(struct kvm *kvm,
193 gfn_t base_gfn, unsigned long npages) 250 gfn_t base_gfn, unsigned long npages)
194{ 251{
195 gfn_t gfn = base_gfn; 252 struct iommu_domain *domain;
253 gfn_t end_gfn, gfn;
196 pfn_t pfn; 254 pfn_t pfn;
197 struct iommu_domain *domain = kvm->arch.iommu_domain;
198 unsigned long i;
199 u64 phys; 255 u64 phys;
200 256
257 domain = kvm->arch.iommu_domain;
258 end_gfn = base_gfn + npages;
259 gfn = base_gfn;
260
201 /* check if iommu exists and in use */ 261 /* check if iommu exists and in use */
202 if (!domain) 262 if (!domain)
203 return; 263 return;
204 264
205 for (i = 0; i < npages; i++) { 265 while (gfn < end_gfn) {
266 unsigned long unmap_pages;
267 int order;
268
269 /* Get physical address */
206 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn)); 270 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
207 pfn = phys >> PAGE_SHIFT; 271 pfn = phys >> PAGE_SHIFT;
208 kvm_release_pfn_clean(pfn); 272
209 gfn++; 273 /* Unmap address from IO address space */
210 } 274 order = iommu_unmap(domain, gfn_to_gpa(gfn), PAGE_SIZE);
275 unmap_pages = 1ULL << order;
211 276
212 iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages); 277 /* Unpin all pages we just unmapped to not leak any memory */
278 kvm_unpin_pages(kvm, pfn, unmap_pages);
279
280 gfn += unmap_pages;
281 }
213} 282}
214 283
215static int kvm_iommu_unmap_memslots(struct kvm *kvm) 284static int kvm_iommu_unmap_memslots(struct kvm *kvm)
@@ -217,7 +286,7 @@ static int kvm_iommu_unmap_memslots(struct kvm *kvm)
217 int i; 286 int i;
218 struct kvm_memslots *slots; 287 struct kvm_memslots *slots;
219 288
220 slots = rcu_dereference(kvm->memslots); 289 slots = kvm_memslots(kvm);
221 290
222 for (i = 0; i < slots->nmemslots; i++) { 291 for (i = 0; i < slots->nmemslots; i++) {
223 kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn, 292 kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn,
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 5a0cd194dce0..f032806a212f 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -341,7 +341,11 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
341 struct mm_struct *mm) 341 struct mm_struct *mm)
342{ 342{
343 struct kvm *kvm = mmu_notifier_to_kvm(mn); 343 struct kvm *kvm = mmu_notifier_to_kvm(mn);
344 int idx;
345
346 idx = srcu_read_lock(&kvm->srcu);
344 kvm_arch_flush_shadow(kvm); 347 kvm_arch_flush_shadow(kvm);
348 srcu_read_unlock(&kvm->srcu, idx);
345} 349}
346 350
347static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { 351static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
@@ -418,9 +422,6 @@ static struct kvm *kvm_create_vm(void)
418 spin_lock(&kvm_lock); 422 spin_lock(&kvm_lock);
419 list_add(&kvm->vm_list, &vm_list); 423 list_add(&kvm->vm_list, &vm_list);
420 spin_unlock(&kvm_lock); 424 spin_unlock(&kvm_lock);
421#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
422 kvm_coalesced_mmio_init(kvm);
423#endif
424out: 425out:
425 return kvm; 426 return kvm;
426 427
@@ -556,6 +557,10 @@ int __kvm_set_memory_region(struct kvm *kvm,
556 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; 557 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
557 npages = mem->memory_size >> PAGE_SHIFT; 558 npages = mem->memory_size >> PAGE_SHIFT;
558 559
560 r = -EINVAL;
561 if (npages > KVM_MEM_MAX_NR_PAGES)
562 goto out;
563
559 if (!npages) 564 if (!npages)
560 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; 565 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
561 566
@@ -648,7 +653,7 @@ skip_lpage:
648 653
649 /* Allocate page dirty bitmap if needed */ 654 /* Allocate page dirty bitmap if needed */
650 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { 655 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
651 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; 656 unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(&new);
652 657
653 new.dirty_bitmap = vmalloc(dirty_bytes); 658 new.dirty_bitmap = vmalloc(dirty_bytes);
654 if (!new.dirty_bitmap) 659 if (!new.dirty_bitmap)
@@ -768,7 +773,7 @@ int kvm_get_dirty_log(struct kvm *kvm,
768{ 773{
769 struct kvm_memory_slot *memslot; 774 struct kvm_memory_slot *memslot;
770 int r, i; 775 int r, i;
771 int n; 776 unsigned long n;
772 unsigned long any = 0; 777 unsigned long any = 0;
773 778
774 r = -EINVAL; 779 r = -EINVAL;
@@ -780,7 +785,7 @@ int kvm_get_dirty_log(struct kvm *kvm,
780 if (!memslot->dirty_bitmap) 785 if (!memslot->dirty_bitmap)
781 goto out; 786 goto out;
782 787
783 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; 788 n = kvm_dirty_bitmap_bytes(memslot);
784 789
785 for (i = 0; !any && i < n/sizeof(long); ++i) 790 for (i = 0; !any && i < n/sizeof(long); ++i)
786 any = memslot->dirty_bitmap[i]; 791 any = memslot->dirty_bitmap[i];
@@ -829,7 +834,7 @@ EXPORT_SYMBOL_GPL(kvm_is_error_hva);
829struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn) 834struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
830{ 835{
831 int i; 836 int i;
832 struct kvm_memslots *slots = rcu_dereference(kvm->memslots); 837 struct kvm_memslots *slots = kvm_memslots(kvm);
833 838
834 for (i = 0; i < slots->nmemslots; ++i) { 839 for (i = 0; i < slots->nmemslots; ++i) {
835 struct kvm_memory_slot *memslot = &slots->memslots[i]; 840 struct kvm_memory_slot *memslot = &slots->memslots[i];
@@ -851,7 +856,7 @@ struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
851int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) 856int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
852{ 857{
853 int i; 858 int i;
854 struct kvm_memslots *slots = rcu_dereference(kvm->memslots); 859 struct kvm_memslots *slots = kvm_memslots(kvm);
855 860
856 gfn = unalias_gfn_instantiation(kvm, gfn); 861 gfn = unalias_gfn_instantiation(kvm, gfn);
857 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { 862 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
@@ -895,7 +900,7 @@ out:
895int memslot_id(struct kvm *kvm, gfn_t gfn) 900int memslot_id(struct kvm *kvm, gfn_t gfn)
896{ 901{
897 int i; 902 int i;
898 struct kvm_memslots *slots = rcu_dereference(kvm->memslots); 903 struct kvm_memslots *slots = kvm_memslots(kvm);
899 struct kvm_memory_slot *memslot = NULL; 904 struct kvm_memory_slot *memslot = NULL;
900 905
901 gfn = unalias_gfn(kvm, gfn); 906 gfn = unalias_gfn(kvm, gfn);
@@ -910,6 +915,11 @@ int memslot_id(struct kvm *kvm, gfn_t gfn)
910 return memslot - slots->memslots; 915 return memslot - slots->memslots;
911} 916}
912 917
918static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
919{
920 return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
921}
922
913unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) 923unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
914{ 924{
915 struct kvm_memory_slot *slot; 925 struct kvm_memory_slot *slot;
@@ -918,7 +928,7 @@ unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
918 slot = gfn_to_memslot_unaliased(kvm, gfn); 928 slot = gfn_to_memslot_unaliased(kvm, gfn);
919 if (!slot || slot->flags & KVM_MEMSLOT_INVALID) 929 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
920 return bad_hva(); 930 return bad_hva();
921 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); 931 return gfn_to_hva_memslot(slot, gfn);
922} 932}
923EXPORT_SYMBOL_GPL(gfn_to_hva); 933EXPORT_SYMBOL_GPL(gfn_to_hva);
924 934
@@ -968,11 +978,6 @@ pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
968} 978}
969EXPORT_SYMBOL_GPL(gfn_to_pfn); 979EXPORT_SYMBOL_GPL(gfn_to_pfn);
970 980
971static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
972{
973 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
974}
975
976pfn_t gfn_to_pfn_memslot(struct kvm *kvm, 981pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
977 struct kvm_memory_slot *slot, gfn_t gfn) 982 struct kvm_memory_slot *slot, gfn_t gfn)
978{ 983{
@@ -1187,9 +1192,7 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1187 if (memslot && memslot->dirty_bitmap) { 1192 if (memslot && memslot->dirty_bitmap) {
1188 unsigned long rel_gfn = gfn - memslot->base_gfn; 1193 unsigned long rel_gfn = gfn - memslot->base_gfn;
1189 1194
1190 /* avoid RMW */ 1195 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
1191 if (!generic_test_le_bit(rel_gfn, memslot->dirty_bitmap))
1192 generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
1193 } 1196 }
1194} 1197}
1195 1198
@@ -1602,7 +1605,6 @@ static long kvm_vm_ioctl(struct file *filp,
1602 r = -EFAULT; 1605 r = -EFAULT;
1603 if (copy_from_user(&zone, argp, sizeof zone)) 1606 if (copy_from_user(&zone, argp, sizeof zone))
1604 goto out; 1607 goto out;
1605 r = -ENXIO;
1606 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); 1608 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1607 if (r) 1609 if (r)
1608 goto out; 1610 goto out;
@@ -1614,7 +1616,6 @@ static long kvm_vm_ioctl(struct file *filp,
1614 r = -EFAULT; 1616 r = -EFAULT;
1615 if (copy_from_user(&zone, argp, sizeof zone)) 1617 if (copy_from_user(&zone, argp, sizeof zone))
1616 goto out; 1618 goto out;
1617 r = -ENXIO;
1618 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); 1619 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1619 if (r) 1620 if (r)
1620 goto out; 1621 goto out;
@@ -1748,12 +1749,19 @@ static struct file_operations kvm_vm_fops = {
1748 1749
1749static int kvm_dev_ioctl_create_vm(void) 1750static int kvm_dev_ioctl_create_vm(void)
1750{ 1751{
1751 int fd; 1752 int fd, r;
1752 struct kvm *kvm; 1753 struct kvm *kvm;
1753 1754
1754 kvm = kvm_create_vm(); 1755 kvm = kvm_create_vm();
1755 if (IS_ERR(kvm)) 1756 if (IS_ERR(kvm))
1756 return PTR_ERR(kvm); 1757 return PTR_ERR(kvm);
1758#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1759 r = kvm_coalesced_mmio_init(kvm);
1760 if (r < 0) {
1761 kvm_put_kvm(kvm);
1762 return r;
1763 }
1764#endif
1757 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR); 1765 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
1758 if (fd < 0) 1766 if (fd < 0)
1759 kvm_put_kvm(kvm); 1767 kvm_put_kvm(kvm);
@@ -1921,11 +1929,6 @@ static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
1921 cpu); 1929 cpu);
1922 hardware_disable(NULL); 1930 hardware_disable(NULL);
1923 break; 1931 break;
1924 case CPU_UP_CANCELED:
1925 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
1926 cpu);
1927 smp_call_function_single(cpu, hardware_disable, NULL, 1);
1928 break;
1929 case CPU_ONLINE: 1932 case CPU_ONLINE:
1930 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", 1933 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
1931 cpu); 1934 cpu);
@@ -1984,7 +1987,9 @@ int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
1984 int len, const void *val) 1987 int len, const void *val)
1985{ 1988{
1986 int i; 1989 int i;
1987 struct kvm_io_bus *bus = rcu_dereference(kvm->buses[bus_idx]); 1990 struct kvm_io_bus *bus;
1991
1992 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
1988 for (i = 0; i < bus->dev_count; i++) 1993 for (i = 0; i < bus->dev_count; i++)
1989 if (!kvm_iodevice_write(bus->devs[i], addr, len, val)) 1994 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
1990 return 0; 1995 return 0;
@@ -1996,8 +2001,9 @@ int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
1996 int len, void *val) 2001 int len, void *val)
1997{ 2002{
1998 int i; 2003 int i;
1999 struct kvm_io_bus *bus = rcu_dereference(kvm->buses[bus_idx]); 2004 struct kvm_io_bus *bus;
2000 2005
2006 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2001 for (i = 0; i < bus->dev_count; i++) 2007 for (i = 0; i < bus->dev_count; i++)
2002 if (!kvm_iodevice_read(bus->devs[i], addr, len, val)) 2008 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2003 return 0; 2009 return 0;
@@ -2172,7 +2178,7 @@ static void kvm_sched_out(struct preempt_notifier *pn,
2172 kvm_arch_vcpu_put(vcpu); 2178 kvm_arch_vcpu_put(vcpu);
2173} 2179}
2174 2180
2175int kvm_init(void *opaque, unsigned int vcpu_size, 2181int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
2176 struct module *module) 2182 struct module *module)
2177{ 2183{
2178 int r; 2184 int r;
@@ -2222,8 +2228,9 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
2222 goto out_free_4; 2228 goto out_free_4;
2223 2229
2224 /* A kmem cache lets us meet the alignment requirements of fx_save. */ 2230 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2225 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, 2231 if (!vcpu_align)
2226 __alignof__(struct kvm_vcpu), 2232 vcpu_align = __alignof__(struct kvm_vcpu);
2233 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
2227 0, NULL); 2234 0, NULL);
2228 if (!kvm_vcpu_cache) { 2235 if (!kvm_vcpu_cache) {
2229 r = -ENOMEM; 2236 r = -ENOMEM;
@@ -2272,7 +2279,6 @@ EXPORT_SYMBOL_GPL(kvm_init);
2272 2279
2273void kvm_exit(void) 2280void kvm_exit(void)
2274{ 2281{
2275 tracepoint_synchronize_unregister();
2276 kvm_exit_debug(); 2282 kvm_exit_debug();
2277 misc_deregister(&kvm_dev); 2283 misc_deregister(&kvm_dev);
2278 kmem_cache_destroy(kvm_vcpu_cache); 2284 kmem_cache_destroy(kvm_vcpu_cache);