multicalls.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_BATCH 32
  27. #define MC_ARGS (MC_BATCH * 16 / sizeof(u64))
  28. struct mc_buffer {
  29. struct multicall_entry entries[MC_BATCH];
  30. u64 args[MC_ARGS];
  31. unsigned mcidx, argidx;
  32. };
  33. static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
  34. DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);
  35. void xen_mc_flush(void)
  36. {
  37. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  38. int ret = 0;
  39. unsigned long flags;
  40. BUG_ON(preemptible());
  41. /* Disable interrupts in case someone comes in and queues
  42. something in the middle */
  43. local_irq_save(flags);
  44. if (b->mcidx) {
  45. int i;
  46. if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
  47. BUG();
  48. for (i = 0; i < b->mcidx; i++)
  49. if (b->entries[i].result < 0)
  50. ret++;
  51. b->mcidx = 0;
  52. b->argidx = 0;
  53. } else
  54. BUG_ON(b->argidx != 0);
  55. local_irq_restore(flags);
  56. BUG_ON(ret);
  57. }
  58. struct multicall_space __xen_mc_entry(size_t args)
  59. {
  60. struct mc_buffer *b = &__get_cpu_var(mc_buffer);
  61. struct multicall_space ret;
  62. unsigned argspace = (args + sizeof(u64) - 1) / sizeof(u64);
  63. BUG_ON(preemptible());
  64. BUG_ON(argspace > MC_ARGS);
  65. if (b->mcidx == MC_BATCH ||
  66. (b->argidx + argspace) > MC_ARGS)
  67. xen_mc_flush();
  68. ret.mc = &b->entries[b->mcidx];
  69. b->mcidx++;
  70. ret.args = &b->args[b->argidx];
  71. b->argidx += argspace;
  72. return ret;
  73. }