spinlock.c 7.2 KB
Newer Older
1 2 3 4 5 6
/*
 * Split spinlock implementation out into its own file, so it can be
 * compiled in a FTRACE-compatible way.
 */
#include <linux/kernel_stat.h>
#include <linux/spinlock.h>
J
Jeremy Fitzhardinge 已提交
7 8
#include <linux/debugfs.h>
#include <linux/log2.h>
9
#include <linux/gfp.h>
10
#include <linux/slab.h>
11 12 13 14 15 16 17

#include <asm/paravirt.h>

#include <xen/interface/xen.h>
#include <xen/events.h>

#include "xen-ops.h"
J
Jeremy Fitzhardinge 已提交
18 19
#include "debugfs.h"

20 21 22 23 24 25 26 27
enum xen_contention_stat {
	TAKEN_SLOW,
	TAKEN_SLOW_PICKUP,
	TAKEN_SLOW_SPURIOUS,
	RELEASED_SLOW,
	RELEASED_SLOW_KICKED,
	NR_CONTENTION_STATS
};
J
Jeremy Fitzhardinge 已提交
28 29


30
#ifdef CONFIG_XEN_DEBUG_FS
31
#define HISTO_BUCKETS	30
32 33 34
static struct xen_spinlock_stats
{
	u32 contention_stats[NR_CONTENTION_STATS];
35 36
	u32 histo_spin_blocked[HISTO_BUCKETS+1];
	u64 time_blocked;
J
Jeremy Fitzhardinge 已提交
37 38 39 40 41 42
} spinlock_stats;

static u8 zero_stats;

static inline void check_zero(void)
{
43 44 45 46 47 48 49
	u8 ret;
	u8 old = ACCESS_ONCE(zero_stats);
	if (unlikely(old)) {
		ret = cmpxchg(&zero_stats, old, 0);
		/* This ensures only one fellow resets the stat */
		if (ret == old)
			memset(&spinlock_stats, 0, sizeof(spinlock_stats));
J
Jeremy Fitzhardinge 已提交
50 51 52
	}
}

53 54 55 56 57
static inline void add_stats(enum xen_contention_stat var, u32 val)
{
	check_zero();
	spinlock_stats.contention_stats[var] += val;
}
J
Jeremy Fitzhardinge 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

static inline u64 spin_time_start(void)
{
	return xen_clocksource_read();
}

static void __spin_time_accum(u64 delta, u32 *array)
{
	unsigned index = ilog2(delta);

	check_zero();

	if (index < HISTO_BUCKETS)
		array[index]++;
	else
		array[HISTO_BUCKETS]++;
}

76
static inline void spin_time_accum_blocked(u64 start)
J
Jeremy Fitzhardinge 已提交
77 78 79
{
	u32 delta = xen_clocksource_read() - start;

80 81
	__spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
	spinlock_stats.time_blocked += delta;
J
Jeremy Fitzhardinge 已提交
82 83 84
}
#else  /* !CONFIG_XEN_DEBUG_FS */
#define TIMEOUT			(1 << 10)
85 86 87
static inline void add_stats(enum xen_contention_stat var, u32 val)
{
}
J
Jeremy Fitzhardinge 已提交
88 89 90 91 92 93

static inline u64 spin_time_start(void)
{
	return 0;
}

94
static inline void spin_time_accum_blocked(u64 start)
J
Jeremy Fitzhardinge 已提交
95 96 97
{
}
#endif  /* CONFIG_XEN_DEBUG_FS */
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*
 * Size struct xen_spinlock so it's the same as arch_spinlock_t.
 */
#if NR_CPUS < 256
typedef u8 xen_spinners_t;
# define inc_spinners(xl) \
	asm(LOCK_PREFIX " incb %0" : "+m" ((xl)->spinners) : : "memory");
# define dec_spinners(xl) \
	asm(LOCK_PREFIX " decb %0" : "+m" ((xl)->spinners) : : "memory");
#else
typedef u16 xen_spinners_t;
# define inc_spinners(xl) \
	asm(LOCK_PREFIX " incw %0" : "+m" ((xl)->spinners) : : "memory");
# define dec_spinners(xl) \
	asm(LOCK_PREFIX " decw %0" : "+m" ((xl)->spinners) : : "memory");
#endif

116 117 118
struct xen_lock_waiting {
	struct arch_spinlock *lock;
	__ticket_t want;
119 120
};

121
static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
122
static DEFINE_PER_CPU(char *, irq_name);
123 124
static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);
static cpumask_t waiting_cpus;
125

126
static void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
127
{
C
Christoph Lameter 已提交
128
	int irq = __this_cpu_read(lock_kicker_irq);
129 130
	struct xen_lock_waiting *w = &__get_cpu_var(lock_waiting);
	int cpu = smp_processor_id();
131
	u64 start;
132
	unsigned long flags;
133 134 135

	/* If kicker interrupts not initialized yet, just spin */
	if (irq == -1)
136
		return;
137

138 139
	start = spin_time_start();

140 141 142 143 144
	/*
	 * Make sure an interrupt handler can't upset things in a
	 * partially setup state.
	 */
	local_irq_save(flags);
145

146 147 148
	w->want = want;
	smp_wmb();
	w->lock = lock;
149

150 151 152
	/* This uses set_bit, which atomic and therefore a barrier */
	cpumask_set_cpu(cpu, &waiting_cpus);
	add_stats(TAKEN_SLOW, 1);
153

154 155
	/* clear pending */
	xen_clear_irq_pending(irq);
156

157 158
	/* Only check lock once pending cleared */
	barrier();
159

160 161 162 163 164 165 166 167 168
	/* check again make sure it didn't become free while
	   we weren't looking  */
	if (ACCESS_ONCE(lock->tickets.head) == want) {
		add_stats(TAKEN_SLOW_PICKUP, 1);
		goto out;
	}
	/* Block until irq becomes pending (or perhaps a spurious wakeup) */
	xen_poll_irq(irq);
	add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq));
T
Thomas Gleixner 已提交
169
	kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
170
out:
171 172 173
	cpumask_clear_cpu(cpu, &waiting_cpus);
	w->lock = NULL;
	local_irq_restore(flags);
174
	spin_time_accum_blocked(start);
175
}
176
PV_CALLEE_SAVE_REGS_THUNK(xen_lock_spinning);
177

178
static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next)
179 180 181
{
	int cpu;

182 183 184 185
	add_stats(RELEASED_SLOW, 1);

	for_each_cpu(cpu, &waiting_cpus) {
		const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu);
J
Jeremy Fitzhardinge 已提交
186

187 188
		if (w->lock == lock && w->want == next) {
			add_stats(RELEASED_SLOW_KICKED, 1);
189
			xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
190
			break;
191 192 193 194 195 196 197 198 199 200
		}
	}
}

static irqreturn_t dummy_handler(int irq, void *dev_id)
{
	BUG();
	return IRQ_HANDLED;
}

201
void xen_init_lock_cpu(int cpu)
202 203
{
	int irq;
204
	char *name;
205

206
	WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
207 208
	     cpu, per_cpu(lock_kicker_irq, cpu));

209 210 211 212 213 214 215
	/*
	 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
	 * (xen: disable PV spinlocks on HVM)
	 */
	if (xen_hvm_domain())
		return;

216 217 218 219 220 221 222 223 224 225 226
	name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
	irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
				     cpu,
				     dummy_handler,
				     IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
				     name,
				     NULL);

	if (irq >= 0) {
		disable_irq(irq); /* make sure it's never delivered */
		per_cpu(lock_kicker_irq, cpu) = irq;
227
		per_cpu(irq_name, cpu) = name;
228 229 230 231 232
	}

	printk("cpu %d spinlock event irq %d\n", cpu, irq);
}

A
Alex Nixon 已提交
233 234
void xen_uninit_lock_cpu(int cpu)
{
235 236 237 238 239 240 241
	/*
	 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
	 * (xen: disable PV spinlocks on HVM)
	 */
	if (xen_hvm_domain())
		return;

A
Alex Nixon 已提交
242
	unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
243
	per_cpu(lock_kicker_irq, cpu) = -1;
244 245
	kfree(per_cpu(irq_name, cpu));
	per_cpu(irq_name, cpu) = NULL;
A
Alex Nixon 已提交
246 247
}

248 249
static bool xen_pvspin __initdata = true;

250 251
void __init xen_init_spinlocks(void)
{
252 253 254 255 256 257 258
	/*
	 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
	 * (xen: disable PV spinlocks on HVM)
	 */
	if (xen_hvm_domain())
		return;

259 260 261 262 263
	if (!xen_pvspin) {
		printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
		return;
	}

264
	pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(xen_lock_spinning);
265
	pv_lock_ops.unlock_kick = xen_unlock_kick;
266
}
J
Jeremy Fitzhardinge 已提交
267

268 269 270 271 272 273 274
static __init int xen_parse_nopvspin(char *arg)
{
	xen_pvspin = false;
	return 0;
}
early_param("xen_nopvspin", xen_parse_nopvspin);

J
Jeremy Fitzhardinge 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
#ifdef CONFIG_XEN_DEBUG_FS

static struct dentry *d_spin_debug;

static int __init xen_spinlock_debugfs(void)
{
	struct dentry *d_xen = xen_init_debugfs();

	if (d_xen == NULL)
		return -ENOMEM;

	d_spin_debug = debugfs_create_dir("spinlocks", d_xen);

	debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);

	debugfs_create_u32("taken_slow", 0444, d_spin_debug,
291
			   &spinlock_stats.contention_stats[TAKEN_SLOW]);
J
Jeremy Fitzhardinge 已提交
292
	debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
293
			   &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
J
Jeremy Fitzhardinge 已提交
294
	debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
295
			   &spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]);
J
Jeremy Fitzhardinge 已提交
296 297

	debugfs_create_u32("released_slow", 0444, d_spin_debug,
298
			   &spinlock_stats.contention_stats[RELEASED_SLOW]);
J
Jeremy Fitzhardinge 已提交
299
	debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
300
			   &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
J
Jeremy Fitzhardinge 已提交
301

302 303
	debugfs_create_u64("time_blocked", 0444, d_spin_debug,
			   &spinlock_stats.time_blocked);
J
Jeremy Fitzhardinge 已提交
304

305 306
	debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
				spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
J
Jeremy Fitzhardinge 已提交
307 308 309 310 311 312

	return 0;
}
fs_initcall(xen_spinlock_debugfs);

#endif	/* CONFIG_XEN_DEBUG_FS */