multicalls.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 / sizeof(u64))
  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. u64 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. for(i = 0; i < b->mcidx; i++) {
  68. printk(" call %2d/%d: op=%lu arg=[%lx] result=%ld\n",
  69. i+1, b->mcidx,
  70. b->debug[i].op,
  71. b->debug[i].args[0],
  72. b->entries[i].result);
  73. }
  74. }
  75. #endif
  76. b->mcidx = 0;
  77. b->argidx = 0;
  78. } else
  79. BUG_ON(b->argidx != 0);
  80. local_irq_restore(flags);
  81. for(i = 0; i < b->cbidx; i++) {
  82. struct callback *cb = &b->callbacks[i];
  83. (*cb->fn)(cb->data);
  84. }
  85. b->cbidx = 0;
  86. BUG_ON(ret);
  87. }
  88. struct multicall_space __xen_mc_entry(size_t args)
  89. {
  90. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  91. struct multicall_space ret;
  92. unsigned argspace = (args + sizeof(u64) - 1) / sizeof(u64);
  93. BUG_ON(preemptible());
  94. BUG_ON(argspace > MC_ARGS);
  95. if (b->mcidx == MC_BATCH ||
  96. (b->argidx + argspace) > MC_ARGS)
  97. xen_mc_flush();
  98. ret.mc = &b->entries[b->mcidx];
  99. b->mcidx++;
  100. ret.args = &b->args[b->argidx];
  101. b->argidx += argspace;
  102. return ret;
  103. }
  104. void xen_mc_callback(void (*fn)(void *), void *data)
  105. {
  106. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  107. struct callback *cb;
  108. if (b->cbidx == MC_BATCH)
  109. xen_mc_flush();
  110. cb = &b->callbacks[b->cbidx++];
  111. cb->fn = fn;
  112. cb->data = data;
  113. }