mmu.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. cur += HV_TLB_FLUSH_UNIT;
  35. } else if (diff) {
  36. gva_list[gva_n] |= (diff - 1) >> PAGE_SHIFT;
  37. cur = end;
  38. }
  39. gva_n++;
  40. } while (cur < end);
  41. return gva_n - offset;
  42. }
  43. static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
  44. const struct flush_tlb_info *info)
  45. {
  46. int cpu, vcpu, gva_n, max_gvas;
  47. struct hv_tlb_flush **flush_pcpu;
  48. struct hv_tlb_flush *flush;
  49. u64 status;
  50. unsigned long flags;
  51. trace_hyperv_mmu_flush_tlb_multi(cpus, info);
  52. if (!hv_hypercall_pg)
  53. goto do_native;
  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. cpu = cpumask_last(cpus);
  90. if (cpu < nr_cpumask_bits && hv_cpu_number_to_vp_number(cpu) >= 64)
  91. goto do_ex_hypercall;
  92. for_each_cpu(cpu, cpus) {
  93. vcpu = hv_cpu_number_to_vp_number(cpu);
  94. if (vcpu == VP_INVAL) {
  95. local_irq_restore(flags);
  96. goto do_native;
  97. }
  98. if (vcpu >= 64)
  99. goto do_ex_hypercall;
  100. __set_bit(vcpu, (unsigned long *)
  101. &flush->processor_mask);
  102. }
  103. /* nothing to flush if 'processor_mask' ends up being empty */
  104. if (!flush->processor_mask) {
  105. local_irq_restore(flags);
  106. return;
  107. }
  108. }
  109. /*
  110. * We can flush not more than max_gvas with one hypercall. Flush the
  111. * whole address space if we were asked to do more.
  112. */
  113. max_gvas = (PAGE_SIZE - sizeof(*flush)) / sizeof(flush->gva_list[0]);
  114. if (info->end == TLB_FLUSH_ALL) {
  115. flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
  116. status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
  117. flush, NULL);
  118. } else if (info->end &&
  119. ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
  120. status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
  121. flush, NULL);
  122. } else {
  123. gva_n = fill_gva_list(flush->gva_list, 0,
  124. info->start, info->end);
  125. status = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST,
  126. gva_n, 0, flush, NULL);
  127. }
  128. goto check_status;
  129. do_ex_hypercall:
  130. status = hyperv_flush_tlb_others_ex(cpus, info);
  131. check_status:
  132. local_irq_restore(flags);
  133. if (hv_result_success(status))
  134. return;
  135. do_native:
  136. native_flush_tlb_multi(cpus, info);
  137. }
  138. static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
  139. const struct flush_tlb_info *info)
  140. {
  141. int nr_bank = 0, max_gvas, gva_n;
  142. struct hv_tlb_flush_ex **flush_pcpu;
  143. struct hv_tlb_flush_ex *flush;
  144. u64 status;
  145. if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
  146. return HV_STATUS_INVALID_PARAMETER;
  147. flush_pcpu = (struct hv_tlb_flush_ex **)
  148. this_cpu_ptr(hyperv_pcpu_input_arg);
  149. flush = *flush_pcpu;
  150. if (info->mm) {
  151. /*
  152. * AddressSpace argument must match the CR3 with PCID bits
  153. * stripped out.
  154. */
  155. flush->address_space = virt_to_phys(info->mm->pgd);
  156. flush->address_space &= CR3_ADDR_MASK;
  157. flush->flags = 0;
  158. } else {
  159. flush->address_space = 0;
  160. flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
  161. }
  162. flush->hv_vp_set.valid_bank_mask = 0;
  163. flush->hv_vp_set.format = HV_GENERIC_SET_SPARSE_4K;
  164. nr_bank = cpumask_to_vpset(&(flush->hv_vp_set), cpus);
  165. if (nr_bank < 0)
  166. return HV_STATUS_INVALID_PARAMETER;
  167. /*
  168. * We can flush not more than max_gvas with one hypercall. Flush the
  169. * whole address space if we were asked to do more.
  170. */
  171. max_gvas =
  172. (PAGE_SIZE - sizeof(*flush) - nr_bank *
  173. sizeof(flush->hv_vp_set.bank_contents[0])) /
  174. sizeof(flush->gva_list[0]);
  175. if (info->end == TLB_FLUSH_ALL) {
  176. flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
  177. status = hv_do_rep_hypercall(
  178. HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
  179. 0, nr_bank, flush, NULL);
  180. } else if (info->end &&
  181. ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
  182. status = hv_do_rep_hypercall(
  183. HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
  184. 0, nr_bank, flush, NULL);
  185. } else {
  186. gva_n = fill_gva_list(flush->gva_list, nr_bank,
  187. info->start, info->end);
  188. status = hv_do_rep_hypercall(
  189. HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
  190. gva_n, nr_bank, flush, NULL);
  191. }
  192. return status;
  193. }
  194. void hyperv_setup_mmu_ops(void)
  195. {
  196. if (!(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED))
  197. return;
  198. pr_info("Using hypercall for remote TLB flush\n");
  199. pv_ops.mmu.flush_tlb_multi = hyperv_flush_tlb_multi;
  200. pv_ops.mmu.tlb_remove_table = tlb_remove_table;
  201. }