multicalls.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 1
  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. if (b->mcidx) {
  57. #if MC_DEBUG
  58. memcpy(b->debug, b->entries,
  59. b->mcidx * sizeof(struct multicall_entry));
  60. #endif
  61. if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
  62. BUG();
  63. for (i = 0; i < b->mcidx; i++)
  64. if (b->entries[i].result < 0)
  65. ret++;
  66. #if MC_DEBUG
  67. if (ret) {
  68. printk(KERN_ERR "%d multicall(s) failed: cpu %d\n",
  69. ret, smp_processor_id());
  70. dump_stack();
  71. for (i = 0; i < b->mcidx; i++) {
  72. printk(KERN_DEBUG " call %2d/%d: op=%lu arg=[%lx] result=%ld\t%pF\n",
  73. i+1, b->mcidx,
  74. b->debug[i].op,
  75. b->debug[i].args[0],
  76. b->entries[i].result,
  77. b->caller[i]);
  78. }
  79. }
  80. #endif
  81. b->mcidx = 0;
  82. b->argidx = 0;
  83. } else
  84. BUG_ON(b->argidx != 0);
  85. for (i = 0; i < b->cbidx; i++) {
  86. struct callback *cb = &b->callbacks[i];
  87. (*cb->fn)(cb->data);
  88. }
  89. b->cbidx = 0;
  90. local_irq_restore(flags);
  91. WARN_ON(ret);
  92. }
  93. struct multicall_space __xen_mc_entry(size_t args)
  94. {
  95. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  96. struct multicall_space ret;
  97. unsigned argidx = roundup(b->argidx, sizeof(u64));
  98. BUG_ON(preemptible());
  99. BUG_ON(b->argidx >= MC_ARGS);
  100. if (b->mcidx == MC_BATCH ||
  101. (argidx + args) >= MC_ARGS) {
  102. xen_mc_flush();
  103. argidx = roundup(b->argidx, sizeof(u64));
  104. }
  105. ret.mc = &b->entries[b->mcidx];
  106. #ifdef MC_DEBUG
  107. b->caller[b->mcidx] = __builtin_return_address(0);
  108. #endif
  109. b->mcidx++;
  110. ret.args = &b->args[argidx];
  111. b->argidx = argidx + args;
  112. BUG_ON(b->argidx >= MC_ARGS);
  113. return ret;
  114. }
  115. struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
  116. {
  117. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  118. struct multicall_space ret = { NULL, NULL };
  119. BUG_ON(preemptible());
  120. BUG_ON(b->argidx >= MC_ARGS);
  121. if (b->mcidx == 0)
  122. return ret;
  123. if (b->entries[b->mcidx - 1].op != op)
  124. return ret;
  125. if ((b->argidx + size) >= MC_ARGS)
  126. return ret;
  127. ret.mc = &b->entries[b->mcidx - 1];
  128. ret.args = &b->args[b->argidx];
  129. b->argidx += size;
  130. BUG_ON(b->argidx >= MC_ARGS);
  131. return ret;
  132. }
  133. void xen_mc_callback(void (*fn)(void *), void *data)
  134. {
  135. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  136. struct callback *cb;
  137. if (b->cbidx == MC_BATCH)
  138. xen_mc_flush();
  139. cb = &b->callbacks[b->cbidx++];
  140. cb->fn = fn;
  141. cb->data = data;
  142. }