mmu.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #define pr_fmt(fmt) "Hyper-V: " fmt
  2. #include <linux/hyperv.h>
  3. #include <linux/log2.h>
  4. #include <linux/slab.h>
  5. #include <linux/types.h>
  6. #include <asm/fpu/api.h>
  7. #include <asm/mshyperv.h>
  8. #include <asm/msr.h>
  9. #include <asm/tlbflush.h>
  10. #define CREATE_TRACE_POINTS
  11. #include <asm/trace/hyperv.h>
  12. /* HvFlushVirtualAddressSpace, HvFlushVirtualAddressList hypercalls */
  13. struct hv_flush_pcpu {
  14. u64 address_space;
  15. u64 flags;
  16. u64 processor_mask;
  17. u64 gva_list[];
  18. };
  19. /* HvFlushVirtualAddressSpaceEx, HvFlushVirtualAddressListEx hypercalls */
  20. struct hv_flush_pcpu_ex {
  21. u64 address_space;
  22. u64 flags;
  23. struct {
  24. u64 format;
  25. u64 valid_bank_mask;
  26. u64 bank_contents[];
  27. } hv_vp_set;
  28. u64 gva_list[];
  29. };
  30. /* Each gva in gva_list encodes up to 4096 pages to flush */
  31. #define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)
  32. static struct hv_flush_pcpu __percpu **pcpu_flush;
  33. static struct hv_flush_pcpu_ex __percpu **pcpu_flush_ex;
  34. /*
  35. * Fills in gva_list starting from offset. Returns the number of items added.
  36. */
  37. static inline int fill_gva_list(u64 gva_list[], int offset,
  38. unsigned long start, unsigned long end)
  39. {
  40. int gva_n = offset;
  41. unsigned long cur = start, diff;
  42. do {
  43. diff = end > cur ? end - cur : 0;
  44. gva_list[gva_n] = cur & PAGE_MASK;
  45. /*
  46. * Lower 12 bits encode the number of additional
  47. * pages to flush (in addition to the 'cur' page).
  48. */
  49. if (diff >= HV_TLB_FLUSH_UNIT)
  50. gva_list[gva_n] |= ~PAGE_MASK;
  51. else if (diff)
  52. gva_list[gva_n] |= (diff - 1) >> PAGE_SHIFT;
  53. cur += HV_TLB_FLUSH_UNIT;
  54. gva_n++;
  55. } while (cur < end);
  56. return gva_n - offset;
  57. }
  58. /* Return the number of banks in the resulting vp_set */
  59. static inline int cpumask_to_vp_set(struct hv_flush_pcpu_ex *flush,
  60. const struct cpumask *cpus)
  61. {
  62. int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
  63. /* valid_bank_mask can represent up to 64 banks */
  64. if (hv_max_vp_index / 64 >= 64)
  65. return 0;
  66. /*
  67. * Clear all banks up to the maximum possible bank as hv_flush_pcpu_ex
  68. * structs are not cleared between calls, we risk flushing unneeded
  69. * vCPUs otherwise.
  70. */
  71. for (vcpu_bank = 0; vcpu_bank <= hv_max_vp_index / 64; vcpu_bank++)
  72. flush->hv_vp_set.bank_contents[vcpu_bank] = 0;
  73. /*
  74. * Some banks may end up being empty but this is acceptable.
  75. */
  76. for_each_cpu(cpu, cpus) {
  77. vcpu = hv_cpu_number_to_vp_number(cpu);
  78. vcpu_bank = vcpu / 64;
  79. vcpu_offset = vcpu % 64;
  80. __set_bit(vcpu_offset, (unsigned long *)
  81. &flush->hv_vp_set.bank_contents[vcpu_bank]);
  82. if (vcpu_bank >= nr_bank)
  83. nr_bank = vcpu_bank + 1;
  84. }
  85. flush->hv_vp_set.valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
  86. return nr_bank;
  87. }
  88. static void hyperv_flush_tlb_others(const struct cpumask *cpus,
  89. const struct flush_tlb_info *info)
  90. {
  91. int cpu, vcpu, gva_n, max_gvas;
  92. struct hv_flush_pcpu **flush_pcpu;
  93. struct hv_flush_pcpu *flush;
  94. u64 status = U64_MAX;
  95. unsigned long flags;
  96. trace_hyperv_mmu_flush_tlb_others(cpus, info);
  97. if (!pcpu_flush || !hv_hypercall_pg)
  98. goto do_native;
  99. if (cpumask_empty(cpus))
  100. return;
  101. local_irq_save(flags);
  102. flush_pcpu = this_cpu_ptr(pcpu_flush);
  103. if (unlikely(!*flush_pcpu))
  104. *flush_pcpu = page_address(alloc_page(GFP_ATOMIC));
  105. flush = *flush_pcpu;
  106. if (unlikely(!flush)) {
  107. local_irq_restore(flags);
  108. goto do_native;
  109. }
  110. if (info->mm) {
  111. /*
  112. * AddressSpace argument must match the CR3 with PCID bits
  113. * stripped out.
  114. */
  115. flush->address_space = virt_to_phys(info->mm->pgd);
  116. flush->address_space &= CR3_ADDR_MASK;
  117. flush->flags = 0;
  118. } else {
  119. flush->address_space = 0;
  120. flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
  121. }
  122. flush->processor_mask = 0;
  123. if (cpumask_equal(cpus, cpu_present_mask)) {
  124. flush->flags |= HV_FLUSH_ALL_PROCESSORS;
  125. } else {
  126. for_each_cpu(cpu, cpus) {
  127. vcpu = hv_cpu_number_to_vp_number(cpu);
  128. if (vcpu >= 64)
  129. goto do_native;
  130. __set_bit(vcpu, (unsigned long *)
  131. &flush->processor_mask);
  132. }
  133. }
  134. /*
  135. * We can flush not more than max_gvas with one hypercall. Flush the
  136. * whole address space if we were asked to do more.
  137. */
  138. max_gvas = (PAGE_SIZE - sizeof(*flush)) / sizeof(flush->gva_list[0]);
  139. if (info->end == TLB_FLUSH_ALL) {
  140. flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
  141. status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
  142. flush, NULL);
  143. } else if (info->end &&
  144. ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
  145. status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
  146. flush, NULL);
  147. } else {
  148. gva_n = fill_gva_list(flush->gva_list, 0,
  149. info->start, info->end);
  150. status = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST,
  151. gva_n, 0, flush, NULL);
  152. }
  153. local_irq_restore(flags);
  154. if (!(status & HV_HYPERCALL_RESULT_MASK))
  155. return;
  156. do_native:
  157. native_flush_tlb_others(cpus, info);
  158. }
  159. static void hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
  160. const struct flush_tlb_info *info)
  161. {
  162. int nr_bank = 0, max_gvas, gva_n;
  163. struct hv_flush_pcpu_ex **flush_pcpu;
  164. struct hv_flush_pcpu_ex *flush;
  165. u64 status = U64_MAX;
  166. unsigned long flags;
  167. trace_hyperv_mmu_flush_tlb_others(cpus, info);
  168. if (!pcpu_flush_ex || !hv_hypercall_pg)
  169. goto do_native;
  170. if (cpumask_empty(cpus))
  171. return;
  172. local_irq_save(flags);
  173. flush_pcpu = this_cpu_ptr(pcpu_flush_ex);
  174. if (unlikely(!*flush_pcpu))
  175. *flush_pcpu = page_address(alloc_page(GFP_ATOMIC));
  176. flush = *flush_pcpu;
  177. if (unlikely(!flush)) {
  178. local_irq_restore(flags);
  179. goto do_native;
  180. }
  181. if (info->mm) {
  182. /*
  183. * AddressSpace argument must match the CR3 with PCID bits
  184. * stripped out.
  185. */
  186. flush->address_space = virt_to_phys(info->mm->pgd);
  187. flush->address_space &= CR3_ADDR_MASK;
  188. flush->flags = 0;
  189. } else {
  190. flush->address_space = 0;
  191. flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
  192. }
  193. flush->hv_vp_set.valid_bank_mask = 0;
  194. if (!cpumask_equal(cpus, cpu_present_mask)) {
  195. flush->hv_vp_set.format = HV_GENERIC_SET_SPARCE_4K;
  196. nr_bank = cpumask_to_vp_set(flush, cpus);
  197. }
  198. if (!nr_bank) {
  199. flush->hv_vp_set.format = HV_GENERIC_SET_ALL;
  200. flush->flags |= HV_FLUSH_ALL_PROCESSORS;
  201. }
  202. /*
  203. * We can flush not more than max_gvas with one hypercall. Flush the
  204. * whole address space if we were asked to do more.
  205. */
  206. max_gvas =
  207. (PAGE_SIZE - sizeof(*flush) - nr_bank *
  208. sizeof(flush->hv_vp_set.bank_contents[0])) /
  209. sizeof(flush->gva_list[0]);
  210. if (info->end == TLB_FLUSH_ALL) {
  211. flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
  212. status = hv_do_rep_hypercall(
  213. HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
  214. 0, nr_bank, flush, NULL);
  215. } else if (info->end &&
  216. ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
  217. status = hv_do_rep_hypercall(
  218. HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
  219. 0, nr_bank, flush, NULL);
  220. } else {
  221. gva_n = fill_gva_list(flush->gva_list, nr_bank,
  222. info->start, info->end);
  223. status = hv_do_rep_hypercall(
  224. HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
  225. gva_n, nr_bank, flush, NULL);
  226. }
  227. local_irq_restore(flags);
  228. if (!(status & HV_HYPERCALL_RESULT_MASK))
  229. return;
  230. do_native:
  231. native_flush_tlb_others(cpus, info);
  232. }
  233. void hyperv_setup_mmu_ops(void)
  234. {
  235. if (!(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED))
  236. return;
  237. if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED)) {
  238. pr_info("Using hypercall for remote TLB flush\n");
  239. pv_mmu_ops.flush_tlb_others = hyperv_flush_tlb_others;
  240. } else {
  241. pr_info("Using ext hypercall for remote TLB flush\n");
  242. pv_mmu_ops.flush_tlb_others = hyperv_flush_tlb_others_ex;
  243. }
  244. }
  245. void hyper_alloc_mmu(void)
  246. {
  247. if (!(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED))
  248. return;
  249. if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
  250. pcpu_flush = alloc_percpu(struct hv_flush_pcpu *);
  251. else
  252. pcpu_flush_ex = alloc_percpu(struct hv_flush_pcpu_ex *);
  253. }