blob: 110aa5cc0c93495e32fa731098909d5a96e93764 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity6aa8b732006-12-10 02:21:36 -08002/*
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 Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Avi Kivity6aa8b732006-12-10 02:21:36 -080014 */
15
Andre Przywaraaf669ac2015-03-26 14:39:29 +000016#include <kvm/iodev.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080017
Avi Kivityedf88412007-12-16 11:02:48 +020018#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080019#include <linux/kvm.h>
20#include <linux/module.h>
21#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/percpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/mm.h>
24#include <linux/miscdevice.h>
25#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/debugfs.h>
28#include <linux/highmem.h>
29#include <linux/file.h>
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +010030#include <linux/syscore_ops.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080031#include <linux/cpu.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010032#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010033#include <linux/sched/mm.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010034#include <linux/sched/stat.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030035#include <linux/cpumask.h>
36#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040037#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030038#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050039#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020040#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050041#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050042#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080043#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030044#include <linux/spinlock.h>
Arnd Bergmann6ff58942009-10-22 14:19:27 +020045#include <linux/compat.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020046#include <linux/srcu.h>
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +010047#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Sasha Levin743eeb02011-07-27 16:00:48 +030049#include <linux/sort.h>
50#include <linux/bsearch.h>
Paolo Bonzinic011d232019-05-17 14:08:53 +020051#include <linux/io.h>
Wanpeng Li2eb06c32019-05-17 16:49:49 +080052#include <linux/lockdep.h>
Junaid Shahidc57c8042019-11-04 12:22:02 +010053#include <linux/kthread.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080054
Avi Kivitye4956062007-06-28 14:15:57 -040055#include <asm/processor.h>
David Matlack2ea75be2014-09-19 16:03:25 -070056#include <asm/ioctl.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080057#include <linux/uaccess.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080058
Laurent Vivier5f94c172008-05-30 16:05:54 +020059#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020060#include "async_pf.h"
Paolo Bonzini3c3c29f2014-09-24 13:02:46 +020061#include "vfio.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020062
Marcelo Tosatti229456f2009-06-17 09:22:14 -030063#define CREATE_TRACE_POINTS
64#include <trace/events/kvm.h>
65
Peter Xufb04a1e2020-09-30 21:22:22 -040066#include <linux/kvm_dirty_ring.h>
67
Janosch Frank536a6f82016-05-18 13:26:23 +020068/* Worst case buffer size needed for holding an integer. */
69#define ITOA_MAX_LEN 12
70
Avi Kivity6aa8b732006-12-10 02:21:36 -080071MODULE_AUTHOR("Qumranet");
72MODULE_LICENSE("GPL");
73
David Hildenbrand920552b2015-09-18 12:34:53 +020074/* Architectures should define their poll value according to the halt latency */
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110075unsigned int halt_poll_ns = KVM_HALT_POLL_NS_DEFAULT;
Roman Storozhenko039c5d12017-06-27 12:51:18 +030076module_param(halt_poll_ns, uint, 0644);
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110077EXPORT_SYMBOL_GPL(halt_poll_ns);
Paolo Bonzinif7819512015-02-04 18:20:58 +010078
Wanpeng Liaca6ff22015-09-03 22:07:38 +080079/* Default doubles per-vcpu halt_poll_ns. */
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110080unsigned int halt_poll_ns_grow = 2;
Roman Storozhenko039c5d12017-06-27 12:51:18 +030081module_param(halt_poll_ns_grow, uint, 0644);
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110082EXPORT_SYMBOL_GPL(halt_poll_ns_grow);
Wanpeng Liaca6ff22015-09-03 22:07:38 +080083
Nir Weiner49113d32019-01-27 12:17:15 +020084/* The start value to grow halt_poll_ns from */
85unsigned int halt_poll_ns_grow_start = 10000; /* 10us */
86module_param(halt_poll_ns_grow_start, uint, 0644);
87EXPORT_SYMBOL_GPL(halt_poll_ns_grow_start);
88
Wanpeng Liaca6ff22015-09-03 22:07:38 +080089/* Default resets per-vcpu halt_poll_ns . */
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110090unsigned int halt_poll_ns_shrink;
Roman Storozhenko039c5d12017-06-27 12:51:18 +030091module_param(halt_poll_ns_shrink, uint, 0644);
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110092EXPORT_SYMBOL_GPL(halt_poll_ns_shrink);
Wanpeng Liaca6ff22015-09-03 22:07:38 +080093
Marcelo Tosattifa40a822009-06-04 15:08:24 -030094/*
95 * Ordering of locks:
96 *
Xiubo Lib7d409d2015-02-26 14:58:24 +080097 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030098 */
99
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800100DEFINE_MUTEX(kvm_lock);
Paolo Bonzini4a937f92013-09-10 12:58:35 +0200101static DEFINE_RAW_SPINLOCK(kvm_count_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800102LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -0800103
Rusty Russell7f59f492008-12-07 21:25:45 +1030104static cpumask_var_t cpus_hardware_enabled;
Xiubo Lif4fee932015-02-26 14:58:21 +0800105static int kvm_usage_count;
Alexander Graf10474ae2009-09-15 11:37:46 +0200106static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +0300107
Sean Christophersonaaba2982019-12-18 13:55:16 -0800108static struct kmem_cache *kvm_vcpu_cache;
Avi Kivity1165f5f2007-04-19 17:27:43 +0300109
Avi Kivity15ad7142007-07-11 18:17:21 +0300110static __read_mostly struct preempt_ops kvm_preempt_ops;
Paolo Bonzini7495e222020-01-09 09:57:19 -0500111static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_running_vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300112
Hollis Blanchard76f7c872008-04-15 16:05:42 -0500113struct dentry *kvm_debugfs_dir;
Paul Mackerrase23a8082015-03-28 14:21:01 +1100114EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115
Janosch Frank536a6f82016-05-18 13:26:23 +0200116static int kvm_debugfs_num_entries;
Milan Pandurov09cbcef2019-12-13 14:07:21 +0100117static const struct file_operations stat_fops_per_vm;
Janosch Frank536a6f82016-05-18 13:26:23 +0200118
Avi Kivitybccf2152007-02-21 18:04:26 +0200119static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
120 unsigned long arg);
Christian Borntraegerde8e5d72015-02-03 09:35:15 +0100121#ifdef CONFIG_KVM_COMPAT
Alexander Graf1dda6062011-06-08 02:45:37 +0200122static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
123 unsigned long arg);
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +0100124#define KVM_COMPAT(c) .compat_ioctl = (c)
125#else
Marc Zyngier9cb09e72019-11-14 13:17:39 +0000126/*
127 * For architectures that don't implement a compat infrastructure,
128 * adopt a double line of defense:
129 * - Prevent a compat task from opening /dev/kvm
130 * - If the open has been done by a 64bit task, and the KVM fd
131 * passed to a compat task, let the ioctls fail.
132 */
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +0100133static long kvm_no_compat_ioctl(struct file *file, unsigned int ioctl,
134 unsigned long arg) { return -EINVAL; }
Marc Zyngierb9876e62019-11-13 16:05:23 +0000135
136static int kvm_no_compat_open(struct inode *inode, struct file *file)
137{
138 return is_compat_task() ? -ENODEV : 0;
139}
140#define KVM_COMPAT(c) .compat_ioctl = kvm_no_compat_ioctl, \
141 .open = kvm_no_compat_open
Alexander Graf1dda6062011-06-08 02:45:37 +0200142#endif
Alexander Graf10474ae2009-09-15 11:37:46 +0200143static int hardware_enable_all(void);
144static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +0200145
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200146static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
Stephen Hemminger79408762013-12-29 12:12:29 -0800147
Andi Kleen52480132014-02-08 08:51:57 +0100148__visible bool kvm_rebooting;
Avi Kivityb7c41452010-12-02 17:52:50 +0200149EXPORT_SYMBOL_GPL(kvm_rebooting);
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300150
Claudio Imbrenda286de8f2017-07-12 17:56:44 +0200151#define KVM_EVENT_CREATE_VM 0
152#define KVM_EVENT_DESTROY_VM 1
153static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm);
154static unsigned long long kvm_createvm_count;
155static unsigned long long kvm_active_vms;
156
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +0900157__weak void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
158 unsigned long start, unsigned long end)
Radim Krčmářb1394e72017-11-30 19:05:45 +0100159{
160}
161
Sean Christophersona78986a2019-11-11 14:12:27 -0800162bool kvm_is_zone_device_pfn(kvm_pfn_t pfn)
163{
164 /*
165 * The metadata used by is_zone_device_page() to determine whether or
166 * not a page is ZONE_DEVICE is guaranteed to be valid if and only if
167 * the device has been pinned, e.g. by get_user_pages(). WARN if the
168 * page_count() is zero to help detect bad usage of this helper.
169 */
170 if (!pfn_valid(pfn) || WARN_ON_ONCE(!page_count(pfn_to_page(pfn))))
171 return false;
172
173 return is_zone_device_page(pfn_to_page(pfn));
174}
175
Dan Williamsba049e92016-01-15 16:56:11 -0800176bool kvm_is_reserved_pfn(kvm_pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300177{
Sean Christophersona78986a2019-11-11 14:12:27 -0800178 /*
179 * ZONE_DEVICE pages currently set PG_reserved, but from a refcounting
180 * perspective they are "normal" pages, albeit with slightly different
181 * usage rules.
182 */
Andrea Arcangeli11feeb42013-07-25 03:04:38 +0200183 if (pfn_valid(pfn))
Sean Christophersona78986a2019-11-11 14:12:27 -0800184 return PageReserved(pfn_to_page(pfn)) &&
Zhuang Yanying7df003c2019-10-12 11:37:31 +0800185 !is_zero_pfn(pfn) &&
Sean Christophersona78986a2019-11-11 14:12:27 -0800186 !kvm_is_zone_device_pfn(pfn);
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300187
188 return true;
189}
190
Sean Christopherson005ba372020-01-08 12:24:36 -0800191bool kvm_is_transparent_hugepage(kvm_pfn_t pfn)
192{
193 struct page *page = pfn_to_page(pfn);
194
195 if (!PageTransCompoundMap(page))
196 return false;
197
198 return is_transparent_hugepage(compound_head(page));
199}
200
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201/*
202 * Switches to specified vcpu, until a matching vcpu_put()
203 */
Christoffer Dallec7660c2017-12-04 21:35:23 +0100204void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205{
Christoffer Dallec7660c2017-12-04 21:35:23 +0100206 int cpu = get_cpu();
Paolo Bonzini7495e222020-01-09 09:57:19 -0500207
208 __this_cpu_write(kvm_running_vcpu, vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300209 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200210 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300211 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200212}
Jim Mattson2f1fe812016-07-08 15:36:06 -0700213EXPORT_SYMBOL_GPL(vcpu_load);
Avi Kivitybccf2152007-02-21 18:04:26 +0200214
Carsten Otte313a3dc2007-10-11 19:16:52 +0200215void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216{
Avi Kivity15ad7142007-07-11 18:17:21 +0300217 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200218 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300219 preempt_notifier_unregister(&vcpu->preempt_notifier);
Paolo Bonzini7495e222020-01-09 09:57:19 -0500220 __this_cpu_write(kvm_running_vcpu, NULL);
Avi Kivity15ad7142007-07-11 18:17:21 +0300221 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222}
Jim Mattson2f1fe812016-07-08 15:36:06 -0700223EXPORT_SYMBOL_GPL(vcpu_put);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224
Paolo Bonzini7a97cec2017-04-27 14:33:43 +0200225/* TODO: merge with kvm_arch_vcpu_should_kick */
226static bool kvm_request_needs_ipi(struct kvm_vcpu *vcpu, unsigned req)
227{
228 int mode = kvm_vcpu_exiting_guest_mode(vcpu);
229
230 /*
231 * We need to wait for the VCPU to reenable interrupts and get out of
232 * READING_SHADOW_PAGE_TABLES mode.
233 */
234 if (req & KVM_REQUEST_WAIT)
235 return mode != OUTSIDE_GUEST_MODE;
236
237 /*
238 * Need to kick a running VCPU, but otherwise there is nothing to do.
239 */
240 return mode == IN_GUEST_MODE;
241}
242
Avi Kivityd9e368d2007-06-07 19:18:30 +0300243static void ack_flush(void *_completed)
244{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300245}
246
Paolo Bonzinib49defe2017-06-30 13:25:45 +0200247static inline bool kvm_kick_many_cpus(const struct cpumask *cpus, bool wait)
248{
249 if (unlikely(!cpus))
250 cpus = cpu_online_mask;
251
252 if (cpumask_empty(cpus))
253 return false;
254
255 smp_call_function_many(cpus, ack_flush, NULL, wait);
256 return true;
257}
258
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200259bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500260 struct kvm_vcpu *except,
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200261 unsigned long *vcpu_bitmap, cpumask_var_t tmp)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300262{
Avi Kivity597a5f52008-07-20 14:24:22 +0300263 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300264 struct kvm_vcpu *vcpu;
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200265 bool called;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030266
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800267 me = get_cpu();
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200268
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300269 kvm_for_each_vcpu(i, vcpu, kvm) {
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500270 if ((vcpu_bitmap && !test_bit(i, vcpu_bitmap)) ||
271 vcpu == except)
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200272 continue;
273
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800274 kvm_make_request(req, vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300275 cpu = vcpu->cpu;
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800276
Radim Krčmář178f02f2017-04-26 22:32:26 +0200277 if (!(req & KVM_REQUEST_NO_WAKEUP) && kvm_vcpu_wake_up(vcpu))
278 continue;
Radim Krčmář6c6e8362017-04-26 22:32:23 +0200279
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200280 if (tmp != NULL && cpu != -1 && cpu != me &&
Paolo Bonzini7a97cec2017-04-27 14:33:43 +0200281 kvm_request_needs_ipi(vcpu, req))
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200282 __cpumask_set_cpu(cpu, tmp);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300283 }
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200284
285 called = kvm_kick_many_cpus(tmp, !!(req & KVM_REQUEST_WAIT));
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800286 put_cpu();
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200287
288 return called;
289}
290
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500291bool kvm_make_all_cpus_request_except(struct kvm *kvm, unsigned int req,
292 struct kvm_vcpu *except)
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200293{
294 cpumask_var_t cpus;
295 bool called;
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200296
297 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
298
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500299 called = kvm_make_vcpus_request_mask(kvm, req, except, NULL, cpus);
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200300
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030301 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030302 return called;
303}
304
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500305bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req)
306{
307 return kvm_make_all_cpus_request_except(kvm, req, NULL);
308}
309
Mario Smarducha6d51012015-01-15 15:58:52 -0800310#ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL
Rusty Russell49846892008-12-08 20:26:24 +1030311void kvm_flush_remote_tlbs(struct kvm *kvm)
312{
Lan Tianyu4ae3cb32016-03-13 11:10:28 +0800313 /*
314 * Read tlbs_dirty before setting KVM_REQ_TLB_FLUSH in
315 * kvm_make_all_cpus_request.
316 */
317 long dirty_count = smp_load_acquire(&kvm->tlbs_dirty);
Xiao Guangronga086f6a2014-04-17 17:06:12 +0800318
Lan Tianyu4ae3cb32016-03-13 11:10:28 +0800319 /*
320 * We want to publish modifications to the page tables before reading
321 * mode. Pairs with a memory barrier in arch-specific code.
322 * - x86: smp_mb__after_srcu_read_unlock in vcpu_enter_guest
323 * and smp_mb in walk_shadow_page_lockless_begin/end.
324 * - powerpc: smp_mb in kvmppc_prepare_to_enter.
325 *
326 * There is already an smp_mb__after_atomic() before
327 * kvm_make_all_cpus_request() reads vcpu->mode. We reuse that
328 * barrier here.
329 */
Tianyu Lanb08660e2018-07-19 08:40:17 +0000330 if (!kvm_arch_flush_remote_tlb(kvm)
331 || kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
Rusty Russell49846892008-12-08 20:26:24 +1030332 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga086f6a2014-04-17 17:06:12 +0800333 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300334}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530335EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
Mario Smarducha6d51012015-01-15 15:58:52 -0800336#endif
Avi Kivityd9e368d2007-06-07 19:18:30 +0300337
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500338void kvm_reload_remote_mmus(struct kvm *kvm)
339{
Tang Chen445b8232014-09-24 15:57:55 +0800340 kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500341}
342
Sean Christopherson6926f952020-07-02 19:35:39 -0700343#ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE
344static inline void *mmu_memory_cache_alloc_obj(struct kvm_mmu_memory_cache *mc,
345 gfp_t gfp_flags)
346{
347 gfp_flags |= mc->gfp_zero;
348
349 if (mc->kmem_cache)
350 return kmem_cache_alloc(mc->kmem_cache, gfp_flags);
351 else
352 return (void *)__get_free_page(gfp_flags);
353}
354
355int kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int min)
356{
357 void *obj;
358
359 if (mc->nobjs >= min)
360 return 0;
361 while (mc->nobjs < ARRAY_SIZE(mc->objects)) {
362 obj = mmu_memory_cache_alloc_obj(mc, GFP_KERNEL_ACCOUNT);
363 if (!obj)
364 return mc->nobjs >= min ? 0 : -ENOMEM;
365 mc->objects[mc->nobjs++] = obj;
366 }
367 return 0;
368}
369
370int kvm_mmu_memory_cache_nr_free_objects(struct kvm_mmu_memory_cache *mc)
371{
372 return mc->nobjs;
373}
374
375void kvm_mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
376{
377 while (mc->nobjs) {
378 if (mc->kmem_cache)
379 kmem_cache_free(mc->kmem_cache, mc->objects[--mc->nobjs]);
380 else
381 free_page((unsigned long)mc->objects[--mc->nobjs]);
382 }
383}
384
385void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
386{
387 void *p;
388
389 if (WARN_ON(!mc->nobjs))
390 p = mmu_memory_cache_alloc_obj(mc, GFP_ATOMIC | __GFP_ACCOUNT);
391 else
392 p = mc->objects[--mc->nobjs];
393 BUG_ON(!p);
394 return p;
395}
396#endif
397
Sean Christopherson8bd826d2019-12-18 13:55:30 -0800398static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000399{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000400 mutex_init(&vcpu->mutex);
401 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000402 vcpu->kvm = kvm;
403 vcpu->vcpu_id = id;
Rik van Riel34bb10b2011-02-01 09:52:41 -0500404 vcpu->pid = NULL;
Davidlohr Buesoda4ad882020-04-23 22:48:37 -0700405 rcuwait_init(&vcpu->wait);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200406 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000407
Feng Wubf9f6ac2015-09-18 22:29:55 +0800408 vcpu->pre_pcpu = -1;
409 INIT_LIST_HEAD(&vcpu->blocked_vcpu_list);
410
Raghavendra K T4c088492012-07-18 19:07:46 +0530411 kvm_vcpu_set_in_spin_loop(vcpu, false);
412 kvm_vcpu_set_dy_eligible(vcpu, false);
Raghavendra K T3a08a8f2013-03-04 23:32:07 +0530413 vcpu->preempted = false;
Wanpeng Lid73eb572019-07-18 19:39:06 +0800414 vcpu->ready = false;
Sean Christophersond5c48de2019-12-18 13:55:17 -0800415 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000416}
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000417
Sean Christopherson4543bdc2019-12-18 13:55:14 -0800418void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
419{
Peter Xufb04a1e2020-09-30 21:22:22 -0400420 kvm_dirty_ring_free(&vcpu->dirty_ring);
Sean Christopherson4543bdc2019-12-18 13:55:14 -0800421 kvm_arch_vcpu_destroy(vcpu);
Sean Christophersone529ef62019-12-18 13:55:15 -0800422
Sean Christopherson9941d222019-12-18 13:55:29 -0800423 /*
424 * No need for rcu_read_lock as VCPU_RUN is the only place that changes
425 * the vcpu->pid pointer, and at destruction time all file descriptors
426 * are already gone.
427 */
428 put_pid(rcu_dereference_protected(vcpu->pid, 1));
429
Sean Christopherson8bd826d2019-12-18 13:55:30 -0800430 free_page((unsigned long)vcpu->run);
Sean Christophersone529ef62019-12-18 13:55:15 -0800431 kmem_cache_free(kvm_vcpu_cache, vcpu);
Sean Christopherson4543bdc2019-12-18 13:55:14 -0800432}
433EXPORT_SYMBOL_GPL(kvm_vcpu_destroy);
434
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200435#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
436static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
437{
438 return container_of(mn, struct kvm, mmu_notifier);
439}
440
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +0900441static void kvm_mmu_notifier_invalidate_range(struct mmu_notifier *mn,
442 struct mm_struct *mm,
443 unsigned long start, unsigned long end)
444{
445 struct kvm *kvm = mmu_notifier_to_kvm(mn);
446 int idx;
447
448 idx = srcu_read_lock(&kvm->srcu);
449 kvm_arch_mmu_notifier_invalidate_range(kvm, start, end);
450 srcu_read_unlock(&kvm->srcu, idx);
451}
452
Izik Eidus3da0dd42009-09-23 21:47:18 +0300453static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
454 struct mm_struct *mm,
455 unsigned long address,
456 pte_t pte)
457{
458 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200459 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300460
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200461 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300462 spin_lock(&kvm->mmu_lock);
463 kvm->mmu_notifier_seq++;
Lan Tianyu0cf853c2018-12-06 21:21:11 +0800464
465 if (kvm_set_spte_hva(kvm, address, pte))
466 kvm_flush_remote_tlbs(kvm);
467
Izik Eidus3da0dd42009-09-23 21:47:18 +0300468 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200469 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300470}
471
Michal Hocko93065ac2018-08-21 21:52:33 -0700472static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800473 const struct mmu_notifier_range *range)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200474{
475 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200476 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200477
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200478 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200479 spin_lock(&kvm->mmu_lock);
480 /*
481 * The count increase must become visible at unlock time as no
482 * spte can be established without taking the mmu_lock and
483 * count is also read inside the mmu_lock critical section.
484 */
485 kvm->mmu_notifier_count++;
Will Deaconfdfe7cb2020-08-11 11:27:24 +0100486 need_tlb_flush = kvm_unmap_hva_range(kvm, range->start, range->end,
487 range->flags);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800488 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200489 /* we've to flush the tlb before the pages can be freed */
490 if (need_tlb_flush)
491 kvm_flush_remote_tlbs(kvm);
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900492
493 spin_unlock(&kvm->mmu_lock);
494 srcu_read_unlock(&kvm->srcu, idx);
Michal Hocko93065ac2018-08-21 21:52:33 -0700495
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +0900496 return 0;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200497}
498
499static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800500 const struct mmu_notifier_range *range)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200501{
502 struct kvm *kvm = mmu_notifier_to_kvm(mn);
503
504 spin_lock(&kvm->mmu_lock);
505 /*
506 * This sequence increase will notify the kvm page fault that
507 * the page that is going to be mapped in the spte could have
508 * been freed.
509 */
510 kvm->mmu_notifier_seq++;
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000511 smp_wmb();
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200512 /*
513 * The above sequence increase must be visible before the
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000514 * below count decrease, which is ensured by the smp_wmb above
515 * in conjunction with the smp_rmb in mmu_notifier_retry().
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200516 */
517 kvm->mmu_notifier_count--;
518 spin_unlock(&kvm->mmu_lock);
519
520 BUG_ON(kvm->mmu_notifier_count < 0);
521}
522
523static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
524 struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700525 unsigned long start,
526 unsigned long end)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200527{
528 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200529 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200530
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200531 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200532 spin_lock(&kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200533
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700534 young = kvm_age_hva(kvm, start, end);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200535 if (young)
536 kvm_flush_remote_tlbs(kvm);
537
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900538 spin_unlock(&kvm->mmu_lock);
539 srcu_read_unlock(&kvm->srcu, idx);
540
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200541 return young;
542}
543
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700544static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,
545 struct mm_struct *mm,
546 unsigned long start,
547 unsigned long end)
548{
549 struct kvm *kvm = mmu_notifier_to_kvm(mn);
550 int young, idx;
551
552 idx = srcu_read_lock(&kvm->srcu);
553 spin_lock(&kvm->mmu_lock);
554 /*
555 * Even though we do not flush TLB, this will still adversely
556 * affect performance on pre-Haswell Intel EPT, where there is
557 * no EPT Access Bit to clear so that we have to tear down EPT
558 * tables instead. If we find this unacceptable, we can always
559 * add a parameter to kvm_age_hva so that it effectively doesn't
560 * do anything on clear_young.
561 *
562 * Also note that currently we never issue secondary TLB flushes
563 * from clear_young, leaving this job up to the regular system
564 * cadence. If we find this inaccurate, we might come up with a
565 * more sophisticated heuristic later.
566 */
567 young = kvm_age_hva(kvm, start, end);
568 spin_unlock(&kvm->mmu_lock);
569 srcu_read_unlock(&kvm->srcu, idx);
570
571 return young;
572}
573
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800574static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
575 struct mm_struct *mm,
576 unsigned long address)
577{
578 struct kvm *kvm = mmu_notifier_to_kvm(mn);
579 int young, idx;
580
581 idx = srcu_read_lock(&kvm->srcu);
582 spin_lock(&kvm->mmu_lock);
583 young = kvm_test_age_hva(kvm, address);
584 spin_unlock(&kvm->mmu_lock);
585 srcu_read_unlock(&kvm->srcu, idx);
586
587 return young;
588}
589
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100590static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
591 struct mm_struct *mm)
592{
593 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800594 int idx;
595
596 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300597 kvm_arch_flush_shadow_all(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800598 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100599}
600
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200601static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +0900602 .invalidate_range = kvm_mmu_notifier_invalidate_range,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200603 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
604 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
605 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700606 .clear_young = kvm_mmu_notifier_clear_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800607 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300608 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100609 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200610};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200611
612static int kvm_init_mmu_notifier(struct kvm *kvm)
613{
614 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
615 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
616}
617
618#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
619
620static int kvm_init_mmu_notifier(struct kvm *kvm)
621{
622 return 0;
623}
624
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200625#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
626
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200627static struct kvm_memslots *kvm_alloc_memslots(void)
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800628{
629 int i;
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200630 struct kvm_memslots *slots;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800631
Ben Gardonb12ce362019-02-11 11:02:49 -0800632 slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL_ACCOUNT);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200633 if (!slots)
634 return NULL;
635
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800636 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
Sean Christopherson36947252020-02-18 13:07:32 -0800637 slots->id_to_index[i] = -1;
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200638
639 return slots;
640}
641
642static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
643{
644 if (!memslot->dirty_bitmap)
645 return;
646
647 kvfree(memslot->dirty_bitmap);
648 memslot->dirty_bitmap = NULL;
649}
650
Sean Christophersone96c81e2020-02-18 13:07:27 -0800651static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200652{
Sean Christophersone96c81e2020-02-18 13:07:27 -0800653 kvm_destroy_dirty_bitmap(slot);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200654
Sean Christophersone96c81e2020-02-18 13:07:27 -0800655 kvm_arch_free_memslot(kvm, slot);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200656
Sean Christophersone96c81e2020-02-18 13:07:27 -0800657 slot->flags = 0;
658 slot->npages = 0;
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200659}
660
661static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots)
662{
663 struct kvm_memory_slot *memslot;
664
665 if (!slots)
666 return;
667
668 kvm_for_each_memslot(memslot, slots)
Sean Christophersone96c81e2020-02-18 13:07:27 -0800669 kvm_free_memslot(kvm, memslot);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200670
671 kvfree(slots);
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800672}
673
Janosch Frank536a6f82016-05-18 13:26:23 +0200674static void kvm_destroy_vm_debugfs(struct kvm *kvm)
675{
676 int i;
677
678 if (!kvm->debugfs_dentry)
679 return;
680
681 debugfs_remove_recursive(kvm->debugfs_dentry);
682
Luiz Capitulino9d5a1dc2016-09-07 14:47:21 -0400683 if (kvm->debugfs_stat_data) {
684 for (i = 0; i < kvm_debugfs_num_entries; i++)
685 kfree(kvm->debugfs_stat_data[i]);
686 kfree(kvm->debugfs_stat_data);
687 }
Janosch Frank536a6f82016-05-18 13:26:23 +0200688}
689
690static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
691{
692 char dir_name[ITOA_MAX_LEN * 2];
693 struct kvm_stat_data *stat_data;
694 struct kvm_stats_debugfs_item *p;
695
696 if (!debugfs_initialized())
697 return 0;
698
699 snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd);
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +0200700 kvm->debugfs_dentry = debugfs_create_dir(dir_name, kvm_debugfs_dir);
Janosch Frank536a6f82016-05-18 13:26:23 +0200701
702 kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries,
703 sizeof(*kvm->debugfs_stat_data),
Ben Gardonb12ce362019-02-11 11:02:49 -0800704 GFP_KERNEL_ACCOUNT);
Janosch Frank536a6f82016-05-18 13:26:23 +0200705 if (!kvm->debugfs_stat_data)
706 return -ENOMEM;
707
708 for (p = debugfs_entries; p->name; p++) {
Ben Gardonb12ce362019-02-11 11:02:49 -0800709 stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
Janosch Frank536a6f82016-05-18 13:26:23 +0200710 if (!stat_data)
711 return -ENOMEM;
712
713 stat_data->kvm = kvm;
Milan Pandurov09cbcef2019-12-13 14:07:21 +0100714 stat_data->dbgfs_item = p;
Janosch Frank536a6f82016-05-18 13:26:23 +0200715 kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
Milan Pandurov09cbcef2019-12-13 14:07:21 +0100716 debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p),
717 kvm->debugfs_dentry, stat_data,
718 &stat_fops_per_vm);
Janosch Frank536a6f82016-05-18 13:26:23 +0200719 }
720 return 0;
721}
722
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100723/*
724 * Called after the VM is otherwise initialized, but just before adding it to
725 * the vm_list.
726 */
727int __weak kvm_arch_post_init_vm(struct kvm *kvm)
728{
729 return 0;
730}
731
732/*
733 * Called just after removing the VM from the vm_list, but before doing any
734 * other destruction.
735 */
736void __weak kvm_arch_pre_destroy_vm(struct kvm *kvm)
737{
738}
739
Carsten Ottee08b9632012-01-04 10:25:20 +0100740static struct kvm *kvm_create_vm(unsigned long type)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100742 struct kvm *kvm = kvm_arch_alloc_vm();
Jim Mattson91219232019-10-24 16:03:26 -0700743 int r = -ENOMEM;
744 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100746 if (!kvm)
747 return ERR_PTR(-ENOMEM);
748
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100749 spin_lock_init(&kvm->mmu_lock);
Vegard Nossumf1f10072017-02-27 14:30:07 -0800750 mmgrab(current->mm);
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100751 kvm->mm = current->mm;
752 kvm_eventfd_init(kvm);
753 mutex_init(&kvm->lock);
754 mutex_init(&kvm->irq_lock);
755 mutex_init(&kvm->slots_lock);
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100756 INIT_LIST_HEAD(&kvm->devices);
757
Jim Mattson91219232019-10-24 16:03:26 -0700758 BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
759
Paolo Bonzini8a441192019-11-04 12:16:49 +0100760 if (init_srcu_struct(&kvm->srcu))
761 goto out_err_no_srcu;
762 if (init_srcu_struct(&kvm->irq_srcu))
763 goto out_err_no_irq_srcu;
764
Paolo Bonzinie2d3fca2019-11-04 13:23:53 +0100765 refcount_set(&kvm->users_count, 1);
Jim Mattson91219232019-10-24 16:03:26 -0700766 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
767 struct kvm_memslots *slots = kvm_alloc_memslots();
768
769 if (!slots)
Jim Mattsona97b0e72019-10-25 13:34:58 +0200770 goto out_err_no_arch_destroy_vm;
Jim Mattson91219232019-10-24 16:03:26 -0700771 /* Generations must be different for each address space. */
772 slots->generation = i;
773 rcu_assign_pointer(kvm->memslots[i], slots);
774 }
775
776 for (i = 0; i < KVM_NR_BUSES; i++) {
777 rcu_assign_pointer(kvm->buses[i],
778 kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT));
779 if (!kvm->buses[i])
Jim Mattsona97b0e72019-10-25 13:34:58 +0200780 goto out_err_no_arch_destroy_vm;
Jim Mattson91219232019-10-24 16:03:26 -0700781 }
782
David Matlackacd05782020-04-17 15:14:46 -0700783 kvm->max_halt_poll_ns = halt_poll_ns;
784
Carsten Ottee08b9632012-01-04 10:25:20 +0100785 r = kvm_arch_init_vm(kvm, type);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100786 if (r)
Jim Mattsona97b0e72019-10-25 13:34:58 +0200787 goto out_err_no_arch_destroy_vm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200788
789 r = hardware_enable_all();
790 if (r)
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100791 goto out_err_no_disable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200792
Paolo Bonzinic77dcac2014-08-06 14:24:45 +0200793#ifdef CONFIG_HAVE_KVM_IRQFD
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300794 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200795#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700797 r = kvm_init_mmu_notifier(kvm);
798 if (r)
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100799 goto out_err_no_mmu_notifier;
800
801 r = kvm_arch_post_init_vm(kvm);
802 if (r)
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700803 goto out_err;
804
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800805 mutex_lock(&kvm_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000806 list_add(&kvm->vm_list, &vm_list);
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800807 mutex_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100808
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +0200809 preempt_notifier_inc();
810
Avi Kivityf17abe92007-02-21 19:28:04 +0200811 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200812
813out_err:
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100814#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
815 if (kvm->mmu_notifier.ops)
816 mmu_notifier_unregister(&kvm->mmu_notifier, current->mm);
817#endif
818out_err_no_mmu_notifier:
Alexander Graf10474ae2009-09-15 11:37:46 +0200819 hardware_disable_all();
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100820out_err_no_disable:
Jim Mattsona97b0e72019-10-25 13:34:58 +0200821 kvm_arch_destroy_vm(kvm);
Jim Mattsona97b0e72019-10-25 13:34:58 +0200822out_err_no_arch_destroy_vm:
Paolo Bonzinie2d3fca2019-11-04 13:23:53 +0100823 WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200824 for (i = 0; i < KVM_NR_BUSES; i++)
Paolo Bonzini3898da92017-08-02 17:55:54 +0200825 kfree(kvm_get_bus(kvm, i));
Paolo Bonzinif481b062015-05-17 17:30:37 +0200826 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
Paolo Bonzini3898da92017-08-02 17:55:54 +0200827 kvm_free_memslots(kvm, __kvm_memslots(kvm, i));
Paolo Bonzini8a441192019-11-04 12:16:49 +0100828 cleanup_srcu_struct(&kvm->irq_srcu);
829out_err_no_irq_srcu:
830 cleanup_srcu_struct(&kvm->srcu);
831out_err_no_srcu:
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100832 kvm_arch_free_vm(kvm);
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100833 mmdrop(current->mm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200834 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200835}
836
Scott Wood07f0a7b2013-04-25 14:11:23 +0000837static void kvm_destroy_devices(struct kvm *kvm)
838{
Geliang Tange6e3b5a2016-01-01 19:47:12 +0800839 struct kvm_device *dev, *tmp;
Scott Wood07f0a7b2013-04-25 14:11:23 +0000840
Christoffer Dalla28ebea2016-08-09 19:13:01 +0200841 /*
842 * We do not need to take the kvm->lock here, because nobody else
843 * has a reference to the struct kvm at this point and therefore
844 * cannot access the devices list anyhow.
845 */
Geliang Tange6e3b5a2016-01-01 19:47:12 +0800846 list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) {
847 list_del(&dev->vm_node);
Scott Wood07f0a7b2013-04-25 14:11:23 +0000848 dev->ops->destroy(dev);
849 }
850}
851
Avi Kivityf17abe92007-02-21 19:28:04 +0200852static void kvm_destroy_vm(struct kvm *kvm)
853{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200854 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200855 struct mm_struct *mm = kvm->mm;
856
Claudio Imbrenda286de8f2017-07-12 17:56:44 +0200857 kvm_uevent_notify_change(KVM_EVENT_DESTROY_VM, kvm);
Janosch Frank536a6f82016-05-18 13:26:23 +0200858 kvm_destroy_vm_debugfs(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800859 kvm_arch_sync_events(kvm);
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800860 mutex_lock(&kvm_lock);
Avi Kivity133de902007-02-12 00:54:44 -0800861 list_del(&kvm->vm_list);
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800862 mutex_unlock(&kvm_lock);
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100863 kvm_arch_pre_destroy_vm(kvm);
864
Avi Kivity399ec802008-11-19 13:58:46 +0200865 kvm_free_irq_routing(kvm);
Peter Xudf630b82017-03-15 16:01:17 +0800866 for (i = 0; i < KVM_NR_BUSES; i++) {
Paolo Bonzini3898da92017-08-02 17:55:54 +0200867 struct kvm_io_bus *bus = kvm_get_bus(kvm, i);
Christian Borntraeger4a12f952017-07-07 10:51:38 +0200868
Christian Borntraeger4a12f952017-07-07 10:51:38 +0200869 if (bus)
870 kvm_io_bus_destroy(bus);
Peter Xudf630b82017-03-15 16:01:17 +0800871 kvm->buses[i] = NULL;
872 }
Avi Kivity980da6c2009-12-20 15:13:43 +0200873 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200874#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
875 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200876#else
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300877 kvm_arch_flush_shadow_all(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200878#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800879 kvm_arch_destroy_vm(kvm);
Scott Wood07f0a7b2013-04-25 14:11:23 +0000880 kvm_destroy_devices(kvm);
Paolo Bonzinif481b062015-05-17 17:30:37 +0200881 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
Paolo Bonzini3898da92017-08-02 17:55:54 +0200882 kvm_free_memslots(kvm, __kvm_memslots(kvm, i));
Paolo Bonzini820b3fc2014-06-03 13:44:17 +0200883 cleanup_srcu_struct(&kvm->irq_srcu);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100884 cleanup_srcu_struct(&kvm->srcu);
885 kvm_arch_free_vm(kvm);
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +0200886 preempt_notifier_dec();
Alexander Graf10474ae2009-09-15 11:37:46 +0200887 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200888 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200889}
890
Izik Eidusd39f13b2008-03-30 16:01:25 +0300891void kvm_get_kvm(struct kvm *kvm)
892{
Elena Reshetovae3736c32017-02-20 13:06:21 +0200893 refcount_inc(&kvm->users_count);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300894}
895EXPORT_SYMBOL_GPL(kvm_get_kvm);
896
897void kvm_put_kvm(struct kvm *kvm)
898{
Elena Reshetovae3736c32017-02-20 13:06:21 +0200899 if (refcount_dec_and_test(&kvm->users_count))
Izik Eidusd39f13b2008-03-30 16:01:25 +0300900 kvm_destroy_vm(kvm);
901}
902EXPORT_SYMBOL_GPL(kvm_put_kvm);
903
Sean Christopherson149487b2019-10-21 15:58:42 -0700904/*
905 * Used to put a reference that was taken on behalf of an object associated
906 * with a user-visible file descriptor, e.g. a vcpu or device, if installation
907 * of the new file descriptor fails and the reference cannot be transferred to
908 * its final owner. In such cases, the caller is still actively using @kvm and
909 * will fail miserably if the refcount unexpectedly hits zero.
910 */
911void kvm_put_kvm_no_destroy(struct kvm *kvm)
912{
913 WARN_ON(refcount_dec_and_test(&kvm->users_count));
914}
915EXPORT_SYMBOL_GPL(kvm_put_kvm_no_destroy);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300916
Avi Kivityf17abe92007-02-21 19:28:04 +0200917static int kvm_vm_release(struct inode *inode, struct file *filp)
918{
919 struct kvm *kvm = filp->private_data;
920
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400921 kvm_irqfd_release(kvm);
922
Izik Eidusd39f13b2008-03-30 16:01:25 +0300923 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 return 0;
925}
926
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900927/*
928 * Allocation size is twice as large as the actual dirty bitmap size.
Sean Christopherson0dff0842020-02-18 13:07:29 -0800929 * See kvm_vm_ioctl_get_dirty_log() why this is needed.
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900930 */
Jay Zhou3c9bd402020-02-27 09:32:27 +0800931static int kvm_alloc_dirty_bitmap(struct kvm_memory_slot *memslot)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900932{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900933 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900934
Ben Gardonb12ce362019-02-11 11:02:49 -0800935 memslot->dirty_bitmap = kvzalloc(dirty_bytes, GFP_KERNEL_ACCOUNT);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900936 if (!memslot->dirty_bitmap)
937 return -ENOMEM;
938
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900939 return 0;
940}
941
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800942/*
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800943 * Delete a memslot by decrementing the number of used slots and shifting all
944 * other entries in the array forward one spot.
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800945 */
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800946static inline void kvm_memslot_delete(struct kvm_memslots *slots,
947 struct kvm_memory_slot *memslot)
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800948{
Igor Mammedov063584d2014-11-13 23:00:13 +0000949 struct kvm_memory_slot *mslots = slots->memslots;
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800950 int i;
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800951
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800952 if (WARN_ON(slots->id_to_index[memslot->id] == -1))
953 return;
Igor Mammedov0e60b072014-12-01 17:29:26 +0000954
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800955 slots->used_slots--;
956
Sean Christopherson0774a962020-03-20 13:55:40 -0700957 if (atomic_read(&slots->lru_slot) >= slots->used_slots)
958 atomic_set(&slots->lru_slot, 0);
959
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800960 for (i = slots->id_to_index[memslot->id]; i < slots->used_slots; i++) {
Igor Mammedov7f379cf2014-12-01 17:29:24 +0000961 mslots[i] = mslots[i + 1];
962 slots->id_to_index[mslots[i].id] = i;
Igor Mammedov7f379cf2014-12-01 17:29:24 +0000963 }
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800964 mslots[i] = *memslot;
965 slots->id_to_index[memslot->id] = -1;
966}
967
968/*
969 * "Insert" a new memslot by incrementing the number of used slots. Returns
970 * the new slot's initial index into the memslots array.
971 */
972static inline int kvm_memslot_insert_back(struct kvm_memslots *slots)
973{
974 return slots->used_slots++;
975}
976
977/*
978 * Move a changed memslot backwards in the array by shifting existing slots
979 * with a higher GFN toward the front of the array. Note, the changed memslot
980 * itself is not preserved in the array, i.e. not swapped at this time, only
981 * its new index into the array is tracked. Returns the changed memslot's
982 * current index into the memslots array.
983 */
984static inline int kvm_memslot_move_backward(struct kvm_memslots *slots,
985 struct kvm_memory_slot *memslot)
986{
987 struct kvm_memory_slot *mslots = slots->memslots;
988 int i;
989
990 if (WARN_ON_ONCE(slots->id_to_index[memslot->id] == -1) ||
991 WARN_ON_ONCE(!slots->used_slots))
992 return -1;
Paolo Bonziniefbeec72014-12-27 18:01:00 +0100993
994 /*
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800995 * Move the target memslot backward in the array by shifting existing
996 * memslots with a higher GFN (than the target memslot) towards the
997 * front of the array.
Paolo Bonziniefbeec72014-12-27 18:01:00 +0100998 */
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800999 for (i = slots->id_to_index[memslot->id]; i < slots->used_slots - 1; i++) {
1000 if (memslot->base_gfn > mslots[i + 1].base_gfn)
1001 break;
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +08001002
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001003 WARN_ON_ONCE(memslot->base_gfn == mslots[i + 1].base_gfn);
1004
1005 /* Shift the next memslot forward one and update its index. */
1006 mslots[i] = mslots[i + 1];
1007 slots->id_to_index[mslots[i].id] = i;
1008 }
1009 return i;
1010}
1011
1012/*
1013 * Move a changed memslot forwards in the array by shifting existing slots with
1014 * a lower GFN toward the back of the array. Note, the changed memslot itself
1015 * is not preserved in the array, i.e. not swapped at this time, only its new
1016 * index into the array is tracked. Returns the changed memslot's final index
1017 * into the memslots array.
1018 */
1019static inline int kvm_memslot_move_forward(struct kvm_memslots *slots,
1020 struct kvm_memory_slot *memslot,
1021 int start)
1022{
1023 struct kvm_memory_slot *mslots = slots->memslots;
1024 int i;
1025
1026 for (i = start; i > 0; i--) {
1027 if (memslot->base_gfn < mslots[i - 1].base_gfn)
1028 break;
1029
1030 WARN_ON_ONCE(memslot->base_gfn == mslots[i - 1].base_gfn);
1031
1032 /* Shift the next memslot back one and update its index. */
1033 mslots[i] = mslots[i - 1];
1034 slots->id_to_index[mslots[i].id] = i;
1035 }
1036 return i;
1037}
1038
1039/*
1040 * Re-sort memslots based on their GFN to account for an added, deleted, or
1041 * moved memslot. Sorting memslots by GFN allows using a binary search during
1042 * memslot lookup.
1043 *
1044 * IMPORTANT: Slots are sorted from highest GFN to lowest GFN! I.e. the entry
1045 * at memslots[0] has the highest GFN.
1046 *
1047 * The sorting algorithm takes advantage of having initially sorted memslots
1048 * and knowing the position of the changed memslot. Sorting is also optimized
1049 * by not swapping the updated memslot and instead only shifting other memslots
1050 * and tracking the new index for the update memslot. Only once its final
1051 * index is known is the updated memslot copied into its position in the array.
1052 *
1053 * - When deleting a memslot, the deleted memslot simply needs to be moved to
1054 * the end of the array.
1055 *
1056 * - When creating a memslot, the algorithm "inserts" the new memslot at the
1057 * end of the array and then it forward to its correct location.
1058 *
1059 * - When moving a memslot, the algorithm first moves the updated memslot
1060 * backward to handle the scenario where the memslot's GFN was changed to a
1061 * lower value. update_memslots() then falls through and runs the same flow
1062 * as creating a memslot to move the memslot forward to handle the scenario
1063 * where its GFN was changed to a higher value.
1064 *
1065 * Note, slots are sorted from highest->lowest instead of lowest->highest for
1066 * historical reasons. Originally, invalid memslots where denoted by having
1067 * GFN=0, thus sorting from highest->lowest naturally sorted invalid memslots
1068 * to the end of the array. The current algorithm uses dedicated logic to
1069 * delete a memslot and thus does not rely on invalid memslots having GFN=0.
1070 *
1071 * The other historical motiviation for highest->lowest was to improve the
1072 * performance of memslot lookup. KVM originally used a linear search starting
1073 * at memslots[0]. On x86, the largest memslot usually has one of the highest,
1074 * if not *the* highest, GFN, as the bulk of the guest's RAM is located in a
1075 * single memslot above the 4gb boundary. As the largest memslot is also the
1076 * most likely to be referenced, sorting it to the front of the array was
1077 * advantageous. The current binary search starts from the middle of the array
1078 * and uses an LRU pointer to improve performance for all memslots and GFNs.
1079 */
1080static void update_memslots(struct kvm_memslots *slots,
1081 struct kvm_memory_slot *memslot,
1082 enum kvm_mr_change change)
1083{
1084 int i;
1085
1086 if (change == KVM_MR_DELETE) {
1087 kvm_memslot_delete(slots, memslot);
1088 } else {
1089 if (change == KVM_MR_CREATE)
1090 i = kvm_memslot_insert_back(slots);
1091 else
1092 i = kvm_memslot_move_backward(slots, memslot);
1093 i = kvm_memslot_move_forward(slots, memslot, i);
1094
1095 /*
1096 * Copy the memslot to its new position in memslots and update
1097 * its index accordingly.
1098 */
1099 slots->memslots[i] = *memslot;
1100 slots->id_to_index[memslot->id] = i;
1101 }
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +08001102}
1103
Paolo Bonzini09170a42015-05-18 13:59:39 +02001104static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem)
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001105{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001106 u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
1107
Christoffer Dall0f8a4de2014-08-26 14:00:37 +02001108#ifdef __KVM_HAVE_READONLY_MEM
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001109 valid_flags |= KVM_MEM_READONLY;
1110#endif
1111
1112 if (mem->flags & ~valid_flags)
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001113 return -EINVAL;
1114
1115 return 0;
1116}
1117
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001118static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
Paolo Bonzinif481b062015-05-17 17:30:37 +02001119 int as_id, struct kvm_memslots *slots)
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001120{
Paolo Bonzinif481b062015-05-17 17:30:37 +02001121 struct kvm_memslots *old_memslots = __kvm_memslots(kvm, as_id);
Sean Christopherson361209e2019-02-05 13:01:14 -08001122 u64 gen = old_memslots->generation;
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001123
Sean Christopherson361209e2019-02-05 13:01:14 -08001124 WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS);
1125 slots->generation = gen | KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS;
David Matlackee3d1572014-08-18 15:46:06 -07001126
Paolo Bonzinif481b062015-05-17 17:30:37 +02001127 rcu_assign_pointer(kvm->memslots[as_id], slots);
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001128 synchronize_srcu_expedited(&kvm->srcu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09001129
David Matlackee3d1572014-08-18 15:46:06 -07001130 /*
Sean Christopherson361209e2019-02-05 13:01:14 -08001131 * Increment the new memslot generation a second time, dropping the
Miaohe Lin00116792019-12-11 14:26:23 +08001132 * update in-progress flag and incrementing the generation based on
Sean Christopherson361209e2019-02-05 13:01:14 -08001133 * the number of address spaces. This provides a unique and easily
1134 * identifiable generation number while the memslots are in flux.
1135 */
1136 gen = slots->generation & ~KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS;
1137
1138 /*
Paolo Bonzini4bd518f2017-02-03 20:44:51 -08001139 * Generations must be unique even across address spaces. We do not need
1140 * a global counter for that, instead the generation space is evenly split
1141 * across address spaces. For example, with two address spaces, address
Sean Christopherson164bf7e2019-02-05 13:01:18 -08001142 * space 0 will use generations 0, 2, 4, ... while address space 1 will
1143 * use generations 1, 3, 5, ...
David Matlackee3d1572014-08-18 15:46:06 -07001144 */
Sean Christopherson164bf7e2019-02-05 13:01:18 -08001145 gen += KVM_ADDRESS_SPACE_NUM;
David Matlackee3d1572014-08-18 15:46:06 -07001146
Sean Christopherson15248252019-02-05 12:54:17 -08001147 kvm_arch_memslots_updated(kvm, gen);
1148
1149 slots->generation = gen;
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09001150
1151 return old_memslots;
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001152}
1153
Sean Christopherson36947252020-02-18 13:07:32 -08001154/*
1155 * Note, at a minimum, the current number of used slots must be allocated, even
1156 * when deleting a memslot, as we need a complete duplicate of the memslots for
1157 * use when invalidating a memslot prior to deleting/moving the memslot.
1158 */
1159static struct kvm_memslots *kvm_dup_memslots(struct kvm_memslots *old,
1160 enum kvm_mr_change change)
1161{
1162 struct kvm_memslots *slots;
1163 size_t old_size, new_size;
1164
1165 old_size = sizeof(struct kvm_memslots) +
1166 (sizeof(struct kvm_memory_slot) * old->used_slots);
1167
1168 if (change == KVM_MR_CREATE)
1169 new_size = old_size + sizeof(struct kvm_memory_slot);
1170 else
1171 new_size = old_size;
1172
1173 slots = kvzalloc(new_size, GFP_KERNEL_ACCOUNT);
1174 if (likely(slots))
1175 memcpy(slots, old, old_size);
1176
1177 return slots;
1178}
1179
Sean Christophersoncf47f502020-02-18 13:07:23 -08001180static int kvm_set_memslot(struct kvm *kvm,
1181 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -08001182 struct kvm_memory_slot *old,
Sean Christophersoncf47f502020-02-18 13:07:23 -08001183 struct kvm_memory_slot *new, int as_id,
1184 enum kvm_mr_change change)
1185{
1186 struct kvm_memory_slot *slot;
1187 struct kvm_memslots *slots;
1188 int r;
1189
Sean Christopherson36947252020-02-18 13:07:32 -08001190 slots = kvm_dup_memslots(__kvm_memslots(kvm, as_id), change);
Sean Christophersoncf47f502020-02-18 13:07:23 -08001191 if (!slots)
1192 return -ENOMEM;
Sean Christophersoncf47f502020-02-18 13:07:23 -08001193
1194 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
1195 /*
1196 * Note, the INVALID flag needs to be in the appropriate entry
1197 * in the freshly allocated memslots, not in @old or @new.
1198 */
1199 slot = id_to_memslot(slots, old->id);
1200 slot->flags |= KVM_MEMSLOT_INVALID;
1201
1202 /*
1203 * We can re-use the old memslots, the only difference from the
1204 * newly installed memslots is the invalid flag, which will get
1205 * dropped by update_memslots anyway. We'll also revert to the
1206 * old memslots if preparing the new memory region fails.
1207 */
1208 slots = install_new_memslots(kvm, as_id, slots);
1209
1210 /* From this point no new shadow pages pointing to a deleted,
1211 * or moved, memslot will be created.
1212 *
1213 * validation of sp->gfn happens in:
1214 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
1215 * - kvm_is_visible_gfn (mmu_check_root)
1216 */
1217 kvm_arch_flush_shadow_memslot(kvm, slot);
1218 }
1219
1220 r = kvm_arch_prepare_memory_region(kvm, new, mem, change);
1221 if (r)
1222 goto out_slots;
1223
1224 update_memslots(slots, new, change);
1225 slots = install_new_memslots(kvm, as_id, slots);
1226
1227 kvm_arch_commit_memory_region(kvm, mem, old, new, change);
1228
1229 kvfree(slots);
1230 return 0;
1231
1232out_slots:
1233 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE)
1234 slots = install_new_memslots(kvm, as_id, slots);
1235 kvfree(slots);
1236 return r;
1237}
1238
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001239static int kvm_delete_memslot(struct kvm *kvm,
1240 const struct kvm_userspace_memory_region *mem,
1241 struct kvm_memory_slot *old, int as_id)
1242{
1243 struct kvm_memory_slot new;
1244 int r;
1245
1246 if (!old->npages)
1247 return -EINVAL;
1248
1249 memset(&new, 0, sizeof(new));
1250 new.id = old->id;
Peter Xu9e9eb222020-10-14 11:26:46 -07001251 /*
1252 * This is only for debugging purpose; it should never be referenced
1253 * for a removed memslot.
1254 */
1255 new.as_id = as_id;
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001256
1257 r = kvm_set_memslot(kvm, mem, old, &new, as_id, KVM_MR_DELETE);
1258 if (r)
1259 return r;
1260
Sean Christophersone96c81e2020-02-18 13:07:27 -08001261 kvm_free_memslot(kvm, old);
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001262 return 0;
1263}
1264
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 * Allocate some memory and give it an address in the guest physical address
1267 * space.
1268 *
1269 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001270 *
Dominik Dingel02d5d552014-10-27 16:22:56 +01001271 * Must be called holding kvm->slots_lock for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001273int __kvm_set_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001274 const struct kvm_userspace_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 struct kvm_memory_slot old, new;
Sean Christopherson163da372020-02-18 13:07:28 -08001277 struct kvm_memory_slot *tmp;
Takuya Yoshikawaf64c0392013-01-29 11:00:07 +09001278 enum kvm_mr_change change;
Sean Christopherson163da372020-02-18 13:07:28 -08001279 int as_id, id;
1280 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001282 r = check_memory_region_flags(mem);
1283 if (r)
Sean Christopherson71a4c302020-02-18 13:07:22 -08001284 return r;
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001285
Paolo Bonzinif481b062015-05-17 17:30:37 +02001286 as_id = mem->slot >> 16;
1287 id = (u16)mem->slot;
1288
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289 /* General sanity checks */
1290 if (mem->memory_size & (PAGE_SIZE - 1))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001291 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001293 return -EINVAL;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +09001294 /* We can read the guest memory with __xxx_user() later on. */
Paolo Bonzini09d952c2020-06-01 04:17:45 -04001295 if ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
Linus Torvalds96d4f262019-01-03 18:57:57 -08001296 !access_ok((void __user *)(unsigned long)mem->userspace_addr,
Paolo Bonzini09d952c2020-06-01 04:17:45 -04001297 mem->memory_size))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001298 return -EINVAL;
Paolo Bonzinif481b062015-05-17 17:30:37 +02001299 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM)
Sean Christopherson71a4c302020-02-18 13:07:22 -08001300 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
Sean Christopherson71a4c302020-02-18 13:07:22 -08001302 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001304 /*
1305 * Make a full copy of the old memslot, the pointer will become stale
1306 * when the memslots are re-sorted by update_memslots(), and the old
1307 * memslot needs to be referenced after calling update_memslots(), e.g.
Sean Christopherson0dff0842020-02-18 13:07:29 -08001308 * to free its resources and for arch specific behavior.
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001309 */
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001310 tmp = id_to_memslot(__kvm_memslots(kvm, as_id), id);
1311 if (tmp) {
1312 old = *tmp;
1313 tmp = NULL;
1314 } else {
1315 memset(&old, 0, sizeof(old));
1316 old.id = id;
1317 }
Sean Christopherson163da372020-02-18 13:07:28 -08001318
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001319 if (!mem->memory_size)
1320 return kvm_delete_memslot(kvm, mem, &old, as_id);
1321
Peter Xu9e9eb222020-10-14 11:26:46 -07001322 new.as_id = as_id;
Paolo Bonzinif481b062015-05-17 17:30:37 +02001323 new.id = id;
Sean Christopherson163da372020-02-18 13:07:28 -08001324 new.base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1325 new.npages = mem->memory_size >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326 new.flags = mem->flags;
Sean Christopherson414de7ab2020-02-18 13:07:20 -08001327 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328
Sean Christopherson163da372020-02-18 13:07:28 -08001329 if (new.npages > KVM_MEM_MAX_NR_PAGES)
1330 return -EINVAL;
1331
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001332 if (!old.npages) {
1333 change = KVM_MR_CREATE;
Sean Christopherson163da372020-02-18 13:07:28 -08001334 new.dirty_bitmap = NULL;
1335 memset(&new.arch, 0, sizeof(new.arch));
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001336 } else { /* Modify an existing slot. */
1337 if ((new.userspace_addr != old.userspace_addr) ||
Sean Christopherson163da372020-02-18 13:07:28 -08001338 (new.npages != old.npages) ||
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001339 ((new.flags ^ old.flags) & KVM_MEM_READONLY))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001340 return -EINVAL;
Paolo Bonzini09170a42015-05-18 13:59:39 +02001341
Sean Christopherson163da372020-02-18 13:07:28 -08001342 if (new.base_gfn != old.base_gfn)
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001343 change = KVM_MR_MOVE;
1344 else if (new.flags != old.flags)
1345 change = KVM_MR_FLAGS_ONLY;
1346 else /* Nothing to change. */
1347 return 0;
Sean Christopherson163da372020-02-18 13:07:28 -08001348
1349 /* Copy dirty_bitmap and arch from the current memslot. */
1350 new.dirty_bitmap = old.dirty_bitmap;
1351 memcpy(&new.arch, &old.arch, sizeof(new.arch));
Paolo Bonzini09170a42015-05-18 13:59:39 +02001352 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353
Takuya Yoshikawaf64c0392013-01-29 11:00:07 +09001354 if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
Takuya Yoshikawa0a706be2013-01-11 18:26:55 +09001355 /* Check for overlaps */
Sean Christopherson163da372020-02-18 13:07:28 -08001356 kvm_for_each_memslot(tmp, __kvm_memslots(kvm, as_id)) {
1357 if (tmp->id == id)
Takuya Yoshikawa0a706be2013-01-11 18:26:55 +09001358 continue;
Sean Christopherson163da372020-02-18 13:07:28 -08001359 if (!((new.base_gfn + new.npages <= tmp->base_gfn) ||
1360 (new.base_gfn >= tmp->base_gfn + tmp->npages)))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001361 return -EEXIST;
Takuya Yoshikawa0a706be2013-01-11 18:26:55 +09001362 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364
Sean Christopherson414de7ab2020-02-18 13:07:20 -08001365 /* Allocate/free page dirty bitmap as needed */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001367 new.dirty_bitmap = NULL;
Sean Christopherson414de7ab2020-02-18 13:07:20 -08001368 else if (!new.dirty_bitmap) {
Jay Zhou3c9bd402020-02-27 09:32:27 +08001369 r = kvm_alloc_dirty_bitmap(&new);
Sean Christopherson71a4c302020-02-18 13:07:22 -08001370 if (r)
1371 return r;
Jay Zhou3c9bd402020-02-27 09:32:27 +08001372
1373 if (kvm_dirty_log_manual_protect_and_init_set(kvm))
1374 bitmap_set(new.dirty_bitmap, 0, new.npages);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 }
1376
Sean Christophersoncf47f502020-02-18 13:07:23 -08001377 r = kvm_set_memslot(kvm, mem, &old, &new, as_id, change);
1378 if (r)
1379 goto out_bitmap;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001380
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001381 if (old.dirty_bitmap && !new.dirty_bitmap)
1382 kvm_destroy_dirty_bitmap(&old);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 return 0;
1384
Sean Christophersonbd0e96f2020-02-18 13:07:21 -08001385out_bitmap:
1386 if (new.dirty_bitmap && !old.dirty_bitmap)
1387 kvm_destroy_dirty_bitmap(&new);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001389}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001390EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1391
1392int kvm_set_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001393 const struct kvm_userspace_memory_region *mem)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001394{
1395 int r;
1396
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001397 mutex_lock(&kvm->slots_lock);
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09001398 r = __kvm_set_memory_region(kvm, mem);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001399 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001400 return r;
1401}
Izik Eidus210c7c42007-10-24 23:52:57 +02001402EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1403
Stephen Hemminger79408762013-12-29 12:12:29 -08001404static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1405 struct kvm_userspace_memory_region *mem)
Izik Eidus210c7c42007-10-24 23:52:57 +02001406{
Paolo Bonzinif481b062015-05-17 17:30:37 +02001407 if ((u16)mem->slot >= KVM_USER_MEM_SLOTS)
Izik Eiduse0d62c72007-10-24 23:57:46 +02001408 return -EINVAL;
Paolo Bonzini09170a42015-05-18 13:59:39 +02001409
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09001410 return kvm_set_memory_region(kvm, mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411}
1412
Sean Christopherson0dff0842020-02-18 13:07:29 -08001413#ifndef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
Sean Christopherson2a49f612020-02-18 13:07:30 -08001414/**
1415 * kvm_get_dirty_log - get a snapshot of dirty pages
1416 * @kvm: pointer to kvm instance
1417 * @log: slot id and address to which we copy the log
1418 * @is_dirty: set to '1' if any dirty pages were found
1419 * @memslot: set to the associated memslot, always valid on success
1420 */
1421int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log,
1422 int *is_dirty, struct kvm_memory_slot **memslot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001424 struct kvm_memslots *slots;
Markus Elfring843574a2017-01-22 17:41:07 +01001425 int i, as_id, id;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001426 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427 unsigned long any = 0;
1428
Peter Xub2cc64c2020-09-30 21:22:24 -04001429 /* Dirty ring tracking is exclusive to dirty log tracking */
1430 if (kvm->dirty_ring_size)
1431 return -ENXIO;
1432
Sean Christopherson2a49f612020-02-18 13:07:30 -08001433 *memslot = NULL;
1434 *is_dirty = 0;
1435
Paolo Bonzinif481b062015-05-17 17:30:37 +02001436 as_id = log->slot >> 16;
1437 id = (u16)log->slot;
1438 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
Markus Elfring843574a2017-01-22 17:41:07 +01001439 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
Paolo Bonzinif481b062015-05-17 17:30:37 +02001441 slots = __kvm_memslots(kvm, as_id);
Sean Christopherson2a49f612020-02-18 13:07:30 -08001442 *memslot = id_to_memslot(slots, id);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001443 if (!(*memslot) || !(*memslot)->dirty_bitmap)
Markus Elfring843574a2017-01-22 17:41:07 +01001444 return -ENOENT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445
Sean Christopherson2a49f612020-02-18 13:07:30 -08001446 kvm_arch_sync_dirty_log(kvm, *memslot);
1447
1448 n = kvm_dirty_bitmap_bytes(*memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449
Uri Lublincd1a4a92007-02-22 16:43:09 +02001450 for (i = 0; !any && i < n/sizeof(long); ++i)
Sean Christopherson2a49f612020-02-18 13:07:30 -08001451 any = (*memslot)->dirty_bitmap[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452
Sean Christopherson2a49f612020-02-18 13:07:30 -08001453 if (copy_to_user(log->dirty_bitmap, (*memslot)->dirty_bitmap, n))
Markus Elfring843574a2017-01-22 17:41:07 +01001454 return -EFAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001456 if (any)
1457 *is_dirty = 1;
Markus Elfring843574a2017-01-22 17:41:07 +01001458 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05301460EXPORT_SYMBOL_GPL(kvm_get_dirty_log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461
Sean Christopherson0dff0842020-02-18 13:07:29 -08001462#else /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
Mario Smarduchba0513b2015-01-15 15:58:53 -08001463/**
Jiang Biaob8b00222019-04-23 19:40:30 +08001464 * kvm_get_dirty_log_protect - get a snapshot of dirty pages
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001465 * and reenable dirty page tracking for the corresponding pages.
Mario Smarduchba0513b2015-01-15 15:58:53 -08001466 * @kvm: pointer to kvm instance
1467 * @log: slot id and address to which we copy the log
Mario Smarduchba0513b2015-01-15 15:58:53 -08001468 *
1469 * We need to keep it in mind that VCPU threads can write to the bitmap
1470 * concurrently. So, to avoid losing track of dirty pages we keep the
1471 * following order:
1472 *
1473 * 1. Take a snapshot of the bit and clear it if needed.
1474 * 2. Write protect the corresponding page.
1475 * 3. Copy the snapshot to the userspace.
1476 * 4. Upon return caller flushes TLB's if needed.
1477 *
1478 * Between 2 and 4, the guest may write to the page using the remaining TLB
1479 * entry. This is not a problem because the page is reported dirty using
1480 * the snapshot taken before and step 4 ensures that writes done after
1481 * exiting to userspace will be logged for the next call.
1482 *
1483 */
Sean Christopherson0dff0842020-02-18 13:07:29 -08001484static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log)
Mario Smarduchba0513b2015-01-15 15:58:53 -08001485{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001486 struct kvm_memslots *slots;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001487 struct kvm_memory_slot *memslot;
Markus Elfring58d6db32017-01-22 17:30:16 +01001488 int i, as_id, id;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001489 unsigned long n;
1490 unsigned long *dirty_bitmap;
1491 unsigned long *dirty_bitmap_buffer;
Sean Christopherson0dff0842020-02-18 13:07:29 -08001492 bool flush;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001493
Peter Xub2cc64c2020-09-30 21:22:24 -04001494 /* Dirty ring tracking is exclusive to dirty log tracking */
1495 if (kvm->dirty_ring_size)
1496 return -ENXIO;
1497
Paolo Bonzinif481b062015-05-17 17:30:37 +02001498 as_id = log->slot >> 16;
1499 id = (u16)log->slot;
1500 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
Markus Elfring58d6db32017-01-22 17:30:16 +01001501 return -EINVAL;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001502
Paolo Bonzinif481b062015-05-17 17:30:37 +02001503 slots = __kvm_memslots(kvm, as_id);
1504 memslot = id_to_memslot(slots, id);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001505 if (!memslot || !memslot->dirty_bitmap)
1506 return -ENOENT;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001507
1508 dirty_bitmap = memslot->dirty_bitmap;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001509
Sean Christopherson0dff0842020-02-18 13:07:29 -08001510 kvm_arch_sync_dirty_log(kvm, memslot);
1511
Mario Smarduchba0513b2015-01-15 15:58:53 -08001512 n = kvm_dirty_bitmap_bytes(memslot);
Sean Christopherson0dff0842020-02-18 13:07:29 -08001513 flush = false;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001514 if (kvm->manual_dirty_log_protect) {
1515 /*
1516 * Unlike kvm_get_dirty_log, we always return false in *flush,
1517 * because no flush is needed until KVM_CLEAR_DIRTY_LOG. There
1518 * is some code duplication between this function and
1519 * kvm_get_dirty_log, but hopefully all architecture
1520 * transition to kvm_get_dirty_log_protect and kvm_get_dirty_log
1521 * can be eliminated.
1522 */
1523 dirty_bitmap_buffer = dirty_bitmap;
1524 } else {
1525 dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);
1526 memset(dirty_bitmap_buffer, 0, n);
Mario Smarduchba0513b2015-01-15 15:58:53 -08001527
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001528 spin_lock(&kvm->mmu_lock);
1529 for (i = 0; i < n / sizeof(long); i++) {
1530 unsigned long mask;
1531 gfn_t offset;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001532
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001533 if (!dirty_bitmap[i])
1534 continue;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001535
Sean Christopherson0dff0842020-02-18 13:07:29 -08001536 flush = true;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001537 mask = xchg(&dirty_bitmap[i], 0);
1538 dirty_bitmap_buffer[i] = mask;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001539
Lan Tianyua67794c2019-02-02 17:20:27 +08001540 offset = i * BITS_PER_LONG;
1541 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
1542 offset, mask);
Takuya Yoshikawa58d29302015-03-17 16:19:58 +09001543 }
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001544 spin_unlock(&kvm->mmu_lock);
Mario Smarduchba0513b2015-01-15 15:58:53 -08001545 }
1546
Sean Christopherson0dff0842020-02-18 13:07:29 -08001547 if (flush)
1548 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
1549
Mario Smarduchba0513b2015-01-15 15:58:53 -08001550 if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
Markus Elfring58d6db32017-01-22 17:30:16 +01001551 return -EFAULT;
1552 return 0;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001553}
Sean Christopherson0dff0842020-02-18 13:07:29 -08001554
1555
1556/**
1557 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1558 * @kvm: kvm instance
1559 * @log: slot id and address to which we copy the log
1560 *
1561 * Steps 1-4 below provide general overview of dirty page logging. See
1562 * kvm_get_dirty_log_protect() function description for additional details.
1563 *
1564 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1565 * always flush the TLB (step 4) even if previous step failed and the dirty
1566 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1567 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1568 * writes will be marked dirty for next log read.
1569 *
1570 * 1. Take a snapshot of the bit and clear it if needed.
1571 * 2. Write protect the corresponding page.
1572 * 3. Copy the snapshot to the userspace.
1573 * 4. Flush TLB's if needed.
1574 */
1575static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1576 struct kvm_dirty_log *log)
1577{
1578 int r;
1579
1580 mutex_lock(&kvm->slots_lock);
1581
1582 r = kvm_get_dirty_log_protect(kvm, log);
1583
1584 mutex_unlock(&kvm->slots_lock);
1585 return r;
1586}
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001587
1588/**
1589 * kvm_clear_dirty_log_protect - clear dirty bits in the bitmap
1590 * and reenable dirty page tracking for the corresponding pages.
1591 * @kvm: pointer to kvm instance
1592 * @log: slot id and address from which to fetch the bitmap of dirty pages
1593 */
Sean Christopherson0dff0842020-02-18 13:07:29 -08001594static int kvm_clear_dirty_log_protect(struct kvm *kvm,
1595 struct kvm_clear_dirty_log *log)
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001596{
1597 struct kvm_memslots *slots;
1598 struct kvm_memory_slot *memslot;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001599 int as_id, id;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001600 gfn_t offset;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001601 unsigned long i, n;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001602 unsigned long *dirty_bitmap;
1603 unsigned long *dirty_bitmap_buffer;
Sean Christopherson0dff0842020-02-18 13:07:29 -08001604 bool flush;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001605
Peter Xub2cc64c2020-09-30 21:22:24 -04001606 /* Dirty ring tracking is exclusive to dirty log tracking */
1607 if (kvm->dirty_ring_size)
1608 return -ENXIO;
1609
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001610 as_id = log->slot >> 16;
1611 id = (u16)log->slot;
1612 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
1613 return -EINVAL;
1614
Paolo Bonzini76d58e02019-04-17 15:28:44 +02001615 if (log->first_page & 63)
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001616 return -EINVAL;
1617
1618 slots = __kvm_memslots(kvm, as_id);
1619 memslot = id_to_memslot(slots, id);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001620 if (!memslot || !memslot->dirty_bitmap)
1621 return -ENOENT;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001622
1623 dirty_bitmap = memslot->dirty_bitmap;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001624
Peter Xu4ddc9202019-05-08 17:15:45 +08001625 n = ALIGN(log->num_pages, BITS_PER_LONG) / 8;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001626
1627 if (log->first_page > memslot->npages ||
Paolo Bonzini76d58e02019-04-17 15:28:44 +02001628 log->num_pages > memslot->npages - log->first_page ||
1629 (log->num_pages < memslot->npages - log->first_page && (log->num_pages & 63)))
1630 return -EINVAL;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001631
Sean Christopherson0dff0842020-02-18 13:07:29 -08001632 kvm_arch_sync_dirty_log(kvm, memslot);
1633
1634 flush = false;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001635 dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);
1636 if (copy_from_user(dirty_bitmap_buffer, log->dirty_bitmap, n))
1637 return -EFAULT;
1638
1639 spin_lock(&kvm->mmu_lock);
Peter Xu53eac7a2019-05-08 17:15:46 +08001640 for (offset = log->first_page, i = offset / BITS_PER_LONG,
1641 n = DIV_ROUND_UP(log->num_pages, BITS_PER_LONG); n--;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001642 i++, offset += BITS_PER_LONG) {
1643 unsigned long mask = *dirty_bitmap_buffer++;
1644 atomic_long_t *p = (atomic_long_t *) &dirty_bitmap[i];
1645 if (!mask)
1646 continue;
1647
1648 mask &= atomic_long_fetch_andnot(mask, p);
1649
1650 /*
1651 * mask contains the bits that really have been cleared. This
1652 * never includes any bits beyond the length of the memslot (if
1653 * the length is not aligned to 64 pages), therefore it is not
1654 * a problem if userspace sets them in log->dirty_bitmap.
1655 */
1656 if (mask) {
Sean Christopherson0dff0842020-02-18 13:07:29 -08001657 flush = true;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001658 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
1659 offset, mask);
1660 }
1661 }
1662 spin_unlock(&kvm->mmu_lock);
1663
Sean Christopherson0dff0842020-02-18 13:07:29 -08001664 if (flush)
1665 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
1666
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001667 return 0;
1668}
Sean Christopherson0dff0842020-02-18 13:07:29 -08001669
1670static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,
1671 struct kvm_clear_dirty_log *log)
1672{
1673 int r;
1674
1675 mutex_lock(&kvm->slots_lock);
1676
1677 r = kvm_clear_dirty_log_protect(kvm, log);
1678
1679 mutex_unlock(&kvm->slots_lock);
1680 return r;
1681}
1682#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
Mario Smarduchba0513b2015-01-15 15:58:53 -08001683
Gleb Natapov49c77542010-10-18 15:22:23 +02001684struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1685{
1686 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
1687}
Avi Kivitya1f4d3952010-06-21 11:44:20 +03001688EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689
Paolo Bonzini8e734852015-05-17 13:58:53 +02001690struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn)
1691{
1692 return __gfn_to_memslot(kvm_vcpu_memslots(vcpu), gfn);
1693}
Paolo Bonzinie72436b2020-04-17 12:21:06 -04001694EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_memslot);
Paolo Bonzini8e734852015-05-17 13:58:53 +02001695
Yaowei Bai33e94152015-11-14 11:21:06 +08001696bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
Izik Eiduse0d62c72007-10-24 23:57:46 +02001697{
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +08001698 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
Izik Eiduse0d62c72007-10-24 23:57:46 +02001699
Paolo Bonzinic36b7152020-04-16 09:48:07 -04001700 return kvm_is_visible_memslot(memslot);
Izik Eiduse0d62c72007-10-24 23:57:46 +02001701}
1702EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1703
Vitaly Kuznetsov995decb2020-07-08 16:00:23 +02001704bool kvm_vcpu_is_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
1705{
1706 struct kvm_memory_slot *memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
1707
1708 return kvm_is_visible_memslot(memslot);
1709}
1710EXPORT_SYMBOL_GPL(kvm_vcpu_is_visible_gfn);
1711
Sean Christophersonf9b84e12020-01-08 12:24:37 -08001712unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn)
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001713{
1714 struct vm_area_struct *vma;
1715 unsigned long addr, size;
1716
1717 size = PAGE_SIZE;
1718
Sean Christopherson42cde482020-01-08 12:24:38 -08001719 addr = kvm_vcpu_gfn_to_hva_prot(vcpu, gfn, NULL);
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001720 if (kvm_is_error_hva(addr))
1721 return PAGE_SIZE;
1722
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001723 mmap_read_lock(current->mm);
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001724 vma = find_vma(current->mm, addr);
1725 if (!vma)
1726 goto out;
1727
1728 size = vma_kernel_pagesize(vma);
1729
1730out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001731 mmap_read_unlock(current->mm);
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001732
1733 return size;
1734}
1735
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001736static bool memslot_is_readonly(struct kvm_memory_slot *slot)
1737{
1738 return slot->flags & KVM_MEM_READONLY;
1739}
1740
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001741static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1742 gfn_t *nr_pages, bool write)
Izik Eidus539cb662007-11-11 22:05:04 +02001743{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001744 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Xiao Guangrongca3a4902012-08-21 11:01:50 +08001745 return KVM_HVA_ERR_BAD;
Xiao Guangrong48987782010-08-22 19:11:43 +08001746
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001747 if (memslot_is_readonly(slot) && write)
1748 return KVM_HVA_ERR_RO_BAD;
Xiao Guangrong48987782010-08-22 19:11:43 +08001749
1750 if (nr_pages)
1751 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1752
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001753 return __gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001754}
Xiao Guangrong48987782010-08-22 19:11:43 +08001755
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001756static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1757 gfn_t *nr_pages)
1758{
1759 return __gfn_to_hva_many(slot, gfn, nr_pages, true);
1760}
1761
1762unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
Stephen Hemminger79408762013-12-29 12:12:29 -08001763 gfn_t gfn)
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001764{
1765 return gfn_to_hva_many(slot, gfn, NULL);
1766}
1767EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
1768
Xiao Guangrong48987782010-08-22 19:11:43 +08001769unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1770{
Gleb Natapov49c77542010-10-18 15:22:23 +02001771 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001772}
Sheng Yang0d150292008-04-25 21:44:50 +08001773EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001774
Paolo Bonzini8e734852015-05-17 13:58:53 +02001775unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn)
1776{
1777 return gfn_to_hva_many(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, NULL);
1778}
1779EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva);
1780
Xiao Guangrong86ab8cf2012-08-21 10:59:53 +08001781/*
Wei Yang970c0d42018-10-09 10:41:15 +08001782 * Return the hva of a @gfn and the R/W attribute if possible.
1783 *
1784 * @slot: the kvm_memory_slot which contains @gfn
1785 * @gfn: the gfn to be translated
1786 * @writable: used to return the read/write attribute of the @slot if the hva
1787 * is valid and @writable is not NULL
Xiao Guangrong86ab8cf2012-08-21 10:59:53 +08001788 */
Christoffer Dall64d83122014-08-19 12:15:00 +02001789unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot,
1790 gfn_t gfn, bool *writable)
Gleb Natapov80300892010-10-19 18:13:41 +02001791{
Gleb Natapova2ac07f2013-10-01 19:58:36 +03001792 unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
1793
1794 if (!kvm_is_error_hva(hva) && writable)
Paolo Bonziniba6a3542013-09-09 13:52:33 +02001795 *writable = !memslot_is_readonly(slot);
1796
Gleb Natapova2ac07f2013-10-01 19:58:36 +03001797 return hva;
Xiao Guangrong86ab8cf2012-08-21 10:59:53 +08001798}
1799
Christoffer Dall64d83122014-08-19 12:15:00 +02001800unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
1801{
1802 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
1803
1804 return gfn_to_hva_memslot_prot(slot, gfn, writable);
1805}
1806
Paolo Bonzini8e734852015-05-17 13:58:53 +02001807unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable)
1808{
1809 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
1810
1811 return gfn_to_hva_memslot_prot(slot, gfn, writable);
1812}
1813
Huang Yingfafc3db2011-01-30 11:15:49 +08001814static inline int check_user_page_hwpoison(unsigned long addr)
1815{
Lorenzo Stoakes0d731752016-10-24 10:57:25 +01001816 int rc, flags = FOLL_HWPOISON | FOLL_WRITE;
Huang Yingfafc3db2011-01-30 11:15:49 +08001817
Lorenzo Stoakes0d731752016-10-24 10:57:25 +01001818 rc = get_user_pages(addr, 1, flags, NULL, NULL);
Huang Yingfafc3db2011-01-30 11:15:49 +08001819 return rc == -EHWPOISON;
1820}
1821
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001822/*
Paolo Bonzinib9b33da2018-07-27 17:44:41 +02001823 * The fast path to get the writable pfn which will be stored in @pfn,
1824 * true indicates success, otherwise false is returned. It's also the
Miaohe Lin311497e2019-12-11 14:26:25 +08001825 * only part that runs if we can in atomic context.
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001826 */
Paolo Bonzinib9b33da2018-07-27 17:44:41 +02001827static bool hva_to_pfn_fast(unsigned long addr, bool write_fault,
1828 bool *writable, kvm_pfn_t *pfn)
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001829{
1830 struct page *page[1];
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001831
Xiao Guangrong12ce13f2012-08-21 11:00:49 +08001832 /*
1833 * Fast pin a writable pfn only if it is a write fault request
1834 * or the caller allows to map a writable pfn for a read fault
1835 * request.
1836 */
1837 if (!(write_fault || writable))
1838 return false;
1839
Souptick Joarderdadbb612020-06-07 21:40:55 -07001840 if (get_user_page_fast_only(addr, FOLL_WRITE, page)) {
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001841 *pfn = page_to_pfn(page[0]);
1842
1843 if (writable)
1844 *writable = true;
1845 return true;
1846 }
1847
1848 return false;
1849}
1850
1851/*
1852 * The slow path to get the pfn of the specified host virtual address,
1853 * 1 indicates success, -errno is returned if error is detected.
1854 */
1855static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
Dan Williamsba049e92016-01-15 16:56:11 -08001856 bool *writable, kvm_pfn_t *pfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001857{
Al Viroce530532017-11-19 17:47:33 -05001858 unsigned int flags = FOLL_HWPOISON;
1859 struct page *page;
Gleb Natapovaf585b92010-10-14 11:22:46 +02001860 int npages = 0;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001861
1862 might_sleep();
1863
1864 if (writable)
1865 *writable = write_fault;
1866
Al Viroce530532017-11-19 17:47:33 -05001867 if (write_fault)
1868 flags |= FOLL_WRITE;
1869 if (async)
1870 flags |= FOLL_NOWAIT;
Lorenzo Stoakesd4944b02016-10-13 01:20:12 +01001871
Al Viroce530532017-11-19 17:47:33 -05001872 npages = get_user_pages_unlocked(addr, 1, &page, flags);
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001873 if (npages != 1)
1874 return npages;
1875
1876 /* map read fault as writable if possible */
Xiao Guangrong12ce13f2012-08-21 11:00:49 +08001877 if (unlikely(!write_fault) && writable) {
Al Viroce530532017-11-19 17:47:33 -05001878 struct page *wpage;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001879
Souptick Joarderdadbb612020-06-07 21:40:55 -07001880 if (get_user_page_fast_only(addr, FOLL_WRITE, &wpage)) {
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001881 *writable = true;
Al Viroce530532017-11-19 17:47:33 -05001882 put_page(page);
1883 page = wpage;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001884 }
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001885 }
Al Viroce530532017-11-19 17:47:33 -05001886 *pfn = page_to_pfn(page);
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001887 return npages;
1888}
1889
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001890static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
1891{
1892 if (unlikely(!(vma->vm_flags & VM_READ)))
1893 return false;
1894
1895 if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
1896 return false;
1897
1898 return true;
1899}
1900
Paolo Bonzini92176a82016-06-07 16:22:47 +02001901static int hva_to_pfn_remapped(struct vm_area_struct *vma,
1902 unsigned long addr, bool *async,
KarimAllah Ahmeda340b3e2018-01-17 19:18:56 +01001903 bool write_fault, bool *writable,
1904 kvm_pfn_t *p_pfn)
Paolo Bonzini92176a82016-06-07 16:22:47 +02001905{
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001906 unsigned long pfn;
1907 int r;
1908
1909 r = follow_pfn(vma, addr, &pfn);
1910 if (r) {
1911 /*
1912 * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does
1913 * not call the fault handler, so do it here.
1914 */
1915 bool unlocked = false;
Peter Xu64019a22020-08-11 18:39:01 -07001916 r = fixup_user_fault(current->mm, addr,
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001917 (write_fault ? FAULT_FLAG_WRITE : 0),
1918 &unlocked);
Paolo Bonzinia8387d02020-05-29 05:42:55 -04001919 if (unlocked)
1920 return -EAGAIN;
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001921 if (r)
1922 return r;
1923
1924 r = follow_pfn(vma, addr, &pfn);
1925 if (r)
1926 return r;
1927
1928 }
1929
KarimAllah Ahmeda340b3e2018-01-17 19:18:56 +01001930 if (writable)
1931 *writable = true;
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001932
1933 /*
1934 * Get a reference here because callers of *hva_to_pfn* and
1935 * *gfn_to_pfn* ultimately call kvm_release_pfn_clean on the
1936 * returned pfn. This is only needed if the VMA has VM_MIXEDMAP
1937 * set, but the kvm_get_pfn/kvm_release_pfn_clean pair will
1938 * simply do nothing for reserved pfns.
1939 *
1940 * Whoever called remap_pfn_range is also going to call e.g.
1941 * unmap_mapping_range before the underlying pages are freed,
1942 * causing a call to our MMU notifier.
1943 */
1944 kvm_get_pfn(pfn);
1945
1946 *p_pfn = pfn;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001947 return 0;
1948}
1949
Xiao Guangrong12ce13f2012-08-21 11:00:49 +08001950/*
1951 * Pin guest page in memory and return its pfn.
1952 * @addr: host virtual address which maps memory to the guest
1953 * @atomic: whether this function can sleep
1954 * @async: whether this function need to wait IO complete if the
1955 * host page is not in the memory
1956 * @write_fault: whether we should get a writable host page
1957 * @writable: whether it allows to map a writable host page for !@write_fault
1958 *
1959 * The function will map a writable host page for these two cases:
1960 * 1): @write_fault = true
1961 * 2): @write_fault = false && @writable, @writable will tell the caller
1962 * whether the mapping is writable.
1963 */
Dan Williamsba049e92016-01-15 16:56:11 -08001964static kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001965 bool write_fault, bool *writable)
1966{
1967 struct vm_area_struct *vma;
Dan Williamsba049e92016-01-15 16:56:11 -08001968 kvm_pfn_t pfn = 0;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001969 int npages, r;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001970
Gleb Natapovaf585b92010-10-14 11:22:46 +02001971 /* we can do it either atomically or asynchronously, not both */
1972 BUG_ON(atomic && async);
1973
Paolo Bonzinib9b33da2018-07-27 17:44:41 +02001974 if (hva_to_pfn_fast(addr, write_fault, writable, &pfn))
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001975 return pfn;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001976
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001977 if (atomic)
1978 return KVM_PFN_ERR_FAULT;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001979
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001980 npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
1981 if (npages == 1)
1982 return pfn;
Gleb Natapovaf585b92010-10-14 11:22:46 +02001983
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001984 mmap_read_lock(current->mm);
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001985 if (npages == -EHWPOISON ||
1986 (!async && check_user_page_hwpoison(addr))) {
1987 pfn = KVM_PFN_ERR_HWPOISON;
1988 goto exit;
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001989 }
Izik Eidus539cb662007-11-11 22:05:04 +02001990
Paolo Bonzinia8387d02020-05-29 05:42:55 -04001991retry:
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001992 vma = find_vma_intersection(current->mm, addr, addr + 1);
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001993
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001994 if (vma == NULL)
1995 pfn = KVM_PFN_ERR_FAULT;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001996 else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) {
KarimAllah Ahmeda340b3e2018-01-17 19:18:56 +01001997 r = hva_to_pfn_remapped(vma, addr, async, write_fault, writable, &pfn);
Paolo Bonzinia8387d02020-05-29 05:42:55 -04001998 if (r == -EAGAIN)
1999 goto retry;
Paolo Bonzini92176a82016-06-07 16:22:47 +02002000 if (r < 0)
2001 pfn = KVM_PFN_ERR_FAULT;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08002002 } else {
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002003 if (async && vma_is_valid(vma, write_fault))
Xiao Guangrong2fc84312012-08-21 11:00:22 +08002004 *async = true;
2005 pfn = KVM_PFN_ERR_FAULT;
2006 }
2007exit:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002008 mmap_read_unlock(current->mm);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002009 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002010}
2011
Dan Williamsba049e92016-01-15 16:56:11 -08002012kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn,
2013 bool atomic, bool *async, bool write_fault,
2014 bool *writable)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08002015{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002016 unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
2017
Paolo Bonzinib2740d32016-02-23 15:36:01 +01002018 if (addr == KVM_HVA_ERR_RO_BAD) {
2019 if (writable)
2020 *writable = false;
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002021 return KVM_PFN_ERR_RO_FAULT;
Paolo Bonzinib2740d32016-02-23 15:36:01 +01002022 }
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002023
Paolo Bonzinib2740d32016-02-23 15:36:01 +01002024 if (kvm_is_error_hva(addr)) {
2025 if (writable)
2026 *writable = false;
Xiao Guangrong81c52c52012-10-16 20:10:59 +08002027 return KVM_PFN_NOSLOT;
Paolo Bonzinib2740d32016-02-23 15:36:01 +01002028 }
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002029
2030 /* Do not map writable pfn in the readonly memslot. */
2031 if (writable && memslot_is_readonly(slot)) {
2032 *writable = false;
2033 writable = NULL;
2034 }
2035
2036 return hva_to_pfn(addr, atomic, async, write_fault,
2037 writable);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08002038}
Paolo Bonzini35204692015-04-02 11:20:48 +02002039EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08002040
Dan Williamsba049e92016-01-15 16:56:11 -08002041kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002042 bool *writable)
2043{
Paolo Bonzinie37afc62015-05-19 16:09:04 +02002044 return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL,
2045 write_fault, writable);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002046}
2047EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
2048
Dan Williamsba049e92016-01-15 16:56:11 -08002049kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02002050{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002051 return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02002052}
Paolo Bonzinie37afc62015-05-19 16:09:04 +02002053EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02002054
Dan Williamsba049e92016-01-15 16:56:11 -08002055kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
Xiao Guangrong037d92d2012-08-21 10:59:12 +08002056{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002057 return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
Xiao Guangrong037d92d2012-08-21 10:59:12 +08002058}
2059EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
2060
Dan Williamsba049e92016-01-15 16:56:11 -08002061kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn)
Paolo Bonzini8e734852015-05-17 13:58:53 +02002062{
2063 return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
2064}
2065EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic);
2066
Dan Williamsba049e92016-01-15 16:56:11 -08002067kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Paolo Bonzinie37afc62015-05-19 16:09:04 +02002068{
2069 return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn);
2070}
2071EXPORT_SYMBOL_GPL(gfn_to_pfn);
2072
Dan Williamsba049e92016-01-15 16:56:11 -08002073kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
Paolo Bonzini8e734852015-05-17 13:58:53 +02002074{
2075 return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
2076}
2077EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn);
2078
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002079int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
2080 struct page **pages, int nr_pages)
Xiao Guangrong48987782010-08-22 19:11:43 +08002081{
2082 unsigned long addr;
Arnd Bergmann076b9252017-08-10 14:14:39 +02002083 gfn_t entry = 0;
Xiao Guangrong48987782010-08-22 19:11:43 +08002084
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002085 addr = gfn_to_hva_many(slot, gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08002086 if (kvm_is_error_hva(addr))
2087 return -1;
2088
2089 if (entry < nr_pages)
2090 return 0;
2091
Souptick Joarderdadbb612020-06-07 21:40:55 -07002092 return get_user_pages_fast_only(addr, nr_pages, FOLL_WRITE, pages);
Xiao Guangrong48987782010-08-22 19:11:43 +08002093}
2094EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
2095
Dan Williamsba049e92016-01-15 16:56:11 -08002096static struct page *kvm_pfn_to_page(kvm_pfn_t pfn)
Xiao Guangronga2766322012-07-26 11:58:59 +08002097{
Xiao Guangrong81c52c52012-10-16 20:10:59 +08002098 if (is_error_noslot_pfn(pfn))
Xiao Guangrong6cede2e2012-08-03 15:41:22 +08002099 return KVM_ERR_PTR_BAD_PAGE;
Xiao Guangronga2766322012-07-26 11:58:59 +08002100
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002101 if (kvm_is_reserved_pfn(pfn)) {
Xiao Guangrongcb9aaa32012-08-03 15:42:10 +08002102 WARN_ON(1);
2103 return KVM_ERR_PTR_BAD_PAGE;
2104 }
2105
Xiao Guangronga2766322012-07-26 11:58:59 +08002106 return pfn_to_page(pfn);
2107}
2108
Anthony Liguori35149e22008-04-02 14:46:56 -05002109struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
2110{
Dan Williamsba049e92016-01-15 16:56:11 -08002111 kvm_pfn_t pfn;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002112
2113 pfn = gfn_to_pfn(kvm, gfn);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002114
Xiao Guangronga2766322012-07-26 11:58:59 +08002115 return kvm_pfn_to_page(pfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +03002116}
2117EXPORT_SYMBOL_GPL(gfn_to_page);
2118
Boris Ostrovsky91724812019-12-05 01:30:51 +00002119void kvm_release_pfn(kvm_pfn_t pfn, bool dirty, struct gfn_to_pfn_cache *cache)
2120{
2121 if (pfn == 0)
2122 return;
2123
2124 if (cache)
2125 cache->pfn = cache->gfn = 0;
2126
2127 if (dirty)
2128 kvm_release_pfn_dirty(pfn);
2129 else
2130 kvm_release_pfn_clean(pfn);
2131}
2132
2133static void kvm_cache_gfn_to_pfn(struct kvm_memory_slot *slot, gfn_t gfn,
2134 struct gfn_to_pfn_cache *cache, u64 gen)
2135{
2136 kvm_release_pfn(cache->pfn, cache->dirty, cache);
2137
2138 cache->pfn = gfn_to_pfn_memslot(slot, gfn);
2139 cache->gfn = gfn;
2140 cache->dirty = false;
2141 cache->generation = gen;
2142}
2143
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002144static int __kvm_map_gfn(struct kvm_memslots *slots, gfn_t gfn,
Boris Ostrovsky91724812019-12-05 01:30:51 +00002145 struct kvm_host_map *map,
2146 struct gfn_to_pfn_cache *cache,
2147 bool atomic)
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002148{
2149 kvm_pfn_t pfn;
2150 void *hva = NULL;
2151 struct page *page = KVM_UNMAPPED_PAGE;
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002152 struct kvm_memory_slot *slot = __gfn_to_memslot(slots, gfn);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002153 u64 gen = slots->generation;
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002154
2155 if (!map)
2156 return -EINVAL;
2157
Boris Ostrovsky91724812019-12-05 01:30:51 +00002158 if (cache) {
2159 if (!cache->pfn || cache->gfn != gfn ||
2160 cache->generation != gen) {
2161 if (atomic)
2162 return -EAGAIN;
2163 kvm_cache_gfn_to_pfn(slot, gfn, cache, gen);
2164 }
2165 pfn = cache->pfn;
2166 } else {
2167 if (atomic)
2168 return -EAGAIN;
2169 pfn = gfn_to_pfn_memslot(slot, gfn);
2170 }
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002171 if (is_error_noslot_pfn(pfn))
2172 return -EINVAL;
2173
2174 if (pfn_valid(pfn)) {
2175 page = pfn_to_page(pfn);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002176 if (atomic)
2177 hva = kmap_atomic(page);
2178 else
2179 hva = kmap(page);
Paolo Bonzinid30b2142019-05-20 12:06:36 +02002180#ifdef CONFIG_HAS_IOMEM
Boris Ostrovsky91724812019-12-05 01:30:51 +00002181 } else if (!atomic) {
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002182 hva = memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002183 } else {
2184 return -EINVAL;
Paolo Bonzinid30b2142019-05-20 12:06:36 +02002185#endif
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002186 }
2187
2188 if (!hva)
2189 return -EFAULT;
2190
2191 map->page = page;
2192 map->hva = hva;
2193 map->pfn = pfn;
2194 map->gfn = gfn;
2195
2196 return 0;
2197}
2198
Boris Ostrovsky91724812019-12-05 01:30:51 +00002199int kvm_map_gfn(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map,
2200 struct gfn_to_pfn_cache *cache, bool atomic)
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002201{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002202 return __kvm_map_gfn(kvm_memslots(vcpu->kvm), gfn, map,
2203 cache, atomic);
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002204}
2205EXPORT_SYMBOL_GPL(kvm_map_gfn);
2206
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002207int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map)
2208{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002209 return __kvm_map_gfn(kvm_vcpu_memslots(vcpu), gfn, map,
2210 NULL, false);
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002211}
2212EXPORT_SYMBOL_GPL(kvm_vcpu_map);
2213
Peter Xu28bd7262020-09-30 21:20:34 -04002214static void __kvm_unmap_gfn(struct kvm *kvm,
2215 struct kvm_memory_slot *memslot,
Boris Ostrovsky91724812019-12-05 01:30:51 +00002216 struct kvm_host_map *map,
2217 struct gfn_to_pfn_cache *cache,
2218 bool dirty, bool atomic)
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002219{
2220 if (!map)
2221 return;
2222
2223 if (!map->hva)
2224 return;
2225
Boris Ostrovsky91724812019-12-05 01:30:51 +00002226 if (map->page != KVM_UNMAPPED_PAGE) {
2227 if (atomic)
2228 kunmap_atomic(map->hva);
2229 else
2230 kunmap(map->page);
2231 }
Christian Borntraegereb1f2f32019-05-27 10:28:25 +02002232#ifdef CONFIG_HAS_IOMEM
Boris Ostrovsky91724812019-12-05 01:30:51 +00002233 else if (!atomic)
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002234 memunmap(map->hva);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002235 else
2236 WARN_ONCE(1, "Unexpected unmapping in atomic context");
Christian Borntraegereb1f2f32019-05-27 10:28:25 +02002237#endif
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002238
Boris Ostrovsky91724812019-12-05 01:30:51 +00002239 if (dirty)
Peter Xu28bd7262020-09-30 21:20:34 -04002240 mark_page_dirty_in_slot(kvm, memslot, map->gfn);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002241
2242 if (cache)
2243 cache->dirty |= dirty;
2244 else
2245 kvm_release_pfn(map->pfn, dirty, NULL);
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002246
2247 map->hva = NULL;
2248 map->page = NULL;
2249}
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002250
Boris Ostrovsky91724812019-12-05 01:30:51 +00002251int kvm_unmap_gfn(struct kvm_vcpu *vcpu, struct kvm_host_map *map,
2252 struct gfn_to_pfn_cache *cache, bool dirty, bool atomic)
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002253{
Peter Xu28bd7262020-09-30 21:20:34 -04002254 __kvm_unmap_gfn(vcpu->kvm, gfn_to_memslot(vcpu->kvm, map->gfn), map,
Boris Ostrovsky91724812019-12-05 01:30:51 +00002255 cache, dirty, atomic);
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002256 return 0;
2257}
2258EXPORT_SYMBOL_GPL(kvm_unmap_gfn);
2259
2260void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty)
2261{
Peter Xu28bd7262020-09-30 21:20:34 -04002262 __kvm_unmap_gfn(vcpu->kvm, kvm_vcpu_gfn_to_memslot(vcpu, map->gfn),
2263 map, NULL, dirty, false);
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002264}
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002265EXPORT_SYMBOL_GPL(kvm_vcpu_unmap);
2266
Paolo Bonzini8e734852015-05-17 13:58:53 +02002267struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2268{
Dan Williamsba049e92016-01-15 16:56:11 -08002269 kvm_pfn_t pfn;
Paolo Bonzini8e734852015-05-17 13:58:53 +02002270
2271 pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn);
2272
2273 return kvm_pfn_to_page(pfn);
2274}
2275EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page);
2276
Izik Eidusb4231d62007-11-20 11:49:33 +02002277void kvm_release_page_clean(struct page *page)
2278{
Xiao Guangrong32cad842012-08-03 15:42:52 +08002279 WARN_ON(is_error_page(page));
2280
Anthony Liguori35149e22008-04-02 14:46:56 -05002281 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02002282}
2283EXPORT_SYMBOL_GPL(kvm_release_page_clean);
2284
Dan Williamsba049e92016-01-15 16:56:11 -08002285void kvm_release_pfn_clean(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002286{
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002287 if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002288 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002289}
2290EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
2291
Izik Eidusb4231d62007-11-20 11:49:33 +02002292void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02002293{
Xiao Guangronga2766322012-07-26 11:58:59 +08002294 WARN_ON(is_error_page(page));
2295
Anthony Liguori35149e22008-04-02 14:46:56 -05002296 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02002297}
Izik Eidusb4231d62007-11-20 11:49:33 +02002298EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02002299
David Hildenbrandf7a65092017-09-01 17:11:43 +02002300void kvm_release_pfn_dirty(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002301{
2302 kvm_set_pfn_dirty(pfn);
2303 kvm_release_pfn_clean(pfn);
2304}
David Hildenbrandf7a65092017-09-01 17:11:43 +02002305EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
Anthony Liguori35149e22008-04-02 14:46:56 -05002306
Dan Williamsba049e92016-01-15 16:56:11 -08002307void kvm_set_pfn_dirty(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002308{
Miaohe Lind29c03a2019-12-05 11:05:05 +08002309 if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn))
2310 SetPageDirty(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002311}
2312EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
2313
Dan Williamsba049e92016-01-15 16:56:11 -08002314void kvm_set_pfn_accessed(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002315{
Sean Christophersona78986a2019-11-11 14:12:27 -08002316 if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002317 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002318}
2319EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
2320
Dan Williamsba049e92016-01-15 16:56:11 -08002321void kvm_get_pfn(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002322{
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002323 if (!kvm_is_reserved_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002324 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002325}
2326EXPORT_SYMBOL_GPL(kvm_get_pfn);
2327
Izik Eidus195aefd2007-10-01 22:14:18 +02002328static int next_segment(unsigned long len, int offset)
2329{
2330 if (len > PAGE_SIZE - offset)
2331 return PAGE_SIZE - offset;
2332 else
2333 return len;
2334}
2335
Paolo Bonzini8e734852015-05-17 13:58:53 +02002336static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn,
2337 void *data, int offset, int len)
Izik Eidus195aefd2007-10-01 22:14:18 +02002338{
Izik Eiduse0506bc2007-11-11 22:10:22 +02002339 int r;
2340 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02002341
Paolo Bonzini8e734852015-05-17 13:58:53 +02002342 addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002343 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02002344 return -EFAULT;
Paolo Bonzini3180a7f2015-04-02 14:08:20 +02002345 r = __copy_from_user(data, (void __user *)addr + offset, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002346 if (r)
2347 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002348 return 0;
2349}
Paolo Bonzini8e734852015-05-17 13:58:53 +02002350
2351int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
2352 int len)
2353{
2354 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
2355
2356 return __kvm_read_guest_page(slot, gfn, data, offset, len);
2357}
Izik Eidus195aefd2007-10-01 22:14:18 +02002358EXPORT_SYMBOL_GPL(kvm_read_guest_page);
2359
Paolo Bonzini8e734852015-05-17 13:58:53 +02002360int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data,
2361 int offset, int len)
2362{
2363 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2364
2365 return __kvm_read_guest_page(slot, gfn, data, offset, len);
2366}
2367EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page);
2368
Izik Eidus195aefd2007-10-01 22:14:18 +02002369int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
2370{
2371 gfn_t gfn = gpa >> PAGE_SHIFT;
2372 int seg;
2373 int offset = offset_in_page(gpa);
2374 int ret;
2375
2376 while ((seg = next_segment(len, offset)) != 0) {
2377 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
2378 if (ret < 0)
2379 return ret;
2380 offset = 0;
2381 len -= seg;
2382 data += seg;
2383 ++gfn;
2384 }
2385 return 0;
2386}
2387EXPORT_SYMBOL_GPL(kvm_read_guest);
2388
Paolo Bonzini8e734852015-05-17 13:58:53 +02002389int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, unsigned long len)
2390{
2391 gfn_t gfn = gpa >> PAGE_SHIFT;
2392 int seg;
2393 int offset = offset_in_page(gpa);
2394 int ret;
2395
2396 while ((seg = next_segment(len, offset)) != 0) {
2397 ret = kvm_vcpu_read_guest_page(vcpu, gfn, data, offset, seg);
2398 if (ret < 0)
2399 return ret;
2400 offset = 0;
2401 len -= seg;
2402 data += seg;
2403 ++gfn;
2404 }
2405 return 0;
2406}
2407EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest);
2408
2409static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
2410 void *data, int offset, unsigned long len)
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002411{
2412 int r;
2413 unsigned long addr;
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002414
Paolo Bonzini8e734852015-05-17 13:58:53 +02002415 addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002416 if (kvm_is_error_hva(addr))
2417 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01002418 pagefault_disable();
Paolo Bonzini3180a7f2015-04-02 14:08:20 +02002419 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01002420 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002421 if (r)
2422 return -EFAULT;
2423 return 0;
2424}
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002425
Paolo Bonzini8e734852015-05-17 13:58:53 +02002426int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa,
2427 void *data, unsigned long len)
2428{
2429 gfn_t gfn = gpa >> PAGE_SHIFT;
2430 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2431 int offset = offset_in_page(gpa);
2432
2433 return __kvm_read_guest_atomic(slot, gfn, data, offset, len);
2434}
2435EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic);
2436
Peter Xu28bd7262020-09-30 21:20:34 -04002437static int __kvm_write_guest_page(struct kvm *kvm,
2438 struct kvm_memory_slot *memslot, gfn_t gfn,
Paolo Bonzini8e734852015-05-17 13:58:53 +02002439 const void *data, int offset, int len)
Izik Eidus195aefd2007-10-01 22:14:18 +02002440{
Izik Eiduse0506bc2007-11-11 22:10:22 +02002441 int r;
2442 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02002443
Radim Krčmář251eb842015-04-10 21:47:27 +02002444 addr = gfn_to_hva_memslot(memslot, gfn);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002445 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02002446 return -EFAULT;
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08002447 r = __copy_to_user((void __user *)addr + offset, data, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002448 if (r)
2449 return -EFAULT;
Peter Xu28bd7262020-09-30 21:20:34 -04002450 mark_page_dirty_in_slot(kvm, memslot, gfn);
Izik Eidus195aefd2007-10-01 22:14:18 +02002451 return 0;
2452}
Paolo Bonzini8e734852015-05-17 13:58:53 +02002453
2454int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn,
2455 const void *data, int offset, int len)
2456{
2457 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
2458
Peter Xu28bd7262020-09-30 21:20:34 -04002459 return __kvm_write_guest_page(kvm, slot, gfn, data, offset, len);
Paolo Bonzini8e734852015-05-17 13:58:53 +02002460}
Izik Eidus195aefd2007-10-01 22:14:18 +02002461EXPORT_SYMBOL_GPL(kvm_write_guest_page);
2462
Paolo Bonzini8e734852015-05-17 13:58:53 +02002463int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
2464 const void *data, int offset, int len)
2465{
2466 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2467
Peter Xu28bd7262020-09-30 21:20:34 -04002468 return __kvm_write_guest_page(vcpu->kvm, slot, gfn, data, offset, len);
Paolo Bonzini8e734852015-05-17 13:58:53 +02002469}
2470EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page);
2471
Izik Eidus195aefd2007-10-01 22:14:18 +02002472int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
2473 unsigned long len)
2474{
2475 gfn_t gfn = gpa >> PAGE_SHIFT;
2476 int seg;
2477 int offset = offset_in_page(gpa);
2478 int ret;
2479
2480 while ((seg = next_segment(len, offset)) != 0) {
2481 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
2482 if (ret < 0)
2483 return ret;
2484 offset = 0;
2485 len -= seg;
2486 data += seg;
2487 ++gfn;
2488 }
2489 return 0;
2490}
Wincy Vanff651cb2014-12-11 08:52:58 +03002491EXPORT_SYMBOL_GPL(kvm_write_guest);
Izik Eidus195aefd2007-10-01 22:14:18 +02002492
Paolo Bonzini8e734852015-05-17 13:58:53 +02002493int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
2494 unsigned long len)
2495{
2496 gfn_t gfn = gpa >> PAGE_SHIFT;
2497 int seg;
2498 int offset = offset_in_page(gpa);
2499 int ret;
2500
2501 while ((seg = next_segment(len, offset)) != 0) {
2502 ret = kvm_vcpu_write_guest_page(vcpu, gfn, data, offset, seg);
2503 if (ret < 0)
2504 return ret;
2505 offset = 0;
2506 len -= seg;
2507 data += seg;
2508 ++gfn;
2509 }
2510 return 0;
2511}
2512EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest);
2513
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002514static int __kvm_gfn_to_hva_cache_init(struct kvm_memslots *slots,
2515 struct gfn_to_hva_cache *ghc,
2516 gpa_t gpa, unsigned long len)
Gleb Natapov49c77542010-10-18 15:22:23 +02002517{
Gleb Natapov49c77542010-10-18 15:22:23 +02002518 int offset = offset_in_page(gpa);
Andrew Honig8f964522013-03-29 09:35:21 -07002519 gfn_t start_gfn = gpa >> PAGE_SHIFT;
2520 gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
2521 gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
2522 gfn_t nr_pages_avail;
Gleb Natapov49c77542010-10-18 15:22:23 +02002523
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002524 /* Update ghc->generation before performing any error checks. */
Gleb Natapov49c77542010-10-18 15:22:23 +02002525 ghc->generation = slots->generation;
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002526
2527 if (start_gfn > end_gfn) {
2528 ghc->hva = KVM_HVA_ERR_BAD;
2529 return -EINVAL;
2530 }
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002531
2532 /*
2533 * If the requested region crosses two memslots, we still
2534 * verify that the entire region is valid here.
2535 */
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002536 for ( ; start_gfn <= end_gfn; start_gfn += nr_pages_avail) {
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002537 ghc->memslot = __gfn_to_memslot(slots, start_gfn);
2538 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
2539 &nr_pages_avail);
2540 if (kvm_is_error_hva(ghc->hva))
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002541 return -EFAULT;
Andrew Honig8f964522013-03-29 09:35:21 -07002542 }
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002543
2544 /* Use the slow path for cross page reads and writes. */
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002545 if (nr_pages_needed == 1)
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002546 ghc->hva += offset;
2547 else
2548 ghc->memslot = NULL;
2549
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002550 ghc->gpa = gpa;
2551 ghc->len = len;
2552 return 0;
Gleb Natapov49c77542010-10-18 15:22:23 +02002553}
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002554
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002555int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002556 gpa_t gpa, unsigned long len)
2557{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002558 struct kvm_memslots *slots = kvm_memslots(kvm);
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002559 return __kvm_gfn_to_hva_cache_init(slots, ghc, gpa, len);
2560}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002561EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
Gleb Natapov49c77542010-10-18 15:22:23 +02002562
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002563int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
Jim Mattson7a86dab2018-12-14 14:34:43 -08002564 void *data, unsigned int offset,
2565 unsigned long len)
Gleb Natapov49c77542010-10-18 15:22:23 +02002566{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002567 struct kvm_memslots *slots = kvm_memslots(kvm);
Gleb Natapov49c77542010-10-18 15:22:23 +02002568 int r;
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002569 gpa_t gpa = ghc->gpa + offset;
Gleb Natapov49c77542010-10-18 15:22:23 +02002570
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002571 BUG_ON(len + offset > ghc->len);
Andrew Honig8f964522013-03-29 09:35:21 -07002572
Sean Christophersondc9ce712020-01-09 15:56:20 -08002573 if (slots->generation != ghc->generation) {
2574 if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len))
2575 return -EFAULT;
2576 }
Andrew Honig8f964522013-03-29 09:35:21 -07002577
Gleb Natapov49c77542010-10-18 15:22:23 +02002578 if (kvm_is_error_hva(ghc->hva))
2579 return -EFAULT;
2580
Sean Christophersonfcfbc612020-01-09 15:56:18 -08002581 if (unlikely(!ghc->memslot))
2582 return kvm_write_guest(kvm, gpa, data, len);
2583
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002584 r = __copy_to_user((void __user *)ghc->hva + offset, data, len);
Gleb Natapov49c77542010-10-18 15:22:23 +02002585 if (r)
2586 return -EFAULT;
Peter Xu28bd7262020-09-30 21:20:34 -04002587 mark_page_dirty_in_slot(kvm, ghc->memslot, gpa >> PAGE_SHIFT);
Gleb Natapov49c77542010-10-18 15:22:23 +02002588
2589 return 0;
2590}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002591EXPORT_SYMBOL_GPL(kvm_write_guest_offset_cached);
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002592
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002593int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
2594 void *data, unsigned long len)
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002595{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002596 return kvm_write_guest_offset_cached(kvm, ghc, data, 0, len);
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002597}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002598EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
Gleb Natapov49c77542010-10-18 15:22:23 +02002599
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002600int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
2601 void *data, unsigned int offset,
2602 unsigned long len)
Gleb Natapove03b6442011-07-11 15:28:11 -04002603{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002604 struct kvm_memslots *slots = kvm_memslots(kvm);
Gleb Natapove03b6442011-07-11 15:28:11 -04002605 int r;
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002606 gpa_t gpa = ghc->gpa + offset;
Gleb Natapove03b6442011-07-11 15:28:11 -04002607
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002608 BUG_ON(len + offset > ghc->len);
Andrew Honig8f964522013-03-29 09:35:21 -07002609
Sean Christophersondc9ce712020-01-09 15:56:20 -08002610 if (slots->generation != ghc->generation) {
2611 if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len))
2612 return -EFAULT;
2613 }
Andrew Honig8f964522013-03-29 09:35:21 -07002614
Gleb Natapove03b6442011-07-11 15:28:11 -04002615 if (kvm_is_error_hva(ghc->hva))
2616 return -EFAULT;
2617
Sean Christophersonfcfbc612020-01-09 15:56:18 -08002618 if (unlikely(!ghc->memslot))
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002619 return kvm_read_guest(kvm, gpa, data, len);
Sean Christophersonfcfbc612020-01-09 15:56:18 -08002620
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002621 r = __copy_from_user(data, (void __user *)ghc->hva + offset, len);
Gleb Natapove03b6442011-07-11 15:28:11 -04002622 if (r)
2623 return -EFAULT;
2624
2625 return 0;
2626}
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002627EXPORT_SYMBOL_GPL(kvm_read_guest_offset_cached);
2628
2629int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
2630 void *data, unsigned long len)
2631{
2632 return kvm_read_guest_offset_cached(kvm, ghc, data, 0, len);
2633}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002634EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
Gleb Natapove03b6442011-07-11 15:28:11 -04002635
Izik Eidus195aefd2007-10-01 22:14:18 +02002636int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
2637{
Paolo Bonzini2f541442020-11-06 05:25:09 -05002638 const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
Izik Eidus195aefd2007-10-01 22:14:18 +02002639 gfn_t gfn = gpa >> PAGE_SHIFT;
2640 int seg;
2641 int offset = offset_in_page(gpa);
2642 int ret;
2643
Kevin Mulveybfda0e82015-02-20 08:21:36 -05002644 while ((seg = next_segment(len, offset)) != 0) {
Paolo Bonzini2f541442020-11-06 05:25:09 -05002645 ret = kvm_write_guest_page(kvm, gfn, zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02002646 if (ret < 0)
2647 return ret;
2648 offset = 0;
2649 len -= seg;
2650 ++gfn;
2651 }
2652 return 0;
2653}
2654EXPORT_SYMBOL_GPL(kvm_clear_guest);
2655
Peter Xu28bd7262020-09-30 21:20:34 -04002656void mark_page_dirty_in_slot(struct kvm *kvm,
2657 struct kvm_memory_slot *memslot,
2658 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659{
Rusty Russell7e9d6192007-07-31 20:41:14 +10002660 if (memslot && memslot->dirty_bitmap) {
2661 unsigned long rel_gfn = gfn - memslot->base_gfn;
Peter Xufb04a1e2020-09-30 21:22:22 -04002662 u32 slot = (memslot->as_id << 16) | memslot->id;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663
Peter Xufb04a1e2020-09-30 21:22:22 -04002664 if (kvm->dirty_ring_size)
2665 kvm_dirty_ring_push(kvm_dirty_ring_get(kvm),
2666 slot, rel_gfn);
2667 else
2668 set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 }
2670}
Ben Gardona6a0b052020-10-14 11:26:55 -07002671EXPORT_SYMBOL_GPL(mark_page_dirty_in_slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672
Gleb Natapov49c77542010-10-18 15:22:23 +02002673void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
2674{
2675 struct kvm_memory_slot *memslot;
2676
2677 memslot = gfn_to_memslot(kvm, gfn);
Peter Xu28bd7262020-09-30 21:20:34 -04002678 mark_page_dirty_in_slot(kvm, memslot, gfn);
Gleb Natapov49c77542010-10-18 15:22:23 +02002679}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302680EXPORT_SYMBOL_GPL(mark_page_dirty);
Gleb Natapov49c77542010-10-18 15:22:23 +02002681
Paolo Bonzini8e734852015-05-17 13:58:53 +02002682void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn)
2683{
2684 struct kvm_memory_slot *memslot;
2685
2686 memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Peter Xu28bd7262020-09-30 21:20:34 -04002687 mark_page_dirty_in_slot(vcpu->kvm, memslot, gfn);
Paolo Bonzini8e734852015-05-17 13:58:53 +02002688}
2689EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty);
2690
Jan H. Schönherr20b70352017-11-24 22:39:01 +01002691void kvm_sigset_activate(struct kvm_vcpu *vcpu)
2692{
2693 if (!vcpu->sigset_active)
2694 return;
2695
2696 /*
2697 * This does a lockless modification of ->real_blocked, which is fine
2698 * because, only current can change ->real_blocked and all readers of
2699 * ->real_blocked don't care as long ->real_blocked is always a subset
2700 * of ->blocked.
2701 */
2702 sigprocmask(SIG_SETMASK, &vcpu->sigset, &current->real_blocked);
2703}
2704
2705void kvm_sigset_deactivate(struct kvm_vcpu *vcpu)
2706{
2707 if (!vcpu->sigset_active)
2708 return;
2709
2710 sigprocmask(SIG_SETMASK, &current->real_blocked, NULL);
2711 sigemptyset(&current->real_blocked);
2712}
2713
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002714static void grow_halt_poll_ns(struct kvm_vcpu *vcpu)
2715{
Nir Weinerdee339b2019-01-27 12:17:16 +02002716 unsigned int old, val, grow, grow_start;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002717
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002718 old = val = vcpu->halt_poll_ns;
Nir Weinerdee339b2019-01-27 12:17:16 +02002719 grow_start = READ_ONCE(halt_poll_ns_grow_start);
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002720 grow = READ_ONCE(halt_poll_ns_grow);
Nir Weiner7fa08e72019-01-27 12:17:14 +02002721 if (!grow)
2722 goto out;
2723
Nir Weinerdee339b2019-01-27 12:17:16 +02002724 val *= grow;
2725 if (val < grow_start)
2726 val = grow_start;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002727
David Matlack313f6362016-03-08 16:19:44 -08002728 if (val > halt_poll_ns)
2729 val = halt_poll_ns;
2730
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002731 vcpu->halt_poll_ns = val;
Nir Weiner7fa08e72019-01-27 12:17:14 +02002732out:
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002733 trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old);
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002734}
2735
2736static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu)
2737{
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002738 unsigned int old, val, shrink;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002739
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002740 old = val = vcpu->halt_poll_ns;
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002741 shrink = READ_ONCE(halt_poll_ns_shrink);
2742 if (shrink == 0)
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002743 val = 0;
2744 else
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002745 val /= shrink;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002746
2747 vcpu->halt_poll_ns = val;
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002748 trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old);
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002749}
2750
Paolo Bonzinif7819512015-02-04 18:20:58 +01002751static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
2752{
Junaid Shahid50c28f22018-06-27 14:59:11 -07002753 int ret = -EINTR;
2754 int idx = srcu_read_lock(&vcpu->kvm->srcu);
2755
Paolo Bonzinif7819512015-02-04 18:20:58 +01002756 if (kvm_arch_vcpu_runnable(vcpu)) {
2757 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Junaid Shahid50c28f22018-06-27 14:59:11 -07002758 goto out;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002759 }
2760 if (kvm_cpu_has_pending_timer(vcpu))
Junaid Shahid50c28f22018-06-27 14:59:11 -07002761 goto out;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002762 if (signal_pending(current))
Junaid Shahid50c28f22018-06-27 14:59:11 -07002763 goto out;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002764
Junaid Shahid50c28f22018-06-27 14:59:11 -07002765 ret = 0;
2766out:
2767 srcu_read_unlock(&vcpu->kvm->srcu, idx);
2768 return ret;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002769}
2770
David Matlackcb953122020-05-08 11:22:40 -07002771static inline void
2772update_halt_poll_stats(struct kvm_vcpu *vcpu, u64 poll_ns, bool waited)
2773{
2774 if (waited)
2775 vcpu->stat.halt_poll_fail_ns += poll_ns;
2776 else
2777 vcpu->stat.halt_poll_success_ns += poll_ns;
2778}
2779
Eddie Dongb6958ce2007-07-18 12:15:21 +03002780/*
2781 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
2782 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05002783void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03002784{
David Matlackcb953122020-05-08 11:22:40 -07002785 ktime_t start, cur, poll_end;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002786 bool waited = false;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002787 u64 block_ns;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002788
Marc Zyngier07ab0f82019-08-02 11:37:09 +01002789 kvm_arch_vcpu_blocking(vcpu);
2790
David Matlackcb953122020-05-08 11:22:40 -07002791 start = cur = poll_end = ktime_get();
Christian Borntraegercdd6ad32019-03-05 05:30:01 -05002792 if (vcpu->halt_poll_ns && !kvm_arch_no_poll(vcpu)) {
Wanpeng Li19020f82015-09-03 22:07:37 +08002793 ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns);
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08002794
Paolo Bonzini62bea5b2015-09-15 18:27:57 +02002795 ++vcpu->stat.halt_attempted_poll;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002796 do {
2797 /*
2798 * This sets KVM_REQ_UNHALT if an interrupt
2799 * arrives.
2800 */
2801 if (kvm_vcpu_check_block(vcpu) < 0) {
2802 ++vcpu->stat.halt_successful_poll;
Christian Borntraeger3491caf2016-05-13 12:16:35 +02002803 if (!vcpu_valid_wakeup(vcpu))
2804 ++vcpu->stat.halt_poll_invalid;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002805 goto out;
2806 }
David Matlackcb953122020-05-08 11:22:40 -07002807 poll_end = cur = ktime_get();
Paolo Bonzinif7819512015-02-04 18:20:58 +01002808 } while (single_task_running() && ktime_before(cur, stop));
2809 }
Eddie Dongb6958ce2007-07-18 12:15:21 +03002810
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002811 prepare_to_rcuwait(&vcpu->wait);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03002812 for (;;) {
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002813 set_current_state(TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03002814
Paolo Bonzinif7819512015-02-04 18:20:58 +01002815 if (kvm_vcpu_check_block(vcpu) < 0)
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03002816 break;
2817
Paolo Bonzinif7819512015-02-04 18:20:58 +01002818 waited = true;
Eddie Dongb6958ce2007-07-18 12:15:21 +03002819 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03002820 }
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002821 finish_rcuwait(&vcpu->wait);
Paolo Bonzinif7819512015-02-04 18:20:58 +01002822 cur = ktime_get();
Paolo Bonzinif7819512015-02-04 18:20:58 +01002823out:
Marc Zyngier07ab0f82019-08-02 11:37:09 +01002824 kvm_arch_vcpu_unblocking(vcpu);
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002825 block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
2826
David Matlackcb953122020-05-08 11:22:40 -07002827 update_halt_poll_stats(
2828 vcpu, ktime_to_ns(ktime_sub(poll_end, start)), waited);
2829
Wanpeng Li44551b22019-09-29 09:06:56 +08002830 if (!kvm_arch_no_poll(vcpu)) {
2831 if (!vcpu_valid_wakeup(vcpu)) {
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002832 shrink_halt_poll_ns(vcpu);
David Matlackacd05782020-04-17 15:14:46 -07002833 } else if (vcpu->kvm->max_halt_poll_ns) {
Wanpeng Li44551b22019-09-29 09:06:56 +08002834 if (block_ns <= vcpu->halt_poll_ns)
2835 ;
2836 /* we had a long block, shrink polling */
David Matlackacd05782020-04-17 15:14:46 -07002837 else if (vcpu->halt_poll_ns &&
2838 block_ns > vcpu->kvm->max_halt_poll_ns)
Wanpeng Li44551b22019-09-29 09:06:56 +08002839 shrink_halt_poll_ns(vcpu);
2840 /* we had a short halt and our poll time is too small */
David Matlackacd05782020-04-17 15:14:46 -07002841 else if (vcpu->halt_poll_ns < vcpu->kvm->max_halt_poll_ns &&
2842 block_ns < vcpu->kvm->max_halt_poll_ns)
Wanpeng Li44551b22019-09-29 09:06:56 +08002843 grow_halt_poll_ns(vcpu);
2844 } else {
2845 vcpu->halt_poll_ns = 0;
2846 }
2847 }
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002848
Christian Borntraeger3491caf2016-05-13 12:16:35 +02002849 trace_kvm_vcpu_wakeup(block_ns, waited, vcpu_valid_wakeup(vcpu));
2850 kvm_arch_vcpu_block_finish(vcpu);
Eddie Dongb6958ce2007-07-18 12:15:21 +03002851}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302852EXPORT_SYMBOL_GPL(kvm_vcpu_block);
Eddie Dongb6958ce2007-07-18 12:15:21 +03002853
Radim Krčmář178f02f2017-04-26 22:32:26 +02002854bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
Christoffer Dallb6d33832012-03-08 16:44:24 -05002855{
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002856 struct rcuwait *waitp;
Christoffer Dallb6d33832012-03-08 16:44:24 -05002857
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002858 waitp = kvm_arch_vcpu_get_wait(vcpu);
2859 if (rcuwait_wake_up(waitp)) {
Wanpeng Lid73eb572019-07-18 19:39:06 +08002860 WRITE_ONCE(vcpu->ready, true);
Christoffer Dallb6d33832012-03-08 16:44:24 -05002861 ++vcpu->stat.halt_wakeup;
Radim Krčmář178f02f2017-04-26 22:32:26 +02002862 return true;
Christoffer Dallb6d33832012-03-08 16:44:24 -05002863 }
2864
Radim Krčmář178f02f2017-04-26 22:32:26 +02002865 return false;
Radim Krčmářdd1a4cc2016-05-04 14:09:44 -05002866}
2867EXPORT_SYMBOL_GPL(kvm_vcpu_wake_up);
2868
Paolo Bonzini0266c892017-05-04 15:14:13 +02002869#ifndef CONFIG_S390
Radim Krčmářdd1a4cc2016-05-04 14:09:44 -05002870/*
2871 * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
2872 */
2873void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
2874{
2875 int me;
2876 int cpu = vcpu->cpu;
2877
Radim Krčmář178f02f2017-04-26 22:32:26 +02002878 if (kvm_vcpu_wake_up(vcpu))
2879 return;
2880
Christoffer Dallb6d33832012-03-08 16:44:24 -05002881 me = get_cpu();
2882 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
2883 if (kvm_arch_vcpu_should_kick(vcpu))
2884 smp_send_reschedule(cpu);
2885 put_cpu();
2886}
Yang Zhanga20ed542013-04-11 19:25:15 +08002887EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
Paolo Bonzini0266c892017-05-04 15:14:13 +02002888#endif /* !CONFIG_S390 */
Christoffer Dallb6d33832012-03-08 16:44:24 -05002889
Dan Carpenterfa933842014-05-23 13:20:42 +03002890int kvm_vcpu_yield_to(struct kvm_vcpu *target)
Konstantin Weitz41628d32012-04-25 15:30:38 +02002891{
2892 struct pid *pid;
2893 struct task_struct *task = NULL;
Dan Carpenterfa933842014-05-23 13:20:42 +03002894 int ret = 0;
Konstantin Weitz41628d32012-04-25 15:30:38 +02002895
2896 rcu_read_lock();
2897 pid = rcu_dereference(target->pid);
2898 if (pid)
Sam Bobroff27fbe64b2014-09-19 09:40:41 +10002899 task = get_pid_task(pid, PIDTYPE_PID);
Konstantin Weitz41628d32012-04-25 15:30:38 +02002900 rcu_read_unlock();
2901 if (!task)
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302902 return ret;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302903 ret = yield_to(task, 1);
Konstantin Weitz41628d32012-04-25 15:30:38 +02002904 put_task_struct(task);
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302905
2906 return ret;
Konstantin Weitz41628d32012-04-25 15:30:38 +02002907}
2908EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
2909
Raghavendra K T06e48c52012-07-19 15:17:52 +05302910/*
2911 * Helper that checks whether a VCPU is eligible for directed yield.
2912 * Most eligible candidate to yield is decided by following heuristics:
2913 *
2914 * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
2915 * (preempted lock holder), indicated by @in_spin_loop.
Fuad Tabba656012c2020-04-01 15:03:10 +01002916 * Set at the beginning and cleared at the end of interception/PLE handler.
Raghavendra K T06e48c52012-07-19 15:17:52 +05302917 *
2918 * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
2919 * chance last time (mostly it has become eligible now since we have probably
2920 * yielded to lockholder in last iteration. This is done by toggling
2921 * @dy_eligible each time a VCPU checked for eligibility.)
2922 *
2923 * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
2924 * to preempted lock-holder could result in wrong VCPU selection and CPU
2925 * burning. Giving priority for a potential lock-holder increases lock
2926 * progress.
2927 *
2928 * Since algorithm is based on heuristics, accessing another VCPU data without
2929 * locking does not harm. It may result in trying to yield to same VCPU, fail
2930 * and continue with next VCPU and so on.
2931 */
Stephen Hemminger79408762013-12-29 12:12:29 -08002932static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
Raghavendra K T06e48c52012-07-19 15:17:52 +05302933{
Scott Wood4a55dd72014-01-09 18:43:16 -06002934#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
Raghavendra K T06e48c52012-07-19 15:17:52 +05302935 bool eligible;
2936
2937 eligible = !vcpu->spin_loop.in_spin_loop ||
Christian Borntraeger34656112014-09-04 21:13:31 +02002938 vcpu->spin_loop.dy_eligible;
Raghavendra K T06e48c52012-07-19 15:17:52 +05302939
2940 if (vcpu->spin_loop.in_spin_loop)
2941 kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
2942
2943 return eligible;
Scott Wood4a55dd72014-01-09 18:43:16 -06002944#else
2945 return true;
Raghavendra K T06e48c52012-07-19 15:17:52 +05302946#endif
Scott Wood4a55dd72014-01-09 18:43:16 -06002947}
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302948
Wanpeng Li17e433b2019-08-05 10:03:19 +08002949/*
2950 * Unlike kvm_arch_vcpu_runnable, this function is called outside
2951 * a vcpu_load/vcpu_put pair. However, for most architectures
2952 * kvm_arch_vcpu_runnable does not require vcpu_load.
2953 */
2954bool __weak kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
2955{
2956 return kvm_arch_vcpu_runnable(vcpu);
2957}
2958
2959static bool vcpu_dy_runnable(struct kvm_vcpu *vcpu)
2960{
2961 if (kvm_arch_dy_runnable(vcpu))
2962 return true;
2963
2964#ifdef CONFIG_KVM_ASYNC_PF
2965 if (!list_empty_careful(&vcpu->async_pf.done))
2966 return true;
2967#endif
2968
2969 return false;
2970}
2971
Longpeng(Mike)199b5762017-08-08 12:05:32 +08002972void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08002973{
Rik van Riel217ece62011-02-01 09:53:28 -05002974 struct kvm *kvm = me->kvm;
2975 struct kvm_vcpu *vcpu;
2976 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
2977 int yielded = 0;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302978 int try = 3;
Rik van Riel217ece62011-02-01 09:53:28 -05002979 int pass;
2980 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08002981
Raghavendra K T4c088492012-07-18 19:07:46 +05302982 kvm_vcpu_set_in_spin_loop(me, true);
Rik van Riel217ece62011-02-01 09:53:28 -05002983 /*
2984 * We boost the priority of a VCPU that is runnable but not
2985 * currently running, because it got preempted by something
2986 * else and called schedule in __vcpu_run. Hopefully that
2987 * VCPU is holding the lock that we need and will release it.
2988 * We approximate round-robin by starting at the last boosted VCPU.
2989 */
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302990 for (pass = 0; pass < 2 && !yielded && try; pass++) {
Rik van Riel217ece62011-02-01 09:53:28 -05002991 kvm_for_each_vcpu(i, vcpu, kvm) {
Rik van Riel5cfc2aa2012-06-19 16:51:04 -04002992 if (!pass && i <= last_boosted_vcpu) {
Rik van Riel217ece62011-02-01 09:53:28 -05002993 i = last_boosted_vcpu;
2994 continue;
2995 } else if (pass && i > last_boosted_vcpu)
2996 break;
Wanpeng Lid73eb572019-07-18 19:39:06 +08002997 if (!READ_ONCE(vcpu->ready))
Raghavendra K T7bc7ae22013-03-04 23:32:27 +05302998 continue;
Rik van Riel217ece62011-02-01 09:53:28 -05002999 if (vcpu == me)
3000 continue;
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07003001 if (rcuwait_active(&vcpu->wait) &&
3002 !vcpu_dy_runnable(vcpu))
Rik van Riel217ece62011-02-01 09:53:28 -05003003 continue;
Wanpeng Li046ddee2019-08-01 11:30:14 +08003004 if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode &&
3005 !kvm_arch_vcpu_in_kernel(vcpu))
Longpeng(Mike)199b5762017-08-08 12:05:32 +08003006 continue;
Raghavendra K T06e48c52012-07-19 15:17:52 +05303007 if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
3008 continue;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05303009
3010 yielded = kvm_vcpu_yield_to(vcpu);
3011 if (yielded > 0) {
Rik van Riel217ece62011-02-01 09:53:28 -05003012 kvm->last_boosted_vcpu = i;
Rik van Riel217ece62011-02-01 09:53:28 -05003013 break;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05303014 } else if (yielded < 0) {
3015 try--;
3016 if (!try)
3017 break;
Rik van Riel217ece62011-02-01 09:53:28 -05003018 }
Rik van Riel217ece62011-02-01 09:53:28 -05003019 }
3020 }
Raghavendra K T4c088492012-07-18 19:07:46 +05303021 kvm_vcpu_set_in_spin_loop(me, false);
Raghavendra K T06e48c52012-07-19 15:17:52 +05303022
3023 /* Ensure vcpu is not eligible during next spinloop */
3024 kvm_vcpu_set_dy_eligible(me, false);
Zhai, Edwind255f4f2009-10-09 18:03:20 +08003025}
3026EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
3027
Peter Xufb04a1e2020-09-30 21:22:22 -04003028static bool kvm_page_in_dirty_ring(struct kvm *kvm, unsigned long pgoff)
3029{
3030#if KVM_DIRTY_LOG_PAGE_OFFSET > 0
3031 return (pgoff >= KVM_DIRTY_LOG_PAGE_OFFSET) &&
3032 (pgoff < KVM_DIRTY_LOG_PAGE_OFFSET +
3033 kvm->dirty_ring_size / PAGE_SIZE);
3034#else
3035 return false;
3036#endif
3037}
3038
Souptick Joarder1499fa82018-04-19 00:49:58 +05303039static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003040{
Dave Jiang11bac802017-02-24 14:56:41 -08003041 struct kvm_vcpu *vcpu = vmf->vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003042 struct page *page;
3043
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11003044 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02003045 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02003046#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11003047 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003048 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02003049#endif
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003050#ifdef CONFIG_KVM_MMIO
Laurent Vivier5f94c172008-05-30 16:05:54 +02003051 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
3052 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
3053#endif
Peter Xufb04a1e2020-09-30 21:22:22 -04003054 else if (kvm_page_in_dirty_ring(vcpu->kvm, vmf->pgoff))
3055 page = kvm_dirty_ring_get_page(
3056 &vcpu->dirty_ring,
3057 vmf->pgoff - KVM_DIRTY_LOG_PAGE_OFFSET);
Avi Kivity039576c2007-03-20 12:46:50 +02003058 else
Carsten Otte5b1c1492012-01-04 10:25:23 +01003059 return kvm_arch_vcpu_fault(vcpu, vmf);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003060 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11003061 vmf->page = page;
3062 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003063}
3064
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003065static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11003066 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003067};
3068
3069static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
3070{
Peter Xufb04a1e2020-09-30 21:22:22 -04003071 struct kvm_vcpu *vcpu = file->private_data;
3072 unsigned long pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
3073
3074 if ((kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff) ||
3075 kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff + pages - 1)) &&
3076 ((vma->vm_flags & VM_EXEC) || !(vma->vm_flags & VM_SHARED)))
3077 return -EINVAL;
3078
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003079 vma->vm_ops = &kvm_vcpu_vm_ops;
3080 return 0;
3081}
3082
Avi Kivitybccf2152007-02-21 18:04:26 +02003083static int kvm_vcpu_release(struct inode *inode, struct file *filp)
3084{
3085 struct kvm_vcpu *vcpu = filp->private_data;
3086
Al Viro66c0b392008-04-19 20:33:56 +01003087 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02003088 return 0;
3089}
3090
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01003091static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02003092 .release = kvm_vcpu_release,
3093 .unlocked_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003094 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003095 .llseek = noop_llseek,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01003096 KVM_COMPAT(kvm_vcpu_compat_ioctl),
Avi Kivitybccf2152007-02-21 18:04:26 +02003097};
3098
3099/*
3100 * Allocates an inode for the vcpu.
3101 */
3102static int create_vcpu_fd(struct kvm_vcpu *vcpu)
3103{
Masatake YAMATOe46b4692018-01-20 04:04:22 +09003104 char name[8 + 1 + ITOA_MAX_LEN + 1];
3105
3106 snprintf(name, sizeof(name), "kvm-vcpu:%d", vcpu->vcpu_id);
3107 return anon_inode_getfd(name, &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC);
Avi Kivitybccf2152007-02-21 18:04:26 +02003108}
3109
Greg KH3e7093d2019-07-31 20:56:20 +02003110static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
Luiz Capitulino45b59392016-09-16 10:27:35 -04003111{
Paolo Bonzini741cbba2019-08-03 08:14:25 +02003112#ifdef __KVM_HAVE_ARCH_VCPU_DEBUGFS
Paolo Bonzinid56f5132020-06-04 15:16:52 +02003113 struct dentry *debugfs_dentry;
Luiz Capitulino45b59392016-09-16 10:27:35 -04003114 char dir_name[ITOA_MAX_LEN * 2];
Luiz Capitulino45b59392016-09-16 10:27:35 -04003115
Luiz Capitulino45b59392016-09-16 10:27:35 -04003116 if (!debugfs_initialized())
Greg KH3e7093d2019-07-31 20:56:20 +02003117 return;
Luiz Capitulino45b59392016-09-16 10:27:35 -04003118
3119 snprintf(dir_name, sizeof(dir_name), "vcpu%d", vcpu->vcpu_id);
Paolo Bonzinid56f5132020-06-04 15:16:52 +02003120 debugfs_dentry = debugfs_create_dir(dir_name,
3121 vcpu->kvm->debugfs_dentry);
Luiz Capitulino45b59392016-09-16 10:27:35 -04003122
Paolo Bonzinid56f5132020-06-04 15:16:52 +02003123 kvm_arch_create_vcpu_debugfs(vcpu, debugfs_dentry);
Paolo Bonzini741cbba2019-08-03 08:14:25 +02003124#endif
Luiz Capitulino45b59392016-09-16 10:27:35 -04003125}
3126
Avi Kivityc5ea7662007-02-20 18:41:05 +02003127/*
3128 * Creates some virtual cpus. Good luck creating more than one.
3129 */
Gleb Natapov73880c82009-06-09 15:56:28 +03003130static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02003131{
3132 int r;
David Hildenbrande09fefd2015-11-05 09:03:50 +01003133 struct kvm_vcpu *vcpu;
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003134 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02003135
Greg Kurz0b1b1df2016-05-09 18:13:37 +02003136 if (id >= KVM_MAX_VCPU_ID)
Andy Honig338c7db2013-11-18 16:09:22 -08003137 return -EINVAL;
3138
Paolo Bonzini6c7caeb2016-06-13 14:48:25 +02003139 mutex_lock(&kvm->lock);
3140 if (kvm->created_vcpus == KVM_MAX_VCPUS) {
3141 mutex_unlock(&kvm->lock);
3142 return -EINVAL;
3143 }
3144
3145 kvm->created_vcpus++;
3146 mutex_unlock(&kvm->lock);
3147
Sean Christopherson897cc382019-12-18 13:55:09 -08003148 r = kvm_arch_vcpu_precreate(kvm, id);
3149 if (r)
3150 goto vcpu_decrement;
3151
Sean Christophersone529ef62019-12-18 13:55:15 -08003152 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
3153 if (!vcpu) {
3154 r = -ENOMEM;
Paolo Bonzini6c7caeb2016-06-13 14:48:25 +02003155 goto vcpu_decrement;
3156 }
Avi Kivityc5ea7662007-02-20 18:41:05 +02003157
Peter Xufcd97ad2020-01-09 09:57:12 -05003158 BUILD_BUG_ON(sizeof(struct kvm_run) > PAGE_SIZE);
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003159 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3160 if (!page) {
3161 r = -ENOMEM;
Sean Christophersone529ef62019-12-18 13:55:15 -08003162 goto vcpu_free;
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003163 }
3164 vcpu->run = page_address(page);
3165
3166 kvm_vcpu_init(vcpu, kvm, id);
Sean Christophersone529ef62019-12-18 13:55:15 -08003167
3168 r = kvm_arch_vcpu_create(vcpu);
3169 if (r)
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003170 goto vcpu_free_run_page;
Sean Christophersone529ef62019-12-18 13:55:15 -08003171
Peter Xufb04a1e2020-09-30 21:22:22 -04003172 if (kvm->dirty_ring_size) {
3173 r = kvm_dirty_ring_alloc(&vcpu->dirty_ring,
3174 id, kvm->dirty_ring_size);
3175 if (r)
3176 goto arch_vcpu_destroy;
3177 }
3178
Shaohua Li11ec2802007-07-23 14:51:37 +08003179 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +01003180 if (kvm_get_vcpu_by_id(kvm, id)) {
3181 r = -EEXIST;
3182 goto unlock_vcpu_destroy;
3183 }
Gleb Natapov73880c82009-06-09 15:56:28 +03003184
Radim Krčmář8750e722019-11-07 07:53:42 -05003185 vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
3186 BUG_ON(kvm->vcpus[vcpu->vcpu_idx]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003187
3188 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01003189 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02003190 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03003191 if (r < 0) {
Sean Christopherson149487b2019-10-21 15:58:42 -07003192 kvm_put_kvm_no_destroy(kvm);
Jan Kiszkad7805922011-05-23 10:33:05 +02003193 goto unlock_vcpu_destroy;
Gleb Natapov73880c82009-06-09 15:56:28 +03003194 }
3195
Radim Krčmář8750e722019-11-07 07:53:42 -05003196 kvm->vcpus[vcpu->vcpu_idx] = vcpu;
Paolo Bonzinidd489242015-07-29 11:32:20 +02003197
3198 /*
3199 * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus
3200 * before kvm->online_vcpu's incremented value.
3201 */
Gleb Natapov73880c82009-06-09 15:56:28 +03003202 smp_wmb();
3203 atomic_inc(&kvm->online_vcpus);
3204
Gleb Natapov73880c82009-06-09 15:56:28 +03003205 mutex_unlock(&kvm->lock);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02003206 kvm_arch_vcpu_postcreate(vcpu);
Paolo Bonzini63d04342020-04-01 00:42:22 +02003207 kvm_create_vcpu_debugfs(vcpu);
Avi Kivitybccf2152007-02-21 18:04:26 +02003208 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02003209
Jan Kiszkad7805922011-05-23 10:33:05 +02003210unlock_vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03003211 mutex_unlock(&kvm->lock);
Peter Xufb04a1e2020-09-30 21:22:22 -04003212 kvm_dirty_ring_free(&vcpu->dirty_ring);
3213arch_vcpu_destroy:
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06003214 kvm_arch_vcpu_destroy(vcpu);
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003215vcpu_free_run_page:
3216 free_page((unsigned long)vcpu->run);
Sean Christophersone529ef62019-12-18 13:55:15 -08003217vcpu_free:
3218 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paolo Bonzini6c7caeb2016-06-13 14:48:25 +02003219vcpu_decrement:
3220 mutex_lock(&kvm->lock);
3221 kvm->created_vcpus--;
3222 mutex_unlock(&kvm->lock);
Avi Kivityc5ea7662007-02-20 18:41:05 +02003223 return r;
3224}
3225
Avi Kivity1961d272007-03-05 19:46:05 +02003226static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
3227{
3228 if (sigset) {
3229 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3230 vcpu->sigset_active = 1;
3231 vcpu->sigset = *sigset;
3232 } else
3233 vcpu->sigset_active = 0;
3234 return 0;
3235}
3236
Avi Kivitybccf2152007-02-21 18:04:26 +02003237static long kvm_vcpu_ioctl(struct file *filp,
3238 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239{
Avi Kivitybccf2152007-02-21 18:04:26 +02003240 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00003241 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003242 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07003243 struct kvm_fpu *fpu = NULL;
3244 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245
Avi Kivity6d4e4c42007-11-21 16:41:05 +02003246 if (vcpu->kvm->mm != current->mm)
3247 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03003248
David Matlack2ea75be2014-09-19 16:03:25 -07003249 if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
3250 return -EINVAL;
3251
Avi Kivity2122ff52010-05-13 11:25:04 +03003252 /*
Paolo Bonzini5cb09442017-12-12 17:41:34 +01003253 * Some architectures have vcpu ioctls that are asynchronous to vcpu
3254 * execution; mutex_lock() would break them.
Avi Kivity2122ff52010-05-13 11:25:04 +03003255 */
Paolo Bonzini5cb09442017-12-12 17:41:34 +01003256 r = kvm_arch_vcpu_async_ioctl(filp, ioctl, arg);
3257 if (r != -ENOIOCTLCMD)
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03003258 return r;
Avi Kivity2122ff52010-05-13 11:25:04 +03003259
Christoffer Dallec7660c2017-12-04 21:35:23 +01003260 if (mutex_lock_killable(&vcpu->mutex))
3261 return -EINTR;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 switch (ioctl) {
Christian Borntraeger0e4524a2017-07-06 14:44:28 +02003263 case KVM_RUN: {
3264 struct pid *oldpid;
Avi Kivityf0fe5102007-03-07 13:11:17 +02003265 r = -EINVAL;
3266 if (arg)
3267 goto out;
Christian Borntraeger0e4524a2017-07-06 14:44:28 +02003268 oldpid = rcu_access_pointer(vcpu->pid);
Eric W. Biederman71dbc8a2017-07-16 21:39:32 -05003269 if (unlikely(oldpid != task_pid(current))) {
Christian Borntraeger7a72f7a2014-08-05 16:44:14 +02003270 /* The thread running this VCPU changed. */
Christoffer Dallbd2a6392018-02-23 17:23:57 +01003271 struct pid *newpid;
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08003272
Christoffer Dallbd2a6392018-02-23 17:23:57 +01003273 r = kvm_arch_vcpu_run_pid_change(vcpu);
3274 if (r)
3275 break;
3276
3277 newpid = get_task_pid(current, PIDTYPE_PID);
Christian Borntraeger7a72f7a2014-08-05 16:44:14 +02003278 rcu_assign_pointer(vcpu->pid, newpid);
3279 if (oldpid)
3280 synchronize_rcu();
3281 put_pid(oldpid);
3282 }
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08003283 r = kvm_arch_vcpu_ioctl_run(vcpu);
Gleb Natapov64be5002010-10-24 16:49:08 +02003284 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285 break;
Christian Borntraeger0e4524a2017-07-06 14:44:28 +02003286 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003288 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003289
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003290 r = -ENOMEM;
Ben Gardonb12ce362019-02-11 11:02:49 -08003291 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL_ACCOUNT);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003292 if (!kvm_regs)
3293 goto out;
3294 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003296 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003298 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
3299 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003300 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003301out_free1:
3302 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303 break;
3304 }
3305 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003306 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003307
Sasha Levinff5c2c02011-12-04 19:36:29 +02003308 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
3309 if (IS_ERR(kvm_regs)) {
3310 r = PTR_ERR(kvm_regs);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003311 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003312 }
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003313 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003314 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003315 break;
3316 }
3317 case KVM_GET_SREGS: {
Ben Gardonb12ce362019-02-11 11:02:49 -08003318 kvm_sregs = kzalloc(sizeof(struct kvm_sregs),
3319 GFP_KERNEL_ACCOUNT);
Dave Hansenfa3795a2008-08-11 10:01:46 -07003320 r = -ENOMEM;
3321 if (!kvm_sregs)
3322 goto out;
3323 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324 if (r)
3325 goto out;
3326 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07003327 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003328 goto out;
3329 r = 0;
3330 break;
3331 }
3332 case KVM_SET_SREGS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003333 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
3334 if (IS_ERR(kvm_sregs)) {
3335 r = PTR_ERR(kvm_sregs);
Guo Chao18595412012-11-02 18:33:21 +08003336 kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003337 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003338 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07003339 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 break;
3341 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003342 case KVM_GET_MP_STATE: {
3343 struct kvm_mp_state mp_state;
3344
3345 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
3346 if (r)
3347 goto out;
3348 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003349 if (copy_to_user(argp, &mp_state, sizeof(mp_state)))
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003350 goto out;
3351 r = 0;
3352 break;
3353 }
3354 case KVM_SET_MP_STATE: {
3355 struct kvm_mp_state mp_state;
3356
3357 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003358 if (copy_from_user(&mp_state, argp, sizeof(mp_state)))
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003359 goto out;
3360 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003361 break;
3362 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363 case KVM_TRANSLATE: {
3364 struct kvm_translation tr;
3365
3366 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003367 if (copy_from_user(&tr, argp, sizeof(tr)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08003369 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370 if (r)
3371 goto out;
3372 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003373 if (copy_to_user(argp, &tr, sizeof(tr)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374 goto out;
3375 r = 0;
3376 break;
3377 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003378 case KVM_SET_GUEST_DEBUG: {
3379 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380
3381 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003382 if (copy_from_user(&dbg, argp, sizeof(dbg)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003384 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 break;
3386 }
Avi Kivity1961d272007-03-05 19:46:05 +02003387 case KVM_SET_SIGNAL_MASK: {
3388 struct kvm_signal_mask __user *sigmask_arg = argp;
3389 struct kvm_signal_mask kvm_sigmask;
3390 sigset_t sigset, *p;
3391
3392 p = NULL;
3393 if (argp) {
3394 r = -EFAULT;
3395 if (copy_from_user(&kvm_sigmask, argp,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003396 sizeof(kvm_sigmask)))
Avi Kivity1961d272007-03-05 19:46:05 +02003397 goto out;
3398 r = -EINVAL;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003399 if (kvm_sigmask.len != sizeof(sigset))
Avi Kivity1961d272007-03-05 19:46:05 +02003400 goto out;
3401 r = -EFAULT;
3402 if (copy_from_user(&sigset, sigmask_arg->sigset,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003403 sizeof(sigset)))
Avi Kivity1961d272007-03-05 19:46:05 +02003404 goto out;
3405 p = &sigset;
3406 }
Andi Kleen376d41f2010-06-10 13:10:47 +02003407 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02003408 break;
3409 }
Avi Kivityb8836732007-04-01 16:34:31 +03003410 case KVM_GET_FPU: {
Ben Gardonb12ce362019-02-11 11:02:49 -08003411 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL_ACCOUNT);
Dave Hansenfa3795a2008-08-11 10:01:46 -07003412 r = -ENOMEM;
3413 if (!fpu)
3414 goto out;
3415 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03003416 if (r)
3417 goto out;
3418 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07003419 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03003420 goto out;
3421 r = 0;
3422 break;
3423 }
3424 case KVM_SET_FPU: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003425 fpu = memdup_user(argp, sizeof(*fpu));
3426 if (IS_ERR(fpu)) {
3427 r = PTR_ERR(fpu);
Guo Chao18595412012-11-02 18:33:21 +08003428 fpu = NULL;
Avi Kivityb8836732007-04-01 16:34:31 +03003429 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003430 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07003431 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03003432 break;
3433 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003434 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02003435 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02003436 }
3437out:
Christoffer Dallec7660c2017-12-04 21:35:23 +01003438 mutex_unlock(&vcpu->mutex);
Dave Hansenfa3795a2008-08-11 10:01:46 -07003439 kfree(fpu);
3440 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02003441 return r;
3442}
3443
Christian Borntraegerde8e5d72015-02-03 09:35:15 +01003444#ifdef CONFIG_KVM_COMPAT
Alexander Graf1dda6062011-06-08 02:45:37 +02003445static long kvm_vcpu_compat_ioctl(struct file *filp,
3446 unsigned int ioctl, unsigned long arg)
3447{
3448 struct kvm_vcpu *vcpu = filp->private_data;
3449 void __user *argp = compat_ptr(arg);
3450 int r;
3451
3452 if (vcpu->kvm->mm != current->mm)
3453 return -EIO;
3454
3455 switch (ioctl) {
3456 case KVM_SET_SIGNAL_MASK: {
3457 struct kvm_signal_mask __user *sigmask_arg = argp;
3458 struct kvm_signal_mask kvm_sigmask;
Alexander Graf1dda6062011-06-08 02:45:37 +02003459 sigset_t sigset;
3460
3461 if (argp) {
3462 r = -EFAULT;
3463 if (copy_from_user(&kvm_sigmask, argp,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003464 sizeof(kvm_sigmask)))
Alexander Graf1dda6062011-06-08 02:45:37 +02003465 goto out;
3466 r = -EINVAL;
Al Viro3968cf62017-09-03 21:45:17 -04003467 if (kvm_sigmask.len != sizeof(compat_sigset_t))
Alexander Graf1dda6062011-06-08 02:45:37 +02003468 goto out;
3469 r = -EFAULT;
Paolo Bonzini1393b4a2020-07-02 05:39:31 -04003470 if (get_compat_sigset(&sigset,
3471 (compat_sigset_t __user *)sigmask_arg->sigset))
Alexander Graf1dda6062011-06-08 02:45:37 +02003472 goto out;
Alan Cox760a9a32012-08-22 14:34:11 +01003473 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
3474 } else
3475 r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
Alexander Graf1dda6062011-06-08 02:45:37 +02003476 break;
3477 }
3478 default:
3479 r = kvm_vcpu_ioctl(filp, ioctl, arg);
3480 }
3481
3482out:
3483 return r;
3484}
3485#endif
3486
Cédric Le Goatera1cd3f02019-04-18 12:39:36 +02003487static int kvm_device_mmap(struct file *filp, struct vm_area_struct *vma)
3488{
3489 struct kvm_device *dev = filp->private_data;
3490
3491 if (dev->ops->mmap)
3492 return dev->ops->mmap(dev, vma);
3493
3494 return -ENODEV;
3495}
3496
Scott Wood852b6d52013-04-12 14:08:42 +00003497static int kvm_device_ioctl_attr(struct kvm_device *dev,
3498 int (*accessor)(struct kvm_device *dev,
3499 struct kvm_device_attr *attr),
3500 unsigned long arg)
3501{
3502 struct kvm_device_attr attr;
3503
3504 if (!accessor)
3505 return -EPERM;
3506
3507 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
3508 return -EFAULT;
3509
3510 return accessor(dev, &attr);
3511}
3512
3513static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
3514 unsigned long arg)
3515{
3516 struct kvm_device *dev = filp->private_data;
3517
Sean Christophersonddba9182019-02-15 12:48:39 -08003518 if (dev->kvm->mm != current->mm)
3519 return -EIO;
3520
Scott Wood852b6d52013-04-12 14:08:42 +00003521 switch (ioctl) {
3522 case KVM_SET_DEVICE_ATTR:
3523 return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
3524 case KVM_GET_DEVICE_ATTR:
3525 return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
3526 case KVM_HAS_DEVICE_ATTR:
3527 return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
3528 default:
3529 if (dev->ops->ioctl)
3530 return dev->ops->ioctl(dev, ioctl, arg);
3531
3532 return -ENOTTY;
3533 }
3534}
3535
Scott Wood852b6d52013-04-12 14:08:42 +00003536static int kvm_device_release(struct inode *inode, struct file *filp)
3537{
3538 struct kvm_device *dev = filp->private_data;
3539 struct kvm *kvm = dev->kvm;
3540
Cédric Le Goater2bde9b32019-04-18 12:39:41 +02003541 if (dev->ops->release) {
3542 mutex_lock(&kvm->lock);
3543 list_del(&dev->vm_node);
3544 dev->ops->release(dev);
3545 mutex_unlock(&kvm->lock);
3546 }
3547
Scott Wood852b6d52013-04-12 14:08:42 +00003548 kvm_put_kvm(kvm);
3549 return 0;
3550}
3551
3552static const struct file_operations kvm_device_fops = {
3553 .unlocked_ioctl = kvm_device_ioctl,
3554 .release = kvm_device_release,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01003555 KVM_COMPAT(kvm_device_ioctl),
Cédric Le Goatera1cd3f02019-04-18 12:39:36 +02003556 .mmap = kvm_device_mmap,
Scott Wood852b6d52013-04-12 14:08:42 +00003557};
3558
3559struct kvm_device *kvm_device_from_filp(struct file *filp)
3560{
3561 if (filp->f_op != &kvm_device_fops)
3562 return NULL;
3563
3564 return filp->private_data;
3565}
3566
Steven Price8538cb22019-10-21 16:28:19 +01003567static const struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
Will Deacond60eacb2014-09-02 10:27:33 +01003568#ifdef CONFIG_KVM_MPIC
3569 [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops,
3570 [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops,
3571#endif
Will Deacond60eacb2014-09-02 10:27:33 +01003572};
3573
Steven Price8538cb22019-10-21 16:28:19 +01003574int kvm_register_device_ops(const struct kvm_device_ops *ops, u32 type)
Will Deacond60eacb2014-09-02 10:27:33 +01003575{
3576 if (type >= ARRAY_SIZE(kvm_device_ops_table))
3577 return -ENOSPC;
3578
3579 if (kvm_device_ops_table[type] != NULL)
3580 return -EEXIST;
3581
3582 kvm_device_ops_table[type] = ops;
3583 return 0;
3584}
3585
Wanpeng Li571ee1b2014-10-09 18:30:08 +08003586void kvm_unregister_device_ops(u32 type)
3587{
3588 if (kvm_device_ops_table[type] != NULL)
3589 kvm_device_ops_table[type] = NULL;
3590}
3591
Scott Wood852b6d52013-04-12 14:08:42 +00003592static int kvm_ioctl_create_device(struct kvm *kvm,
3593 struct kvm_create_device *cd)
3594{
Steven Price8538cb22019-10-21 16:28:19 +01003595 const struct kvm_device_ops *ops = NULL;
Scott Wood852b6d52013-04-12 14:08:42 +00003596 struct kvm_device *dev;
3597 bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
Paolo Bonzini1d487e92019-04-11 11:16:47 +02003598 int type;
Scott Wood852b6d52013-04-12 14:08:42 +00003599 int ret;
3600
Will Deacond60eacb2014-09-02 10:27:33 +01003601 if (cd->type >= ARRAY_SIZE(kvm_device_ops_table))
Scott Wood852b6d52013-04-12 14:08:42 +00003602 return -ENODEV;
Will Deacond60eacb2014-09-02 10:27:33 +01003603
Paolo Bonzini1d487e92019-04-11 11:16:47 +02003604 type = array_index_nospec(cd->type, ARRAY_SIZE(kvm_device_ops_table));
3605 ops = kvm_device_ops_table[type];
Will Deacond60eacb2014-09-02 10:27:33 +01003606 if (ops == NULL)
3607 return -ENODEV;
Scott Wood852b6d52013-04-12 14:08:42 +00003608
3609 if (test)
3610 return 0;
3611
Ben Gardonb12ce362019-02-11 11:02:49 -08003612 dev = kzalloc(sizeof(*dev), GFP_KERNEL_ACCOUNT);
Scott Wood852b6d52013-04-12 14:08:42 +00003613 if (!dev)
3614 return -ENOMEM;
3615
3616 dev->ops = ops;
3617 dev->kvm = kvm;
Scott Wood852b6d52013-04-12 14:08:42 +00003618
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003619 mutex_lock(&kvm->lock);
Paolo Bonzini1d487e92019-04-11 11:16:47 +02003620 ret = ops->create(dev, type);
Scott Wood852b6d52013-04-12 14:08:42 +00003621 if (ret < 0) {
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003622 mutex_unlock(&kvm->lock);
Scott Wood852b6d52013-04-12 14:08:42 +00003623 kfree(dev);
3624 return ret;
3625 }
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003626 list_add(&dev->vm_node, &kvm->devices);
3627 mutex_unlock(&kvm->lock);
Scott Wood852b6d52013-04-12 14:08:42 +00003628
Christoffer Dall023e9fd2016-08-09 19:13:00 +02003629 if (ops->init)
3630 ops->init(dev);
3631
Jann Horncfa39382019-01-26 01:54:33 +01003632 kvm_get_kvm(kvm);
Yann Droneaud24009b02013-08-24 22:14:07 +02003633 ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC);
Scott Wood852b6d52013-04-12 14:08:42 +00003634 if (ret < 0) {
Sean Christopherson149487b2019-10-21 15:58:42 -07003635 kvm_put_kvm_no_destroy(kvm);
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003636 mutex_lock(&kvm->lock);
3637 list_del(&dev->vm_node);
3638 mutex_unlock(&kvm->lock);
Dan Carpentera0f1d212016-11-30 22:21:05 +03003639 ops->destroy(dev);
Scott Wood852b6d52013-04-12 14:08:42 +00003640 return ret;
3641 }
3642
Scott Wood852b6d52013-04-12 14:08:42 +00003643 cd->fd = ret;
3644 return 0;
3645}
3646
Alexander Graf92b591a2014-07-14 18:33:08 +02003647static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
3648{
3649 switch (arg) {
3650 case KVM_CAP_USER_MEMORY:
3651 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
3652 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Alexander Graf92b591a2014-07-14 18:33:08 +02003653 case KVM_CAP_INTERNAL_ERROR_DATA:
3654#ifdef CONFIG_HAVE_KVM_MSI
3655 case KVM_CAP_SIGNAL_MSI:
3656#endif
Paul Mackerras297e2102014-06-30 20:51:13 +10003657#ifdef CONFIG_HAVE_KVM_IRQFD
Paolo Bonzinidc9be0f2015-03-05 11:54:46 +01003658 case KVM_CAP_IRQFD:
Alexander Graf92b591a2014-07-14 18:33:08 +02003659 case KVM_CAP_IRQFD_RESAMPLE:
3660#endif
Jason Wange9ea5062015-09-15 14:41:59 +08003661 case KVM_CAP_IOEVENTFD_ANY_LENGTH:
Alexander Graf92b591a2014-07-14 18:33:08 +02003662 case KVM_CAP_CHECK_EXTENSION_VM:
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003663 case KVM_CAP_ENABLE_CAP_VM:
David Matlackacd05782020-04-17 15:14:46 -07003664 case KVM_CAP_HALT_POLL:
Alexander Graf92b591a2014-07-14 18:33:08 +02003665 return 1;
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003666#ifdef CONFIG_KVM_MMIO
Paolo Bonzini30422552017-03-31 13:53:22 +02003667 case KVM_CAP_COALESCED_MMIO:
3668 return KVM_COALESCED_MMIO_PAGE_OFFSET;
Peng Hao0804c842018-10-14 07:09:55 +08003669 case KVM_CAP_COALESCED_PIO:
3670 return 1;
Paolo Bonzini30422552017-03-31 13:53:22 +02003671#endif
Jay Zhou3c9bd402020-02-27 09:32:27 +08003672#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
3673 case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2:
3674 return KVM_DIRTY_LOG_MANUAL_CAPS;
3675#endif
Alexander Graf92b591a2014-07-14 18:33:08 +02003676#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
3677 case KVM_CAP_IRQ_ROUTING:
3678 return KVM_MAX_IRQ_ROUTES;
3679#endif
Paolo Bonzinif481b062015-05-17 17:30:37 +02003680#if KVM_ADDRESS_SPACE_NUM > 1
3681 case KVM_CAP_MULTI_ADDRESS_SPACE:
3682 return KVM_ADDRESS_SPACE_NUM;
3683#endif
Paolo Bonzinic110ae52019-03-28 17:24:03 +01003684 case KVM_CAP_NR_MEMSLOTS:
3685 return KVM_USER_MEM_SLOTS;
Peter Xufb04a1e2020-09-30 21:22:22 -04003686 case KVM_CAP_DIRTY_LOG_RING:
3687#if KVM_DIRTY_LOG_PAGE_OFFSET > 0
3688 return KVM_DIRTY_RING_MAX_ENTRIES * sizeof(struct kvm_dirty_gfn);
3689#else
3690 return 0;
3691#endif
Alexander Graf92b591a2014-07-14 18:33:08 +02003692 default:
3693 break;
3694 }
3695 return kvm_vm_ioctl_check_extension(kvm, arg);
3696}
3697
Peter Xufb04a1e2020-09-30 21:22:22 -04003698static int kvm_vm_ioctl_enable_dirty_log_ring(struct kvm *kvm, u32 size)
3699{
3700 int r;
3701
3702 if (!KVM_DIRTY_LOG_PAGE_OFFSET)
3703 return -EINVAL;
3704
3705 /* the size should be power of 2 */
3706 if (!size || (size & (size - 1)))
3707 return -EINVAL;
3708
3709 /* Should be bigger to keep the reserved entries, or a page */
3710 if (size < kvm_dirty_ring_get_rsvd_entries() *
3711 sizeof(struct kvm_dirty_gfn) || size < PAGE_SIZE)
3712 return -EINVAL;
3713
3714 if (size > KVM_DIRTY_RING_MAX_ENTRIES *
3715 sizeof(struct kvm_dirty_gfn))
3716 return -E2BIG;
3717
3718 /* We only allow it to set once */
3719 if (kvm->dirty_ring_size)
3720 return -EINVAL;
3721
3722 mutex_lock(&kvm->lock);
3723
3724 if (kvm->created_vcpus) {
3725 /* We don't allow to change this value after vcpu created */
3726 r = -EINVAL;
3727 } else {
3728 kvm->dirty_ring_size = size;
3729 r = 0;
3730 }
3731
3732 mutex_unlock(&kvm->lock);
3733 return r;
3734}
3735
3736static int kvm_vm_ioctl_reset_dirty_pages(struct kvm *kvm)
3737{
3738 int i;
3739 struct kvm_vcpu *vcpu;
3740 int cleared = 0;
3741
3742 if (!kvm->dirty_ring_size)
3743 return -EINVAL;
3744
3745 mutex_lock(&kvm->slots_lock);
3746
3747 kvm_for_each_vcpu(i, vcpu, kvm)
3748 cleared += kvm_dirty_ring_reset(vcpu->kvm, &vcpu->dirty_ring);
3749
3750 mutex_unlock(&kvm->slots_lock);
3751
3752 if (cleared)
3753 kvm_flush_remote_tlbs(kvm);
3754
3755 return cleared;
3756}
3757
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003758int __attribute__((weak)) kvm_vm_ioctl_enable_cap(struct kvm *kvm,
3759 struct kvm_enable_cap *cap)
3760{
3761 return -EINVAL;
3762}
3763
3764static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,
3765 struct kvm_enable_cap *cap)
3766{
3767 switch (cap->cap) {
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003768#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
Jay Zhou3c9bd402020-02-27 09:32:27 +08003769 case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2: {
3770 u64 allowed_options = KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE;
3771
3772 if (cap->args[0] & KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE)
3773 allowed_options = KVM_DIRTY_LOG_MANUAL_CAPS;
3774
3775 if (cap->flags || (cap->args[0] & ~allowed_options))
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003776 return -EINVAL;
3777 kvm->manual_dirty_log_protect = cap->args[0];
3778 return 0;
Jay Zhou3c9bd402020-02-27 09:32:27 +08003779 }
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003780#endif
David Matlackacd05782020-04-17 15:14:46 -07003781 case KVM_CAP_HALT_POLL: {
3782 if (cap->flags || cap->args[0] != (unsigned int)cap->args[0])
3783 return -EINVAL;
3784
3785 kvm->max_halt_poll_ns = cap->args[0];
3786 return 0;
3787 }
Peter Xufb04a1e2020-09-30 21:22:22 -04003788 case KVM_CAP_DIRTY_LOG_RING:
3789 return kvm_vm_ioctl_enable_dirty_log_ring(kvm, cap->args[0]);
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003790 default:
3791 return kvm_vm_ioctl_enable_cap(kvm, cap);
3792 }
3793}
3794
Avi Kivitybccf2152007-02-21 18:04:26 +02003795static long kvm_vm_ioctl(struct file *filp,
3796 unsigned int ioctl, unsigned long arg)
3797{
3798 struct kvm *kvm = filp->private_data;
3799 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003800 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02003801
Avi Kivity6d4e4c42007-11-21 16:41:05 +02003802 if (kvm->mm != current->mm)
3803 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02003804 switch (ioctl) {
3805 case KVM_CREATE_VCPU:
3806 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02003807 break;
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003808 case KVM_ENABLE_CAP: {
3809 struct kvm_enable_cap cap;
3810
3811 r = -EFAULT;
3812 if (copy_from_user(&cap, argp, sizeof(cap)))
3813 goto out;
3814 r = kvm_vm_ioctl_enable_cap_generic(kvm, &cap);
3815 break;
3816 }
Izik Eidus6fc138d2007-10-09 19:20:39 +02003817 case KVM_SET_USER_MEMORY_REGION: {
3818 struct kvm_userspace_memory_region kvm_userspace_mem;
3819
3820 r = -EFAULT;
3821 if (copy_from_user(&kvm_userspace_mem, argp,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003822 sizeof(kvm_userspace_mem)))
Izik Eidus6fc138d2007-10-09 19:20:39 +02003823 goto out;
3824
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003825 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826 break;
3827 }
3828 case KVM_GET_DIRTY_LOG: {
3829 struct kvm_dirty_log log;
3830
3831 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003832 if (copy_from_user(&log, argp, sizeof(log)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003833 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003834 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835 break;
3836 }
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003837#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
3838 case KVM_CLEAR_DIRTY_LOG: {
3839 struct kvm_clear_dirty_log log;
3840
3841 r = -EFAULT;
3842 if (copy_from_user(&log, argp, sizeof(log)))
3843 goto out;
3844 r = kvm_vm_ioctl_clear_dirty_log(kvm, &log);
3845 break;
3846 }
3847#endif
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003848#ifdef CONFIG_KVM_MMIO
Laurent Vivier5f94c172008-05-30 16:05:54 +02003849 case KVM_REGISTER_COALESCED_MMIO: {
3850 struct kvm_coalesced_mmio_zone zone;
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08003851
Laurent Vivier5f94c172008-05-30 16:05:54 +02003852 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003853 if (copy_from_user(&zone, argp, sizeof(zone)))
Laurent Vivier5f94c172008-05-30 16:05:54 +02003854 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02003855 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
Laurent Vivier5f94c172008-05-30 16:05:54 +02003856 break;
3857 }
3858 case KVM_UNREGISTER_COALESCED_MMIO: {
3859 struct kvm_coalesced_mmio_zone zone;
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08003860
Laurent Vivier5f94c172008-05-30 16:05:54 +02003861 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003862 if (copy_from_user(&zone, argp, sizeof(zone)))
Laurent Vivier5f94c172008-05-30 16:05:54 +02003863 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02003864 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
Laurent Vivier5f94c172008-05-30 16:05:54 +02003865 break;
3866 }
3867#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04003868 case KVM_IRQFD: {
3869 struct kvm_irqfd data;
3870
3871 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003872 if (copy_from_user(&data, argp, sizeof(data)))
Gregory Haskins721eecbf2009-05-20 10:30:49 -04003873 goto out;
Alex Williamsond4db2932012-06-29 09:56:08 -06003874 r = kvm_irqfd(kvm, &data);
Gregory Haskins721eecbf2009-05-20 10:30:49 -04003875 break;
3876 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003877 case KVM_IOEVENTFD: {
3878 struct kvm_ioeventfd data;
3879
3880 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003881 if (copy_from_user(&data, argp, sizeof(data)))
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003882 goto out;
3883 r = kvm_ioeventfd(kvm, &data);
3884 break;
3885 }
Jan Kiszka07975ad2012-03-29 21:14:12 +02003886#ifdef CONFIG_HAVE_KVM_MSI
3887 case KVM_SIGNAL_MSI: {
3888 struct kvm_msi msi;
3889
3890 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003891 if (copy_from_user(&msi, argp, sizeof(msi)))
Jan Kiszka07975ad2012-03-29 21:14:12 +02003892 goto out;
3893 r = kvm_send_userspace_msi(kvm, &msi);
3894 break;
3895 }
3896#endif
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003897#ifdef __KVM_HAVE_IRQ_LINE
3898 case KVM_IRQ_LINE_STATUS:
3899 case KVM_IRQ_LINE: {
3900 struct kvm_irq_level irq_event;
3901
3902 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003903 if (copy_from_user(&irq_event, argp, sizeof(irq_event)))
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003904 goto out;
3905
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003906 r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
3907 ioctl == KVM_IRQ_LINE_STATUS);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003908 if (r)
3909 goto out;
3910
3911 r = -EFAULT;
3912 if (ioctl == KVM_IRQ_LINE_STATUS) {
Xiubo Li893bdbf2015-02-26 14:58:19 +08003913 if (copy_to_user(argp, &irq_event, sizeof(irq_event)))
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003914 goto out;
3915 }
3916
3917 r = 0;
3918 break;
3919 }
3920#endif
Alexander Grafaa8d5942013-04-15 21:12:53 +02003921#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
3922 case KVM_SET_GSI_ROUTING: {
3923 struct kvm_irq_routing routing;
3924 struct kvm_irq_routing __user *urouting;
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003925 struct kvm_irq_routing_entry *entries = NULL;
Alexander Grafaa8d5942013-04-15 21:12:53 +02003926
3927 r = -EFAULT;
3928 if (copy_from_user(&routing, argp, sizeof(routing)))
3929 goto out;
3930 r = -EINVAL;
David Hildenbrand5c0aea02017-04-28 17:06:20 +02003931 if (!kvm_arch_can_set_irq_routing(kvm))
3932 goto out;
Xiubo Licaf1ff22016-06-15 18:00:33 +08003933 if (routing.nr > KVM_MAX_IRQ_ROUTES)
Alexander Grafaa8d5942013-04-15 21:12:53 +02003934 goto out;
3935 if (routing.flags)
3936 goto out;
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003937 if (routing.nr) {
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003938 urouting = argp;
Denis Efremov7ec28e22020-06-03 13:11:31 +03003939 entries = vmemdup_user(urouting->entries,
3940 array_size(sizeof(*entries),
3941 routing.nr));
3942 if (IS_ERR(entries)) {
3943 r = PTR_ERR(entries);
3944 goto out;
3945 }
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003946 }
Alexander Grafaa8d5942013-04-15 21:12:53 +02003947 r = kvm_set_irq_routing(kvm, entries, routing.nr,
3948 routing.flags);
Denis Efremov7ec28e22020-06-03 13:11:31 +03003949 kvfree(entries);
Alexander Grafaa8d5942013-04-15 21:12:53 +02003950 break;
3951 }
3952#endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
Scott Wood852b6d52013-04-12 14:08:42 +00003953 case KVM_CREATE_DEVICE: {
3954 struct kvm_create_device cd;
3955
3956 r = -EFAULT;
3957 if (copy_from_user(&cd, argp, sizeof(cd)))
3958 goto out;
3959
3960 r = kvm_ioctl_create_device(kvm, &cd);
3961 if (r)
3962 goto out;
3963
3964 r = -EFAULT;
3965 if (copy_to_user(argp, &cd, sizeof(cd)))
3966 goto out;
3967
3968 r = 0;
3969 break;
3970 }
Alexander Graf92b591a2014-07-14 18:33:08 +02003971 case KVM_CHECK_EXTENSION:
3972 r = kvm_vm_ioctl_check_extension_generic(kvm, arg);
3973 break;
Peter Xufb04a1e2020-09-30 21:22:22 -04003974 case KVM_RESET_DIRTY_RINGS:
3975 r = kvm_vm_ioctl_reset_dirty_pages(kvm);
3976 break;
Avi Kivityf17abe92007-02-21 19:28:04 +02003977 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01003978 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02003979 }
3980out:
3981 return r;
3982}
3983
Christian Borntraegerde8e5d72015-02-03 09:35:15 +01003984#ifdef CONFIG_KVM_COMPAT
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003985struct compat_kvm_dirty_log {
3986 __u32 slot;
3987 __u32 padding1;
3988 union {
3989 compat_uptr_t dirty_bitmap; /* one bit per page */
3990 __u64 padding2;
3991 };
3992};
3993
3994static long kvm_vm_compat_ioctl(struct file *filp,
3995 unsigned int ioctl, unsigned long arg)
3996{
3997 struct kvm *kvm = filp->private_data;
3998 int r;
3999
4000 if (kvm->mm != current->mm)
4001 return -EIO;
4002 switch (ioctl) {
4003 case KVM_GET_DIRTY_LOG: {
4004 struct compat_kvm_dirty_log compat_log;
4005 struct kvm_dirty_log log;
4006
Arnd Bergmann6ff58942009-10-22 14:19:27 +02004007 if (copy_from_user(&compat_log, (void __user *)arg,
4008 sizeof(compat_log)))
Markus Elfringf6a3b162017-01-22 11:30:21 +01004009 return -EFAULT;
Arnd Bergmann6ff58942009-10-22 14:19:27 +02004010 log.slot = compat_log.slot;
4011 log.padding1 = compat_log.padding1;
4012 log.padding2 = compat_log.padding2;
4013 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
4014
4015 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Arnd Bergmann6ff58942009-10-22 14:19:27 +02004016 break;
4017 }
4018 default:
4019 r = kvm_vm_ioctl(filp, ioctl, arg);
4020 }
Arnd Bergmann6ff58942009-10-22 14:19:27 +02004021 return r;
4022}
4023#endif
4024
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01004025static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02004026 .release = kvm_vm_release,
4027 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02004028 .llseek = noop_llseek,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01004029 KVM_COMPAT(kvm_vm_compat_ioctl),
Avi Kivityf17abe92007-02-21 19:28:04 +02004030};
4031
Carsten Ottee08b9632012-01-04 10:25:20 +01004032static int kvm_dev_ioctl_create_vm(unsigned long type)
Avi Kivityf17abe92007-02-21 19:28:04 +02004033{
Heiko Carstensaac87632010-10-27 17:22:10 +02004034 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02004035 struct kvm *kvm;
Al Viro506cfba2016-07-14 18:54:17 +02004036 struct file *file;
Avi Kivityf17abe92007-02-21 19:28:04 +02004037
Carsten Ottee08b9632012-01-04 10:25:20 +01004038 kvm = kvm_create_vm(type);
Avi Kivityd6d28162007-06-28 08:38:16 -04004039 if (IS_ERR(kvm))
4040 return PTR_ERR(kvm);
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02004041#ifdef CONFIG_KVM_MMIO
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09004042 r = kvm_coalesced_mmio_init(kvm);
Markus Elfring78588332017-11-21 13:40:17 +01004043 if (r < 0)
4044 goto put_kvm;
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09004045#endif
Al Viro506cfba2016-07-14 18:54:17 +02004046 r = get_unused_fd_flags(O_CLOEXEC);
Markus Elfring78588332017-11-21 13:40:17 +01004047 if (r < 0)
4048 goto put_kvm;
4049
Al Viro506cfba2016-07-14 18:54:17 +02004050 file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
4051 if (IS_ERR(file)) {
4052 put_unused_fd(r);
Markus Elfring78588332017-11-21 13:40:17 +01004053 r = PTR_ERR(file);
4054 goto put_kvm;
Al Viro506cfba2016-07-14 18:54:17 +02004055 }
Janosch Frank536a6f82016-05-18 13:26:23 +02004056
Paolo Bonzini525df862017-06-27 15:45:09 +02004057 /*
4058 * Don't call kvm_put_kvm anymore at this point; file->f_op is
4059 * already set, with ->release() being kvm_vm_release(). In error
4060 * cases it will be called by the final fput(file) and will take
4061 * care of doing kvm_put_kvm(kvm).
4062 */
Janosch Frank536a6f82016-05-18 13:26:23 +02004063 if (kvm_create_vm_debugfs(kvm, r) < 0) {
Al Viro506cfba2016-07-14 18:54:17 +02004064 put_unused_fd(r);
4065 fput(file);
Janosch Frank536a6f82016-05-18 13:26:23 +02004066 return -ENOMEM;
4067 }
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004068 kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02004069
Al Viro506cfba2016-07-14 18:54:17 +02004070 fd_install(r, file);
Heiko Carstensaac87632010-10-27 17:22:10 +02004071 return r;
Markus Elfring78588332017-11-21 13:40:17 +01004072
4073put_kvm:
4074 kvm_put_kvm(kvm);
4075 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02004076}
4077
4078static long kvm_dev_ioctl(struct file *filp,
4079 unsigned int ioctl, unsigned long arg)
4080{
Avi Kivity07c45a32007-03-07 13:05:38 +02004081 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02004082
4083 switch (ioctl) {
4084 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02004085 if (arg)
4086 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02004087 r = KVM_API_VERSION;
4088 break;
4089 case KVM_CREATE_VM:
Carsten Ottee08b9632012-01-04 10:25:20 +01004090 r = kvm_dev_ioctl_create_vm(arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02004091 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08004092 case KVM_CHECK_EXTENSION:
Alexander Graf784aa3d2014-07-14 18:27:35 +02004093 r = kvm_vm_ioctl_check_extension_generic(NULL, arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02004094 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02004095 case KVM_GET_VCPU_MMAP_SIZE:
Avi Kivity07c45a32007-03-07 13:05:38 +02004096 if (arg)
4097 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02004098 r = PAGE_SIZE; /* struct kvm_run */
4099#ifdef CONFIG_X86
4100 r += PAGE_SIZE; /* pio data page */
4101#endif
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02004102#ifdef CONFIG_KVM_MMIO
Laurent Vivier5f94c172008-05-30 16:05:54 +02004103 r += PAGE_SIZE; /* coalesced mmio ring page */
4104#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02004105 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04004106 case KVM_TRACE_ENABLE:
4107 case KVM_TRACE_PAUSE:
4108 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03004109 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04004110 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111 default:
Carsten Otte043405e2007-10-10 17:16:19 +02004112 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113 }
4114out:
4115 return r;
4116}
4117
Avi Kivity6aa8b732006-12-10 02:21:36 -08004118static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004119 .unlocked_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02004120 .llseek = noop_llseek,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01004121 KVM_COMPAT(kvm_dev_ioctl),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004122};
4123
4124static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02004125 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126 "kvm",
4127 &kvm_chardev_ops,
4128};
4129
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004130static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03004131{
4132 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02004133 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03004134
Rusty Russell7f59f492008-12-07 21:25:45 +10304135 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03004136 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02004137
Rusty Russell7f59f492008-12-07 21:25:45 +10304138 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02004139
Radim Krčmář13a34e02014-08-28 15:13:03 +02004140 r = kvm_arch_hardware_enable();
Alexander Graf10474ae2009-09-15 11:37:46 +02004141
4142 if (r) {
4143 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
4144 atomic_inc(&hardware_enable_failed);
Xiubo Li1170adc2015-02-26 14:58:26 +08004145 pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu);
Alexander Graf10474ae2009-09-15 11:37:46 +02004146 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03004147}
4148
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004149static int kvm_starting_cpu(unsigned int cpu)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004150{
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004151 raw_spin_lock(&kvm_count_lock);
Paolo Bonzini4fa92fb2013-09-10 12:57:17 +02004152 if (kvm_usage_count)
4153 hardware_enable_nolock(NULL);
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004154 raw_spin_unlock(&kvm_count_lock);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004155 return 0;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004156}
4157
4158static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03004159{
4160 int cpu = raw_smp_processor_id();
4161
Rusty Russell7f59f492008-12-07 21:25:45 +10304162 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03004163 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10304164 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Radim Krčmář13a34e02014-08-28 15:13:03 +02004165 kvm_arch_hardware_disable();
Avi Kivity1b6c0162007-05-24 13:03:52 +03004166}
4167
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004168static int kvm_dying_cpu(unsigned int cpu)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004169{
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004170 raw_spin_lock(&kvm_count_lock);
Paolo Bonzini4fa92fb2013-09-10 12:57:17 +02004171 if (kvm_usage_count)
4172 hardware_disable_nolock(NULL);
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004173 raw_spin_unlock(&kvm_count_lock);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004174 return 0;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004175}
4176
Alexander Graf10474ae2009-09-15 11:37:46 +02004177static void hardware_disable_all_nolock(void)
4178{
4179 BUG_ON(!kvm_usage_count);
4180
4181 kvm_usage_count--;
4182 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004183 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02004184}
4185
4186static void hardware_disable_all(void)
4187{
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004188 raw_spin_lock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02004189 hardware_disable_all_nolock();
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004190 raw_spin_unlock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02004191}
4192
4193static int hardware_enable_all(void)
4194{
4195 int r = 0;
4196
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004197 raw_spin_lock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02004198
4199 kvm_usage_count++;
4200 if (kvm_usage_count == 1) {
4201 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004202 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02004203
4204 if (atomic_read(&hardware_enable_failed)) {
4205 hardware_disable_all_nolock();
4206 r = -EBUSY;
4207 }
4208 }
4209
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004210 raw_spin_unlock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02004211
4212 return r;
4213}
4214
Rusty Russell9a2b85c2007-07-17 23:17:55 +10004215static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04004216 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10004217{
Sheng Yang8e1c1812009-04-29 11:09:04 +08004218 /*
4219 * Some (well, at least mine) BIOSes hang on reboot if
4220 * in vmx root mode.
4221 *
4222 * And Intel TXT required VMX off for all cpu when system shutdown.
4223 */
Xiubo Li1170adc2015-02-26 14:58:26 +08004224 pr_info("kvm: exiting hardware virtualization\n");
Sheng Yang8e1c1812009-04-29 11:09:04 +08004225 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004226 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10004227 return NOTIFY_OK;
4228}
4229
4230static struct notifier_block kvm_reboot_notifier = {
4231 .notifier_call = kvm_reboot,
4232 .priority = 0,
4233};
4234
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004235static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004236{
4237 int i;
4238
4239 for (i = 0; i < bus->dev_count; i++) {
Sasha Levin743eeb02011-07-27 16:00:48 +03004240 struct kvm_io_device *pos = bus->range[i].dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004241
4242 kvm_iodevice_destructor(pos);
4243 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004244 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004245}
4246
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004247static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1,
Xiubo Li20e87b72015-02-26 14:58:25 +08004248 const struct kvm_io_range *r2)
Sasha Levin743eeb02011-07-27 16:00:48 +03004249{
Jason Wang8f4216c72015-09-15 14:41:57 +08004250 gpa_t addr1 = r1->addr;
4251 gpa_t addr2 = r2->addr;
4252
4253 if (addr1 < addr2)
Sasha Levin743eeb02011-07-27 16:00:48 +03004254 return -1;
Jason Wang8f4216c72015-09-15 14:41:57 +08004255
4256 /* If r2->len == 0, match the exact address. If r2->len != 0,
4257 * accept any overlapping write. Any order is acceptable for
4258 * overlapping ranges, because kvm_io_bus_get_first_dev ensures
4259 * we process all of them.
4260 */
4261 if (r2->len) {
4262 addr1 += r1->len;
4263 addr2 += r2->len;
4264 }
4265
4266 if (addr1 > addr2)
Sasha Levin743eeb02011-07-27 16:00:48 +03004267 return 1;
Jason Wang8f4216c72015-09-15 14:41:57 +08004268
Sasha Levin743eeb02011-07-27 16:00:48 +03004269 return 0;
4270}
4271
Paolo Bonzinia343c9b2013-07-16 13:03:29 +02004272static int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
4273{
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004274 return kvm_io_bus_cmp(p1, p2);
Paolo Bonzinia343c9b2013-07-16 13:03:29 +02004275}
4276
Geoff Levand39369f72013-04-05 19:20:30 +00004277static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
Sasha Levin743eeb02011-07-27 16:00:48 +03004278 gpa_t addr, int len)
4279{
4280 struct kvm_io_range *range, key;
4281 int off;
4282
4283 key = (struct kvm_io_range) {
4284 .addr = addr,
4285 .len = len,
4286 };
4287
4288 range = bsearch(&key, bus->range, bus->dev_count,
4289 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
4290 if (range == NULL)
4291 return -ENOENT;
4292
4293 off = range - bus->range;
4294
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004295 while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0)
Sasha Levin743eeb02011-07-27 16:00:48 +03004296 off--;
4297
4298 return off;
4299}
4300
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004301static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004302 struct kvm_io_range *range, const void *val)
4303{
4304 int idx;
4305
4306 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
4307 if (idx < 0)
4308 return -EOPNOTSUPP;
4309
4310 while (idx < bus->dev_count &&
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004311 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004312 if (!kvm_iodevice_write(vcpu, bus->range[idx].dev, range->addr,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004313 range->len, val))
4314 return idx;
4315 idx++;
4316 }
4317
4318 return -EOPNOTSUPP;
4319}
4320
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004321/* kvm_io_bus_write - called under kvm->slots_lock */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004322int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004323 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004324{
Cornelia Huck126a5af2013-07-03 16:30:53 +02004325 struct kvm_io_bus *bus;
4326 struct kvm_io_range range;
4327 int r;
4328
4329 range = (struct kvm_io_range) {
4330 .addr = addr,
4331 .len = len,
4332 };
4333
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004334 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004335 if (!bus)
4336 return -ENOMEM;
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004337 r = __kvm_io_bus_write(vcpu, bus, &range, val);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004338 return r < 0 ? r : 0;
4339}
Leo Yana2420102019-02-22 16:10:09 +08004340EXPORT_SYMBOL_GPL(kvm_io_bus_write);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004341
4342/* kvm_io_bus_write_cookie - called under kvm->slots_lock */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004343int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
4344 gpa_t addr, int len, const void *val, long cookie)
Cornelia Huck126a5af2013-07-03 16:30:53 +02004345{
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08004346 struct kvm_io_bus *bus;
Sasha Levin743eeb02011-07-27 16:00:48 +03004347 struct kvm_io_range range;
4348
4349 range = (struct kvm_io_range) {
4350 .addr = addr,
4351 .len = len,
4352 };
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08004353
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004354 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004355 if (!bus)
4356 return -ENOMEM;
Cornelia Huck126a5af2013-07-03 16:30:53 +02004357
4358 /* First try the device referenced by cookie. */
4359 if ((cookie >= 0) && (cookie < bus->dev_count) &&
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004360 (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004361 if (!kvm_iodevice_write(vcpu, bus->range[cookie].dev, addr, len,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004362 val))
4363 return cookie;
4364
4365 /*
4366 * cookie contained garbage; fall back to search and return the
4367 * correct cookie value.
4368 */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004369 return __kvm_io_bus_write(vcpu, bus, &range, val);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004370}
4371
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004372static int __kvm_io_bus_read(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
4373 struct kvm_io_range *range, void *val)
Cornelia Huck126a5af2013-07-03 16:30:53 +02004374{
4375 int idx;
4376
4377 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
Sasha Levin743eeb02011-07-27 16:00:48 +03004378 if (idx < 0)
4379 return -EOPNOTSUPP;
4380
4381 while (idx < bus->dev_count &&
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004382 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004383 if (!kvm_iodevice_read(vcpu, bus->range[idx].dev, range->addr,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004384 range->len, val))
4385 return idx;
Sasha Levin743eeb02011-07-27 16:00:48 +03004386 idx++;
4387 }
4388
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004389 return -EOPNOTSUPP;
4390}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004391
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004392/* kvm_io_bus_read - called under kvm->slots_lock */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004393int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004394 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004395{
Cornelia Huck126a5af2013-07-03 16:30:53 +02004396 struct kvm_io_bus *bus;
4397 struct kvm_io_range range;
4398 int r;
4399
4400 range = (struct kvm_io_range) {
4401 .addr = addr,
4402 .len = len,
4403 };
4404
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004405 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004406 if (!bus)
4407 return -ENOMEM;
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004408 r = __kvm_io_bus_read(vcpu, bus, &range, val);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004409 return r < 0 ? r : 0;
4410}
4411
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004412/* Caller must hold slots_lock. */
Sasha Levin743eeb02011-07-27 16:00:48 +03004413int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
4414 int len, struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03004415{
Gal Hammerd4c67a72018-01-16 15:34:41 +02004416 int i;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004417 struct kvm_io_bus *new_bus, *bus;
Gal Hammerd4c67a72018-01-16 15:34:41 +02004418 struct kvm_io_range range;
Gregory Haskins090b7af2009-07-07 17:08:44 -04004419
Christian Borntraeger4a12f952017-07-07 10:51:38 +02004420 bus = kvm_get_bus(kvm, bus_idx);
David Hildenbrand90db1042017-03-23 18:24:19 +01004421 if (!bus)
4422 return -ENOMEM;
4423
Amos Kong6ea34c92013-05-25 06:44:15 +08004424 /* exclude ioeventfd which is limited by maximum fd */
4425 if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
Gregory Haskins090b7af2009-07-07 17:08:44 -04004426 return -ENOSPC;
4427
Gustavo A. R. Silva90952cd2019-01-30 17:07:47 +01004428 new_bus = kmalloc(struct_size(bus, range, bus->dev_count + 1),
Ben Gardonb12ce362019-02-11 11:02:49 -08004429 GFP_KERNEL_ACCOUNT);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004430 if (!new_bus)
4431 return -ENOMEM;
Gal Hammerd4c67a72018-01-16 15:34:41 +02004432
4433 range = (struct kvm_io_range) {
4434 .addr = addr,
4435 .len = len,
4436 .dev = dev,
4437 };
4438
4439 for (i = 0; i < bus->dev_count; i++)
4440 if (kvm_io_bus_cmp(&bus->range[i], &range) > 0)
4441 break;
4442
4443 memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
4444 new_bus->dev_count++;
4445 new_bus->range[i] = range;
4446 memcpy(new_bus->range + i + 1, bus->range + i,
4447 (bus->dev_count - i) * sizeof(struct kvm_io_range));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004448 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
4449 synchronize_srcu_expedited(&kvm->srcu);
4450 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04004451
4452 return 0;
4453}
4454
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004455/* Caller must hold slots_lock. */
David Hildenbrand90db1042017-03-23 18:24:19 +01004456void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4457 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04004458{
Rustam Kovhaevf6588662020-09-07 11:55:35 -07004459 int i, j;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004460 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03004461
Christian Borntraeger4a12f952017-07-07 10:51:38 +02004462 bus = kvm_get_bus(kvm, bus_idx);
Peter Xudf630b82017-03-15 16:01:17 +08004463 if (!bus)
David Hildenbrand90db1042017-03-23 18:24:19 +01004464 return;
Peter Xudf630b82017-03-15 16:01:17 +08004465
Amos Konga13007162012-03-09 12:17:32 +08004466 for (i = 0; i < bus->dev_count; i++)
4467 if (bus->range[i].dev == dev) {
Gregory Haskins090b7af2009-07-07 17:08:44 -04004468 break;
4469 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004470
David Hildenbrand90db1042017-03-23 18:24:19 +01004471 if (i == bus->dev_count)
4472 return;
Amos Konga13007162012-03-09 12:17:32 +08004473
Gustavo A. R. Silva90952cd2019-01-30 17:07:47 +01004474 new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1),
Ben Gardonb12ce362019-02-11 11:02:49 -08004475 GFP_KERNEL_ACCOUNT);
Rustam Kovhaevf6588662020-09-07 11:55:35 -07004476 if (new_bus) {
Rustam Kovhaev871c4332020-09-18 05:05:00 -07004477 memcpy(new_bus, bus, struct_size(bus, range, i));
Rustam Kovhaevf6588662020-09-07 11:55:35 -07004478 new_bus->dev_count--;
4479 memcpy(new_bus->range + i, bus->range + i + 1,
Rustam Kovhaev871c4332020-09-18 05:05:00 -07004480 flex_array_size(new_bus, range, new_bus->dev_count - i));
Rustam Kovhaevf6588662020-09-07 11:55:35 -07004481 } else {
David Hildenbrand90db1042017-03-23 18:24:19 +01004482 pr_err("kvm: failed to shrink bus, removing it completely\n");
Rustam Kovhaevf6588662020-09-07 11:55:35 -07004483 for (j = 0; j < bus->dev_count; j++) {
4484 if (j == i)
4485 continue;
4486 kvm_iodevice_destructor(bus->range[j].dev);
4487 }
David Hildenbrand90db1042017-03-23 18:24:19 +01004488 }
Amos Konga13007162012-03-09 12:17:32 +08004489
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004490 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
4491 synchronize_srcu_expedited(&kvm->srcu);
4492 kfree(bus);
David Hildenbrand90db1042017-03-23 18:24:19 +01004493 return;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004494}
4495
Andre Przywara8a39d002016-07-15 12:43:26 +01004496struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4497 gpa_t addr)
4498{
4499 struct kvm_io_bus *bus;
4500 int dev_idx, srcu_idx;
4501 struct kvm_io_device *iodev = NULL;
4502
4503 srcu_idx = srcu_read_lock(&kvm->srcu);
4504
4505 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004506 if (!bus)
4507 goto out_unlock;
Andre Przywara8a39d002016-07-15 12:43:26 +01004508
4509 dev_idx = kvm_io_bus_get_first_dev(bus, addr, 1);
4510 if (dev_idx < 0)
4511 goto out_unlock;
4512
4513 iodev = bus->range[dev_idx].dev;
4514
4515out_unlock:
4516 srcu_read_unlock(&kvm->srcu, srcu_idx);
4517
4518 return iodev;
4519}
4520EXPORT_SYMBOL_GPL(kvm_io_bus_get_dev);
4521
Janosch Frank536a6f82016-05-18 13:26:23 +02004522static int kvm_debugfs_open(struct inode *inode, struct file *file,
4523 int (*get)(void *, u64 *), int (*set)(void *, u64),
4524 const char *fmt)
4525{
4526 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
4527 inode->i_private;
4528
4529 /* The debugfs files are a reference to the kvm struct which
4530 * is still valid when kvm_destroy_vm is called.
4531 * To avoid the race between open and the removal of the debugfs
4532 * directory we test against the users count.
4533 */
Elena Reshetovae3736c32017-02-20 13:06:21 +02004534 if (!refcount_inc_not_zero(&stat_data->kvm->users_count))
Janosch Frank536a6f82016-05-18 13:26:23 +02004535 return -ENOENT;
4536
Paolo Bonzini833b45d2019-09-30 18:48:44 +02004537 if (simple_attr_open(inode, file, get,
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004538 KVM_DBGFS_GET_MODE(stat_data->dbgfs_item) & 0222
4539 ? set : NULL,
4540 fmt)) {
Janosch Frank536a6f82016-05-18 13:26:23 +02004541 kvm_put_kvm(stat_data->kvm);
4542 return -ENOMEM;
4543 }
4544
4545 return 0;
4546}
4547
4548static int kvm_debugfs_release(struct inode *inode, struct file *file)
4549{
4550 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
4551 inode->i_private;
4552
4553 simple_attr_release(inode, file);
4554 kvm_put_kvm(stat_data->kvm);
4555
4556 return 0;
4557}
4558
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004559static int kvm_get_stat_per_vm(struct kvm *kvm, size_t offset, u64 *val)
Janosch Frank536a6f82016-05-18 13:26:23 +02004560{
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004561 *val = *(ulong *)((void *)kvm + offset);
Janosch Frank536a6f82016-05-18 13:26:23 +02004562
4563 return 0;
4564}
4565
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004566static int kvm_clear_stat_per_vm(struct kvm *kvm, size_t offset)
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004567{
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004568 *(ulong *)((void *)kvm + offset) = 0;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004569
4570 return 0;
4571}
4572
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004573static int kvm_get_stat_per_vcpu(struct kvm *kvm, size_t offset, u64 *val)
Janosch Frank536a6f82016-05-18 13:26:23 +02004574{
4575 int i;
Janosch Frank536a6f82016-05-18 13:26:23 +02004576 struct kvm_vcpu *vcpu;
4577
4578 *val = 0;
4579
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004580 kvm_for_each_vcpu(i, vcpu, kvm)
4581 *val += *(u64 *)((void *)vcpu + offset);
Janosch Frank536a6f82016-05-18 13:26:23 +02004582
4583 return 0;
4584}
4585
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004586static int kvm_clear_stat_per_vcpu(struct kvm *kvm, size_t offset)
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004587{
4588 int i;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004589 struct kvm_vcpu *vcpu;
4590
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004591 kvm_for_each_vcpu(i, vcpu, kvm)
4592 *(u64 *)((void *)vcpu + offset) = 0;
4593
4594 return 0;
4595}
4596
4597static int kvm_stat_data_get(void *data, u64 *val)
4598{
4599 int r = -EFAULT;
4600 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4601
4602 switch (stat_data->dbgfs_item->kind) {
4603 case KVM_STAT_VM:
4604 r = kvm_get_stat_per_vm(stat_data->kvm,
4605 stat_data->dbgfs_item->offset, val);
4606 break;
4607 case KVM_STAT_VCPU:
4608 r = kvm_get_stat_per_vcpu(stat_data->kvm,
4609 stat_data->dbgfs_item->offset, val);
4610 break;
4611 }
4612
4613 return r;
4614}
4615
4616static int kvm_stat_data_clear(void *data, u64 val)
4617{
4618 int r = -EFAULT;
4619 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4620
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004621 if (val)
4622 return -EINVAL;
4623
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004624 switch (stat_data->dbgfs_item->kind) {
4625 case KVM_STAT_VM:
4626 r = kvm_clear_stat_per_vm(stat_data->kvm,
4627 stat_data->dbgfs_item->offset);
4628 break;
4629 case KVM_STAT_VCPU:
4630 r = kvm_clear_stat_per_vcpu(stat_data->kvm,
4631 stat_data->dbgfs_item->offset);
4632 break;
4633 }
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004634
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004635 return r;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004636}
4637
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004638static int kvm_stat_data_open(struct inode *inode, struct file *file)
Janosch Frank536a6f82016-05-18 13:26:23 +02004639{
4640 __simple_attr_check_format("%llu\n", 0ull);
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004641 return kvm_debugfs_open(inode, file, kvm_stat_data_get,
4642 kvm_stat_data_clear, "%llu\n");
Janosch Frank536a6f82016-05-18 13:26:23 +02004643}
4644
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004645static const struct file_operations stat_fops_per_vm = {
4646 .owner = THIS_MODULE,
4647 .open = kvm_stat_data_open,
Janosch Frank536a6f82016-05-18 13:26:23 +02004648 .release = kvm_debugfs_release,
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004649 .read = simple_attr_read,
4650 .write = simple_attr_write,
4651 .llseek = no_llseek,
Janosch Frank536a6f82016-05-18 13:26:23 +02004652};
4653
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004654static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02004655{
4656 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02004657 struct kvm *kvm;
Janosch Frank536a6f82016-05-18 13:26:23 +02004658 u64 tmp_val;
Avi Kivityba1389b2007-11-18 16:24:12 +02004659
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004660 *val = 0;
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004661 mutex_lock(&kvm_lock);
Janosch Frank536a6f82016-05-18 13:26:23 +02004662 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004663 kvm_get_stat_per_vm(kvm, offset, &tmp_val);
Janosch Frank536a6f82016-05-18 13:26:23 +02004664 *val += tmp_val;
4665 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004666 mutex_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004667 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02004668}
4669
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004670static int vm_stat_clear(void *_offset, u64 val)
4671{
4672 unsigned offset = (long)_offset;
4673 struct kvm *kvm;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004674
4675 if (val)
4676 return -EINVAL;
4677
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004678 mutex_lock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004679 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004680 kvm_clear_stat_per_vm(kvm, offset);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004681 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004682 mutex_unlock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004683
4684 return 0;
4685}
4686
4687DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n");
Avi Kivityba1389b2007-11-18 16:24:12 +02004688
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004689static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03004690{
4691 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03004692 struct kvm *kvm;
Janosch Frank536a6f82016-05-18 13:26:23 +02004693 u64 tmp_val;
Avi Kivity1165f5f2007-04-19 17:27:43 +03004694
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004695 *val = 0;
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004696 mutex_lock(&kvm_lock);
Janosch Frank536a6f82016-05-18 13:26:23 +02004697 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004698 kvm_get_stat_per_vcpu(kvm, offset, &tmp_val);
Janosch Frank536a6f82016-05-18 13:26:23 +02004699 *val += tmp_val;
4700 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004701 mutex_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004702 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03004703}
4704
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004705static int vcpu_stat_clear(void *_offset, u64 val)
4706{
4707 unsigned offset = (long)_offset;
4708 struct kvm *kvm;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004709
4710 if (val)
4711 return -EINVAL;
4712
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004713 mutex_lock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004714 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004715 kvm_clear_stat_per_vcpu(kvm, offset);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004716 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004717 mutex_unlock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004718
4719 return 0;
4720}
4721
4722DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear,
4723 "%llu\n");
Avi Kivityba1389b2007-11-18 16:24:12 +02004724
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004725static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02004726 [KVM_STAT_VCPU] = &vcpu_stat_fops,
4727 [KVM_STAT_VM] = &vm_stat_fops,
4728};
Avi Kivity1165f5f2007-04-19 17:27:43 +03004729
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004730static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
4731{
4732 struct kobj_uevent_env *env;
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004733 unsigned long long created, active;
4734
4735 if (!kvm_dev.this_device || !kvm)
4736 return;
4737
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004738 mutex_lock(&kvm_lock);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004739 if (type == KVM_EVENT_CREATE_VM) {
4740 kvm_createvm_count++;
4741 kvm_active_vms++;
4742 } else if (type == KVM_EVENT_DESTROY_VM) {
4743 kvm_active_vms--;
4744 }
4745 created = kvm_createvm_count;
4746 active = kvm_active_vms;
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004747 mutex_unlock(&kvm_lock);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004748
Ben Gardonb12ce362019-02-11 11:02:49 -08004749 env = kzalloc(sizeof(*env), GFP_KERNEL_ACCOUNT);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004750 if (!env)
4751 return;
4752
4753 add_uevent_var(env, "CREATED=%llu", created);
4754 add_uevent_var(env, "COUNT=%llu", active);
4755
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004756 if (type == KVM_EVENT_CREATE_VM) {
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004757 add_uevent_var(env, "EVENT=create");
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004758 kvm->userspace_pid = task_pid_nr(current);
4759 } else if (type == KVM_EVENT_DESTROY_VM) {
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004760 add_uevent_var(env, "EVENT=destroy");
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004761 }
4762 add_uevent_var(env, "PID=%d", kvm->userspace_pid);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004763
Greg Kroah-Hartman8ed05792019-02-28 16:34:37 +01004764 if (!IS_ERR_OR_NULL(kvm->debugfs_dentry)) {
Ben Gardonb12ce362019-02-11 11:02:49 -08004765 char *tmp, *p = kmalloc(PATH_MAX, GFP_KERNEL_ACCOUNT);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004766
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004767 if (p) {
4768 tmp = dentry_path_raw(kvm->debugfs_dentry, p, PATH_MAX);
4769 if (!IS_ERR(tmp))
4770 add_uevent_var(env, "STATS_PATH=%s", tmp);
4771 kfree(p);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004772 }
4773 }
4774 /* no need for checks, since we are adding at most only 5 keys */
4775 env->envp[env->envp_idx++] = NULL;
4776 kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp);
4777 kfree(env);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004778}
4779
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02004780static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781{
4782 struct kvm_stats_debugfs_item *p;
4783
Hollis Blanchard76f7c872008-04-15 16:05:42 -05004784 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Hamo4f69b682011-12-15 14:23:16 +08004785
Janosch Frank536a6f82016-05-18 13:26:23 +02004786 kvm_debugfs_num_entries = 0;
4787 for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004788 debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p),
4789 kvm_debugfs_dir, (void *)(long)p->offset,
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02004790 stat_fops[p->kind]);
Hamo4f69b682011-12-15 14:23:16 +08004791 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792}
4793
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004794static int kvm_suspend(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08004795{
Alexander Graf10474ae2009-09-15 11:37:46 +02004796 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004797 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08004798 return 0;
4799}
4800
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004801static void kvm_resume(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08004802{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10004803 if (kvm_usage_count) {
Wanpeng Li2eb06c32019-05-17 16:49:49 +08004804#ifdef CONFIG_LOCKDEP
4805 WARN_ON(lockdep_is_held(&kvm_count_lock));
4806#endif
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004807 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10004808 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08004809}
4810
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004811static struct syscore_ops kvm_syscore_ops = {
Avi Kivity59ae6c62007-02-12 00:54:48 -08004812 .suspend = kvm_suspend,
4813 .resume = kvm_resume,
4814};
4815
Avi Kivity15ad7142007-07-11 18:17:21 +03004816static inline
4817struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
4818{
4819 return container_of(pn, struct kvm_vcpu, preempt_notifier);
4820}
4821
4822static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
4823{
4824 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08004825
Wanpeng Li046ddee2019-08-01 11:30:14 +08004826 WRITE_ONCE(vcpu->preempted, false);
Wanpeng Lid73eb572019-07-18 19:39:06 +08004827 WRITE_ONCE(vcpu->ready, false);
Avi Kivity15ad7142007-07-11 18:17:21 +03004828
Paolo Bonzini7495e222020-01-09 09:57:19 -05004829 __this_cpu_write(kvm_running_vcpu, vcpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02004830 kvm_arch_sched_in(vcpu, cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004831 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03004832}
4833
4834static void kvm_sched_out(struct preempt_notifier *pn,
4835 struct task_struct *next)
4836{
4837 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
4838
Wanpeng Lid73eb572019-07-18 19:39:06 +08004839 if (current->state == TASK_RUNNING) {
Wanpeng Li046ddee2019-08-01 11:30:14 +08004840 WRITE_ONCE(vcpu->preempted, true);
Wanpeng Lid73eb572019-07-18 19:39:06 +08004841 WRITE_ONCE(vcpu->ready, true);
4842 }
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004843 kvm_arch_vcpu_put(vcpu);
Paolo Bonzini7495e222020-01-09 09:57:19 -05004844 __this_cpu_write(kvm_running_vcpu, NULL);
4845}
4846
4847/**
4848 * kvm_get_running_vcpu - get the vcpu running on the current CPU.
Marc Zyngier1f03b2b2020-02-07 16:34:10 +00004849 *
4850 * We can disable preemption locally around accessing the per-CPU variable,
4851 * and use the resolved vcpu pointer after enabling preemption again,
4852 * because even if the current thread is migrated to another CPU, reading
4853 * the per-CPU value later will give us the same value as we update the
4854 * per-CPU variable in the preempt notifier handlers.
Paolo Bonzini7495e222020-01-09 09:57:19 -05004855 */
4856struct kvm_vcpu *kvm_get_running_vcpu(void)
4857{
Marc Zyngier1f03b2b2020-02-07 16:34:10 +00004858 struct kvm_vcpu *vcpu;
4859
4860 preempt_disable();
4861 vcpu = __this_cpu_read(kvm_running_vcpu);
4862 preempt_enable();
4863
4864 return vcpu;
Paolo Bonzini7495e222020-01-09 09:57:19 -05004865}
Wanpeng Li379a3c82020-04-28 14:23:27 +08004866EXPORT_SYMBOL_GPL(kvm_get_running_vcpu);
Paolo Bonzini7495e222020-01-09 09:57:19 -05004867
4868/**
4869 * kvm_get_running_vcpus - get the per-CPU array of currently running vcpus.
4870 */
4871struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
4872{
4873 return &kvm_running_vcpu;
Avi Kivity15ad7142007-07-11 18:17:21 +03004874}
4875
Sean Christophersonb9904082020-03-21 13:25:55 -07004876struct kvm_cpu_compat_check {
4877 void *opaque;
4878 int *ret;
4879};
4880
4881static void check_processor_compat(void *data)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004882{
Sean Christophersonb9904082020-03-21 13:25:55 -07004883 struct kvm_cpu_compat_check *c = data;
4884
4885 *c->ret = kvm_arch_check_processor_compat(c->opaque);
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004886}
4887
Avi Kivity0ee75be2010-04-28 15:39:01 +03004888int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10004889 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890{
Sean Christophersonb9904082020-03-21 13:25:55 -07004891 struct kvm_cpu_compat_check c;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004892 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004893 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004895 r = kvm_arch_init(opaque);
4896 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08004897 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004898
Asias He7dac16c2013-05-08 10:57:29 +08004899 /*
4900 * kvm_arch_init makes sure there's at most one caller
4901 * for architectures that support multiple implementations,
4902 * like intel and amd on x86.
Paolo Bonzini36343f62016-10-26 13:35:56 +02004903 * kvm_arch_init must be called before kvm_irqfd_init to avoid creating
4904 * conflicts in case kvm is already setup for another implementation.
Asias He7dac16c2013-05-08 10:57:29 +08004905 */
Paolo Bonzini36343f62016-10-26 13:35:56 +02004906 r = kvm_irqfd_init();
4907 if (r)
4908 goto out_irqfd;
Asias He7dac16c2013-05-08 10:57:29 +08004909
Avi Kivity8437a6172009-06-06 14:52:35 -07004910 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10304911 r = -ENOMEM;
4912 goto out_free_0;
4913 }
4914
Sean Christophersonb9904082020-03-21 13:25:55 -07004915 r = kvm_arch_hardware_setup(opaque);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004916 if (r < 0)
Miaohe Linfaf0be22019-11-23 10:45:50 +08004917 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918
Sean Christophersonb9904082020-03-21 13:25:55 -07004919 c.ret = &r;
4920 c.opaque = opaque;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004921 for_each_online_cpu(cpu) {
Sean Christophersonb9904082020-03-21 13:25:55 -07004922 smp_call_function_single(cpu, check_processor_compat, &c, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03004923 if (r < 0)
Miaohe Linfaf0be22019-11-23 10:45:50 +08004924 goto out_free_2;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004925 }
4926
Thomas Gleixner73c1b412016-12-21 20:19:54 +01004927 r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "kvm/cpu:starting",
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004928 kvm_starting_cpu, kvm_dying_cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08004929 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08004930 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004931 register_reboot_notifier(&kvm_reboot_notifier);
4932
Rusty Russellc16f8622007-07-30 21:12:19 +10004933 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03004934 if (!vcpu_align)
4935 vcpu_align = __alignof__(struct kvm_vcpu);
Paolo Bonzini46515732017-10-26 15:45:46 +02004936 kvm_vcpu_cache =
4937 kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align,
4938 SLAB_ACCOUNT,
4939 offsetof(struct kvm_vcpu, arch),
4940 sizeof_field(struct kvm_vcpu, arch),
4941 NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10004942 if (!kvm_vcpu_cache) {
4943 r = -ENOMEM;
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004944 goto out_free_3;
Rusty Russellc16f8622007-07-30 21:12:19 +10004945 }
4946
Gleb Natapovaf585b92010-10-14 11:22:46 +02004947 r = kvm_async_pf_init();
4948 if (r)
4949 goto out_free;
4950
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01004952 kvm_vm_fops.owner = module;
4953 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004954
4955 r = misc_register(&kvm_dev);
4956 if (r) {
Xiubo Li1170adc2015-02-26 14:58:26 +08004957 pr_err("kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02004958 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004959 }
4960
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004961 register_syscore_ops(&kvm_syscore_ops);
4962
Avi Kivity15ad7142007-07-11 18:17:21 +03004963 kvm_preempt_ops.sched_in = kvm_sched_in;
4964 kvm_preempt_ops.sched_out = kvm_sched_out;
4965
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02004966 kvm_init_debug();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07004967
Paolo Bonzini3c3c29f2014-09-24 13:02:46 +02004968 r = kvm_vfio_ops_init();
4969 WARN_ON(r);
4970
Avi Kivityc7addb92007-09-16 18:58:32 +02004971 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004972
Gleb Natapovaf585b92010-10-14 11:22:46 +02004973out_unreg:
4974 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10004976 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08004977out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978 unregister_reboot_notifier(&kvm_reboot_notifier);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004979 cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
Zhang Xiantaod23087842007-11-29 15:35:39 +08004980out_free_2:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004981 kvm_arch_hardware_unsetup();
Miaohe Linfaf0be22019-11-23 10:45:50 +08004982out_free_1:
Rusty Russell7f59f492008-12-07 21:25:45 +10304983 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08004984out_free_0:
Cornelia Hucka0f155e2013-02-28 12:33:18 +01004985 kvm_irqfd_exit();
Paolo Bonzini36343f62016-10-26 13:35:56 +02004986out_irqfd:
Asias He7dac16c2013-05-08 10:57:29 +08004987 kvm_arch_exit();
4988out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08004989 return r;
4990}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004991EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004992
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004993void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004994{
Janosch Frank4bd33b52015-10-14 12:37:35 +02004995 debugfs_remove_recursive(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10004997 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02004998 kvm_async_pf_deinit();
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004999 unregister_syscore_ops(&kvm_syscore_ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000 unregister_reboot_notifier(&kvm_reboot_notifier);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00005001 cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09005002 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005003 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08005004 kvm_arch_exit();
Cornelia Hucka0f155e2013-02-28 12:33:18 +01005005 kvm_irqfd_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10305006 free_cpumask_var(cpus_hardware_enabled);
Wanpeng Li571ee1b2014-10-09 18:30:08 +08005007 kvm_vfio_ops_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08005008}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08005009EXPORT_SYMBOL_GPL(kvm_exit);
Junaid Shahidc57c8042019-11-04 12:22:02 +01005010
5011struct kvm_vm_worker_thread_context {
5012 struct kvm *kvm;
5013 struct task_struct *parent;
5014 struct completion init_done;
5015 kvm_vm_thread_fn_t thread_fn;
5016 uintptr_t data;
5017 int err;
5018};
5019
5020static int kvm_vm_worker_thread(void *context)
5021{
5022 /*
5023 * The init_context is allocated on the stack of the parent thread, so
5024 * we have to locally copy anything that is needed beyond initialization
5025 */
5026 struct kvm_vm_worker_thread_context *init_context = context;
5027 struct kvm *kvm = init_context->kvm;
5028 kvm_vm_thread_fn_t thread_fn = init_context->thread_fn;
5029 uintptr_t data = init_context->data;
5030 int err;
5031
5032 err = kthread_park(current);
5033 /* kthread_park(current) is never supposed to return an error */
5034 WARN_ON(err != 0);
5035 if (err)
5036 goto init_complete;
5037
5038 err = cgroup_attach_task_all(init_context->parent, current);
5039 if (err) {
5040 kvm_err("%s: cgroup_attach_task_all failed with err %d\n",
5041 __func__, err);
5042 goto init_complete;
5043 }
5044
5045 set_user_nice(current, task_nice(init_context->parent));
5046
5047init_complete:
5048 init_context->err = err;
5049 complete(&init_context->init_done);
5050 init_context = NULL;
5051
5052 if (err)
5053 return err;
5054
5055 /* Wait to be woken up by the spawner before proceeding. */
5056 kthread_parkme();
5057
5058 if (!kthread_should_stop())
5059 err = thread_fn(kvm, data);
5060
5061 return err;
5062}
5063
5064int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn,
5065 uintptr_t data, const char *name,
5066 struct task_struct **thread_ptr)
5067{
5068 struct kvm_vm_worker_thread_context init_context = {};
5069 struct task_struct *thread;
5070
5071 *thread_ptr = NULL;
5072 init_context.kvm = kvm;
5073 init_context.parent = current;
5074 init_context.thread_fn = thread_fn;
5075 init_context.data = data;
5076 init_completion(&init_context.init_done);
5077
5078 thread = kthread_run(kvm_vm_worker_thread, &init_context,
5079 "%s-%d", name, task_pid_nr(current));
5080 if (IS_ERR(thread))
5081 return PTR_ERR(thread);
5082
5083 /* kthread_run is never supposed to return NULL */
5084 WARN_ON(thread == NULL);
5085
5086 wait_for_completion(&init_context.init_done);
5087
5088 if (!init_context.err)
5089 *thread_ptr = thread;
5090
5091 return init_context.err;
5092}