]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - arch/x86/xen/multicalls.c
Merge tag 'nfsd-5.3-1' of git://linux-nfs.org/~bfields/linux
[mirror_ubuntu-eoan-kernel.git] / arch / x86 / xen / multicalls.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5ead97c8
JF
2/*
3 * Xen hypercall batching.
4 *
5 * Xen allows multiple hypercalls to be issued at once, using the
6 * multicall interface. This allows the cost of trapping into the
7 * hypervisor to be amortized over several calls.
8 *
9 * This file implements a simple interface for multicalls. There's a
10 * per-cpu buffer of outstanding multicalls. When you want to queue a
11 * multicall for issuing, you can allocate a multicall slot for the
12 * call and its arguments, along with storage for space which is
13 * pointed to by the arguments (for passing pointers to structures,
14 * etc). When the multicall is actually issued, all the space for the
15 * commands and allocated memory is freed for reuse.
16 *
17 * Multicalls are flushed whenever any of the buffers get full, or
18 * when explicitly requested. There's no way to get per-multicall
19 * return results back. It will BUG if any of the multicalls fail.
20 *
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
22 */
23#include <linux/percpu.h>
f120f13e 24#include <linux/hardirq.h>
994025ca 25#include <linux/debugfs.h>
5ead97c8
JF
26
27#include <asm/xen/hypercall.h>
28
29#include "multicalls.h"
994025ca
JF
30#include "debugfs.h"
31
32#define MC_BATCH 32
5ead97c8 33
ffc78767 34#define MC_DEBUG 0
a122d623 35
400d3494 36#define MC_ARGS (MC_BATCH * 16)
5ead97c8 37
994025ca 38
5ead97c8 39struct mc_buffer {
2a6f6d09 40 unsigned mcidx, argidx, cbidx;
5ead97c8 41 struct multicall_entry entries[MC_BATCH];
a122d623
JF
42#if MC_DEBUG
43 struct multicall_entry debug[MC_BATCH];
b93d51dc 44 void *caller[MC_BATCH];
a122d623 45#endif
400d3494 46 unsigned char args[MC_ARGS];
91e0c5f3
JF
47 struct callback {
48 void (*fn)(void *);
49 void *data;
50 } callbacks[MC_BATCH];
5ead97c8
JF
51};
52
53static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
54DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);
55
56void xen_mc_flush(void)
57{
89cbc767 58 struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
eac303bf 59 struct multicall_entry *mc;
5ead97c8
JF
60 int ret = 0;
61 unsigned long flags;
91e0c5f3 62 int i;
5ead97c8 63
f120f13e
JF
64 BUG_ON(preemptible());
65
5ead97c8
JF
66 /* Disable interrupts in case someone comes in and queues
67 something in the middle */
68 local_irq_save(flags);
69
c796f213
JF
70 trace_xen_mc_flush(b->mcidx, b->argidx, b->cbidx);
71
a7b40310
JG
72#if MC_DEBUG
73 memcpy(b->debug, b->entries,
74 b->mcidx * sizeof(struct multicall_entry));
75#endif
76
eac303bf
JF
77 switch (b->mcidx) {
78 case 0:
79 /* no-op */
80 BUG_ON(b->argidx != 0);
81 break;
82
83 case 1:
84 /* Singleton multicall - bypass multicall machinery
85 and just do the call directly. */
86 mc = &b->entries[0];
87
cd913922
JG
88 mc->result = xen_single_call(mc->op, mc->args[0], mc->args[1],
89 mc->args[2], mc->args[3],
90 mc->args[4]);
eac303bf
JF
91 ret = mc->result < 0;
92 break;
93
94 default:
5ead97c8
JF
95 if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
96 BUG();
97 for (i = 0; i < b->mcidx; i++)
98 if (b->entries[i].result < 0)
99 ret++;
a7b40310 100 }
a122d623 101
a7b40310
JG
102 if (WARN_ON(ret)) {
103 pr_err("%d of %d multicall(s) failed: cpu %d\n",
104 ret, b->mcidx, smp_processor_id());
105 for (i = 0; i < b->mcidx; i++) {
106 if (b->entries[i].result < 0) {
a122d623 107#if MC_DEBUG
d75f773c 108 pr_err(" call %2d: op=%lu arg=[%lx] result=%ld\t%pS\n",
a7b40310 109 i + 1,
a122d623
JF
110 b->debug[i].op,
111 b->debug[i].args[0],
b93d51dc
IC
112 b->entries[i].result,
113 b->caller[i]);
a7b40310
JG
114#else
115 pr_err(" call %2d: op=%lu arg=[%lx] result=%ld\n",
116 i + 1,
117 b->entries[i].op,
118 b->entries[i].args[0],
119 b->entries[i].result);
120#endif
a122d623
JF
121 }
122 }
eac303bf 123 }
a122d623 124
eac303bf
JF
125 b->mcidx = 0;
126 b->argidx = 0;
5ead97c8 127
7ebed39f 128 for (i = 0; i < b->cbidx; i++) {
91e0c5f3
JF
129 struct callback *cb = &b->callbacks[i];
130
131 (*cb->fn)(cb->data);
132 }
133 b->cbidx = 0;
134
c9960863 135 local_irq_restore(flags);
5ead97c8
JF
136}
137
138struct multicall_space __xen_mc_entry(size_t args)
139{
89cbc767 140 struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
5ead97c8 141 struct multicall_space ret;
400d3494 142 unsigned argidx = roundup(b->argidx, sizeof(u64));
5ead97c8 143
c796f213
JF
144 trace_xen_mc_entry_alloc(args);
145
f120f13e 146 BUG_ON(preemptible());
f124c6ae 147 BUG_ON(b->argidx >= MC_ARGS);
5ead97c8 148
4a7b005d
JF
149 if (unlikely(b->mcidx == MC_BATCH ||
150 (argidx + args) >= MC_ARGS)) {
c796f213
JF
151 trace_xen_mc_flush_reason((b->mcidx == MC_BATCH) ?
152 XEN_MC_FL_BATCH : XEN_MC_FL_ARGS);
5ead97c8 153 xen_mc_flush();
400d3494
JF
154 argidx = roundup(b->argidx, sizeof(u64));
155 }
5ead97c8
JF
156
157 ret.mc = &b->entries[b->mcidx];
ffc78767 158#if MC_DEBUG
b93d51dc
IC
159 b->caller[b->mcidx] = __builtin_return_address(0);
160#endif
5ead97c8 161 b->mcidx++;
400d3494
JF
162 ret.args = &b->args[argidx];
163 b->argidx = argidx + args;
164
f124c6ae 165 BUG_ON(b->argidx >= MC_ARGS);
400d3494
JF
166 return ret;
167}
168
169struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
170{
89cbc767 171 struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
400d3494
JF
172 struct multicall_space ret = { NULL, NULL };
173
174 BUG_ON(preemptible());
f124c6ae 175 BUG_ON(b->argidx >= MC_ARGS);
400d3494 176
c796f213
JF
177 if (unlikely(b->mcidx == 0 ||
178 b->entries[b->mcidx - 1].op != op)) {
179 trace_xen_mc_extend_args(op, size, XEN_MC_XE_BAD_OP);
180 goto out;
181 }
400d3494 182
c796f213
JF
183 if (unlikely((b->argidx + size) >= MC_ARGS)) {
184 trace_xen_mc_extend_args(op, size, XEN_MC_XE_NO_SPACE);
185 goto out;
186 }
400d3494
JF
187
188 ret.mc = &b->entries[b->mcidx - 1];
5ead97c8 189 ret.args = &b->args[b->argidx];
400d3494 190 b->argidx += size;
5ead97c8 191
f124c6ae 192 BUG_ON(b->argidx >= MC_ARGS);
c796f213
JF
193
194 trace_xen_mc_extend_args(op, size, XEN_MC_XE_OK);
195out:
5ead97c8
JF
196 return ret;
197}
91e0c5f3
JF
198
199void xen_mc_callback(void (*fn)(void *), void *data)
200{
89cbc767 201 struct mc_buffer *b = this_cpu_ptr(&mc_buffer);
91e0c5f3
JF
202 struct callback *cb;
203
c796f213
JF
204 if (b->cbidx == MC_BATCH) {
205 trace_xen_mc_flush_reason(XEN_MC_FL_CALLBACK);
91e0c5f3 206 xen_mc_flush();
c796f213
JF
207 }
208
209 trace_xen_mc_callback(fn, data);
91e0c5f3
JF
210
211 cb = &b->callbacks[b->cbidx++];
212 cb->fn = fn;
213 cb->data = data;
214}