hv_proc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/vmalloc.h>
  4. #include <linux/mm.h>
  5. #include <linux/clockchips.h>
  6. #include <linux/acpi.h>
  7. #include <linux/hyperv.h>
  8. #include <linux/slab.h>
  9. #include <linux/cpuhotplug.h>
  10. #include <linux/minmax.h>
  11. #include <asm/hypervisor.h>
  12. #include <asm/mshyperv.h>
  13. #include <asm/apic.h>
  14. #include <asm/trace/hyperv.h>
  15. /*
  16. * See struct hv_deposit_memory. The first u64 is partition ID, the rest
  17. * are GPAs.
  18. */
  19. #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
  20. /* Deposits exact number of pages. Must be called with interrupts enabled. */
  21. int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
  22. {
  23. struct page **pages, *page;
  24. int *counts;
  25. int num_allocations;
  26. int i, j, page_count;
  27. int order;
  28. u64 status;
  29. int ret;
  30. u64 base_pfn;
  31. struct hv_deposit_memory *input_page;
  32. unsigned long flags;
  33. if (num_pages > HV_DEPOSIT_MAX)
  34. return -E2BIG;
  35. if (!num_pages)
  36. return 0;
  37. /* One buffer for page pointers and counts */
  38. page = alloc_page(GFP_KERNEL);
  39. if (!page)
  40. return -ENOMEM;
  41. pages = page_address(page);
  42. counts = kcalloc(HV_DEPOSIT_MAX, sizeof(int), GFP_KERNEL);
  43. if (!counts) {
  44. free_page((unsigned long)pages);
  45. return -ENOMEM;
  46. }
  47. /* Allocate all the pages before disabling interrupts */
  48. i = 0;
  49. while (num_pages) {
  50. /* Find highest order we can actually allocate */
  51. order = 31 - __builtin_clz(num_pages);
  52. while (1) {
  53. pages[i] = alloc_pages_node(node, GFP_KERNEL, order);
  54. if (pages[i])
  55. break;
  56. if (!order) {
  57. ret = -ENOMEM;
  58. num_allocations = i;
  59. goto err_free_allocations;
  60. }
  61. --order;
  62. }
  63. split_page(pages[i], order);
  64. counts[i] = 1 << order;
  65. num_pages -= counts[i];
  66. i++;
  67. }
  68. num_allocations = i;
  69. local_irq_save(flags);
  70. input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
  71. input_page->partition_id = partition_id;
  72. /* Populate gpa_page_list - these will fit on the input page */
  73. for (i = 0, page_count = 0; i < num_allocations; ++i) {
  74. base_pfn = page_to_pfn(pages[i]);
  75. for (j = 0; j < counts[i]; ++j, ++page_count)
  76. input_page->gpa_page_list[page_count] = base_pfn + j;
  77. }
  78. status = hv_do_rep_hypercall(HVCALL_DEPOSIT_MEMORY,
  79. page_count, 0, input_page, NULL);
  80. local_irq_restore(flags);
  81. if (!hv_result_success(status)) {
  82. pr_err("Failed to deposit pages: %lld\n", status);
  83. ret = hv_result(status);
  84. goto err_free_allocations;
  85. }
  86. ret = 0;
  87. goto free_buf;
  88. err_free_allocations:
  89. for (i = 0; i < num_allocations; ++i) {
  90. base_pfn = page_to_pfn(pages[i]);
  91. for (j = 0; j < counts[i]; ++j)
  92. __free_page(pfn_to_page(base_pfn + j));
  93. }
  94. free_buf:
  95. free_page((unsigned long)pages);
  96. kfree(counts);
  97. return ret;
  98. }
  99. int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
  100. {
  101. struct hv_add_logical_processor_in *input;
  102. struct hv_add_logical_processor_out *output;
  103. u64 status;
  104. unsigned long flags;
  105. int ret = HV_STATUS_SUCCESS;
  106. int pxm = node_to_pxm(node);
  107. /*
  108. * When adding a logical processor, the hypervisor may return
  109. * HV_STATUS_INSUFFICIENT_MEMORY. When that happens, we deposit more
  110. * pages and retry.
  111. */
  112. do {
  113. local_irq_save(flags);
  114. input = *this_cpu_ptr(hyperv_pcpu_input_arg);
  115. /* We don't do anything with the output right now */
  116. output = *this_cpu_ptr(hyperv_pcpu_output_arg);
  117. input->lp_index = lp_index;
  118. input->apic_id = apic_id;
  119. input->flags = 0;
  120. input->proximity_domain_info.domain_id = pxm;
  121. input->proximity_domain_info.flags.reserved = 0;
  122. input->proximity_domain_info.flags.proximity_info_valid = 1;
  123. input->proximity_domain_info.flags.proximity_preferred = 1;
  124. status = hv_do_hypercall(HVCALL_ADD_LOGICAL_PROCESSOR,
  125. input, output);
  126. local_irq_restore(flags);
  127. if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
  128. if (!hv_result_success(status)) {
  129. pr_err("%s: cpu %u apic ID %u, %lld\n", __func__,
  130. lp_index, apic_id, status);
  131. ret = hv_result(status);
  132. }
  133. break;
  134. }
  135. ret = hv_call_deposit_pages(node, hv_current_partition_id, 1);
  136. } while (!ret);
  137. return ret;
  138. }
  139. int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
  140. {
  141. struct hv_create_vp *input;
  142. u64 status;
  143. unsigned long irq_flags;
  144. int ret = HV_STATUS_SUCCESS;
  145. int pxm = node_to_pxm(node);
  146. /* Root VPs don't seem to need pages deposited */
  147. if (partition_id != hv_current_partition_id) {
  148. /* The value 90 is empirically determined. It may change. */
  149. ret = hv_call_deposit_pages(node, partition_id, 90);
  150. if (ret)
  151. return ret;
  152. }
  153. do {
  154. local_irq_save(irq_flags);
  155. input = *this_cpu_ptr(hyperv_pcpu_input_arg);
  156. input->partition_id = partition_id;
  157. input->vp_index = vp_index;
  158. input->flags = flags;
  159. input->subnode_type = HvSubnodeAny;
  160. if (node != NUMA_NO_NODE) {
  161. input->proximity_domain_info.domain_id = pxm;
  162. input->proximity_domain_info.flags.reserved = 0;
  163. input->proximity_domain_info.flags.proximity_info_valid = 1;
  164. input->proximity_domain_info.flags.proximity_preferred = 1;
  165. } else {
  166. input->proximity_domain_info.as_uint64 = 0;
  167. }
  168. status = hv_do_hypercall(HVCALL_CREATE_VP, input, NULL);
  169. local_irq_restore(irq_flags);
  170. if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
  171. if (!hv_result_success(status)) {
  172. pr_err("%s: vcpu %u, lp %u, %lld\n", __func__,
  173. vp_index, flags, status);
  174. ret = hv_result(status);
  175. }
  176. break;
  177. }
  178. ret = hv_call_deposit_pages(node, partition_id, 1);
  179. } while (!ret);
  180. return ret;
  181. }