blob: 737666db02de5bcc0028c55f52931d5d06c03d38 [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
Janosch Frank536a6f82016-05-18 13:26:23 +020066/* Worst case buffer size needed for holding an integer. */
67#define ITOA_MAX_LEN 12
68
Avi Kivity6aa8b732006-12-10 02:21:36 -080069MODULE_AUTHOR("Qumranet");
70MODULE_LICENSE("GPL");
71
David Hildenbrand920552b2015-09-18 12:34:53 +020072/* Architectures should define their poll value according to the halt latency */
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110073unsigned int halt_poll_ns = KVM_HALT_POLL_NS_DEFAULT;
Roman Storozhenko039c5d12017-06-27 12:51:18 +030074module_param(halt_poll_ns, uint, 0644);
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110075EXPORT_SYMBOL_GPL(halt_poll_ns);
Paolo Bonzinif7819512015-02-04 18:20:58 +010076
Wanpeng Liaca6ff22015-09-03 22:07:38 +080077/* Default doubles per-vcpu halt_poll_ns. */
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110078unsigned int halt_poll_ns_grow = 2;
Roman Storozhenko039c5d12017-06-27 12:51:18 +030079module_param(halt_poll_ns_grow, uint, 0644);
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110080EXPORT_SYMBOL_GPL(halt_poll_ns_grow);
Wanpeng Liaca6ff22015-09-03 22:07:38 +080081
Nir Weiner49113d32019-01-27 12:17:15 +020082/* The start value to grow halt_poll_ns from */
83unsigned int halt_poll_ns_grow_start = 10000; /* 10us */
84module_param(halt_poll_ns_grow_start, uint, 0644);
85EXPORT_SYMBOL_GPL(halt_poll_ns_grow_start);
86
Wanpeng Liaca6ff22015-09-03 22:07:38 +080087/* Default resets per-vcpu halt_poll_ns . */
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110088unsigned int halt_poll_ns_shrink;
Roman Storozhenko039c5d12017-06-27 12:51:18 +030089module_param(halt_poll_ns_shrink, uint, 0644);
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110090EXPORT_SYMBOL_GPL(halt_poll_ns_shrink);
Wanpeng Liaca6ff22015-09-03 22:07:38 +080091
Marcelo Tosattifa40a822009-06-04 15:08:24 -030092/*
93 * Ordering of locks:
94 *
Xiubo Lib7d409d2015-02-26 14:58:24 +080095 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030096 */
97
Junaid Shahid0d9ce162019-01-03 17:14:28 -080098DEFINE_MUTEX(kvm_lock);
Paolo Bonzini4a937f92013-09-10 12:58:35 +020099static DEFINE_RAW_SPINLOCK(kvm_count_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800100LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -0800101
Rusty Russell7f59f492008-12-07 21:25:45 +1030102static cpumask_var_t cpus_hardware_enabled;
Xiubo Lif4fee932015-02-26 14:58:21 +0800103static int kvm_usage_count;
Alexander Graf10474ae2009-09-15 11:37:46 +0200104static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +0300105
Sean Christophersonaaba2982019-12-18 13:55:16 -0800106static struct kmem_cache *kvm_vcpu_cache;
Avi Kivity1165f5f2007-04-19 17:27:43 +0300107
Avi Kivity15ad7142007-07-11 18:17:21 +0300108static __read_mostly struct preempt_ops kvm_preempt_ops;
Paolo Bonzini7495e222020-01-09 09:57:19 -0500109static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_running_vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300110
Hollis Blanchard76f7c872008-04-15 16:05:42 -0500111struct dentry *kvm_debugfs_dir;
Paul Mackerrase23a8082015-03-28 14:21:01 +1100112EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
Janosch Frank536a6f82016-05-18 13:26:23 +0200114static int kvm_debugfs_num_entries;
Milan Pandurov09cbcef2019-12-13 14:07:21 +0100115static const struct file_operations stat_fops_per_vm;
Janosch Frank536a6f82016-05-18 13:26:23 +0200116
Avi Kivitybccf2152007-02-21 18:04:26 +0200117static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
118 unsigned long arg);
Christian Borntraegerde8e5d72015-02-03 09:35:15 +0100119#ifdef CONFIG_KVM_COMPAT
Alexander Graf1dda6062011-06-08 02:45:37 +0200120static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
121 unsigned long arg);
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +0100122#define KVM_COMPAT(c) .compat_ioctl = (c)
123#else
Marc Zyngier9cb09e72019-11-14 13:17:39 +0000124/*
125 * For architectures that don't implement a compat infrastructure,
126 * adopt a double line of defense:
127 * - Prevent a compat task from opening /dev/kvm
128 * - If the open has been done by a 64bit task, and the KVM fd
129 * passed to a compat task, let the ioctls fail.
130 */
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +0100131static long kvm_no_compat_ioctl(struct file *file, unsigned int ioctl,
132 unsigned long arg) { return -EINVAL; }
Marc Zyngierb9876e62019-11-13 16:05:23 +0000133
134static int kvm_no_compat_open(struct inode *inode, struct file *file)
135{
136 return is_compat_task() ? -ENODEV : 0;
137}
138#define KVM_COMPAT(c) .compat_ioctl = kvm_no_compat_ioctl, \
139 .open = kvm_no_compat_open
Alexander Graf1dda6062011-06-08 02:45:37 +0200140#endif
Alexander Graf10474ae2009-09-15 11:37:46 +0200141static int hardware_enable_all(void);
142static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +0200143
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200144static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
Stephen Hemminger79408762013-12-29 12:12:29 -0800145
Paolo Bonzinibc009e42015-05-26 12:43:41 +0200146static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot, gfn_t gfn);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200147
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{
420 kvm_arch_vcpu_destroy(vcpu);
Sean Christophersone529ef62019-12-18 13:55:15 -0800421
Sean Christopherson9941d222019-12-18 13:55:29 -0800422 /*
423 * No need for rcu_read_lock as VCPU_RUN is the only place that changes
424 * the vcpu->pid pointer, and at destruction time all file descriptors
425 * are already gone.
426 */
427 put_pid(rcu_dereference_protected(vcpu->pid, 1));
428
Sean Christopherson8bd826d2019-12-18 13:55:30 -0800429 free_page((unsigned long)vcpu->run);
Sean Christophersone529ef62019-12-18 13:55:15 -0800430 kmem_cache_free(kvm_vcpu_cache, vcpu);
Sean Christopherson4543bdc2019-12-18 13:55:14 -0800431}
432EXPORT_SYMBOL_GPL(kvm_vcpu_destroy);
433
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200434#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
435static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
436{
437 return container_of(mn, struct kvm, mmu_notifier);
438}
439
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +0900440static void kvm_mmu_notifier_invalidate_range(struct mmu_notifier *mn,
441 struct mm_struct *mm,
442 unsigned long start, unsigned long end)
443{
444 struct kvm *kvm = mmu_notifier_to_kvm(mn);
445 int idx;
446
447 idx = srcu_read_lock(&kvm->srcu);
448 kvm_arch_mmu_notifier_invalidate_range(kvm, start, end);
449 srcu_read_unlock(&kvm->srcu, idx);
450}
451
Izik Eidus3da0dd42009-09-23 21:47:18 +0300452static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
453 struct mm_struct *mm,
454 unsigned long address,
455 pte_t pte)
456{
457 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200458 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300459
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200460 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300461 spin_lock(&kvm->mmu_lock);
462 kvm->mmu_notifier_seq++;
Lan Tianyu0cf853c2018-12-06 21:21:11 +0800463
464 if (kvm_set_spte_hva(kvm, address, pte))
465 kvm_flush_remote_tlbs(kvm);
466
Izik Eidus3da0dd42009-09-23 21:47:18 +0300467 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200468 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300469}
470
Michal Hocko93065ac2018-08-21 21:52:33 -0700471static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800472 const struct mmu_notifier_range *range)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200473{
474 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200475 int need_tlb_flush = 0, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200476
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200477 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200478 spin_lock(&kvm->mmu_lock);
479 /*
480 * The count increase must become visible at unlock time as no
481 * spte can be established without taking the mmu_lock and
482 * count is also read inside the mmu_lock critical section.
483 */
484 kvm->mmu_notifier_count++;
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800485 need_tlb_flush = kvm_unmap_hva_range(kvm, range->start, range->end);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800486 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200487 /* we've to flush the tlb before the pages can be freed */
488 if (need_tlb_flush)
489 kvm_flush_remote_tlbs(kvm);
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900490
491 spin_unlock(&kvm->mmu_lock);
492 srcu_read_unlock(&kvm->srcu, idx);
Michal Hocko93065ac2018-08-21 21:52:33 -0700493
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +0900494 return 0;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200495}
496
497static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800498 const struct mmu_notifier_range *range)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200499{
500 struct kvm *kvm = mmu_notifier_to_kvm(mn);
501
502 spin_lock(&kvm->mmu_lock);
503 /*
504 * This sequence increase will notify the kvm page fault that
505 * the page that is going to be mapped in the spte could have
506 * been freed.
507 */
508 kvm->mmu_notifier_seq++;
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000509 smp_wmb();
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200510 /*
511 * The above sequence increase must be visible before the
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000512 * below count decrease, which is ensured by the smp_wmb above
513 * in conjunction with the smp_rmb in mmu_notifier_retry().
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200514 */
515 kvm->mmu_notifier_count--;
516 spin_unlock(&kvm->mmu_lock);
517
518 BUG_ON(kvm->mmu_notifier_count < 0);
519}
520
521static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
522 struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700523 unsigned long start,
524 unsigned long end)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200525{
526 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200527 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200528
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200529 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200530 spin_lock(&kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200531
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700532 young = kvm_age_hva(kvm, start, end);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200533 if (young)
534 kvm_flush_remote_tlbs(kvm);
535
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900536 spin_unlock(&kvm->mmu_lock);
537 srcu_read_unlock(&kvm->srcu, idx);
538
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200539 return young;
540}
541
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700542static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,
543 struct mm_struct *mm,
544 unsigned long start,
545 unsigned long end)
546{
547 struct kvm *kvm = mmu_notifier_to_kvm(mn);
548 int young, idx;
549
550 idx = srcu_read_lock(&kvm->srcu);
551 spin_lock(&kvm->mmu_lock);
552 /*
553 * Even though we do not flush TLB, this will still adversely
554 * affect performance on pre-Haswell Intel EPT, where there is
555 * no EPT Access Bit to clear so that we have to tear down EPT
556 * tables instead. If we find this unacceptable, we can always
557 * add a parameter to kvm_age_hva so that it effectively doesn't
558 * do anything on clear_young.
559 *
560 * Also note that currently we never issue secondary TLB flushes
561 * from clear_young, leaving this job up to the regular system
562 * cadence. If we find this inaccurate, we might come up with a
563 * more sophisticated heuristic later.
564 */
565 young = kvm_age_hva(kvm, start, end);
566 spin_unlock(&kvm->mmu_lock);
567 srcu_read_unlock(&kvm->srcu, idx);
568
569 return young;
570}
571
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800572static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
573 struct mm_struct *mm,
574 unsigned long address)
575{
576 struct kvm *kvm = mmu_notifier_to_kvm(mn);
577 int young, idx;
578
579 idx = srcu_read_lock(&kvm->srcu);
580 spin_lock(&kvm->mmu_lock);
581 young = kvm_test_age_hva(kvm, address);
582 spin_unlock(&kvm->mmu_lock);
583 srcu_read_unlock(&kvm->srcu, idx);
584
585 return young;
586}
587
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100588static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
589 struct mm_struct *mm)
590{
591 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800592 int idx;
593
594 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300595 kvm_arch_flush_shadow_all(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800596 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100597}
598
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200599static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
Eiichi Tsukatae649b3f2020-06-06 13:26:27 +0900600 .invalidate_range = kvm_mmu_notifier_invalidate_range,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200601 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
602 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
603 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700604 .clear_young = kvm_mmu_notifier_clear_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800605 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300606 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100607 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200608};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200609
610static int kvm_init_mmu_notifier(struct kvm *kvm)
611{
612 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
613 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
614}
615
616#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
617
618static int kvm_init_mmu_notifier(struct kvm *kvm)
619{
620 return 0;
621}
622
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200623#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
624
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200625static struct kvm_memslots *kvm_alloc_memslots(void)
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800626{
627 int i;
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200628 struct kvm_memslots *slots;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800629
Ben Gardonb12ce362019-02-11 11:02:49 -0800630 slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL_ACCOUNT);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200631 if (!slots)
632 return NULL;
633
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800634 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
Sean Christopherson36947252020-02-18 13:07:32 -0800635 slots->id_to_index[i] = -1;
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200636
637 return slots;
638}
639
640static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
641{
642 if (!memslot->dirty_bitmap)
643 return;
644
645 kvfree(memslot->dirty_bitmap);
646 memslot->dirty_bitmap = NULL;
647}
648
Sean Christophersone96c81e2020-02-18 13:07:27 -0800649static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200650{
Sean Christophersone96c81e2020-02-18 13:07:27 -0800651 kvm_destroy_dirty_bitmap(slot);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200652
Sean Christophersone96c81e2020-02-18 13:07:27 -0800653 kvm_arch_free_memslot(kvm, slot);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200654
Sean Christophersone96c81e2020-02-18 13:07:27 -0800655 slot->flags = 0;
656 slot->npages = 0;
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200657}
658
659static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots)
660{
661 struct kvm_memory_slot *memslot;
662
663 if (!slots)
664 return;
665
666 kvm_for_each_memslot(memslot, slots)
Sean Christophersone96c81e2020-02-18 13:07:27 -0800667 kvm_free_memslot(kvm, memslot);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200668
669 kvfree(slots);
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800670}
671
Janosch Frank536a6f82016-05-18 13:26:23 +0200672static void kvm_destroy_vm_debugfs(struct kvm *kvm)
673{
674 int i;
675
676 if (!kvm->debugfs_dentry)
677 return;
678
679 debugfs_remove_recursive(kvm->debugfs_dentry);
680
Luiz Capitulino9d5a1dc2016-09-07 14:47:21 -0400681 if (kvm->debugfs_stat_data) {
682 for (i = 0; i < kvm_debugfs_num_entries; i++)
683 kfree(kvm->debugfs_stat_data[i]);
684 kfree(kvm->debugfs_stat_data);
685 }
Janosch Frank536a6f82016-05-18 13:26:23 +0200686}
687
688static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
689{
690 char dir_name[ITOA_MAX_LEN * 2];
691 struct kvm_stat_data *stat_data;
692 struct kvm_stats_debugfs_item *p;
693
694 if (!debugfs_initialized())
695 return 0;
696
697 snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd);
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +0200698 kvm->debugfs_dentry = debugfs_create_dir(dir_name, kvm_debugfs_dir);
Janosch Frank536a6f82016-05-18 13:26:23 +0200699
700 kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries,
701 sizeof(*kvm->debugfs_stat_data),
Ben Gardonb12ce362019-02-11 11:02:49 -0800702 GFP_KERNEL_ACCOUNT);
Janosch Frank536a6f82016-05-18 13:26:23 +0200703 if (!kvm->debugfs_stat_data)
704 return -ENOMEM;
705
706 for (p = debugfs_entries; p->name; p++) {
Ben Gardonb12ce362019-02-11 11:02:49 -0800707 stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
Janosch Frank536a6f82016-05-18 13:26:23 +0200708 if (!stat_data)
709 return -ENOMEM;
710
711 stat_data->kvm = kvm;
Milan Pandurov09cbcef2019-12-13 14:07:21 +0100712 stat_data->dbgfs_item = p;
Janosch Frank536a6f82016-05-18 13:26:23 +0200713 kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
Milan Pandurov09cbcef2019-12-13 14:07:21 +0100714 debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p),
715 kvm->debugfs_dentry, stat_data,
716 &stat_fops_per_vm);
Janosch Frank536a6f82016-05-18 13:26:23 +0200717 }
718 return 0;
719}
720
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100721/*
722 * Called after the VM is otherwise initialized, but just before adding it to
723 * the vm_list.
724 */
725int __weak kvm_arch_post_init_vm(struct kvm *kvm)
726{
727 return 0;
728}
729
730/*
731 * Called just after removing the VM from the vm_list, but before doing any
732 * other destruction.
733 */
734void __weak kvm_arch_pre_destroy_vm(struct kvm *kvm)
735{
736}
737
Carsten Ottee08b9632012-01-04 10:25:20 +0100738static struct kvm *kvm_create_vm(unsigned long type)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100740 struct kvm *kvm = kvm_arch_alloc_vm();
Jim Mattson91219232019-10-24 16:03:26 -0700741 int r = -ENOMEM;
742 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100744 if (!kvm)
745 return ERR_PTR(-ENOMEM);
746
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100747 spin_lock_init(&kvm->mmu_lock);
Vegard Nossumf1f10072017-02-27 14:30:07 -0800748 mmgrab(current->mm);
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100749 kvm->mm = current->mm;
750 kvm_eventfd_init(kvm);
751 mutex_init(&kvm->lock);
752 mutex_init(&kvm->irq_lock);
753 mutex_init(&kvm->slots_lock);
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100754 INIT_LIST_HEAD(&kvm->devices);
755
Jim Mattson91219232019-10-24 16:03:26 -0700756 BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
757
Paolo Bonzini8a441192019-11-04 12:16:49 +0100758 if (init_srcu_struct(&kvm->srcu))
759 goto out_err_no_srcu;
760 if (init_srcu_struct(&kvm->irq_srcu))
761 goto out_err_no_irq_srcu;
762
Paolo Bonzinie2d3fca2019-11-04 13:23:53 +0100763 refcount_set(&kvm->users_count, 1);
Jim Mattson91219232019-10-24 16:03:26 -0700764 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
765 struct kvm_memslots *slots = kvm_alloc_memslots();
766
767 if (!slots)
Jim Mattsona97b0e72019-10-25 13:34:58 +0200768 goto out_err_no_arch_destroy_vm;
Jim Mattson91219232019-10-24 16:03:26 -0700769 /* Generations must be different for each address space. */
770 slots->generation = i;
771 rcu_assign_pointer(kvm->memslots[i], slots);
772 }
773
774 for (i = 0; i < KVM_NR_BUSES; i++) {
775 rcu_assign_pointer(kvm->buses[i],
776 kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT));
777 if (!kvm->buses[i])
Jim Mattsona97b0e72019-10-25 13:34:58 +0200778 goto out_err_no_arch_destroy_vm;
Jim Mattson91219232019-10-24 16:03:26 -0700779 }
780
David Matlackacd05782020-04-17 15:14:46 -0700781 kvm->max_halt_poll_ns = halt_poll_ns;
782
Carsten Ottee08b9632012-01-04 10:25:20 +0100783 r = kvm_arch_init_vm(kvm, type);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100784 if (r)
Jim Mattsona97b0e72019-10-25 13:34:58 +0200785 goto out_err_no_arch_destroy_vm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200786
787 r = hardware_enable_all();
788 if (r)
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100789 goto out_err_no_disable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200790
Paolo Bonzinic77dcac2014-08-06 14:24:45 +0200791#ifdef CONFIG_HAVE_KVM_IRQFD
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300792 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200793#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700795 r = kvm_init_mmu_notifier(kvm);
796 if (r)
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100797 goto out_err_no_mmu_notifier;
798
799 r = kvm_arch_post_init_vm(kvm);
800 if (r)
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700801 goto out_err;
802
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800803 mutex_lock(&kvm_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000804 list_add(&kvm->vm_list, &vm_list);
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800805 mutex_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100806
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +0200807 preempt_notifier_inc();
808
Avi Kivityf17abe92007-02-21 19:28:04 +0200809 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200810
811out_err:
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100812#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
813 if (kvm->mmu_notifier.ops)
814 mmu_notifier_unregister(&kvm->mmu_notifier, current->mm);
815#endif
816out_err_no_mmu_notifier:
Alexander Graf10474ae2009-09-15 11:37:46 +0200817 hardware_disable_all();
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100818out_err_no_disable:
Jim Mattsona97b0e72019-10-25 13:34:58 +0200819 kvm_arch_destroy_vm(kvm);
Jim Mattsona97b0e72019-10-25 13:34:58 +0200820out_err_no_arch_destroy_vm:
Paolo Bonzinie2d3fca2019-11-04 13:23:53 +0100821 WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200822 for (i = 0; i < KVM_NR_BUSES; i++)
Paolo Bonzini3898da92017-08-02 17:55:54 +0200823 kfree(kvm_get_bus(kvm, i));
Paolo Bonzinif481b062015-05-17 17:30:37 +0200824 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
Paolo Bonzini3898da92017-08-02 17:55:54 +0200825 kvm_free_memslots(kvm, __kvm_memslots(kvm, i));
Paolo Bonzini8a441192019-11-04 12:16:49 +0100826 cleanup_srcu_struct(&kvm->irq_srcu);
827out_err_no_irq_srcu:
828 cleanup_srcu_struct(&kvm->srcu);
829out_err_no_srcu:
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100830 kvm_arch_free_vm(kvm);
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100831 mmdrop(current->mm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200832 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200833}
834
Scott Wood07f0a7b2013-04-25 14:11:23 +0000835static void kvm_destroy_devices(struct kvm *kvm)
836{
Geliang Tange6e3b5a2016-01-01 19:47:12 +0800837 struct kvm_device *dev, *tmp;
Scott Wood07f0a7b2013-04-25 14:11:23 +0000838
Christoffer Dalla28ebea2016-08-09 19:13:01 +0200839 /*
840 * We do not need to take the kvm->lock here, because nobody else
841 * has a reference to the struct kvm at this point and therefore
842 * cannot access the devices list anyhow.
843 */
Geliang Tange6e3b5a2016-01-01 19:47:12 +0800844 list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) {
845 list_del(&dev->vm_node);
Scott Wood07f0a7b2013-04-25 14:11:23 +0000846 dev->ops->destroy(dev);
847 }
848}
849
Avi Kivityf17abe92007-02-21 19:28:04 +0200850static void kvm_destroy_vm(struct kvm *kvm)
851{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200852 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200853 struct mm_struct *mm = kvm->mm;
854
Claudio Imbrenda286de8f2017-07-12 17:56:44 +0200855 kvm_uevent_notify_change(KVM_EVENT_DESTROY_VM, kvm);
Janosch Frank536a6f82016-05-18 13:26:23 +0200856 kvm_destroy_vm_debugfs(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800857 kvm_arch_sync_events(kvm);
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800858 mutex_lock(&kvm_lock);
Avi Kivity133de902007-02-12 00:54:44 -0800859 list_del(&kvm->vm_list);
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800860 mutex_unlock(&kvm_lock);
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100861 kvm_arch_pre_destroy_vm(kvm);
862
Avi Kivity399ec802008-11-19 13:58:46 +0200863 kvm_free_irq_routing(kvm);
Peter Xudf630b82017-03-15 16:01:17 +0800864 for (i = 0; i < KVM_NR_BUSES; i++) {
Paolo Bonzini3898da92017-08-02 17:55:54 +0200865 struct kvm_io_bus *bus = kvm_get_bus(kvm, i);
Christian Borntraeger4a12f952017-07-07 10:51:38 +0200866
Christian Borntraeger4a12f952017-07-07 10:51:38 +0200867 if (bus)
868 kvm_io_bus_destroy(bus);
Peter Xudf630b82017-03-15 16:01:17 +0800869 kvm->buses[i] = NULL;
870 }
Avi Kivity980da6c2009-12-20 15:13:43 +0200871 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200872#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
873 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200874#else
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300875 kvm_arch_flush_shadow_all(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200876#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800877 kvm_arch_destroy_vm(kvm);
Scott Wood07f0a7b2013-04-25 14:11:23 +0000878 kvm_destroy_devices(kvm);
Paolo Bonzinif481b062015-05-17 17:30:37 +0200879 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
Paolo Bonzini3898da92017-08-02 17:55:54 +0200880 kvm_free_memslots(kvm, __kvm_memslots(kvm, i));
Paolo Bonzini820b3fc2014-06-03 13:44:17 +0200881 cleanup_srcu_struct(&kvm->irq_srcu);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100882 cleanup_srcu_struct(&kvm->srcu);
883 kvm_arch_free_vm(kvm);
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +0200884 preempt_notifier_dec();
Alexander Graf10474ae2009-09-15 11:37:46 +0200885 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200886 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200887}
888
Izik Eidusd39f13b2008-03-30 16:01:25 +0300889void kvm_get_kvm(struct kvm *kvm)
890{
Elena Reshetovae3736c32017-02-20 13:06:21 +0200891 refcount_inc(&kvm->users_count);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300892}
893EXPORT_SYMBOL_GPL(kvm_get_kvm);
894
895void kvm_put_kvm(struct kvm *kvm)
896{
Elena Reshetovae3736c32017-02-20 13:06:21 +0200897 if (refcount_dec_and_test(&kvm->users_count))
Izik Eidusd39f13b2008-03-30 16:01:25 +0300898 kvm_destroy_vm(kvm);
899}
900EXPORT_SYMBOL_GPL(kvm_put_kvm);
901
Sean Christopherson149487b2019-10-21 15:58:42 -0700902/*
903 * Used to put a reference that was taken on behalf of an object associated
904 * with a user-visible file descriptor, e.g. a vcpu or device, if installation
905 * of the new file descriptor fails and the reference cannot be transferred to
906 * its final owner. In such cases, the caller is still actively using @kvm and
907 * will fail miserably if the refcount unexpectedly hits zero.
908 */
909void kvm_put_kvm_no_destroy(struct kvm *kvm)
910{
911 WARN_ON(refcount_dec_and_test(&kvm->users_count));
912}
913EXPORT_SYMBOL_GPL(kvm_put_kvm_no_destroy);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300914
Avi Kivityf17abe92007-02-21 19:28:04 +0200915static int kvm_vm_release(struct inode *inode, struct file *filp)
916{
917 struct kvm *kvm = filp->private_data;
918
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400919 kvm_irqfd_release(kvm);
920
Izik Eidusd39f13b2008-03-30 16:01:25 +0300921 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 return 0;
923}
924
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900925/*
926 * Allocation size is twice as large as the actual dirty bitmap size.
Sean Christopherson0dff0842020-02-18 13:07:29 -0800927 * See kvm_vm_ioctl_get_dirty_log() why this is needed.
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900928 */
Jay Zhou3c9bd402020-02-27 09:32:27 +0800929static int kvm_alloc_dirty_bitmap(struct kvm_memory_slot *memslot)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900930{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900931 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900932
Ben Gardonb12ce362019-02-11 11:02:49 -0800933 memslot->dirty_bitmap = kvzalloc(dirty_bytes, GFP_KERNEL_ACCOUNT);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900934 if (!memslot->dirty_bitmap)
935 return -ENOMEM;
936
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900937 return 0;
938}
939
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800940/*
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800941 * Delete a memslot by decrementing the number of used slots and shifting all
942 * other entries in the array forward one spot.
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800943 */
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800944static inline void kvm_memslot_delete(struct kvm_memslots *slots,
945 struct kvm_memory_slot *memslot)
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800946{
Igor Mammedov063584d2014-11-13 23:00:13 +0000947 struct kvm_memory_slot *mslots = slots->memslots;
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800948 int i;
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800949
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800950 if (WARN_ON(slots->id_to_index[memslot->id] == -1))
951 return;
Igor Mammedov0e60b072014-12-01 17:29:26 +0000952
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800953 slots->used_slots--;
954
Sean Christopherson0774a962020-03-20 13:55:40 -0700955 if (atomic_read(&slots->lru_slot) >= slots->used_slots)
956 atomic_set(&slots->lru_slot, 0);
957
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800958 for (i = slots->id_to_index[memslot->id]; i < slots->used_slots; i++) {
Igor Mammedov7f379cf2014-12-01 17:29:24 +0000959 mslots[i] = mslots[i + 1];
960 slots->id_to_index[mslots[i].id] = i;
Igor Mammedov7f379cf2014-12-01 17:29:24 +0000961 }
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800962 mslots[i] = *memslot;
963 slots->id_to_index[memslot->id] = -1;
964}
965
966/*
967 * "Insert" a new memslot by incrementing the number of used slots. Returns
968 * the new slot's initial index into the memslots array.
969 */
970static inline int kvm_memslot_insert_back(struct kvm_memslots *slots)
971{
972 return slots->used_slots++;
973}
974
975/*
976 * Move a changed memslot backwards in the array by shifting existing slots
977 * with a higher GFN toward the front of the array. Note, the changed memslot
978 * itself is not preserved in the array, i.e. not swapped at this time, only
979 * its new index into the array is tracked. Returns the changed memslot's
980 * current index into the memslots array.
981 */
982static inline int kvm_memslot_move_backward(struct kvm_memslots *slots,
983 struct kvm_memory_slot *memslot)
984{
985 struct kvm_memory_slot *mslots = slots->memslots;
986 int i;
987
988 if (WARN_ON_ONCE(slots->id_to_index[memslot->id] == -1) ||
989 WARN_ON_ONCE(!slots->used_slots))
990 return -1;
Paolo Bonziniefbeec72014-12-27 18:01:00 +0100991
992 /*
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800993 * Move the target memslot backward in the array by shifting existing
994 * memslots with a higher GFN (than the target memslot) towards the
995 * front of the array.
Paolo Bonziniefbeec72014-12-27 18:01:00 +0100996 */
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800997 for (i = slots->id_to_index[memslot->id]; i < slots->used_slots - 1; i++) {
998 if (memslot->base_gfn > mslots[i + 1].base_gfn)
999 break;
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +08001000
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001001 WARN_ON_ONCE(memslot->base_gfn == mslots[i + 1].base_gfn);
1002
1003 /* Shift the next memslot forward one and update its index. */
1004 mslots[i] = mslots[i + 1];
1005 slots->id_to_index[mslots[i].id] = i;
1006 }
1007 return i;
1008}
1009
1010/*
1011 * Move a changed memslot forwards in the array by shifting existing slots with
1012 * a lower GFN toward the back of the array. Note, the changed memslot itself
1013 * is not preserved in the array, i.e. not swapped at this time, only its new
1014 * index into the array is tracked. Returns the changed memslot's final index
1015 * into the memslots array.
1016 */
1017static inline int kvm_memslot_move_forward(struct kvm_memslots *slots,
1018 struct kvm_memory_slot *memslot,
1019 int start)
1020{
1021 struct kvm_memory_slot *mslots = slots->memslots;
1022 int i;
1023
1024 for (i = start; i > 0; i--) {
1025 if (memslot->base_gfn < mslots[i - 1].base_gfn)
1026 break;
1027
1028 WARN_ON_ONCE(memslot->base_gfn == mslots[i - 1].base_gfn);
1029
1030 /* Shift the next memslot back one and update its index. */
1031 mslots[i] = mslots[i - 1];
1032 slots->id_to_index[mslots[i].id] = i;
1033 }
1034 return i;
1035}
1036
1037/*
1038 * Re-sort memslots based on their GFN to account for an added, deleted, or
1039 * moved memslot. Sorting memslots by GFN allows using a binary search during
1040 * memslot lookup.
1041 *
1042 * IMPORTANT: Slots are sorted from highest GFN to lowest GFN! I.e. the entry
1043 * at memslots[0] has the highest GFN.
1044 *
1045 * The sorting algorithm takes advantage of having initially sorted memslots
1046 * and knowing the position of the changed memslot. Sorting is also optimized
1047 * by not swapping the updated memslot and instead only shifting other memslots
1048 * and tracking the new index for the update memslot. Only once its final
1049 * index is known is the updated memslot copied into its position in the array.
1050 *
1051 * - When deleting a memslot, the deleted memslot simply needs to be moved to
1052 * the end of the array.
1053 *
1054 * - When creating a memslot, the algorithm "inserts" the new memslot at the
1055 * end of the array and then it forward to its correct location.
1056 *
1057 * - When moving a memslot, the algorithm first moves the updated memslot
1058 * backward to handle the scenario where the memslot's GFN was changed to a
1059 * lower value. update_memslots() then falls through and runs the same flow
1060 * as creating a memslot to move the memslot forward to handle the scenario
1061 * where its GFN was changed to a higher value.
1062 *
1063 * Note, slots are sorted from highest->lowest instead of lowest->highest for
1064 * historical reasons. Originally, invalid memslots where denoted by having
1065 * GFN=0, thus sorting from highest->lowest naturally sorted invalid memslots
1066 * to the end of the array. The current algorithm uses dedicated logic to
1067 * delete a memslot and thus does not rely on invalid memslots having GFN=0.
1068 *
1069 * The other historical motiviation for highest->lowest was to improve the
1070 * performance of memslot lookup. KVM originally used a linear search starting
1071 * at memslots[0]. On x86, the largest memslot usually has one of the highest,
1072 * if not *the* highest, GFN, as the bulk of the guest's RAM is located in a
1073 * single memslot above the 4gb boundary. As the largest memslot is also the
1074 * most likely to be referenced, sorting it to the front of the array was
1075 * advantageous. The current binary search starts from the middle of the array
1076 * and uses an LRU pointer to improve performance for all memslots and GFNs.
1077 */
1078static void update_memslots(struct kvm_memslots *slots,
1079 struct kvm_memory_slot *memslot,
1080 enum kvm_mr_change change)
1081{
1082 int i;
1083
1084 if (change == KVM_MR_DELETE) {
1085 kvm_memslot_delete(slots, memslot);
1086 } else {
1087 if (change == KVM_MR_CREATE)
1088 i = kvm_memslot_insert_back(slots);
1089 else
1090 i = kvm_memslot_move_backward(slots, memslot);
1091 i = kvm_memslot_move_forward(slots, memslot, i);
1092
1093 /*
1094 * Copy the memslot to its new position in memslots and update
1095 * its index accordingly.
1096 */
1097 slots->memslots[i] = *memslot;
1098 slots->id_to_index[memslot->id] = i;
1099 }
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +08001100}
1101
Paolo Bonzini09170a42015-05-18 13:59:39 +02001102static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem)
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001103{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001104 u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
1105
Christoffer Dall0f8a4de2014-08-26 14:00:37 +02001106#ifdef __KVM_HAVE_READONLY_MEM
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001107 valid_flags |= KVM_MEM_READONLY;
1108#endif
1109
1110 if (mem->flags & ~valid_flags)
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001111 return -EINVAL;
1112
1113 return 0;
1114}
1115
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001116static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
Paolo Bonzinif481b062015-05-17 17:30:37 +02001117 int as_id, struct kvm_memslots *slots)
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001118{
Paolo Bonzinif481b062015-05-17 17:30:37 +02001119 struct kvm_memslots *old_memslots = __kvm_memslots(kvm, as_id);
Sean Christopherson361209e2019-02-05 13:01:14 -08001120 u64 gen = old_memslots->generation;
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001121
Sean Christopherson361209e2019-02-05 13:01:14 -08001122 WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS);
1123 slots->generation = gen | KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS;
David Matlackee3d1572014-08-18 15:46:06 -07001124
Paolo Bonzinif481b062015-05-17 17:30:37 +02001125 rcu_assign_pointer(kvm->memslots[as_id], slots);
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001126 synchronize_srcu_expedited(&kvm->srcu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09001127
David Matlackee3d1572014-08-18 15:46:06 -07001128 /*
Sean Christopherson361209e2019-02-05 13:01:14 -08001129 * Increment the new memslot generation a second time, dropping the
Miaohe Lin00116792019-12-11 14:26:23 +08001130 * update in-progress flag and incrementing the generation based on
Sean Christopherson361209e2019-02-05 13:01:14 -08001131 * the number of address spaces. This provides a unique and easily
1132 * identifiable generation number while the memslots are in flux.
1133 */
1134 gen = slots->generation & ~KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS;
1135
1136 /*
Paolo Bonzini4bd518f2017-02-03 20:44:51 -08001137 * Generations must be unique even across address spaces. We do not need
1138 * a global counter for that, instead the generation space is evenly split
1139 * across address spaces. For example, with two address spaces, address
Sean Christopherson164bf7e2019-02-05 13:01:18 -08001140 * space 0 will use generations 0, 2, 4, ... while address space 1 will
1141 * use generations 1, 3, 5, ...
David Matlackee3d1572014-08-18 15:46:06 -07001142 */
Sean Christopherson164bf7e2019-02-05 13:01:18 -08001143 gen += KVM_ADDRESS_SPACE_NUM;
David Matlackee3d1572014-08-18 15:46:06 -07001144
Sean Christopherson15248252019-02-05 12:54:17 -08001145 kvm_arch_memslots_updated(kvm, gen);
1146
1147 slots->generation = gen;
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09001148
1149 return old_memslots;
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001150}
1151
Sean Christopherson36947252020-02-18 13:07:32 -08001152/*
1153 * Note, at a minimum, the current number of used slots must be allocated, even
1154 * when deleting a memslot, as we need a complete duplicate of the memslots for
1155 * use when invalidating a memslot prior to deleting/moving the memslot.
1156 */
1157static struct kvm_memslots *kvm_dup_memslots(struct kvm_memslots *old,
1158 enum kvm_mr_change change)
1159{
1160 struct kvm_memslots *slots;
1161 size_t old_size, new_size;
1162
1163 old_size = sizeof(struct kvm_memslots) +
1164 (sizeof(struct kvm_memory_slot) * old->used_slots);
1165
1166 if (change == KVM_MR_CREATE)
1167 new_size = old_size + sizeof(struct kvm_memory_slot);
1168 else
1169 new_size = old_size;
1170
1171 slots = kvzalloc(new_size, GFP_KERNEL_ACCOUNT);
1172 if (likely(slots))
1173 memcpy(slots, old, old_size);
1174
1175 return slots;
1176}
1177
Sean Christophersoncf47f502020-02-18 13:07:23 -08001178static int kvm_set_memslot(struct kvm *kvm,
1179 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -08001180 struct kvm_memory_slot *old,
Sean Christophersoncf47f502020-02-18 13:07:23 -08001181 struct kvm_memory_slot *new, int as_id,
1182 enum kvm_mr_change change)
1183{
1184 struct kvm_memory_slot *slot;
1185 struct kvm_memslots *slots;
1186 int r;
1187
Sean Christopherson36947252020-02-18 13:07:32 -08001188 slots = kvm_dup_memslots(__kvm_memslots(kvm, as_id), change);
Sean Christophersoncf47f502020-02-18 13:07:23 -08001189 if (!slots)
1190 return -ENOMEM;
Sean Christophersoncf47f502020-02-18 13:07:23 -08001191
1192 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
1193 /*
1194 * Note, the INVALID flag needs to be in the appropriate entry
1195 * in the freshly allocated memslots, not in @old or @new.
1196 */
1197 slot = id_to_memslot(slots, old->id);
1198 slot->flags |= KVM_MEMSLOT_INVALID;
1199
1200 /*
1201 * We can re-use the old memslots, the only difference from the
1202 * newly installed memslots is the invalid flag, which will get
1203 * dropped by update_memslots anyway. We'll also revert to the
1204 * old memslots if preparing the new memory region fails.
1205 */
1206 slots = install_new_memslots(kvm, as_id, slots);
1207
1208 /* From this point no new shadow pages pointing to a deleted,
1209 * or moved, memslot will be created.
1210 *
1211 * validation of sp->gfn happens in:
1212 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
1213 * - kvm_is_visible_gfn (mmu_check_root)
1214 */
1215 kvm_arch_flush_shadow_memslot(kvm, slot);
1216 }
1217
1218 r = kvm_arch_prepare_memory_region(kvm, new, mem, change);
1219 if (r)
1220 goto out_slots;
1221
1222 update_memslots(slots, new, change);
1223 slots = install_new_memslots(kvm, as_id, slots);
1224
1225 kvm_arch_commit_memory_region(kvm, mem, old, new, change);
1226
1227 kvfree(slots);
1228 return 0;
1229
1230out_slots:
1231 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE)
1232 slots = install_new_memslots(kvm, as_id, slots);
1233 kvfree(slots);
1234 return r;
1235}
1236
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001237static int kvm_delete_memslot(struct kvm *kvm,
1238 const struct kvm_userspace_memory_region *mem,
1239 struct kvm_memory_slot *old, int as_id)
1240{
1241 struct kvm_memory_slot new;
1242 int r;
1243
1244 if (!old->npages)
1245 return -EINVAL;
1246
1247 memset(&new, 0, sizeof(new));
1248 new.id = old->id;
1249
1250 r = kvm_set_memslot(kvm, mem, old, &new, as_id, KVM_MR_DELETE);
1251 if (r)
1252 return r;
1253
Sean Christophersone96c81e2020-02-18 13:07:27 -08001254 kvm_free_memslot(kvm, old);
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001255 return 0;
1256}
1257
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 * Allocate some memory and give it an address in the guest physical address
1260 * space.
1261 *
1262 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001263 *
Dominik Dingel02d5d552014-10-27 16:22:56 +01001264 * Must be called holding kvm->slots_lock for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001266int __kvm_set_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001267 const struct kvm_userspace_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269 struct kvm_memory_slot old, new;
Sean Christopherson163da372020-02-18 13:07:28 -08001270 struct kvm_memory_slot *tmp;
Takuya Yoshikawaf64c0392013-01-29 11:00:07 +09001271 enum kvm_mr_change change;
Sean Christopherson163da372020-02-18 13:07:28 -08001272 int as_id, id;
1273 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001275 r = check_memory_region_flags(mem);
1276 if (r)
Sean Christopherson71a4c302020-02-18 13:07:22 -08001277 return r;
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001278
Paolo Bonzinif481b062015-05-17 17:30:37 +02001279 as_id = mem->slot >> 16;
1280 id = (u16)mem->slot;
1281
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282 /* General sanity checks */
1283 if (mem->memory_size & (PAGE_SIZE - 1))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001284 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001286 return -EINVAL;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +09001287 /* We can read the guest memory with __xxx_user() later on. */
Paolo Bonzini09d952c2020-06-01 04:17:45 -04001288 if ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
Linus Torvalds96d4f262019-01-03 18:57:57 -08001289 !access_ok((void __user *)(unsigned long)mem->userspace_addr,
Paolo Bonzini09d952c2020-06-01 04:17:45 -04001290 mem->memory_size))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001291 return -EINVAL;
Paolo Bonzinif481b062015-05-17 17:30:37 +02001292 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM)
Sean Christopherson71a4c302020-02-18 13:07:22 -08001293 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
Sean Christopherson71a4c302020-02-18 13:07:22 -08001295 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001297 /*
1298 * Make a full copy of the old memslot, the pointer will become stale
1299 * when the memslots are re-sorted by update_memslots(), and the old
1300 * memslot needs to be referenced after calling update_memslots(), e.g.
Sean Christopherson0dff0842020-02-18 13:07:29 -08001301 * to free its resources and for arch specific behavior.
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001302 */
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001303 tmp = id_to_memslot(__kvm_memslots(kvm, as_id), id);
1304 if (tmp) {
1305 old = *tmp;
1306 tmp = NULL;
1307 } else {
1308 memset(&old, 0, sizeof(old));
1309 old.id = id;
1310 }
Sean Christopherson163da372020-02-18 13:07:28 -08001311
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001312 if (!mem->memory_size)
1313 return kvm_delete_memslot(kvm, mem, &old, as_id);
1314
Paolo Bonzinif481b062015-05-17 17:30:37 +02001315 new.id = id;
Sean Christopherson163da372020-02-18 13:07:28 -08001316 new.base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1317 new.npages = mem->memory_size >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 new.flags = mem->flags;
Sean Christopherson414de7ab2020-02-18 13:07:20 -08001319 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320
Sean Christopherson163da372020-02-18 13:07:28 -08001321 if (new.npages > KVM_MEM_MAX_NR_PAGES)
1322 return -EINVAL;
1323
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001324 if (!old.npages) {
1325 change = KVM_MR_CREATE;
Sean Christopherson163da372020-02-18 13:07:28 -08001326 new.dirty_bitmap = NULL;
1327 memset(&new.arch, 0, sizeof(new.arch));
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001328 } else { /* Modify an existing slot. */
1329 if ((new.userspace_addr != old.userspace_addr) ||
Sean Christopherson163da372020-02-18 13:07:28 -08001330 (new.npages != old.npages) ||
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001331 ((new.flags ^ old.flags) & KVM_MEM_READONLY))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001332 return -EINVAL;
Paolo Bonzini09170a42015-05-18 13:59:39 +02001333
Sean Christopherson163da372020-02-18 13:07:28 -08001334 if (new.base_gfn != old.base_gfn)
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001335 change = KVM_MR_MOVE;
1336 else if (new.flags != old.flags)
1337 change = KVM_MR_FLAGS_ONLY;
1338 else /* Nothing to change. */
1339 return 0;
Sean Christopherson163da372020-02-18 13:07:28 -08001340
1341 /* Copy dirty_bitmap and arch from the current memslot. */
1342 new.dirty_bitmap = old.dirty_bitmap;
1343 memcpy(&new.arch, &old.arch, sizeof(new.arch));
Paolo Bonzini09170a42015-05-18 13:59:39 +02001344 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345
Takuya Yoshikawaf64c0392013-01-29 11:00:07 +09001346 if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
Takuya Yoshikawa0a706be2013-01-11 18:26:55 +09001347 /* Check for overlaps */
Sean Christopherson163da372020-02-18 13:07:28 -08001348 kvm_for_each_memslot(tmp, __kvm_memslots(kvm, as_id)) {
1349 if (tmp->id == id)
Takuya Yoshikawa0a706be2013-01-11 18:26:55 +09001350 continue;
Sean Christopherson163da372020-02-18 13:07:28 -08001351 if (!((new.base_gfn + new.npages <= tmp->base_gfn) ||
1352 (new.base_gfn >= tmp->base_gfn + tmp->npages)))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001353 return -EEXIST;
Takuya Yoshikawa0a706be2013-01-11 18:26:55 +09001354 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356
Sean Christopherson414de7ab2020-02-18 13:07:20 -08001357 /* Allocate/free page dirty bitmap as needed */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001359 new.dirty_bitmap = NULL;
Sean Christopherson414de7ab2020-02-18 13:07:20 -08001360 else if (!new.dirty_bitmap) {
Jay Zhou3c9bd402020-02-27 09:32:27 +08001361 r = kvm_alloc_dirty_bitmap(&new);
Sean Christopherson71a4c302020-02-18 13:07:22 -08001362 if (r)
1363 return r;
Jay Zhou3c9bd402020-02-27 09:32:27 +08001364
1365 if (kvm_dirty_log_manual_protect_and_init_set(kvm))
1366 bitmap_set(new.dirty_bitmap, 0, new.npages);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 }
1368
Sean Christophersoncf47f502020-02-18 13:07:23 -08001369 r = kvm_set_memslot(kvm, mem, &old, &new, as_id, change);
1370 if (r)
1371 goto out_bitmap;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001372
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001373 if (old.dirty_bitmap && !new.dirty_bitmap)
1374 kvm_destroy_dirty_bitmap(&old);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 return 0;
1376
Sean Christophersonbd0e96f2020-02-18 13:07:21 -08001377out_bitmap:
1378 if (new.dirty_bitmap && !old.dirty_bitmap)
1379 kvm_destroy_dirty_bitmap(&new);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001381}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001382EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1383
1384int kvm_set_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001385 const struct kvm_userspace_memory_region *mem)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001386{
1387 int r;
1388
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001389 mutex_lock(&kvm->slots_lock);
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09001390 r = __kvm_set_memory_region(kvm, mem);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001391 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001392 return r;
1393}
Izik Eidus210c7c42007-10-24 23:52:57 +02001394EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1395
Stephen Hemminger79408762013-12-29 12:12:29 -08001396static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1397 struct kvm_userspace_memory_region *mem)
Izik Eidus210c7c42007-10-24 23:52:57 +02001398{
Paolo Bonzinif481b062015-05-17 17:30:37 +02001399 if ((u16)mem->slot >= KVM_USER_MEM_SLOTS)
Izik Eiduse0d62c72007-10-24 23:57:46 +02001400 return -EINVAL;
Paolo Bonzini09170a42015-05-18 13:59:39 +02001401
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09001402 return kvm_set_memory_region(kvm, mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403}
1404
Sean Christopherson0dff0842020-02-18 13:07:29 -08001405#ifndef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
Sean Christopherson2a49f612020-02-18 13:07:30 -08001406/**
1407 * kvm_get_dirty_log - get a snapshot of dirty pages
1408 * @kvm: pointer to kvm instance
1409 * @log: slot id and address to which we copy the log
1410 * @is_dirty: set to '1' if any dirty pages were found
1411 * @memslot: set to the associated memslot, always valid on success
1412 */
1413int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log,
1414 int *is_dirty, struct kvm_memory_slot **memslot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001416 struct kvm_memslots *slots;
Markus Elfring843574a2017-01-22 17:41:07 +01001417 int i, as_id, id;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001418 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419 unsigned long any = 0;
1420
Sean Christopherson2a49f612020-02-18 13:07:30 -08001421 *memslot = NULL;
1422 *is_dirty = 0;
1423
Paolo Bonzinif481b062015-05-17 17:30:37 +02001424 as_id = log->slot >> 16;
1425 id = (u16)log->slot;
1426 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
Markus Elfring843574a2017-01-22 17:41:07 +01001427 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428
Paolo Bonzinif481b062015-05-17 17:30:37 +02001429 slots = __kvm_memslots(kvm, as_id);
Sean Christopherson2a49f612020-02-18 13:07:30 -08001430 *memslot = id_to_memslot(slots, id);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001431 if (!(*memslot) || !(*memslot)->dirty_bitmap)
Markus Elfring843574a2017-01-22 17:41:07 +01001432 return -ENOENT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433
Sean Christopherson2a49f612020-02-18 13:07:30 -08001434 kvm_arch_sync_dirty_log(kvm, *memslot);
1435
1436 n = kvm_dirty_bitmap_bytes(*memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437
Uri Lublincd1a4a92007-02-22 16:43:09 +02001438 for (i = 0; !any && i < n/sizeof(long); ++i)
Sean Christopherson2a49f612020-02-18 13:07:30 -08001439 any = (*memslot)->dirty_bitmap[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
Sean Christopherson2a49f612020-02-18 13:07:30 -08001441 if (copy_to_user(log->dirty_bitmap, (*memslot)->dirty_bitmap, n))
Markus Elfring843574a2017-01-22 17:41:07 +01001442 return -EFAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001444 if (any)
1445 *is_dirty = 1;
Markus Elfring843574a2017-01-22 17:41:07 +01001446 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05301448EXPORT_SYMBOL_GPL(kvm_get_dirty_log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449
Sean Christopherson0dff0842020-02-18 13:07:29 -08001450#else /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
Mario Smarduchba0513b2015-01-15 15:58:53 -08001451/**
Jiang Biaob8b00222019-04-23 19:40:30 +08001452 * kvm_get_dirty_log_protect - get a snapshot of dirty pages
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001453 * and reenable dirty page tracking for the corresponding pages.
Mario Smarduchba0513b2015-01-15 15:58:53 -08001454 * @kvm: pointer to kvm instance
1455 * @log: slot id and address to which we copy the log
Mario Smarduchba0513b2015-01-15 15:58:53 -08001456 *
1457 * We need to keep it in mind that VCPU threads can write to the bitmap
1458 * concurrently. So, to avoid losing track of dirty pages we keep the
1459 * following order:
1460 *
1461 * 1. Take a snapshot of the bit and clear it if needed.
1462 * 2. Write protect the corresponding page.
1463 * 3. Copy the snapshot to the userspace.
1464 * 4. Upon return caller flushes TLB's if needed.
1465 *
1466 * Between 2 and 4, the guest may write to the page using the remaining TLB
1467 * entry. This is not a problem because the page is reported dirty using
1468 * the snapshot taken before and step 4 ensures that writes done after
1469 * exiting to userspace will be logged for the next call.
1470 *
1471 */
Sean Christopherson0dff0842020-02-18 13:07:29 -08001472static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log)
Mario Smarduchba0513b2015-01-15 15:58:53 -08001473{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001474 struct kvm_memslots *slots;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001475 struct kvm_memory_slot *memslot;
Markus Elfring58d6db32017-01-22 17:30:16 +01001476 int i, as_id, id;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001477 unsigned long n;
1478 unsigned long *dirty_bitmap;
1479 unsigned long *dirty_bitmap_buffer;
Sean Christopherson0dff0842020-02-18 13:07:29 -08001480 bool flush;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001481
Paolo Bonzinif481b062015-05-17 17:30:37 +02001482 as_id = log->slot >> 16;
1483 id = (u16)log->slot;
1484 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
Markus Elfring58d6db32017-01-22 17:30:16 +01001485 return -EINVAL;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001486
Paolo Bonzinif481b062015-05-17 17:30:37 +02001487 slots = __kvm_memslots(kvm, as_id);
1488 memslot = id_to_memslot(slots, id);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001489 if (!memslot || !memslot->dirty_bitmap)
1490 return -ENOENT;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001491
1492 dirty_bitmap = memslot->dirty_bitmap;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001493
Sean Christopherson0dff0842020-02-18 13:07:29 -08001494 kvm_arch_sync_dirty_log(kvm, memslot);
1495
Mario Smarduchba0513b2015-01-15 15:58:53 -08001496 n = kvm_dirty_bitmap_bytes(memslot);
Sean Christopherson0dff0842020-02-18 13:07:29 -08001497 flush = false;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001498 if (kvm->manual_dirty_log_protect) {
1499 /*
1500 * Unlike kvm_get_dirty_log, we always return false in *flush,
1501 * because no flush is needed until KVM_CLEAR_DIRTY_LOG. There
1502 * is some code duplication between this function and
1503 * kvm_get_dirty_log, but hopefully all architecture
1504 * transition to kvm_get_dirty_log_protect and kvm_get_dirty_log
1505 * can be eliminated.
1506 */
1507 dirty_bitmap_buffer = dirty_bitmap;
1508 } else {
1509 dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);
1510 memset(dirty_bitmap_buffer, 0, n);
Mario Smarduchba0513b2015-01-15 15:58:53 -08001511
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001512 spin_lock(&kvm->mmu_lock);
1513 for (i = 0; i < n / sizeof(long); i++) {
1514 unsigned long mask;
1515 gfn_t offset;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001516
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001517 if (!dirty_bitmap[i])
1518 continue;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001519
Sean Christopherson0dff0842020-02-18 13:07:29 -08001520 flush = true;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001521 mask = xchg(&dirty_bitmap[i], 0);
1522 dirty_bitmap_buffer[i] = mask;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001523
Lan Tianyua67794c2019-02-02 17:20:27 +08001524 offset = i * BITS_PER_LONG;
1525 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
1526 offset, mask);
Takuya Yoshikawa58d29302015-03-17 16:19:58 +09001527 }
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001528 spin_unlock(&kvm->mmu_lock);
Mario Smarduchba0513b2015-01-15 15:58:53 -08001529 }
1530
Sean Christopherson0dff0842020-02-18 13:07:29 -08001531 if (flush)
1532 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
1533
Mario Smarduchba0513b2015-01-15 15:58:53 -08001534 if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
Markus Elfring58d6db32017-01-22 17:30:16 +01001535 return -EFAULT;
1536 return 0;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001537}
Sean Christopherson0dff0842020-02-18 13:07:29 -08001538
1539
1540/**
1541 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1542 * @kvm: kvm instance
1543 * @log: slot id and address to which we copy the log
1544 *
1545 * Steps 1-4 below provide general overview of dirty page logging. See
1546 * kvm_get_dirty_log_protect() function description for additional details.
1547 *
1548 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1549 * always flush the TLB (step 4) even if previous step failed and the dirty
1550 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1551 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1552 * writes will be marked dirty for next log read.
1553 *
1554 * 1. Take a snapshot of the bit and clear it if needed.
1555 * 2. Write protect the corresponding page.
1556 * 3. Copy the snapshot to the userspace.
1557 * 4. Flush TLB's if needed.
1558 */
1559static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1560 struct kvm_dirty_log *log)
1561{
1562 int r;
1563
1564 mutex_lock(&kvm->slots_lock);
1565
1566 r = kvm_get_dirty_log_protect(kvm, log);
1567
1568 mutex_unlock(&kvm->slots_lock);
1569 return r;
1570}
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001571
1572/**
1573 * kvm_clear_dirty_log_protect - clear dirty bits in the bitmap
1574 * and reenable dirty page tracking for the corresponding pages.
1575 * @kvm: pointer to kvm instance
1576 * @log: slot id and address from which to fetch the bitmap of dirty pages
1577 */
Sean Christopherson0dff0842020-02-18 13:07:29 -08001578static int kvm_clear_dirty_log_protect(struct kvm *kvm,
1579 struct kvm_clear_dirty_log *log)
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001580{
1581 struct kvm_memslots *slots;
1582 struct kvm_memory_slot *memslot;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001583 int as_id, id;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001584 gfn_t offset;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001585 unsigned long i, n;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001586 unsigned long *dirty_bitmap;
1587 unsigned long *dirty_bitmap_buffer;
Sean Christopherson0dff0842020-02-18 13:07:29 -08001588 bool flush;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001589
1590 as_id = log->slot >> 16;
1591 id = (u16)log->slot;
1592 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
1593 return -EINVAL;
1594
Paolo Bonzini76d58e02019-04-17 15:28:44 +02001595 if (log->first_page & 63)
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001596 return -EINVAL;
1597
1598 slots = __kvm_memslots(kvm, as_id);
1599 memslot = id_to_memslot(slots, id);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001600 if (!memslot || !memslot->dirty_bitmap)
1601 return -ENOENT;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001602
1603 dirty_bitmap = memslot->dirty_bitmap;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001604
Peter Xu4ddc9202019-05-08 17:15:45 +08001605 n = ALIGN(log->num_pages, BITS_PER_LONG) / 8;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001606
1607 if (log->first_page > memslot->npages ||
Paolo Bonzini76d58e02019-04-17 15:28:44 +02001608 log->num_pages > memslot->npages - log->first_page ||
1609 (log->num_pages < memslot->npages - log->first_page && (log->num_pages & 63)))
1610 return -EINVAL;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001611
Sean Christopherson0dff0842020-02-18 13:07:29 -08001612 kvm_arch_sync_dirty_log(kvm, memslot);
1613
1614 flush = false;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001615 dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);
1616 if (copy_from_user(dirty_bitmap_buffer, log->dirty_bitmap, n))
1617 return -EFAULT;
1618
1619 spin_lock(&kvm->mmu_lock);
Peter Xu53eac7a2019-05-08 17:15:46 +08001620 for (offset = log->first_page, i = offset / BITS_PER_LONG,
1621 n = DIV_ROUND_UP(log->num_pages, BITS_PER_LONG); n--;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001622 i++, offset += BITS_PER_LONG) {
1623 unsigned long mask = *dirty_bitmap_buffer++;
1624 atomic_long_t *p = (atomic_long_t *) &dirty_bitmap[i];
1625 if (!mask)
1626 continue;
1627
1628 mask &= atomic_long_fetch_andnot(mask, p);
1629
1630 /*
1631 * mask contains the bits that really have been cleared. This
1632 * never includes any bits beyond the length of the memslot (if
1633 * the length is not aligned to 64 pages), therefore it is not
1634 * a problem if userspace sets them in log->dirty_bitmap.
1635 */
1636 if (mask) {
Sean Christopherson0dff0842020-02-18 13:07:29 -08001637 flush = true;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001638 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
1639 offset, mask);
1640 }
1641 }
1642 spin_unlock(&kvm->mmu_lock);
1643
Sean Christopherson0dff0842020-02-18 13:07:29 -08001644 if (flush)
1645 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
1646
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001647 return 0;
1648}
Sean Christopherson0dff0842020-02-18 13:07:29 -08001649
1650static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,
1651 struct kvm_clear_dirty_log *log)
1652{
1653 int r;
1654
1655 mutex_lock(&kvm->slots_lock);
1656
1657 r = kvm_clear_dirty_log_protect(kvm, log);
1658
1659 mutex_unlock(&kvm->slots_lock);
1660 return r;
1661}
1662#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
Mario Smarduchba0513b2015-01-15 15:58:53 -08001663
Gleb Natapov49c77542010-10-18 15:22:23 +02001664struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1665{
1666 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
1667}
Avi Kivitya1f4d3952010-06-21 11:44:20 +03001668EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669
Paolo Bonzini8e734852015-05-17 13:58:53 +02001670struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn)
1671{
1672 return __gfn_to_memslot(kvm_vcpu_memslots(vcpu), gfn);
1673}
Paolo Bonzinie72436b2020-04-17 12:21:06 -04001674EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_memslot);
Paolo Bonzini8e734852015-05-17 13:58:53 +02001675
Yaowei Bai33e94152015-11-14 11:21:06 +08001676bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
Izik Eiduse0d62c72007-10-24 23:57:46 +02001677{
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +08001678 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
Izik Eiduse0d62c72007-10-24 23:57:46 +02001679
Paolo Bonzinic36b7152020-04-16 09:48:07 -04001680 return kvm_is_visible_memslot(memslot);
Izik Eiduse0d62c72007-10-24 23:57:46 +02001681}
1682EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1683
Vitaly Kuznetsov995decb2020-07-08 16:00:23 +02001684bool kvm_vcpu_is_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
1685{
1686 struct kvm_memory_slot *memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
1687
1688 return kvm_is_visible_memslot(memslot);
1689}
1690EXPORT_SYMBOL_GPL(kvm_vcpu_is_visible_gfn);
1691
Sean Christophersonf9b84e12020-01-08 12:24:37 -08001692unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn)
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001693{
1694 struct vm_area_struct *vma;
1695 unsigned long addr, size;
1696
1697 size = PAGE_SIZE;
1698
Sean Christopherson42cde482020-01-08 12:24:38 -08001699 addr = kvm_vcpu_gfn_to_hva_prot(vcpu, gfn, NULL);
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001700 if (kvm_is_error_hva(addr))
1701 return PAGE_SIZE;
1702
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001703 mmap_read_lock(current->mm);
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001704 vma = find_vma(current->mm, addr);
1705 if (!vma)
1706 goto out;
1707
1708 size = vma_kernel_pagesize(vma);
1709
1710out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001711 mmap_read_unlock(current->mm);
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001712
1713 return size;
1714}
1715
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001716static bool memslot_is_readonly(struct kvm_memory_slot *slot)
1717{
1718 return slot->flags & KVM_MEM_READONLY;
1719}
1720
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001721static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1722 gfn_t *nr_pages, bool write)
Izik Eidus539cb662007-11-11 22:05:04 +02001723{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001724 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Xiao Guangrongca3a4902012-08-21 11:01:50 +08001725 return KVM_HVA_ERR_BAD;
Xiao Guangrong48987782010-08-22 19:11:43 +08001726
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001727 if (memslot_is_readonly(slot) && write)
1728 return KVM_HVA_ERR_RO_BAD;
Xiao Guangrong48987782010-08-22 19:11:43 +08001729
1730 if (nr_pages)
1731 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1732
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001733 return __gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001734}
Xiao Guangrong48987782010-08-22 19:11:43 +08001735
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001736static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1737 gfn_t *nr_pages)
1738{
1739 return __gfn_to_hva_many(slot, gfn, nr_pages, true);
1740}
1741
1742unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
Stephen Hemminger79408762013-12-29 12:12:29 -08001743 gfn_t gfn)
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001744{
1745 return gfn_to_hva_many(slot, gfn, NULL);
1746}
1747EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
1748
Xiao Guangrong48987782010-08-22 19:11:43 +08001749unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1750{
Gleb Natapov49c77542010-10-18 15:22:23 +02001751 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001752}
Sheng Yang0d150292008-04-25 21:44:50 +08001753EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001754
Paolo Bonzini8e734852015-05-17 13:58:53 +02001755unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn)
1756{
1757 return gfn_to_hva_many(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, NULL);
1758}
1759EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva);
1760
Xiao Guangrong86ab8cf2012-08-21 10:59:53 +08001761/*
Wei Yang970c0d42018-10-09 10:41:15 +08001762 * Return the hva of a @gfn and the R/W attribute if possible.
1763 *
1764 * @slot: the kvm_memory_slot which contains @gfn
1765 * @gfn: the gfn to be translated
1766 * @writable: used to return the read/write attribute of the @slot if the hva
1767 * is valid and @writable is not NULL
Xiao Guangrong86ab8cf2012-08-21 10:59:53 +08001768 */
Christoffer Dall64d83122014-08-19 12:15:00 +02001769unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot,
1770 gfn_t gfn, bool *writable)
Gleb Natapov80300892010-10-19 18:13:41 +02001771{
Gleb Natapova2ac07f2013-10-01 19:58:36 +03001772 unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
1773
1774 if (!kvm_is_error_hva(hva) && writable)
Paolo Bonziniba6a3542013-09-09 13:52:33 +02001775 *writable = !memslot_is_readonly(slot);
1776
Gleb Natapova2ac07f2013-10-01 19:58:36 +03001777 return hva;
Xiao Guangrong86ab8cf2012-08-21 10:59:53 +08001778}
1779
Christoffer Dall64d83122014-08-19 12:15:00 +02001780unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
1781{
1782 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
1783
1784 return gfn_to_hva_memslot_prot(slot, gfn, writable);
1785}
1786
Paolo Bonzini8e734852015-05-17 13:58:53 +02001787unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable)
1788{
1789 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
1790
1791 return gfn_to_hva_memslot_prot(slot, gfn, writable);
1792}
1793
Huang Yingfafc3db2011-01-30 11:15:49 +08001794static inline int check_user_page_hwpoison(unsigned long addr)
1795{
Lorenzo Stoakes0d731752016-10-24 10:57:25 +01001796 int rc, flags = FOLL_HWPOISON | FOLL_WRITE;
Huang Yingfafc3db2011-01-30 11:15:49 +08001797
Lorenzo Stoakes0d731752016-10-24 10:57:25 +01001798 rc = get_user_pages(addr, 1, flags, NULL, NULL);
Huang Yingfafc3db2011-01-30 11:15:49 +08001799 return rc == -EHWPOISON;
1800}
1801
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001802/*
Paolo Bonzinib9b33da2018-07-27 17:44:41 +02001803 * The fast path to get the writable pfn which will be stored in @pfn,
1804 * true indicates success, otherwise false is returned. It's also the
Miaohe Lin311497e2019-12-11 14:26:25 +08001805 * only part that runs if we can in atomic context.
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001806 */
Paolo Bonzinib9b33da2018-07-27 17:44:41 +02001807static bool hva_to_pfn_fast(unsigned long addr, bool write_fault,
1808 bool *writable, kvm_pfn_t *pfn)
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001809{
1810 struct page *page[1];
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001811
Xiao Guangrong12ce13f2012-08-21 11:00:49 +08001812 /*
1813 * Fast pin a writable pfn only if it is a write fault request
1814 * or the caller allows to map a writable pfn for a read fault
1815 * request.
1816 */
1817 if (!(write_fault || writable))
1818 return false;
1819
Souptick Joarderdadbb612020-06-07 21:40:55 -07001820 if (get_user_page_fast_only(addr, FOLL_WRITE, page)) {
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001821 *pfn = page_to_pfn(page[0]);
1822
1823 if (writable)
1824 *writable = true;
1825 return true;
1826 }
1827
1828 return false;
1829}
1830
1831/*
1832 * The slow path to get the pfn of the specified host virtual address,
1833 * 1 indicates success, -errno is returned if error is detected.
1834 */
1835static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
Dan Williamsba049e92016-01-15 16:56:11 -08001836 bool *writable, kvm_pfn_t *pfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001837{
Al Viroce530532017-11-19 17:47:33 -05001838 unsigned int flags = FOLL_HWPOISON;
1839 struct page *page;
Gleb Natapovaf585b92010-10-14 11:22:46 +02001840 int npages = 0;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001841
1842 might_sleep();
1843
1844 if (writable)
1845 *writable = write_fault;
1846
Al Viroce530532017-11-19 17:47:33 -05001847 if (write_fault)
1848 flags |= FOLL_WRITE;
1849 if (async)
1850 flags |= FOLL_NOWAIT;
Lorenzo Stoakesd4944b02016-10-13 01:20:12 +01001851
Al Viroce530532017-11-19 17:47:33 -05001852 npages = get_user_pages_unlocked(addr, 1, &page, flags);
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001853 if (npages != 1)
1854 return npages;
1855
1856 /* map read fault as writable if possible */
Xiao Guangrong12ce13f2012-08-21 11:00:49 +08001857 if (unlikely(!write_fault) && writable) {
Al Viroce530532017-11-19 17:47:33 -05001858 struct page *wpage;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001859
Souptick Joarderdadbb612020-06-07 21:40:55 -07001860 if (get_user_page_fast_only(addr, FOLL_WRITE, &wpage)) {
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001861 *writable = true;
Al Viroce530532017-11-19 17:47:33 -05001862 put_page(page);
1863 page = wpage;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001864 }
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001865 }
Al Viroce530532017-11-19 17:47:33 -05001866 *pfn = page_to_pfn(page);
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001867 return npages;
1868}
1869
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001870static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
1871{
1872 if (unlikely(!(vma->vm_flags & VM_READ)))
1873 return false;
1874
1875 if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
1876 return false;
1877
1878 return true;
1879}
1880
Paolo Bonzini92176a82016-06-07 16:22:47 +02001881static int hva_to_pfn_remapped(struct vm_area_struct *vma,
1882 unsigned long addr, bool *async,
KarimAllah Ahmeda340b3e2018-01-17 19:18:56 +01001883 bool write_fault, bool *writable,
1884 kvm_pfn_t *p_pfn)
Paolo Bonzini92176a82016-06-07 16:22:47 +02001885{
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001886 unsigned long pfn;
1887 int r;
1888
1889 r = follow_pfn(vma, addr, &pfn);
1890 if (r) {
1891 /*
1892 * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does
1893 * not call the fault handler, so do it here.
1894 */
1895 bool unlocked = false;
Peter Xu64019a22020-08-11 18:39:01 -07001896 r = fixup_user_fault(current->mm, addr,
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001897 (write_fault ? FAULT_FLAG_WRITE : 0),
1898 &unlocked);
Paolo Bonzinia8387d02020-05-29 05:42:55 -04001899 if (unlocked)
1900 return -EAGAIN;
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001901 if (r)
1902 return r;
1903
1904 r = follow_pfn(vma, addr, &pfn);
1905 if (r)
1906 return r;
1907
1908 }
1909
KarimAllah Ahmeda340b3e2018-01-17 19:18:56 +01001910 if (writable)
1911 *writable = true;
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001912
1913 /*
1914 * Get a reference here because callers of *hva_to_pfn* and
1915 * *gfn_to_pfn* ultimately call kvm_release_pfn_clean on the
1916 * returned pfn. This is only needed if the VMA has VM_MIXEDMAP
1917 * set, but the kvm_get_pfn/kvm_release_pfn_clean pair will
1918 * simply do nothing for reserved pfns.
1919 *
1920 * Whoever called remap_pfn_range is also going to call e.g.
1921 * unmap_mapping_range before the underlying pages are freed,
1922 * causing a call to our MMU notifier.
1923 */
1924 kvm_get_pfn(pfn);
1925
1926 *p_pfn = pfn;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001927 return 0;
1928}
1929
Xiao Guangrong12ce13f2012-08-21 11:00:49 +08001930/*
1931 * Pin guest page in memory and return its pfn.
1932 * @addr: host virtual address which maps memory to the guest
1933 * @atomic: whether this function can sleep
1934 * @async: whether this function need to wait IO complete if the
1935 * host page is not in the memory
1936 * @write_fault: whether we should get a writable host page
1937 * @writable: whether it allows to map a writable host page for !@write_fault
1938 *
1939 * The function will map a writable host page for these two cases:
1940 * 1): @write_fault = true
1941 * 2): @write_fault = false && @writable, @writable will tell the caller
1942 * whether the mapping is writable.
1943 */
Dan Williamsba049e92016-01-15 16:56:11 -08001944static kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001945 bool write_fault, bool *writable)
1946{
1947 struct vm_area_struct *vma;
Dan Williamsba049e92016-01-15 16:56:11 -08001948 kvm_pfn_t pfn = 0;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001949 int npages, r;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001950
Gleb Natapovaf585b92010-10-14 11:22:46 +02001951 /* we can do it either atomically or asynchronously, not both */
1952 BUG_ON(atomic && async);
1953
Paolo Bonzinib9b33da2018-07-27 17:44:41 +02001954 if (hva_to_pfn_fast(addr, write_fault, writable, &pfn))
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001955 return pfn;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001956
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001957 if (atomic)
1958 return KVM_PFN_ERR_FAULT;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001959
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001960 npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
1961 if (npages == 1)
1962 return pfn;
Gleb Natapovaf585b92010-10-14 11:22:46 +02001963
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001964 mmap_read_lock(current->mm);
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001965 if (npages == -EHWPOISON ||
1966 (!async && check_user_page_hwpoison(addr))) {
1967 pfn = KVM_PFN_ERR_HWPOISON;
1968 goto exit;
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001969 }
Izik Eidus539cb662007-11-11 22:05:04 +02001970
Paolo Bonzinia8387d02020-05-29 05:42:55 -04001971retry:
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001972 vma = find_vma_intersection(current->mm, addr, addr + 1);
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001973
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001974 if (vma == NULL)
1975 pfn = KVM_PFN_ERR_FAULT;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001976 else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) {
KarimAllah Ahmeda340b3e2018-01-17 19:18:56 +01001977 r = hva_to_pfn_remapped(vma, addr, async, write_fault, writable, &pfn);
Paolo Bonzinia8387d02020-05-29 05:42:55 -04001978 if (r == -EAGAIN)
1979 goto retry;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001980 if (r < 0)
1981 pfn = KVM_PFN_ERR_FAULT;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001982 } else {
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001983 if (async && vma_is_valid(vma, write_fault))
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001984 *async = true;
1985 pfn = KVM_PFN_ERR_FAULT;
1986 }
1987exit:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001988 mmap_read_unlock(current->mm);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001989 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001990}
1991
Dan Williamsba049e92016-01-15 16:56:11 -08001992kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn,
1993 bool atomic, bool *async, bool write_fault,
1994 bool *writable)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001995{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001996 unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
1997
Paolo Bonzinib2740d32016-02-23 15:36:01 +01001998 if (addr == KVM_HVA_ERR_RO_BAD) {
1999 if (writable)
2000 *writable = false;
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002001 return KVM_PFN_ERR_RO_FAULT;
Paolo Bonzinib2740d32016-02-23 15:36:01 +01002002 }
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002003
Paolo Bonzinib2740d32016-02-23 15:36:01 +01002004 if (kvm_is_error_hva(addr)) {
2005 if (writable)
2006 *writable = false;
Xiao Guangrong81c52c52012-10-16 20:10:59 +08002007 return KVM_PFN_NOSLOT;
Paolo Bonzinib2740d32016-02-23 15:36:01 +01002008 }
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002009
2010 /* Do not map writable pfn in the readonly memslot. */
2011 if (writable && memslot_is_readonly(slot)) {
2012 *writable = false;
2013 writable = NULL;
2014 }
2015
2016 return hva_to_pfn(addr, atomic, async, write_fault,
2017 writable);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08002018}
Paolo Bonzini35204692015-04-02 11:20:48 +02002019EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08002020
Dan Williamsba049e92016-01-15 16:56:11 -08002021kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002022 bool *writable)
2023{
Paolo Bonzinie37afc62015-05-19 16:09:04 +02002024 return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL,
2025 write_fault, writable);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002026}
2027EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
2028
Dan Williamsba049e92016-01-15 16:56:11 -08002029kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02002030{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002031 return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02002032}
Paolo Bonzinie37afc62015-05-19 16:09:04 +02002033EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02002034
Dan Williamsba049e92016-01-15 16:56:11 -08002035kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
Xiao Guangrong037d92d2012-08-21 10:59:12 +08002036{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002037 return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
Xiao Guangrong037d92d2012-08-21 10:59:12 +08002038}
2039EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
2040
Dan Williamsba049e92016-01-15 16:56:11 -08002041kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn)
Paolo Bonzini8e734852015-05-17 13:58:53 +02002042{
2043 return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
2044}
2045EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic);
2046
Dan Williamsba049e92016-01-15 16:56:11 -08002047kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Paolo Bonzinie37afc62015-05-19 16:09:04 +02002048{
2049 return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn);
2050}
2051EXPORT_SYMBOL_GPL(gfn_to_pfn);
2052
Dan Williamsba049e92016-01-15 16:56:11 -08002053kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
Paolo Bonzini8e734852015-05-17 13:58:53 +02002054{
2055 return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
2056}
2057EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn);
2058
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002059int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
2060 struct page **pages, int nr_pages)
Xiao Guangrong48987782010-08-22 19:11:43 +08002061{
2062 unsigned long addr;
Arnd Bergmann076b9252017-08-10 14:14:39 +02002063 gfn_t entry = 0;
Xiao Guangrong48987782010-08-22 19:11:43 +08002064
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002065 addr = gfn_to_hva_many(slot, gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08002066 if (kvm_is_error_hva(addr))
2067 return -1;
2068
2069 if (entry < nr_pages)
2070 return 0;
2071
Souptick Joarderdadbb612020-06-07 21:40:55 -07002072 return get_user_pages_fast_only(addr, nr_pages, FOLL_WRITE, pages);
Xiao Guangrong48987782010-08-22 19:11:43 +08002073}
2074EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
2075
Dan Williamsba049e92016-01-15 16:56:11 -08002076static struct page *kvm_pfn_to_page(kvm_pfn_t pfn)
Xiao Guangronga2766322012-07-26 11:58:59 +08002077{
Xiao Guangrong81c52c52012-10-16 20:10:59 +08002078 if (is_error_noslot_pfn(pfn))
Xiao Guangrong6cede2e2012-08-03 15:41:22 +08002079 return KVM_ERR_PTR_BAD_PAGE;
Xiao Guangronga2766322012-07-26 11:58:59 +08002080
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002081 if (kvm_is_reserved_pfn(pfn)) {
Xiao Guangrongcb9aaa32012-08-03 15:42:10 +08002082 WARN_ON(1);
2083 return KVM_ERR_PTR_BAD_PAGE;
2084 }
2085
Xiao Guangronga2766322012-07-26 11:58:59 +08002086 return pfn_to_page(pfn);
2087}
2088
Anthony Liguori35149e22008-04-02 14:46:56 -05002089struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
2090{
Dan Williamsba049e92016-01-15 16:56:11 -08002091 kvm_pfn_t pfn;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002092
2093 pfn = gfn_to_pfn(kvm, gfn);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002094
Xiao Guangronga2766322012-07-26 11:58:59 +08002095 return kvm_pfn_to_page(pfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +03002096}
2097EXPORT_SYMBOL_GPL(gfn_to_page);
2098
Boris Ostrovsky91724812019-12-05 01:30:51 +00002099void kvm_release_pfn(kvm_pfn_t pfn, bool dirty, struct gfn_to_pfn_cache *cache)
2100{
2101 if (pfn == 0)
2102 return;
2103
2104 if (cache)
2105 cache->pfn = cache->gfn = 0;
2106
2107 if (dirty)
2108 kvm_release_pfn_dirty(pfn);
2109 else
2110 kvm_release_pfn_clean(pfn);
2111}
2112
2113static void kvm_cache_gfn_to_pfn(struct kvm_memory_slot *slot, gfn_t gfn,
2114 struct gfn_to_pfn_cache *cache, u64 gen)
2115{
2116 kvm_release_pfn(cache->pfn, cache->dirty, cache);
2117
2118 cache->pfn = gfn_to_pfn_memslot(slot, gfn);
2119 cache->gfn = gfn;
2120 cache->dirty = false;
2121 cache->generation = gen;
2122}
2123
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002124static int __kvm_map_gfn(struct kvm_memslots *slots, gfn_t gfn,
Boris Ostrovsky91724812019-12-05 01:30:51 +00002125 struct kvm_host_map *map,
2126 struct gfn_to_pfn_cache *cache,
2127 bool atomic)
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002128{
2129 kvm_pfn_t pfn;
2130 void *hva = NULL;
2131 struct page *page = KVM_UNMAPPED_PAGE;
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002132 struct kvm_memory_slot *slot = __gfn_to_memslot(slots, gfn);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002133 u64 gen = slots->generation;
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002134
2135 if (!map)
2136 return -EINVAL;
2137
Boris Ostrovsky91724812019-12-05 01:30:51 +00002138 if (cache) {
2139 if (!cache->pfn || cache->gfn != gfn ||
2140 cache->generation != gen) {
2141 if (atomic)
2142 return -EAGAIN;
2143 kvm_cache_gfn_to_pfn(slot, gfn, cache, gen);
2144 }
2145 pfn = cache->pfn;
2146 } else {
2147 if (atomic)
2148 return -EAGAIN;
2149 pfn = gfn_to_pfn_memslot(slot, gfn);
2150 }
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002151 if (is_error_noslot_pfn(pfn))
2152 return -EINVAL;
2153
2154 if (pfn_valid(pfn)) {
2155 page = pfn_to_page(pfn);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002156 if (atomic)
2157 hva = kmap_atomic(page);
2158 else
2159 hva = kmap(page);
Paolo Bonzinid30b2142019-05-20 12:06:36 +02002160#ifdef CONFIG_HAS_IOMEM
Boris Ostrovsky91724812019-12-05 01:30:51 +00002161 } else if (!atomic) {
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002162 hva = memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002163 } else {
2164 return -EINVAL;
Paolo Bonzinid30b2142019-05-20 12:06:36 +02002165#endif
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002166 }
2167
2168 if (!hva)
2169 return -EFAULT;
2170
2171 map->page = page;
2172 map->hva = hva;
2173 map->pfn = pfn;
2174 map->gfn = gfn;
2175
2176 return 0;
2177}
2178
Boris Ostrovsky91724812019-12-05 01:30:51 +00002179int kvm_map_gfn(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map,
2180 struct gfn_to_pfn_cache *cache, bool atomic)
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002181{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002182 return __kvm_map_gfn(kvm_memslots(vcpu->kvm), gfn, map,
2183 cache, atomic);
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002184}
2185EXPORT_SYMBOL_GPL(kvm_map_gfn);
2186
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002187int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map)
2188{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002189 return __kvm_map_gfn(kvm_vcpu_memslots(vcpu), gfn, map,
2190 NULL, false);
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002191}
2192EXPORT_SYMBOL_GPL(kvm_vcpu_map);
2193
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002194static void __kvm_unmap_gfn(struct kvm_memory_slot *memslot,
Boris Ostrovsky91724812019-12-05 01:30:51 +00002195 struct kvm_host_map *map,
2196 struct gfn_to_pfn_cache *cache,
2197 bool dirty, bool atomic)
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002198{
2199 if (!map)
2200 return;
2201
2202 if (!map->hva)
2203 return;
2204
Boris Ostrovsky91724812019-12-05 01:30:51 +00002205 if (map->page != KVM_UNMAPPED_PAGE) {
2206 if (atomic)
2207 kunmap_atomic(map->hva);
2208 else
2209 kunmap(map->page);
2210 }
Christian Borntraegereb1f2f32019-05-27 10:28:25 +02002211#ifdef CONFIG_HAS_IOMEM
Boris Ostrovsky91724812019-12-05 01:30:51 +00002212 else if (!atomic)
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002213 memunmap(map->hva);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002214 else
2215 WARN_ONCE(1, "Unexpected unmapping in atomic context");
Christian Borntraegereb1f2f32019-05-27 10:28:25 +02002216#endif
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002217
Boris Ostrovsky91724812019-12-05 01:30:51 +00002218 if (dirty)
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002219 mark_page_dirty_in_slot(memslot, map->gfn);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002220
2221 if (cache)
2222 cache->dirty |= dirty;
2223 else
2224 kvm_release_pfn(map->pfn, dirty, NULL);
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002225
2226 map->hva = NULL;
2227 map->page = NULL;
2228}
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002229
Boris Ostrovsky91724812019-12-05 01:30:51 +00002230int kvm_unmap_gfn(struct kvm_vcpu *vcpu, struct kvm_host_map *map,
2231 struct gfn_to_pfn_cache *cache, bool dirty, bool atomic)
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002232{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002233 __kvm_unmap_gfn(gfn_to_memslot(vcpu->kvm, map->gfn), map,
2234 cache, dirty, atomic);
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002235 return 0;
2236}
2237EXPORT_SYMBOL_GPL(kvm_unmap_gfn);
2238
2239void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty)
2240{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002241 __kvm_unmap_gfn(kvm_vcpu_gfn_to_memslot(vcpu, map->gfn), map, NULL,
2242 dirty, false);
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002243}
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002244EXPORT_SYMBOL_GPL(kvm_vcpu_unmap);
2245
Paolo Bonzini8e734852015-05-17 13:58:53 +02002246struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2247{
Dan Williamsba049e92016-01-15 16:56:11 -08002248 kvm_pfn_t pfn;
Paolo Bonzini8e734852015-05-17 13:58:53 +02002249
2250 pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn);
2251
2252 return kvm_pfn_to_page(pfn);
2253}
2254EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page);
2255
Izik Eidusb4231d62007-11-20 11:49:33 +02002256void kvm_release_page_clean(struct page *page)
2257{
Xiao Guangrong32cad842012-08-03 15:42:52 +08002258 WARN_ON(is_error_page(page));
2259
Anthony Liguori35149e22008-04-02 14:46:56 -05002260 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02002261}
2262EXPORT_SYMBOL_GPL(kvm_release_page_clean);
2263
Dan Williamsba049e92016-01-15 16:56:11 -08002264void kvm_release_pfn_clean(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002265{
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002266 if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002267 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002268}
2269EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
2270
Izik Eidusb4231d62007-11-20 11:49:33 +02002271void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02002272{
Xiao Guangronga2766322012-07-26 11:58:59 +08002273 WARN_ON(is_error_page(page));
2274
Anthony Liguori35149e22008-04-02 14:46:56 -05002275 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02002276}
Izik Eidusb4231d62007-11-20 11:49:33 +02002277EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02002278
David Hildenbrandf7a65092017-09-01 17:11:43 +02002279void kvm_release_pfn_dirty(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002280{
2281 kvm_set_pfn_dirty(pfn);
2282 kvm_release_pfn_clean(pfn);
2283}
David Hildenbrandf7a65092017-09-01 17:11:43 +02002284EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
Anthony Liguori35149e22008-04-02 14:46:56 -05002285
Dan Williamsba049e92016-01-15 16:56:11 -08002286void kvm_set_pfn_dirty(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002287{
Miaohe Lind29c03a2019-12-05 11:05:05 +08002288 if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn))
2289 SetPageDirty(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002290}
2291EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
2292
Dan Williamsba049e92016-01-15 16:56:11 -08002293void kvm_set_pfn_accessed(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002294{
Sean Christophersona78986a2019-11-11 14:12:27 -08002295 if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002296 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002297}
2298EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
2299
Dan Williamsba049e92016-01-15 16:56:11 -08002300void kvm_get_pfn(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002301{
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002302 if (!kvm_is_reserved_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002303 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002304}
2305EXPORT_SYMBOL_GPL(kvm_get_pfn);
2306
Izik Eidus195aefd2007-10-01 22:14:18 +02002307static int next_segment(unsigned long len, int offset)
2308{
2309 if (len > PAGE_SIZE - offset)
2310 return PAGE_SIZE - offset;
2311 else
2312 return len;
2313}
2314
Paolo Bonzini8e734852015-05-17 13:58:53 +02002315static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn,
2316 void *data, int offset, int len)
Izik Eidus195aefd2007-10-01 22:14:18 +02002317{
Izik Eiduse0506bc2007-11-11 22:10:22 +02002318 int r;
2319 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02002320
Paolo Bonzini8e734852015-05-17 13:58:53 +02002321 addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002322 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02002323 return -EFAULT;
Paolo Bonzini3180a7f2015-04-02 14:08:20 +02002324 r = __copy_from_user(data, (void __user *)addr + offset, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002325 if (r)
2326 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002327 return 0;
2328}
Paolo Bonzini8e734852015-05-17 13:58:53 +02002329
2330int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
2331 int len)
2332{
2333 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
2334
2335 return __kvm_read_guest_page(slot, gfn, data, offset, len);
2336}
Izik Eidus195aefd2007-10-01 22:14:18 +02002337EXPORT_SYMBOL_GPL(kvm_read_guest_page);
2338
Paolo Bonzini8e734852015-05-17 13:58:53 +02002339int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data,
2340 int offset, int len)
2341{
2342 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2343
2344 return __kvm_read_guest_page(slot, gfn, data, offset, len);
2345}
2346EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page);
2347
Izik Eidus195aefd2007-10-01 22:14:18 +02002348int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
2349{
2350 gfn_t gfn = gpa >> PAGE_SHIFT;
2351 int seg;
2352 int offset = offset_in_page(gpa);
2353 int ret;
2354
2355 while ((seg = next_segment(len, offset)) != 0) {
2356 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
2357 if (ret < 0)
2358 return ret;
2359 offset = 0;
2360 len -= seg;
2361 data += seg;
2362 ++gfn;
2363 }
2364 return 0;
2365}
2366EXPORT_SYMBOL_GPL(kvm_read_guest);
2367
Paolo Bonzini8e734852015-05-17 13:58:53 +02002368int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, unsigned long len)
2369{
2370 gfn_t gfn = gpa >> PAGE_SHIFT;
2371 int seg;
2372 int offset = offset_in_page(gpa);
2373 int ret;
2374
2375 while ((seg = next_segment(len, offset)) != 0) {
2376 ret = kvm_vcpu_read_guest_page(vcpu, gfn, data, offset, seg);
2377 if (ret < 0)
2378 return ret;
2379 offset = 0;
2380 len -= seg;
2381 data += seg;
2382 ++gfn;
2383 }
2384 return 0;
2385}
2386EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest);
2387
2388static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
2389 void *data, int offset, unsigned long len)
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002390{
2391 int r;
2392 unsigned long addr;
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002393
Paolo Bonzini8e734852015-05-17 13:58:53 +02002394 addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002395 if (kvm_is_error_hva(addr))
2396 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01002397 pagefault_disable();
Paolo Bonzini3180a7f2015-04-02 14:08:20 +02002398 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01002399 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002400 if (r)
2401 return -EFAULT;
2402 return 0;
2403}
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002404
Paolo Bonzini8e734852015-05-17 13:58:53 +02002405int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa,
2406 void *data, unsigned long len)
2407{
2408 gfn_t gfn = gpa >> PAGE_SHIFT;
2409 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2410 int offset = offset_in_page(gpa);
2411
2412 return __kvm_read_guest_atomic(slot, gfn, data, offset, len);
2413}
2414EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic);
2415
2416static int __kvm_write_guest_page(struct kvm_memory_slot *memslot, gfn_t gfn,
2417 const void *data, int offset, int len)
Izik Eidus195aefd2007-10-01 22:14:18 +02002418{
Izik Eiduse0506bc2007-11-11 22:10:22 +02002419 int r;
2420 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02002421
Radim Krčmář251eb842015-04-10 21:47:27 +02002422 addr = gfn_to_hva_memslot(memslot, gfn);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002423 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02002424 return -EFAULT;
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08002425 r = __copy_to_user((void __user *)addr + offset, data, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002426 if (r)
2427 return -EFAULT;
Paolo Bonzinibc009e42015-05-26 12:43:41 +02002428 mark_page_dirty_in_slot(memslot, gfn);
Izik Eidus195aefd2007-10-01 22:14:18 +02002429 return 0;
2430}
Paolo Bonzini8e734852015-05-17 13:58:53 +02002431
2432int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn,
2433 const void *data, int offset, int len)
2434{
2435 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
2436
2437 return __kvm_write_guest_page(slot, gfn, data, offset, len);
2438}
Izik Eidus195aefd2007-10-01 22:14:18 +02002439EXPORT_SYMBOL_GPL(kvm_write_guest_page);
2440
Paolo Bonzini8e734852015-05-17 13:58:53 +02002441int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
2442 const void *data, int offset, int len)
2443{
2444 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2445
2446 return __kvm_write_guest_page(slot, gfn, data, offset, len);
2447}
2448EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page);
2449
Izik Eidus195aefd2007-10-01 22:14:18 +02002450int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
2451 unsigned long len)
2452{
2453 gfn_t gfn = gpa >> PAGE_SHIFT;
2454 int seg;
2455 int offset = offset_in_page(gpa);
2456 int ret;
2457
2458 while ((seg = next_segment(len, offset)) != 0) {
2459 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
2460 if (ret < 0)
2461 return ret;
2462 offset = 0;
2463 len -= seg;
2464 data += seg;
2465 ++gfn;
2466 }
2467 return 0;
2468}
Wincy Vanff651cb2014-12-11 08:52:58 +03002469EXPORT_SYMBOL_GPL(kvm_write_guest);
Izik Eidus195aefd2007-10-01 22:14:18 +02002470
Paolo Bonzini8e734852015-05-17 13:58:53 +02002471int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
2472 unsigned long len)
2473{
2474 gfn_t gfn = gpa >> PAGE_SHIFT;
2475 int seg;
2476 int offset = offset_in_page(gpa);
2477 int ret;
2478
2479 while ((seg = next_segment(len, offset)) != 0) {
2480 ret = kvm_vcpu_write_guest_page(vcpu, gfn, data, offset, seg);
2481 if (ret < 0)
2482 return ret;
2483 offset = 0;
2484 len -= seg;
2485 data += seg;
2486 ++gfn;
2487 }
2488 return 0;
2489}
2490EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest);
2491
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002492static int __kvm_gfn_to_hva_cache_init(struct kvm_memslots *slots,
2493 struct gfn_to_hva_cache *ghc,
2494 gpa_t gpa, unsigned long len)
Gleb Natapov49c77542010-10-18 15:22:23 +02002495{
Gleb Natapov49c77542010-10-18 15:22:23 +02002496 int offset = offset_in_page(gpa);
Andrew Honig8f964522013-03-29 09:35:21 -07002497 gfn_t start_gfn = gpa >> PAGE_SHIFT;
2498 gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
2499 gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
2500 gfn_t nr_pages_avail;
Gleb Natapov49c77542010-10-18 15:22:23 +02002501
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002502 /* Update ghc->generation before performing any error checks. */
Gleb Natapov49c77542010-10-18 15:22:23 +02002503 ghc->generation = slots->generation;
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002504
2505 if (start_gfn > end_gfn) {
2506 ghc->hva = KVM_HVA_ERR_BAD;
2507 return -EINVAL;
2508 }
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002509
2510 /*
2511 * If the requested region crosses two memslots, we still
2512 * verify that the entire region is valid here.
2513 */
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002514 for ( ; start_gfn <= end_gfn; start_gfn += nr_pages_avail) {
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002515 ghc->memslot = __gfn_to_memslot(slots, start_gfn);
2516 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
2517 &nr_pages_avail);
2518 if (kvm_is_error_hva(ghc->hva))
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002519 return -EFAULT;
Andrew Honig8f964522013-03-29 09:35:21 -07002520 }
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002521
2522 /* Use the slow path for cross page reads and writes. */
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002523 if (nr_pages_needed == 1)
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002524 ghc->hva += offset;
2525 else
2526 ghc->memslot = NULL;
2527
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002528 ghc->gpa = gpa;
2529 ghc->len = len;
2530 return 0;
Gleb Natapov49c77542010-10-18 15:22:23 +02002531}
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002532
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002533int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002534 gpa_t gpa, unsigned long len)
2535{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002536 struct kvm_memslots *slots = kvm_memslots(kvm);
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002537 return __kvm_gfn_to_hva_cache_init(slots, ghc, gpa, len);
2538}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002539EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
Gleb Natapov49c77542010-10-18 15:22:23 +02002540
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002541int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
Jim Mattson7a86dab2018-12-14 14:34:43 -08002542 void *data, unsigned int offset,
2543 unsigned long len)
Gleb Natapov49c77542010-10-18 15:22:23 +02002544{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002545 struct kvm_memslots *slots = kvm_memslots(kvm);
Gleb Natapov49c77542010-10-18 15:22:23 +02002546 int r;
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002547 gpa_t gpa = ghc->gpa + offset;
Gleb Natapov49c77542010-10-18 15:22:23 +02002548
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002549 BUG_ON(len + offset > ghc->len);
Andrew Honig8f964522013-03-29 09:35:21 -07002550
Sean Christophersondc9ce712020-01-09 15:56:20 -08002551 if (slots->generation != ghc->generation) {
2552 if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len))
2553 return -EFAULT;
2554 }
Andrew Honig8f964522013-03-29 09:35:21 -07002555
Gleb Natapov49c77542010-10-18 15:22:23 +02002556 if (kvm_is_error_hva(ghc->hva))
2557 return -EFAULT;
2558
Sean Christophersonfcfbc612020-01-09 15:56:18 -08002559 if (unlikely(!ghc->memslot))
2560 return kvm_write_guest(kvm, gpa, data, len);
2561
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002562 r = __copy_to_user((void __user *)ghc->hva + offset, data, len);
Gleb Natapov49c77542010-10-18 15:22:23 +02002563 if (r)
2564 return -EFAULT;
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002565 mark_page_dirty_in_slot(ghc->memslot, gpa >> PAGE_SHIFT);
Gleb Natapov49c77542010-10-18 15:22:23 +02002566
2567 return 0;
2568}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002569EXPORT_SYMBOL_GPL(kvm_write_guest_offset_cached);
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002570
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002571int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
2572 void *data, unsigned long len)
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002573{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002574 return kvm_write_guest_offset_cached(kvm, ghc, data, 0, len);
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002575}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002576EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
Gleb Natapov49c77542010-10-18 15:22:23 +02002577
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002578int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
2579 void *data, unsigned int offset,
2580 unsigned long len)
Gleb Natapove03b6442011-07-11 15:28:11 -04002581{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002582 struct kvm_memslots *slots = kvm_memslots(kvm);
Gleb Natapove03b6442011-07-11 15:28:11 -04002583 int r;
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002584 gpa_t gpa = ghc->gpa + offset;
Gleb Natapove03b6442011-07-11 15:28:11 -04002585
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002586 BUG_ON(len + offset > ghc->len);
Andrew Honig8f964522013-03-29 09:35:21 -07002587
Sean Christophersondc9ce712020-01-09 15:56:20 -08002588 if (slots->generation != ghc->generation) {
2589 if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len))
2590 return -EFAULT;
2591 }
Andrew Honig8f964522013-03-29 09:35:21 -07002592
Gleb Natapove03b6442011-07-11 15:28:11 -04002593 if (kvm_is_error_hva(ghc->hva))
2594 return -EFAULT;
2595
Sean Christophersonfcfbc612020-01-09 15:56:18 -08002596 if (unlikely(!ghc->memslot))
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002597 return kvm_read_guest(kvm, gpa, data, len);
Sean Christophersonfcfbc612020-01-09 15:56:18 -08002598
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002599 r = __copy_from_user(data, (void __user *)ghc->hva + offset, len);
Gleb Natapove03b6442011-07-11 15:28:11 -04002600 if (r)
2601 return -EFAULT;
2602
2603 return 0;
2604}
Vitaly Kuznetsov0958f0c2020-05-25 16:41:19 +02002605EXPORT_SYMBOL_GPL(kvm_read_guest_offset_cached);
2606
2607int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
2608 void *data, unsigned long len)
2609{
2610 return kvm_read_guest_offset_cached(kvm, ghc, data, 0, len);
2611}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002612EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
Gleb Natapove03b6442011-07-11 15:28:11 -04002613
Izik Eidus195aefd2007-10-01 22:14:18 +02002614int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
2615{
Heiko Carstens8a3caa62013-11-18 10:35:55 +01002616 const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
2617
2618 return kvm_write_guest_page(kvm, gfn, zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02002619}
2620EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
2621
2622int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
2623{
2624 gfn_t gfn = gpa >> PAGE_SHIFT;
2625 int seg;
2626 int offset = offset_in_page(gpa);
2627 int ret;
2628
Kevin Mulveybfda0e82015-02-20 08:21:36 -05002629 while ((seg = next_segment(len, offset)) != 0) {
Izik Eidus195aefd2007-10-01 22:14:18 +02002630 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
2631 if (ret < 0)
2632 return ret;
2633 offset = 0;
2634 len -= seg;
2635 ++gfn;
2636 }
2637 return 0;
2638}
2639EXPORT_SYMBOL_GPL(kvm_clear_guest);
2640
Paolo Bonzinibc009e42015-05-26 12:43:41 +02002641static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot,
Stephen Hemminger79408762013-12-29 12:12:29 -08002642 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643{
Rusty Russell7e9d6192007-07-31 20:41:14 +10002644 if (memslot && memslot->dirty_bitmap) {
2645 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646
Takuya Yoshikawab74ca3b2012-10-04 17:13:12 -07002647 set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 }
2649}
2650
Gleb Natapov49c77542010-10-18 15:22:23 +02002651void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
2652{
2653 struct kvm_memory_slot *memslot;
2654
2655 memslot = gfn_to_memslot(kvm, gfn);
Paolo Bonzinibc009e42015-05-26 12:43:41 +02002656 mark_page_dirty_in_slot(memslot, gfn);
Gleb Natapov49c77542010-10-18 15:22:23 +02002657}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302658EXPORT_SYMBOL_GPL(mark_page_dirty);
Gleb Natapov49c77542010-10-18 15:22:23 +02002659
Paolo Bonzini8e734852015-05-17 13:58:53 +02002660void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn)
2661{
2662 struct kvm_memory_slot *memslot;
2663
2664 memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2665 mark_page_dirty_in_slot(memslot, gfn);
2666}
2667EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty);
2668
Jan H. Schönherr20b70352017-11-24 22:39:01 +01002669void kvm_sigset_activate(struct kvm_vcpu *vcpu)
2670{
2671 if (!vcpu->sigset_active)
2672 return;
2673
2674 /*
2675 * This does a lockless modification of ->real_blocked, which is fine
2676 * because, only current can change ->real_blocked and all readers of
2677 * ->real_blocked don't care as long ->real_blocked is always a subset
2678 * of ->blocked.
2679 */
2680 sigprocmask(SIG_SETMASK, &vcpu->sigset, &current->real_blocked);
2681}
2682
2683void kvm_sigset_deactivate(struct kvm_vcpu *vcpu)
2684{
2685 if (!vcpu->sigset_active)
2686 return;
2687
2688 sigprocmask(SIG_SETMASK, &current->real_blocked, NULL);
2689 sigemptyset(&current->real_blocked);
2690}
2691
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002692static void grow_halt_poll_ns(struct kvm_vcpu *vcpu)
2693{
Nir Weinerdee339b2019-01-27 12:17:16 +02002694 unsigned int old, val, grow, grow_start;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002695
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002696 old = val = vcpu->halt_poll_ns;
Nir Weinerdee339b2019-01-27 12:17:16 +02002697 grow_start = READ_ONCE(halt_poll_ns_grow_start);
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002698 grow = READ_ONCE(halt_poll_ns_grow);
Nir Weiner7fa08e72019-01-27 12:17:14 +02002699 if (!grow)
2700 goto out;
2701
Nir Weinerdee339b2019-01-27 12:17:16 +02002702 val *= grow;
2703 if (val < grow_start)
2704 val = grow_start;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002705
David Matlack313f6362016-03-08 16:19:44 -08002706 if (val > halt_poll_ns)
2707 val = halt_poll_ns;
2708
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002709 vcpu->halt_poll_ns = val;
Nir Weiner7fa08e72019-01-27 12:17:14 +02002710out:
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002711 trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old);
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002712}
2713
2714static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu)
2715{
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002716 unsigned int old, val, shrink;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002717
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002718 old = val = vcpu->halt_poll_ns;
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002719 shrink = READ_ONCE(halt_poll_ns_shrink);
2720 if (shrink == 0)
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002721 val = 0;
2722 else
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002723 val /= shrink;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002724
2725 vcpu->halt_poll_ns = val;
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002726 trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old);
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002727}
2728
Paolo Bonzinif7819512015-02-04 18:20:58 +01002729static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
2730{
Junaid Shahid50c28f22018-06-27 14:59:11 -07002731 int ret = -EINTR;
2732 int idx = srcu_read_lock(&vcpu->kvm->srcu);
2733
Paolo Bonzinif7819512015-02-04 18:20:58 +01002734 if (kvm_arch_vcpu_runnable(vcpu)) {
2735 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Junaid Shahid50c28f22018-06-27 14:59:11 -07002736 goto out;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002737 }
2738 if (kvm_cpu_has_pending_timer(vcpu))
Junaid Shahid50c28f22018-06-27 14:59:11 -07002739 goto out;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002740 if (signal_pending(current))
Junaid Shahid50c28f22018-06-27 14:59:11 -07002741 goto out;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002742
Junaid Shahid50c28f22018-06-27 14:59:11 -07002743 ret = 0;
2744out:
2745 srcu_read_unlock(&vcpu->kvm->srcu, idx);
2746 return ret;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002747}
2748
David Matlackcb953122020-05-08 11:22:40 -07002749static inline void
2750update_halt_poll_stats(struct kvm_vcpu *vcpu, u64 poll_ns, bool waited)
2751{
2752 if (waited)
2753 vcpu->stat.halt_poll_fail_ns += poll_ns;
2754 else
2755 vcpu->stat.halt_poll_success_ns += poll_ns;
2756}
2757
Eddie Dongb6958ce2007-07-18 12:15:21 +03002758/*
2759 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
2760 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05002761void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03002762{
David Matlackcb953122020-05-08 11:22:40 -07002763 ktime_t start, cur, poll_end;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002764 bool waited = false;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002765 u64 block_ns;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002766
Marc Zyngier07ab0f82019-08-02 11:37:09 +01002767 kvm_arch_vcpu_blocking(vcpu);
2768
David Matlackcb953122020-05-08 11:22:40 -07002769 start = cur = poll_end = ktime_get();
Christian Borntraegercdd6ad32019-03-05 05:30:01 -05002770 if (vcpu->halt_poll_ns && !kvm_arch_no_poll(vcpu)) {
Wanpeng Li19020f82015-09-03 22:07:37 +08002771 ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns);
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08002772
Paolo Bonzini62bea5b2015-09-15 18:27:57 +02002773 ++vcpu->stat.halt_attempted_poll;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002774 do {
2775 /*
2776 * This sets KVM_REQ_UNHALT if an interrupt
2777 * arrives.
2778 */
2779 if (kvm_vcpu_check_block(vcpu) < 0) {
2780 ++vcpu->stat.halt_successful_poll;
Christian Borntraeger3491caf2016-05-13 12:16:35 +02002781 if (!vcpu_valid_wakeup(vcpu))
2782 ++vcpu->stat.halt_poll_invalid;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002783 goto out;
2784 }
David Matlackcb953122020-05-08 11:22:40 -07002785 poll_end = cur = ktime_get();
Paolo Bonzinif7819512015-02-04 18:20:58 +01002786 } while (single_task_running() && ktime_before(cur, stop));
2787 }
Eddie Dongb6958ce2007-07-18 12:15:21 +03002788
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002789 prepare_to_rcuwait(&vcpu->wait);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03002790 for (;;) {
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002791 set_current_state(TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03002792
Paolo Bonzinif7819512015-02-04 18:20:58 +01002793 if (kvm_vcpu_check_block(vcpu) < 0)
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03002794 break;
2795
Paolo Bonzinif7819512015-02-04 18:20:58 +01002796 waited = true;
Eddie Dongb6958ce2007-07-18 12:15:21 +03002797 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03002798 }
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002799 finish_rcuwait(&vcpu->wait);
Paolo Bonzinif7819512015-02-04 18:20:58 +01002800 cur = ktime_get();
Paolo Bonzinif7819512015-02-04 18:20:58 +01002801out:
Marc Zyngier07ab0f82019-08-02 11:37:09 +01002802 kvm_arch_vcpu_unblocking(vcpu);
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002803 block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
2804
David Matlackcb953122020-05-08 11:22:40 -07002805 update_halt_poll_stats(
2806 vcpu, ktime_to_ns(ktime_sub(poll_end, start)), waited);
2807
Wanpeng Li44551b22019-09-29 09:06:56 +08002808 if (!kvm_arch_no_poll(vcpu)) {
2809 if (!vcpu_valid_wakeup(vcpu)) {
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002810 shrink_halt_poll_ns(vcpu);
David Matlackacd05782020-04-17 15:14:46 -07002811 } else if (vcpu->kvm->max_halt_poll_ns) {
Wanpeng Li44551b22019-09-29 09:06:56 +08002812 if (block_ns <= vcpu->halt_poll_ns)
2813 ;
2814 /* we had a long block, shrink polling */
David Matlackacd05782020-04-17 15:14:46 -07002815 else if (vcpu->halt_poll_ns &&
2816 block_ns > vcpu->kvm->max_halt_poll_ns)
Wanpeng Li44551b22019-09-29 09:06:56 +08002817 shrink_halt_poll_ns(vcpu);
2818 /* we had a short halt and our poll time is too small */
David Matlackacd05782020-04-17 15:14:46 -07002819 else if (vcpu->halt_poll_ns < vcpu->kvm->max_halt_poll_ns &&
2820 block_ns < vcpu->kvm->max_halt_poll_ns)
Wanpeng Li44551b22019-09-29 09:06:56 +08002821 grow_halt_poll_ns(vcpu);
2822 } else {
2823 vcpu->halt_poll_ns = 0;
2824 }
2825 }
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002826
Christian Borntraeger3491caf2016-05-13 12:16:35 +02002827 trace_kvm_vcpu_wakeup(block_ns, waited, vcpu_valid_wakeup(vcpu));
2828 kvm_arch_vcpu_block_finish(vcpu);
Eddie Dongb6958ce2007-07-18 12:15:21 +03002829}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302830EXPORT_SYMBOL_GPL(kvm_vcpu_block);
Eddie Dongb6958ce2007-07-18 12:15:21 +03002831
Radim Krčmář178f02f2017-04-26 22:32:26 +02002832bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
Christoffer Dallb6d33832012-03-08 16:44:24 -05002833{
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002834 struct rcuwait *waitp;
Christoffer Dallb6d33832012-03-08 16:44:24 -05002835
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002836 waitp = kvm_arch_vcpu_get_wait(vcpu);
2837 if (rcuwait_wake_up(waitp)) {
Wanpeng Lid73eb572019-07-18 19:39:06 +08002838 WRITE_ONCE(vcpu->ready, true);
Christoffer Dallb6d33832012-03-08 16:44:24 -05002839 ++vcpu->stat.halt_wakeup;
Radim Krčmář178f02f2017-04-26 22:32:26 +02002840 return true;
Christoffer Dallb6d33832012-03-08 16:44:24 -05002841 }
2842
Radim Krčmář178f02f2017-04-26 22:32:26 +02002843 return false;
Radim Krčmářdd1a4cc2016-05-04 14:09:44 -05002844}
2845EXPORT_SYMBOL_GPL(kvm_vcpu_wake_up);
2846
Paolo Bonzini0266c892017-05-04 15:14:13 +02002847#ifndef CONFIG_S390
Radim Krčmářdd1a4cc2016-05-04 14:09:44 -05002848/*
2849 * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
2850 */
2851void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
2852{
2853 int me;
2854 int cpu = vcpu->cpu;
2855
Radim Krčmář178f02f2017-04-26 22:32:26 +02002856 if (kvm_vcpu_wake_up(vcpu))
2857 return;
2858
Christoffer Dallb6d33832012-03-08 16:44:24 -05002859 me = get_cpu();
2860 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
2861 if (kvm_arch_vcpu_should_kick(vcpu))
2862 smp_send_reschedule(cpu);
2863 put_cpu();
2864}
Yang Zhanga20ed542013-04-11 19:25:15 +08002865EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
Paolo Bonzini0266c892017-05-04 15:14:13 +02002866#endif /* !CONFIG_S390 */
Christoffer Dallb6d33832012-03-08 16:44:24 -05002867
Dan Carpenterfa933842014-05-23 13:20:42 +03002868int kvm_vcpu_yield_to(struct kvm_vcpu *target)
Konstantin Weitz41628d32012-04-25 15:30:38 +02002869{
2870 struct pid *pid;
2871 struct task_struct *task = NULL;
Dan Carpenterfa933842014-05-23 13:20:42 +03002872 int ret = 0;
Konstantin Weitz41628d32012-04-25 15:30:38 +02002873
2874 rcu_read_lock();
2875 pid = rcu_dereference(target->pid);
2876 if (pid)
Sam Bobroff27fbe64b2014-09-19 09:40:41 +10002877 task = get_pid_task(pid, PIDTYPE_PID);
Konstantin Weitz41628d32012-04-25 15:30:38 +02002878 rcu_read_unlock();
2879 if (!task)
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302880 return ret;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302881 ret = yield_to(task, 1);
Konstantin Weitz41628d32012-04-25 15:30:38 +02002882 put_task_struct(task);
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302883
2884 return ret;
Konstantin Weitz41628d32012-04-25 15:30:38 +02002885}
2886EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
2887
Raghavendra K T06e48c52012-07-19 15:17:52 +05302888/*
2889 * Helper that checks whether a VCPU is eligible for directed yield.
2890 * Most eligible candidate to yield is decided by following heuristics:
2891 *
2892 * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
2893 * (preempted lock holder), indicated by @in_spin_loop.
Fuad Tabba656012c2020-04-01 15:03:10 +01002894 * Set at the beginning and cleared at the end of interception/PLE handler.
Raghavendra K T06e48c52012-07-19 15:17:52 +05302895 *
2896 * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
2897 * chance last time (mostly it has become eligible now since we have probably
2898 * yielded to lockholder in last iteration. This is done by toggling
2899 * @dy_eligible each time a VCPU checked for eligibility.)
2900 *
2901 * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
2902 * to preempted lock-holder could result in wrong VCPU selection and CPU
2903 * burning. Giving priority for a potential lock-holder increases lock
2904 * progress.
2905 *
2906 * Since algorithm is based on heuristics, accessing another VCPU data without
2907 * locking does not harm. It may result in trying to yield to same VCPU, fail
2908 * and continue with next VCPU and so on.
2909 */
Stephen Hemminger79408762013-12-29 12:12:29 -08002910static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
Raghavendra K T06e48c52012-07-19 15:17:52 +05302911{
Scott Wood4a55dd72014-01-09 18:43:16 -06002912#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
Raghavendra K T06e48c52012-07-19 15:17:52 +05302913 bool eligible;
2914
2915 eligible = !vcpu->spin_loop.in_spin_loop ||
Christian Borntraeger34656112014-09-04 21:13:31 +02002916 vcpu->spin_loop.dy_eligible;
Raghavendra K T06e48c52012-07-19 15:17:52 +05302917
2918 if (vcpu->spin_loop.in_spin_loop)
2919 kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
2920
2921 return eligible;
Scott Wood4a55dd72014-01-09 18:43:16 -06002922#else
2923 return true;
Raghavendra K T06e48c52012-07-19 15:17:52 +05302924#endif
Scott Wood4a55dd72014-01-09 18:43:16 -06002925}
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302926
Wanpeng Li17e433b2019-08-05 10:03:19 +08002927/*
2928 * Unlike kvm_arch_vcpu_runnable, this function is called outside
2929 * a vcpu_load/vcpu_put pair. However, for most architectures
2930 * kvm_arch_vcpu_runnable does not require vcpu_load.
2931 */
2932bool __weak kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
2933{
2934 return kvm_arch_vcpu_runnable(vcpu);
2935}
2936
2937static bool vcpu_dy_runnable(struct kvm_vcpu *vcpu)
2938{
2939 if (kvm_arch_dy_runnable(vcpu))
2940 return true;
2941
2942#ifdef CONFIG_KVM_ASYNC_PF
2943 if (!list_empty_careful(&vcpu->async_pf.done))
2944 return true;
2945#endif
2946
2947 return false;
2948}
2949
Longpeng(Mike)199b5762017-08-08 12:05:32 +08002950void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08002951{
Rik van Riel217ece62011-02-01 09:53:28 -05002952 struct kvm *kvm = me->kvm;
2953 struct kvm_vcpu *vcpu;
2954 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
2955 int yielded = 0;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302956 int try = 3;
Rik van Riel217ece62011-02-01 09:53:28 -05002957 int pass;
2958 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08002959
Raghavendra K T4c088492012-07-18 19:07:46 +05302960 kvm_vcpu_set_in_spin_loop(me, true);
Rik van Riel217ece62011-02-01 09:53:28 -05002961 /*
2962 * We boost the priority of a VCPU that is runnable but not
2963 * currently running, because it got preempted by something
2964 * else and called schedule in __vcpu_run. Hopefully that
2965 * VCPU is holding the lock that we need and will release it.
2966 * We approximate round-robin by starting at the last boosted VCPU.
2967 */
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302968 for (pass = 0; pass < 2 && !yielded && try; pass++) {
Rik van Riel217ece62011-02-01 09:53:28 -05002969 kvm_for_each_vcpu(i, vcpu, kvm) {
Rik van Riel5cfc2aa2012-06-19 16:51:04 -04002970 if (!pass && i <= last_boosted_vcpu) {
Rik van Riel217ece62011-02-01 09:53:28 -05002971 i = last_boosted_vcpu;
2972 continue;
2973 } else if (pass && i > last_boosted_vcpu)
2974 break;
Wanpeng Lid73eb572019-07-18 19:39:06 +08002975 if (!READ_ONCE(vcpu->ready))
Raghavendra K T7bc7ae22013-03-04 23:32:27 +05302976 continue;
Rik van Riel217ece62011-02-01 09:53:28 -05002977 if (vcpu == me)
2978 continue;
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002979 if (rcuwait_active(&vcpu->wait) &&
2980 !vcpu_dy_runnable(vcpu))
Rik van Riel217ece62011-02-01 09:53:28 -05002981 continue;
Wanpeng Li046ddee2019-08-01 11:30:14 +08002982 if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode &&
2983 !kvm_arch_vcpu_in_kernel(vcpu))
Longpeng(Mike)199b5762017-08-08 12:05:32 +08002984 continue;
Raghavendra K T06e48c52012-07-19 15:17:52 +05302985 if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
2986 continue;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302987
2988 yielded = kvm_vcpu_yield_to(vcpu);
2989 if (yielded > 0) {
Rik van Riel217ece62011-02-01 09:53:28 -05002990 kvm->last_boosted_vcpu = i;
Rik van Riel217ece62011-02-01 09:53:28 -05002991 break;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302992 } else if (yielded < 0) {
2993 try--;
2994 if (!try)
2995 break;
Rik van Riel217ece62011-02-01 09:53:28 -05002996 }
Rik van Riel217ece62011-02-01 09:53:28 -05002997 }
2998 }
Raghavendra K T4c088492012-07-18 19:07:46 +05302999 kvm_vcpu_set_in_spin_loop(me, false);
Raghavendra K T06e48c52012-07-19 15:17:52 +05303000
3001 /* Ensure vcpu is not eligible during next spinloop */
3002 kvm_vcpu_set_dy_eligible(me, false);
Zhai, Edwind255f4f2009-10-09 18:03:20 +08003003}
3004EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
3005
Souptick Joarder1499fa82018-04-19 00:49:58 +05303006static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003007{
Dave Jiang11bac802017-02-24 14:56:41 -08003008 struct kvm_vcpu *vcpu = vmf->vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003009 struct page *page;
3010
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11003011 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02003012 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02003013#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11003014 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003015 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02003016#endif
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003017#ifdef CONFIG_KVM_MMIO
Laurent Vivier5f94c172008-05-30 16:05:54 +02003018 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
3019 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
3020#endif
Avi Kivity039576c2007-03-20 12:46:50 +02003021 else
Carsten Otte5b1c1492012-01-04 10:25:23 +01003022 return kvm_arch_vcpu_fault(vcpu, vmf);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003023 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11003024 vmf->page = page;
3025 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003026}
3027
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003028static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11003029 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003030};
3031
3032static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
3033{
3034 vma->vm_ops = &kvm_vcpu_vm_ops;
3035 return 0;
3036}
3037
Avi Kivitybccf2152007-02-21 18:04:26 +02003038static int kvm_vcpu_release(struct inode *inode, struct file *filp)
3039{
3040 struct kvm_vcpu *vcpu = filp->private_data;
3041
Al Viro66c0b392008-04-19 20:33:56 +01003042 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02003043 return 0;
3044}
3045
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01003046static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02003047 .release = kvm_vcpu_release,
3048 .unlocked_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02003049 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003050 .llseek = noop_llseek,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01003051 KVM_COMPAT(kvm_vcpu_compat_ioctl),
Avi Kivitybccf2152007-02-21 18:04:26 +02003052};
3053
3054/*
3055 * Allocates an inode for the vcpu.
3056 */
3057static int create_vcpu_fd(struct kvm_vcpu *vcpu)
3058{
Masatake YAMATOe46b4692018-01-20 04:04:22 +09003059 char name[8 + 1 + ITOA_MAX_LEN + 1];
3060
3061 snprintf(name, sizeof(name), "kvm-vcpu:%d", vcpu->vcpu_id);
3062 return anon_inode_getfd(name, &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC);
Avi Kivitybccf2152007-02-21 18:04:26 +02003063}
3064
Greg KH3e7093d2019-07-31 20:56:20 +02003065static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
Luiz Capitulino45b59392016-09-16 10:27:35 -04003066{
Paolo Bonzini741cbba2019-08-03 08:14:25 +02003067#ifdef __KVM_HAVE_ARCH_VCPU_DEBUGFS
Paolo Bonzinid56f5132020-06-04 15:16:52 +02003068 struct dentry *debugfs_dentry;
Luiz Capitulino45b59392016-09-16 10:27:35 -04003069 char dir_name[ITOA_MAX_LEN * 2];
Luiz Capitulino45b59392016-09-16 10:27:35 -04003070
Luiz Capitulino45b59392016-09-16 10:27:35 -04003071 if (!debugfs_initialized())
Greg KH3e7093d2019-07-31 20:56:20 +02003072 return;
Luiz Capitulino45b59392016-09-16 10:27:35 -04003073
3074 snprintf(dir_name, sizeof(dir_name), "vcpu%d", vcpu->vcpu_id);
Paolo Bonzinid56f5132020-06-04 15:16:52 +02003075 debugfs_dentry = debugfs_create_dir(dir_name,
3076 vcpu->kvm->debugfs_dentry);
Luiz Capitulino45b59392016-09-16 10:27:35 -04003077
Paolo Bonzinid56f5132020-06-04 15:16:52 +02003078 kvm_arch_create_vcpu_debugfs(vcpu, debugfs_dentry);
Paolo Bonzini741cbba2019-08-03 08:14:25 +02003079#endif
Luiz Capitulino45b59392016-09-16 10:27:35 -04003080}
3081
Avi Kivityc5ea7662007-02-20 18:41:05 +02003082/*
3083 * Creates some virtual cpus. Good luck creating more than one.
3084 */
Gleb Natapov73880c82009-06-09 15:56:28 +03003085static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02003086{
3087 int r;
David Hildenbrande09fefd2015-11-05 09:03:50 +01003088 struct kvm_vcpu *vcpu;
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003089 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02003090
Greg Kurz0b1b1df2016-05-09 18:13:37 +02003091 if (id >= KVM_MAX_VCPU_ID)
Andy Honig338c7db2013-11-18 16:09:22 -08003092 return -EINVAL;
3093
Paolo Bonzini6c7caeb2016-06-13 14:48:25 +02003094 mutex_lock(&kvm->lock);
3095 if (kvm->created_vcpus == KVM_MAX_VCPUS) {
3096 mutex_unlock(&kvm->lock);
3097 return -EINVAL;
3098 }
3099
3100 kvm->created_vcpus++;
3101 mutex_unlock(&kvm->lock);
3102
Sean Christopherson897cc382019-12-18 13:55:09 -08003103 r = kvm_arch_vcpu_precreate(kvm, id);
3104 if (r)
3105 goto vcpu_decrement;
3106
Sean Christophersone529ef62019-12-18 13:55:15 -08003107 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
3108 if (!vcpu) {
3109 r = -ENOMEM;
Paolo Bonzini6c7caeb2016-06-13 14:48:25 +02003110 goto vcpu_decrement;
3111 }
Avi Kivityc5ea7662007-02-20 18:41:05 +02003112
Peter Xufcd97ad2020-01-09 09:57:12 -05003113 BUILD_BUG_ON(sizeof(struct kvm_run) > PAGE_SIZE);
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003114 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3115 if (!page) {
3116 r = -ENOMEM;
Sean Christophersone529ef62019-12-18 13:55:15 -08003117 goto vcpu_free;
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003118 }
3119 vcpu->run = page_address(page);
3120
3121 kvm_vcpu_init(vcpu, kvm, id);
Sean Christophersone529ef62019-12-18 13:55:15 -08003122
3123 r = kvm_arch_vcpu_create(vcpu);
3124 if (r)
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003125 goto vcpu_free_run_page;
Sean Christophersone529ef62019-12-18 13:55:15 -08003126
Shaohua Li11ec2802007-07-23 14:51:37 +08003127 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +01003128 if (kvm_get_vcpu_by_id(kvm, id)) {
3129 r = -EEXIST;
3130 goto unlock_vcpu_destroy;
3131 }
Gleb Natapov73880c82009-06-09 15:56:28 +03003132
Radim Krčmář8750e722019-11-07 07:53:42 -05003133 vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
3134 BUG_ON(kvm->vcpus[vcpu->vcpu_idx]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003135
3136 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01003137 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02003138 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03003139 if (r < 0) {
Sean Christopherson149487b2019-10-21 15:58:42 -07003140 kvm_put_kvm_no_destroy(kvm);
Jan Kiszkad7805922011-05-23 10:33:05 +02003141 goto unlock_vcpu_destroy;
Gleb Natapov73880c82009-06-09 15:56:28 +03003142 }
3143
Radim Krčmář8750e722019-11-07 07:53:42 -05003144 kvm->vcpus[vcpu->vcpu_idx] = vcpu;
Paolo Bonzinidd489242015-07-29 11:32:20 +02003145
3146 /*
3147 * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus
3148 * before kvm->online_vcpu's incremented value.
3149 */
Gleb Natapov73880c82009-06-09 15:56:28 +03003150 smp_wmb();
3151 atomic_inc(&kvm->online_vcpus);
3152
Gleb Natapov73880c82009-06-09 15:56:28 +03003153 mutex_unlock(&kvm->lock);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02003154 kvm_arch_vcpu_postcreate(vcpu);
Paolo Bonzini63d04342020-04-01 00:42:22 +02003155 kvm_create_vcpu_debugfs(vcpu);
Avi Kivitybccf2152007-02-21 18:04:26 +02003156 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02003157
Jan Kiszkad7805922011-05-23 10:33:05 +02003158unlock_vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03003159 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06003160 kvm_arch_vcpu_destroy(vcpu);
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003161vcpu_free_run_page:
3162 free_page((unsigned long)vcpu->run);
Sean Christophersone529ef62019-12-18 13:55:15 -08003163vcpu_free:
3164 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paolo Bonzini6c7caeb2016-06-13 14:48:25 +02003165vcpu_decrement:
3166 mutex_lock(&kvm->lock);
3167 kvm->created_vcpus--;
3168 mutex_unlock(&kvm->lock);
Avi Kivityc5ea7662007-02-20 18:41:05 +02003169 return r;
3170}
3171
Avi Kivity1961d272007-03-05 19:46:05 +02003172static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
3173{
3174 if (sigset) {
3175 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3176 vcpu->sigset_active = 1;
3177 vcpu->sigset = *sigset;
3178 } else
3179 vcpu->sigset_active = 0;
3180 return 0;
3181}
3182
Avi Kivitybccf2152007-02-21 18:04:26 +02003183static long kvm_vcpu_ioctl(struct file *filp,
3184 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185{
Avi Kivitybccf2152007-02-21 18:04:26 +02003186 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00003187 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003188 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07003189 struct kvm_fpu *fpu = NULL;
3190 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191
Avi Kivity6d4e4c42007-11-21 16:41:05 +02003192 if (vcpu->kvm->mm != current->mm)
3193 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03003194
David Matlack2ea75be2014-09-19 16:03:25 -07003195 if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
3196 return -EINVAL;
3197
Avi Kivity2122ff52010-05-13 11:25:04 +03003198 /*
Paolo Bonzini5cb09442017-12-12 17:41:34 +01003199 * Some architectures have vcpu ioctls that are asynchronous to vcpu
3200 * execution; mutex_lock() would break them.
Avi Kivity2122ff52010-05-13 11:25:04 +03003201 */
Paolo Bonzini5cb09442017-12-12 17:41:34 +01003202 r = kvm_arch_vcpu_async_ioctl(filp, ioctl, arg);
3203 if (r != -ENOIOCTLCMD)
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03003204 return r;
Avi Kivity2122ff52010-05-13 11:25:04 +03003205
Christoffer Dallec7660c2017-12-04 21:35:23 +01003206 if (mutex_lock_killable(&vcpu->mutex))
3207 return -EINTR;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 switch (ioctl) {
Christian Borntraeger0e4524a2017-07-06 14:44:28 +02003209 case KVM_RUN: {
3210 struct pid *oldpid;
Avi Kivityf0fe5102007-03-07 13:11:17 +02003211 r = -EINVAL;
3212 if (arg)
3213 goto out;
Christian Borntraeger0e4524a2017-07-06 14:44:28 +02003214 oldpid = rcu_access_pointer(vcpu->pid);
Eric W. Biederman71dbc8a2017-07-16 21:39:32 -05003215 if (unlikely(oldpid != task_pid(current))) {
Christian Borntraeger7a72f7a2014-08-05 16:44:14 +02003216 /* The thread running this VCPU changed. */
Christoffer Dallbd2a6392018-02-23 17:23:57 +01003217 struct pid *newpid;
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08003218
Christoffer Dallbd2a6392018-02-23 17:23:57 +01003219 r = kvm_arch_vcpu_run_pid_change(vcpu);
3220 if (r)
3221 break;
3222
3223 newpid = get_task_pid(current, PIDTYPE_PID);
Christian Borntraeger7a72f7a2014-08-05 16:44:14 +02003224 rcu_assign_pointer(vcpu->pid, newpid);
3225 if (oldpid)
3226 synchronize_rcu();
3227 put_pid(oldpid);
3228 }
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08003229 r = kvm_arch_vcpu_ioctl_run(vcpu);
Gleb Natapov64be5002010-10-24 16:49:08 +02003230 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 break;
Christian Borntraeger0e4524a2017-07-06 14:44:28 +02003232 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003234 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003236 r = -ENOMEM;
Ben Gardonb12ce362019-02-11 11:02:49 -08003237 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL_ACCOUNT);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003238 if (!kvm_regs)
3239 goto out;
3240 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003242 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003244 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
3245 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003247out_free1:
3248 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249 break;
3250 }
3251 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003252 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253
Sasha Levinff5c2c02011-12-04 19:36:29 +02003254 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
3255 if (IS_ERR(kvm_regs)) {
3256 r = PTR_ERR(kvm_regs);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003257 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003258 }
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003259 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003260 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 break;
3262 }
3263 case KVM_GET_SREGS: {
Ben Gardonb12ce362019-02-11 11:02:49 -08003264 kvm_sregs = kzalloc(sizeof(struct kvm_sregs),
3265 GFP_KERNEL_ACCOUNT);
Dave Hansenfa3795a2008-08-11 10:01:46 -07003266 r = -ENOMEM;
3267 if (!kvm_sregs)
3268 goto out;
3269 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270 if (r)
3271 goto out;
3272 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07003273 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274 goto out;
3275 r = 0;
3276 break;
3277 }
3278 case KVM_SET_SREGS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003279 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
3280 if (IS_ERR(kvm_sregs)) {
3281 r = PTR_ERR(kvm_sregs);
Guo Chao18595412012-11-02 18:33:21 +08003282 kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003284 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07003285 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003286 break;
3287 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003288 case KVM_GET_MP_STATE: {
3289 struct kvm_mp_state mp_state;
3290
3291 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
3292 if (r)
3293 goto out;
3294 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003295 if (copy_to_user(argp, &mp_state, sizeof(mp_state)))
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003296 goto out;
3297 r = 0;
3298 break;
3299 }
3300 case KVM_SET_MP_STATE: {
3301 struct kvm_mp_state mp_state;
3302
3303 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003304 if (copy_from_user(&mp_state, argp, sizeof(mp_state)))
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003305 goto out;
3306 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003307 break;
3308 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309 case KVM_TRANSLATE: {
3310 struct kvm_translation tr;
3311
3312 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003313 if (copy_from_user(&tr, argp, sizeof(tr)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08003315 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316 if (r)
3317 goto out;
3318 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003319 if (copy_to_user(argp, &tr, sizeof(tr)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003320 goto out;
3321 r = 0;
3322 break;
3323 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003324 case KVM_SET_GUEST_DEBUG: {
3325 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326
3327 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003328 if (copy_from_user(&dbg, argp, sizeof(dbg)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003330 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003331 break;
3332 }
Avi Kivity1961d272007-03-05 19:46:05 +02003333 case KVM_SET_SIGNAL_MASK: {
3334 struct kvm_signal_mask __user *sigmask_arg = argp;
3335 struct kvm_signal_mask kvm_sigmask;
3336 sigset_t sigset, *p;
3337
3338 p = NULL;
3339 if (argp) {
3340 r = -EFAULT;
3341 if (copy_from_user(&kvm_sigmask, argp,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003342 sizeof(kvm_sigmask)))
Avi Kivity1961d272007-03-05 19:46:05 +02003343 goto out;
3344 r = -EINVAL;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003345 if (kvm_sigmask.len != sizeof(sigset))
Avi Kivity1961d272007-03-05 19:46:05 +02003346 goto out;
3347 r = -EFAULT;
3348 if (copy_from_user(&sigset, sigmask_arg->sigset,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003349 sizeof(sigset)))
Avi Kivity1961d272007-03-05 19:46:05 +02003350 goto out;
3351 p = &sigset;
3352 }
Andi Kleen376d41f2010-06-10 13:10:47 +02003353 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02003354 break;
3355 }
Avi Kivityb8836732007-04-01 16:34:31 +03003356 case KVM_GET_FPU: {
Ben Gardonb12ce362019-02-11 11:02:49 -08003357 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL_ACCOUNT);
Dave Hansenfa3795a2008-08-11 10:01:46 -07003358 r = -ENOMEM;
3359 if (!fpu)
3360 goto out;
3361 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03003362 if (r)
3363 goto out;
3364 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07003365 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03003366 goto out;
3367 r = 0;
3368 break;
3369 }
3370 case KVM_SET_FPU: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003371 fpu = memdup_user(argp, sizeof(*fpu));
3372 if (IS_ERR(fpu)) {
3373 r = PTR_ERR(fpu);
Guo Chao18595412012-11-02 18:33:21 +08003374 fpu = NULL;
Avi Kivityb8836732007-04-01 16:34:31 +03003375 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003376 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07003377 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03003378 break;
3379 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003380 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02003381 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02003382 }
3383out:
Christoffer Dallec7660c2017-12-04 21:35:23 +01003384 mutex_unlock(&vcpu->mutex);
Dave Hansenfa3795a2008-08-11 10:01:46 -07003385 kfree(fpu);
3386 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02003387 return r;
3388}
3389
Christian Borntraegerde8e5d72015-02-03 09:35:15 +01003390#ifdef CONFIG_KVM_COMPAT
Alexander Graf1dda6062011-06-08 02:45:37 +02003391static long kvm_vcpu_compat_ioctl(struct file *filp,
3392 unsigned int ioctl, unsigned long arg)
3393{
3394 struct kvm_vcpu *vcpu = filp->private_data;
3395 void __user *argp = compat_ptr(arg);
3396 int r;
3397
3398 if (vcpu->kvm->mm != current->mm)
3399 return -EIO;
3400
3401 switch (ioctl) {
3402 case KVM_SET_SIGNAL_MASK: {
3403 struct kvm_signal_mask __user *sigmask_arg = argp;
3404 struct kvm_signal_mask kvm_sigmask;
Alexander Graf1dda6062011-06-08 02:45:37 +02003405 sigset_t sigset;
3406
3407 if (argp) {
3408 r = -EFAULT;
3409 if (copy_from_user(&kvm_sigmask, argp,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003410 sizeof(kvm_sigmask)))
Alexander Graf1dda6062011-06-08 02:45:37 +02003411 goto out;
3412 r = -EINVAL;
Al Viro3968cf62017-09-03 21:45:17 -04003413 if (kvm_sigmask.len != sizeof(compat_sigset_t))
Alexander Graf1dda6062011-06-08 02:45:37 +02003414 goto out;
3415 r = -EFAULT;
Paolo Bonzini1393b4a2020-07-02 05:39:31 -04003416 if (get_compat_sigset(&sigset,
3417 (compat_sigset_t __user *)sigmask_arg->sigset))
Alexander Graf1dda6062011-06-08 02:45:37 +02003418 goto out;
Alan Cox760a9a32012-08-22 14:34:11 +01003419 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
3420 } else
3421 r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
Alexander Graf1dda6062011-06-08 02:45:37 +02003422 break;
3423 }
3424 default:
3425 r = kvm_vcpu_ioctl(filp, ioctl, arg);
3426 }
3427
3428out:
3429 return r;
3430}
3431#endif
3432
Cédric Le Goatera1cd3f02019-04-18 12:39:36 +02003433static int kvm_device_mmap(struct file *filp, struct vm_area_struct *vma)
3434{
3435 struct kvm_device *dev = filp->private_data;
3436
3437 if (dev->ops->mmap)
3438 return dev->ops->mmap(dev, vma);
3439
3440 return -ENODEV;
3441}
3442
Scott Wood852b6d52013-04-12 14:08:42 +00003443static int kvm_device_ioctl_attr(struct kvm_device *dev,
3444 int (*accessor)(struct kvm_device *dev,
3445 struct kvm_device_attr *attr),
3446 unsigned long arg)
3447{
3448 struct kvm_device_attr attr;
3449
3450 if (!accessor)
3451 return -EPERM;
3452
3453 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
3454 return -EFAULT;
3455
3456 return accessor(dev, &attr);
3457}
3458
3459static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
3460 unsigned long arg)
3461{
3462 struct kvm_device *dev = filp->private_data;
3463
Sean Christophersonddba9182019-02-15 12:48:39 -08003464 if (dev->kvm->mm != current->mm)
3465 return -EIO;
3466
Scott Wood852b6d52013-04-12 14:08:42 +00003467 switch (ioctl) {
3468 case KVM_SET_DEVICE_ATTR:
3469 return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
3470 case KVM_GET_DEVICE_ATTR:
3471 return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
3472 case KVM_HAS_DEVICE_ATTR:
3473 return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
3474 default:
3475 if (dev->ops->ioctl)
3476 return dev->ops->ioctl(dev, ioctl, arg);
3477
3478 return -ENOTTY;
3479 }
3480}
3481
Scott Wood852b6d52013-04-12 14:08:42 +00003482static int kvm_device_release(struct inode *inode, struct file *filp)
3483{
3484 struct kvm_device *dev = filp->private_data;
3485 struct kvm *kvm = dev->kvm;
3486
Cédric Le Goater2bde9b32019-04-18 12:39:41 +02003487 if (dev->ops->release) {
3488 mutex_lock(&kvm->lock);
3489 list_del(&dev->vm_node);
3490 dev->ops->release(dev);
3491 mutex_unlock(&kvm->lock);
3492 }
3493
Scott Wood852b6d52013-04-12 14:08:42 +00003494 kvm_put_kvm(kvm);
3495 return 0;
3496}
3497
3498static const struct file_operations kvm_device_fops = {
3499 .unlocked_ioctl = kvm_device_ioctl,
3500 .release = kvm_device_release,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01003501 KVM_COMPAT(kvm_device_ioctl),
Cédric Le Goatera1cd3f02019-04-18 12:39:36 +02003502 .mmap = kvm_device_mmap,
Scott Wood852b6d52013-04-12 14:08:42 +00003503};
3504
3505struct kvm_device *kvm_device_from_filp(struct file *filp)
3506{
3507 if (filp->f_op != &kvm_device_fops)
3508 return NULL;
3509
3510 return filp->private_data;
3511}
3512
Steven Price8538cb22019-10-21 16:28:19 +01003513static const struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
Will Deacond60eacb2014-09-02 10:27:33 +01003514#ifdef CONFIG_KVM_MPIC
3515 [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops,
3516 [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops,
3517#endif
Will Deacond60eacb2014-09-02 10:27:33 +01003518};
3519
Steven Price8538cb22019-10-21 16:28:19 +01003520int kvm_register_device_ops(const struct kvm_device_ops *ops, u32 type)
Will Deacond60eacb2014-09-02 10:27:33 +01003521{
3522 if (type >= ARRAY_SIZE(kvm_device_ops_table))
3523 return -ENOSPC;
3524
3525 if (kvm_device_ops_table[type] != NULL)
3526 return -EEXIST;
3527
3528 kvm_device_ops_table[type] = ops;
3529 return 0;
3530}
3531
Wanpeng Li571ee1b2014-10-09 18:30:08 +08003532void kvm_unregister_device_ops(u32 type)
3533{
3534 if (kvm_device_ops_table[type] != NULL)
3535 kvm_device_ops_table[type] = NULL;
3536}
3537
Scott Wood852b6d52013-04-12 14:08:42 +00003538static int kvm_ioctl_create_device(struct kvm *kvm,
3539 struct kvm_create_device *cd)
3540{
Steven Price8538cb22019-10-21 16:28:19 +01003541 const struct kvm_device_ops *ops = NULL;
Scott Wood852b6d52013-04-12 14:08:42 +00003542 struct kvm_device *dev;
3543 bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
Paolo Bonzini1d487e92019-04-11 11:16:47 +02003544 int type;
Scott Wood852b6d52013-04-12 14:08:42 +00003545 int ret;
3546
Will Deacond60eacb2014-09-02 10:27:33 +01003547 if (cd->type >= ARRAY_SIZE(kvm_device_ops_table))
Scott Wood852b6d52013-04-12 14:08:42 +00003548 return -ENODEV;
Will Deacond60eacb2014-09-02 10:27:33 +01003549
Paolo Bonzini1d487e92019-04-11 11:16:47 +02003550 type = array_index_nospec(cd->type, ARRAY_SIZE(kvm_device_ops_table));
3551 ops = kvm_device_ops_table[type];
Will Deacond60eacb2014-09-02 10:27:33 +01003552 if (ops == NULL)
3553 return -ENODEV;
Scott Wood852b6d52013-04-12 14:08:42 +00003554
3555 if (test)
3556 return 0;
3557
Ben Gardonb12ce362019-02-11 11:02:49 -08003558 dev = kzalloc(sizeof(*dev), GFP_KERNEL_ACCOUNT);
Scott Wood852b6d52013-04-12 14:08:42 +00003559 if (!dev)
3560 return -ENOMEM;
3561
3562 dev->ops = ops;
3563 dev->kvm = kvm;
Scott Wood852b6d52013-04-12 14:08:42 +00003564
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003565 mutex_lock(&kvm->lock);
Paolo Bonzini1d487e92019-04-11 11:16:47 +02003566 ret = ops->create(dev, type);
Scott Wood852b6d52013-04-12 14:08:42 +00003567 if (ret < 0) {
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003568 mutex_unlock(&kvm->lock);
Scott Wood852b6d52013-04-12 14:08:42 +00003569 kfree(dev);
3570 return ret;
3571 }
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003572 list_add(&dev->vm_node, &kvm->devices);
3573 mutex_unlock(&kvm->lock);
Scott Wood852b6d52013-04-12 14:08:42 +00003574
Christoffer Dall023e9fd2016-08-09 19:13:00 +02003575 if (ops->init)
3576 ops->init(dev);
3577
Jann Horncfa39382019-01-26 01:54:33 +01003578 kvm_get_kvm(kvm);
Yann Droneaud24009b02013-08-24 22:14:07 +02003579 ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC);
Scott Wood852b6d52013-04-12 14:08:42 +00003580 if (ret < 0) {
Sean Christopherson149487b2019-10-21 15:58:42 -07003581 kvm_put_kvm_no_destroy(kvm);
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003582 mutex_lock(&kvm->lock);
3583 list_del(&dev->vm_node);
3584 mutex_unlock(&kvm->lock);
Dan Carpentera0f1d212016-11-30 22:21:05 +03003585 ops->destroy(dev);
Scott Wood852b6d52013-04-12 14:08:42 +00003586 return ret;
3587 }
3588
Scott Wood852b6d52013-04-12 14:08:42 +00003589 cd->fd = ret;
3590 return 0;
3591}
3592
Alexander Graf92b591a2014-07-14 18:33:08 +02003593static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
3594{
3595 switch (arg) {
3596 case KVM_CAP_USER_MEMORY:
3597 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
3598 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Alexander Graf92b591a2014-07-14 18:33:08 +02003599 case KVM_CAP_INTERNAL_ERROR_DATA:
3600#ifdef CONFIG_HAVE_KVM_MSI
3601 case KVM_CAP_SIGNAL_MSI:
3602#endif
Paul Mackerras297e2102014-06-30 20:51:13 +10003603#ifdef CONFIG_HAVE_KVM_IRQFD
Paolo Bonzinidc9be0f2015-03-05 11:54:46 +01003604 case KVM_CAP_IRQFD:
Alexander Graf92b591a2014-07-14 18:33:08 +02003605 case KVM_CAP_IRQFD_RESAMPLE:
3606#endif
Jason Wange9ea5062015-09-15 14:41:59 +08003607 case KVM_CAP_IOEVENTFD_ANY_LENGTH:
Alexander Graf92b591a2014-07-14 18:33:08 +02003608 case KVM_CAP_CHECK_EXTENSION_VM:
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003609 case KVM_CAP_ENABLE_CAP_VM:
David Matlackacd05782020-04-17 15:14:46 -07003610 case KVM_CAP_HALT_POLL:
Alexander Graf92b591a2014-07-14 18:33:08 +02003611 return 1;
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003612#ifdef CONFIG_KVM_MMIO
Paolo Bonzini30422552017-03-31 13:53:22 +02003613 case KVM_CAP_COALESCED_MMIO:
3614 return KVM_COALESCED_MMIO_PAGE_OFFSET;
Peng Hao0804c842018-10-14 07:09:55 +08003615 case KVM_CAP_COALESCED_PIO:
3616 return 1;
Paolo Bonzini30422552017-03-31 13:53:22 +02003617#endif
Jay Zhou3c9bd402020-02-27 09:32:27 +08003618#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
3619 case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2:
3620 return KVM_DIRTY_LOG_MANUAL_CAPS;
3621#endif
Alexander Graf92b591a2014-07-14 18:33:08 +02003622#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
3623 case KVM_CAP_IRQ_ROUTING:
3624 return KVM_MAX_IRQ_ROUTES;
3625#endif
Paolo Bonzinif481b062015-05-17 17:30:37 +02003626#if KVM_ADDRESS_SPACE_NUM > 1
3627 case KVM_CAP_MULTI_ADDRESS_SPACE:
3628 return KVM_ADDRESS_SPACE_NUM;
3629#endif
Paolo Bonzinic110ae52019-03-28 17:24:03 +01003630 case KVM_CAP_NR_MEMSLOTS:
3631 return KVM_USER_MEM_SLOTS;
Alexander Graf92b591a2014-07-14 18:33:08 +02003632 default:
3633 break;
3634 }
3635 return kvm_vm_ioctl_check_extension(kvm, arg);
3636}
3637
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003638int __attribute__((weak)) kvm_vm_ioctl_enable_cap(struct kvm *kvm,
3639 struct kvm_enable_cap *cap)
3640{
3641 return -EINVAL;
3642}
3643
3644static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,
3645 struct kvm_enable_cap *cap)
3646{
3647 switch (cap->cap) {
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003648#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
Jay Zhou3c9bd402020-02-27 09:32:27 +08003649 case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2: {
3650 u64 allowed_options = KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE;
3651
3652 if (cap->args[0] & KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE)
3653 allowed_options = KVM_DIRTY_LOG_MANUAL_CAPS;
3654
3655 if (cap->flags || (cap->args[0] & ~allowed_options))
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003656 return -EINVAL;
3657 kvm->manual_dirty_log_protect = cap->args[0];
3658 return 0;
Jay Zhou3c9bd402020-02-27 09:32:27 +08003659 }
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003660#endif
David Matlackacd05782020-04-17 15:14:46 -07003661 case KVM_CAP_HALT_POLL: {
3662 if (cap->flags || cap->args[0] != (unsigned int)cap->args[0])
3663 return -EINVAL;
3664
3665 kvm->max_halt_poll_ns = cap->args[0];
3666 return 0;
3667 }
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003668 default:
3669 return kvm_vm_ioctl_enable_cap(kvm, cap);
3670 }
3671}
3672
Avi Kivitybccf2152007-02-21 18:04:26 +02003673static long kvm_vm_ioctl(struct file *filp,
3674 unsigned int ioctl, unsigned long arg)
3675{
3676 struct kvm *kvm = filp->private_data;
3677 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003678 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02003679
Avi Kivity6d4e4c42007-11-21 16:41:05 +02003680 if (kvm->mm != current->mm)
3681 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02003682 switch (ioctl) {
3683 case KVM_CREATE_VCPU:
3684 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02003685 break;
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003686 case KVM_ENABLE_CAP: {
3687 struct kvm_enable_cap cap;
3688
3689 r = -EFAULT;
3690 if (copy_from_user(&cap, argp, sizeof(cap)))
3691 goto out;
3692 r = kvm_vm_ioctl_enable_cap_generic(kvm, &cap);
3693 break;
3694 }
Izik Eidus6fc138d2007-10-09 19:20:39 +02003695 case KVM_SET_USER_MEMORY_REGION: {
3696 struct kvm_userspace_memory_region kvm_userspace_mem;
3697
3698 r = -EFAULT;
3699 if (copy_from_user(&kvm_userspace_mem, argp,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003700 sizeof(kvm_userspace_mem)))
Izik Eidus6fc138d2007-10-09 19:20:39 +02003701 goto out;
3702
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003703 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003704 break;
3705 }
3706 case KVM_GET_DIRTY_LOG: {
3707 struct kvm_dirty_log log;
3708
3709 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003710 if (copy_from_user(&log, argp, sizeof(log)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003711 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003712 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003713 break;
3714 }
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003715#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
3716 case KVM_CLEAR_DIRTY_LOG: {
3717 struct kvm_clear_dirty_log log;
3718
3719 r = -EFAULT;
3720 if (copy_from_user(&log, argp, sizeof(log)))
3721 goto out;
3722 r = kvm_vm_ioctl_clear_dirty_log(kvm, &log);
3723 break;
3724 }
3725#endif
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003726#ifdef CONFIG_KVM_MMIO
Laurent Vivier5f94c172008-05-30 16:05:54 +02003727 case KVM_REGISTER_COALESCED_MMIO: {
3728 struct kvm_coalesced_mmio_zone zone;
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08003729
Laurent Vivier5f94c172008-05-30 16:05:54 +02003730 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003731 if (copy_from_user(&zone, argp, sizeof(zone)))
Laurent Vivier5f94c172008-05-30 16:05:54 +02003732 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02003733 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
Laurent Vivier5f94c172008-05-30 16:05:54 +02003734 break;
3735 }
3736 case KVM_UNREGISTER_COALESCED_MMIO: {
3737 struct kvm_coalesced_mmio_zone zone;
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08003738
Laurent Vivier5f94c172008-05-30 16:05:54 +02003739 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003740 if (copy_from_user(&zone, argp, sizeof(zone)))
Laurent Vivier5f94c172008-05-30 16:05:54 +02003741 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02003742 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
Laurent Vivier5f94c172008-05-30 16:05:54 +02003743 break;
3744 }
3745#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04003746 case KVM_IRQFD: {
3747 struct kvm_irqfd data;
3748
3749 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003750 if (copy_from_user(&data, argp, sizeof(data)))
Gregory Haskins721eecbf2009-05-20 10:30:49 -04003751 goto out;
Alex Williamsond4db2932012-06-29 09:56:08 -06003752 r = kvm_irqfd(kvm, &data);
Gregory Haskins721eecbf2009-05-20 10:30:49 -04003753 break;
3754 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003755 case KVM_IOEVENTFD: {
3756 struct kvm_ioeventfd data;
3757
3758 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003759 if (copy_from_user(&data, argp, sizeof(data)))
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003760 goto out;
3761 r = kvm_ioeventfd(kvm, &data);
3762 break;
3763 }
Jan Kiszka07975ad2012-03-29 21:14:12 +02003764#ifdef CONFIG_HAVE_KVM_MSI
3765 case KVM_SIGNAL_MSI: {
3766 struct kvm_msi msi;
3767
3768 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003769 if (copy_from_user(&msi, argp, sizeof(msi)))
Jan Kiszka07975ad2012-03-29 21:14:12 +02003770 goto out;
3771 r = kvm_send_userspace_msi(kvm, &msi);
3772 break;
3773 }
3774#endif
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003775#ifdef __KVM_HAVE_IRQ_LINE
3776 case KVM_IRQ_LINE_STATUS:
3777 case KVM_IRQ_LINE: {
3778 struct kvm_irq_level irq_event;
3779
3780 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003781 if (copy_from_user(&irq_event, argp, sizeof(irq_event)))
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003782 goto out;
3783
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003784 r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
3785 ioctl == KVM_IRQ_LINE_STATUS);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003786 if (r)
3787 goto out;
3788
3789 r = -EFAULT;
3790 if (ioctl == KVM_IRQ_LINE_STATUS) {
Xiubo Li893bdbf2015-02-26 14:58:19 +08003791 if (copy_to_user(argp, &irq_event, sizeof(irq_event)))
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003792 goto out;
3793 }
3794
3795 r = 0;
3796 break;
3797 }
3798#endif
Alexander Grafaa8d5942013-04-15 21:12:53 +02003799#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
3800 case KVM_SET_GSI_ROUTING: {
3801 struct kvm_irq_routing routing;
3802 struct kvm_irq_routing __user *urouting;
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003803 struct kvm_irq_routing_entry *entries = NULL;
Alexander Grafaa8d5942013-04-15 21:12:53 +02003804
3805 r = -EFAULT;
3806 if (copy_from_user(&routing, argp, sizeof(routing)))
3807 goto out;
3808 r = -EINVAL;
David Hildenbrand5c0aea02017-04-28 17:06:20 +02003809 if (!kvm_arch_can_set_irq_routing(kvm))
3810 goto out;
Xiubo Licaf1ff22016-06-15 18:00:33 +08003811 if (routing.nr > KVM_MAX_IRQ_ROUTES)
Alexander Grafaa8d5942013-04-15 21:12:53 +02003812 goto out;
3813 if (routing.flags)
3814 goto out;
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003815 if (routing.nr) {
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003816 urouting = argp;
Denis Efremov7ec28e22020-06-03 13:11:31 +03003817 entries = vmemdup_user(urouting->entries,
3818 array_size(sizeof(*entries),
3819 routing.nr));
3820 if (IS_ERR(entries)) {
3821 r = PTR_ERR(entries);
3822 goto out;
3823 }
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003824 }
Alexander Grafaa8d5942013-04-15 21:12:53 +02003825 r = kvm_set_irq_routing(kvm, entries, routing.nr,
3826 routing.flags);
Denis Efremov7ec28e22020-06-03 13:11:31 +03003827 kvfree(entries);
Alexander Grafaa8d5942013-04-15 21:12:53 +02003828 break;
3829 }
3830#endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
Scott Wood852b6d52013-04-12 14:08:42 +00003831 case KVM_CREATE_DEVICE: {
3832 struct kvm_create_device cd;
3833
3834 r = -EFAULT;
3835 if (copy_from_user(&cd, argp, sizeof(cd)))
3836 goto out;
3837
3838 r = kvm_ioctl_create_device(kvm, &cd);
3839 if (r)
3840 goto out;
3841
3842 r = -EFAULT;
3843 if (copy_to_user(argp, &cd, sizeof(cd)))
3844 goto out;
3845
3846 r = 0;
3847 break;
3848 }
Alexander Graf92b591a2014-07-14 18:33:08 +02003849 case KVM_CHECK_EXTENSION:
3850 r = kvm_vm_ioctl_check_extension_generic(kvm, arg);
3851 break;
Avi Kivityf17abe92007-02-21 19:28:04 +02003852 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01003853 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02003854 }
3855out:
3856 return r;
3857}
3858
Christian Borntraegerde8e5d72015-02-03 09:35:15 +01003859#ifdef CONFIG_KVM_COMPAT
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003860struct compat_kvm_dirty_log {
3861 __u32 slot;
3862 __u32 padding1;
3863 union {
3864 compat_uptr_t dirty_bitmap; /* one bit per page */
3865 __u64 padding2;
3866 };
3867};
3868
3869static long kvm_vm_compat_ioctl(struct file *filp,
3870 unsigned int ioctl, unsigned long arg)
3871{
3872 struct kvm *kvm = filp->private_data;
3873 int r;
3874
3875 if (kvm->mm != current->mm)
3876 return -EIO;
3877 switch (ioctl) {
3878 case KVM_GET_DIRTY_LOG: {
3879 struct compat_kvm_dirty_log compat_log;
3880 struct kvm_dirty_log log;
3881
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003882 if (copy_from_user(&compat_log, (void __user *)arg,
3883 sizeof(compat_log)))
Markus Elfringf6a3b162017-01-22 11:30:21 +01003884 return -EFAULT;
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003885 log.slot = compat_log.slot;
3886 log.padding1 = compat_log.padding1;
3887 log.padding2 = compat_log.padding2;
3888 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
3889
3890 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003891 break;
3892 }
3893 default:
3894 r = kvm_vm_ioctl(filp, ioctl, arg);
3895 }
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003896 return r;
3897}
3898#endif
3899
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01003900static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02003901 .release = kvm_vm_release,
3902 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003903 .llseek = noop_llseek,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01003904 KVM_COMPAT(kvm_vm_compat_ioctl),
Avi Kivityf17abe92007-02-21 19:28:04 +02003905};
3906
Carsten Ottee08b9632012-01-04 10:25:20 +01003907static int kvm_dev_ioctl_create_vm(unsigned long type)
Avi Kivityf17abe92007-02-21 19:28:04 +02003908{
Heiko Carstensaac87632010-10-27 17:22:10 +02003909 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003910 struct kvm *kvm;
Al Viro506cfba2016-07-14 18:54:17 +02003911 struct file *file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003912
Carsten Ottee08b9632012-01-04 10:25:20 +01003913 kvm = kvm_create_vm(type);
Avi Kivityd6d28162007-06-28 08:38:16 -04003914 if (IS_ERR(kvm))
3915 return PTR_ERR(kvm);
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003916#ifdef CONFIG_KVM_MMIO
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09003917 r = kvm_coalesced_mmio_init(kvm);
Markus Elfring78588332017-11-21 13:40:17 +01003918 if (r < 0)
3919 goto put_kvm;
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09003920#endif
Al Viro506cfba2016-07-14 18:54:17 +02003921 r = get_unused_fd_flags(O_CLOEXEC);
Markus Elfring78588332017-11-21 13:40:17 +01003922 if (r < 0)
3923 goto put_kvm;
3924
Al Viro506cfba2016-07-14 18:54:17 +02003925 file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
3926 if (IS_ERR(file)) {
3927 put_unused_fd(r);
Markus Elfring78588332017-11-21 13:40:17 +01003928 r = PTR_ERR(file);
3929 goto put_kvm;
Al Viro506cfba2016-07-14 18:54:17 +02003930 }
Janosch Frank536a6f82016-05-18 13:26:23 +02003931
Paolo Bonzini525df862017-06-27 15:45:09 +02003932 /*
3933 * Don't call kvm_put_kvm anymore at this point; file->f_op is
3934 * already set, with ->release() being kvm_vm_release(). In error
3935 * cases it will be called by the final fput(file) and will take
3936 * care of doing kvm_put_kvm(kvm).
3937 */
Janosch Frank536a6f82016-05-18 13:26:23 +02003938 if (kvm_create_vm_debugfs(kvm, r) < 0) {
Al Viro506cfba2016-07-14 18:54:17 +02003939 put_unused_fd(r);
3940 fput(file);
Janosch Frank536a6f82016-05-18 13:26:23 +02003941 return -ENOMEM;
3942 }
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02003943 kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02003944
Al Viro506cfba2016-07-14 18:54:17 +02003945 fd_install(r, file);
Heiko Carstensaac87632010-10-27 17:22:10 +02003946 return r;
Markus Elfring78588332017-11-21 13:40:17 +01003947
3948put_kvm:
3949 kvm_put_kvm(kvm);
3950 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003951}
3952
3953static long kvm_dev_ioctl(struct file *filp,
3954 unsigned int ioctl, unsigned long arg)
3955{
Avi Kivity07c45a32007-03-07 13:05:38 +02003956 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003957
3958 switch (ioctl) {
3959 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003960 if (arg)
3961 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003962 r = KVM_API_VERSION;
3963 break;
3964 case KVM_CREATE_VM:
Carsten Ottee08b9632012-01-04 10:25:20 +01003965 r = kvm_dev_ioctl_create_vm(arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02003966 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003967 case KVM_CHECK_EXTENSION:
Alexander Graf784aa3d2014-07-14 18:27:35 +02003968 r = kvm_vm_ioctl_check_extension_generic(NULL, arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02003969 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02003970 case KVM_GET_VCPU_MMAP_SIZE:
Avi Kivity07c45a32007-03-07 13:05:38 +02003971 if (arg)
3972 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02003973 r = PAGE_SIZE; /* struct kvm_run */
3974#ifdef CONFIG_X86
3975 r += PAGE_SIZE; /* pio data page */
3976#endif
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003977#ifdef CONFIG_KVM_MMIO
Laurent Vivier5f94c172008-05-30 16:05:54 +02003978 r += PAGE_SIZE; /* coalesced mmio ring page */
3979#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02003980 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04003981 case KVM_TRACE_ENABLE:
3982 case KVM_TRACE_PAUSE:
3983 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03003984 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04003985 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003987 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003988 }
3989out:
3990 return r;
3991}
3992
Avi Kivity6aa8b732006-12-10 02:21:36 -08003993static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994 .unlocked_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003995 .llseek = noop_llseek,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01003996 KVM_COMPAT(kvm_dev_ioctl),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997};
3998
3999static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02004000 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004001 "kvm",
4002 &kvm_chardev_ops,
4003};
4004
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004005static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03004006{
4007 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02004008 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03004009
Rusty Russell7f59f492008-12-07 21:25:45 +10304010 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03004011 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02004012
Rusty Russell7f59f492008-12-07 21:25:45 +10304013 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02004014
Radim Krčmář13a34e02014-08-28 15:13:03 +02004015 r = kvm_arch_hardware_enable();
Alexander Graf10474ae2009-09-15 11:37:46 +02004016
4017 if (r) {
4018 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
4019 atomic_inc(&hardware_enable_failed);
Xiubo Li1170adc2015-02-26 14:58:26 +08004020 pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu);
Alexander Graf10474ae2009-09-15 11:37:46 +02004021 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03004022}
4023
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004024static int kvm_starting_cpu(unsigned int cpu)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004025{
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004026 raw_spin_lock(&kvm_count_lock);
Paolo Bonzini4fa92fb2013-09-10 12:57:17 +02004027 if (kvm_usage_count)
4028 hardware_enable_nolock(NULL);
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004029 raw_spin_unlock(&kvm_count_lock);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004030 return 0;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004031}
4032
4033static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03004034{
4035 int cpu = raw_smp_processor_id();
4036
Rusty Russell7f59f492008-12-07 21:25:45 +10304037 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03004038 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10304039 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Radim Krčmář13a34e02014-08-28 15:13:03 +02004040 kvm_arch_hardware_disable();
Avi Kivity1b6c0162007-05-24 13:03:52 +03004041}
4042
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004043static int kvm_dying_cpu(unsigned int cpu)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004044{
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004045 raw_spin_lock(&kvm_count_lock);
Paolo Bonzini4fa92fb2013-09-10 12:57:17 +02004046 if (kvm_usage_count)
4047 hardware_disable_nolock(NULL);
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004048 raw_spin_unlock(&kvm_count_lock);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004049 return 0;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004050}
4051
Alexander Graf10474ae2009-09-15 11:37:46 +02004052static void hardware_disable_all_nolock(void)
4053{
4054 BUG_ON(!kvm_usage_count);
4055
4056 kvm_usage_count--;
4057 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004058 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02004059}
4060
4061static void hardware_disable_all(void)
4062{
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004063 raw_spin_lock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02004064 hardware_disable_all_nolock();
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004065 raw_spin_unlock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02004066}
4067
4068static int hardware_enable_all(void)
4069{
4070 int r = 0;
4071
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004072 raw_spin_lock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02004073
4074 kvm_usage_count++;
4075 if (kvm_usage_count == 1) {
4076 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004077 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02004078
4079 if (atomic_read(&hardware_enable_failed)) {
4080 hardware_disable_all_nolock();
4081 r = -EBUSY;
4082 }
4083 }
4084
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004085 raw_spin_unlock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02004086
4087 return r;
4088}
4089
Rusty Russell9a2b85c2007-07-17 23:17:55 +10004090static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04004091 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10004092{
Sheng Yang8e1c1812009-04-29 11:09:04 +08004093 /*
4094 * Some (well, at least mine) BIOSes hang on reboot if
4095 * in vmx root mode.
4096 *
4097 * And Intel TXT required VMX off for all cpu when system shutdown.
4098 */
Xiubo Li1170adc2015-02-26 14:58:26 +08004099 pr_info("kvm: exiting hardware virtualization\n");
Sheng Yang8e1c1812009-04-29 11:09:04 +08004100 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004101 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10004102 return NOTIFY_OK;
4103}
4104
4105static struct notifier_block kvm_reboot_notifier = {
4106 .notifier_call = kvm_reboot,
4107 .priority = 0,
4108};
4109
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004110static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004111{
4112 int i;
4113
4114 for (i = 0; i < bus->dev_count; i++) {
Sasha Levin743eeb02011-07-27 16:00:48 +03004115 struct kvm_io_device *pos = bus->range[i].dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004116
4117 kvm_iodevice_destructor(pos);
4118 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004119 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004120}
4121
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004122static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1,
Xiubo Li20e87b72015-02-26 14:58:25 +08004123 const struct kvm_io_range *r2)
Sasha Levin743eeb02011-07-27 16:00:48 +03004124{
Jason Wang8f4216c72015-09-15 14:41:57 +08004125 gpa_t addr1 = r1->addr;
4126 gpa_t addr2 = r2->addr;
4127
4128 if (addr1 < addr2)
Sasha Levin743eeb02011-07-27 16:00:48 +03004129 return -1;
Jason Wang8f4216c72015-09-15 14:41:57 +08004130
4131 /* If r2->len == 0, match the exact address. If r2->len != 0,
4132 * accept any overlapping write. Any order is acceptable for
4133 * overlapping ranges, because kvm_io_bus_get_first_dev ensures
4134 * we process all of them.
4135 */
4136 if (r2->len) {
4137 addr1 += r1->len;
4138 addr2 += r2->len;
4139 }
4140
4141 if (addr1 > addr2)
Sasha Levin743eeb02011-07-27 16:00:48 +03004142 return 1;
Jason Wang8f4216c72015-09-15 14:41:57 +08004143
Sasha Levin743eeb02011-07-27 16:00:48 +03004144 return 0;
4145}
4146
Paolo Bonzinia343c9b2013-07-16 13:03:29 +02004147static int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
4148{
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004149 return kvm_io_bus_cmp(p1, p2);
Paolo Bonzinia343c9b2013-07-16 13:03:29 +02004150}
4151
Geoff Levand39369f72013-04-05 19:20:30 +00004152static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
Sasha Levin743eeb02011-07-27 16:00:48 +03004153 gpa_t addr, int len)
4154{
4155 struct kvm_io_range *range, key;
4156 int off;
4157
4158 key = (struct kvm_io_range) {
4159 .addr = addr,
4160 .len = len,
4161 };
4162
4163 range = bsearch(&key, bus->range, bus->dev_count,
4164 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
4165 if (range == NULL)
4166 return -ENOENT;
4167
4168 off = range - bus->range;
4169
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004170 while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0)
Sasha Levin743eeb02011-07-27 16:00:48 +03004171 off--;
4172
4173 return off;
4174}
4175
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004176static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004177 struct kvm_io_range *range, const void *val)
4178{
4179 int idx;
4180
4181 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
4182 if (idx < 0)
4183 return -EOPNOTSUPP;
4184
4185 while (idx < bus->dev_count &&
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004186 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004187 if (!kvm_iodevice_write(vcpu, bus->range[idx].dev, range->addr,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004188 range->len, val))
4189 return idx;
4190 idx++;
4191 }
4192
4193 return -EOPNOTSUPP;
4194}
4195
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004196/* kvm_io_bus_write - called under kvm->slots_lock */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004197int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004198 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004199{
Cornelia Huck126a5af2013-07-03 16:30:53 +02004200 struct kvm_io_bus *bus;
4201 struct kvm_io_range range;
4202 int r;
4203
4204 range = (struct kvm_io_range) {
4205 .addr = addr,
4206 .len = len,
4207 };
4208
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004209 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004210 if (!bus)
4211 return -ENOMEM;
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004212 r = __kvm_io_bus_write(vcpu, bus, &range, val);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004213 return r < 0 ? r : 0;
4214}
Leo Yana2420102019-02-22 16:10:09 +08004215EXPORT_SYMBOL_GPL(kvm_io_bus_write);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004216
4217/* kvm_io_bus_write_cookie - called under kvm->slots_lock */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004218int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
4219 gpa_t addr, int len, const void *val, long cookie)
Cornelia Huck126a5af2013-07-03 16:30:53 +02004220{
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08004221 struct kvm_io_bus *bus;
Sasha Levin743eeb02011-07-27 16:00:48 +03004222 struct kvm_io_range range;
4223
4224 range = (struct kvm_io_range) {
4225 .addr = addr,
4226 .len = len,
4227 };
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08004228
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004229 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004230 if (!bus)
4231 return -ENOMEM;
Cornelia Huck126a5af2013-07-03 16:30:53 +02004232
4233 /* First try the device referenced by cookie. */
4234 if ((cookie >= 0) && (cookie < bus->dev_count) &&
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004235 (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004236 if (!kvm_iodevice_write(vcpu, bus->range[cookie].dev, addr, len,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004237 val))
4238 return cookie;
4239
4240 /*
4241 * cookie contained garbage; fall back to search and return the
4242 * correct cookie value.
4243 */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004244 return __kvm_io_bus_write(vcpu, bus, &range, val);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004245}
4246
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004247static int __kvm_io_bus_read(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
4248 struct kvm_io_range *range, void *val)
Cornelia Huck126a5af2013-07-03 16:30:53 +02004249{
4250 int idx;
4251
4252 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
Sasha Levin743eeb02011-07-27 16:00:48 +03004253 if (idx < 0)
4254 return -EOPNOTSUPP;
4255
4256 while (idx < bus->dev_count &&
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004257 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004258 if (!kvm_iodevice_read(vcpu, bus->range[idx].dev, range->addr,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004259 range->len, val))
4260 return idx;
Sasha Levin743eeb02011-07-27 16:00:48 +03004261 idx++;
4262 }
4263
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004264 return -EOPNOTSUPP;
4265}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004266
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004267/* kvm_io_bus_read - called under kvm->slots_lock */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004268int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004269 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004270{
Cornelia Huck126a5af2013-07-03 16:30:53 +02004271 struct kvm_io_bus *bus;
4272 struct kvm_io_range range;
4273 int r;
4274
4275 range = (struct kvm_io_range) {
4276 .addr = addr,
4277 .len = len,
4278 };
4279
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004280 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004281 if (!bus)
4282 return -ENOMEM;
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004283 r = __kvm_io_bus_read(vcpu, bus, &range, val);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004284 return r < 0 ? r : 0;
4285}
4286
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004287/* Caller must hold slots_lock. */
Sasha Levin743eeb02011-07-27 16:00:48 +03004288int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
4289 int len, struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03004290{
Gal Hammerd4c67a72018-01-16 15:34:41 +02004291 int i;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004292 struct kvm_io_bus *new_bus, *bus;
Gal Hammerd4c67a72018-01-16 15:34:41 +02004293 struct kvm_io_range range;
Gregory Haskins090b7af2009-07-07 17:08:44 -04004294
Christian Borntraeger4a12f952017-07-07 10:51:38 +02004295 bus = kvm_get_bus(kvm, bus_idx);
David Hildenbrand90db1042017-03-23 18:24:19 +01004296 if (!bus)
4297 return -ENOMEM;
4298
Amos Kong6ea34c92013-05-25 06:44:15 +08004299 /* exclude ioeventfd which is limited by maximum fd */
4300 if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
Gregory Haskins090b7af2009-07-07 17:08:44 -04004301 return -ENOSPC;
4302
Gustavo A. R. Silva90952cd2019-01-30 17:07:47 +01004303 new_bus = kmalloc(struct_size(bus, range, bus->dev_count + 1),
Ben Gardonb12ce362019-02-11 11:02:49 -08004304 GFP_KERNEL_ACCOUNT);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004305 if (!new_bus)
4306 return -ENOMEM;
Gal Hammerd4c67a72018-01-16 15:34:41 +02004307
4308 range = (struct kvm_io_range) {
4309 .addr = addr,
4310 .len = len,
4311 .dev = dev,
4312 };
4313
4314 for (i = 0; i < bus->dev_count; i++)
4315 if (kvm_io_bus_cmp(&bus->range[i], &range) > 0)
4316 break;
4317
4318 memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
4319 new_bus->dev_count++;
4320 new_bus->range[i] = range;
4321 memcpy(new_bus->range + i + 1, bus->range + i,
4322 (bus->dev_count - i) * sizeof(struct kvm_io_range));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004323 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
4324 synchronize_srcu_expedited(&kvm->srcu);
4325 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04004326
4327 return 0;
4328}
4329
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004330/* Caller must hold slots_lock. */
David Hildenbrand90db1042017-03-23 18:24:19 +01004331void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4332 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04004333{
David Hildenbrand90db1042017-03-23 18:24:19 +01004334 int i;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004335 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03004336
Christian Borntraeger4a12f952017-07-07 10:51:38 +02004337 bus = kvm_get_bus(kvm, bus_idx);
Peter Xudf630b82017-03-15 16:01:17 +08004338 if (!bus)
David Hildenbrand90db1042017-03-23 18:24:19 +01004339 return;
Peter Xudf630b82017-03-15 16:01:17 +08004340
Amos Konga13007162012-03-09 12:17:32 +08004341 for (i = 0; i < bus->dev_count; i++)
4342 if (bus->range[i].dev == dev) {
Gregory Haskins090b7af2009-07-07 17:08:44 -04004343 break;
4344 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004345
David Hildenbrand90db1042017-03-23 18:24:19 +01004346 if (i == bus->dev_count)
4347 return;
Amos Konga13007162012-03-09 12:17:32 +08004348
Gustavo A. R. Silva90952cd2019-01-30 17:07:47 +01004349 new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1),
Ben Gardonb12ce362019-02-11 11:02:49 -08004350 GFP_KERNEL_ACCOUNT);
David Hildenbrand90db1042017-03-23 18:24:19 +01004351 if (!new_bus) {
4352 pr_err("kvm: failed to shrink bus, removing it completely\n");
4353 goto broken;
4354 }
Amos Konga13007162012-03-09 12:17:32 +08004355
4356 memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
4357 new_bus->dev_count--;
4358 memcpy(new_bus->range + i, bus->range + i + 1,
4359 (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004360
David Hildenbrand90db1042017-03-23 18:24:19 +01004361broken:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004362 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
4363 synchronize_srcu_expedited(&kvm->srcu);
4364 kfree(bus);
David Hildenbrand90db1042017-03-23 18:24:19 +01004365 return;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004366}
4367
Andre Przywara8a39d002016-07-15 12:43:26 +01004368struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4369 gpa_t addr)
4370{
4371 struct kvm_io_bus *bus;
4372 int dev_idx, srcu_idx;
4373 struct kvm_io_device *iodev = NULL;
4374
4375 srcu_idx = srcu_read_lock(&kvm->srcu);
4376
4377 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004378 if (!bus)
4379 goto out_unlock;
Andre Przywara8a39d002016-07-15 12:43:26 +01004380
4381 dev_idx = kvm_io_bus_get_first_dev(bus, addr, 1);
4382 if (dev_idx < 0)
4383 goto out_unlock;
4384
4385 iodev = bus->range[dev_idx].dev;
4386
4387out_unlock:
4388 srcu_read_unlock(&kvm->srcu, srcu_idx);
4389
4390 return iodev;
4391}
4392EXPORT_SYMBOL_GPL(kvm_io_bus_get_dev);
4393
Janosch Frank536a6f82016-05-18 13:26:23 +02004394static int kvm_debugfs_open(struct inode *inode, struct file *file,
4395 int (*get)(void *, u64 *), int (*set)(void *, u64),
4396 const char *fmt)
4397{
4398 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
4399 inode->i_private;
4400
4401 /* The debugfs files are a reference to the kvm struct which
4402 * is still valid when kvm_destroy_vm is called.
4403 * To avoid the race between open and the removal of the debugfs
4404 * directory we test against the users count.
4405 */
Elena Reshetovae3736c32017-02-20 13:06:21 +02004406 if (!refcount_inc_not_zero(&stat_data->kvm->users_count))
Janosch Frank536a6f82016-05-18 13:26:23 +02004407 return -ENOENT;
4408
Paolo Bonzini833b45d2019-09-30 18:48:44 +02004409 if (simple_attr_open(inode, file, get,
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004410 KVM_DBGFS_GET_MODE(stat_data->dbgfs_item) & 0222
4411 ? set : NULL,
4412 fmt)) {
Janosch Frank536a6f82016-05-18 13:26:23 +02004413 kvm_put_kvm(stat_data->kvm);
4414 return -ENOMEM;
4415 }
4416
4417 return 0;
4418}
4419
4420static int kvm_debugfs_release(struct inode *inode, struct file *file)
4421{
4422 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
4423 inode->i_private;
4424
4425 simple_attr_release(inode, file);
4426 kvm_put_kvm(stat_data->kvm);
4427
4428 return 0;
4429}
4430
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004431static int kvm_get_stat_per_vm(struct kvm *kvm, size_t offset, u64 *val)
Janosch Frank536a6f82016-05-18 13:26:23 +02004432{
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004433 *val = *(ulong *)((void *)kvm + offset);
Janosch Frank536a6f82016-05-18 13:26:23 +02004434
4435 return 0;
4436}
4437
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004438static int kvm_clear_stat_per_vm(struct kvm *kvm, size_t offset)
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004439{
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004440 *(ulong *)((void *)kvm + offset) = 0;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004441
4442 return 0;
4443}
4444
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004445static int kvm_get_stat_per_vcpu(struct kvm *kvm, size_t offset, u64 *val)
Janosch Frank536a6f82016-05-18 13:26:23 +02004446{
4447 int i;
Janosch Frank536a6f82016-05-18 13:26:23 +02004448 struct kvm_vcpu *vcpu;
4449
4450 *val = 0;
4451
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004452 kvm_for_each_vcpu(i, vcpu, kvm)
4453 *val += *(u64 *)((void *)vcpu + offset);
Janosch Frank536a6f82016-05-18 13:26:23 +02004454
4455 return 0;
4456}
4457
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004458static int kvm_clear_stat_per_vcpu(struct kvm *kvm, size_t offset)
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004459{
4460 int i;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004461 struct kvm_vcpu *vcpu;
4462
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004463 kvm_for_each_vcpu(i, vcpu, kvm)
4464 *(u64 *)((void *)vcpu + offset) = 0;
4465
4466 return 0;
4467}
4468
4469static int kvm_stat_data_get(void *data, u64 *val)
4470{
4471 int r = -EFAULT;
4472 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4473
4474 switch (stat_data->dbgfs_item->kind) {
4475 case KVM_STAT_VM:
4476 r = kvm_get_stat_per_vm(stat_data->kvm,
4477 stat_data->dbgfs_item->offset, val);
4478 break;
4479 case KVM_STAT_VCPU:
4480 r = kvm_get_stat_per_vcpu(stat_data->kvm,
4481 stat_data->dbgfs_item->offset, val);
4482 break;
4483 }
4484
4485 return r;
4486}
4487
4488static int kvm_stat_data_clear(void *data, u64 val)
4489{
4490 int r = -EFAULT;
4491 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4492
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004493 if (val)
4494 return -EINVAL;
4495
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004496 switch (stat_data->dbgfs_item->kind) {
4497 case KVM_STAT_VM:
4498 r = kvm_clear_stat_per_vm(stat_data->kvm,
4499 stat_data->dbgfs_item->offset);
4500 break;
4501 case KVM_STAT_VCPU:
4502 r = kvm_clear_stat_per_vcpu(stat_data->kvm,
4503 stat_data->dbgfs_item->offset);
4504 break;
4505 }
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004506
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004507 return r;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004508}
4509
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004510static int kvm_stat_data_open(struct inode *inode, struct file *file)
Janosch Frank536a6f82016-05-18 13:26:23 +02004511{
4512 __simple_attr_check_format("%llu\n", 0ull);
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004513 return kvm_debugfs_open(inode, file, kvm_stat_data_get,
4514 kvm_stat_data_clear, "%llu\n");
Janosch Frank536a6f82016-05-18 13:26:23 +02004515}
4516
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004517static const struct file_operations stat_fops_per_vm = {
4518 .owner = THIS_MODULE,
4519 .open = kvm_stat_data_open,
Janosch Frank536a6f82016-05-18 13:26:23 +02004520 .release = kvm_debugfs_release,
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004521 .read = simple_attr_read,
4522 .write = simple_attr_write,
4523 .llseek = no_llseek,
Janosch Frank536a6f82016-05-18 13:26:23 +02004524};
4525
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004526static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02004527{
4528 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02004529 struct kvm *kvm;
Janosch Frank536a6f82016-05-18 13:26:23 +02004530 u64 tmp_val;
Avi Kivityba1389b2007-11-18 16:24:12 +02004531
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004532 *val = 0;
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004533 mutex_lock(&kvm_lock);
Janosch Frank536a6f82016-05-18 13:26:23 +02004534 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004535 kvm_get_stat_per_vm(kvm, offset, &tmp_val);
Janosch Frank536a6f82016-05-18 13:26:23 +02004536 *val += tmp_val;
4537 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004538 mutex_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004539 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02004540}
4541
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004542static int vm_stat_clear(void *_offset, u64 val)
4543{
4544 unsigned offset = (long)_offset;
4545 struct kvm *kvm;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004546
4547 if (val)
4548 return -EINVAL;
4549
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004550 mutex_lock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004551 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004552 kvm_clear_stat_per_vm(kvm, offset);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004553 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004554 mutex_unlock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004555
4556 return 0;
4557}
4558
4559DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n");
Avi Kivityba1389b2007-11-18 16:24:12 +02004560
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004561static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03004562{
4563 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03004564 struct kvm *kvm;
Janosch Frank536a6f82016-05-18 13:26:23 +02004565 u64 tmp_val;
Avi Kivity1165f5f2007-04-19 17:27:43 +03004566
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004567 *val = 0;
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004568 mutex_lock(&kvm_lock);
Janosch Frank536a6f82016-05-18 13:26:23 +02004569 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004570 kvm_get_stat_per_vcpu(kvm, offset, &tmp_val);
Janosch Frank536a6f82016-05-18 13:26:23 +02004571 *val += tmp_val;
4572 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004573 mutex_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004574 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03004575}
4576
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004577static int vcpu_stat_clear(void *_offset, u64 val)
4578{
4579 unsigned offset = (long)_offset;
4580 struct kvm *kvm;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004581
4582 if (val)
4583 return -EINVAL;
4584
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004585 mutex_lock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004586 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004587 kvm_clear_stat_per_vcpu(kvm, offset);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004588 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004589 mutex_unlock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004590
4591 return 0;
4592}
4593
4594DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear,
4595 "%llu\n");
Avi Kivityba1389b2007-11-18 16:24:12 +02004596
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004597static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02004598 [KVM_STAT_VCPU] = &vcpu_stat_fops,
4599 [KVM_STAT_VM] = &vm_stat_fops,
4600};
Avi Kivity1165f5f2007-04-19 17:27:43 +03004601
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004602static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
4603{
4604 struct kobj_uevent_env *env;
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004605 unsigned long long created, active;
4606
4607 if (!kvm_dev.this_device || !kvm)
4608 return;
4609
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004610 mutex_lock(&kvm_lock);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004611 if (type == KVM_EVENT_CREATE_VM) {
4612 kvm_createvm_count++;
4613 kvm_active_vms++;
4614 } else if (type == KVM_EVENT_DESTROY_VM) {
4615 kvm_active_vms--;
4616 }
4617 created = kvm_createvm_count;
4618 active = kvm_active_vms;
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004619 mutex_unlock(&kvm_lock);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004620
Ben Gardonb12ce362019-02-11 11:02:49 -08004621 env = kzalloc(sizeof(*env), GFP_KERNEL_ACCOUNT);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004622 if (!env)
4623 return;
4624
4625 add_uevent_var(env, "CREATED=%llu", created);
4626 add_uevent_var(env, "COUNT=%llu", active);
4627
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004628 if (type == KVM_EVENT_CREATE_VM) {
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004629 add_uevent_var(env, "EVENT=create");
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004630 kvm->userspace_pid = task_pid_nr(current);
4631 } else if (type == KVM_EVENT_DESTROY_VM) {
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004632 add_uevent_var(env, "EVENT=destroy");
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004633 }
4634 add_uevent_var(env, "PID=%d", kvm->userspace_pid);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004635
Greg Kroah-Hartman8ed05792019-02-28 16:34:37 +01004636 if (!IS_ERR_OR_NULL(kvm->debugfs_dentry)) {
Ben Gardonb12ce362019-02-11 11:02:49 -08004637 char *tmp, *p = kmalloc(PATH_MAX, GFP_KERNEL_ACCOUNT);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004638
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004639 if (p) {
4640 tmp = dentry_path_raw(kvm->debugfs_dentry, p, PATH_MAX);
4641 if (!IS_ERR(tmp))
4642 add_uevent_var(env, "STATS_PATH=%s", tmp);
4643 kfree(p);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004644 }
4645 }
4646 /* no need for checks, since we are adding at most only 5 keys */
4647 env->envp[env->envp_idx++] = NULL;
4648 kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp);
4649 kfree(env);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004650}
4651
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02004652static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004653{
4654 struct kvm_stats_debugfs_item *p;
4655
Hollis Blanchard76f7c872008-04-15 16:05:42 -05004656 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Hamo4f69b682011-12-15 14:23:16 +08004657
Janosch Frank536a6f82016-05-18 13:26:23 +02004658 kvm_debugfs_num_entries = 0;
4659 for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004660 debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p),
4661 kvm_debugfs_dir, (void *)(long)p->offset,
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02004662 stat_fops[p->kind]);
Hamo4f69b682011-12-15 14:23:16 +08004663 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004664}
4665
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004666static int kvm_suspend(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08004667{
Alexander Graf10474ae2009-09-15 11:37:46 +02004668 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004669 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08004670 return 0;
4671}
4672
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004673static void kvm_resume(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08004674{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10004675 if (kvm_usage_count) {
Wanpeng Li2eb06c32019-05-17 16:49:49 +08004676#ifdef CONFIG_LOCKDEP
4677 WARN_ON(lockdep_is_held(&kvm_count_lock));
4678#endif
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004679 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10004680 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08004681}
4682
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004683static struct syscore_ops kvm_syscore_ops = {
Avi Kivity59ae6c62007-02-12 00:54:48 -08004684 .suspend = kvm_suspend,
4685 .resume = kvm_resume,
4686};
4687
Avi Kivity15ad7142007-07-11 18:17:21 +03004688static inline
4689struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
4690{
4691 return container_of(pn, struct kvm_vcpu, preempt_notifier);
4692}
4693
4694static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
4695{
4696 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08004697
Wanpeng Li046ddee2019-08-01 11:30:14 +08004698 WRITE_ONCE(vcpu->preempted, false);
Wanpeng Lid73eb572019-07-18 19:39:06 +08004699 WRITE_ONCE(vcpu->ready, false);
Avi Kivity15ad7142007-07-11 18:17:21 +03004700
Paolo Bonzini7495e222020-01-09 09:57:19 -05004701 __this_cpu_write(kvm_running_vcpu, vcpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02004702 kvm_arch_sched_in(vcpu, cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004703 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03004704}
4705
4706static void kvm_sched_out(struct preempt_notifier *pn,
4707 struct task_struct *next)
4708{
4709 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
4710
Wanpeng Lid73eb572019-07-18 19:39:06 +08004711 if (current->state == TASK_RUNNING) {
Wanpeng Li046ddee2019-08-01 11:30:14 +08004712 WRITE_ONCE(vcpu->preempted, true);
Wanpeng Lid73eb572019-07-18 19:39:06 +08004713 WRITE_ONCE(vcpu->ready, true);
4714 }
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004715 kvm_arch_vcpu_put(vcpu);
Paolo Bonzini7495e222020-01-09 09:57:19 -05004716 __this_cpu_write(kvm_running_vcpu, NULL);
4717}
4718
4719/**
4720 * kvm_get_running_vcpu - get the vcpu running on the current CPU.
Marc Zyngier1f03b2b2020-02-07 16:34:10 +00004721 *
4722 * We can disable preemption locally around accessing the per-CPU variable,
4723 * and use the resolved vcpu pointer after enabling preemption again,
4724 * because even if the current thread is migrated to another CPU, reading
4725 * the per-CPU value later will give us the same value as we update the
4726 * per-CPU variable in the preempt notifier handlers.
Paolo Bonzini7495e222020-01-09 09:57:19 -05004727 */
4728struct kvm_vcpu *kvm_get_running_vcpu(void)
4729{
Marc Zyngier1f03b2b2020-02-07 16:34:10 +00004730 struct kvm_vcpu *vcpu;
4731
4732 preempt_disable();
4733 vcpu = __this_cpu_read(kvm_running_vcpu);
4734 preempt_enable();
4735
4736 return vcpu;
Paolo Bonzini7495e222020-01-09 09:57:19 -05004737}
Wanpeng Li379a3c82020-04-28 14:23:27 +08004738EXPORT_SYMBOL_GPL(kvm_get_running_vcpu);
Paolo Bonzini7495e222020-01-09 09:57:19 -05004739
4740/**
4741 * kvm_get_running_vcpus - get the per-CPU array of currently running vcpus.
4742 */
4743struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
4744{
4745 return &kvm_running_vcpu;
Avi Kivity15ad7142007-07-11 18:17:21 +03004746}
4747
Sean Christophersonb9904082020-03-21 13:25:55 -07004748struct kvm_cpu_compat_check {
4749 void *opaque;
4750 int *ret;
4751};
4752
4753static void check_processor_compat(void *data)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004754{
Sean Christophersonb9904082020-03-21 13:25:55 -07004755 struct kvm_cpu_compat_check *c = data;
4756
4757 *c->ret = kvm_arch_check_processor_compat(c->opaque);
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004758}
4759
Avi Kivity0ee75be2010-04-28 15:39:01 +03004760int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10004761 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004762{
Sean Christophersonb9904082020-03-21 13:25:55 -07004763 struct kvm_cpu_compat_check c;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004764 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004765 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004766
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004767 r = kvm_arch_init(opaque);
4768 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08004769 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004770
Asias He7dac16c2013-05-08 10:57:29 +08004771 /*
4772 * kvm_arch_init makes sure there's at most one caller
4773 * for architectures that support multiple implementations,
4774 * like intel and amd on x86.
Paolo Bonzini36343f62016-10-26 13:35:56 +02004775 * kvm_arch_init must be called before kvm_irqfd_init to avoid creating
4776 * conflicts in case kvm is already setup for another implementation.
Asias He7dac16c2013-05-08 10:57:29 +08004777 */
Paolo Bonzini36343f62016-10-26 13:35:56 +02004778 r = kvm_irqfd_init();
4779 if (r)
4780 goto out_irqfd;
Asias He7dac16c2013-05-08 10:57:29 +08004781
Avi Kivity8437a6172009-06-06 14:52:35 -07004782 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10304783 r = -ENOMEM;
4784 goto out_free_0;
4785 }
4786
Sean Christophersonb9904082020-03-21 13:25:55 -07004787 r = kvm_arch_hardware_setup(opaque);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788 if (r < 0)
Miaohe Linfaf0be22019-11-23 10:45:50 +08004789 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790
Sean Christophersonb9904082020-03-21 13:25:55 -07004791 c.ret = &r;
4792 c.opaque = opaque;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004793 for_each_online_cpu(cpu) {
Sean Christophersonb9904082020-03-21 13:25:55 -07004794 smp_call_function_single(cpu, check_processor_compat, &c, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03004795 if (r < 0)
Miaohe Linfaf0be22019-11-23 10:45:50 +08004796 goto out_free_2;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004797 }
4798
Thomas Gleixner73c1b412016-12-21 20:19:54 +01004799 r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "kvm/cpu:starting",
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004800 kvm_starting_cpu, kvm_dying_cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08004801 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08004802 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004803 register_reboot_notifier(&kvm_reboot_notifier);
4804
Rusty Russellc16f8622007-07-30 21:12:19 +10004805 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03004806 if (!vcpu_align)
4807 vcpu_align = __alignof__(struct kvm_vcpu);
Paolo Bonzini46515732017-10-26 15:45:46 +02004808 kvm_vcpu_cache =
4809 kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align,
4810 SLAB_ACCOUNT,
4811 offsetof(struct kvm_vcpu, arch),
4812 sizeof_field(struct kvm_vcpu, arch),
4813 NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10004814 if (!kvm_vcpu_cache) {
4815 r = -ENOMEM;
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004816 goto out_free_3;
Rusty Russellc16f8622007-07-30 21:12:19 +10004817 }
4818
Gleb Natapovaf585b92010-10-14 11:22:46 +02004819 r = kvm_async_pf_init();
4820 if (r)
4821 goto out_free;
4822
Avi Kivity6aa8b732006-12-10 02:21:36 -08004823 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01004824 kvm_vm_fops.owner = module;
4825 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004826
4827 r = misc_register(&kvm_dev);
4828 if (r) {
Xiubo Li1170adc2015-02-26 14:58:26 +08004829 pr_err("kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02004830 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004831 }
4832
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004833 register_syscore_ops(&kvm_syscore_ops);
4834
Avi Kivity15ad7142007-07-11 18:17:21 +03004835 kvm_preempt_ops.sched_in = kvm_sched_in;
4836 kvm_preempt_ops.sched_out = kvm_sched_out;
4837
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02004838 kvm_init_debug();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07004839
Paolo Bonzini3c3c29f2014-09-24 13:02:46 +02004840 r = kvm_vfio_ops_init();
4841 WARN_ON(r);
4842
Avi Kivityc7addb92007-09-16 18:58:32 +02004843 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004844
Gleb Natapovaf585b92010-10-14 11:22:46 +02004845out_unreg:
4846 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004847out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10004848 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08004849out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 unregister_reboot_notifier(&kvm_reboot_notifier);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004851 cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
Zhang Xiantaod23087842007-11-29 15:35:39 +08004852out_free_2:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004853 kvm_arch_hardware_unsetup();
Miaohe Linfaf0be22019-11-23 10:45:50 +08004854out_free_1:
Rusty Russell7f59f492008-12-07 21:25:45 +10304855 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08004856out_free_0:
Cornelia Hucka0f155e2013-02-28 12:33:18 +01004857 kvm_irqfd_exit();
Paolo Bonzini36343f62016-10-26 13:35:56 +02004858out_irqfd:
Asias He7dac16c2013-05-08 10:57:29 +08004859 kvm_arch_exit();
4860out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08004861 return r;
4862}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004863EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004864
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004865void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866{
Janosch Frank4bd33b52015-10-14 12:37:35 +02004867 debugfs_remove_recursive(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10004869 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02004870 kvm_async_pf_deinit();
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004871 unregister_syscore_ops(&kvm_syscore_ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872 unregister_reboot_notifier(&kvm_reboot_notifier);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004873 cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004874 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004875 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004876 kvm_arch_exit();
Cornelia Hucka0f155e2013-02-28 12:33:18 +01004877 kvm_irqfd_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10304878 free_cpumask_var(cpus_hardware_enabled);
Wanpeng Li571ee1b2014-10-09 18:30:08 +08004879 kvm_vfio_ops_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004881EXPORT_SYMBOL_GPL(kvm_exit);
Junaid Shahidc57c8042019-11-04 12:22:02 +01004882
4883struct kvm_vm_worker_thread_context {
4884 struct kvm *kvm;
4885 struct task_struct *parent;
4886 struct completion init_done;
4887 kvm_vm_thread_fn_t thread_fn;
4888 uintptr_t data;
4889 int err;
4890};
4891
4892static int kvm_vm_worker_thread(void *context)
4893{
4894 /*
4895 * The init_context is allocated on the stack of the parent thread, so
4896 * we have to locally copy anything that is needed beyond initialization
4897 */
4898 struct kvm_vm_worker_thread_context *init_context = context;
4899 struct kvm *kvm = init_context->kvm;
4900 kvm_vm_thread_fn_t thread_fn = init_context->thread_fn;
4901 uintptr_t data = init_context->data;
4902 int err;
4903
4904 err = kthread_park(current);
4905 /* kthread_park(current) is never supposed to return an error */
4906 WARN_ON(err != 0);
4907 if (err)
4908 goto init_complete;
4909
4910 err = cgroup_attach_task_all(init_context->parent, current);
4911 if (err) {
4912 kvm_err("%s: cgroup_attach_task_all failed with err %d\n",
4913 __func__, err);
4914 goto init_complete;
4915 }
4916
4917 set_user_nice(current, task_nice(init_context->parent));
4918
4919init_complete:
4920 init_context->err = err;
4921 complete(&init_context->init_done);
4922 init_context = NULL;
4923
4924 if (err)
4925 return err;
4926
4927 /* Wait to be woken up by the spawner before proceeding. */
4928 kthread_parkme();
4929
4930 if (!kthread_should_stop())
4931 err = thread_fn(kvm, data);
4932
4933 return err;
4934}
4935
4936int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn,
4937 uintptr_t data, const char *name,
4938 struct task_struct **thread_ptr)
4939{
4940 struct kvm_vm_worker_thread_context init_context = {};
4941 struct task_struct *thread;
4942
4943 *thread_ptr = NULL;
4944 init_context.kvm = kvm;
4945 init_context.parent = current;
4946 init_context.thread_fn = thread_fn;
4947 init_context.data = data;
4948 init_completion(&init_context.init_done);
4949
4950 thread = kthread_run(kvm_vm_worker_thread, &init_context,
4951 "%s-%d", name, task_pid_nr(current));
4952 if (IS_ERR(thread))
4953 return PTR_ERR(thread);
4954
4955 /* kthread_run is never supposed to return NULL */
4956 WARN_ON(thread == NULL);
4957
4958 wait_for_completion(&init_context.init_done);
4959
4960 if (!init_context.err)
4961 *thread_ptr = thread;
4962
4963 return init_context.err;
4964}