Thomas Gleixner | 775c8a3 | 2019-06-04 10:11:37 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 2 | /* |
| 3 | * kvm asynchronous fault support |
| 4 | * |
| 5 | * Copyright 2010 Red Hat, Inc. |
| 6 | * |
| 7 | * Author: |
| 8 | * Gleb Natapov <gleb@redhat.com> |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/kvm_host.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/mmu_context.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 15 | #include <linux/sched/mm.h> |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 16 | |
| 17 | #include "async_pf.h" |
| 18 | #include <trace/events/kvm.h> |
| 19 | |
| 20 | static struct kmem_cache *async_pf_cache; |
| 21 | |
| 22 | int kvm_async_pf_init(void) |
| 23 | { |
| 24 | async_pf_cache = KMEM_CACHE(kvm_async_pf, 0); |
| 25 | |
| 26 | if (!async_pf_cache) |
| 27 | return -ENOMEM; |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | void kvm_async_pf_deinit(void) |
| 33 | { |
Markus Elfring | 4f52696 | 2015-11-15 10:40:36 +0100 | [diff] [blame] | 34 | kmem_cache_destroy(async_pf_cache); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 35 | async_pf_cache = NULL; |
| 36 | } |
| 37 | |
| 38 | void kvm_async_pf_vcpu_init(struct kvm_vcpu *vcpu) |
| 39 | { |
| 40 | INIT_LIST_HEAD(&vcpu->async_pf.done); |
| 41 | INIT_LIST_HEAD(&vcpu->async_pf.queue); |
| 42 | spin_lock_init(&vcpu->async_pf.lock); |
| 43 | } |
| 44 | |
| 45 | static void async_pf_execute(struct work_struct *work) |
| 46 | { |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 47 | struct kvm_async_pf *apf = |
| 48 | container_of(work, struct kvm_async_pf, work); |
| 49 | struct mm_struct *mm = apf->mm; |
| 50 | struct kvm_vcpu *vcpu = apf->vcpu; |
| 51 | unsigned long addr = apf->addr; |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 52 | gpa_t cr2_or_gpa = apf->cr2_or_gpa; |
Lorenzo Stoakes | 8b7457e | 2016-12-14 15:06:55 -0800 | [diff] [blame] | 53 | int locked = 1; |
Vitaly Kuznetsov | 557a961 | 2020-05-25 16:41:21 +0200 | [diff] [blame] | 54 | bool first; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 55 | |
| 56 | might_sleep(); |
| 57 | |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 58 | /* |
Wei Yang | bdd303c | 2018-11-05 14:45:03 +0800 | [diff] [blame] | 59 | * This work is run asynchronously to the task which owns |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 60 | * mm and might be done in another context, so we must |
Lorenzo Stoakes | 8b7457e | 2016-12-14 15:06:55 -0800 | [diff] [blame] | 61 | * access remotely. |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 62 | */ |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 63 | mmap_read_lock(mm); |
Peter Xu | 64019a2 | 2020-08-11 18:39:01 -0700 | [diff] [blame] | 64 | get_user_pages_remote(mm, addr, 1, FOLL_WRITE, NULL, NULL, |
Lorenzo Stoakes | 8b7457e | 2016-12-14 15:06:55 -0800 | [diff] [blame] | 65 | &locked); |
| 66 | if (locked) |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 67 | mmap_read_unlock(mm); |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 68 | |
Paolo Bonzini | 4425f56 | 2020-01-20 16:14:37 +0100 | [diff] [blame] | 69 | if (IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC)) |
| 70 | kvm_arch_async_page_present(vcpu, apf); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 71 | |
| 72 | spin_lock(&vcpu->async_pf.lock); |
Vitaly Kuznetsov | 557a961 | 2020-05-25 16:41:21 +0200 | [diff] [blame] | 73 | first = list_empty(&vcpu->async_pf.done); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 74 | list_add_tail(&apf->link, &vcpu->async_pf.done); |
Paolo Bonzini | 22583f0 | 2016-11-17 15:55:45 +0100 | [diff] [blame] | 75 | apf->vcpu = NULL; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 76 | spin_unlock(&vcpu->async_pf.lock); |
| 77 | |
Vitaly Kuznetsov | 557a961 | 2020-05-25 16:41:21 +0200 | [diff] [blame] | 78 | if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first) |
| 79 | kvm_arch_async_page_present_queued(vcpu); |
| 80 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 81 | /* |
| 82 | * apf may be freed by kvm_check_async_pf_completion() after |
| 83 | * this point |
| 84 | */ |
| 85 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 86 | trace_kvm_async_pf_completed(addr, cr2_or_gpa); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 87 | |
Sean Christopherson | d92a5d1 | 2021-10-08 19:12:12 -0700 | [diff] [blame] | 88 | __kvm_vcpu_wake_up(vcpu); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 89 | |
Oleg Nesterov | 41c22f6 | 2014-04-21 15:26:01 +0200 | [diff] [blame] | 90 | mmput(mm); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 91 | kvm_put_kvm(vcpu->kvm); |
| 92 | } |
| 93 | |
| 94 | void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu) |
| 95 | { |
Paolo Bonzini | 22583f0 | 2016-11-17 15:55:45 +0100 | [diff] [blame] | 96 | spin_lock(&vcpu->async_pf.lock); |
| 97 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 98 | /* cancel outstanding work queue item */ |
| 99 | while (!list_empty(&vcpu->async_pf.queue)) { |
| 100 | struct kvm_async_pf *work = |
Geliang Tang | 433da86 | 2016-01-01 19:47:15 +0800 | [diff] [blame] | 101 | list_first_entry(&vcpu->async_pf.queue, |
| 102 | typeof(*work), queue); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 103 | list_del(&work->queue); |
Dominik Dingel | 9f2ceda | 2013-09-03 12:31:16 +0200 | [diff] [blame] | 104 | |
Paolo Bonzini | 22583f0 | 2016-11-17 15:55:45 +0100 | [diff] [blame] | 105 | /* |
| 106 | * We know it's present in vcpu->async_pf.done, do |
| 107 | * nothing here. |
| 108 | */ |
| 109 | if (!work->vcpu) |
| 110 | continue; |
| 111 | |
| 112 | spin_unlock(&vcpu->async_pf.lock); |
Dominik Dingel | 9f2ceda | 2013-09-03 12:31:16 +0200 | [diff] [blame] | 113 | #ifdef CONFIG_KVM_ASYNC_PF_SYNC |
| 114 | flush_work(&work->work); |
| 115 | #else |
Radim Krčmář | 98fda16 | 2013-09-04 22:32:24 +0200 | [diff] [blame] | 116 | if (cancel_work_sync(&work->work)) { |
Oleg Nesterov | 41c22f6 | 2014-04-21 15:26:01 +0200 | [diff] [blame] | 117 | mmput(work->mm); |
Radim Krčmář | 28b441e | 2013-09-04 22:32:23 +0200 | [diff] [blame] | 118 | kvm_put_kvm(vcpu->kvm); /* == work->vcpu->kvm */ |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 119 | kmem_cache_free(async_pf_cache, work); |
Radim Krčmář | 28b441e | 2013-09-04 22:32:23 +0200 | [diff] [blame] | 120 | } |
Dominik Dingel | 9f2ceda | 2013-09-03 12:31:16 +0200 | [diff] [blame] | 121 | #endif |
Paolo Bonzini | 22583f0 | 2016-11-17 15:55:45 +0100 | [diff] [blame] | 122 | spin_lock(&vcpu->async_pf.lock); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 125 | while (!list_empty(&vcpu->async_pf.done)) { |
| 126 | struct kvm_async_pf *work = |
Geliang Tang | 433da86 | 2016-01-01 19:47:15 +0800 | [diff] [blame] | 127 | list_first_entry(&vcpu->async_pf.done, |
| 128 | typeof(*work), link); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 129 | list_del(&work->link); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 130 | kmem_cache_free(async_pf_cache, work); |
| 131 | } |
| 132 | spin_unlock(&vcpu->async_pf.lock); |
| 133 | |
| 134 | vcpu->async_pf.queued = 0; |
| 135 | } |
| 136 | |
| 137 | void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu) |
| 138 | { |
| 139 | struct kvm_async_pf *work; |
| 140 | |
Xiao Guangrong | 15096ff | 2010-11-02 17:35:35 +0800 | [diff] [blame] | 141 | while (!list_empty_careful(&vcpu->async_pf.done) && |
Vitaly Kuznetsov | 7c0ade6 | 2020-05-25 16:41:18 +0200 | [diff] [blame] | 142 | kvm_arch_can_dequeue_async_page_present(vcpu)) { |
Xiao Guangrong | 15096ff | 2010-11-02 17:35:35 +0800 | [diff] [blame] | 143 | spin_lock(&vcpu->async_pf.lock); |
| 144 | work = list_first_entry(&vcpu->async_pf.done, typeof(*work), |
| 145 | link); |
| 146 | list_del(&work->link); |
| 147 | spin_unlock(&vcpu->async_pf.lock); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 148 | |
chai wen | f2e1066 | 2013-10-14 22:22:33 +0800 | [diff] [blame] | 149 | kvm_arch_async_page_ready(vcpu, work); |
Paolo Bonzini | 4425f56 | 2020-01-20 16:14:37 +0100 | [diff] [blame] | 150 | if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC)) |
| 151 | kvm_arch_async_page_present(vcpu, work); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 152 | |
Xiao Guangrong | 15096ff | 2010-11-02 17:35:35 +0800 | [diff] [blame] | 153 | list_del(&work->queue); |
| 154 | vcpu->async_pf.queued--; |
Xiao Guangrong | 15096ff | 2010-11-02 17:35:35 +0800 | [diff] [blame] | 155 | kmem_cache_free(async_pf_cache, work); |
| 156 | } |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 159 | /* |
| 160 | * Try to schedule a job to handle page fault asynchronously. Returns 'true' on |
| 161 | * success, 'false' on failure (page fault has to be handled synchronously). |
| 162 | */ |
| 163 | bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
| 164 | unsigned long hva, struct kvm_arch_async_pf *arch) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 165 | { |
| 166 | struct kvm_async_pf *work; |
| 167 | |
| 168 | if (vcpu->async_pf.queued >= ASYNC_PF_PER_VCPU) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 169 | return false; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 170 | |
Vitaly Kuznetsov | 7863e34 | 2020-06-10 19:55:31 +0200 | [diff] [blame] | 171 | /* Arch specific code should not do async PF in this case */ |
| 172 | if (unlikely(kvm_is_error_hva(hva))) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 173 | return false; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * do alloc nowait since if we are going to sleep anyway we |
| 177 | * may as well sleep faulting in page |
| 178 | */ |
Christian Borntraeger | d744479 | 2016-02-19 13:11:46 +0100 | [diff] [blame] | 179 | work = kmem_cache_zalloc(async_pf_cache, GFP_NOWAIT | __GFP_NOWARN); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 180 | if (!work) |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 181 | return false; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 182 | |
chai wen | f2e1066 | 2013-10-14 22:22:33 +0800 | [diff] [blame] | 183 | work->wakeup_all = false; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 184 | work->vcpu = vcpu; |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 185 | work->cr2_or_gpa = cr2_or_gpa; |
Dominik Dingel | e0ead41 | 2013-06-06 15:32:37 +0200 | [diff] [blame] | 186 | work->addr = hva; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 187 | work->arch = *arch; |
| 188 | work->mm = current->mm; |
Vegard Nossum | 3fce371 | 2017-02-27 14:30:10 -0800 | [diff] [blame] | 189 | mmget(work->mm); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 190 | kvm_get_kvm(work->vcpu->kvm); |
| 191 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 192 | INIT_WORK(&work->work, async_pf_execute); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 193 | |
| 194 | list_add_tail(&work->queue, &vcpu->async_pf.queue); |
| 195 | vcpu->async_pf.queued++; |
Vitaly Kuznetsov | 2a18b7e | 2020-06-10 19:55:32 +0200 | [diff] [blame] | 196 | work->notpresent_injected = kvm_arch_async_page_not_present(vcpu, work); |
Vitaly Kuznetsov | 7863e34 | 2020-06-10 19:55:31 +0200 | [diff] [blame] | 197 | |
| 198 | schedule_work(&work->work); |
| 199 | |
Vitaly Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 200 | return true; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 201 | } |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 202 | |
| 203 | int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu) |
| 204 | { |
| 205 | struct kvm_async_pf *work; |
Vitaly Kuznetsov | 557a961 | 2020-05-25 16:41:21 +0200 | [diff] [blame] | 206 | bool first; |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 207 | |
Xiao Guangrong | 64f638c | 2010-11-01 17:03:44 +0800 | [diff] [blame] | 208 | if (!list_empty_careful(&vcpu->async_pf.done)) |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 209 | return 0; |
| 210 | |
| 211 | work = kmem_cache_zalloc(async_pf_cache, GFP_ATOMIC); |
| 212 | if (!work) |
| 213 | return -ENOMEM; |
| 214 | |
chai wen | f2e1066 | 2013-10-14 22:22:33 +0800 | [diff] [blame] | 215 | work->wakeup_all = true; |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 216 | INIT_LIST_HEAD(&work->queue); /* for list_del to work */ |
| 217 | |
Xiao Guangrong | 64f638c | 2010-11-01 17:03:44 +0800 | [diff] [blame] | 218 | spin_lock(&vcpu->async_pf.lock); |
Vitaly Kuznetsov | 557a961 | 2020-05-25 16:41:21 +0200 | [diff] [blame] | 219 | first = list_empty(&vcpu->async_pf.done); |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 220 | list_add_tail(&work->link, &vcpu->async_pf.done); |
Xiao Guangrong | 64f638c | 2010-11-01 17:03:44 +0800 | [diff] [blame] | 221 | spin_unlock(&vcpu->async_pf.lock); |
| 222 | |
Vitaly Kuznetsov | 557a961 | 2020-05-25 16:41:21 +0200 | [diff] [blame] | 223 | if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first) |
| 224 | kvm_arch_async_page_present_queued(vcpu); |
| 225 | |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 226 | vcpu->async_pf.queued++; |
| 227 | return 0; |
| 228 | } |