Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney...
[linux-2.6-block.git] / arch / x86 / xen / multicalls.c
CommitLineData
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 38struct mc_buffer {
2a6f6d09 39 unsigned mcidx, argidx, cbidx;
5ead97c8 40 struct multicall_entry entries[MC_BATCH];
a122d623
JF
41#if MC_DEBUG
42 struct multicall_entry debug[MC_BATCH];
b93d51dc 43 void *caller[MC_BATCH];
a122d623 44#endif
400d3494 45 unsigned char args[MC_ARGS];
91e0c5f3
JF
46 struct callback {
47 void (*fn)(void *);
48 void *data;
49 } callbacks[MC_BATCH];
5ead97c8
JF
50};
51
52static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
53DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);
54
55void xen_mc_flush(void)
56{
89cbc767 57 struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
eac303bf 58 struct multicall_entry *mc;
5ead97c8
JF
59 int ret = 0;
60 unsigned long flags;
91e0c5f3 61 int i;
5ead97c8 62
f120f13e
JF
63 BUG_ON(preemptible());
64
5ead97c8
JF
65 /* Disable interrupts in case someone comes in and queues
66 something in the middle */
67 local_irq_save(flags);
68
c796f213
JF
69 trace_xen_mc_flush(b->mcidx, b->argidx, b->cbidx);
70
eac303bf
JF
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:
a122d623
JF
89#if MC_DEBUG
90 memcpy(b->debug, b->entries,
91 b->mcidx * sizeof(struct multicall_entry));
92#endif
93
5ead97c8
JF
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++;
a122d623
JF
99
100#if MC_DEBUG
101 if (ret) {
102 printk(KERN_ERR "%d multicall(s) failed: cpu %d\n",
103 ret, smp_processor_id());
8ba6c2b0 104 dump_stack();
7ebed39f 105 for (i = 0; i < b->mcidx; i++) {
b93d51dc 106 printk(KERN_DEBUG " call %2d/%d: op=%lu arg=[%lx] result=%ld\t%pF\n",
a122d623
JF
107 i+1, b->mcidx,
108 b->debug[i].op,
109 b->debug[i].args[0],
b93d51dc
IC
110 b->entries[i].result,
111 b->caller[i]);
a122d623
JF
112 }
113 }
114#endif
eac303bf 115 }
a122d623 116
eac303bf
JF
117 b->mcidx = 0;
118 b->argidx = 0;
5ead97c8 119
7ebed39f 120 for (i = 0; i < b->cbidx; i++) {
91e0c5f3
JF
121 struct callback *cb = &b->callbacks[i];
122
123 (*cb->fn)(cb->data);
124 }
125 b->cbidx = 0;
126
c9960863
JF
127 local_irq_restore(flags);
128
3d39e9d0 129 WARN_ON(ret);
5ead97c8
JF
130}
131
132struct multicall_space __xen_mc_entry(size_t args)
133{
89cbc767 134 struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
5ead97c8 135 struct multicall_space ret;
400d3494 136 unsigned argidx = roundup(b->argidx, sizeof(u64));
5ead97c8 137
c796f213
JF
138 trace_xen_mc_entry_alloc(args);
139
f120f13e 140 BUG_ON(preemptible());
f124c6ae 141 BUG_ON(b->argidx >= MC_ARGS);
5ead97c8 142
4a7b005d
JF
143 if (unlikely(b->mcidx == MC_BATCH ||
144 (argidx + args) >= MC_ARGS)) {
c796f213
JF
145 trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ?
146 XEN_MC_FL_BATCH : XEN_MC_FL_ARGS);
5ead97c8 147 xen_mc_flush();
400d3494
JF
148 argidx = roundup(b->argidx, sizeof(u64));
149 }
5ead97c8
JF
150
151 ret.mc = &b->entries[b->mcidx];
ffc78767 152#if MC_DEBUG
b93d51dc
IC
153 b->caller[b->mcidx] = __builtin_return_address(0);
154#endif
5ead97c8 155 b->mcidx++;
400d3494
JF
156 ret.args = &b->args[argidx];
157 b->argidx = argidx + args;
158
f124c6ae 159 BUG_ON(b->argidx >= MC_ARGS);
400d3494
JF
160 return ret;
161}
162
163struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
164{
89cbc767 165 struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
400d3494
JF
166 struct multicall_space ret = { NULL, NULL };
167
168 BUG_ON(preemptible());
f124c6ae 169 BUG_ON(b->argidx >= MC_ARGS);
400d3494 170
c796f213
JF
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 }
400d3494 176
c796f213
JF
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 }
400d3494
JF
181
182 ret.mc = &b->entries[b->mcidx - 1];
5ead97c8 183 ret.args = &b->args[b->argidx];
400d3494 184 b->argidx += size;
5ead97c8 185
f124c6ae 186 BUG_ON(b->argidx >= MC_ARGS);
c796f213
JF
187
188 trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK);
189out:
5ead97c8
JF
190 return ret;
191}
91e0c5f3
JF
192
193void xen_mc_callback(void (*fn)(void *), void *data)
194{
89cbc767 195 struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
91e0c5f3
JF
196 struct callback *cb;
197
c796f213
JF
198 if (b->cbidx == MC_BATCH) {
199 trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK);
91e0c5f3 200 xen_mc_flush();
c796f213
JF
201 }
202
203 trace_xen_mc_callback(fn, data);
91e0c5f3
JF
204
205 cb = &b->callbacks[b->cbidx++];
206 cb->fn = fn;
207 cb->data = data;
208}