mmu.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include <asm/tlb.h>
  11. #define CREATE_TRACE_POINTS
  12. #include <asm/trace/hyperv.h>
  13. /* Each gva in gva_list encodes up to 4096 pages to flush */
  14. #define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)
  15. static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
  16. const struct flush_tlb_info *info);
  17. /*
  18. * Fills in gva_list starting from offset. Returns the number of items added.
  19. */
  20. static inline int fill_gva_list(u64 gva_list[], int offset,
  21. unsigned long start, unsigned long end)
  22. {
  23. int gva_n = offset;
  24. unsigned long cur = start, diff;
  25. do {
  26. diff = end > cur ? end - cur : 0;
  27. gva_list[gva_n] = cur & PAGE_MASK;
  28. /*
  29. * Lower 12 bits encode the number of additional
  30. * pages to flush (in addition to the 'cur' page).
  31. */
  32. if (diff >= HV_TLB_FLUSH_UNIT)
  33. gva_list[gva_n] |= ~PAGE_MASK;
  34. else if (diff)
  35. gva_list[gva_n] |= (diff - 1) >> PAGE_SHIFT;
  36. cur += HV_TLB_FLUSH_UNIT;
  37. gva_n++;
  38. } while (cur < end);
  39. return gva_n - offset;
  40. }
  41. static void hyperv_flush_tlb_others(const struct cpumask *cpus,
  42. const struct flush_tlb_info *info)
  43. {
  44. int cpu, vcpu, gva_n, max_gvas;
  45. struct hv_tlb_flush **flush_pcpu;
  46. struct hv_tlb_flush *flush;
  47. u64 status = U64_MAX;
  48. unsigned long flags;
  49. trace_hyperv_mmu_flush_tlb_others(cpus, info);
  50. if (!hv_hypercall_pg)
  51. goto do_native;
  52. if (cpumask_empty(cpus))
  53. return;
  54. local_irq_save(flags);
  55. flush_pcpu = (struct hv_tlb_flush **)
  56. this_cpu_ptr(hyperv_pcpu_input_arg);
  57. flush = *flush_pcpu;
  58. if (unlikely(!flush)) {
  59. local_irq_restore(flags);
  60. goto do_native;
  61. }
  62. if (info->mm) {
  63. /*
  64. * AddressSpace argument must match the CR3 with PCID bits
  65. * stripped out.
  66. */
  67. flush->address_space = virt_to_phys(info->mm->pgd);
  68. flush->address_space &= CR3_ADDR_MASK;
  69. flush->flags = 0;
  70. } else {
  71. flush->address_space = 0;
  72. flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
  73. }
  74. flush->processor_mask = 0;
  75. if (cpumask_equal(cpus, cpu_present_mask)) {
  76. flush->flags |= HV_FLUSH_ALL_PROCESSORS;
  77. } else {
  78. /*
  79. * From the supplied CPU set we need to figure out if we can get
  80. * away with cheaper HVCALL_FLUSH_VIRTUAL_ADDRESS_{LIST,SPACE}
  81. * hypercalls. This is possible when the highest VP number in
  82. * the set is < 64. As VP numbers are usually in ascending order
  83. * and match Linux CPU ids, here is an optimization: we check
  84. * the VP number for the highest bit in the supplied set first
  85. * so we can quickly find out if using *_EX hypercalls is a
  86. * must. We will also check all VP numbers when walking the
  87. * supplied CPU set to remain correct in all cases.
  88. */
  89. if (hv_cpu_number_to_vp_number(cpumask_last(cpus)) >= 64)
  90. goto do_ex_hypercall;
  91. for_each_cpu(cpu, cpus) {
  92. vcpu = hv_cpu_number_to_vp_number(cpu);
  93. if (vcpu == VP_INVAL) {
  94. local_irq_restore(flags);
  95. goto do_native;
  96. }
  97. if (vcpu >= 64)
  98. goto do_ex_hypercall;
  99. __set_bit(vcpu, (unsigned long *)
  100. &flush->processor_mask);
  101. }
  102. }
  103. /*
  104. * We can flush not more than max_gvas with one hypercall. Flush the
  105. * whole address space if we were asked to do more.
  106. */
  107. max_gvas = (PAGE_SIZE - sizeof(*flush)) / sizeof(flush->gva_list[0]);
  108. if (info->end == TLB_FLUSH_ALL) {
  109. flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
  110. status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
  111. flush, NULL);
  112. } else if (info->end &&
  113. ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
  114. status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
  115. flush, NULL);
  116. } else {
  117. gva_n = fill_gva_list(flush->gva_list, 0,
  118. info->start, info->end);
  119. status = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST,
  120. gva_n, 0, flush, NULL);
  121. }
  122. goto check_status;
  123. do_ex_hypercall:
  124. status = hyperv_flush_tlb_others_ex(cpus, info);
  125. check_status:
  126. local_irq_restore(flags);
  127. if (!(status & HV_HYPERCALL_RESULT_MASK))
  128. return;
  129. do_native:
  130. native_flush_tlb_others(cpus, info);
  131. }
  132. static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
  133. const struct flush_tlb_info *info)
  134. {
  135. int nr_bank = 0, max_gvas, gva_n;
  136. struct hv_tlb_flush_ex **flush_pcpu;
  137. struct hv_tlb_flush_ex *flush;
  138. u64 status;
  139. if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
  140. return U64_MAX;
  141. flush_pcpu = (struct hv_tlb_flush_ex **)
  142. this_cpu_ptr(hyperv_pcpu_input_arg);
  143. flush = *flush_pcpu;
  144. if (info->mm) {
  145. /*
  146. * AddressSpace argument must match the CR3 with PCID bits
  147. * stripped out.
  148. */
  149. flush->address_space = virt_to_phys(info->mm->pgd);
  150. flush->address_space &= CR3_ADDR_MASK;
  151. flush->flags = 0;
  152. } else {
  153. flush->address_space = 0;
  154. flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
  155. }
  156. flush->hv_vp_set.valid_bank_mask = 0;
  157. flush->hv_vp_set.format = HV_GENERIC_SET_SPARSE_4K;
  158. nr_bank = cpumask_to_vpset(&(flush->hv_vp_set), cpus);
  159. if (nr_bank < 0)
  160. return U64_MAX;
  161. /*
  162. * We can flush not more than max_gvas with one hypercall. Flush the
  163. * whole address space if we were asked to do more.
  164. */
  165. max_gvas =
  166. (PAGE_SIZE - sizeof(*flush) - nr_bank *
  167. sizeof(flush->hv_vp_set.bank_contents[0])) /
  168. sizeof(flush->gva_list[0]);
  169. if (info->end == TLB_FLUSH_ALL) {
  170. flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
  171. status = hv_do_rep_hypercall(
  172. HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
  173. 0, nr_bank, flush, NULL);
  174. } else if (info->end &&
  175. ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
  176. status = hv_do_rep_hypercall(
  177. HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
  178. 0, nr_bank, flush, NULL);
  179. } else {
  180. gva_n = fill_gva_list(flush->gva_list, nr_bank,
  181. info->start, info->end);
  182. status = hv_do_rep_hypercall(
  183. HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
  184. gva_n, nr_bank, flush, NULL);
  185. }
  186. return status;
  187. }
  188. void hyperv_setup_mmu_ops(void)
  189. {
  190. if (!(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED))
  191. return;
  192. pr_info("Using hypercall for remote TLB flush\n");
  193. pv_ops.mmu.flush_tlb_others = hyperv_flush_tlb_others;
  194. pv_ops.mmu.tlb_remove_table = tlb_remove_table;
  195. }