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