multicalls.c 4.8 KB

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