multicalls.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xen hypercall batching.
  4. *
  5. * Xen allows multiple hypercalls to be issued at once, using the
  6. * multicall interface. This allows the cost of trapping into the
  7. * hypervisor to be amortized over several calls.
  8. *
  9. * This file implements a simple interface for multicalls. There's a
  10. * per-cpu buffer of outstanding multicalls. When you want to queue a
  11. * multicall for issuing, you can allocate a multicall slot for the
  12. * call and its arguments, along with storage for space which is
  13. * pointed to by the arguments (for passing pointers to structures,
  14. * etc). When the multicall is actually issued, all the space for the
  15. * commands and allocated memory is freed for reuse.
  16. *
  17. * Multicalls are flushed whenever any of the buffers get full, or
  18. * when explicitly requested. There's no way to get per-multicall
  19. * return results back. It will BUG if any of the multicalls fail.
  20. *
  21. * Jeremy Fitzhardinge <[email protected]>, XenSource Inc, 2007
  22. */
  23. #include <linux/percpu.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/debugfs.h>
  26. #include <asm/xen/hypercall.h>
  27. #include "multicalls.h"
  28. #include "debugfs.h"
  29. #define MC_BATCH 32
  30. #define MC_DEBUG 0
  31. #define MC_ARGS (MC_BATCH * 16)
  32. struct mc_buffer {
  33. unsigned mcidx, argidx, cbidx;
  34. struct multicall_entry entries[MC_BATCH];
  35. #if MC_DEBUG
  36. struct multicall_entry debug[MC_BATCH];
  37. void *caller[MC_BATCH];
  38. #endif
  39. unsigned char args[MC_ARGS];
  40. struct callback {
  41. void (*fn)(void *);
  42. void *data;
  43. } callbacks[MC_BATCH];
  44. };
  45. static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
  46. DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);
  47. void xen_mc_flush(void)
  48. {
  49. struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
  50. struct multicall_entry *mc;
  51. int ret = 0;
  52. unsigned long flags;
  53. int i;
  54. BUG_ON(preemptible());
  55. /* Disable interrupts in case someone comes in and queues
  56. something in the middle */
  57. local_irq_save(flags);
  58. trace_xen_mc_flush(b->mcidx, b->argidx, b->cbidx);
  59. #if MC_DEBUG
  60. memcpy(b->debug, b->entries,
  61. b->mcidx * sizeof(struct multicall_entry));
  62. #endif
  63. switch (b->mcidx) {
  64. case 0:
  65. /* no-op */
  66. BUG_ON(b->argidx != 0);
  67. break;
  68. case 1:
  69. /* Singleton multicall - bypass multicall machinery
  70. and just do the call directly. */
  71. mc = &b->entries[0];
  72. mc->result = xen_single_call(mc->op, mc->args[0], mc->args[1],
  73. mc->args[2], mc->args[3],
  74. mc->args[4]);
  75. ret = mc->result < 0;
  76. break;
  77. default:
  78. if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
  79. BUG();
  80. for (i = 0; i < b->mcidx; i++)
  81. if (b->entries[i].result < 0)
  82. ret++;
  83. }
  84. if (WARN_ON(ret)) {
  85. pr_err("%d of %d multicall(s) failed: cpu %d\n",
  86. ret, b->mcidx, smp_processor_id());
  87. for (i = 0; i < b->mcidx; i++) {
  88. if (b->entries[i].result < 0) {
  89. #if MC_DEBUG
  90. pr_err(" call %2d: op=%lu arg=[%lx] result=%ld\t%pS\n",
  91. i + 1,
  92. b->debug[i].op,
  93. b->debug[i].args[0],
  94. b->entries[i].result,
  95. b->caller[i]);
  96. #else
  97. pr_err(" call %2d: op=%lu arg=[%lx] result=%ld\n",
  98. i + 1,
  99. b->entries[i].op,
  100. b->entries[i].args[0],
  101. b->entries[i].result);
  102. #endif
  103. }
  104. }
  105. }
  106. b->mcidx = 0;
  107. b->argidx = 0;
  108. for (i = 0; i < b->cbidx; i++) {
  109. struct callback *cb = &b->callbacks[i];
  110. (*cb->fn)(cb->data);
  111. }
  112. b->cbidx = 0;
  113. local_irq_restore(flags);
  114. }
  115. struct multicall_space __xen_mc_entry(size_t args)
  116. {
  117. struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
  118. struct multicall_space ret;
  119. unsigned argidx = roundup(b->argidx, sizeof(u64));
  120. trace_xen_mc_entry_alloc(args);
  121. BUG_ON(preemptible());
  122. BUG_ON(b->argidx >= MC_ARGS);
  123. if (unlikely(b->mcidx == MC_BATCH ||
  124. (argidx + args) >= MC_ARGS)) {
  125. trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ?
  126. XEN_MC_FL_BATCH : XEN_MC_FL_ARGS);
  127. xen_mc_flush();
  128. argidx = roundup(b->argidx, sizeof(u64));
  129. }
  130. ret.mc = &b->entries[b->mcidx];
  131. #if MC_DEBUG
  132. b->caller[b->mcidx] = __builtin_return_address(0);
  133. #endif
  134. b->mcidx++;
  135. ret.args = &b->args[argidx];
  136. b->argidx = argidx + args;
  137. BUG_ON(b->argidx >= MC_ARGS);
  138. return ret;
  139. }
  140. struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
  141. {
  142. struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
  143. struct multicall_space ret = { NULL, NULL };
  144. BUG_ON(preemptible());
  145. BUG_ON(b->argidx >= MC_ARGS);
  146. if (unlikely(b->mcidx == 0 ||
  147. b->entries[b->mcidx - 1].op != op)) {
  148. trace_xen_mc_extend_args(op, size, XEN_MC_XE_BAD_OP);
  149. goto out;
  150. }
  151. if (unlikely((b->argidx + size) >= MC_ARGS)) {
  152. trace_xen_mc_extend_args(op, size, XEN_MC_XE_NO_SPACE);
  153. goto out;
  154. }
  155. ret.mc = &b->entries[b->mcidx - 1];
  156. ret.args = &b->args[b->argidx];
  157. b->argidx += size;
  158. BUG_ON(b->argidx >= MC_ARGS);
  159. trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK);
  160. out:
  161. return ret;
  162. }
  163. void xen_mc_callback(void (*fn)(void *), void *data)
  164. {
  165. struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
  166. struct callback *cb;
  167. if (b->cbidx == MC_BATCH) {
  168. trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK);
  169. xen_mc_flush();
  170. }
  171. trace_xen_mc_callback(fn, data);
  172. cb = &b->callbacks[b->cbidx++];
  173. cb->fn = fn;
  174. cb->data = data;
  175. }