hv_proc.c 4.8 KB

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