blob: 0d82003e76adab8afe2ef3848f0c4384cd99601e [file] [log] [blame]
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001/*
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 Fitzhardingef120f132007-07-17 18:37:06 -070023#include <linux/hardirq.h>
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070024#include <linux/debugfs.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070025
26#include <asm/xen/hypercall.h>
27
28#include "multicalls.h"
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070029#include "debugfs.h"
30
31#define MC_BATCH 32
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070032
Jeremy Fitzhardingeffc78762010-12-17 17:19:42 -080033#define MC_DEBUG 0
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070034
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -070035#define MC_ARGS (MC_BATCH * 16)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070036
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070037
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070038struct mc_buffer {
Jeremy Fitzhardinge2a6f6d02010-12-17 17:33:11 -080039 unsigned mcidx, argidx, cbidx;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070040 struct multicall_entry entries[MC_BATCH];
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070041#if MC_DEBUG
42 struct multicall_entry debug[MC_BATCH];
Ian Campbellb93d51d2009-02-06 13:35:57 -080043 void *caller[MC_BATCH];
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -070044#endif
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -070045 unsigned char args[MC_ARGS];
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -070046 struct callback {
47 void (*fn)(void *);
48 void *data;
49 } callbacks[MC_BATCH];
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070050};
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{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070057 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
Jeremy Fitzhardingeeac303b2010-12-17 17:32:28 -080058 struct multicall_entry *mc;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070059 int ret = 0;
60 unsigned long flags;
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -070061 int i;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070062
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070063 BUG_ON(preemptible());
64
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070065 /* Disable interrupts in case someone comes in and queues
66 something in the middle */
67 local_irq_save(flags);
68
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -080069 trace_xen_mc_flush(b->mcidx, b->argidx, b->cbidx);
70
Jeremy Fitzhardingeeac303b2010-12-17 17:32:28 -080071 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 Fitzhardingea122d622007-10-16 11:51:31 -070089#if MC_DEBUG
90 memcpy(b->debug, b->entries,
91 b->mcidx * sizeof(struct multicall_entry));
92#endif
93
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070094 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 Fitzhardingea122d622007-10-16 11:51:31 -070099
100#if MC_DEBUG
101 if (ret) {
102 printk(KERN_ERR "%d multicall(s) failed: cpu %d\n",
103 ret, smp_processor_id());
Jeremy Fitzhardinge8ba6c2b2008-07-08 15:06:30 -0700104 dump_stack();
Paolo Ciarrocchi7ebed392008-02-22 23:11:59 +0100105 for (i = 0; i < b->mcidx; i++) {
Ian Campbellb93d51d2009-02-06 13:35:57 -0800106 printk(KERN_DEBUG " call %2d/%d: op=%lu arg=[%lx] result=%ld\t%pF\n",
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -0700107 i+1, b->mcidx,
108 b->debug[i].op,
109 b->debug[i].args[0],
Ian Campbellb93d51d2009-02-06 13:35:57 -0800110 b->entries[i].result,
111 b->caller[i]);
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -0700112 }
113 }
114#endif
Jeremy Fitzhardingeeac303b2010-12-17 17:32:28 -0800115 }
Jeremy Fitzhardingea122d622007-10-16 11:51:31 -0700116
Jeremy Fitzhardingeeac303b2010-12-17 17:32:28 -0800117 b->mcidx = 0;
118 b->argidx = 0;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700119
Paolo Ciarrocchi7ebed392008-02-22 23:11:59 +0100120 for (i = 0; i < b->cbidx; i++) {
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -0700121 struct callback *cb = &b->callbacks[i];
122
123 (*cb->fn)(cb->data);
124 }
125 b->cbidx = 0;
126
Jeremy Fitzhardingec9960862009-02-06 13:38:56 -0800127 local_irq_restore(flags);
128
Jeremy Fitzhardinge3d39e9d2009-02-06 13:38:51 -0800129 WARN_ON(ret);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700130}
131
132struct multicall_space __xen_mc_entry(size_t args)
133{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700134 struct mc_buffer *b = &__get_cpu_var(mc_buffer);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700135 struct multicall_space ret;
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700136 unsigned argidx = roundup(b->argidx, sizeof(u64));
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700137
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800138 trace_xen_mc_entry_alloc(args);
139
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700140 BUG_ON(preemptible());
Dan Carpenterf124c6a2011-06-03 07:45:28 +0300141 BUG_ON(b->argidx >= MC_ARGS);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700142
Jeremy Fitzhardinge4a7b0052010-12-17 17:20:25 -0800143 if (unlikely(b->mcidx == MC_BATCH ||
144 (argidx + args) >= MC_ARGS)) {
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800145 trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ?
146 XEN_MC_FL_BATCH : XEN_MC_FL_ARGS);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700147 xen_mc_flush();
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700148 argidx = roundup(b->argidx, sizeof(u64));
149 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700150
151 ret.mc = &b->entries[b->mcidx];
Jeremy Fitzhardingeffc78762010-12-17 17:19:42 -0800152#if MC_DEBUG
Ian Campbellb93d51d2009-02-06 13:35:57 -0800153 b->caller[b->mcidx] = __builtin_return_address(0);
154#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700155 b->mcidx++;
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700156 ret.args = &b->args[argidx];
157 b->argidx = argidx + args;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700158
Dan Carpenterf124c6a2011-06-03 07:45:28 +0300159 BUG_ON(b->argidx >= MC_ARGS);
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700160 return ret;
161}
162
163struct 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 Carpenterf124c6a2011-06-03 07:45:28 +0300169 BUG_ON(b->argidx >= MC_ARGS);
Jeremy Fitzhardinge400d3492008-06-16 04:30:03 -0700170
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800171 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 Fitzhardinge400d3492008-06-16 04:30:03 -0700176
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800177 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 Fitzhardinge400d3492008-06-16 04:30:03 -0700181
182 ret.mc = &b->entries[b->mcidx - 1];
183 ret.args = &b->args[b->argidx];
184 b->argidx += size;
185
Dan Carpenterf124c6a2011-06-03 07:45:28 +0300186 BUG_ON(b->argidx >= MC_ARGS);
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800187
188 trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK);
189out:
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700190 return ret;
191}
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -0700192
193void 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 Fitzhardingec796f212010-12-16 14:33:27 -0800198 if (b->cbidx == MC_BATCH) {
199 trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK);
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -0700200 xen_mc_flush();
Jeremy Fitzhardingec796f212010-12-16 14:33:27 -0800201 }
202
203 trace_xen_mc_callback(fn, data);
Jeremy Fitzhardinge91e0c5f2007-10-16 11:51:30 -0700204
205 cb = &b->callbacks[b->cbidx++];
206 cb->fn = fn;
207 cb->data = data;
208}