blob: 0ad2378fe6ad7ec38b0afa5150f0a5653b34149d [file] [log] [blame]
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +02001#define pr_fmt(fmt) "Hyper-V: " fmt
2
3#include <linux/hyperv.h>
4#include <linux/log2.h>
5#include <linux/slab.h>
6#include <linux/types.h>
7
8#include <asm/fpu/api.h>
9#include <asm/mshyperv.h>
10#include <asm/msr.h>
11#include <asm/tlbflush.h>
Peter Zijlstra48a8b972018-08-22 17:30:16 +020012#include <asm/tlb.h>
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020013
Vitaly Kuznetsov773b79f2017-08-02 18:09:21 +020014#define CREATE_TRACE_POINTS
15#include <asm/trace/hyperv.h>
16
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020017/* Each gva in gva_list encodes up to 4096 pages to flush */
18#define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)
19
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +020020static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
21 const struct flush_tlb_info *info);
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +020022
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020023/*
24 * Fills in gva_list starting from offset. Returns the number of items added.
25 */
26static inline int fill_gva_list(u64 gva_list[], int offset,
27 unsigned long start, unsigned long end)
28{
29 int gva_n = offset;
30 unsigned long cur = start, diff;
31
32 do {
33 diff = end > cur ? end - cur : 0;
34
35 gva_list[gva_n] = cur & PAGE_MASK;
36 /*
37 * Lower 12 bits encode the number of additional
38 * pages to flush (in addition to the 'cur' page).
39 */
Tianyu Lan4030b4c2019-09-02 20:41:43 +080040 if (diff >= HV_TLB_FLUSH_UNIT) {
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020041 gva_list[gva_n] |= ~PAGE_MASK;
Tianyu Lan4030b4c2019-09-02 20:41:43 +080042 cur += HV_TLB_FLUSH_UNIT;
43 } else if (diff) {
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020044 gva_list[gva_n] |= (diff - 1) >> PAGE_SHIFT;
Tianyu Lan4030b4c2019-09-02 20:41:43 +080045 cur = end;
46 }
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020047
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020048 gva_n++;
49
50 } while (cur < end);
51
52 return gva_n - offset;
53}
54
Nadav Amit4ce94ea2021-02-20 15:17:07 -080055static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
56 const struct flush_tlb_info *info)
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020057{
58 int cpu, vcpu, gva_n, max_gvas;
Vitaly Kuznetsovc9c92be2018-05-16 17:21:24 +020059 struct hv_tlb_flush **flush_pcpu;
60 struct hv_tlb_flush *flush;
Joseph Salisbury753ed9c2021-04-16 17:43:03 -070061 u64 status;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020062 unsigned long flags;
63
Nadav Amit4ce94ea2021-02-20 15:17:07 -080064 trace_hyperv_mmu_flush_tlb_multi(cpus, info);
Vitaly Kuznetsov773b79f2017-08-02 18:09:21 +020065
K. Y. Srinivasan9a2d78e2018-05-16 14:53:34 -070066 if (!hv_hypercall_pg)
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020067 goto do_native;
68
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020069 local_irq_save(flags);
70
Vitaly Kuznetsovc9c92be2018-05-16 17:21:24 +020071 flush_pcpu = (struct hv_tlb_flush **)
K. Y. Srinivasan9a2d78e2018-05-16 14:53:34 -070072 this_cpu_ptr(hyperv_pcpu_input_arg);
Vitaly Kuznetsov60d73a72017-10-05 13:39:24 +020073
74 flush = *flush_pcpu;
75
76 if (unlikely(!flush)) {
77 local_irq_restore(flags);
78 goto do_native;
79 }
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020080
81 if (info->mm) {
Vitaly Kuznetsov617ab452018-01-24 11:36:29 +010082 /*
83 * AddressSpace argument must match the CR3 with PCID bits
84 * stripped out.
85 */
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020086 flush->address_space = virt_to_phys(info->mm->pgd);
Vitaly Kuznetsov617ab452018-01-24 11:36:29 +010087 flush->address_space &= CR3_ADDR_MASK;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020088 flush->flags = 0;
89 } else {
90 flush->address_space = 0;
91 flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
92 }
93
94 flush->processor_mask = 0;
95 if (cpumask_equal(cpus, cpu_present_mask)) {
96 flush->flags |= HV_FLUSH_ALL_PROCESSORS;
97 } else {
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +020098 /*
99 * From the supplied CPU set we need to figure out if we can get
100 * away with cheaper HVCALL_FLUSH_VIRTUAL_ADDRESS_{LIST,SPACE}
101 * hypercalls. This is possible when the highest VP number in
102 * the set is < 64. As VP numbers are usually in ascending order
103 * and match Linux CPU ids, here is an optimization: we check
104 * the VP number for the highest bit in the supplied set first
105 * so we can quickly find out if using *_EX hypercalls is a
106 * must. We will also check all VP numbers when walking the
107 * supplied CPU set to remain correct in all cases.
108 */
Vitaly Kuznetsov51500b72022-01-06 10:46:11 +0100109 cpu = cpumask_last(cpus);
110
111 if (cpu < nr_cpumask_bits && hv_cpu_number_to_vp_number(cpu) >= 64)
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200112 goto do_ex_hypercall;
113
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200114 for_each_cpu(cpu, cpus) {
115 vcpu = hv_cpu_number_to_vp_number(cpu);
Vitaly Kuznetsov110d2a72018-07-09 19:40:12 +0200116 if (vcpu == VP_INVAL) {
117 local_irq_restore(flags);
118 goto do_native;
119 }
120
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200121 if (vcpu >= 64)
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200122 goto do_ex_hypercall;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200123
124 __set_bit(vcpu, (unsigned long *)
125 &flush->processor_mask);
126 }
Vitaly Kuznetsov51500b72022-01-06 10:46:11 +0100127
128 /* nothing to flush if 'processor_mask' ends up being empty */
129 if (!flush->processor_mask) {
130 local_irq_restore(flags);
131 return;
132 }
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200133 }
134
135 /*
136 * We can flush not more than max_gvas with one hypercall. Flush the
137 * whole address space if we were asked to do more.
138 */
139 max_gvas = (PAGE_SIZE - sizeof(*flush)) / sizeof(flush->gva_list[0]);
140
141 if (info->end == TLB_FLUSH_ALL) {
142 flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
143 status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
144 flush, NULL);
145 } else if (info->end &&
146 ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
147 status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
148 flush, NULL);
149 } else {
150 gva_n = fill_gva_list(flush->gva_list, 0,
151 info->start, info->end);
152 status = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST,
153 gva_n, 0, flush, NULL);
154 }
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200155 goto check_status;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200156
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200157do_ex_hypercall:
158 status = hyperv_flush_tlb_others_ex(cpus, info);
159
160check_status:
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200161 local_irq_restore(flags);
162
Joseph Salisbury753ed9c2021-04-16 17:43:03 -0700163 if (hv_result_success(status))
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200164 return;
165do_native:
Nadav Amit4ce94ea2021-02-20 15:17:07 -0800166 native_flush_tlb_multi(cpus, info);
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200167}
168
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200169static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
170 const struct flush_tlb_info *info)
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200171{
172 int nr_bank = 0, max_gvas, gva_n;
Vitaly Kuznetsovc9c92be2018-05-16 17:21:24 +0200173 struct hv_tlb_flush_ex **flush_pcpu;
174 struct hv_tlb_flush_ex *flush;
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200175 u64 status;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200176
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200177 if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
Joseph Salisbury753ed9c2021-04-16 17:43:03 -0700178 return HV_STATUS_INVALID_PARAMETER;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200179
Vitaly Kuznetsovc9c92be2018-05-16 17:21:24 +0200180 flush_pcpu = (struct hv_tlb_flush_ex **)
K. Y. Srinivasan9a2d78e2018-05-16 14:53:34 -0700181 this_cpu_ptr(hyperv_pcpu_input_arg);
Vitaly Kuznetsov60d73a72017-10-05 13:39:24 +0200182
183 flush = *flush_pcpu;
184
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200185 if (info->mm) {
Vitaly Kuznetsov617ab452018-01-24 11:36:29 +0100186 /*
187 * AddressSpace argument must match the CR3 with PCID bits
188 * stripped out.
189 */
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200190 flush->address_space = virt_to_phys(info->mm->pgd);
Vitaly Kuznetsov617ab452018-01-24 11:36:29 +0100191 flush->address_space &= CR3_ADDR_MASK;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200192 flush->flags = 0;
193 } else {
194 flush->address_space = 0;
195 flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
196 }
197
198 flush->hv_vp_set.valid_bank_mask = 0;
199
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200200 flush->hv_vp_set.format = HV_GENERIC_SET_SPARSE_4K;
201 nr_bank = cpumask_to_vpset(&(flush->hv_vp_set), cpus);
Vitaly Kuznetsov0f0caa52018-07-09 19:40:11 +0200202 if (nr_bank < 0)
Joseph Salisbury753ed9c2021-04-16 17:43:03 -0700203 return HV_STATUS_INVALID_PARAMETER;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200204
205 /*
206 * We can flush not more than max_gvas with one hypercall. Flush the
207 * whole address space if we were asked to do more.
208 */
209 max_gvas =
210 (PAGE_SIZE - sizeof(*flush) - nr_bank *
211 sizeof(flush->hv_vp_set.bank_contents[0])) /
212 sizeof(flush->gva_list[0]);
213
214 if (info->end == TLB_FLUSH_ALL) {
215 flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
216 status = hv_do_rep_hypercall(
217 HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
Marcelo Henrique Cerriab7ff472017-10-05 10:34:29 -0300218 0, nr_bank, flush, NULL);
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200219 } else if (info->end &&
220 ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
221 status = hv_do_rep_hypercall(
222 HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
Marcelo Henrique Cerriab7ff472017-10-05 10:34:29 -0300223 0, nr_bank, flush, NULL);
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200224 } else {
225 gva_n = fill_gva_list(flush->gva_list, nr_bank,
226 info->start, info->end);
227 status = hv_do_rep_hypercall(
228 HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
Marcelo Henrique Cerriab7ff472017-10-05 10:34:29 -0300229 gva_n, nr_bank, flush, NULL);
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200230 }
231
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200232 return status;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200233}
234
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200235void hyperv_setup_mmu_ops(void)
236{
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200237 if (!(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED))
238 return;
239
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200240 pr_info("Using hypercall for remote TLB flush\n");
Nadav Amit4ce94ea2021-02-20 15:17:07 -0800241 pv_ops.mmu.flush_tlb_multi = hyperv_flush_tlb_multi;
Juergen Gross5c835112018-08-28 09:40:19 +0200242 pv_ops.mmu.tlb_remove_table = tlb_remove_table;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200243}