multicalls.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <asm/xen/hypercall.h>
  25. #include "multicalls.h"
  26. #define MC_DEBUG 1
  27. #define MC_BATCH 32
  28. #define MC_ARGS (MC_BATCH * 16)
  29. struct mc_buffer {
  30. struct multicall_entry entries[MC_BATCH];
  31. #if MC_DEBUG
  32. struct multicall_entry debug[MC_BATCH];
  33. #endif
  34. unsigned char args[MC_ARGS];
  35. struct callback {
  36. void (*fn)(void *);
  37. void *data;
  38. } callbacks[MC_BATCH];
  39. unsigned mcidx, argidx, cbidx;
  40. };
  41. static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
  42. DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);
  43. void xen_mc_flush(void)
  44. {
  45. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  46. int ret = 0;
  47. unsigned long flags;
  48. int i;
  49. BUG_ON(preemptible());
  50. /* Disable interrupts in case someone comes in and queues
  51. something in the middle */
  52. local_irq_save(flags);
  53. if (b->mcidx) {
  54. #if MC_DEBUG
  55. memcpy(b->debug, b->entries,
  56. b->mcidx * sizeof(struct multicall_entry));
  57. #endif
  58. if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
  59. BUG();
  60. for (i = 0; i < b->mcidx; i++)
  61. if (b->entries[i].result < 0)
  62. ret++;
  63. #if MC_DEBUG
  64. if (ret) {
  65. printk(KERN_ERR "%d multicall(s) failed: cpu %d\n",
  66. ret, smp_processor_id());
  67. dump_stack();
  68. for (i = 0; i < b->mcidx; i++) {
  69. printk(" call %2d/%d: op=%lu arg=[%lx] result=%ld\n",
  70. i+1, b->mcidx,
  71. b->debug[i].op,
  72. b->debug[i].args[0],
  73. b->entries[i].result);
  74. }
  75. }
  76. #endif
  77. b->mcidx = 0;
  78. b->argidx = 0;
  79. } else
  80. BUG_ON(b->argidx != 0);
  81. local_irq_restore(flags);
  82. for (i = 0; i < b->cbidx; i++) {
  83. struct callback *cb = &b->callbacks[i];
  84. (*cb->fn)(cb->data);
  85. }
  86. b->cbidx = 0;
  87. BUG_ON(ret);
  88. }
  89. struct multicall_space __xen_mc_entry(size_t args)
  90. {
  91. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  92. struct multicall_space ret;
  93. unsigned argidx = roundup(b->argidx, sizeof(u64));
  94. BUG_ON(preemptible());
  95. BUG_ON(b->argidx > MC_ARGS);
  96. if (b->mcidx == MC_BATCH ||
  97. (argidx + args) > MC_ARGS) {
  98. xen_mc_flush();
  99. argidx = roundup(b->argidx, sizeof(u64));
  100. }
  101. ret.mc = &b->entries[b->mcidx];
  102. b->mcidx++;
  103. ret.args = &b->args[argidx];
  104. b->argidx = argidx + args;
  105. BUG_ON(b->argidx > MC_ARGS);
  106. return ret;
  107. }
  108. struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
  109. {
  110. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  111. struct multicall_space ret = { NULL, NULL };
  112. BUG_ON(preemptible());
  113. BUG_ON(b->argidx > MC_ARGS);
  114. if (b->mcidx == 0)
  115. return ret;
  116. if (b->entries[b->mcidx - 1].op != op)
  117. return ret;
  118. if ((b->argidx + size) > MC_ARGS)
  119. return ret;
  120. ret.mc = &b->entries[b->mcidx - 1];
  121. ret.args = &b->args[b->argidx];
  122. b->argidx += size;
  123. BUG_ON(b->argidx > MC_ARGS);
  124. return ret;
  125. }
  126. void xen_mc_callback(void (*fn)(void *), void *data)
  127. {
  128. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  129. struct callback *cb;
  130. if (b->cbidx == MC_BATCH)
  131. xen_mc_flush();
  132. cb = &b->callbacks[b->cbidx++];
  133. cb->fn = fn;
  134. cb->data = data;
  135. }