multicalls.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <jeremy@xensource.com>, 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. switch (b->mcidx) {
  60. case 0:
  61. /* no-op */
  62. BUG_ON(b->argidx != 0);
  63. break;
  64. case 1:
  65. /* Singleton multicall - bypass multicall machinery
  66. and just do the call directly. */
  67. mc = &b->entries[0];
  68. mc->result = xen_single_call(mc->op, mc->args[0], mc->args[1],
  69. mc->args[2], mc->args[3],
  70. mc->args[4]);
  71. ret = mc->result < 0;
  72. break;
  73. default:
  74. #if MC_DEBUG
  75. memcpy(b->debug, b->entries,
  76. b->mcidx * sizeof(struct multicall_entry));
  77. #endif
  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. #if MC_DEBUG
  84. if (ret) {
  85. printk(KERN_ERR "%d multicall(s) failed: cpu %d\n",
  86. ret, smp_processor_id());
  87. dump_stack();
  88. for (i = 0; i < b->mcidx; i++) {
  89. printk(KERN_DEBUG " call %2d/%d: op=%lu arg=[%lx] result=%ld\t%pF\n",
  90. i+1, b->mcidx,
  91. b->debug[i].op,
  92. b->debug[i].args[0],
  93. b->entries[i].result,
  94. b->caller[i]);
  95. }
  96. }
  97. #endif
  98. }
  99. b->mcidx = 0;
  100. b->argidx = 0;
  101. for (i = 0; i < b->cbidx; i++) {
  102. struct callback *cb = &b->callbacks[i];
  103. (*cb->fn)(cb->data);
  104. }
  105. b->cbidx = 0;
  106. local_irq_restore(flags);
  107. WARN_ON(ret);
  108. }
  109. struct multicall_space __xen_mc_entry(size_t args)
  110. {
  111. struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
  112. struct multicall_space ret;
  113. unsigned argidx = roundup(b->argidx, sizeof(u64));
  114. trace_xen_mc_entry_alloc(args);
  115. BUG_ON(preemptible());
  116. BUG_ON(b->argidx >= MC_ARGS);
  117. if (unlikely(b->mcidx == MC_BATCH ||
  118. (argidx + args) >= MC_ARGS)) {
  119. trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ?
  120. XEN_MC_FL_BATCH : XEN_MC_FL_ARGS);
  121. xen_mc_flush();
  122. argidx = roundup(b->argidx, sizeof(u64));
  123. }
  124. ret.mc = &b->entries[b->mcidx];
  125. #if MC_DEBUG
  126. b->caller[b->mcidx] = __builtin_return_address(0);
  127. #endif
  128. b->mcidx++;
  129. ret.args = &b->args[argidx];
  130. b->argidx = argidx + args;
  131. BUG_ON(b->argidx >= MC_ARGS);
  132. return ret;
  133. }
  134. struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
  135. {
  136. struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
  137. struct multicall_space ret = { NULL, NULL };
  138. BUG_ON(preemptible());
  139. BUG_ON(b->argidx >= MC_ARGS);
  140. if (unlikely(b->mcidx == 0 ||
  141. b->entries[b->mcidx - 1].op != op)) {
  142. trace_xen_mc_extend_args(op, size, XEN_MC_XE_BAD_OP);
  143. goto out;
  144. }
  145. if (unlikely((b->argidx + size) >= MC_ARGS)) {
  146. trace_xen_mc_extend_args(op, size, XEN_MC_XE_NO_SPACE);
  147. goto out;
  148. }
  149. ret.mc = &b->entries[b->mcidx - 1];
  150. ret.args = &b->args[b->argidx];
  151. b->argidx += size;
  152. BUG_ON(b->argidx >= MC_ARGS);
  153. trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK);
  154. out:
  155. return ret;
  156. }
  157. void xen_mc_callback(void (*fn)(void *), void *data)
  158. {
  159. struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
  160. struct callback *cb;
  161. if (b->cbidx == MC_BATCH) {
  162. trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK);
  163. xen_mc_flush();
  164. }
  165. trace_xen_mc_callback(fn, data);
  166. cb = &b->callbacks[b->cbidx++];
  167. cb->fn = fn;
  168. cb->data = data;
  169. }