Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 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> |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 23 | #include <linux/hardirq.h> |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 24 | #include <linux/debugfs.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 25 | |
| 26 | #include <asm/xen/hypercall.h> |
| 27 | |
| 28 | #include "multicalls.h" |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 29 | #include "debugfs.h" |
| 30 | |
| 31 | #define MC_BATCH 32 |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 32 | |
Jeremy Fitzhardinge | ffc7876 | 2010-12-17 17:19:42 -0800 | [diff] [blame^] | 33 | #define MC_DEBUG 0 |
Jeremy Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 34 | |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 35 | #define MC_ARGS (MC_BATCH * 16) |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 36 | |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 37 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 38 | struct mc_buffer { |
| 39 | struct multicall_entry entries[MC_BATCH]; |
Jeremy Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 40 | #if MC_DEBUG |
| 41 | struct multicall_entry debug[MC_BATCH]; |
Ian Campbell | b93d51d | 2009-02-06 13:35:57 -0800 | [diff] [blame] | 42 | void *caller[MC_BATCH]; |
Jeremy Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 43 | #endif |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 44 | unsigned char args[MC_ARGS]; |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 45 | struct callback { |
| 46 | void (*fn)(void *); |
| 47 | void *data; |
| 48 | } callbacks[MC_BATCH]; |
| 49 | unsigned mcidx, argidx, cbidx; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static DEFINE_PER_CPU(struct mc_buffer, mc_buffer); |
| 53 | DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags); |
| 54 | |
| 55 | void xen_mc_flush(void) |
| 56 | { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 57 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 58 | int ret = 0; |
| 59 | unsigned long flags; |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 60 | int i; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 61 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 62 | BUG_ON(preemptible()); |
| 63 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 64 | /* Disable interrupts in case someone comes in and queues |
| 65 | something in the middle */ |
| 66 | local_irq_save(flags); |
| 67 | |
Jeremy Fitzhardinge | c796f21 | 2010-12-16 14:33:27 -0800 | [diff] [blame] | 68 | trace_xen_mc_flush(b->mcidx, b->argidx, b->cbidx); |
| 69 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 70 | if (b->mcidx) { |
Jeremy Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 71 | #if MC_DEBUG |
| 72 | memcpy(b->debug, b->entries, |
| 73 | b->mcidx * sizeof(struct multicall_entry)); |
| 74 | #endif |
| 75 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 76 | if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0) |
| 77 | BUG(); |
| 78 | for (i = 0; i < b->mcidx; i++) |
| 79 | if (b->entries[i].result < 0) |
| 80 | ret++; |
Jeremy Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 81 | |
| 82 | #if MC_DEBUG |
| 83 | if (ret) { |
| 84 | printk(KERN_ERR "%d multicall(s) failed: cpu %d\n", |
| 85 | ret, smp_processor_id()); |
Jeremy Fitzhardinge | 8ba6c2b | 2008-07-08 15:06:30 -0700 | [diff] [blame] | 86 | dump_stack(); |
Paolo Ciarrocchi | 7ebed39 | 2008-02-22 23:11:59 +0100 | [diff] [blame] | 87 | for (i = 0; i < b->mcidx; i++) { |
Ian Campbell | b93d51d | 2009-02-06 13:35:57 -0800 | [diff] [blame] | 88 | printk(KERN_DEBUG " call %2d/%d: op=%lu arg=[%lx] result=%ld\t%pF\n", |
Jeremy Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 89 | i+1, b->mcidx, |
| 90 | b->debug[i].op, |
| 91 | b->debug[i].args[0], |
Ian Campbell | b93d51d | 2009-02-06 13:35:57 -0800 | [diff] [blame] | 92 | b->entries[i].result, |
| 93 | b->caller[i]); |
Jeremy Fitzhardinge | a122d62 | 2007-10-16 11:51:31 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | #endif |
| 97 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 98 | b->mcidx = 0; |
| 99 | b->argidx = 0; |
| 100 | } else |
| 101 | BUG_ON(b->argidx != 0); |
| 102 | |
Paolo Ciarrocchi | 7ebed39 | 2008-02-22 23:11:59 +0100 | [diff] [blame] | 103 | for (i = 0; i < b->cbidx; i++) { |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 104 | struct callback *cb = &b->callbacks[i]; |
| 105 | |
| 106 | (*cb->fn)(cb->data); |
| 107 | } |
| 108 | b->cbidx = 0; |
| 109 | |
Jeremy Fitzhardinge | c996086 | 2009-02-06 13:38:56 -0800 | [diff] [blame] | 110 | local_irq_restore(flags); |
| 111 | |
Jeremy Fitzhardinge | 3d39e9d | 2009-02-06 13:38:51 -0800 | [diff] [blame] | 112 | WARN_ON(ret); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | struct multicall_space __xen_mc_entry(size_t args) |
| 116 | { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 117 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 118 | struct multicall_space ret; |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 119 | unsigned argidx = roundup(b->argidx, sizeof(u64)); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 120 | |
Jeremy Fitzhardinge | c796f21 | 2010-12-16 14:33:27 -0800 | [diff] [blame] | 121 | trace_xen_mc_entry_alloc(args); |
| 122 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 123 | BUG_ON(preemptible()); |
Dan Carpenter | f124c6a | 2011-06-03 07:45:28 +0300 | [diff] [blame] | 124 | BUG_ON(b->argidx >= MC_ARGS); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 125 | |
| 126 | if (b->mcidx == MC_BATCH || |
Dan Carpenter | f124c6a | 2011-06-03 07:45:28 +0300 | [diff] [blame] | 127 | (argidx + args) >= MC_ARGS) { |
Jeremy Fitzhardinge | c796f21 | 2010-12-16 14:33:27 -0800 | [diff] [blame] | 128 | trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ? |
| 129 | XEN_MC_FL_BATCH : XEN_MC_FL_ARGS); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 130 | xen_mc_flush(); |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 131 | argidx = roundup(b->argidx, sizeof(u64)); |
| 132 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 133 | |
| 134 | ret.mc = &b->entries[b->mcidx]; |
Jeremy Fitzhardinge | ffc7876 | 2010-12-17 17:19:42 -0800 | [diff] [blame^] | 135 | #if MC_DEBUG |
Ian Campbell | b93d51d | 2009-02-06 13:35:57 -0800 | [diff] [blame] | 136 | b->caller[b->mcidx] = __builtin_return_address(0); |
| 137 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 138 | b->mcidx++; |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 139 | ret.args = &b->args[argidx]; |
| 140 | b->argidx = argidx + args; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 141 | |
Dan Carpenter | f124c6a | 2011-06-03 07:45:28 +0300 | [diff] [blame] | 142 | BUG_ON(b->argidx >= MC_ARGS); |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | struct multicall_space xen_mc_extend_args(unsigned long op, size_t size) |
| 147 | { |
| 148 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
| 149 | struct multicall_space ret = { NULL, NULL }; |
| 150 | |
| 151 | BUG_ON(preemptible()); |
Dan Carpenter | f124c6a | 2011-06-03 07:45:28 +0300 | [diff] [blame] | 152 | BUG_ON(b->argidx >= MC_ARGS); |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 153 | |
Jeremy Fitzhardinge | c796f21 | 2010-12-16 14:33:27 -0800 | [diff] [blame] | 154 | if (unlikely(b->mcidx == 0 || |
| 155 | b->entries[b->mcidx - 1].op != op)) { |
| 156 | trace_xen_mc_extend_args(op, size, XEN_MC_XE_BAD_OP); |
| 157 | goto out; |
| 158 | } |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 159 | |
Jeremy Fitzhardinge | c796f21 | 2010-12-16 14:33:27 -0800 | [diff] [blame] | 160 | if (unlikely((b->argidx + size) >= MC_ARGS)) { |
| 161 | trace_xen_mc_extend_args(op, size, XEN_MC_XE_NO_SPACE); |
| 162 | goto out; |
| 163 | } |
Jeremy Fitzhardinge | 400d349 | 2008-06-16 04:30:03 -0700 | [diff] [blame] | 164 | |
| 165 | ret.mc = &b->entries[b->mcidx - 1]; |
| 166 | ret.args = &b->args[b->argidx]; |
| 167 | b->argidx += size; |
| 168 | |
Dan Carpenter | f124c6a | 2011-06-03 07:45:28 +0300 | [diff] [blame] | 169 | BUG_ON(b->argidx >= MC_ARGS); |
Jeremy Fitzhardinge | c796f21 | 2010-12-16 14:33:27 -0800 | [diff] [blame] | 170 | |
| 171 | trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK); |
| 172 | out: |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 173 | return ret; |
| 174 | } |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 175 | |
| 176 | void xen_mc_callback(void (*fn)(void *), void *data) |
| 177 | { |
| 178 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
| 179 | struct callback *cb; |
| 180 | |
Jeremy Fitzhardinge | c796f21 | 2010-12-16 14:33:27 -0800 | [diff] [blame] | 181 | if (b->cbidx == MC_BATCH) { |
| 182 | trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK); |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 183 | xen_mc_flush(); |
Jeremy Fitzhardinge | c796f21 | 2010-12-16 14:33:27 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | trace_xen_mc_callback(fn, data); |
Jeremy Fitzhardinge | 91e0c5f | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 187 | |
| 188 | cb = &b->callbacks[b->cbidx++]; |
| 189 | cb->fn = fn; |
| 190 | cb->data = data; |
| 191 | } |