Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Kernel-based Virtual Machine driver for Linux |
| 4 | * |
| 5 | * This module enables machines with Intel VT-x extensions to run virtual |
| 6 | * machines without emulation or binary translation. |
| 7 | * |
| 8 | * Copyright (C) 2006 Qumranet, Inc. |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 9 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10 | * |
| 11 | * Authors: |
| 12 | * Avi Kivity <avi@qumranet.com> |
| 13 | * Yaniv Kamay <yaniv@qumranet.com> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
Andre Przywara | af669ac | 2015-03-26 14:39:29 +0000 | [diff] [blame] | 16 | #include <kvm/iodev.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 17 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 18 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | #include <linux/kvm.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/errno.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 22 | #include <linux/percpu.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 23 | #include <linux/mm.h> |
| 24 | #include <linux/miscdevice.h> |
| 25 | #include <linux/vmalloc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/reboot.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 27 | #include <linux/debugfs.h> |
| 28 | #include <linux/highmem.h> |
| 29 | #include <linux/file.h> |
Rafael J. Wysocki | fb3600c | 2011-03-23 22:16:23 +0100 | [diff] [blame] | 30 | #include <linux/syscore_ops.h> |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 31 | #include <linux/cpu.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 32 | #include <linux/sched/signal.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 33 | #include <linux/sched/mm.h> |
Ingo Molnar | 03441a3 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 34 | #include <linux/sched/stat.h> |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 35 | #include <linux/cpumask.h> |
| 36 | #include <linux/smp.h> |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 37 | #include <linux/anon_inodes.h> |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 38 | #include <linux/profile.h> |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 39 | #include <linux/kvm_para.h> |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 40 | #include <linux/pagemap.h> |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 41 | #include <linux/mman.h> |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 42 | #include <linux/swap.h> |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 43 | #include <linux/bitops.h> |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 44 | #include <linux/spinlock.h> |
Arnd Bergmann | 6ff5894 | 2009-10-22 14:19:27 +0200 | [diff] [blame] | 45 | #include <linux/compat.h> |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 46 | #include <linux/srcu.h> |
Joerg Roedel | 8f0b1ab | 2010-01-28 12:37:56 +0100 | [diff] [blame] | 47 | #include <linux/hugetlb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 48 | #include <linux/slab.h> |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 49 | #include <linux/sort.h> |
| 50 | #include <linux/bsearch.h> |
Paolo Bonzini | c011d23 | 2019-05-17 14:08:53 +0200 | [diff] [blame] | 51 | #include <linux/io.h> |
Wanpeng Li | 2eb06c3 | 2019-05-17 16:49:49 +0800 | [diff] [blame] | 52 | #include <linux/lockdep.h> |
Junaid Shahid | c57c804 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 53 | #include <linux/kthread.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 54 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 55 | #include <asm/processor.h> |
David Matlack | 2ea75be | 2014-09-19 16:03:25 -0700 | [diff] [blame] | 56 | #include <asm/ioctl.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 57 | #include <linux/uaccess.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 58 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 59 | #include "coalesced_mmio.h" |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 60 | #include "async_pf.h" |
Paolo Bonzini | 3c3c29f | 2014-09-24 13:02:46 +0200 | [diff] [blame] | 61 | #include "vfio.h" |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 62 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 63 | #define CREATE_TRACE_POINTS |
| 64 | #include <trace/events/kvm.h> |
| 65 | |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 66 | /* Worst case buffer size needed for holding an integer. */ |
| 67 | #define ITOA_MAX_LEN 12 |
| 68 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 69 | MODULE_AUTHOR("Qumranet"); |
| 70 | MODULE_LICENSE("GPL"); |
| 71 | |
David Hildenbrand | 920552b | 2015-09-18 12:34:53 +0200 | [diff] [blame] | 72 | /* Architectures should define their poll value according to the halt latency */ |
Suraj Jitindar Singh | ec76d81 | 2016-10-14 11:53:19 +1100 | [diff] [blame] | 73 | unsigned int halt_poll_ns = KVM_HALT_POLL_NS_DEFAULT; |
Roman Storozhenko | 039c5d1 | 2017-06-27 12:51:18 +0300 | [diff] [blame] | 74 | module_param(halt_poll_ns, uint, 0644); |
Suraj Jitindar Singh | ec76d81 | 2016-10-14 11:53:19 +1100 | [diff] [blame] | 75 | EXPORT_SYMBOL_GPL(halt_poll_ns); |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 76 | |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 77 | /* Default doubles per-vcpu halt_poll_ns. */ |
Suraj Jitindar Singh | ec76d81 | 2016-10-14 11:53:19 +1100 | [diff] [blame] | 78 | unsigned int halt_poll_ns_grow = 2; |
Roman Storozhenko | 039c5d1 | 2017-06-27 12:51:18 +0300 | [diff] [blame] | 79 | module_param(halt_poll_ns_grow, uint, 0644); |
Suraj Jitindar Singh | ec76d81 | 2016-10-14 11:53:19 +1100 | [diff] [blame] | 80 | EXPORT_SYMBOL_GPL(halt_poll_ns_grow); |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 81 | |
Nir Weiner | 49113d3 | 2019-01-27 12:17:15 +0200 | [diff] [blame] | 82 | /* The start value to grow halt_poll_ns from */ |
| 83 | unsigned int halt_poll_ns_grow_start = 10000; /* 10us */ |
| 84 | module_param(halt_poll_ns_grow_start, uint, 0644); |
| 85 | EXPORT_SYMBOL_GPL(halt_poll_ns_grow_start); |
| 86 | |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 87 | /* Default resets per-vcpu halt_poll_ns . */ |
Suraj Jitindar Singh | ec76d81 | 2016-10-14 11:53:19 +1100 | [diff] [blame] | 88 | unsigned int halt_poll_ns_shrink; |
Roman Storozhenko | 039c5d1 | 2017-06-27 12:51:18 +0300 | [diff] [blame] | 89 | module_param(halt_poll_ns_shrink, uint, 0644); |
Suraj Jitindar Singh | ec76d81 | 2016-10-14 11:53:19 +1100 | [diff] [blame] | 90 | EXPORT_SYMBOL_GPL(halt_poll_ns_shrink); |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 91 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 92 | /* |
| 93 | * Ordering of locks: |
| 94 | * |
Xiubo Li | b7d409d | 2015-02-26 14:58:24 +0800 | [diff] [blame] | 95 | * kvm->lock --> kvm->slots_lock --> kvm->irq_lock |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 96 | */ |
| 97 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 98 | DEFINE_MUTEX(kvm_lock); |
Paolo Bonzini | 4a937f9 | 2013-09-10 12:58:35 +0200 | [diff] [blame] | 99 | static DEFINE_RAW_SPINLOCK(kvm_count_lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 100 | LIST_HEAD(vm_list); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 101 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 102 | static cpumask_var_t cpus_hardware_enabled; |
Xiubo Li | f4fee93 | 2015-02-26 14:58:21 +0800 | [diff] [blame] | 103 | static int kvm_usage_count; |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 104 | static atomic_t hardware_enable_failed; |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 105 | |
Sean Christopherson | aaba298 | 2019-12-18 13:55:16 -0800 | [diff] [blame] | 106 | static struct kmem_cache *kvm_vcpu_cache; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 107 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 108 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
Paolo Bonzini | 7495e22 | 2020-01-09 09:57:19 -0500 | [diff] [blame] | 109 | static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_running_vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 110 | |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 111 | struct dentry *kvm_debugfs_dir; |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 112 | EXPORT_SYMBOL_GPL(kvm_debugfs_dir); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 113 | |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 114 | static int kvm_debugfs_num_entries; |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 115 | static const struct file_operations stat_fops_per_vm; |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 116 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 117 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
| 118 | unsigned long arg); |
Christian Borntraeger | de8e5d7 | 2015-02-03 09:35:15 +0100 | [diff] [blame] | 119 | #ifdef CONFIG_KVM_COMPAT |
Alexander Graf | 1dda606 | 2011-06-08 02:45:37 +0200 | [diff] [blame] | 120 | static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl, |
| 121 | unsigned long arg); |
Marc Zyngier | 7ddfd3e | 2018-06-17 10:16:21 +0100 | [diff] [blame] | 122 | #define KVM_COMPAT(c) .compat_ioctl = (c) |
| 123 | #else |
Marc Zyngier | 9cb09e7 | 2019-11-14 13:17:39 +0000 | [diff] [blame] | 124 | /* |
| 125 | * For architectures that don't implement a compat infrastructure, |
| 126 | * adopt a double line of defense: |
| 127 | * - Prevent a compat task from opening /dev/kvm |
| 128 | * - If the open has been done by a 64bit task, and the KVM fd |
| 129 | * passed to a compat task, let the ioctls fail. |
| 130 | */ |
Marc Zyngier | 7ddfd3e | 2018-06-17 10:16:21 +0100 | [diff] [blame] | 131 | static long kvm_no_compat_ioctl(struct file *file, unsigned int ioctl, |
| 132 | unsigned long arg) { return -EINVAL; } |
Marc Zyngier | b9876e6 | 2019-11-13 16:05:23 +0000 | [diff] [blame] | 133 | |
| 134 | static int kvm_no_compat_open(struct inode *inode, struct file *file) |
| 135 | { |
| 136 | return is_compat_task() ? -ENODEV : 0; |
| 137 | } |
| 138 | #define KVM_COMPAT(c) .compat_ioctl = kvm_no_compat_ioctl, \ |
| 139 | .open = kvm_no_compat_open |
Alexander Graf | 1dda606 | 2011-06-08 02:45:37 +0200 | [diff] [blame] | 140 | #endif |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 141 | static int hardware_enable_all(void); |
| 142 | static void hardware_disable_all(void); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 143 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 144 | static void kvm_io_bus_destroy(struct kvm_io_bus *bus); |
Stephen Hemminger | 7940876 | 2013-12-29 12:12:29 -0800 | [diff] [blame] | 145 | |
Andi Kleen | 5248013 | 2014-02-08 08:51:57 +0100 | [diff] [blame] | 146 | __visible bool kvm_rebooting; |
Avi Kivity | b7c4145 | 2010-12-02 17:52:50 +0200 | [diff] [blame] | 147 | EXPORT_SYMBOL_GPL(kvm_rebooting); |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 148 | |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 149 | #define KVM_EVENT_CREATE_VM 0 |
| 150 | #define KVM_EVENT_DESTROY_VM 1 |
| 151 | static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm); |
| 152 | static unsigned long long kvm_createvm_count; |
| 153 | static unsigned long long kvm_active_vms; |
| 154 | |
Eiichi Tsukata | e649b3f | 2020-06-06 13:26:27 +0900 | [diff] [blame] | 155 | __weak void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, |
| 156 | unsigned long start, unsigned long end) |
Radim Krčmář | b1394e7 | 2017-11-30 19:05:45 +0100 | [diff] [blame] | 157 | { |
| 158 | } |
| 159 | |
Sean Christopherson | a78986a | 2019-11-11 14:12:27 -0800 | [diff] [blame] | 160 | bool kvm_is_zone_device_pfn(kvm_pfn_t pfn) |
| 161 | { |
| 162 | /* |
| 163 | * The metadata used by is_zone_device_page() to determine whether or |
| 164 | * not a page is ZONE_DEVICE is guaranteed to be valid if and only if |
| 165 | * the device has been pinned, e.g. by get_user_pages(). WARN if the |
| 166 | * page_count() is zero to help detect bad usage of this helper. |
| 167 | */ |
| 168 | if (!pfn_valid(pfn) || WARN_ON_ONCE(!page_count(pfn_to_page(pfn)))) |
| 169 | return false; |
| 170 | |
| 171 | return is_zone_device_page(pfn_to_page(pfn)); |
| 172 | } |
| 173 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 174 | bool kvm_is_reserved_pfn(kvm_pfn_t pfn) |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame] | 175 | { |
Sean Christopherson | a78986a | 2019-11-11 14:12:27 -0800 | [diff] [blame] | 176 | /* |
| 177 | * ZONE_DEVICE pages currently set PG_reserved, but from a refcounting |
| 178 | * perspective they are "normal" pages, albeit with slightly different |
| 179 | * usage rules. |
| 180 | */ |
Andrea Arcangeli | 11feeb4 | 2013-07-25 03:04:38 +0200 | [diff] [blame] | 181 | if (pfn_valid(pfn)) |
Sean Christopherson | a78986a | 2019-11-11 14:12:27 -0800 | [diff] [blame] | 182 | return PageReserved(pfn_to_page(pfn)) && |
Zhuang Yanying | 7df003c | 2019-10-12 11:37:31 +0800 | [diff] [blame] | 183 | !is_zero_pfn(pfn) && |
Sean Christopherson | a78986a | 2019-11-11 14:12:27 -0800 | [diff] [blame] | 184 | !kvm_is_zone_device_pfn(pfn); |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame] | 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
Sean Christopherson | 005ba37 | 2020-01-08 12:24:36 -0800 | [diff] [blame] | 189 | bool kvm_is_transparent_hugepage(kvm_pfn_t pfn) |
| 190 | { |
| 191 | struct page *page = pfn_to_page(pfn); |
| 192 | |
| 193 | if (!PageTransCompoundMap(page)) |
| 194 | return false; |
| 195 | |
| 196 | return is_transparent_hugepage(compound_head(page)); |
| 197 | } |
| 198 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 199 | /* |
| 200 | * Switches to specified vcpu, until a matching vcpu_put() |
| 201 | */ |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 202 | void vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 203 | { |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 204 | int cpu = get_cpu(); |
Paolo Bonzini | 7495e22 | 2020-01-09 09:57:19 -0500 | [diff] [blame] | 205 | |
| 206 | __this_cpu_write(kvm_running_vcpu, vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 207 | preempt_notifier_register(&vcpu->preempt_notifier); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 208 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 209 | put_cpu(); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 210 | } |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 211 | EXPORT_SYMBOL_GPL(vcpu_load); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 212 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 213 | void vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 214 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 215 | preempt_disable(); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 216 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 217 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
Paolo Bonzini | 7495e22 | 2020-01-09 09:57:19 -0500 | [diff] [blame] | 218 | __this_cpu_write(kvm_running_vcpu, NULL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 219 | preempt_enable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 220 | } |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 221 | EXPORT_SYMBOL_GPL(vcpu_put); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 222 | |
Paolo Bonzini | 7a97cec | 2017-04-27 14:33:43 +0200 | [diff] [blame] | 223 | /* TODO: merge with kvm_arch_vcpu_should_kick */ |
| 224 | static bool kvm_request_needs_ipi(struct kvm_vcpu *vcpu, unsigned req) |
| 225 | { |
| 226 | int mode = kvm_vcpu_exiting_guest_mode(vcpu); |
| 227 | |
| 228 | /* |
| 229 | * We need to wait for the VCPU to reenable interrupts and get out of |
| 230 | * READING_SHADOW_PAGE_TABLES mode. |
| 231 | */ |
| 232 | if (req & KVM_REQUEST_WAIT) |
| 233 | return mode != OUTSIDE_GUEST_MODE; |
| 234 | |
| 235 | /* |
| 236 | * Need to kick a running VCPU, but otherwise there is nothing to do. |
| 237 | */ |
| 238 | return mode == IN_GUEST_MODE; |
| 239 | } |
| 240 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 241 | static void ack_flush(void *_completed) |
| 242 | { |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 243 | } |
| 244 | |
Paolo Bonzini | b49defe | 2017-06-30 13:25:45 +0200 | [diff] [blame] | 245 | static inline bool kvm_kick_many_cpus(const struct cpumask *cpus, bool wait) |
| 246 | { |
| 247 | if (unlikely(!cpus)) |
| 248 | cpus = cpu_online_mask; |
| 249 | |
| 250 | if (cpumask_empty(cpus)) |
| 251 | return false; |
| 252 | |
| 253 | smp_call_function_many(cpus, ack_flush, NULL, wait); |
| 254 | return true; |
| 255 | } |
| 256 | |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 257 | bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req, |
Suravee Suthikulpanit | 54163a3 | 2020-05-06 08:17:53 -0500 | [diff] [blame] | 258 | struct kvm_vcpu *except, |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 259 | unsigned long *vcpu_bitmap, cpumask_var_t tmp) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 260 | { |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 261 | int i, cpu, me; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 262 | struct kvm_vcpu *vcpu; |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 263 | bool called; |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 264 | |
Xiao Guangrong | 3cba413 | 2011-01-12 15:41:22 +0800 | [diff] [blame] | 265 | me = get_cpu(); |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 266 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 267 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Suravee Suthikulpanit | 54163a3 | 2020-05-06 08:17:53 -0500 | [diff] [blame] | 268 | if ((vcpu_bitmap && !test_bit(i, vcpu_bitmap)) || |
| 269 | vcpu == except) |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 270 | continue; |
| 271 | |
Xiao Guangrong | 3cba413 | 2011-01-12 15:41:22 +0800 | [diff] [blame] | 272 | kvm_make_request(req, vcpu); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 273 | cpu = vcpu->cpu; |
Xiao Guangrong | 6b7e2d0 | 2011-01-12 15:40:31 +0800 | [diff] [blame] | 274 | |
Radim Krčmář | 178f02f | 2017-04-26 22:32:26 +0200 | [diff] [blame] | 275 | if (!(req & KVM_REQUEST_NO_WAKEUP) && kvm_vcpu_wake_up(vcpu)) |
| 276 | continue; |
Radim Krčmář | 6c6e836 | 2017-04-26 22:32:23 +0200 | [diff] [blame] | 277 | |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 278 | if (tmp != NULL && cpu != -1 && cpu != me && |
Paolo Bonzini | 7a97cec | 2017-04-27 14:33:43 +0200 | [diff] [blame] | 279 | kvm_request_needs_ipi(vcpu, req)) |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 280 | __cpumask_set_cpu(cpu, tmp); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 281 | } |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 282 | |
| 283 | called = kvm_kick_many_cpus(tmp, !!(req & KVM_REQUEST_WAIT)); |
Xiao Guangrong | 3cba413 | 2011-01-12 15:41:22 +0800 | [diff] [blame] | 284 | put_cpu(); |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 285 | |
| 286 | return called; |
| 287 | } |
| 288 | |
Suravee Suthikulpanit | 54163a3 | 2020-05-06 08:17:53 -0500 | [diff] [blame] | 289 | bool kvm_make_all_cpus_request_except(struct kvm *kvm, unsigned int req, |
| 290 | struct kvm_vcpu *except) |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 291 | { |
| 292 | cpumask_var_t cpus; |
| 293 | bool called; |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 294 | |
| 295 | zalloc_cpumask_var(&cpus, GFP_ATOMIC); |
| 296 | |
Suravee Suthikulpanit | 54163a3 | 2020-05-06 08:17:53 -0500 | [diff] [blame] | 297 | called = kvm_make_vcpus_request_mask(kvm, req, except, NULL, cpus); |
Vitaly Kuznetsov | 7053df4 | 2018-05-16 17:21:28 +0200 | [diff] [blame] | 298 | |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 299 | free_cpumask_var(cpus); |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 300 | return called; |
| 301 | } |
| 302 | |
Suravee Suthikulpanit | 54163a3 | 2020-05-06 08:17:53 -0500 | [diff] [blame] | 303 | bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req) |
| 304 | { |
| 305 | return kvm_make_all_cpus_request_except(kvm, req, NULL); |
| 306 | } |
| 307 | |
Mario Smarduch | a6d5101 | 2015-01-15 15:58:52 -0800 | [diff] [blame] | 308 | #ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 309 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
| 310 | { |
Lan Tianyu | 4ae3cb3 | 2016-03-13 11:10:28 +0800 | [diff] [blame] | 311 | /* |
| 312 | * Read tlbs_dirty before setting KVM_REQ_TLB_FLUSH in |
| 313 | * kvm_make_all_cpus_request. |
| 314 | */ |
| 315 | long dirty_count = smp_load_acquire(&kvm->tlbs_dirty); |
Xiao Guangrong | a086f6a | 2014-04-17 17:06:12 +0800 | [diff] [blame] | 316 | |
Lan Tianyu | 4ae3cb3 | 2016-03-13 11:10:28 +0800 | [diff] [blame] | 317 | /* |
| 318 | * We want to publish modifications to the page tables before reading |
| 319 | * mode. Pairs with a memory barrier in arch-specific code. |
| 320 | * - x86: smp_mb__after_srcu_read_unlock in vcpu_enter_guest |
| 321 | * and smp_mb in walk_shadow_page_lockless_begin/end. |
| 322 | * - powerpc: smp_mb in kvmppc_prepare_to_enter. |
| 323 | * |
| 324 | * There is already an smp_mb__after_atomic() before |
| 325 | * kvm_make_all_cpus_request() reads vcpu->mode. We reuse that |
| 326 | * barrier here. |
| 327 | */ |
Tianyu Lan | b08660e | 2018-07-19 08:40:17 +0000 | [diff] [blame] | 328 | if (!kvm_arch_flush_remote_tlb(kvm) |
| 329 | || kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH)) |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 330 | ++kvm->stat.remote_tlb_flush; |
Xiao Guangrong | a086f6a | 2014-04-17 17:06:12 +0800 | [diff] [blame] | 331 | cmpxchg(&kvm->tlbs_dirty, dirty_count, 0); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 332 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 333 | EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs); |
Mario Smarduch | a6d5101 | 2015-01-15 15:58:52 -0800 | [diff] [blame] | 334 | #endif |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 335 | |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 336 | void kvm_reload_remote_mmus(struct kvm *kvm) |
| 337 | { |
Tang Chen | 445b823 | 2014-09-24 15:57:55 +0800 | [diff] [blame] | 338 | kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 339 | } |
| 340 | |
Sean Christopherson | 6926f95 | 2020-07-02 19:35:39 -0700 | [diff] [blame] | 341 | #ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE |
| 342 | static inline void *mmu_memory_cache_alloc_obj(struct kvm_mmu_memory_cache *mc, |
| 343 | gfp_t gfp_flags) |
| 344 | { |
| 345 | gfp_flags |= mc->gfp_zero; |
| 346 | |
| 347 | if (mc->kmem_cache) |
| 348 | return kmem_cache_alloc(mc->kmem_cache, gfp_flags); |
| 349 | else |
| 350 | return (void *)__get_free_page(gfp_flags); |
| 351 | } |
| 352 | |
| 353 | int kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int min) |
| 354 | { |
| 355 | void *obj; |
| 356 | |
| 357 | if (mc->nobjs >= min) |
| 358 | return 0; |
| 359 | while (mc->nobjs < ARRAY_SIZE(mc->objects)) { |
| 360 | obj = mmu_memory_cache_alloc_obj(mc, GFP_KERNEL_ACCOUNT); |
| 361 | if (!obj) |
| 362 | return mc->nobjs >= min ? 0 : -ENOMEM; |
| 363 | mc->objects[mc->nobjs++] = obj; |
| 364 | } |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | int kvm_mmu_memory_cache_nr_free_objects(struct kvm_mmu_memory_cache *mc) |
| 369 | { |
| 370 | return mc->nobjs; |
| 371 | } |
| 372 | |
| 373 | void kvm_mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc) |
| 374 | { |
| 375 | while (mc->nobjs) { |
| 376 | if (mc->kmem_cache) |
| 377 | kmem_cache_free(mc->kmem_cache, mc->objects[--mc->nobjs]); |
| 378 | else |
| 379 | free_page((unsigned long)mc->objects[--mc->nobjs]); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc) |
| 384 | { |
| 385 | void *p; |
| 386 | |
| 387 | if (WARN_ON(!mc->nobjs)) |
| 388 | p = mmu_memory_cache_alloc_obj(mc, GFP_ATOMIC | __GFP_ACCOUNT); |
| 389 | else |
| 390 | p = mc->objects[--mc->nobjs]; |
| 391 | BUG_ON(!p); |
| 392 | return p; |
| 393 | } |
| 394 | #endif |
| 395 | |
Sean Christopherson | 8bd826d | 2019-12-18 13:55:30 -0800 | [diff] [blame] | 396 | static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 397 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 398 | mutex_init(&vcpu->mutex); |
| 399 | vcpu->cpu = -1; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 400 | vcpu->kvm = kvm; |
| 401 | vcpu->vcpu_id = id; |
Rik van Riel | 34bb10b | 2011-02-01 09:52:41 -0500 | [diff] [blame] | 402 | vcpu->pid = NULL; |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 403 | rcuwait_init(&vcpu->wait); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 404 | kvm_async_pf_vcpu_init(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 405 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 406 | vcpu->pre_pcpu = -1; |
| 407 | INIT_LIST_HEAD(&vcpu->blocked_vcpu_list); |
| 408 | |
Raghavendra K T | 4c08849 | 2012-07-18 19:07:46 +0530 | [diff] [blame] | 409 | kvm_vcpu_set_in_spin_loop(vcpu, false); |
| 410 | kvm_vcpu_set_dy_eligible(vcpu, false); |
Raghavendra K T | 3a08a8f | 2013-03-04 23:32:07 +0530 | [diff] [blame] | 411 | vcpu->preempted = false; |
Wanpeng Li | d73eb57 | 2019-07-18 19:39:06 +0800 | [diff] [blame] | 412 | vcpu->ready = false; |
Sean Christopherson | d5c48de | 2019-12-18 13:55:17 -0800 | [diff] [blame] | 413 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 414 | } |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 415 | |
Sean Christopherson | 4543bdc | 2019-12-18 13:55:14 -0800 | [diff] [blame] | 416 | void kvm_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 417 | { |
| 418 | kvm_arch_vcpu_destroy(vcpu); |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 419 | |
Sean Christopherson | 9941d22 | 2019-12-18 13:55:29 -0800 | [diff] [blame] | 420 | /* |
| 421 | * No need for rcu_read_lock as VCPU_RUN is the only place that changes |
| 422 | * the vcpu->pid pointer, and at destruction time all file descriptors |
| 423 | * are already gone. |
| 424 | */ |
| 425 | put_pid(rcu_dereference_protected(vcpu->pid, 1)); |
| 426 | |
Sean Christopherson | 8bd826d | 2019-12-18 13:55:30 -0800 | [diff] [blame] | 427 | free_page((unsigned long)vcpu->run); |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 428 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
Sean Christopherson | 4543bdc | 2019-12-18 13:55:14 -0800 | [diff] [blame] | 429 | } |
| 430 | EXPORT_SYMBOL_GPL(kvm_vcpu_destroy); |
| 431 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 432 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 433 | static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) |
| 434 | { |
| 435 | return container_of(mn, struct kvm, mmu_notifier); |
| 436 | } |
| 437 | |
Eiichi Tsukata | e649b3f | 2020-06-06 13:26:27 +0900 | [diff] [blame] | 438 | static void kvm_mmu_notifier_invalidate_range(struct mmu_notifier *mn, |
| 439 | struct mm_struct *mm, |
| 440 | unsigned long start, unsigned long end) |
| 441 | { |
| 442 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 443 | int idx; |
| 444 | |
| 445 | idx = srcu_read_lock(&kvm->srcu); |
| 446 | kvm_arch_mmu_notifier_invalidate_range(kvm, start, end); |
| 447 | srcu_read_unlock(&kvm->srcu, idx); |
| 448 | } |
| 449 | |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 450 | static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, |
| 451 | struct mm_struct *mm, |
| 452 | unsigned long address, |
| 453 | pte_t pte) |
| 454 | { |
| 455 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 456 | int idx; |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 457 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 458 | idx = srcu_read_lock(&kvm->srcu); |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 459 | spin_lock(&kvm->mmu_lock); |
| 460 | kvm->mmu_notifier_seq++; |
Lan Tianyu | 0cf853c | 2018-12-06 21:21:11 +0800 | [diff] [blame] | 461 | |
| 462 | if (kvm_set_spte_hva(kvm, address, pte)) |
| 463 | kvm_flush_remote_tlbs(kvm); |
| 464 | |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 465 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 466 | srcu_read_unlock(&kvm->srcu, idx); |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 467 | } |
| 468 | |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 469 | static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, |
Jérôme Glisse | 5d6527a | 2018-12-28 00:38:05 -0800 | [diff] [blame] | 470 | const struct mmu_notifier_range *range) |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 471 | { |
| 472 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 473 | int need_tlb_flush = 0, idx; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 474 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 475 | idx = srcu_read_lock(&kvm->srcu); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 476 | spin_lock(&kvm->mmu_lock); |
| 477 | /* |
| 478 | * The count increase must become visible at unlock time as no |
| 479 | * spte can be established without taking the mmu_lock and |
| 480 | * count is also read inside the mmu_lock critical section. |
| 481 | */ |
| 482 | kvm->mmu_notifier_count++; |
Will Deacon | fdfe7cb | 2020-08-11 11:27:24 +0100 | [diff] [blame] | 483 | need_tlb_flush = kvm_unmap_hva_range(kvm, range->start, range->end, |
| 484 | range->flags); |
Xiao Guangrong | a4ee1ca | 2010-11-23 11:13:00 +0800 | [diff] [blame] | 485 | need_tlb_flush |= kvm->tlbs_dirty; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 486 | /* we've to flush the tlb before the pages can be freed */ |
| 487 | if (need_tlb_flush) |
| 488 | kvm_flush_remote_tlbs(kvm); |
Takuya Yoshikawa | 565f3be | 2012-02-10 15:28:31 +0900 | [diff] [blame] | 489 | |
| 490 | spin_unlock(&kvm->mmu_lock); |
| 491 | srcu_read_unlock(&kvm->srcu, idx); |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 492 | |
Eiichi Tsukata | e649b3f | 2020-06-06 13:26:27 +0900 | [diff] [blame] | 493 | return 0; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, |
Jérôme Glisse | 5d6527a | 2018-12-28 00:38:05 -0800 | [diff] [blame] | 497 | const struct mmu_notifier_range *range) |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 498 | { |
| 499 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 500 | |
| 501 | spin_lock(&kvm->mmu_lock); |
| 502 | /* |
| 503 | * This sequence increase will notify the kvm page fault that |
| 504 | * the page that is going to be mapped in the spte could have |
| 505 | * been freed. |
| 506 | */ |
| 507 | kvm->mmu_notifier_seq++; |
Paul Mackerras | a355aa5 | 2011-12-12 12:37:21 +0000 | [diff] [blame] | 508 | smp_wmb(); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 509 | /* |
| 510 | * The above sequence increase must be visible before the |
Paul Mackerras | a355aa5 | 2011-12-12 12:37:21 +0000 | [diff] [blame] | 511 | * below count decrease, which is ensured by the smp_wmb above |
| 512 | * in conjunction with the smp_rmb in mmu_notifier_retry(). |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 513 | */ |
| 514 | kvm->mmu_notifier_count--; |
| 515 | spin_unlock(&kvm->mmu_lock); |
| 516 | |
| 517 | BUG_ON(kvm->mmu_notifier_count < 0); |
| 518 | } |
| 519 | |
| 520 | static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, |
| 521 | struct mm_struct *mm, |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 522 | unsigned long start, |
| 523 | unsigned long end) |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 524 | { |
| 525 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 526 | int young, idx; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 527 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 528 | idx = srcu_read_lock(&kvm->srcu); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 529 | spin_lock(&kvm->mmu_lock); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 530 | |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 531 | young = kvm_age_hva(kvm, start, end); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 532 | if (young) |
| 533 | kvm_flush_remote_tlbs(kvm); |
| 534 | |
Takuya Yoshikawa | 565f3be | 2012-02-10 15:28:31 +0900 | [diff] [blame] | 535 | spin_unlock(&kvm->mmu_lock); |
| 536 | srcu_read_unlock(&kvm->srcu, idx); |
| 537 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 538 | return young; |
| 539 | } |
| 540 | |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 541 | static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn, |
| 542 | struct mm_struct *mm, |
| 543 | unsigned long start, |
| 544 | unsigned long end) |
| 545 | { |
| 546 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 547 | int young, idx; |
| 548 | |
| 549 | idx = srcu_read_lock(&kvm->srcu); |
| 550 | spin_lock(&kvm->mmu_lock); |
| 551 | /* |
| 552 | * Even though we do not flush TLB, this will still adversely |
| 553 | * affect performance on pre-Haswell Intel EPT, where there is |
| 554 | * no EPT Access Bit to clear so that we have to tear down EPT |
| 555 | * tables instead. If we find this unacceptable, we can always |
| 556 | * add a parameter to kvm_age_hva so that it effectively doesn't |
| 557 | * do anything on clear_young. |
| 558 | * |
| 559 | * Also note that currently we never issue secondary TLB flushes |
| 560 | * from clear_young, leaving this job up to the regular system |
| 561 | * cadence. If we find this inaccurate, we might come up with a |
| 562 | * more sophisticated heuristic later. |
| 563 | */ |
| 564 | young = kvm_age_hva(kvm, start, end); |
| 565 | spin_unlock(&kvm->mmu_lock); |
| 566 | srcu_read_unlock(&kvm->srcu, idx); |
| 567 | |
| 568 | return young; |
| 569 | } |
| 570 | |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 571 | static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn, |
| 572 | struct mm_struct *mm, |
| 573 | unsigned long address) |
| 574 | { |
| 575 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 576 | int young, idx; |
| 577 | |
| 578 | idx = srcu_read_lock(&kvm->srcu); |
| 579 | spin_lock(&kvm->mmu_lock); |
| 580 | young = kvm_test_age_hva(kvm, address); |
| 581 | spin_unlock(&kvm->mmu_lock); |
| 582 | srcu_read_unlock(&kvm->srcu, idx); |
| 583 | |
| 584 | return young; |
| 585 | } |
| 586 | |
Marcelo Tosatti | 85db06e | 2008-12-10 21:23:26 +0100 | [diff] [blame] | 587 | static void kvm_mmu_notifier_release(struct mmu_notifier *mn, |
| 588 | struct mm_struct *mm) |
| 589 | { |
| 590 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
Lai Jiangshan | eda2bed | 2010-04-20 14:29:29 +0800 | [diff] [blame] | 591 | int idx; |
| 592 | |
| 593 | idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 594 | kvm_arch_flush_shadow_all(kvm); |
Lai Jiangshan | eda2bed | 2010-04-20 14:29:29 +0800 | [diff] [blame] | 595 | srcu_read_unlock(&kvm->srcu, idx); |
Marcelo Tosatti | 85db06e | 2008-12-10 21:23:26 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 598 | static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { |
Eiichi Tsukata | e649b3f | 2020-06-06 13:26:27 +0900 | [diff] [blame] | 599 | .invalidate_range = kvm_mmu_notifier_invalidate_range, |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 600 | .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start, |
| 601 | .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end, |
| 602 | .clear_flush_young = kvm_mmu_notifier_clear_flush_young, |
Vladimir Davydov | 1d7715c | 2015-09-09 15:35:41 -0700 | [diff] [blame] | 603 | .clear_young = kvm_mmu_notifier_clear_young, |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 604 | .test_young = kvm_mmu_notifier_test_young, |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 605 | .change_pte = kvm_mmu_notifier_change_pte, |
Marcelo Tosatti | 85db06e | 2008-12-10 21:23:26 +0100 | [diff] [blame] | 606 | .release = kvm_mmu_notifier_release, |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 607 | }; |
Avi Kivity | 4c07b0a | 2009-12-20 14:54:04 +0200 | [diff] [blame] | 608 | |
| 609 | static int kvm_init_mmu_notifier(struct kvm *kvm) |
| 610 | { |
| 611 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; |
| 612 | return mmu_notifier_register(&kvm->mmu_notifier, current->mm); |
| 613 | } |
| 614 | |
| 615 | #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */ |
| 616 | |
| 617 | static int kvm_init_mmu_notifier(struct kvm *kvm) |
| 618 | { |
| 619 | return 0; |
| 620 | } |
| 621 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 622 | #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ |
| 623 | |
Paolo Bonzini | a47d2b0 | 2015-05-17 11:41:37 +0200 | [diff] [blame] | 624 | static struct kvm_memslots *kvm_alloc_memslots(void) |
Xiao Guangrong | bf3e05b | 2011-11-24 17:40:57 +0800 | [diff] [blame] | 625 | { |
| 626 | int i; |
Paolo Bonzini | a47d2b0 | 2015-05-17 11:41:37 +0200 | [diff] [blame] | 627 | struct kvm_memslots *slots; |
Xiao Guangrong | bf3e05b | 2011-11-24 17:40:57 +0800 | [diff] [blame] | 628 | |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 629 | slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL_ACCOUNT); |
Paolo Bonzini | a47d2b0 | 2015-05-17 11:41:37 +0200 | [diff] [blame] | 630 | if (!slots) |
| 631 | return NULL; |
| 632 | |
Xiao Guangrong | bf3e05b | 2011-11-24 17:40:57 +0800 | [diff] [blame] | 633 | for (i = 0; i < KVM_MEM_SLOTS_NUM; i++) |
Sean Christopherson | 3694725 | 2020-02-18 13:07:32 -0800 | [diff] [blame] | 634 | slots->id_to_index[i] = -1; |
Paolo Bonzini | a47d2b0 | 2015-05-17 11:41:37 +0200 | [diff] [blame] | 635 | |
| 636 | return slots; |
| 637 | } |
| 638 | |
| 639 | static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot) |
| 640 | { |
| 641 | if (!memslot->dirty_bitmap) |
| 642 | return; |
| 643 | |
| 644 | kvfree(memslot->dirty_bitmap); |
| 645 | memslot->dirty_bitmap = NULL; |
| 646 | } |
| 647 | |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 648 | static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) |
Paolo Bonzini | a47d2b0 | 2015-05-17 11:41:37 +0200 | [diff] [blame] | 649 | { |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 650 | kvm_destroy_dirty_bitmap(slot); |
Paolo Bonzini | a47d2b0 | 2015-05-17 11:41:37 +0200 | [diff] [blame] | 651 | |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 652 | kvm_arch_free_memslot(kvm, slot); |
Paolo Bonzini | a47d2b0 | 2015-05-17 11:41:37 +0200 | [diff] [blame] | 653 | |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 654 | slot->flags = 0; |
| 655 | slot->npages = 0; |
Paolo Bonzini | a47d2b0 | 2015-05-17 11:41:37 +0200 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots) |
| 659 | { |
| 660 | struct kvm_memory_slot *memslot; |
| 661 | |
| 662 | if (!slots) |
| 663 | return; |
| 664 | |
| 665 | kvm_for_each_memslot(memslot, slots) |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 666 | kvm_free_memslot(kvm, memslot); |
Paolo Bonzini | a47d2b0 | 2015-05-17 11:41:37 +0200 | [diff] [blame] | 667 | |
| 668 | kvfree(slots); |
Xiao Guangrong | bf3e05b | 2011-11-24 17:40:57 +0800 | [diff] [blame] | 669 | } |
| 670 | |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 671 | static void kvm_destroy_vm_debugfs(struct kvm *kvm) |
| 672 | { |
| 673 | int i; |
| 674 | |
| 675 | if (!kvm->debugfs_dentry) |
| 676 | return; |
| 677 | |
| 678 | debugfs_remove_recursive(kvm->debugfs_dentry); |
| 679 | |
Luiz Capitulino | 9d5a1dc | 2016-09-07 14:47:21 -0400 | [diff] [blame] | 680 | if (kvm->debugfs_stat_data) { |
| 681 | for (i = 0; i < kvm_debugfs_num_entries; i++) |
| 682 | kfree(kvm->debugfs_stat_data[i]); |
| 683 | kfree(kvm->debugfs_stat_data); |
| 684 | } |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) |
| 688 | { |
| 689 | char dir_name[ITOA_MAX_LEN * 2]; |
| 690 | struct kvm_stat_data *stat_data; |
| 691 | struct kvm_stats_debugfs_item *p; |
| 692 | |
| 693 | if (!debugfs_initialized()) |
| 694 | return 0; |
| 695 | |
| 696 | snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd); |
Greg Kroah-Hartman | 929f45e | 2018-05-29 18:22:04 +0200 | [diff] [blame] | 697 | kvm->debugfs_dentry = debugfs_create_dir(dir_name, kvm_debugfs_dir); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 698 | |
| 699 | kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries, |
| 700 | sizeof(*kvm->debugfs_stat_data), |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 701 | GFP_KERNEL_ACCOUNT); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 702 | if (!kvm->debugfs_stat_data) |
| 703 | return -ENOMEM; |
| 704 | |
| 705 | for (p = debugfs_entries; p->name; p++) { |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 706 | stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 707 | if (!stat_data) |
| 708 | return -ENOMEM; |
| 709 | |
| 710 | stat_data->kvm = kvm; |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 711 | stat_data->dbgfs_item = p; |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 712 | kvm->debugfs_stat_data[p - debugfs_entries] = stat_data; |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 713 | debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p), |
| 714 | kvm->debugfs_dentry, stat_data, |
| 715 | &stat_fops_per_vm); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 716 | } |
| 717 | return 0; |
| 718 | } |
| 719 | |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 720 | /* |
| 721 | * Called after the VM is otherwise initialized, but just before adding it to |
| 722 | * the vm_list. |
| 723 | */ |
| 724 | int __weak kvm_arch_post_init_vm(struct kvm *kvm) |
| 725 | { |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * Called just after removing the VM from the vm_list, but before doing any |
| 731 | * other destruction. |
| 732 | */ |
| 733 | void __weak kvm_arch_pre_destroy_vm(struct kvm *kvm) |
| 734 | { |
| 735 | } |
| 736 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 737 | static struct kvm *kvm_create_vm(unsigned long type) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 738 | { |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 739 | struct kvm *kvm = kvm_arch_alloc_vm(); |
Jim Mattson | 9121923 | 2019-10-24 16:03:26 -0700 | [diff] [blame] | 740 | int r = -ENOMEM; |
| 741 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 742 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 743 | if (!kvm) |
| 744 | return ERR_PTR(-ENOMEM); |
| 745 | |
Paolo Bonzini | e9ad4ec | 2016-03-21 10:15:25 +0100 | [diff] [blame] | 746 | spin_lock_init(&kvm->mmu_lock); |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 747 | mmgrab(current->mm); |
Paolo Bonzini | e9ad4ec | 2016-03-21 10:15:25 +0100 | [diff] [blame] | 748 | kvm->mm = current->mm; |
| 749 | kvm_eventfd_init(kvm); |
| 750 | mutex_init(&kvm->lock); |
| 751 | mutex_init(&kvm->irq_lock); |
| 752 | mutex_init(&kvm->slots_lock); |
Paolo Bonzini | e9ad4ec | 2016-03-21 10:15:25 +0100 | [diff] [blame] | 753 | INIT_LIST_HEAD(&kvm->devices); |
| 754 | |
Jim Mattson | 9121923 | 2019-10-24 16:03:26 -0700 | [diff] [blame] | 755 | BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); |
| 756 | |
Paolo Bonzini | 8a44119 | 2019-11-04 12:16:49 +0100 | [diff] [blame] | 757 | if (init_srcu_struct(&kvm->srcu)) |
| 758 | goto out_err_no_srcu; |
| 759 | if (init_srcu_struct(&kvm->irq_srcu)) |
| 760 | goto out_err_no_irq_srcu; |
| 761 | |
Paolo Bonzini | e2d3fca | 2019-11-04 13:23:53 +0100 | [diff] [blame] | 762 | refcount_set(&kvm->users_count, 1); |
Jim Mattson | 9121923 | 2019-10-24 16:03:26 -0700 | [diff] [blame] | 763 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
| 764 | struct kvm_memslots *slots = kvm_alloc_memslots(); |
| 765 | |
| 766 | if (!slots) |
Jim Mattson | a97b0e7 | 2019-10-25 13:34:58 +0200 | [diff] [blame] | 767 | goto out_err_no_arch_destroy_vm; |
Jim Mattson | 9121923 | 2019-10-24 16:03:26 -0700 | [diff] [blame] | 768 | /* Generations must be different for each address space. */ |
| 769 | slots->generation = i; |
| 770 | rcu_assign_pointer(kvm->memslots[i], slots); |
| 771 | } |
| 772 | |
| 773 | for (i = 0; i < KVM_NR_BUSES; i++) { |
| 774 | rcu_assign_pointer(kvm->buses[i], |
| 775 | kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT)); |
| 776 | if (!kvm->buses[i]) |
Jim Mattson | a97b0e7 | 2019-10-25 13:34:58 +0200 | [diff] [blame] | 777 | goto out_err_no_arch_destroy_vm; |
Jim Mattson | 9121923 | 2019-10-24 16:03:26 -0700 | [diff] [blame] | 778 | } |
| 779 | |
David Matlack | acd0578 | 2020-04-17 15:14:46 -0700 | [diff] [blame] | 780 | kvm->max_halt_poll_ns = halt_poll_ns; |
| 781 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 782 | r = kvm_arch_init_vm(kvm, type); |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 783 | if (r) |
Jim Mattson | a97b0e7 | 2019-10-25 13:34:58 +0200 | [diff] [blame] | 784 | goto out_err_no_arch_destroy_vm; |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 785 | |
| 786 | r = hardware_enable_all(); |
| 787 | if (r) |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 788 | goto out_err_no_disable; |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 789 | |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 790 | #ifdef CONFIG_HAVE_KVM_IRQFD |
Gleb Natapov | 136bdfe | 2009-08-24 11:54:23 +0300 | [diff] [blame] | 791 | INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 792 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 793 | |
Mike Waychison | 74b5c5b | 2011-06-03 13:04:53 -0700 | [diff] [blame] | 794 | r = kvm_init_mmu_notifier(kvm); |
| 795 | if (r) |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 796 | goto out_err_no_mmu_notifier; |
| 797 | |
| 798 | r = kvm_arch_post_init_vm(kvm); |
| 799 | if (r) |
Mike Waychison | 74b5c5b | 2011-06-03 13:04:53 -0700 | [diff] [blame] | 800 | goto out_err; |
| 801 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 802 | mutex_lock(&kvm_lock); |
Rusty Russell | 5e58cfe | 2007-07-23 17:08:21 +1000 | [diff] [blame] | 803 | list_add(&kvm->vm_list, &vm_list); |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 804 | mutex_unlock(&kvm_lock); |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 805 | |
Peter Zijlstra | 2ecd9d2 | 2015-07-03 18:53:58 +0200 | [diff] [blame] | 806 | preempt_notifier_inc(); |
| 807 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 808 | return kvm; |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 809 | |
| 810 | out_err: |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 811 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 812 | if (kvm->mmu_notifier.ops) |
| 813 | mmu_notifier_unregister(&kvm->mmu_notifier, current->mm); |
| 814 | #endif |
| 815 | out_err_no_mmu_notifier: |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 816 | hardware_disable_all(); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 817 | out_err_no_disable: |
Jim Mattson | a97b0e7 | 2019-10-25 13:34:58 +0200 | [diff] [blame] | 818 | kvm_arch_destroy_vm(kvm); |
Jim Mattson | a97b0e7 | 2019-10-25 13:34:58 +0200 | [diff] [blame] | 819 | out_err_no_arch_destroy_vm: |
Paolo Bonzini | e2d3fca | 2019-11-04 13:23:53 +0100 | [diff] [blame] | 820 | WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count)); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 821 | for (i = 0; i < KVM_NR_BUSES; i++) |
Paolo Bonzini | 3898da9 | 2017-08-02 17:55:54 +0200 | [diff] [blame] | 822 | kfree(kvm_get_bus(kvm, i)); |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 823 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) |
Paolo Bonzini | 3898da9 | 2017-08-02 17:55:54 +0200 | [diff] [blame] | 824 | kvm_free_memslots(kvm, __kvm_memslots(kvm, i)); |
Paolo Bonzini | 8a44119 | 2019-11-04 12:16:49 +0100 | [diff] [blame] | 825 | cleanup_srcu_struct(&kvm->irq_srcu); |
| 826 | out_err_no_irq_srcu: |
| 827 | cleanup_srcu_struct(&kvm->srcu); |
| 828 | out_err_no_srcu: |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 829 | kvm_arch_free_vm(kvm); |
Paolo Bonzini | e9ad4ec | 2016-03-21 10:15:25 +0100 | [diff] [blame] | 830 | mmdrop(current->mm); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 831 | return ERR_PTR(r); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Scott Wood | 07f0a7b | 2013-04-25 14:11:23 +0000 | [diff] [blame] | 834 | static void kvm_destroy_devices(struct kvm *kvm) |
| 835 | { |
Geliang Tang | e6e3b5a | 2016-01-01 19:47:12 +0800 | [diff] [blame] | 836 | struct kvm_device *dev, *tmp; |
Scott Wood | 07f0a7b | 2013-04-25 14:11:23 +0000 | [diff] [blame] | 837 | |
Christoffer Dall | a28ebea | 2016-08-09 19:13:01 +0200 | [diff] [blame] | 838 | /* |
| 839 | * We do not need to take the kvm->lock here, because nobody else |
| 840 | * has a reference to the struct kvm at this point and therefore |
| 841 | * cannot access the devices list anyhow. |
| 842 | */ |
Geliang Tang | e6e3b5a | 2016-01-01 19:47:12 +0800 | [diff] [blame] | 843 | list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) { |
| 844 | list_del(&dev->vm_node); |
Scott Wood | 07f0a7b | 2013-04-25 14:11:23 +0000 | [diff] [blame] | 845 | dev->ops->destroy(dev); |
| 846 | } |
| 847 | } |
| 848 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 849 | static void kvm_destroy_vm(struct kvm *kvm) |
| 850 | { |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 851 | int i; |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 852 | struct mm_struct *mm = kvm->mm; |
| 853 | |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 854 | kvm_uevent_notify_change(KVM_EVENT_DESTROY_VM, kvm); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 855 | kvm_destroy_vm_debugfs(kvm); |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 856 | kvm_arch_sync_events(kvm); |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 857 | mutex_lock(&kvm_lock); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 858 | list_del(&kvm->vm_list); |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 859 | mutex_unlock(&kvm_lock); |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 860 | kvm_arch_pre_destroy_vm(kvm); |
| 861 | |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 862 | kvm_free_irq_routing(kvm); |
Peter Xu | df630b8 | 2017-03-15 16:01:17 +0800 | [diff] [blame] | 863 | for (i = 0; i < KVM_NR_BUSES; i++) { |
Paolo Bonzini | 3898da9 | 2017-08-02 17:55:54 +0200 | [diff] [blame] | 864 | struct kvm_io_bus *bus = kvm_get_bus(kvm, i); |
Christian Borntraeger | 4a12f95 | 2017-07-07 10:51:38 +0200 | [diff] [blame] | 865 | |
Christian Borntraeger | 4a12f95 | 2017-07-07 10:51:38 +0200 | [diff] [blame] | 866 | if (bus) |
| 867 | kvm_io_bus_destroy(bus); |
Peter Xu | df630b8 | 2017-03-15 16:01:17 +0800 | [diff] [blame] | 868 | kvm->buses[i] = NULL; |
| 869 | } |
Avi Kivity | 980da6c | 2009-12-20 15:13:43 +0200 | [diff] [blame] | 870 | kvm_coalesced_mmio_free(kvm); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 871 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 872 | mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); |
Gleb Natapov | f00be0c | 2009-03-19 12:20:36 +0200 | [diff] [blame] | 873 | #else |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 874 | kvm_arch_flush_shadow_all(kvm); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 875 | #endif |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 876 | kvm_arch_destroy_vm(kvm); |
Scott Wood | 07f0a7b | 2013-04-25 14:11:23 +0000 | [diff] [blame] | 877 | kvm_destroy_devices(kvm); |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 878 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) |
Paolo Bonzini | 3898da9 | 2017-08-02 17:55:54 +0200 | [diff] [blame] | 879 | kvm_free_memslots(kvm, __kvm_memslots(kvm, i)); |
Paolo Bonzini | 820b3fc | 2014-06-03 13:44:17 +0200 | [diff] [blame] | 880 | cleanup_srcu_struct(&kvm->irq_srcu); |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 881 | cleanup_srcu_struct(&kvm->srcu); |
| 882 | kvm_arch_free_vm(kvm); |
Peter Zijlstra | 2ecd9d2 | 2015-07-03 18:53:58 +0200 | [diff] [blame] | 883 | preempt_notifier_dec(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 884 | hardware_disable_all(); |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 885 | mmdrop(mm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 886 | } |
| 887 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 888 | void kvm_get_kvm(struct kvm *kvm) |
| 889 | { |
Elena Reshetova | e3736c3 | 2017-02-20 13:06:21 +0200 | [diff] [blame] | 890 | refcount_inc(&kvm->users_count); |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 891 | } |
| 892 | EXPORT_SYMBOL_GPL(kvm_get_kvm); |
| 893 | |
| 894 | void kvm_put_kvm(struct kvm *kvm) |
| 895 | { |
Elena Reshetova | e3736c3 | 2017-02-20 13:06:21 +0200 | [diff] [blame] | 896 | if (refcount_dec_and_test(&kvm->users_count)) |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 897 | kvm_destroy_vm(kvm); |
| 898 | } |
| 899 | EXPORT_SYMBOL_GPL(kvm_put_kvm); |
| 900 | |
Sean Christopherson | 149487b | 2019-10-21 15:58:42 -0700 | [diff] [blame] | 901 | /* |
| 902 | * Used to put a reference that was taken on behalf of an object associated |
| 903 | * with a user-visible file descriptor, e.g. a vcpu or device, if installation |
| 904 | * of the new file descriptor fails and the reference cannot be transferred to |
| 905 | * its final owner. In such cases, the caller is still actively using @kvm and |
| 906 | * will fail miserably if the refcount unexpectedly hits zero. |
| 907 | */ |
| 908 | void kvm_put_kvm_no_destroy(struct kvm *kvm) |
| 909 | { |
| 910 | WARN_ON(refcount_dec_and_test(&kvm->users_count)); |
| 911 | } |
| 912 | EXPORT_SYMBOL_GPL(kvm_put_kvm_no_destroy); |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 913 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 914 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
| 915 | { |
| 916 | struct kvm *kvm = filp->private_data; |
| 917 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 918 | kvm_irqfd_release(kvm); |
| 919 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 920 | kvm_put_kvm(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 921 | return 0; |
| 922 | } |
| 923 | |
Takuya Yoshikawa | 515a012 | 2010-10-27 18:23:54 +0900 | [diff] [blame] | 924 | /* |
| 925 | * Allocation size is twice as large as the actual dirty bitmap size. |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 926 | * See kvm_vm_ioctl_get_dirty_log() why this is needed. |
Takuya Yoshikawa | 515a012 | 2010-10-27 18:23:54 +0900 | [diff] [blame] | 927 | */ |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 928 | static int kvm_alloc_dirty_bitmap(struct kvm_memory_slot *memslot) |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 929 | { |
Takuya Yoshikawa | 515a012 | 2010-10-27 18:23:54 +0900 | [diff] [blame] | 930 | unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot); |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 931 | |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 932 | memslot->dirty_bitmap = kvzalloc(dirty_bytes, GFP_KERNEL_ACCOUNT); |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 933 | if (!memslot->dirty_bitmap) |
| 934 | return -ENOMEM; |
| 935 | |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 936 | return 0; |
| 937 | } |
| 938 | |
Xiao Guangrong | bf3e05b | 2011-11-24 17:40:57 +0800 | [diff] [blame] | 939 | /* |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 940 | * Delete a memslot by decrementing the number of used slots and shifting all |
| 941 | * other entries in the array forward one spot. |
Xiao Guangrong | bf3e05b | 2011-11-24 17:40:57 +0800 | [diff] [blame] | 942 | */ |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 943 | static inline void kvm_memslot_delete(struct kvm_memslots *slots, |
| 944 | struct kvm_memory_slot *memslot) |
Xiao Guangrong | bf3e05b | 2011-11-24 17:40:57 +0800 | [diff] [blame] | 945 | { |
Igor Mammedov | 063584d | 2014-11-13 23:00:13 +0000 | [diff] [blame] | 946 | struct kvm_memory_slot *mslots = slots->memslots; |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 947 | int i; |
Xiao Guangrong | f85e2cb | 2011-11-24 17:41:54 +0800 | [diff] [blame] | 948 | |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 949 | if (WARN_ON(slots->id_to_index[memslot->id] == -1)) |
| 950 | return; |
Igor Mammedov | 0e60b07 | 2014-12-01 17:29:26 +0000 | [diff] [blame] | 951 | |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 952 | slots->used_slots--; |
| 953 | |
Sean Christopherson | 0774a96 | 2020-03-20 13:55:40 -0700 | [diff] [blame] | 954 | if (atomic_read(&slots->lru_slot) >= slots->used_slots) |
| 955 | atomic_set(&slots->lru_slot, 0); |
| 956 | |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 957 | for (i = slots->id_to_index[memslot->id]; i < slots->used_slots; i++) { |
Igor Mammedov | 7f379cf | 2014-12-01 17:29:24 +0000 | [diff] [blame] | 958 | mslots[i] = mslots[i + 1]; |
| 959 | slots->id_to_index[mslots[i].id] = i; |
Igor Mammedov | 7f379cf | 2014-12-01 17:29:24 +0000 | [diff] [blame] | 960 | } |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 961 | mslots[i] = *memslot; |
| 962 | slots->id_to_index[memslot->id] = -1; |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * "Insert" a new memslot by incrementing the number of used slots. Returns |
| 967 | * the new slot's initial index into the memslots array. |
| 968 | */ |
| 969 | static inline int kvm_memslot_insert_back(struct kvm_memslots *slots) |
| 970 | { |
| 971 | return slots->used_slots++; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * Move a changed memslot backwards in the array by shifting existing slots |
| 976 | * with a higher GFN toward the front of the array. Note, the changed memslot |
| 977 | * itself is not preserved in the array, i.e. not swapped at this time, only |
| 978 | * its new index into the array is tracked. Returns the changed memslot's |
| 979 | * current index into the memslots array. |
| 980 | */ |
| 981 | static inline int kvm_memslot_move_backward(struct kvm_memslots *slots, |
| 982 | struct kvm_memory_slot *memslot) |
| 983 | { |
| 984 | struct kvm_memory_slot *mslots = slots->memslots; |
| 985 | int i; |
| 986 | |
| 987 | if (WARN_ON_ONCE(slots->id_to_index[memslot->id] == -1) || |
| 988 | WARN_ON_ONCE(!slots->used_slots)) |
| 989 | return -1; |
Paolo Bonzini | efbeec7 | 2014-12-27 18:01:00 +0100 | [diff] [blame] | 990 | |
| 991 | /* |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 992 | * Move the target memslot backward in the array by shifting existing |
| 993 | * memslots with a higher GFN (than the target memslot) towards the |
| 994 | * front of the array. |
Paolo Bonzini | efbeec7 | 2014-12-27 18:01:00 +0100 | [diff] [blame] | 995 | */ |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 996 | for (i = slots->id_to_index[memslot->id]; i < slots->used_slots - 1; i++) { |
| 997 | if (memslot->base_gfn > mslots[i + 1].base_gfn) |
| 998 | break; |
Xiao Guangrong | f85e2cb | 2011-11-24 17:41:54 +0800 | [diff] [blame] | 999 | |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 1000 | WARN_ON_ONCE(memslot->base_gfn == mslots[i + 1].base_gfn); |
| 1001 | |
| 1002 | /* Shift the next memslot forward one and update its index. */ |
| 1003 | mslots[i] = mslots[i + 1]; |
| 1004 | slots->id_to_index[mslots[i].id] = i; |
| 1005 | } |
| 1006 | return i; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * Move a changed memslot forwards in the array by shifting existing slots with |
| 1011 | * a lower GFN toward the back of the array. Note, the changed memslot itself |
| 1012 | * is not preserved in the array, i.e. not swapped at this time, only its new |
| 1013 | * index into the array is tracked. Returns the changed memslot's final index |
| 1014 | * into the memslots array. |
| 1015 | */ |
| 1016 | static inline int kvm_memslot_move_forward(struct kvm_memslots *slots, |
| 1017 | struct kvm_memory_slot *memslot, |
| 1018 | int start) |
| 1019 | { |
| 1020 | struct kvm_memory_slot *mslots = slots->memslots; |
| 1021 | int i; |
| 1022 | |
| 1023 | for (i = start; i > 0; i--) { |
| 1024 | if (memslot->base_gfn < mslots[i - 1].base_gfn) |
| 1025 | break; |
| 1026 | |
| 1027 | WARN_ON_ONCE(memslot->base_gfn == mslots[i - 1].base_gfn); |
| 1028 | |
| 1029 | /* Shift the next memslot back one and update its index. */ |
| 1030 | mslots[i] = mslots[i - 1]; |
| 1031 | slots->id_to_index[mslots[i].id] = i; |
| 1032 | } |
| 1033 | return i; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * Re-sort memslots based on their GFN to account for an added, deleted, or |
| 1038 | * moved memslot. Sorting memslots by GFN allows using a binary search during |
| 1039 | * memslot lookup. |
| 1040 | * |
| 1041 | * IMPORTANT: Slots are sorted from highest GFN to lowest GFN! I.e. the entry |
| 1042 | * at memslots[0] has the highest GFN. |
| 1043 | * |
| 1044 | * The sorting algorithm takes advantage of having initially sorted memslots |
| 1045 | * and knowing the position of the changed memslot. Sorting is also optimized |
| 1046 | * by not swapping the updated memslot and instead only shifting other memslots |
| 1047 | * and tracking the new index for the update memslot. Only once its final |
| 1048 | * index is known is the updated memslot copied into its position in the array. |
| 1049 | * |
| 1050 | * - When deleting a memslot, the deleted memslot simply needs to be moved to |
| 1051 | * the end of the array. |
| 1052 | * |
| 1053 | * - When creating a memslot, the algorithm "inserts" the new memslot at the |
| 1054 | * end of the array and then it forward to its correct location. |
| 1055 | * |
| 1056 | * - When moving a memslot, the algorithm first moves the updated memslot |
| 1057 | * backward to handle the scenario where the memslot's GFN was changed to a |
| 1058 | * lower value. update_memslots() then falls through and runs the same flow |
| 1059 | * as creating a memslot to move the memslot forward to handle the scenario |
| 1060 | * where its GFN was changed to a higher value. |
| 1061 | * |
| 1062 | * Note, slots are sorted from highest->lowest instead of lowest->highest for |
| 1063 | * historical reasons. Originally, invalid memslots where denoted by having |
| 1064 | * GFN=0, thus sorting from highest->lowest naturally sorted invalid memslots |
| 1065 | * to the end of the array. The current algorithm uses dedicated logic to |
| 1066 | * delete a memslot and thus does not rely on invalid memslots having GFN=0. |
| 1067 | * |
| 1068 | * The other historical motiviation for highest->lowest was to improve the |
| 1069 | * performance of memslot lookup. KVM originally used a linear search starting |
| 1070 | * at memslots[0]. On x86, the largest memslot usually has one of the highest, |
| 1071 | * if not *the* highest, GFN, as the bulk of the guest's RAM is located in a |
| 1072 | * single memslot above the 4gb boundary. As the largest memslot is also the |
| 1073 | * most likely to be referenced, sorting it to the front of the array was |
| 1074 | * advantageous. The current binary search starts from the middle of the array |
| 1075 | * and uses an LRU pointer to improve performance for all memslots and GFNs. |
| 1076 | */ |
| 1077 | static void update_memslots(struct kvm_memslots *slots, |
| 1078 | struct kvm_memory_slot *memslot, |
| 1079 | enum kvm_mr_change change) |
| 1080 | { |
| 1081 | int i; |
| 1082 | |
| 1083 | if (change == KVM_MR_DELETE) { |
| 1084 | kvm_memslot_delete(slots, memslot); |
| 1085 | } else { |
| 1086 | if (change == KVM_MR_CREATE) |
| 1087 | i = kvm_memslot_insert_back(slots); |
| 1088 | else |
| 1089 | i = kvm_memslot_move_backward(slots, memslot); |
| 1090 | i = kvm_memslot_move_forward(slots, memslot, i); |
| 1091 | |
| 1092 | /* |
| 1093 | * Copy the memslot to its new position in memslots and update |
| 1094 | * its index accordingly. |
| 1095 | */ |
| 1096 | slots->memslots[i] = *memslot; |
| 1097 | slots->id_to_index[memslot->id] = i; |
| 1098 | } |
Xiao Guangrong | bf3e05b | 2011-11-24 17:40:57 +0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 1101 | static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem) |
Xiao Guangrong | a50d64d | 2012-08-21 10:58:13 +0800 | [diff] [blame] | 1102 | { |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1103 | u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; |
| 1104 | |
Christoffer Dall | 0f8a4de | 2014-08-26 14:00:37 +0200 | [diff] [blame] | 1105 | #ifdef __KVM_HAVE_READONLY_MEM |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1106 | valid_flags |= KVM_MEM_READONLY; |
| 1107 | #endif |
| 1108 | |
| 1109 | if (mem->flags & ~valid_flags) |
Xiao Guangrong | a50d64d | 2012-08-21 10:58:13 +0800 | [diff] [blame] | 1110 | return -EINVAL; |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
Gleb Natapov | 7ec4fb4 | 2012-12-24 17:49:30 +0200 | [diff] [blame] | 1115 | static struct kvm_memslots *install_new_memslots(struct kvm *kvm, |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1116 | int as_id, struct kvm_memslots *slots) |
Gleb Natapov | 7ec4fb4 | 2012-12-24 17:49:30 +0200 | [diff] [blame] | 1117 | { |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1118 | struct kvm_memslots *old_memslots = __kvm_memslots(kvm, as_id); |
Sean Christopherson | 361209e | 2019-02-05 13:01:14 -0800 | [diff] [blame] | 1119 | u64 gen = old_memslots->generation; |
Gleb Natapov | 7ec4fb4 | 2012-12-24 17:49:30 +0200 | [diff] [blame] | 1120 | |
Sean Christopherson | 361209e | 2019-02-05 13:01:14 -0800 | [diff] [blame] | 1121 | WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS); |
| 1122 | slots->generation = gen | KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS; |
David Matlack | ee3d157 | 2014-08-18 15:46:06 -0700 | [diff] [blame] | 1123 | |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1124 | rcu_assign_pointer(kvm->memslots[as_id], slots); |
Gleb Natapov | 7ec4fb4 | 2012-12-24 17:49:30 +0200 | [diff] [blame] | 1125 | synchronize_srcu_expedited(&kvm->srcu); |
Takuya Yoshikawa | e59dbe0 | 2013-07-04 13:40:29 +0900 | [diff] [blame] | 1126 | |
David Matlack | ee3d157 | 2014-08-18 15:46:06 -0700 | [diff] [blame] | 1127 | /* |
Sean Christopherson | 361209e | 2019-02-05 13:01:14 -0800 | [diff] [blame] | 1128 | * Increment the new memslot generation a second time, dropping the |
Miaohe Lin | 0011679 | 2019-12-11 14:26:23 +0800 | [diff] [blame] | 1129 | * update in-progress flag and incrementing the generation based on |
Sean Christopherson | 361209e | 2019-02-05 13:01:14 -0800 | [diff] [blame] | 1130 | * the number of address spaces. This provides a unique and easily |
| 1131 | * identifiable generation number while the memslots are in flux. |
| 1132 | */ |
| 1133 | gen = slots->generation & ~KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS; |
| 1134 | |
| 1135 | /* |
Paolo Bonzini | 4bd518f | 2017-02-03 20:44:51 -0800 | [diff] [blame] | 1136 | * Generations must be unique even across address spaces. We do not need |
| 1137 | * a global counter for that, instead the generation space is evenly split |
| 1138 | * across address spaces. For example, with two address spaces, address |
Sean Christopherson | 164bf7e | 2019-02-05 13:01:18 -0800 | [diff] [blame] | 1139 | * space 0 will use generations 0, 2, 4, ... while address space 1 will |
| 1140 | * use generations 1, 3, 5, ... |
David Matlack | ee3d157 | 2014-08-18 15:46:06 -0700 | [diff] [blame] | 1141 | */ |
Sean Christopherson | 164bf7e | 2019-02-05 13:01:18 -0800 | [diff] [blame] | 1142 | gen += KVM_ADDRESS_SPACE_NUM; |
David Matlack | ee3d157 | 2014-08-18 15:46:06 -0700 | [diff] [blame] | 1143 | |
Sean Christopherson | 1524825 | 2019-02-05 12:54:17 -0800 | [diff] [blame] | 1144 | kvm_arch_memslots_updated(kvm, gen); |
| 1145 | |
| 1146 | slots->generation = gen; |
Takuya Yoshikawa | e59dbe0 | 2013-07-04 13:40:29 +0900 | [diff] [blame] | 1147 | |
| 1148 | return old_memslots; |
Gleb Natapov | 7ec4fb4 | 2012-12-24 17:49:30 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Sean Christopherson | 3694725 | 2020-02-18 13:07:32 -0800 | [diff] [blame] | 1151 | /* |
| 1152 | * Note, at a minimum, the current number of used slots must be allocated, even |
| 1153 | * when deleting a memslot, as we need a complete duplicate of the memslots for |
| 1154 | * use when invalidating a memslot prior to deleting/moving the memslot. |
| 1155 | */ |
| 1156 | static struct kvm_memslots *kvm_dup_memslots(struct kvm_memslots *old, |
| 1157 | enum kvm_mr_change change) |
| 1158 | { |
| 1159 | struct kvm_memslots *slots; |
| 1160 | size_t old_size, new_size; |
| 1161 | |
| 1162 | old_size = sizeof(struct kvm_memslots) + |
| 1163 | (sizeof(struct kvm_memory_slot) * old->used_slots); |
| 1164 | |
| 1165 | if (change == KVM_MR_CREATE) |
| 1166 | new_size = old_size + sizeof(struct kvm_memory_slot); |
| 1167 | else |
| 1168 | new_size = old_size; |
| 1169 | |
| 1170 | slots = kvzalloc(new_size, GFP_KERNEL_ACCOUNT); |
| 1171 | if (likely(slots)) |
| 1172 | memcpy(slots, old, old_size); |
| 1173 | |
| 1174 | return slots; |
| 1175 | } |
| 1176 | |
Sean Christopherson | cf47f50 | 2020-02-18 13:07:23 -0800 | [diff] [blame] | 1177 | static int kvm_set_memslot(struct kvm *kvm, |
| 1178 | const struct kvm_userspace_memory_region *mem, |
Sean Christopherson | 9d4c197 | 2020-02-18 13:07:24 -0800 | [diff] [blame] | 1179 | struct kvm_memory_slot *old, |
Sean Christopherson | cf47f50 | 2020-02-18 13:07:23 -0800 | [diff] [blame] | 1180 | struct kvm_memory_slot *new, int as_id, |
| 1181 | enum kvm_mr_change change) |
| 1182 | { |
| 1183 | struct kvm_memory_slot *slot; |
| 1184 | struct kvm_memslots *slots; |
| 1185 | int r; |
| 1186 | |
Sean Christopherson | 3694725 | 2020-02-18 13:07:32 -0800 | [diff] [blame] | 1187 | slots = kvm_dup_memslots(__kvm_memslots(kvm, as_id), change); |
Sean Christopherson | cf47f50 | 2020-02-18 13:07:23 -0800 | [diff] [blame] | 1188 | if (!slots) |
| 1189 | return -ENOMEM; |
Sean Christopherson | cf47f50 | 2020-02-18 13:07:23 -0800 | [diff] [blame] | 1190 | |
| 1191 | if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) { |
| 1192 | /* |
| 1193 | * Note, the INVALID flag needs to be in the appropriate entry |
| 1194 | * in the freshly allocated memslots, not in @old or @new. |
| 1195 | */ |
| 1196 | slot = id_to_memslot(slots, old->id); |
| 1197 | slot->flags |= KVM_MEMSLOT_INVALID; |
| 1198 | |
| 1199 | /* |
| 1200 | * We can re-use the old memslots, the only difference from the |
| 1201 | * newly installed memslots is the invalid flag, which will get |
| 1202 | * dropped by update_memslots anyway. We'll also revert to the |
| 1203 | * old memslots if preparing the new memory region fails. |
| 1204 | */ |
| 1205 | slots = install_new_memslots(kvm, as_id, slots); |
| 1206 | |
| 1207 | /* From this point no new shadow pages pointing to a deleted, |
| 1208 | * or moved, memslot will be created. |
| 1209 | * |
| 1210 | * validation of sp->gfn happens in: |
| 1211 | * - gfn_to_hva (kvm_read_guest, gfn_to_pfn) |
| 1212 | * - kvm_is_visible_gfn (mmu_check_root) |
| 1213 | */ |
| 1214 | kvm_arch_flush_shadow_memslot(kvm, slot); |
| 1215 | } |
| 1216 | |
| 1217 | r = kvm_arch_prepare_memory_region(kvm, new, mem, change); |
| 1218 | if (r) |
| 1219 | goto out_slots; |
| 1220 | |
| 1221 | update_memslots(slots, new, change); |
| 1222 | slots = install_new_memslots(kvm, as_id, slots); |
| 1223 | |
| 1224 | kvm_arch_commit_memory_region(kvm, mem, old, new, change); |
| 1225 | |
| 1226 | kvfree(slots); |
| 1227 | return 0; |
| 1228 | |
| 1229 | out_slots: |
| 1230 | if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) |
| 1231 | slots = install_new_memslots(kvm, as_id, slots); |
| 1232 | kvfree(slots); |
| 1233 | return r; |
| 1234 | } |
| 1235 | |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1236 | static int kvm_delete_memslot(struct kvm *kvm, |
| 1237 | const struct kvm_userspace_memory_region *mem, |
| 1238 | struct kvm_memory_slot *old, int as_id) |
| 1239 | { |
| 1240 | struct kvm_memory_slot new; |
| 1241 | int r; |
| 1242 | |
| 1243 | if (!old->npages) |
| 1244 | return -EINVAL; |
| 1245 | |
| 1246 | memset(&new, 0, sizeof(new)); |
| 1247 | new.id = old->id; |
Peter Xu | 9e9eb22 | 2020-10-14 11:26:46 -0700 | [diff] [blame] | 1248 | /* |
| 1249 | * This is only for debugging purpose; it should never be referenced |
| 1250 | * for a removed memslot. |
| 1251 | */ |
| 1252 | new.as_id = as_id; |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1253 | |
| 1254 | r = kvm_set_memslot(kvm, mem, old, &new, as_id, KVM_MR_DELETE); |
| 1255 | if (r) |
| 1256 | return r; |
| 1257 | |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 1258 | kvm_free_memslot(kvm, old); |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1259 | return 0; |
| 1260 | } |
| 1261 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1262 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1263 | * Allocate some memory and give it an address in the guest physical address |
| 1264 | * space. |
| 1265 | * |
| 1266 | * Discontiguous memory is allowed, mostly for framebuffers. |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1267 | * |
Dominik Dingel | 02d5d55 | 2014-10-27 16:22:56 +0100 | [diff] [blame] | 1268 | * Must be called holding kvm->slots_lock for write. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1269 | */ |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1270 | int __kvm_set_memory_region(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 1271 | const struct kvm_userspace_memory_region *mem) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1272 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1273 | struct kvm_memory_slot old, new; |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1274 | struct kvm_memory_slot *tmp; |
Takuya Yoshikawa | f64c039 | 2013-01-29 11:00:07 +0900 | [diff] [blame] | 1275 | enum kvm_mr_change change; |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1276 | int as_id, id; |
| 1277 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1278 | |
Xiao Guangrong | a50d64d | 2012-08-21 10:58:13 +0800 | [diff] [blame] | 1279 | r = check_memory_region_flags(mem); |
| 1280 | if (r) |
Sean Christopherson | 71a4c30 | 2020-02-18 13:07:22 -0800 | [diff] [blame] | 1281 | return r; |
Xiao Guangrong | a50d64d | 2012-08-21 10:58:13 +0800 | [diff] [blame] | 1282 | |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1283 | as_id = mem->slot >> 16; |
| 1284 | id = (u16)mem->slot; |
| 1285 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1286 | /* General sanity checks */ |
| 1287 | if (mem->memory_size & (PAGE_SIZE - 1)) |
Sean Christopherson | 71a4c30 | 2020-02-18 13:07:22 -0800 | [diff] [blame] | 1288 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1289 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
Sean Christopherson | 71a4c30 | 2020-02-18 13:07:22 -0800 | [diff] [blame] | 1290 | return -EINVAL; |
Takuya Yoshikawa | fa3d315 | 2011-05-07 16:35:38 +0900 | [diff] [blame] | 1291 | /* We can read the guest memory with __xxx_user() later on. */ |
Paolo Bonzini | 09d952c | 2020-06-01 04:17:45 -0400 | [diff] [blame] | 1292 | if ((mem->userspace_addr & (PAGE_SIZE - 1)) || |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1293 | !access_ok((void __user *)(unsigned long)mem->userspace_addr, |
Paolo Bonzini | 09d952c | 2020-06-01 04:17:45 -0400 | [diff] [blame] | 1294 | mem->memory_size)) |
Sean Christopherson | 71a4c30 | 2020-02-18 13:07:22 -0800 | [diff] [blame] | 1295 | return -EINVAL; |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1296 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM) |
Sean Christopherson | 71a4c30 | 2020-02-18 13:07:22 -0800 | [diff] [blame] | 1297 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1298 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
Sean Christopherson | 71a4c30 | 2020-02-18 13:07:22 -0800 | [diff] [blame] | 1299 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1300 | |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1301 | /* |
| 1302 | * Make a full copy of the old memslot, the pointer will become stale |
| 1303 | * when the memslots are re-sorted by update_memslots(), and the old |
| 1304 | * memslot needs to be referenced after calling update_memslots(), e.g. |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1305 | * to free its resources and for arch specific behavior. |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1306 | */ |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 1307 | tmp = id_to_memslot(__kvm_memslots(kvm, as_id), id); |
| 1308 | if (tmp) { |
| 1309 | old = *tmp; |
| 1310 | tmp = NULL; |
| 1311 | } else { |
| 1312 | memset(&old, 0, sizeof(old)); |
| 1313 | old.id = id; |
| 1314 | } |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1315 | |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1316 | if (!mem->memory_size) |
| 1317 | return kvm_delete_memslot(kvm, mem, &old, as_id); |
| 1318 | |
Peter Xu | 9e9eb22 | 2020-10-14 11:26:46 -0700 | [diff] [blame] | 1319 | new.as_id = as_id; |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1320 | new.id = id; |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1321 | new.base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 1322 | new.npages = mem->memory_size >> PAGE_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1323 | new.flags = mem->flags; |
Sean Christopherson | 414de7ab | 2020-02-18 13:07:20 -0800 | [diff] [blame] | 1324 | new.userspace_addr = mem->userspace_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1325 | |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1326 | if (new.npages > KVM_MEM_MAX_NR_PAGES) |
| 1327 | return -EINVAL; |
| 1328 | |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1329 | if (!old.npages) { |
| 1330 | change = KVM_MR_CREATE; |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1331 | new.dirty_bitmap = NULL; |
| 1332 | memset(&new.arch, 0, sizeof(new.arch)); |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1333 | } else { /* Modify an existing slot. */ |
| 1334 | if ((new.userspace_addr != old.userspace_addr) || |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1335 | (new.npages != old.npages) || |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1336 | ((new.flags ^ old.flags) & KVM_MEM_READONLY)) |
Sean Christopherson | 71a4c30 | 2020-02-18 13:07:22 -0800 | [diff] [blame] | 1337 | return -EINVAL; |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 1338 | |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1339 | if (new.base_gfn != old.base_gfn) |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1340 | change = KVM_MR_MOVE; |
| 1341 | else if (new.flags != old.flags) |
| 1342 | change = KVM_MR_FLAGS_ONLY; |
| 1343 | else /* Nothing to change. */ |
| 1344 | return 0; |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1345 | |
| 1346 | /* Copy dirty_bitmap and arch from the current memslot. */ |
| 1347 | new.dirty_bitmap = old.dirty_bitmap; |
| 1348 | memcpy(&new.arch, &old.arch, sizeof(new.arch)); |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 1349 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1350 | |
Takuya Yoshikawa | f64c039 | 2013-01-29 11:00:07 +0900 | [diff] [blame] | 1351 | if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) { |
Takuya Yoshikawa | 0a706be | 2013-01-11 18:26:55 +0900 | [diff] [blame] | 1352 | /* Check for overlaps */ |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1353 | kvm_for_each_memslot(tmp, __kvm_memslots(kvm, as_id)) { |
| 1354 | if (tmp->id == id) |
Takuya Yoshikawa | 0a706be | 2013-01-11 18:26:55 +0900 | [diff] [blame] | 1355 | continue; |
Sean Christopherson | 163da37 | 2020-02-18 13:07:28 -0800 | [diff] [blame] | 1356 | if (!((new.base_gfn + new.npages <= tmp->base_gfn) || |
| 1357 | (new.base_gfn >= tmp->base_gfn + tmp->npages))) |
Sean Christopherson | 71a4c30 | 2020-02-18 13:07:22 -0800 | [diff] [blame] | 1358 | return -EEXIST; |
Takuya Yoshikawa | 0a706be | 2013-01-11 18:26:55 +0900 | [diff] [blame] | 1359 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1360 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1361 | |
Sean Christopherson | 414de7ab | 2020-02-18 13:07:20 -0800 | [diff] [blame] | 1362 | /* Allocate/free page dirty bitmap as needed */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1363 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1364 | new.dirty_bitmap = NULL; |
Sean Christopherson | 414de7ab | 2020-02-18 13:07:20 -0800 | [diff] [blame] | 1365 | else if (!new.dirty_bitmap) { |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 1366 | r = kvm_alloc_dirty_bitmap(&new); |
Sean Christopherson | 71a4c30 | 2020-02-18 13:07:22 -0800 | [diff] [blame] | 1367 | if (r) |
| 1368 | return r; |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 1369 | |
| 1370 | if (kvm_dirty_log_manual_protect_and_init_set(kvm)) |
| 1371 | bitmap_set(new.dirty_bitmap, 0, new.npages); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
Sean Christopherson | cf47f50 | 2020-02-18 13:07:23 -0800 | [diff] [blame] | 1374 | r = kvm_set_memslot(kvm, mem, &old, &new, as_id, change); |
| 1375 | if (r) |
| 1376 | goto out_bitmap; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 1377 | |
Sean Christopherson | 5c0b4f3 | 2020-02-18 13:07:26 -0800 | [diff] [blame] | 1378 | if (old.dirty_bitmap && !new.dirty_bitmap) |
| 1379 | kvm_destroy_dirty_bitmap(&old); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1380 | return 0; |
| 1381 | |
Sean Christopherson | bd0e96f | 2020-02-18 13:07:21 -0800 | [diff] [blame] | 1382 | out_bitmap: |
| 1383 | if (new.dirty_bitmap && !old.dirty_bitmap) |
| 1384 | kvm_destroy_dirty_bitmap(&new); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1385 | return r; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 1386 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1387 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
| 1388 | |
| 1389 | int kvm_set_memory_region(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 1390 | const struct kvm_userspace_memory_region *mem) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1391 | { |
| 1392 | int r; |
| 1393 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 1394 | mutex_lock(&kvm->slots_lock); |
Takuya Yoshikawa | 47ae31e | 2013-02-27 19:43:00 +0900 | [diff] [blame] | 1395 | r = __kvm_set_memory_region(kvm, mem); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 1396 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1397 | return r; |
| 1398 | } |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 1399 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
| 1400 | |
Stephen Hemminger | 7940876 | 2013-12-29 12:12:29 -0800 | [diff] [blame] | 1401 | static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 1402 | struct kvm_userspace_memory_region *mem) |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 1403 | { |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1404 | if ((u16)mem->slot >= KVM_USER_MEM_SLOTS) |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 1405 | return -EINVAL; |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 1406 | |
Takuya Yoshikawa | 47ae31e | 2013-02-27 19:43:00 +0900 | [diff] [blame] | 1407 | return kvm_set_memory_region(kvm, mem); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1408 | } |
| 1409 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1410 | #ifndef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT |
Sean Christopherson | 2a49f61 | 2020-02-18 13:07:30 -0800 | [diff] [blame] | 1411 | /** |
| 1412 | * kvm_get_dirty_log - get a snapshot of dirty pages |
| 1413 | * @kvm: pointer to kvm instance |
| 1414 | * @log: slot id and address to which we copy the log |
| 1415 | * @is_dirty: set to '1' if any dirty pages were found |
| 1416 | * @memslot: set to the associated memslot, always valid on success |
| 1417 | */ |
| 1418 | int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log, |
| 1419 | int *is_dirty, struct kvm_memory_slot **memslot) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1420 | { |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 1421 | struct kvm_memslots *slots; |
Markus Elfring | 843574a | 2017-01-22 17:41:07 +0100 | [diff] [blame] | 1422 | int i, as_id, id; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 1423 | unsigned long n; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1424 | unsigned long any = 0; |
| 1425 | |
Sean Christopherson | 2a49f61 | 2020-02-18 13:07:30 -0800 | [diff] [blame] | 1426 | *memslot = NULL; |
| 1427 | *is_dirty = 0; |
| 1428 | |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1429 | as_id = log->slot >> 16; |
| 1430 | id = (u16)log->slot; |
| 1431 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) |
Markus Elfring | 843574a | 2017-01-22 17:41:07 +0100 | [diff] [blame] | 1432 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1433 | |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1434 | slots = __kvm_memslots(kvm, as_id); |
Sean Christopherson | 2a49f61 | 2020-02-18 13:07:30 -0800 | [diff] [blame] | 1435 | *memslot = id_to_memslot(slots, id); |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 1436 | if (!(*memslot) || !(*memslot)->dirty_bitmap) |
Markus Elfring | 843574a | 2017-01-22 17:41:07 +0100 | [diff] [blame] | 1437 | return -ENOENT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1438 | |
Sean Christopherson | 2a49f61 | 2020-02-18 13:07:30 -0800 | [diff] [blame] | 1439 | kvm_arch_sync_dirty_log(kvm, *memslot); |
| 1440 | |
| 1441 | n = kvm_dirty_bitmap_bytes(*memslot); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1442 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 1443 | for (i = 0; !any && i < n/sizeof(long); ++i) |
Sean Christopherson | 2a49f61 | 2020-02-18 13:07:30 -0800 | [diff] [blame] | 1444 | any = (*memslot)->dirty_bitmap[i]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1445 | |
Sean Christopherson | 2a49f61 | 2020-02-18 13:07:30 -0800 | [diff] [blame] | 1446 | if (copy_to_user(log->dirty_bitmap, (*memslot)->dirty_bitmap, n)) |
Markus Elfring | 843574a | 2017-01-22 17:41:07 +0100 | [diff] [blame] | 1447 | return -EFAULT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1448 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 1449 | if (any) |
| 1450 | *is_dirty = 1; |
Markus Elfring | 843574a | 2017-01-22 17:41:07 +0100 | [diff] [blame] | 1451 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1452 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 1453 | EXPORT_SYMBOL_GPL(kvm_get_dirty_log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1454 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1455 | #else /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1456 | /** |
Jiang Biao | b8b0022 | 2019-04-23 19:40:30 +0800 | [diff] [blame] | 1457 | * kvm_get_dirty_log_protect - get a snapshot of dirty pages |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1458 | * and reenable dirty page tracking for the corresponding pages. |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1459 | * @kvm: pointer to kvm instance |
| 1460 | * @log: slot id and address to which we copy the log |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1461 | * |
| 1462 | * We need to keep it in mind that VCPU threads can write to the bitmap |
| 1463 | * concurrently. So, to avoid losing track of dirty pages we keep the |
| 1464 | * following order: |
| 1465 | * |
| 1466 | * 1. Take a snapshot of the bit and clear it if needed. |
| 1467 | * 2. Write protect the corresponding page. |
| 1468 | * 3. Copy the snapshot to the userspace. |
| 1469 | * 4. Upon return caller flushes TLB's if needed. |
| 1470 | * |
| 1471 | * Between 2 and 4, the guest may write to the page using the remaining TLB |
| 1472 | * entry. This is not a problem because the page is reported dirty using |
| 1473 | * the snapshot taken before and step 4 ensures that writes done after |
| 1474 | * exiting to userspace will be logged for the next call. |
| 1475 | * |
| 1476 | */ |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1477 | static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log) |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1478 | { |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 1479 | struct kvm_memslots *slots; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1480 | struct kvm_memory_slot *memslot; |
Markus Elfring | 58d6db3 | 2017-01-22 17:30:16 +0100 | [diff] [blame] | 1481 | int i, as_id, id; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1482 | unsigned long n; |
| 1483 | unsigned long *dirty_bitmap; |
| 1484 | unsigned long *dirty_bitmap_buffer; |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1485 | bool flush; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1486 | |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1487 | as_id = log->slot >> 16; |
| 1488 | id = (u16)log->slot; |
| 1489 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) |
Markus Elfring | 58d6db3 | 2017-01-22 17:30:16 +0100 | [diff] [blame] | 1490 | return -EINVAL; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1491 | |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 1492 | slots = __kvm_memslots(kvm, as_id); |
| 1493 | memslot = id_to_memslot(slots, id); |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 1494 | if (!memslot || !memslot->dirty_bitmap) |
| 1495 | return -ENOENT; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1496 | |
| 1497 | dirty_bitmap = memslot->dirty_bitmap; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1498 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1499 | kvm_arch_sync_dirty_log(kvm, memslot); |
| 1500 | |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1501 | n = kvm_dirty_bitmap_bytes(memslot); |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1502 | flush = false; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1503 | if (kvm->manual_dirty_log_protect) { |
| 1504 | /* |
| 1505 | * Unlike kvm_get_dirty_log, we always return false in *flush, |
| 1506 | * because no flush is needed until KVM_CLEAR_DIRTY_LOG. There |
| 1507 | * is some code duplication between this function and |
| 1508 | * kvm_get_dirty_log, but hopefully all architecture |
| 1509 | * transition to kvm_get_dirty_log_protect and kvm_get_dirty_log |
| 1510 | * can be eliminated. |
| 1511 | */ |
| 1512 | dirty_bitmap_buffer = dirty_bitmap; |
| 1513 | } else { |
| 1514 | dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot); |
| 1515 | memset(dirty_bitmap_buffer, 0, n); |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1516 | |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1517 | spin_lock(&kvm->mmu_lock); |
| 1518 | for (i = 0; i < n / sizeof(long); i++) { |
| 1519 | unsigned long mask; |
| 1520 | gfn_t offset; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1521 | |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1522 | if (!dirty_bitmap[i]) |
| 1523 | continue; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1524 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1525 | flush = true; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1526 | mask = xchg(&dirty_bitmap[i], 0); |
| 1527 | dirty_bitmap_buffer[i] = mask; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1528 | |
Lan Tianyu | a67794c | 2019-02-02 17:20:27 +0800 | [diff] [blame] | 1529 | offset = i * BITS_PER_LONG; |
| 1530 | kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, |
| 1531 | offset, mask); |
Takuya Yoshikawa | 58d2930 | 2015-03-17 16:19:58 +0900 | [diff] [blame] | 1532 | } |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1533 | spin_unlock(&kvm->mmu_lock); |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1536 | if (flush) |
| 1537 | kvm_arch_flush_remote_tlbs_memslot(kvm, memslot); |
| 1538 | |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1539 | if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n)) |
Markus Elfring | 58d6db3 | 2017-01-22 17:30:16 +0100 | [diff] [blame] | 1540 | return -EFAULT; |
| 1541 | return 0; |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1542 | } |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1543 | |
| 1544 | |
| 1545 | /** |
| 1546 | * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot |
| 1547 | * @kvm: kvm instance |
| 1548 | * @log: slot id and address to which we copy the log |
| 1549 | * |
| 1550 | * Steps 1-4 below provide general overview of dirty page logging. See |
| 1551 | * kvm_get_dirty_log_protect() function description for additional details. |
| 1552 | * |
| 1553 | * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we |
| 1554 | * always flush the TLB (step 4) even if previous step failed and the dirty |
| 1555 | * bitmap may be corrupt. Regardless of previous outcome the KVM logging API |
| 1556 | * does not preclude user space subsequent dirty log read. Flushing TLB ensures |
| 1557 | * writes will be marked dirty for next log read. |
| 1558 | * |
| 1559 | * 1. Take a snapshot of the bit and clear it if needed. |
| 1560 | * 2. Write protect the corresponding page. |
| 1561 | * 3. Copy the snapshot to the userspace. |
| 1562 | * 4. Flush TLB's if needed. |
| 1563 | */ |
| 1564 | static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 1565 | struct kvm_dirty_log *log) |
| 1566 | { |
| 1567 | int r; |
| 1568 | |
| 1569 | mutex_lock(&kvm->slots_lock); |
| 1570 | |
| 1571 | r = kvm_get_dirty_log_protect(kvm, log); |
| 1572 | |
| 1573 | mutex_unlock(&kvm->slots_lock); |
| 1574 | return r; |
| 1575 | } |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1576 | |
| 1577 | /** |
| 1578 | * kvm_clear_dirty_log_protect - clear dirty bits in the bitmap |
| 1579 | * and reenable dirty page tracking for the corresponding pages. |
| 1580 | * @kvm: pointer to kvm instance |
| 1581 | * @log: slot id and address from which to fetch the bitmap of dirty pages |
| 1582 | */ |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1583 | static int kvm_clear_dirty_log_protect(struct kvm *kvm, |
| 1584 | struct kvm_clear_dirty_log *log) |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1585 | { |
| 1586 | struct kvm_memslots *slots; |
| 1587 | struct kvm_memory_slot *memslot; |
Tomas Bortoli | 98938aa | 2019-01-02 18:29:37 +0100 | [diff] [blame] | 1588 | int as_id, id; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1589 | gfn_t offset; |
Tomas Bortoli | 98938aa | 2019-01-02 18:29:37 +0100 | [diff] [blame] | 1590 | unsigned long i, n; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1591 | unsigned long *dirty_bitmap; |
| 1592 | unsigned long *dirty_bitmap_buffer; |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1593 | bool flush; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1594 | |
| 1595 | as_id = log->slot >> 16; |
| 1596 | id = (u16)log->slot; |
| 1597 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) |
| 1598 | return -EINVAL; |
| 1599 | |
Paolo Bonzini | 76d58e0 | 2019-04-17 15:28:44 +0200 | [diff] [blame] | 1600 | if (log->first_page & 63) |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1601 | return -EINVAL; |
| 1602 | |
| 1603 | slots = __kvm_memslots(kvm, as_id); |
| 1604 | memslot = id_to_memslot(slots, id); |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 1605 | if (!memslot || !memslot->dirty_bitmap) |
| 1606 | return -ENOENT; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1607 | |
| 1608 | dirty_bitmap = memslot->dirty_bitmap; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1609 | |
Peter Xu | 4ddc920 | 2019-05-08 17:15:45 +0800 | [diff] [blame] | 1610 | n = ALIGN(log->num_pages, BITS_PER_LONG) / 8; |
Tomas Bortoli | 98938aa | 2019-01-02 18:29:37 +0100 | [diff] [blame] | 1611 | |
| 1612 | if (log->first_page > memslot->npages || |
Paolo Bonzini | 76d58e0 | 2019-04-17 15:28:44 +0200 | [diff] [blame] | 1613 | log->num_pages > memslot->npages - log->first_page || |
| 1614 | (log->num_pages < memslot->npages - log->first_page && (log->num_pages & 63))) |
| 1615 | return -EINVAL; |
Tomas Bortoli | 98938aa | 2019-01-02 18:29:37 +0100 | [diff] [blame] | 1616 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1617 | kvm_arch_sync_dirty_log(kvm, memslot); |
| 1618 | |
| 1619 | flush = false; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1620 | dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot); |
| 1621 | if (copy_from_user(dirty_bitmap_buffer, log->dirty_bitmap, n)) |
| 1622 | return -EFAULT; |
| 1623 | |
| 1624 | spin_lock(&kvm->mmu_lock); |
Peter Xu | 53eac7a | 2019-05-08 17:15:46 +0800 | [diff] [blame] | 1625 | for (offset = log->first_page, i = offset / BITS_PER_LONG, |
| 1626 | n = DIV_ROUND_UP(log->num_pages, BITS_PER_LONG); n--; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1627 | i++, offset += BITS_PER_LONG) { |
| 1628 | unsigned long mask = *dirty_bitmap_buffer++; |
| 1629 | atomic_long_t *p = (atomic_long_t *) &dirty_bitmap[i]; |
| 1630 | if (!mask) |
| 1631 | continue; |
| 1632 | |
| 1633 | mask &= atomic_long_fetch_andnot(mask, p); |
| 1634 | |
| 1635 | /* |
| 1636 | * mask contains the bits that really have been cleared. This |
| 1637 | * never includes any bits beyond the length of the memslot (if |
| 1638 | * the length is not aligned to 64 pages), therefore it is not |
| 1639 | * a problem if userspace sets them in log->dirty_bitmap. |
| 1640 | */ |
| 1641 | if (mask) { |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1642 | flush = true; |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1643 | kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, |
| 1644 | offset, mask); |
| 1645 | } |
| 1646 | } |
| 1647 | spin_unlock(&kvm->mmu_lock); |
| 1648 | |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1649 | if (flush) |
| 1650 | kvm_arch_flush_remote_tlbs_memslot(kvm, memslot); |
| 1651 | |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 1652 | return 0; |
| 1653 | } |
Sean Christopherson | 0dff084 | 2020-02-18 13:07:29 -0800 | [diff] [blame] | 1654 | |
| 1655 | static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, |
| 1656 | struct kvm_clear_dirty_log *log) |
| 1657 | { |
| 1658 | int r; |
| 1659 | |
| 1660 | mutex_lock(&kvm->slots_lock); |
| 1661 | |
| 1662 | r = kvm_clear_dirty_log_protect(kvm, log); |
| 1663 | |
| 1664 | mutex_unlock(&kvm->slots_lock); |
| 1665 | return r; |
| 1666 | } |
| 1667 | #endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ |
Mario Smarduch | ba0513b | 2015-01-15 15:58:53 -0800 | [diff] [blame] | 1668 | |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 1669 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 1670 | { |
| 1671 | return __gfn_to_memslot(kvm_memslots(kvm), gfn); |
| 1672 | } |
Avi Kivity | a1f4d395 | 2010-06-21 11:44:20 +0300 | [diff] [blame] | 1673 | EXPORT_SYMBOL_GPL(gfn_to_memslot); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1674 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 1675 | struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 1676 | { |
| 1677 | return __gfn_to_memslot(kvm_vcpu_memslots(vcpu), gfn); |
| 1678 | } |
Paolo Bonzini | e72436b | 2020-04-17 12:21:06 -0400 | [diff] [blame] | 1679 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_memslot); |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 1680 | |
Yaowei Bai | 33e9415 | 2015-11-14 11:21:06 +0800 | [diff] [blame] | 1681 | bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 1682 | { |
Xiao Guangrong | bf3e05b | 2011-11-24 17:40:57 +0800 | [diff] [blame] | 1683 | struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn); |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 1684 | |
Paolo Bonzini | c36b715 | 2020-04-16 09:48:07 -0400 | [diff] [blame] | 1685 | return kvm_is_visible_memslot(memslot); |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 1686 | } |
| 1687 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); |
| 1688 | |
Vitaly Kuznetsov | 995decb | 2020-07-08 16:00:23 +0200 | [diff] [blame] | 1689 | bool kvm_vcpu_is_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 1690 | { |
| 1691 | struct kvm_memory_slot *memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
| 1692 | |
| 1693 | return kvm_is_visible_memslot(memslot); |
| 1694 | } |
| 1695 | EXPORT_SYMBOL_GPL(kvm_vcpu_is_visible_gfn); |
| 1696 | |
Sean Christopherson | f9b84e1 | 2020-01-08 12:24:37 -0800 | [diff] [blame] | 1697 | unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn) |
Joerg Roedel | 8f0b1ab | 2010-01-28 12:37:56 +0100 | [diff] [blame] | 1698 | { |
| 1699 | struct vm_area_struct *vma; |
| 1700 | unsigned long addr, size; |
| 1701 | |
| 1702 | size = PAGE_SIZE; |
| 1703 | |
Sean Christopherson | 42cde48 | 2020-01-08 12:24:38 -0800 | [diff] [blame] | 1704 | addr = kvm_vcpu_gfn_to_hva_prot(vcpu, gfn, NULL); |
Joerg Roedel | 8f0b1ab | 2010-01-28 12:37:56 +0100 | [diff] [blame] | 1705 | if (kvm_is_error_hva(addr)) |
| 1706 | return PAGE_SIZE; |
| 1707 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1708 | mmap_read_lock(current->mm); |
Joerg Roedel | 8f0b1ab | 2010-01-28 12:37:56 +0100 | [diff] [blame] | 1709 | vma = find_vma(current->mm, addr); |
| 1710 | if (!vma) |
| 1711 | goto out; |
| 1712 | |
| 1713 | size = vma_kernel_pagesize(vma); |
| 1714 | |
| 1715 | out: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1716 | mmap_read_unlock(current->mm); |
Joerg Roedel | 8f0b1ab | 2010-01-28 12:37:56 +0100 | [diff] [blame] | 1717 | |
| 1718 | return size; |
| 1719 | } |
| 1720 | |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1721 | static bool memslot_is_readonly(struct kvm_memory_slot *slot) |
| 1722 | { |
| 1723 | return slot->flags & KVM_MEM_READONLY; |
| 1724 | } |
| 1725 | |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1726 | static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, |
| 1727 | gfn_t *nr_pages, bool write) |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1728 | { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 1729 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID) |
Xiao Guangrong | ca3a490 | 2012-08-21 11:01:50 +0800 | [diff] [blame] | 1730 | return KVM_HVA_ERR_BAD; |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 1731 | |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1732 | if (memslot_is_readonly(slot) && write) |
| 1733 | return KVM_HVA_ERR_RO_BAD; |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 1734 | |
| 1735 | if (nr_pages) |
| 1736 | *nr_pages = slot->npages - (gfn - slot->base_gfn); |
| 1737 | |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1738 | return __gfn_to_hva_memslot(slot, gfn); |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1739 | } |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 1740 | |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1741 | static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, |
| 1742 | gfn_t *nr_pages) |
| 1743 | { |
| 1744 | return __gfn_to_hva_many(slot, gfn, nr_pages, true); |
| 1745 | } |
| 1746 | |
| 1747 | unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, |
Stephen Hemminger | 7940876 | 2013-12-29 12:12:29 -0800 | [diff] [blame] | 1748 | gfn_t gfn) |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1749 | { |
| 1750 | return gfn_to_hva_many(slot, gfn, NULL); |
| 1751 | } |
| 1752 | EXPORT_SYMBOL_GPL(gfn_to_hva_memslot); |
| 1753 | |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 1754 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
| 1755 | { |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 1756 | return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL); |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 1757 | } |
Sheng Yang | 0d15029 | 2008-04-25 21:44:50 +0800 | [diff] [blame] | 1758 | EXPORT_SYMBOL_GPL(gfn_to_hva); |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1759 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 1760 | unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 1761 | { |
| 1762 | return gfn_to_hva_many(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, NULL); |
| 1763 | } |
| 1764 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva); |
| 1765 | |
Xiao Guangrong | 86ab8cf | 2012-08-21 10:59:53 +0800 | [diff] [blame] | 1766 | /* |
Wei Yang | 970c0d4 | 2018-10-09 10:41:15 +0800 | [diff] [blame] | 1767 | * Return the hva of a @gfn and the R/W attribute if possible. |
| 1768 | * |
| 1769 | * @slot: the kvm_memory_slot which contains @gfn |
| 1770 | * @gfn: the gfn to be translated |
| 1771 | * @writable: used to return the read/write attribute of the @slot if the hva |
| 1772 | * is valid and @writable is not NULL |
Xiao Guangrong | 86ab8cf | 2012-08-21 10:59:53 +0800 | [diff] [blame] | 1773 | */ |
Christoffer Dall | 64d8312 | 2014-08-19 12:15:00 +0200 | [diff] [blame] | 1774 | unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, |
| 1775 | gfn_t gfn, bool *writable) |
Gleb Natapov | 8030089 | 2010-10-19 18:13:41 +0200 | [diff] [blame] | 1776 | { |
Gleb Natapov | a2ac07f | 2013-10-01 19:58:36 +0300 | [diff] [blame] | 1777 | unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false); |
| 1778 | |
| 1779 | if (!kvm_is_error_hva(hva) && writable) |
Paolo Bonzini | ba6a354 | 2013-09-09 13:52:33 +0200 | [diff] [blame] | 1780 | *writable = !memslot_is_readonly(slot); |
| 1781 | |
Gleb Natapov | a2ac07f | 2013-10-01 19:58:36 +0300 | [diff] [blame] | 1782 | return hva; |
Xiao Guangrong | 86ab8cf | 2012-08-21 10:59:53 +0800 | [diff] [blame] | 1783 | } |
| 1784 | |
Christoffer Dall | 64d8312 | 2014-08-19 12:15:00 +0200 | [diff] [blame] | 1785 | unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable) |
| 1786 | { |
| 1787 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); |
| 1788 | |
| 1789 | return gfn_to_hva_memslot_prot(slot, gfn, writable); |
| 1790 | } |
| 1791 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 1792 | unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable) |
| 1793 | { |
| 1794 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
| 1795 | |
| 1796 | return gfn_to_hva_memslot_prot(slot, gfn, writable); |
| 1797 | } |
| 1798 | |
Huang Ying | fafc3db | 2011-01-30 11:15:49 +0800 | [diff] [blame] | 1799 | static inline int check_user_page_hwpoison(unsigned long addr) |
| 1800 | { |
Lorenzo Stoakes | 0d73175 | 2016-10-24 10:57:25 +0100 | [diff] [blame] | 1801 | int rc, flags = FOLL_HWPOISON | FOLL_WRITE; |
Huang Ying | fafc3db | 2011-01-30 11:15:49 +0800 | [diff] [blame] | 1802 | |
Lorenzo Stoakes | 0d73175 | 2016-10-24 10:57:25 +0100 | [diff] [blame] | 1803 | rc = get_user_pages(addr, 1, flags, NULL, NULL); |
Huang Ying | fafc3db | 2011-01-30 11:15:49 +0800 | [diff] [blame] | 1804 | return rc == -EHWPOISON; |
| 1805 | } |
| 1806 | |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1807 | /* |
Paolo Bonzini | b9b33da | 2018-07-27 17:44:41 +0200 | [diff] [blame] | 1808 | * The fast path to get the writable pfn which will be stored in @pfn, |
| 1809 | * true indicates success, otherwise false is returned. It's also the |
Miaohe Lin | 311497e | 2019-12-11 14:26:25 +0800 | [diff] [blame] | 1810 | * only part that runs if we can in atomic context. |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1811 | */ |
Paolo Bonzini | b9b33da | 2018-07-27 17:44:41 +0200 | [diff] [blame] | 1812 | static bool hva_to_pfn_fast(unsigned long addr, bool write_fault, |
| 1813 | bool *writable, kvm_pfn_t *pfn) |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1814 | { |
| 1815 | struct page *page[1]; |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1816 | |
Xiao Guangrong | 12ce13f | 2012-08-21 11:00:49 +0800 | [diff] [blame] | 1817 | /* |
| 1818 | * Fast pin a writable pfn only if it is a write fault request |
| 1819 | * or the caller allows to map a writable pfn for a read fault |
| 1820 | * request. |
| 1821 | */ |
| 1822 | if (!(write_fault || writable)) |
| 1823 | return false; |
| 1824 | |
Souptick Joarder | dadbb61 | 2020-06-07 21:40:55 -0700 | [diff] [blame] | 1825 | if (get_user_page_fast_only(addr, FOLL_WRITE, page)) { |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1826 | *pfn = page_to_pfn(page[0]); |
| 1827 | |
| 1828 | if (writable) |
| 1829 | *writable = true; |
| 1830 | return true; |
| 1831 | } |
| 1832 | |
| 1833 | return false; |
| 1834 | } |
| 1835 | |
| 1836 | /* |
| 1837 | * The slow path to get the pfn of the specified host virtual address, |
| 1838 | * 1 indicates success, -errno is returned if error is detected. |
| 1839 | */ |
| 1840 | static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 1841 | bool *writable, kvm_pfn_t *pfn) |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1842 | { |
Al Viro | ce53053 | 2017-11-19 17:47:33 -0500 | [diff] [blame] | 1843 | unsigned int flags = FOLL_HWPOISON; |
| 1844 | struct page *page; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 1845 | int npages = 0; |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1846 | |
| 1847 | might_sleep(); |
| 1848 | |
| 1849 | if (writable) |
| 1850 | *writable = write_fault; |
| 1851 | |
Al Viro | ce53053 | 2017-11-19 17:47:33 -0500 | [diff] [blame] | 1852 | if (write_fault) |
| 1853 | flags |= FOLL_WRITE; |
| 1854 | if (async) |
| 1855 | flags |= FOLL_NOWAIT; |
Lorenzo Stoakes | d4944b0 | 2016-10-13 01:20:12 +0100 | [diff] [blame] | 1856 | |
Al Viro | ce53053 | 2017-11-19 17:47:33 -0500 | [diff] [blame] | 1857 | npages = get_user_pages_unlocked(addr, 1, &page, flags); |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1858 | if (npages != 1) |
| 1859 | return npages; |
| 1860 | |
| 1861 | /* map read fault as writable if possible */ |
Xiao Guangrong | 12ce13f | 2012-08-21 11:00:49 +0800 | [diff] [blame] | 1862 | if (unlikely(!write_fault) && writable) { |
Al Viro | ce53053 | 2017-11-19 17:47:33 -0500 | [diff] [blame] | 1863 | struct page *wpage; |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1864 | |
Souptick Joarder | dadbb61 | 2020-06-07 21:40:55 -0700 | [diff] [blame] | 1865 | if (get_user_page_fast_only(addr, FOLL_WRITE, &wpage)) { |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1866 | *writable = true; |
Al Viro | ce53053 | 2017-11-19 17:47:33 -0500 | [diff] [blame] | 1867 | put_page(page); |
| 1868 | page = wpage; |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1869 | } |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1870 | } |
Al Viro | ce53053 | 2017-11-19 17:47:33 -0500 | [diff] [blame] | 1871 | *pfn = page_to_pfn(page); |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1872 | return npages; |
| 1873 | } |
| 1874 | |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1875 | static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault) |
| 1876 | { |
| 1877 | if (unlikely(!(vma->vm_flags & VM_READ))) |
| 1878 | return false; |
| 1879 | |
| 1880 | if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE)))) |
| 1881 | return false; |
| 1882 | |
| 1883 | return true; |
| 1884 | } |
| 1885 | |
Paolo Bonzini | 92176a8 | 2016-06-07 16:22:47 +0200 | [diff] [blame] | 1886 | static int hva_to_pfn_remapped(struct vm_area_struct *vma, |
| 1887 | unsigned long addr, bool *async, |
KarimAllah Ahmed | a340b3e | 2018-01-17 19:18:56 +0100 | [diff] [blame] | 1888 | bool write_fault, bool *writable, |
| 1889 | kvm_pfn_t *p_pfn) |
Paolo Bonzini | 92176a8 | 2016-06-07 16:22:47 +0200 | [diff] [blame] | 1890 | { |
Paolo Bonzini | add6a0c | 2016-06-07 17:51:18 +0200 | [diff] [blame] | 1891 | unsigned long pfn; |
| 1892 | int r; |
| 1893 | |
| 1894 | r = follow_pfn(vma, addr, &pfn); |
| 1895 | if (r) { |
| 1896 | /* |
| 1897 | * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does |
| 1898 | * not call the fault handler, so do it here. |
| 1899 | */ |
| 1900 | bool unlocked = false; |
Peter Xu | 64019a2 | 2020-08-11 18:39:01 -0700 | [diff] [blame] | 1901 | r = fixup_user_fault(current->mm, addr, |
Paolo Bonzini | add6a0c | 2016-06-07 17:51:18 +0200 | [diff] [blame] | 1902 | (write_fault ? FAULT_FLAG_WRITE : 0), |
| 1903 | &unlocked); |
Paolo Bonzini | a8387d0 | 2020-05-29 05:42:55 -0400 | [diff] [blame] | 1904 | if (unlocked) |
| 1905 | return -EAGAIN; |
Paolo Bonzini | add6a0c | 2016-06-07 17:51:18 +0200 | [diff] [blame] | 1906 | if (r) |
| 1907 | return r; |
| 1908 | |
| 1909 | r = follow_pfn(vma, addr, &pfn); |
| 1910 | if (r) |
| 1911 | return r; |
| 1912 | |
| 1913 | } |
| 1914 | |
KarimAllah Ahmed | a340b3e | 2018-01-17 19:18:56 +0100 | [diff] [blame] | 1915 | if (writable) |
| 1916 | *writable = true; |
Paolo Bonzini | add6a0c | 2016-06-07 17:51:18 +0200 | [diff] [blame] | 1917 | |
| 1918 | /* |
| 1919 | * Get a reference here because callers of *hva_to_pfn* and |
| 1920 | * *gfn_to_pfn* ultimately call kvm_release_pfn_clean on the |
| 1921 | * returned pfn. This is only needed if the VMA has VM_MIXEDMAP |
| 1922 | * set, but the kvm_get_pfn/kvm_release_pfn_clean pair will |
| 1923 | * simply do nothing for reserved pfns. |
| 1924 | * |
| 1925 | * Whoever called remap_pfn_range is also going to call e.g. |
| 1926 | * unmap_mapping_range before the underlying pages are freed, |
| 1927 | * causing a call to our MMU notifier. |
| 1928 | */ |
| 1929 | kvm_get_pfn(pfn); |
| 1930 | |
| 1931 | *p_pfn = pfn; |
Paolo Bonzini | 92176a8 | 2016-06-07 16:22:47 +0200 | [diff] [blame] | 1932 | return 0; |
| 1933 | } |
| 1934 | |
Xiao Guangrong | 12ce13f | 2012-08-21 11:00:49 +0800 | [diff] [blame] | 1935 | /* |
| 1936 | * Pin guest page in memory and return its pfn. |
| 1937 | * @addr: host virtual address which maps memory to the guest |
| 1938 | * @atomic: whether this function can sleep |
| 1939 | * @async: whether this function need to wait IO complete if the |
| 1940 | * host page is not in the memory |
| 1941 | * @write_fault: whether we should get a writable host page |
| 1942 | * @writable: whether it allows to map a writable host page for !@write_fault |
| 1943 | * |
| 1944 | * The function will map a writable host page for these two cases: |
| 1945 | * 1): @write_fault = true |
| 1946 | * 2): @write_fault = false && @writable, @writable will tell the caller |
| 1947 | * whether the mapping is writable. |
| 1948 | */ |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 1949 | static kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async, |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1950 | bool write_fault, bool *writable) |
| 1951 | { |
| 1952 | struct vm_area_struct *vma; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 1953 | kvm_pfn_t pfn = 0; |
Paolo Bonzini | 92176a8 | 2016-06-07 16:22:47 +0200 | [diff] [blame] | 1954 | int npages, r; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1955 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 1956 | /* we can do it either atomically or asynchronously, not both */ |
| 1957 | BUG_ON(atomic && async); |
| 1958 | |
Paolo Bonzini | b9b33da | 2018-07-27 17:44:41 +0200 | [diff] [blame] | 1959 | if (hva_to_pfn_fast(addr, write_fault, writable, &pfn)) |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1960 | return pfn; |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1961 | |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1962 | if (atomic) |
| 1963 | return KVM_PFN_ERR_FAULT; |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1964 | |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1965 | npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn); |
| 1966 | if (npages == 1) |
| 1967 | return pfn; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 1968 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1969 | mmap_read_lock(current->mm); |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1970 | if (npages == -EHWPOISON || |
| 1971 | (!async && check_user_page_hwpoison(addr))) { |
| 1972 | pfn = KVM_PFN_ERR_HWPOISON; |
| 1973 | goto exit; |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 1974 | } |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1975 | |
Paolo Bonzini | a8387d0 | 2020-05-29 05:42:55 -0400 | [diff] [blame] | 1976 | retry: |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1977 | vma = find_vma_intersection(current->mm, addr, addr + 1); |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 1978 | |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1979 | if (vma == NULL) |
| 1980 | pfn = KVM_PFN_ERR_FAULT; |
Paolo Bonzini | 92176a8 | 2016-06-07 16:22:47 +0200 | [diff] [blame] | 1981 | else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) { |
KarimAllah Ahmed | a340b3e | 2018-01-17 19:18:56 +0100 | [diff] [blame] | 1982 | r = hva_to_pfn_remapped(vma, addr, async, write_fault, writable, &pfn); |
Paolo Bonzini | a8387d0 | 2020-05-29 05:42:55 -0400 | [diff] [blame] | 1983 | if (r == -EAGAIN) |
| 1984 | goto retry; |
Paolo Bonzini | 92176a8 | 2016-06-07 16:22:47 +0200 | [diff] [blame] | 1985 | if (r < 0) |
| 1986 | pfn = KVM_PFN_ERR_FAULT; |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1987 | } else { |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 1988 | if (async && vma_is_valid(vma, write_fault)) |
Xiao Guangrong | 2fc8431 | 2012-08-21 11:00:22 +0800 | [diff] [blame] | 1989 | *async = true; |
| 1990 | pfn = KVM_PFN_ERR_FAULT; |
| 1991 | } |
| 1992 | exit: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1993 | mmap_read_unlock(current->mm); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1994 | return pfn; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1995 | } |
| 1996 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 1997 | kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, |
| 1998 | bool atomic, bool *async, bool write_fault, |
| 1999 | bool *writable) |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 2000 | { |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 2001 | unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault); |
| 2002 | |
Paolo Bonzini | b2740d3 | 2016-02-23 15:36:01 +0100 | [diff] [blame] | 2003 | if (addr == KVM_HVA_ERR_RO_BAD) { |
| 2004 | if (writable) |
| 2005 | *writable = false; |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 2006 | return KVM_PFN_ERR_RO_FAULT; |
Paolo Bonzini | b2740d3 | 2016-02-23 15:36:01 +0100 | [diff] [blame] | 2007 | } |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 2008 | |
Paolo Bonzini | b2740d3 | 2016-02-23 15:36:01 +0100 | [diff] [blame] | 2009 | if (kvm_is_error_hva(addr)) { |
| 2010 | if (writable) |
| 2011 | *writable = false; |
Xiao Guangrong | 81c52c5 | 2012-10-16 20:10:59 +0800 | [diff] [blame] | 2012 | return KVM_PFN_NOSLOT; |
Paolo Bonzini | b2740d3 | 2016-02-23 15:36:01 +0100 | [diff] [blame] | 2013 | } |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 2014 | |
| 2015 | /* Do not map writable pfn in the readonly memslot. */ |
| 2016 | if (writable && memslot_is_readonly(slot)) { |
| 2017 | *writable = false; |
| 2018 | writable = NULL; |
| 2019 | } |
| 2020 | |
| 2021 | return hva_to_pfn(addr, atomic, async, write_fault, |
| 2022 | writable); |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 2023 | } |
Paolo Bonzini | 3520469 | 2015-04-02 11:20:48 +0200 | [diff] [blame] | 2024 | EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot); |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 2025 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2026 | kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 2027 | bool *writable) |
| 2028 | { |
Paolo Bonzini | e37afc6 | 2015-05-19 16:09:04 +0200 | [diff] [blame] | 2029 | return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL, |
| 2030 | write_fault, writable); |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 2031 | } |
| 2032 | EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); |
| 2033 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2034 | kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn) |
Marcelo Tosatti | 506f0d6 | 2009-12-23 14:35:19 -0200 | [diff] [blame] | 2035 | { |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 2036 | return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL); |
Marcelo Tosatti | 506f0d6 | 2009-12-23 14:35:19 -0200 | [diff] [blame] | 2037 | } |
Paolo Bonzini | e37afc6 | 2015-05-19 16:09:04 +0200 | [diff] [blame] | 2038 | EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot); |
Marcelo Tosatti | 506f0d6 | 2009-12-23 14:35:19 -0200 | [diff] [blame] | 2039 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2040 | kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn) |
Xiao Guangrong | 037d92d | 2012-08-21 10:59:12 +0800 | [diff] [blame] | 2041 | { |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 2042 | return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL); |
Xiao Guangrong | 037d92d | 2012-08-21 10:59:12 +0800 | [diff] [blame] | 2043 | } |
| 2044 | EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic); |
| 2045 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2046 | kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn) |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2047 | { |
| 2048 | return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn); |
| 2049 | } |
| 2050 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic); |
| 2051 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2052 | kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) |
Paolo Bonzini | e37afc6 | 2015-05-19 16:09:04 +0200 | [diff] [blame] | 2053 | { |
| 2054 | return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn); |
| 2055 | } |
| 2056 | EXPORT_SYMBOL_GPL(gfn_to_pfn); |
| 2057 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2058 | kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2059 | { |
| 2060 | return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn); |
| 2061 | } |
| 2062 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn); |
| 2063 | |
Paolo Bonzini | d9ef13c | 2015-05-19 16:01:50 +0200 | [diff] [blame] | 2064 | int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn, |
| 2065 | struct page **pages, int nr_pages) |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 2066 | { |
| 2067 | unsigned long addr; |
Arnd Bergmann | 076b925 | 2017-08-10 14:14:39 +0200 | [diff] [blame] | 2068 | gfn_t entry = 0; |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 2069 | |
Paolo Bonzini | d9ef13c | 2015-05-19 16:01:50 +0200 | [diff] [blame] | 2070 | addr = gfn_to_hva_many(slot, gfn, &entry); |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 2071 | if (kvm_is_error_hva(addr)) |
| 2072 | return -1; |
| 2073 | |
| 2074 | if (entry < nr_pages) |
| 2075 | return 0; |
| 2076 | |
Souptick Joarder | dadbb61 | 2020-06-07 21:40:55 -0700 | [diff] [blame] | 2077 | return get_user_pages_fast_only(addr, nr_pages, FOLL_WRITE, pages); |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 2078 | } |
| 2079 | EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic); |
| 2080 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2081 | static struct page *kvm_pfn_to_page(kvm_pfn_t pfn) |
Xiao Guangrong | a276632 | 2012-07-26 11:58:59 +0800 | [diff] [blame] | 2082 | { |
Xiao Guangrong | 81c52c5 | 2012-10-16 20:10:59 +0800 | [diff] [blame] | 2083 | if (is_error_noslot_pfn(pfn)) |
Xiao Guangrong | 6cede2e | 2012-08-03 15:41:22 +0800 | [diff] [blame] | 2084 | return KVM_ERR_PTR_BAD_PAGE; |
Xiao Guangrong | a276632 | 2012-07-26 11:58:59 +0800 | [diff] [blame] | 2085 | |
Ard Biesheuvel | bf4bea8 | 2014-11-10 08:33:56 +0000 | [diff] [blame] | 2086 | if (kvm_is_reserved_pfn(pfn)) { |
Xiao Guangrong | cb9aaa3 | 2012-08-03 15:42:10 +0800 | [diff] [blame] | 2087 | WARN_ON(1); |
| 2088 | return KVM_ERR_PTR_BAD_PAGE; |
| 2089 | } |
| 2090 | |
Xiao Guangrong | a276632 | 2012-07-26 11:58:59 +0800 | [diff] [blame] | 2091 | return pfn_to_page(pfn); |
| 2092 | } |
| 2093 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2094 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
| 2095 | { |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2096 | kvm_pfn_t pfn; |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 2097 | |
| 2098 | pfn = gfn_to_pfn(kvm, gfn); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 2099 | |
Xiao Guangrong | a276632 | 2012-07-26 11:58:59 +0800 | [diff] [blame] | 2100 | return kvm_pfn_to_page(pfn); |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 2101 | } |
| 2102 | EXPORT_SYMBOL_GPL(gfn_to_page); |
| 2103 | |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2104 | void kvm_release_pfn(kvm_pfn_t pfn, bool dirty, struct gfn_to_pfn_cache *cache) |
| 2105 | { |
| 2106 | if (pfn == 0) |
| 2107 | return; |
| 2108 | |
| 2109 | if (cache) |
| 2110 | cache->pfn = cache->gfn = 0; |
| 2111 | |
| 2112 | if (dirty) |
| 2113 | kvm_release_pfn_dirty(pfn); |
| 2114 | else |
| 2115 | kvm_release_pfn_clean(pfn); |
| 2116 | } |
| 2117 | |
| 2118 | static void kvm_cache_gfn_to_pfn(struct kvm_memory_slot *slot, gfn_t gfn, |
| 2119 | struct gfn_to_pfn_cache *cache, u64 gen) |
| 2120 | { |
| 2121 | kvm_release_pfn(cache->pfn, cache->dirty, cache); |
| 2122 | |
| 2123 | cache->pfn = gfn_to_pfn_memslot(slot, gfn); |
| 2124 | cache->gfn = gfn; |
| 2125 | cache->dirty = false; |
| 2126 | cache->generation = gen; |
| 2127 | } |
| 2128 | |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2129 | static int __kvm_map_gfn(struct kvm_memslots *slots, gfn_t gfn, |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2130 | struct kvm_host_map *map, |
| 2131 | struct gfn_to_pfn_cache *cache, |
| 2132 | bool atomic) |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2133 | { |
| 2134 | kvm_pfn_t pfn; |
| 2135 | void *hva = NULL; |
| 2136 | struct page *page = KVM_UNMAPPED_PAGE; |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2137 | struct kvm_memory_slot *slot = __gfn_to_memslot(slots, gfn); |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2138 | u64 gen = slots->generation; |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2139 | |
| 2140 | if (!map) |
| 2141 | return -EINVAL; |
| 2142 | |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2143 | if (cache) { |
| 2144 | if (!cache->pfn || cache->gfn != gfn || |
| 2145 | cache->generation != gen) { |
| 2146 | if (atomic) |
| 2147 | return -EAGAIN; |
| 2148 | kvm_cache_gfn_to_pfn(slot, gfn, cache, gen); |
| 2149 | } |
| 2150 | pfn = cache->pfn; |
| 2151 | } else { |
| 2152 | if (atomic) |
| 2153 | return -EAGAIN; |
| 2154 | pfn = gfn_to_pfn_memslot(slot, gfn); |
| 2155 | } |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2156 | if (is_error_noslot_pfn(pfn)) |
| 2157 | return -EINVAL; |
| 2158 | |
| 2159 | if (pfn_valid(pfn)) { |
| 2160 | page = pfn_to_page(pfn); |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2161 | if (atomic) |
| 2162 | hva = kmap_atomic(page); |
| 2163 | else |
| 2164 | hva = kmap(page); |
Paolo Bonzini | d30b214 | 2019-05-20 12:06:36 +0200 | [diff] [blame] | 2165 | #ifdef CONFIG_HAS_IOMEM |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2166 | } else if (!atomic) { |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2167 | hva = memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB); |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2168 | } else { |
| 2169 | return -EINVAL; |
Paolo Bonzini | d30b214 | 2019-05-20 12:06:36 +0200 | [diff] [blame] | 2170 | #endif |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | if (!hva) |
| 2174 | return -EFAULT; |
| 2175 | |
| 2176 | map->page = page; |
| 2177 | map->hva = hva; |
| 2178 | map->pfn = pfn; |
| 2179 | map->gfn = gfn; |
| 2180 | |
| 2181 | return 0; |
| 2182 | } |
| 2183 | |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2184 | int kvm_map_gfn(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map, |
| 2185 | struct gfn_to_pfn_cache *cache, bool atomic) |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2186 | { |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2187 | return __kvm_map_gfn(kvm_memslots(vcpu->kvm), gfn, map, |
| 2188 | cache, atomic); |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2189 | } |
| 2190 | EXPORT_SYMBOL_GPL(kvm_map_gfn); |
| 2191 | |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2192 | int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) |
| 2193 | { |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2194 | return __kvm_map_gfn(kvm_vcpu_memslots(vcpu), gfn, map, |
| 2195 | NULL, false); |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2196 | } |
| 2197 | EXPORT_SYMBOL_GPL(kvm_vcpu_map); |
| 2198 | |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2199 | static void __kvm_unmap_gfn(struct kvm_memory_slot *memslot, |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2200 | struct kvm_host_map *map, |
| 2201 | struct gfn_to_pfn_cache *cache, |
| 2202 | bool dirty, bool atomic) |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2203 | { |
| 2204 | if (!map) |
| 2205 | return; |
| 2206 | |
| 2207 | if (!map->hva) |
| 2208 | return; |
| 2209 | |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2210 | if (map->page != KVM_UNMAPPED_PAGE) { |
| 2211 | if (atomic) |
| 2212 | kunmap_atomic(map->hva); |
| 2213 | else |
| 2214 | kunmap(map->page); |
| 2215 | } |
Christian Borntraeger | eb1f2f3 | 2019-05-27 10:28:25 +0200 | [diff] [blame] | 2216 | #ifdef CONFIG_HAS_IOMEM |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2217 | else if (!atomic) |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2218 | memunmap(map->hva); |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2219 | else |
| 2220 | WARN_ONCE(1, "Unexpected unmapping in atomic context"); |
Christian Borntraeger | eb1f2f3 | 2019-05-27 10:28:25 +0200 | [diff] [blame] | 2221 | #endif |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2222 | |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2223 | if (dirty) |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2224 | mark_page_dirty_in_slot(memslot, map->gfn); |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2225 | |
| 2226 | if (cache) |
| 2227 | cache->dirty |= dirty; |
| 2228 | else |
| 2229 | kvm_release_pfn(map->pfn, dirty, NULL); |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2230 | |
| 2231 | map->hva = NULL; |
| 2232 | map->page = NULL; |
| 2233 | } |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2234 | |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2235 | int kvm_unmap_gfn(struct kvm_vcpu *vcpu, struct kvm_host_map *map, |
| 2236 | struct gfn_to_pfn_cache *cache, bool dirty, bool atomic) |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2237 | { |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2238 | __kvm_unmap_gfn(gfn_to_memslot(vcpu->kvm, map->gfn), map, |
| 2239 | cache, dirty, atomic); |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2240 | return 0; |
| 2241 | } |
| 2242 | EXPORT_SYMBOL_GPL(kvm_unmap_gfn); |
| 2243 | |
| 2244 | void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty) |
| 2245 | { |
Boris Ostrovsky | 9172481 | 2019-12-05 01:30:51 +0000 | [diff] [blame] | 2246 | __kvm_unmap_gfn(kvm_vcpu_gfn_to_memslot(vcpu, map->gfn), map, NULL, |
| 2247 | dirty, false); |
Boris Ostrovsky | 1eff70a | 2019-11-12 16:35:06 +0000 | [diff] [blame] | 2248 | } |
KarimAllah Ahmed | e45adf6 | 2019-01-31 21:24:34 +0100 | [diff] [blame] | 2249 | EXPORT_SYMBOL_GPL(kvm_vcpu_unmap); |
| 2250 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2251 | struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 2252 | { |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2253 | kvm_pfn_t pfn; |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2254 | |
| 2255 | pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn); |
| 2256 | |
| 2257 | return kvm_pfn_to_page(pfn); |
| 2258 | } |
| 2259 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page); |
| 2260 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 2261 | void kvm_release_page_clean(struct page *page) |
| 2262 | { |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 2263 | WARN_ON(is_error_page(page)); |
| 2264 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2265 | kvm_release_pfn_clean(page_to_pfn(page)); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 2266 | } |
| 2267 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); |
| 2268 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2269 | void kvm_release_pfn_clean(kvm_pfn_t pfn) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2270 | { |
Ard Biesheuvel | bf4bea8 | 2014-11-10 08:33:56 +0000 | [diff] [blame] | 2271 | if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 2272 | put_page(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2273 | } |
| 2274 | EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); |
| 2275 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 2276 | void kvm_release_page_dirty(struct page *page) |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 2277 | { |
Xiao Guangrong | a276632 | 2012-07-26 11:58:59 +0800 | [diff] [blame] | 2278 | WARN_ON(is_error_page(page)); |
| 2279 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2280 | kvm_release_pfn_dirty(page_to_pfn(page)); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 2281 | } |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 2282 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 2283 | |
David Hildenbrand | f7a6509 | 2017-09-01 17:11:43 +0200 | [diff] [blame] | 2284 | void kvm_release_pfn_dirty(kvm_pfn_t pfn) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2285 | { |
| 2286 | kvm_set_pfn_dirty(pfn); |
| 2287 | kvm_release_pfn_clean(pfn); |
| 2288 | } |
David Hildenbrand | f7a6509 | 2017-09-01 17:11:43 +0200 | [diff] [blame] | 2289 | EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2290 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2291 | void kvm_set_pfn_dirty(kvm_pfn_t pfn) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2292 | { |
Miaohe Lin | d29c03a | 2019-12-05 11:05:05 +0800 | [diff] [blame] | 2293 | if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn)) |
| 2294 | SetPageDirty(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2295 | } |
| 2296 | EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); |
| 2297 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2298 | void kvm_set_pfn_accessed(kvm_pfn_t pfn) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2299 | { |
Sean Christopherson | a78986a | 2019-11-11 14:12:27 -0800 | [diff] [blame] | 2300 | if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 2301 | mark_page_accessed(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2302 | } |
| 2303 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); |
| 2304 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2305 | void kvm_get_pfn(kvm_pfn_t pfn) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2306 | { |
Ard Biesheuvel | bf4bea8 | 2014-11-10 08:33:56 +0000 | [diff] [blame] | 2307 | if (!kvm_is_reserved_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 2308 | get_page(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2309 | } |
| 2310 | EXPORT_SYMBOL_GPL(kvm_get_pfn); |
| 2311 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2312 | static int next_segment(unsigned long len, int offset) |
| 2313 | { |
| 2314 | if (len > PAGE_SIZE - offset) |
| 2315 | return PAGE_SIZE - offset; |
| 2316 | else |
| 2317 | return len; |
| 2318 | } |
| 2319 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2320 | static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, |
| 2321 | void *data, int offset, int len) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2322 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 2323 | int r; |
| 2324 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2325 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2326 | addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 2327 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2328 | return -EFAULT; |
Paolo Bonzini | 3180a7f | 2015-04-02 14:08:20 +0200 | [diff] [blame] | 2329 | r = __copy_from_user(data, (void __user *)addr + offset, len); |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 2330 | if (r) |
| 2331 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2332 | return 0; |
| 2333 | } |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2334 | |
| 2335 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 2336 | int len) |
| 2337 | { |
| 2338 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); |
| 2339 | |
| 2340 | return __kvm_read_guest_page(slot, gfn, data, offset, len); |
| 2341 | } |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2342 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
| 2343 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2344 | int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, |
| 2345 | int offset, int len) |
| 2346 | { |
| 2347 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
| 2348 | |
| 2349 | return __kvm_read_guest_page(slot, gfn, data, offset, len); |
| 2350 | } |
| 2351 | EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page); |
| 2352 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2353 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
| 2354 | { |
| 2355 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 2356 | int seg; |
| 2357 | int offset = offset_in_page(gpa); |
| 2358 | int ret; |
| 2359 | |
| 2360 | while ((seg = next_segment(len, offset)) != 0) { |
| 2361 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); |
| 2362 | if (ret < 0) |
| 2363 | return ret; |
| 2364 | offset = 0; |
| 2365 | len -= seg; |
| 2366 | data += seg; |
| 2367 | ++gfn; |
| 2368 | } |
| 2369 | return 0; |
| 2370 | } |
| 2371 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 2372 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2373 | int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, unsigned long len) |
| 2374 | { |
| 2375 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 2376 | int seg; |
| 2377 | int offset = offset_in_page(gpa); |
| 2378 | int ret; |
| 2379 | |
| 2380 | while ((seg = next_segment(len, offset)) != 0) { |
| 2381 | ret = kvm_vcpu_read_guest_page(vcpu, gfn, data, offset, seg); |
| 2382 | if (ret < 0) |
| 2383 | return ret; |
| 2384 | offset = 0; |
| 2385 | len -= seg; |
| 2386 | data += seg; |
| 2387 | ++gfn; |
| 2388 | } |
| 2389 | return 0; |
| 2390 | } |
| 2391 | EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest); |
| 2392 | |
| 2393 | static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn, |
| 2394 | void *data, int offset, unsigned long len) |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 2395 | { |
| 2396 | int r; |
| 2397 | unsigned long addr; |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 2398 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2399 | addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 2400 | if (kvm_is_error_hva(addr)) |
| 2401 | return -EFAULT; |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 2402 | pagefault_disable(); |
Paolo Bonzini | 3180a7f | 2015-04-02 14:08:20 +0200 | [diff] [blame] | 2403 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 2404 | pagefault_enable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 2405 | if (r) |
| 2406 | return -EFAULT; |
| 2407 | return 0; |
| 2408 | } |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 2409 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2410 | int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 2411 | void *data, unsigned long len) |
| 2412 | { |
| 2413 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 2414 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
| 2415 | int offset = offset_in_page(gpa); |
| 2416 | |
| 2417 | return __kvm_read_guest_atomic(slot, gfn, data, offset, len); |
| 2418 | } |
| 2419 | EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic); |
| 2420 | |
| 2421 | static int __kvm_write_guest_page(struct kvm_memory_slot *memslot, gfn_t gfn, |
| 2422 | const void *data, int offset, int len) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2423 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 2424 | int r; |
| 2425 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2426 | |
Radim Krčmář | 251eb84 | 2015-04-10 21:47:27 +0200 | [diff] [blame] | 2427 | addr = gfn_to_hva_memslot(memslot, gfn); |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 2428 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2429 | return -EFAULT; |
Xiao Guangrong | 8b0cedf | 2011-05-15 23:22:04 +0800 | [diff] [blame] | 2430 | r = __copy_to_user((void __user *)addr + offset, data, len); |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 2431 | if (r) |
| 2432 | return -EFAULT; |
Paolo Bonzini | bc009e4 | 2015-05-26 12:43:41 +0200 | [diff] [blame] | 2433 | mark_page_dirty_in_slot(memslot, gfn); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2434 | return 0; |
| 2435 | } |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2436 | |
| 2437 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, |
| 2438 | const void *data, int offset, int len) |
| 2439 | { |
| 2440 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); |
| 2441 | |
| 2442 | return __kvm_write_guest_page(slot, gfn, data, offset, len); |
| 2443 | } |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2444 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
| 2445 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2446 | int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 2447 | const void *data, int offset, int len) |
| 2448 | { |
| 2449 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
| 2450 | |
| 2451 | return __kvm_write_guest_page(slot, gfn, data, offset, len); |
| 2452 | } |
| 2453 | EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page); |
| 2454 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2455 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 2456 | unsigned long len) |
| 2457 | { |
| 2458 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 2459 | int seg; |
| 2460 | int offset = offset_in_page(gpa); |
| 2461 | int ret; |
| 2462 | |
| 2463 | while ((seg = next_segment(len, offset)) != 0) { |
| 2464 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); |
| 2465 | if (ret < 0) |
| 2466 | return ret; |
| 2467 | offset = 0; |
| 2468 | len -= seg; |
| 2469 | data += seg; |
| 2470 | ++gfn; |
| 2471 | } |
| 2472 | return 0; |
| 2473 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 2474 | EXPORT_SYMBOL_GPL(kvm_write_guest); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2475 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2476 | int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data, |
| 2477 | unsigned long len) |
| 2478 | { |
| 2479 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 2480 | int seg; |
| 2481 | int offset = offset_in_page(gpa); |
| 2482 | int ret; |
| 2483 | |
| 2484 | while ((seg = next_segment(len, offset)) != 0) { |
| 2485 | ret = kvm_vcpu_write_guest_page(vcpu, gfn, data, offset, seg); |
| 2486 | if (ret < 0) |
| 2487 | return ret; |
| 2488 | offset = 0; |
| 2489 | len -= seg; |
| 2490 | data += seg; |
| 2491 | ++gfn; |
| 2492 | } |
| 2493 | return 0; |
| 2494 | } |
| 2495 | EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest); |
| 2496 | |
Paolo Bonzini | 5a2d436 | 2017-02-03 20:32:28 -0800 | [diff] [blame] | 2497 | static int __kvm_gfn_to_hva_cache_init(struct kvm_memslots *slots, |
| 2498 | struct gfn_to_hva_cache *ghc, |
| 2499 | gpa_t gpa, unsigned long len) |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2500 | { |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2501 | int offset = offset_in_page(gpa); |
Andrew Honig | 8f96452 | 2013-03-29 09:35:21 -0700 | [diff] [blame] | 2502 | gfn_t start_gfn = gpa >> PAGE_SHIFT; |
| 2503 | gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT; |
| 2504 | gfn_t nr_pages_needed = end_gfn - start_gfn + 1; |
| 2505 | gfn_t nr_pages_avail; |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2506 | |
Sean Christopherson | 6ad1e29 | 2020-01-09 14:58:55 -0500 | [diff] [blame] | 2507 | /* Update ghc->generation before performing any error checks. */ |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2508 | ghc->generation = slots->generation; |
Sean Christopherson | 6ad1e29 | 2020-01-09 14:58:55 -0500 | [diff] [blame] | 2509 | |
| 2510 | if (start_gfn > end_gfn) { |
| 2511 | ghc->hva = KVM_HVA_ERR_BAD; |
| 2512 | return -EINVAL; |
| 2513 | } |
Jim Mattson | f1b9dd5 | 2018-12-17 13:53:33 -0800 | [diff] [blame] | 2514 | |
| 2515 | /* |
| 2516 | * If the requested region crosses two memslots, we still |
| 2517 | * verify that the entire region is valid here. |
| 2518 | */ |
Sean Christopherson | 6ad1e29 | 2020-01-09 14:58:55 -0500 | [diff] [blame] | 2519 | for ( ; start_gfn <= end_gfn; start_gfn += nr_pages_avail) { |
Jim Mattson | f1b9dd5 | 2018-12-17 13:53:33 -0800 | [diff] [blame] | 2520 | ghc->memslot = __gfn_to_memslot(slots, start_gfn); |
| 2521 | ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, |
| 2522 | &nr_pages_avail); |
| 2523 | if (kvm_is_error_hva(ghc->hva)) |
Sean Christopherson | 6ad1e29 | 2020-01-09 14:58:55 -0500 | [diff] [blame] | 2524 | return -EFAULT; |
Andrew Honig | 8f96452 | 2013-03-29 09:35:21 -0700 | [diff] [blame] | 2525 | } |
Jim Mattson | f1b9dd5 | 2018-12-17 13:53:33 -0800 | [diff] [blame] | 2526 | |
| 2527 | /* Use the slow path for cross page reads and writes. */ |
Sean Christopherson | 6ad1e29 | 2020-01-09 14:58:55 -0500 | [diff] [blame] | 2528 | if (nr_pages_needed == 1) |
Jim Mattson | f1b9dd5 | 2018-12-17 13:53:33 -0800 | [diff] [blame] | 2529 | ghc->hva += offset; |
| 2530 | else |
| 2531 | ghc->memslot = NULL; |
| 2532 | |
Sean Christopherson | 6ad1e29 | 2020-01-09 14:58:55 -0500 | [diff] [blame] | 2533 | ghc->gpa = gpa; |
| 2534 | ghc->len = len; |
| 2535 | return 0; |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2536 | } |
Paolo Bonzini | 5a2d436 | 2017-02-03 20:32:28 -0800 | [diff] [blame] | 2537 | |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2538 | int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
Paolo Bonzini | 5a2d436 | 2017-02-03 20:32:28 -0800 | [diff] [blame] | 2539 | gpa_t gpa, unsigned long len) |
| 2540 | { |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2541 | struct kvm_memslots *slots = kvm_memslots(kvm); |
Paolo Bonzini | 5a2d436 | 2017-02-03 20:32:28 -0800 | [diff] [blame] | 2542 | return __kvm_gfn_to_hva_cache_init(slots, ghc, gpa, len); |
| 2543 | } |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2544 | EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init); |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2545 | |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2546 | int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
Jim Mattson | 7a86dab | 2018-12-14 14:34:43 -0800 | [diff] [blame] | 2547 | void *data, unsigned int offset, |
| 2548 | unsigned long len) |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2549 | { |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2550 | struct kvm_memslots *slots = kvm_memslots(kvm); |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2551 | int r; |
Pan Xinhui | 4ec6e86 | 2016-11-02 05:08:34 -0400 | [diff] [blame] | 2552 | gpa_t gpa = ghc->gpa + offset; |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2553 | |
Pan Xinhui | 4ec6e86 | 2016-11-02 05:08:34 -0400 | [diff] [blame] | 2554 | BUG_ON(len + offset > ghc->len); |
Andrew Honig | 8f96452 | 2013-03-29 09:35:21 -0700 | [diff] [blame] | 2555 | |
Sean Christopherson | dc9ce71 | 2020-01-09 15:56:20 -0800 | [diff] [blame] | 2556 | if (slots->generation != ghc->generation) { |
| 2557 | if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len)) |
| 2558 | return -EFAULT; |
| 2559 | } |
Andrew Honig | 8f96452 | 2013-03-29 09:35:21 -0700 | [diff] [blame] | 2560 | |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2561 | if (kvm_is_error_hva(ghc->hva)) |
| 2562 | return -EFAULT; |
| 2563 | |
Sean Christopherson | fcfbc61 | 2020-01-09 15:56:18 -0800 | [diff] [blame] | 2564 | if (unlikely(!ghc->memslot)) |
| 2565 | return kvm_write_guest(kvm, gpa, data, len); |
| 2566 | |
Pan Xinhui | 4ec6e86 | 2016-11-02 05:08:34 -0400 | [diff] [blame] | 2567 | r = __copy_to_user((void __user *)ghc->hva + offset, data, len); |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2568 | if (r) |
| 2569 | return -EFAULT; |
Pan Xinhui | 4ec6e86 | 2016-11-02 05:08:34 -0400 | [diff] [blame] | 2570 | mark_page_dirty_in_slot(ghc->memslot, gpa >> PAGE_SHIFT); |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2571 | |
| 2572 | return 0; |
| 2573 | } |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2574 | EXPORT_SYMBOL_GPL(kvm_write_guest_offset_cached); |
Pan Xinhui | 4ec6e86 | 2016-11-02 05:08:34 -0400 | [diff] [blame] | 2575 | |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2576 | int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
| 2577 | void *data, unsigned long len) |
Pan Xinhui | 4ec6e86 | 2016-11-02 05:08:34 -0400 | [diff] [blame] | 2578 | { |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2579 | return kvm_write_guest_offset_cached(kvm, ghc, data, 0, len); |
Pan Xinhui | 4ec6e86 | 2016-11-02 05:08:34 -0400 | [diff] [blame] | 2580 | } |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2581 | EXPORT_SYMBOL_GPL(kvm_write_guest_cached); |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2582 | |
Vitaly Kuznetsov | 0958f0c | 2020-05-25 16:41:19 +0200 | [diff] [blame] | 2583 | int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
| 2584 | void *data, unsigned int offset, |
| 2585 | unsigned long len) |
Gleb Natapov | e03b644 | 2011-07-11 15:28:11 -0400 | [diff] [blame] | 2586 | { |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2587 | struct kvm_memslots *slots = kvm_memslots(kvm); |
Gleb Natapov | e03b644 | 2011-07-11 15:28:11 -0400 | [diff] [blame] | 2588 | int r; |
Vitaly Kuznetsov | 0958f0c | 2020-05-25 16:41:19 +0200 | [diff] [blame] | 2589 | gpa_t gpa = ghc->gpa + offset; |
Gleb Natapov | e03b644 | 2011-07-11 15:28:11 -0400 | [diff] [blame] | 2590 | |
Vitaly Kuznetsov | 0958f0c | 2020-05-25 16:41:19 +0200 | [diff] [blame] | 2591 | BUG_ON(len + offset > ghc->len); |
Andrew Honig | 8f96452 | 2013-03-29 09:35:21 -0700 | [diff] [blame] | 2592 | |
Sean Christopherson | dc9ce71 | 2020-01-09 15:56:20 -0800 | [diff] [blame] | 2593 | if (slots->generation != ghc->generation) { |
| 2594 | if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len)) |
| 2595 | return -EFAULT; |
| 2596 | } |
Andrew Honig | 8f96452 | 2013-03-29 09:35:21 -0700 | [diff] [blame] | 2597 | |
Gleb Natapov | e03b644 | 2011-07-11 15:28:11 -0400 | [diff] [blame] | 2598 | if (kvm_is_error_hva(ghc->hva)) |
| 2599 | return -EFAULT; |
| 2600 | |
Sean Christopherson | fcfbc61 | 2020-01-09 15:56:18 -0800 | [diff] [blame] | 2601 | if (unlikely(!ghc->memslot)) |
Vitaly Kuznetsov | 0958f0c | 2020-05-25 16:41:19 +0200 | [diff] [blame] | 2602 | return kvm_read_guest(kvm, gpa, data, len); |
Sean Christopherson | fcfbc61 | 2020-01-09 15:56:18 -0800 | [diff] [blame] | 2603 | |
Vitaly Kuznetsov | 0958f0c | 2020-05-25 16:41:19 +0200 | [diff] [blame] | 2604 | r = __copy_from_user(data, (void __user *)ghc->hva + offset, len); |
Gleb Natapov | e03b644 | 2011-07-11 15:28:11 -0400 | [diff] [blame] | 2605 | if (r) |
| 2606 | return -EFAULT; |
| 2607 | |
| 2608 | return 0; |
| 2609 | } |
Vitaly Kuznetsov | 0958f0c | 2020-05-25 16:41:19 +0200 | [diff] [blame] | 2610 | EXPORT_SYMBOL_GPL(kvm_read_guest_offset_cached); |
| 2611 | |
| 2612 | int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
| 2613 | void *data, unsigned long len) |
| 2614 | { |
| 2615 | return kvm_read_guest_offset_cached(kvm, ghc, data, 0, len); |
| 2616 | } |
Paolo Bonzini | 4e335d9 | 2017-05-02 16:20:18 +0200 | [diff] [blame] | 2617 | EXPORT_SYMBOL_GPL(kvm_read_guest_cached); |
Gleb Natapov | e03b644 | 2011-07-11 15:28:11 -0400 | [diff] [blame] | 2618 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2619 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) |
| 2620 | { |
Paolo Bonzini | 2f54144 | 2020-11-06 05:25:09 -0500 | [diff] [blame^] | 2621 | const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0))); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2622 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 2623 | int seg; |
| 2624 | int offset = offset_in_page(gpa); |
| 2625 | int ret; |
| 2626 | |
Kevin Mulvey | bfda0e8 | 2015-02-20 08:21:36 -0500 | [diff] [blame] | 2627 | while ((seg = next_segment(len, offset)) != 0) { |
Paolo Bonzini | 2f54144 | 2020-11-06 05:25:09 -0500 | [diff] [blame^] | 2628 | ret = kvm_write_guest_page(kvm, gfn, zero_page, offset, len); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2629 | if (ret < 0) |
| 2630 | return ret; |
| 2631 | offset = 0; |
| 2632 | len -= seg; |
| 2633 | ++gfn; |
| 2634 | } |
| 2635 | return 0; |
| 2636 | } |
| 2637 | EXPORT_SYMBOL_GPL(kvm_clear_guest); |
| 2638 | |
Ben Gardon | a6a0b05 | 2020-10-14 11:26:55 -0700 | [diff] [blame] | 2639 | void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot, gfn_t gfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2640 | { |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 2641 | if (memslot && memslot->dirty_bitmap) { |
| 2642 | unsigned long rel_gfn = gfn - memslot->base_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2643 | |
Takuya Yoshikawa | b74ca3b | 2012-10-04 17:13:12 -0700 | [diff] [blame] | 2644 | set_bit_le(rel_gfn, memslot->dirty_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2645 | } |
| 2646 | } |
Ben Gardon | a6a0b05 | 2020-10-14 11:26:55 -0700 | [diff] [blame] | 2647 | EXPORT_SYMBOL_GPL(mark_page_dirty_in_slot); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2648 | |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2649 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 2650 | { |
| 2651 | struct kvm_memory_slot *memslot; |
| 2652 | |
| 2653 | memslot = gfn_to_memslot(kvm, gfn); |
Paolo Bonzini | bc009e4 | 2015-05-26 12:43:41 +0200 | [diff] [blame] | 2654 | mark_page_dirty_in_slot(memslot, gfn); |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2655 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 2656 | EXPORT_SYMBOL_GPL(mark_page_dirty); |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 2657 | |
Paolo Bonzini | 8e73485 | 2015-05-17 13:58:53 +0200 | [diff] [blame] | 2658 | void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 2659 | { |
| 2660 | struct kvm_memory_slot *memslot; |
| 2661 | |
| 2662 | memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
| 2663 | mark_page_dirty_in_slot(memslot, gfn); |
| 2664 | } |
| 2665 | EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty); |
| 2666 | |
Jan H. Schönherr | 20b7035 | 2017-11-24 22:39:01 +0100 | [diff] [blame] | 2667 | void kvm_sigset_activate(struct kvm_vcpu *vcpu) |
| 2668 | { |
| 2669 | if (!vcpu->sigset_active) |
| 2670 | return; |
| 2671 | |
| 2672 | /* |
| 2673 | * This does a lockless modification of ->real_blocked, which is fine |
| 2674 | * because, only current can change ->real_blocked and all readers of |
| 2675 | * ->real_blocked don't care as long ->real_blocked is always a subset |
| 2676 | * of ->blocked. |
| 2677 | */ |
| 2678 | sigprocmask(SIG_SETMASK, &vcpu->sigset, ¤t->real_blocked); |
| 2679 | } |
| 2680 | |
| 2681 | void kvm_sigset_deactivate(struct kvm_vcpu *vcpu) |
| 2682 | { |
| 2683 | if (!vcpu->sigset_active) |
| 2684 | return; |
| 2685 | |
| 2686 | sigprocmask(SIG_SETMASK, ¤t->real_blocked, NULL); |
| 2687 | sigemptyset(¤t->real_blocked); |
| 2688 | } |
| 2689 | |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2690 | static void grow_halt_poll_ns(struct kvm_vcpu *vcpu) |
| 2691 | { |
Nir Weiner | dee339b | 2019-01-27 12:17:16 +0200 | [diff] [blame] | 2692 | unsigned int old, val, grow, grow_start; |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2693 | |
Wanpeng Li | 2cbd782 | 2015-09-03 22:07:39 +0800 | [diff] [blame] | 2694 | old = val = vcpu->halt_poll_ns; |
Nir Weiner | dee339b | 2019-01-27 12:17:16 +0200 | [diff] [blame] | 2695 | grow_start = READ_ONCE(halt_poll_ns_grow_start); |
Christian Borntraeger | 6b6de68 | 2016-02-09 13:47:55 +0100 | [diff] [blame] | 2696 | grow = READ_ONCE(halt_poll_ns_grow); |
Nir Weiner | 7fa08e7 | 2019-01-27 12:17:14 +0200 | [diff] [blame] | 2697 | if (!grow) |
| 2698 | goto out; |
| 2699 | |
Nir Weiner | dee339b | 2019-01-27 12:17:16 +0200 | [diff] [blame] | 2700 | val *= grow; |
| 2701 | if (val < grow_start) |
| 2702 | val = grow_start; |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2703 | |
David Matlack | 313f636 | 2016-03-08 16:19:44 -0800 | [diff] [blame] | 2704 | if (val > halt_poll_ns) |
| 2705 | val = halt_poll_ns; |
| 2706 | |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2707 | vcpu->halt_poll_ns = val; |
Nir Weiner | 7fa08e7 | 2019-01-27 12:17:14 +0200 | [diff] [blame] | 2708 | out: |
Wanpeng Li | 2cbd782 | 2015-09-03 22:07:39 +0800 | [diff] [blame] | 2709 | trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old); |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu) |
| 2713 | { |
Christian Borntraeger | 6b6de68 | 2016-02-09 13:47:55 +0100 | [diff] [blame] | 2714 | unsigned int old, val, shrink; |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2715 | |
Wanpeng Li | 2cbd782 | 2015-09-03 22:07:39 +0800 | [diff] [blame] | 2716 | old = val = vcpu->halt_poll_ns; |
Christian Borntraeger | 6b6de68 | 2016-02-09 13:47:55 +0100 | [diff] [blame] | 2717 | shrink = READ_ONCE(halt_poll_ns_shrink); |
| 2718 | if (shrink == 0) |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2719 | val = 0; |
| 2720 | else |
Christian Borntraeger | 6b6de68 | 2016-02-09 13:47:55 +0100 | [diff] [blame] | 2721 | val /= shrink; |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2722 | |
| 2723 | vcpu->halt_poll_ns = val; |
Wanpeng Li | 2cbd782 | 2015-09-03 22:07:39 +0800 | [diff] [blame] | 2724 | trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old); |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2725 | } |
| 2726 | |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2727 | static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu) |
| 2728 | { |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 2729 | int ret = -EINTR; |
| 2730 | int idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 2731 | |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2732 | if (kvm_arch_vcpu_runnable(vcpu)) { |
| 2733 | kvm_make_request(KVM_REQ_UNHALT, vcpu); |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 2734 | goto out; |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2735 | } |
| 2736 | if (kvm_cpu_has_pending_timer(vcpu)) |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 2737 | goto out; |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2738 | if (signal_pending(current)) |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 2739 | goto out; |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2740 | |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 2741 | ret = 0; |
| 2742 | out: |
| 2743 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 2744 | return ret; |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2745 | } |
| 2746 | |
David Matlack | cb95312 | 2020-05-08 11:22:40 -0700 | [diff] [blame] | 2747 | static inline void |
| 2748 | update_halt_poll_stats(struct kvm_vcpu *vcpu, u64 poll_ns, bool waited) |
| 2749 | { |
| 2750 | if (waited) |
| 2751 | vcpu->stat.halt_poll_fail_ns += poll_ns; |
| 2752 | else |
| 2753 | vcpu->stat.halt_poll_success_ns += poll_ns; |
| 2754 | } |
| 2755 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 2756 | /* |
| 2757 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. |
| 2758 | */ |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2759 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 2760 | { |
David Matlack | cb95312 | 2020-05-08 11:22:40 -0700 | [diff] [blame] | 2761 | ktime_t start, cur, poll_end; |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2762 | bool waited = false; |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2763 | u64 block_ns; |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2764 | |
Marc Zyngier | 07ab0f8 | 2019-08-02 11:37:09 +0100 | [diff] [blame] | 2765 | kvm_arch_vcpu_blocking(vcpu); |
| 2766 | |
David Matlack | cb95312 | 2020-05-08 11:22:40 -0700 | [diff] [blame] | 2767 | start = cur = poll_end = ktime_get(); |
Christian Borntraeger | cdd6ad3 | 2019-03-05 05:30:01 -0500 | [diff] [blame] | 2768 | if (vcpu->halt_poll_ns && !kvm_arch_no_poll(vcpu)) { |
Wanpeng Li | 19020f8 | 2015-09-03 22:07:37 +0800 | [diff] [blame] | 2769 | ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns); |
Xiubo Li | f95ef0cd | 2015-02-26 14:58:23 +0800 | [diff] [blame] | 2770 | |
Paolo Bonzini | 62bea5b | 2015-09-15 18:27:57 +0200 | [diff] [blame] | 2771 | ++vcpu->stat.halt_attempted_poll; |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2772 | do { |
| 2773 | /* |
| 2774 | * This sets KVM_REQ_UNHALT if an interrupt |
| 2775 | * arrives. |
| 2776 | */ |
| 2777 | if (kvm_vcpu_check_block(vcpu) < 0) { |
| 2778 | ++vcpu->stat.halt_successful_poll; |
Christian Borntraeger | 3491caf | 2016-05-13 12:16:35 +0200 | [diff] [blame] | 2779 | if (!vcpu_valid_wakeup(vcpu)) |
| 2780 | ++vcpu->stat.halt_poll_invalid; |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2781 | goto out; |
| 2782 | } |
David Matlack | cb95312 | 2020-05-08 11:22:40 -0700 | [diff] [blame] | 2783 | poll_end = cur = ktime_get(); |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2784 | } while (single_task_running() && ktime_before(cur, stop)); |
| 2785 | } |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 2786 | |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 2787 | prepare_to_rcuwait(&vcpu->wait); |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 2788 | for (;;) { |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 2789 | set_current_state(TASK_INTERRUPTIBLE); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 2790 | |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2791 | if (kvm_vcpu_check_block(vcpu) < 0) |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 2792 | break; |
| 2793 | |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2794 | waited = true; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 2795 | schedule(); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 2796 | } |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 2797 | finish_rcuwait(&vcpu->wait); |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2798 | cur = ktime_get(); |
Paolo Bonzini | f781951 | 2015-02-04 18:20:58 +0100 | [diff] [blame] | 2799 | out: |
Marc Zyngier | 07ab0f8 | 2019-08-02 11:37:09 +0100 | [diff] [blame] | 2800 | kvm_arch_vcpu_unblocking(vcpu); |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2801 | block_ns = ktime_to_ns(cur) - ktime_to_ns(start); |
| 2802 | |
David Matlack | cb95312 | 2020-05-08 11:22:40 -0700 | [diff] [blame] | 2803 | update_halt_poll_stats( |
| 2804 | vcpu, ktime_to_ns(ktime_sub(poll_end, start)), waited); |
| 2805 | |
Wanpeng Li | 44551b2 | 2019-09-29 09:06:56 +0800 | [diff] [blame] | 2806 | if (!kvm_arch_no_poll(vcpu)) { |
| 2807 | if (!vcpu_valid_wakeup(vcpu)) { |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2808 | shrink_halt_poll_ns(vcpu); |
David Matlack | acd0578 | 2020-04-17 15:14:46 -0700 | [diff] [blame] | 2809 | } else if (vcpu->kvm->max_halt_poll_ns) { |
Wanpeng Li | 44551b2 | 2019-09-29 09:06:56 +0800 | [diff] [blame] | 2810 | if (block_ns <= vcpu->halt_poll_ns) |
| 2811 | ; |
| 2812 | /* we had a long block, shrink polling */ |
David Matlack | acd0578 | 2020-04-17 15:14:46 -0700 | [diff] [blame] | 2813 | else if (vcpu->halt_poll_ns && |
| 2814 | block_ns > vcpu->kvm->max_halt_poll_ns) |
Wanpeng Li | 44551b2 | 2019-09-29 09:06:56 +0800 | [diff] [blame] | 2815 | shrink_halt_poll_ns(vcpu); |
| 2816 | /* we had a short halt and our poll time is too small */ |
David Matlack | acd0578 | 2020-04-17 15:14:46 -0700 | [diff] [blame] | 2817 | else if (vcpu->halt_poll_ns < vcpu->kvm->max_halt_poll_ns && |
| 2818 | block_ns < vcpu->kvm->max_halt_poll_ns) |
Wanpeng Li | 44551b2 | 2019-09-29 09:06:56 +0800 | [diff] [blame] | 2819 | grow_halt_poll_ns(vcpu); |
| 2820 | } else { |
| 2821 | vcpu->halt_poll_ns = 0; |
| 2822 | } |
| 2823 | } |
Wanpeng Li | aca6ff2 | 2015-09-03 22:07:38 +0800 | [diff] [blame] | 2824 | |
Christian Borntraeger | 3491caf | 2016-05-13 12:16:35 +0200 | [diff] [blame] | 2825 | trace_kvm_vcpu_wakeup(block_ns, waited, vcpu_valid_wakeup(vcpu)); |
| 2826 | kvm_arch_vcpu_block_finish(vcpu); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 2827 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 2828 | EXPORT_SYMBOL_GPL(kvm_vcpu_block); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 2829 | |
Radim Krčmář | 178f02f | 2017-04-26 22:32:26 +0200 | [diff] [blame] | 2830 | bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu) |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 2831 | { |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 2832 | struct rcuwait *waitp; |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 2833 | |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 2834 | waitp = kvm_arch_vcpu_get_wait(vcpu); |
| 2835 | if (rcuwait_wake_up(waitp)) { |
Wanpeng Li | d73eb57 | 2019-07-18 19:39:06 +0800 | [diff] [blame] | 2836 | WRITE_ONCE(vcpu->ready, true); |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 2837 | ++vcpu->stat.halt_wakeup; |
Radim Krčmář | 178f02f | 2017-04-26 22:32:26 +0200 | [diff] [blame] | 2838 | return true; |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 2839 | } |
| 2840 | |
Radim Krčmář | 178f02f | 2017-04-26 22:32:26 +0200 | [diff] [blame] | 2841 | return false; |
Radim Krčmář | dd1a4cc | 2016-05-04 14:09:44 -0500 | [diff] [blame] | 2842 | } |
| 2843 | EXPORT_SYMBOL_GPL(kvm_vcpu_wake_up); |
| 2844 | |
Paolo Bonzini | 0266c89 | 2017-05-04 15:14:13 +0200 | [diff] [blame] | 2845 | #ifndef CONFIG_S390 |
Radim Krčmář | dd1a4cc | 2016-05-04 14:09:44 -0500 | [diff] [blame] | 2846 | /* |
| 2847 | * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode. |
| 2848 | */ |
| 2849 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) |
| 2850 | { |
| 2851 | int me; |
| 2852 | int cpu = vcpu->cpu; |
| 2853 | |
Radim Krčmář | 178f02f | 2017-04-26 22:32:26 +0200 | [diff] [blame] | 2854 | if (kvm_vcpu_wake_up(vcpu)) |
| 2855 | return; |
| 2856 | |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 2857 | me = get_cpu(); |
| 2858 | if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) |
| 2859 | if (kvm_arch_vcpu_should_kick(vcpu)) |
| 2860 | smp_send_reschedule(cpu); |
| 2861 | put_cpu(); |
| 2862 | } |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 2863 | EXPORT_SYMBOL_GPL(kvm_vcpu_kick); |
Paolo Bonzini | 0266c89 | 2017-05-04 15:14:13 +0200 | [diff] [blame] | 2864 | #endif /* !CONFIG_S390 */ |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 2865 | |
Dan Carpenter | fa93384 | 2014-05-23 13:20:42 +0300 | [diff] [blame] | 2866 | int kvm_vcpu_yield_to(struct kvm_vcpu *target) |
Konstantin Weitz | 41628d3 | 2012-04-25 15:30:38 +0200 | [diff] [blame] | 2867 | { |
| 2868 | struct pid *pid; |
| 2869 | struct task_struct *task = NULL; |
Dan Carpenter | fa93384 | 2014-05-23 13:20:42 +0300 | [diff] [blame] | 2870 | int ret = 0; |
Konstantin Weitz | 41628d3 | 2012-04-25 15:30:38 +0200 | [diff] [blame] | 2871 | |
| 2872 | rcu_read_lock(); |
| 2873 | pid = rcu_dereference(target->pid); |
| 2874 | if (pid) |
Sam Bobroff | 27fbe64b | 2014-09-19 09:40:41 +1000 | [diff] [blame] | 2875 | task = get_pid_task(pid, PIDTYPE_PID); |
Konstantin Weitz | 41628d3 | 2012-04-25 15:30:38 +0200 | [diff] [blame] | 2876 | rcu_read_unlock(); |
| 2877 | if (!task) |
Raghavendra K T | c45c528 | 2013-01-22 13:09:24 +0530 | [diff] [blame] | 2878 | return ret; |
Raghavendra K T | c45c528 | 2013-01-22 13:09:24 +0530 | [diff] [blame] | 2879 | ret = yield_to(task, 1); |
Konstantin Weitz | 41628d3 | 2012-04-25 15:30:38 +0200 | [diff] [blame] | 2880 | put_task_struct(task); |
Raghavendra K T | c45c528 | 2013-01-22 13:09:24 +0530 | [diff] [blame] | 2881 | |
| 2882 | return ret; |
Konstantin Weitz | 41628d3 | 2012-04-25 15:30:38 +0200 | [diff] [blame] | 2883 | } |
| 2884 | EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to); |
| 2885 | |
Raghavendra K T | 06e48c5 | 2012-07-19 15:17:52 +0530 | [diff] [blame] | 2886 | /* |
| 2887 | * Helper that checks whether a VCPU is eligible for directed yield. |
| 2888 | * Most eligible candidate to yield is decided by following heuristics: |
| 2889 | * |
| 2890 | * (a) VCPU which has not done pl-exit or cpu relax intercepted recently |
| 2891 | * (preempted lock holder), indicated by @in_spin_loop. |
Fuad Tabba | 656012c | 2020-04-01 15:03:10 +0100 | [diff] [blame] | 2892 | * Set at the beginning and cleared at the end of interception/PLE handler. |
Raghavendra K T | 06e48c5 | 2012-07-19 15:17:52 +0530 | [diff] [blame] | 2893 | * |
| 2894 | * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get |
| 2895 | * chance last time (mostly it has become eligible now since we have probably |
| 2896 | * yielded to lockholder in last iteration. This is done by toggling |
| 2897 | * @dy_eligible each time a VCPU checked for eligibility.) |
| 2898 | * |
| 2899 | * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding |
| 2900 | * to preempted lock-holder could result in wrong VCPU selection and CPU |
| 2901 | * burning. Giving priority for a potential lock-holder increases lock |
| 2902 | * progress. |
| 2903 | * |
| 2904 | * Since algorithm is based on heuristics, accessing another VCPU data without |
| 2905 | * locking does not harm. It may result in trying to yield to same VCPU, fail |
| 2906 | * and continue with next VCPU and so on. |
| 2907 | */ |
Stephen Hemminger | 7940876 | 2013-12-29 12:12:29 -0800 | [diff] [blame] | 2908 | static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu) |
Raghavendra K T | 06e48c5 | 2012-07-19 15:17:52 +0530 | [diff] [blame] | 2909 | { |
Scott Wood | 4a55dd7 | 2014-01-09 18:43:16 -0600 | [diff] [blame] | 2910 | #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT |
Raghavendra K T | 06e48c5 | 2012-07-19 15:17:52 +0530 | [diff] [blame] | 2911 | bool eligible; |
| 2912 | |
| 2913 | eligible = !vcpu->spin_loop.in_spin_loop || |
Christian Borntraeger | 3465611 | 2014-09-04 21:13:31 +0200 | [diff] [blame] | 2914 | vcpu->spin_loop.dy_eligible; |
Raghavendra K T | 06e48c5 | 2012-07-19 15:17:52 +0530 | [diff] [blame] | 2915 | |
| 2916 | if (vcpu->spin_loop.in_spin_loop) |
| 2917 | kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible); |
| 2918 | |
| 2919 | return eligible; |
Scott Wood | 4a55dd7 | 2014-01-09 18:43:16 -0600 | [diff] [blame] | 2920 | #else |
| 2921 | return true; |
Raghavendra K T | 06e48c5 | 2012-07-19 15:17:52 +0530 | [diff] [blame] | 2922 | #endif |
Scott Wood | 4a55dd7 | 2014-01-09 18:43:16 -0600 | [diff] [blame] | 2923 | } |
Raghavendra K T | c45c528 | 2013-01-22 13:09:24 +0530 | [diff] [blame] | 2924 | |
Wanpeng Li | 17e433b | 2019-08-05 10:03:19 +0800 | [diff] [blame] | 2925 | /* |
| 2926 | * Unlike kvm_arch_vcpu_runnable, this function is called outside |
| 2927 | * a vcpu_load/vcpu_put pair. However, for most architectures |
| 2928 | * kvm_arch_vcpu_runnable does not require vcpu_load. |
| 2929 | */ |
| 2930 | bool __weak kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) |
| 2931 | { |
| 2932 | return kvm_arch_vcpu_runnable(vcpu); |
| 2933 | } |
| 2934 | |
| 2935 | static bool vcpu_dy_runnable(struct kvm_vcpu *vcpu) |
| 2936 | { |
| 2937 | if (kvm_arch_dy_runnable(vcpu)) |
| 2938 | return true; |
| 2939 | |
| 2940 | #ifdef CONFIG_KVM_ASYNC_PF |
| 2941 | if (!list_empty_careful(&vcpu->async_pf.done)) |
| 2942 | return true; |
| 2943 | #endif |
| 2944 | |
| 2945 | return false; |
| 2946 | } |
| 2947 | |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 2948 | void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) |
Zhai, Edwin | d255f4f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2949 | { |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2950 | struct kvm *kvm = me->kvm; |
| 2951 | struct kvm_vcpu *vcpu; |
| 2952 | int last_boosted_vcpu = me->kvm->last_boosted_vcpu; |
| 2953 | int yielded = 0; |
Raghavendra K T | c45c528 | 2013-01-22 13:09:24 +0530 | [diff] [blame] | 2954 | int try = 3; |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2955 | int pass; |
| 2956 | int i; |
Zhai, Edwin | d255f4f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2957 | |
Raghavendra K T | 4c08849 | 2012-07-18 19:07:46 +0530 | [diff] [blame] | 2958 | kvm_vcpu_set_in_spin_loop(me, true); |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2959 | /* |
| 2960 | * We boost the priority of a VCPU that is runnable but not |
| 2961 | * currently running, because it got preempted by something |
| 2962 | * else and called schedule in __vcpu_run. Hopefully that |
| 2963 | * VCPU is holding the lock that we need and will release it. |
| 2964 | * We approximate round-robin by starting at the last boosted VCPU. |
| 2965 | */ |
Raghavendra K T | c45c528 | 2013-01-22 13:09:24 +0530 | [diff] [blame] | 2966 | for (pass = 0; pass < 2 && !yielded && try; pass++) { |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2967 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Rik van Riel | 5cfc2aa | 2012-06-19 16:51:04 -0400 | [diff] [blame] | 2968 | if (!pass && i <= last_boosted_vcpu) { |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2969 | i = last_boosted_vcpu; |
| 2970 | continue; |
| 2971 | } else if (pass && i > last_boosted_vcpu) |
| 2972 | break; |
Wanpeng Li | d73eb57 | 2019-07-18 19:39:06 +0800 | [diff] [blame] | 2973 | if (!READ_ONCE(vcpu->ready)) |
Raghavendra K T | 7bc7ae2 | 2013-03-04 23:32:27 +0530 | [diff] [blame] | 2974 | continue; |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2975 | if (vcpu == me) |
| 2976 | continue; |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 2977 | if (rcuwait_active(&vcpu->wait) && |
| 2978 | !vcpu_dy_runnable(vcpu)) |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2979 | continue; |
Wanpeng Li | 046ddee | 2019-08-01 11:30:14 +0800 | [diff] [blame] | 2980 | if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode && |
| 2981 | !kvm_arch_vcpu_in_kernel(vcpu)) |
Longpeng(Mike) | 199b576 | 2017-08-08 12:05:32 +0800 | [diff] [blame] | 2982 | continue; |
Raghavendra K T | 06e48c5 | 2012-07-19 15:17:52 +0530 | [diff] [blame] | 2983 | if (!kvm_vcpu_eligible_for_directed_yield(vcpu)) |
| 2984 | continue; |
Raghavendra K T | c45c528 | 2013-01-22 13:09:24 +0530 | [diff] [blame] | 2985 | |
| 2986 | yielded = kvm_vcpu_yield_to(vcpu); |
| 2987 | if (yielded > 0) { |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2988 | kvm->last_boosted_vcpu = i; |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2989 | break; |
Raghavendra K T | c45c528 | 2013-01-22 13:09:24 +0530 | [diff] [blame] | 2990 | } else if (yielded < 0) { |
| 2991 | try--; |
| 2992 | if (!try) |
| 2993 | break; |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2994 | } |
Rik van Riel | 217ece6 | 2011-02-01 09:53:28 -0500 | [diff] [blame] | 2995 | } |
| 2996 | } |
Raghavendra K T | 4c08849 | 2012-07-18 19:07:46 +0530 | [diff] [blame] | 2997 | kvm_vcpu_set_in_spin_loop(me, false); |
Raghavendra K T | 06e48c5 | 2012-07-19 15:17:52 +0530 | [diff] [blame] | 2998 | |
| 2999 | /* Ensure vcpu is not eligible during next spinloop */ |
| 3000 | kvm_vcpu_set_dy_eligible(me, false); |
Zhai, Edwin | d255f4f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3001 | } |
| 3002 | EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin); |
| 3003 | |
Souptick Joarder | 1499fa8 | 2018-04-19 00:49:58 +0530 | [diff] [blame] | 3004 | static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf) |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 3005 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3006 | struct kvm_vcpu *vcpu = vmf->vma->vm_file->private_data; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 3007 | struct page *page; |
| 3008 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 3009 | if (vmf->pgoff == 0) |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 3010 | page = virt_to_page(vcpu->run); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 3011 | #ifdef CONFIG_X86 |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 3012 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3013 | page = virt_to_page(vcpu->arch.pio_data); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 3014 | #endif |
Paolo Bonzini | 4b4357e | 2017-03-31 13:53:23 +0200 | [diff] [blame] | 3015 | #ifdef CONFIG_KVM_MMIO |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3016 | else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET) |
| 3017 | page = virt_to_page(vcpu->kvm->coalesced_mmio_ring); |
| 3018 | #endif |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 3019 | else |
Carsten Otte | 5b1c149 | 2012-01-04 10:25:23 +0100 | [diff] [blame] | 3020 | return kvm_arch_vcpu_fault(vcpu, vmf); |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 3021 | get_page(page); |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 3022 | vmf->page = page; |
| 3023 | return 0; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 3024 | } |
| 3025 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 3026 | static const struct vm_operations_struct kvm_vcpu_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 3027 | .fault = kvm_vcpu_fault, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 3028 | }; |
| 3029 | |
| 3030 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 3031 | { |
| 3032 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 3033 | return 0; |
| 3034 | } |
| 3035 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3036 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 3037 | { |
| 3038 | struct kvm_vcpu *vcpu = filp->private_data; |
| 3039 | |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 3040 | kvm_put_kvm(vcpu->kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3041 | return 0; |
| 3042 | } |
| 3043 | |
Christian Borntraeger | 3d3aab1 | 2008-12-02 11:17:32 +0100 | [diff] [blame] | 3044 | static struct file_operations kvm_vcpu_fops = { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3045 | .release = kvm_vcpu_release, |
| 3046 | .unlocked_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 3047 | .mmap = kvm_vcpu_mmap, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 3048 | .llseek = noop_llseek, |
Marc Zyngier | 7ddfd3e | 2018-06-17 10:16:21 +0100 | [diff] [blame] | 3049 | KVM_COMPAT(kvm_vcpu_compat_ioctl), |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3050 | }; |
| 3051 | |
| 3052 | /* |
| 3053 | * Allocates an inode for the vcpu. |
| 3054 | */ |
| 3055 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 3056 | { |
Masatake YAMATO | e46b469 | 2018-01-20 04:04:22 +0900 | [diff] [blame] | 3057 | char name[8 + 1 + ITOA_MAX_LEN + 1]; |
| 3058 | |
| 3059 | snprintf(name, sizeof(name), "kvm-vcpu:%d", vcpu->vcpu_id); |
| 3060 | return anon_inode_getfd(name, &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3061 | } |
| 3062 | |
Greg KH | 3e7093d | 2019-07-31 20:56:20 +0200 | [diff] [blame] | 3063 | static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) |
Luiz Capitulino | 45b5939 | 2016-09-16 10:27:35 -0400 | [diff] [blame] | 3064 | { |
Paolo Bonzini | 741cbba | 2019-08-03 08:14:25 +0200 | [diff] [blame] | 3065 | #ifdef __KVM_HAVE_ARCH_VCPU_DEBUGFS |
Paolo Bonzini | d56f513 | 2020-06-04 15:16:52 +0200 | [diff] [blame] | 3066 | struct dentry *debugfs_dentry; |
Luiz Capitulino | 45b5939 | 2016-09-16 10:27:35 -0400 | [diff] [blame] | 3067 | char dir_name[ITOA_MAX_LEN * 2]; |
Luiz Capitulino | 45b5939 | 2016-09-16 10:27:35 -0400 | [diff] [blame] | 3068 | |
Luiz Capitulino | 45b5939 | 2016-09-16 10:27:35 -0400 | [diff] [blame] | 3069 | if (!debugfs_initialized()) |
Greg KH | 3e7093d | 2019-07-31 20:56:20 +0200 | [diff] [blame] | 3070 | return; |
Luiz Capitulino | 45b5939 | 2016-09-16 10:27:35 -0400 | [diff] [blame] | 3071 | |
| 3072 | snprintf(dir_name, sizeof(dir_name), "vcpu%d", vcpu->vcpu_id); |
Paolo Bonzini | d56f513 | 2020-06-04 15:16:52 +0200 | [diff] [blame] | 3073 | debugfs_dentry = debugfs_create_dir(dir_name, |
| 3074 | vcpu->kvm->debugfs_dentry); |
Luiz Capitulino | 45b5939 | 2016-09-16 10:27:35 -0400 | [diff] [blame] | 3075 | |
Paolo Bonzini | d56f513 | 2020-06-04 15:16:52 +0200 | [diff] [blame] | 3076 | kvm_arch_create_vcpu_debugfs(vcpu, debugfs_dentry); |
Paolo Bonzini | 741cbba | 2019-08-03 08:14:25 +0200 | [diff] [blame] | 3077 | #endif |
Luiz Capitulino | 45b5939 | 2016-09-16 10:27:35 -0400 | [diff] [blame] | 3078 | } |
| 3079 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 3080 | /* |
| 3081 | * Creates some virtual cpus. Good luck creating more than one. |
| 3082 | */ |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 3083 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 3084 | { |
| 3085 | int r; |
David Hildenbrand | e09fefd | 2015-11-05 09:03:50 +0100 | [diff] [blame] | 3086 | struct kvm_vcpu *vcpu; |
Sean Christopherson | 8bd826d | 2019-12-18 13:55:30 -0800 | [diff] [blame] | 3087 | struct page *page; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 3088 | |
Greg Kurz | 0b1b1df | 2016-05-09 18:13:37 +0200 | [diff] [blame] | 3089 | if (id >= KVM_MAX_VCPU_ID) |
Andy Honig | 338c7db | 2013-11-18 16:09:22 -0800 | [diff] [blame] | 3090 | return -EINVAL; |
| 3091 | |
Paolo Bonzini | 6c7caeb | 2016-06-13 14:48:25 +0200 | [diff] [blame] | 3092 | mutex_lock(&kvm->lock); |
| 3093 | if (kvm->created_vcpus == KVM_MAX_VCPUS) { |
| 3094 | mutex_unlock(&kvm->lock); |
| 3095 | return -EINVAL; |
| 3096 | } |
| 3097 | |
| 3098 | kvm->created_vcpus++; |
| 3099 | mutex_unlock(&kvm->lock); |
| 3100 | |
Sean Christopherson | 897cc38 | 2019-12-18 13:55:09 -0800 | [diff] [blame] | 3101 | r = kvm_arch_vcpu_precreate(kvm, id); |
| 3102 | if (r) |
| 3103 | goto vcpu_decrement; |
| 3104 | |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3105 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
| 3106 | if (!vcpu) { |
| 3107 | r = -ENOMEM; |
Paolo Bonzini | 6c7caeb | 2016-06-13 14:48:25 +0200 | [diff] [blame] | 3108 | goto vcpu_decrement; |
| 3109 | } |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 3110 | |
Peter Xu | fcd97ad | 2020-01-09 09:57:12 -0500 | [diff] [blame] | 3111 | BUILD_BUG_ON(sizeof(struct kvm_run) > PAGE_SIZE); |
Sean Christopherson | 8bd826d | 2019-12-18 13:55:30 -0800 | [diff] [blame] | 3112 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 3113 | if (!page) { |
| 3114 | r = -ENOMEM; |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3115 | goto vcpu_free; |
Sean Christopherson | 8bd826d | 2019-12-18 13:55:30 -0800 | [diff] [blame] | 3116 | } |
| 3117 | vcpu->run = page_address(page); |
| 3118 | |
| 3119 | kvm_vcpu_init(vcpu, kvm, id); |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3120 | |
| 3121 | r = kvm_arch_vcpu_create(vcpu); |
| 3122 | if (r) |
Sean Christopherson | 8bd826d | 2019-12-18 13:55:30 -0800 | [diff] [blame] | 3123 | goto vcpu_free_run_page; |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3124 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 3125 | mutex_lock(&kvm->lock); |
David Hildenbrand | e09fefd | 2015-11-05 09:03:50 +0100 | [diff] [blame] | 3126 | if (kvm_get_vcpu_by_id(kvm, id)) { |
| 3127 | r = -EEXIST; |
| 3128 | goto unlock_vcpu_destroy; |
| 3129 | } |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 3130 | |
Radim Krčmář | 8750e72 | 2019-11-07 07:53:42 -0500 | [diff] [blame] | 3131 | vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus); |
| 3132 | BUG_ON(kvm->vcpus[vcpu->vcpu_idx]); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3133 | |
| 3134 | /* Now it's all set up, let userspace reach it */ |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 3135 | kvm_get_kvm(kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3136 | r = create_vcpu_fd(vcpu); |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 3137 | if (r < 0) { |
Sean Christopherson | 149487b | 2019-10-21 15:58:42 -0700 | [diff] [blame] | 3138 | kvm_put_kvm_no_destroy(kvm); |
Jan Kiszka | d780592 | 2011-05-23 10:33:05 +0200 | [diff] [blame] | 3139 | goto unlock_vcpu_destroy; |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 3140 | } |
| 3141 | |
Radim Krčmář | 8750e72 | 2019-11-07 07:53:42 -0500 | [diff] [blame] | 3142 | kvm->vcpus[vcpu->vcpu_idx] = vcpu; |
Paolo Bonzini | dd48924 | 2015-07-29 11:32:20 +0200 | [diff] [blame] | 3143 | |
| 3144 | /* |
| 3145 | * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus |
| 3146 | * before kvm->online_vcpu's incremented value. |
| 3147 | */ |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 3148 | smp_wmb(); |
| 3149 | atomic_inc(&kvm->online_vcpus); |
| 3150 | |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 3151 | mutex_unlock(&kvm->lock); |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 3152 | kvm_arch_vcpu_postcreate(vcpu); |
Paolo Bonzini | 63d0434 | 2020-04-01 00:42:22 +0200 | [diff] [blame] | 3153 | kvm_create_vcpu_debugfs(vcpu); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3154 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 3155 | |
Jan Kiszka | d780592 | 2011-05-23 10:33:05 +0200 | [diff] [blame] | 3156 | unlock_vcpu_destroy: |
Glauber Costa | 7d8fece | 2008-09-17 23:16:59 -0300 | [diff] [blame] | 3157 | mutex_unlock(&kvm->lock); |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 3158 | kvm_arch_vcpu_destroy(vcpu); |
Sean Christopherson | 8bd826d | 2019-12-18 13:55:30 -0800 | [diff] [blame] | 3159 | vcpu_free_run_page: |
| 3160 | free_page((unsigned long)vcpu->run); |
Sean Christopherson | e529ef6 | 2019-12-18 13:55:15 -0800 | [diff] [blame] | 3161 | vcpu_free: |
| 3162 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
Paolo Bonzini | 6c7caeb | 2016-06-13 14:48:25 +0200 | [diff] [blame] | 3163 | vcpu_decrement: |
| 3164 | mutex_lock(&kvm->lock); |
| 3165 | kvm->created_vcpus--; |
| 3166 | mutex_unlock(&kvm->lock); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 3167 | return r; |
| 3168 | } |
| 3169 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 3170 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 3171 | { |
| 3172 | if (sigset) { |
| 3173 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 3174 | vcpu->sigset_active = 1; |
| 3175 | vcpu->sigset = *sigset; |
| 3176 | } else |
| 3177 | vcpu->sigset_active = 0; |
| 3178 | return 0; |
| 3179 | } |
| 3180 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3181 | static long kvm_vcpu_ioctl(struct file *filp, |
| 3182 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3183 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3184 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 3185 | void __user *argp = (void __user *)arg; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3186 | int r; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 3187 | struct kvm_fpu *fpu = NULL; |
| 3188 | struct kvm_sregs *kvm_sregs = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3189 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 3190 | if (vcpu->kvm->mm != current->mm) |
| 3191 | return -EIO; |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 3192 | |
David Matlack | 2ea75be | 2014-09-19 16:03:25 -0700 | [diff] [blame] | 3193 | if (unlikely(_IOC_TYPE(ioctl) != KVMIO)) |
| 3194 | return -EINVAL; |
| 3195 | |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 3196 | /* |
Paolo Bonzini | 5cb0944 | 2017-12-12 17:41:34 +0100 | [diff] [blame] | 3197 | * Some architectures have vcpu ioctls that are asynchronous to vcpu |
| 3198 | * execution; mutex_lock() would break them. |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 3199 | */ |
Paolo Bonzini | 5cb0944 | 2017-12-12 17:41:34 +0100 | [diff] [blame] | 3200 | r = kvm_arch_vcpu_async_ioctl(filp, ioctl, arg); |
| 3201 | if (r != -ENOIOCTLCMD) |
Michael S. Tsirkin | 9fc7744 | 2012-09-16 11:50:30 +0300 | [diff] [blame] | 3202 | return r; |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 3203 | |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 3204 | if (mutex_lock_killable(&vcpu->mutex)) |
| 3205 | return -EINTR; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3206 | switch (ioctl) { |
Christian Borntraeger | 0e4524a | 2017-07-06 14:44:28 +0200 | [diff] [blame] | 3207 | case KVM_RUN: { |
| 3208 | struct pid *oldpid; |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 3209 | r = -EINVAL; |
| 3210 | if (arg) |
| 3211 | goto out; |
Christian Borntraeger | 0e4524a | 2017-07-06 14:44:28 +0200 | [diff] [blame] | 3212 | oldpid = rcu_access_pointer(vcpu->pid); |
Eric W. Biederman | 71dbc8a | 2017-07-16 21:39:32 -0500 | [diff] [blame] | 3213 | if (unlikely(oldpid != task_pid(current))) { |
Christian Borntraeger | 7a72f7a | 2014-08-05 16:44:14 +0200 | [diff] [blame] | 3214 | /* The thread running this VCPU changed. */ |
Christoffer Dall | bd2a639 | 2018-02-23 17:23:57 +0100 | [diff] [blame] | 3215 | struct pid *newpid; |
Xiubo Li | f95ef0cd | 2015-02-26 14:58:23 +0800 | [diff] [blame] | 3216 | |
Christoffer Dall | bd2a639 | 2018-02-23 17:23:57 +0100 | [diff] [blame] | 3217 | r = kvm_arch_vcpu_run_pid_change(vcpu); |
| 3218 | if (r) |
| 3219 | break; |
| 3220 | |
| 3221 | newpid = get_task_pid(current, PIDTYPE_PID); |
Christian Borntraeger | 7a72f7a | 2014-08-05 16:44:14 +0200 | [diff] [blame] | 3222 | rcu_assign_pointer(vcpu->pid, newpid); |
| 3223 | if (oldpid) |
| 3224 | synchronize_rcu(); |
| 3225 | put_pid(oldpid); |
| 3226 | } |
Tianjia Zhang | 1b94f6f | 2020-04-16 13:10:57 +0800 | [diff] [blame] | 3227 | r = kvm_arch_vcpu_ioctl_run(vcpu); |
Gleb Natapov | 64be500 | 2010-10-24 16:49:08 +0200 | [diff] [blame] | 3228 | trace_kvm_userspace_exit(vcpu->run->exit_reason, r); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3229 | break; |
Christian Borntraeger | 0e4524a | 2017-07-06 14:44:28 +0200 | [diff] [blame] | 3230 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3231 | case KVM_GET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3232 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3233 | |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3234 | r = -ENOMEM; |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 3235 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL_ACCOUNT); |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3236 | if (!kvm_regs) |
| 3237 | goto out; |
| 3238 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3239 | if (r) |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3240 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3241 | r = -EFAULT; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3242 | if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs))) |
| 3243 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3244 | r = 0; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3245 | out_free1: |
| 3246 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3247 | break; |
| 3248 | } |
| 3249 | case KVM_SET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3250 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3251 | |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 3252 | kvm_regs = memdup_user(argp, sizeof(*kvm_regs)); |
| 3253 | if (IS_ERR(kvm_regs)) { |
| 3254 | r = PTR_ERR(kvm_regs); |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3255 | goto out; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 3256 | } |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3257 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs); |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 3258 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3259 | break; |
| 3260 | } |
| 3261 | case KVM_GET_SREGS: { |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 3262 | kvm_sregs = kzalloc(sizeof(struct kvm_sregs), |
| 3263 | GFP_KERNEL_ACCOUNT); |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 3264 | r = -ENOMEM; |
| 3265 | if (!kvm_sregs) |
| 3266 | goto out; |
| 3267 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3268 | if (r) |
| 3269 | goto out; |
| 3270 | r = -EFAULT; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 3271 | if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs))) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3272 | goto out; |
| 3273 | r = 0; |
| 3274 | break; |
| 3275 | } |
| 3276 | case KVM_SET_SREGS: { |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 3277 | kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs)); |
| 3278 | if (IS_ERR(kvm_sregs)) { |
| 3279 | r = PTR_ERR(kvm_sregs); |
Guo Chao | 1859541 | 2012-11-02 18:33:21 +0800 | [diff] [blame] | 3280 | kvm_sregs = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3281 | goto out; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 3282 | } |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 3283 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3284 | break; |
| 3285 | } |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3286 | case KVM_GET_MP_STATE: { |
| 3287 | struct kvm_mp_state mp_state; |
| 3288 | |
| 3289 | r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state); |
| 3290 | if (r) |
| 3291 | goto out; |
| 3292 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3293 | if (copy_to_user(argp, &mp_state, sizeof(mp_state))) |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3294 | goto out; |
| 3295 | r = 0; |
| 3296 | break; |
| 3297 | } |
| 3298 | case KVM_SET_MP_STATE: { |
| 3299 | struct kvm_mp_state mp_state; |
| 3300 | |
| 3301 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3302 | if (copy_from_user(&mp_state, argp, sizeof(mp_state))) |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3303 | goto out; |
| 3304 | r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state); |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3305 | break; |
| 3306 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3307 | case KVM_TRANSLATE: { |
| 3308 | struct kvm_translation tr; |
| 3309 | |
| 3310 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3311 | if (copy_from_user(&tr, argp, sizeof(tr))) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3312 | goto out; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 3313 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3314 | if (r) |
| 3315 | goto out; |
| 3316 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3317 | if (copy_to_user(argp, &tr, sizeof(tr))) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3318 | goto out; |
| 3319 | r = 0; |
| 3320 | break; |
| 3321 | } |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3322 | case KVM_SET_GUEST_DEBUG: { |
| 3323 | struct kvm_guest_debug dbg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3324 | |
| 3325 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3326 | if (copy_from_user(&dbg, argp, sizeof(dbg))) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3327 | goto out; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3328 | r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3329 | break; |
| 3330 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 3331 | case KVM_SET_SIGNAL_MASK: { |
| 3332 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 3333 | struct kvm_signal_mask kvm_sigmask; |
| 3334 | sigset_t sigset, *p; |
| 3335 | |
| 3336 | p = NULL; |
| 3337 | if (argp) { |
| 3338 | r = -EFAULT; |
| 3339 | if (copy_from_user(&kvm_sigmask, argp, |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3340 | sizeof(kvm_sigmask))) |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 3341 | goto out; |
| 3342 | r = -EINVAL; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3343 | if (kvm_sigmask.len != sizeof(sigset)) |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 3344 | goto out; |
| 3345 | r = -EFAULT; |
| 3346 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3347 | sizeof(sigset))) |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 3348 | goto out; |
| 3349 | p = &sigset; |
| 3350 | } |
Andi Kleen | 376d41f | 2010-06-10 13:10:47 +0200 | [diff] [blame] | 3351 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, p); |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 3352 | break; |
| 3353 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 3354 | case KVM_GET_FPU: { |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 3355 | fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL_ACCOUNT); |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 3356 | r = -ENOMEM; |
| 3357 | if (!fpu) |
| 3358 | goto out; |
| 3359 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 3360 | if (r) |
| 3361 | goto out; |
| 3362 | r = -EFAULT; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 3363 | if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu))) |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 3364 | goto out; |
| 3365 | r = 0; |
| 3366 | break; |
| 3367 | } |
| 3368 | case KVM_SET_FPU: { |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 3369 | fpu = memdup_user(argp, sizeof(*fpu)); |
| 3370 | if (IS_ERR(fpu)) { |
| 3371 | r = PTR_ERR(fpu); |
Guo Chao | 1859541 | 2012-11-02 18:33:21 +0800 | [diff] [blame] | 3372 | fpu = NULL; |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 3373 | goto out; |
Sasha Levin | ff5c2c0 | 2011-12-04 19:36:29 +0200 | [diff] [blame] | 3374 | } |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 3375 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 3376 | break; |
| 3377 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3378 | default: |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 3379 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3380 | } |
| 3381 | out: |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 3382 | mutex_unlock(&vcpu->mutex); |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 3383 | kfree(fpu); |
| 3384 | kfree(kvm_sregs); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3385 | return r; |
| 3386 | } |
| 3387 | |
Christian Borntraeger | de8e5d7 | 2015-02-03 09:35:15 +0100 | [diff] [blame] | 3388 | #ifdef CONFIG_KVM_COMPAT |
Alexander Graf | 1dda606 | 2011-06-08 02:45:37 +0200 | [diff] [blame] | 3389 | static long kvm_vcpu_compat_ioctl(struct file *filp, |
| 3390 | unsigned int ioctl, unsigned long arg) |
| 3391 | { |
| 3392 | struct kvm_vcpu *vcpu = filp->private_data; |
| 3393 | void __user *argp = compat_ptr(arg); |
| 3394 | int r; |
| 3395 | |
| 3396 | if (vcpu->kvm->mm != current->mm) |
| 3397 | return -EIO; |
| 3398 | |
| 3399 | switch (ioctl) { |
| 3400 | case KVM_SET_SIGNAL_MASK: { |
| 3401 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 3402 | struct kvm_signal_mask kvm_sigmask; |
Alexander Graf | 1dda606 | 2011-06-08 02:45:37 +0200 | [diff] [blame] | 3403 | sigset_t sigset; |
| 3404 | |
| 3405 | if (argp) { |
| 3406 | r = -EFAULT; |
| 3407 | if (copy_from_user(&kvm_sigmask, argp, |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3408 | sizeof(kvm_sigmask))) |
Alexander Graf | 1dda606 | 2011-06-08 02:45:37 +0200 | [diff] [blame] | 3409 | goto out; |
| 3410 | r = -EINVAL; |
Al Viro | 3968cf6 | 2017-09-03 21:45:17 -0400 | [diff] [blame] | 3411 | if (kvm_sigmask.len != sizeof(compat_sigset_t)) |
Alexander Graf | 1dda606 | 2011-06-08 02:45:37 +0200 | [diff] [blame] | 3412 | goto out; |
| 3413 | r = -EFAULT; |
Paolo Bonzini | 1393b4a | 2020-07-02 05:39:31 -0400 | [diff] [blame] | 3414 | if (get_compat_sigset(&sigset, |
| 3415 | (compat_sigset_t __user *)sigmask_arg->sigset)) |
Alexander Graf | 1dda606 | 2011-06-08 02:45:37 +0200 | [diff] [blame] | 3416 | goto out; |
Alan Cox | 760a9a3 | 2012-08-22 14:34:11 +0100 | [diff] [blame] | 3417 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
| 3418 | } else |
| 3419 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL); |
Alexander Graf | 1dda606 | 2011-06-08 02:45:37 +0200 | [diff] [blame] | 3420 | break; |
| 3421 | } |
| 3422 | default: |
| 3423 | r = kvm_vcpu_ioctl(filp, ioctl, arg); |
| 3424 | } |
| 3425 | |
| 3426 | out: |
| 3427 | return r; |
| 3428 | } |
| 3429 | #endif |
| 3430 | |
Cédric Le Goater | a1cd3f0 | 2019-04-18 12:39:36 +0200 | [diff] [blame] | 3431 | static int kvm_device_mmap(struct file *filp, struct vm_area_struct *vma) |
| 3432 | { |
| 3433 | struct kvm_device *dev = filp->private_data; |
| 3434 | |
| 3435 | if (dev->ops->mmap) |
| 3436 | return dev->ops->mmap(dev, vma); |
| 3437 | |
| 3438 | return -ENODEV; |
| 3439 | } |
| 3440 | |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3441 | static int kvm_device_ioctl_attr(struct kvm_device *dev, |
| 3442 | int (*accessor)(struct kvm_device *dev, |
| 3443 | struct kvm_device_attr *attr), |
| 3444 | unsigned long arg) |
| 3445 | { |
| 3446 | struct kvm_device_attr attr; |
| 3447 | |
| 3448 | if (!accessor) |
| 3449 | return -EPERM; |
| 3450 | |
| 3451 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) |
| 3452 | return -EFAULT; |
| 3453 | |
| 3454 | return accessor(dev, &attr); |
| 3455 | } |
| 3456 | |
| 3457 | static long kvm_device_ioctl(struct file *filp, unsigned int ioctl, |
| 3458 | unsigned long arg) |
| 3459 | { |
| 3460 | struct kvm_device *dev = filp->private_data; |
| 3461 | |
Sean Christopherson | ddba918 | 2019-02-15 12:48:39 -0800 | [diff] [blame] | 3462 | if (dev->kvm->mm != current->mm) |
| 3463 | return -EIO; |
| 3464 | |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3465 | switch (ioctl) { |
| 3466 | case KVM_SET_DEVICE_ATTR: |
| 3467 | return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg); |
| 3468 | case KVM_GET_DEVICE_ATTR: |
| 3469 | return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg); |
| 3470 | case KVM_HAS_DEVICE_ATTR: |
| 3471 | return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg); |
| 3472 | default: |
| 3473 | if (dev->ops->ioctl) |
| 3474 | return dev->ops->ioctl(dev, ioctl, arg); |
| 3475 | |
| 3476 | return -ENOTTY; |
| 3477 | } |
| 3478 | } |
| 3479 | |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3480 | static int kvm_device_release(struct inode *inode, struct file *filp) |
| 3481 | { |
| 3482 | struct kvm_device *dev = filp->private_data; |
| 3483 | struct kvm *kvm = dev->kvm; |
| 3484 | |
Cédric Le Goater | 2bde9b3 | 2019-04-18 12:39:41 +0200 | [diff] [blame] | 3485 | if (dev->ops->release) { |
| 3486 | mutex_lock(&kvm->lock); |
| 3487 | list_del(&dev->vm_node); |
| 3488 | dev->ops->release(dev); |
| 3489 | mutex_unlock(&kvm->lock); |
| 3490 | } |
| 3491 | |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3492 | kvm_put_kvm(kvm); |
| 3493 | return 0; |
| 3494 | } |
| 3495 | |
| 3496 | static const struct file_operations kvm_device_fops = { |
| 3497 | .unlocked_ioctl = kvm_device_ioctl, |
| 3498 | .release = kvm_device_release, |
Marc Zyngier | 7ddfd3e | 2018-06-17 10:16:21 +0100 | [diff] [blame] | 3499 | KVM_COMPAT(kvm_device_ioctl), |
Cédric Le Goater | a1cd3f0 | 2019-04-18 12:39:36 +0200 | [diff] [blame] | 3500 | .mmap = kvm_device_mmap, |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3501 | }; |
| 3502 | |
| 3503 | struct kvm_device *kvm_device_from_filp(struct file *filp) |
| 3504 | { |
| 3505 | if (filp->f_op != &kvm_device_fops) |
| 3506 | return NULL; |
| 3507 | |
| 3508 | return filp->private_data; |
| 3509 | } |
| 3510 | |
Steven Price | 8538cb2 | 2019-10-21 16:28:19 +0100 | [diff] [blame] | 3511 | static const struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = { |
Will Deacon | d60eacb | 2014-09-02 10:27:33 +0100 | [diff] [blame] | 3512 | #ifdef CONFIG_KVM_MPIC |
| 3513 | [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops, |
| 3514 | [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops, |
| 3515 | #endif |
Will Deacon | d60eacb | 2014-09-02 10:27:33 +0100 | [diff] [blame] | 3516 | }; |
| 3517 | |
Steven Price | 8538cb2 | 2019-10-21 16:28:19 +0100 | [diff] [blame] | 3518 | int kvm_register_device_ops(const struct kvm_device_ops *ops, u32 type) |
Will Deacon | d60eacb | 2014-09-02 10:27:33 +0100 | [diff] [blame] | 3519 | { |
| 3520 | if (type >= ARRAY_SIZE(kvm_device_ops_table)) |
| 3521 | return -ENOSPC; |
| 3522 | |
| 3523 | if (kvm_device_ops_table[type] != NULL) |
| 3524 | return -EEXIST; |
| 3525 | |
| 3526 | kvm_device_ops_table[type] = ops; |
| 3527 | return 0; |
| 3528 | } |
| 3529 | |
Wanpeng Li | 571ee1b | 2014-10-09 18:30:08 +0800 | [diff] [blame] | 3530 | void kvm_unregister_device_ops(u32 type) |
| 3531 | { |
| 3532 | if (kvm_device_ops_table[type] != NULL) |
| 3533 | kvm_device_ops_table[type] = NULL; |
| 3534 | } |
| 3535 | |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3536 | static int kvm_ioctl_create_device(struct kvm *kvm, |
| 3537 | struct kvm_create_device *cd) |
| 3538 | { |
Steven Price | 8538cb2 | 2019-10-21 16:28:19 +0100 | [diff] [blame] | 3539 | const struct kvm_device_ops *ops = NULL; |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3540 | struct kvm_device *dev; |
| 3541 | bool test = cd->flags & KVM_CREATE_DEVICE_TEST; |
Paolo Bonzini | 1d487e9 | 2019-04-11 11:16:47 +0200 | [diff] [blame] | 3542 | int type; |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3543 | int ret; |
| 3544 | |
Will Deacon | d60eacb | 2014-09-02 10:27:33 +0100 | [diff] [blame] | 3545 | if (cd->type >= ARRAY_SIZE(kvm_device_ops_table)) |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3546 | return -ENODEV; |
Will Deacon | d60eacb | 2014-09-02 10:27:33 +0100 | [diff] [blame] | 3547 | |
Paolo Bonzini | 1d487e9 | 2019-04-11 11:16:47 +0200 | [diff] [blame] | 3548 | type = array_index_nospec(cd->type, ARRAY_SIZE(kvm_device_ops_table)); |
| 3549 | ops = kvm_device_ops_table[type]; |
Will Deacon | d60eacb | 2014-09-02 10:27:33 +0100 | [diff] [blame] | 3550 | if (ops == NULL) |
| 3551 | return -ENODEV; |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3552 | |
| 3553 | if (test) |
| 3554 | return 0; |
| 3555 | |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 3556 | dev = kzalloc(sizeof(*dev), GFP_KERNEL_ACCOUNT); |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3557 | if (!dev) |
| 3558 | return -ENOMEM; |
| 3559 | |
| 3560 | dev->ops = ops; |
| 3561 | dev->kvm = kvm; |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3562 | |
Christoffer Dall | a28ebea | 2016-08-09 19:13:01 +0200 | [diff] [blame] | 3563 | mutex_lock(&kvm->lock); |
Paolo Bonzini | 1d487e9 | 2019-04-11 11:16:47 +0200 | [diff] [blame] | 3564 | ret = ops->create(dev, type); |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3565 | if (ret < 0) { |
Christoffer Dall | a28ebea | 2016-08-09 19:13:01 +0200 | [diff] [blame] | 3566 | mutex_unlock(&kvm->lock); |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3567 | kfree(dev); |
| 3568 | return ret; |
| 3569 | } |
Christoffer Dall | a28ebea | 2016-08-09 19:13:01 +0200 | [diff] [blame] | 3570 | list_add(&dev->vm_node, &kvm->devices); |
| 3571 | mutex_unlock(&kvm->lock); |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3572 | |
Christoffer Dall | 023e9fd | 2016-08-09 19:13:00 +0200 | [diff] [blame] | 3573 | if (ops->init) |
| 3574 | ops->init(dev); |
| 3575 | |
Jann Horn | cfa3938 | 2019-01-26 01:54:33 +0100 | [diff] [blame] | 3576 | kvm_get_kvm(kvm); |
Yann Droneaud | 24009b0 | 2013-08-24 22:14:07 +0200 | [diff] [blame] | 3577 | ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC); |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3578 | if (ret < 0) { |
Sean Christopherson | 149487b | 2019-10-21 15:58:42 -0700 | [diff] [blame] | 3579 | kvm_put_kvm_no_destroy(kvm); |
Christoffer Dall | a28ebea | 2016-08-09 19:13:01 +0200 | [diff] [blame] | 3580 | mutex_lock(&kvm->lock); |
| 3581 | list_del(&dev->vm_node); |
| 3582 | mutex_unlock(&kvm->lock); |
Dan Carpenter | a0f1d21 | 2016-11-30 22:21:05 +0300 | [diff] [blame] | 3583 | ops->destroy(dev); |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3584 | return ret; |
| 3585 | } |
| 3586 | |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3587 | cd->fd = ret; |
| 3588 | return 0; |
| 3589 | } |
| 3590 | |
Alexander Graf | 92b591a | 2014-07-14 18:33:08 +0200 | [diff] [blame] | 3591 | static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) |
| 3592 | { |
| 3593 | switch (arg) { |
| 3594 | case KVM_CAP_USER_MEMORY: |
| 3595 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: |
| 3596 | case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS: |
Alexander Graf | 92b591a | 2014-07-14 18:33:08 +0200 | [diff] [blame] | 3597 | case KVM_CAP_INTERNAL_ERROR_DATA: |
| 3598 | #ifdef CONFIG_HAVE_KVM_MSI |
| 3599 | case KVM_CAP_SIGNAL_MSI: |
| 3600 | #endif |
Paul Mackerras | 297e210 | 2014-06-30 20:51:13 +1000 | [diff] [blame] | 3601 | #ifdef CONFIG_HAVE_KVM_IRQFD |
Paolo Bonzini | dc9be0f | 2015-03-05 11:54:46 +0100 | [diff] [blame] | 3602 | case KVM_CAP_IRQFD: |
Alexander Graf | 92b591a | 2014-07-14 18:33:08 +0200 | [diff] [blame] | 3603 | case KVM_CAP_IRQFD_RESAMPLE: |
| 3604 | #endif |
Jason Wang | e9ea506 | 2015-09-15 14:41:59 +0800 | [diff] [blame] | 3605 | case KVM_CAP_IOEVENTFD_ANY_LENGTH: |
Alexander Graf | 92b591a | 2014-07-14 18:33:08 +0200 | [diff] [blame] | 3606 | case KVM_CAP_CHECK_EXTENSION_VM: |
Paolo Bonzini | e5d83c7 | 2017-02-16 10:40:56 +0100 | [diff] [blame] | 3607 | case KVM_CAP_ENABLE_CAP_VM: |
David Matlack | acd0578 | 2020-04-17 15:14:46 -0700 | [diff] [blame] | 3608 | case KVM_CAP_HALT_POLL: |
Alexander Graf | 92b591a | 2014-07-14 18:33:08 +0200 | [diff] [blame] | 3609 | return 1; |
Paolo Bonzini | 4b4357e | 2017-03-31 13:53:23 +0200 | [diff] [blame] | 3610 | #ifdef CONFIG_KVM_MMIO |
Paolo Bonzini | 3042255 | 2017-03-31 13:53:22 +0200 | [diff] [blame] | 3611 | case KVM_CAP_COALESCED_MMIO: |
| 3612 | return KVM_COALESCED_MMIO_PAGE_OFFSET; |
Peng Hao | 0804c84 | 2018-10-14 07:09:55 +0800 | [diff] [blame] | 3613 | case KVM_CAP_COALESCED_PIO: |
| 3614 | return 1; |
Paolo Bonzini | 3042255 | 2017-03-31 13:53:22 +0200 | [diff] [blame] | 3615 | #endif |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 3616 | #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT |
| 3617 | case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2: |
| 3618 | return KVM_DIRTY_LOG_MANUAL_CAPS; |
| 3619 | #endif |
Alexander Graf | 92b591a | 2014-07-14 18:33:08 +0200 | [diff] [blame] | 3620 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
| 3621 | case KVM_CAP_IRQ_ROUTING: |
| 3622 | return KVM_MAX_IRQ_ROUTES; |
| 3623 | #endif |
Paolo Bonzini | f481b06 | 2015-05-17 17:30:37 +0200 | [diff] [blame] | 3624 | #if KVM_ADDRESS_SPACE_NUM > 1 |
| 3625 | case KVM_CAP_MULTI_ADDRESS_SPACE: |
| 3626 | return KVM_ADDRESS_SPACE_NUM; |
| 3627 | #endif |
Paolo Bonzini | c110ae5 | 2019-03-28 17:24:03 +0100 | [diff] [blame] | 3628 | case KVM_CAP_NR_MEMSLOTS: |
| 3629 | return KVM_USER_MEM_SLOTS; |
Alexander Graf | 92b591a | 2014-07-14 18:33:08 +0200 | [diff] [blame] | 3630 | default: |
| 3631 | break; |
| 3632 | } |
| 3633 | return kvm_vm_ioctl_check_extension(kvm, arg); |
| 3634 | } |
| 3635 | |
Paolo Bonzini | e5d83c7 | 2017-02-16 10:40:56 +0100 | [diff] [blame] | 3636 | int __attribute__((weak)) kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
| 3637 | struct kvm_enable_cap *cap) |
| 3638 | { |
| 3639 | return -EINVAL; |
| 3640 | } |
| 3641 | |
| 3642 | static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, |
| 3643 | struct kvm_enable_cap *cap) |
| 3644 | { |
| 3645 | switch (cap->cap) { |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 3646 | #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 3647 | case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2: { |
| 3648 | u64 allowed_options = KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE; |
| 3649 | |
| 3650 | if (cap->args[0] & KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE) |
| 3651 | allowed_options = KVM_DIRTY_LOG_MANUAL_CAPS; |
| 3652 | |
| 3653 | if (cap->flags || (cap->args[0] & ~allowed_options)) |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 3654 | return -EINVAL; |
| 3655 | kvm->manual_dirty_log_protect = cap->args[0]; |
| 3656 | return 0; |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 3657 | } |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 3658 | #endif |
David Matlack | acd0578 | 2020-04-17 15:14:46 -0700 | [diff] [blame] | 3659 | case KVM_CAP_HALT_POLL: { |
| 3660 | if (cap->flags || cap->args[0] != (unsigned int)cap->args[0]) |
| 3661 | return -EINVAL; |
| 3662 | |
| 3663 | kvm->max_halt_poll_ns = cap->args[0]; |
| 3664 | return 0; |
| 3665 | } |
Paolo Bonzini | e5d83c7 | 2017-02-16 10:40:56 +0100 | [diff] [blame] | 3666 | default: |
| 3667 | return kvm_vm_ioctl_enable_cap(kvm, cap); |
| 3668 | } |
| 3669 | } |
| 3670 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3671 | static long kvm_vm_ioctl(struct file *filp, |
| 3672 | unsigned int ioctl, unsigned long arg) |
| 3673 | { |
| 3674 | struct kvm *kvm = filp->private_data; |
| 3675 | void __user *argp = (void __user *)arg; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3676 | int r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3677 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 3678 | if (kvm->mm != current->mm) |
| 3679 | return -EIO; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3680 | switch (ioctl) { |
| 3681 | case KVM_CREATE_VCPU: |
| 3682 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 3683 | break; |
Paolo Bonzini | e5d83c7 | 2017-02-16 10:40:56 +0100 | [diff] [blame] | 3684 | case KVM_ENABLE_CAP: { |
| 3685 | struct kvm_enable_cap cap; |
| 3686 | |
| 3687 | r = -EFAULT; |
| 3688 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 3689 | goto out; |
| 3690 | r = kvm_vm_ioctl_enable_cap_generic(kvm, &cap); |
| 3691 | break; |
| 3692 | } |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 3693 | case KVM_SET_USER_MEMORY_REGION: { |
| 3694 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 3695 | |
| 3696 | r = -EFAULT; |
| 3697 | if (copy_from_user(&kvm_userspace_mem, argp, |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3698 | sizeof(kvm_userspace_mem))) |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 3699 | goto out; |
| 3700 | |
Takuya Yoshikawa | 47ae31e | 2013-02-27 19:43:00 +0900 | [diff] [blame] | 3701 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3702 | break; |
| 3703 | } |
| 3704 | case KVM_GET_DIRTY_LOG: { |
| 3705 | struct kvm_dirty_log log; |
| 3706 | |
| 3707 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3708 | if (copy_from_user(&log, argp, sizeof(log))) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3709 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 3710 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3711 | break; |
| 3712 | } |
Paolo Bonzini | 2a31b9d | 2018-10-23 02:36:47 +0200 | [diff] [blame] | 3713 | #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT |
| 3714 | case KVM_CLEAR_DIRTY_LOG: { |
| 3715 | struct kvm_clear_dirty_log log; |
| 3716 | |
| 3717 | r = -EFAULT; |
| 3718 | if (copy_from_user(&log, argp, sizeof(log))) |
| 3719 | goto out; |
| 3720 | r = kvm_vm_ioctl_clear_dirty_log(kvm, &log); |
| 3721 | break; |
| 3722 | } |
| 3723 | #endif |
Paolo Bonzini | 4b4357e | 2017-03-31 13:53:23 +0200 | [diff] [blame] | 3724 | #ifdef CONFIG_KVM_MMIO |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3725 | case KVM_REGISTER_COALESCED_MMIO: { |
| 3726 | struct kvm_coalesced_mmio_zone zone; |
Xiubo Li | f95ef0cd | 2015-02-26 14:58:23 +0800 | [diff] [blame] | 3727 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3728 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3729 | if (copy_from_user(&zone, argp, sizeof(zone))) |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3730 | goto out; |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3731 | r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3732 | break; |
| 3733 | } |
| 3734 | case KVM_UNREGISTER_COALESCED_MMIO: { |
| 3735 | struct kvm_coalesced_mmio_zone zone; |
Xiubo Li | f95ef0cd | 2015-02-26 14:58:23 +0800 | [diff] [blame] | 3736 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3737 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3738 | if (copy_from_user(&zone, argp, sizeof(zone))) |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3739 | goto out; |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3740 | r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3741 | break; |
| 3742 | } |
| 3743 | #endif |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 3744 | case KVM_IRQFD: { |
| 3745 | struct kvm_irqfd data; |
| 3746 | |
| 3747 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3748 | if (copy_from_user(&data, argp, sizeof(data))) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 3749 | goto out; |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 3750 | r = kvm_irqfd(kvm, &data); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 3751 | break; |
| 3752 | } |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 3753 | case KVM_IOEVENTFD: { |
| 3754 | struct kvm_ioeventfd data; |
| 3755 | |
| 3756 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3757 | if (copy_from_user(&data, argp, sizeof(data))) |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 3758 | goto out; |
| 3759 | r = kvm_ioeventfd(kvm, &data); |
| 3760 | break; |
| 3761 | } |
Jan Kiszka | 07975ad | 2012-03-29 21:14:12 +0200 | [diff] [blame] | 3762 | #ifdef CONFIG_HAVE_KVM_MSI |
| 3763 | case KVM_SIGNAL_MSI: { |
| 3764 | struct kvm_msi msi; |
| 3765 | |
| 3766 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3767 | if (copy_from_user(&msi, argp, sizeof(msi))) |
Jan Kiszka | 07975ad | 2012-03-29 21:14:12 +0200 | [diff] [blame] | 3768 | goto out; |
| 3769 | r = kvm_send_userspace_msi(kvm, &msi); |
| 3770 | break; |
| 3771 | } |
| 3772 | #endif |
Christoffer Dall | 23d43cf | 2012-07-24 08:51:20 -0400 | [diff] [blame] | 3773 | #ifdef __KVM_HAVE_IRQ_LINE |
| 3774 | case KVM_IRQ_LINE_STATUS: |
| 3775 | case KVM_IRQ_LINE: { |
| 3776 | struct kvm_irq_level irq_event; |
| 3777 | |
| 3778 | r = -EFAULT; |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3779 | if (copy_from_user(&irq_event, argp, sizeof(irq_event))) |
Christoffer Dall | 23d43cf | 2012-07-24 08:51:20 -0400 | [diff] [blame] | 3780 | goto out; |
| 3781 | |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 3782 | r = kvm_vm_ioctl_irq_line(kvm, &irq_event, |
| 3783 | ioctl == KVM_IRQ_LINE_STATUS); |
Christoffer Dall | 23d43cf | 2012-07-24 08:51:20 -0400 | [diff] [blame] | 3784 | if (r) |
| 3785 | goto out; |
| 3786 | |
| 3787 | r = -EFAULT; |
| 3788 | if (ioctl == KVM_IRQ_LINE_STATUS) { |
Xiubo Li | 893bdbf | 2015-02-26 14:58:19 +0800 | [diff] [blame] | 3789 | if (copy_to_user(argp, &irq_event, sizeof(irq_event))) |
Christoffer Dall | 23d43cf | 2012-07-24 08:51:20 -0400 | [diff] [blame] | 3790 | goto out; |
| 3791 | } |
| 3792 | |
| 3793 | r = 0; |
| 3794 | break; |
| 3795 | } |
| 3796 | #endif |
Alexander Graf | aa8d594 | 2013-04-15 21:12:53 +0200 | [diff] [blame] | 3797 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
| 3798 | case KVM_SET_GSI_ROUTING: { |
| 3799 | struct kvm_irq_routing routing; |
| 3800 | struct kvm_irq_routing __user *urouting; |
Paolo Bonzini | f8c1b85 | 2016-06-01 14:09:22 +0200 | [diff] [blame] | 3801 | struct kvm_irq_routing_entry *entries = NULL; |
Alexander Graf | aa8d594 | 2013-04-15 21:12:53 +0200 | [diff] [blame] | 3802 | |
| 3803 | r = -EFAULT; |
| 3804 | if (copy_from_user(&routing, argp, sizeof(routing))) |
| 3805 | goto out; |
| 3806 | r = -EINVAL; |
David Hildenbrand | 5c0aea0 | 2017-04-28 17:06:20 +0200 | [diff] [blame] | 3807 | if (!kvm_arch_can_set_irq_routing(kvm)) |
| 3808 | goto out; |
Xiubo Li | caf1ff2 | 2016-06-15 18:00:33 +0800 | [diff] [blame] | 3809 | if (routing.nr > KVM_MAX_IRQ_ROUTES) |
Alexander Graf | aa8d594 | 2013-04-15 21:12:53 +0200 | [diff] [blame] | 3810 | goto out; |
| 3811 | if (routing.flags) |
| 3812 | goto out; |
Paolo Bonzini | f8c1b85 | 2016-06-01 14:09:22 +0200 | [diff] [blame] | 3813 | if (routing.nr) { |
Paolo Bonzini | f8c1b85 | 2016-06-01 14:09:22 +0200 | [diff] [blame] | 3814 | urouting = argp; |
Denis Efremov | 7ec28e2 | 2020-06-03 13:11:31 +0300 | [diff] [blame] | 3815 | entries = vmemdup_user(urouting->entries, |
| 3816 | array_size(sizeof(*entries), |
| 3817 | routing.nr)); |
| 3818 | if (IS_ERR(entries)) { |
| 3819 | r = PTR_ERR(entries); |
| 3820 | goto out; |
| 3821 | } |
Paolo Bonzini | f8c1b85 | 2016-06-01 14:09:22 +0200 | [diff] [blame] | 3822 | } |
Alexander Graf | aa8d594 | 2013-04-15 21:12:53 +0200 | [diff] [blame] | 3823 | r = kvm_set_irq_routing(kvm, entries, routing.nr, |
| 3824 | routing.flags); |
Denis Efremov | 7ec28e2 | 2020-06-03 13:11:31 +0300 | [diff] [blame] | 3825 | kvfree(entries); |
Alexander Graf | aa8d594 | 2013-04-15 21:12:53 +0200 | [diff] [blame] | 3826 | break; |
| 3827 | } |
| 3828 | #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */ |
Scott Wood | 852b6d5 | 2013-04-12 14:08:42 +0000 | [diff] [blame] | 3829 | case KVM_CREATE_DEVICE: { |
| 3830 | struct kvm_create_device cd; |
| 3831 | |
| 3832 | r = -EFAULT; |
| 3833 | if (copy_from_user(&cd, argp, sizeof(cd))) |
| 3834 | goto out; |
| 3835 | |
| 3836 | r = kvm_ioctl_create_device(kvm, &cd); |
| 3837 | if (r) |
| 3838 | goto out; |
| 3839 | |
| 3840 | r = -EFAULT; |
| 3841 | if (copy_to_user(argp, &cd, sizeof(cd))) |
| 3842 | goto out; |
| 3843 | |
| 3844 | r = 0; |
| 3845 | break; |
| 3846 | } |
Alexander Graf | 92b591a | 2014-07-14 18:33:08 +0200 | [diff] [blame] | 3847 | case KVM_CHECK_EXTENSION: |
| 3848 | r = kvm_vm_ioctl_check_extension_generic(kvm, arg); |
| 3849 | break; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3850 | default: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3851 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3852 | } |
| 3853 | out: |
| 3854 | return r; |
| 3855 | } |
| 3856 | |
Christian Borntraeger | de8e5d7 | 2015-02-03 09:35:15 +0100 | [diff] [blame] | 3857 | #ifdef CONFIG_KVM_COMPAT |
Arnd Bergmann | 6ff5894 | 2009-10-22 14:19:27 +0200 | [diff] [blame] | 3858 | struct compat_kvm_dirty_log { |
| 3859 | __u32 slot; |
| 3860 | __u32 padding1; |
| 3861 | union { |
| 3862 | compat_uptr_t dirty_bitmap; /* one bit per page */ |
| 3863 | __u64 padding2; |
| 3864 | }; |
| 3865 | }; |
| 3866 | |
| 3867 | static long kvm_vm_compat_ioctl(struct file *filp, |
| 3868 | unsigned int ioctl, unsigned long arg) |
| 3869 | { |
| 3870 | struct kvm *kvm = filp->private_data; |
| 3871 | int r; |
| 3872 | |
| 3873 | if (kvm->mm != current->mm) |
| 3874 | return -EIO; |
| 3875 | switch (ioctl) { |
| 3876 | case KVM_GET_DIRTY_LOG: { |
| 3877 | struct compat_kvm_dirty_log compat_log; |
| 3878 | struct kvm_dirty_log log; |
| 3879 | |
Arnd Bergmann | 6ff5894 | 2009-10-22 14:19:27 +0200 | [diff] [blame] | 3880 | if (copy_from_user(&compat_log, (void __user *)arg, |
| 3881 | sizeof(compat_log))) |
Markus Elfring | f6a3b16 | 2017-01-22 11:30:21 +0100 | [diff] [blame] | 3882 | return -EFAULT; |
Arnd Bergmann | 6ff5894 | 2009-10-22 14:19:27 +0200 | [diff] [blame] | 3883 | log.slot = compat_log.slot; |
| 3884 | log.padding1 = compat_log.padding1; |
| 3885 | log.padding2 = compat_log.padding2; |
| 3886 | log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap); |
| 3887 | |
| 3888 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Arnd Bergmann | 6ff5894 | 2009-10-22 14:19:27 +0200 | [diff] [blame] | 3889 | break; |
| 3890 | } |
| 3891 | default: |
| 3892 | r = kvm_vm_ioctl(filp, ioctl, arg); |
| 3893 | } |
Arnd Bergmann | 6ff5894 | 2009-10-22 14:19:27 +0200 | [diff] [blame] | 3894 | return r; |
| 3895 | } |
| 3896 | #endif |
| 3897 | |
Christian Borntraeger | 3d3aab1 | 2008-12-02 11:17:32 +0100 | [diff] [blame] | 3898 | static struct file_operations kvm_vm_fops = { |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3899 | .release = kvm_vm_release, |
| 3900 | .unlocked_ioctl = kvm_vm_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 3901 | .llseek = noop_llseek, |
Marc Zyngier | 7ddfd3e | 2018-06-17 10:16:21 +0100 | [diff] [blame] | 3902 | KVM_COMPAT(kvm_vm_compat_ioctl), |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3903 | }; |
| 3904 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 3905 | static int kvm_dev_ioctl_create_vm(unsigned long type) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3906 | { |
Heiko Carstens | aac8763 | 2010-10-27 17:22:10 +0200 | [diff] [blame] | 3907 | int r; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3908 | struct kvm *kvm; |
Al Viro | 506cfba | 2016-07-14 18:54:17 +0200 | [diff] [blame] | 3909 | struct file *file; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3910 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 3911 | kvm = kvm_create_vm(type); |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 3912 | if (IS_ERR(kvm)) |
| 3913 | return PTR_ERR(kvm); |
Paolo Bonzini | 4b4357e | 2017-03-31 13:53:23 +0200 | [diff] [blame] | 3914 | #ifdef CONFIG_KVM_MMIO |
Takuya Yoshikawa | 6ce5a09 | 2010-03-15 22:13:30 +0900 | [diff] [blame] | 3915 | r = kvm_coalesced_mmio_init(kvm); |
Markus Elfring | 7858833 | 2017-11-21 13:40:17 +0100 | [diff] [blame] | 3916 | if (r < 0) |
| 3917 | goto put_kvm; |
Takuya Yoshikawa | 6ce5a09 | 2010-03-15 22:13:30 +0900 | [diff] [blame] | 3918 | #endif |
Al Viro | 506cfba | 2016-07-14 18:54:17 +0200 | [diff] [blame] | 3919 | r = get_unused_fd_flags(O_CLOEXEC); |
Markus Elfring | 7858833 | 2017-11-21 13:40:17 +0100 | [diff] [blame] | 3920 | if (r < 0) |
| 3921 | goto put_kvm; |
| 3922 | |
Al Viro | 506cfba | 2016-07-14 18:54:17 +0200 | [diff] [blame] | 3923 | file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR); |
| 3924 | if (IS_ERR(file)) { |
| 3925 | put_unused_fd(r); |
Markus Elfring | 7858833 | 2017-11-21 13:40:17 +0100 | [diff] [blame] | 3926 | r = PTR_ERR(file); |
| 3927 | goto put_kvm; |
Al Viro | 506cfba | 2016-07-14 18:54:17 +0200 | [diff] [blame] | 3928 | } |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 3929 | |
Paolo Bonzini | 525df86 | 2017-06-27 15:45:09 +0200 | [diff] [blame] | 3930 | /* |
| 3931 | * Don't call kvm_put_kvm anymore at this point; file->f_op is |
| 3932 | * already set, with ->release() being kvm_vm_release(). In error |
| 3933 | * cases it will be called by the final fput(file) and will take |
| 3934 | * care of doing kvm_put_kvm(kvm). |
| 3935 | */ |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 3936 | if (kvm_create_vm_debugfs(kvm, r) < 0) { |
Al Viro | 506cfba | 2016-07-14 18:54:17 +0200 | [diff] [blame] | 3937 | put_unused_fd(r); |
| 3938 | fput(file); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 3939 | return -ENOMEM; |
| 3940 | } |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 3941 | kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3942 | |
Al Viro | 506cfba | 2016-07-14 18:54:17 +0200 | [diff] [blame] | 3943 | fd_install(r, file); |
Heiko Carstens | aac8763 | 2010-10-27 17:22:10 +0200 | [diff] [blame] | 3944 | return r; |
Markus Elfring | 7858833 | 2017-11-21 13:40:17 +0100 | [diff] [blame] | 3945 | |
| 3946 | put_kvm: |
| 3947 | kvm_put_kvm(kvm); |
| 3948 | return r; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | static long kvm_dev_ioctl(struct file *filp, |
| 3952 | unsigned int ioctl, unsigned long arg) |
| 3953 | { |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 3954 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3955 | |
| 3956 | switch (ioctl) { |
| 3957 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 3958 | if (arg) |
| 3959 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3960 | r = KVM_API_VERSION; |
| 3961 | break; |
| 3962 | case KVM_CREATE_VM: |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 3963 | r = kvm_dev_ioctl_create_vm(arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 3964 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 3965 | case KVM_CHECK_EXTENSION: |
Alexander Graf | 784aa3d | 2014-07-14 18:27:35 +0200 | [diff] [blame] | 3966 | r = kvm_vm_ioctl_check_extension_generic(NULL, arg); |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 3967 | break; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 3968 | case KVM_GET_VCPU_MMAP_SIZE: |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 3969 | if (arg) |
| 3970 | goto out; |
Avi Kivity | adb1ff4 | 2008-01-24 15:13:08 +0200 | [diff] [blame] | 3971 | r = PAGE_SIZE; /* struct kvm_run */ |
| 3972 | #ifdef CONFIG_X86 |
| 3973 | r += PAGE_SIZE; /* pio data page */ |
| 3974 | #endif |
Paolo Bonzini | 4b4357e | 2017-03-31 13:53:23 +0200 | [diff] [blame] | 3975 | #ifdef CONFIG_KVM_MMIO |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 3976 | r += PAGE_SIZE; /* coalesced mmio ring page */ |
| 3977 | #endif |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 3978 | break; |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 3979 | case KVM_TRACE_ENABLE: |
| 3980 | case KVM_TRACE_PAUSE: |
| 3981 | case KVM_TRACE_DISABLE: |
Marcelo Tosatti | 2023a29 | 2009-06-18 11:47:28 -0300 | [diff] [blame] | 3982 | r = -EOPNOTSUPP; |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 3983 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3984 | default: |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3985 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3986 | } |
| 3987 | out: |
| 3988 | return r; |
| 3989 | } |
| 3990 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3991 | static struct file_operations kvm_chardev_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3992 | .unlocked_ioctl = kvm_dev_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 3993 | .llseek = noop_llseek, |
Marc Zyngier | 7ddfd3e | 2018-06-17 10:16:21 +0100 | [diff] [blame] | 3994 | KVM_COMPAT(kvm_dev_ioctl), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3995 | }; |
| 3996 | |
| 3997 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 3998 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3999 | "kvm", |
| 4000 | &kvm_chardev_ops, |
| 4001 | }; |
| 4002 | |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4003 | static void hardware_enable_nolock(void *junk) |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 4004 | { |
| 4005 | int cpu = raw_smp_processor_id(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4006 | int r; |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 4007 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 4008 | if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 4009 | return; |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4010 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 4011 | cpumask_set_cpu(cpu, cpus_hardware_enabled); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4012 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4013 | r = kvm_arch_hardware_enable(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4014 | |
| 4015 | if (r) { |
| 4016 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); |
| 4017 | atomic_inc(&hardware_enable_failed); |
Xiubo Li | 1170adc | 2015-02-26 14:58:26 +0800 | [diff] [blame] | 4018 | pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4019 | } |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 4020 | } |
| 4021 | |
Thomas Gleixner | 8c18b2d | 2016-07-13 17:16:37 +0000 | [diff] [blame] | 4022 | static int kvm_starting_cpu(unsigned int cpu) |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4023 | { |
Paolo Bonzini | 4a937f9 | 2013-09-10 12:58:35 +0200 | [diff] [blame] | 4024 | raw_spin_lock(&kvm_count_lock); |
Paolo Bonzini | 4fa92fb | 2013-09-10 12:57:17 +0200 | [diff] [blame] | 4025 | if (kvm_usage_count) |
| 4026 | hardware_enable_nolock(NULL); |
Paolo Bonzini | 4a937f9 | 2013-09-10 12:58:35 +0200 | [diff] [blame] | 4027 | raw_spin_unlock(&kvm_count_lock); |
Thomas Gleixner | 8c18b2d | 2016-07-13 17:16:37 +0000 | [diff] [blame] | 4028 | return 0; |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | static void hardware_disable_nolock(void *junk) |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 4032 | { |
| 4033 | int cpu = raw_smp_processor_id(); |
| 4034 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 4035 | if (!cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 4036 | return; |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 4037 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4038 | kvm_arch_hardware_disable(); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 4039 | } |
| 4040 | |
Thomas Gleixner | 8c18b2d | 2016-07-13 17:16:37 +0000 | [diff] [blame] | 4041 | static int kvm_dying_cpu(unsigned int cpu) |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4042 | { |
Paolo Bonzini | 4a937f9 | 2013-09-10 12:58:35 +0200 | [diff] [blame] | 4043 | raw_spin_lock(&kvm_count_lock); |
Paolo Bonzini | 4fa92fb | 2013-09-10 12:57:17 +0200 | [diff] [blame] | 4044 | if (kvm_usage_count) |
| 4045 | hardware_disable_nolock(NULL); |
Paolo Bonzini | 4a937f9 | 2013-09-10 12:58:35 +0200 | [diff] [blame] | 4046 | raw_spin_unlock(&kvm_count_lock); |
Thomas Gleixner | 8c18b2d | 2016-07-13 17:16:37 +0000 | [diff] [blame] | 4047 | return 0; |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4048 | } |
| 4049 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4050 | static void hardware_disable_all_nolock(void) |
| 4051 | { |
| 4052 | BUG_ON(!kvm_usage_count); |
| 4053 | |
| 4054 | kvm_usage_count--; |
| 4055 | if (!kvm_usage_count) |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4056 | on_each_cpu(hardware_disable_nolock, NULL, 1); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | static void hardware_disable_all(void) |
| 4060 | { |
Paolo Bonzini | 4a937f9 | 2013-09-10 12:58:35 +0200 | [diff] [blame] | 4061 | raw_spin_lock(&kvm_count_lock); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4062 | hardware_disable_all_nolock(); |
Paolo Bonzini | 4a937f9 | 2013-09-10 12:58:35 +0200 | [diff] [blame] | 4063 | raw_spin_unlock(&kvm_count_lock); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | static int hardware_enable_all(void) |
| 4067 | { |
| 4068 | int r = 0; |
| 4069 | |
Paolo Bonzini | 4a937f9 | 2013-09-10 12:58:35 +0200 | [diff] [blame] | 4070 | raw_spin_lock(&kvm_count_lock); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4071 | |
| 4072 | kvm_usage_count++; |
| 4073 | if (kvm_usage_count == 1) { |
| 4074 | atomic_set(&hardware_enable_failed, 0); |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4075 | on_each_cpu(hardware_enable_nolock, NULL, 1); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4076 | |
| 4077 | if (atomic_read(&hardware_enable_failed)) { |
| 4078 | hardware_disable_all_nolock(); |
| 4079 | r = -EBUSY; |
| 4080 | } |
| 4081 | } |
| 4082 | |
Paolo Bonzini | 4a937f9 | 2013-09-10 12:58:35 +0200 | [diff] [blame] | 4083 | raw_spin_unlock(&kvm_count_lock); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4084 | |
| 4085 | return r; |
| 4086 | } |
| 4087 | |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 4088 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4089 | void *v) |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 4090 | { |
Sheng Yang | 8e1c181 | 2009-04-29 11:09:04 +0800 | [diff] [blame] | 4091 | /* |
| 4092 | * Some (well, at least mine) BIOSes hang on reboot if |
| 4093 | * in vmx root mode. |
| 4094 | * |
| 4095 | * And Intel TXT required VMX off for all cpu when system shutdown. |
| 4096 | */ |
Xiubo Li | 1170adc | 2015-02-26 14:58:26 +0800 | [diff] [blame] | 4097 | pr_info("kvm: exiting hardware virtualization\n"); |
Sheng Yang | 8e1c181 | 2009-04-29 11:09:04 +0800 | [diff] [blame] | 4098 | kvm_rebooting = true; |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4099 | on_each_cpu(hardware_disable_nolock, NULL, 1); |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 4100 | return NOTIFY_OK; |
| 4101 | } |
| 4102 | |
| 4103 | static struct notifier_block kvm_reboot_notifier = { |
| 4104 | .notifier_call = kvm_reboot, |
| 4105 | .priority = 0, |
| 4106 | }; |
| 4107 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 4108 | static void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 4109 | { |
| 4110 | int i; |
| 4111 | |
| 4112 | for (i = 0; i < bus->dev_count; i++) { |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4113 | struct kvm_io_device *pos = bus->range[i].dev; |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 4114 | |
| 4115 | kvm_iodevice_destructor(pos); |
| 4116 | } |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 4117 | kfree(bus); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 4118 | } |
| 4119 | |
Paolo Bonzini | c21fbff | 2013-08-27 15:41:41 +0200 | [diff] [blame] | 4120 | static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1, |
Xiubo Li | 20e87b7 | 2015-02-26 14:58:25 +0800 | [diff] [blame] | 4121 | const struct kvm_io_range *r2) |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4122 | { |
Jason Wang | 8f4216c7 | 2015-09-15 14:41:57 +0800 | [diff] [blame] | 4123 | gpa_t addr1 = r1->addr; |
| 4124 | gpa_t addr2 = r2->addr; |
| 4125 | |
| 4126 | if (addr1 < addr2) |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4127 | return -1; |
Jason Wang | 8f4216c7 | 2015-09-15 14:41:57 +0800 | [diff] [blame] | 4128 | |
| 4129 | /* If r2->len == 0, match the exact address. If r2->len != 0, |
| 4130 | * accept any overlapping write. Any order is acceptable for |
| 4131 | * overlapping ranges, because kvm_io_bus_get_first_dev ensures |
| 4132 | * we process all of them. |
| 4133 | */ |
| 4134 | if (r2->len) { |
| 4135 | addr1 += r1->len; |
| 4136 | addr2 += r2->len; |
| 4137 | } |
| 4138 | |
| 4139 | if (addr1 > addr2) |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4140 | return 1; |
Jason Wang | 8f4216c7 | 2015-09-15 14:41:57 +0800 | [diff] [blame] | 4141 | |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4142 | return 0; |
| 4143 | } |
| 4144 | |
Paolo Bonzini | a343c9b | 2013-07-16 13:03:29 +0200 | [diff] [blame] | 4145 | static int kvm_io_bus_sort_cmp(const void *p1, const void *p2) |
| 4146 | { |
Paolo Bonzini | c21fbff | 2013-08-27 15:41:41 +0200 | [diff] [blame] | 4147 | return kvm_io_bus_cmp(p1, p2); |
Paolo Bonzini | a343c9b | 2013-07-16 13:03:29 +0200 | [diff] [blame] | 4148 | } |
| 4149 | |
Geoff Levand | 39369f7 | 2013-04-05 19:20:30 +0000 | [diff] [blame] | 4150 | static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus, |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4151 | gpa_t addr, int len) |
| 4152 | { |
| 4153 | struct kvm_io_range *range, key; |
| 4154 | int off; |
| 4155 | |
| 4156 | key = (struct kvm_io_range) { |
| 4157 | .addr = addr, |
| 4158 | .len = len, |
| 4159 | }; |
| 4160 | |
| 4161 | range = bsearch(&key, bus->range, bus->dev_count, |
| 4162 | sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp); |
| 4163 | if (range == NULL) |
| 4164 | return -ENOENT; |
| 4165 | |
| 4166 | off = range - bus->range; |
| 4167 | |
Paolo Bonzini | c21fbff | 2013-08-27 15:41:41 +0200 | [diff] [blame] | 4168 | while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0) |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4169 | off--; |
| 4170 | |
| 4171 | return off; |
| 4172 | } |
| 4173 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4174 | static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus, |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4175 | struct kvm_io_range *range, const void *val) |
| 4176 | { |
| 4177 | int idx; |
| 4178 | |
| 4179 | idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len); |
| 4180 | if (idx < 0) |
| 4181 | return -EOPNOTSUPP; |
| 4182 | |
| 4183 | while (idx < bus->dev_count && |
Paolo Bonzini | c21fbff | 2013-08-27 15:41:41 +0200 | [diff] [blame] | 4184 | kvm_io_bus_cmp(range, &bus->range[idx]) == 0) { |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4185 | if (!kvm_iodevice_write(vcpu, bus->range[idx].dev, range->addr, |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4186 | range->len, val)) |
| 4187 | return idx; |
| 4188 | idx++; |
| 4189 | } |
| 4190 | |
| 4191 | return -EOPNOTSUPP; |
| 4192 | } |
| 4193 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 4194 | /* kvm_io_bus_write - called under kvm->slots_lock */ |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4195 | int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 4196 | int len, const void *val) |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 4197 | { |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4198 | struct kvm_io_bus *bus; |
| 4199 | struct kvm_io_range range; |
| 4200 | int r; |
| 4201 | |
| 4202 | range = (struct kvm_io_range) { |
| 4203 | .addr = addr, |
| 4204 | .len = len, |
| 4205 | }; |
| 4206 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4207 | bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4208 | if (!bus) |
| 4209 | return -ENOMEM; |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4210 | r = __kvm_io_bus_write(vcpu, bus, &range, val); |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4211 | return r < 0 ? r : 0; |
| 4212 | } |
Leo Yan | a242010 | 2019-02-22 16:10:09 +0800 | [diff] [blame] | 4213 | EXPORT_SYMBOL_GPL(kvm_io_bus_write); |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4214 | |
| 4215 | /* kvm_io_bus_write_cookie - called under kvm->slots_lock */ |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4216 | int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, |
| 4217 | gpa_t addr, int len, const void *val, long cookie) |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4218 | { |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 4219 | struct kvm_io_bus *bus; |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4220 | struct kvm_io_range range; |
| 4221 | |
| 4222 | range = (struct kvm_io_range) { |
| 4223 | .addr = addr, |
| 4224 | .len = len, |
| 4225 | }; |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 4226 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4227 | bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4228 | if (!bus) |
| 4229 | return -ENOMEM; |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4230 | |
| 4231 | /* First try the device referenced by cookie. */ |
| 4232 | if ((cookie >= 0) && (cookie < bus->dev_count) && |
Paolo Bonzini | c21fbff | 2013-08-27 15:41:41 +0200 | [diff] [blame] | 4233 | (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0)) |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4234 | if (!kvm_iodevice_write(vcpu, bus->range[cookie].dev, addr, len, |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4235 | val)) |
| 4236 | return cookie; |
| 4237 | |
| 4238 | /* |
| 4239 | * cookie contained garbage; fall back to search and return the |
| 4240 | * correct cookie value. |
| 4241 | */ |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4242 | return __kvm_io_bus_write(vcpu, bus, &range, val); |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4243 | } |
| 4244 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4245 | static int __kvm_io_bus_read(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus, |
| 4246 | struct kvm_io_range *range, void *val) |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4247 | { |
| 4248 | int idx; |
| 4249 | |
| 4250 | idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len); |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4251 | if (idx < 0) |
| 4252 | return -EOPNOTSUPP; |
| 4253 | |
| 4254 | while (idx < bus->dev_count && |
Paolo Bonzini | c21fbff | 2013-08-27 15:41:41 +0200 | [diff] [blame] | 4255 | kvm_io_bus_cmp(range, &bus->range[idx]) == 0) { |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4256 | if (!kvm_iodevice_read(vcpu, bus->range[idx].dev, range->addr, |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4257 | range->len, val)) |
| 4258 | return idx; |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4259 | idx++; |
| 4260 | } |
| 4261 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 4262 | return -EOPNOTSUPP; |
| 4263 | } |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 4264 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 4265 | /* kvm_io_bus_read - called under kvm->slots_lock */ |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4266 | int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 4267 | int len, void *val) |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 4268 | { |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4269 | struct kvm_io_bus *bus; |
| 4270 | struct kvm_io_range range; |
| 4271 | int r; |
| 4272 | |
| 4273 | range = (struct kvm_io_range) { |
| 4274 | .addr = addr, |
| 4275 | .len = len, |
| 4276 | }; |
| 4277 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4278 | bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4279 | if (!bus) |
| 4280 | return -ENOMEM; |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 4281 | r = __kvm_io_bus_read(vcpu, bus, &range, val); |
Cornelia Huck | 126a5af | 2013-07-03 16:30:53 +0200 | [diff] [blame] | 4282 | return r < 0 ? r : 0; |
| 4283 | } |
| 4284 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4285 | /* Caller must hold slots_lock. */ |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 4286 | int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, |
| 4287 | int len, struct kvm_io_device *dev) |
Michael S. Tsirkin | 6c47469 | 2009-06-29 22:24:26 +0300 | [diff] [blame] | 4288 | { |
Gal Hammer | d4c67a7 | 2018-01-16 15:34:41 +0200 | [diff] [blame] | 4289 | int i; |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 4290 | struct kvm_io_bus *new_bus, *bus; |
Gal Hammer | d4c67a7 | 2018-01-16 15:34:41 +0200 | [diff] [blame] | 4291 | struct kvm_io_range range; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 4292 | |
Christian Borntraeger | 4a12f95 | 2017-07-07 10:51:38 +0200 | [diff] [blame] | 4293 | bus = kvm_get_bus(kvm, bus_idx); |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4294 | if (!bus) |
| 4295 | return -ENOMEM; |
| 4296 | |
Amos Kong | 6ea34c9 | 2013-05-25 06:44:15 +0800 | [diff] [blame] | 4297 | /* exclude ioeventfd which is limited by maximum fd */ |
| 4298 | if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1) |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 4299 | return -ENOSPC; |
| 4300 | |
Gustavo A. R. Silva | 90952cd | 2019-01-30 17:07:47 +0100 | [diff] [blame] | 4301 | new_bus = kmalloc(struct_size(bus, range, bus->dev_count + 1), |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 4302 | GFP_KERNEL_ACCOUNT); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 4303 | if (!new_bus) |
| 4304 | return -ENOMEM; |
Gal Hammer | d4c67a7 | 2018-01-16 15:34:41 +0200 | [diff] [blame] | 4305 | |
| 4306 | range = (struct kvm_io_range) { |
| 4307 | .addr = addr, |
| 4308 | .len = len, |
| 4309 | .dev = dev, |
| 4310 | }; |
| 4311 | |
| 4312 | for (i = 0; i < bus->dev_count; i++) |
| 4313 | if (kvm_io_bus_cmp(&bus->range[i], &range) > 0) |
| 4314 | break; |
| 4315 | |
| 4316 | memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range)); |
| 4317 | new_bus->dev_count++; |
| 4318 | new_bus->range[i] = range; |
| 4319 | memcpy(new_bus->range + i + 1, bus->range + i, |
| 4320 | (bus->dev_count - i) * sizeof(struct kvm_io_range)); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 4321 | rcu_assign_pointer(kvm->buses[bus_idx], new_bus); |
| 4322 | synchronize_srcu_expedited(&kvm->srcu); |
| 4323 | kfree(bus); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 4324 | |
| 4325 | return 0; |
| 4326 | } |
| 4327 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4328 | /* Caller must hold slots_lock. */ |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4329 | void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, |
| 4330 | struct kvm_io_device *dev) |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 4331 | { |
Rustam Kovhaev | f658866 | 2020-09-07 11:55:35 -0700 | [diff] [blame] | 4332 | int i, j; |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 4333 | struct kvm_io_bus *new_bus, *bus; |
Michael S. Tsirkin | 6c47469 | 2009-06-29 22:24:26 +0300 | [diff] [blame] | 4334 | |
Christian Borntraeger | 4a12f95 | 2017-07-07 10:51:38 +0200 | [diff] [blame] | 4335 | bus = kvm_get_bus(kvm, bus_idx); |
Peter Xu | df630b8 | 2017-03-15 16:01:17 +0800 | [diff] [blame] | 4336 | if (!bus) |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4337 | return; |
Peter Xu | df630b8 | 2017-03-15 16:01:17 +0800 | [diff] [blame] | 4338 | |
Amos Kong | a1300716 | 2012-03-09 12:17:32 +0800 | [diff] [blame] | 4339 | for (i = 0; i < bus->dev_count; i++) |
| 4340 | if (bus->range[i].dev == dev) { |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 4341 | break; |
| 4342 | } |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 4343 | |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4344 | if (i == bus->dev_count) |
| 4345 | return; |
Amos Kong | a1300716 | 2012-03-09 12:17:32 +0800 | [diff] [blame] | 4346 | |
Gustavo A. R. Silva | 90952cd | 2019-01-30 17:07:47 +0100 | [diff] [blame] | 4347 | new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1), |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 4348 | GFP_KERNEL_ACCOUNT); |
Rustam Kovhaev | f658866 | 2020-09-07 11:55:35 -0700 | [diff] [blame] | 4349 | if (new_bus) { |
Rustam Kovhaev | 871c433 | 2020-09-18 05:05:00 -0700 | [diff] [blame] | 4350 | memcpy(new_bus, bus, struct_size(bus, range, i)); |
Rustam Kovhaev | f658866 | 2020-09-07 11:55:35 -0700 | [diff] [blame] | 4351 | new_bus->dev_count--; |
| 4352 | memcpy(new_bus->range + i, bus->range + i + 1, |
Rustam Kovhaev | 871c433 | 2020-09-18 05:05:00 -0700 | [diff] [blame] | 4353 | flex_array_size(new_bus, range, new_bus->dev_count - i)); |
Rustam Kovhaev | f658866 | 2020-09-07 11:55:35 -0700 | [diff] [blame] | 4354 | } else { |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4355 | pr_err("kvm: failed to shrink bus, removing it completely\n"); |
Rustam Kovhaev | f658866 | 2020-09-07 11:55:35 -0700 | [diff] [blame] | 4356 | for (j = 0; j < bus->dev_count; j++) { |
| 4357 | if (j == i) |
| 4358 | continue; |
| 4359 | kvm_iodevice_destructor(bus->range[j].dev); |
| 4360 | } |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4361 | } |
Amos Kong | a1300716 | 2012-03-09 12:17:32 +0800 | [diff] [blame] | 4362 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 4363 | rcu_assign_pointer(kvm->buses[bus_idx], new_bus); |
| 4364 | synchronize_srcu_expedited(&kvm->srcu); |
| 4365 | kfree(bus); |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4366 | return; |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 4367 | } |
| 4368 | |
Andre Przywara | 8a39d00 | 2016-07-15 12:43:26 +0100 | [diff] [blame] | 4369 | struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx, |
| 4370 | gpa_t addr) |
| 4371 | { |
| 4372 | struct kvm_io_bus *bus; |
| 4373 | int dev_idx, srcu_idx; |
| 4374 | struct kvm_io_device *iodev = NULL; |
| 4375 | |
| 4376 | srcu_idx = srcu_read_lock(&kvm->srcu); |
| 4377 | |
| 4378 | bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu); |
David Hildenbrand | 90db104 | 2017-03-23 18:24:19 +0100 | [diff] [blame] | 4379 | if (!bus) |
| 4380 | goto out_unlock; |
Andre Przywara | 8a39d00 | 2016-07-15 12:43:26 +0100 | [diff] [blame] | 4381 | |
| 4382 | dev_idx = kvm_io_bus_get_first_dev(bus, addr, 1); |
| 4383 | if (dev_idx < 0) |
| 4384 | goto out_unlock; |
| 4385 | |
| 4386 | iodev = bus->range[dev_idx].dev; |
| 4387 | |
| 4388 | out_unlock: |
| 4389 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
| 4390 | |
| 4391 | return iodev; |
| 4392 | } |
| 4393 | EXPORT_SYMBOL_GPL(kvm_io_bus_get_dev); |
| 4394 | |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4395 | static int kvm_debugfs_open(struct inode *inode, struct file *file, |
| 4396 | int (*get)(void *, u64 *), int (*set)(void *, u64), |
| 4397 | const char *fmt) |
| 4398 | { |
| 4399 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *) |
| 4400 | inode->i_private; |
| 4401 | |
| 4402 | /* The debugfs files are a reference to the kvm struct which |
| 4403 | * is still valid when kvm_destroy_vm is called. |
| 4404 | * To avoid the race between open and the removal of the debugfs |
| 4405 | * directory we test against the users count. |
| 4406 | */ |
Elena Reshetova | e3736c3 | 2017-02-20 13:06:21 +0200 | [diff] [blame] | 4407 | if (!refcount_inc_not_zero(&stat_data->kvm->users_count)) |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4408 | return -ENOENT; |
| 4409 | |
Paolo Bonzini | 833b45d | 2019-09-30 18:48:44 +0200 | [diff] [blame] | 4410 | if (simple_attr_open(inode, file, get, |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4411 | KVM_DBGFS_GET_MODE(stat_data->dbgfs_item) & 0222 |
| 4412 | ? set : NULL, |
| 4413 | fmt)) { |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4414 | kvm_put_kvm(stat_data->kvm); |
| 4415 | return -ENOMEM; |
| 4416 | } |
| 4417 | |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
| 4421 | static int kvm_debugfs_release(struct inode *inode, struct file *file) |
| 4422 | { |
| 4423 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *) |
| 4424 | inode->i_private; |
| 4425 | |
| 4426 | simple_attr_release(inode, file); |
| 4427 | kvm_put_kvm(stat_data->kvm); |
| 4428 | |
| 4429 | return 0; |
| 4430 | } |
| 4431 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4432 | static int kvm_get_stat_per_vm(struct kvm *kvm, size_t offset, u64 *val) |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4433 | { |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4434 | *val = *(ulong *)((void *)kvm + offset); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4435 | |
| 4436 | return 0; |
| 4437 | } |
| 4438 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4439 | static int kvm_clear_stat_per_vm(struct kvm *kvm, size_t offset) |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4440 | { |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4441 | *(ulong *)((void *)kvm + offset) = 0; |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4442 | |
| 4443 | return 0; |
| 4444 | } |
| 4445 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4446 | static int kvm_get_stat_per_vcpu(struct kvm *kvm, size_t offset, u64 *val) |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4447 | { |
| 4448 | int i; |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4449 | struct kvm_vcpu *vcpu; |
| 4450 | |
| 4451 | *val = 0; |
| 4452 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4453 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 4454 | *val += *(u64 *)((void *)vcpu + offset); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4455 | |
| 4456 | return 0; |
| 4457 | } |
| 4458 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4459 | static int kvm_clear_stat_per_vcpu(struct kvm *kvm, size_t offset) |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4460 | { |
| 4461 | int i; |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4462 | struct kvm_vcpu *vcpu; |
| 4463 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4464 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 4465 | *(u64 *)((void *)vcpu + offset) = 0; |
| 4466 | |
| 4467 | return 0; |
| 4468 | } |
| 4469 | |
| 4470 | static int kvm_stat_data_get(void *data, u64 *val) |
| 4471 | { |
| 4472 | int r = -EFAULT; |
| 4473 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data; |
| 4474 | |
| 4475 | switch (stat_data->dbgfs_item->kind) { |
| 4476 | case KVM_STAT_VM: |
| 4477 | r = kvm_get_stat_per_vm(stat_data->kvm, |
| 4478 | stat_data->dbgfs_item->offset, val); |
| 4479 | break; |
| 4480 | case KVM_STAT_VCPU: |
| 4481 | r = kvm_get_stat_per_vcpu(stat_data->kvm, |
| 4482 | stat_data->dbgfs_item->offset, val); |
| 4483 | break; |
| 4484 | } |
| 4485 | |
| 4486 | return r; |
| 4487 | } |
| 4488 | |
| 4489 | static int kvm_stat_data_clear(void *data, u64 val) |
| 4490 | { |
| 4491 | int r = -EFAULT; |
| 4492 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data; |
| 4493 | |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4494 | if (val) |
| 4495 | return -EINVAL; |
| 4496 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4497 | switch (stat_data->dbgfs_item->kind) { |
| 4498 | case KVM_STAT_VM: |
| 4499 | r = kvm_clear_stat_per_vm(stat_data->kvm, |
| 4500 | stat_data->dbgfs_item->offset); |
| 4501 | break; |
| 4502 | case KVM_STAT_VCPU: |
| 4503 | r = kvm_clear_stat_per_vcpu(stat_data->kvm, |
| 4504 | stat_data->dbgfs_item->offset); |
| 4505 | break; |
| 4506 | } |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4507 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4508 | return r; |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4509 | } |
| 4510 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4511 | static int kvm_stat_data_open(struct inode *inode, struct file *file) |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4512 | { |
| 4513 | __simple_attr_check_format("%llu\n", 0ull); |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4514 | return kvm_debugfs_open(inode, file, kvm_stat_data_get, |
| 4515 | kvm_stat_data_clear, "%llu\n"); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4518 | static const struct file_operations stat_fops_per_vm = { |
| 4519 | .owner = THIS_MODULE, |
| 4520 | .open = kvm_stat_data_open, |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4521 | .release = kvm_debugfs_release, |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4522 | .read = simple_attr_read, |
| 4523 | .write = simple_attr_write, |
| 4524 | .llseek = no_llseek, |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4525 | }; |
| 4526 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 4527 | static int vm_stat_get(void *_offset, u64 *val) |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 4528 | { |
| 4529 | unsigned offset = (long)_offset; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 4530 | struct kvm *kvm; |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4531 | u64 tmp_val; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 4532 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 4533 | *val = 0; |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4534 | mutex_lock(&kvm_lock); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4535 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4536 | kvm_get_stat_per_vm(kvm, offset, &tmp_val); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4537 | *val += tmp_val; |
| 4538 | } |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4539 | mutex_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 4540 | return 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 4541 | } |
| 4542 | |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4543 | static int vm_stat_clear(void *_offset, u64 val) |
| 4544 | { |
| 4545 | unsigned offset = (long)_offset; |
| 4546 | struct kvm *kvm; |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4547 | |
| 4548 | if (val) |
| 4549 | return -EINVAL; |
| 4550 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4551 | mutex_lock(&kvm_lock); |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4552 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4553 | kvm_clear_stat_per_vm(kvm, offset); |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4554 | } |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4555 | mutex_unlock(&kvm_lock); |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4556 | |
| 4557 | return 0; |
| 4558 | } |
| 4559 | |
| 4560 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n"); |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 4561 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 4562 | static int vcpu_stat_get(void *_offset, u64 *val) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4563 | { |
| 4564 | unsigned offset = (long)_offset; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4565 | struct kvm *kvm; |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4566 | u64 tmp_val; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4567 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 4568 | *val = 0; |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4569 | mutex_lock(&kvm_lock); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4570 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4571 | kvm_get_stat_per_vcpu(kvm, offset, &tmp_val); |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4572 | *val += tmp_val; |
| 4573 | } |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4574 | mutex_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 4575 | return 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4576 | } |
| 4577 | |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4578 | static int vcpu_stat_clear(void *_offset, u64 val) |
| 4579 | { |
| 4580 | unsigned offset = (long)_offset; |
| 4581 | struct kvm *kvm; |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4582 | |
| 4583 | if (val) |
| 4584 | return -EINVAL; |
| 4585 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4586 | mutex_lock(&kvm_lock); |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4587 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4588 | kvm_clear_stat_per_vcpu(kvm, offset); |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4589 | } |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4590 | mutex_unlock(&kvm_lock); |
Suraj Jitindar Singh | ce35ef2 | 2016-10-19 13:49:47 +1100 | [diff] [blame] | 4591 | |
| 4592 | return 0; |
| 4593 | } |
| 4594 | |
| 4595 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear, |
| 4596 | "%llu\n"); |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 4597 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4598 | static const struct file_operations *stat_fops[] = { |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 4599 | [KVM_STAT_VCPU] = &vcpu_stat_fops, |
| 4600 | [KVM_STAT_VM] = &vm_stat_fops, |
| 4601 | }; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4602 | |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4603 | static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm) |
| 4604 | { |
| 4605 | struct kobj_uevent_env *env; |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4606 | unsigned long long created, active; |
| 4607 | |
| 4608 | if (!kvm_dev.this_device || !kvm) |
| 4609 | return; |
| 4610 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4611 | mutex_lock(&kvm_lock); |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4612 | if (type == KVM_EVENT_CREATE_VM) { |
| 4613 | kvm_createvm_count++; |
| 4614 | kvm_active_vms++; |
| 4615 | } else if (type == KVM_EVENT_DESTROY_VM) { |
| 4616 | kvm_active_vms--; |
| 4617 | } |
| 4618 | created = kvm_createvm_count; |
| 4619 | active = kvm_active_vms; |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 4620 | mutex_unlock(&kvm_lock); |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4621 | |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 4622 | env = kzalloc(sizeof(*env), GFP_KERNEL_ACCOUNT); |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4623 | if (!env) |
| 4624 | return; |
| 4625 | |
| 4626 | add_uevent_var(env, "CREATED=%llu", created); |
| 4627 | add_uevent_var(env, "COUNT=%llu", active); |
| 4628 | |
Claudio Imbrenda | fdeaf7e | 2017-07-24 13:40:03 +0200 | [diff] [blame] | 4629 | if (type == KVM_EVENT_CREATE_VM) { |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4630 | add_uevent_var(env, "EVENT=create"); |
Claudio Imbrenda | fdeaf7e | 2017-07-24 13:40:03 +0200 | [diff] [blame] | 4631 | kvm->userspace_pid = task_pid_nr(current); |
| 4632 | } else if (type == KVM_EVENT_DESTROY_VM) { |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4633 | add_uevent_var(env, "EVENT=destroy"); |
Claudio Imbrenda | fdeaf7e | 2017-07-24 13:40:03 +0200 | [diff] [blame] | 4634 | } |
| 4635 | add_uevent_var(env, "PID=%d", kvm->userspace_pid); |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4636 | |
Greg Kroah-Hartman | 8ed0579 | 2019-02-28 16:34:37 +0100 | [diff] [blame] | 4637 | if (!IS_ERR_OR_NULL(kvm->debugfs_dentry)) { |
Ben Gardon | b12ce36 | 2019-02-11 11:02:49 -0800 | [diff] [blame] | 4638 | char *tmp, *p = kmalloc(PATH_MAX, GFP_KERNEL_ACCOUNT); |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4639 | |
Claudio Imbrenda | fdeaf7e | 2017-07-24 13:40:03 +0200 | [diff] [blame] | 4640 | if (p) { |
| 4641 | tmp = dentry_path_raw(kvm->debugfs_dentry, p, PATH_MAX); |
| 4642 | if (!IS_ERR(tmp)) |
| 4643 | add_uevent_var(env, "STATS_PATH=%s", tmp); |
| 4644 | kfree(p); |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4645 | } |
| 4646 | } |
| 4647 | /* no need for checks, since we are adding at most only 5 keys */ |
| 4648 | env->envp[env->envp_idx++] = NULL; |
| 4649 | kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp); |
| 4650 | kfree(env); |
Claudio Imbrenda | 286de8f | 2017-07-12 17:56:44 +0200 | [diff] [blame] | 4651 | } |
| 4652 | |
Greg Kroah-Hartman | 929f45e | 2018-05-29 18:22:04 +0200 | [diff] [blame] | 4653 | static void kvm_init_debug(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4654 | { |
| 4655 | struct kvm_stats_debugfs_item *p; |
| 4656 | |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 4657 | kvm_debugfs_dir = debugfs_create_dir("kvm", NULL); |
Hamo | 4f69b68 | 2011-12-15 14:23:16 +0800 | [diff] [blame] | 4658 | |
Janosch Frank | 536a6f8 | 2016-05-18 13:26:23 +0200 | [diff] [blame] | 4659 | kvm_debugfs_num_entries = 0; |
| 4660 | for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) { |
Milan Pandurov | 09cbcef | 2019-12-13 14:07:21 +0100 | [diff] [blame] | 4661 | debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p), |
| 4662 | kvm_debugfs_dir, (void *)(long)p->offset, |
Greg Kroah-Hartman | 929f45e | 2018-05-29 18:22:04 +0200 | [diff] [blame] | 4663 | stat_fops[p->kind]); |
Hamo | 4f69b68 | 2011-12-15 14:23:16 +0800 | [diff] [blame] | 4664 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4665 | } |
| 4666 | |
Rafael J. Wysocki | fb3600c | 2011-03-23 22:16:23 +0100 | [diff] [blame] | 4667 | static int kvm_suspend(void) |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 4668 | { |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4669 | if (kvm_usage_count) |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4670 | hardware_disable_nolock(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 4671 | return 0; |
| 4672 | } |
| 4673 | |
Rafael J. Wysocki | fb3600c | 2011-03-23 22:16:23 +0100 | [diff] [blame] | 4674 | static void kvm_resume(void) |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 4675 | { |
Zachary Amsden | ca84d1a | 2010-08-19 22:07:28 -1000 | [diff] [blame] | 4676 | if (kvm_usage_count) { |
Wanpeng Li | 2eb06c3 | 2019-05-17 16:49:49 +0800 | [diff] [blame] | 4677 | #ifdef CONFIG_LOCKDEP |
| 4678 | WARN_ON(lockdep_is_held(&kvm_count_lock)); |
| 4679 | #endif |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4680 | hardware_enable_nolock(NULL); |
Zachary Amsden | ca84d1a | 2010-08-19 22:07:28 -1000 | [diff] [blame] | 4681 | } |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 4682 | } |
| 4683 | |
Rafael J. Wysocki | fb3600c | 2011-03-23 22:16:23 +0100 | [diff] [blame] | 4684 | static struct syscore_ops kvm_syscore_ops = { |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 4685 | .suspend = kvm_suspend, |
| 4686 | .resume = kvm_resume, |
| 4687 | }; |
| 4688 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 4689 | static inline |
| 4690 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) |
| 4691 | { |
| 4692 | return container_of(pn, struct kvm_vcpu, preempt_notifier); |
| 4693 | } |
| 4694 | |
| 4695 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) |
| 4696 | { |
| 4697 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
Xiubo Li | f95ef0cd | 2015-02-26 14:58:23 +0800 | [diff] [blame] | 4698 | |
Wanpeng Li | 046ddee | 2019-08-01 11:30:14 +0800 | [diff] [blame] | 4699 | WRITE_ONCE(vcpu->preempted, false); |
Wanpeng Li | d73eb57 | 2019-07-18 19:39:06 +0800 | [diff] [blame] | 4700 | WRITE_ONCE(vcpu->ready, false); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 4701 | |
Paolo Bonzini | 7495e22 | 2020-01-09 09:57:19 -0500 | [diff] [blame] | 4702 | __this_cpu_write(kvm_running_vcpu, vcpu); |
Radim Krčmář | e790d9e | 2014-08-21 18:08:05 +0200 | [diff] [blame] | 4703 | kvm_arch_sched_in(vcpu, cpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4704 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | static void kvm_sched_out(struct preempt_notifier *pn, |
| 4708 | struct task_struct *next) |
| 4709 | { |
| 4710 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 4711 | |
Wanpeng Li | d73eb57 | 2019-07-18 19:39:06 +0800 | [diff] [blame] | 4712 | if (current->state == TASK_RUNNING) { |
Wanpeng Li | 046ddee | 2019-08-01 11:30:14 +0800 | [diff] [blame] | 4713 | WRITE_ONCE(vcpu->preempted, true); |
Wanpeng Li | d73eb57 | 2019-07-18 19:39:06 +0800 | [diff] [blame] | 4714 | WRITE_ONCE(vcpu->ready, true); |
| 4715 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4716 | kvm_arch_vcpu_put(vcpu); |
Paolo Bonzini | 7495e22 | 2020-01-09 09:57:19 -0500 | [diff] [blame] | 4717 | __this_cpu_write(kvm_running_vcpu, NULL); |
| 4718 | } |
| 4719 | |
| 4720 | /** |
| 4721 | * kvm_get_running_vcpu - get the vcpu running on the current CPU. |
Marc Zyngier | 1f03b2b | 2020-02-07 16:34:10 +0000 | [diff] [blame] | 4722 | * |
| 4723 | * We can disable preemption locally around accessing the per-CPU variable, |
| 4724 | * and use the resolved vcpu pointer after enabling preemption again, |
| 4725 | * because even if the current thread is migrated to another CPU, reading |
| 4726 | * the per-CPU value later will give us the same value as we update the |
| 4727 | * per-CPU variable in the preempt notifier handlers. |
Paolo Bonzini | 7495e22 | 2020-01-09 09:57:19 -0500 | [diff] [blame] | 4728 | */ |
| 4729 | struct kvm_vcpu *kvm_get_running_vcpu(void) |
| 4730 | { |
Marc Zyngier | 1f03b2b | 2020-02-07 16:34:10 +0000 | [diff] [blame] | 4731 | struct kvm_vcpu *vcpu; |
| 4732 | |
| 4733 | preempt_disable(); |
| 4734 | vcpu = __this_cpu_read(kvm_running_vcpu); |
| 4735 | preempt_enable(); |
| 4736 | |
| 4737 | return vcpu; |
Paolo Bonzini | 7495e22 | 2020-01-09 09:57:19 -0500 | [diff] [blame] | 4738 | } |
Wanpeng Li | 379a3c8 | 2020-04-28 14:23:27 +0800 | [diff] [blame] | 4739 | EXPORT_SYMBOL_GPL(kvm_get_running_vcpu); |
Paolo Bonzini | 7495e22 | 2020-01-09 09:57:19 -0500 | [diff] [blame] | 4740 | |
| 4741 | /** |
| 4742 | * kvm_get_running_vcpus - get the per-CPU array of currently running vcpus. |
| 4743 | */ |
| 4744 | struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void) |
| 4745 | { |
| 4746 | return &kvm_running_vcpu; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 4747 | } |
| 4748 | |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 4749 | struct kvm_cpu_compat_check { |
| 4750 | void *opaque; |
| 4751 | int *ret; |
| 4752 | }; |
| 4753 | |
| 4754 | static void check_processor_compat(void *data) |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 4755 | { |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 4756 | struct kvm_cpu_compat_check *c = data; |
| 4757 | |
| 4758 | *c->ret = kvm_arch_check_processor_compat(c->opaque); |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 4759 | } |
| 4760 | |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 4761 | int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 4762 | struct module *module) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4763 | { |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 4764 | struct kvm_cpu_compat_check c; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4765 | int r; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4766 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4767 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4768 | r = kvm_arch_init(opaque); |
| 4769 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 4770 | goto out_fail; |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 4771 | |
Asias He | 7dac16c | 2013-05-08 10:57:29 +0800 | [diff] [blame] | 4772 | /* |
| 4773 | * kvm_arch_init makes sure there's at most one caller |
| 4774 | * for architectures that support multiple implementations, |
| 4775 | * like intel and amd on x86. |
Paolo Bonzini | 36343f6 | 2016-10-26 13:35:56 +0200 | [diff] [blame] | 4776 | * kvm_arch_init must be called before kvm_irqfd_init to avoid creating |
| 4777 | * conflicts in case kvm is already setup for another implementation. |
Asias He | 7dac16c | 2013-05-08 10:57:29 +0800 | [diff] [blame] | 4778 | */ |
Paolo Bonzini | 36343f6 | 2016-10-26 13:35:56 +0200 | [diff] [blame] | 4779 | r = kvm_irqfd_init(); |
| 4780 | if (r) |
| 4781 | goto out_irqfd; |
Asias He | 7dac16c | 2013-05-08 10:57:29 +0800 | [diff] [blame] | 4782 | |
Avi Kivity | 8437a617 | 2009-06-06 14:52:35 -0700 | [diff] [blame] | 4783 | if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) { |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 4784 | r = -ENOMEM; |
| 4785 | goto out_free_0; |
| 4786 | } |
| 4787 | |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 4788 | r = kvm_arch_hardware_setup(opaque); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4789 | if (r < 0) |
Miaohe Lin | faf0be2 | 2019-11-23 10:45:50 +0800 | [diff] [blame] | 4790 | goto out_free_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4791 | |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 4792 | c.ret = &r; |
| 4793 | c.opaque = opaque; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4794 | for_each_online_cpu(cpu) { |
Sean Christopherson | b990408 | 2020-03-21 13:25:55 -0700 | [diff] [blame] | 4795 | smp_call_function_single(cpu, check_processor_compat, &c, 1); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4796 | if (r < 0) |
Miaohe Lin | faf0be2 | 2019-11-23 10:45:50 +0800 | [diff] [blame] | 4797 | goto out_free_2; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4798 | } |
| 4799 | |
Thomas Gleixner | 73c1b41 | 2016-12-21 20:19:54 +0100 | [diff] [blame] | 4800 | r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "kvm/cpu:starting", |
Thomas Gleixner | 8c18b2d | 2016-07-13 17:16:37 +0000 | [diff] [blame] | 4801 | kvm_starting_cpu, kvm_dying_cpu); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 4802 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 4803 | goto out_free_2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4804 | register_reboot_notifier(&kvm_reboot_notifier); |
| 4805 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 4806 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 4807 | if (!vcpu_align) |
| 4808 | vcpu_align = __alignof__(struct kvm_vcpu); |
Paolo Bonzini | 4651573 | 2017-10-26 15:45:46 +0200 | [diff] [blame] | 4809 | kvm_vcpu_cache = |
| 4810 | kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align, |
| 4811 | SLAB_ACCOUNT, |
| 4812 | offsetof(struct kvm_vcpu, arch), |
| 4813 | sizeof_field(struct kvm_vcpu, arch), |
| 4814 | NULL); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 4815 | if (!kvm_vcpu_cache) { |
| 4816 | r = -ENOMEM; |
Rafael J. Wysocki | fb3600c | 2011-03-23 22:16:23 +0100 | [diff] [blame] | 4817 | goto out_free_3; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 4818 | } |
| 4819 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 4820 | r = kvm_async_pf_init(); |
| 4821 | if (r) |
| 4822 | goto out_free; |
| 4823 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4824 | kvm_chardev_ops.owner = module; |
Christian Borntraeger | 3d3aab1 | 2008-12-02 11:17:32 +0100 | [diff] [blame] | 4825 | kvm_vm_fops.owner = module; |
| 4826 | kvm_vcpu_fops.owner = module; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4827 | |
| 4828 | r = misc_register(&kvm_dev); |
| 4829 | if (r) { |
Xiubo Li | 1170adc | 2015-02-26 14:58:26 +0800 | [diff] [blame] | 4830 | pr_err("kvm: misc device register failed\n"); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 4831 | goto out_unreg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4832 | } |
| 4833 | |
Rafael J. Wysocki | fb3600c | 2011-03-23 22:16:23 +0100 | [diff] [blame] | 4834 | register_syscore_ops(&kvm_syscore_ops); |
| 4835 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 4836 | kvm_preempt_ops.sched_in = kvm_sched_in; |
| 4837 | kvm_preempt_ops.sched_out = kvm_sched_out; |
| 4838 | |
Greg Kroah-Hartman | 929f45e | 2018-05-29 18:22:04 +0200 | [diff] [blame] | 4839 | kvm_init_debug(); |
Darrick J. Wong | 0ea4ed8 | 2009-10-14 16:21:00 -0700 | [diff] [blame] | 4840 | |
Paolo Bonzini | 3c3c29f | 2014-09-24 13:02:46 +0200 | [diff] [blame] | 4841 | r = kvm_vfio_ops_init(); |
| 4842 | WARN_ON(r); |
| 4843 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 4844 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4845 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 4846 | out_unreg: |
| 4847 | kvm_async_pf_deinit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4848 | out_free: |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 4849 | kmem_cache_destroy(kvm_vcpu_cache); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 4850 | out_free_3: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4851 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Thomas Gleixner | 8c18b2d | 2016-07-13 17:16:37 +0000 | [diff] [blame] | 4852 | cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 4853 | out_free_2: |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4854 | kvm_arch_hardware_unsetup(); |
Miaohe Lin | faf0be2 | 2019-11-23 10:45:50 +0800 | [diff] [blame] | 4855 | out_free_1: |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 4856 | free_cpumask_var(cpus_hardware_enabled); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 4857 | out_free_0: |
Cornelia Huck | a0f155e | 2013-02-28 12:33:18 +0100 | [diff] [blame] | 4858 | kvm_irqfd_exit(); |
Paolo Bonzini | 36343f6 | 2016-10-26 13:35:56 +0200 | [diff] [blame] | 4859 | out_irqfd: |
Asias He | 7dac16c | 2013-05-08 10:57:29 +0800 | [diff] [blame] | 4860 | kvm_arch_exit(); |
| 4861 | out_fail: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4862 | return r; |
| 4863 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 4864 | EXPORT_SYMBOL_GPL(kvm_init); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4865 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 4866 | void kvm_exit(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4867 | { |
Janosch Frank | 4bd33b5 | 2015-10-14 12:37:35 +0200 | [diff] [blame] | 4868 | debugfs_remove_recursive(kvm_debugfs_dir); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4869 | misc_deregister(&kvm_dev); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 4870 | kmem_cache_destroy(kvm_vcpu_cache); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 4871 | kvm_async_pf_deinit(); |
Rafael J. Wysocki | fb3600c | 2011-03-23 22:16:23 +0100 | [diff] [blame] | 4872 | unregister_syscore_ops(&kvm_syscore_ops); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4873 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Thomas Gleixner | 8c18b2d | 2016-07-13 17:16:37 +0000 | [diff] [blame] | 4874 | cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); |
Takuya Yoshikawa | 75b7127 | 2010-11-16 17:37:41 +0900 | [diff] [blame] | 4875 | on_each_cpu(hardware_disable_nolock, NULL, 1); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4876 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4877 | kvm_arch_exit(); |
Cornelia Huck | a0f155e | 2013-02-28 12:33:18 +0100 | [diff] [blame] | 4878 | kvm_irqfd_exit(); |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 4879 | free_cpumask_var(cpus_hardware_enabled); |
Wanpeng Li | 571ee1b | 2014-10-09 18:30:08 +0800 | [diff] [blame] | 4880 | kvm_vfio_ops_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4881 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 4882 | EXPORT_SYMBOL_GPL(kvm_exit); |
Junaid Shahid | c57c804 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 4883 | |
| 4884 | struct kvm_vm_worker_thread_context { |
| 4885 | struct kvm *kvm; |
| 4886 | struct task_struct *parent; |
| 4887 | struct completion init_done; |
| 4888 | kvm_vm_thread_fn_t thread_fn; |
| 4889 | uintptr_t data; |
| 4890 | int err; |
| 4891 | }; |
| 4892 | |
| 4893 | static int kvm_vm_worker_thread(void *context) |
| 4894 | { |
| 4895 | /* |
| 4896 | * The init_context is allocated on the stack of the parent thread, so |
| 4897 | * we have to locally copy anything that is needed beyond initialization |
| 4898 | */ |
| 4899 | struct kvm_vm_worker_thread_context *init_context = context; |
| 4900 | struct kvm *kvm = init_context->kvm; |
| 4901 | kvm_vm_thread_fn_t thread_fn = init_context->thread_fn; |
| 4902 | uintptr_t data = init_context->data; |
| 4903 | int err; |
| 4904 | |
| 4905 | err = kthread_park(current); |
| 4906 | /* kthread_park(current) is never supposed to return an error */ |
| 4907 | WARN_ON(err != 0); |
| 4908 | if (err) |
| 4909 | goto init_complete; |
| 4910 | |
| 4911 | err = cgroup_attach_task_all(init_context->parent, current); |
| 4912 | if (err) { |
| 4913 | kvm_err("%s: cgroup_attach_task_all failed with err %d\n", |
| 4914 | __func__, err); |
| 4915 | goto init_complete; |
| 4916 | } |
| 4917 | |
| 4918 | set_user_nice(current, task_nice(init_context->parent)); |
| 4919 | |
| 4920 | init_complete: |
| 4921 | init_context->err = err; |
| 4922 | complete(&init_context->init_done); |
| 4923 | init_context = NULL; |
| 4924 | |
| 4925 | if (err) |
| 4926 | return err; |
| 4927 | |
| 4928 | /* Wait to be woken up by the spawner before proceeding. */ |
| 4929 | kthread_parkme(); |
| 4930 | |
| 4931 | if (!kthread_should_stop()) |
| 4932 | err = thread_fn(kvm, data); |
| 4933 | |
| 4934 | return err; |
| 4935 | } |
| 4936 | |
| 4937 | int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn, |
| 4938 | uintptr_t data, const char *name, |
| 4939 | struct task_struct **thread_ptr) |
| 4940 | { |
| 4941 | struct kvm_vm_worker_thread_context init_context = {}; |
| 4942 | struct task_struct *thread; |
| 4943 | |
| 4944 | *thread_ptr = NULL; |
| 4945 | init_context.kvm = kvm; |
| 4946 | init_context.parent = current; |
| 4947 | init_context.thread_fn = thread_fn; |
| 4948 | init_context.data = data; |
| 4949 | init_completion(&init_context.init_done); |
| 4950 | |
| 4951 | thread = kthread_run(kvm_vm_worker_thread, &init_context, |
| 4952 | "%s-%d", name, task_pid_nr(current)); |
| 4953 | if (IS_ERR(thread)) |
| 4954 | return PTR_ERR(thread); |
| 4955 | |
| 4956 | /* kthread_run is never supposed to return NULL */ |
| 4957 | WARN_ON(thread == NULL); |
| 4958 | |
| 4959 | wait_for_completion(&init_context.init_done); |
| 4960 | |
| 4961 | if (!init_context.err) |
| 4962 | *thread_ptr = thread; |
| 4963 | |
| 4964 | return init_context.err; |
| 4965 | } |