Commit | Line | Data |
---|---|---|
5ead97c8 JF |
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> | |
f120f13e | 23 | #include <linux/hardirq.h> |
994025ca | 24 | #include <linux/debugfs.h> |
5ead97c8 JF |
25 | |
26 | #include <asm/xen/hypercall.h> | |
27 | ||
28 | #include "multicalls.h" | |
994025ca JF |
29 | #include "debugfs.h" |
30 | ||
31 | #define MC_BATCH 32 | |
5ead97c8 | 32 | |
ffc78767 | 33 | #define MC_DEBUG 0 |
a122d623 | 34 | |
400d3494 | 35 | #define MC_ARGS (MC_BATCH * 16) |
5ead97c8 | 36 | |
994025ca | 37 | |
5ead97c8 JF |
38 | struct mc_buffer { |
39 | struct multicall_entry entries[MC_BATCH]; | |
a122d623 JF |
40 | #if MC_DEBUG |
41 | struct multicall_entry debug[MC_BATCH]; | |
b93d51dc | 42 | void *caller[MC_BATCH]; |
a122d623 | 43 | #endif |
400d3494 | 44 | unsigned char args[MC_ARGS]; |
91e0c5f3 JF |
45 | struct callback { |
46 | void (*fn)(void *); | |
47 | void *data; | |
48 | } callbacks[MC_BATCH]; | |
49 | unsigned mcidx, argidx, cbidx; | |
5ead97c8 JF |
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 | { | |
f120f13e | 57 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
5ead97c8 JF |
58 | int ret = 0; |
59 | unsigned long flags; | |
91e0c5f3 | 60 | int i; |
5ead97c8 | 61 | |
f120f13e JF |
62 | BUG_ON(preemptible()); |
63 | ||
5ead97c8 JF |
64 | /* Disable interrupts in case someone comes in and queues |
65 | something in the middle */ | |
66 | local_irq_save(flags); | |
67 | ||
c796f213 JF |
68 | trace_xen_mc_flush(b->mcidx, b->argidx, b->cbidx); |
69 | ||
5ead97c8 | 70 | if (b->mcidx) { |
a122d623 JF |
71 | #if MC_DEBUG |
72 | memcpy(b->debug, b->entries, | |
73 | b->mcidx * sizeof(struct multicall_entry)); | |
74 | #endif | |
75 | ||
5ead97c8 JF |
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++; | |
a122d623 JF |
81 | |
82 | #if MC_DEBUG | |
83 | if (ret) { | |
84 | printk(KERN_ERR "%d multicall(s) failed: cpu %d\n", | |
85 | ret, smp_processor_id()); | |
8ba6c2b0 | 86 | dump_stack(); |
7ebed39f | 87 | for (i = 0; i < b->mcidx; i++) { |
b93d51dc | 88 | printk(KERN_DEBUG " call %2d/%d: op=%lu arg=[%lx] result=%ld\t%pF\n", |
a122d623 JF |
89 | i+1, b->mcidx, |
90 | b->debug[i].op, | |
91 | b->debug[i].args[0], | |
b93d51dc IC |
92 | b->entries[i].result, |
93 | b->caller[i]); | |
a122d623 JF |
94 | } |
95 | } | |
96 | #endif | |
97 | ||
5ead97c8 JF |
98 | b->mcidx = 0; |
99 | b->argidx = 0; | |
100 | } else | |
101 | BUG_ON(b->argidx != 0); | |
102 | ||
7ebed39f | 103 | for (i = 0; i < b->cbidx; i++) { |
91e0c5f3 JF |
104 | struct callback *cb = &b->callbacks[i]; |
105 | ||
106 | (*cb->fn)(cb->data); | |
107 | } | |
108 | b->cbidx = 0; | |
109 | ||
c9960863 JF |
110 | local_irq_restore(flags); |
111 | ||
3d39e9d0 | 112 | WARN_ON(ret); |
5ead97c8 JF |
113 | } |
114 | ||
115 | struct multicall_space __xen_mc_entry(size_t args) | |
116 | { | |
f120f13e | 117 | struct mc_buffer *b = &__get_cpu_var(mc_buffer); |
5ead97c8 | 118 | struct multicall_space ret; |
400d3494 | 119 | unsigned argidx = roundup(b->argidx, sizeof(u64)); |
5ead97c8 | 120 | |
c796f213 JF |
121 | trace_xen_mc_entry_alloc(args); |
122 | ||
f120f13e | 123 | BUG_ON(preemptible()); |
f124c6ae | 124 | BUG_ON(b->argidx >= MC_ARGS); |
5ead97c8 | 125 | |
4a7b005d JF |
126 | if (unlikely(b->mcidx == MC_BATCH || |
127 | (argidx + args) >= MC_ARGS)) { | |
c796f213 JF |
128 | trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ? |
129 | XEN_MC_FL_BATCH : XEN_MC_FL_ARGS); | |
5ead97c8 | 130 | xen_mc_flush(); |
400d3494 JF |
131 | argidx = roundup(b->argidx, sizeof(u64)); |
132 | } | |
5ead97c8 JF |
133 | |
134 | ret.mc = &b->entries[b->mcidx]; | |
ffc78767 | 135 | #if MC_DEBUG |
b93d51dc IC |
136 | b->caller[b->mcidx] = __builtin_return_address(0); |
137 | #endif | |
5ead97c8 | 138 | b->mcidx++; |
400d3494 JF |
139 | ret.args = &b->args[argidx]; |
140 | b->argidx = argidx + args; | |
141 | ||
f124c6ae | 142 | BUG_ON(b->argidx >= MC_ARGS); |
400d3494 JF |
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()); | |
f124c6ae | 152 | BUG_ON(b->argidx >= MC_ARGS); |
400d3494 | 153 | |
c796f213 JF |
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 | } | |
400d3494 | 159 | |
c796f213 JF |
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 | } | |
400d3494 JF |
164 | |
165 | ret.mc = &b->entries[b->mcidx - 1]; | |
5ead97c8 | 166 | ret.args = &b->args[b->argidx]; |
400d3494 | 167 | b->argidx += size; |
5ead97c8 | 168 | |
f124c6ae | 169 | BUG_ON(b->argidx >= MC_ARGS); |
c796f213 JF |
170 | |
171 | trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK); | |
172 | out: | |
5ead97c8 JF |
173 | return ret; |
174 | } | |
91e0c5f3 JF |
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 | ||
c796f213 JF |
181 | if (b->cbidx == MC_BATCH) { |
182 | trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK); | |
91e0c5f3 | 183 | xen_mc_flush(); |
c796f213 JF |
184 | } |
185 | ||
186 | trace_xen_mc_callback(fn, data); | |
91e0c5f3 JF |
187 | |
188 | cb = &b->callbacks[b->cbidx++]; | |
189 | cb->fn = fn; | |
190 | cb->data = data; | |
191 | } |