multicalls.c 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Xen hypercall batching.
 *
 * Xen allows multiple hypercalls to be issued at once, using the
 * multicall interface.  This allows the cost of trapping into the
 * hypervisor to be amortized over several calls.
 *
 * This file implements a simple interface for multicalls.  There's a
 * per-cpu buffer of outstanding multicalls.  When you want to queue a
 * multicall for issuing, you can allocate a multicall slot for the
 * call and its arguments, along with storage for space which is
 * pointed to by the arguments (for passing pointers to structures,
 * etc).  When the multicall is actually issued, all the space for the
 * commands and allocated memory is freed for reuse.
 *
 * Multicalls are flushed whenever any of the buffers get full, or
 * when explicitly requested.  There's no way to get per-multicall
 * return results back.  It will BUG if any of the multicalls fail.
 *
 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
 */
#include <linux/percpu.h>
23
#include <linux/hardirq.h>
24 25 26 27 28

#include <asm/xen/hypercall.h>

#include "multicalls.h"

29 30
#define MC_DEBUG	1

J
Jeremy Fitzhardinge 已提交
31
#define MC_BATCH	32
32
#define MC_ARGS		(MC_BATCH * 16)
33 34 35

struct mc_buffer {
	struct multicall_entry entries[MC_BATCH];
36 37 38
#if MC_DEBUG
	struct multicall_entry debug[MC_BATCH];
#endif
39
	unsigned char args[MC_ARGS];
40 41 42 43 44
	struct callback {
		void (*fn)(void *);
		void *data;
	} callbacks[MC_BATCH];
	unsigned mcidx, argidx, cbidx;
45 46 47 48 49 50 51
};

static DEFINE_PER_CPU(struct mc_buffer, mc_buffer);
DEFINE_PER_CPU(unsigned long, xen_mc_irq_flags);

void xen_mc_flush(void)
{
52
	struct mc_buffer *b = &__get_cpu_var(mc_buffer);
53 54
	int ret = 0;
	unsigned long flags;
55
	int i;
56

57 58
	BUG_ON(preemptible());

59 60 61 62 63
	/* Disable interrupts in case someone comes in and queues
	   something in the middle */
	local_irq_save(flags);

	if (b->mcidx) {
64 65 66 67 68
#if MC_DEBUG
		memcpy(b->debug, b->entries,
		       b->mcidx * sizeof(struct multicall_entry));
#endif

69 70 71 72 73
		if (HYPERVISOR_multicall(b->entries, b->mcidx) != 0)
			BUG();
		for (i = 0; i < b->mcidx; i++)
			if (b->entries[i].result < 0)
				ret++;
74 75 76 77 78

#if MC_DEBUG
		if (ret) {
			printk(KERN_ERR "%d multicall(s) failed: cpu %d\n",
			       ret, smp_processor_id());
79
			for (i = 0; i < b->mcidx; i++) {
80 81 82 83 84 85 86 87 88
				printk("  call %2d/%d: op=%lu arg=[%lx] result=%ld\n",
				       i+1, b->mcidx,
				       b->debug[i].op,
				       b->debug[i].args[0],
				       b->entries[i].result);
			}
		}
#endif

89 90 91 92 93 94 95
		b->mcidx = 0;
		b->argidx = 0;
	} else
		BUG_ON(b->argidx != 0);

	local_irq_restore(flags);

96
	for (i = 0; i < b->cbidx; i++) {
97 98 99 100 101 102
		struct callback *cb = &b->callbacks[i];

		(*cb->fn)(cb->data);
	}
	b->cbidx = 0;

103 104 105 106 107
	BUG_ON(ret);
}

struct multicall_space __xen_mc_entry(size_t args)
{
108
	struct mc_buffer *b = &__get_cpu_var(mc_buffer);
109
	struct multicall_space ret;
110
	unsigned argidx = roundup(b->argidx, sizeof(u64));
111

112
	BUG_ON(preemptible());
113
	BUG_ON(b->argidx > MC_ARGS);
114 115

	if (b->mcidx == MC_BATCH ||
116
	    (argidx + args) > MC_ARGS) {
117
		xen_mc_flush();
118 119
		argidx = roundup(b->argidx, sizeof(u64));
	}
120 121 122

	ret.mc = &b->entries[b->mcidx];
	b->mcidx++;
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	ret.args = &b->args[argidx];
	b->argidx = argidx + args;

	BUG_ON(b->argidx > MC_ARGS);
	return ret;
}

struct multicall_space xen_mc_extend_args(unsigned long op, size_t size)
{
	struct mc_buffer *b = &__get_cpu_var(mc_buffer);
	struct multicall_space ret = { NULL, NULL };

	BUG_ON(preemptible());
	BUG_ON(b->argidx > MC_ARGS);

	if (b->mcidx == 0)
		return ret;

	if (b->entries[b->mcidx - 1].op != op)
		return ret;

	if ((b->argidx + size) > MC_ARGS)
		return ret;

	ret.mc = &b->entries[b->mcidx - 1];
148
	ret.args = &b->args[b->argidx];
149
	b->argidx += size;
150

151
	BUG_ON(b->argidx > MC_ARGS);
152 153
	return ret;
}
154 155 156 157 158 159 160 161 162 163 164 165 166

void xen_mc_callback(void (*fn)(void *), void *data)
{
	struct mc_buffer *b = &__get_cpu_var(mc_buffer);
	struct callback *cb;

	if (b->cbidx == MC_BATCH)
		xen_mc_flush();

	cb = &b->callbacks[b->cbidx++];
	cb->fn = fn;
	cb->data = data;
}