multicalls.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. struct multicall_entry entries[MC_BATCH];
  33. #if MC_DEBUG
  34. struct multicall_entry debug[MC_BATCH];
  35. void *caller[MC_BATCH];
  36. #endif
  37. unsigned char args[MC_ARGS];
  38. struct callback {
  39. void (*fn)(void *);
  40. void *data;
  41. } callbacks[MC_BATCH];
  42. unsigned mcidx, argidx, cbidx;
  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 = &__get_cpu_var(mc_buffer);
  49. int ret = 0;
  50. unsigned long flags;
  51. int i;
  52. BUG_ON(preemptible());
  53. /* Disable interrupts in case someone comes in and queues
  54. something in the middle */
  55. local_irq_save(flags);
  56. trace_xen_mc_flush(b->mcidx, b->argidx, b->cbidx);
  57. if (b->mcidx) {
  58. #if MC_DEBUG
  59. memcpy(b->debug, b->entries,
  60. b->mcidx * sizeof(struct multicall_entry));
  61. #endif
  62. if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
  63. BUG();
  64. for (i = 0; i < b->mcidx; i++)
  65. if (b->entries[i].result < 0)
  66. ret++;
  67. #if MC_DEBUG
  68. if (ret) {
  69. printk(KERN_ERR "%d multicall(s) failed: cpu %d\n",
  70. ret, smp_processor_id());
  71. dump_stack();
  72. for (i = 0; i < b->mcidx; i++) {
  73. printk(KERN_DEBUG " call %2d/%d: op=%lu arg=[%lx] result=%ld\t%pF\n",
  74. i+1, b->mcidx,
  75. b->debug[i].op,
  76. b->debug[i].args[0],
  77. b->entries[i].result,
  78. b->caller[i]);
  79. }
  80. }
  81. #endif
  82. b->mcidx = 0;
  83. b->argidx = 0;
  84. } else
  85. BUG_ON(b->argidx != 0);
  86. for (i = 0; i < b->cbidx; i++) {
  87. struct callback *cb = &b->callbacks[i];
  88. (*cb->fn)(cb->data);
  89. }
  90. b->cbidx = 0;
  91. local_irq_restore(flags);
  92. WARN_ON(ret);
  93. }
  94. struct multicall_space __xen_mc_entry(size_t args)
  95. {
  96. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  97. struct multicall_space ret;
  98. unsigned argidx = roundup(b->argidx, sizeof(u64));
  99. trace_xen_mc_entry_alloc(args);
  100. BUG_ON(preemptible());
  101. BUG_ON(b->argidx >= MC_ARGS);
  102. if (b->mcidx == MC_BATCH ||
  103. (argidx + args) >= MC_ARGS) {
  104. trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ?
  105. XEN_MC_FL_BATCH : XEN_MC_FL_ARGS);
  106. xen_mc_flush();
  107. argidx = roundup(b->argidx, sizeof(u64));
  108. }
  109. ret.mc = &b->entries[b->mcidx];
  110. #if MC_DEBUG
  111. b->caller[b->mcidx] = __builtin_return_address(0);
  112. #endif
  113. b->mcidx++;
  114. ret.args = &b->args[argidx];
  115. b->argidx = argidx + args;
  116. BUG_ON(b->argidx >= MC_ARGS);
  117. return ret;
  118. }
  119. struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
  120. {
  121. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  122. struct multicall_space ret = { NULL, NULL };
  123. BUG_ON(preemptible());
  124. BUG_ON(b->argidx >= MC_ARGS);
  125. if (unlikely(b->mcidx == 0 ||
  126. b->entries[b->mcidx - 1].op != op)) {
  127. trace_xen_mc_extend_args(op, size, XEN_MC_XE_BAD_OP);
  128. goto out;
  129. }
  130. if (unlikely((b->argidx + size) >= MC_ARGS)) {
  131. trace_xen_mc_extend_args(op, size, XEN_MC_XE_NO_SPACE);
  132. goto out;
  133. }
  134. ret.mc = &b->entries[b->mcidx - 1];
  135. ret.args = &b->args[b->argidx];
  136. b->argidx += size;
  137. BUG_ON(b->argidx >= MC_ARGS);
  138. trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK);
  139. out:
  140. return ret;
  141. }
  142. void xen_mc_callback(void (*fn)(void *), void *data)
  143. {
  144. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  145. struct callback *cb;
  146. if (b->cbidx == MC_BATCH) {
  147. trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK);
  148. xen_mc_flush();
  149. }
  150. trace_xen_mc_callback(fn, data);
  151. cb = &b->callbacks[b->cbidx++];
  152. cb->fn = fn;
  153. cb->data = data;
  154. }