mmu.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_others(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 = U64_MAX;
  50. unsigned long flags;
  51. trace_hyperv_mmu_flush_tlb_others(cpus, info);
  52. if (!hv_hypercall_pg)
  53. goto do_native;
  54. if (cpumask_empty(cpus))
  55. return;
  56. local_irq_save(flags);
  57. flush_pcpu = (struct hv_tlb_flush **)
  58. this_cpu_ptr(hyperv_pcpu_input_arg);
  59. flush = *flush_pcpu;
  60. if (unlikely(!flush)) {
  61. local_irq_restore(flags);
  62. goto do_native;
  63. }
  64. if (info->mm) {
  65. /*
  66. * AddressSpace argument must match the CR3 with PCID bits
  67. * stripped out.
  68. */
  69. flush->address_space = virt_to_phys(info->mm->pgd);
  70. flush->address_space &= CR3_ADDR_MASK;
  71. flush->flags = 0;
  72. } else {
  73. flush->address_space = 0;
  74. flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
  75. }
  76. flush->processor_mask = 0;
  77. if (cpumask_equal(cpus, cpu_present_mask)) {
  78. flush->flags |= HV_FLUSH_ALL_PROCESSORS;
  79. } else {
  80. /*
  81. * From the supplied CPU set we need to figure out if we can get
  82. * away with cheaper HVCALL_FLUSH_VIRTUAL_ADDRESS_{LIST,SPACE}
  83. * hypercalls. This is possible when the highest VP number in
  84. * the set is < 64. As VP numbers are usually in ascending order
  85. * and match Linux CPU ids, here is an optimization: we check
  86. * the VP number for the highest bit in the supplied set first
  87. * so we can quickly find out if using *_EX hypercalls is a
  88. * must. We will also check all VP numbers when walking the
  89. * supplied CPU set to remain correct in all cases.
  90. */
  91. if (hv_cpu_number_to_vp_number(cpumask_last(cpus)) >= 64)
  92. goto do_ex_hypercall;
  93. for_each_cpu(cpu, cpus) {
  94. vcpu = hv_cpu_number_to_vp_number(cpu);
  95. if (vcpu == VP_INVAL) {
  96. local_irq_restore(flags);
  97. goto do_native;
  98. }
  99. if (vcpu >= 64)
  100. goto do_ex_hypercall;
  101. __set_bit(vcpu, (unsigned long *)
  102. &flush->processor_mask);
  103. }
  104. }
  105. /*
  106. * We can flush not more than max_gvas with one hypercall. Flush the
  107. * whole address space if we were asked to do more.
  108. */
  109. max_gvas = (PAGE_SIZE - sizeof(*flush)) / sizeof(flush->gva_list[0]);
  110. if (info->end == TLB_FLUSH_ALL) {
  111. flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
  112. status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
  113. flush, NULL);
  114. } else if (info->end &&
  115. ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
  116. status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
  117. flush, NULL);
  118. } else {
  119. gva_n = fill_gva_list(flush->gva_list, 0,
  120. info->start, info->end);
  121. status = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST,
  122. gva_n, 0, flush, NULL);
  123. }
  124. goto check_status;
  125. do_ex_hypercall:
  126. status = hyperv_flush_tlb_others_ex(cpus, info);
  127. check_status:
  128. local_irq_restore(flags);
  129. if (!(status & HV_HYPERCALL_RESULT_MASK))
  130. return;
  131. do_native:
  132. native_flush_tlb_others(cpus, info);
  133. }
  134. static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
  135. const struct flush_tlb_info *info)
  136. {
  137. int nr_bank = 0, max_gvas, gva_n;
  138. struct hv_tlb_flush_ex **flush_pcpu;
  139. struct hv_tlb_flush_ex *flush;
  140. u64 status;
  141. if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
  142. return U64_MAX;
  143. flush_pcpu = (struct hv_tlb_flush_ex **)
  144. this_cpu_ptr(hyperv_pcpu_input_arg);
  145. flush = *flush_pcpu;
  146. if (info->mm) {
  147. /*
  148. * AddressSpace argument must match the CR3 with PCID bits
  149. * stripped out.
  150. */
  151. flush->address_space = virt_to_phys(info->mm->pgd);
  152. flush->address_space &= CR3_ADDR_MASK;
  153. flush->flags = 0;
  154. } else {
  155. flush->address_space = 0;
  156. flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
  157. }
  158. flush->hv_vp_set.valid_bank_mask = 0;
  159. flush->hv_vp_set.format = HV_GENERIC_SET_SPARSE_4K;
  160. nr_bank = cpumask_to_vpset(&(flush->hv_vp_set), cpus);
  161. if (nr_bank < 0)
  162. return U64_MAX;
  163. /*
  164. * We can flush not more than max_gvas with one hypercall. Flush the
  165. * whole address space if we were asked to do more.
  166. */
  167. max_gvas =
  168. (PAGE_SIZE - sizeof(*flush) - nr_bank *
  169. sizeof(flush->hv_vp_set.bank_contents[0])) /
  170. sizeof(flush->gva_list[0]);
  171. if (info->end == TLB_FLUSH_ALL) {
  172. flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
  173. status = hv_do_rep_hypercall(
  174. HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
  175. 0, nr_bank, flush, NULL);
  176. } else if (info->end &&
  177. ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
  178. status = hv_do_rep_hypercall(
  179. HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
  180. 0, nr_bank, flush, NULL);
  181. } else {
  182. gva_n = fill_gva_list(flush->gva_list, nr_bank,
  183. info->start, info->end);
  184. status = hv_do_rep_hypercall(
  185. HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
  186. gva_n, nr_bank, flush, NULL);
  187. }
  188. return status;
  189. }
  190. void hyperv_setup_mmu_ops(void)
  191. {
  192. if (!(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED))
  193. return;
  194. pr_info("Using hypercall for remote TLB flush\n");
  195. pv_mmu_ops.flush_tlb_others = hyperv_flush_tlb_others;
  196. pv_mmu_ops.tlb_remove_table = tlb_remove_table;
  197. }