Lines Matching refs:kvm

146 static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm);
152 __weak void kvm_arch_guest_memory_reclaimed(struct kvm *kvm) in kvm_arch_guest_memory_reclaimed() argument
285 bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req, in kvm_make_vcpus_request_mask() argument
299 vcpu = kvm_get_vcpu(kvm, i); in kvm_make_vcpus_request_mask()
311 bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req) in kvm_make_all_cpus_request() argument
324 kvm_for_each_vcpu(i, vcpu, kvm) in kvm_make_all_cpus_request()
334 void kvm_flush_remote_tlbs(struct kvm *kvm) in kvm_flush_remote_tlbs() argument
336 ++kvm->stat.generic.remote_tlb_flush_requests; in kvm_flush_remote_tlbs()
349 if (!kvm_arch_flush_remote_tlbs(kvm) in kvm_flush_remote_tlbs()
350 || kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH)) in kvm_flush_remote_tlbs()
351 ++kvm->stat.generic.remote_tlb_flush; in kvm_flush_remote_tlbs()
355 void kvm_flush_remote_tlbs_range(struct kvm *kvm, gfn_t gfn, u64 nr_pages) in kvm_flush_remote_tlbs_range() argument
357 if (!kvm_arch_flush_remote_tlbs_range(kvm, gfn, nr_pages)) in kvm_flush_remote_tlbs_range()
365 kvm_flush_remote_tlbs(kvm); in kvm_flush_remote_tlbs_range()
368 void kvm_flush_remote_tlbs_memslot(struct kvm *kvm, in kvm_flush_remote_tlbs_memslot() argument
378 lockdep_assert_held(&kvm->slots_lock); in kvm_flush_remote_tlbs_memslot()
379 kvm_flush_remote_tlbs_range(kvm, memslot->base_gfn, memslot->npages); in kvm_flush_remote_tlbs_memslot()
382 static void kvm_flush_shadow_all(struct kvm *kvm) in kvm_flush_shadow_all() argument
384 kvm_arch_flush_shadow_all(kvm); in kvm_flush_shadow_all()
385 kvm_arch_guest_memory_reclaimed(kvm); in kvm_flush_shadow_all()
482 static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) in kvm_vcpu_init() argument
486 vcpu->kvm = kvm; in kvm_vcpu_init()
522 void kvm_destroy_vcpus(struct kvm *kvm) in kvm_destroy_vcpus() argument
527 kvm_for_each_vcpu(i, vcpu, kvm) { in kvm_destroy_vcpus()
529 xa_erase(&kvm->vcpu_array, i); in kvm_destroy_vcpus()
532 atomic_set(&kvm->online_vcpus, 0); in kvm_destroy_vcpus()
537 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) in mmu_notifier_to_kvm()
539 return container_of(mn, struct kvm, mmu_notifier); in mmu_notifier_to_kvm()
542 typedef bool (*gfn_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range);
544 typedef void (*on_lock_fn_t)(struct kvm *kvm);
592 static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, in __kvm_handle_hva_range() argument
612 idx = srcu_read_lock(&kvm->srcu); in __kvm_handle_hva_range()
614 for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { in __kvm_handle_hva_range()
617 slots = __kvm_memslots(kvm, i); in __kvm_handle_hva_range()
646 KVM_MMU_LOCK(kvm); in __kvm_handle_hva_range()
648 range->on_lock(kvm); in __kvm_handle_hva_range()
653 r.ret |= range->handler(kvm, &gfn_range); in __kvm_handle_hva_range()
658 kvm_flush_remote_tlbs(kvm); in __kvm_handle_hva_range()
662 KVM_MMU_UNLOCK(kvm); in __kvm_handle_hva_range()
664 srcu_read_unlock(&kvm->srcu, idx); in __kvm_handle_hva_range()
674 struct kvm *kvm = mmu_notifier_to_kvm(mn); in kvm_handle_hva_range() local
684 return __kvm_handle_hva_range(kvm, &range).ret; in kvm_handle_hva_range()
692 struct kvm *kvm = mmu_notifier_to_kvm(mn); in kvm_handle_hva_range_no_flush() local
702 return __kvm_handle_hva_range(kvm, &range).ret; in kvm_handle_hva_range_no_flush()
705 void kvm_mmu_invalidate_begin(struct kvm *kvm) in kvm_mmu_invalidate_begin() argument
707 lockdep_assert_held_write(&kvm->mmu_lock); in kvm_mmu_invalidate_begin()
713 kvm->mmu_invalidate_in_progress++; in kvm_mmu_invalidate_begin()
715 if (likely(kvm->mmu_invalidate_in_progress == 1)) { in kvm_mmu_invalidate_begin()
716 kvm->mmu_invalidate_range_start = INVALID_GPA; in kvm_mmu_invalidate_begin()
717 kvm->mmu_invalidate_range_end = INVALID_GPA; in kvm_mmu_invalidate_begin()
721 void kvm_mmu_invalidate_range_add(struct kvm *kvm, gfn_t start, gfn_t end) in kvm_mmu_invalidate_range_add() argument
723 lockdep_assert_held_write(&kvm->mmu_lock); in kvm_mmu_invalidate_range_add()
725 WARN_ON_ONCE(!kvm->mmu_invalidate_in_progress); in kvm_mmu_invalidate_range_add()
727 if (likely(kvm->mmu_invalidate_range_start == INVALID_GPA)) { in kvm_mmu_invalidate_range_add()
728 kvm->mmu_invalidate_range_start = start; in kvm_mmu_invalidate_range_add()
729 kvm->mmu_invalidate_range_end = end; in kvm_mmu_invalidate_range_add()
740 kvm->mmu_invalidate_range_start = in kvm_mmu_invalidate_range_add()
741 min(kvm->mmu_invalidate_range_start, start); in kvm_mmu_invalidate_range_add()
742 kvm->mmu_invalidate_range_end = in kvm_mmu_invalidate_range_add()
743 max(kvm->mmu_invalidate_range_end, end); in kvm_mmu_invalidate_range_add()
747 bool kvm_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) in kvm_mmu_unmap_gfn_range() argument
749 kvm_mmu_invalidate_range_add(kvm, range->start, range->end); in kvm_mmu_unmap_gfn_range()
750 return kvm_unmap_gfn_range(kvm, range); in kvm_mmu_unmap_gfn_range()
756 struct kvm *kvm = mmu_notifier_to_kvm(mn); in kvm_mmu_notifier_invalidate_range_start() local
776 spin_lock(&kvm->mn_invalidate_lock); in kvm_mmu_notifier_invalidate_range_start()
777 kvm->mn_active_invalidate_count++; in kvm_mmu_notifier_invalidate_range_start()
778 spin_unlock(&kvm->mn_invalidate_lock); in kvm_mmu_notifier_invalidate_range_start()
790 gfn_to_pfn_cache_invalidate_start(kvm, range->start, range->end); in kvm_mmu_notifier_invalidate_range_start()
797 if (__kvm_handle_hva_range(kvm, &hva_range).found_memslot) in kvm_mmu_notifier_invalidate_range_start()
798 kvm_arch_guest_memory_reclaimed(kvm); in kvm_mmu_notifier_invalidate_range_start()
803 void kvm_mmu_invalidate_end(struct kvm *kvm) in kvm_mmu_invalidate_end() argument
805 lockdep_assert_held_write(&kvm->mmu_lock); in kvm_mmu_invalidate_end()
812 kvm->mmu_invalidate_seq++; in kvm_mmu_invalidate_end()
819 kvm->mmu_invalidate_in_progress--; in kvm_mmu_invalidate_end()
820 KVM_BUG_ON(kvm->mmu_invalidate_in_progress < 0, kvm); in kvm_mmu_invalidate_end()
826 WARN_ON_ONCE(kvm->mmu_invalidate_range_start == INVALID_GPA); in kvm_mmu_invalidate_end()
832 struct kvm *kvm = mmu_notifier_to_kvm(mn); in kvm_mmu_notifier_invalidate_range_end() local
843 __kvm_handle_hva_range(kvm, &hva_range); in kvm_mmu_notifier_invalidate_range_end()
846 spin_lock(&kvm->mn_invalidate_lock); in kvm_mmu_notifier_invalidate_range_end()
847 if (!WARN_ON_ONCE(!kvm->mn_active_invalidate_count)) in kvm_mmu_notifier_invalidate_range_end()
848 --kvm->mn_active_invalidate_count; in kvm_mmu_notifier_invalidate_range_end()
849 wake = !kvm->mn_active_invalidate_count; in kvm_mmu_notifier_invalidate_range_end()
850 spin_unlock(&kvm->mn_invalidate_lock); in kvm_mmu_notifier_invalidate_range_end()
857 rcuwait_wake_up(&kvm->mn_memslots_update_rcuwait); in kvm_mmu_notifier_invalidate_range_end()
906 struct kvm *kvm = mmu_notifier_to_kvm(mn); in kvm_mmu_notifier_release() local
909 idx = srcu_read_lock(&kvm->srcu); in kvm_mmu_notifier_release()
910 kvm_flush_shadow_all(kvm); in kvm_mmu_notifier_release()
911 srcu_read_unlock(&kvm->srcu, idx); in kvm_mmu_notifier_release()
923 static int kvm_init_mmu_notifier(struct kvm *kvm) in kvm_init_mmu_notifier() argument
925 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; in kvm_init_mmu_notifier()
926 return mmu_notifier_register(&kvm->mmu_notifier, current->mm); in kvm_init_mmu_notifier()
931 static int kvm_init_mmu_notifier(struct kvm *kvm) in kvm_init_mmu_notifier() argument
943 struct kvm *kvm = container_of(bl, struct kvm, pm_notifier); in kvm_pm_notifier_call() local
945 return kvm_arch_pm_notifier(kvm, state); in kvm_pm_notifier_call()
948 static void kvm_init_pm_notifier(struct kvm *kvm) in kvm_init_pm_notifier() argument
950 kvm->pm_notifier.notifier_call = kvm_pm_notifier_call; in kvm_init_pm_notifier()
952 kvm->pm_notifier.priority = INT_MAX; in kvm_init_pm_notifier()
953 register_pm_notifier(&kvm->pm_notifier); in kvm_init_pm_notifier()
956 static void kvm_destroy_pm_notifier(struct kvm *kvm) in kvm_destroy_pm_notifier() argument
958 unregister_pm_notifier(&kvm->pm_notifier); in kvm_destroy_pm_notifier()
961 static void kvm_init_pm_notifier(struct kvm *kvm) in kvm_init_pm_notifier() argument
965 static void kvm_destroy_pm_notifier(struct kvm *kvm) in kvm_destroy_pm_notifier() argument
980 static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) in kvm_free_memslot() argument
987 kvm_arch_free_memslot(kvm, slot); in kvm_free_memslot()
992 static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots) in kvm_free_memslots() argument
1008 kvm_free_memslot(kvm, memslot); in kvm_free_memslots()
1024 static void kvm_destroy_vm_debugfs(struct kvm *kvm) in kvm_destroy_vm_debugfs() argument
1030 if (IS_ERR(kvm->debugfs_dentry)) in kvm_destroy_vm_debugfs()
1033 debugfs_remove_recursive(kvm->debugfs_dentry); in kvm_destroy_vm_debugfs()
1035 if (kvm->debugfs_stat_data) { in kvm_destroy_vm_debugfs()
1037 kfree(kvm->debugfs_stat_data[i]); in kvm_destroy_vm_debugfs()
1038 kfree(kvm->debugfs_stat_data); in kvm_destroy_vm_debugfs()
1042 static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname) in kvm_create_vm_debugfs() argument
1070 kvm->debugfs_dentry = dent; in kvm_create_vm_debugfs()
1071 kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries, in kvm_create_vm_debugfs()
1072 sizeof(*kvm->debugfs_stat_data), in kvm_create_vm_debugfs()
1074 if (!kvm->debugfs_stat_data) in kvm_create_vm_debugfs()
1083 stat_data->kvm = kvm; in kvm_create_vm_debugfs()
1086 kvm->debugfs_stat_data[i] = stat_data; in kvm_create_vm_debugfs()
1088 kvm->debugfs_dentry, stat_data, in kvm_create_vm_debugfs()
1098 stat_data->kvm = kvm; in kvm_create_vm_debugfs()
1101 kvm->debugfs_stat_data[i + kvm_vm_stats_header.num_desc] = stat_data; in kvm_create_vm_debugfs()
1103 kvm->debugfs_dentry, stat_data, in kvm_create_vm_debugfs()
1107 kvm_arch_create_vm_debugfs(kvm); in kvm_create_vm_debugfs()
1110 kvm_destroy_vm_debugfs(kvm); in kvm_create_vm_debugfs()
1118 int __weak kvm_arch_post_init_vm(struct kvm *kvm) in kvm_arch_post_init_vm() argument
1127 void __weak kvm_arch_pre_destroy_vm(struct kvm *kvm) in kvm_arch_pre_destroy_vm() argument
1137 void __weak kvm_arch_create_vm_debugfs(struct kvm *kvm) in kvm_arch_create_vm_debugfs() argument
1141 static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) in kvm_create_vm()
1143 struct kvm *kvm = kvm_arch_alloc_vm(); in kvm_create_vm() local
1147 if (!kvm) in kvm_create_vm()
1150 KVM_MMU_LOCK_INIT(kvm); in kvm_create_vm()
1152 kvm->mm = current->mm; in kvm_create_vm()
1153 kvm_eventfd_init(kvm); in kvm_create_vm()
1154 mutex_init(&kvm->lock); in kvm_create_vm()
1155 mutex_init(&kvm->irq_lock); in kvm_create_vm()
1156 mutex_init(&kvm->slots_lock); in kvm_create_vm()
1157 mutex_init(&kvm->slots_arch_lock); in kvm_create_vm()
1158 spin_lock_init(&kvm->mn_invalidate_lock); in kvm_create_vm()
1159 rcuwait_init(&kvm->mn_memslots_update_rcuwait); in kvm_create_vm()
1160 xa_init(&kvm->vcpu_array); in kvm_create_vm()
1162 xa_init(&kvm->mem_attr_array); in kvm_create_vm()
1165 INIT_LIST_HEAD(&kvm->gpc_list); in kvm_create_vm()
1166 spin_lock_init(&kvm->gpc_lock); in kvm_create_vm()
1168 INIT_LIST_HEAD(&kvm->devices); in kvm_create_vm()
1169 kvm->max_vcpus = KVM_MAX_VCPUS; in kvm_create_vm()
1177 kvm->debugfs_dentry = ERR_PTR(-ENOENT); in kvm_create_vm()
1179 snprintf(kvm->stats_id, sizeof(kvm->stats_id), "kvm-%d", in kvm_create_vm()
1183 if (init_srcu_struct(&kvm->srcu)) in kvm_create_vm()
1185 if (init_srcu_struct(&kvm->irq_srcu)) in kvm_create_vm()
1188 r = kvm_init_irq_routing(kvm); in kvm_create_vm()
1192 refcount_set(&kvm->users_count, 1); in kvm_create_vm()
1194 for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { in kvm_create_vm()
1196 slots = &kvm->__memslots[i][j]; in kvm_create_vm()
1208 rcu_assign_pointer(kvm->memslots[i], &kvm->__memslots[i][0]); in kvm_create_vm()
1213 rcu_assign_pointer(kvm->buses[i], in kvm_create_vm()
1215 if (!kvm->buses[i]) in kvm_create_vm()
1219 r = kvm_arch_init_vm(kvm, type); in kvm_create_vm()
1228 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); in kvm_create_vm()
1231 r = kvm_init_mmu_notifier(kvm); in kvm_create_vm()
1235 r = kvm_coalesced_mmio_init(kvm); in kvm_create_vm()
1239 r = kvm_create_vm_debugfs(kvm, fdname); in kvm_create_vm()
1243 r = kvm_arch_post_init_vm(kvm); in kvm_create_vm()
1248 list_add(&kvm->vm_list, &vm_list); in kvm_create_vm()
1252 kvm_init_pm_notifier(kvm); in kvm_create_vm()
1254 return kvm; in kvm_create_vm()
1257 kvm_destroy_vm_debugfs(kvm); in kvm_create_vm()
1259 kvm_coalesced_mmio_free(kvm); in kvm_create_vm()
1262 if (kvm->mmu_notifier.ops) in kvm_create_vm()
1263 mmu_notifier_unregister(&kvm->mmu_notifier, current->mm); in kvm_create_vm()
1268 kvm_arch_destroy_vm(kvm); in kvm_create_vm()
1270 WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count)); in kvm_create_vm()
1272 kfree(kvm_get_bus(kvm, i)); in kvm_create_vm()
1273 kvm_free_irq_routing(kvm); in kvm_create_vm()
1275 cleanup_srcu_struct(&kvm->irq_srcu); in kvm_create_vm()
1277 cleanup_srcu_struct(&kvm->srcu); in kvm_create_vm()
1279 kvm_arch_free_vm(kvm); in kvm_create_vm()
1284 static void kvm_destroy_devices(struct kvm *kvm) in kvm_destroy_devices() argument
1299 list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) { in kvm_destroy_devices()
1305 static void kvm_destroy_vm(struct kvm *kvm) in kvm_destroy_vm() argument
1308 struct mm_struct *mm = kvm->mm; in kvm_destroy_vm()
1310 kvm_destroy_pm_notifier(kvm); in kvm_destroy_vm()
1311 kvm_uevent_notify_change(KVM_EVENT_DESTROY_VM, kvm); in kvm_destroy_vm()
1312 kvm_destroy_vm_debugfs(kvm); in kvm_destroy_vm()
1313 kvm_arch_sync_events(kvm); in kvm_destroy_vm()
1315 list_del(&kvm->vm_list); in kvm_destroy_vm()
1317 kvm_arch_pre_destroy_vm(kvm); in kvm_destroy_vm()
1319 kvm_free_irq_routing(kvm); in kvm_destroy_vm()
1321 struct kvm_io_bus *bus = kvm_get_bus(kvm, i); in kvm_destroy_vm()
1325 kvm->buses[i] = NULL; in kvm_destroy_vm()
1327 kvm_coalesced_mmio_free(kvm); in kvm_destroy_vm()
1329 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); in kvm_destroy_vm()
1342 WARN_ON(rcuwait_active(&kvm->mn_memslots_update_rcuwait)); in kvm_destroy_vm()
1343 if (kvm->mn_active_invalidate_count) in kvm_destroy_vm()
1344 kvm->mn_active_invalidate_count = 0; in kvm_destroy_vm()
1346 WARN_ON(kvm->mmu_invalidate_in_progress); in kvm_destroy_vm()
1348 kvm_flush_shadow_all(kvm); in kvm_destroy_vm()
1350 kvm_arch_destroy_vm(kvm); in kvm_destroy_vm()
1351 kvm_destroy_devices(kvm); in kvm_destroy_vm()
1352 for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { in kvm_destroy_vm()
1353 kvm_free_memslots(kvm, &kvm->__memslots[i][0]); in kvm_destroy_vm()
1354 kvm_free_memslots(kvm, &kvm->__memslots[i][1]); in kvm_destroy_vm()
1356 cleanup_srcu_struct(&kvm->irq_srcu); in kvm_destroy_vm()
1357 cleanup_srcu_struct(&kvm->srcu); in kvm_destroy_vm()
1359 xa_destroy(&kvm->mem_attr_array); in kvm_destroy_vm()
1361 kvm_arch_free_vm(kvm); in kvm_destroy_vm()
1367 void kvm_get_kvm(struct kvm *kvm) in kvm_get_kvm() argument
1369 refcount_inc(&kvm->users_count); in kvm_get_kvm()
1377 bool kvm_get_kvm_safe(struct kvm *kvm) in kvm_get_kvm_safe() argument
1379 return refcount_inc_not_zero(&kvm->users_count); in kvm_get_kvm_safe()
1383 void kvm_put_kvm(struct kvm *kvm) in kvm_put_kvm() argument
1385 if (refcount_dec_and_test(&kvm->users_count)) in kvm_put_kvm()
1386 kvm_destroy_vm(kvm); in kvm_put_kvm()
1397 void kvm_put_kvm_no_destroy(struct kvm *kvm) in kvm_put_kvm_no_destroy() argument
1399 WARN_ON(refcount_dec_and_test(&kvm->users_count)); in kvm_put_kvm_no_destroy()
1405 struct kvm *kvm = filp->private_data; in kvm_vm_release() local
1407 kvm_irqfd_release(kvm); in kvm_vm_release()
1409 kvm_put_kvm(kvm); in kvm_vm_release()
1428 static struct kvm_memslots *kvm_get_inactive_memslots(struct kvm *kvm, int as_id) in kvm_get_inactive_memslots() argument
1430 struct kvm_memslots *active = __kvm_memslots(kvm, as_id); in kvm_get_inactive_memslots()
1433 return &kvm->__memslots[as_id][node_idx_inactive]; in kvm_get_inactive_memslots()
1508 static void kvm_replace_memslot(struct kvm *kvm, in kvm_replace_memslot() argument
1513 struct kvm_memslots *slots = kvm_get_inactive_memslots(kvm, as_id); in kvm_replace_memslot()
1569 static int check_memory_region_flags(struct kvm *kvm, in check_memory_region_flags() argument
1574 if (kvm_arch_has_private_mem(kvm)) in check_memory_region_flags()
1586 if (kvm_arch_has_readonly_mem(kvm) && in check_memory_region_flags()
1596 static void kvm_swap_active_memslots(struct kvm *kvm, int as_id) in kvm_swap_active_memslots() argument
1598 struct kvm_memslots *slots = kvm_get_inactive_memslots(kvm, as_id); in kvm_swap_active_memslots()
1601 u64 gen = __kvm_memslots(kvm, as_id)->generation; in kvm_swap_active_memslots()
1611 spin_lock(&kvm->mn_invalidate_lock); in kvm_swap_active_memslots()
1612 prepare_to_rcuwait(&kvm->mn_memslots_update_rcuwait); in kvm_swap_active_memslots()
1613 while (kvm->mn_active_invalidate_count) { in kvm_swap_active_memslots()
1615 spin_unlock(&kvm->mn_invalidate_lock); in kvm_swap_active_memslots()
1617 spin_lock(&kvm->mn_invalidate_lock); in kvm_swap_active_memslots()
1619 finish_rcuwait(&kvm->mn_memslots_update_rcuwait); in kvm_swap_active_memslots()
1620 rcu_assign_pointer(kvm->memslots[as_id], slots); in kvm_swap_active_memslots()
1621 spin_unlock(&kvm->mn_invalidate_lock); in kvm_swap_active_memslots()
1628 mutex_unlock(&kvm->slots_arch_lock); in kvm_swap_active_memslots()
1630 synchronize_srcu_expedited(&kvm->srcu); in kvm_swap_active_memslots()
1647 gen += kvm_arch_nr_memslot_as_ids(kvm); in kvm_swap_active_memslots()
1649 kvm_arch_memslots_updated(kvm, gen); in kvm_swap_active_memslots()
1654 static int kvm_prepare_memory_region(struct kvm *kvm, in kvm_prepare_memory_region() argument
1673 else if (kvm_use_dirty_bitmap(kvm)) { in kvm_prepare_memory_region()
1678 if (kvm_dirty_log_manual_protect_and_init_set(kvm)) in kvm_prepare_memory_region()
1683 r = kvm_arch_prepare_memory_region(kvm, old, new, change); in kvm_prepare_memory_region()
1692 static void kvm_commit_memory_region(struct kvm *kvm, in kvm_commit_memory_region() argument
1704 kvm->nr_memslot_pages -= old->npages; in kvm_commit_memory_region()
1706 kvm->nr_memslot_pages += new->npages; in kvm_commit_memory_region()
1710 atomic_set(&kvm->nr_memslots_dirty_logging, in kvm_commit_memory_region()
1711 atomic_read(&kvm->nr_memslots_dirty_logging) + change); in kvm_commit_memory_region()
1714 kvm_arch_commit_memory_region(kvm, old, new, change); in kvm_commit_memory_region()
1722 kvm_free_memslot(kvm, old); in kvm_commit_memory_region()
1754 static void kvm_activate_memslot(struct kvm *kvm, in kvm_activate_memslot() argument
1760 kvm_swap_active_memslots(kvm, as_id); in kvm_activate_memslot()
1763 kvm_replace_memslot(kvm, old, new); in kvm_activate_memslot()
1779 static void kvm_invalidate_memslot(struct kvm *kvm, in kvm_invalidate_memslot() argument
1790 kvm_replace_memslot(kvm, old, invalid_slot); in kvm_invalidate_memslot()
1797 kvm_swap_active_memslots(kvm, old->as_id); in kvm_invalidate_memslot()
1805 kvm_arch_flush_shadow_memslot(kvm, old); in kvm_invalidate_memslot()
1806 kvm_arch_guest_memory_reclaimed(kvm); in kvm_invalidate_memslot()
1809 mutex_lock(&kvm->slots_arch_lock); in kvm_invalidate_memslot()
1821 static void kvm_create_memslot(struct kvm *kvm, in kvm_create_memslot() argument
1825 kvm_replace_memslot(kvm, NULL, new); in kvm_create_memslot()
1826 kvm_activate_memslot(kvm, NULL, new); in kvm_create_memslot()
1829 static void kvm_delete_memslot(struct kvm *kvm, in kvm_delete_memslot() argument
1837 kvm_replace_memslot(kvm, old, NULL); in kvm_delete_memslot()
1838 kvm_activate_memslot(kvm, invalid_slot, NULL); in kvm_delete_memslot()
1841 static void kvm_move_memslot(struct kvm *kvm, in kvm_move_memslot() argument
1850 kvm_replace_memslot(kvm, old, new); in kvm_move_memslot()
1851 kvm_activate_memslot(kvm, invalid_slot, new); in kvm_move_memslot()
1854 static void kvm_update_flags_memslot(struct kvm *kvm, in kvm_update_flags_memslot() argument
1863 kvm_replace_memslot(kvm, old, new); in kvm_update_flags_memslot()
1864 kvm_activate_memslot(kvm, old, new); in kvm_update_flags_memslot()
1867 static int kvm_set_memslot(struct kvm *kvm, in kvm_set_memslot() argument
1889 mutex_lock(&kvm->slots_arch_lock); in kvm_set_memslot()
1907 mutex_unlock(&kvm->slots_arch_lock); in kvm_set_memslot()
1910 kvm_invalidate_memslot(kvm, old, invalid_slot); in kvm_set_memslot()
1913 r = kvm_prepare_memory_region(kvm, old, new, change); in kvm_set_memslot()
1922 kvm_activate_memslot(kvm, invalid_slot, old); in kvm_set_memslot()
1925 mutex_unlock(&kvm->slots_arch_lock); in kvm_set_memslot()
1938 kvm_create_memslot(kvm, new); in kvm_set_memslot()
1940 kvm_delete_memslot(kvm, old, invalid_slot); in kvm_set_memslot()
1942 kvm_move_memslot(kvm, old, new, invalid_slot); in kvm_set_memslot()
1944 kvm_update_flags_memslot(kvm, old, new); in kvm_set_memslot()
1957 kvm_commit_memory_region(kvm, old, new, change); in kvm_set_memslot()
1983 int __kvm_set_memory_region(struct kvm *kvm, in __kvm_set_memory_region() argument
1994 r = check_memory_region_flags(kvm, mem); in __kvm_set_memory_region()
2017 if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_MEM_SLOTS_NUM) in __kvm_set_memory_region()
2024 slots = __kvm_memslots(kvm, as_id); in __kvm_set_memory_region()
2036 if (WARN_ON_ONCE(kvm->nr_memslot_pages < old->npages)) in __kvm_set_memory_region()
2039 return kvm_set_memslot(kvm, old, NULL, KVM_MR_DELETE); in __kvm_set_memory_region()
2052 if ((kvm->nr_memslot_pages + npages) < kvm->nr_memslot_pages) in __kvm_set_memory_region()
2087 r = kvm_gmem_bind(kvm, new, mem->guest_memfd, mem->guest_memfd_offset); in __kvm_set_memory_region()
2092 r = kvm_set_memslot(kvm, old, new, change); in __kvm_set_memory_region()
2107 int kvm_set_memory_region(struct kvm *kvm, in kvm_set_memory_region() argument
2112 mutex_lock(&kvm->slots_lock); in kvm_set_memory_region()
2113 r = __kvm_set_memory_region(kvm, mem); in kvm_set_memory_region()
2114 mutex_unlock(&kvm->slots_lock); in kvm_set_memory_region()
2119 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, in kvm_vm_ioctl_set_memory_region() argument
2125 return kvm_set_memory_region(kvm, mem); in kvm_vm_ioctl_set_memory_region()
2136 int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log, in kvm_get_dirty_log() argument
2145 if (!kvm_use_dirty_bitmap(kvm)) in kvm_get_dirty_log()
2153 if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS) in kvm_get_dirty_log()
2156 slots = __kvm_memslots(kvm, as_id); in kvm_get_dirty_log()
2161 kvm_arch_sync_dirty_log(kvm, *memslot); in kvm_get_dirty_log()
2199 static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log) in kvm_get_dirty_log_protect() argument
2210 if (!kvm_use_dirty_bitmap(kvm)) in kvm_get_dirty_log_protect()
2215 if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS) in kvm_get_dirty_log_protect()
2218 slots = __kvm_memslots(kvm, as_id); in kvm_get_dirty_log_protect()
2225 kvm_arch_sync_dirty_log(kvm, memslot); in kvm_get_dirty_log_protect()
2229 if (kvm->manual_dirty_log_protect) { in kvm_get_dirty_log_protect()
2243 KVM_MMU_LOCK(kvm); in kvm_get_dirty_log_protect()
2256 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, in kvm_get_dirty_log_protect()
2259 KVM_MMU_UNLOCK(kvm); in kvm_get_dirty_log_protect()
2263 kvm_flush_remote_tlbs_memslot(kvm, memslot); in kvm_get_dirty_log_protect()
2290 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, in kvm_vm_ioctl_get_dirty_log() argument
2295 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_get_dirty_log()
2297 r = kvm_get_dirty_log_protect(kvm, log); in kvm_vm_ioctl_get_dirty_log()
2299 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_get_dirty_log()
2309 static int kvm_clear_dirty_log_protect(struct kvm *kvm, in kvm_clear_dirty_log_protect() argument
2322 if (!kvm_use_dirty_bitmap(kvm)) in kvm_clear_dirty_log_protect()
2327 if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS) in kvm_clear_dirty_log_protect()
2333 slots = __kvm_memslots(kvm, as_id); in kvm_clear_dirty_log_protect()
2347 kvm_arch_sync_dirty_log(kvm, memslot); in kvm_clear_dirty_log_protect()
2354 KVM_MMU_LOCK(kvm); in kvm_clear_dirty_log_protect()
2373 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, in kvm_clear_dirty_log_protect()
2377 KVM_MMU_UNLOCK(kvm); in kvm_clear_dirty_log_protect()
2380 kvm_flush_remote_tlbs_memslot(kvm, memslot); in kvm_clear_dirty_log_protect()
2385 static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, in kvm_vm_ioctl_clear_dirty_log() argument
2390 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_clear_dirty_log()
2392 r = kvm_clear_dirty_log_protect(kvm, log); in kvm_vm_ioctl_clear_dirty_log()
2394 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_clear_dirty_log()
2400 static u64 kvm_supported_mem_attributes(struct kvm *kvm) in kvm_supported_mem_attributes() argument
2402 if (!kvm || kvm_arch_has_private_mem(kvm)) in kvm_supported_mem_attributes()
2412 bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, in kvm_range_has_memory_attributes() argument
2415 XA_STATE(xas, &kvm->mem_attr_array, start); in kvm_range_has_memory_attributes()
2419 mask &= kvm_supported_mem_attributes(kvm); in kvm_range_has_memory_attributes()
2424 return (kvm_get_memory_attributes(kvm, start) & mask) == attrs; in kvm_range_has_memory_attributes()
2443 static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, in kvm_handle_gfn_range() argument
2457 for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { in kvm_handle_gfn_range()
2458 slots = __kvm_memslots(kvm, i); in kvm_handle_gfn_range()
2471 KVM_MMU_LOCK(kvm); in kvm_handle_gfn_range()
2473 range->on_lock(kvm); in kvm_handle_gfn_range()
2476 ret |= range->handler(kvm, &gfn_range); in kvm_handle_gfn_range()
2481 kvm_flush_remote_tlbs(kvm); in kvm_handle_gfn_range()
2484 KVM_MMU_UNLOCK(kvm); in kvm_handle_gfn_range()
2487 static bool kvm_pre_set_memory_attributes(struct kvm *kvm, in kvm_pre_set_memory_attributes() argument
2501 kvm_mmu_invalidate_range_add(kvm, range->start, range->end); in kvm_pre_set_memory_attributes()
2503 return kvm_arch_pre_set_memory_attributes(kvm, range); in kvm_pre_set_memory_attributes()
2507 static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, in kvm_vm_set_mem_attributes() argument
2532 mutex_lock(&kvm->slots_lock); in kvm_vm_set_mem_attributes()
2535 if (kvm_range_has_memory_attributes(kvm, start, end, ~0, attributes)) in kvm_vm_set_mem_attributes()
2543 r = xa_reserve(&kvm->mem_attr_array, i, GFP_KERNEL_ACCOUNT); in kvm_vm_set_mem_attributes()
2548 kvm_handle_gfn_range(kvm, &pre_set_range); in kvm_vm_set_mem_attributes()
2551 r = xa_err(xa_store(&kvm->mem_attr_array, i, entry, in kvm_vm_set_mem_attributes()
2553 KVM_BUG_ON(r, kvm); in kvm_vm_set_mem_attributes()
2556 kvm_handle_gfn_range(kvm, &post_set_range); in kvm_vm_set_mem_attributes()
2559 mutex_unlock(&kvm->slots_lock); in kvm_vm_set_mem_attributes()
2563 static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, in kvm_vm_ioctl_set_mem_attributes() argument
2571 if (attrs->attributes & ~kvm_supported_mem_attributes(kvm)) in kvm_vm_ioctl_set_mem_attributes()
2588 return kvm_vm_set_mem_attributes(kvm, start, end, attrs->attributes); in kvm_vm_ioctl_set_mem_attributes()
2592 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) in gfn_to_memslot() argument
2594 return __gfn_to_memslot(kvm_memslots(kvm), gfn); in gfn_to_memslot()
2631 bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) in kvm_is_visible_gfn() argument
2633 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn); in kvm_is_visible_gfn()
2704 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) in gfn_to_hva() argument
2706 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL); in gfn_to_hva()
2735 unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable) in gfn_to_hva_prot() argument
2737 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); in gfn_to_hva_prot()
3016 kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, in gfn_to_pfn_prot() argument
3019 return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, false, in gfn_to_pfn_prot()
3038 kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) in gfn_to_pfn() argument
3040 return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn); in gfn_to_pfn()
3067 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) in gfn_to_page() argument
3072 pfn = gfn_to_pfn(kvm, gfn); in gfn_to_page()
3102 pfn = gfn_to_pfn(vcpu->kvm, gfn); in kvm_vcpu_map()
3273 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, in kvm_read_guest_page() argument
3276 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); in kvm_read_guest_page()
3291 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) in kvm_read_guest() argument
3299 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); in kvm_read_guest()
3363 static int __kvm_write_guest_page(struct kvm *kvm, in __kvm_write_guest_page() argument
3379 mark_page_dirty_in_slot(kvm, memslot, gfn); in __kvm_write_guest_page()
3383 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, in kvm_write_guest_page() argument
3386 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); in kvm_write_guest_page()
3388 return __kvm_write_guest_page(kvm, slot, gfn, data, offset, len); in kvm_write_guest_page()
3397 return __kvm_write_guest_page(vcpu->kvm, slot, gfn, data, offset, len); in kvm_vcpu_write_guest_page()
3401 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, in kvm_write_guest() argument
3410 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); in kvm_write_guest()
3484 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, in kvm_gfn_to_hva_cache_init() argument
3487 struct kvm_memslots *slots = kvm_memslots(kvm); in kvm_gfn_to_hva_cache_init()
3492 int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, in kvm_write_guest_offset_cached() argument
3496 struct kvm_memslots *slots = kvm_memslots(kvm); in kvm_write_guest_offset_cached()
3512 return kvm_write_guest(kvm, gpa, data, len); in kvm_write_guest_offset_cached()
3517 mark_page_dirty_in_slot(kvm, ghc->memslot, gpa >> PAGE_SHIFT); in kvm_write_guest_offset_cached()
3523 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, in kvm_write_guest_cached() argument
3526 return kvm_write_guest_offset_cached(kvm, ghc, data, 0, len); in kvm_write_guest_cached()
3530 int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, in kvm_read_guest_offset_cached() argument
3534 struct kvm_memslots *slots = kvm_memslots(kvm); in kvm_read_guest_offset_cached()
3550 return kvm_read_guest(kvm, gpa, data, len); in kvm_read_guest_offset_cached()
3560 int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, in kvm_read_guest_cached() argument
3563 return kvm_read_guest_offset_cached(kvm, ghc, data, 0, len); in kvm_read_guest_cached()
3567 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) in kvm_clear_guest() argument
3576 ret = kvm_write_guest_page(kvm, gfn, zero_page, offset, seg); in kvm_clear_guest()
3587 void mark_page_dirty_in_slot(struct kvm *kvm, in mark_page_dirty_in_slot() argument
3594 if (WARN_ON_ONCE(vcpu && vcpu->kvm != kvm)) in mark_page_dirty_in_slot()
3597 WARN_ON_ONCE(!vcpu && !kvm_arch_allow_write_without_running_vcpu(kvm)); in mark_page_dirty_in_slot()
3604 if (kvm->dirty_ring_size && vcpu) in mark_page_dirty_in_slot()
3612 void mark_page_dirty(struct kvm *kvm, gfn_t gfn) in mark_page_dirty() argument
3616 memslot = gfn_to_memslot(kvm, gfn); in mark_page_dirty()
3617 mark_page_dirty_in_slot(kvm, memslot, gfn); in mark_page_dirty()
3626 mark_page_dirty_in_slot(vcpu->kvm, memslot, gfn); in kvm_vcpu_mark_page_dirty()
3694 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_vcpu_check_block()
3707 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_vcpu_check_block()
3772 struct kvm *kvm = vcpu->kvm; in kvm_vcpu_max_halt_poll_ns() local
3774 if (kvm->override_halt_poll_ns) { in kvm_vcpu_max_halt_poll_ns()
3782 return READ_ONCE(kvm->max_halt_poll_ns); in kvm_vcpu_max_halt_poll_ns()
4022 struct kvm *kvm = me->kvm; in kvm_vcpu_on_spin() local
4030 last_boosted_vcpu = READ_ONCE(kvm->last_boosted_vcpu); in kvm_vcpu_on_spin()
4040 kvm_for_each_vcpu(i, vcpu, kvm) { in kvm_vcpu_on_spin()
4068 WRITE_ONCE(kvm->last_boosted_vcpu, i); in kvm_vcpu_on_spin()
4084 static bool kvm_page_in_dirty_ring(struct kvm *kvm, unsigned long pgoff) in kvm_page_in_dirty_ring() argument
4089 kvm->dirty_ring_size / PAGE_SIZE); in kvm_page_in_dirty_ring()
4108 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring); in kvm_vcpu_fault()
4110 else if (kvm_page_in_dirty_ring(vcpu->kvm, vmf->pgoff)) in kvm_vcpu_fault()
4130 if ((kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff) || in kvm_vcpu_mmap()
4131 kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff + pages - 1)) && in kvm_vcpu_mmap()
4143 kvm_put_kvm(vcpu->kvm); in kvm_vcpu_release()
4189 vcpu->kvm->debugfs_dentry); in kvm_create_vcpu_debugfs()
4200 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id) in kvm_vm_ioctl_create_vcpu() argument
4218 mutex_lock(&kvm->lock); in kvm_vm_ioctl_create_vcpu()
4219 if (kvm->created_vcpus >= kvm->max_vcpus) { in kvm_vm_ioctl_create_vcpu()
4220 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_create_vcpu()
4224 r = kvm_arch_vcpu_precreate(kvm, id); in kvm_vm_ioctl_create_vcpu()
4226 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_create_vcpu()
4230 kvm->created_vcpus++; in kvm_vm_ioctl_create_vcpu()
4231 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_create_vcpu()
4247 kvm_vcpu_init(vcpu, kvm, id); in kvm_vm_ioctl_create_vcpu()
4253 if (kvm->dirty_ring_size) { in kvm_vm_ioctl_create_vcpu()
4255 id, kvm->dirty_ring_size); in kvm_vm_ioctl_create_vcpu()
4260 mutex_lock(&kvm->lock); in kvm_vm_ioctl_create_vcpu()
4268 if (kvm_get_vcpu_by_id(kvm, id)) { in kvm_vm_ioctl_create_vcpu()
4273 vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus); in kvm_vm_ioctl_create_vcpu()
4274 r = xa_reserve(&kvm->vcpu_array, vcpu->vcpu_idx, GFP_KERNEL_ACCOUNT); in kvm_vm_ioctl_create_vcpu()
4279 kvm_get_kvm(kvm); in kvm_vm_ioctl_create_vcpu()
4284 if (KVM_BUG_ON(xa_store(&kvm->vcpu_array, vcpu->vcpu_idx, vcpu, 0), kvm)) { in kvm_vm_ioctl_create_vcpu()
4294 atomic_inc(&kvm->online_vcpus); in kvm_vm_ioctl_create_vcpu()
4296 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_create_vcpu()
4302 kvm_put_kvm_no_destroy(kvm); in kvm_vm_ioctl_create_vcpu()
4303 xa_release(&kvm->vcpu_array, vcpu->vcpu_idx); in kvm_vm_ioctl_create_vcpu()
4305 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_create_vcpu()
4314 mutex_lock(&kvm->lock); in kvm_vm_ioctl_create_vcpu()
4315 kvm->created_vcpus--; in kvm_vm_ioctl_create_vcpu()
4316 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_create_vcpu()
4345 kvm_put_kvm(vcpu->kvm); in kvm_vcpu_stats_release()
4374 kvm_get_kvm(vcpu->kvm); in kvm_vcpu_ioctl_get_stats_fd()
4399 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_vcpu_pre_fault_memory()
4420 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_vcpu_pre_fault_memory()
4437 if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead) in kvm_vcpu_ioctl()
4663 if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead) in kvm_vcpu_compat_ioctl()
4729 if (dev->kvm->mm != current->mm || dev->kvm->vm_dead) in kvm_device_ioctl()
4750 struct kvm *kvm = dev->kvm; in kvm_device_release() local
4753 mutex_lock(&kvm->lock); in kvm_device_release()
4757 mutex_unlock(&kvm->lock); in kvm_device_release()
4760 kvm_put_kvm(kvm); in kvm_device_release()
4804 static int kvm_ioctl_create_device(struct kvm *kvm, in kvm_ioctl_create_device() argument
4829 dev->kvm = kvm; in kvm_ioctl_create_device()
4831 mutex_lock(&kvm->lock); in kvm_ioctl_create_device()
4834 mutex_unlock(&kvm->lock); in kvm_ioctl_create_device()
4838 list_add_rcu(&dev->vm_node, &kvm->devices); in kvm_ioctl_create_device()
4839 mutex_unlock(&kvm->lock); in kvm_ioctl_create_device()
4844 kvm_get_kvm(kvm); in kvm_ioctl_create_device()
4847 kvm_put_kvm_no_destroy(kvm); in kvm_ioctl_create_device()
4848 mutex_lock(&kvm->lock); in kvm_ioctl_create_device()
4853 mutex_unlock(&kvm->lock); in kvm_ioctl_create_device()
4863 static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) in kvm_vm_ioctl_check_extension_generic() argument
4898 if (kvm) in kvm_vm_ioctl_check_extension_generic()
4899 return kvm_arch_nr_memslot_as_ids(kvm); in kvm_vm_ioctl_check_extension_generic()
4925 return kvm_supported_mem_attributes(kvm); in kvm_vm_ioctl_check_extension_generic()
4929 return !kvm || kvm_arch_has_private_mem(kvm); in kvm_vm_ioctl_check_extension_generic()
4934 return kvm_vm_ioctl_check_extension(kvm, arg); in kvm_vm_ioctl_check_extension_generic()
4937 static int kvm_vm_ioctl_enable_dirty_log_ring(struct kvm *kvm, u32 size) in kvm_vm_ioctl_enable_dirty_log_ring() argument
4958 if (kvm->dirty_ring_size) in kvm_vm_ioctl_enable_dirty_log_ring()
4961 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_dirty_log_ring()
4963 if (kvm->created_vcpus) { in kvm_vm_ioctl_enable_dirty_log_ring()
4967 kvm->dirty_ring_size = size; in kvm_vm_ioctl_enable_dirty_log_ring()
4971 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_dirty_log_ring()
4975 static int kvm_vm_ioctl_reset_dirty_pages(struct kvm *kvm) in kvm_vm_ioctl_reset_dirty_pages() argument
4981 if (!kvm->dirty_ring_size) in kvm_vm_ioctl_reset_dirty_pages()
4984 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_reset_dirty_pages()
4986 kvm_for_each_vcpu(i, vcpu, kvm) in kvm_vm_ioctl_reset_dirty_pages()
4987 cleared += kvm_dirty_ring_reset(vcpu->kvm, &vcpu->dirty_ring); in kvm_vm_ioctl_reset_dirty_pages()
4989 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_reset_dirty_pages()
4992 kvm_flush_remote_tlbs(kvm); in kvm_vm_ioctl_reset_dirty_pages()
4997 int __attribute__((weak)) kvm_vm_ioctl_enable_cap(struct kvm *kvm, in kvm_vm_ioctl_enable_cap() argument
5003 bool kvm_are_all_memslots_empty(struct kvm *kvm) in kvm_are_all_memslots_empty() argument
5007 lockdep_assert_held(&kvm->slots_lock); in kvm_are_all_memslots_empty()
5009 for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { in kvm_are_all_memslots_empty()
5010 if (!kvm_memslots_empty(__kvm_memslots(kvm, i))) in kvm_are_all_memslots_empty()
5018 static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, in kvm_vm_ioctl_enable_cap_generic() argument
5031 kvm->manual_dirty_log_protect = cap->args[0]; in kvm_vm_ioctl_enable_cap_generic()
5039 kvm->max_halt_poll_ns = cap->args[0]; in kvm_vm_ioctl_enable_cap_generic()
5048 kvm->override_halt_poll_ns = true; in kvm_vm_ioctl_enable_cap_generic()
5054 if (!kvm_vm_ioctl_check_extension_generic(kvm, cap->cap)) in kvm_vm_ioctl_enable_cap_generic()
5057 return kvm_vm_ioctl_enable_dirty_log_ring(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap_generic()
5062 !kvm->dirty_ring_size || cap->flags) in kvm_vm_ioctl_enable_cap_generic()
5065 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_enable_cap_generic()
5072 if (kvm_are_all_memslots_empty(kvm)) { in kvm_vm_ioctl_enable_cap_generic()
5073 kvm->dirty_ring_with_bitmap = true; in kvm_vm_ioctl_enable_cap_generic()
5077 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_enable_cap_generic()
5082 return kvm_vm_ioctl_enable_cap(kvm, cap); in kvm_vm_ioctl_enable_cap_generic()
5089 struct kvm *kvm = file->private_data; in kvm_vm_stats_read() local
5091 return kvm_stats_read(kvm->stats_id, &kvm_vm_stats_header, in kvm_vm_stats_read()
5092 &kvm_vm_stats_desc[0], &kvm->stat, in kvm_vm_stats_read()
5093 sizeof(kvm->stat), user_buffer, size, offset); in kvm_vm_stats_read()
5098 struct kvm *kvm = file->private_data; in kvm_vm_stats_release() local
5100 kvm_put_kvm(kvm); in kvm_vm_stats_release()
5111 static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm) in kvm_vm_ioctl_get_stats_fd() argument
5121 &kvm_vm_stats_fops, kvm, O_RDONLY); in kvm_vm_ioctl_get_stats_fd()
5127 kvm_get_kvm(kvm); in kvm_vm_ioctl_get_stats_fd()
5146 struct kvm *kvm = filp->private_data; in kvm_vm_ioctl() local
5150 if (kvm->mm != current->mm || kvm->vm_dead) in kvm_vm_ioctl()
5154 r = kvm_vm_ioctl_create_vcpu(kvm, arg); in kvm_vm_ioctl()
5162 r = kvm_vm_ioctl_enable_cap_generic(kvm, &cap); in kvm_vm_ioctl()
5197 r = kvm_vm_ioctl_set_memory_region(kvm, &mem); in kvm_vm_ioctl()
5206 r = kvm_vm_ioctl_get_dirty_log(kvm, &log); in kvm_vm_ioctl()
5216 r = kvm_vm_ioctl_clear_dirty_log(kvm, &log); in kvm_vm_ioctl()
5227 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); in kvm_vm_ioctl()
5236 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); in kvm_vm_ioctl()
5246 r = kvm_irqfd(kvm, &data); in kvm_vm_ioctl()
5255 r = kvm_ioeventfd(kvm, &data); in kvm_vm_ioctl()
5265 r = kvm_send_userspace_msi(kvm, &msi); in kvm_vm_ioctl()
5278 r = kvm_vm_ioctl_irq_line(kvm, &irq_event, in kvm_vm_ioctl()
5303 if (!kvm_arch_can_set_irq_routing(kvm)) in kvm_vm_ioctl()
5318 r = kvm_set_irq_routing(kvm, entries, routing.nr, in kvm_vm_ioctl()
5332 r = kvm_vm_ioctl_set_mem_attributes(kvm, &attrs); in kvm_vm_ioctl()
5343 r = kvm_ioctl_create_device(kvm, &cd); in kvm_vm_ioctl()
5355 r = kvm_vm_ioctl_check_extension_generic(kvm, arg); in kvm_vm_ioctl()
5358 r = kvm_vm_ioctl_reset_dirty_pages(kvm); in kvm_vm_ioctl()
5361 r = kvm_vm_ioctl_get_stats_fd(kvm); in kvm_vm_ioctl()
5371 r = kvm_gmem_create(kvm, &guest_memfd); in kvm_vm_ioctl()
5411 struct kvm *kvm = filp->private_data; in kvm_vm_compat_ioctl() local
5414 if (kvm->mm != current->mm || kvm->vm_dead) in kvm_vm_compat_ioctl()
5436 r = kvm_vm_ioctl_clear_dirty_log(kvm, &log); in kvm_vm_compat_ioctl()
5452 r = kvm_vm_ioctl_get_dirty_log(kvm, &log); in kvm_vm_compat_ioctl()
5479 struct kvm *kvm; in kvm_dev_ioctl_create_vm() local
5488 kvm = kvm_create_vm(type, fdname); in kvm_dev_ioctl_create_vm()
5489 if (IS_ERR(kvm)) { in kvm_dev_ioctl_create_vm()
5490 r = PTR_ERR(kvm); in kvm_dev_ioctl_create_vm()
5494 file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR); in kvm_dev_ioctl_create_vm()
5506 kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm); in kvm_dev_ioctl_create_vm()
5512 kvm_put_kvm(kvm); in kvm_dev_ioctl_create_vm()
5874 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); in kvm_io_bus_write()
5894 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); in kvm_io_bus_write_cookie()
5945 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); in kvm_io_bus_read()
5952 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, in kvm_io_bus_register_dev() argument
5959 lockdep_assert_held(&kvm->slots_lock); in kvm_io_bus_register_dev()
5961 bus = kvm_get_bus(kvm, bus_idx); in kvm_io_bus_register_dev()
5989 rcu_assign_pointer(kvm->buses[bus_idx], new_bus); in kvm_io_bus_register_dev()
5990 synchronize_srcu_expedited(&kvm->srcu); in kvm_io_bus_register_dev()
5996 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, in kvm_io_bus_unregister_dev() argument
6002 lockdep_assert_held(&kvm->slots_lock); in kvm_io_bus_unregister_dev()
6004 bus = kvm_get_bus(kvm, bus_idx); in kvm_io_bus_unregister_dev()
6026 rcu_assign_pointer(kvm->buses[bus_idx], new_bus); in kvm_io_bus_unregister_dev()
6027 synchronize_srcu_expedited(&kvm->srcu); in kvm_io_bus_unregister_dev()
6044 struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx, in kvm_io_bus_get_dev() argument
6051 srcu_idx = srcu_read_lock(&kvm->srcu); in kvm_io_bus_get_dev()
6053 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu); in kvm_io_bus_get_dev()
6064 srcu_read_unlock(&kvm->srcu, srcu_idx); in kvm_io_bus_get_dev()
6082 if (!kvm_get_kvm_safe(stat_data->kvm)) in kvm_debugfs_open()
6089 kvm_put_kvm(stat_data->kvm); in kvm_debugfs_open()
6099 kvm_put_kvm(stat_data->kvm); in kvm_debugfs_release()
6104 static int kvm_get_stat_per_vm(struct kvm *kvm, size_t offset, u64 *val) in kvm_get_stat_per_vm() argument
6106 *val = *(u64 *)((void *)(&kvm->stat) + offset); in kvm_get_stat_per_vm()
6111 static int kvm_clear_stat_per_vm(struct kvm *kvm, size_t offset) in kvm_clear_stat_per_vm() argument
6113 *(u64 *)((void *)(&kvm->stat) + offset) = 0; in kvm_clear_stat_per_vm()
6118 static int kvm_get_stat_per_vcpu(struct kvm *kvm, size_t offset, u64 *val) in kvm_get_stat_per_vcpu() argument
6125 kvm_for_each_vcpu(i, vcpu, kvm) in kvm_get_stat_per_vcpu()
6131 static int kvm_clear_stat_per_vcpu(struct kvm *kvm, size_t offset) in kvm_clear_stat_per_vcpu() argument
6136 kvm_for_each_vcpu(i, vcpu, kvm) in kvm_clear_stat_per_vcpu()
6149 r = kvm_get_stat_per_vm(stat_data->kvm, in kvm_stat_data_get()
6153 r = kvm_get_stat_per_vcpu(stat_data->kvm, in kvm_stat_data_get()
6171 r = kvm_clear_stat_per_vm(stat_data->kvm, in kvm_stat_data_clear()
6175 r = kvm_clear_stat_per_vcpu(stat_data->kvm, in kvm_stat_data_clear()
6201 struct kvm *kvm; in vm_stat_get() local
6206 list_for_each_entry(kvm, &vm_list, vm_list) { in vm_stat_get()
6207 kvm_get_stat_per_vm(kvm, offset, &tmp_val); in vm_stat_get()
6217 struct kvm *kvm; in vm_stat_clear() local
6223 list_for_each_entry(kvm, &vm_list, vm_list) { in vm_stat_clear()
6224 kvm_clear_stat_per_vm(kvm, offset); in vm_stat_clear()
6237 struct kvm *kvm; in vcpu_stat_get() local
6242 list_for_each_entry(kvm, &vm_list, vm_list) { in vcpu_stat_get()
6243 kvm_get_stat_per_vcpu(kvm, offset, &tmp_val); in vcpu_stat_get()
6253 struct kvm *kvm; in vcpu_stat_clear() local
6259 list_for_each_entry(kvm, &vm_list, vm_list) { in vcpu_stat_clear()
6260 kvm_clear_stat_per_vcpu(kvm, offset); in vcpu_stat_clear()
6271 static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm) in kvm_uevent_notify_change() argument
6276 if (!kvm_dev.this_device || !kvm) in kvm_uevent_notify_change()
6299 kvm->userspace_pid = task_pid_nr(current); in kvm_uevent_notify_change()
6303 add_uevent_var(env, "PID=%d", kvm->userspace_pid); in kvm_uevent_notify_change()
6305 if (!IS_ERR(kvm->debugfs_dentry)) { in kvm_uevent_notify_change()
6309 tmp = dentry_path_raw(kvm->debugfs_dentry, p, PATH_MAX); in kvm_uevent_notify_change()
6566 struct kvm *kvm; member
6582 struct kvm *kvm = init_context->kvm; in kvm_vm_worker_thread() local
6614 err = thread_fn(kvm, data); in kvm_vm_worker_thread()
6638 int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn, in kvm_vm_create_worker_thread() argument
6646 init_context.kvm = kvm; in kvm_vm_create_worker_thread()