aboutsummaryrefslogtreecommitdiffstats
path: root/virt/kvm/kvm_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'virt/kvm/kvm_main.c')
-rw-r--r--virt/kvm/kvm_main.c1366
1 files changed, 416 insertions, 950 deletions
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 7495ce347344..c82ae2492634 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -22,7 +22,6 @@
22#include <linux/module.h> 22#include <linux/module.h>
23#include <linux/errno.h> 23#include <linux/errno.h>
24#include <linux/percpu.h> 24#include <linux/percpu.h>
25#include <linux/gfp.h>
26#include <linux/mm.h> 25#include <linux/mm.h>
27#include <linux/miscdevice.h> 26#include <linux/miscdevice.h>
28#include <linux/vmalloc.h> 27#include <linux/vmalloc.h>
@@ -43,21 +42,18 @@
43#include <linux/swap.h> 42#include <linux/swap.h>
44#include <linux/bitops.h> 43#include <linux/bitops.h>
45#include <linux/spinlock.h> 44#include <linux/spinlock.h>
45#include <linux/compat.h>
46#include <linux/srcu.h>
47#include <linux/hugetlb.h>
48#include <linux/slab.h>
46 49
47#include <asm/processor.h> 50#include <asm/processor.h>
48#include <asm/io.h> 51#include <asm/io.h>
49#include <asm/uaccess.h> 52#include <asm/uaccess.h>
50#include <asm/pgtable.h> 53#include <asm/pgtable.h>
54#include <asm-generic/bitops/le.h>
51 55
52#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
53#include "coalesced_mmio.h" 56#include "coalesced_mmio.h"
54#endif
55
56#ifdef KVM_CAP_DEVICE_ASSIGNMENT
57#include <linux/pci.h>
58#include <linux/interrupt.h>
59#include "irq.h"
60#endif
61 57
62#define CREATE_TRACE_POINTS 58#define CREATE_TRACE_POINTS
63#include <trace/events/kvm.h> 59#include <trace/events/kvm.h>
@@ -68,13 +64,15 @@ MODULE_LICENSE("GPL");
68/* 64/*
69 * Ordering of locks: 65 * Ordering of locks:
70 * 66 *
71 * kvm->slots_lock --> kvm->lock --> kvm->irq_lock 67 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
72 */ 68 */
73 69
74DEFINE_SPINLOCK(kvm_lock); 70DEFINE_SPINLOCK(kvm_lock);
75LIST_HEAD(vm_list); 71LIST_HEAD(vm_list);
76 72
77static cpumask_var_t cpus_hardware_enabled; 73static cpumask_var_t cpus_hardware_enabled;
74static int kvm_usage_count = 0;
75static atomic_t hardware_enable_failed;
78 76
79struct kmem_cache *kvm_vcpu_cache; 77struct kmem_cache *kvm_vcpu_cache;
80EXPORT_SYMBOL_GPL(kvm_vcpu_cache); 78EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
@@ -85,615 +83,15 @@ struct dentry *kvm_debugfs_dir;
85 83
86static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, 84static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
87 unsigned long arg); 85 unsigned long arg);
86static int hardware_enable_all(void);
87static void hardware_disable_all(void);
88
89static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
88 90
89static bool kvm_rebooting; 91static bool kvm_rebooting;
90 92
91static bool largepages_enabled = true; 93static bool largepages_enabled = true;
92 94
93#ifdef KVM_CAP_DEVICE_ASSIGNMENT
94static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
95 int assigned_dev_id)
96{
97 struct list_head *ptr;
98 struct kvm_assigned_dev_kernel *match;
99
100 list_for_each(ptr, head) {
101 match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
102 if (match->assigned_dev_id == assigned_dev_id)
103 return match;
104 }
105 return NULL;
106}
107
108static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
109 *assigned_dev, int irq)
110{
111 int i, index;
112 struct msix_entry *host_msix_entries;
113
114 host_msix_entries = assigned_dev->host_msix_entries;
115
116 index = -1;
117 for (i = 0; i < assigned_dev->entries_nr; i++)
118 if (irq == host_msix_entries[i].vector) {
119 index = i;
120 break;
121 }
122 if (index < 0) {
123 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
124 return 0;
125 }
126
127 return index;
128}
129
130static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
131{
132 struct kvm_assigned_dev_kernel *assigned_dev;
133 struct kvm *kvm;
134 int i;
135
136 assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
137 interrupt_work);
138 kvm = assigned_dev->kvm;
139
140 mutex_lock(&kvm->irq_lock);
141 spin_lock_irq(&assigned_dev->assigned_dev_lock);
142 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
143 struct kvm_guest_msix_entry *guest_entries =
144 assigned_dev->guest_msix_entries;
145 for (i = 0; i < assigned_dev->entries_nr; i++) {
146 if (!(guest_entries[i].flags &
147 KVM_ASSIGNED_MSIX_PENDING))
148 continue;
149 guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING;
150 kvm_set_irq(assigned_dev->kvm,
151 assigned_dev->irq_source_id,
152 guest_entries[i].vector, 1);
153 }
154 } else
155 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
156 assigned_dev->guest_irq, 1);
157
158 spin_unlock_irq(&assigned_dev->assigned_dev_lock);
159 mutex_unlock(&assigned_dev->kvm->irq_lock);
160}
161
162static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
163{
164 unsigned long flags;
165 struct kvm_assigned_dev_kernel *assigned_dev =
166 (struct kvm_assigned_dev_kernel *) dev_id;
167
168 spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags);
169 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
170 int index = find_index_from_host_irq(assigned_dev, irq);
171 if (index < 0)
172 goto out;
173 assigned_dev->guest_msix_entries[index].flags |=
174 KVM_ASSIGNED_MSIX_PENDING;
175 }
176
177 schedule_work(&assigned_dev->interrupt_work);
178
179 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
180 disable_irq_nosync(irq);
181 assigned_dev->host_irq_disabled = true;
182 }
183
184out:
185 spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags);
186 return IRQ_HANDLED;
187}
188
189/* Ack the irq line for an assigned device */
190static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
191{
192 struct kvm_assigned_dev_kernel *dev;
193 unsigned long flags;
194
195 if (kian->gsi == -1)
196 return;
197
198 dev = container_of(kian, struct kvm_assigned_dev_kernel,
199 ack_notifier);
200
201 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
202
203 /* The guest irq may be shared so this ack may be
204 * from another device.
205 */
206 spin_lock_irqsave(&dev->assigned_dev_lock, flags);
207 if (dev->host_irq_disabled) {
208 enable_irq(dev->host_irq);
209 dev->host_irq_disabled = false;
210 }
211 spin_unlock_irqrestore(&dev->assigned_dev_lock, flags);
212}
213
214static void deassign_guest_irq(struct kvm *kvm,
215 struct kvm_assigned_dev_kernel *assigned_dev)
216{
217 kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
218 assigned_dev->ack_notifier.gsi = -1;
219
220 if (assigned_dev->irq_source_id != -1)
221 kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
222 assigned_dev->irq_source_id = -1;
223 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK);
224}
225
226/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
227static void deassign_host_irq(struct kvm *kvm,
228 struct kvm_assigned_dev_kernel *assigned_dev)
229{
230 /*
231 * In kvm_free_device_irq, cancel_work_sync return true if:
232 * 1. work is scheduled, and then cancelled.
233 * 2. work callback is executed.
234 *
235 * The first one ensured that the irq is disabled and no more events
236 * would happen. But for the second one, the irq may be enabled (e.g.
237 * for MSI). So we disable irq here to prevent further events.
238 *
239 * Notice this maybe result in nested disable if the interrupt type is
240 * INTx, but it's OK for we are going to free it.
241 *
242 * If this function is a part of VM destroy, please ensure that till
243 * now, the kvm state is still legal for probably we also have to wait
244 * interrupt_work done.
245 */
246 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
247 int i;
248 for (i = 0; i < assigned_dev->entries_nr; i++)
249 disable_irq_nosync(assigned_dev->
250 host_msix_entries[i].vector);
251
252 cancel_work_sync(&assigned_dev->interrupt_work);
253
254 for (i = 0; i < assigned_dev->entries_nr; i++)
255 free_irq(assigned_dev->host_msix_entries[i].vector,
256 (void *)assigned_dev);
257
258 assigned_dev->entries_nr = 0;
259 kfree(assigned_dev->host_msix_entries);
260 kfree(assigned_dev->guest_msix_entries);
261 pci_disable_msix(assigned_dev->dev);
262 } else {
263 /* Deal with MSI and INTx */
264 disable_irq_nosync(assigned_dev->host_irq);
265 cancel_work_sync(&assigned_dev->interrupt_work);
266
267 free_irq(assigned_dev->host_irq, (void *)assigned_dev);
268
269 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
270 pci_disable_msi(assigned_dev->dev);
271 }
272
273 assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
274}
275
276static int kvm_deassign_irq(struct kvm *kvm,
277 struct kvm_assigned_dev_kernel *assigned_dev,
278 unsigned long irq_requested_type)
279{
280 unsigned long guest_irq_type, host_irq_type;
281
282 if (!irqchip_in_kernel(kvm))
283 return -EINVAL;
284 /* no irq assignment to deassign */
285 if (!assigned_dev->irq_requested_type)
286 return -ENXIO;
287
288 host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK;
289 guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK;
290
291 if (host_irq_type)
292 deassign_host_irq(kvm, assigned_dev);
293 if (guest_irq_type)
294 deassign_guest_irq(kvm, assigned_dev);
295
296 return 0;
297}
298
299static void kvm_free_assigned_irq(struct kvm *kvm,
300 struct kvm_assigned_dev_kernel *assigned_dev)
301{
302 kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type);
303}
304
305static void kvm_free_assigned_device(struct kvm *kvm,
306 struct kvm_assigned_dev_kernel
307 *assigned_dev)
308{
309 kvm_free_assigned_irq(kvm, assigned_dev);
310
311 pci_reset_function(assigned_dev->dev);
312
313 pci_release_regions(assigned_dev->dev);
314 pci_disable_device(assigned_dev->dev);
315 pci_dev_put(assigned_dev->dev);
316
317 list_del(&assigned_dev->list);
318 kfree(assigned_dev);
319}
320
321void kvm_free_all_assigned_devices(struct kvm *kvm)
322{
323 struct list_head *ptr, *ptr2;
324 struct kvm_assigned_dev_kernel *assigned_dev;
325
326 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
327 assigned_dev = list_entry(ptr,
328 struct kvm_assigned_dev_kernel,
329 list);
330
331 kvm_free_assigned_device(kvm, assigned_dev);
332 }
333}
334
335static int assigned_device_enable_host_intx(struct kvm *kvm,
336 struct kvm_assigned_dev_kernel *dev)
337{
338 dev->host_irq = dev->dev->irq;
339 /* Even though this is PCI, we don't want to use shared
340 * interrupts. Sharing host devices with guest-assigned devices
341 * on the same interrupt line is not a happy situation: there
342 * are going to be long delays in accepting, acking, etc.
343 */
344 if (request_irq(dev->host_irq, kvm_assigned_dev_intr,
345 0, "kvm_assigned_intx_device", (void *)dev))
346 return -EIO;
347 return 0;
348}
349
350#ifdef __KVM_HAVE_MSI
351static int assigned_device_enable_host_msi(struct kvm *kvm,
352 struct kvm_assigned_dev_kernel *dev)
353{
354 int r;
355
356 if (!dev->dev->msi_enabled) {
357 r = pci_enable_msi(dev->dev);
358 if (r)
359 return r;
360 }
361
362 dev->host_irq = dev->dev->irq;
363 if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0,
364 "kvm_assigned_msi_device", (void *)dev)) {
365 pci_disable_msi(dev->dev);
366 return -EIO;
367 }
368
369 return 0;
370}
371#endif
372
373#ifdef __KVM_HAVE_MSIX
374static int assigned_device_enable_host_msix(struct kvm *kvm,
375 struct kvm_assigned_dev_kernel *dev)
376{
377 int i, r = -EINVAL;
378
379 /* host_msix_entries and guest_msix_entries should have been
380 * initialized */
381 if (dev->entries_nr == 0)
382 return r;
383
384 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
385 if (r)
386 return r;
387
388 for (i = 0; i < dev->entries_nr; i++) {
389 r = request_irq(dev->host_msix_entries[i].vector,
390 kvm_assigned_dev_intr, 0,
391 "kvm_assigned_msix_device",
392 (void *)dev);
393 /* FIXME: free requested_irq's on failure */
394 if (r)
395 return r;
396 }
397
398 return 0;
399}
400
401#endif
402
403static int assigned_device_enable_guest_intx(struct kvm *kvm,
404 struct kvm_assigned_dev_kernel *dev,
405 struct kvm_assigned_irq *irq)
406{
407 dev->guest_irq = irq->guest_irq;
408 dev->ack_notifier.gsi = irq->guest_irq;
409 return 0;
410}
411
412#ifdef __KVM_HAVE_MSI
413static int assigned_device_enable_guest_msi(struct kvm *kvm,
414 struct kvm_assigned_dev_kernel *dev,
415 struct kvm_assigned_irq *irq)
416{
417 dev->guest_irq = irq->guest_irq;
418 dev->ack_notifier.gsi = -1;
419 dev->host_irq_disabled = false;
420 return 0;
421}
422#endif
423#ifdef __KVM_HAVE_MSIX
424static int assigned_device_enable_guest_msix(struct kvm *kvm,
425 struct kvm_assigned_dev_kernel *dev,
426 struct kvm_assigned_irq *irq)
427{
428 dev->guest_irq = irq->guest_irq;
429 dev->ack_notifier.gsi = -1;
430 dev->host_irq_disabled = false;
431 return 0;
432}
433#endif
434
435static int assign_host_irq(struct kvm *kvm,
436 struct kvm_assigned_dev_kernel *dev,
437 __u32 host_irq_type)
438{
439 int r = -EEXIST;
440
441 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
442 return r;
443
444 switch (host_irq_type) {
445 case KVM_DEV_IRQ_HOST_INTX:
446 r = assigned_device_enable_host_intx(kvm, dev);
447 break;
448#ifdef __KVM_HAVE_MSI
449 case KVM_DEV_IRQ_HOST_MSI:
450 r = assigned_device_enable_host_msi(kvm, dev);
451 break;
452#endif
453#ifdef __KVM_HAVE_MSIX
454 case KVM_DEV_IRQ_HOST_MSIX:
455 r = assigned_device_enable_host_msix(kvm, dev);
456 break;
457#endif
458 default:
459 r = -EINVAL;
460 }
461
462 if (!r)
463 dev->irq_requested_type |= host_irq_type;
464
465 return r;
466}
467
468static int assign_guest_irq(struct kvm *kvm,
469 struct kvm_assigned_dev_kernel *dev,
470 struct kvm_assigned_irq *irq,
471 unsigned long guest_irq_type)
472{
473 int id;
474 int r = -EEXIST;
475
476 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
477 return r;
478
479 id = kvm_request_irq_source_id(kvm);
480 if (id < 0)
481 return id;
482
483 dev->irq_source_id = id;
484
485 switch (guest_irq_type) {
486 case KVM_DEV_IRQ_GUEST_INTX:
487 r = assigned_device_enable_guest_intx(kvm, dev, irq);
488 break;
489#ifdef __KVM_HAVE_MSI
490 case KVM_DEV_IRQ_GUEST_MSI:
491 r = assigned_device_enable_guest_msi(kvm, dev, irq);
492 break;
493#endif
494#ifdef __KVM_HAVE_MSIX
495 case KVM_DEV_IRQ_GUEST_MSIX:
496 r = assigned_device_enable_guest_msix(kvm, dev, irq);
497 break;
498#endif
499 default:
500 r = -EINVAL;
501 }
502
503 if (!r) {
504 dev->irq_requested_type |= guest_irq_type;
505 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
506 } else
507 kvm_free_irq_source_id(kvm, dev->irq_source_id);
508
509 return r;
510}
511
512/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
513static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
514 struct kvm_assigned_irq *assigned_irq)
515{
516 int r = -EINVAL;
517 struct kvm_assigned_dev_kernel *match;
518 unsigned long host_irq_type, guest_irq_type;
519
520 if (!capable(CAP_SYS_RAWIO))
521 return -EPERM;
522
523 if (!irqchip_in_kernel(kvm))
524 return r;
525
526 mutex_lock(&kvm->lock);
527 r = -ENODEV;
528 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
529 assigned_irq->assigned_dev_id);
530 if (!match)
531 goto out;
532
533 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
534 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
535
536 r = -EINVAL;
537 /* can only assign one type at a time */
538 if (hweight_long(host_irq_type) > 1)
539 goto out;
540 if (hweight_long(guest_irq_type) > 1)
541 goto out;
542 if (host_irq_type == 0 && guest_irq_type == 0)
543 goto out;
544
545 r = 0;
546 if (host_irq_type)
547 r = assign_host_irq(kvm, match, host_irq_type);
548 if (r)
549 goto out;
550
551 if (guest_irq_type)
552 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
553out:
554 mutex_unlock(&kvm->lock);
555 return r;
556}
557
558static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
559 struct kvm_assigned_irq
560 *assigned_irq)
561{
562 int r = -ENODEV;
563 struct kvm_assigned_dev_kernel *match;
564
565 mutex_lock(&kvm->lock);
566
567 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
568 assigned_irq->assigned_dev_id);
569 if (!match)
570 goto out;
571
572 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
573out:
574 mutex_unlock(&kvm->lock);
575 return r;
576}
577
578static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
579 struct kvm_assigned_pci_dev *assigned_dev)
580{
581 int r = 0;
582 struct kvm_assigned_dev_kernel *match;
583 struct pci_dev *dev;
584
585 down_read(&kvm->slots_lock);
586 mutex_lock(&kvm->lock);
587
588 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
589 assigned_dev->assigned_dev_id);
590 if (match) {
591 /* device already assigned */
592 r = -EEXIST;
593 goto out;
594 }
595
596 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
597 if (match == NULL) {
598 printk(KERN_INFO "%s: Couldn't allocate memory\n",
599 __func__);
600 r = -ENOMEM;
601 goto out;
602 }
603 dev = pci_get_bus_and_slot(assigned_dev->busnr,
604 assigned_dev->devfn);
605 if (!dev) {
606 printk(KERN_INFO "%s: host device not found\n", __func__);
607 r = -EINVAL;
608 goto out_free;
609 }
610 if (pci_enable_device(dev)) {
611 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
612 r = -EBUSY;
613 goto out_put;
614 }
615 r = pci_request_regions(dev, "kvm_assigned_device");
616 if (r) {
617 printk(KERN_INFO "%s: Could not get access to device regions\n",
618 __func__);
619 goto out_disable;
620 }
621
622 pci_reset_function(dev);
623
624 match->assigned_dev_id = assigned_dev->assigned_dev_id;
625 match->host_busnr = assigned_dev->busnr;
626 match->host_devfn = assigned_dev->devfn;
627 match->flags = assigned_dev->flags;
628 match->dev = dev;
629 spin_lock_init(&match->assigned_dev_lock);
630 match->irq_source_id = -1;
631 match->kvm = kvm;
632 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
633 INIT_WORK(&match->interrupt_work,
634 kvm_assigned_dev_interrupt_work_handler);
635
636 list_add(&match->list, &kvm->arch.assigned_dev_head);
637
638 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
639 if (!kvm->arch.iommu_domain) {
640 r = kvm_iommu_map_guest(kvm);
641 if (r)
642 goto out_list_del;
643 }
644 r = kvm_assign_device(kvm, match);
645 if (r)
646 goto out_list_del;
647 }
648
649out:
650 mutex_unlock(&kvm->lock);
651 up_read(&kvm->slots_lock);
652 return r;
653out_list_del:
654 list_del(&match->list);
655 pci_release_regions(dev);
656out_disable:
657 pci_disable_device(dev);
658out_put:
659 pci_dev_put(dev);
660out_free:
661 kfree(match);
662 mutex_unlock(&kvm->lock);
663 up_read(&kvm->slots_lock);
664 return r;
665}
666#endif
667
668#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
669static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
670 struct kvm_assigned_pci_dev *assigned_dev)
671{
672 int r = 0;
673 struct kvm_assigned_dev_kernel *match;
674
675 mutex_lock(&kvm->lock);
676
677 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
678 assigned_dev->assigned_dev_id);
679 if (!match) {
680 printk(KERN_INFO "%s: device hasn't been assigned before, "
681 "so cannot be deassigned\n", __func__);
682 r = -EINVAL;
683 goto out;
684 }
685
686 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
687 kvm_deassign_device(kvm, match);
688
689 kvm_free_assigned_device(kvm, match);
690
691out:
692 mutex_unlock(&kvm->lock);
693 return r;
694}
695#endif
696
697inline int kvm_is_mmio_pfn(pfn_t pfn) 95inline int kvm_is_mmio_pfn(pfn_t pfn)
698{ 96{
699 if (pfn_valid(pfn)) { 97 if (pfn_valid(pfn)) {
@@ -740,7 +138,7 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
740 138
741 zalloc_cpumask_var(&cpus, GFP_ATOMIC); 139 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
742 140
743 spin_lock(&kvm->requests_lock); 141 raw_spin_lock(&kvm->requests_lock);
744 me = smp_processor_id(); 142 me = smp_processor_id();
745 kvm_for_each_vcpu(i, vcpu, kvm) { 143 kvm_for_each_vcpu(i, vcpu, kvm) {
746 if (test_and_set_bit(req, &vcpu->requests)) 144 if (test_and_set_bit(req, &vcpu->requests))
@@ -755,7 +153,7 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
755 smp_call_function_many(cpus, ack_flush, NULL, 1); 153 smp_call_function_many(cpus, ack_flush, NULL, 1);
756 else 154 else
757 called = false; 155 called = false;
758 spin_unlock(&kvm->requests_lock); 156 raw_spin_unlock(&kvm->requests_lock);
759 free_cpumask_var(cpus); 157 free_cpumask_var(cpus);
760 return called; 158 return called;
761} 159}
@@ -819,7 +217,7 @@ static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
819 unsigned long address) 217 unsigned long address)
820{ 218{
821 struct kvm *kvm = mmu_notifier_to_kvm(mn); 219 struct kvm *kvm = mmu_notifier_to_kvm(mn);
822 int need_tlb_flush; 220 int need_tlb_flush, idx;
823 221
824 /* 222 /*
825 * When ->invalidate_page runs, the linux pte has been zapped 223 * When ->invalidate_page runs, the linux pte has been zapped
@@ -839,10 +237,12 @@ static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
839 * pte after kvm_unmap_hva returned, without noticing the page 237 * pte after kvm_unmap_hva returned, without noticing the page
840 * is going to be freed. 238 * is going to be freed.
841 */ 239 */
240 idx = srcu_read_lock(&kvm->srcu);
842 spin_lock(&kvm->mmu_lock); 241 spin_lock(&kvm->mmu_lock);
843 kvm->mmu_notifier_seq++; 242 kvm->mmu_notifier_seq++;
844 need_tlb_flush = kvm_unmap_hva(kvm, address); 243 need_tlb_flush = kvm_unmap_hva(kvm, address);
845 spin_unlock(&kvm->mmu_lock); 244 spin_unlock(&kvm->mmu_lock);
245 srcu_read_unlock(&kvm->srcu, idx);
846 246
847 /* we've to flush the tlb before the pages can be freed */ 247 /* we've to flush the tlb before the pages can be freed */
848 if (need_tlb_flush) 248 if (need_tlb_flush)
@@ -856,11 +256,14 @@ static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
856 pte_t pte) 256 pte_t pte)
857{ 257{
858 struct kvm *kvm = mmu_notifier_to_kvm(mn); 258 struct kvm *kvm = mmu_notifier_to_kvm(mn);
259 int idx;
859 260
261 idx = srcu_read_lock(&kvm->srcu);
860 spin_lock(&kvm->mmu_lock); 262 spin_lock(&kvm->mmu_lock);
861 kvm->mmu_notifier_seq++; 263 kvm->mmu_notifier_seq++;
862 kvm_set_spte_hva(kvm, address, pte); 264 kvm_set_spte_hva(kvm, address, pte);
863 spin_unlock(&kvm->mmu_lock); 265 spin_unlock(&kvm->mmu_lock);
266 srcu_read_unlock(&kvm->srcu, idx);
864} 267}
865 268
866static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, 269static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
@@ -869,8 +272,9 @@ static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
869 unsigned long end) 272 unsigned long end)
870{ 273{
871 struct kvm *kvm = mmu_notifier_to_kvm(mn); 274 struct kvm *kvm = mmu_notifier_to_kvm(mn);
872 int need_tlb_flush = 0; 275 int need_tlb_flush = 0, idx;
873 276
277 idx = srcu_read_lock(&kvm->srcu);
874 spin_lock(&kvm->mmu_lock); 278 spin_lock(&kvm->mmu_lock);
875 /* 279 /*
876 * The count increase must become visible at unlock time as no 280 * The count increase must become visible at unlock time as no
@@ -881,6 +285,7 @@ static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
881 for (; start < end; start += PAGE_SIZE) 285 for (; start < end; start += PAGE_SIZE)
882 need_tlb_flush |= kvm_unmap_hva(kvm, start); 286 need_tlb_flush |= kvm_unmap_hva(kvm, start);
883 spin_unlock(&kvm->mmu_lock); 287 spin_unlock(&kvm->mmu_lock);
288 srcu_read_unlock(&kvm->srcu, idx);
884 289
885 /* we've to flush the tlb before the pages can be freed */ 290 /* we've to flush the tlb before the pages can be freed */
886 if (need_tlb_flush) 291 if (need_tlb_flush)
@@ -918,11 +323,13 @@ static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
918 unsigned long address) 323 unsigned long address)
919{ 324{
920 struct kvm *kvm = mmu_notifier_to_kvm(mn); 325 struct kvm *kvm = mmu_notifier_to_kvm(mn);
921 int young; 326 int young, idx;
922 327
328 idx = srcu_read_lock(&kvm->srcu);
923 spin_lock(&kvm->mmu_lock); 329 spin_lock(&kvm->mmu_lock);
924 young = kvm_age_hva(kvm, address); 330 young = kvm_age_hva(kvm, address);
925 spin_unlock(&kvm->mmu_lock); 331 spin_unlock(&kvm->mmu_lock);
332 srcu_read_unlock(&kvm->srcu, idx);
926 333
927 if (young) 334 if (young)
928 kvm_flush_remote_tlbs(kvm); 335 kvm_flush_remote_tlbs(kvm);
@@ -934,7 +341,11 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
934 struct mm_struct *mm) 341 struct mm_struct *mm)
935{ 342{
936 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);
937 kvm_arch_flush_shadow(kvm); 347 kvm_arch_flush_shadow(kvm);
348 srcu_read_unlock(&kvm->srcu, idx);
938} 349}
939 350
940static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { 351static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
@@ -945,57 +356,68 @@ static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
945 .change_pte = kvm_mmu_notifier_change_pte, 356 .change_pte = kvm_mmu_notifier_change_pte,
946 .release = kvm_mmu_notifier_release, 357 .release = kvm_mmu_notifier_release,
947}; 358};
359
360static int kvm_init_mmu_notifier(struct kvm *kvm)
361{
362 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
363 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
364}
365
366#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
367
368static int kvm_init_mmu_notifier(struct kvm *kvm)
369{
370 return 0;
371}
372
948#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ 373#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
949 374
950static struct kvm *kvm_create_vm(void) 375static struct kvm *kvm_create_vm(void)
951{ 376{
377 int r = 0, i;
952 struct kvm *kvm = kvm_arch_create_vm(); 378 struct kvm *kvm = kvm_arch_create_vm();
953#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
954 struct page *page;
955#endif
956 379
957 if (IS_ERR(kvm)) 380 if (IS_ERR(kvm))
958 goto out; 381 goto out;
382
383 r = hardware_enable_all();
384 if (r)
385 goto out_err_nodisable;
386
959#ifdef CONFIG_HAVE_KVM_IRQCHIP 387#ifdef CONFIG_HAVE_KVM_IRQCHIP
960 INIT_LIST_HEAD(&kvm->irq_routing);
961 INIT_HLIST_HEAD(&kvm->mask_notifier_list); 388 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
389 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
962#endif 390#endif
963 391
964#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET 392 r = -ENOMEM;
965 page = alloc_page(GFP_KERNEL | __GFP_ZERO); 393 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
966 if (!page) { 394 if (!kvm->memslots)
967 kfree(kvm); 395 goto out_err;
968 return ERR_PTR(-ENOMEM); 396 if (init_srcu_struct(&kvm->srcu))
397 goto out_err;
398 for (i = 0; i < KVM_NR_BUSES; i++) {
399 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
400 GFP_KERNEL);
401 if (!kvm->buses[i]) {
402 cleanup_srcu_struct(&kvm->srcu);
403 goto out_err;
404 }
969 } 405 }
970 kvm->coalesced_mmio_ring =
971 (struct kvm_coalesced_mmio_ring *)page_address(page);
972#endif
973 406
974#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) 407 r = kvm_init_mmu_notifier(kvm);
975 { 408 if (r) {
976 int err; 409 cleanup_srcu_struct(&kvm->srcu);
977 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; 410 goto out_err;
978 err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
979 if (err) {
980#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
981 put_page(page);
982#endif
983 kfree(kvm);
984 return ERR_PTR(err);
985 }
986 } 411 }
987#endif
988 412
989 kvm->mm = current->mm; 413 kvm->mm = current->mm;
990 atomic_inc(&kvm->mm->mm_count); 414 atomic_inc(&kvm->mm->mm_count);
991 spin_lock_init(&kvm->mmu_lock); 415 spin_lock_init(&kvm->mmu_lock);
992 spin_lock_init(&kvm->requests_lock); 416 raw_spin_lock_init(&kvm->requests_lock);
993 kvm_io_bus_init(&kvm->pio_bus);
994 kvm_eventfd_init(kvm); 417 kvm_eventfd_init(kvm);
995 mutex_init(&kvm->lock); 418 mutex_init(&kvm->lock);
996 mutex_init(&kvm->irq_lock); 419 mutex_init(&kvm->irq_lock);
997 kvm_io_bus_init(&kvm->mmio_bus); 420 mutex_init(&kvm->slots_lock);
998 init_rwsem(&kvm->slots_lock);
999 atomic_set(&kvm->users_count, 1); 421 atomic_set(&kvm->users_count, 1);
1000 spin_lock(&kvm_lock); 422 spin_lock(&kvm_lock);
1001 list_add(&kvm->vm_list, &vm_list); 423 list_add(&kvm->vm_list, &vm_list);
@@ -1005,6 +427,15 @@ static struct kvm *kvm_create_vm(void)
1005#endif 427#endif
1006out: 428out:
1007 return kvm; 429 return kvm;
430
431out_err:
432 hardware_disable_all();
433out_err_nodisable:
434 for (i = 0; i < KVM_NR_BUSES; i++)
435 kfree(kvm->buses[i]);
436 kfree(kvm->memslots);
437 kfree(kvm);
438 return ERR_PTR(r);
1008} 439}
1009 440
1010/* 441/*
@@ -1037,13 +468,17 @@ static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
1037void kvm_free_physmem(struct kvm *kvm) 468void kvm_free_physmem(struct kvm *kvm)
1038{ 469{
1039 int i; 470 int i;
471 struct kvm_memslots *slots = kvm->memslots;
472
473 for (i = 0; i < slots->nmemslots; ++i)
474 kvm_free_physmem_slot(&slots->memslots[i], NULL);
1040 475
1041 for (i = 0; i < kvm->nmemslots; ++i) 476 kfree(kvm->memslots);
1042 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
1043} 477}
1044 478
1045static void kvm_destroy_vm(struct kvm *kvm) 479static void kvm_destroy_vm(struct kvm *kvm)
1046{ 480{
481 int i;
1047 struct mm_struct *mm = kvm->mm; 482 struct mm_struct *mm = kvm->mm;
1048 483
1049 kvm_arch_sync_events(kvm); 484 kvm_arch_sync_events(kvm);
@@ -1051,18 +486,16 @@ static void kvm_destroy_vm(struct kvm *kvm)
1051 list_del(&kvm->vm_list); 486 list_del(&kvm->vm_list);
1052 spin_unlock(&kvm_lock); 487 spin_unlock(&kvm_lock);
1053 kvm_free_irq_routing(kvm); 488 kvm_free_irq_routing(kvm);
1054 kvm_io_bus_destroy(&kvm->pio_bus); 489 for (i = 0; i < KVM_NR_BUSES; i++)
1055 kvm_io_bus_destroy(&kvm->mmio_bus); 490 kvm_io_bus_destroy(kvm->buses[i]);
1056#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET 491 kvm_coalesced_mmio_free(kvm);
1057 if (kvm->coalesced_mmio_ring != NULL)
1058 free_page((unsigned long)kvm->coalesced_mmio_ring);
1059#endif
1060#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) 492#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1061 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); 493 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
1062#else 494#else
1063 kvm_arch_flush_shadow(kvm); 495 kvm_arch_flush_shadow(kvm);
1064#endif 496#endif
1065 kvm_arch_destroy_vm(kvm); 497 kvm_arch_destroy_vm(kvm);
498 hardware_disable_all();
1066 mmdrop(mm); 499 mmdrop(mm);
1067} 500}
1068 501
@@ -1102,12 +535,13 @@ int __kvm_set_memory_region(struct kvm *kvm,
1102 struct kvm_userspace_memory_region *mem, 535 struct kvm_userspace_memory_region *mem,
1103 int user_alloc) 536 int user_alloc)
1104{ 537{
1105 int r; 538 int r, flush_shadow = 0;
1106 gfn_t base_gfn; 539 gfn_t base_gfn;
1107 unsigned long npages; 540 unsigned long npages;
1108 unsigned long i; 541 unsigned long i;
1109 struct kvm_memory_slot *memslot; 542 struct kvm_memory_slot *memslot;
1110 struct kvm_memory_slot old, new; 543 struct kvm_memory_slot old, new;
544 struct kvm_memslots *slots, *old_memslots;
1111 545
1112 r = -EINVAL; 546 r = -EINVAL;
1113 /* General sanity checks */ 547 /* General sanity checks */
@@ -1122,7 +556,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
1122 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) 556 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1123 goto out; 557 goto out;
1124 558
1125 memslot = &kvm->memslots[mem->slot]; 559 memslot = &kvm->memslots->memslots[mem->slot];
1126 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; 560 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1127 npages = mem->memory_size >> PAGE_SHIFT; 561 npages = mem->memory_size >> PAGE_SHIFT;
1128 562
@@ -1143,7 +577,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
1143 /* Check for overlaps */ 577 /* Check for overlaps */
1144 r = -EEXIST; 578 r = -EEXIST;
1145 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { 579 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1146 struct kvm_memory_slot *s = &kvm->memslots[i]; 580 struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
1147 581
1148 if (s == memslot || !s->npages) 582 if (s == memslot || !s->npages)
1149 continue; 583 continue;
@@ -1169,15 +603,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
1169 memset(new.rmap, 0, npages * sizeof(*new.rmap)); 603 memset(new.rmap, 0, npages * sizeof(*new.rmap));
1170 604
1171 new.user_alloc = user_alloc; 605 new.user_alloc = user_alloc;
1172 /* 606 new.userspace_addr = mem->userspace_addr;
1173 * hva_to_rmmap() serialzies with the mmu_lock and to be
1174 * safe it has to ignore memslots with !user_alloc &&
1175 * !userspace_addr.
1176 */
1177 if (user_alloc)
1178 new.userspace_addr = mem->userspace_addr;
1179 else
1180 new.userspace_addr = 0;
1181 } 607 }
1182 if (!npages) 608 if (!npages)
1183 goto skip_lpage; 609 goto skip_lpage;
@@ -1226,14 +652,15 @@ skip_lpage:
1226 652
1227 /* Allocate page dirty bitmap if needed */ 653 /* Allocate page dirty bitmap if needed */
1228 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { 654 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1229 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; 655 unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(&new);
1230 656
1231 new.dirty_bitmap = vmalloc(dirty_bytes); 657 new.dirty_bitmap = vmalloc(dirty_bytes);
1232 if (!new.dirty_bitmap) 658 if (!new.dirty_bitmap)
1233 goto out_free; 659 goto out_free;
1234 memset(new.dirty_bitmap, 0, dirty_bytes); 660 memset(new.dirty_bitmap, 0, dirty_bytes);
661 /* destroy any largepage mappings for dirty tracking */
1235 if (old.npages) 662 if (old.npages)
1236 kvm_arch_flush_shadow(kvm); 663 flush_shadow = 1;
1237 } 664 }
1238#else /* not defined CONFIG_S390 */ 665#else /* not defined CONFIG_S390 */
1239 new.user_alloc = user_alloc; 666 new.user_alloc = user_alloc;
@@ -1241,36 +668,72 @@ skip_lpage:
1241 new.userspace_addr = mem->userspace_addr; 668 new.userspace_addr = mem->userspace_addr;
1242#endif /* not defined CONFIG_S390 */ 669#endif /* not defined CONFIG_S390 */
1243 670
1244 if (!npages) 671 if (!npages) {
672 r = -ENOMEM;
673 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
674 if (!slots)
675 goto out_free;
676 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
677 if (mem->slot >= slots->nmemslots)
678 slots->nmemslots = mem->slot + 1;
679 slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
680
681 old_memslots = kvm->memslots;
682 rcu_assign_pointer(kvm->memslots, slots);
683 synchronize_srcu_expedited(&kvm->srcu);
684 /* From this point no new shadow pages pointing to a deleted
685 * memslot will be created.
686 *
687 * validation of sp->gfn happens in:
688 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
689 * - kvm_is_visible_gfn (mmu_check_roots)
690 */
1245 kvm_arch_flush_shadow(kvm); 691 kvm_arch_flush_shadow(kvm);
692 kfree(old_memslots);
693 }
1246 694
1247 spin_lock(&kvm->mmu_lock); 695 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
1248 if (mem->slot >= kvm->nmemslots) 696 if (r)
1249 kvm->nmemslots = mem->slot + 1;
1250
1251 *memslot = new;
1252 spin_unlock(&kvm->mmu_lock);
1253
1254 r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1255 if (r) {
1256 spin_lock(&kvm->mmu_lock);
1257 *memslot = old;
1258 spin_unlock(&kvm->mmu_lock);
1259 goto out_free; 697 goto out_free;
1260 }
1261 698
1262 kvm_free_physmem_slot(&old, npages ? &new : NULL);
1263 /* Slot deletion case: we have to update the current slot */
1264 spin_lock(&kvm->mmu_lock);
1265 if (!npages)
1266 *memslot = old;
1267 spin_unlock(&kvm->mmu_lock);
1268#ifdef CONFIG_DMAR 699#ifdef CONFIG_DMAR
1269 /* map the pages in iommu page table */ 700 /* map the pages in iommu page table */
1270 r = kvm_iommu_map_pages(kvm, base_gfn, npages); 701 if (npages) {
1271 if (r) 702 r = kvm_iommu_map_pages(kvm, &new);
1272 goto out; 703 if (r)
704 goto out_free;
705 }
1273#endif 706#endif
707
708 r = -ENOMEM;
709 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
710 if (!slots)
711 goto out_free;
712 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
713 if (mem->slot >= slots->nmemslots)
714 slots->nmemslots = mem->slot + 1;
715
716 /* actual memory is freed via old in kvm_free_physmem_slot below */
717 if (!npages) {
718 new.rmap = NULL;
719 new.dirty_bitmap = NULL;
720 for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
721 new.lpage_info[i] = NULL;
722 }
723
724 slots->memslots[mem->slot] = new;
725 old_memslots = kvm->memslots;
726 rcu_assign_pointer(kvm->memslots, slots);
727 synchronize_srcu_expedited(&kvm->srcu);
728
729 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
730
731 kvm_free_physmem_slot(&old, &new);
732 kfree(old_memslots);
733
734 if (flush_shadow)
735 kvm_arch_flush_shadow(kvm);
736
1274 return 0; 737 return 0;
1275 738
1276out_free: 739out_free:
@@ -1287,9 +750,9 @@ int kvm_set_memory_region(struct kvm *kvm,
1287{ 750{
1288 int r; 751 int r;
1289 752
1290 down_write(&kvm->slots_lock); 753 mutex_lock(&kvm->slots_lock);
1291 r = __kvm_set_memory_region(kvm, mem, user_alloc); 754 r = __kvm_set_memory_region(kvm, mem, user_alloc);
1292 up_write(&kvm->slots_lock); 755 mutex_unlock(&kvm->slots_lock);
1293 return r; 756 return r;
1294} 757}
1295EXPORT_SYMBOL_GPL(kvm_set_memory_region); 758EXPORT_SYMBOL_GPL(kvm_set_memory_region);
@@ -1309,19 +772,19 @@ int kvm_get_dirty_log(struct kvm *kvm,
1309{ 772{
1310 struct kvm_memory_slot *memslot; 773 struct kvm_memory_slot *memslot;
1311 int r, i; 774 int r, i;
1312 int n; 775 unsigned long n;
1313 unsigned long any = 0; 776 unsigned long any = 0;
1314 777
1315 r = -EINVAL; 778 r = -EINVAL;
1316 if (log->slot >= KVM_MEMORY_SLOTS) 779 if (log->slot >= KVM_MEMORY_SLOTS)
1317 goto out; 780 goto out;
1318 781
1319 memslot = &kvm->memslots[log->slot]; 782 memslot = &kvm->memslots->memslots[log->slot];
1320 r = -ENOENT; 783 r = -ENOENT;
1321 if (!memslot->dirty_bitmap) 784 if (!memslot->dirty_bitmap)
1322 goto out; 785 goto out;
1323 786
1324 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; 787 n = kvm_dirty_bitmap_bytes(memslot);
1325 788
1326 for (i = 0; !any && i < n/sizeof(long); ++i) 789 for (i = 0; !any && i < n/sizeof(long); ++i)
1327 any = memslot->dirty_bitmap[i]; 790 any = memslot->dirty_bitmap[i];
@@ -1370,9 +833,10 @@ EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1370struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn) 833struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1371{ 834{
1372 int i; 835 int i;
836 struct kvm_memslots *slots = rcu_dereference(kvm->memslots);
1373 837
1374 for (i = 0; i < kvm->nmemslots; ++i) { 838 for (i = 0; i < slots->nmemslots; ++i) {
1375 struct kvm_memory_slot *memslot = &kvm->memslots[i]; 839 struct kvm_memory_slot *memslot = &slots->memslots[i];
1376 840
1377 if (gfn >= memslot->base_gfn 841 if (gfn >= memslot->base_gfn
1378 && gfn < memslot->base_gfn + memslot->npages) 842 && gfn < memslot->base_gfn + memslot->npages)
@@ -1391,10 +855,14 @@ struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1391int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) 855int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1392{ 856{
1393 int i; 857 int i;
858 struct kvm_memslots *slots = rcu_dereference(kvm->memslots);
1394 859
1395 gfn = unalias_gfn(kvm, gfn); 860 gfn = unalias_gfn_instantiation(kvm, gfn);
1396 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { 861 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1397 struct kvm_memory_slot *memslot = &kvm->memslots[i]; 862 struct kvm_memory_slot *memslot = &slots->memslots[i];
863
864 if (memslot->flags & KVM_MEMSLOT_INVALID)
865 continue;
1398 866
1399 if (gfn >= memslot->base_gfn 867 if (gfn >= memslot->base_gfn
1400 && gfn < memslot->base_gfn + memslot->npages) 868 && gfn < memslot->base_gfn + memslot->npages)
@@ -1404,33 +872,68 @@ int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1404} 872}
1405EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); 873EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1406 874
875unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
876{
877 struct vm_area_struct *vma;
878 unsigned long addr, size;
879
880 size = PAGE_SIZE;
881
882 addr = gfn_to_hva(kvm, gfn);
883 if (kvm_is_error_hva(addr))
884 return PAGE_SIZE;
885
886 down_read(&current->mm->mmap_sem);
887 vma = find_vma(current->mm, addr);
888 if (!vma)
889 goto out;
890
891 size = vma_kernel_pagesize(vma);
892
893out:
894 up_read(&current->mm->mmap_sem);
895
896 return size;
897}
898
899int memslot_id(struct kvm *kvm, gfn_t gfn)
900{
901 int i;
902 struct kvm_memslots *slots = rcu_dereference(kvm->memslots);
903 struct kvm_memory_slot *memslot = NULL;
904
905 gfn = unalias_gfn(kvm, gfn);
906 for (i = 0; i < slots->nmemslots; ++i) {
907 memslot = &slots->memslots[i];
908
909 if (gfn >= memslot->base_gfn
910 && gfn < memslot->base_gfn + memslot->npages)
911 break;
912 }
913
914 return memslot - slots->memslots;
915}
916
1407unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) 917unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1408{ 918{
1409 struct kvm_memory_slot *slot; 919 struct kvm_memory_slot *slot;
1410 920
1411 gfn = unalias_gfn(kvm, gfn); 921 gfn = unalias_gfn_instantiation(kvm, gfn);
1412 slot = gfn_to_memslot_unaliased(kvm, gfn); 922 slot = gfn_to_memslot_unaliased(kvm, gfn);
1413 if (!slot) 923 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
1414 return bad_hva(); 924 return bad_hva();
1415 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); 925 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1416} 926}
1417EXPORT_SYMBOL_GPL(gfn_to_hva); 927EXPORT_SYMBOL_GPL(gfn_to_hva);
1418 928
1419pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) 929static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr)
1420{ 930{
1421 struct page *page[1]; 931 struct page *page[1];
1422 unsigned long addr;
1423 int npages; 932 int npages;
1424 pfn_t pfn; 933 pfn_t pfn;
1425 934
1426 might_sleep(); 935 might_sleep();
1427 936
1428 addr = gfn_to_hva(kvm, gfn);
1429 if (kvm_is_error_hva(addr)) {
1430 get_page(bad_page);
1431 return page_to_pfn(bad_page);
1432 }
1433
1434 npages = get_user_pages_fast(addr, 1, 1, page); 937 npages = get_user_pages_fast(addr, 1, 1, page);
1435 938
1436 if (unlikely(npages != 1)) { 939 if (unlikely(npages != 1)) {
@@ -1455,8 +958,32 @@ pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1455 return pfn; 958 return pfn;
1456} 959}
1457 960
961pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
962{
963 unsigned long addr;
964
965 addr = gfn_to_hva(kvm, gfn);
966 if (kvm_is_error_hva(addr)) {
967 get_page(bad_page);
968 return page_to_pfn(bad_page);
969 }
970
971 return hva_to_pfn(kvm, addr);
972}
1458EXPORT_SYMBOL_GPL(gfn_to_pfn); 973EXPORT_SYMBOL_GPL(gfn_to_pfn);
1459 974
975static unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
976{
977 return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
978}
979
980pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
981 struct kvm_memory_slot *slot, gfn_t gfn)
982{
983 unsigned long addr = gfn_to_hva_memslot(slot, gfn);
984 return hva_to_pfn(kvm, addr);
985}
986
1460struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) 987struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1461{ 988{
1462 pfn_t pfn; 989 pfn_t pfn;
@@ -1663,10 +1190,13 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1663 memslot = gfn_to_memslot_unaliased(kvm, gfn); 1190 memslot = gfn_to_memslot_unaliased(kvm, gfn);
1664 if (memslot && memslot->dirty_bitmap) { 1191 if (memslot && memslot->dirty_bitmap) {
1665 unsigned long rel_gfn = gfn - memslot->base_gfn; 1192 unsigned long rel_gfn = gfn - memslot->base_gfn;
1193 unsigned long *p = memslot->dirty_bitmap +
1194 rel_gfn / BITS_PER_LONG;
1195 int offset = rel_gfn % BITS_PER_LONG;
1666 1196
1667 /* avoid RMW */ 1197 /* avoid RMW */
1668 if (!test_bit(rel_gfn, memslot->dirty_bitmap)) 1198 if (!generic_test_le_bit(offset, p))
1669 set_bit(rel_gfn, memslot->dirty_bitmap); 1199 generic___set_le_bit(offset, p);
1670 } 1200 }
1671} 1201}
1672 1202
@@ -1689,9 +1219,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1689 if (signal_pending(current)) 1219 if (signal_pending(current))
1690 break; 1220 break;
1691 1221
1692 vcpu_put(vcpu);
1693 schedule(); 1222 schedule();
1694 vcpu_load(vcpu);
1695 } 1223 }
1696 1224
1697 finish_wait(&vcpu->wq, &wait); 1225 finish_wait(&vcpu->wq, &wait);
@@ -1705,6 +1233,21 @@ void kvm_resched(struct kvm_vcpu *vcpu)
1705} 1233}
1706EXPORT_SYMBOL_GPL(kvm_resched); 1234EXPORT_SYMBOL_GPL(kvm_resched);
1707 1235
1236void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
1237{
1238 ktime_t expires;
1239 DEFINE_WAIT(wait);
1240
1241 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1242
1243 /* Sleep for 100 us, and hope lock-holder got scheduled */
1244 expires = ktime_add_ns(ktime_get(), 100000UL);
1245 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1246
1247 finish_wait(&vcpu->wq, &wait);
1248}
1249EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1250
1708static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1251static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1709{ 1252{
1710 struct kvm_vcpu *vcpu = vma->vm_file->private_data; 1253 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
@@ -1757,7 +1300,7 @@ static struct file_operations kvm_vcpu_fops = {
1757 */ 1300 */
1758static int create_vcpu_fd(struct kvm_vcpu *vcpu) 1301static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1759{ 1302{
1760 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0); 1303 return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
1761} 1304}
1762 1305
1763/* 1306/*
@@ -1828,88 +1371,6 @@ static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1828 return 0; 1371 return 0;
1829} 1372}
1830 1373
1831#ifdef __KVM_HAVE_MSIX
1832static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
1833 struct kvm_assigned_msix_nr *entry_nr)
1834{
1835 int r = 0;
1836 struct kvm_assigned_dev_kernel *adev;
1837
1838 mutex_lock(&kvm->lock);
1839
1840 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1841 entry_nr->assigned_dev_id);
1842 if (!adev) {
1843 r = -EINVAL;
1844 goto msix_nr_out;
1845 }
1846
1847 if (adev->entries_nr == 0) {
1848 adev->entries_nr = entry_nr->entry_nr;
1849 if (adev->entries_nr == 0 ||
1850 adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) {
1851 r = -EINVAL;
1852 goto msix_nr_out;
1853 }
1854
1855 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
1856 entry_nr->entry_nr,
1857 GFP_KERNEL);
1858 if (!adev->host_msix_entries) {
1859 r = -ENOMEM;
1860 goto msix_nr_out;
1861 }
1862 adev->guest_msix_entries = kzalloc(
1863 sizeof(struct kvm_guest_msix_entry) *
1864 entry_nr->entry_nr, GFP_KERNEL);
1865 if (!adev->guest_msix_entries) {
1866 kfree(adev->host_msix_entries);
1867 r = -ENOMEM;
1868 goto msix_nr_out;
1869 }
1870 } else /* Not allowed set MSI-X number twice */
1871 r = -EINVAL;
1872msix_nr_out:
1873 mutex_unlock(&kvm->lock);
1874 return r;
1875}
1876
1877static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
1878 struct kvm_assigned_msix_entry *entry)
1879{
1880 int r = 0, i;
1881 struct kvm_assigned_dev_kernel *adev;
1882
1883 mutex_lock(&kvm->lock);
1884
1885 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
1886 entry->assigned_dev_id);
1887
1888 if (!adev) {
1889 r = -EINVAL;
1890 goto msix_entry_out;
1891 }
1892
1893 for (i = 0; i < adev->entries_nr; i++)
1894 if (adev->guest_msix_entries[i].vector == 0 ||
1895 adev->guest_msix_entries[i].entry == entry->entry) {
1896 adev->guest_msix_entries[i].entry = entry->entry;
1897 adev->guest_msix_entries[i].vector = entry->gsi;
1898 adev->host_msix_entries[i].entry = entry->entry;
1899 break;
1900 }
1901 if (i == adev->entries_nr) {
1902 r = -ENOSPC;
1903 goto msix_entry_out;
1904 }
1905
1906msix_entry_out:
1907 mutex_unlock(&kvm->lock);
1908
1909 return r;
1910}
1911#endif
1912
1913static long kvm_vcpu_ioctl(struct file *filp, 1374static long kvm_vcpu_ioctl(struct file *filp,
1914 unsigned int ioctl, unsigned long arg) 1375 unsigned int ioctl, unsigned long arg)
1915{ 1376{
@@ -2168,112 +1629,6 @@ static long kvm_vm_ioctl(struct file *filp,
2168 break; 1629 break;
2169 } 1630 }
2170#endif 1631#endif
2171#ifdef KVM_CAP_DEVICE_ASSIGNMENT
2172 case KVM_ASSIGN_PCI_DEVICE: {
2173 struct kvm_assigned_pci_dev assigned_dev;
2174
2175 r = -EFAULT;
2176 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2177 goto out;
2178 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2179 if (r)
2180 goto out;
2181 break;
2182 }
2183 case KVM_ASSIGN_IRQ: {
2184 r = -EOPNOTSUPP;
2185 break;
2186 }
2187#ifdef KVM_CAP_ASSIGN_DEV_IRQ
2188 case KVM_ASSIGN_DEV_IRQ: {
2189 struct kvm_assigned_irq assigned_irq;
2190
2191 r = -EFAULT;
2192 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2193 goto out;
2194 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2195 if (r)
2196 goto out;
2197 break;
2198 }
2199 case KVM_DEASSIGN_DEV_IRQ: {
2200 struct kvm_assigned_irq assigned_irq;
2201
2202 r = -EFAULT;
2203 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2204 goto out;
2205 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
2206 if (r)
2207 goto out;
2208 break;
2209 }
2210#endif
2211#endif
2212#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
2213 case KVM_DEASSIGN_PCI_DEVICE: {
2214 struct kvm_assigned_pci_dev assigned_dev;
2215
2216 r = -EFAULT;
2217 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2218 goto out;
2219 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
2220 if (r)
2221 goto out;
2222 break;
2223 }
2224#endif
2225#ifdef KVM_CAP_IRQ_ROUTING
2226 case KVM_SET_GSI_ROUTING: {
2227 struct kvm_irq_routing routing;
2228 struct kvm_irq_routing __user *urouting;
2229 struct kvm_irq_routing_entry *entries;
2230
2231 r = -EFAULT;
2232 if (copy_from_user(&routing, argp, sizeof(routing)))
2233 goto out;
2234 r = -EINVAL;
2235 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
2236 goto out;
2237 if (routing.flags)
2238 goto out;
2239 r = -ENOMEM;
2240 entries = vmalloc(routing.nr * sizeof(*entries));
2241 if (!entries)
2242 goto out;
2243 r = -EFAULT;
2244 urouting = argp;
2245 if (copy_from_user(entries, urouting->entries,
2246 routing.nr * sizeof(*entries)))
2247 goto out_free_irq_routing;
2248 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2249 routing.flags);
2250 out_free_irq_routing:
2251 vfree(entries);
2252 break;
2253 }
2254#endif /* KVM_CAP_IRQ_ROUTING */
2255#ifdef __KVM_HAVE_MSIX
2256 case KVM_ASSIGN_SET_MSIX_NR: {
2257 struct kvm_assigned_msix_nr entry_nr;
2258 r = -EFAULT;
2259 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
2260 goto out;
2261 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
2262 if (r)
2263 goto out;
2264 break;
2265 }
2266 case KVM_ASSIGN_SET_MSIX_ENTRY: {
2267 struct kvm_assigned_msix_entry entry;
2268 r = -EFAULT;
2269 if (copy_from_user(&entry, argp, sizeof entry))
2270 goto out;
2271 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
2272 if (r)
2273 goto out;
2274 break;
2275 }
2276#endif
2277 case KVM_IRQFD: { 1632 case KVM_IRQFD: {
2278 struct kvm_irqfd data; 1633 struct kvm_irqfd data;
2279 1634
@@ -2305,10 +1660,58 @@ static long kvm_vm_ioctl(struct file *filp,
2305#endif 1660#endif
2306 default: 1661 default:
2307 r = kvm_arch_vm_ioctl(filp, ioctl, arg); 1662 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1663 if (r == -ENOTTY)
1664 r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
1665 }
1666out:
1667 return r;
1668}
1669
1670#ifdef CONFIG_COMPAT
1671struct compat_kvm_dirty_log {
1672 __u32 slot;
1673 __u32 padding1;
1674 union {
1675 compat_uptr_t dirty_bitmap; /* one bit per page */
1676 __u64 padding2;
1677 };
1678};
1679
1680static long kvm_vm_compat_ioctl(struct file *filp,
1681 unsigned int ioctl, unsigned long arg)
1682{
1683 struct kvm *kvm = filp->private_data;
1684 int r;
1685
1686 if (kvm->mm != current->mm)
1687 return -EIO;
1688 switch (ioctl) {
1689 case KVM_GET_DIRTY_LOG: {
1690 struct compat_kvm_dirty_log compat_log;
1691 struct kvm_dirty_log log;
1692
1693 r = -EFAULT;
1694 if (copy_from_user(&compat_log, (void __user *)arg,
1695 sizeof(compat_log)))
1696 goto out;
1697 log.slot = compat_log.slot;
1698 log.padding1 = compat_log.padding1;
1699 log.padding2 = compat_log.padding2;
1700 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
1701
1702 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1703 if (r)
1704 goto out;
1705 break;
1706 }
1707 default:
1708 r = kvm_vm_ioctl(filp, ioctl, arg);
2308 } 1709 }
1710
2309out: 1711out:
2310 return r; 1712 return r;
2311} 1713}
1714#endif
2312 1715
2313static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1716static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2314{ 1717{
@@ -2344,7 +1747,9 @@ static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2344static struct file_operations kvm_vm_fops = { 1747static struct file_operations kvm_vm_fops = {
2345 .release = kvm_vm_release, 1748 .release = kvm_vm_release,
2346 .unlocked_ioctl = kvm_vm_ioctl, 1749 .unlocked_ioctl = kvm_vm_ioctl,
2347 .compat_ioctl = kvm_vm_ioctl, 1750#ifdef CONFIG_COMPAT
1751 .compat_ioctl = kvm_vm_compat_ioctl,
1752#endif
2348 .mmap = kvm_vm_mmap, 1753 .mmap = kvm_vm_mmap,
2349}; 1754};
2350 1755
@@ -2356,7 +1761,7 @@ static int kvm_dev_ioctl_create_vm(void)
2356 kvm = kvm_create_vm(); 1761 kvm = kvm_create_vm();
2357 if (IS_ERR(kvm)) 1762 if (IS_ERR(kvm))
2358 return PTR_ERR(kvm); 1763 return PTR_ERR(kvm);
2359 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0); 1764 fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
2360 if (fd < 0) 1765 if (fd < 0)
2361 kvm_put_kvm(kvm); 1766 kvm_put_kvm(kvm);
2362 1767
@@ -2372,6 +1777,7 @@ static long kvm_dev_ioctl_check_extension_generic(long arg)
2372#ifdef CONFIG_KVM_APIC_ARCHITECTURE 1777#ifdef CONFIG_KVM_APIC_ARCHITECTURE
2373 case KVM_CAP_SET_BOOT_CPU_ID: 1778 case KVM_CAP_SET_BOOT_CPU_ID:
2374#endif 1779#endif
1780 case KVM_CAP_INTERNAL_ERROR_DATA:
2375 return 1; 1781 return 1;
2376#ifdef CONFIG_HAVE_KVM_IRQCHIP 1782#ifdef CONFIG_HAVE_KVM_IRQCHIP
2377 case KVM_CAP_IRQ_ROUTING: 1783 case KVM_CAP_IRQ_ROUTING:
@@ -2442,11 +1848,21 @@ static struct miscdevice kvm_dev = {
2442static void hardware_enable(void *junk) 1848static void hardware_enable(void *junk)
2443{ 1849{
2444 int cpu = raw_smp_processor_id(); 1850 int cpu = raw_smp_processor_id();
1851 int r;
2445 1852
2446 if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) 1853 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2447 return; 1854 return;
1855
2448 cpumask_set_cpu(cpu, cpus_hardware_enabled); 1856 cpumask_set_cpu(cpu, cpus_hardware_enabled);
2449 kvm_arch_hardware_enable(NULL); 1857
1858 r = kvm_arch_hardware_enable(NULL);
1859
1860 if (r) {
1861 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
1862 atomic_inc(&hardware_enable_failed);
1863 printk(KERN_INFO "kvm: enabling virtualization on "
1864 "CPU%d failed\n", cpu);
1865 }
2450} 1866}
2451 1867
2452static void hardware_disable(void *junk) 1868static void hardware_disable(void *junk)
@@ -2459,11 +1875,52 @@ static void hardware_disable(void *junk)
2459 kvm_arch_hardware_disable(NULL); 1875 kvm_arch_hardware_disable(NULL);
2460} 1876}
2461 1877
1878static void hardware_disable_all_nolock(void)
1879{
1880 BUG_ON(!kvm_usage_count);
1881
1882 kvm_usage_count--;
1883 if (!kvm_usage_count)
1884 on_each_cpu(hardware_disable, NULL, 1);
1885}
1886
1887static void hardware_disable_all(void)
1888{
1889 spin_lock(&kvm_lock);
1890 hardware_disable_all_nolock();
1891 spin_unlock(&kvm_lock);
1892}
1893
1894static int hardware_enable_all(void)
1895{
1896 int r = 0;
1897
1898 spin_lock(&kvm_lock);
1899
1900 kvm_usage_count++;
1901 if (kvm_usage_count == 1) {
1902 atomic_set(&hardware_enable_failed, 0);
1903 on_each_cpu(hardware_enable, NULL, 1);
1904
1905 if (atomic_read(&hardware_enable_failed)) {
1906 hardware_disable_all_nolock();
1907 r = -EBUSY;
1908 }
1909 }
1910
1911 spin_unlock(&kvm_lock);
1912
1913 return r;
1914}
1915
2462static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, 1916static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2463 void *v) 1917 void *v)
2464{ 1918{
2465 int cpu = (long)v; 1919 int cpu = (long)v;
2466 1920
1921 if (!kvm_usage_count)
1922 return NOTIFY_OK;
1923
2467 val &= ~CPU_TASKS_FROZEN; 1924 val &= ~CPU_TASKS_FROZEN;
2468 switch (val) { 1925 switch (val) {
2469 case CPU_DYING: 1926 case CPU_DYING:
@@ -2517,12 +1974,7 @@ static struct notifier_block kvm_reboot_notifier = {
2517 .priority = 0, 1974 .priority = 0,
2518}; 1975};
2519 1976
2520void kvm_io_bus_init(struct kvm_io_bus *bus) 1977static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2521{
2522 memset(bus, 0, sizeof(*bus));
2523}
2524
2525void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2526{ 1978{
2527 int i; 1979 int i;
2528 1980
@@ -2531,13 +1983,15 @@ void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2531 1983
2532 kvm_iodevice_destructor(pos); 1984 kvm_iodevice_destructor(pos);
2533 } 1985 }
1986 kfree(bus);
2534} 1987}
2535 1988
2536/* kvm_io_bus_write - called under kvm->slots_lock */ 1989/* kvm_io_bus_write - called under kvm->slots_lock */
2537int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr, 1990int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2538 int len, const void *val) 1991 int len, const void *val)
2539{ 1992{
2540 int i; 1993 int i;
1994 struct kvm_io_bus *bus = rcu_dereference(kvm->buses[bus_idx]);
2541 for (i = 0; i < bus->dev_count; i++) 1995 for (i = 0; i < bus->dev_count; i++)
2542 if (!kvm_iodevice_write(bus->devs[i], addr, len, val)) 1996 if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
2543 return 0; 1997 return 0;
@@ -2545,59 +1999,71 @@ int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
2545} 1999}
2546 2000
2547/* kvm_io_bus_read - called under kvm->slots_lock */ 2001/* kvm_io_bus_read - called under kvm->slots_lock */
2548int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val) 2002int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2003 int len, void *val)
2549{ 2004{
2550 int i; 2005 int i;
2006 struct kvm_io_bus *bus = rcu_dereference(kvm->buses[bus_idx]);
2007
2551 for (i = 0; i < bus->dev_count; i++) 2008 for (i = 0; i < bus->dev_count; i++)
2552 if (!kvm_iodevice_read(bus->devs[i], addr, len, val)) 2009 if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
2553 return 0; 2010 return 0;
2554 return -EOPNOTSUPP; 2011 return -EOPNOTSUPP;
2555} 2012}
2556 2013
2557int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus, 2014/* Caller must hold slots_lock. */
2558 struct kvm_io_device *dev) 2015int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2016 struct kvm_io_device *dev)
2559{ 2017{
2560 int ret; 2018 struct kvm_io_bus *new_bus, *bus;
2561
2562 down_write(&kvm->slots_lock);
2563 ret = __kvm_io_bus_register_dev(bus, dev);
2564 up_write(&kvm->slots_lock);
2565
2566 return ret;
2567}
2568 2019
2569/* An unlocked version. Caller must have write lock on slots_lock. */ 2020 bus = kvm->buses[bus_idx];
2570int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
2571 struct kvm_io_device *dev)
2572{
2573 if (bus->dev_count > NR_IOBUS_DEVS-1) 2021 if (bus->dev_count > NR_IOBUS_DEVS-1)
2574 return -ENOSPC; 2022 return -ENOSPC;
2575 2023
2576 bus->devs[bus->dev_count++] = dev; 2024 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2025 if (!new_bus)
2026 return -ENOMEM;
2027 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2028 new_bus->devs[new_bus->dev_count++] = dev;
2029 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2030 synchronize_srcu_expedited(&kvm->srcu);
2031 kfree(bus);
2577 2032
2578 return 0; 2033 return 0;
2579} 2034}
2580 2035
2581void kvm_io_bus_unregister_dev(struct kvm *kvm, 2036/* Caller must hold slots_lock. */
2582 struct kvm_io_bus *bus, 2037int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2583 struct kvm_io_device *dev) 2038 struct kvm_io_device *dev)
2584{ 2039{
2585 down_write(&kvm->slots_lock); 2040 int i, r;
2586 __kvm_io_bus_unregister_dev(bus, dev); 2041 struct kvm_io_bus *new_bus, *bus;
2587 up_write(&kvm->slots_lock);
2588}
2589 2042
2590/* An unlocked version. Caller must have write lock on slots_lock. */ 2043 new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
2591void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus, 2044 if (!new_bus)
2592 struct kvm_io_device *dev) 2045 return -ENOMEM;
2593{
2594 int i;
2595 2046
2596 for (i = 0; i < bus->dev_count; i++) 2047 bus = kvm->buses[bus_idx];
2597 if (bus->devs[i] == dev) { 2048 memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
2598 bus->devs[i] = bus->devs[--bus->dev_count]; 2049
2050 r = -ENOENT;
2051 for (i = 0; i < new_bus->dev_count; i++)
2052 if (new_bus->devs[i] == dev) {
2053 r = 0;
2054 new_bus->devs[i] = new_bus->devs[--new_bus->dev_count];
2599 break; 2055 break;
2600 } 2056 }
2057
2058 if (r) {
2059 kfree(new_bus);
2060 return r;
2061 }
2062
2063 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2064 synchronize_srcu_expedited(&kvm->srcu);
2065 kfree(bus);
2066 return r;
2601} 2067}
2602 2068
2603static struct notifier_block kvm_cpu_notifier = { 2069static struct notifier_block kvm_cpu_notifier = {
@@ -2666,13 +2132,15 @@ static void kvm_exit_debug(void)
2666 2132
2667static int kvm_suspend(struct sys_device *dev, pm_message_t state) 2133static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2668{ 2134{
2669 hardware_disable(NULL); 2135 if (kvm_usage_count)
2136 hardware_disable(NULL);
2670 return 0; 2137 return 0;
2671} 2138}
2672 2139
2673static int kvm_resume(struct sys_device *dev) 2140static int kvm_resume(struct sys_device *dev)
2674{ 2141{
2675 hardware_enable(NULL); 2142 if (kvm_usage_count)
2143 hardware_enable(NULL);
2676 return 0; 2144 return 0;
2677} 2145}
2678 2146
@@ -2747,7 +2215,6 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
2747 goto out_free_1; 2215 goto out_free_1;
2748 } 2216 }
2749 2217
2750 on_each_cpu(hardware_enable, NULL, 1);
2751 r = register_cpu_notifier(&kvm_cpu_notifier); 2218 r = register_cpu_notifier(&kvm_cpu_notifier);
2752 if (r) 2219 if (r)
2753 goto out_free_2; 2220 goto out_free_2;
@@ -2797,7 +2264,6 @@ out_free_3:
2797 unregister_reboot_notifier(&kvm_reboot_notifier); 2264 unregister_reboot_notifier(&kvm_reboot_notifier);
2798 unregister_cpu_notifier(&kvm_cpu_notifier); 2265 unregister_cpu_notifier(&kvm_cpu_notifier);
2799out_free_2: 2266out_free_2:
2800 on_each_cpu(hardware_disable, NULL, 1);
2801out_free_1: 2267out_free_1:
2802 kvm_arch_hardware_unsetup(); 2268 kvm_arch_hardware_unsetup();
2803out_free_0a: 2269out_free_0a: