1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
5 * This module enables machines with Intel VT-x extensions to run virtual
6 * machines without emulation or binary translation.
8 * Copyright (C) 2006 Qumranet, Inc.
9 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
16 #include <kvm/iodev.h>
18 #include <linux/kvm_host.h>
19 #include <linux/kvm.h>
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/percpu.h>
24 #include <linux/miscdevice.h>
25 #include <linux/vmalloc.h>
26 #include <linux/reboot.h>
27 #include <linux/debugfs.h>
28 #include <linux/highmem.h>
29 #include <linux/file.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/cpu.h>
32 #include <linux/sched/signal.h>
33 #include <linux/sched/mm.h>
34 #include <linux/sched/stat.h>
35 #include <linux/cpumask.h>
36 #include <linux/smp.h>
37 #include <linux/anon_inodes.h>
38 #include <linux/profile.h>
39 #include <linux/kvm_para.h>
40 #include <linux/pagemap.h>
41 #include <linux/mman.h>
42 #include <linux/swap.h>
43 #include <linux/bitops.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>
49 #include <linux/sort.h>
50 #include <linux/bsearch.h>
52 #include <linux/lockdep.h>
53 #include <linux/kthread.h>
54 #include <linux/suspend.h>
56 #include <asm/processor.h>
57 #include <asm/ioctl.h>
58 #include <linux/uaccess.h>
60 #include "coalesced_mmio.h"
65 #define CREATE_TRACE_POINTS
66 #include <trace/events/kvm.h>
68 #include <linux/kvm_dirty_ring.h>
70 /* Worst case buffer size needed for holding an integer. */
71 #define ITOA_MAX_LEN 12
73 MODULE_AUTHOR("Qumranet");
74 MODULE_LICENSE("GPL");
76 /* Architectures should define their poll value according to the halt latency */
77 unsigned int halt_poll_ns = KVM_HALT_POLL_NS_DEFAULT;
78 module_param(halt_poll_ns, uint, 0644);
79 EXPORT_SYMBOL_GPL(halt_poll_ns);
81 /* Default doubles per-vcpu halt_poll_ns. */
82 unsigned int halt_poll_ns_grow = 2;
83 module_param(halt_poll_ns_grow, uint, 0644);
84 EXPORT_SYMBOL_GPL(halt_poll_ns_grow);
86 /* The start value to grow halt_poll_ns from */
87 unsigned int halt_poll_ns_grow_start = 10000; /* 10us */
88 module_param(halt_poll_ns_grow_start, uint, 0644);
89 EXPORT_SYMBOL_GPL(halt_poll_ns_grow_start);
91 /* Default resets per-vcpu halt_poll_ns . */
92 unsigned int halt_poll_ns_shrink;
93 module_param(halt_poll_ns_shrink, uint, 0644);
94 EXPORT_SYMBOL_GPL(halt_poll_ns_shrink);
99 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
102 DEFINE_MUTEX(kvm_lock);
103 static DEFINE_RAW_SPINLOCK(kvm_count_lock);
106 static cpumask_var_t cpus_hardware_enabled;
107 static int kvm_usage_count;
108 static atomic_t hardware_enable_failed;
110 static struct kmem_cache *kvm_vcpu_cache;
112 static __read_mostly struct preempt_ops kvm_preempt_ops;
113 static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_running_vcpu);
115 struct dentry *kvm_debugfs_dir;
116 EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
118 static const struct file_operations stat_fops_per_vm;
120 static struct file_operations kvm_chardev_ops;
122 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
124 #ifdef CONFIG_KVM_COMPAT
125 static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
127 #define KVM_COMPAT(c) .compat_ioctl = (c)
130 * For architectures that don't implement a compat infrastructure,
131 * adopt a double line of defense:
132 * - Prevent a compat task from opening /dev/kvm
133 * - If the open has been done by a 64bit task, and the KVM fd
134 * passed to a compat task, let the ioctls fail.
136 static long kvm_no_compat_ioctl(struct file *file, unsigned int ioctl,
137 unsigned long arg) { return -EINVAL; }
139 static int kvm_no_compat_open(struct inode *inode, struct file *file)
141 return is_compat_task() ? -ENODEV : 0;
143 #define KVM_COMPAT(c) .compat_ioctl = kvm_no_compat_ioctl, \
144 .open = kvm_no_compat_open
146 static int hardware_enable_all(void);
147 static void hardware_disable_all(void);
149 static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
151 __visible bool kvm_rebooting;
152 EXPORT_SYMBOL_GPL(kvm_rebooting);
154 #define KVM_EVENT_CREATE_VM 0
155 #define KVM_EVENT_DESTROY_VM 1
156 static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm);
157 static unsigned long long kvm_createvm_count;
158 static unsigned long long kvm_active_vms;
160 static DEFINE_PER_CPU(cpumask_var_t, cpu_kick_mask);
162 __weak void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
163 unsigned long start, unsigned long end)
167 __weak void kvm_arch_guest_memory_reclaimed(struct kvm *kvm)
171 bool kvm_is_zone_device_page(struct page *page)
174 * The metadata used by is_zone_device_page() to determine whether or
175 * not a page is ZONE_DEVICE is guaranteed to be valid if and only if
176 * the device has been pinned, e.g. by get_user_pages(). WARN if the
177 * page_count() is zero to help detect bad usage of this helper.
179 if (WARN_ON_ONCE(!page_count(page)))
182 return is_zone_device_page(page);
186 * Returns a 'struct page' if the pfn is "valid" and backed by a refcounted
187 * page, NULL otherwise. Note, the list of refcounted PG_reserved page types
188 * is likely incomplete, it has been compiled purely through people wanting to
189 * back guest with a certain type of memory and encountering issues.
191 struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn)
198 page = pfn_to_page(pfn);
199 if (!PageReserved(page))
202 /* The ZERO_PAGE(s) is marked PG_reserved, but is refcounted. */
203 if (is_zero_pfn(pfn))
207 * ZONE_DEVICE pages currently set PG_reserved, but from a refcounting
208 * perspective they are "normal" pages, albeit with slightly different
211 if (kvm_is_zone_device_page(page))
218 * Switches to specified vcpu, until a matching vcpu_put()
220 void vcpu_load(struct kvm_vcpu *vcpu)
224 __this_cpu_write(kvm_running_vcpu, vcpu);
225 preempt_notifier_register(&vcpu->preempt_notifier);
226 kvm_arch_vcpu_load(vcpu, cpu);
229 EXPORT_SYMBOL_GPL(vcpu_load);
231 void vcpu_put(struct kvm_vcpu *vcpu)
234 kvm_arch_vcpu_put(vcpu);
235 preempt_notifier_unregister(&vcpu->preempt_notifier);
236 __this_cpu_write(kvm_running_vcpu, NULL);
239 EXPORT_SYMBOL_GPL(vcpu_put);
241 /* TODO: merge with kvm_arch_vcpu_should_kick */
242 static bool kvm_request_needs_ipi(struct kvm_vcpu *vcpu, unsigned req)
244 int mode = kvm_vcpu_exiting_guest_mode(vcpu);
247 * We need to wait for the VCPU to reenable interrupts and get out of
248 * READING_SHADOW_PAGE_TABLES mode.
250 if (req & KVM_REQUEST_WAIT)
251 return mode != OUTSIDE_GUEST_MODE;
254 * Need to kick a running VCPU, but otherwise there is nothing to do.
256 return mode == IN_GUEST_MODE;
259 static void ack_kick(void *_completed)
263 static inline bool kvm_kick_many_cpus(struct cpumask *cpus, bool wait)
265 if (cpumask_empty(cpus))
268 smp_call_function_many(cpus, ack_kick, NULL, wait);
272 static void kvm_make_vcpu_request(struct kvm_vcpu *vcpu, unsigned int req,
273 struct cpumask *tmp, int current_cpu)
277 if (likely(!(req & KVM_REQUEST_NO_ACTION)))
278 __kvm_make_request(req, vcpu);
280 if (!(req & KVM_REQUEST_NO_WAKEUP) && kvm_vcpu_wake_up(vcpu))
284 * Note, the vCPU could get migrated to a different pCPU at any point
285 * after kvm_request_needs_ipi(), which could result in sending an IPI
286 * to the previous pCPU. But, that's OK because the purpose of the IPI
287 * is to ensure the vCPU returns to OUTSIDE_GUEST_MODE, which is
288 * satisfied if the vCPU migrates. Entering READING_SHADOW_PAGE_TABLES
289 * after this point is also OK, as the requirement is only that KVM wait
290 * for vCPUs that were reading SPTEs _before_ any changes were
291 * finalized. See kvm_vcpu_kick() for more details on handling requests.
293 if (kvm_request_needs_ipi(vcpu, req)) {
294 cpu = READ_ONCE(vcpu->cpu);
295 if (cpu != -1 && cpu != current_cpu)
296 __cpumask_set_cpu(cpu, tmp);
300 bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
301 unsigned long *vcpu_bitmap)
303 struct kvm_vcpu *vcpu;
304 struct cpumask *cpus;
310 cpus = this_cpu_cpumask_var_ptr(cpu_kick_mask);
313 for_each_set_bit(i, vcpu_bitmap, KVM_MAX_VCPUS) {
314 vcpu = kvm_get_vcpu(kvm, i);
317 kvm_make_vcpu_request(vcpu, req, cpus, me);
320 called = kvm_kick_many_cpus(cpus, !!(req & KVM_REQUEST_WAIT));
326 bool kvm_make_all_cpus_request_except(struct kvm *kvm, unsigned int req,
327 struct kvm_vcpu *except)
329 struct kvm_vcpu *vcpu;
330 struct cpumask *cpus;
337 cpus = this_cpu_cpumask_var_ptr(cpu_kick_mask);
340 kvm_for_each_vcpu(i, vcpu, kvm) {
343 kvm_make_vcpu_request(vcpu, req, cpus, me);
346 called = kvm_kick_many_cpus(cpus, !!(req & KVM_REQUEST_WAIT));
352 bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req)
354 return kvm_make_all_cpus_request_except(kvm, req, NULL);
356 EXPORT_SYMBOL_GPL(kvm_make_all_cpus_request);
358 #ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL
359 void kvm_flush_remote_tlbs(struct kvm *kvm)
361 ++kvm->stat.generic.remote_tlb_flush_requests;
364 * We want to publish modifications to the page tables before reading
365 * mode. Pairs with a memory barrier in arch-specific code.
366 * - x86: smp_mb__after_srcu_read_unlock in vcpu_enter_guest
367 * and smp_mb in walk_shadow_page_lockless_begin/end.
368 * - powerpc: smp_mb in kvmppc_prepare_to_enter.
370 * There is already an smp_mb__after_atomic() before
371 * kvm_make_all_cpus_request() reads vcpu->mode. We reuse that
374 if (!kvm_arch_flush_remote_tlb(kvm)
375 || kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
376 ++kvm->stat.generic.remote_tlb_flush;
378 EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
381 static void kvm_flush_shadow_all(struct kvm *kvm)
383 kvm_arch_flush_shadow_all(kvm);
384 kvm_arch_guest_memory_reclaimed(kvm);
387 #ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE
388 static inline void *mmu_memory_cache_alloc_obj(struct kvm_mmu_memory_cache *mc,
391 gfp_flags |= mc->gfp_zero;
394 return kmem_cache_alloc(mc->kmem_cache, gfp_flags);
396 return (void *)__get_free_page(gfp_flags);
399 int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity, int min)
401 gfp_t gfp = mc->gfp_custom ? mc->gfp_custom : GFP_KERNEL_ACCOUNT;
404 if (mc->nobjs >= min)
407 if (unlikely(!mc->objects)) {
408 if (WARN_ON_ONCE(!capacity))
411 mc->objects = kvmalloc_array(sizeof(void *), capacity, gfp);
415 mc->capacity = capacity;
418 /* It is illegal to request a different capacity across topups. */
419 if (WARN_ON_ONCE(mc->capacity != capacity))
422 while (mc->nobjs < mc->capacity) {
423 obj = mmu_memory_cache_alloc_obj(mc, gfp);
425 return mc->nobjs >= min ? 0 : -ENOMEM;
426 mc->objects[mc->nobjs++] = obj;
431 int kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int min)
433 return __kvm_mmu_topup_memory_cache(mc, KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, min);
436 int kvm_mmu_memory_cache_nr_free_objects(struct kvm_mmu_memory_cache *mc)
441 void kvm_mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
445 kmem_cache_free(mc->kmem_cache, mc->objects[--mc->nobjs]);
447 free_page((unsigned long)mc->objects[--mc->nobjs]);
456 void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
460 if (WARN_ON(!mc->nobjs))
461 p = mmu_memory_cache_alloc_obj(mc, GFP_ATOMIC | __GFP_ACCOUNT);
463 p = mc->objects[--mc->nobjs];
469 static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
471 mutex_init(&vcpu->mutex);
476 #ifndef __KVM_HAVE_ARCH_WQP
477 rcuwait_init(&vcpu->wait);
479 kvm_async_pf_vcpu_init(vcpu);
481 kvm_vcpu_set_in_spin_loop(vcpu, false);
482 kvm_vcpu_set_dy_eligible(vcpu, false);
483 vcpu->preempted = false;
485 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
486 vcpu->last_used_slot = NULL;
489 static void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
491 kvm_arch_vcpu_destroy(vcpu);
492 kvm_dirty_ring_free(&vcpu->dirty_ring);
495 * No need for rcu_read_lock as VCPU_RUN is the only place that changes
496 * the vcpu->pid pointer, and at destruction time all file descriptors
499 put_pid(rcu_dereference_protected(vcpu->pid, 1));
501 free_page((unsigned long)vcpu->run);
502 kmem_cache_free(kvm_vcpu_cache, vcpu);
505 void kvm_destroy_vcpus(struct kvm *kvm)
508 struct kvm_vcpu *vcpu;
510 kvm_for_each_vcpu(i, vcpu, kvm) {
511 kvm_vcpu_destroy(vcpu);
512 xa_erase(&kvm->vcpu_array, i);
515 atomic_set(&kvm->online_vcpus, 0);
517 EXPORT_SYMBOL_GPL(kvm_destroy_vcpus);
519 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
520 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
522 return container_of(mn, struct kvm, mmu_notifier);
525 static void kvm_mmu_notifier_invalidate_range(struct mmu_notifier *mn,
526 struct mm_struct *mm,
527 unsigned long start, unsigned long end)
529 struct kvm *kvm = mmu_notifier_to_kvm(mn);
532 idx = srcu_read_lock(&kvm->srcu);
533 kvm_arch_mmu_notifier_invalidate_range(kvm, start, end);
534 srcu_read_unlock(&kvm->srcu, idx);
537 typedef bool (*hva_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range);
539 typedef void (*on_lock_fn_t)(struct kvm *kvm, unsigned long start,
542 typedef void (*on_unlock_fn_t)(struct kvm *kvm);
544 struct kvm_hva_range {
548 hva_handler_t handler;
549 on_lock_fn_t on_lock;
550 on_unlock_fn_t on_unlock;
556 * Use a dedicated stub instead of NULL to indicate that there is no callback
557 * function/handler. The compiler technically can't guarantee that a real
558 * function will have a non-zero address, and so it will generate code to
559 * check for !NULL, whereas comparing against a stub will be elided at compile
560 * time (unless the compiler is getting long in the tooth, e.g. gcc 4.9).
562 static void kvm_null_fn(void)
566 #define IS_KVM_NULL_FN(fn) ((fn) == (void *)kvm_null_fn)
568 /* Iterate over each memslot intersecting [start, last] (inclusive) range */
569 #define kvm_for_each_memslot_in_hva_range(node, slots, start, last) \
570 for (node = interval_tree_iter_first(&slots->hva_tree, start, last); \
572 node = interval_tree_iter_next(node, start, last)) \
574 static __always_inline int __kvm_handle_hva_range(struct kvm *kvm,
575 const struct kvm_hva_range *range)
577 bool ret = false, locked = false;
578 struct kvm_gfn_range gfn_range;
579 struct kvm_memory_slot *slot;
580 struct kvm_memslots *slots;
583 if (WARN_ON_ONCE(range->end <= range->start))
586 /* A null handler is allowed if and only if on_lock() is provided. */
587 if (WARN_ON_ONCE(IS_KVM_NULL_FN(range->on_lock) &&
588 IS_KVM_NULL_FN(range->handler)))
591 idx = srcu_read_lock(&kvm->srcu);
593 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
594 struct interval_tree_node *node;
596 slots = __kvm_memslots(kvm, i);
597 kvm_for_each_memslot_in_hva_range(node, slots,
598 range->start, range->end - 1) {
599 unsigned long hva_start, hva_end;
601 slot = container_of(node, struct kvm_memory_slot, hva_node[slots->node_idx]);
602 hva_start = max(range->start, slot->userspace_addr);
603 hva_end = min(range->end, slot->userspace_addr +
604 (slot->npages << PAGE_SHIFT));
607 * To optimize for the likely case where the address
608 * range is covered by zero or one memslots, don't
609 * bother making these conditional (to avoid writes on
610 * the second or later invocation of the handler).
612 gfn_range.pte = range->pte;
613 gfn_range.may_block = range->may_block;
616 * {gfn(page) | page intersects with [hva_start, hva_end)} =
617 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
619 gfn_range.start = hva_to_gfn_memslot(hva_start, slot);
620 gfn_range.end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, slot);
621 gfn_range.slot = slot;
626 if (!IS_KVM_NULL_FN(range->on_lock))
627 range->on_lock(kvm, range->start, range->end);
628 if (IS_KVM_NULL_FN(range->handler))
631 ret |= range->handler(kvm, &gfn_range);
635 if (range->flush_on_ret && ret)
636 kvm_flush_remote_tlbs(kvm);
640 if (!IS_KVM_NULL_FN(range->on_unlock))
641 range->on_unlock(kvm);
644 srcu_read_unlock(&kvm->srcu, idx);
646 /* The notifiers are averse to booleans. :-( */
650 static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn,
654 hva_handler_t handler)
656 struct kvm *kvm = mmu_notifier_to_kvm(mn);
657 const struct kvm_hva_range range = {
662 .on_lock = (void *)kvm_null_fn,
663 .on_unlock = (void *)kvm_null_fn,
664 .flush_on_ret = true,
668 return __kvm_handle_hva_range(kvm, &range);
671 static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn,
674 hva_handler_t handler)
676 struct kvm *kvm = mmu_notifier_to_kvm(mn);
677 const struct kvm_hva_range range = {
682 .on_lock = (void *)kvm_null_fn,
683 .on_unlock = (void *)kvm_null_fn,
684 .flush_on_ret = false,
688 return __kvm_handle_hva_range(kvm, &range);
690 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
691 struct mm_struct *mm,
692 unsigned long address,
695 struct kvm *kvm = mmu_notifier_to_kvm(mn);
697 trace_kvm_set_spte_hva(address);
700 * .change_pte() must be surrounded by .invalidate_range_{start,end}().
701 * If mmu_notifier_count is zero, then no in-progress invalidations,
702 * including this one, found a relevant memslot at start(); rechecking
703 * memslots here is unnecessary. Note, a false positive (count elevated
704 * by a different invalidation) is sub-optimal but functionally ok.
706 WARN_ON_ONCE(!READ_ONCE(kvm->mn_active_invalidate_count));
707 if (!READ_ONCE(kvm->mmu_notifier_count))
710 kvm_handle_hva_range(mn, address, address + 1, pte, kvm_set_spte_gfn);
713 void kvm_inc_notifier_count(struct kvm *kvm, unsigned long start,
717 * The count increase must become visible at unlock time as no
718 * spte can be established without taking the mmu_lock and
719 * count is also read inside the mmu_lock critical section.
721 kvm->mmu_notifier_count++;
722 if (likely(kvm->mmu_notifier_count == 1)) {
723 kvm->mmu_notifier_range_start = start;
724 kvm->mmu_notifier_range_end = end;
727 * Fully tracking multiple concurrent ranges has diminishing
728 * returns. Keep things simple and just find the minimal range
729 * which includes the current and new ranges. As there won't be
730 * enough information to subtract a range after its invalidate
731 * completes, any ranges invalidated concurrently will
732 * accumulate and persist until all outstanding invalidates
735 kvm->mmu_notifier_range_start =
736 min(kvm->mmu_notifier_range_start, start);
737 kvm->mmu_notifier_range_end =
738 max(kvm->mmu_notifier_range_end, end);
742 static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
743 const struct mmu_notifier_range *range)
745 struct kvm *kvm = mmu_notifier_to_kvm(mn);
746 const struct kvm_hva_range hva_range = {
747 .start = range->start,
750 .handler = kvm_unmap_gfn_range,
751 .on_lock = kvm_inc_notifier_count,
752 .on_unlock = kvm_arch_guest_memory_reclaimed,
753 .flush_on_ret = true,
754 .may_block = mmu_notifier_range_blockable(range),
757 trace_kvm_unmap_hva_range(range->start, range->end);
760 * Prevent memslot modification between range_start() and range_end()
761 * so that conditionally locking provides the same result in both
762 * functions. Without that guarantee, the mmu_notifier_count
763 * adjustments will be imbalanced.
765 * Pairs with the decrement in range_end().
767 spin_lock(&kvm->mn_invalidate_lock);
768 kvm->mn_active_invalidate_count++;
769 spin_unlock(&kvm->mn_invalidate_lock);
772 * Invalidate pfn caches _before_ invalidating the secondary MMUs, i.e.
773 * before acquiring mmu_lock, to avoid holding mmu_lock while acquiring
774 * each cache's lock. There are relatively few caches in existence at
775 * any given time, and the caches themselves can check for hva overlap,
776 * i.e. don't need to rely on memslot overlap checks for performance.
777 * Because this runs without holding mmu_lock, the pfn caches must use
778 * mn_active_invalidate_count (see above) instead of mmu_notifier_count.
780 gfn_to_pfn_cache_invalidate_start(kvm, range->start, range->end,
781 hva_range.may_block);
783 __kvm_handle_hva_range(kvm, &hva_range);
788 void kvm_dec_notifier_count(struct kvm *kvm, unsigned long start,
792 * This sequence increase will notify the kvm page fault that
793 * the page that is going to be mapped in the spte could have
796 kvm->mmu_notifier_seq++;
799 * The above sequence increase must be visible before the
800 * below count decrease, which is ensured by the smp_wmb above
801 * in conjunction with the smp_rmb in mmu_notifier_retry().
803 kvm->mmu_notifier_count--;
806 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
807 const struct mmu_notifier_range *range)
809 struct kvm *kvm = mmu_notifier_to_kvm(mn);
810 const struct kvm_hva_range hva_range = {
811 .start = range->start,
814 .handler = (void *)kvm_null_fn,
815 .on_lock = kvm_dec_notifier_count,
816 .on_unlock = (void *)kvm_null_fn,
817 .flush_on_ret = false,
818 .may_block = mmu_notifier_range_blockable(range),
822 __kvm_handle_hva_range(kvm, &hva_range);
824 /* Pairs with the increment in range_start(). */
825 spin_lock(&kvm->mn_invalidate_lock);
826 wake = (--kvm->mn_active_invalidate_count == 0);
827 spin_unlock(&kvm->mn_invalidate_lock);
830 * There can only be one waiter, since the wait happens under
834 rcuwait_wake_up(&kvm->mn_memslots_update_rcuwait);
836 BUG_ON(kvm->mmu_notifier_count < 0);
839 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
840 struct mm_struct *mm,
844 trace_kvm_age_hva(start, end);
846 return kvm_handle_hva_range(mn, start, end, __pte(0), kvm_age_gfn);
849 static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,
850 struct mm_struct *mm,
854 trace_kvm_age_hva(start, end);
857 * Even though we do not flush TLB, this will still adversely
858 * affect performance on pre-Haswell Intel EPT, where there is
859 * no EPT Access Bit to clear so that we have to tear down EPT
860 * tables instead. If we find this unacceptable, we can always
861 * add a parameter to kvm_age_hva so that it effectively doesn't
862 * do anything on clear_young.
864 * Also note that currently we never issue secondary TLB flushes
865 * from clear_young, leaving this job up to the regular system
866 * cadence. If we find this inaccurate, we might come up with a
867 * more sophisticated heuristic later.
869 return kvm_handle_hva_range_no_flush(mn, start, end, kvm_age_gfn);
872 static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
873 struct mm_struct *mm,
874 unsigned long address)
876 trace_kvm_test_age_hva(address);
878 return kvm_handle_hva_range_no_flush(mn, address, address + 1,
882 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
883 struct mm_struct *mm)
885 struct kvm *kvm = mmu_notifier_to_kvm(mn);
888 idx = srcu_read_lock(&kvm->srcu);
889 kvm_flush_shadow_all(kvm);
890 srcu_read_unlock(&kvm->srcu, idx);
893 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
894 .invalidate_range = kvm_mmu_notifier_invalidate_range,
895 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
896 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
897 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
898 .clear_young = kvm_mmu_notifier_clear_young,
899 .test_young = kvm_mmu_notifier_test_young,
900 .change_pte = kvm_mmu_notifier_change_pte,
901 .release = kvm_mmu_notifier_release,
904 static int kvm_init_mmu_notifier(struct kvm *kvm)
906 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
907 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
910 #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
912 static int kvm_init_mmu_notifier(struct kvm *kvm)
917 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
919 #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER
920 static int kvm_pm_notifier_call(struct notifier_block *bl,
924 struct kvm *kvm = container_of(bl, struct kvm, pm_notifier);
926 return kvm_arch_pm_notifier(kvm, state);
929 static void kvm_init_pm_notifier(struct kvm *kvm)
931 kvm->pm_notifier.notifier_call = kvm_pm_notifier_call;
932 /* Suspend KVM before we suspend ftrace, RCU, etc. */
933 kvm->pm_notifier.priority = INT_MAX;
934 register_pm_notifier(&kvm->pm_notifier);
937 static void kvm_destroy_pm_notifier(struct kvm *kvm)
939 unregister_pm_notifier(&kvm->pm_notifier);
941 #else /* !CONFIG_HAVE_KVM_PM_NOTIFIER */
942 static void kvm_init_pm_notifier(struct kvm *kvm)
946 static void kvm_destroy_pm_notifier(struct kvm *kvm)
949 #endif /* CONFIG_HAVE_KVM_PM_NOTIFIER */
951 static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
953 if (!memslot->dirty_bitmap)
956 kvfree(memslot->dirty_bitmap);
957 memslot->dirty_bitmap = NULL;
960 /* This does not remove the slot from struct kvm_memslots data structures */
961 static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
963 kvm_destroy_dirty_bitmap(slot);
965 kvm_arch_free_memslot(kvm, slot);
970 static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots)
972 struct hlist_node *idnode;
973 struct kvm_memory_slot *memslot;
977 * The same memslot objects live in both active and inactive sets,
978 * arbitrarily free using index '1' so the second invocation of this
979 * function isn't operating over a structure with dangling pointers
980 * (even though this function isn't actually touching them).
982 if (!slots->node_idx)
985 hash_for_each_safe(slots->id_hash, bkt, idnode, memslot, id_node[1])
986 kvm_free_memslot(kvm, memslot);
989 static umode_t kvm_stats_debugfs_mode(const struct _kvm_stats_desc *pdesc)
991 switch (pdesc->desc.flags & KVM_STATS_TYPE_MASK) {
992 case KVM_STATS_TYPE_INSTANT:
994 case KVM_STATS_TYPE_CUMULATIVE:
995 case KVM_STATS_TYPE_PEAK:
1002 static void kvm_destroy_vm_debugfs(struct kvm *kvm)
1005 int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc +
1006 kvm_vcpu_stats_header.num_desc;
1008 if (IS_ERR(kvm->debugfs_dentry))
1011 debugfs_remove_recursive(kvm->debugfs_dentry);
1013 if (kvm->debugfs_stat_data) {
1014 for (i = 0; i < kvm_debugfs_num_entries; i++)
1015 kfree(kvm->debugfs_stat_data[i]);
1016 kfree(kvm->debugfs_stat_data);
1020 static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
1022 static DEFINE_MUTEX(kvm_debugfs_lock);
1023 struct dentry *dent;
1024 char dir_name[ITOA_MAX_LEN * 2];
1025 struct kvm_stat_data *stat_data;
1026 const struct _kvm_stats_desc *pdesc;
1028 int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc +
1029 kvm_vcpu_stats_header.num_desc;
1031 if (!debugfs_initialized())
1034 snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd);
1035 mutex_lock(&kvm_debugfs_lock);
1036 dent = debugfs_lookup(dir_name, kvm_debugfs_dir);
1038 pr_warn_ratelimited("KVM: debugfs: duplicate directory %s\n", dir_name);
1040 mutex_unlock(&kvm_debugfs_lock);
1043 dent = debugfs_create_dir(dir_name, kvm_debugfs_dir);
1044 mutex_unlock(&kvm_debugfs_lock);
1048 kvm->debugfs_dentry = dent;
1049 kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries,
1050 sizeof(*kvm->debugfs_stat_data),
1051 GFP_KERNEL_ACCOUNT);
1052 if (!kvm->debugfs_stat_data)
1055 for (i = 0; i < kvm_vm_stats_header.num_desc; ++i) {
1056 pdesc = &kvm_vm_stats_desc[i];
1057 stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
1061 stat_data->kvm = kvm;
1062 stat_data->desc = pdesc;
1063 stat_data->kind = KVM_STAT_VM;
1064 kvm->debugfs_stat_data[i] = stat_data;
1065 debugfs_create_file(pdesc->name, kvm_stats_debugfs_mode(pdesc),
1066 kvm->debugfs_dentry, stat_data,
1070 for (i = 0; i < kvm_vcpu_stats_header.num_desc; ++i) {
1071 pdesc = &kvm_vcpu_stats_desc[i];
1072 stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
1076 stat_data->kvm = kvm;
1077 stat_data->desc = pdesc;
1078 stat_data->kind = KVM_STAT_VCPU;
1079 kvm->debugfs_stat_data[i + kvm_vm_stats_header.num_desc] = stat_data;
1080 debugfs_create_file(pdesc->name, kvm_stats_debugfs_mode(pdesc),
1081 kvm->debugfs_dentry, stat_data,
1085 ret = kvm_arch_create_vm_debugfs(kvm);
1087 kvm_destroy_vm_debugfs(kvm);
1095 * Called after the VM is otherwise initialized, but just before adding it to
1098 int __weak kvm_arch_post_init_vm(struct kvm *kvm)
1104 * Called just after removing the VM from the vm_list, but before doing any
1105 * other destruction.
1107 void __weak kvm_arch_pre_destroy_vm(struct kvm *kvm)
1112 * Called after per-vm debugfs created. When called kvm->debugfs_dentry should
1113 * be setup already, so we can create arch-specific debugfs entries under it.
1114 * Cleanup should be automatic done in kvm_destroy_vm_debugfs() recursively, so
1115 * a per-arch destroy interface is not needed.
1117 int __weak kvm_arch_create_vm_debugfs(struct kvm *kvm)
1122 static struct kvm *kvm_create_vm(unsigned long type)
1124 struct kvm *kvm = kvm_arch_alloc_vm();
1125 struct kvm_memslots *slots;
1130 return ERR_PTR(-ENOMEM);
1132 KVM_MMU_LOCK_INIT(kvm);
1133 mmgrab(current->mm);
1134 kvm->mm = current->mm;
1135 kvm_eventfd_init(kvm);
1136 mutex_init(&kvm->lock);
1137 mutex_init(&kvm->irq_lock);
1138 mutex_init(&kvm->slots_lock);
1139 mutex_init(&kvm->slots_arch_lock);
1140 spin_lock_init(&kvm->mn_invalidate_lock);
1141 rcuwait_init(&kvm->mn_memslots_update_rcuwait);
1142 xa_init(&kvm->vcpu_array);
1144 INIT_LIST_HEAD(&kvm->gpc_list);
1145 spin_lock_init(&kvm->gpc_lock);
1147 INIT_LIST_HEAD(&kvm->devices);
1148 kvm->max_vcpus = KVM_MAX_VCPUS;
1150 BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
1153 * Force subsequent debugfs file creations to fail if the VM directory
1154 * is not created (by kvm_create_vm_debugfs()).
1156 kvm->debugfs_dentry = ERR_PTR(-ENOENT);
1158 if (init_srcu_struct(&kvm->srcu))
1159 goto out_err_no_srcu;
1160 if (init_srcu_struct(&kvm->irq_srcu))
1161 goto out_err_no_irq_srcu;
1163 refcount_set(&kvm->users_count, 1);
1164 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
1165 for (j = 0; j < 2; j++) {
1166 slots = &kvm->__memslots[i][j];
1168 atomic_long_set(&slots->last_used_slot, (unsigned long)NULL);
1169 slots->hva_tree = RB_ROOT_CACHED;
1170 slots->gfn_tree = RB_ROOT;
1171 hash_init(slots->id_hash);
1172 slots->node_idx = j;
1174 /* Generations must be different for each address space. */
1175 slots->generation = i;
1178 rcu_assign_pointer(kvm->memslots[i], &kvm->__memslots[i][0]);
1181 for (i = 0; i < KVM_NR_BUSES; i++) {
1182 rcu_assign_pointer(kvm->buses[i],
1183 kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT));
1185 goto out_err_no_arch_destroy_vm;
1188 kvm->max_halt_poll_ns = halt_poll_ns;
1190 r = kvm_arch_init_vm(kvm, type);
1192 goto out_err_no_arch_destroy_vm;
1194 r = hardware_enable_all();
1196 goto out_err_no_disable;
1198 #ifdef CONFIG_HAVE_KVM_IRQFD
1199 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
1202 r = kvm_init_mmu_notifier(kvm);
1204 goto out_err_no_mmu_notifier;
1206 r = kvm_arch_post_init_vm(kvm);
1210 mutex_lock(&kvm_lock);
1211 list_add(&kvm->vm_list, &vm_list);
1212 mutex_unlock(&kvm_lock);
1214 preempt_notifier_inc();
1215 kvm_init_pm_notifier(kvm);
1218 * When the fd passed to this ioctl() is opened it pins the module,
1219 * but try_module_get() also prevents getting a reference if the module
1220 * is in MODULE_STATE_GOING (e.g. if someone ran "rmmod --wait").
1222 if (!try_module_get(kvm_chardev_ops.owner)) {
1230 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1231 if (kvm->mmu_notifier.ops)
1232 mmu_notifier_unregister(&kvm->mmu_notifier, current->mm);
1234 out_err_no_mmu_notifier:
1235 hardware_disable_all();
1237 kvm_arch_destroy_vm(kvm);
1238 out_err_no_arch_destroy_vm:
1239 WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
1240 for (i = 0; i < KVM_NR_BUSES; i++)
1241 kfree(kvm_get_bus(kvm, i));
1242 cleanup_srcu_struct(&kvm->irq_srcu);
1243 out_err_no_irq_srcu:
1244 cleanup_srcu_struct(&kvm->srcu);
1246 kvm_arch_free_vm(kvm);
1247 mmdrop(current->mm);
1251 static void kvm_destroy_devices(struct kvm *kvm)
1253 struct kvm_device *dev, *tmp;
1256 * We do not need to take the kvm->lock here, because nobody else
1257 * has a reference to the struct kvm at this point and therefore
1258 * cannot access the devices list anyhow.
1260 list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) {
1261 list_del(&dev->vm_node);
1262 dev->ops->destroy(dev);
1266 static void kvm_destroy_vm(struct kvm *kvm)
1269 struct mm_struct *mm = kvm->mm;
1271 kvm_destroy_pm_notifier(kvm);
1272 kvm_uevent_notify_change(KVM_EVENT_DESTROY_VM, kvm);
1273 kvm_destroy_vm_debugfs(kvm);
1274 kvm_arch_sync_events(kvm);
1275 mutex_lock(&kvm_lock);
1276 list_del(&kvm->vm_list);
1277 mutex_unlock(&kvm_lock);
1278 kvm_arch_pre_destroy_vm(kvm);
1280 kvm_free_irq_routing(kvm);
1281 for (i = 0; i < KVM_NR_BUSES; i++) {
1282 struct kvm_io_bus *bus = kvm_get_bus(kvm, i);
1285 kvm_io_bus_destroy(bus);
1286 kvm->buses[i] = NULL;
1288 kvm_coalesced_mmio_free(kvm);
1289 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1290 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
1292 * At this point, pending calls to invalidate_range_start()
1293 * have completed but no more MMU notifiers will run, so
1294 * mn_active_invalidate_count may remain unbalanced.
1295 * No threads can be waiting in install_new_memslots as the
1296 * last reference on KVM has been dropped, but freeing
1297 * memslots would deadlock without this manual intervention.
1299 WARN_ON(rcuwait_active(&kvm->mn_memslots_update_rcuwait));
1300 kvm->mn_active_invalidate_count = 0;
1302 kvm_flush_shadow_all(kvm);
1304 kvm_arch_destroy_vm(kvm);
1305 kvm_destroy_devices(kvm);
1306 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
1307 kvm_free_memslots(kvm, &kvm->__memslots[i][0]);
1308 kvm_free_memslots(kvm, &kvm->__memslots[i][1]);
1310 cleanup_srcu_struct(&kvm->irq_srcu);
1311 cleanup_srcu_struct(&kvm->srcu);
1312 kvm_arch_free_vm(kvm);
1313 preempt_notifier_dec();
1314 hardware_disable_all();
1316 module_put(kvm_chardev_ops.owner);
1319 void kvm_get_kvm(struct kvm *kvm)
1321 refcount_inc(&kvm->users_count);
1323 EXPORT_SYMBOL_GPL(kvm_get_kvm);
1326 * Make sure the vm is not during destruction, which is a safe version of
1327 * kvm_get_kvm(). Return true if kvm referenced successfully, false otherwise.
1329 bool kvm_get_kvm_safe(struct kvm *kvm)
1331 return refcount_inc_not_zero(&kvm->users_count);
1333 EXPORT_SYMBOL_GPL(kvm_get_kvm_safe);
1335 void kvm_put_kvm(struct kvm *kvm)
1337 if (refcount_dec_and_test(&kvm->users_count))
1338 kvm_destroy_vm(kvm);
1340 EXPORT_SYMBOL_GPL(kvm_put_kvm);
1343 * Used to put a reference that was taken on behalf of an object associated
1344 * with a user-visible file descriptor, e.g. a vcpu or device, if installation
1345 * of the new file descriptor fails and the reference cannot be transferred to
1346 * its final owner. In such cases, the caller is still actively using @kvm and
1347 * will fail miserably if the refcount unexpectedly hits zero.
1349 void kvm_put_kvm_no_destroy(struct kvm *kvm)
1351 WARN_ON(refcount_dec_and_test(&kvm->users_count));
1353 EXPORT_SYMBOL_GPL(kvm_put_kvm_no_destroy);
1355 static int kvm_vm_release(struct inode *inode, struct file *filp)
1357 struct kvm *kvm = filp->private_data;
1359 kvm_irqfd_release(kvm);
1366 * Allocation size is twice as large as the actual dirty bitmap size.
1367 * See kvm_vm_ioctl_get_dirty_log() why this is needed.
1369 static int kvm_alloc_dirty_bitmap(struct kvm_memory_slot *memslot)
1371 unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(memslot);
1373 memslot->dirty_bitmap = __vcalloc(2, dirty_bytes, GFP_KERNEL_ACCOUNT);
1374 if (!memslot->dirty_bitmap)
1380 static struct kvm_memslots *kvm_get_inactive_memslots(struct kvm *kvm, int as_id)
1382 struct kvm_memslots *active = __kvm_memslots(kvm, as_id);
1383 int node_idx_inactive = active->node_idx ^ 1;
1385 return &kvm->__memslots[as_id][node_idx_inactive];
1389 * Helper to get the address space ID when one of memslot pointers may be NULL.
1390 * This also serves as a sanity that at least one of the pointers is non-NULL,
1391 * and that their address space IDs don't diverge.
1393 static int kvm_memslots_get_as_id(struct kvm_memory_slot *a,
1394 struct kvm_memory_slot *b)
1396 if (WARN_ON_ONCE(!a && !b))
1404 WARN_ON_ONCE(a->as_id != b->as_id);
1408 static void kvm_insert_gfn_node(struct kvm_memslots *slots,
1409 struct kvm_memory_slot *slot)
1411 struct rb_root *gfn_tree = &slots->gfn_tree;
1412 struct rb_node **node, *parent;
1413 int idx = slots->node_idx;
1416 for (node = &gfn_tree->rb_node; *node; ) {
1417 struct kvm_memory_slot *tmp;
1419 tmp = container_of(*node, struct kvm_memory_slot, gfn_node[idx]);
1421 if (slot->base_gfn < tmp->base_gfn)
1422 node = &(*node)->rb_left;
1423 else if (slot->base_gfn > tmp->base_gfn)
1424 node = &(*node)->rb_right;
1429 rb_link_node(&slot->gfn_node[idx], parent, node);
1430 rb_insert_color(&slot->gfn_node[idx], gfn_tree);
1433 static void kvm_erase_gfn_node(struct kvm_memslots *slots,
1434 struct kvm_memory_slot *slot)
1436 rb_erase(&slot->gfn_node[slots->node_idx], &slots->gfn_tree);
1439 static void kvm_replace_gfn_node(struct kvm_memslots *slots,
1440 struct kvm_memory_slot *old,
1441 struct kvm_memory_slot *new)
1443 int idx = slots->node_idx;
1445 WARN_ON_ONCE(old->base_gfn != new->base_gfn);
1447 rb_replace_node(&old->gfn_node[idx], &new->gfn_node[idx],
1452 * Replace @old with @new in the inactive memslots.
1454 * With NULL @old this simply adds @new.
1455 * With NULL @new this simply removes @old.
1457 * If @new is non-NULL its hva_node[slots_idx] range has to be set
1460 static void kvm_replace_memslot(struct kvm *kvm,
1461 struct kvm_memory_slot *old,
1462 struct kvm_memory_slot *new)
1464 int as_id = kvm_memslots_get_as_id(old, new);
1465 struct kvm_memslots *slots = kvm_get_inactive_memslots(kvm, as_id);
1466 int idx = slots->node_idx;
1469 hash_del(&old->id_node[idx]);
1470 interval_tree_remove(&old->hva_node[idx], &slots->hva_tree);
1472 if ((long)old == atomic_long_read(&slots->last_used_slot))
1473 atomic_long_set(&slots->last_used_slot, (long)new);
1476 kvm_erase_gfn_node(slots, old);
1482 * Initialize @new's hva range. Do this even when replacing an @old
1483 * slot, kvm_copy_memslot() deliberately does not touch node data.
1485 new->hva_node[idx].start = new->userspace_addr;
1486 new->hva_node[idx].last = new->userspace_addr +
1487 (new->npages << PAGE_SHIFT) - 1;
1490 * (Re)Add the new memslot. There is no O(1) interval_tree_replace(),
1491 * hva_node needs to be swapped with remove+insert even though hva can't
1492 * change when replacing an existing slot.
1494 hash_add(slots->id_hash, &new->id_node[idx], new->id);
1495 interval_tree_insert(&new->hva_node[idx], &slots->hva_tree);
1498 * If the memslot gfn is unchanged, rb_replace_node() can be used to
1499 * switch the node in the gfn tree instead of removing the old and
1500 * inserting the new as two separate operations. Replacement is a
1501 * single O(1) operation versus two O(log(n)) operations for
1504 if (old && old->base_gfn == new->base_gfn) {
1505 kvm_replace_gfn_node(slots, old, new);
1508 kvm_erase_gfn_node(slots, old);
1509 kvm_insert_gfn_node(slots, new);
1513 static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem)
1515 u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
1517 #ifdef __KVM_HAVE_READONLY_MEM
1518 valid_flags |= KVM_MEM_READONLY;
1521 if (mem->flags & ~valid_flags)
1527 static void kvm_swap_active_memslots(struct kvm *kvm, int as_id)
1529 struct kvm_memslots *slots = kvm_get_inactive_memslots(kvm, as_id);
1531 /* Grab the generation from the activate memslots. */
1532 u64 gen = __kvm_memslots(kvm, as_id)->generation;
1534 WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS);
1535 slots->generation = gen | KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS;
1538 * Do not store the new memslots while there are invalidations in
1539 * progress, otherwise the locking in invalidate_range_start and
1540 * invalidate_range_end will be unbalanced.
1542 spin_lock(&kvm->mn_invalidate_lock);
1543 prepare_to_rcuwait(&kvm->mn_memslots_update_rcuwait);
1544 while (kvm->mn_active_invalidate_count) {
1545 set_current_state(TASK_UNINTERRUPTIBLE);
1546 spin_unlock(&kvm->mn_invalidate_lock);
1548 spin_lock(&kvm->mn_invalidate_lock);
1550 finish_rcuwait(&kvm->mn_memslots_update_rcuwait);
1551 rcu_assign_pointer(kvm->memslots[as_id], slots);
1552 spin_unlock(&kvm->mn_invalidate_lock);
1555 * Acquired in kvm_set_memslot. Must be released before synchronize
1556 * SRCU below in order to avoid deadlock with another thread
1557 * acquiring the slots_arch_lock in an srcu critical section.
1559 mutex_unlock(&kvm->slots_arch_lock);
1561 synchronize_srcu_expedited(&kvm->srcu);
1564 * Increment the new memslot generation a second time, dropping the
1565 * update in-progress flag and incrementing the generation based on
1566 * the number of address spaces. This provides a unique and easily
1567 * identifiable generation number while the memslots are in flux.
1569 gen = slots->generation & ~KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS;
1572 * Generations must be unique even across address spaces. We do not need
1573 * a global counter for that, instead the generation space is evenly split
1574 * across address spaces. For example, with two address spaces, address
1575 * space 0 will use generations 0, 2, 4, ... while address space 1 will
1576 * use generations 1, 3, 5, ...
1578 gen += KVM_ADDRESS_SPACE_NUM;
1580 kvm_arch_memslots_updated(kvm, gen);
1582 slots->generation = gen;
1585 static int kvm_prepare_memory_region(struct kvm *kvm,
1586 const struct kvm_memory_slot *old,
1587 struct kvm_memory_slot *new,
1588 enum kvm_mr_change change)
1593 * If dirty logging is disabled, nullify the bitmap; the old bitmap
1594 * will be freed on "commit". If logging is enabled in both old and
1595 * new, reuse the existing bitmap. If logging is enabled only in the
1596 * new and KVM isn't using a ring buffer, allocate and initialize a
1599 if (change != KVM_MR_DELETE) {
1600 if (!(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
1601 new->dirty_bitmap = NULL;
1602 else if (old && old->dirty_bitmap)
1603 new->dirty_bitmap = old->dirty_bitmap;
1604 else if (!kvm->dirty_ring_size) {
1605 r = kvm_alloc_dirty_bitmap(new);
1609 if (kvm_dirty_log_manual_protect_and_init_set(kvm))
1610 bitmap_set(new->dirty_bitmap, 0, new->npages);
1614 r = kvm_arch_prepare_memory_region(kvm, old, new, change);
1616 /* Free the bitmap on failure if it was allocated above. */
1617 if (r && new && new->dirty_bitmap && (!old || !old->dirty_bitmap))
1618 kvm_destroy_dirty_bitmap(new);
1623 static void kvm_commit_memory_region(struct kvm *kvm,
1624 struct kvm_memory_slot *old,
1625 const struct kvm_memory_slot *new,
1626 enum kvm_mr_change change)
1629 * Update the total number of memslot pages before calling the arch
1630 * hook so that architectures can consume the result directly.
1632 if (change == KVM_MR_DELETE)
1633 kvm->nr_memslot_pages -= old->npages;
1634 else if (change == KVM_MR_CREATE)
1635 kvm->nr_memslot_pages += new->npages;
1637 kvm_arch_commit_memory_region(kvm, old, new, change);
1641 /* Nothing more to do. */
1644 /* Free the old memslot and all its metadata. */
1645 kvm_free_memslot(kvm, old);
1648 case KVM_MR_FLAGS_ONLY:
1650 * Free the dirty bitmap as needed; the below check encompasses
1651 * both the flags and whether a ring buffer is being used)
1653 if (old->dirty_bitmap && !new->dirty_bitmap)
1654 kvm_destroy_dirty_bitmap(old);
1657 * The final quirk. Free the detached, old slot, but only its
1658 * memory, not any metadata. Metadata, including arch specific
1659 * data, may be reused by @new.
1669 * Activate @new, which must be installed in the inactive slots by the caller,
1670 * by swapping the active slots and then propagating @new to @old once @old is
1671 * unreachable and can be safely modified.
1673 * With NULL @old this simply adds @new to @active (while swapping the sets).
1674 * With NULL @new this simply removes @old from @active and frees it
1675 * (while also swapping the sets).
1677 static void kvm_activate_memslot(struct kvm *kvm,
1678 struct kvm_memory_slot *old,
1679 struct kvm_memory_slot *new)
1681 int as_id = kvm_memslots_get_as_id(old, new);
1683 kvm_swap_active_memslots(kvm, as_id);
1685 /* Propagate the new memslot to the now inactive memslots. */
1686 kvm_replace_memslot(kvm, old, new);
1689 static void kvm_copy_memslot(struct kvm_memory_slot *dest,
1690 const struct kvm_memory_slot *src)
1692 dest->base_gfn = src->base_gfn;
1693 dest->npages = src->npages;
1694 dest->dirty_bitmap = src->dirty_bitmap;
1695 dest->arch = src->arch;
1696 dest->userspace_addr = src->userspace_addr;
1697 dest->flags = src->flags;
1699 dest->as_id = src->as_id;
1702 static void kvm_invalidate_memslot(struct kvm *kvm,
1703 struct kvm_memory_slot *old,
1704 struct kvm_memory_slot *invalid_slot)
1707 * Mark the current slot INVALID. As with all memslot modifications,
1708 * this must be done on an unreachable slot to avoid modifying the
1709 * current slot in the active tree.
1711 kvm_copy_memslot(invalid_slot, old);
1712 invalid_slot->flags |= KVM_MEMSLOT_INVALID;
1713 kvm_replace_memslot(kvm, old, invalid_slot);
1716 * Activate the slot that is now marked INVALID, but don't propagate
1717 * the slot to the now inactive slots. The slot is either going to be
1718 * deleted or recreated as a new slot.
1720 kvm_swap_active_memslots(kvm, old->as_id);
1723 * From this point no new shadow pages pointing to a deleted, or moved,
1724 * memslot will be created. Validation of sp->gfn happens in:
1725 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
1726 * - kvm_is_visible_gfn (mmu_check_root)
1728 kvm_arch_flush_shadow_memslot(kvm, old);
1729 kvm_arch_guest_memory_reclaimed(kvm);
1731 /* Was released by kvm_swap_active_memslots, reacquire. */
1732 mutex_lock(&kvm->slots_arch_lock);
1735 * Copy the arch-specific field of the newly-installed slot back to the
1736 * old slot as the arch data could have changed between releasing
1737 * slots_arch_lock in install_new_memslots() and re-acquiring the lock
1738 * above. Writers are required to retrieve memslots *after* acquiring
1739 * slots_arch_lock, thus the active slot's data is guaranteed to be fresh.
1741 old->arch = invalid_slot->arch;
1744 static void kvm_create_memslot(struct kvm *kvm,
1745 struct kvm_memory_slot *new)
1747 /* Add the new memslot to the inactive set and activate. */
1748 kvm_replace_memslot(kvm, NULL, new);
1749 kvm_activate_memslot(kvm, NULL, new);
1752 static void kvm_delete_memslot(struct kvm *kvm,
1753 struct kvm_memory_slot *old,
1754 struct kvm_memory_slot *invalid_slot)
1757 * Remove the old memslot (in the inactive memslots) by passing NULL as
1758 * the "new" slot, and for the invalid version in the active slots.
1760 kvm_replace_memslot(kvm, old, NULL);
1761 kvm_activate_memslot(kvm, invalid_slot, NULL);
1764 static void kvm_move_memslot(struct kvm *kvm,
1765 struct kvm_memory_slot *old,
1766 struct kvm_memory_slot *new,
1767 struct kvm_memory_slot *invalid_slot)
1770 * Replace the old memslot in the inactive slots, and then swap slots
1771 * and replace the current INVALID with the new as well.
1773 kvm_replace_memslot(kvm, old, new);
1774 kvm_activate_memslot(kvm, invalid_slot, new);
1777 static void kvm_update_flags_memslot(struct kvm *kvm,
1778 struct kvm_memory_slot *old,
1779 struct kvm_memory_slot *new)
1782 * Similar to the MOVE case, but the slot doesn't need to be zapped as
1783 * an intermediate step. Instead, the old memslot is simply replaced
1784 * with a new, updated copy in both memslot sets.
1786 kvm_replace_memslot(kvm, old, new);
1787 kvm_activate_memslot(kvm, old, new);
1790 static int kvm_set_memslot(struct kvm *kvm,
1791 struct kvm_memory_slot *old,
1792 struct kvm_memory_slot *new,
1793 enum kvm_mr_change change)
1795 struct kvm_memory_slot *invalid_slot;
1799 * Released in kvm_swap_active_memslots.
1801 * Must be held from before the current memslots are copied until
1802 * after the new memslots are installed with rcu_assign_pointer,
1803 * then released before the synchronize srcu in kvm_swap_active_memslots.
1805 * When modifying memslots outside of the slots_lock, must be held
1806 * before reading the pointer to the current memslots until after all
1807 * changes to those memslots are complete.
1809 * These rules ensure that installing new memslots does not lose
1810 * changes made to the previous memslots.
1812 mutex_lock(&kvm->slots_arch_lock);
1815 * Invalidate the old slot if it's being deleted or moved. This is
1816 * done prior to actually deleting/moving the memslot to allow vCPUs to
1817 * continue running by ensuring there are no mappings or shadow pages
1818 * for the memslot when it is deleted/moved. Without pre-invalidation
1819 * (and without a lock), a window would exist between effecting the
1820 * delete/move and committing the changes in arch code where KVM or a
1821 * guest could access a non-existent memslot.
1823 * Modifications are done on a temporary, unreachable slot. The old
1824 * slot needs to be preserved in case a later step fails and the
1825 * invalidation needs to be reverted.
1827 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
1828 invalid_slot = kzalloc(sizeof(*invalid_slot), GFP_KERNEL_ACCOUNT);
1829 if (!invalid_slot) {
1830 mutex_unlock(&kvm->slots_arch_lock);
1833 kvm_invalidate_memslot(kvm, old, invalid_slot);
1836 r = kvm_prepare_memory_region(kvm, old, new, change);
1839 * For DELETE/MOVE, revert the above INVALID change. No
1840 * modifications required since the original slot was preserved
1841 * in the inactive slots. Changing the active memslots also
1842 * release slots_arch_lock.
1844 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
1845 kvm_activate_memslot(kvm, invalid_slot, old);
1846 kfree(invalid_slot);
1848 mutex_unlock(&kvm->slots_arch_lock);
1854 * For DELETE and MOVE, the working slot is now active as the INVALID
1855 * version of the old slot. MOVE is particularly special as it reuses
1856 * the old slot and returns a copy of the old slot (in working_slot).
1857 * For CREATE, there is no old slot. For DELETE and FLAGS_ONLY, the
1858 * old slot is detached but otherwise preserved.
1860 if (change == KVM_MR_CREATE)
1861 kvm_create_memslot(kvm, new);
1862 else if (change == KVM_MR_DELETE)
1863 kvm_delete_memslot(kvm, old, invalid_slot);
1864 else if (change == KVM_MR_MOVE)
1865 kvm_move_memslot(kvm, old, new, invalid_slot);
1866 else if (change == KVM_MR_FLAGS_ONLY)
1867 kvm_update_flags_memslot(kvm, old, new);
1871 /* Free the temporary INVALID slot used for DELETE and MOVE. */
1872 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE)
1873 kfree(invalid_slot);
1876 * No need to refresh new->arch, changes after dropping slots_arch_lock
1877 * will directly hit the final, active memslot. Architectures are
1878 * responsible for knowing that new->arch may be stale.
1880 kvm_commit_memory_region(kvm, old, new, change);
1885 static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id,
1886 gfn_t start, gfn_t end)
1888 struct kvm_memslot_iter iter;
1890 kvm_for_each_memslot_in_gfn_range(&iter, slots, start, end) {
1891 if (iter.slot->id != id)
1899 * Allocate some memory and give it an address in the guest physical address
1902 * Discontiguous memory is allowed, mostly for framebuffers.
1904 * Must be called holding kvm->slots_lock for write.
1906 int __kvm_set_memory_region(struct kvm *kvm,
1907 const struct kvm_userspace_memory_region *mem)
1909 struct kvm_memory_slot *old, *new;
1910 struct kvm_memslots *slots;
1911 enum kvm_mr_change change;
1912 unsigned long npages;
1917 r = check_memory_region_flags(mem);
1921 as_id = mem->slot >> 16;
1922 id = (u16)mem->slot;
1924 /* General sanity checks */
1925 if ((mem->memory_size & (PAGE_SIZE - 1)) ||
1926 (mem->memory_size != (unsigned long)mem->memory_size))
1928 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
1930 /* We can read the guest memory with __xxx_user() later on. */
1931 if ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
1932 (mem->userspace_addr != untagged_addr(mem->userspace_addr)) ||
1933 !access_ok((void __user *)(unsigned long)mem->userspace_addr,
1936 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM)
1938 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
1940 if ((mem->memory_size >> PAGE_SHIFT) > KVM_MEM_MAX_NR_PAGES)
1943 slots = __kvm_memslots(kvm, as_id);
1946 * Note, the old memslot (and the pointer itself!) may be invalidated
1947 * and/or destroyed by kvm_set_memslot().
1949 old = id_to_memslot(slots, id);
1951 if (!mem->memory_size) {
1952 if (!old || !old->npages)
1955 if (WARN_ON_ONCE(kvm->nr_memslot_pages < old->npages))
1958 return kvm_set_memslot(kvm, old, NULL, KVM_MR_DELETE);
1961 base_gfn = (mem->guest_phys_addr >> PAGE_SHIFT);
1962 npages = (mem->memory_size >> PAGE_SHIFT);
1964 if (!old || !old->npages) {
1965 change = KVM_MR_CREATE;
1968 * To simplify KVM internals, the total number of pages across
1969 * all memslots must fit in an unsigned long.
1971 if ((kvm->nr_memslot_pages + npages) < kvm->nr_memslot_pages)
1973 } else { /* Modify an existing slot. */
1974 if ((mem->userspace_addr != old->userspace_addr) ||
1975 (npages != old->npages) ||
1976 ((mem->flags ^ old->flags) & KVM_MEM_READONLY))
1979 if (base_gfn != old->base_gfn)
1980 change = KVM_MR_MOVE;
1981 else if (mem->flags != old->flags)
1982 change = KVM_MR_FLAGS_ONLY;
1983 else /* Nothing to change. */
1987 if ((change == KVM_MR_CREATE || change == KVM_MR_MOVE) &&
1988 kvm_check_memslot_overlap(slots, id, base_gfn, base_gfn + npages))
1991 /* Allocate a slot that will persist in the memslot. */
1992 new = kzalloc(sizeof(*new), GFP_KERNEL_ACCOUNT);
1998 new->base_gfn = base_gfn;
1999 new->npages = npages;
2000 new->flags = mem->flags;
2001 new->userspace_addr = mem->userspace_addr;
2003 r = kvm_set_memslot(kvm, old, new, change);
2008 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
2010 int kvm_set_memory_region(struct kvm *kvm,
2011 const struct kvm_userspace_memory_region *mem)
2015 mutex_lock(&kvm->slots_lock);
2016 r = __kvm_set_memory_region(kvm, mem);
2017 mutex_unlock(&kvm->slots_lock);
2020 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
2022 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
2023 struct kvm_userspace_memory_region *mem)
2025 if ((u16)mem->slot >= KVM_USER_MEM_SLOTS)
2028 return kvm_set_memory_region(kvm, mem);
2031 #ifndef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
2033 * kvm_get_dirty_log - get a snapshot of dirty pages
2034 * @kvm: pointer to kvm instance
2035 * @log: slot id and address to which we copy the log
2036 * @is_dirty: set to '1' if any dirty pages were found
2037 * @memslot: set to the associated memslot, always valid on success
2039 int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log,
2040 int *is_dirty, struct kvm_memory_slot **memslot)
2042 struct kvm_memslots *slots;
2045 unsigned long any = 0;
2047 /* Dirty ring tracking is exclusive to dirty log tracking */
2048 if (kvm->dirty_ring_size)
2054 as_id = log->slot >> 16;
2055 id = (u16)log->slot;
2056 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
2059 slots = __kvm_memslots(kvm, as_id);
2060 *memslot = id_to_memslot(slots, id);
2061 if (!(*memslot) || !(*memslot)->dirty_bitmap)
2064 kvm_arch_sync_dirty_log(kvm, *memslot);
2066 n = kvm_dirty_bitmap_bytes(*memslot);
2068 for (i = 0; !any && i < n/sizeof(long); ++i)
2069 any = (*memslot)->dirty_bitmap[i];
2071 if (copy_to_user(log->dirty_bitmap, (*memslot)->dirty_bitmap, n))
2078 EXPORT_SYMBOL_GPL(kvm_get_dirty_log);
2080 #else /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
2082 * kvm_get_dirty_log_protect - get a snapshot of dirty pages
2083 * and reenable dirty page tracking for the corresponding pages.
2084 * @kvm: pointer to kvm instance
2085 * @log: slot id and address to which we copy the log
2087 * We need to keep it in mind that VCPU threads can write to the bitmap
2088 * concurrently. So, to avoid losing track of dirty pages we keep the
2091 * 1. Take a snapshot of the bit and clear it if needed.
2092 * 2. Write protect the corresponding page.
2093 * 3. Copy the snapshot to the userspace.
2094 * 4. Upon return caller flushes TLB's if needed.
2096 * Between 2 and 4, the guest may write to the page using the remaining TLB
2097 * entry. This is not a problem because the page is reported dirty using
2098 * the snapshot taken before and step 4 ensures that writes done after
2099 * exiting to userspace will be logged for the next call.
2102 static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log)
2104 struct kvm_memslots *slots;
2105 struct kvm_memory_slot *memslot;
2108 unsigned long *dirty_bitmap;
2109 unsigned long *dirty_bitmap_buffer;
2112 /* Dirty ring tracking is exclusive to dirty log tracking */
2113 if (kvm->dirty_ring_size)
2116 as_id = log->slot >> 16;
2117 id = (u16)log->slot;
2118 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
2121 slots = __kvm_memslots(kvm, as_id);
2122 memslot = id_to_memslot(slots, id);
2123 if (!memslot || !memslot->dirty_bitmap)
2126 dirty_bitmap = memslot->dirty_bitmap;
2128 kvm_arch_sync_dirty_log(kvm, memslot);
2130 n = kvm_dirty_bitmap_bytes(memslot);
2132 if (kvm->manual_dirty_log_protect) {
2134 * Unlike kvm_get_dirty_log, we always return false in *flush,
2135 * because no flush is needed until KVM_CLEAR_DIRTY_LOG. There
2136 * is some code duplication between this function and
2137 * kvm_get_dirty_log, but hopefully all architecture
2138 * transition to kvm_get_dirty_log_protect and kvm_get_dirty_log
2139 * can be eliminated.
2141 dirty_bitmap_buffer = dirty_bitmap;
2143 dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);
2144 memset(dirty_bitmap_buffer, 0, n);
2147 for (i = 0; i < n / sizeof(long); i++) {
2151 if (!dirty_bitmap[i])
2155 mask = xchg(&dirty_bitmap[i], 0);
2156 dirty_bitmap_buffer[i] = mask;
2158 offset = i * BITS_PER_LONG;
2159 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
2162 KVM_MMU_UNLOCK(kvm);
2166 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
2168 if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
2175 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
2176 * @kvm: kvm instance
2177 * @log: slot id and address to which we copy the log
2179 * Steps 1-4 below provide general overview of dirty page logging. See
2180 * kvm_get_dirty_log_protect() function description for additional details.
2182 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
2183 * always flush the TLB (step 4) even if previous step failed and the dirty
2184 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
2185 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
2186 * writes will be marked dirty for next log read.
2188 * 1. Take a snapshot of the bit and clear it if needed.
2189 * 2. Write protect the corresponding page.
2190 * 3. Copy the snapshot to the userspace.
2191 * 4. Flush TLB's if needed.
2193 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2194 struct kvm_dirty_log *log)
2198 mutex_lock(&kvm->slots_lock);
2200 r = kvm_get_dirty_log_protect(kvm, log);
2202 mutex_unlock(&kvm->slots_lock);
2207 * kvm_clear_dirty_log_protect - clear dirty bits in the bitmap
2208 * and reenable dirty page tracking for the corresponding pages.
2209 * @kvm: pointer to kvm instance
2210 * @log: slot id and address from which to fetch the bitmap of dirty pages
2212 static int kvm_clear_dirty_log_protect(struct kvm *kvm,
2213 struct kvm_clear_dirty_log *log)
2215 struct kvm_memslots *slots;
2216 struct kvm_memory_slot *memslot;
2220 unsigned long *dirty_bitmap;
2221 unsigned long *dirty_bitmap_buffer;
2224 /* Dirty ring tracking is exclusive to dirty log tracking */
2225 if (kvm->dirty_ring_size)
2228 as_id = log->slot >> 16;
2229 id = (u16)log->slot;
2230 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
2233 if (log->first_page & 63)
2236 slots = __kvm_memslots(kvm, as_id);
2237 memslot = id_to_memslot(slots, id);
2238 if (!memslot || !memslot->dirty_bitmap)
2241 dirty_bitmap = memslot->dirty_bitmap;
2243 n = ALIGN(log->num_pages, BITS_PER_LONG) / 8;
2245 if (log->first_page > memslot->npages ||
2246 log->num_pages > memslot->npages - log->first_page ||
2247 (log->num_pages < memslot->npages - log->first_page && (log->num_pages & 63)))
2250 kvm_arch_sync_dirty_log(kvm, memslot);
2253 dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);
2254 if (copy_from_user(dirty_bitmap_buffer, log->dirty_bitmap, n))
2258 for (offset = log->first_page, i = offset / BITS_PER_LONG,
2259 n = DIV_ROUND_UP(log->num_pages, BITS_PER_LONG); n--;
2260 i++, offset += BITS_PER_LONG) {
2261 unsigned long mask = *dirty_bitmap_buffer++;
2262 atomic_long_t *p = (atomic_long_t *) &dirty_bitmap[i];
2266 mask &= atomic_long_fetch_andnot(mask, p);
2269 * mask contains the bits that really have been cleared. This
2270 * never includes any bits beyond the length of the memslot (if
2271 * the length is not aligned to 64 pages), therefore it is not
2272 * a problem if userspace sets them in log->dirty_bitmap.
2276 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
2280 KVM_MMU_UNLOCK(kvm);
2283 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
2288 static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,
2289 struct kvm_clear_dirty_log *log)
2293 mutex_lock(&kvm->slots_lock);
2295 r = kvm_clear_dirty_log_protect(kvm, log);
2297 mutex_unlock(&kvm->slots_lock);
2300 #endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
2302 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
2304 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
2306 EXPORT_SYMBOL_GPL(gfn_to_memslot);
2308 struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn)
2310 struct kvm_memslots *slots = kvm_vcpu_memslots(vcpu);
2311 u64 gen = slots->generation;
2312 struct kvm_memory_slot *slot;
2315 * This also protects against using a memslot from a different address space,
2316 * since different address spaces have different generation numbers.
2318 if (unlikely(gen != vcpu->last_used_slot_gen)) {
2319 vcpu->last_used_slot = NULL;
2320 vcpu->last_used_slot_gen = gen;
2323 slot = try_get_memslot(vcpu->last_used_slot, gfn);
2328 * Fall back to searching all memslots. We purposely use
2329 * search_memslots() instead of __gfn_to_memslot() to avoid
2330 * thrashing the VM-wide last_used_slot in kvm_memslots.
2332 slot = search_memslots(slots, gfn, false);
2334 vcpu->last_used_slot = slot;
2341 bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
2343 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
2345 return kvm_is_visible_memslot(memslot);
2347 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
2349 bool kvm_vcpu_is_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
2351 struct kvm_memory_slot *memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2353 return kvm_is_visible_memslot(memslot);
2355 EXPORT_SYMBOL_GPL(kvm_vcpu_is_visible_gfn);
2357 unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn)
2359 struct vm_area_struct *vma;
2360 unsigned long addr, size;
2364 addr = kvm_vcpu_gfn_to_hva_prot(vcpu, gfn, NULL);
2365 if (kvm_is_error_hva(addr))
2368 mmap_read_lock(current->mm);
2369 vma = find_vma(current->mm, addr);
2373 size = vma_kernel_pagesize(vma);
2376 mmap_read_unlock(current->mm);
2381 static bool memslot_is_readonly(const struct kvm_memory_slot *slot)
2383 return slot->flags & KVM_MEM_READONLY;
2386 static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t gfn,
2387 gfn_t *nr_pages, bool write)
2389 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
2390 return KVM_HVA_ERR_BAD;
2392 if (memslot_is_readonly(slot) && write)
2393 return KVM_HVA_ERR_RO_BAD;
2396 *nr_pages = slot->npages - (gfn - slot->base_gfn);
2398 return __gfn_to_hva_memslot(slot, gfn);
2401 static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
2404 return __gfn_to_hva_many(slot, gfn, nr_pages, true);
2407 unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
2410 return gfn_to_hva_many(slot, gfn, NULL);
2412 EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
2414 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
2416 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
2418 EXPORT_SYMBOL_GPL(gfn_to_hva);
2420 unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn)
2422 return gfn_to_hva_many(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, NULL);
2424 EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva);
2427 * Return the hva of a @gfn and the R/W attribute if possible.
2429 * @slot: the kvm_memory_slot which contains @gfn
2430 * @gfn: the gfn to be translated
2431 * @writable: used to return the read/write attribute of the @slot if the hva
2432 * is valid and @writable is not NULL
2434 unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot,
2435 gfn_t gfn, bool *writable)
2437 unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
2439 if (!kvm_is_error_hva(hva) && writable)
2440 *writable = !memslot_is_readonly(slot);
2445 unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
2447 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
2449 return gfn_to_hva_memslot_prot(slot, gfn, writable);
2452 unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable)
2454 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2456 return gfn_to_hva_memslot_prot(slot, gfn, writable);
2459 static inline int check_user_page_hwpoison(unsigned long addr)
2461 int rc, flags = FOLL_HWPOISON | FOLL_WRITE;
2463 rc = get_user_pages(addr, 1, flags, NULL, NULL);
2464 return rc == -EHWPOISON;
2468 * The fast path to get the writable pfn which will be stored in @pfn,
2469 * true indicates success, otherwise false is returned. It's also the
2470 * only part that runs if we can in atomic context.
2472 static bool hva_to_pfn_fast(unsigned long addr, bool write_fault,
2473 bool *writable, kvm_pfn_t *pfn)
2475 struct page *page[1];
2478 * Fast pin a writable pfn only if it is a write fault request
2479 * or the caller allows to map a writable pfn for a read fault
2482 if (!(write_fault || writable))
2485 if (get_user_page_fast_only(addr, FOLL_WRITE, page)) {
2486 *pfn = page_to_pfn(page[0]);
2497 * The slow path to get the pfn of the specified host virtual address,
2498 * 1 indicates success, -errno is returned if error is detected.
2500 static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
2501 bool *writable, kvm_pfn_t *pfn)
2503 unsigned int flags = FOLL_HWPOISON;
2510 *writable = write_fault;
2513 flags |= FOLL_WRITE;
2515 flags |= FOLL_NOWAIT;
2517 npages = get_user_pages_unlocked(addr, 1, &page, flags);
2521 /* map read fault as writable if possible */
2522 if (unlikely(!write_fault) && writable) {
2525 if (get_user_page_fast_only(addr, FOLL_WRITE, &wpage)) {
2531 *pfn = page_to_pfn(page);
2535 static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
2537 if (unlikely(!(vma->vm_flags & VM_READ)))
2540 if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
2546 static int kvm_try_get_pfn(kvm_pfn_t pfn)
2548 struct page *page = kvm_pfn_to_refcounted_page(pfn);
2553 return get_page_unless_zero(page);
2556 static int hva_to_pfn_remapped(struct vm_area_struct *vma,
2557 unsigned long addr, bool write_fault,
2558 bool *writable, kvm_pfn_t *p_pfn)
2565 r = follow_pte(vma->vm_mm, addr, &ptep, &ptl);
2568 * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does
2569 * not call the fault handler, so do it here.
2571 bool unlocked = false;
2572 r = fixup_user_fault(current->mm, addr,
2573 (write_fault ? FAULT_FLAG_WRITE : 0),
2580 r = follow_pte(vma->vm_mm, addr, &ptep, &ptl);
2585 if (write_fault && !pte_write(*ptep)) {
2586 pfn = KVM_PFN_ERR_RO_FAULT;
2591 *writable = pte_write(*ptep);
2592 pfn = pte_pfn(*ptep);
2595 * Get a reference here because callers of *hva_to_pfn* and
2596 * *gfn_to_pfn* ultimately call kvm_release_pfn_clean on the
2597 * returned pfn. This is only needed if the VMA has VM_MIXEDMAP
2598 * set, but the kvm_try_get_pfn/kvm_release_pfn_clean pair will
2599 * simply do nothing for reserved pfns.
2601 * Whoever called remap_pfn_range is also going to call e.g.
2602 * unmap_mapping_range before the underlying pages are freed,
2603 * causing a call to our MMU notifier.
2605 * Certain IO or PFNMAP mappings can be backed with valid
2606 * struct pages, but be allocated without refcounting e.g.,
2607 * tail pages of non-compound higher order allocations, which
2608 * would then underflow the refcount when the caller does the
2609 * required put_page. Don't allow those pages here.
2611 if (!kvm_try_get_pfn(pfn))
2615 pte_unmap_unlock(ptep, ptl);
2622 * Pin guest page in memory and return its pfn.
2623 * @addr: host virtual address which maps memory to the guest
2624 * @atomic: whether this function can sleep
2625 * @async: whether this function need to wait IO complete if the
2626 * host page is not in the memory
2627 * @write_fault: whether we should get a writable host page
2628 * @writable: whether it allows to map a writable host page for !@write_fault
2630 * The function will map a writable host page for these two cases:
2631 * 1): @write_fault = true
2632 * 2): @write_fault = false && @writable, @writable will tell the caller
2633 * whether the mapping is writable.
2635 kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
2636 bool write_fault, bool *writable)
2638 struct vm_area_struct *vma;
2642 /* we can do it either atomically or asynchronously, not both */
2643 BUG_ON(atomic && async);
2645 if (hva_to_pfn_fast(addr, write_fault, writable, &pfn))
2649 return KVM_PFN_ERR_FAULT;
2651 npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
2655 mmap_read_lock(current->mm);
2656 if (npages == -EHWPOISON ||
2657 (!async && check_user_page_hwpoison(addr))) {
2658 pfn = KVM_PFN_ERR_HWPOISON;
2663 vma = vma_lookup(current->mm, addr);
2666 pfn = KVM_PFN_ERR_FAULT;
2667 else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) {
2668 r = hva_to_pfn_remapped(vma, addr, write_fault, writable, &pfn);
2672 pfn = KVM_PFN_ERR_FAULT;
2674 if (async && vma_is_valid(vma, write_fault))
2676 pfn = KVM_PFN_ERR_FAULT;
2679 mmap_read_unlock(current->mm);
2683 kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn,
2684 bool atomic, bool *async, bool write_fault,
2685 bool *writable, hva_t *hva)
2687 unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
2692 if (addr == KVM_HVA_ERR_RO_BAD) {
2695 return KVM_PFN_ERR_RO_FAULT;
2698 if (kvm_is_error_hva(addr)) {
2701 return KVM_PFN_NOSLOT;
2704 /* Do not map writable pfn in the readonly memslot. */
2705 if (writable && memslot_is_readonly(slot)) {
2710 return hva_to_pfn(addr, atomic, async, write_fault,
2713 EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot);
2715 kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
2718 return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL,
2719 write_fault, writable, NULL);
2721 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
2723 kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn)
2725 return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL, NULL);
2727 EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot);
2729 kvm_pfn_t gfn_to_pfn_memslot_atomic(const struct kvm_memory_slot *slot, gfn_t gfn)
2731 return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL, NULL);
2733 EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
2735 kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn)
2737 return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
2739 EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic);
2741 kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
2743 return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn);
2745 EXPORT_SYMBOL_GPL(gfn_to_pfn);
2747 kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
2749 return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
2751 EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn);
2753 int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
2754 struct page **pages, int nr_pages)
2759 addr = gfn_to_hva_many(slot, gfn, &entry);
2760 if (kvm_is_error_hva(addr))
2763 if (entry < nr_pages)
2766 return get_user_pages_fast_only(addr, nr_pages, FOLL_WRITE, pages);
2768 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
2771 * Do not use this helper unless you are absolutely certain the gfn _must_ be
2772 * backed by 'struct page'. A valid example is if the backing memslot is
2773 * controlled by KVM. Note, if the returned page is valid, it's refcount has
2774 * been elevated by gfn_to_pfn().
2776 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
2781 pfn = gfn_to_pfn(kvm, gfn);
2783 if (is_error_noslot_pfn(pfn))
2784 return KVM_ERR_PTR_BAD_PAGE;
2786 page = kvm_pfn_to_refcounted_page(pfn);
2788 return KVM_ERR_PTR_BAD_PAGE;
2792 EXPORT_SYMBOL_GPL(gfn_to_page);
2794 void kvm_release_pfn(kvm_pfn_t pfn, bool dirty)
2797 kvm_release_pfn_dirty(pfn);
2799 kvm_release_pfn_clean(pfn);
2802 int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map)
2806 struct page *page = KVM_UNMAPPED_PAGE;
2811 pfn = gfn_to_pfn(vcpu->kvm, gfn);
2812 if (is_error_noslot_pfn(pfn))
2815 if (pfn_valid(pfn)) {
2816 page = pfn_to_page(pfn);
2818 #ifdef CONFIG_HAS_IOMEM
2820 hva = memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB);
2834 EXPORT_SYMBOL_GPL(kvm_vcpu_map);
2836 void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty)
2844 if (map->page != KVM_UNMAPPED_PAGE)
2846 #ifdef CONFIG_HAS_IOMEM
2852 kvm_vcpu_mark_page_dirty(vcpu, map->gfn);
2854 kvm_release_pfn(map->pfn, dirty);
2859 EXPORT_SYMBOL_GPL(kvm_vcpu_unmap);
2861 static bool kvm_is_ad_tracked_page(struct page *page)
2864 * Per page-flags.h, pages tagged PG_reserved "should in general not be
2865 * touched (e.g. set dirty) except by its owner".
2867 return !PageReserved(page);
2870 static void kvm_set_page_dirty(struct page *page)
2872 if (kvm_is_ad_tracked_page(page))
2876 static void kvm_set_page_accessed(struct page *page)
2878 if (kvm_is_ad_tracked_page(page))
2879 mark_page_accessed(page);
2882 void kvm_release_page_clean(struct page *page)
2884 WARN_ON(is_error_page(page));
2886 kvm_set_page_accessed(page);
2889 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
2891 void kvm_release_pfn_clean(kvm_pfn_t pfn)
2895 if (is_error_noslot_pfn(pfn))
2898 page = kvm_pfn_to_refcounted_page(pfn);
2902 kvm_release_page_clean(page);
2904 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
2906 void kvm_release_page_dirty(struct page *page)
2908 WARN_ON(is_error_page(page));
2910 kvm_set_page_dirty(page);
2911 kvm_release_page_clean(page);
2913 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
2915 void kvm_release_pfn_dirty(kvm_pfn_t pfn)
2919 if (is_error_noslot_pfn(pfn))
2922 page = kvm_pfn_to_refcounted_page(pfn);
2926 kvm_release_page_dirty(page);
2928 EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
2931 * Note, checking for an error/noslot pfn is the caller's responsibility when
2932 * directly marking a page dirty/accessed. Unlike the "release" helpers, the
2933 * "set" helpers are not to be used when the pfn might point at garbage.
2935 void kvm_set_pfn_dirty(kvm_pfn_t pfn)
2937 if (WARN_ON(is_error_noslot_pfn(pfn)))
2941 kvm_set_page_dirty(pfn_to_page(pfn));
2943 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
2945 void kvm_set_pfn_accessed(kvm_pfn_t pfn)
2947 if (WARN_ON(is_error_noslot_pfn(pfn)))
2951 kvm_set_page_accessed(pfn_to_page(pfn));
2953 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
2955 static int next_segment(unsigned long len, int offset)
2957 if (len > PAGE_SIZE - offset)
2958 return PAGE_SIZE - offset;
2963 static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn,
2964 void *data, int offset, int len)
2969 addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
2970 if (kvm_is_error_hva(addr))
2972 r = __copy_from_user(data, (void __user *)addr + offset, len);
2978 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
2981 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
2983 return __kvm_read_guest_page(slot, gfn, data, offset, len);
2985 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
2987 int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data,
2988 int offset, int len)
2990 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2992 return __kvm_read_guest_page(slot, gfn, data, offset, len);
2994 EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page);
2996 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
2998 gfn_t gfn = gpa >> PAGE_SHIFT;
3000 int offset = offset_in_page(gpa);
3003 while ((seg = next_segment(len, offset)) != 0) {
3004 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
3014 EXPORT_SYMBOL_GPL(kvm_read_guest);
3016 int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, unsigned long len)
3018 gfn_t gfn = gpa >> PAGE_SHIFT;
3020 int offset = offset_in_page(gpa);
3023 while ((seg = next_segment(len, offset)) != 0) {
3024 ret = kvm_vcpu_read_guest_page(vcpu, gfn, data, offset, seg);
3034 EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest);
3036 static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
3037 void *data, int offset, unsigned long len)
3042 addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
3043 if (kvm_is_error_hva(addr))
3045 pagefault_disable();
3046 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
3053 int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa,
3054 void *data, unsigned long len)
3056 gfn_t gfn = gpa >> PAGE_SHIFT;
3057 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
3058 int offset = offset_in_page(gpa);
3060 return __kvm_read_guest_atomic(slot, gfn, data, offset, len);
3062 EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic);
3064 static int __kvm_write_guest_page(struct kvm *kvm,
3065 struct kvm_memory_slot *memslot, gfn_t gfn,
3066 const void *data, int offset, int len)
3071 addr = gfn_to_hva_memslot(memslot, gfn);
3072 if (kvm_is_error_hva(addr))
3074 r = __copy_to_user((void __user *)addr + offset, data, len);
3077 mark_page_dirty_in_slot(kvm, memslot, gfn);
3081 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn,
3082 const void *data, int offset, int len)
3084 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
3086 return __kvm_write_guest_page(kvm, slot, gfn, data, offset, len);
3088 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
3090 int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
3091 const void *data, int offset, int len)
3093 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
3095 return __kvm_write_guest_page(vcpu->kvm, slot, gfn, data, offset, len);
3097 EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page);
3099 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
3102 gfn_t gfn = gpa >> PAGE_SHIFT;
3104 int offset = offset_in_page(gpa);
3107 while ((seg = next_segment(len, offset)) != 0) {
3108 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
3118 EXPORT_SYMBOL_GPL(kvm_write_guest);
3120 int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
3123 gfn_t gfn = gpa >> PAGE_SHIFT;
3125 int offset = offset_in_page(gpa);
3128 while ((seg = next_segment(len, offset)) != 0) {
3129 ret = kvm_vcpu_write_guest_page(vcpu, gfn, data, offset, seg);
3139 EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest);
3141 static int __kvm_gfn_to_hva_cache_init(struct kvm_memslots *slots,
3142 struct gfn_to_hva_cache *ghc,
3143 gpa_t gpa, unsigned long len)
3145 int offset = offset_in_page(gpa);
3146 gfn_t start_gfn = gpa >> PAGE_SHIFT;
3147 gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
3148 gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
3149 gfn_t nr_pages_avail;
3151 /* Update ghc->generation before performing any error checks. */
3152 ghc->generation = slots->generation;
3154 if (start_gfn > end_gfn) {
3155 ghc->hva = KVM_HVA_ERR_BAD;
3160 * If the requested region crosses two memslots, we still
3161 * verify that the entire region is valid here.
3163 for ( ; start_gfn <= end_gfn; start_gfn += nr_pages_avail) {
3164 ghc->memslot = __gfn_to_memslot(slots, start_gfn);
3165 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
3167 if (kvm_is_error_hva(ghc->hva))
3171 /* Use the slow path for cross page reads and writes. */
3172 if (nr_pages_needed == 1)
3175 ghc->memslot = NULL;
3182 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
3183 gpa_t gpa, unsigned long len)
3185 struct kvm_memslots *slots = kvm_memslots(kvm);
3186 return __kvm_gfn_to_hva_cache_init(slots, ghc, gpa, len);
3188 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
3190 int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
3191 void *data, unsigned int offset,
3194 struct kvm_memslots *slots = kvm_memslots(kvm);
3196 gpa_t gpa = ghc->gpa + offset;
3198 if (WARN_ON_ONCE(len + offset > ghc->len))
3201 if (slots->generation != ghc->generation) {
3202 if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len))
3206 if (kvm_is_error_hva(ghc->hva))
3209 if (unlikely(!ghc->memslot))
3210 return kvm_write_guest(kvm, gpa, data, len);
3212 r = __copy_to_user((void __user *)ghc->hva + offset, data, len);
3215 mark_page_dirty_in_slot(kvm, ghc->memslot, gpa >> PAGE_SHIFT);
3219 EXPORT_SYMBOL_GPL(kvm_write_guest_offset_cached);
3221 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
3222 void *data, unsigned long len)
3224 return kvm_write_guest_offset_cached(kvm, ghc, data, 0, len);
3226 EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
3228 int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
3229 void *data, unsigned int offset,
3232 struct kvm_memslots *slots = kvm_memslots(kvm);
3234 gpa_t gpa = ghc->gpa + offset;
3236 if (WARN_ON_ONCE(len + offset > ghc->len))
3239 if (slots->generation != ghc->generation) {
3240 if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len))
3244 if (kvm_is_error_hva(ghc->hva))
3247 if (unlikely(!ghc->memslot))
3248 return kvm_read_guest(kvm, gpa, data, len);
3250 r = __copy_from_user(data, (void __user *)ghc->hva + offset, len);
3256 EXPORT_SYMBOL_GPL(kvm_read_guest_offset_cached);
3258 int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
3259 void *data, unsigned long len)
3261 return kvm_read_guest_offset_cached(kvm, ghc, data, 0, len);
3263 EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
3265 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
3267 const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
3268 gfn_t gfn = gpa >> PAGE_SHIFT;
3270 int offset = offset_in_page(gpa);
3273 while ((seg = next_segment(len, offset)) != 0) {
3274 ret = kvm_write_guest_page(kvm, gfn, zero_page, offset, len);
3283 EXPORT_SYMBOL_GPL(kvm_clear_guest);
3285 void mark_page_dirty_in_slot(struct kvm *kvm,
3286 const struct kvm_memory_slot *memslot,
3289 struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
3291 #ifdef CONFIG_HAVE_KVM_DIRTY_RING
3292 if (WARN_ON_ONCE(!vcpu) || WARN_ON_ONCE(vcpu->kvm != kvm))
3296 if (memslot && kvm_slot_dirty_track_enabled(memslot)) {
3297 unsigned long rel_gfn = gfn - memslot->base_gfn;
3298 u32 slot = (memslot->as_id << 16) | memslot->id;
3300 if (kvm->dirty_ring_size)
3301 kvm_dirty_ring_push(&vcpu->dirty_ring,
3304 set_bit_le(rel_gfn, memslot->dirty_bitmap);
3307 EXPORT_SYMBOL_GPL(mark_page_dirty_in_slot);
3309 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
3311 struct kvm_memory_slot *memslot;
3313 memslot = gfn_to_memslot(kvm, gfn);
3314 mark_page_dirty_in_slot(kvm, memslot, gfn);
3316 EXPORT_SYMBOL_GPL(mark_page_dirty);
3318 void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn)
3320 struct kvm_memory_slot *memslot;
3322 memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
3323 mark_page_dirty_in_slot(vcpu->kvm, memslot, gfn);
3325 EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty);
3327 void kvm_sigset_activate(struct kvm_vcpu *vcpu)
3329 if (!vcpu->sigset_active)
3333 * This does a lockless modification of ->real_blocked, which is fine
3334 * because, only current can change ->real_blocked and all readers of
3335 * ->real_blocked don't care as long ->real_blocked is always a subset
3338 sigprocmask(SIG_SETMASK, &vcpu->sigset, ¤t->real_blocked);
3341 void kvm_sigset_deactivate(struct kvm_vcpu *vcpu)
3343 if (!vcpu->sigset_active)
3346 sigprocmask(SIG_SETMASK, ¤t->real_blocked, NULL);
3347 sigemptyset(¤t->real_blocked);
3350 static void grow_halt_poll_ns(struct kvm_vcpu *vcpu)
3352 unsigned int old, val, grow, grow_start;
3354 old = val = vcpu->halt_poll_ns;
3355 grow_start = READ_ONCE(halt_poll_ns_grow_start);
3356 grow = READ_ONCE(halt_poll_ns_grow);
3361 if (val < grow_start)
3364 if (val > vcpu->kvm->max_halt_poll_ns)
3365 val = vcpu->kvm->max_halt_poll_ns;
3367 vcpu->halt_poll_ns = val;
3369 trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old);
3372 static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu)
3374 unsigned int old, val, shrink, grow_start;
3376 old = val = vcpu->halt_poll_ns;
3377 shrink = READ_ONCE(halt_poll_ns_shrink);
3378 grow_start = READ_ONCE(halt_poll_ns_grow_start);
3384 if (val < grow_start)
3387 vcpu->halt_poll_ns = val;
3388 trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old);
3391 static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
3394 int idx = srcu_read_lock(&vcpu->kvm->srcu);
3396 if (kvm_arch_vcpu_runnable(vcpu)) {
3397 kvm_make_request(KVM_REQ_UNHALT, vcpu);
3400 if (kvm_cpu_has_pending_timer(vcpu))
3402 if (signal_pending(current))
3404 if (kvm_check_request(KVM_REQ_UNBLOCK, vcpu))
3409 srcu_read_unlock(&vcpu->kvm->srcu, idx);
3414 * Block the vCPU until the vCPU is runnable, an event arrives, or a signal is
3415 * pending. This is mostly used when halting a vCPU, but may also be used
3416 * directly for other vCPU non-runnable states, e.g. x86's Wait-For-SIPI.
3418 bool kvm_vcpu_block(struct kvm_vcpu *vcpu)
3420 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
3421 bool waited = false;
3423 vcpu->stat.generic.blocking = 1;
3426 kvm_arch_vcpu_blocking(vcpu);
3427 prepare_to_rcuwait(wait);
3431 set_current_state(TASK_INTERRUPTIBLE);
3433 if (kvm_vcpu_check_block(vcpu) < 0)
3441 finish_rcuwait(wait);
3442 kvm_arch_vcpu_unblocking(vcpu);
3445 vcpu->stat.generic.blocking = 0;
3450 static inline void update_halt_poll_stats(struct kvm_vcpu *vcpu, ktime_t start,
3451 ktime_t end, bool success)
3453 struct kvm_vcpu_stat_generic *stats = &vcpu->stat.generic;
3454 u64 poll_ns = ktime_to_ns(ktime_sub(end, start));
3456 ++vcpu->stat.generic.halt_attempted_poll;
3459 ++vcpu->stat.generic.halt_successful_poll;
3461 if (!vcpu_valid_wakeup(vcpu))
3462 ++vcpu->stat.generic.halt_poll_invalid;
3464 stats->halt_poll_success_ns += poll_ns;
3465 KVM_STATS_LOG_HIST_UPDATE(stats->halt_poll_success_hist, poll_ns);
3467 stats->halt_poll_fail_ns += poll_ns;
3468 KVM_STATS_LOG_HIST_UPDATE(stats->halt_poll_fail_hist, poll_ns);
3473 * Emulate a vCPU halt condition, e.g. HLT on x86, WFI on arm, etc... If halt
3474 * polling is enabled, busy wait for a short time before blocking to avoid the
3475 * expensive block+unblock sequence if a wake event arrives soon after the vCPU
3478 void kvm_vcpu_halt(struct kvm_vcpu *vcpu)
3480 bool halt_poll_allowed = !kvm_arch_no_poll(vcpu);
3481 bool do_halt_poll = halt_poll_allowed && vcpu->halt_poll_ns;
3482 ktime_t start, cur, poll_end;
3483 bool waited = false;
3486 start = cur = poll_end = ktime_get();
3488 ktime_t stop = ktime_add_ns(start, vcpu->halt_poll_ns);
3492 * This sets KVM_REQ_UNHALT if an interrupt
3495 if (kvm_vcpu_check_block(vcpu) < 0)
3498 poll_end = cur = ktime_get();
3499 } while (kvm_vcpu_can_poll(cur, stop));
3502 waited = kvm_vcpu_block(vcpu);
3506 vcpu->stat.generic.halt_wait_ns +=
3507 ktime_to_ns(cur) - ktime_to_ns(poll_end);
3508 KVM_STATS_LOG_HIST_UPDATE(vcpu->stat.generic.halt_wait_hist,
3509 ktime_to_ns(cur) - ktime_to_ns(poll_end));
3512 /* The total time the vCPU was "halted", including polling time. */
3513 halt_ns = ktime_to_ns(cur) - ktime_to_ns(start);
3516 * Note, halt-polling is considered successful so long as the vCPU was
3517 * never actually scheduled out, i.e. even if the wake event arrived
3518 * after of the halt-polling loop itself, but before the full wait.
3521 update_halt_poll_stats(vcpu, start, poll_end, !waited);
3523 if (halt_poll_allowed) {
3524 if (!vcpu_valid_wakeup(vcpu)) {
3525 shrink_halt_poll_ns(vcpu);
3526 } else if (vcpu->kvm->max_halt_poll_ns) {
3527 if (halt_ns <= vcpu->halt_poll_ns)
3529 /* we had a long block, shrink polling */
3530 else if (vcpu->halt_poll_ns &&
3531 halt_ns > vcpu->kvm->max_halt_poll_ns)
3532 shrink_halt_poll_ns(vcpu);
3533 /* we had a short halt and our poll time is too small */
3534 else if (vcpu->halt_poll_ns < vcpu->kvm->max_halt_poll_ns &&
3535 halt_ns < vcpu->kvm->max_halt_poll_ns)
3536 grow_halt_poll_ns(vcpu);
3538 vcpu->halt_poll_ns = 0;
3542 trace_kvm_vcpu_wakeup(halt_ns, waited, vcpu_valid_wakeup(vcpu));
3544 EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
3546 bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
3548 if (__kvm_vcpu_wake_up(vcpu)) {
3549 WRITE_ONCE(vcpu->ready, true);
3550 ++vcpu->stat.generic.halt_wakeup;
3556 EXPORT_SYMBOL_GPL(kvm_vcpu_wake_up);
3560 * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
3562 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
3566 if (kvm_vcpu_wake_up(vcpu))
3571 * The only state change done outside the vcpu mutex is IN_GUEST_MODE
3572 * to EXITING_GUEST_MODE. Therefore the moderately expensive "should
3573 * kick" check does not need atomic operations if kvm_vcpu_kick is used
3574 * within the vCPU thread itself.
3576 if (vcpu == __this_cpu_read(kvm_running_vcpu)) {
3577 if (vcpu->mode == IN_GUEST_MODE)
3578 WRITE_ONCE(vcpu->mode, EXITING_GUEST_MODE);
3583 * Note, the vCPU could get migrated to a different pCPU at any point
3584 * after kvm_arch_vcpu_should_kick(), which could result in sending an
3585 * IPI to the previous pCPU. But, that's ok because the purpose of the
3586 * IPI is to force the vCPU to leave IN_GUEST_MODE, and migrating the
3587 * vCPU also requires it to leave IN_GUEST_MODE.
3589 if (kvm_arch_vcpu_should_kick(vcpu)) {
3590 cpu = READ_ONCE(vcpu->cpu);
3591 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
3592 smp_send_reschedule(cpu);
3597 EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
3598 #endif /* !CONFIG_S390 */
3600 int kvm_vcpu_yield_to(struct kvm_vcpu *target)
3603 struct task_struct *task = NULL;
3607 pid = rcu_dereference(target->pid);
3609 task = get_pid_task(pid, PIDTYPE_PID);
3613 ret = yield_to(task, 1);
3614 put_task_struct(task);
3618 EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
3621 * Helper that checks whether a VCPU is eligible for directed yield.
3622 * Most eligible candidate to yield is decided by following heuristics:
3624 * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
3625 * (preempted lock holder), indicated by @in_spin_loop.
3626 * Set at the beginning and cleared at the end of interception/PLE handler.
3628 * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
3629 * chance last time (mostly it has become eligible now since we have probably
3630 * yielded to lockholder in last iteration. This is done by toggling
3631 * @dy_eligible each time a VCPU checked for eligibility.)
3633 * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
3634 * to preempted lock-holder could result in wrong VCPU selection and CPU
3635 * burning. Giving priority for a potential lock-holder increases lock
3638 * Since algorithm is based on heuristics, accessing another VCPU data without
3639 * locking does not harm. It may result in trying to yield to same VCPU, fail
3640 * and continue with next VCPU and so on.
3642 static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
3644 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
3647 eligible = !vcpu->spin_loop.in_spin_loop ||
3648 vcpu->spin_loop.dy_eligible;
3650 if (vcpu->spin_loop.in_spin_loop)
3651 kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
3660 * Unlike kvm_arch_vcpu_runnable, this function is called outside
3661 * a vcpu_load/vcpu_put pair. However, for most architectures
3662 * kvm_arch_vcpu_runnable does not require vcpu_load.
3664 bool __weak kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
3666 return kvm_arch_vcpu_runnable(vcpu);
3669 static bool vcpu_dy_runnable(struct kvm_vcpu *vcpu)
3671 if (kvm_arch_dy_runnable(vcpu))
3674 #ifdef CONFIG_KVM_ASYNC_PF
3675 if (!list_empty_careful(&vcpu->async_pf.done))
3682 bool __weak kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu)
3687 void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
3689 struct kvm *kvm = me->kvm;
3690 struct kvm_vcpu *vcpu;
3691 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
3697 kvm_vcpu_set_in_spin_loop(me, true);
3699 * We boost the priority of a VCPU that is runnable but not
3700 * currently running, because it got preempted by something
3701 * else and called schedule in __vcpu_run. Hopefully that
3702 * VCPU is holding the lock that we need and will release it.
3703 * We approximate round-robin by starting at the last boosted VCPU.
3705 for (pass = 0; pass < 2 && !yielded && try; pass++) {
3706 kvm_for_each_vcpu(i, vcpu, kvm) {
3707 if (!pass && i <= last_boosted_vcpu) {
3708 i = last_boosted_vcpu;
3710 } else if (pass && i > last_boosted_vcpu)
3712 if (!READ_ONCE(vcpu->ready))
3716 if (kvm_vcpu_is_blocking(vcpu) && !vcpu_dy_runnable(vcpu))
3718 if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode &&
3719 !kvm_arch_dy_has_pending_interrupt(vcpu) &&
3720 !kvm_arch_vcpu_in_kernel(vcpu))
3722 if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
3725 yielded = kvm_vcpu_yield_to(vcpu);
3727 kvm->last_boosted_vcpu = i;
3729 } else if (yielded < 0) {
3736 kvm_vcpu_set_in_spin_loop(me, false);
3738 /* Ensure vcpu is not eligible during next spinloop */
3739 kvm_vcpu_set_dy_eligible(me, false);
3741 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
3743 static bool kvm_page_in_dirty_ring(struct kvm *kvm, unsigned long pgoff)
3745 #ifdef CONFIG_HAVE_KVM_DIRTY_RING
3746 return (pgoff >= KVM_DIRTY_LOG_PAGE_OFFSET) &&
3747 (pgoff < KVM_DIRTY_LOG_PAGE_OFFSET +
3748 kvm->dirty_ring_size / PAGE_SIZE);
3754 static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
3756 struct kvm_vcpu *vcpu = vmf->vma->vm_file->private_data;
3759 if (vmf->pgoff == 0)
3760 page = virt_to_page(vcpu->run);
3762 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
3763 page = virt_to_page(vcpu->arch.pio_data);
3765 #ifdef CONFIG_KVM_MMIO
3766 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
3767 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
3769 else if (kvm_page_in_dirty_ring(vcpu->kvm, vmf->pgoff))
3770 page = kvm_dirty_ring_get_page(
3772 vmf->pgoff - KVM_DIRTY_LOG_PAGE_OFFSET);
3774 return kvm_arch_vcpu_fault(vcpu, vmf);
3780 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
3781 .fault = kvm_vcpu_fault,
3784 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
3786 struct kvm_vcpu *vcpu = file->private_data;
3787 unsigned long pages = vma_pages(vma);
3789 if ((kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff) ||
3790 kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff + pages - 1)) &&
3791 ((vma->vm_flags & VM_EXEC) || !(vma->vm_flags & VM_SHARED)))
3794 vma->vm_ops = &kvm_vcpu_vm_ops;
3798 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
3800 struct kvm_vcpu *vcpu = filp->private_data;
3802 kvm_put_kvm(vcpu->kvm);
3806 static const struct file_operations kvm_vcpu_fops = {
3807 .release = kvm_vcpu_release,
3808 .unlocked_ioctl = kvm_vcpu_ioctl,
3809 .mmap = kvm_vcpu_mmap,
3810 .llseek = noop_llseek,
3811 KVM_COMPAT(kvm_vcpu_compat_ioctl),
3815 * Allocates an inode for the vcpu.
3817 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
3819 char name[8 + 1 + ITOA_MAX_LEN + 1];
3821 snprintf(name, sizeof(name), "kvm-vcpu:%d", vcpu->vcpu_id);
3822 return anon_inode_getfd(name, &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC);
3825 #ifdef __KVM_HAVE_ARCH_VCPU_DEBUGFS
3826 static int vcpu_get_pid(void *data, u64 *val)
3828 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
3829 *val = pid_nr(rcu_access_pointer(vcpu->pid));
3833 DEFINE_SIMPLE_ATTRIBUTE(vcpu_get_pid_fops, vcpu_get_pid, NULL, "%llu\n");
3835 static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
3837 struct dentry *debugfs_dentry;
3838 char dir_name[ITOA_MAX_LEN * 2];
3840 if (!debugfs_initialized())
3843 snprintf(dir_name, sizeof(dir_name), "vcpu%d", vcpu->vcpu_id);
3844 debugfs_dentry = debugfs_create_dir(dir_name,
3845 vcpu->kvm->debugfs_dentry);
3846 debugfs_create_file("pid", 0444, debugfs_dentry, vcpu,
3847 &vcpu_get_pid_fops);
3849 kvm_arch_create_vcpu_debugfs(vcpu, debugfs_dentry);
3854 * Creates some virtual cpus. Good luck creating more than one.
3856 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
3859 struct kvm_vcpu *vcpu;
3862 if (id >= KVM_MAX_VCPU_IDS)
3865 mutex_lock(&kvm->lock);
3866 if (kvm->created_vcpus >= kvm->max_vcpus) {
3867 mutex_unlock(&kvm->lock);
3871 r = kvm_arch_vcpu_precreate(kvm, id);
3873 mutex_unlock(&kvm->lock);
3877 kvm->created_vcpus++;
3878 mutex_unlock(&kvm->lock);
3880 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
3883 goto vcpu_decrement;
3886 BUILD_BUG_ON(sizeof(struct kvm_run) > PAGE_SIZE);
3887 page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
3892 vcpu->run = page_address(page);
3894 kvm_vcpu_init(vcpu, kvm, id);
3896 r = kvm_arch_vcpu_create(vcpu);
3898 goto vcpu_free_run_page;
3900 if (kvm->dirty_ring_size) {
3901 r = kvm_dirty_ring_alloc(&vcpu->dirty_ring,
3902 id, kvm->dirty_ring_size);
3904 goto arch_vcpu_destroy;
3907 mutex_lock(&kvm->lock);
3908 if (kvm_get_vcpu_by_id(kvm, id)) {
3910 goto unlock_vcpu_destroy;
3913 vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
3914 r = xa_insert(&kvm->vcpu_array, vcpu->vcpu_idx, vcpu, GFP_KERNEL_ACCOUNT);
3915 BUG_ON(r == -EBUSY);
3917 goto unlock_vcpu_destroy;
3919 /* Fill the stats id string for the vcpu */
3920 snprintf(vcpu->stats_id, sizeof(vcpu->stats_id), "kvm-%d/vcpu-%d",
3921 task_pid_nr(current), id);
3923 /* Now it's all set up, let userspace reach it */
3925 r = create_vcpu_fd(vcpu);
3927 xa_erase(&kvm->vcpu_array, vcpu->vcpu_idx);
3928 kvm_put_kvm_no_destroy(kvm);
3929 goto unlock_vcpu_destroy;
3933 * Pairs with smp_rmb() in kvm_get_vcpu. Store the vcpu
3934 * pointer before kvm->online_vcpu's incremented value.
3937 atomic_inc(&kvm->online_vcpus);
3939 mutex_unlock(&kvm->lock);
3940 kvm_arch_vcpu_postcreate(vcpu);
3941 kvm_create_vcpu_debugfs(vcpu);
3944 unlock_vcpu_destroy:
3945 mutex_unlock(&kvm->lock);
3946 kvm_dirty_ring_free(&vcpu->dirty_ring);
3948 kvm_arch_vcpu_destroy(vcpu);
3950 free_page((unsigned long)vcpu->run);
3952 kmem_cache_free(kvm_vcpu_cache, vcpu);
3954 mutex_lock(&kvm->lock);
3955 kvm->created_vcpus--;
3956 mutex_unlock(&kvm->lock);
3960 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
3963 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3964 vcpu->sigset_active = 1;
3965 vcpu->sigset = *sigset;
3967 vcpu->sigset_active = 0;
3971 static ssize_t kvm_vcpu_stats_read(struct file *file, char __user *user_buffer,
3972 size_t size, loff_t *offset)
3974 struct kvm_vcpu *vcpu = file->private_data;
3976 return kvm_stats_read(vcpu->stats_id, &kvm_vcpu_stats_header,
3977 &kvm_vcpu_stats_desc[0], &vcpu->stat,
3978 sizeof(vcpu->stat), user_buffer, size, offset);
3981 static const struct file_operations kvm_vcpu_stats_fops = {
3982 .read = kvm_vcpu_stats_read,
3983 .llseek = noop_llseek,
3986 static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu)
3990 char name[15 + ITOA_MAX_LEN + 1];
3992 snprintf(name, sizeof(name), "kvm-vcpu-stats:%d", vcpu->vcpu_id);
3994 fd = get_unused_fd_flags(O_CLOEXEC);
3998 file = anon_inode_getfile(name, &kvm_vcpu_stats_fops, vcpu, O_RDONLY);
4001 return PTR_ERR(file);
4003 file->f_mode |= FMODE_PREAD;
4004 fd_install(fd, file);
4009 static long kvm_vcpu_ioctl(struct file *filp,
4010 unsigned int ioctl, unsigned long arg)
4012 struct kvm_vcpu *vcpu = filp->private_data;
4013 void __user *argp = (void __user *)arg;
4015 struct kvm_fpu *fpu = NULL;
4016 struct kvm_sregs *kvm_sregs = NULL;
4018 if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead)
4021 if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
4025 * Some architectures have vcpu ioctls that are asynchronous to vcpu
4026 * execution; mutex_lock() would break them.
4028 r = kvm_arch_vcpu_async_ioctl(filp, ioctl, arg);
4029 if (r != -ENOIOCTLCMD)
4032 if (mutex_lock_killable(&vcpu->mutex))
4040 oldpid = rcu_access_pointer(vcpu->pid);
4041 if (unlikely(oldpid != task_pid(current))) {
4042 /* The thread running this VCPU changed. */
4045 r = kvm_arch_vcpu_run_pid_change(vcpu);
4049 newpid = get_task_pid(current, PIDTYPE_PID);
4050 rcu_assign_pointer(vcpu->pid, newpid);
4055 r = kvm_arch_vcpu_ioctl_run(vcpu);
4056 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
4059 case KVM_GET_REGS: {
4060 struct kvm_regs *kvm_regs;
4063 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL_ACCOUNT);
4066 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
4070 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
4077 case KVM_SET_REGS: {
4078 struct kvm_regs *kvm_regs;
4080 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
4081 if (IS_ERR(kvm_regs)) {
4082 r = PTR_ERR(kvm_regs);
4085 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
4089 case KVM_GET_SREGS: {
4090 kvm_sregs = kzalloc(sizeof(struct kvm_sregs),
4091 GFP_KERNEL_ACCOUNT);
4095 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
4099 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
4104 case KVM_SET_SREGS: {
4105 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
4106 if (IS_ERR(kvm_sregs)) {
4107 r = PTR_ERR(kvm_sregs);
4111 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
4114 case KVM_GET_MP_STATE: {
4115 struct kvm_mp_state mp_state;
4117 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
4121 if (copy_to_user(argp, &mp_state, sizeof(mp_state)))
4126 case KVM_SET_MP_STATE: {
4127 struct kvm_mp_state mp_state;
4130 if (copy_from_user(&mp_state, argp, sizeof(mp_state)))
4132 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
4135 case KVM_TRANSLATE: {
4136 struct kvm_translation tr;
4139 if (copy_from_user(&tr, argp, sizeof(tr)))
4141 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
4145 if (copy_to_user(argp, &tr, sizeof(tr)))
4150 case KVM_SET_GUEST_DEBUG: {
4151 struct kvm_guest_debug dbg;
4154 if (copy_from_user(&dbg, argp, sizeof(dbg)))
4156 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
4159 case KVM_SET_SIGNAL_MASK: {
4160 struct kvm_signal_mask __user *sigmask_arg = argp;
4161 struct kvm_signal_mask kvm_sigmask;
4162 sigset_t sigset, *p;
4167 if (copy_from_user(&kvm_sigmask, argp,
4168 sizeof(kvm_sigmask)))
4171 if (kvm_sigmask.len != sizeof(sigset))
4174 if (copy_from_user(&sigset, sigmask_arg->sigset,
4179 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
4183 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL_ACCOUNT);
4187 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
4191 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
4197 fpu = memdup_user(argp, sizeof(*fpu));
4203 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
4206 case KVM_GET_STATS_FD: {
4207 r = kvm_vcpu_ioctl_get_stats_fd(vcpu);
4211 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
4214 mutex_unlock(&vcpu->mutex);
4220 #ifdef CONFIG_KVM_COMPAT
4221 static long kvm_vcpu_compat_ioctl(struct file *filp,
4222 unsigned int ioctl, unsigned long arg)
4224 struct kvm_vcpu *vcpu = filp->private_data;
4225 void __user *argp = compat_ptr(arg);
4228 if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead)
4232 case KVM_SET_SIGNAL_MASK: {
4233 struct kvm_signal_mask __user *sigmask_arg = argp;
4234 struct kvm_signal_mask kvm_sigmask;
4239 if (copy_from_user(&kvm_sigmask, argp,
4240 sizeof(kvm_sigmask)))
4243 if (kvm_sigmask.len != sizeof(compat_sigset_t))
4246 if (get_compat_sigset(&sigset,
4247 (compat_sigset_t __user *)sigmask_arg->sigset))
4249 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
4251 r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
4255 r = kvm_vcpu_ioctl(filp, ioctl, arg);
4263 static int kvm_device_mmap(struct file *filp, struct vm_area_struct *vma)
4265 struct kvm_device *dev = filp->private_data;
4268 return dev->ops->mmap(dev, vma);
4273 static int kvm_device_ioctl_attr(struct kvm_device *dev,
4274 int (*accessor)(struct kvm_device *dev,
4275 struct kvm_device_attr *attr),
4278 struct kvm_device_attr attr;
4283 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
4286 return accessor(dev, &attr);
4289 static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
4292 struct kvm_device *dev = filp->private_data;
4294 if (dev->kvm->mm != current->mm || dev->kvm->vm_dead)
4298 case KVM_SET_DEVICE_ATTR:
4299 return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
4300 case KVM_GET_DEVICE_ATTR:
4301 return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
4302 case KVM_HAS_DEVICE_ATTR:
4303 return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
4305 if (dev->ops->ioctl)
4306 return dev->ops->ioctl(dev, ioctl, arg);
4312 static int kvm_device_release(struct inode *inode, struct file *filp)
4314 struct kvm_device *dev = filp->private_data;
4315 struct kvm *kvm = dev->kvm;
4317 if (dev->ops->release) {
4318 mutex_lock(&kvm->lock);
4319 list_del(&dev->vm_node);
4320 dev->ops->release(dev);
4321 mutex_unlock(&kvm->lock);
4328 static const struct file_operations kvm_device_fops = {
4329 .unlocked_ioctl = kvm_device_ioctl,
4330 .release = kvm_device_release,
4331 KVM_COMPAT(kvm_device_ioctl),
4332 .mmap = kvm_device_mmap,
4335 struct kvm_device *kvm_device_from_filp(struct file *filp)
4337 if (filp->f_op != &kvm_device_fops)
4340 return filp->private_data;
4343 static const struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
4344 #ifdef CONFIG_KVM_MPIC
4345 [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops,
4346 [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops,
4350 int kvm_register_device_ops(const struct kvm_device_ops *ops, u32 type)
4352 if (type >= ARRAY_SIZE(kvm_device_ops_table))
4355 if (kvm_device_ops_table[type] != NULL)
4358 kvm_device_ops_table[type] = ops;
4362 void kvm_unregister_device_ops(u32 type)
4364 if (kvm_device_ops_table[type] != NULL)
4365 kvm_device_ops_table[type] = NULL;
4368 static int kvm_ioctl_create_device(struct kvm *kvm,
4369 struct kvm_create_device *cd)
4371 const struct kvm_device_ops *ops = NULL;
4372 struct kvm_device *dev;
4373 bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
4377 if (cd->type >= ARRAY_SIZE(kvm_device_ops_table))
4380 type = array_index_nospec(cd->type, ARRAY_SIZE(kvm_device_ops_table));
4381 ops = kvm_device_ops_table[type];
4388 dev = kzalloc(sizeof(*dev), GFP_KERNEL_ACCOUNT);
4395 mutex_lock(&kvm->lock);
4396 ret = ops->create(dev, type);
4398 mutex_unlock(&kvm->lock);
4402 list_add(&dev->vm_node, &kvm->devices);
4403 mutex_unlock(&kvm->lock);
4409 ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC);
4411 kvm_put_kvm_no_destroy(kvm);
4412 mutex_lock(&kvm->lock);
4413 list_del(&dev->vm_node);
4416 mutex_unlock(&kvm->lock);
4426 static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
4429 case KVM_CAP_USER_MEMORY:
4430 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
4431 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
4432 case KVM_CAP_INTERNAL_ERROR_DATA:
4433 #ifdef CONFIG_HAVE_KVM_MSI
4434 case KVM_CAP_SIGNAL_MSI:
4436 #ifdef CONFIG_HAVE_KVM_IRQFD
4438 case KVM_CAP_IRQFD_RESAMPLE:
4440 case KVM_CAP_IOEVENTFD_ANY_LENGTH:
4441 case KVM_CAP_CHECK_EXTENSION_VM:
4442 case KVM_CAP_ENABLE_CAP_VM:
4443 case KVM_CAP_HALT_POLL:
4445 #ifdef CONFIG_KVM_MMIO
4446 case KVM_CAP_COALESCED_MMIO:
4447 return KVM_COALESCED_MMIO_PAGE_OFFSET;
4448 case KVM_CAP_COALESCED_PIO:
4451 #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
4452 case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2:
4453 return KVM_DIRTY_LOG_MANUAL_CAPS;
4455 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
4456 case KVM_CAP_IRQ_ROUTING:
4457 return KVM_MAX_IRQ_ROUTES;
4459 #if KVM_ADDRESS_SPACE_NUM > 1
4460 case KVM_CAP_MULTI_ADDRESS_SPACE:
4461 return KVM_ADDRESS_SPACE_NUM;
4463 case KVM_CAP_NR_MEMSLOTS:
4464 return KVM_USER_MEM_SLOTS;
4465 case KVM_CAP_DIRTY_LOG_RING:
4466 #ifdef CONFIG_HAVE_KVM_DIRTY_RING
4467 return KVM_DIRTY_RING_MAX_ENTRIES * sizeof(struct kvm_dirty_gfn);
4471 case KVM_CAP_BINARY_STATS_FD:
4472 case KVM_CAP_SYSTEM_EVENT_DATA:
4477 return kvm_vm_ioctl_check_extension(kvm, arg);
4480 static int kvm_vm_ioctl_enable_dirty_log_ring(struct kvm *kvm, u32 size)
4484 if (!KVM_DIRTY_LOG_PAGE_OFFSET)
4487 /* the size should be power of 2 */
4488 if (!size || (size & (size - 1)))
4491 /* Should be bigger to keep the reserved entries, or a page */
4492 if (size < kvm_dirty_ring_get_rsvd_entries() *
4493 sizeof(struct kvm_dirty_gfn) || size < PAGE_SIZE)
4496 if (size > KVM_DIRTY_RING_MAX_ENTRIES *
4497 sizeof(struct kvm_dirty_gfn))
4500 /* We only allow it to set once */
4501 if (kvm->dirty_ring_size)
4504 mutex_lock(&kvm->lock);
4506 if (kvm->created_vcpus) {
4507 /* We don't allow to change this value after vcpu created */
4510 kvm->dirty_ring_size = size;
4514 mutex_unlock(&kvm->lock);
4518 static int kvm_vm_ioctl_reset_dirty_pages(struct kvm *kvm)
4521 struct kvm_vcpu *vcpu;
4524 if (!kvm->dirty_ring_size)
4527 mutex_lock(&kvm->slots_lock);
4529 kvm_for_each_vcpu(i, vcpu, kvm)
4530 cleared += kvm_dirty_ring_reset(vcpu->kvm, &vcpu->dirty_ring);
4532 mutex_unlock(&kvm->slots_lock);
4535 kvm_flush_remote_tlbs(kvm);
4540 int __attribute__((weak)) kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4541 struct kvm_enable_cap *cap)
4546 static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,
4547 struct kvm_enable_cap *cap)
4550 #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
4551 case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2: {
4552 u64 allowed_options = KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE;
4554 if (cap->args[0] & KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE)
4555 allowed_options = KVM_DIRTY_LOG_MANUAL_CAPS;
4557 if (cap->flags || (cap->args[0] & ~allowed_options))
4559 kvm->manual_dirty_log_protect = cap->args[0];
4563 case KVM_CAP_HALT_POLL: {
4564 if (cap->flags || cap->args[0] != (unsigned int)cap->args[0])
4567 kvm->max_halt_poll_ns = cap->args[0];
4570 case KVM_CAP_DIRTY_LOG_RING:
4571 return kvm_vm_ioctl_enable_dirty_log_ring(kvm, cap->args[0]);
4573 return kvm_vm_ioctl_enable_cap(kvm, cap);
4577 static ssize_t kvm_vm_stats_read(struct file *file, char __user *user_buffer,
4578 size_t size, loff_t *offset)
4580 struct kvm *kvm = file->private_data;
4582 return kvm_stats_read(kvm->stats_id, &kvm_vm_stats_header,
4583 &kvm_vm_stats_desc[0], &kvm->stat,
4584 sizeof(kvm->stat), user_buffer, size, offset);
4587 static const struct file_operations kvm_vm_stats_fops = {
4588 .read = kvm_vm_stats_read,
4589 .llseek = noop_llseek,
4592 static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm)
4597 fd = get_unused_fd_flags(O_CLOEXEC);
4601 file = anon_inode_getfile("kvm-vm-stats",
4602 &kvm_vm_stats_fops, kvm, O_RDONLY);
4605 return PTR_ERR(file);
4607 file->f_mode |= FMODE_PREAD;
4608 fd_install(fd, file);
4613 static long kvm_vm_ioctl(struct file *filp,
4614 unsigned int ioctl, unsigned long arg)
4616 struct kvm *kvm = filp->private_data;
4617 void __user *argp = (void __user *)arg;
4620 if (kvm->mm != current->mm || kvm->vm_dead)
4623 case KVM_CREATE_VCPU:
4624 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
4626 case KVM_ENABLE_CAP: {
4627 struct kvm_enable_cap cap;
4630 if (copy_from_user(&cap, argp, sizeof(cap)))
4632 r = kvm_vm_ioctl_enable_cap_generic(kvm, &cap);
4635 case KVM_SET_USER_MEMORY_REGION: {
4636 struct kvm_userspace_memory_region kvm_userspace_mem;
4639 if (copy_from_user(&kvm_userspace_mem, argp,
4640 sizeof(kvm_userspace_mem)))
4643 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
4646 case KVM_GET_DIRTY_LOG: {
4647 struct kvm_dirty_log log;
4650 if (copy_from_user(&log, argp, sizeof(log)))
4652 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
4655 #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
4656 case KVM_CLEAR_DIRTY_LOG: {
4657 struct kvm_clear_dirty_log log;
4660 if (copy_from_user(&log, argp, sizeof(log)))
4662 r = kvm_vm_ioctl_clear_dirty_log(kvm, &log);
4666 #ifdef CONFIG_KVM_MMIO
4667 case KVM_REGISTER_COALESCED_MMIO: {
4668 struct kvm_coalesced_mmio_zone zone;
4671 if (copy_from_user(&zone, argp, sizeof(zone)))
4673 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
4676 case KVM_UNREGISTER_COALESCED_MMIO: {
4677 struct kvm_coalesced_mmio_zone zone;
4680 if (copy_from_user(&zone, argp, sizeof(zone)))
4682 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
4687 struct kvm_irqfd data;
4690 if (copy_from_user(&data, argp, sizeof(data)))
4692 r = kvm_irqfd(kvm, &data);
4695 case KVM_IOEVENTFD: {
4696 struct kvm_ioeventfd data;
4699 if (copy_from_user(&data, argp, sizeof(data)))
4701 r = kvm_ioeventfd(kvm, &data);
4704 #ifdef CONFIG_HAVE_KVM_MSI
4705 case KVM_SIGNAL_MSI: {
4709 if (copy_from_user(&msi, argp, sizeof(msi)))
4711 r = kvm_send_userspace_msi(kvm, &msi);
4715 #ifdef __KVM_HAVE_IRQ_LINE
4716 case KVM_IRQ_LINE_STATUS:
4717 case KVM_IRQ_LINE: {
4718 struct kvm_irq_level irq_event;
4721 if (copy_from_user(&irq_event, argp, sizeof(irq_event)))
4724 r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
4725 ioctl == KVM_IRQ_LINE_STATUS);
4730 if (ioctl == KVM_IRQ_LINE_STATUS) {
4731 if (copy_to_user(argp, &irq_event, sizeof(irq_event)))
4739 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
4740 case KVM_SET_GSI_ROUTING: {
4741 struct kvm_irq_routing routing;
4742 struct kvm_irq_routing __user *urouting;
4743 struct kvm_irq_routing_entry *entries = NULL;
4746 if (copy_from_user(&routing, argp, sizeof(routing)))
4749 if (!kvm_arch_can_set_irq_routing(kvm))
4751 if (routing.nr > KVM_MAX_IRQ_ROUTES)
4757 entries = vmemdup_user(urouting->entries,
4758 array_size(sizeof(*entries),
4760 if (IS_ERR(entries)) {
4761 r = PTR_ERR(entries);
4765 r = kvm_set_irq_routing(kvm, entries, routing.nr,
4770 #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
4771 case KVM_CREATE_DEVICE: {
4772 struct kvm_create_device cd;
4775 if (copy_from_user(&cd, argp, sizeof(cd)))
4778 r = kvm_ioctl_create_device(kvm, &cd);
4783 if (copy_to_user(argp, &cd, sizeof(cd)))
4789 case KVM_CHECK_EXTENSION:
4790 r = kvm_vm_ioctl_check_extension_generic(kvm, arg);
4792 case KVM_RESET_DIRTY_RINGS:
4793 r = kvm_vm_ioctl_reset_dirty_pages(kvm);
4795 case KVM_GET_STATS_FD:
4796 r = kvm_vm_ioctl_get_stats_fd(kvm);
4799 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
4805 #ifdef CONFIG_KVM_COMPAT
4806 struct compat_kvm_dirty_log {
4810 compat_uptr_t dirty_bitmap; /* one bit per page */
4815 struct compat_kvm_clear_dirty_log {
4820 compat_uptr_t dirty_bitmap; /* one bit per page */
4825 static long kvm_vm_compat_ioctl(struct file *filp,
4826 unsigned int ioctl, unsigned long arg)
4828 struct kvm *kvm = filp->private_data;
4831 if (kvm->mm != current->mm || kvm->vm_dead)
4834 #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
4835 case KVM_CLEAR_DIRTY_LOG: {
4836 struct compat_kvm_clear_dirty_log compat_log;
4837 struct kvm_clear_dirty_log log;
4839 if (copy_from_user(&compat_log, (void __user *)arg,
4840 sizeof(compat_log)))
4842 log.slot = compat_log.slot;
4843 log.num_pages = compat_log.num_pages;
4844 log.first_page = compat_log.first_page;
4845 log.padding2 = compat_log.padding2;
4846 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
4848 r = kvm_vm_ioctl_clear_dirty_log(kvm, &log);
4852 case KVM_GET_DIRTY_LOG: {
4853 struct compat_kvm_dirty_log compat_log;
4854 struct kvm_dirty_log log;
4856 if (copy_from_user(&compat_log, (void __user *)arg,
4857 sizeof(compat_log)))
4859 log.slot = compat_log.slot;
4860 log.padding1 = compat_log.padding1;
4861 log.padding2 = compat_log.padding2;
4862 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
4864 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
4868 r = kvm_vm_ioctl(filp, ioctl, arg);
4874 static const struct file_operations kvm_vm_fops = {
4875 .release = kvm_vm_release,
4876 .unlocked_ioctl = kvm_vm_ioctl,
4877 .llseek = noop_llseek,
4878 KVM_COMPAT(kvm_vm_compat_ioctl),
4881 bool file_is_kvm(struct file *file)
4883 return file && file->f_op == &kvm_vm_fops;
4885 EXPORT_SYMBOL_GPL(file_is_kvm);
4887 static int kvm_dev_ioctl_create_vm(unsigned long type)
4893 kvm = kvm_create_vm(type);
4895 return PTR_ERR(kvm);
4896 #ifdef CONFIG_KVM_MMIO
4897 r = kvm_coalesced_mmio_init(kvm);
4901 r = get_unused_fd_flags(O_CLOEXEC);
4905 snprintf(kvm->stats_id, sizeof(kvm->stats_id),
4906 "kvm-%d", task_pid_nr(current));
4908 file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
4916 * Don't call kvm_put_kvm anymore at this point; file->f_op is
4917 * already set, with ->release() being kvm_vm_release(). In error
4918 * cases it will be called by the final fput(file) and will take
4919 * care of doing kvm_put_kvm(kvm).
4921 if (kvm_create_vm_debugfs(kvm, r) < 0) {
4926 kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
4928 fd_install(r, file);
4936 static long kvm_dev_ioctl(struct file *filp,
4937 unsigned int ioctl, unsigned long arg)
4942 case KVM_GET_API_VERSION:
4945 r = KVM_API_VERSION;
4948 r = kvm_dev_ioctl_create_vm(arg);
4950 case KVM_CHECK_EXTENSION:
4951 r = kvm_vm_ioctl_check_extension_generic(NULL, arg);
4953 case KVM_GET_VCPU_MMAP_SIZE:
4956 r = PAGE_SIZE; /* struct kvm_run */
4958 r += PAGE_SIZE; /* pio data page */
4960 #ifdef CONFIG_KVM_MMIO
4961 r += PAGE_SIZE; /* coalesced mmio ring page */
4964 case KVM_TRACE_ENABLE:
4965 case KVM_TRACE_PAUSE:
4966 case KVM_TRACE_DISABLE:
4970 return kvm_arch_dev_ioctl(filp, ioctl, arg);
4976 static struct file_operations kvm_chardev_ops = {
4977 .unlocked_ioctl = kvm_dev_ioctl,
4978 .llseek = noop_llseek,
4979 KVM_COMPAT(kvm_dev_ioctl),
4982 static struct miscdevice kvm_dev = {
4988 static void hardware_enable_nolock(void *junk)
4990 int cpu = raw_smp_processor_id();
4993 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
4996 cpumask_set_cpu(cpu, cpus_hardware_enabled);
4998 r = kvm_arch_hardware_enable();
5001 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
5002 atomic_inc(&hardware_enable_failed);
5003 pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu);
5007 static int kvm_starting_cpu(unsigned int cpu)
5009 raw_spin_lock(&kvm_count_lock);
5010 if (kvm_usage_count)
5011 hardware_enable_nolock(NULL);
5012 raw_spin_unlock(&kvm_count_lock);
5016 static void hardware_disable_nolock(void *junk)
5018 int cpu = raw_smp_processor_id();
5020 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
5022 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
5023 kvm_arch_hardware_disable();
5026 static int kvm_dying_cpu(unsigned int cpu)
5028 raw_spin_lock(&kvm_count_lock);
5029 if (kvm_usage_count)
5030 hardware_disable_nolock(NULL);
5031 raw_spin_unlock(&kvm_count_lock);
5035 static void hardware_disable_all_nolock(void)
5037 BUG_ON(!kvm_usage_count);
5040 if (!kvm_usage_count)
5041 on_each_cpu(hardware_disable_nolock, NULL, 1);
5044 static void hardware_disable_all(void)
5046 raw_spin_lock(&kvm_count_lock);
5047 hardware_disable_all_nolock();
5048 raw_spin_unlock(&kvm_count_lock);
5051 static int hardware_enable_all(void)
5055 raw_spin_lock(&kvm_count_lock);
5058 if (kvm_usage_count == 1) {
5059 atomic_set(&hardware_enable_failed, 0);
5060 on_each_cpu(hardware_enable_nolock, NULL, 1);
5062 if (atomic_read(&hardware_enable_failed)) {
5063 hardware_disable_all_nolock();
5068 raw_spin_unlock(&kvm_count_lock);
5073 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
5077 * Some (well, at least mine) BIOSes hang on reboot if
5080 * And Intel TXT required VMX off for all cpu when system shutdown.
5082 pr_info("kvm: exiting hardware virtualization\n");
5083 kvm_rebooting = true;
5084 on_each_cpu(hardware_disable_nolock, NULL, 1);
5088 static struct notifier_block kvm_reboot_notifier = {
5089 .notifier_call = kvm_reboot,
5093 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
5097 for (i = 0; i < bus->dev_count; i++) {
5098 struct kvm_io_device *pos = bus->range[i].dev;
5100 kvm_iodevice_destructor(pos);
5105 static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1,
5106 const struct kvm_io_range *r2)
5108 gpa_t addr1 = r1->addr;
5109 gpa_t addr2 = r2->addr;
5114 /* If r2->len == 0, match the exact address. If r2->len != 0,
5115 * accept any overlapping write. Any order is acceptable for
5116 * overlapping ranges, because kvm_io_bus_get_first_dev ensures
5117 * we process all of them.
5130 static int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
5132 return kvm_io_bus_cmp(p1, p2);
5135 static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
5136 gpa_t addr, int len)
5138 struct kvm_io_range *range, key;
5141 key = (struct kvm_io_range) {
5146 range = bsearch(&key, bus->range, bus->dev_count,
5147 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
5151 off = range - bus->range;
5153 while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0)
5159 static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
5160 struct kvm_io_range *range, const void *val)
5164 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
5168 while (idx < bus->dev_count &&
5169 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
5170 if (!kvm_iodevice_write(vcpu, bus->range[idx].dev, range->addr,
5179 /* kvm_io_bus_write - called under kvm->slots_lock */
5180 int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
5181 int len, const void *val)
5183 struct kvm_io_bus *bus;
5184 struct kvm_io_range range;
5187 range = (struct kvm_io_range) {
5192 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
5195 r = __kvm_io_bus_write(vcpu, bus, &range, val);
5196 return r < 0 ? r : 0;
5198 EXPORT_SYMBOL_GPL(kvm_io_bus_write);
5200 /* kvm_io_bus_write_cookie - called under kvm->slots_lock */
5201 int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
5202 gpa_t addr, int len, const void *val, long cookie)
5204 struct kvm_io_bus *bus;
5205 struct kvm_io_range range;
5207 range = (struct kvm_io_range) {
5212 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
5216 /* First try the device referenced by cookie. */
5217 if ((cookie >= 0) && (cookie < bus->dev_count) &&
5218 (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
5219 if (!kvm_iodevice_write(vcpu, bus->range[cookie].dev, addr, len,
5224 * cookie contained garbage; fall back to search and return the
5225 * correct cookie value.
5227 return __kvm_io_bus_write(vcpu, bus, &range, val);
5230 static int __kvm_io_bus_read(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
5231 struct kvm_io_range *range, void *val)
5235 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
5239 while (idx < bus->dev_count &&
5240 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
5241 if (!kvm_iodevice_read(vcpu, bus->range[idx].dev, range->addr,
5250 /* kvm_io_bus_read - called under kvm->slots_lock */
5251 int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
5254 struct kvm_io_bus *bus;
5255 struct kvm_io_range range;
5258 range = (struct kvm_io_range) {
5263 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
5266 r = __kvm_io_bus_read(vcpu, bus, &range, val);
5267 return r < 0 ? r : 0;
5270 /* Caller must hold slots_lock. */
5271 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
5272 int len, struct kvm_io_device *dev)
5275 struct kvm_io_bus *new_bus, *bus;
5276 struct kvm_io_range range;
5278 bus = kvm_get_bus(kvm, bus_idx);
5282 /* exclude ioeventfd which is limited by maximum fd */
5283 if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
5286 new_bus = kmalloc(struct_size(bus, range, bus->dev_count + 1),
5287 GFP_KERNEL_ACCOUNT);
5291 range = (struct kvm_io_range) {
5297 for (i = 0; i < bus->dev_count; i++)
5298 if (kvm_io_bus_cmp(&bus->range[i], &range) > 0)
5301 memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
5302 new_bus->dev_count++;
5303 new_bus->range[i] = range;
5304 memcpy(new_bus->range + i + 1, bus->range + i,
5305 (bus->dev_count - i) * sizeof(struct kvm_io_range));
5306 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
5307 synchronize_srcu_expedited(&kvm->srcu);
5313 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
5314 struct kvm_io_device *dev)
5317 struct kvm_io_bus *new_bus, *bus;
5319 lockdep_assert_held(&kvm->slots_lock);
5321 bus = kvm_get_bus(kvm, bus_idx);
5325 for (i = 0; i < bus->dev_count; i++) {
5326 if (bus->range[i].dev == dev) {
5331 if (i == bus->dev_count)
5334 new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1),
5335 GFP_KERNEL_ACCOUNT);
5337 memcpy(new_bus, bus, struct_size(bus, range, i));
5338 new_bus->dev_count--;
5339 memcpy(new_bus->range + i, bus->range + i + 1,
5340 flex_array_size(new_bus, range, new_bus->dev_count - i));
5343 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
5344 synchronize_srcu_expedited(&kvm->srcu);
5346 /* Destroy the old bus _after_ installing the (null) bus. */
5348 pr_err("kvm: failed to shrink bus, removing it completely\n");
5349 for (j = 0; j < bus->dev_count; j++) {
5352 kvm_iodevice_destructor(bus->range[j].dev);
5357 return new_bus ? 0 : -ENOMEM;
5360 struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
5363 struct kvm_io_bus *bus;
5364 int dev_idx, srcu_idx;
5365 struct kvm_io_device *iodev = NULL;
5367 srcu_idx = srcu_read_lock(&kvm->srcu);
5369 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
5373 dev_idx = kvm_io_bus_get_first_dev(bus, addr, 1);
5377 iodev = bus->range[dev_idx].dev;
5380 srcu_read_unlock(&kvm->srcu, srcu_idx);
5384 EXPORT_SYMBOL_GPL(kvm_io_bus_get_dev);
5386 static int kvm_debugfs_open(struct inode *inode, struct file *file,
5387 int (*get)(void *, u64 *), int (*set)(void *, u64),
5390 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
5394 * The debugfs files are a reference to the kvm struct which
5395 * is still valid when kvm_destroy_vm is called. kvm_get_kvm_safe
5396 * avoids the race between open and the removal of the debugfs directory.
5398 if (!kvm_get_kvm_safe(stat_data->kvm))
5401 if (simple_attr_open(inode, file, get,
5402 kvm_stats_debugfs_mode(stat_data->desc) & 0222
5405 kvm_put_kvm(stat_data->kvm);
5412 static int kvm_debugfs_release(struct inode *inode, struct file *file)
5414 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
5417 simple_attr_release(inode, file);
5418 kvm_put_kvm(stat_data->kvm);
5423 static int kvm_get_stat_per_vm(struct kvm *kvm, size_t offset, u64 *val)
5425 *val = *(u64 *)((void *)(&kvm->stat) + offset);
5430 static int kvm_clear_stat_per_vm(struct kvm *kvm, size_t offset)
5432 *(u64 *)((void *)(&kvm->stat) + offset) = 0;
5437 static int kvm_get_stat_per_vcpu(struct kvm *kvm, size_t offset, u64 *val)
5440 struct kvm_vcpu *vcpu;
5444 kvm_for_each_vcpu(i, vcpu, kvm)
5445 *val += *(u64 *)((void *)(&vcpu->stat) + offset);
5450 static int kvm_clear_stat_per_vcpu(struct kvm *kvm, size_t offset)
5453 struct kvm_vcpu *vcpu;
5455 kvm_for_each_vcpu(i, vcpu, kvm)
5456 *(u64 *)((void *)(&vcpu->stat) + offset) = 0;
5461 static int kvm_stat_data_get(void *data, u64 *val)
5464 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
5466 switch (stat_data->kind) {
5468 r = kvm_get_stat_per_vm(stat_data->kvm,
5469 stat_data->desc->desc.offset, val);
5472 r = kvm_get_stat_per_vcpu(stat_data->kvm,
5473 stat_data->desc->desc.offset, val);
5480 static int kvm_stat_data_clear(void *data, u64 val)
5483 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
5488 switch (stat_data->kind) {
5490 r = kvm_clear_stat_per_vm(stat_data->kvm,
5491 stat_data->desc->desc.offset);
5494 r = kvm_clear_stat_per_vcpu(stat_data->kvm,
5495 stat_data->desc->desc.offset);
5502 static int kvm_stat_data_open(struct inode *inode, struct file *file)
5504 __simple_attr_check_format("%llu\n", 0ull);
5505 return kvm_debugfs_open(inode, file, kvm_stat_data_get,
5506 kvm_stat_data_clear, "%llu\n");
5509 static const struct file_operations stat_fops_per_vm = {
5510 .owner = THIS_MODULE,
5511 .open = kvm_stat_data_open,
5512 .release = kvm_debugfs_release,
5513 .read = simple_attr_read,
5514 .write = simple_attr_write,
5515 .llseek = no_llseek,
5518 static int vm_stat_get(void *_offset, u64 *val)
5520 unsigned offset = (long)_offset;
5525 mutex_lock(&kvm_lock);
5526 list_for_each_entry(kvm, &vm_list, vm_list) {
5527 kvm_get_stat_per_vm(kvm, offset, &tmp_val);
5530 mutex_unlock(&kvm_lock);
5534 static int vm_stat_clear(void *_offset, u64 val)
5536 unsigned offset = (long)_offset;
5542 mutex_lock(&kvm_lock);
5543 list_for_each_entry(kvm, &vm_list, vm_list) {
5544 kvm_clear_stat_per_vm(kvm, offset);
5546 mutex_unlock(&kvm_lock);
5551 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n");
5552 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_readonly_fops, vm_stat_get, NULL, "%llu\n");
5554 static int vcpu_stat_get(void *_offset, u64 *val)
5556 unsigned offset = (long)_offset;
5561 mutex_lock(&kvm_lock);
5562 list_for_each_entry(kvm, &vm_list, vm_list) {
5563 kvm_get_stat_per_vcpu(kvm, offset, &tmp_val);
5566 mutex_unlock(&kvm_lock);
5570 static int vcpu_stat_clear(void *_offset, u64 val)
5572 unsigned offset = (long)_offset;
5578 mutex_lock(&kvm_lock);
5579 list_for_each_entry(kvm, &vm_list, vm_list) {
5580 kvm_clear_stat_per_vcpu(kvm, offset);
5582 mutex_unlock(&kvm_lock);
5587 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear,
5589 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_readonly_fops, vcpu_stat_get, NULL, "%llu\n");
5591 static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
5593 struct kobj_uevent_env *env;
5594 unsigned long long created, active;
5596 if (!kvm_dev.this_device || !kvm)
5599 mutex_lock(&kvm_lock);
5600 if (type == KVM_EVENT_CREATE_VM) {
5601 kvm_createvm_count++;
5603 } else if (type == KVM_EVENT_DESTROY_VM) {
5606 created = kvm_createvm_count;
5607 active = kvm_active_vms;
5608 mutex_unlock(&kvm_lock);
5610 env = kzalloc(sizeof(*env), GFP_KERNEL_ACCOUNT);
5614 add_uevent_var(env, "CREATED=%llu", created);
5615 add_uevent_var(env, "COUNT=%llu", active);
5617 if (type == KVM_EVENT_CREATE_VM) {
5618 add_uevent_var(env, "EVENT=create");
5619 kvm->userspace_pid = task_pid_nr(current);
5620 } else if (type == KVM_EVENT_DESTROY_VM) {
5621 add_uevent_var(env, "EVENT=destroy");
5623 add_uevent_var(env, "PID=%d", kvm->userspace_pid);
5625 if (!IS_ERR(kvm->debugfs_dentry)) {
5626 char *tmp, *p = kmalloc(PATH_MAX, GFP_KERNEL_ACCOUNT);
5629 tmp = dentry_path_raw(kvm->debugfs_dentry, p, PATH_MAX);
5631 add_uevent_var(env, "STATS_PATH=%s", tmp);
5635 /* no need for checks, since we are adding at most only 5 keys */
5636 env->envp[env->envp_idx++] = NULL;
5637 kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp);
5641 static void kvm_init_debug(void)
5643 const struct file_operations *fops;
5644 const struct _kvm_stats_desc *pdesc;
5647 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
5649 for (i = 0; i < kvm_vm_stats_header.num_desc; ++i) {
5650 pdesc = &kvm_vm_stats_desc[i];
5651 if (kvm_stats_debugfs_mode(pdesc) & 0222)
5652 fops = &vm_stat_fops;
5654 fops = &vm_stat_readonly_fops;
5655 debugfs_create_file(pdesc->name, kvm_stats_debugfs_mode(pdesc),
5657 (void *)(long)pdesc->desc.offset, fops);
5660 for (i = 0; i < kvm_vcpu_stats_header.num_desc; ++i) {
5661 pdesc = &kvm_vcpu_stats_desc[i];
5662 if (kvm_stats_debugfs_mode(pdesc) & 0222)
5663 fops = &vcpu_stat_fops;
5665 fops = &vcpu_stat_readonly_fops;
5666 debugfs_create_file(pdesc->name, kvm_stats_debugfs_mode(pdesc),
5668 (void *)(long)pdesc->desc.offset, fops);
5672 static int kvm_suspend(void)
5674 if (kvm_usage_count)
5675 hardware_disable_nolock(NULL);
5679 static void kvm_resume(void)
5681 if (kvm_usage_count) {
5682 lockdep_assert_not_held(&kvm_count_lock);
5683 hardware_enable_nolock(NULL);
5687 static struct syscore_ops kvm_syscore_ops = {
5688 .suspend = kvm_suspend,
5689 .resume = kvm_resume,
5693 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
5695 return container_of(pn, struct kvm_vcpu, preempt_notifier);
5698 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
5700 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
5702 WRITE_ONCE(vcpu->preempted, false);
5703 WRITE_ONCE(vcpu->ready, false);
5705 __this_cpu_write(kvm_running_vcpu, vcpu);
5706 kvm_arch_sched_in(vcpu, cpu);
5707 kvm_arch_vcpu_load(vcpu, cpu);
5710 static void kvm_sched_out(struct preempt_notifier *pn,
5711 struct task_struct *next)
5713 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
5715 if (current->on_rq) {
5716 WRITE_ONCE(vcpu->preempted, true);
5717 WRITE_ONCE(vcpu->ready, true);
5719 kvm_arch_vcpu_put(vcpu);
5720 __this_cpu_write(kvm_running_vcpu, NULL);
5724 * kvm_get_running_vcpu - get the vcpu running on the current CPU.
5726 * We can disable preemption locally around accessing the per-CPU variable,
5727 * and use the resolved vcpu pointer after enabling preemption again,
5728 * because even if the current thread is migrated to another CPU, reading
5729 * the per-CPU value later will give us the same value as we update the
5730 * per-CPU variable in the preempt notifier handlers.
5732 struct kvm_vcpu *kvm_get_running_vcpu(void)
5734 struct kvm_vcpu *vcpu;
5737 vcpu = __this_cpu_read(kvm_running_vcpu);
5742 EXPORT_SYMBOL_GPL(kvm_get_running_vcpu);
5745 * kvm_get_running_vcpus - get the per-CPU array of currently running vcpus.
5747 struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
5749 return &kvm_running_vcpu;
5752 #ifdef CONFIG_GUEST_PERF_EVENTS
5753 static unsigned int kvm_guest_state(void)
5755 struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
5758 if (!kvm_arch_pmi_in_guest(vcpu))
5761 state = PERF_GUEST_ACTIVE;
5762 if (!kvm_arch_vcpu_in_kernel(vcpu))
5763 state |= PERF_GUEST_USER;
5768 static unsigned long kvm_guest_get_ip(void)
5770 struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
5772 /* Retrieving the IP must be guarded by a call to kvm_guest_state(). */
5773 if (WARN_ON_ONCE(!kvm_arch_pmi_in_guest(vcpu)))
5776 return kvm_arch_vcpu_get_ip(vcpu);
5779 static struct perf_guest_info_callbacks kvm_guest_cbs = {
5780 .state = kvm_guest_state,
5781 .get_ip = kvm_guest_get_ip,
5782 .handle_intel_pt_intr = NULL,
5785 void kvm_register_perf_callbacks(unsigned int (*pt_intr_handler)(void))
5787 kvm_guest_cbs.handle_intel_pt_intr = pt_intr_handler;
5788 perf_register_guest_info_callbacks(&kvm_guest_cbs);
5790 void kvm_unregister_perf_callbacks(void)
5792 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
5796 struct kvm_cpu_compat_check {
5801 static void check_processor_compat(void *data)
5803 struct kvm_cpu_compat_check *c = data;
5805 *c->ret = kvm_arch_check_processor_compat(c->opaque);
5808 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
5809 struct module *module)
5811 struct kvm_cpu_compat_check c;
5815 r = kvm_arch_init(opaque);
5820 * kvm_arch_init makes sure there's at most one caller
5821 * for architectures that support multiple implementations,
5822 * like intel and amd on x86.
5823 * kvm_arch_init must be called before kvm_irqfd_init to avoid creating
5824 * conflicts in case kvm is already setup for another implementation.
5826 r = kvm_irqfd_init();
5830 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
5835 r = kvm_arch_hardware_setup(opaque);
5841 for_each_online_cpu(cpu) {
5842 smp_call_function_single(cpu, check_processor_compat, &c, 1);
5847 r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "kvm/cpu:starting",
5848 kvm_starting_cpu, kvm_dying_cpu);
5851 register_reboot_notifier(&kvm_reboot_notifier);
5853 /* A kmem cache lets us meet the alignment requirements of fx_save. */
5855 vcpu_align = __alignof__(struct kvm_vcpu);
5857 kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align,
5859 offsetof(struct kvm_vcpu, arch),
5860 offsetofend(struct kvm_vcpu, stats_id)
5861 - offsetof(struct kvm_vcpu, arch),
5863 if (!kvm_vcpu_cache) {
5868 for_each_possible_cpu(cpu) {
5869 if (!alloc_cpumask_var_node(&per_cpu(cpu_kick_mask, cpu),
5870 GFP_KERNEL, cpu_to_node(cpu))) {
5876 r = kvm_async_pf_init();
5880 kvm_chardev_ops.owner = module;
5882 r = misc_register(&kvm_dev);
5884 pr_err("kvm: misc device register failed\n");
5888 register_syscore_ops(&kvm_syscore_ops);
5890 kvm_preempt_ops.sched_in = kvm_sched_in;
5891 kvm_preempt_ops.sched_out = kvm_sched_out;
5895 r = kvm_vfio_ops_init();
5901 kvm_async_pf_deinit();
5903 for_each_possible_cpu(cpu)
5904 free_cpumask_var(per_cpu(cpu_kick_mask, cpu));
5906 kmem_cache_destroy(kvm_vcpu_cache);
5908 unregister_reboot_notifier(&kvm_reboot_notifier);
5909 cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
5911 kvm_arch_hardware_unsetup();
5913 free_cpumask_var(cpus_hardware_enabled);
5921 EXPORT_SYMBOL_GPL(kvm_init);
5927 debugfs_remove_recursive(kvm_debugfs_dir);
5928 misc_deregister(&kvm_dev);
5929 for_each_possible_cpu(cpu)
5930 free_cpumask_var(per_cpu(cpu_kick_mask, cpu));
5931 kmem_cache_destroy(kvm_vcpu_cache);
5932 kvm_async_pf_deinit();
5933 unregister_syscore_ops(&kvm_syscore_ops);
5934 unregister_reboot_notifier(&kvm_reboot_notifier);
5935 cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
5936 on_each_cpu(hardware_disable_nolock, NULL, 1);
5937 kvm_arch_hardware_unsetup();
5940 free_cpumask_var(cpus_hardware_enabled);
5941 kvm_vfio_ops_exit();
5943 EXPORT_SYMBOL_GPL(kvm_exit);
5945 struct kvm_vm_worker_thread_context {
5947 struct task_struct *parent;
5948 struct completion init_done;
5949 kvm_vm_thread_fn_t thread_fn;
5954 static int kvm_vm_worker_thread(void *context)
5957 * The init_context is allocated on the stack of the parent thread, so
5958 * we have to locally copy anything that is needed beyond initialization
5960 struct kvm_vm_worker_thread_context *init_context = context;
5961 struct task_struct *parent;
5962 struct kvm *kvm = init_context->kvm;
5963 kvm_vm_thread_fn_t thread_fn = init_context->thread_fn;
5964 uintptr_t data = init_context->data;
5967 err = kthread_park(current);
5968 /* kthread_park(current) is never supposed to return an error */
5973 err = cgroup_attach_task_all(init_context->parent, current);
5975 kvm_err("%s: cgroup_attach_task_all failed with err %d\n",
5980 set_user_nice(current, task_nice(init_context->parent));
5983 init_context->err = err;
5984 complete(&init_context->init_done);
5985 init_context = NULL;
5990 /* Wait to be woken up by the spawner before proceeding. */
5993 if (!kthread_should_stop())
5994 err = thread_fn(kvm, data);
5998 * Move kthread back to its original cgroup to prevent it lingering in
5999 * the cgroup of the VM process, after the latter finishes its
6002 * kthread_stop() waits on the 'exited' completion condition which is
6003 * set in exit_mm(), via mm_release(), in do_exit(). However, the
6004 * kthread is removed from the cgroup in the cgroup_exit() which is
6005 * called after the exit_mm(). This causes the kthread_stop() to return
6006 * before the kthread actually quits the cgroup.
6009 parent = rcu_dereference(current->real_parent);
6010 get_task_struct(parent);
6012 cgroup_attach_task_all(parent, current);
6013 put_task_struct(parent);
6018 int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn,
6019 uintptr_t data, const char *name,
6020 struct task_struct **thread_ptr)
6022 struct kvm_vm_worker_thread_context init_context = {};
6023 struct task_struct *thread;
6026 init_context.kvm = kvm;
6027 init_context.parent = current;
6028 init_context.thread_fn = thread_fn;
6029 init_context.data = data;
6030 init_completion(&init_context.init_done);
6032 thread = kthread_run(kvm_vm_worker_thread, &init_context,
6033 "%s-%d", name, task_pid_nr(current));
6035 return PTR_ERR(thread);
6037 /* kthread_run is never supposed to return NULL */
6038 WARN_ON(thread == NULL);
6040 wait_for_completion(&init_context.init_done);
6042 if (!init_context.err)
6043 *thread_ptr = thread;
6045 return init_context.err;