1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * KVM L1 hypervisor optimizations on Hyper-V.
4 */
5
6 #include <linux/kvm_host.h>
7 #include <asm/mshyperv.h>
8
9 #include "hyperv.h"
10 #include "kvm_onhyperv.h"
11
kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list * flush,void * data)12 static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
13 void *data)
14 {
15 struct kvm_tlb_range *range = data;
16
17 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
18 range->pages);
19 }
20
hv_remote_flush_root_tdp(hpa_t root_tdp,struct kvm_tlb_range * range)21 static inline int hv_remote_flush_root_tdp(hpa_t root_tdp,
22 struct kvm_tlb_range *range)
23 {
24 if (range)
25 return hyperv_flush_guest_mapping_range(root_tdp,
26 kvm_fill_hv_flush_list_func, (void *)range);
27 else
28 return hyperv_flush_guest_mapping(root_tdp);
29 }
30
hv_remote_flush_tlb_with_range(struct kvm * kvm,struct kvm_tlb_range * range)31 int hv_remote_flush_tlb_with_range(struct kvm *kvm,
32 struct kvm_tlb_range *range)
33 {
34 struct kvm_arch *kvm_arch = &kvm->arch;
35 struct kvm_vcpu *vcpu;
36 int ret = 0, i, nr_unique_valid_roots;
37 hpa_t root;
38
39 spin_lock(&kvm_arch->hv_root_tdp_lock);
40
41 if (!VALID_PAGE(kvm_arch->hv_root_tdp)) {
42 nr_unique_valid_roots = 0;
43
44 /*
45 * Flush all valid roots, and see if all vCPUs have converged
46 * on a common root, in which case future flushes can skip the
47 * loop and flush the common root.
48 */
49 kvm_for_each_vcpu(i, vcpu, kvm) {
50 root = vcpu->arch.hv_root_tdp;
51 if (!VALID_PAGE(root) || root == kvm_arch->hv_root_tdp)
52 continue;
53
54 /*
55 * Set the tracked root to the first valid root. Keep
56 * this root for the entirety of the loop even if more
57 * roots are encountered as a low effort optimization
58 * to avoid flushing the same (first) root again.
59 */
60 if (++nr_unique_valid_roots == 1)
61 kvm_arch->hv_root_tdp = root;
62
63 if (!ret)
64 ret = hv_remote_flush_root_tdp(root, range);
65
66 /*
67 * Stop processing roots if a failure occurred and
68 * multiple valid roots have already been detected.
69 */
70 if (ret && nr_unique_valid_roots > 1)
71 break;
72 }
73
74 /*
75 * The optimized flush of a single root can't be used if there
76 * are multiple valid roots (obviously).
77 */
78 if (nr_unique_valid_roots > 1)
79 kvm_arch->hv_root_tdp = INVALID_PAGE;
80 } else {
81 ret = hv_remote_flush_root_tdp(kvm_arch->hv_root_tdp, range);
82 }
83
84 spin_unlock(&kvm_arch->hv_root_tdp_lock);
85 return ret;
86 }
87 EXPORT_SYMBOL_GPL(hv_remote_flush_tlb_with_range);
88
hv_remote_flush_tlb(struct kvm * kvm)89 int hv_remote_flush_tlb(struct kvm *kvm)
90 {
91 return hv_remote_flush_tlb_with_range(kvm, NULL);
92 }
93 EXPORT_SYMBOL_GPL(hv_remote_flush_tlb);
94