~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/virt/kvm/kvm_main.c

Version: ~ [ linux-5.13-rc1 ] ~ [ linux-5.12.2 ] ~ [ linux-5.11.19 ] ~ [ linux-5.10.35 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.117 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.190 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.232 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.268 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.268 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Kernel-based Virtual Machine driver for Linux
  3  *
  4  * This module enables machines with Intel VT-x extensions to run virtual
  5  * machines without emulation or binary translation.
  6  *
  7  * Copyright (C) 2006 Qumranet, Inc.
  8  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9  *
 10  * Authors:
 11  *   Avi Kivity   <avi@qumranet.com>
 12  *   Yaniv Kamay  <yaniv@qumranet.com>
 13  *
 14  * This work is licensed under the terms of the GNU GPL, version 2.  See
 15  * the COPYING file in the top-level directory.
 16  *
 17  */
 18 
 19 #include "iodev.h"
 20 
 21 #include <linux/kvm_host.h>
 22 #include <linux/kvm.h>
 23 #include <linux/module.h>
 24 #include <linux/errno.h>
 25 #include <linux/percpu.h>
 26 #include <linux/mm.h>
 27 #include <linux/miscdevice.h>
 28 #include <linux/vmalloc.h>
 29 #include <linux/reboot.h>
 30 #include <linux/debugfs.h>
 31 #include <linux/highmem.h>
 32 #include <linux/file.h>
 33 #include <linux/syscore_ops.h>
 34 #include <linux/cpu.h>
 35 #include <linux/sched.h>
 36 #include <linux/cpumask.h>
 37 #include <linux/smp.h>
 38 #include <linux/anon_inodes.h>
 39 #include <linux/profile.h>
 40 #include <linux/kvm_para.h>
 41 #include <linux/pagemap.h>
 42 #include <linux/mman.h>
 43 #include <linux/swap.h>
 44 #include <linux/bitops.h>
 45 #include <linux/spinlock.h>
 46 #include <linux/compat.h>
 47 #include <linux/srcu.h>
 48 #include <linux/hugetlb.h>
 49 #include <linux/slab.h>
 50 #include <linux/sort.h>
 51 #include <linux/bsearch.h>
 52 
 53 #include <asm/processor.h>
 54 #include <asm/io.h>
 55 #include <asm/ioctl.h>
 56 #include <asm/uaccess.h>
 57 #include <asm/pgtable.h>
 58 
 59 #include "coalesced_mmio.h"
 60 #include "async_pf.h"
 61 
 62 #define CREATE_TRACE_POINTS
 63 #include <trace/events/kvm.h>
 64 
 65 MODULE_AUTHOR("Qumranet");
 66 MODULE_LICENSE("GPL");
 67 
 68 /*
 69  * Ordering of locks:
 70  *
 71  *              kvm->lock --> kvm->slots_lock --> kvm->irq_lock
 72  */
 73 
 74 DEFINE_SPINLOCK(kvm_lock);
 75 static DEFINE_RAW_SPINLOCK(kvm_count_lock);
 76 LIST_HEAD(vm_list);
 77 
 78 static cpumask_var_t cpus_hardware_enabled;
 79 static int kvm_usage_count = 0;
 80 static atomic_t hardware_enable_failed;
 81 
 82 struct kmem_cache *kvm_vcpu_cache;
 83 EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
 84 
 85 static __read_mostly struct preempt_ops kvm_preempt_ops;
 86 
 87 struct dentry *kvm_debugfs_dir;
 88 
 89 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
 90                            unsigned long arg);
 91 #ifdef CONFIG_COMPAT
 92 static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
 93                                   unsigned long arg);
 94 #endif
 95 static int hardware_enable_all(void);
 96 static void hardware_disable_all(void);
 97 
 98 static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
 99 
100 static void kvm_release_pfn_dirty(pfn_t pfn);
101 static void mark_page_dirty_in_slot(struct kvm *kvm,
102                                     struct kvm_memory_slot *memslot, gfn_t gfn);
103 
104 bool kvm_rebooting;
105 EXPORT_SYMBOL_GPL(kvm_rebooting);
106 
107 static bool largepages_enabled = true;
108 
109 bool kvm_is_mmio_pfn(pfn_t pfn)
110 {
111         if (pfn_valid(pfn))
112                 return PageReserved(pfn_to_page(pfn));
113 
114         return true;
115 }
116 
117 /*
118  * Switches to specified vcpu, until a matching vcpu_put()
119  */
120 int vcpu_load(struct kvm_vcpu *vcpu)
121 {
122         int cpu;
123 
124         if (mutex_lock_killable(&vcpu->mutex))
125                 return -EINTR;
126         if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
127                 /* The thread running this VCPU changed. */
128                 struct pid *oldpid = vcpu->pid;
129                 struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
130                 rcu_assign_pointer(vcpu->pid, newpid);
131                 synchronize_rcu();
132                 put_pid(oldpid);
133         }
134         cpu = get_cpu();
135         preempt_notifier_register(&vcpu->preempt_notifier);
136         kvm_arch_vcpu_load(vcpu, cpu);
137         put_cpu();
138         return 0;
139 }
140 
141 void vcpu_put(struct kvm_vcpu *vcpu)
142 {
143         preempt_disable();
144         kvm_arch_vcpu_put(vcpu);
145         preempt_notifier_unregister(&vcpu->preempt_notifier);
146         preempt_enable();
147         mutex_unlock(&vcpu->mutex);
148 }
149 
150 static void ack_flush(void *_completed)
151 {
152 }
153 
154 static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
155 {
156         int i, cpu, me;
157         cpumask_var_t cpus;
158         bool called = true;
159         struct kvm_vcpu *vcpu;
160 
161         zalloc_cpumask_var(&cpus, GFP_ATOMIC);
162 
163         me = get_cpu();
164         kvm_for_each_vcpu(i, vcpu, kvm) {
165                 kvm_make_request(req, vcpu);
166                 cpu = vcpu->cpu;
167 
168                 /* Set ->requests bit before we read ->mode */
169                 smp_mb();
170 
171                 if (cpus != NULL && cpu != -1 && cpu != me &&
172                       kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
173                         cpumask_set_cpu(cpu, cpus);
174         }
175         if (unlikely(cpus == NULL))
176                 smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
177         else if (!cpumask_empty(cpus))
178                 smp_call_function_many(cpus, ack_flush, NULL, 1);
179         else
180                 called = false;
181         put_cpu();
182         free_cpumask_var(cpus);
183         return called;
184 }
185 
186 void kvm_flush_remote_tlbs(struct kvm *kvm)
187 {
188         long dirty_count = kvm->tlbs_dirty;
189 
190         smp_mb();
191         if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
192                 ++kvm->stat.remote_tlb_flush;
193         cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
194 }
195 EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
196 
197 void kvm_reload_remote_mmus(struct kvm *kvm)
198 {
199         make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
200 }
201 
202 void kvm_make_mclock_inprogress_request(struct kvm *kvm)
203 {
204         make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
205 }
206 
207 void kvm_make_scan_ioapic_request(struct kvm *kvm)
208 {
209         make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
210 }
211 
212 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
213 {
214         struct page *page;
215         int r;
216 
217         mutex_init(&vcpu->mutex);
218         vcpu->cpu = -1;
219         vcpu->kvm = kvm;
220         vcpu->vcpu_id = id;
221         vcpu->pid = NULL;
222         init_waitqueue_head(&vcpu->wq);
223         kvm_async_pf_vcpu_init(vcpu);
224 
225         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
226         if (!page) {
227                 r = -ENOMEM;
228                 goto fail;
229         }
230         vcpu->run = page_address(page);
231 
232         kvm_vcpu_set_in_spin_loop(vcpu, false);
233         kvm_vcpu_set_dy_eligible(vcpu, false);
234         vcpu->preempted = false;
235 
236         r = kvm_arch_vcpu_init(vcpu);
237         if (r < 0)
238                 goto fail_free_run;
239         return 0;
240 
241 fail_free_run:
242         free_page((unsigned long)vcpu->run);
243 fail:
244         return r;
245 }
246 EXPORT_SYMBOL_GPL(kvm_vcpu_init);
247 
248 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
249 {
250         put_pid(vcpu->pid);
251         kvm_arch_vcpu_uninit(vcpu);
252         free_page((unsigned long)vcpu->run);
253 }
254 EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
255 
256 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
257 static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
258 {
259         return container_of(mn, struct kvm, mmu_notifier);
260 }
261 
262 static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
263                                              struct mm_struct *mm,
264                                              unsigned long address)
265 {
266         struct kvm *kvm = mmu_notifier_to_kvm(mn);
267         int need_tlb_flush, idx;
268 
269         /*
270          * When ->invalidate_page runs, the linux pte has been zapped
271          * already but the page is still allocated until
272          * ->invalidate_page returns. So if we increase the sequence
273          * here the kvm page fault will notice if the spte can't be
274          * established because the page is going to be freed. If
275          * instead the kvm page fault establishes the spte before
276          * ->invalidate_page runs, kvm_unmap_hva will release it
277          * before returning.
278          *
279          * The sequence increase only need to be seen at spin_unlock
280          * time, and not at spin_lock time.
281          *
282          * Increasing the sequence after the spin_unlock would be
283          * unsafe because the kvm page fault could then establish the
284          * pte after kvm_unmap_hva returned, without noticing the page
285          * is going to be freed.
286          */
287         idx = srcu_read_lock(&kvm->srcu);
288         spin_lock(&kvm->mmu_lock);
289 
290         kvm->mmu_notifier_seq++;
291         need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
292         /* we've to flush the tlb before the pages can be freed */
293         if (need_tlb_flush)
294                 kvm_flush_remote_tlbs(kvm);
295 
296         spin_unlock(&kvm->mmu_lock);
297         srcu_read_unlock(&kvm->srcu, idx);
298 }
299 
300 static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
301                                         struct mm_struct *mm,
302                                         unsigned long address,
303                                         pte_t pte)
304 {
305         struct kvm *kvm = mmu_notifier_to_kvm(mn);
306         int idx;
307 
308         idx = srcu_read_lock(&kvm->srcu);
309         spin_lock(&kvm->mmu_lock);
310         kvm->mmu_notifier_seq++;
311         kvm_set_spte_hva(kvm, address, pte);
312         spin_unlock(&kvm->mmu_lock);
313         srcu_read_unlock(&kvm->srcu, idx);
314 }
315 
316 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
317                                                     struct mm_struct *mm,
318                                                     unsigned long start,
319                                                     unsigned long end)
320 {
321         struct kvm *kvm = mmu_notifier_to_kvm(mn);
322         int need_tlb_flush = 0, idx;
323 
324         idx = srcu_read_lock(&kvm->srcu);
325         spin_lock(&kvm->mmu_lock);
326         /*
327          * The count increase must become visible at unlock time as no
328          * spte can be established without taking the mmu_lock and
329          * count is also read inside the mmu_lock critical section.
330          */
331         kvm->mmu_notifier_count++;
332         need_tlb_flush = kvm_unmap_hva_range(kvm, start, end);
333         need_tlb_flush |= kvm->tlbs_dirty;
334         /* we've to flush the tlb before the pages can be freed */
335         if (need_tlb_flush)
336                 kvm_flush_remote_tlbs(kvm);
337 
338         spin_unlock(&kvm->mmu_lock);
339         srcu_read_unlock(&kvm->srcu, idx);
340 }
341 
342 static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
343                                                   struct mm_struct *mm,
344                                                   unsigned long start,
345                                                   unsigned long end)
346 {
347         struct kvm *kvm = mmu_notifier_to_kvm(mn);
348 
349         spin_lock(&kvm->mmu_lock);
350         /*
351          * This sequence increase will notify the kvm page fault that
352          * the page that is going to be mapped in the spte could have
353          * been freed.
354          */
355         kvm->mmu_notifier_seq++;
356         smp_wmb();
357         /*
358          * The above sequence increase must be visible before the
359          * below count decrease, which is ensured by the smp_wmb above
360          * in conjunction with the smp_rmb in mmu_notifier_retry().
361          */
362         kvm->mmu_notifier_count--;
363         spin_unlock(&kvm->mmu_lock);
364 
365         BUG_ON(kvm->mmu_notifier_count < 0);
366 }
367 
368 static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
369                                               struct mm_struct *mm,
370                                               unsigned long address)
371 {
372         struct kvm *kvm = mmu_notifier_to_kvm(mn);
373         int young, idx;
374 
375         idx = srcu_read_lock(&kvm->srcu);
376         spin_lock(&kvm->mmu_lock);
377 
378         young = kvm_age_hva(kvm, address);
379         if (young)
380                 kvm_flush_remote_tlbs(kvm);
381 
382         spin_unlock(&kvm->mmu_lock);
383         srcu_read_unlock(&kvm->srcu, idx);
384 
385         return young;
386 }
387 
388 static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
389                                        struct mm_struct *mm,
390                                        unsigned long address)
391 {
392         struct kvm *kvm = mmu_notifier_to_kvm(mn);
393         int young, idx;
394 
395         idx = srcu_read_lock(&kvm->srcu);
396         spin_lock(&kvm->mmu_lock);
397         young = kvm_test_age_hva(kvm, address);
398         spin_unlock(&kvm->mmu_lock);
399         srcu_read_unlock(&kvm->srcu, idx);
400 
401         return young;
402 }
403 
404 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
405                                      struct mm_struct *mm)
406 {
407         struct kvm *kvm = mmu_notifier_to_kvm(mn);
408         int idx;
409 
410         idx = srcu_read_lock(&kvm->srcu);
411         kvm_arch_flush_shadow_all(kvm);
412         srcu_read_unlock(&kvm->srcu, idx);
413 }
414 
415 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
416         .invalidate_page        = kvm_mmu_notifier_invalidate_page,
417         .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
418         .invalidate_range_end   = kvm_mmu_notifier_invalidate_range_end,
419         .clear_flush_young      = kvm_mmu_notifier_clear_flush_young,
420         .test_young             = kvm_mmu_notifier_test_young,
421         .change_pte             = kvm_mmu_notifier_change_pte,
422         .release                = kvm_mmu_notifier_release,
423 };
424 
425 static int kvm_init_mmu_notifier(struct kvm *kvm)
426 {
427         kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
428         return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
429 }
430 
431 #else  /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
432 
433 static int kvm_init_mmu_notifier(struct kvm *kvm)
434 {
435         return 0;
436 }
437 
438 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
439 
440 static void kvm_init_memslots_id(struct kvm *kvm)
441 {
442         int i;
443         struct kvm_memslots *slots = kvm->memslots;
444 
445         for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
446                 slots->id_to_index[i] = slots->memslots[i].id = i;
447 }
448 
449 static struct kvm *kvm_create_vm(unsigned long type)
450 {
451         int r, i;
452         struct kvm *kvm = kvm_arch_alloc_vm();
453 
454         if (!kvm)
455                 return ERR_PTR(-ENOMEM);
456 
457         spin_lock_init(&kvm->mmu_lock);
458         atomic_inc(&current->mm->mm_count);
459         kvm->mm = current->mm;
460         kvm_eventfd_init(kvm);
461         mutex_init(&kvm->lock);
462         mutex_init(&kvm->irq_lock);
463         mutex_init(&kvm->slots_lock);
464         atomic_set(&kvm->users_count, 1);
465         INIT_LIST_HEAD(&kvm->devices);
466 
467         r = kvm_arch_init_vm(kvm, type);
468         if (r)
469                 goto out_err_nodisable;
470 
471         r = hardware_enable_all();
472         if (r)
473                 goto out_err_nodisable;
474 
475 #ifdef CONFIG_HAVE_KVM_IRQCHIP
476         INIT_HLIST_HEAD(&kvm->mask_notifier_list);
477         INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
478 #endif
479 
480         BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
481 
482         r = -ENOMEM;
483         kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
484         if (!kvm->memslots)
485                 goto out_err_nosrcu;
486         kvm_init_memslots_id(kvm);
487         if (init_srcu_struct(&kvm->srcu))
488                 goto out_err_nosrcu;
489         for (i = 0; i < KVM_NR_BUSES; i++) {
490                 kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
491                                         GFP_KERNEL);
492                 if (!kvm->buses[i])
493                         goto out_err;
494         }
495 
496         r = kvm_init_mmu_notifier(kvm);
497         if (r)
498                 goto out_err;
499 
500         spin_lock(&kvm_lock);
501         list_add(&kvm->vm_list, &vm_list);
502         spin_unlock(&kvm_lock);
503 
504         return kvm;
505 
506 out_err:
507         cleanup_srcu_struct(&kvm->srcu);
508 out_err_nosrcu:
509         hardware_disable_all();
510 out_err_nodisable:
511         for (i = 0; i < KVM_NR_BUSES; i++)
512                 kfree(kvm->buses[i]);
513         kfree(kvm->memslots);
514         kvm_arch_free_vm(kvm);
515         mmdrop(current->mm);
516         return ERR_PTR(r);
517 }
518 
519 /*
520  * Avoid using vmalloc for a small buffer.
521  * Should not be used when the size is statically known.
522  */
523 void *kvm_kvzalloc(unsigned long size)
524 {
525         if (size > PAGE_SIZE)
526                 return vzalloc(size);
527         else
528                 return kzalloc(size, GFP_KERNEL);
529 }
530 
531 void kvm_kvfree(const void *addr)
532 {
533         if (is_vmalloc_addr(addr))
534                 vfree(addr);
535         else
536                 kfree(addr);
537 }
538 
539 static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
540 {
541         if (!memslot->dirty_bitmap)
542                 return;
543 
544         kvm_kvfree(memslot->dirty_bitmap);
545         memslot->dirty_bitmap = NULL;
546 }
547 
548 /*
549  * Free any memory in @free but not in @dont.
550  */
551 static void kvm_free_physmem_slot(struct kvm *kvm, struct kvm_memory_slot *free,
552                                   struct kvm_memory_slot *dont)
553 {
554         if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
555                 kvm_destroy_dirty_bitmap(free);
556 
557         kvm_arch_free_memslot(kvm, free, dont);
558 
559         free->npages = 0;
560 }
561 
562 static void kvm_free_physmem(struct kvm *kvm)
563 {
564         struct kvm_memslots *slots = kvm->memslots;
565         struct kvm_memory_slot *memslot;
566 
567         kvm_for_each_memslot(memslot, slots)
568                 kvm_free_physmem_slot(kvm, memslot, NULL);
569 
570         kfree(kvm->memslots);
571 }
572 
573 static void kvm_destroy_devices(struct kvm *kvm)
574 {
575         struct list_head *node, *tmp;
576 
577         list_for_each_safe(node, tmp, &kvm->devices) {
578                 struct kvm_device *dev =
579                         list_entry(node, struct kvm_device, vm_node);
580 
581                 list_del(node);
582                 dev->ops->destroy(dev);
583         }
584 }
585 
586 static void kvm_destroy_vm(struct kvm *kvm)
587 {
588         int i;
589         struct mm_struct *mm = kvm->mm;
590 
591         kvm_arch_sync_events(kvm);
592         spin_lock(&kvm_lock);
593         list_del(&kvm->vm_list);
594         spin_unlock(&kvm_lock);
595         kvm_free_irq_routing(kvm);
596         for (i = 0; i < KVM_NR_BUSES; i++)
597                 kvm_io_bus_destroy(kvm->buses[i]);
598         kvm_coalesced_mmio_free(kvm);
599 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
600         mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
601 #else
602         kvm_arch_flush_shadow_all(kvm);
603 #endif
604         kvm_arch_destroy_vm(kvm);
605         kvm_destroy_devices(kvm);
606         kvm_free_physmem(kvm);
607         cleanup_srcu_struct(&kvm->srcu);
608         kvm_arch_free_vm(kvm);
609         hardware_disable_all();
610         mmdrop(mm);
611 }
612 
613 void kvm_get_kvm(struct kvm *kvm)
614 {
615         atomic_inc(&kvm->users_count);
616 }
617 EXPORT_SYMBOL_GPL(kvm_get_kvm);
618 
619 void kvm_put_kvm(struct kvm *kvm)
620 {
621         if (atomic_dec_and_test(&kvm->users_count))
622                 kvm_destroy_vm(kvm);
623 }
624 EXPORT_SYMBOL_GPL(kvm_put_kvm);
625 
626 
627 static int kvm_vm_release(struct inode *inode, struct file *filp)
628 {
629         struct kvm *kvm = filp->private_data;
630 
631         kvm_irqfd_release(kvm);
632 
633         kvm_put_kvm(kvm);
634         return 0;
635 }
636 
637 /*
638  * Allocation size is twice as large as the actual dirty bitmap size.
639  * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
640  */
641 static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
642 {
643 #ifndef CONFIG_S390
644         unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
645 
646         memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes);
647         if (!memslot->dirty_bitmap)
648                 return -ENOMEM;
649 
650 #endif /* !CONFIG_S390 */
651         return 0;
652 }
653 
654 static int cmp_memslot(const void *slot1, const void *slot2)
655 {
656         struct kvm_memory_slot *s1, *s2;
657 
658         s1 = (struct kvm_memory_slot *)slot1;
659         s2 = (struct kvm_memory_slot *)slot2;
660 
661         if (s1->npages < s2->npages)
662                 return 1;
663         if (s1->npages > s2->npages)
664                 return -1;
665 
666         return 0;
667 }
668 
669 /*
670  * Sort the memslots base on its size, so the larger slots
671  * will get better fit.
672  */
673 static void sort_memslots(struct kvm_memslots *slots)
674 {
675         int i;
676 
677         sort(slots->memslots, KVM_MEM_SLOTS_NUM,
678               sizeof(struct kvm_memory_slot), cmp_memslot, NULL);
679 
680         for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
681                 slots->id_to_index[slots->memslots[i].id] = i;
682 }
683 
684 static void update_memslots(struct kvm_memslots *slots,
685                             struct kvm_memory_slot *new)
686 {
687         if (new) {
688                 int id = new->id;
689                 struct kvm_memory_slot *old = id_to_memslot(slots, id);
690                 unsigned long npages = old->npages;
691 
692                 *old = *new;
693                 if (new->npages != npages)
694                         sort_memslots(slots);
695         }
696 }
697 
698 static int check_memory_region_flags(struct kvm_userspace_memory_region *mem)
699 {
700         u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
701 
702 #ifdef KVM_CAP_READONLY_MEM
703         valid_flags |= KVM_MEM_READONLY;
704 #endif
705 
706         if (mem->flags & ~valid_flags)
707                 return -EINVAL;
708 
709         return 0;
710 }
711 
712 static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
713                 struct kvm_memslots *slots, struct kvm_memory_slot *new)
714 {
715         struct kvm_memslots *old_memslots = kvm->memslots;
716 
717         /*
718          * Set the low bit in the generation, which disables SPTE caching
719          * until the end of synchronize_srcu_expedited.
720          */
721         WARN_ON(old_memslots->generation & 1);
722         slots->generation = old_memslots->generation + 1;
723 
724         update_memslots(slots, new);
725         rcu_assign_pointer(kvm->memslots, slots);
726         synchronize_srcu_expedited(&kvm->srcu);
727 
728         /*
729          * Increment the new memslot generation a second time. This prevents
730          * vm exits that race with memslot updates from caching a memslot
731          * generation that will (potentially) be valid forever.
732          */
733         slots->generation++;
734 
735         kvm_arch_memslots_updated(kvm);
736 
737         return old_memslots;
738 }
739 
740 /*
741  * Allocate some memory and give it an address in the guest physical address
742  * space.
743  *
744  * Discontiguous memory is allowed, mostly for framebuffers.
745  *
746  * Must be called holding mmap_sem for write.
747  */
748 int __kvm_set_memory_region(struct kvm *kvm,
749                             struct kvm_userspace_memory_region *mem)
750 {
751         int r;
752         gfn_t base_gfn;
753         unsigned long npages;
754         struct kvm_memory_slot *slot;
755         struct kvm_memory_slot old, new;
756         struct kvm_memslots *slots = NULL, *old_memslots;
757         enum kvm_mr_change change;
758 
759         r = check_memory_region_flags(mem);
760         if (r)
761                 goto out;
762 
763         r = -EINVAL;
764         /* General sanity checks */
765         if (mem->memory_size & (PAGE_SIZE - 1))
766                 goto out;
767         if (mem->guest_phys_addr & (PAGE_SIZE - 1))
768                 goto out;
769         /* We can read the guest memory with __xxx_user() later on. */
770         if ((mem->slot < KVM_USER_MEM_SLOTS) &&
771             ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
772              !access_ok(VERIFY_WRITE,
773                         (void __user *)(unsigned long)mem->userspace_addr,
774                         mem->memory_size)))
775                 goto out;
776         if (mem->slot >= KVM_MEM_SLOTS_NUM)
777                 goto out;
778         if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
779                 goto out;
780 
781         slot = id_to_memslot(kvm->memslots, mem->slot);
782         base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
783         npages = mem->memory_size >> PAGE_SHIFT;
784 
785         r = -EINVAL;
786         if (npages > KVM_MEM_MAX_NR_PAGES)
787                 goto out;
788 
789         if (!npages)
790                 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
791 
792         new = old = *slot;
793 
794         new.id = mem->slot;
795         new.base_gfn = base_gfn;
796         new.npages = npages;
797         new.flags = mem->flags;
798 
799         r = -EINVAL;
800         if (npages) {
801                 if (!old.npages)
802                         change = KVM_MR_CREATE;
803                 else { /* Modify an existing slot. */
804                         if ((mem->userspace_addr != old.userspace_addr) ||
805                             (npages != old.npages) ||
806                             ((new.flags ^ old.flags) & KVM_MEM_READONLY))
807                                 goto out;
808 
809                         if (base_gfn != old.base_gfn)
810                                 change = KVM_MR_MOVE;
811                         else if (new.flags != old.flags)
812                                 change = KVM_MR_FLAGS_ONLY;
813                         else { /* Nothing to change. */
814                                 r = 0;
815                                 goto out;
816                         }
817                 }
818         } else if (old.npages) {
819                 change = KVM_MR_DELETE;
820         } else /* Modify a non-existent slot: disallowed. */
821                 goto out;
822 
823         if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
824                 /* Check for overlaps */
825                 r = -EEXIST;
826                 kvm_for_each_memslot(slot, kvm->memslots) {
827                         if ((slot->id >= KVM_USER_MEM_SLOTS) ||
828                             (slot->id == mem->slot))
829                                 continue;
830                         if (!((base_gfn + npages <= slot->base_gfn) ||
831                               (base_gfn >= slot->base_gfn + slot->npages)))
832                                 goto out;
833                 }
834         }
835 
836         /* Free page dirty bitmap if unneeded */
837         if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
838                 new.dirty_bitmap = NULL;
839 
840         r = -ENOMEM;
841         if (change == KVM_MR_CREATE) {
842                 new.userspace_addr = mem->userspace_addr;
843 
844                 if (kvm_arch_create_memslot(kvm, &new, npages))
845                         goto out_free;
846         }
847 
848         /* Allocate page dirty bitmap if needed */
849         if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
850                 if (kvm_create_dirty_bitmap(&new) < 0)
851                         goto out_free;
852         }
853 
854         if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) {
855                 r = -ENOMEM;
856                 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
857                                 GFP_KERNEL);
858                 if (!slots)
859                         goto out_free;
860                 slot = id_to_memslot(slots, mem->slot);
861                 slot->flags |= KVM_MEMSLOT_INVALID;
862 
863                 old_memslots = install_new_memslots(kvm, slots, NULL);
864 
865                 /* slot was deleted or moved, clear iommu mapping */
866                 kvm_iommu_unmap_pages(kvm, &old);
867                 /* From this point no new shadow pages pointing to a deleted,
868                  * or moved, memslot will be created.
869                  *
870                  * validation of sp->gfn happens in:
871                  *      - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
872                  *      - kvm_is_visible_gfn (mmu_check_roots)
873                  */
874                 kvm_arch_flush_shadow_memslot(kvm, slot);
875                 slots = old_memslots;
876         }
877 
878         r = kvm_arch_prepare_memory_region(kvm, &new, mem, change);
879         if (r)
880                 goto out_slots;
881 
882         r = -ENOMEM;
883         /*
884          * We can re-use the old_memslots from above, the only difference
885          * from the currently installed memslots is the invalid flag.  This
886          * will get overwritten by update_memslots anyway.
887          */
888         if (!slots) {
889                 slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
890                                 GFP_KERNEL);
891                 if (!slots)
892                         goto out_free;
893         }
894 
895         /* actual memory is freed via old in kvm_free_physmem_slot below */
896         if (change == KVM_MR_DELETE) {
897                 new.dirty_bitmap = NULL;
898                 memset(&new.arch, 0, sizeof(new.arch));
899         }
900 
901         old_memslots = install_new_memslots(kvm, slots, &new);
902 
903         kvm_arch_commit_memory_region(kvm, mem, &old, change);
904 
905         kvm_free_physmem_slot(kvm, &old, &new);
906         kfree(old_memslots);
907 
908         /*
909          * IOMMU mapping:  New slots need to be mapped.  Old slots need to be
910          * un-mapped and re-mapped if their base changes.  Since base change
911          * unmapping is handled above with slot deletion, mapping alone is
912          * needed here.  Anything else the iommu might care about for existing
913          * slots (size changes, userspace addr changes and read-only flag
914          * changes) is disallowed above, so any other attribute changes getting
915          * here can be skipped.
916          */
917         if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
918                 r = kvm_iommu_map_pages(kvm, &new);
919                 return r;
920         }
921 
922         return 0;
923 
924 out_slots:
925         kfree(slots);
926 out_free:
927         kvm_free_physmem_slot(kvm, &new, &old);
928 out:
929         return r;
930 }
931 EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
932 
933 int kvm_set_memory_region(struct kvm *kvm,
934                           struct kvm_userspace_memory_region *mem)
935 {
936         int r;
937 
938         mutex_lock(&kvm->slots_lock);
939         r = __kvm_set_memory_region(kvm, mem);
940         mutex_unlock(&kvm->slots_lock);
941         return r;
942 }
943 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
944 
945 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
946                                           struct kvm_userspace_memory_region *mem)
947 {
948         if (mem->slot >= KVM_USER_MEM_SLOTS)
949                 return -EINVAL;
950         return kvm_set_memory_region(kvm, mem);
951 }
952 
953 int kvm_get_dirty_log(struct kvm *kvm,
954                         struct kvm_dirty_log *log, int *is_dirty)
955 {
956         struct kvm_memory_slot *memslot;
957         int r, i;
958         unsigned long n;
959         unsigned long any = 0;
960 
961         r = -EINVAL;
962         if (log->slot >= KVM_USER_MEM_SLOTS)
963                 goto out;
964 
965         memslot = id_to_memslot(kvm->memslots, log->slot);
966         r = -ENOENT;
967         if (!memslot->dirty_bitmap)
968                 goto out;
969 
970         n = kvm_dirty_bitmap_bytes(memslot);
971 
972         for (i = 0; !any && i < n/sizeof(long); ++i)
973                 any = memslot->dirty_bitmap[i];
974 
975         r = -EFAULT;
976         if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
977                 goto out;
978 
979         if (any)
980                 *is_dirty = 1;
981 
982         r = 0;
983 out:
984         return r;
985 }
986 EXPORT_SYMBOL_GPL(kvm_get_dirty_log);
987 
988 bool kvm_largepages_enabled(void)
989 {
990         return largepages_enabled;
991 }
992 
993 void kvm_disable_largepages(void)
994 {
995         largepages_enabled = false;
996 }
997 EXPORT_SYMBOL_GPL(kvm_disable_largepages);
998 
999 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1000 {
1001         return __gfn_to_memslot(kvm_memslots(kvm), gfn);
1002 }
1003 EXPORT_SYMBOL_GPL(gfn_to_memslot);
1004 
1005 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1006 {
1007         struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
1008 
1009         if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS ||
1010               memslot->flags & KVM_MEMSLOT_INVALID)
1011                 return 0;
1012 
1013         return 1;
1014 }
1015 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1016 
1017 unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
1018 {
1019         struct vm_area_struct *vma;
1020         unsigned long addr, size;
1021 
1022         size = PAGE_SIZE;
1023 
1024         addr = gfn_to_hva(kvm, gfn);
1025         if (kvm_is_error_hva(addr))
1026                 return PAGE_SIZE;
1027 
1028         down_read(&current->mm->mmap_sem);
1029         vma = find_vma(current->mm, addr);
1030         if (!vma)
1031                 goto out;
1032 
1033         size = vma_kernel_pagesize(vma);
1034 
1035 out:
1036         up_read(&current->mm->mmap_sem);
1037 
1038         return size;
1039 }
1040 
1041 static bool memslot_is_readonly(struct kvm_memory_slot *slot)
1042 {
1043         return slot->flags & KVM_MEM_READONLY;
1044 }
1045 
1046 static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1047                                        gfn_t *nr_pages, bool write)
1048 {
1049         if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
1050                 return KVM_HVA_ERR_BAD;
1051 
1052         if (memslot_is_readonly(slot) && write)
1053                 return KVM_HVA_ERR_RO_BAD;
1054 
1055         if (nr_pages)
1056                 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1057 
1058         return __gfn_to_hva_memslot(slot, gfn);
1059 }
1060 
1061 static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1062                                      gfn_t *nr_pages)
1063 {
1064         return __gfn_to_hva_many(slot, gfn, nr_pages, true);
1065 }
1066 
1067 unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
1068                                         gfn_t gfn)
1069 {
1070         return gfn_to_hva_many(slot, gfn, NULL);
1071 }
1072 EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
1073 
1074 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1075 {
1076         return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
1077 }
1078 EXPORT_SYMBOL_GPL(gfn_to_hva);
1079 
1080 /*
1081  * If writable is set to false, the hva returned by this function is only
1082  * allowed to be read.
1083  */
1084 unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
1085 {
1086         struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
1087         unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
1088 
1089         if (!kvm_is_error_hva(hva) && writable)
1090                 *writable = !memslot_is_readonly(slot);
1091 
1092         return hva;
1093 }
1094 
1095 static int kvm_read_hva(void *data, void __user *hva, int len)
1096 {
1097         return __copy_from_user(data, hva, len);
1098 }
1099 
1100 static int kvm_read_hva_atomic(void *data, void __user *hva, int len)
1101 {
1102         return __copy_from_user_inatomic(data, hva, len);
1103 }
1104 
1105 static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
1106         unsigned long start, int write, struct page **page)
1107 {
1108         int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
1109 
1110         if (write)
1111                 flags |= FOLL_WRITE;
1112 
1113         return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
1114 }
1115 
1116 static inline int check_user_page_hwpoison(unsigned long addr)
1117 {
1118         int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
1119 
1120         rc = __get_user_pages(current, current->mm, addr, 1,
1121                               flags, NULL, NULL, NULL);
1122         return rc == -EHWPOISON;
1123 }
1124 
1125 /*
1126  * The atomic path to get the writable pfn which will be stored in @pfn,
1127  * true indicates success, otherwise false is returned.
1128  */
1129 static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
1130                             bool write_fault, bool *writable, pfn_t *pfn)
1131 {
1132         struct page *page[1];
1133         int npages;
1134 
1135         if (!(async || atomic))
1136                 return false;
1137 
1138         /*
1139          * Fast pin a writable pfn only if it is a write fault request
1140          * or the caller allows to map a writable pfn for a read fault
1141          * request.
1142          */
1143         if (!(write_fault || writable))
1144                 return false;
1145 
1146         npages = __get_user_pages_fast(addr, 1, 1, page);
1147         if (npages == 1) {
1148                 *pfn = page_to_pfn(page[0]);
1149 
1150                 if (writable)
1151                         *writable = true;
1152                 return true;
1153         }
1154 
1155         return false;
1156 }
1157 
1158 /*
1159  * The slow path to get the pfn of the specified host virtual address,
1160  * 1 indicates success, -errno is returned if error is detected.
1161  */
1162 static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
1163                            bool *writable, pfn_t *pfn)
1164 {
1165         struct page *page[1];
1166         int npages = 0;
1167 
1168         might_sleep();
1169 
1170         if (writable)
1171                 *writable = write_fault;
1172 
1173         if (async) {
1174                 down_read(&current->mm->mmap_sem);
1175                 npages = get_user_page_nowait(current, current->mm,
1176                                               addr, write_fault, page);
1177                 up_read(&current->mm->mmap_sem);
1178         } else
1179                 npages = get_user_pages_fast(addr, 1, write_fault,
1180                                              page);
1181         if (npages != 1)
1182                 return npages;
1183 
1184         /* map read fault as writable if possible */
1185         if (unlikely(!write_fault) && writable) {
1186                 struct page *wpage[1];
1187 
1188                 npages = __get_user_pages_fast(addr, 1, 1, wpage);
1189                 if (npages == 1) {
1190                         *writable = true;
1191                         put_page(page[0]);
1192                         page[0] = wpage[0];
1193                 }
1194 
1195                 npages = 1;
1196         }
1197         *pfn = page_to_pfn(page[0]);
1198         return npages;
1199 }
1200 
1201 static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
1202 {
1203         if (unlikely(!(vma->vm_flags & VM_READ)))
1204                 return false;
1205 
1206         if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
1207                 return false;
1208 
1209         return true;
1210 }
1211 
1212 /*
1213  * Pin guest page in memory and return its pfn.
1214  * @addr: host virtual address which maps memory to the guest
1215  * @atomic: whether this function can sleep
1216  * @async: whether this function need to wait IO complete if the
1217  *         host page is not in the memory
1218  * @write_fault: whether we should get a writable host page
1219  * @writable: whether it allows to map a writable host page for !@write_fault
1220  *
1221  * The function will map a writable host page for these two cases:
1222  * 1): @write_fault = true
1223  * 2): @write_fault = false && @writable, @writable will tell the caller
1224  *     whether the mapping is writable.
1225  */
1226 static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
1227                         bool write_fault, bool *writable)
1228 {
1229         struct vm_area_struct *vma;
1230         pfn_t pfn = 0;
1231         int npages;
1232 
1233         /* we can do it either atomically or asynchronously, not both */
1234         BUG_ON(atomic && async);
1235 
1236         if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn))
1237                 return pfn;
1238 
1239         if (atomic)
1240                 return KVM_PFN_ERR_FAULT;
1241 
1242         npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
1243         if (npages == 1)
1244                 return pfn;
1245 
1246         down_read(&current->mm->mmap_sem);
1247         if (npages == -EHWPOISON ||
1248               (!async && check_user_page_hwpoison(addr))) {
1249                 pfn = KVM_PFN_ERR_HWPOISON;
1250                 goto exit;
1251         }
1252 
1253         vma = find_vma_intersection(current->mm, addr, addr + 1);
1254 
1255         if (vma == NULL)
1256                 pfn = KVM_PFN_ERR_FAULT;
1257         else if ((vma->vm_flags & VM_PFNMAP)) {
1258                 pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
1259                         vma->vm_pgoff;
1260                 BUG_ON(!kvm_is_mmio_pfn(pfn));
1261         } else {
1262                 if (async && vma_is_valid(vma, write_fault))
1263                         *async = true;
1264                 pfn = KVM_PFN_ERR_FAULT;
1265         }
1266 exit:
1267         up_read(&current->mm->mmap_sem);
1268         return pfn;
1269 }
1270 
1271 static pfn_t
1272 __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic,
1273                      bool *async, bool write_fault, bool *writable)
1274 {
1275         unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
1276 
1277         if (addr == KVM_HVA_ERR_RO_BAD)
1278                 return KVM_PFN_ERR_RO_FAULT;
1279 
1280         if (kvm_is_error_hva(addr))
1281                 return KVM_PFN_NOSLOT;
1282 
1283         /* Do not map writable pfn in the readonly memslot. */
1284         if (writable && memslot_is_readonly(slot)) {
1285                 *writable = false;
1286                 writable = NULL;
1287         }
1288 
1289         return hva_to_pfn(addr, atomic, async, write_fault,
1290                           writable);
1291 }
1292 
1293 static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
1294                           bool write_fault, bool *writable)
1295 {
1296         struct kvm_memory_slot *slot;
1297 
1298         if (async)
1299                 *async = false;
1300 
1301         slot = gfn_to_memslot(kvm, gfn);
1302 
1303         return __gfn_to_pfn_memslot(slot, gfn, atomic, async, write_fault,
1304                                     writable);
1305 }
1306 
1307 pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
1308 {
1309         return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
1310 }
1311 EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
1312 
1313 pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
1314                        bool write_fault, bool *writable)
1315 {
1316         return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
1317 }
1318 EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
1319 
1320 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1321 {
1322         return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
1323 }
1324 EXPORT_SYMBOL_GPL(gfn_to_pfn);
1325 
1326 pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
1327                       bool *writable)
1328 {
1329         return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
1330 }
1331 EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1332 
1333 pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
1334 {
1335         return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
1336 }
1337 
1338 pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
1339 {
1340         return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
1341 }
1342 EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
1343 
1344 int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
1345                                                                   int nr_pages)
1346 {
1347         unsigned long addr;
1348         gfn_t entry;
1349 
1350         addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
1351         if (kvm_is_error_hva(addr))
1352                 return -1;
1353 
1354         if (entry < nr_pages)
1355                 return 0;
1356 
1357         return __get_user_pages_fast(addr, nr_pages, 1, pages);
1358 }
1359 EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
1360 
1361 static struct page *kvm_pfn_to_page(pfn_t pfn)
1362 {
1363         if (is_error_noslot_pfn(pfn))
1364                 return KVM_ERR_PTR_BAD_PAGE;
1365 
1366         if (kvm_is_mmio_pfn(pfn)) {
1367                 WARN_ON(1);
1368                 return KVM_ERR_PTR_BAD_PAGE;
1369         }
1370 
1371         return pfn_to_page(pfn);
1372 }
1373 
1374 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1375 {
1376         pfn_t pfn;
1377 
1378         pfn = gfn_to_pfn(kvm, gfn);
1379 
1380         return kvm_pfn_to_page(pfn);
1381 }
1382 
1383 EXPORT_SYMBOL_GPL(gfn_to_page);
1384 
1385 void kvm_release_page_clean(struct page *page)
1386 {
1387         WARN_ON(is_error_page(page));
1388 
1389         kvm_release_pfn_clean(page_to_pfn(page));
1390 }
1391 EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1392 
1393 void kvm_release_pfn_clean(pfn_t pfn)
1394 {
1395         if (!is_error_noslot_pfn(pfn) && !kvm_is_mmio_pfn(pfn))
1396                 put_page(pfn_to_page(pfn));
1397 }
1398 EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1399 
1400 void kvm_release_page_dirty(struct page *page)
1401 {
1402         WARN_ON(is_error_page(page));
1403 
1404         kvm_release_pfn_dirty(page_to_pfn(page));
1405 }
1406 EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1407 
1408 static void kvm_release_pfn_dirty(pfn_t pfn)
1409 {
1410         kvm_set_pfn_dirty(pfn);
1411         kvm_release_pfn_clean(pfn);
1412 }
1413 
1414 void kvm_set_pfn_dirty(pfn_t pfn)
1415 {
1416         if (!kvm_is_mmio_pfn(pfn)) {
1417                 struct page *page = pfn_to_page(pfn);
1418                 if (!PageReserved(page))
1419                         SetPageDirty(page);
1420         }
1421 }
1422 EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1423 
1424 void kvm_set_pfn_accessed(pfn_t pfn)
1425 {
1426         if (!kvm_is_mmio_pfn(pfn))
1427                 mark_page_accessed(pfn_to_page(pfn));
1428 }
1429 EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1430 
1431 void kvm_get_pfn(pfn_t pfn)
1432 {
1433         if (!kvm_is_mmio_pfn(pfn))
1434                 get_page(pfn_to_page(pfn));
1435 }
1436 EXPORT_SYMBOL_GPL(kvm_get_pfn);
1437 
1438 static int next_segment(unsigned long len, int offset)
1439 {
1440         if (len > PAGE_SIZE - offset)
1441                 return PAGE_SIZE - offset;
1442         else
1443                 return len;
1444 }
1445 
1446 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1447                         int len)
1448 {
1449         int r;
1450         unsigned long addr;
1451 
1452         addr = gfn_to_hva_prot(kvm, gfn, NULL);
1453         if (kvm_is_error_hva(addr))
1454                 return -EFAULT;
1455         r = kvm_read_hva(data, (void __user *)addr + offset, len);
1456         if (r)
1457                 return -EFAULT;
1458         return 0;
1459 }
1460 EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1461 
1462 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1463 {
1464         gfn_t gfn = gpa >> PAGE_SHIFT;
1465         int seg;
1466         int offset = offset_in_page(gpa);
1467         int ret;
1468 
1469         while ((seg = next_segment(len, offset)) != 0) {
1470                 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1471                 if (ret < 0)
1472                         return ret;
1473                 offset = 0;
1474                 len -= seg;
1475                 data += seg;
1476                 ++gfn;
1477         }
1478         return 0;
1479 }
1480 EXPORT_SYMBOL_GPL(kvm_read_guest);
1481 
1482 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1483                           unsigned long len)
1484 {
1485         int r;
1486         unsigned long addr;
1487         gfn_t gfn = gpa >> PAGE_SHIFT;
1488         int offset = offset_in_page(gpa);
1489 
1490         addr = gfn_to_hva_prot(kvm, gfn, NULL);
1491         if (kvm_is_error_hva(addr))
1492                 return -EFAULT;
1493         pagefault_disable();
1494         r = kvm_read_hva_atomic(data, (void __user *)addr + offset, len);
1495         pagefault_enable();
1496         if (r)
1497                 return -EFAULT;
1498         return 0;
1499 }
1500 EXPORT_SYMBOL(kvm_read_guest_atomic);
1501 
1502 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1503                          int offset, int len)
1504 {
1505         int r;
1506         unsigned long addr;
1507 
1508         addr = gfn_to_hva(kvm, gfn);
1509         if (kvm_is_error_hva(addr))
1510                 return -EFAULT;
1511         r = __copy_to_user((void __user *)addr + offset, data, len);
1512         if (r)
1513                 return -EFAULT;
1514         mark_page_dirty(kvm, gfn);
1515         return 0;
1516 }
1517 EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1518 
1519 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1520                     unsigned long len)
1521 {
1522         gfn_t gfn = gpa >> PAGE_SHIFT;
1523         int seg;
1524         int offset = offset_in_page(gpa);
1525         int ret;
1526 
1527         while ((seg = next_segment(len, offset)) != 0) {
1528                 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1529                 if (ret < 0)
1530                         return ret;
1531                 offset = 0;
1532                 len -= seg;
1533                 data += seg;
1534                 ++gfn;
1535         }
1536         return 0;
1537 }
1538 
1539 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1540                               gpa_t gpa, unsigned long len)
1541 {
1542         struct kvm_memslots *slots = kvm_memslots(kvm);
1543         int offset = offset_in_page(gpa);
1544         gfn_t start_gfn = gpa >> PAGE_SHIFT;
1545         gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
1546         gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
1547         gfn_t nr_pages_avail;
1548 
1549         ghc->gpa = gpa;
1550         ghc->generation = slots->generation;
1551         ghc->len = len;
1552         ghc->memslot = gfn_to_memslot(kvm, start_gfn);
1553         ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, NULL);
1554         if (!kvm_is_error_hva(ghc->hva) && nr_pages_needed <= 1) {
1555                 ghc->hva += offset;
1556         } else {
1557                 /*
1558                  * If the requested region crosses two memslots, we still
1559                  * verify that the entire region is valid here.
1560                  */
1561                 while (start_gfn <= end_gfn) {
1562                         ghc->memslot = gfn_to_memslot(kvm, start_gfn);
1563                         ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
1564                                                    &nr_pages_avail);
1565                         if (kvm_is_error_hva(ghc->hva))
1566                                 return -EFAULT;
1567                         start_gfn += nr_pages_avail;
1568                 }
1569                 /* Use the slow path for cross page reads and writes. */
1570                 ghc->memslot = NULL;
1571         }
1572         return 0;
1573 }
1574 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
1575 
1576 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1577                            void *data, unsigned long len)
1578 {
1579         struct kvm_memslots *slots = kvm_memslots(kvm);
1580         int r;
1581 
1582         BUG_ON(len > ghc->len);
1583 
1584         if (slots->generation != ghc->generation)
1585                 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
1586 
1587         if (unlikely(!ghc->memslot))
1588                 return kvm_write_guest(kvm, ghc->gpa, data, len);
1589 
1590         if (kvm_is_error_hva(ghc->hva))
1591                 return -EFAULT;
1592 
1593         r = __copy_to_user((void __user *)ghc->hva, data, len);
1594         if (r)
1595                 return -EFAULT;
1596         mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
1597 
1598         return 0;
1599 }
1600 EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
1601 
1602 int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
1603                            void *data, unsigned long len)
1604 {
1605         struct kvm_memslots *slots = kvm_memslots(kvm);
1606         int r;
1607 
1608         BUG_ON(len > ghc->len);
1609 
1610         if (slots->generation != ghc->generation)
1611                 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
1612 
1613         if (unlikely(!ghc->memslot))
1614                 return kvm_read_guest(kvm, ghc->gpa, data, len);
1615 
1616         if (kvm_is_error_hva(ghc->hva))
1617                 return -EFAULT;
1618 
1619         r = __copy_from_user(data, (void __user *)ghc->hva, len);
1620         if (r)
1621                 return -EFAULT;
1622 
1623         return 0;
1624 }
1625 EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
1626 
1627 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1628 {
1629         const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
1630 
1631         return kvm_write_guest_page(kvm, gfn, zero_page, offset, len);
1632 }
1633 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1634 
1635 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1636 {
1637         gfn_t gfn = gpa >> PAGE_SHIFT;
1638         int seg;
1639         int offset = offset_in_page(gpa);
1640         int ret;
1641 
1642         while ((seg = next_segment(len, offset)) != 0) {
1643                 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1644                 if (ret < 0)
1645                         return ret;
1646                 offset = 0;
1647                 len -= seg;
1648                 ++gfn;
1649         }
1650         return 0;
1651 }
1652 EXPORT_SYMBOL_GPL(kvm_clear_guest);
1653 
1654 static void mark_page_dirty_in_slot(struct kvm *kvm,
1655                                     struct kvm_memory_slot *memslot,
1656                                     gfn_t gfn)
1657 {
1658         if (memslot && memslot->dirty_bitmap) {
1659                 unsigned long rel_gfn = gfn - memslot->base_gfn;
1660 
1661                 set_bit_le(rel_gfn, memslot->dirty_bitmap);
1662         }
1663 }
1664 
1665 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1666 {
1667         struct kvm_memory_slot *memslot;
1668 
1669         memslot = gfn_to_memslot(kvm, gfn);
1670         mark_page_dirty_in_slot(kvm, memslot, gfn);
1671 }
1672 EXPORT_SYMBOL_GPL(mark_page_dirty);
1673 
1674 /*
1675  * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1676  */
1677 void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1678 {
1679         DEFINE_WAIT(wait);
1680 
1681         for (;;) {
1682                 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1683 
1684                 if (kvm_arch_vcpu_runnable(vcpu)) {
1685                         kvm_make_request(KVM_REQ_UNHALT, vcpu);
1686                         break;
1687                 }
1688                 if (kvm_cpu_has_pending_timer(vcpu))
1689                         break;
1690                 if (signal_pending(current))
1691                         break;
1692 
1693                 schedule();
1694         }
1695 
1696         finish_wait(&vcpu->wq, &wait);
1697 }
1698 EXPORT_SYMBOL_GPL(kvm_vcpu_block);
1699 
1700 #ifndef CONFIG_S390
1701 /*
1702  * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
1703  */
1704 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1705 {
1706         int me;
1707         int cpu = vcpu->cpu;
1708         wait_queue_head_t *wqp;
1709 
1710         wqp = kvm_arch_vcpu_wq(vcpu);
1711         if (waitqueue_active(wqp)) {
1712                 wake_up_interruptible(wqp);
1713                 ++vcpu->stat.halt_wakeup;
1714         }
1715 
1716         me = get_cpu();
1717         if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
1718                 if (kvm_arch_vcpu_should_kick(vcpu))
1719                         smp_send_reschedule(cpu);
1720         put_cpu();
1721 }
1722 EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
1723 #endif /* !CONFIG_S390 */
1724 
1725 bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
1726 {
1727         struct pid *pid;
1728         struct task_struct *task = NULL;
1729         bool ret = false;
1730 
1731         rcu_read_lock();
1732         pid = rcu_dereference(target->pid);
1733         if (pid)
1734                 task = get_pid_task(target->pid, PIDTYPE_PID);
1735         rcu_read_unlock();
1736         if (!task)
1737                 return ret;
1738         if (task->flags & PF_VCPU) {
1739                 put_task_struct(task);
1740                 return ret;
1741         }
1742         ret = yield_to(task, 1);
1743         put_task_struct(task);
1744 
1745         return ret;
1746 }
1747 EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
1748 
1749 /*
1750  * Helper that checks whether a VCPU is eligible for directed yield.
1751  * Most eligible candidate to yield is decided by following heuristics:
1752  *
1753  *  (a) VCPU which has not done pl-exit or cpu relax intercepted recently
1754  *  (preempted lock holder), indicated by @in_spin_loop.
1755  *  Set at the beiginning and cleared at the end of interception/PLE handler.
1756  *
1757  *  (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
1758  *  chance last time (mostly it has become eligible now since we have probably
1759  *  yielded to lockholder in last iteration. This is done by toggling
1760  *  @dy_eligible each time a VCPU checked for eligibility.)
1761  *
1762  *  Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
1763  *  to preempted lock-holder could result in wrong VCPU selection and CPU
1764  *  burning. Giving priority for a potential lock-holder increases lock
1765  *  progress.
1766  *
1767  *  Since algorithm is based on heuristics, accessing another VCPU data without
1768  *  locking does not harm. It may result in trying to yield to  same VCPU, fail
1769  *  and continue with next VCPU and so on.
1770  */
1771 static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
1772 {
1773 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
1774         bool eligible;
1775 
1776         eligible = !vcpu->spin_loop.in_spin_loop ||
1777                         (vcpu->spin_loop.in_spin_loop &&
1778                          vcpu->spin_loop.dy_eligible);
1779 
1780         if (vcpu->spin_loop.in_spin_loop)
1781                 kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
1782 
1783         return eligible;
1784 #else
1785         return true;
1786 #endif
1787 }
1788 
1789 void kvm_vcpu_on_spin(struct kvm_vcpu *me)
1790 {
1791         struct kvm *kvm = me->kvm;
1792         struct kvm_vcpu *vcpu;
1793         int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
1794         int yielded = 0;
1795         int try = 3;
1796         int pass;
1797         int i;
1798 
1799         kvm_vcpu_set_in_spin_loop(me, true);
1800         /*
1801          * We boost the priority of a VCPU that is runnable but not
1802          * currently running, because it got preempted by something
1803          * else and called schedule in __vcpu_run.  Hopefully that
1804          * VCPU is holding the lock that we need and will release it.
1805          * We approximate round-robin by starting at the last boosted VCPU.
1806          */
1807         for (pass = 0; pass < 2 && !yielded && try; pass++) {
1808                 kvm_for_each_vcpu(i, vcpu, kvm) {
1809                         if (!pass && i <= last_boosted_vcpu) {
1810                                 i = last_boosted_vcpu;
1811                                 continue;
1812                         } else if (pass && i > last_boosted_vcpu)
1813                                 break;
1814                         if (!ACCESS_ONCE(vcpu->preempted))
1815                                 continue;
1816                         if (vcpu == me)
1817                                 continue;
1818                         if (waitqueue_active(&vcpu->wq))
1819                                 continue;
1820                         if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
1821                                 continue;
1822 
1823                         yielded = kvm_vcpu_yield_to(vcpu);
1824                         if (yielded > 0) {
1825                                 kvm->last_boosted_vcpu = i;
1826                                 break;
1827                         } else if (yielded < 0) {
1828                                 try--;
1829                                 if (!try)
1830                                         break;
1831                         }
1832                 }
1833         }
1834         kvm_vcpu_set_in_spin_loop(me, false);
1835 
1836         /* Ensure vcpu is not eligible during next spinloop */
1837         kvm_vcpu_set_dy_eligible(me, false);
1838 }
1839 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
1840 
1841 static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1842 {
1843         struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1844         struct page *page;
1845 
1846         if (vmf->pgoff == 0)
1847                 page = virt_to_page(vcpu->run);
1848 #ifdef CONFIG_X86
1849         else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1850                 page = virt_to_page(vcpu->arch.pio_data);
1851 #endif
1852 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1853         else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1854                 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1855 #endif
1856         else
1857                 return kvm_arch_vcpu_fault(vcpu, vmf);
1858         get_page(page);
1859         vmf->page = page;
1860         return 0;
1861 }
1862 
1863 static const struct vm_operations_struct kvm_vcpu_vm_ops = {
1864         .fault = kvm_vcpu_fault,
1865 };
1866 
1867 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1868 {
1869         vma->vm_ops = &kvm_vcpu_vm_ops;
1870         return 0;
1871 }
1872 
1873 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1874 {
1875         struct kvm_vcpu *vcpu = filp->private_data;
1876 
1877         kvm_put_kvm(vcpu->kvm);
1878         return 0;
1879 }
1880 
1881 static struct file_operations kvm_vcpu_fops = {
1882         .release        = kvm_vcpu_release,
1883         .unlocked_ioctl = kvm_vcpu_ioctl,
1884 #ifdef CONFIG_COMPAT
1885         .compat_ioctl   = kvm_vcpu_compat_ioctl,
1886 #endif
1887         .mmap           = kvm_vcpu_mmap,
1888         .llseek         = noop_llseek,
1889 };
1890 
1891 /*
1892  * Allocates an inode for the vcpu.
1893  */
1894 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1895 {
1896         return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC);
1897 }
1898 
1899 /*
1900  * Creates some virtual cpus.  Good luck creating more than one.
1901  */
1902 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
1903 {
1904         int r;
1905         struct kvm_vcpu *vcpu, *v;
1906 
1907         if (id >= KVM_MAX_VCPUS)
1908                 return -EINVAL;
1909 
1910         vcpu = kvm_arch_vcpu_create(kvm, id);
1911         if (IS_ERR(vcpu))
1912                 return PTR_ERR(vcpu);
1913 
1914         preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1915 
1916         r = kvm_arch_vcpu_setup(vcpu);
1917         if (r)
1918                 goto vcpu_destroy;
1919 
1920         mutex_lock(&kvm->lock);
1921         if (!kvm_vcpu_compatible(vcpu)) {
1922                 r = -EINVAL;
1923                 goto unlock_vcpu_destroy;
1924         }
1925         if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1926                 r = -EINVAL;
1927                 goto unlock_vcpu_destroy;
1928         }
1929 
1930         kvm_for_each_vcpu(r, v, kvm)
1931                 if (v->vcpu_id == id) {
1932                         r = -EEXIST;
1933                         goto unlock_vcpu_destroy;
1934                 }
1935 
1936         BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
1937 
1938         /* Now it's all set up, let userspace reach it */
1939         kvm_get_kvm(kvm);
1940         r = create_vcpu_fd(vcpu);
1941         if (r < 0) {
1942                 kvm_put_kvm(kvm);
1943                 goto unlock_vcpu_destroy;
1944         }
1945 
1946         kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1947         smp_wmb();
1948         atomic_inc(&kvm->online_vcpus);
1949 
1950         mutex_unlock(&kvm->lock);
1951         kvm_arch_vcpu_postcreate(vcpu);
1952         return r;
1953 
1954 unlock_vcpu_destroy:
1955         mutex_unlock(&kvm->lock);
1956 vcpu_destroy:
1957         kvm_arch_vcpu_destroy(vcpu);
1958         return r;
1959 }
1960 
1961 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1962 {
1963         if (sigset) {
1964                 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1965                 vcpu->sigset_active = 1;
1966                 vcpu->sigset = *sigset;
1967         } else
1968                 vcpu->sigset_active = 0;
1969         return 0;
1970 }
1971 
1972 static long kvm_vcpu_ioctl(struct file *filp,
1973                            unsigned int ioctl, unsigned long arg)
1974 {
1975         struct kvm_vcpu *vcpu = filp->private_data;
1976         void __user *argp = (void __user *)arg;
1977         int r;
1978         struct kvm_fpu *fpu = NULL;
1979         struct kvm_sregs *kvm_sregs = NULL;
1980 
1981         if (vcpu->kvm->mm != current->mm)
1982                 return -EIO;
1983 
1984         if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
1985                 return -EINVAL;
1986 
1987 #if defined(CONFIG_S390) || defined(CONFIG_PPC) || defined(CONFIG_MIPS)
1988         /*
1989          * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
1990          * so vcpu_load() would break it.
1991          */
1992         if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
1993                 return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1994 #endif
1995 
1996 
1997         r = vcpu_load(vcpu);
1998         if (r)
1999                 return r;
2000         switch (ioctl) {
2001         case KVM_RUN:
2002                 r = -EINVAL;
2003                 if (arg)
2004                         goto out;
2005                 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
2006                 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
2007                 break;
2008         case KVM_GET_REGS: {
2009                 struct kvm_regs *kvm_regs;
2010 
2011                 r = -ENOMEM;
2012                 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
2013                 if (!kvm_regs)
2014                         goto out;
2015                 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
2016                 if (r)
2017                         goto out_free1;
2018                 r = -EFAULT;
2019                 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
2020                         goto out_free1;
2021                 r = 0;
2022 out_free1:
2023                 kfree(kvm_regs);
2024                 break;
2025         }
2026         case KVM_SET_REGS: {
2027                 struct kvm_regs *kvm_regs;
2028 
2029                 r = -ENOMEM;
2030                 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
2031                 if (IS_ERR(kvm_regs)) {
2032                         r = PTR_ERR(kvm_regs);
2033                         goto out;
2034                 }
2035                 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
2036                 kfree(kvm_regs);
2037                 break;
2038         }
2039         case KVM_GET_SREGS: {
2040                 kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
2041                 r = -ENOMEM;
2042                 if (!kvm_sregs)
2043                         goto out;
2044                 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
2045                 if (r)
2046                         goto out;
2047                 r = -EFAULT;
2048                 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
2049                         goto out;
2050                 r = 0;
2051                 break;
2052         }
2053         case KVM_SET_SREGS: {
2054                 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
2055                 if (IS_ERR(kvm_sregs)) {
2056                         r = PTR_ERR(kvm_sregs);
2057                         kvm_sregs = NULL;
2058                         goto out;
2059                 }
2060                 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
2061                 break;
2062         }
2063         case KVM_GET_MP_STATE: {
2064                 struct kvm_mp_state mp_state;
2065 
2066                 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
2067                 if (r)
2068                         goto out;
2069                 r = -EFAULT;
2070                 if (copy_to_user(argp, &mp_state, sizeof mp_state))
2071                         goto out;
2072                 r = 0;
2073                 break;
2074         }
2075         case KVM_SET_MP_STATE: {
2076                 struct kvm_mp_state mp_state;
2077 
2078                 r = -EFAULT;
2079                 if (copy_from_user(&mp_state, argp, sizeof mp_state))
2080                         goto out;
2081                 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
2082                 break;
2083         }
2084         case KVM_TRANSLATE: {
2085                 struct kvm_translation tr;
2086 
2087                 r = -EFAULT;
2088                 if (copy_from_user(&tr, argp, sizeof tr))
2089                         goto out;
2090                 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
2091                 if (r)
2092                         goto out;
2093                 r = -EFAULT;
2094                 if (copy_to_user(argp, &tr, sizeof tr))
2095                         goto out;
2096                 r = 0;
2097                 break;
2098         }
2099         case KVM_SET_GUEST_DEBUG: {
2100                 struct kvm_guest_debug dbg;
2101 
2102                 r = -EFAULT;
2103                 if (copy_from_user(&dbg, argp, sizeof dbg))
2104                         goto out;
2105                 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
2106                 break;
2107         }
2108         case KVM_SET_SIGNAL_MASK: {
2109                 struct kvm_signal_mask __user *sigmask_arg = argp;
2110                 struct kvm_signal_mask kvm_sigmask;
2111                 sigset_t sigset, *p;
2112 
2113                 p = NULL;
2114                 if (argp) {
2115                         r = -EFAULT;
2116                         if (copy_from_user(&kvm_sigmask, argp,
2117                                            sizeof kvm_sigmask))
2118                                 goto out;
2119                         r = -EINVAL;
2120                         if (kvm_sigmask.len != sizeof sigset)
2121                                 goto out;
2122                         r = -EFAULT;
2123                         if (copy_from_user(&sigset, sigmask_arg->sigset,
2124                                            sizeof sigset))
2125                                 goto out;
2126                         p = &sigset;
2127                 }
2128                 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
2129                 break;
2130         }
2131         case KVM_GET_FPU: {
2132                 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
2133                 r = -ENOMEM;
2134                 if (!fpu)
2135                         goto out;
2136                 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
2137                 if (r)
2138                         goto out;
2139                 r = -EFAULT;
2140                 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
2141                         goto out;
2142                 r = 0;
2143                 break;
2144         }
2145         case KVM_SET_FPU: {
2146                 fpu = memdup_user(argp, sizeof(*fpu));
2147                 if (IS_ERR(fpu)) {
2148                         r = PTR_ERR(fpu);
2149                         fpu = NULL;
2150                         goto out;
2151                 }
2152                 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
2153                 break;
2154         }
2155         default:
2156                 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
2157         }
2158 out:
2159         vcpu_put(vcpu);
2160         kfree(fpu);
2161         kfree(kvm_sregs);
2162         return r;
2163 }
2164 
2165 #ifdef CONFIG_COMPAT
2166 static long kvm_vcpu_compat_ioctl(struct file *filp,
2167                                   unsigned int ioctl, unsigned long arg)
2168 {
2169         struct kvm_vcpu *vcpu = filp->private_data;
2170         void __user *argp = compat_ptr(arg);
2171         int r;
2172 
2173         if (vcpu->kvm->mm != current->mm)
2174                 return -EIO;
2175 
2176         switch (ioctl) {
2177         case KVM_SET_SIGNAL_MASK: {
2178                 struct kvm_signal_mask __user *sigmask_arg = argp;
2179                 struct kvm_signal_mask kvm_sigmask;
2180                 compat_sigset_t csigset;
2181                 sigset_t sigset;
2182 
2183                 if (argp) {
2184                         r = -EFAULT;
2185                         if (copy_from_user(&kvm_sigmask, argp,
2186                                            sizeof kvm_sigmask))
2187                                 goto out;
2188                         r = -EINVAL;
2189                         if (kvm_sigmask.len != sizeof csigset)
2190                                 goto out;
2191                         r = -EFAULT;
2192                         if (copy_from_user(&csigset, sigmask_arg->sigset,
2193                                            sizeof csigset))
2194                                 goto out;
2195                         sigset_from_compat(&sigset, &csigset);
2196                         r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2197                 } else
2198                         r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
2199                 break;
2200         }
2201         default:
2202                 r = kvm_vcpu_ioctl(filp, ioctl, arg);
2203         }
2204 
2205 out:
2206         return r;
2207 }
2208 #endif
2209 
2210 static int kvm_device_ioctl_attr(struct kvm_device *dev,
2211                                  int (*accessor)(struct kvm_device *dev,
2212                                                  struct kvm_device_attr *attr),
2213                                  unsigned long arg)
2214 {
2215         struct kvm_device_attr attr;
2216 
2217         if (!accessor)
2218                 return -EPERM;
2219 
2220         if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2221                 return -EFAULT;
2222 
2223         return accessor(dev, &attr);
2224 }
2225 
2226 static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
2227                              unsigned long arg)
2228 {
2229         struct kvm_device *dev = filp->private_data;
2230 
2231         switch (ioctl) {
2232         case KVM_SET_DEVICE_ATTR:
2233                 return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
2234         case KVM_GET_DEVICE_ATTR:
2235                 return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
2236         case KVM_HAS_DEVICE_ATTR:
2237                 return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
2238         default:
2239                 if (dev->ops->ioctl)
2240                         return dev->ops->ioctl(dev, ioctl, arg);
2241 
2242                 return -ENOTTY;
2243         }
2244 }
2245 
2246 static int kvm_device_release(struct inode *inode, struct file *filp)
2247 {
2248         struct kvm_device *dev = filp->private_data;
2249         struct kvm *kvm = dev->kvm;
2250 
2251         kvm_put_kvm(kvm);
2252         return 0;
2253 }
2254 
2255 static const struct file_operations kvm_device_fops = {
2256         .unlocked_ioctl = kvm_device_ioctl,
2257 #ifdef CONFIG_COMPAT
2258         .compat_ioctl = kvm_device_ioctl,
2259 #endif
2260         .release = kvm_device_release,
2261 };
2262 
2263 struct kvm_device *kvm_device_from_filp(struct file *filp)
2264 {
2265         if (filp->f_op != &kvm_device_fops)
2266                 return NULL;
2267 
2268         return filp->private_data;
2269 }
2270 
2271 static int kvm_ioctl_create_device(struct kvm *kvm,
2272                                    struct kvm_create_device *cd)
2273 {
2274         struct kvm_device_ops *ops = NULL;
2275         struct kvm_device *dev;
2276         bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
2277         int ret;
2278 
2279         switch (cd->type) {
2280 #ifdef CONFIG_KVM_MPIC
2281         case KVM_DEV_TYPE_FSL_MPIC_20:
2282         case KVM_DEV_TYPE_FSL_MPIC_42:
2283                 ops = &kvm_mpic_ops;
2284                 break;
2285 #endif
2286 #ifdef CONFIG_KVM_XICS
2287         case KVM_DEV_TYPE_XICS:
2288                 ops = &kvm_xics_ops;
2289                 break;
2290 #endif
2291 #ifdef CONFIG_KVM_VFIO
2292         case KVM_DEV_TYPE_VFIO:
2293                 ops = &kvm_vfio_ops;
2294                 break;
2295 #endif
2296 #ifdef CONFIG_KVM_ARM_VGIC
2297         case KVM_DEV_TYPE_ARM_VGIC_V2:
2298                 ops = &kvm_arm_vgic_v2_ops;
2299                 break;
2300 #endif
2301         default:
2302                 return -ENODEV;
2303         }
2304 
2305         if (test)
2306                 return 0;
2307 
2308         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2309         if (!dev)
2310                 return -ENOMEM;
2311 
2312         dev->ops = ops;
2313         dev->kvm = kvm;
2314 
2315         ret = ops->create(dev, cd->type);
2316         if (ret < 0) {
2317                 kfree(dev);
2318                 return ret;
2319         }
2320 
2321         ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC);
2322         if (ret < 0) {
2323                 ops->destroy(dev);
2324                 return ret;
2325         }
2326 
2327         list_add(&dev->vm_node, &kvm->devices);
2328         kvm_get_kvm(kvm);
2329         cd->fd = ret;
2330         return 0;
2331 }
2332 
2333 static long kvm_vm_ioctl(struct file *filp,
2334                            unsigned int ioctl, unsigned long arg)
2335 {
2336         struct kvm *kvm = filp->private_data;
2337         void __user *argp = (void __user *)arg;
2338         int r;
2339 
2340         if (kvm->mm != current->mm)
2341                 return -EIO;
2342         switch (ioctl) {
2343         case KVM_CREATE_VCPU:
2344                 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2345                 break;
2346         case KVM_SET_USER_MEMORY_REGION: {
2347                 struct kvm_userspace_memory_region kvm_userspace_mem;
2348 
2349                 r = -EFAULT;
2350                 if (copy_from_user(&kvm_userspace_mem, argp,
2351                                                 sizeof kvm_userspace_mem))
2352                         goto out;
2353 
2354                 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
2355                 break;
2356         }
2357         case KVM_GET_DIRTY_LOG: {
2358                 struct kvm_dirty_log log;
2359 
2360                 r = -EFAULT;
2361                 if (copy_from_user(&log, argp, sizeof log))
2362                         goto out;
2363                 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2364                 break;
2365         }
2366 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2367         case KVM_REGISTER_COALESCED_MMIO: {
2368                 struct kvm_coalesced_mmio_zone zone;
2369                 r = -EFAULT;
2370                 if (copy_from_user(&zone, argp, sizeof zone))
2371                         goto out;
2372                 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
2373                 break;
2374         }
2375         case KVM_UNREGISTER_COALESCED_MMIO: {
2376                 struct kvm_coalesced_mmio_zone zone;
2377                 r = -EFAULT;
2378                 if (copy_from_user(&zone, argp, sizeof zone))
2379                         goto out;
2380                 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
2381                 break;
2382         }
2383 #endif
2384         case KVM_IRQFD: {
2385                 struct kvm_irqfd data;
2386 
2387                 r = -EFAULT;
2388                 if (copy_from_user(&data, argp, sizeof data))
2389                         goto out;
2390                 r = kvm_irqfd(kvm, &data);
2391                 break;
2392         }
2393         case KVM_IOEVENTFD: {
2394                 struct kvm_ioeventfd data;
2395 
2396                 r = -EFAULT;
2397                 if (copy_from_user(&data, argp, sizeof data))
2398                         goto out;
2399                 r = kvm_ioeventfd(kvm, &data);
2400                 break;
2401         }
2402 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2403         case KVM_SET_BOOT_CPU_ID:
2404                 r = 0;
2405                 mutex_lock(&kvm->lock);
2406                 if (atomic_read(&kvm->online_vcpus) != 0)
2407                         r = -EBUSY;
2408                 else
2409                         kvm->bsp_vcpu_id = arg;
2410                 mutex_unlock(&kvm->lock);
2411                 break;
2412 #endif
2413 #ifdef CONFIG_HAVE_KVM_MSI
2414         case KVM_SIGNAL_MSI: {
2415                 struct kvm_msi msi;
2416 
2417                 r = -EFAULT;
2418                 if (copy_from_user(&msi, argp, sizeof msi))
2419                         goto out;
2420                 r = kvm_send_userspace_msi(kvm, &msi);
2421                 break;
2422         }
2423 #endif
2424 #ifdef __KVM_HAVE_IRQ_LINE
2425         case KVM_IRQ_LINE_STATUS:
2426         case KVM_IRQ_LINE: {
2427                 struct kvm_irq_level irq_event;
2428 
2429                 r = -EFAULT;
2430                 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2431                         goto out;
2432 
2433                 r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
2434                                         ioctl == KVM_IRQ_LINE_STATUS);
2435                 if (r)
2436                         goto out;
2437 
2438                 r = -EFAULT;
2439                 if (ioctl == KVM_IRQ_LINE_STATUS) {
2440                         if (copy_to_user(argp, &irq_event, sizeof irq_event))
2441                                 goto out;
2442                 }
2443 
2444                 r = 0;
2445                 break;
2446         }
2447 #endif
2448 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
2449         case KVM_SET_GSI_ROUTING: {
2450                 struct kvm_irq_routing routing;
2451                 struct kvm_irq_routing __user *urouting;
2452                 struct kvm_irq_routing_entry *entries;
2453 
2454                 r = -EFAULT;
2455                 if (copy_from_user(&routing, argp, sizeof(routing)))
2456                         goto out;
2457                 r = -EINVAL;
2458                 if (routing.nr > KVM_MAX_IRQ_ROUTES)
2459                         goto out;
2460                 if (routing.flags)
2461                         goto out;
2462                 r = -ENOMEM;
2463                 entries = vmalloc(routing.nr * sizeof(*entries));
2464                 if (!entries)
2465                         goto out;
2466                 r = -EFAULT;
2467                 urouting = argp;
2468                 if (copy_from_user(entries, urouting->entries,
2469                                    routing.nr * sizeof(*entries)))
2470                         goto out_free_irq_routing;
2471                 r = kvm_set_irq_routing(kvm, entries, routing.nr,
2472                                         routing.flags);
2473         out_free_irq_routing:
2474                 vfree(entries);
2475                 break;
2476         }
2477 #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
2478         case KVM_CREATE_DEVICE: {
2479                 struct kvm_create_device cd;
2480 
2481                 r = -EFAULT;
2482                 if (copy_from_user(&cd, argp, sizeof(cd)))
2483                         goto out;
2484 
2485                 r = kvm_ioctl_create_device(kvm, &cd);
2486                 if (r)
2487                         goto out;
2488 
2489                 r = -EFAULT;
2490                 if (copy_to_user(argp, &cd, sizeof(cd)))
2491                         goto out;
2492 
2493                 r = 0;
2494                 break;
2495         }
2496         default:
2497                 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
2498                 if (r == -ENOTTY)
2499                         r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
2500         }
2501 out:
2502         return r;
2503 }
2504 
2505 #ifdef CONFIG_COMPAT
2506 struct compat_kvm_dirty_log {
2507         __u32 slot;
2508         __u32 padding1;
2509         union {
2510                 compat_uptr_t dirty_bitmap; /* one bit per page */
2511                 __u64 padding2;
2512         };
2513 };
2514 
2515 static long kvm_vm_compat_ioctl(struct file *filp,
2516                            unsigned int ioctl, unsigned long arg)
2517 {
2518         struct kvm *kvm = filp->private_data;
2519         int r;
2520 
2521         if (kvm->mm != current->mm)
2522                 return -EIO;
2523         switch (ioctl) {
2524         case KVM_GET_DIRTY_LOG: {
2525                 struct compat_kvm_dirty_log compat_log;
2526                 struct kvm_dirty_log log;
2527 
2528                 r = -EFAULT;
2529                 if (copy_from_user(&compat_log, (void __user *)arg,
2530                                    sizeof(compat_log)))
2531                         goto out;
2532                 log.slot         = compat_log.slot;
2533                 log.padding1     = compat_log.padding1;
2534                 log.padding2     = compat_log.padding2;
2535                 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
2536 
2537                 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2538                 break;
2539         }
2540         default:
2541                 r = kvm_vm_ioctl(filp, ioctl, arg);
2542         }
2543 
2544 out:
2545         return r;
2546 }
2547 #endif
2548 
2549 static struct file_operations kvm_vm_fops = {
2550         .release        = kvm_vm_release,
2551         .unlocked_ioctl = kvm_vm_ioctl,
2552 #ifdef CONFIG_COMPAT
2553         .compat_ioctl   = kvm_vm_compat_ioctl,
2554 #endif
2555         .llseek         = noop_llseek,
2556 };
2557 
2558 static int kvm_dev_ioctl_create_vm(unsigned long type)
2559 {
2560         int r;
2561         struct kvm *kvm;
2562 
2563         kvm = kvm_create_vm(type);
2564         if (IS_ERR(kvm))
2565                 return PTR_ERR(kvm);
2566 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2567         r = kvm_coalesced_mmio_init(kvm);
2568         if (r < 0) {
2569                 kvm_put_kvm(kvm);
2570                 return r;
2571         }
2572 #endif
2573         r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR | O_CLOEXEC);
2574         if (r < 0)
2575                 kvm_put_kvm(kvm);
2576 
2577         return r;
2578 }
2579 
2580 static long kvm_dev_ioctl_check_extension_generic(long arg)
2581 {
2582         switch (arg) {
2583         case KVM_CAP_USER_MEMORY:
2584         case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2585         case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2586 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
2587         case KVM_CAP_SET_BOOT_CPU_ID:
2588 #endif
2589         case KVM_CAP_INTERNAL_ERROR_DATA:
2590 #ifdef CONFIG_HAVE_KVM_MSI
2591         case KVM_CAP_SIGNAL_MSI:
2592 #endif
2593 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
2594         case KVM_CAP_IRQFD_RESAMPLE:
2595 #endif
2596                 return 1;
2597 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
2598         case KVM_CAP_IRQ_ROUTING:
2599                 return KVM_MAX_IRQ_ROUTES;
2600 #endif
2601         default:
2602                 break;
2603         }
2604         return kvm_dev_ioctl_check_extension(arg);
2605 }
2606 
2607 static long kvm_dev_ioctl(struct file *filp,
2608                           unsigned int ioctl, unsigned long arg)
2609 {
2610         long r = -EINVAL;
2611 
2612         switch (ioctl) {
2613         case KVM_GET_API_VERSION:
2614                 r = -EINVAL;
2615                 if (arg)
2616                         goto out;
2617                 r = KVM_API_VERSION;
2618                 break;
2619         case KVM_CREATE_VM:
2620                 r = kvm_dev_ioctl_create_vm(arg);
2621                 break;
2622         case KVM_CHECK_EXTENSION:
2623                 r = kvm_dev_ioctl_check_extension_generic(arg);
2624                 break;
2625         case KVM_GET_VCPU_MMAP_SIZE:
2626                 r = -EINVAL;
2627                 if (arg)
2628                         goto out;
2629                 r = PAGE_SIZE;     /* struct kvm_run */
2630 #ifdef CONFIG_X86
2631                 r += PAGE_SIZE;    /* pio data page */
2632 #endif
2633 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2634                 r += PAGE_SIZE;    /* coalesced mmio ring page */
2635 #endif
2636                 break;
2637         case KVM_TRACE_ENABLE:
2638         case KVM_TRACE_PAUSE:
2639         case KVM_TRACE_DISABLE:
2640                 r = -EOPNOTSUPP;
2641                 break;
2642         default:
2643                 return kvm_arch_dev_ioctl(filp, ioctl, arg);
2644         }
2645 out:
2646         return r;
2647 }
2648 
2649 static struct file_operations kvm_chardev_ops = {
2650         .unlocked_ioctl = kvm_dev_ioctl,
2651         .compat_ioctl   = kvm_dev_ioctl,
2652         .llseek         = noop_llseek,
2653 };
2654 
2655 static struct miscdevice kvm_dev = {
2656         KVM_MINOR,
2657         "kvm",
2658         &kvm_chardev_ops,
2659 };
2660 
2661 static void hardware_enable_nolock(void *junk)
2662 {
2663         int cpu = raw_smp_processor_id();
2664         int r;
2665 
2666         if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2667                 return;
2668 
2669         cpumask_set_cpu(cpu, cpus_hardware_enabled);
2670 
2671         r = kvm_arch_hardware_enable(NULL);
2672 
2673         if (r) {
2674                 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2675                 atomic_inc(&hardware_enable_failed);
2676                 printk(KERN_INFO "kvm: enabling virtualization on "
2677                                  "CPU%d failed\n", cpu);
2678         }
2679 }
2680 
2681 static void hardware_enable(void)
2682 {
2683         raw_spin_lock(&kvm_count_lock);
2684         if (kvm_usage_count)
2685                 hardware_enable_nolock(NULL);
2686         raw_spin_unlock(&kvm_count_lock);
2687 }
2688 
2689 static void hardware_disable_nolock(void *junk)
2690 {
2691         int cpu = raw_smp_processor_id();
2692 
2693         if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2694                 return;
2695         cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2696         kvm_arch_hardware_disable(NULL);
2697 }
2698 
2699 static void hardware_disable(void)
2700 {
2701         raw_spin_lock(&kvm_count_lock);
2702         if (kvm_usage_count)
2703                 hardware_disable_nolock(NULL);
2704         raw_spin_unlock(&kvm_count_lock);
2705 }
2706 
2707 static void hardware_disable_all_nolock(void)
2708 {
2709         BUG_ON(!kvm_usage_count);
2710 
2711         kvm_usage_count--;
2712         if (!kvm_usage_count)
2713                 on_each_cpu(hardware_disable_nolock, NULL, 1);
2714 }
2715 
2716 static void hardware_disable_all(void)
2717 {
2718         raw_spin_lock(&kvm_count_lock);
2719         hardware_disable_all_nolock();
2720         raw_spin_unlock(&kvm_count_lock);
2721 }
2722 
2723 static int hardware_enable_all(void)
2724 {
2725         int r = 0;
2726 
2727         raw_spin_lock(&kvm_count_lock);
2728 
2729         kvm_usage_count++;
2730         if (kvm_usage_count == 1) {
2731                 atomic_set(&hardware_enable_failed, 0);
2732                 on_each_cpu(hardware_enable_nolock, NULL, 1);
2733 
2734                 if (atomic_read(&hardware_enable_failed)) {
2735                         hardware_disable_all_nolock();
2736                         r = -EBUSY;
2737                 }
2738         }
2739 
2740         raw_spin_unlock(&kvm_count_lock);
2741 
2742         return r;
2743 }
2744 
2745 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2746                            void *v)
2747 {
2748         int cpu = (long)v;
2749 
2750         val &= ~CPU_TASKS_FROZEN;
2751         switch (val) {
2752         case CPU_DYING:
2753                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2754                        cpu);
2755                 hardware_disable();
2756                 break;
2757         case CPU_STARTING:
2758                 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2759                        cpu);
2760                 hardware_enable();
2761                 break;
2762         }
2763         return NOTIFY_OK;
2764 }
2765 
2766 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2767                       void *v)
2768 {
2769         /*
2770          * Some (well, at least mine) BIOSes hang on reboot if
2771          * in vmx root mode.
2772          *
2773          * And Intel TXT required VMX off for all cpu when system shutdown.
2774          */
2775         printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2776         kvm_rebooting = true;
2777         on_each_cpu(hardware_disable_nolock, NULL, 1);
2778         return NOTIFY_OK;
2779 }
2780 
2781 static struct notifier_block kvm_reboot_notifier = {
2782         .notifier_call = kvm_reboot,
2783         .priority = 0,
2784 };
2785 
2786 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2787 {
2788         int i;
2789 
2790         for (i = 0; i < bus->dev_count; i++) {
2791                 struct kvm_io_device *pos = bus->range[i].dev;
2792 
2793                 kvm_iodevice_destructor(pos);
2794         }
2795         kfree(bus);
2796 }
2797 
2798 static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1,
2799                                  const struct kvm_io_range *r2)
2800 {
2801         gpa_t addr1 = r1->addr;
2802         gpa_t addr2 = r2->addr;
2803 
2804         if (addr1 < addr2)
2805                 return -1;
2806 
2807         /* If r2->len == 0, match the exact address.  If r2->len != 0,
2808          * accept any overlapping write.  Any order is acceptable for
2809          * overlapping ranges, because kvm_io_bus_get_first_dev ensures
2810          * we process all of them.
2811          */
2812         if (r2->len) {
2813                 addr1 += r1->len;
2814                 addr2 += r2->len;
2815         }
2816 
2817         if (addr1 > addr2)
2818                 return 1;
2819 
2820         return 0;
2821 }
2822 
2823 static int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
2824 {
2825         return kvm_io_bus_cmp(p1, p2);
2826 }
2827 
2828 static int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
2829                           gpa_t addr, int len)
2830 {
2831         bus->range[bus->dev_count++] = (struct kvm_io_range) {
2832                 .addr = addr,
2833                 .len = len,
2834                 .dev = dev,
2835         };
2836 
2837         sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
2838                 kvm_io_bus_sort_cmp, NULL);
2839 
2840         return 0;
2841 }
2842 
2843 static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
2844                              gpa_t addr, int len)
2845 {
2846         struct kvm_io_range *range, key;
2847         int off;
2848 
2849         key = (struct kvm_io_range) {
2850                 .addr = addr,
2851                 .len = len,
2852         };
2853 
2854         range = bsearch(&key, bus->range, bus->dev_count,
2855                         sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
2856         if (range == NULL)
2857                 return -ENOENT;
2858 
2859         off = range - bus->range;
2860 
2861         while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0)
2862                 off--;
2863 
2864         return off;
2865 }
2866 
2867 static int __kvm_io_bus_write(struct kvm_io_bus *bus,
2868                               struct kvm_io_range *range, const void *val)
2869 {
2870         int idx;
2871 
2872         idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
2873         if (idx < 0)
2874                 return -EOPNOTSUPP;
2875 
2876         while (idx < bus->dev_count &&
2877                 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
2878                 if (!kvm_iodevice_write(bus->range[idx].dev, range->addr,
2879                                         range->len, val))
2880                         return idx;
2881                 idx++;
2882         }
2883 
2884         return -EOPNOTSUPP;
2885 }
2886 
2887 /* kvm_io_bus_write - called under kvm->slots_lock */
2888 int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2889                      int len, const void *val)
2890 {
2891         struct kvm_io_bus *bus;
2892         struct kvm_io_range range;
2893         int r;
2894 
2895         range = (struct kvm_io_range) {
2896                 .addr = addr,
2897                 .len = len,
2898         };
2899 
2900         bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2901         r = __kvm_io_bus_write(bus, &range, val);
2902         return r < 0 ? r : 0;
2903 }
2904 
2905 /* kvm_io_bus_write_cookie - called under kvm->slots_lock */
2906 int kvm_io_bus_write_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2907                             int len, const void *val, long cookie)
2908 {
2909         struct kvm_io_bus *bus;
2910         struct kvm_io_range range;
2911 
2912         range = (struct kvm_io_range) {
2913                 .addr = addr,
2914                 .len = len,
2915         };
2916 
2917         bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2918 
2919         /* First try the device referenced by cookie. */
2920         if ((cookie >= 0) && (cookie < bus->dev_count) &&
2921             (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
2922                 if (!kvm_iodevice_write(bus->range[cookie].dev, addr, len,
2923                                         val))
2924                         return cookie;
2925 
2926         /*
2927          * cookie contained garbage; fall back to search and return the
2928          * correct cookie value.
2929          */
2930         return __kvm_io_bus_write(bus, &range, val);
2931 }
2932 
2933 static int __kvm_io_bus_read(struct kvm_io_bus *bus, struct kvm_io_range *range,
2934                              void *val)
2935 {
2936         int idx;
2937 
2938         idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
2939         if (idx < 0)
2940                 return -EOPNOTSUPP;
2941 
2942         while (idx < bus->dev_count &&
2943                 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
2944                 if (!kvm_iodevice_read(bus->range[idx].dev, range->addr,
2945                                        range->len, val))
2946                         return idx;
2947                 idx++;
2948         }
2949 
2950         return -EOPNOTSUPP;
2951 }
2952 
2953 /* kvm_io_bus_read - called under kvm->slots_lock */
2954 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2955                     int len, void *val)
2956 {
2957         struct kvm_io_bus *bus;
2958         struct kvm_io_range range;
2959         int r;
2960 
2961         range = (struct kvm_io_range) {
2962                 .addr = addr,
2963                 .len = len,
2964         };
2965 
2966         bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
2967         r = __kvm_io_bus_read(bus, &range, val);
2968         return r < 0 ? r : 0;
2969 }
2970 
2971 
2972 /* Caller must hold slots_lock. */
2973 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
2974                             int len, struct kvm_io_device *dev)
2975 {
2976         struct kvm_io_bus *new_bus, *bus;
2977 
2978         bus = kvm->buses[bus_idx];
2979         /* exclude ioeventfd which is limited by maximum fd */
2980         if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
2981                 return -ENOSPC;
2982 
2983         new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count + 1) *
2984                           sizeof(struct kvm_io_range)), GFP_KERNEL);
2985         if (!new_bus)
2986                 return -ENOMEM;
2987         memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
2988                sizeof(struct kvm_io_range)));
2989         kvm_io_bus_insert_dev(new_bus, dev, addr, len);
2990         rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
2991         synchronize_srcu_expedited(&kvm->srcu);
2992         kfree(bus);
2993 
2994         return 0;
2995 }
2996 
2997 /* Caller must hold slots_lock. */
2998 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
2999                               struct kvm_io_device *dev)
3000 {
3001         int i, r;
3002         struct kvm_io_bus *new_bus, *bus;
3003 
3004         bus = kvm->buses[bus_idx];
3005         r = -ENOENT;
3006         for (i = 0; i < bus->dev_count; i++)
3007                 if (bus->range[i].dev == dev) {
3008                         r = 0;
3009                         break;
3010                 }
3011 
3012         if (r)
3013                 return r;
3014 
3015         new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count - 1) *
3016                           sizeof(struct kvm_io_range)), GFP_KERNEL);
3017         if (!new_bus)
3018                 return -ENOMEM;
3019 
3020         memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
3021         new_bus->dev_count--;
3022         memcpy(new_bus->range + i, bus->range + i + 1,
3023                (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
3024 
3025         rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
3026         synchronize_srcu_expedited(&kvm->srcu);
3027         kfree(bus);
3028         return r;
3029 }
3030 
3031 static struct notifier_block kvm_cpu_notifier = {
3032         .notifier_call = kvm_cpu_hotplug,
3033 };
3034 
3035 static int vm_stat_get(void *_offset, u64 *val)
3036 {
3037         unsigned offset = (long)_offset;
3038         struct kvm *kvm;
3039 
3040         *val = 0;
3041         spin_lock(&kvm_lock);
3042         list_for_each_entry(kvm, &vm_list, vm_list)
3043                 *val += *(u32 *)((void *)kvm + offset);
3044         spin_unlock(&kvm_lock);
3045         return 0;
3046 }
3047 
3048 DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
3049 
3050 static int vcpu_stat_get(void *_offset, u64 *val)
3051 {
3052         unsigned offset = (long)_offset;
3053         struct kvm *kvm;
3054         struct kvm_vcpu *vcpu;
3055         int i;
3056 
3057         *val = 0;
3058         spin_lock(&kvm_lock);
3059         list_for_each_entry(kvm, &vm_list, vm_list)
3060                 kvm_for_each_vcpu(i, vcpu, kvm)
3061                         *val += *(u32 *)((void *)vcpu + offset);
3062 
3063         spin_unlock(&kvm_lock);
3064         return 0;
3065 }
3066 
3067 DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
3068 
3069 static const struct file_operations *stat_fops[] = {
3070         [KVM_STAT_VCPU] = &vcpu_stat_fops,
3071         [KVM_STAT_VM]   = &vm_stat_fops,
3072 };
3073 
3074 static int kvm_init_debug(void)
3075 {
3076         int r = -EEXIST;
3077         struct kvm_stats_debugfs_item *p;
3078 
3079         kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
3080         if (kvm_debugfs_dir == NULL)
3081                 goto out;
3082 
3083         for (p = debugfs_entries; p->name; ++p) {
3084                 p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
3085                                                 (void *)(long)p->offset,
3086                                                 stat_fops[p->kind]);
3087                 if (p->dentry == NULL)
3088                         goto out_dir;
3089         }
3090 
3091         return 0;
3092 
3093 out_dir:
3094         debugfs_remove_recursive(kvm_debugfs_dir);
3095 out:
3096         return r;
3097 }
3098 
3099 static void kvm_exit_debug(void)
3100 {
3101         struct kvm_stats_debugfs_item *p;
3102 
3103         for (p = debugfs_entries; p->name; ++p)
3104                 debugfs_remove(p->dentry);
3105         debugfs_remove(kvm_debugfs_dir);
3106 }
3107 
3108 static int kvm_suspend(void)
3109 {
3110         if (kvm_usage_count)
3111                 hardware_disable_nolock(NULL);
3112         return 0;
3113 }
3114 
3115 static void kvm_resume(void)
3116 {
3117         if (kvm_usage_count) {
3118                 WARN_ON(raw_spin_is_locked(&kvm_count_lock));
3119                 hardware_enable_nolock(NULL);
3120         }
3121 }
3122 
3123 static struct syscore_ops kvm_syscore_ops = {
3124         .suspend = kvm_suspend,
3125         .resume = kvm_resume,
3126 };
3127 
3128 static inline
3129 struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3130 {
3131         return container_of(pn, struct kvm_vcpu, preempt_notifier);
3132 }
3133 
3134 static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3135 {
3136         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3137         if (vcpu->preempted)
3138                 vcpu->preempted = false;
3139 
3140         kvm_arch_vcpu_load(vcpu, cpu);
3141 }
3142 
3143 static void kvm_sched_out(struct preempt_notifier *pn,
3144                           struct task_struct *next)
3145 {
3146         struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3147 
3148         if (current->state == TASK_RUNNING)
3149                 vcpu->preempted = true;
3150         kvm_arch_vcpu_put(vcpu);
3151 }
3152 
3153 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
3154                   struct module *module)
3155 {
3156         int r;
3157         int cpu;
3158 
3159         r = kvm_arch_init(opaque);
3160         if (r)
3161                 goto out_fail;
3162 
3163         /*
3164          * kvm_arch_init makes sure there's at most one caller
3165          * for architectures that support multiple implementations,
3166          * like intel and amd on x86.
3167          * kvm_arch_init must be called before kvm_irqfd_init to avoid creating
3168          * conflicts in case kvm is already setup for another implementation.
3169          */
3170         r = kvm_irqfd_init();
3171         if (r)
3172                 goto out_irqfd;
3173 
3174         if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
3175                 r = -ENOMEM;
3176                 goto out_free_0;
3177         }
3178 
3179         r = kvm_arch_hardware_setup();
3180         if (r < 0)
3181                 goto out_free_0a;
3182 
3183         for_each_online_cpu(cpu) {
3184                 smp_call_function_single(cpu,
3185                                 kvm_arch_check_processor_compat,
3186                                 &r, 1);
3187                 if (r < 0)
3188                         goto out_free_1;
3189         }
3190 
3191         r = register_cpu_notifier(&kvm_cpu_notifier);
3192         if (r)
3193                 goto out_free_2;
3194         register_reboot_notifier(&kvm_reboot_notifier);
3195 
3196         /* A kmem cache lets us meet the alignment requirements of fx_save. */
3197         if (!vcpu_align)
3198                 vcpu_align = __alignof__(struct kvm_vcpu);
3199         kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
3200                                            0, NULL);
3201         if (!kvm_vcpu_cache) {
3202                 r = -ENOMEM;
3203                 goto out_free_3;
3204         }
3205 
3206         r = kvm_async_pf_init();
3207         if (r)
3208                 goto out_free;
3209 
3210         kvm_chardev_ops.owner = module;
3211         kvm_vm_fops.owner = module;
3212         kvm_vcpu_fops.owner = module;
3213 
3214         r = misc_register(&kvm_dev);
3215         if (r) {
3216                 printk(KERN_ERR "kvm: misc device register failed\n");
3217                 goto out_unreg;
3218         }
3219 
3220         register_syscore_ops(&kvm_syscore_ops);
3221 
3222         kvm_preempt_ops.sched_in = kvm_sched_in;
3223         kvm_preempt_ops.sched_out = kvm_sched_out;
3224 
3225         r = kvm_init_debug();
3226         if (r) {
3227                 printk(KERN_ERR "kvm: create debugfs files failed\n");
3228                 goto out_undebugfs;
3229         }
3230 
3231         return 0;
3232 
3233 out_undebugfs:
3234         unregister_syscore_ops(&kvm_syscore_ops);
3235         misc_deregister(&kvm_dev);
3236 out_unreg:
3237         kvm_async_pf_deinit();
3238 out_free:
3239         kmem_cache_destroy(kvm_vcpu_cache);
3240 out_free_3:
3241         unregister_reboot_notifier(&kvm_reboot_notifier);
3242         unregister_cpu_notifier(&kvm_cpu_notifier);
3243 out_free_2:
3244 out_free_1:
3245         kvm_arch_hardware_unsetup();
3246 out_free_0a:
3247         free_cpumask_var(cpus_hardware_enabled);
3248 out_free_0:
3249         kvm_irqfd_exit();
3250 out_irqfd:
3251         kvm_arch_exit();
3252 out_fail:
3253         return r;
3254 }
3255 EXPORT_SYMBOL_GPL(kvm_init);
3256 
3257 void kvm_exit(void)
3258 {
3259         kvm_exit_debug();
3260         misc_deregister(&kvm_dev);
3261         kmem_cache_destroy(kvm_vcpu_cache);
3262         kvm_async_pf_deinit();
3263         unregister_syscore_ops(&kvm_syscore_ops);
3264         unregister_reboot_notifier(&kvm_reboot_notifier);
3265         unregister_cpu_notifier(&kvm_cpu_notifier);
3266         on_each_cpu(hardware_disable_nolock, NULL, 1);
3267         kvm_arch_hardware_unsetup();
3268         kvm_arch_exit();
3269         kvm_irqfd_exit();
3270         free_cpumask_var(cpus_hardware_enabled);
3271 }
3272 EXPORT_SYMBOL_GPL(kvm_exit);
3273 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp