hv_proc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/slab.h>
  7. #include <linux/cpuhotplug.h>
  8. #include <linux/minmax.h>
  9. #include <linux/export.h>
  10. #include <asm/mshyperv.h>
  11. /*
  12. * See struct hv_deposit_memory. The first u64 is partition ID, the rest
  13. * are GPAs.
  14. */
  15. #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
  16. /* Deposits exact number of pages. Must be called with interrupts enabled. */
  17. int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
  18. {
  19. struct page **pages, *page;
  20. int *counts;
  21. int num_allocations;
  22. int i, j, page_count;
  23. int order;
  24. u64 status;
  25. int ret;
  26. u64 base_pfn;
  27. struct hv_deposit_memory *input_page;
  28. unsigned long flags;
  29. if (num_pages > HV_DEPOSIT_MAX)
  30. return -E2BIG;
  31. if (!num_pages)
  32. return 0;
  33. /* One buffer for page pointers and counts */
  34. page = alloc_page(GFP_KERNEL);
  35. if (!page)
  36. return -ENOMEM;
  37. pages = page_address(page);
  38. counts = kzalloc_objs(int, HV_DEPOSIT_MAX);
  39. if (!counts) {
  40. free_page((unsigned long)pages);
  41. return -ENOMEM;
  42. }
  43. /* Allocate all the pages before disabling interrupts */
  44. i = 0;
  45. while (num_pages) {
  46. /* Find highest order we can actually allocate */
  47. order = 31 - __builtin_clz(num_pages);
  48. while (1) {
  49. pages[i] = alloc_pages_node(node, GFP_KERNEL, order);
  50. if (pages[i])
  51. break;
  52. if (!order) {
  53. ret = -ENOMEM;
  54. num_allocations = i;
  55. goto err_free_allocations;
  56. }
  57. --order;
  58. }
  59. split_page(pages[i], order);
  60. counts[i] = 1 << order;
  61. num_pages -= counts[i];
  62. i++;
  63. }
  64. num_allocations = i;
  65. local_irq_save(flags);
  66. input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
  67. input_page->partition_id = partition_id;
  68. /* Populate gpa_page_list - these will fit on the input page */
  69. for (i = 0, page_count = 0; i < num_allocations; ++i) {
  70. base_pfn = page_to_pfn(pages[i]);
  71. for (j = 0; j < counts[i]; ++j, ++page_count)
  72. input_page->gpa_page_list[page_count] = base_pfn + j;
  73. }
  74. status = hv_do_rep_hypercall(HVCALL_DEPOSIT_MEMORY,
  75. page_count, 0, input_page, NULL);
  76. local_irq_restore(flags);
  77. if (!hv_result_success(status)) {
  78. hv_status_err(status, "\n");
  79. ret = hv_result_to_errno(status);
  80. goto err_free_allocations;
  81. }
  82. ret = 0;
  83. goto free_buf;
  84. err_free_allocations:
  85. for (i = 0; i < num_allocations; ++i) {
  86. base_pfn = page_to_pfn(pages[i]);
  87. for (j = 0; j < counts[i]; ++j)
  88. __free_page(pfn_to_page(base_pfn + j));
  89. }
  90. free_buf:
  91. free_page((unsigned long)pages);
  92. kfree(counts);
  93. return ret;
  94. }
  95. EXPORT_SYMBOL_GPL(hv_call_deposit_pages);
  96. int hv_deposit_memory_node(int node, u64 partition_id,
  97. u64 hv_status)
  98. {
  99. u32 num_pages = 1;
  100. switch (hv_result(hv_status)) {
  101. case HV_STATUS_INSUFFICIENT_MEMORY:
  102. break;
  103. case HV_STATUS_INSUFFICIENT_CONTIGUOUS_MEMORY:
  104. num_pages = HV_MAX_CONTIGUOUS_ALLOCATION_PAGES;
  105. break;
  106. case HV_STATUS_INSUFFICIENT_CONTIGUOUS_ROOT_MEMORY:
  107. num_pages = HV_MAX_CONTIGUOUS_ALLOCATION_PAGES;
  108. fallthrough;
  109. case HV_STATUS_INSUFFICIENT_ROOT_MEMORY:
  110. if (!hv_root_partition()) {
  111. hv_status_err(hv_status, "Unexpected root memory deposit\n");
  112. return -ENOMEM;
  113. }
  114. partition_id = HV_PARTITION_ID_SELF;
  115. break;
  116. default:
  117. hv_status_err(hv_status, "Unexpected!\n");
  118. return -ENOMEM;
  119. }
  120. return hv_call_deposit_pages(node, partition_id, num_pages);
  121. }
  122. EXPORT_SYMBOL_GPL(hv_deposit_memory_node);
  123. bool hv_result_needs_memory(u64 status)
  124. {
  125. switch (hv_result(status)) {
  126. case HV_STATUS_INSUFFICIENT_MEMORY:
  127. case HV_STATUS_INSUFFICIENT_CONTIGUOUS_MEMORY:
  128. case HV_STATUS_INSUFFICIENT_ROOT_MEMORY:
  129. case HV_STATUS_INSUFFICIENT_CONTIGUOUS_ROOT_MEMORY:
  130. return true;
  131. }
  132. return false;
  133. }
  134. EXPORT_SYMBOL_GPL(hv_result_needs_memory);
  135. int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
  136. {
  137. struct hv_input_add_logical_processor *input;
  138. struct hv_output_add_logical_processor *output;
  139. u64 status;
  140. unsigned long flags;
  141. int ret = 0;
  142. /*
  143. * When adding a logical processor, the hypervisor may return
  144. * HV_STATUS_INSUFFICIENT_MEMORY. When that happens, we deposit more
  145. * pages and retry.
  146. */
  147. do {
  148. local_irq_save(flags);
  149. input = *this_cpu_ptr(hyperv_pcpu_input_arg);
  150. /* We don't do anything with the output right now */
  151. output = *this_cpu_ptr(hyperv_pcpu_output_arg);
  152. input->lp_index = lp_index;
  153. input->apic_id = apic_id;
  154. input->proximity_domain_info = hv_numa_node_to_pxm_info(node);
  155. status = hv_do_hypercall(HVCALL_ADD_LOGICAL_PROCESSOR,
  156. input, output);
  157. local_irq_restore(flags);
  158. if (!hv_result_needs_memory(status)) {
  159. if (!hv_result_success(status)) {
  160. hv_status_err(status, "cpu %u apic ID: %u\n",
  161. lp_index, apic_id);
  162. ret = hv_result_to_errno(status);
  163. }
  164. break;
  165. }
  166. ret = hv_deposit_memory_node(node, hv_current_partition_id,
  167. status);
  168. } while (!ret);
  169. return ret;
  170. }
  171. int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
  172. {
  173. struct hv_create_vp *input;
  174. u64 status;
  175. unsigned long irq_flags;
  176. int ret = 0;
  177. /* Root VPs don't seem to need pages deposited */
  178. if (partition_id != hv_current_partition_id) {
  179. /* The value 90 is empirically determined. It may change. */
  180. ret = hv_call_deposit_pages(node, partition_id, 90);
  181. if (ret)
  182. return ret;
  183. }
  184. do {
  185. local_irq_save(irq_flags);
  186. input = *this_cpu_ptr(hyperv_pcpu_input_arg);
  187. input->partition_id = partition_id;
  188. input->vp_index = vp_index;
  189. input->flags = flags;
  190. input->subnode_type = HV_SUBNODE_ANY;
  191. input->proximity_domain_info = hv_numa_node_to_pxm_info(node);
  192. status = hv_do_hypercall(HVCALL_CREATE_VP, input, NULL);
  193. local_irq_restore(irq_flags);
  194. if (!hv_result_needs_memory(status)) {
  195. if (!hv_result_success(status)) {
  196. hv_status_err(status, "vcpu: %u, lp: %u\n",
  197. vp_index, flags);
  198. ret = hv_result_to_errno(status);
  199. }
  200. break;
  201. }
  202. ret = hv_deposit_memory_node(node, partition_id, status);
  203. } while (!ret);
  204. return ret;
  205. }
  206. EXPORT_SYMBOL_GPL(hv_call_create_vp);