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.c201
1 files changed, 136 insertions, 65 deletions
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 1cd693a76a51..adc68feb5c5a 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -217,6 +217,11 @@ void kvm_make_mclock_inprogress_request(struct kvm *kvm)
217 make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS); 217 make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
218} 218}
219 219
220void kvm_make_update_eoibitmap_request(struct kvm *kvm)
221{
222 make_all_cpus_request(kvm, KVM_REQ_EOIBITMAP);
223}
224
220int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) 225int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
221{ 226{
222 struct page *page; 227 struct page *page;
@@ -474,6 +479,8 @@ static struct kvm *kvm_create_vm(unsigned long type)
474 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); 479 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
475#endif 480#endif
476 481
482 BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
483
477 r = -ENOMEM; 484 r = -ENOMEM;
478 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL); 485 kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
479 if (!kvm->memslots) 486 if (!kvm->memslots)
@@ -670,7 +677,8 @@ static void sort_memslots(struct kvm_memslots *slots)
670 slots->id_to_index[slots->memslots[i].id] = i; 677 slots->id_to_index[slots->memslots[i].id] = i;
671} 678}
672 679
673void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new) 680void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new,
681 u64 last_generation)
674{ 682{
675 if (new) { 683 if (new) {
676 int id = new->id; 684 int id = new->id;
@@ -682,7 +690,7 @@ void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new)
682 sort_memslots(slots); 690 sort_memslots(slots);
683 } 691 }
684 692
685 slots->generation++; 693 slots->generation = last_generation + 1;
686} 694}
687 695
688static int check_memory_region_flags(struct kvm_userspace_memory_region *mem) 696static int check_memory_region_flags(struct kvm_userspace_memory_region *mem)
@@ -699,6 +707,35 @@ static int check_memory_region_flags(struct kvm_userspace_memory_region *mem)
699 return 0; 707 return 0;
700} 708}
701 709
710static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
711 struct kvm_memslots *slots, struct kvm_memory_slot *new)
712{
713 struct kvm_memslots *old_memslots = kvm->memslots;
714
715 update_memslots(slots, new, kvm->memslots->generation);
716 rcu_assign_pointer(kvm->memslots, slots);
717 synchronize_srcu_expedited(&kvm->srcu);
718 return old_memslots;
719}
720
721/*
722 * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations:
723 * - create a new memory slot
724 * - delete an existing memory slot
725 * - modify an existing memory slot
726 * -- move it in the guest physical memory space
727 * -- just change its flags
728 *
729 * Since flags can be changed by some of these operations, the following
730 * differentiation is the best we can do for __kvm_set_memory_region():
731 */
732enum kvm_mr_change {
733 KVM_MR_CREATE,
734 KVM_MR_DELETE,
735 KVM_MR_MOVE,
736 KVM_MR_FLAGS_ONLY,
737};
738
702/* 739/*
703 * Allocate some memory and give it an address in the guest physical address 740 * Allocate some memory and give it an address in the guest physical address
704 * space. 741 * space.
@@ -709,14 +746,15 @@ static int check_memory_region_flags(struct kvm_userspace_memory_region *mem)
709 */ 746 */
710int __kvm_set_memory_region(struct kvm *kvm, 747int __kvm_set_memory_region(struct kvm *kvm,
711 struct kvm_userspace_memory_region *mem, 748 struct kvm_userspace_memory_region *mem,
712 int user_alloc) 749 bool user_alloc)
713{ 750{
714 int r; 751 int r;
715 gfn_t base_gfn; 752 gfn_t base_gfn;
716 unsigned long npages; 753 unsigned long npages;
717 struct kvm_memory_slot *memslot, *slot; 754 struct kvm_memory_slot *slot;
718 struct kvm_memory_slot old, new; 755 struct kvm_memory_slot old, new;
719 struct kvm_memslots *slots, *old_memslots; 756 struct kvm_memslots *slots = NULL, *old_memslots;
757 enum kvm_mr_change change;
720 758
721 r = check_memory_region_flags(mem); 759 r = check_memory_region_flags(mem);
722 if (r) 760 if (r)
@@ -740,7 +778,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
740 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) 778 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
741 goto out; 779 goto out;
742 780
743 memslot = id_to_memslot(kvm->memslots, mem->slot); 781 slot = id_to_memslot(kvm->memslots, mem->slot);
744 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; 782 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
745 npages = mem->memory_size >> PAGE_SHIFT; 783 npages = mem->memory_size >> PAGE_SHIFT;
746 784
@@ -751,26 +789,48 @@ int __kvm_set_memory_region(struct kvm *kvm,
751 if (!npages) 789 if (!npages)
752 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; 790 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
753 791
754 new = old = *memslot; 792 new = old = *slot;
755 793
756 new.id = mem->slot; 794 new.id = mem->slot;
757 new.base_gfn = base_gfn; 795 new.base_gfn = base_gfn;
758 new.npages = npages; 796 new.npages = npages;
759 new.flags = mem->flags; 797 new.flags = mem->flags;
760 798
761 /* Disallow changing a memory slot's size. */
762 r = -EINVAL; 799 r = -EINVAL;
763 if (npages && old.npages && npages != old.npages) 800 if (npages) {
764 goto out_free; 801 if (!old.npages)
802 change = KVM_MR_CREATE;
803 else { /* Modify an existing slot. */
804 if ((mem->userspace_addr != old.userspace_addr) ||
805 (npages != old.npages) ||
806 ((new.flags ^ old.flags) & KVM_MEM_READONLY))
807 goto out;
765 808
766 /* Check for overlaps */ 809 if (base_gfn != old.base_gfn)
767 r = -EEXIST; 810 change = KVM_MR_MOVE;
768 kvm_for_each_memslot(slot, kvm->memslots) { 811 else if (new.flags != old.flags)
769 if (slot->id >= KVM_MEMORY_SLOTS || slot == memslot) 812 change = KVM_MR_FLAGS_ONLY;
770 continue; 813 else { /* Nothing to change. */
771 if (!((base_gfn + npages <= slot->base_gfn) || 814 r = 0;
772 (base_gfn >= slot->base_gfn + slot->npages))) 815 goto out;
773 goto out_free; 816 }
817 }
818 } else if (old.npages) {
819 change = KVM_MR_DELETE;
820 } else /* Modify a non-existent slot: disallowed. */
821 goto out;
822
823 if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
824 /* Check for overlaps */
825 r = -EEXIST;
826 kvm_for_each_memslot(slot, kvm->memslots) {
827 if ((slot->id >= KVM_USER_MEM_SLOTS) ||
828 (slot->id == mem->slot))
829 continue;
830 if (!((base_gfn + npages <= slot->base_gfn) ||
831 (base_gfn >= slot->base_gfn + slot->npages)))
832 goto out;
833 }
774 } 834 }
775 835
776 /* Free page dirty bitmap if unneeded */ 836 /* Free page dirty bitmap if unneeded */
@@ -778,10 +838,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
778 new.dirty_bitmap = NULL; 838 new.dirty_bitmap = NULL;
779 839
780 r = -ENOMEM; 840 r = -ENOMEM;
781 841 if (change == KVM_MR_CREATE) {
782 /* Allocate if a slot is being created */
783 if (npages && !old.npages) {
784 new.user_alloc = user_alloc;
785 new.userspace_addr = mem->userspace_addr; 842 new.userspace_addr = mem->userspace_addr;
786 843
787 if (kvm_arch_create_memslot(&new, npages)) 844 if (kvm_arch_create_memslot(&new, npages))
@@ -792,12 +849,9 @@ int __kvm_set_memory_region(struct kvm *kvm,
792 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { 849 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
793 if (kvm_create_dirty_bitmap(&new) < 0) 850 if (kvm_create_dirty_bitmap(&new) < 0)
794 goto out_free; 851 goto out_free;
795 /* destroy any largepage mappings for dirty tracking */
796 } 852 }
797 853
798 if (!npages || base_gfn != old.base_gfn) { 854 if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) {
799 struct kvm_memory_slot *slot;
800
801 r = -ENOMEM; 855 r = -ENOMEM;
802 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots), 856 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
803 GFP_KERNEL); 857 GFP_KERNEL);
@@ -806,11 +860,10 @@ int __kvm_set_memory_region(struct kvm *kvm,
806 slot = id_to_memslot(slots, mem->slot); 860 slot = id_to_memslot(slots, mem->slot);
807 slot->flags |= KVM_MEMSLOT_INVALID; 861 slot->flags |= KVM_MEMSLOT_INVALID;
808 862
809 update_memslots(slots, NULL); 863 old_memslots = install_new_memslots(kvm, slots, NULL);
810 864
811 old_memslots = kvm->memslots; 865 /* slot was deleted or moved, clear iommu mapping */
812 rcu_assign_pointer(kvm->memslots, slots); 866 kvm_iommu_unmap_pages(kvm, &old);
813 synchronize_srcu_expedited(&kvm->srcu);
814 /* From this point no new shadow pages pointing to a deleted, 867 /* From this point no new shadow pages pointing to a deleted,
815 * or moved, memslot will be created. 868 * or moved, memslot will be created.
816 * 869 *
@@ -819,37 +872,48 @@ int __kvm_set_memory_region(struct kvm *kvm,
819 * - kvm_is_visible_gfn (mmu_check_roots) 872 * - kvm_is_visible_gfn (mmu_check_roots)
820 */ 873 */
821 kvm_arch_flush_shadow_memslot(kvm, slot); 874 kvm_arch_flush_shadow_memslot(kvm, slot);
822 kfree(old_memslots); 875 slots = old_memslots;
823 } 876 }
824 877
825 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc); 878 r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
826 if (r) 879 if (r)
827 goto out_free; 880 goto out_slots;
828 881
829 /* map/unmap the pages in iommu page table */ 882 r = -ENOMEM;
830 if (npages) { 883 /*
831 r = kvm_iommu_map_pages(kvm, &new); 884 * We can re-use the old_memslots from above, the only difference
832 if (r) 885 * from the currently installed memslots is the invalid flag. This
886 * will get overwritten by update_memslots anyway.
887 */
888 if (!slots) {
889 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
890 GFP_KERNEL);
891 if (!slots)
833 goto out_free; 892 goto out_free;
834 } else 893 }
835 kvm_iommu_unmap_pages(kvm, &old);
836 894
837 r = -ENOMEM; 895 /*
838 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots), 896 * IOMMU mapping: New slots need to be mapped. Old slots need to be
839 GFP_KERNEL); 897 * un-mapped and re-mapped if their base changes. Since base change
840 if (!slots) 898 * unmapping is handled above with slot deletion, mapping alone is
841 goto out_free; 899 * needed here. Anything else the iommu might care about for existing
900 * slots (size changes, userspace addr changes and read-only flag
901 * changes) is disallowed above, so any other attribute changes getting
902 * here can be skipped.
903 */
904 if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
905 r = kvm_iommu_map_pages(kvm, &new);
906 if (r)
907 goto out_slots;
908 }
842 909
843 /* actual memory is freed via old in kvm_free_physmem_slot below */ 910 /* actual memory is freed via old in kvm_free_physmem_slot below */
844 if (!npages) { 911 if (change == KVM_MR_DELETE) {
845 new.dirty_bitmap = NULL; 912 new.dirty_bitmap = NULL;
846 memset(&new.arch, 0, sizeof(new.arch)); 913 memset(&new.arch, 0, sizeof(new.arch));
847 } 914 }
848 915
849 update_memslots(slots, &new); 916 old_memslots = install_new_memslots(kvm, slots, &new);
850 old_memslots = kvm->memslots;
851 rcu_assign_pointer(kvm->memslots, slots);
852 synchronize_srcu_expedited(&kvm->srcu);
853 917
854 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc); 918 kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
855 919
@@ -858,17 +922,18 @@ int __kvm_set_memory_region(struct kvm *kvm,
858 922
859 return 0; 923 return 0;
860 924
925out_slots:
926 kfree(slots);
861out_free: 927out_free:
862 kvm_free_physmem_slot(&new, &old); 928 kvm_free_physmem_slot(&new, &old);
863out: 929out:
864 return r; 930 return r;
865
866} 931}
867EXPORT_SYMBOL_GPL(__kvm_set_memory_region); 932EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
868 933
869int kvm_set_memory_region(struct kvm *kvm, 934int kvm_set_memory_region(struct kvm *kvm,
870 struct kvm_userspace_memory_region *mem, 935 struct kvm_userspace_memory_region *mem,
871 int user_alloc) 936 bool user_alloc)
872{ 937{
873 int r; 938 int r;
874 939
@@ -882,9 +947,9 @@ EXPORT_SYMBOL_GPL(kvm_set_memory_region);
882int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, 947int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
883 struct 948 struct
884 kvm_userspace_memory_region *mem, 949 kvm_userspace_memory_region *mem,
885 int user_alloc) 950 bool user_alloc)
886{ 951{
887 if (mem->slot >= KVM_MEMORY_SLOTS) 952 if (mem->slot >= KVM_USER_MEM_SLOTS)
888 return -EINVAL; 953 return -EINVAL;
889 return kvm_set_memory_region(kvm, mem, user_alloc); 954 return kvm_set_memory_region(kvm, mem, user_alloc);
890} 955}
@@ -898,7 +963,7 @@ int kvm_get_dirty_log(struct kvm *kvm,
898 unsigned long any = 0; 963 unsigned long any = 0;
899 964
900 r = -EINVAL; 965 r = -EINVAL;
901 if (log->slot >= KVM_MEMORY_SLOTS) 966 if (log->slot >= KVM_USER_MEM_SLOTS)
902 goto out; 967 goto out;
903 968
904 memslot = id_to_memslot(kvm->memslots, log->slot); 969 memslot = id_to_memslot(kvm->memslots, log->slot);
@@ -944,7 +1009,7 @@ int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
944{ 1009{
945 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn); 1010 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
946 1011
947 if (!memslot || memslot->id >= KVM_MEMORY_SLOTS || 1012 if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS ||
948 memslot->flags & KVM_MEMSLOT_INVALID) 1013 memslot->flags & KVM_MEMSLOT_INVALID)
949 return 0; 1014 return 0;
950 1015
@@ -1641,6 +1706,7 @@ bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
1641{ 1706{
1642 struct pid *pid; 1707 struct pid *pid;
1643 struct task_struct *task = NULL; 1708 struct task_struct *task = NULL;
1709 bool ret = false;
1644 1710
1645 rcu_read_lock(); 1711 rcu_read_lock();
1646 pid = rcu_dereference(target->pid); 1712 pid = rcu_dereference(target->pid);
@@ -1648,17 +1714,15 @@ bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
1648 task = get_pid_task(target->pid, PIDTYPE_PID); 1714 task = get_pid_task(target->pid, PIDTYPE_PID);
1649 rcu_read_unlock(); 1715 rcu_read_unlock();
1650 if (!task) 1716 if (!task)
1651 return false; 1717 return ret;
1652 if (task->flags & PF_VCPU) { 1718 if (task->flags & PF_VCPU) {
1653 put_task_struct(task); 1719 put_task_struct(task);
1654 return false; 1720 return ret;
1655 }
1656 if (yield_to(task, 1)) {
1657 put_task_struct(task);
1658 return true;
1659 } 1721 }
1722 ret = yield_to(task, 1);
1660 put_task_struct(task); 1723 put_task_struct(task);
1661 return false; 1724
1725 return ret;
1662} 1726}
1663EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to); 1727EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
1664 1728
@@ -1699,12 +1763,14 @@ bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
1699 return eligible; 1763 return eligible;
1700} 1764}
1701#endif 1765#endif
1766
1702void kvm_vcpu_on_spin(struct kvm_vcpu *me) 1767void kvm_vcpu_on_spin(struct kvm_vcpu *me)
1703{ 1768{
1704 struct kvm *kvm = me->kvm; 1769 struct kvm *kvm = me->kvm;
1705 struct kvm_vcpu *vcpu; 1770 struct kvm_vcpu *vcpu;
1706 int last_boosted_vcpu = me->kvm->last_boosted_vcpu; 1771 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1707 int yielded = 0; 1772 int yielded = 0;
1773 int try = 3;
1708 int pass; 1774 int pass;
1709 int i; 1775 int i;
1710 1776
@@ -1716,7 +1782,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
1716 * VCPU is holding the lock that we need and will release it. 1782 * VCPU is holding the lock that we need and will release it.
1717 * We approximate round-robin by starting at the last boosted VCPU. 1783 * We approximate round-robin by starting at the last boosted VCPU.
1718 */ 1784 */
1719 for (pass = 0; pass < 2 && !yielded; pass++) { 1785 for (pass = 0; pass < 2 && !yielded && try; pass++) {
1720 kvm_for_each_vcpu(i, vcpu, kvm) { 1786 kvm_for_each_vcpu(i, vcpu, kvm) {
1721 if (!pass && i <= last_boosted_vcpu) { 1787 if (!pass && i <= last_boosted_vcpu) {
1722 i = last_boosted_vcpu; 1788 i = last_boosted_vcpu;
@@ -1729,10 +1795,15 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
1729 continue; 1795 continue;
1730 if (!kvm_vcpu_eligible_for_directed_yield(vcpu)) 1796 if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
1731 continue; 1797 continue;
1732 if (kvm_vcpu_yield_to(vcpu)) { 1798
1799 yielded = kvm_vcpu_yield_to(vcpu);
1800 if (yielded > 0) {
1733 kvm->last_boosted_vcpu = i; 1801 kvm->last_boosted_vcpu = i;
1734 yielded = 1;
1735 break; 1802 break;
1803 } else if (yielded < 0) {
1804 try--;
1805 if (!try)
1806 break;
1736 } 1807 }
1737 } 1808 }
1738 } 1809 }
@@ -2127,7 +2198,7 @@ static long kvm_vm_ioctl(struct file *filp,
2127 sizeof kvm_userspace_mem)) 2198 sizeof kvm_userspace_mem))
2128 goto out; 2199 goto out;
2129 2200
2130 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); 2201 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, true);
2131 break; 2202 break;
2132 } 2203 }
2133 case KVM_GET_DIRTY_LOG: { 2204 case KVM_GET_DIRTY_LOG: {